Make the linespec/location completer ignore data symbols
[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
34eaf542
TT
86typedef struct symbol *symbolp;
87DEF_VEC_P (symbolp);
88
73be47f5
DE
89/* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
b4f54984
DE
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92static unsigned int dwarf_read_debug = 0;
45cfd468 93
d97bc12b 94/* When non-zero, dump DIEs after they are read in. */
b4f54984 95static unsigned int dwarf_die_debug = 0;
d97bc12b 96
27e0867f
DE
97/* When non-zero, dump line number entries as they are read in. */
98static unsigned int dwarf_line_debug = 0;
99
900e11f9
JK
100/* When non-zero, cross-check physname against demangler. */
101static int check_physname = 0;
102
481860b3 103/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 104static int use_deprecated_index_sections = 0;
481860b3 105
6502dd73
DJ
106static const struct objfile_data *dwarf2_objfile_data_key;
107
f1e6e072
TT
108/* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110static int dwarf2_locexpr_index;
111static int dwarf2_loclist_index;
112static int dwarf2_locexpr_block_index;
113static int dwarf2_loclist_block_index;
114
73869dc2
DE
115/* A descriptor for dwarf sections.
116
117 S.ASECTION, SIZE are typically initialized when the objfile is first
118 scanned. BUFFER, READIN are filled in later when the section is read.
119 If the section contained compressed data then SIZE is updated to record
120 the uncompressed size of the section.
121
122 DWP file format V2 introduces a wrinkle that is easiest to handle by
123 creating the concept of virtual sections contained within a real section.
124 In DWP V2 the sections of the input DWO files are concatenated together
125 into one section, but section offsets are kept relative to the original
126 input section.
127 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
128 the real section this "virtual" section is contained in, and BUFFER,SIZE
129 describe the virtual section. */
130
dce234bc
PP
131struct dwarf2_section_info
132{
73869dc2
DE
133 union
134 {
e5aa3347 135 /* If this is a real section, the bfd section. */
049412e3 136 asection *section;
73869dc2 137 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 138 section. */
73869dc2
DE
139 struct dwarf2_section_info *containing_section;
140 } s;
19ac8c2e 141 /* Pointer to section data, only valid if readin. */
d521ce57 142 const gdb_byte *buffer;
73869dc2 143 /* The size of the section, real or virtual. */
dce234bc 144 bfd_size_type size;
73869dc2
DE
145 /* If this is a virtual section, the offset in the real section.
146 Only valid if is_virtual. */
147 bfd_size_type virtual_offset;
be391dca 148 /* True if we have tried to read this section. */
73869dc2
DE
149 char readin;
150 /* True if this is a virtual section, False otherwise.
049412e3 151 This specifies which of s.section and s.containing_section to use. */
73869dc2 152 char is_virtual;
dce234bc
PP
153};
154
8b70b953
TT
155typedef struct dwarf2_section_info dwarf2_section_info_def;
156DEF_VEC_O (dwarf2_section_info_def);
157
9291a0cd
TT
158/* All offsets in the index are of this type. It must be
159 architecture-independent. */
160typedef uint32_t offset_type;
161
162DEF_VEC_I (offset_type);
163
156942c7
DE
164/* Ensure only legit values are used. */
165#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
166 do { \
167 gdb_assert ((unsigned int) (value) <= 1); \
168 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
169 } while (0)
170
171/* Ensure only legit values are used. */
172#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
173 do { \
174 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
175 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
176 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
177 } while (0)
178
179/* Ensure we don't use more than the alloted nuber of bits for the CU. */
180#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
181 do { \
182 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
183 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
184 } while (0)
185
3f563c84
PA
186#if WORDS_BIGENDIAN
187
188/* Convert VALUE between big- and little-endian. */
189
190static offset_type
191byte_swap (offset_type value)
192{
193 offset_type result;
194
195 result = (value & 0xff) << 24;
196 result |= (value & 0xff00) << 8;
197 result |= (value & 0xff0000) >> 8;
198 result |= (value & 0xff000000) >> 24;
199 return result;
200}
201
202#define MAYBE_SWAP(V) byte_swap (V)
203
204#else
205#define MAYBE_SWAP(V) static_cast<offset_type> (V)
206#endif /* WORDS_BIGENDIAN */
207
208/* An index into a (C++) symbol name component in a symbol name as
209 recorded in the mapped_index's symbol table. For each C++ symbol
210 in the symbol table, we record one entry for the start of each
211 component in the symbol in a table of name components, and then
212 sort the table, in order to be able to binary search symbol names,
213 ignoring leading namespaces, both completion and regular look up.
214 For example, for symbol "A::B::C", we'll have an entry that points
215 to "A::B::C", another that points to "B::C", and another for "C".
216 Note that function symbols in GDB index have no parameter
217 information, just the function/method names. You can convert a
218 name_component to a "const char *" using the
219 'mapped_index::symbol_name_at(offset_type)' method. */
220
221struct name_component
222{
223 /* Offset in the symbol name where the component starts. Stored as
224 a (32-bit) offset instead of a pointer to save memory and improve
225 locality on 64-bit architectures. */
226 offset_type name_offset;
227
228 /* The symbol's index in the symbol and constant pool tables of a
229 mapped_index. */
230 offset_type idx;
231};
232
9291a0cd
TT
233/* A description of the mapped index. The file format is described in
234 a comment by the code that writes the index. */
235struct mapped_index
236{
559a7a62
JK
237 /* Index data format version. */
238 int version;
239
9291a0cd
TT
240 /* The total length of the buffer. */
241 off_t total_size;
b11b1f88 242
9291a0cd
TT
243 /* A pointer to the address table data. */
244 const gdb_byte *address_table;
b11b1f88 245
9291a0cd
TT
246 /* Size of the address table data in bytes. */
247 offset_type address_table_size;
b11b1f88 248
3876f04e
DE
249 /* The symbol table, implemented as a hash table. */
250 const offset_type *symbol_table;
b11b1f88 251
9291a0cd 252 /* Size in slots, each slot is 2 offset_types. */
3876f04e 253 offset_type symbol_table_slots;
b11b1f88 254
9291a0cd
TT
255 /* A pointer to the constant pool. */
256 const char *constant_pool;
3f563c84
PA
257
258 /* The name_component table (a sorted vector). See name_component's
259 description above. */
260 std::vector<name_component> name_components;
261
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
264 const char *symbol_name_at (offset_type idx) const
265 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
9291a0cd
TT
266};
267
95554aad
TT
268typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
269DEF_VEC_P (dwarf2_per_cu_ptr);
270
52059ffd
TT
271struct tu_stats
272{
273 int nr_uniq_abbrev_tables;
274 int nr_symtabs;
275 int nr_symtab_sharers;
276 int nr_stmt_less_type_units;
277 int nr_all_type_units_reallocs;
278};
279
9cdd5dbd
DE
280/* Collection of data recorded per objfile.
281 This hangs off of dwarf2_objfile_data_key. */
282
6502dd73
DJ
283struct dwarf2_per_objfile
284{
330cdd98
PA
285 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
286 dwarf2 section names, or is NULL if the standard ELF names are
287 used. */
288 dwarf2_per_objfile (struct objfile *objfile,
289 const dwarf2_debug_sections *names);
ae038cb0 290
330cdd98
PA
291 ~dwarf2_per_objfile ();
292
d6541620 293 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
294
295 /* Free all cached compilation units. */
296 void free_cached_comp_units ();
297private:
298 /* This function is mapped across the sections and remembers the
299 offset and size of each of the debugging sections we are
300 interested in. */
301 void locate_sections (bfd *abfd, asection *sectp,
302 const dwarf2_debug_sections &names);
303
304public:
305 dwarf2_section_info info {};
306 dwarf2_section_info abbrev {};
307 dwarf2_section_info line {};
308 dwarf2_section_info loc {};
309 dwarf2_section_info loclists {};
310 dwarf2_section_info macinfo {};
311 dwarf2_section_info macro {};
312 dwarf2_section_info str {};
313 dwarf2_section_info line_str {};
314 dwarf2_section_info ranges {};
315 dwarf2_section_info rnglists {};
316 dwarf2_section_info addr {};
317 dwarf2_section_info frame {};
318 dwarf2_section_info eh_frame {};
319 dwarf2_section_info gdb_index {};
320
321 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 322
be391dca 323 /* Back link. */
330cdd98 324 struct objfile *objfile = NULL;
be391dca 325
d467dd73 326 /* Table of all the compilation units. This is used to locate
10b3939b 327 the target compilation unit of a particular reference. */
330cdd98 328 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
329
330 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 331 int n_comp_units = 0;
ae038cb0 332
1fd400ff 333 /* The number of .debug_types-related CUs. */
330cdd98 334 int n_type_units = 0;
1fd400ff 335
6aa5f3a6
DE
336 /* The number of elements allocated in all_type_units.
337 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 338 int n_allocated_type_units = 0;
6aa5f3a6 339
a2ce51a0
DE
340 /* The .debug_types-related CUs (TUs).
341 This is stored in malloc space because we may realloc it. */
330cdd98 342 struct signatured_type **all_type_units = NULL;
1fd400ff 343
f4dc4d17
DE
344 /* Table of struct type_unit_group objects.
345 The hash key is the DW_AT_stmt_list value. */
330cdd98 346 htab_t type_unit_groups {};
72dca2f5 347
348e048f
DE
348 /* A table mapping .debug_types signatures to its signatured_type entry.
349 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 350 htab_t signatured_types {};
348e048f 351
f4dc4d17
DE
352 /* Type unit statistics, to see how well the scaling improvements
353 are doing. */
330cdd98 354 struct tu_stats tu_stats {};
f4dc4d17
DE
355
356 /* A chain of compilation units that are currently read in, so that
357 they can be freed later. */
330cdd98 358 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 359
3019eac3
DE
360 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
361 This is NULL if the table hasn't been allocated yet. */
330cdd98 362 htab_t dwo_files {};
3019eac3 363
330cdd98
PA
364 /* True if we've checked for whether there is a DWP file. */
365 bool dwp_checked = false;
80626a55
DE
366
367 /* The DWP file if there is one, or NULL. */
330cdd98 368 struct dwp_file *dwp_file = NULL;
80626a55 369
36586728
TT
370 /* The shared '.dwz' file, if one exists. This is used when the
371 original data was compressed using 'dwz -m'. */
330cdd98 372 struct dwz_file *dwz_file = NULL;
36586728 373
330cdd98 374 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 375 VMA of 0. */
330cdd98 376 bool has_section_at_zero = false;
9291a0cd 377
ae2de4f8
DE
378 /* True if we are using the mapped index,
379 or we are faking it for OBJF_READNOW's sake. */
330cdd98 380 bool using_index = false;
9291a0cd 381
ae2de4f8 382 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 383 mapped_index *index_table = NULL;
98bfdba5 384
7b9f3c50 385 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
386 TUs typically share line table entries with a CU, so we maintain a
387 separate table of all line table entries to support the sharing.
388 Note that while there can be way more TUs than CUs, we've already
389 sorted all the TUs into "type unit groups", grouped by their
390 DW_AT_stmt_list value. Therefore the only sharing done here is with a
391 CU and its associated TU group if there is one. */
330cdd98 392 htab_t quick_file_names_table {};
7b9f3c50 393
98bfdba5
PA
394 /* Set during partial symbol reading, to prevent queueing of full
395 symbols. */
330cdd98 396 bool reading_partial_symbols = false;
673bfd45 397
dee91e82 398 /* Table mapping type DIEs to their struct type *.
673bfd45 399 This is NULL if not allocated yet.
02142a6c 400 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 401 htab_t die_type_hash {};
95554aad
TT
402
403 /* The CUs we recently read. */
330cdd98 404 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
405
406 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 407 htab_t line_header_hash {};
bbf2f4df
PA
408
409 /* Table containing all filenames. This is an optional because the
410 table is lazily constructed on first access. */
411 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
412};
413
414static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 415
251d32d9 416/* Default names of the debugging sections. */
c906108c 417
233a11ab
CS
418/* Note that if the debugging section has been compressed, it might
419 have a name like .zdebug_info. */
420
9cdd5dbd
DE
421static const struct dwarf2_debug_sections dwarf2_elf_names =
422{
251d32d9
TG
423 { ".debug_info", ".zdebug_info" },
424 { ".debug_abbrev", ".zdebug_abbrev" },
425 { ".debug_line", ".zdebug_line" },
426 { ".debug_loc", ".zdebug_loc" },
43988095 427 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 428 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 429 { ".debug_macro", ".zdebug_macro" },
251d32d9 430 { ".debug_str", ".zdebug_str" },
43988095 431 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 432 { ".debug_ranges", ".zdebug_ranges" },
43988095 433 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 434 { ".debug_types", ".zdebug_types" },
3019eac3 435 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
436 { ".debug_frame", ".zdebug_frame" },
437 { ".eh_frame", NULL },
24d3216f
TT
438 { ".gdb_index", ".zgdb_index" },
439 23
251d32d9 440};
c906108c 441
80626a55 442/* List of DWO/DWP sections. */
3019eac3 443
80626a55 444static const struct dwop_section_names
3019eac3
DE
445{
446 struct dwarf2_section_names abbrev_dwo;
447 struct dwarf2_section_names info_dwo;
448 struct dwarf2_section_names line_dwo;
449 struct dwarf2_section_names loc_dwo;
43988095 450 struct dwarf2_section_names loclists_dwo;
09262596
DE
451 struct dwarf2_section_names macinfo_dwo;
452 struct dwarf2_section_names macro_dwo;
3019eac3
DE
453 struct dwarf2_section_names str_dwo;
454 struct dwarf2_section_names str_offsets_dwo;
455 struct dwarf2_section_names types_dwo;
80626a55
DE
456 struct dwarf2_section_names cu_index;
457 struct dwarf2_section_names tu_index;
3019eac3 458}
80626a55 459dwop_section_names =
3019eac3
DE
460{
461 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
462 { ".debug_info.dwo", ".zdebug_info.dwo" },
463 { ".debug_line.dwo", ".zdebug_line.dwo" },
464 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 465 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
466 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
467 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
468 { ".debug_str.dwo", ".zdebug_str.dwo" },
469 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
470 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
471 { ".debug_cu_index", ".zdebug_cu_index" },
472 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
473};
474
c906108c
SS
475/* local data types */
476
107d2387
AC
477/* The data in a compilation unit header, after target2host
478 translation, looks like this. */
c906108c 479struct comp_unit_head
a738430d 480{
c764a876 481 unsigned int length;
a738430d 482 short version;
a738430d
MK
483 unsigned char addr_size;
484 unsigned char signed_addr_p;
9c541725 485 sect_offset abbrev_sect_off;
57349743 486
a738430d
MK
487 /* Size of file offsets; either 4 or 8. */
488 unsigned int offset_size;
57349743 489
a738430d
MK
490 /* Size of the length field; either 4 or 12. */
491 unsigned int initial_length_size;
57349743 492
43988095
JK
493 enum dwarf_unit_type unit_type;
494
a738430d
MK
495 /* Offset to the first byte of this compilation unit header in the
496 .debug_info section, for resolving relative reference dies. */
9c541725 497 sect_offset sect_off;
57349743 498
d00adf39
DE
499 /* Offset to first die in this cu from the start of the cu.
500 This will be the first byte following the compilation unit header. */
9c541725 501 cu_offset first_die_cu_offset;
43988095
JK
502
503 /* 64-bit signature of this type unit - it is valid only for
504 UNIT_TYPE DW_UT_type. */
505 ULONGEST signature;
506
507 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 508 cu_offset type_cu_offset_in_tu;
a738430d 509};
c906108c 510
3da10d80
KS
511/* Type used for delaying computation of method physnames.
512 See comments for compute_delayed_physnames. */
513struct delayed_method_info
514{
515 /* The type to which the method is attached, i.e., its parent class. */
516 struct type *type;
517
518 /* The index of the method in the type's function fieldlists. */
519 int fnfield_index;
520
521 /* The index of the method in the fieldlist. */
522 int index;
523
524 /* The name of the DIE. */
525 const char *name;
526
527 /* The DIE associated with this method. */
528 struct die_info *die;
529};
530
531typedef struct delayed_method_info delayed_method_info;
532DEF_VEC_O (delayed_method_info);
533
e7c27a73
DJ
534/* Internal state when decoding a particular compilation unit. */
535struct dwarf2_cu
536{
537 /* The objfile containing this compilation unit. */
538 struct objfile *objfile;
539
d00adf39 540 /* The header of the compilation unit. */
e7c27a73 541 struct comp_unit_head header;
e142c38c 542
d00adf39
DE
543 /* Base address of this compilation unit. */
544 CORE_ADDR base_address;
545
546 /* Non-zero if base_address has been set. */
547 int base_known;
548
e142c38c
DJ
549 /* The language we are debugging. */
550 enum language language;
551 const struct language_defn *language_defn;
552
b0f35d58
DL
553 const char *producer;
554
e142c38c
DJ
555 /* The generic symbol table building routines have separate lists for
556 file scope symbols and all all other scopes (local scopes). So
557 we need to select the right one to pass to add_symbol_to_list().
558 We do it by keeping a pointer to the correct list in list_in_scope.
559
560 FIXME: The original dwarf code just treated the file scope as the
561 first local scope, and all other local scopes as nested local
562 scopes, and worked fine. Check to see if we really need to
563 distinguish these in buildsym.c. */
564 struct pending **list_in_scope;
565
433df2d4
DE
566 /* The abbrev table for this CU.
567 Normally this points to the abbrev table in the objfile.
568 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
569 struct abbrev_table *abbrev_table;
72bf9492 570
b64f50a1
JK
571 /* Hash table holding all the loaded partial DIEs
572 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
573 htab_t partial_dies;
574
575 /* Storage for things with the same lifetime as this read-in compilation
576 unit, including partial DIEs. */
577 struct obstack comp_unit_obstack;
578
ae038cb0
DJ
579 /* When multiple dwarf2_cu structures are living in memory, this field
580 chains them all together, so that they can be released efficiently.
581 We will probably also want a generation counter so that most-recently-used
582 compilation units are cached... */
583 struct dwarf2_per_cu_data *read_in_chain;
584
69d751e3 585 /* Backlink to our per_cu entry. */
ae038cb0
DJ
586 struct dwarf2_per_cu_data *per_cu;
587
588 /* How many compilation units ago was this CU last referenced? */
589 int last_used;
590
b64f50a1
JK
591 /* A hash table of DIE cu_offset for following references with
592 die_info->offset.sect_off as hash. */
51545339 593 htab_t die_hash;
10b3939b
DJ
594
595 /* Full DIEs if read in. */
596 struct die_info *dies;
597
598 /* A set of pointers to dwarf2_per_cu_data objects for compilation
599 units referenced by this one. Only set during full symbol processing;
600 partial symbol tables do not have dependencies. */
601 htab_t dependencies;
602
cb1df416
DJ
603 /* Header data from the line table, during full symbol processing. */
604 struct line_header *line_header;
4c8aa72d
PA
605 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
606 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
607 this is the DW_TAG_compile_unit die for this CU. We'll hold on
608 to the line header as long as this DIE is being processed. See
609 process_die_scope. */
610 die_info *line_header_die_owner;
cb1df416 611
3da10d80
KS
612 /* A list of methods which need to have physnames computed
613 after all type information has been read. */
614 VEC (delayed_method_info) *method_list;
615
96408a79
SA
616 /* To be copied to symtab->call_site_htab. */
617 htab_t call_site_htab;
618
034e5797
DE
619 /* Non-NULL if this CU came from a DWO file.
620 There is an invariant here that is important to remember:
621 Except for attributes copied from the top level DIE in the "main"
622 (or "stub") file in preparation for reading the DWO file
623 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
624 Either there isn't a DWO file (in which case this is NULL and the point
625 is moot), or there is and either we're not going to read it (in which
626 case this is NULL) or there is and we are reading it (in which case this
627 is non-NULL). */
3019eac3
DE
628 struct dwo_unit *dwo_unit;
629
630 /* The DW_AT_addr_base attribute if present, zero otherwise
631 (zero is a valid value though).
1dbab08b 632 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
633 ULONGEST addr_base;
634
2e3cf129
DE
635 /* The DW_AT_ranges_base attribute if present, zero otherwise
636 (zero is a valid value though).
1dbab08b 637 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 638 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
639 be used without needing to know whether DWO files are in use or not.
640 N.B. This does not apply to DW_AT_ranges appearing in
641 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
642 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
643 DW_AT_ranges_base *would* have to be applied, and we'd have to care
644 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
645 ULONGEST ranges_base;
646
ae038cb0
DJ
647 /* Mark used when releasing cached dies. */
648 unsigned int mark : 1;
649
8be455d7
JK
650 /* This CU references .debug_loc. See the symtab->locations_valid field.
651 This test is imperfect as there may exist optimized debug code not using
652 any location list and still facing inlining issues if handled as
653 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 654 unsigned int has_loclist : 1;
ba919b58 655
1b80a9fa
JK
656 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
657 if all the producer_is_* fields are valid. This information is cached
658 because profiling CU expansion showed excessive time spent in
659 producer_is_gxx_lt_4_6. */
ba919b58
TT
660 unsigned int checked_producer : 1;
661 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 662 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 663 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
664
665 /* When set, the file that we're processing is known to have
666 debugging info for C++ namespaces. GCC 3.3.x did not produce
667 this information, but later versions do. */
668
669 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
670};
671
10b3939b
DJ
672/* Persistent data held for a compilation unit, even when not
673 processing it. We put a pointer to this structure in the
28dee7f5 674 read_symtab_private field of the psymtab. */
10b3939b 675
ae038cb0
DJ
676struct dwarf2_per_cu_data
677{
36586728 678 /* The start offset and length of this compilation unit.
45452591 679 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
680 initial_length_size.
681 If the DIE refers to a DWO file, this is always of the original die,
682 not the DWO file. */
9c541725 683 sect_offset sect_off;
36586728 684 unsigned int length;
ae038cb0 685
43988095
JK
686 /* DWARF standard version this data has been read from (such as 4 or 5). */
687 short dwarf_version;
688
ae038cb0
DJ
689 /* Flag indicating this compilation unit will be read in before
690 any of the current compilation units are processed. */
c764a876 691 unsigned int queued : 1;
ae038cb0 692
0d99eb77
DE
693 /* This flag will be set when reading partial DIEs if we need to load
694 absolutely all DIEs for this compilation unit, instead of just the ones
695 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
696 hash table and don't find it. */
697 unsigned int load_all_dies : 1;
698
0186c6a7
DE
699 /* Non-zero if this CU is from .debug_types.
700 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
701 this is non-zero. */
3019eac3
DE
702 unsigned int is_debug_types : 1;
703
36586728
TT
704 /* Non-zero if this CU is from the .dwz file. */
705 unsigned int is_dwz : 1;
706
a2ce51a0
DE
707 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
708 This flag is only valid if is_debug_types is true.
709 We can't read a CU directly from a DWO file: There are required
710 attributes in the stub. */
711 unsigned int reading_dwo_directly : 1;
712
7ee85ab1
DE
713 /* Non-zero if the TU has been read.
714 This is used to assist the "Stay in DWO Optimization" for Fission:
715 When reading a DWO, it's faster to read TUs from the DWO instead of
716 fetching them from random other DWOs (due to comdat folding).
717 If the TU has already been read, the optimization is unnecessary
718 (and unwise - we don't want to change where gdb thinks the TU lives
719 "midflight").
720 This flag is only valid if is_debug_types is true. */
721 unsigned int tu_read : 1;
722
3019eac3
DE
723 /* The section this CU/TU lives in.
724 If the DIE refers to a DWO file, this is always the original die,
725 not the DWO file. */
8a0459fd 726 struct dwarf2_section_info *section;
348e048f 727
17ea53c3 728 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
729 of the CU cache it gets reset to NULL again. This is left as NULL for
730 dummy CUs (a CU header, but nothing else). */
ae038cb0 731 struct dwarf2_cu *cu;
1c379e20 732
9cdd5dbd
DE
733 /* The corresponding objfile.
734 Normally we can get the objfile from dwarf2_per_objfile.
735 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
736 struct objfile *objfile;
737
fffbe6a8
YQ
738 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
739 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
740 union
741 {
742 /* The partial symbol table associated with this compilation unit,
95554aad 743 or NULL for unread partial units. */
9291a0cd
TT
744 struct partial_symtab *psymtab;
745
746 /* Data needed by the "quick" functions. */
747 struct dwarf2_per_cu_quick_data *quick;
748 } v;
95554aad 749
796a7ff8
DE
750 /* The CUs we import using DW_TAG_imported_unit. This is filled in
751 while reading psymtabs, used to compute the psymtab dependencies,
752 and then cleared. Then it is filled in again while reading full
753 symbols, and only deleted when the objfile is destroyed.
754
755 This is also used to work around a difference between the way gold
756 generates .gdb_index version <=7 and the way gdb does. Arguably this
757 is a gold bug. For symbols coming from TUs, gold records in the index
758 the CU that includes the TU instead of the TU itself. This breaks
759 dw2_lookup_symbol: It assumes that if the index says symbol X lives
760 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
761 will find X. Alas TUs live in their own symtab, so after expanding CU Y
762 we need to look in TU Z to find X. Fortunately, this is akin to
763 DW_TAG_imported_unit, so we just use the same mechanism: For
764 .gdb_index version <=7 this also records the TUs that the CU referred
765 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
766 indices so we only pay a price for gold generated indices.
767 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 768 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
769};
770
348e048f
DE
771/* Entry in the signatured_types hash table. */
772
773struct signatured_type
774{
42e7ad6c 775 /* The "per_cu" object of this type.
ac9ec31b 776 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
777 N.B.: This is the first member so that it's easy to convert pointers
778 between them. */
779 struct dwarf2_per_cu_data per_cu;
780
3019eac3 781 /* The type's signature. */
348e048f
DE
782 ULONGEST signature;
783
3019eac3 784 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
785 If this TU is a DWO stub and the definition lives in a DWO file
786 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
787 cu_offset type_offset_in_tu;
788
789 /* Offset in the section of the type's DIE.
790 If the definition lives in a DWO file, this is the offset in the
791 .debug_types.dwo section.
792 The value is zero until the actual value is known.
793 Zero is otherwise not a valid section offset. */
794 sect_offset type_offset_in_section;
0186c6a7
DE
795
796 /* Type units are grouped by their DW_AT_stmt_list entry so that they
797 can share them. This points to the containing symtab. */
798 struct type_unit_group *type_unit_group;
ac9ec31b
DE
799
800 /* The type.
801 The first time we encounter this type we fully read it in and install it
802 in the symbol tables. Subsequent times we only need the type. */
803 struct type *type;
a2ce51a0
DE
804
805 /* Containing DWO unit.
806 This field is valid iff per_cu.reading_dwo_directly. */
807 struct dwo_unit *dwo_unit;
348e048f
DE
808};
809
0186c6a7
DE
810typedef struct signatured_type *sig_type_ptr;
811DEF_VEC_P (sig_type_ptr);
812
094b34ac
DE
813/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
814 This includes type_unit_group and quick_file_names. */
815
816struct stmt_list_hash
817{
818 /* The DWO unit this table is from or NULL if there is none. */
819 struct dwo_unit *dwo_unit;
820
821 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 822 sect_offset line_sect_off;
094b34ac
DE
823};
824
f4dc4d17
DE
825/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
826 an object of this type. */
827
828struct type_unit_group
829{
0186c6a7 830 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
831 To simplify things we create an artificial CU that "includes" all the
832 type units using this stmt_list so that the rest of the code still has
833 a "per_cu" handle on the symtab.
834 This PER_CU is recognized by having no section. */
8a0459fd 835#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
836 struct dwarf2_per_cu_data per_cu;
837
0186c6a7
DE
838 /* The TUs that share this DW_AT_stmt_list entry.
839 This is added to while parsing type units to build partial symtabs,
840 and is deleted afterwards and not used again. */
841 VEC (sig_type_ptr) *tus;
f4dc4d17 842
43f3e411 843 /* The compunit symtab.
094b34ac 844 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
845 so we create an essentially anonymous symtab as the compunit symtab. */
846 struct compunit_symtab *compunit_symtab;
f4dc4d17 847
094b34ac
DE
848 /* The data used to construct the hash key. */
849 struct stmt_list_hash hash;
f4dc4d17
DE
850
851 /* The number of symtabs from the line header.
852 The value here must match line_header.num_file_names. */
853 unsigned int num_symtabs;
854
855 /* The symbol tables for this TU (obtained from the files listed in
856 DW_AT_stmt_list).
857 WARNING: The order of entries here must match the order of entries
858 in the line header. After the first TU using this type_unit_group, the
859 line header for the subsequent TUs is recreated from this. This is done
860 because we need to use the same symtabs for each TU using the same
861 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
862 there's no guarantee the line header doesn't have duplicate entries. */
863 struct symtab **symtabs;
864};
865
73869dc2 866/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
867
868struct dwo_sections
869{
870 struct dwarf2_section_info abbrev;
3019eac3
DE
871 struct dwarf2_section_info line;
872 struct dwarf2_section_info loc;
43988095 873 struct dwarf2_section_info loclists;
09262596
DE
874 struct dwarf2_section_info macinfo;
875 struct dwarf2_section_info macro;
3019eac3
DE
876 struct dwarf2_section_info str;
877 struct dwarf2_section_info str_offsets;
80626a55
DE
878 /* In the case of a virtual DWO file, these two are unused. */
879 struct dwarf2_section_info info;
3019eac3
DE
880 VEC (dwarf2_section_info_def) *types;
881};
882
c88ee1f0 883/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
884
885struct dwo_unit
886{
887 /* Backlink to the containing struct dwo_file. */
888 struct dwo_file *dwo_file;
889
890 /* The "id" that distinguishes this CU/TU.
891 .debug_info calls this "dwo_id", .debug_types calls this "signature".
892 Since signatures came first, we stick with it for consistency. */
893 ULONGEST signature;
894
895 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 896 struct dwarf2_section_info *section;
3019eac3 897
9c541725
PA
898 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
899 sect_offset sect_off;
3019eac3
DE
900 unsigned int length;
901
902 /* For types, offset in the type's DIE of the type defined by this TU. */
903 cu_offset type_offset_in_tu;
904};
905
73869dc2
DE
906/* include/dwarf2.h defines the DWP section codes.
907 It defines a max value but it doesn't define a min value, which we
908 use for error checking, so provide one. */
909
910enum dwp_v2_section_ids
911{
912 DW_SECT_MIN = 1
913};
914
80626a55 915/* Data for one DWO file.
57d63ce2
DE
916
917 This includes virtual DWO files (a virtual DWO file is a DWO file as it
918 appears in a DWP file). DWP files don't really have DWO files per se -
919 comdat folding of types "loses" the DWO file they came from, and from
920 a high level view DWP files appear to contain a mass of random types.
921 However, to maintain consistency with the non-DWP case we pretend DWP
922 files contain virtual DWO files, and we assign each TU with one virtual
923 DWO file (generally based on the line and abbrev section offsets -
924 a heuristic that seems to work in practice). */
3019eac3
DE
925
926struct dwo_file
927{
0ac5b59e 928 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
929 For virtual DWO files the name is constructed from the section offsets
930 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
931 from related CU+TUs. */
0ac5b59e
DE
932 const char *dwo_name;
933
934 /* The DW_AT_comp_dir attribute. */
935 const char *comp_dir;
3019eac3 936
80626a55
DE
937 /* The bfd, when the file is open. Otherwise this is NULL.
938 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
939 bfd *dbfd;
3019eac3 940
73869dc2
DE
941 /* The sections that make up this DWO file.
942 Remember that for virtual DWO files in DWP V2, these are virtual
943 sections (for lack of a better name). */
3019eac3
DE
944 struct dwo_sections sections;
945
33c5cd75
DB
946 /* The CUs in the file.
947 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
948 an extension to handle LLVM's Link Time Optimization output (where
949 multiple source files may be compiled into a single object/dwo pair). */
950 htab_t cus;
3019eac3
DE
951
952 /* Table of TUs in the file.
953 Each element is a struct dwo_unit. */
954 htab_t tus;
955};
956
80626a55
DE
957/* These sections are what may appear in a DWP file. */
958
959struct dwp_sections
960{
73869dc2 961 /* These are used by both DWP version 1 and 2. */
80626a55
DE
962 struct dwarf2_section_info str;
963 struct dwarf2_section_info cu_index;
964 struct dwarf2_section_info tu_index;
73869dc2
DE
965
966 /* These are only used by DWP version 2 files.
967 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
968 sections are referenced by section number, and are not recorded here.
969 In DWP version 2 there is at most one copy of all these sections, each
970 section being (effectively) comprised of the concatenation of all of the
971 individual sections that exist in the version 1 format.
972 To keep the code simple we treat each of these concatenated pieces as a
973 section itself (a virtual section?). */
974 struct dwarf2_section_info abbrev;
975 struct dwarf2_section_info info;
976 struct dwarf2_section_info line;
977 struct dwarf2_section_info loc;
978 struct dwarf2_section_info macinfo;
979 struct dwarf2_section_info macro;
980 struct dwarf2_section_info str_offsets;
981 struct dwarf2_section_info types;
80626a55
DE
982};
983
73869dc2
DE
984/* These sections are what may appear in a virtual DWO file in DWP version 1.
985 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 986
73869dc2 987struct virtual_v1_dwo_sections
80626a55
DE
988{
989 struct dwarf2_section_info abbrev;
990 struct dwarf2_section_info line;
991 struct dwarf2_section_info loc;
992 struct dwarf2_section_info macinfo;
993 struct dwarf2_section_info macro;
994 struct dwarf2_section_info str_offsets;
995 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 996 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
997 struct dwarf2_section_info info_or_types;
998};
999
73869dc2
DE
1000/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1001 In version 2, the sections of the DWO files are concatenated together
1002 and stored in one section of that name. Thus each ELF section contains
1003 several "virtual" sections. */
1004
1005struct virtual_v2_dwo_sections
1006{
1007 bfd_size_type abbrev_offset;
1008 bfd_size_type abbrev_size;
1009
1010 bfd_size_type line_offset;
1011 bfd_size_type line_size;
1012
1013 bfd_size_type loc_offset;
1014 bfd_size_type loc_size;
1015
1016 bfd_size_type macinfo_offset;
1017 bfd_size_type macinfo_size;
1018
1019 bfd_size_type macro_offset;
1020 bfd_size_type macro_size;
1021
1022 bfd_size_type str_offsets_offset;
1023 bfd_size_type str_offsets_size;
1024
1025 /* Each DWP hash table entry records one CU or one TU.
1026 That is recorded here, and copied to dwo_unit.section. */
1027 bfd_size_type info_or_types_offset;
1028 bfd_size_type info_or_types_size;
1029};
1030
80626a55
DE
1031/* Contents of DWP hash tables. */
1032
1033struct dwp_hash_table
1034{
73869dc2 1035 uint32_t version, nr_columns;
80626a55 1036 uint32_t nr_units, nr_slots;
73869dc2
DE
1037 const gdb_byte *hash_table, *unit_table;
1038 union
1039 {
1040 struct
1041 {
1042 const gdb_byte *indices;
1043 } v1;
1044 struct
1045 {
1046 /* This is indexed by column number and gives the id of the section
1047 in that column. */
1048#define MAX_NR_V2_DWO_SECTIONS \
1049 (1 /* .debug_info or .debug_types */ \
1050 + 1 /* .debug_abbrev */ \
1051 + 1 /* .debug_line */ \
1052 + 1 /* .debug_loc */ \
1053 + 1 /* .debug_str_offsets */ \
1054 + 1 /* .debug_macro or .debug_macinfo */)
1055 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1056 const gdb_byte *offsets;
1057 const gdb_byte *sizes;
1058 } v2;
1059 } section_pool;
80626a55
DE
1060};
1061
1062/* Data for one DWP file. */
1063
1064struct dwp_file
1065{
1066 /* Name of the file. */
1067 const char *name;
1068
73869dc2
DE
1069 /* File format version. */
1070 int version;
1071
93417882 1072 /* The bfd. */
80626a55
DE
1073 bfd *dbfd;
1074
1075 /* Section info for this file. */
1076 struct dwp_sections sections;
1077
57d63ce2 1078 /* Table of CUs in the file. */
80626a55
DE
1079 const struct dwp_hash_table *cus;
1080
1081 /* Table of TUs in the file. */
1082 const struct dwp_hash_table *tus;
1083
19ac8c2e
DE
1084 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1085 htab_t loaded_cus;
1086 htab_t loaded_tus;
80626a55 1087
73869dc2
DE
1088 /* Table to map ELF section numbers to their sections.
1089 This is only needed for the DWP V1 file format. */
80626a55
DE
1090 unsigned int num_sections;
1091 asection **elf_sections;
1092};
1093
36586728
TT
1094/* This represents a '.dwz' file. */
1095
1096struct dwz_file
1097{
1098 /* A dwz file can only contain a few sections. */
1099 struct dwarf2_section_info abbrev;
1100 struct dwarf2_section_info info;
1101 struct dwarf2_section_info str;
1102 struct dwarf2_section_info line;
1103 struct dwarf2_section_info macro;
2ec9a5e0 1104 struct dwarf2_section_info gdb_index;
36586728
TT
1105
1106 /* The dwz's BFD. */
1107 bfd *dwz_bfd;
1108};
1109
0963b4bd
MS
1110/* Struct used to pass misc. parameters to read_die_and_children, et
1111 al. which are used for both .debug_info and .debug_types dies.
1112 All parameters here are unchanging for the life of the call. This
dee91e82 1113 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1114
1115struct die_reader_specs
1116{
a32a8923 1117 /* The bfd of die_section. */
93311388
DE
1118 bfd* abfd;
1119
1120 /* The CU of the DIE we are parsing. */
1121 struct dwarf2_cu *cu;
1122
80626a55 1123 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1124 struct dwo_file *dwo_file;
1125
dee91e82 1126 /* The section the die comes from.
3019eac3 1127 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1128 struct dwarf2_section_info *die_section;
1129
1130 /* die_section->buffer. */
d521ce57 1131 const gdb_byte *buffer;
f664829e
DE
1132
1133 /* The end of the buffer. */
1134 const gdb_byte *buffer_end;
a2ce51a0
DE
1135
1136 /* The value of the DW_AT_comp_dir attribute. */
1137 const char *comp_dir;
93311388
DE
1138};
1139
fd820528 1140/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1141typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1142 const gdb_byte *info_ptr,
dee91e82
DE
1143 struct die_info *comp_unit_die,
1144 int has_children,
1145 void *data);
1146
ecfb656c
PA
1147/* A 1-based directory index. This is a strong typedef to prevent
1148 accidentally using a directory index as a 0-based index into an
1149 array/vector. */
1150enum class dir_index : unsigned int {};
1151
1152/* Likewise, a 1-based file name index. */
1153enum class file_name_index : unsigned int {};
1154
52059ffd
TT
1155struct file_entry
1156{
fff8551c
PA
1157 file_entry () = default;
1158
ecfb656c 1159 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1160 unsigned int mod_time_, unsigned int length_)
1161 : name (name_),
ecfb656c 1162 d_index (d_index_),
fff8551c
PA
1163 mod_time (mod_time_),
1164 length (length_)
1165 {}
1166
ecfb656c
PA
1167 /* Return the include directory at D_INDEX stored in LH. Returns
1168 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1169 const char *include_dir (const line_header *lh) const;
1170
fff8551c
PA
1171 /* The file name. Note this is an observing pointer. The memory is
1172 owned by debug_line_buffer. */
1173 const char *name {};
1174
8c43009f 1175 /* The directory index (1-based). */
ecfb656c 1176 dir_index d_index {};
fff8551c
PA
1177
1178 unsigned int mod_time {};
1179
1180 unsigned int length {};
1181
1182 /* True if referenced by the Line Number Program. */
1183 bool included_p {};
1184
83769d0b 1185 /* The associated symbol table, if any. */
fff8551c 1186 struct symtab *symtab {};
52059ffd
TT
1187};
1188
debd256d
JB
1189/* The line number information for a compilation unit (found in the
1190 .debug_line section) begins with a "statement program header",
1191 which contains the following information. */
1192struct line_header
1193{
fff8551c
PA
1194 line_header ()
1195 : offset_in_dwz {}
1196 {}
1197
1198 /* Add an entry to the include directory table. */
1199 void add_include_dir (const char *include_dir);
1200
1201 /* Add an entry to the file name table. */
ecfb656c 1202 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1203 unsigned int mod_time, unsigned int length);
1204
ecfb656c 1205 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1206 is out of bounds. */
ecfb656c 1207 const char *include_dir_at (dir_index index) const
8c43009f 1208 {
ecfb656c
PA
1209 /* Convert directory index number (1-based) to vector index
1210 (0-based). */
1211 size_t vec_index = to_underlying (index) - 1;
1212
1213 if (vec_index >= include_dirs.size ())
8c43009f 1214 return NULL;
ecfb656c 1215 return include_dirs[vec_index];
8c43009f
PA
1216 }
1217
ecfb656c 1218 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1219 is out of bounds. */
ecfb656c 1220 file_entry *file_name_at (file_name_index index)
8c43009f 1221 {
ecfb656c
PA
1222 /* Convert file name index number (1-based) to vector index
1223 (0-based). */
1224 size_t vec_index = to_underlying (index) - 1;
1225
1226 if (vec_index >= file_names.size ())
fff8551c 1227 return NULL;
ecfb656c 1228 return &file_names[vec_index];
fff8551c
PA
1229 }
1230
1231 /* Const version of the above. */
1232 const file_entry *file_name_at (unsigned int index) const
1233 {
1234 if (index >= file_names.size ())
8c43009f
PA
1235 return NULL;
1236 return &file_names[index];
1237 }
1238
527f3840 1239 /* Offset of line number information in .debug_line section. */
9c541725 1240 sect_offset sect_off {};
527f3840
JK
1241
1242 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1243 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1244
1245 unsigned int total_length {};
1246 unsigned short version {};
1247 unsigned int header_length {};
1248 unsigned char minimum_instruction_length {};
1249 unsigned char maximum_ops_per_instruction {};
1250 unsigned char default_is_stmt {};
1251 int line_base {};
1252 unsigned char line_range {};
1253 unsigned char opcode_base {};
debd256d
JB
1254
1255 /* standard_opcode_lengths[i] is the number of operands for the
1256 standard opcode whose value is i. This means that
1257 standard_opcode_lengths[0] is unused, and the last meaningful
1258 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1259 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1260
fff8551c
PA
1261 /* The include_directories table. Note these are observing
1262 pointers. The memory is owned by debug_line_buffer. */
1263 std::vector<const char *> include_dirs;
debd256d 1264
fff8551c
PA
1265 /* The file_names table. */
1266 std::vector<file_entry> file_names;
debd256d
JB
1267
1268 /* The start and end of the statement program following this
6502dd73 1269 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1270 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1271};
c906108c 1272
fff8551c
PA
1273typedef std::unique_ptr<line_header> line_header_up;
1274
8c43009f
PA
1275const char *
1276file_entry::include_dir (const line_header *lh) const
1277{
ecfb656c 1278 return lh->include_dir_at (d_index);
8c43009f
PA
1279}
1280
c906108c 1281/* When we construct a partial symbol table entry we only
0963b4bd 1282 need this much information. */
c906108c
SS
1283struct partial_die_info
1284 {
72bf9492 1285 /* Offset of this DIE. */
9c541725 1286 sect_offset sect_off;
72bf9492
DJ
1287
1288 /* DWARF-2 tag for this DIE. */
1289 ENUM_BITFIELD(dwarf_tag) tag : 16;
1290
72bf9492
DJ
1291 /* Assorted flags describing the data found in this DIE. */
1292 unsigned int has_children : 1;
1293 unsigned int is_external : 1;
1294 unsigned int is_declaration : 1;
1295 unsigned int has_type : 1;
1296 unsigned int has_specification : 1;
1297 unsigned int has_pc_info : 1;
481860b3 1298 unsigned int may_be_inlined : 1;
72bf9492 1299
0c1b455e
TT
1300 /* This DIE has been marked DW_AT_main_subprogram. */
1301 unsigned int main_subprogram : 1;
1302
72bf9492
DJ
1303 /* Flag set if the SCOPE field of this structure has been
1304 computed. */
1305 unsigned int scope_set : 1;
1306
fa4028e9
JB
1307 /* Flag set if the DIE has a byte_size attribute. */
1308 unsigned int has_byte_size : 1;
1309
ff908ebf
AW
1310 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1311 unsigned int has_const_value : 1;
1312
98bfdba5
PA
1313 /* Flag set if any of the DIE's children are template arguments. */
1314 unsigned int has_template_arguments : 1;
1315
abc72ce4
DE
1316 /* Flag set if fixup_partial_die has been called on this die. */
1317 unsigned int fixup_called : 1;
1318
36586728
TT
1319 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1320 unsigned int is_dwz : 1;
1321
1322 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1323 unsigned int spec_is_dwz : 1;
1324
72bf9492 1325 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1326 sometimes a default name for unnamed DIEs. */
15d034d0 1327 const char *name;
72bf9492 1328
abc72ce4
DE
1329 /* The linkage name, if present. */
1330 const char *linkage_name;
1331
72bf9492
DJ
1332 /* The scope to prepend to our children. This is generally
1333 allocated on the comp_unit_obstack, so will disappear
1334 when this compilation unit leaves the cache. */
15d034d0 1335 const char *scope;
72bf9492 1336
95554aad
TT
1337 /* Some data associated with the partial DIE. The tag determines
1338 which field is live. */
1339 union
1340 {
1341 /* The location description associated with this DIE, if any. */
1342 struct dwarf_block *locdesc;
1343 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1344 sect_offset sect_off;
95554aad 1345 } d;
72bf9492
DJ
1346
1347 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1348 CORE_ADDR lowpc;
1349 CORE_ADDR highpc;
72bf9492 1350
93311388 1351 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1352 DW_AT_sibling, if any. */
abc72ce4
DE
1353 /* NOTE: This member isn't strictly necessary, read_partial_die could
1354 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1355 const gdb_byte *sibling;
72bf9492
DJ
1356
1357 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1358 DW_AT_specification (or DW_AT_abstract_origin or
1359 DW_AT_extension). */
b64f50a1 1360 sect_offset spec_offset;
72bf9492
DJ
1361
1362 /* Pointers to this DIE's parent, first child, and next sibling,
1363 if any. */
1364 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1365 };
1366
0963b4bd 1367/* This data structure holds the information of an abbrev. */
c906108c
SS
1368struct abbrev_info
1369 {
1370 unsigned int number; /* number identifying abbrev */
1371 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1372 unsigned short has_children; /* boolean */
1373 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1374 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1375 struct abbrev_info *next; /* next in chain */
1376 };
1377
1378struct attr_abbrev
1379 {
9d25dd43
DE
1380 ENUM_BITFIELD(dwarf_attribute) name : 16;
1381 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1382
1383 /* It is valid only if FORM is DW_FORM_implicit_const. */
1384 LONGEST implicit_const;
c906108c
SS
1385 };
1386
433df2d4
DE
1387/* Size of abbrev_table.abbrev_hash_table. */
1388#define ABBREV_HASH_SIZE 121
1389
1390/* Top level data structure to contain an abbreviation table. */
1391
1392struct abbrev_table
1393{
f4dc4d17
DE
1394 /* Where the abbrev table came from.
1395 This is used as a sanity check when the table is used. */
9c541725 1396 sect_offset sect_off;
433df2d4
DE
1397
1398 /* Storage for the abbrev table. */
1399 struct obstack abbrev_obstack;
1400
1401 /* Hash table of abbrevs.
1402 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1403 It could be statically allocated, but the previous code didn't so we
1404 don't either. */
1405 struct abbrev_info **abbrevs;
1406};
1407
0963b4bd 1408/* Attributes have a name and a value. */
b60c80d6
DJ
1409struct attribute
1410 {
9d25dd43 1411 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1412 ENUM_BITFIELD(dwarf_form) form : 15;
1413
1414 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1415 field should be in u.str (existing only for DW_STRING) but it is kept
1416 here for better struct attribute alignment. */
1417 unsigned int string_is_canonical : 1;
1418
b60c80d6
DJ
1419 union
1420 {
15d034d0 1421 const char *str;
b60c80d6 1422 struct dwarf_block *blk;
43bbcdc2
PH
1423 ULONGEST unsnd;
1424 LONGEST snd;
b60c80d6 1425 CORE_ADDR addr;
ac9ec31b 1426 ULONGEST signature;
b60c80d6
DJ
1427 }
1428 u;
1429 };
1430
0963b4bd 1431/* This data structure holds a complete die structure. */
c906108c
SS
1432struct die_info
1433 {
76815b17
DE
1434 /* DWARF-2 tag for this DIE. */
1435 ENUM_BITFIELD(dwarf_tag) tag : 16;
1436
1437 /* Number of attributes */
98bfdba5
PA
1438 unsigned char num_attrs;
1439
1440 /* True if we're presently building the full type name for the
1441 type derived from this DIE. */
1442 unsigned char building_fullname : 1;
76815b17 1443
adde2bff
DE
1444 /* True if this die is in process. PR 16581. */
1445 unsigned char in_process : 1;
1446
76815b17
DE
1447 /* Abbrev number */
1448 unsigned int abbrev;
1449
93311388 1450 /* Offset in .debug_info or .debug_types section. */
9c541725 1451 sect_offset sect_off;
78ba4af6
JB
1452
1453 /* The dies in a compilation unit form an n-ary tree. PARENT
1454 points to this die's parent; CHILD points to the first child of
1455 this node; and all the children of a given node are chained
4950bc1c 1456 together via their SIBLING fields. */
639d11d3
DC
1457 struct die_info *child; /* Its first child, if any. */
1458 struct die_info *sibling; /* Its next sibling, if any. */
1459 struct die_info *parent; /* Its parent, if any. */
c906108c 1460
b60c80d6
DJ
1461 /* An array of attributes, with NUM_ATTRS elements. There may be
1462 zero, but it's not common and zero-sized arrays are not
1463 sufficiently portable C. */
1464 struct attribute attrs[1];
c906108c
SS
1465 };
1466
0963b4bd 1467/* Get at parts of an attribute structure. */
c906108c
SS
1468
1469#define DW_STRING(attr) ((attr)->u.str)
8285870a 1470#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1471#define DW_UNSND(attr) ((attr)->u.unsnd)
1472#define DW_BLOCK(attr) ((attr)->u.blk)
1473#define DW_SND(attr) ((attr)->u.snd)
1474#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1475#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1476
0963b4bd 1477/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1478struct dwarf_block
1479 {
56eb65bd 1480 size_t size;
1d6edc3c
JK
1481
1482 /* Valid only if SIZE is not zero. */
d521ce57 1483 const gdb_byte *data;
c906108c
SS
1484 };
1485
c906108c
SS
1486#ifndef ATTR_ALLOC_CHUNK
1487#define ATTR_ALLOC_CHUNK 4
1488#endif
1489
c906108c
SS
1490/* Allocate fields for structs, unions and enums in this size. */
1491#ifndef DW_FIELD_ALLOC_CHUNK
1492#define DW_FIELD_ALLOC_CHUNK 4
1493#endif
1494
c906108c
SS
1495/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1496 but this would require a corresponding change in unpack_field_as_long
1497 and friends. */
1498static int bits_per_byte = 8;
1499
52059ffd
TT
1500struct nextfield
1501{
1502 struct nextfield *next;
1503 int accessibility;
1504 int virtuality;
1505 struct field field;
1506};
1507
1508struct nextfnfield
1509{
1510 struct nextfnfield *next;
1511 struct fn_field fnfield;
1512};
1513
1514struct fnfieldlist
1515{
1516 const char *name;
1517 int length;
1518 struct nextfnfield *head;
1519};
1520
1521struct typedef_field_list
1522{
1523 struct typedef_field field;
1524 struct typedef_field_list *next;
1525};
1526
c906108c
SS
1527/* The routines that read and process dies for a C struct or C++ class
1528 pass lists of data member fields and lists of member function fields
1529 in an instance of a field_info structure, as defined below. */
1530struct field_info
c5aa993b 1531 {
0963b4bd 1532 /* List of data member and baseclasses fields. */
52059ffd 1533 struct nextfield *fields, *baseclasses;
c906108c 1534
7d0ccb61 1535 /* Number of fields (including baseclasses). */
c5aa993b 1536 int nfields;
c906108c 1537
c5aa993b
JM
1538 /* Number of baseclasses. */
1539 int nbaseclasses;
c906108c 1540
c5aa993b
JM
1541 /* Set if the accesibility of one of the fields is not public. */
1542 int non_public_fields;
c906108c 1543
c5aa993b
JM
1544 /* Member function fieldlist array, contains name of possibly overloaded
1545 member function, number of overloaded member functions and a pointer
1546 to the head of the member function field chain. */
52059ffd 1547 struct fnfieldlist *fnfieldlists;
c906108c 1548
c5aa993b
JM
1549 /* Number of entries in the fnfieldlists array. */
1550 int nfnfields;
98751a41
JK
1551
1552 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1553 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1554 struct typedef_field_list *typedef_field_list;
98751a41 1555 unsigned typedef_field_list_count;
c5aa993b 1556 };
c906108c 1557
10b3939b
DJ
1558/* One item on the queue of compilation units to read in full symbols
1559 for. */
1560struct dwarf2_queue_item
1561{
1562 struct dwarf2_per_cu_data *per_cu;
95554aad 1563 enum language pretend_language;
10b3939b
DJ
1564 struct dwarf2_queue_item *next;
1565};
1566
1567/* The current queue. */
1568static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1569
ae038cb0
DJ
1570/* Loaded secondary compilation units are kept in memory until they
1571 have not been referenced for the processing of this many
1572 compilation units. Set this to zero to disable caching. Cache
1573 sizes of up to at least twenty will improve startup time for
1574 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1575static int dwarf_max_cache_age = 5;
920d2a44 1576static void
b4f54984
DE
1577show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1578 struct cmd_list_element *c, const char *value)
920d2a44 1579{
3e43a32a 1580 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1581 "DWARF compilation units is %s.\n"),
920d2a44
AC
1582 value);
1583}
4390d890 1584\f
c906108c
SS
1585/* local function prototypes */
1586
a32a8923
DE
1587static const char *get_section_name (const struct dwarf2_section_info *);
1588
1589static const char *get_section_file_name (const struct dwarf2_section_info *);
1590
918dd910
JK
1591static void dwarf2_find_base_address (struct die_info *die,
1592 struct dwarf2_cu *cu);
1593
0018ea6f
DE
1594static struct partial_symtab *create_partial_symtab
1595 (struct dwarf2_per_cu_data *per_cu, const char *name);
1596
f1902523
JK
1597static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1598 const gdb_byte *info_ptr,
1599 struct die_info *type_unit_die,
1600 int has_children, void *data);
1601
c67a9c90 1602static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1603
72bf9492
DJ
1604static void scan_partial_symbols (struct partial_die_info *,
1605 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1606 int, struct dwarf2_cu *);
c906108c 1607
72bf9492
DJ
1608static void add_partial_symbol (struct partial_die_info *,
1609 struct dwarf2_cu *);
63d06c5c 1610
72bf9492
DJ
1611static void add_partial_namespace (struct partial_die_info *pdi,
1612 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1613 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1614
5d7cb8df 1615static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1616 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1617 struct dwarf2_cu *cu);
1618
72bf9492
DJ
1619static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1620 struct dwarf2_cu *cu);
91c24f0a 1621
bc30ff58
JB
1622static void add_partial_subprogram (struct partial_die_info *pdi,
1623 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1624 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1625
257e7a09
YQ
1626static void dwarf2_read_symtab (struct partial_symtab *,
1627 struct objfile *);
c906108c 1628
a14ed312 1629static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1630
433df2d4
DE
1631static struct abbrev_info *abbrev_table_lookup_abbrev
1632 (const struct abbrev_table *, unsigned int);
1633
1634static struct abbrev_table *abbrev_table_read_table
1635 (struct dwarf2_section_info *, sect_offset);
1636
1637static void abbrev_table_free (struct abbrev_table *);
1638
f4dc4d17
DE
1639static void abbrev_table_free_cleanup (void *);
1640
dee91e82
DE
1641static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1642 struct dwarf2_section_info *);
c906108c 1643
f3dd6933 1644static void dwarf2_free_abbrev_table (void *);
c906108c 1645
d521ce57 1646static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1647
dee91e82 1648static struct partial_die_info *load_partial_dies
d521ce57 1649 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1650
d521ce57
TT
1651static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1652 struct partial_die_info *,
1653 struct abbrev_info *,
1654 unsigned int,
1655 const gdb_byte *);
c906108c 1656
36586728 1657static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1658 struct dwarf2_cu *);
72bf9492
DJ
1659
1660static void fixup_partial_die (struct partial_die_info *,
1661 struct dwarf2_cu *);
1662
d521ce57
TT
1663static const gdb_byte *read_attribute (const struct die_reader_specs *,
1664 struct attribute *, struct attr_abbrev *,
1665 const gdb_byte *);
a8329558 1666
a1855c1d 1667static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1668
a1855c1d 1669static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1670
a1855c1d 1671static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1672
a1855c1d 1673static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1674
a1855c1d 1675static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1676
d521ce57 1677static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1678 unsigned int *);
c906108c 1679
d521ce57 1680static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1681
1682static LONGEST read_checked_initial_length_and_offset
d521ce57 1683 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1684 unsigned int *, unsigned int *);
613e1657 1685
d521ce57
TT
1686static LONGEST read_offset (bfd *, const gdb_byte *,
1687 const struct comp_unit_head *,
c764a876
DE
1688 unsigned int *);
1689
d521ce57 1690static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1691
f4dc4d17
DE
1692static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1693 sect_offset);
1694
d521ce57 1695static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1696
d521ce57 1697static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1698
d521ce57
TT
1699static const char *read_indirect_string (bfd *, const gdb_byte *,
1700 const struct comp_unit_head *,
1701 unsigned int *);
4bdf3d34 1702
43988095
JK
1703static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1704 const struct comp_unit_head *,
1705 unsigned int *);
36586728 1706
43988095 1707static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1708
d521ce57 1709static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1710
d521ce57
TT
1711static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1712 const gdb_byte *,
3019eac3
DE
1713 unsigned int *);
1714
d521ce57 1715static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1716 ULONGEST str_index);
3019eac3 1717
e142c38c 1718static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1719
e142c38c
DJ
1720static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1721 struct dwarf2_cu *);
c906108c 1722
348e048f 1723static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1724 unsigned int);
348e048f 1725
7d45c7c3
KB
1726static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1727 struct dwarf2_cu *cu);
1728
05cf31d1
JB
1729static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1730 struct dwarf2_cu *cu);
1731
e142c38c 1732static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1733
e142c38c 1734static struct die_info *die_specification (struct die_info *die,
f2f0e013 1735 struct dwarf2_cu **);
63d06c5c 1736
9c541725 1737static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1738 struct dwarf2_cu *cu);
debd256d 1739
f3f5162e 1740static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1741 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1742 CORE_ADDR, int decode_mapping);
c906108c 1743
4d663531 1744static void dwarf2_start_subfile (const char *, const char *);
c906108c 1745
43f3e411
DE
1746static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1747 const char *, const char *,
1748 CORE_ADDR);
f4dc4d17 1749
a14ed312 1750static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1751 struct dwarf2_cu *);
c906108c 1752
34eaf542
TT
1753static struct symbol *new_symbol_full (struct die_info *, struct type *,
1754 struct dwarf2_cu *, struct symbol *);
1755
ff39bb5e 1756static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1757 struct dwarf2_cu *);
c906108c 1758
ff39bb5e 1759static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1760 struct type *type,
1761 const char *name,
1762 struct obstack *obstack,
12df843f 1763 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1764 const gdb_byte **bytes,
98bfdba5 1765 struct dwarf2_locexpr_baton **baton);
2df3850c 1766
e7c27a73 1767static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1768
b4ba55a1
JB
1769static int need_gnat_info (struct dwarf2_cu *);
1770
3e43a32a
MS
1771static struct type *die_descriptive_type (struct die_info *,
1772 struct dwarf2_cu *);
b4ba55a1
JB
1773
1774static void set_descriptive_type (struct type *, struct die_info *,
1775 struct dwarf2_cu *);
1776
e7c27a73
DJ
1777static struct type *die_containing_type (struct die_info *,
1778 struct dwarf2_cu *);
c906108c 1779
ff39bb5e 1780static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1781 struct dwarf2_cu *);
c906108c 1782
f792889a 1783static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1784
673bfd45
DE
1785static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1786
0d5cff50 1787static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1788
6e70227d 1789static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1790 const char *suffix, int physname,
1791 struct dwarf2_cu *cu);
63d06c5c 1792
e7c27a73 1793static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1794
348e048f
DE
1795static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1796
e7c27a73 1797static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1798
e7c27a73 1799static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1800
96408a79
SA
1801static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1802
ff013f42
JK
1803static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1804 struct dwarf2_cu *, struct partial_symtab *);
1805
3a2b436a 1806/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1807 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1808enum pc_bounds_kind
1809{
e385593e 1810 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1811 PC_BOUNDS_NOT_PRESENT,
1812
e385593e
JK
1813 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1814 were present but they do not form a valid range of PC addresses. */
1815 PC_BOUNDS_INVALID,
1816
3a2b436a
JK
1817 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1818 PC_BOUNDS_RANGES,
1819
1820 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1821 PC_BOUNDS_HIGH_LOW,
1822};
1823
1824static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1825 CORE_ADDR *, CORE_ADDR *,
1826 struct dwarf2_cu *,
1827 struct partial_symtab *);
c906108c 1828
fae299cd
DC
1829static void get_scope_pc_bounds (struct die_info *,
1830 CORE_ADDR *, CORE_ADDR *,
1831 struct dwarf2_cu *);
1832
801e3a5b
JB
1833static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1834 CORE_ADDR, struct dwarf2_cu *);
1835
a14ed312 1836static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1837 struct dwarf2_cu *);
c906108c 1838
a14ed312 1839static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1840 struct type *, struct dwarf2_cu *);
c906108c 1841
a14ed312 1842static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1843 struct die_info *, struct type *,
e7c27a73 1844 struct dwarf2_cu *);
c906108c 1845
a14ed312 1846static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1847 struct type *,
1848 struct dwarf2_cu *);
c906108c 1849
134d01f1 1850static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1851
e7c27a73 1852static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1853
e7c27a73 1854static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1855
5d7cb8df
JK
1856static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1857
22cee43f
PMR
1858static struct using_direct **using_directives (enum language);
1859
27aa8d6a
SW
1860static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1861
74921315
KS
1862static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1863
f55ee35c
JK
1864static struct type *read_module_type (struct die_info *die,
1865 struct dwarf2_cu *cu);
1866
38d518c9 1867static const char *namespace_name (struct die_info *die,
e142c38c 1868 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1869
134d01f1 1870static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1871
e7c27a73 1872static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1873
6e70227d 1874static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1875 struct dwarf2_cu *);
1876
bf6af496 1877static struct die_info *read_die_and_siblings_1
d521ce57 1878 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1879 struct die_info *);
639d11d3 1880
dee91e82 1881static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1882 const gdb_byte *info_ptr,
1883 const gdb_byte **new_info_ptr,
639d11d3
DC
1884 struct die_info *parent);
1885
d521ce57
TT
1886static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1887 struct die_info **, const gdb_byte *,
1888 int *, int);
3019eac3 1889
d521ce57
TT
1890static const gdb_byte *read_full_die (const struct die_reader_specs *,
1891 struct die_info **, const gdb_byte *,
1892 int *);
93311388 1893
e7c27a73 1894static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1895
15d034d0
TT
1896static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1897 struct obstack *);
71c25dea 1898
15d034d0 1899static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1900
15d034d0 1901static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1902 struct die_info *die,
1903 struct dwarf2_cu *cu);
1904
ca69b9e6
DE
1905static const char *dwarf2_physname (const char *name, struct die_info *die,
1906 struct dwarf2_cu *cu);
1907
e142c38c 1908static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1909 struct dwarf2_cu **);
9219021c 1910
f39c6ffd 1911static const char *dwarf_tag_name (unsigned int);
c906108c 1912
f39c6ffd 1913static const char *dwarf_attr_name (unsigned int);
c906108c 1914
f39c6ffd 1915static const char *dwarf_form_name (unsigned int);
c906108c 1916
a121b7c1 1917static const char *dwarf_bool_name (unsigned int);
c906108c 1918
f39c6ffd 1919static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1920
f9aca02d 1921static struct die_info *sibling_die (struct die_info *);
c906108c 1922
d97bc12b
DE
1923static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1924
1925static void dump_die_for_error (struct die_info *);
1926
1927static void dump_die_1 (struct ui_file *, int level, int max_level,
1928 struct die_info *);
c906108c 1929
d97bc12b 1930/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1931
51545339 1932static void store_in_ref_table (struct die_info *,
10b3939b 1933 struct dwarf2_cu *);
c906108c 1934
ff39bb5e 1935static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1936
ff39bb5e 1937static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1938
348e048f 1939static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1940 const struct attribute *,
348e048f
DE
1941 struct dwarf2_cu **);
1942
10b3939b 1943static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1944 const struct attribute *,
f2f0e013 1945 struct dwarf2_cu **);
c906108c 1946
348e048f 1947static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1948 const struct attribute *,
348e048f
DE
1949 struct dwarf2_cu **);
1950
ac9ec31b
DE
1951static struct type *get_signatured_type (struct die_info *, ULONGEST,
1952 struct dwarf2_cu *);
1953
1954static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1955 const struct attribute *,
ac9ec31b
DE
1956 struct dwarf2_cu *);
1957
e5fe5e75 1958static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1959
52dc124a 1960static void read_signatured_type (struct signatured_type *);
348e048f 1961
63e43d3a
PMR
1962static int attr_to_dynamic_prop (const struct attribute *attr,
1963 struct die_info *die, struct dwarf2_cu *cu,
1964 struct dynamic_prop *prop);
1965
c906108c
SS
1966/* memory allocation interface */
1967
7b5a2f43 1968static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1969
b60c80d6 1970static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1971
43f3e411 1972static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1973
6e5a29e1 1974static int attr_form_is_block (const struct attribute *);
8e19ed76 1975
6e5a29e1 1976static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1977
6e5a29e1 1978static int attr_form_is_constant (const struct attribute *);
3690dd37 1979
6e5a29e1 1980static int attr_form_is_ref (const struct attribute *);
7771576e 1981
8cf6f0b1
TT
1982static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1983 struct dwarf2_loclist_baton *baton,
ff39bb5e 1984 const struct attribute *attr);
8cf6f0b1 1985
ff39bb5e 1986static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1987 struct symbol *sym,
f1e6e072
TT
1988 struct dwarf2_cu *cu,
1989 int is_block);
4c2df51b 1990
d521ce57
TT
1991static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1992 const gdb_byte *info_ptr,
1993 struct abbrev_info *abbrev);
4bb7a0a7 1994
72bf9492
DJ
1995static void free_stack_comp_unit (void *);
1996
72bf9492
DJ
1997static hashval_t partial_die_hash (const void *item);
1998
1999static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2000
ae038cb0 2001static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 2002 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 2003
9816fde3 2004static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2005 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2006
2007static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2008 struct die_info *comp_unit_die,
2009 enum language pretend_language);
93311388 2010
68dc6402 2011static void free_heap_comp_unit (void *);
ae038cb0
DJ
2012
2013static void free_cached_comp_units (void *);
2014
2015static void age_cached_comp_units (void);
2016
dee91e82 2017static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2018
f792889a
DJ
2019static struct type *set_die_type (struct die_info *, struct type *,
2020 struct dwarf2_cu *);
1c379e20 2021
ae038cb0
DJ
2022static void create_all_comp_units (struct objfile *);
2023
0e50663e 2024static int create_all_type_units (struct objfile *);
1fd400ff 2025
95554aad
TT
2026static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2027 enum language);
10b3939b 2028
95554aad
TT
2029static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2030 enum language);
10b3939b 2031
f4dc4d17
DE
2032static void process_full_type_unit (struct dwarf2_per_cu_data *,
2033 enum language);
2034
10b3939b
DJ
2035static void dwarf2_add_dependence (struct dwarf2_cu *,
2036 struct dwarf2_per_cu_data *);
2037
ae038cb0
DJ
2038static void dwarf2_mark (struct dwarf2_cu *);
2039
2040static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2041
b64f50a1 2042static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2043 struct dwarf2_per_cu_data *);
673bfd45 2044
f792889a 2045static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2046
9291a0cd
TT
2047static void dwarf2_release_queue (void *dummy);
2048
95554aad
TT
2049static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2050 enum language pretend_language);
2051
a0f42c21 2052static void process_queue (void);
9291a0cd 2053
d721ba37
PA
2054/* The return type of find_file_and_directory. Note, the enclosed
2055 string pointers are only valid while this object is valid. */
2056
2057struct file_and_directory
2058{
2059 /* The filename. This is never NULL. */
2060 const char *name;
2061
2062 /* The compilation directory. NULL if not known. If we needed to
2063 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2064 points directly to the DW_AT_comp_dir string attribute owned by
2065 the obstack that owns the DIE. */
2066 const char *comp_dir;
2067
2068 /* If we needed to build a new string for comp_dir, this is what
2069 owns the storage. */
2070 std::string comp_dir_storage;
2071};
2072
2073static file_and_directory find_file_and_directory (struct die_info *die,
2074 struct dwarf2_cu *cu);
9291a0cd
TT
2075
2076static char *file_full_name (int file, struct line_header *lh,
2077 const char *comp_dir);
2078
43988095
JK
2079/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2080enum class rcuh_kind { COMPILE, TYPE };
2081
d521ce57 2082static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2083 (struct comp_unit_head *header,
2084 struct dwarf2_section_info *section,
d521ce57 2085 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2086 rcuh_kind section_kind);
36586728 2087
fd820528 2088static void init_cutu_and_read_dies
f4dc4d17
DE
2089 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2090 int use_existing_cu, int keep,
3019eac3
DE
2091 die_reader_func_ftype *die_reader_func, void *data);
2092
dee91e82
DE
2093static void init_cutu_and_read_dies_simple
2094 (struct dwarf2_per_cu_data *this_cu,
2095 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2096
673bfd45 2097static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2098
3019eac3
DE
2099static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2100
57d63ce2
DE
2101static struct dwo_unit *lookup_dwo_unit_in_dwp
2102 (struct dwp_file *dwp_file, const char *comp_dir,
2103 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2104
2105static struct dwp_file *get_dwp_file (void);
2106
3019eac3 2107static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2108 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2109
2110static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2111 (struct signatured_type *, const char *, const char *);
3019eac3 2112
89e63ee4
DE
2113static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2114
3019eac3
DE
2115static void free_dwo_file_cleanup (void *);
2116
95554aad
TT
2117static void process_cu_includes (void);
2118
1b80a9fa 2119static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2120
2121static void free_line_header_voidp (void *arg);
4390d890
DE
2122\f
2123/* Various complaints about symbol reading that don't abort the process. */
2124
2125static void
2126dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2127{
2128 complaint (&symfile_complaints,
2129 _("statement list doesn't fit in .debug_line section"));
2130}
2131
2132static void
2133dwarf2_debug_line_missing_file_complaint (void)
2134{
2135 complaint (&symfile_complaints,
2136 _(".debug_line section has line data without a file"));
2137}
2138
2139static void
2140dwarf2_debug_line_missing_end_sequence_complaint (void)
2141{
2142 complaint (&symfile_complaints,
2143 _(".debug_line section has line "
2144 "program sequence without an end"));
2145}
2146
2147static void
2148dwarf2_complex_location_expr_complaint (void)
2149{
2150 complaint (&symfile_complaints, _("location expression too complex"));
2151}
2152
2153static void
2154dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2155 int arg3)
2156{
2157 complaint (&symfile_complaints,
2158 _("const value length mismatch for '%s', got %d, expected %d"),
2159 arg1, arg2, arg3);
2160}
2161
2162static void
2163dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2164{
2165 complaint (&symfile_complaints,
2166 _("debug info runs off end of %s section"
2167 " [in module %s]"),
a32a8923
DE
2168 get_section_name (section),
2169 get_section_file_name (section));
4390d890 2170}
1b80a9fa 2171
4390d890
DE
2172static void
2173dwarf2_macro_malformed_definition_complaint (const char *arg1)
2174{
2175 complaint (&symfile_complaints,
2176 _("macro debug info contains a "
2177 "malformed macro definition:\n`%s'"),
2178 arg1);
2179}
2180
2181static void
2182dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2183{
2184 complaint (&symfile_complaints,
2185 _("invalid attribute class or form for '%s' in '%s'"),
2186 arg1, arg2);
2187}
527f3840
JK
2188
2189/* Hash function for line_header_hash. */
2190
2191static hashval_t
2192line_header_hash (const struct line_header *ofs)
2193{
9c541725 2194 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2195}
2196
2197/* Hash function for htab_create_alloc_ex for line_header_hash. */
2198
2199static hashval_t
2200line_header_hash_voidp (const void *item)
2201{
9a3c8263 2202 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2203
2204 return line_header_hash (ofs);
2205}
2206
2207/* Equality function for line_header_hash. */
2208
2209static int
2210line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2211{
9a3c8263
SM
2212 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2213 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2214
9c541725 2215 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2216 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2217}
2218
4390d890 2219\f
9291a0cd 2220
31aa7e4e
JB
2221/* Read the given attribute value as an address, taking the attribute's
2222 form into account. */
2223
2224static CORE_ADDR
2225attr_value_as_address (struct attribute *attr)
2226{
2227 CORE_ADDR addr;
2228
2229 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2230 {
2231 /* Aside from a few clearly defined exceptions, attributes that
2232 contain an address must always be in DW_FORM_addr form.
2233 Unfortunately, some compilers happen to be violating this
2234 requirement by encoding addresses using other forms, such
2235 as DW_FORM_data4 for example. For those broken compilers,
2236 we try to do our best, without any guarantee of success,
2237 to interpret the address correctly. It would also be nice
2238 to generate a complaint, but that would require us to maintain
2239 a list of legitimate cases where a non-address form is allowed,
2240 as well as update callers to pass in at least the CU's DWARF
2241 version. This is more overhead than what we're willing to
2242 expand for a pretty rare case. */
2243 addr = DW_UNSND (attr);
2244 }
2245 else
2246 addr = DW_ADDR (attr);
2247
2248 return addr;
2249}
2250
9291a0cd
TT
2251/* The suffix for an index file. */
2252#define INDEX_SUFFIX ".gdb-index"
2253
330cdd98
PA
2254/* See declaration. */
2255
2256dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2257 const dwarf2_debug_sections *names)
2258 : objfile (objfile_)
2259{
2260 if (names == NULL)
2261 names = &dwarf2_elf_names;
2262
2263 bfd *obfd = objfile->obfd;
2264
2265 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2266 locate_sections (obfd, sec, *names);
2267}
2268
2269dwarf2_per_objfile::~dwarf2_per_objfile ()
2270{
2271 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2272 free_cached_comp_units ();
2273
2274 if (quick_file_names_table)
2275 htab_delete (quick_file_names_table);
2276
2277 if (line_header_hash)
2278 htab_delete (line_header_hash);
2279
2280 /* Everything else should be on the objfile obstack. */
2281}
2282
2283/* See declaration. */
2284
2285void
2286dwarf2_per_objfile::free_cached_comp_units ()
2287{
2288 dwarf2_per_cu_data *per_cu = read_in_chain;
2289 dwarf2_per_cu_data **last_chain = &read_in_chain;
2290 while (per_cu != NULL)
2291 {
2292 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2293
2294 free_heap_comp_unit (per_cu->cu);
2295 *last_chain = next_cu;
2296 per_cu = next_cu;
2297 }
2298}
2299
c906108c 2300/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2301 information and return true if we have enough to do something.
2302 NAMES points to the dwarf2 section names, or is NULL if the standard
2303 ELF names are used. */
c906108c
SS
2304
2305int
251d32d9
TG
2306dwarf2_has_info (struct objfile *objfile,
2307 const struct dwarf2_debug_sections *names)
c906108c 2308{
9a3c8263
SM
2309 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2310 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2311 if (!dwarf2_per_objfile)
2312 {
2313 /* Initialize per-objfile state. */
2314 struct dwarf2_per_objfile *data
8d749320 2315 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2316
330cdd98
PA
2317 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2318 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2319 }
73869dc2 2320 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2321 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2322 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2323 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2324}
2325
2326/* Return the containing section of virtual section SECTION. */
2327
2328static struct dwarf2_section_info *
2329get_containing_section (const struct dwarf2_section_info *section)
2330{
2331 gdb_assert (section->is_virtual);
2332 return section->s.containing_section;
c906108c
SS
2333}
2334
a32a8923
DE
2335/* Return the bfd owner of SECTION. */
2336
2337static struct bfd *
2338get_section_bfd_owner (const struct dwarf2_section_info *section)
2339{
73869dc2
DE
2340 if (section->is_virtual)
2341 {
2342 section = get_containing_section (section);
2343 gdb_assert (!section->is_virtual);
2344 }
049412e3 2345 return section->s.section->owner;
a32a8923
DE
2346}
2347
2348/* Return the bfd section of SECTION.
2349 Returns NULL if the section is not present. */
2350
2351static asection *
2352get_section_bfd_section (const struct dwarf2_section_info *section)
2353{
73869dc2
DE
2354 if (section->is_virtual)
2355 {
2356 section = get_containing_section (section);
2357 gdb_assert (!section->is_virtual);
2358 }
049412e3 2359 return section->s.section;
a32a8923
DE
2360}
2361
2362/* Return the name of SECTION. */
2363
2364static const char *
2365get_section_name (const struct dwarf2_section_info *section)
2366{
2367 asection *sectp = get_section_bfd_section (section);
2368
2369 gdb_assert (sectp != NULL);
2370 return bfd_section_name (get_section_bfd_owner (section), sectp);
2371}
2372
2373/* Return the name of the file SECTION is in. */
2374
2375static const char *
2376get_section_file_name (const struct dwarf2_section_info *section)
2377{
2378 bfd *abfd = get_section_bfd_owner (section);
2379
2380 return bfd_get_filename (abfd);
2381}
2382
2383/* Return the id of SECTION.
2384 Returns 0 if SECTION doesn't exist. */
2385
2386static int
2387get_section_id (const struct dwarf2_section_info *section)
2388{
2389 asection *sectp = get_section_bfd_section (section);
2390
2391 if (sectp == NULL)
2392 return 0;
2393 return sectp->id;
2394}
2395
2396/* Return the flags of SECTION.
73869dc2 2397 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2398
2399static int
2400get_section_flags (const struct dwarf2_section_info *section)
2401{
2402 asection *sectp = get_section_bfd_section (section);
2403
2404 gdb_assert (sectp != NULL);
2405 return bfd_get_section_flags (sectp->owner, sectp);
2406}
2407
251d32d9
TG
2408/* When loading sections, we look either for uncompressed section or for
2409 compressed section names. */
233a11ab
CS
2410
2411static int
251d32d9
TG
2412section_is_p (const char *section_name,
2413 const struct dwarf2_section_names *names)
233a11ab 2414{
251d32d9
TG
2415 if (names->normal != NULL
2416 && strcmp (section_name, names->normal) == 0)
2417 return 1;
2418 if (names->compressed != NULL
2419 && strcmp (section_name, names->compressed) == 0)
2420 return 1;
2421 return 0;
233a11ab
CS
2422}
2423
330cdd98 2424/* See declaration. */
c906108c 2425
330cdd98
PA
2426void
2427dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2428 const dwarf2_debug_sections &names)
c906108c 2429{
dc7650b8 2430 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2431
dc7650b8
JK
2432 if ((aflag & SEC_HAS_CONTENTS) == 0)
2433 {
2434 }
330cdd98 2435 else if (section_is_p (sectp->name, &names.info))
c906108c 2436 {
330cdd98
PA
2437 this->info.s.section = sectp;
2438 this->info.size = bfd_get_section_size (sectp);
c906108c 2439 }
330cdd98 2440 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2441 {
330cdd98
PA
2442 this->abbrev.s.section = sectp;
2443 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2444 }
330cdd98 2445 else if (section_is_p (sectp->name, &names.line))
c906108c 2446 {
330cdd98
PA
2447 this->line.s.section = sectp;
2448 this->line.size = bfd_get_section_size (sectp);
c906108c 2449 }
330cdd98 2450 else if (section_is_p (sectp->name, &names.loc))
c906108c 2451 {
330cdd98
PA
2452 this->loc.s.section = sectp;
2453 this->loc.size = bfd_get_section_size (sectp);
c906108c 2454 }
330cdd98 2455 else if (section_is_p (sectp->name, &names.loclists))
43988095 2456 {
330cdd98
PA
2457 this->loclists.s.section = sectp;
2458 this->loclists.size = bfd_get_section_size (sectp);
43988095 2459 }
330cdd98 2460 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2461 {
330cdd98
PA
2462 this->macinfo.s.section = sectp;
2463 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2464 }
330cdd98 2465 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2466 {
330cdd98
PA
2467 this->macro.s.section = sectp;
2468 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2469 }
330cdd98 2470 else if (section_is_p (sectp->name, &names.str))
c906108c 2471 {
330cdd98
PA
2472 this->str.s.section = sectp;
2473 this->str.size = bfd_get_section_size (sectp);
c906108c 2474 }
330cdd98 2475 else if (section_is_p (sectp->name, &names.line_str))
43988095 2476 {
330cdd98
PA
2477 this->line_str.s.section = sectp;
2478 this->line_str.size = bfd_get_section_size (sectp);
43988095 2479 }
330cdd98 2480 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2481 {
330cdd98
PA
2482 this->addr.s.section = sectp;
2483 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2484 }
330cdd98 2485 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2486 {
330cdd98
PA
2487 this->frame.s.section = sectp;
2488 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2489 }
330cdd98 2490 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2491 {
330cdd98
PA
2492 this->eh_frame.s.section = sectp;
2493 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2494 }
330cdd98 2495 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2496 {
330cdd98
PA
2497 this->ranges.s.section = sectp;
2498 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2499 }
330cdd98 2500 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2501 {
330cdd98
PA
2502 this->rnglists.s.section = sectp;
2503 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2504 }
330cdd98 2505 else if (section_is_p (sectp->name, &names.types))
348e048f 2506 {
8b70b953
TT
2507 struct dwarf2_section_info type_section;
2508
2509 memset (&type_section, 0, sizeof (type_section));
049412e3 2510 type_section.s.section = sectp;
8b70b953
TT
2511 type_section.size = bfd_get_section_size (sectp);
2512
330cdd98 2513 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2514 &type_section);
348e048f 2515 }
330cdd98 2516 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2517 {
330cdd98
PA
2518 this->gdb_index.s.section = sectp;
2519 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2520 }
dce234bc 2521
b4e1fd61 2522 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2523 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2524 this->has_section_at_zero = true;
c906108c
SS
2525}
2526
fceca515
DE
2527/* A helper function that decides whether a section is empty,
2528 or not present. */
9e0ac564
TT
2529
2530static int
19ac8c2e 2531dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2532{
73869dc2
DE
2533 if (section->is_virtual)
2534 return section->size == 0;
049412e3 2535 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2536}
2537
3019eac3
DE
2538/* Read the contents of the section INFO.
2539 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2540 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2541 of the DWO file.
dce234bc 2542 If the section is compressed, uncompress it before returning. */
c906108c 2543
dce234bc
PP
2544static void
2545dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2546{
a32a8923 2547 asection *sectp;
3019eac3 2548 bfd *abfd;
dce234bc 2549 gdb_byte *buf, *retbuf;
c906108c 2550
be391dca
TT
2551 if (info->readin)
2552 return;
dce234bc 2553 info->buffer = NULL;
be391dca 2554 info->readin = 1;
188dd5d6 2555
9e0ac564 2556 if (dwarf2_section_empty_p (info))
dce234bc 2557 return;
c906108c 2558
a32a8923 2559 sectp = get_section_bfd_section (info);
3019eac3 2560
73869dc2
DE
2561 /* If this is a virtual section we need to read in the real one first. */
2562 if (info->is_virtual)
2563 {
2564 struct dwarf2_section_info *containing_section =
2565 get_containing_section (info);
2566
2567 gdb_assert (sectp != NULL);
2568 if ((sectp->flags & SEC_RELOC) != 0)
2569 {
2570 error (_("Dwarf Error: DWP format V2 with relocations is not"
2571 " supported in section %s [in module %s]"),
2572 get_section_name (info), get_section_file_name (info));
2573 }
2574 dwarf2_read_section (objfile, containing_section);
2575 /* Other code should have already caught virtual sections that don't
2576 fit. */
2577 gdb_assert (info->virtual_offset + info->size
2578 <= containing_section->size);
2579 /* If the real section is empty or there was a problem reading the
2580 section we shouldn't get here. */
2581 gdb_assert (containing_section->buffer != NULL);
2582 info->buffer = containing_section->buffer + info->virtual_offset;
2583 return;
2584 }
2585
4bf44c1c
TT
2586 /* If the section has relocations, we must read it ourselves.
2587 Otherwise we attach it to the BFD. */
2588 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2589 {
d521ce57 2590 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2591 return;
dce234bc 2592 }
dce234bc 2593
224c3ddb 2594 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2595 info->buffer = buf;
dce234bc
PP
2596
2597 /* When debugging .o files, we may need to apply relocations; see
2598 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2599 We never compress sections in .o files, so we only need to
2600 try this when the section is not compressed. */
ac8035ab 2601 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2602 if (retbuf != NULL)
2603 {
2604 info->buffer = retbuf;
2605 return;
2606 }
2607
a32a8923
DE
2608 abfd = get_section_bfd_owner (info);
2609 gdb_assert (abfd != NULL);
2610
dce234bc
PP
2611 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2612 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2613 {
2614 error (_("Dwarf Error: Can't read DWARF data"
2615 " in section %s [in module %s]"),
2616 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2617 }
dce234bc
PP
2618}
2619
9e0ac564
TT
2620/* A helper function that returns the size of a section in a safe way.
2621 If you are positive that the section has been read before using the
2622 size, then it is safe to refer to the dwarf2_section_info object's
2623 "size" field directly. In other cases, you must call this
2624 function, because for compressed sections the size field is not set
2625 correctly until the section has been read. */
2626
2627static bfd_size_type
2628dwarf2_section_size (struct objfile *objfile,
2629 struct dwarf2_section_info *info)
2630{
2631 if (!info->readin)
2632 dwarf2_read_section (objfile, info);
2633 return info->size;
2634}
2635
dce234bc 2636/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2637 SECTION_NAME. */
af34e669 2638
dce234bc 2639void
3017a003
TG
2640dwarf2_get_section_info (struct objfile *objfile,
2641 enum dwarf2_section_enum sect,
d521ce57 2642 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2643 bfd_size_type *sizep)
2644{
2645 struct dwarf2_per_objfile *data
9a3c8263
SM
2646 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2647 dwarf2_objfile_data_key);
dce234bc 2648 struct dwarf2_section_info *info;
a3b2a86b
TT
2649
2650 /* We may see an objfile without any DWARF, in which case we just
2651 return nothing. */
2652 if (data == NULL)
2653 {
2654 *sectp = NULL;
2655 *bufp = NULL;
2656 *sizep = 0;
2657 return;
2658 }
3017a003
TG
2659 switch (sect)
2660 {
2661 case DWARF2_DEBUG_FRAME:
2662 info = &data->frame;
2663 break;
2664 case DWARF2_EH_FRAME:
2665 info = &data->eh_frame;
2666 break;
2667 default:
2668 gdb_assert_not_reached ("unexpected section");
2669 }
dce234bc 2670
9e0ac564 2671 dwarf2_read_section (objfile, info);
dce234bc 2672
a32a8923 2673 *sectp = get_section_bfd_section (info);
dce234bc
PP
2674 *bufp = info->buffer;
2675 *sizep = info->size;
2676}
2677
36586728
TT
2678/* A helper function to find the sections for a .dwz file. */
2679
2680static void
2681locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2682{
9a3c8263 2683 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2684
2685 /* Note that we only support the standard ELF names, because .dwz
2686 is ELF-only (at the time of writing). */
2687 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2688 {
049412e3 2689 dwz_file->abbrev.s.section = sectp;
36586728
TT
2690 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2691 }
2692 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2693 {
049412e3 2694 dwz_file->info.s.section = sectp;
36586728
TT
2695 dwz_file->info.size = bfd_get_section_size (sectp);
2696 }
2697 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2698 {
049412e3 2699 dwz_file->str.s.section = sectp;
36586728
TT
2700 dwz_file->str.size = bfd_get_section_size (sectp);
2701 }
2702 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2703 {
049412e3 2704 dwz_file->line.s.section = sectp;
36586728
TT
2705 dwz_file->line.size = bfd_get_section_size (sectp);
2706 }
2707 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2708 {
049412e3 2709 dwz_file->macro.s.section = sectp;
36586728
TT
2710 dwz_file->macro.size = bfd_get_section_size (sectp);
2711 }
2ec9a5e0
TT
2712 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2713 {
049412e3 2714 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2715 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2716 }
36586728
TT
2717}
2718
4db1a1dc
TT
2719/* Open the separate '.dwz' debug file, if needed. Return NULL if
2720 there is no .gnu_debugaltlink section in the file. Error if there
2721 is such a section but the file cannot be found. */
36586728
TT
2722
2723static struct dwz_file *
2724dwarf2_get_dwz_file (void)
2725{
36586728
TT
2726 const char *filename;
2727 struct dwz_file *result;
acd13123 2728 bfd_size_type buildid_len_arg;
dc294be5
TT
2729 size_t buildid_len;
2730 bfd_byte *buildid;
36586728
TT
2731
2732 if (dwarf2_per_objfile->dwz_file != NULL)
2733 return dwarf2_per_objfile->dwz_file;
2734
4db1a1dc 2735 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2736 gdb::unique_xmalloc_ptr<char> data
2737 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2738 &buildid_len_arg, &buildid));
4db1a1dc
TT
2739 if (data == NULL)
2740 {
2741 if (bfd_get_error () == bfd_error_no_error)
2742 return NULL;
2743 error (_("could not read '.gnu_debugaltlink' section: %s"),
2744 bfd_errmsg (bfd_get_error ()));
2745 }
791afaa2
TT
2746
2747 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2748
acd13123
TT
2749 buildid_len = (size_t) buildid_len_arg;
2750
791afaa2 2751 filename = data.get ();
d721ba37
PA
2752
2753 std::string abs_storage;
36586728
TT
2754 if (!IS_ABSOLUTE_PATH (filename))
2755 {
14278e1f
TT
2756 gdb::unique_xmalloc_ptr<char> abs
2757 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2758
14278e1f 2759 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2760 filename = abs_storage.c_str ();
36586728
TT
2761 }
2762
dc294be5
TT
2763 /* First try the file name given in the section. If that doesn't
2764 work, try to use the build-id instead. */
192b62ce 2765 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2766 if (dwz_bfd != NULL)
36586728 2767 {
192b62ce
TT
2768 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2769 dwz_bfd.release ();
36586728
TT
2770 }
2771
dc294be5
TT
2772 if (dwz_bfd == NULL)
2773 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2774
2775 if (dwz_bfd == NULL)
2776 error (_("could not find '.gnu_debugaltlink' file for %s"),
2777 objfile_name (dwarf2_per_objfile->objfile));
2778
36586728
TT
2779 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2780 struct dwz_file);
192b62ce 2781 result->dwz_bfd = dwz_bfd.release ();
36586728 2782
192b62ce 2783 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2784
192b62ce 2785 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2786 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2787 return result;
2788}
9291a0cd 2789\f
7b9f3c50
DE
2790/* DWARF quick_symbols_functions support. */
2791
2792/* TUs can share .debug_line entries, and there can be a lot more TUs than
2793 unique line tables, so we maintain a separate table of all .debug_line
2794 derived entries to support the sharing.
2795 All the quick functions need is the list of file names. We discard the
2796 line_header when we're done and don't need to record it here. */
2797struct quick_file_names
2798{
094b34ac
DE
2799 /* The data used to construct the hash key. */
2800 struct stmt_list_hash hash;
7b9f3c50
DE
2801
2802 /* The number of entries in file_names, real_names. */
2803 unsigned int num_file_names;
2804
2805 /* The file names from the line table, after being run through
2806 file_full_name. */
2807 const char **file_names;
2808
2809 /* The file names from the line table after being run through
2810 gdb_realpath. These are computed lazily. */
2811 const char **real_names;
2812};
2813
2814/* When using the index (and thus not using psymtabs), each CU has an
2815 object of this type. This is used to hold information needed by
2816 the various "quick" methods. */
2817struct dwarf2_per_cu_quick_data
2818{
2819 /* The file table. This can be NULL if there was no file table
2820 or it's currently not read in.
2821 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2822 struct quick_file_names *file_names;
2823
2824 /* The corresponding symbol table. This is NULL if symbols for this
2825 CU have not yet been read. */
43f3e411 2826 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2827
2828 /* A temporary mark bit used when iterating over all CUs in
2829 expand_symtabs_matching. */
2830 unsigned int mark : 1;
2831
2832 /* True if we've tried to read the file table and found there isn't one.
2833 There will be no point in trying to read it again next time. */
2834 unsigned int no_file_data : 1;
2835};
2836
094b34ac
DE
2837/* Utility hash function for a stmt_list_hash. */
2838
2839static hashval_t
2840hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2841{
2842 hashval_t v = 0;
2843
2844 if (stmt_list_hash->dwo_unit != NULL)
2845 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2846 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2847 return v;
2848}
2849
2850/* Utility equality function for a stmt_list_hash. */
2851
2852static int
2853eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2854 const struct stmt_list_hash *rhs)
2855{
2856 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2857 return 0;
2858 if (lhs->dwo_unit != NULL
2859 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2860 return 0;
2861
9c541725 2862 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2863}
2864
7b9f3c50
DE
2865/* Hash function for a quick_file_names. */
2866
2867static hashval_t
2868hash_file_name_entry (const void *e)
2869{
9a3c8263
SM
2870 const struct quick_file_names *file_data
2871 = (const struct quick_file_names *) e;
7b9f3c50 2872
094b34ac 2873 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2874}
2875
2876/* Equality function for a quick_file_names. */
2877
2878static int
2879eq_file_name_entry (const void *a, const void *b)
2880{
9a3c8263
SM
2881 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2882 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2883
094b34ac 2884 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2885}
2886
2887/* Delete function for a quick_file_names. */
2888
2889static void
2890delete_file_name_entry (void *e)
2891{
9a3c8263 2892 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2893 int i;
2894
2895 for (i = 0; i < file_data->num_file_names; ++i)
2896 {
2897 xfree ((void*) file_data->file_names[i]);
2898 if (file_data->real_names)
2899 xfree ((void*) file_data->real_names[i]);
2900 }
2901
2902 /* The space for the struct itself lives on objfile_obstack,
2903 so we don't free it here. */
2904}
2905
2906/* Create a quick_file_names hash table. */
2907
2908static htab_t
2909create_quick_file_names_table (unsigned int nr_initial_entries)
2910{
2911 return htab_create_alloc (nr_initial_entries,
2912 hash_file_name_entry, eq_file_name_entry,
2913 delete_file_name_entry, xcalloc, xfree);
2914}
9291a0cd 2915
918dd910
JK
2916/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2917 have to be created afterwards. You should call age_cached_comp_units after
2918 processing PER_CU->CU. dw2_setup must have been already called. */
2919
2920static void
2921load_cu (struct dwarf2_per_cu_data *per_cu)
2922{
3019eac3 2923 if (per_cu->is_debug_types)
e5fe5e75 2924 load_full_type_unit (per_cu);
918dd910 2925 else
95554aad 2926 load_full_comp_unit (per_cu, language_minimal);
918dd910 2927
cc12ce38
DE
2928 if (per_cu->cu == NULL)
2929 return; /* Dummy CU. */
2dc860c0
DE
2930
2931 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2932}
2933
a0f42c21 2934/* Read in the symbols for PER_CU. */
2fdf6df6 2935
9291a0cd 2936static void
a0f42c21 2937dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2938{
2939 struct cleanup *back_to;
2940
f4dc4d17
DE
2941 /* Skip type_unit_groups, reading the type units they contain
2942 is handled elsewhere. */
2943 if (IS_TYPE_UNIT_GROUP (per_cu))
2944 return;
2945
9291a0cd
TT
2946 back_to = make_cleanup (dwarf2_release_queue, NULL);
2947
95554aad 2948 if (dwarf2_per_objfile->using_index
43f3e411 2949 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2950 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2951 {
2952 queue_comp_unit (per_cu, language_minimal);
2953 load_cu (per_cu);
89e63ee4
DE
2954
2955 /* If we just loaded a CU from a DWO, and we're working with an index
2956 that may badly handle TUs, load all the TUs in that DWO as well.
2957 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2958 if (!per_cu->is_debug_types
cc12ce38 2959 && per_cu->cu != NULL
89e63ee4
DE
2960 && per_cu->cu->dwo_unit != NULL
2961 && dwarf2_per_objfile->index_table != NULL
2962 && dwarf2_per_objfile->index_table->version <= 7
2963 /* DWP files aren't supported yet. */
2964 && get_dwp_file () == NULL)
2965 queue_and_load_all_dwo_tus (per_cu);
95554aad 2966 }
9291a0cd 2967
a0f42c21 2968 process_queue ();
9291a0cd
TT
2969
2970 /* Age the cache, releasing compilation units that have not
2971 been used recently. */
2972 age_cached_comp_units ();
2973
2974 do_cleanups (back_to);
2975}
2976
2977/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2978 the objfile from which this CU came. Returns the resulting symbol
2979 table. */
2fdf6df6 2980
43f3e411 2981static struct compunit_symtab *
a0f42c21 2982dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2983{
95554aad 2984 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2985 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2986 {
2987 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2988 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2989 dw2_do_instantiate_symtab (per_cu);
95554aad 2990 process_cu_includes ();
9291a0cd
TT
2991 do_cleanups (back_to);
2992 }
f194fefb 2993
43f3e411 2994 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2995}
2996
8832e7e3 2997/* Return the CU/TU given its index.
f4dc4d17
DE
2998
2999 This is intended for loops like:
3000
3001 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3002 + dwarf2_per_objfile->n_type_units); ++i)
3003 {
8832e7e3 3004 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3005
3006 ...;
3007 }
3008*/
2fdf6df6 3009
1fd400ff 3010static struct dwarf2_per_cu_data *
8832e7e3 3011dw2_get_cutu (int index)
1fd400ff
TT
3012{
3013 if (index >= dwarf2_per_objfile->n_comp_units)
3014 {
f4dc4d17 3015 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3016 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3017 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3018 }
3019
3020 return dwarf2_per_objfile->all_comp_units[index];
3021}
3022
8832e7e3
DE
3023/* Return the CU given its index.
3024 This differs from dw2_get_cutu in that it's for when you know INDEX
3025 refers to a CU. */
f4dc4d17
DE
3026
3027static struct dwarf2_per_cu_data *
8832e7e3 3028dw2_get_cu (int index)
f4dc4d17 3029{
8832e7e3 3030 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3031
1fd400ff
TT
3032 return dwarf2_per_objfile->all_comp_units[index];
3033}
3034
2ec9a5e0
TT
3035/* A helper for create_cus_from_index that handles a given list of
3036 CUs. */
2fdf6df6 3037
74a0d9f6 3038static void
2ec9a5e0
TT
3039create_cus_from_index_list (struct objfile *objfile,
3040 const gdb_byte *cu_list, offset_type n_elements,
3041 struct dwarf2_section_info *section,
3042 int is_dwz,
3043 int base_offset)
9291a0cd
TT
3044{
3045 offset_type i;
9291a0cd 3046
2ec9a5e0 3047 for (i = 0; i < n_elements; i += 2)
9291a0cd 3048 {
74a0d9f6 3049 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3050
3051 sect_offset sect_off
3052 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3053 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3054 cu_list += 2 * 8;
3055
9c541725
PA
3056 dwarf2_per_cu_data *the_cu
3057 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3058 struct dwarf2_per_cu_data);
3059 the_cu->sect_off = sect_off;
9291a0cd
TT
3060 the_cu->length = length;
3061 the_cu->objfile = objfile;
8a0459fd 3062 the_cu->section = section;
9291a0cd
TT
3063 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3064 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3065 the_cu->is_dwz = is_dwz;
3066 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3067 }
9291a0cd
TT
3068}
3069
2ec9a5e0 3070/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3071 the CU objects for this objfile. */
2ec9a5e0 3072
74a0d9f6 3073static void
2ec9a5e0
TT
3074create_cus_from_index (struct objfile *objfile,
3075 const gdb_byte *cu_list, offset_type cu_list_elements,
3076 const gdb_byte *dwz_list, offset_type dwz_elements)
3077{
3078 struct dwz_file *dwz;
3079
3080 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3081 dwarf2_per_objfile->all_comp_units =
3082 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3083 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3084
74a0d9f6
JK
3085 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3086 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3087
3088 if (dwz_elements == 0)
74a0d9f6 3089 return;
2ec9a5e0
TT
3090
3091 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3092 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3093 cu_list_elements / 2);
2ec9a5e0
TT
3094}
3095
1fd400ff 3096/* Create the signatured type hash table from the index. */
673bfd45 3097
74a0d9f6 3098static void
673bfd45 3099create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3100 struct dwarf2_section_info *section,
673bfd45
DE
3101 const gdb_byte *bytes,
3102 offset_type elements)
1fd400ff
TT
3103{
3104 offset_type i;
673bfd45 3105 htab_t sig_types_hash;
1fd400ff 3106
6aa5f3a6
DE
3107 dwarf2_per_objfile->n_type_units
3108 = dwarf2_per_objfile->n_allocated_type_units
3109 = elements / 3;
8d749320
SM
3110 dwarf2_per_objfile->all_type_units =
3111 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3112
673bfd45 3113 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3114
3115 for (i = 0; i < elements; i += 3)
3116 {
52dc124a 3117 struct signatured_type *sig_type;
9c541725 3118 ULONGEST signature;
1fd400ff 3119 void **slot;
9c541725 3120 cu_offset type_offset_in_tu;
1fd400ff 3121
74a0d9f6 3122 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3123 sect_offset sect_off
3124 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3125 type_offset_in_tu
3126 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3127 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3128 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3129 bytes += 3 * 8;
3130
52dc124a 3131 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3132 struct signatured_type);
52dc124a 3133 sig_type->signature = signature;
9c541725 3134 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3135 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3136 sig_type->per_cu.section = section;
9c541725 3137 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3138 sig_type->per_cu.objfile = objfile;
3139 sig_type->per_cu.v.quick
1fd400ff
TT
3140 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3141 struct dwarf2_per_cu_quick_data);
3142
52dc124a
DE
3143 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3144 *slot = sig_type;
1fd400ff 3145
b4dd5633 3146 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3147 }
3148
673bfd45 3149 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3150}
3151
9291a0cd
TT
3152/* Read the address map data from the mapped index, and use it to
3153 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3154
9291a0cd
TT
3155static void
3156create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3157{
3e29f34a 3158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3159 const gdb_byte *iter, *end;
9291a0cd 3160 struct addrmap *mutable_map;
9291a0cd
TT
3161 CORE_ADDR baseaddr;
3162
8268c778
PA
3163 auto_obstack temp_obstack;
3164
9291a0cd
TT
3165 mutable_map = addrmap_create_mutable (&temp_obstack);
3166
3167 iter = index->address_table;
3168 end = iter + index->address_table_size;
3169
3170 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3171
3172 while (iter < end)
3173 {
3174 ULONGEST hi, lo, cu_index;
3175 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3176 iter += 8;
3177 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3178 iter += 8;
3179 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3180 iter += 4;
f652bce2 3181
24a55014 3182 if (lo > hi)
f652bce2 3183 {
24a55014
DE
3184 complaint (&symfile_complaints,
3185 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3186 hex_string (lo), hex_string (hi));
24a55014 3187 continue;
f652bce2 3188 }
24a55014
DE
3189
3190 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3191 {
3192 complaint (&symfile_complaints,
3193 _(".gdb_index address table has invalid CU number %u"),
3194 (unsigned) cu_index);
24a55014 3195 continue;
f652bce2 3196 }
24a55014 3197
3e29f34a
MR
3198 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3199 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3200 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3201 }
3202
3203 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3204 &objfile->objfile_obstack);
9291a0cd
TT
3205}
3206
59d7bcaf
JK
3207/* The hash function for strings in the mapped index. This is the same as
3208 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3209 implementation. This is necessary because the hash function is tied to the
3210 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3211 SYMBOL_HASH_NEXT.
3212
3213 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3214
9291a0cd 3215static hashval_t
559a7a62 3216mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3217{
3218 const unsigned char *str = (const unsigned char *) p;
3219 hashval_t r = 0;
3220 unsigned char c;
3221
3222 while ((c = *str++) != 0)
559a7a62
JK
3223 {
3224 if (index_version >= 5)
3225 c = tolower (c);
3226 r = r * 67 + c - 113;
3227 }
9291a0cd
TT
3228
3229 return r;
3230}
3231
3232/* Find a slot in the mapped index INDEX for the object named NAME.
3233 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3234 constant pool and return true. If NAME cannot be found, return
3235 false. */
2fdf6df6 3236
109483d9 3237static bool
9291a0cd
TT
3238find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3239 offset_type **vec_out)
3240{
0cf03b49 3241 offset_type hash;
9291a0cd 3242 offset_type slot, step;
559a7a62 3243 int (*cmp) (const char *, const char *);
9291a0cd 3244
791afaa2 3245 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3246 if (current_language->la_language == language_cplus
45280282
IB
3247 || current_language->la_language == language_fortran
3248 || current_language->la_language == language_d)
0cf03b49
JK
3249 {
3250 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3251 not contain any. */
a8719064 3252
72998fb3 3253 if (strchr (name, '(') != NULL)
0cf03b49 3254 {
109483d9 3255 without_params = cp_remove_params (name);
0cf03b49 3256
72998fb3 3257 if (without_params != NULL)
791afaa2 3258 name = without_params.get ();
0cf03b49
JK
3259 }
3260 }
3261
559a7a62 3262 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3263 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3264 simulate our NAME being searched is also lowercased. */
3265 hash = mapped_index_string_hash ((index->version == 4
3266 && case_sensitivity == case_sensitive_off
3267 ? 5 : index->version),
3268 name);
3269
3876f04e
DE
3270 slot = hash & (index->symbol_table_slots - 1);
3271 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3272 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3273
3274 for (;;)
3275 {
3276 /* Convert a slot number to an offset into the table. */
3277 offset_type i = 2 * slot;
3278 const char *str;
3876f04e 3279 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3280 return false;
9291a0cd 3281
3876f04e 3282 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3283 if (!cmp (name, str))
9291a0cd
TT
3284 {
3285 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3286 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3287 return true;
9291a0cd
TT
3288 }
3289
3876f04e 3290 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3291 }
3292}
3293
2ec9a5e0
TT
3294/* A helper function that reads the .gdb_index from SECTION and fills
3295 in MAP. FILENAME is the name of the file containing the section;
3296 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3297 ok to use deprecated sections.
3298
3299 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3300 out parameters that are filled in with information about the CU and
3301 TU lists in the section.
3302
3303 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3304
9291a0cd 3305static int
2ec9a5e0
TT
3306read_index_from_section (struct objfile *objfile,
3307 const char *filename,
3308 int deprecated_ok,
3309 struct dwarf2_section_info *section,
3310 struct mapped_index *map,
3311 const gdb_byte **cu_list,
3312 offset_type *cu_list_elements,
3313 const gdb_byte **types_list,
3314 offset_type *types_list_elements)
9291a0cd 3315{
948f8e3d 3316 const gdb_byte *addr;
2ec9a5e0 3317 offset_type version;
b3b272e1 3318 offset_type *metadata;
1fd400ff 3319 int i;
9291a0cd 3320
2ec9a5e0 3321 if (dwarf2_section_empty_p (section))
9291a0cd 3322 return 0;
82430852
JK
3323
3324 /* Older elfutils strip versions could keep the section in the main
3325 executable while splitting it for the separate debug info file. */
a32a8923 3326 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3327 return 0;
3328
2ec9a5e0 3329 dwarf2_read_section (objfile, section);
9291a0cd 3330
2ec9a5e0 3331 addr = section->buffer;
9291a0cd 3332 /* Version check. */
1fd400ff 3333 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3334 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3335 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3336 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3337 indices. */
831adc1f 3338 if (version < 4)
481860b3
GB
3339 {
3340 static int warning_printed = 0;
3341 if (!warning_printed)
3342 {
3343 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3344 filename);
481860b3
GB
3345 warning_printed = 1;
3346 }
3347 return 0;
3348 }
3349 /* Index version 4 uses a different hash function than index version
3350 5 and later.
3351
3352 Versions earlier than 6 did not emit psymbols for inlined
3353 functions. Using these files will cause GDB not to be able to
3354 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3355 indices unless the user has done
3356 "set use-deprecated-index-sections on". */
2ec9a5e0 3357 if (version < 6 && !deprecated_ok)
481860b3
GB
3358 {
3359 static int warning_printed = 0;
3360 if (!warning_printed)
3361 {
e615022a
DE
3362 warning (_("\
3363Skipping deprecated .gdb_index section in %s.\n\
3364Do \"set use-deprecated-index-sections on\" before the file is read\n\
3365to use the section anyway."),
2ec9a5e0 3366 filename);
481860b3
GB
3367 warning_printed = 1;
3368 }
3369 return 0;
3370 }
796a7ff8 3371 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3372 of the TU (for symbols coming from TUs),
3373 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3374 Plus gold-generated indices can have duplicate entries for global symbols,
3375 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3376 These are just performance bugs, and we can't distinguish gdb-generated
3377 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3378
481860b3 3379 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3380 longer backward compatible. */
796a7ff8 3381 if (version > 8)
594e8718 3382 return 0;
9291a0cd 3383
559a7a62 3384 map->version = version;
2ec9a5e0 3385 map->total_size = section->size;
9291a0cd
TT
3386
3387 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3388
3389 i = 0;
2ec9a5e0
TT
3390 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3391 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3392 / 8);
1fd400ff
TT
3393 ++i;
3394
2ec9a5e0
TT
3395 *types_list = addr + MAYBE_SWAP (metadata[i]);
3396 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3397 - MAYBE_SWAP (metadata[i]))
3398 / 8);
987d643c 3399 ++i;
1fd400ff
TT
3400
3401 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3402 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3403 - MAYBE_SWAP (metadata[i]));
3404 ++i;
3405
3876f04e
DE
3406 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3407 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3408 - MAYBE_SWAP (metadata[i]))
3409 / (2 * sizeof (offset_type)));
1fd400ff 3410 ++i;
9291a0cd 3411
f9d83a0b 3412 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3413
2ec9a5e0
TT
3414 return 1;
3415}
3416
3417
3418/* Read the index file. If everything went ok, initialize the "quick"
3419 elements of all the CUs and return 1. Otherwise, return 0. */
3420
3421static int
3422dwarf2_read_index (struct objfile *objfile)
3423{
3424 struct mapped_index local_map, *map;
3425 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3426 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3427 struct dwz_file *dwz;
2ec9a5e0 3428
4262abfb 3429 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3430 use_deprecated_index_sections,
3431 &dwarf2_per_objfile->gdb_index, &local_map,
3432 &cu_list, &cu_list_elements,
3433 &types_list, &types_list_elements))
3434 return 0;
3435
0fefef59 3436 /* Don't use the index if it's empty. */
2ec9a5e0 3437 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3438 return 0;
3439
2ec9a5e0
TT
3440 /* If there is a .dwz file, read it so we can get its CU list as
3441 well. */
4db1a1dc
TT
3442 dwz = dwarf2_get_dwz_file ();
3443 if (dwz != NULL)
2ec9a5e0 3444 {
2ec9a5e0
TT
3445 struct mapped_index dwz_map;
3446 const gdb_byte *dwz_types_ignore;
3447 offset_type dwz_types_elements_ignore;
3448
3449 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3450 1,
3451 &dwz->gdb_index, &dwz_map,
3452 &dwz_list, &dwz_list_elements,
3453 &dwz_types_ignore,
3454 &dwz_types_elements_ignore))
3455 {
3456 warning (_("could not read '.gdb_index' section from %s; skipping"),
3457 bfd_get_filename (dwz->dwz_bfd));
3458 return 0;
3459 }
3460 }
3461
74a0d9f6
JK
3462 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3463 dwz_list_elements);
1fd400ff 3464
8b70b953
TT
3465 if (types_list_elements)
3466 {
3467 struct dwarf2_section_info *section;
3468
3469 /* We can only handle a single .debug_types when we have an
3470 index. */
3471 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3472 return 0;
3473
3474 section = VEC_index (dwarf2_section_info_def,
3475 dwarf2_per_objfile->types, 0);
3476
74a0d9f6
JK
3477 create_signatured_type_table_from_index (objfile, section, types_list,
3478 types_list_elements);
8b70b953 3479 }
9291a0cd 3480
2ec9a5e0
TT
3481 create_addrmap_from_index (objfile, &local_map);
3482
8d749320 3483 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3484 map = new (map) mapped_index ();
2ec9a5e0 3485 *map = local_map;
9291a0cd
TT
3486
3487 dwarf2_per_objfile->index_table = map;
3488 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3489 dwarf2_per_objfile->quick_file_names_table =
3490 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3491
3492 return 1;
3493}
3494
3495/* A helper for the "quick" functions which sets the global
3496 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3497
9291a0cd
TT
3498static void
3499dw2_setup (struct objfile *objfile)
3500{
9a3c8263
SM
3501 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3502 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3503 gdb_assert (dwarf2_per_objfile);
3504}
3505
dee91e82 3506/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3507
dee91e82
DE
3508static void
3509dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3510 const gdb_byte *info_ptr,
dee91e82
DE
3511 struct die_info *comp_unit_die,
3512 int has_children,
3513 void *data)
9291a0cd 3514{
dee91e82
DE
3515 struct dwarf2_cu *cu = reader->cu;
3516 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3517 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3518 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3519 struct attribute *attr;
dee91e82 3520 int i;
7b9f3c50
DE
3521 void **slot;
3522 struct quick_file_names *qfn;
9291a0cd 3523
0186c6a7
DE
3524 gdb_assert (! this_cu->is_debug_types);
3525
07261596
TT
3526 /* Our callers never want to match partial units -- instead they
3527 will match the enclosing full CU. */
3528 if (comp_unit_die->tag == DW_TAG_partial_unit)
3529 {
3530 this_cu->v.quick->no_file_data = 1;
3531 return;
3532 }
3533
0186c6a7 3534 lh_cu = this_cu;
7b9f3c50 3535 slot = NULL;
dee91e82 3536
fff8551c 3537 line_header_up lh;
9c541725 3538 sect_offset line_offset {};
fff8551c 3539
dee91e82 3540 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3541 if (attr)
3542 {
7b9f3c50
DE
3543 struct quick_file_names find_entry;
3544
9c541725 3545 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3546
3547 /* We may have already read in this line header (TU line header sharing).
3548 If we have we're done. */
094b34ac 3549 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3550 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3551 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3552 &find_entry, INSERT);
3553 if (*slot != NULL)
3554 {
9a3c8263 3555 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3556 return;
7b9f3c50
DE
3557 }
3558
3019eac3 3559 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3560 }
3561 if (lh == NULL)
3562 {
094b34ac 3563 lh_cu->v.quick->no_file_data = 1;
dee91e82 3564 return;
9291a0cd
TT
3565 }
3566
8d749320 3567 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3568 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3569 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3570 gdb_assert (slot != NULL);
3571 *slot = qfn;
9291a0cd 3572
d721ba37 3573 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3574
fff8551c 3575 qfn->num_file_names = lh->file_names.size ();
8d749320 3576 qfn->file_names =
fff8551c
PA
3577 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3578 for (i = 0; i < lh->file_names.size (); ++i)
3579 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3580 qfn->real_names = NULL;
9291a0cd 3581
094b34ac 3582 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3583}
3584
3585/* A helper for the "quick" functions which attempts to read the line
3586 table for THIS_CU. */
3587
3588static struct quick_file_names *
e4a48d9d 3589dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3590{
0186c6a7
DE
3591 /* This should never be called for TUs. */
3592 gdb_assert (! this_cu->is_debug_types);
3593 /* Nor type unit groups. */
3594 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3595
dee91e82
DE
3596 if (this_cu->v.quick->file_names != NULL)
3597 return this_cu->v.quick->file_names;
3598 /* If we know there is no line data, no point in looking again. */
3599 if (this_cu->v.quick->no_file_data)
3600 return NULL;
3601
0186c6a7 3602 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3603
3604 if (this_cu->v.quick->no_file_data)
3605 return NULL;
3606 return this_cu->v.quick->file_names;
9291a0cd
TT
3607}
3608
3609/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3610 real path for a given file name from the line table. */
2fdf6df6 3611
9291a0cd 3612static const char *
7b9f3c50
DE
3613dw2_get_real_path (struct objfile *objfile,
3614 struct quick_file_names *qfn, int index)
9291a0cd 3615{
7b9f3c50
DE
3616 if (qfn->real_names == NULL)
3617 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3618 qfn->num_file_names, const char *);
9291a0cd 3619
7b9f3c50 3620 if (qfn->real_names[index] == NULL)
14278e1f 3621 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3622
7b9f3c50 3623 return qfn->real_names[index];
9291a0cd
TT
3624}
3625
3626static struct symtab *
3627dw2_find_last_source_symtab (struct objfile *objfile)
3628{
43f3e411 3629 struct compunit_symtab *cust;
9291a0cd 3630 int index;
ae2de4f8 3631
9291a0cd
TT
3632 dw2_setup (objfile);
3633 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3634 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3635 if (cust == NULL)
3636 return NULL;
3637 return compunit_primary_filetab (cust);
9291a0cd
TT
3638}
3639
7b9f3c50
DE
3640/* Traversal function for dw2_forget_cached_source_info. */
3641
3642static int
3643dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3644{
7b9f3c50 3645 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3646
7b9f3c50 3647 if (file_data->real_names)
9291a0cd 3648 {
7b9f3c50 3649 int i;
9291a0cd 3650
7b9f3c50 3651 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3652 {
7b9f3c50
DE
3653 xfree ((void*) file_data->real_names[i]);
3654 file_data->real_names[i] = NULL;
9291a0cd
TT
3655 }
3656 }
7b9f3c50
DE
3657
3658 return 1;
3659}
3660
3661static void
3662dw2_forget_cached_source_info (struct objfile *objfile)
3663{
3664 dw2_setup (objfile);
3665
3666 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3667 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3668}
3669
f8eba3c6
TT
3670/* Helper function for dw2_map_symtabs_matching_filename that expands
3671 the symtabs and calls the iterator. */
3672
3673static int
3674dw2_map_expand_apply (struct objfile *objfile,
3675 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3676 const char *name, const char *real_path,
14bc53a8 3677 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3678{
43f3e411 3679 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3680
3681 /* Don't visit already-expanded CUs. */
43f3e411 3682 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3683 return 0;
3684
3685 /* This may expand more than one symtab, and we want to iterate over
3686 all of them. */
a0f42c21 3687 dw2_instantiate_symtab (per_cu);
f8eba3c6 3688
14bc53a8
PA
3689 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3690 last_made, callback);
f8eba3c6
TT
3691}
3692
3693/* Implementation of the map_symtabs_matching_filename method. */
3694
14bc53a8
PA
3695static bool
3696dw2_map_symtabs_matching_filename
3697 (struct objfile *objfile, const char *name, const char *real_path,
3698 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3699{
3700 int i;
c011a4f4 3701 const char *name_basename = lbasename (name);
9291a0cd
TT
3702
3703 dw2_setup (objfile);
ae2de4f8 3704
848e3e78
DE
3705 /* The rule is CUs specify all the files, including those used by
3706 any TU, so there's no need to scan TUs here. */
f4dc4d17 3707
848e3e78 3708 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3709 {
3710 int j;
8832e7e3 3711 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3712 struct quick_file_names *file_data;
9291a0cd 3713
3d7bb9d9 3714 /* We only need to look at symtabs not already expanded. */
43f3e411 3715 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3716 continue;
3717
e4a48d9d 3718 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3719 if (file_data == NULL)
9291a0cd
TT
3720 continue;
3721
7b9f3c50 3722 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3723 {
7b9f3c50 3724 const char *this_name = file_data->file_names[j];
da235a7c 3725 const char *this_real_name;
9291a0cd 3726
af529f8f 3727 if (compare_filenames_for_search (this_name, name))
9291a0cd 3728 {
f5b95b50 3729 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3730 callback))
3731 return true;
288e77a7 3732 continue;
4aac40c8 3733 }
9291a0cd 3734
c011a4f4
DE
3735 /* Before we invoke realpath, which can get expensive when many
3736 files are involved, do a quick comparison of the basenames. */
3737 if (! basenames_may_differ
3738 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3739 continue;
3740
da235a7c
JK
3741 this_real_name = dw2_get_real_path (objfile, file_data, j);
3742 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3743 {
da235a7c 3744 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3745 callback))
3746 return true;
288e77a7 3747 continue;
da235a7c 3748 }
9291a0cd 3749
da235a7c
JK
3750 if (real_path != NULL)
3751 {
af529f8f
JK
3752 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3753 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3754 if (this_real_name != NULL
af529f8f 3755 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3756 {
f5b95b50 3757 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3758 callback))
3759 return true;
288e77a7 3760 continue;
9291a0cd
TT
3761 }
3762 }
3763 }
3764 }
3765
14bc53a8 3766 return false;
9291a0cd
TT
3767}
3768
da51c347
DE
3769/* Struct used to manage iterating over all CUs looking for a symbol. */
3770
3771struct dw2_symtab_iterator
9291a0cd 3772{
da51c347
DE
3773 /* The internalized form of .gdb_index. */
3774 struct mapped_index *index;
3775 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3776 int want_specific_block;
3777 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3778 Unused if !WANT_SPECIFIC_BLOCK. */
3779 int block_index;
3780 /* The kind of symbol we're looking for. */
3781 domain_enum domain;
3782 /* The list of CUs from the index entry of the symbol,
3783 or NULL if not found. */
3784 offset_type *vec;
3785 /* The next element in VEC to look at. */
3786 int next;
3787 /* The number of elements in VEC, or zero if there is no match. */
3788 int length;
8943b874
DE
3789 /* Have we seen a global version of the symbol?
3790 If so we can ignore all further global instances.
3791 This is to work around gold/15646, inefficient gold-generated
3792 indices. */
3793 int global_seen;
da51c347 3794};
9291a0cd 3795
da51c347
DE
3796/* Initialize the index symtab iterator ITER.
3797 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3798 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3799
9291a0cd 3800static void
da51c347
DE
3801dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3802 struct mapped_index *index,
3803 int want_specific_block,
3804 int block_index,
3805 domain_enum domain,
3806 const char *name)
3807{
3808 iter->index = index;
3809 iter->want_specific_block = want_specific_block;
3810 iter->block_index = block_index;
3811 iter->domain = domain;
3812 iter->next = 0;
8943b874 3813 iter->global_seen = 0;
da51c347
DE
3814
3815 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3816 iter->length = MAYBE_SWAP (*iter->vec);
3817 else
3818 {
3819 iter->vec = NULL;
3820 iter->length = 0;
3821 }
3822}
3823
3824/* Return the next matching CU or NULL if there are no more. */
3825
3826static struct dwarf2_per_cu_data *
3827dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3828{
3829 for ( ; iter->next < iter->length; ++iter->next)
3830 {
3831 offset_type cu_index_and_attrs =
3832 MAYBE_SWAP (iter->vec[iter->next + 1]);
3833 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3834 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3835 int want_static = iter->block_index != GLOBAL_BLOCK;
3836 /* This value is only valid for index versions >= 7. */
3837 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3838 gdb_index_symbol_kind symbol_kind =
3839 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3840 /* Only check the symbol attributes if they're present.
3841 Indices prior to version 7 don't record them,
3842 and indices >= 7 may elide them for certain symbols
3843 (gold does this). */
3844 int attrs_valid =
3845 (iter->index->version >= 7
3846 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3847
3190f0c6
DE
3848 /* Don't crash on bad data. */
3849 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3850 + dwarf2_per_objfile->n_type_units))
3851 {
3852 complaint (&symfile_complaints,
3853 _(".gdb_index entry has bad CU index"
4262abfb
JK
3854 " [in module %s]"),
3855 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3856 continue;
3857 }
3858
8832e7e3 3859 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3860
da51c347 3861 /* Skip if already read in. */
43f3e411 3862 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3863 continue;
3864
8943b874
DE
3865 /* Check static vs global. */
3866 if (attrs_valid)
3867 {
3868 if (iter->want_specific_block
3869 && want_static != is_static)
3870 continue;
3871 /* Work around gold/15646. */
3872 if (!is_static && iter->global_seen)
3873 continue;
3874 if (!is_static)
3875 iter->global_seen = 1;
3876 }
da51c347
DE
3877
3878 /* Only check the symbol's kind if it has one. */
3879 if (attrs_valid)
3880 {
3881 switch (iter->domain)
3882 {
3883 case VAR_DOMAIN:
3884 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3885 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3886 /* Some types are also in VAR_DOMAIN. */
3887 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3888 continue;
3889 break;
3890 case STRUCT_DOMAIN:
3891 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3892 continue;
3893 break;
3894 case LABEL_DOMAIN:
3895 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3896 continue;
3897 break;
3898 default:
3899 break;
3900 }
3901 }
3902
3903 ++iter->next;
3904 return per_cu;
3905 }
3906
3907 return NULL;
3908}
3909
43f3e411 3910static struct compunit_symtab *
da51c347
DE
3911dw2_lookup_symbol (struct objfile *objfile, int block_index,
3912 const char *name, domain_enum domain)
9291a0cd 3913{
43f3e411 3914 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3915 struct mapped_index *index;
3916
9291a0cd
TT
3917 dw2_setup (objfile);
3918
b5ec771e
PA
3919 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3920
156942c7
DE
3921 index = dwarf2_per_objfile->index_table;
3922
da51c347 3923 /* index is NULL if OBJF_READNOW. */
156942c7 3924 if (index)
9291a0cd 3925 {
da51c347
DE
3926 struct dw2_symtab_iterator iter;
3927 struct dwarf2_per_cu_data *per_cu;
3928
3929 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3930
da51c347 3931 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3932 {
b2e2f908 3933 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3934 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3935 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3936 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3937
b2e2f908
DE
3938 sym = block_find_symbol (block, name, domain,
3939 block_find_non_opaque_type_preferred,
3940 &with_opaque);
3941
da51c347
DE
3942 /* Some caution must be observed with overloaded functions
3943 and methods, since the index will not contain any overload
3944 information (but NAME might contain it). */
da51c347 3945
b2e2f908 3946 if (sym != NULL
b5ec771e 3947 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3948 return stab;
3949 if (with_opaque != NULL
b5ec771e 3950 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3951 stab_best = stab;
da51c347
DE
3952
3953 /* Keep looking through other CUs. */
9291a0cd
TT
3954 }
3955 }
9291a0cd 3956
da51c347 3957 return stab_best;
9291a0cd
TT
3958}
3959
3960static void
3961dw2_print_stats (struct objfile *objfile)
3962{
e4a48d9d 3963 int i, total, count;
9291a0cd
TT
3964
3965 dw2_setup (objfile);
e4a48d9d 3966 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3967 count = 0;
e4a48d9d 3968 for (i = 0; i < total; ++i)
9291a0cd 3969 {
8832e7e3 3970 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3971
43f3e411 3972 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3973 ++count;
3974 }
e4a48d9d 3975 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3976 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3977}
3978
779bd270
DE
3979/* This dumps minimal information about the index.
3980 It is called via "mt print objfiles".
3981 One use is to verify .gdb_index has been loaded by the
3982 gdb.dwarf2/gdb-index.exp testcase. */
3983
9291a0cd
TT
3984static void
3985dw2_dump (struct objfile *objfile)
3986{
779bd270
DE
3987 dw2_setup (objfile);
3988 gdb_assert (dwarf2_per_objfile->using_index);
3989 printf_filtered (".gdb_index:");
3990 if (dwarf2_per_objfile->index_table != NULL)
3991 {
3992 printf_filtered (" version %d\n",
3993 dwarf2_per_objfile->index_table->version);
3994 }
3995 else
3996 printf_filtered (" faked for \"readnow\"\n");
3997 printf_filtered ("\n");
9291a0cd
TT
3998}
3999
4000static void
3189cb12
DE
4001dw2_relocate (struct objfile *objfile,
4002 const struct section_offsets *new_offsets,
4003 const struct section_offsets *delta)
9291a0cd
TT
4004{
4005 /* There's nothing to relocate here. */
4006}
4007
4008static void
4009dw2_expand_symtabs_for_function (struct objfile *objfile,
4010 const char *func_name)
4011{
da51c347
DE
4012 struct mapped_index *index;
4013
4014 dw2_setup (objfile);
4015
4016 index = dwarf2_per_objfile->index_table;
4017
4018 /* index is NULL if OBJF_READNOW. */
4019 if (index)
4020 {
4021 struct dw2_symtab_iterator iter;
4022 struct dwarf2_per_cu_data *per_cu;
4023
4024 /* Note: It doesn't matter what we pass for block_index here. */
4025 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4026 func_name);
4027
4028 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4029 dw2_instantiate_symtab (per_cu);
4030 }
9291a0cd
TT
4031}
4032
4033static void
4034dw2_expand_all_symtabs (struct objfile *objfile)
4035{
4036 int i;
4037
4038 dw2_setup (objfile);
1fd400ff
TT
4039
4040 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4041 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4042 {
8832e7e3 4043 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4044
a0f42c21 4045 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4046 }
4047}
4048
4049static void
652a8996
JK
4050dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4051 const char *fullname)
9291a0cd
TT
4052{
4053 int i;
4054
4055 dw2_setup (objfile);
d4637a04
DE
4056
4057 /* We don't need to consider type units here.
4058 This is only called for examining code, e.g. expand_line_sal.
4059 There can be an order of magnitude (or more) more type units
4060 than comp units, and we avoid them if we can. */
4061
4062 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4063 {
4064 int j;
8832e7e3 4065 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4066 struct quick_file_names *file_data;
9291a0cd 4067
3d7bb9d9 4068 /* We only need to look at symtabs not already expanded. */
43f3e411 4069 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4070 continue;
4071
e4a48d9d 4072 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4073 if (file_data == NULL)
9291a0cd
TT
4074 continue;
4075
7b9f3c50 4076 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4077 {
652a8996
JK
4078 const char *this_fullname = file_data->file_names[j];
4079
4080 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4081 {
a0f42c21 4082 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4083 break;
4084 }
4085 }
4086 }
4087}
4088
9291a0cd 4089static void
ade7ed9e 4090dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4091 const char * name, domain_enum domain,
ade7ed9e 4092 int global,
40658b94
PH
4093 int (*callback) (struct block *,
4094 struct symbol *, void *),
b5ec771e 4095 void *data, symbol_name_match_type match,
2edb89d3 4096 symbol_compare_ftype *ordered_compare)
9291a0cd 4097{
40658b94 4098 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4099 current language is Ada for a non-Ada objfile using GNU index. As Ada
4100 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4101}
4102
b5ec771e
PA
4103/* Symbol name matcher for .gdb_index names.
4104
4105 Symbol names in .gdb_index have a few particularities:
4106
4107 - There's no indication of which is the language of each symbol.
4108
4109 Since each language has its own symbol name matching algorithm,
4110 and we don't know which language is the right one, we must match
3f563c84
PA
4111 each symbol against all languages. This would be a potential
4112 performance problem if it were not mitigated by the
4113 mapped_index::name_components lookup table, which significantly
4114 reduces the number of times we need to call into this matcher,
4115 making it a non-issue.
b5ec771e
PA
4116
4117 - Symbol names in the index have no overload (parameter)
4118 information. I.e., in C++, "foo(int)" and "foo(long)" both
4119 appear as "foo" in the index, for example.
4120
4121 This means that the lookup names passed to the symbol name
4122 matcher functions must have no parameter information either
4123 because (e.g.) symbol search name "foo" does not match
4124 lookup-name "foo(int)" [while swapping search name for lookup
4125 name would match].
4126*/
4127class gdb_index_symbol_name_matcher
4128{
4129public:
4130 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4131 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4132
4133 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4134 Returns true if any matcher matches. */
4135 bool matches (const char *symbol_name);
4136
4137private:
4138 /* A reference to the lookup name we're matching against. */
4139 const lookup_name_info &m_lookup_name;
4140
4141 /* A vector holding all the different symbol name matchers, for all
4142 languages. */
4143 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4144};
4145
4146gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4147 (const lookup_name_info &lookup_name)
4148 : m_lookup_name (lookup_name)
4149{
4150 /* Prepare the vector of comparison functions upfront, to avoid
4151 doing the same work for each symbol. Care is taken to avoid
4152 matching with the same matcher more than once if/when multiple
4153 languages use the same matcher function. */
4154 auto &matchers = m_symbol_name_matcher_funcs;
4155 matchers.reserve (nr_languages);
4156
4157 matchers.push_back (default_symbol_name_matcher);
4158
4159 for (int i = 0; i < nr_languages; i++)
4160 {
4161 const language_defn *lang = language_def ((enum language) i);
4162 if (lang->la_get_symbol_name_matcher != NULL)
4163 {
4164 symbol_name_matcher_ftype *name_matcher
4165 = lang->la_get_symbol_name_matcher (m_lookup_name);
4166
4167 /* Don't insert the same comparison routine more than once.
4168 Note that we do this linear walk instead of a cheaper
4169 sorted insert, or use a std::set or something like that,
4170 because relative order of function addresses is not
4171 stable. This is not a problem in practice because the
4172 number of supported languages is low, and the cost here
4173 is tiny compared to the number of searches we'll do
4174 afterwards using this object. */
4175 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4176 == matchers.end ())
4177 matchers.push_back (name_matcher);
4178 }
4179 }
4180}
4181
4182bool
4183gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4184{
4185 for (auto matches_name : m_symbol_name_matcher_funcs)
4186 if (matches_name (symbol_name, m_lookup_name, NULL))
4187 return true;
4188
4189 return false;
4190}
4191
3f563c84
PA
4192/* Helper for dw2_expand_symtabs_matching that works with a
4193 mapped_index instead of the containing objfile. This is split to a
4194 separate function in order to be able to unit test the
4195 name_components matching using a mock mapped_index. For each
4196 symbol name that matches, calls MATCH_CALLBACK, passing it the
4197 symbol's index in the mapped_index symbol table. */
61d96d7e 4198
3f563c84
PA
4199static void
4200dw2_expand_symtabs_matching_symbol
4201 (mapped_index &index,
c62446b1 4202 const lookup_name_info &lookup_name_in,
3f563c84
PA
4203 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4204 enum search_domain kind,
4205 gdb::function_view<void (offset_type)> match_callback)
4206{
c62446b1
PA
4207 lookup_name_info lookup_name_without_params
4208 = lookup_name_in.make_ignore_params ();
3f563c84 4209 gdb_index_symbol_name_matcher lookup_name_matcher
c62446b1 4210 (lookup_name_without_params);
3f563c84
PA
4211
4212 auto *name_cmp = case_sensitivity == case_sensitive_on ? strcmp : strcasecmp;
4213
4214 /* Build the symbol name component sorted vector, if we haven't yet.
4215 The code below only knows how to break apart components of C++
4216 symbol names (and other languages that use '::' as
4217 namespace/module separator). If we add support for wild matching
4218 to some language that uses some other operator (E.g., Ada, Go and
4219 D use '.'), then we'll need to try splitting the symbol name
4220 according to that language too. Note that Ada does support wild
4221 matching, but doesn't currently support .gdb_index. */
4222 if (index.name_components.empty ())
4223 {
4224 for (size_t iter = 0; iter < index.symbol_table_slots; ++iter)
4225 {
4226 offset_type idx = 2 * iter;
4227
4228 if (index.symbol_table[idx] == 0
4229 && index.symbol_table[idx + 1] == 0)
4230 continue;
4231
4232 const char *name = index.symbol_name_at (idx);
4233
4234 /* Add each name component to the name component table. */
4235 unsigned int previous_len = 0;
4236 for (unsigned int current_len = cp_find_first_component (name);
4237 name[current_len] != '\0';
4238 current_len += cp_find_first_component (name + current_len))
4239 {
4240 gdb_assert (name[current_len] == ':');
4241 index.name_components.push_back ({previous_len, idx});
4242 /* Skip the '::'. */
4243 current_len += 2;
4244 previous_len = current_len;
4245 }
4246 index.name_components.push_back ({previous_len, idx});
4247 }
9291a0cd 4248
3f563c84
PA
4249 /* Sort name_components elements by name. */
4250 auto name_comp_compare = [&] (const name_component &left,
4251 const name_component &right)
4252 {
4253 const char *left_qualified = index.symbol_name_at (left.idx);
4254 const char *right_qualified = index.symbol_name_at (right.idx);
4255
4256 const char *left_name = left_qualified + left.name_offset;
4257 const char *right_name = right_qualified + right.name_offset;
4258
4259 return name_cmp (left_name, right_name) < 0;
4260 };
4261
4262 std::sort (index.name_components.begin (),
4263 index.name_components.end (),
4264 name_comp_compare);
4265 }
4266
4267 const char *cplus
c62446b1 4268 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4269
3f563c84
PA
4270 /* Comparison function object for lower_bound that matches against a
4271 given symbol name. */
4272 auto lookup_compare_lower = [&] (const name_component &elem,
4273 const char *name)
4274 {
4275 const char *elem_qualified = index.symbol_name_at (elem.idx);
4276 const char *elem_name = elem_qualified + elem.name_offset;
4277 return name_cmp (elem_name, name) < 0;
4278 };
4279
4280 /* Comparison function object for upper_bound that matches against a
4281 given symbol name. */
4282 auto lookup_compare_upper = [&] (const char *name,
4283 const name_component &elem)
4284 {
4285 const char *elem_qualified = index.symbol_name_at (elem.idx);
4286 const char *elem_name = elem_qualified + elem.name_offset;
4287 return name_cmp (name, elem_name) < 0;
4288 };
4289
4290 auto begin = index.name_components.begin ();
4291 auto end = index.name_components.end ();
4292
4293 /* Find the lower bound. */
4294 auto lower = [&] ()
4295 {
c62446b1 4296 if (lookup_name_in.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4297 return begin;
4298 else
4299 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4300 } ();
4301
4302 /* Find the upper bound. */
4303 auto upper = [&] ()
4304 {
c62446b1 4305 if (lookup_name_in.completion_mode ())
3f563c84
PA
4306 {
4307 /* The string frobbing below won't work if the string is
4308 empty. We don't need it then, anyway -- if we're
4309 completing an empty string, then we want to iterate over
4310 the whole range. */
4311 if (cplus[0] == '\0')
4312 return end;
4313
4314 /* In completion mode, increment the last character because
4315 we want UPPER to point past all symbols names that have
4316 the same prefix. */
4317 std::string after = cplus;
4318
4319 gdb_assert (after.back () != 0xff);
4320 after.back ()++;
4321
4322 return std::upper_bound (lower, end, after.c_str (),
4323 lookup_compare_upper);
4324 }
4325 else
4326 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4327 } ();
4328
4329 /* Now for each symbol name in range, check to see if we have a name
4330 match, and if so, call the MATCH_CALLBACK callback. */
4331
4332 /* The same symbol may appear more than once in the range though.
4333 E.g., if we're looking for symbols that complete "w", and we have
4334 a symbol named "w1::w2", we'll find the two name components for
4335 that same symbol in the range. To be sure we only call the
4336 callback once per symbol, we first collect the symbol name
4337 indexes that matched in a temporary vector and ignore
4338 duplicates. */
4339 std::vector<offset_type> matches;
4340 matches.reserve (std::distance (lower, upper));
4341
4342 for (;lower != upper; ++lower)
4343 {
4344 const char *qualified = index.symbol_name_at (lower->idx);
4345
4346 if (!lookup_name_matcher.matches (qualified)
4347 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4348 continue;
4349
3f563c84
PA
4350 matches.push_back (lower->idx);
4351 }
4352
4353 std::sort (matches.begin (), matches.end ());
4354
4355 /* Finally call the callback, once per match. */
4356 ULONGEST prev = -1;
4357 for (offset_type idx : matches)
4358 {
4359 if (prev != idx)
4360 {
4361 match_callback (idx);
4362 prev = idx;
4363 }
4364 }
4365
4366 /* Above we use a type wider than idx's for 'prev', since 0 and
4367 (offset_type)-1 are both possible values. */
4368 static_assert (sizeof (prev) > sizeof (offset_type), "");
4369}
4370
c62446b1
PA
4371#if GDB_SELF_TEST
4372
4373namespace selftests { namespace dw2_expand_symtabs_matching {
4374
4375/* A wrapper around mapped_index that builds a mock mapped_index, from
4376 the symbol list passed as parameter to the constructor. */
4377class mock_mapped_index
4378{
4379public:
4380 template<size_t N>
4381 mock_mapped_index (const char *(&symbols)[N])
4382 : mock_mapped_index (symbols, N)
4383 {}
4384
4385 /* Access the built index. */
4386 mapped_index &index ()
4387 { return m_index; }
4388
4389 /* Disable copy. */
4390 mock_mapped_index(const mock_mapped_index &) = delete;
4391 void operator= (const mock_mapped_index &) = delete;
4392
4393private:
4394 mock_mapped_index (const char **symbols, size_t symbols_size)
4395 {
4396 /* No string can live at offset zero. Add a dummy entry. */
4397 obstack_grow_str0 (&m_constant_pool, "");
4398
4399 for (size_t i = 0; i < symbols_size; i++)
4400 {
4401 const char *sym = symbols[i];
4402 size_t offset = obstack_object_size (&m_constant_pool);
4403 obstack_grow_str0 (&m_constant_pool, sym);
4404 m_symbol_table.push_back (offset);
4405 m_symbol_table.push_back (0);
4406 };
4407
4408 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4409 m_index.symbol_table = m_symbol_table.data ();
4410 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4411 }
4412
4413public:
4414 /* The built mapped_index. */
4415 mapped_index m_index{};
4416
4417 /* The storage that the built mapped_index uses for symbol and
4418 constant pool tables. */
4419 std::vector<offset_type> m_symbol_table;
4420 auto_obstack m_constant_pool;
4421};
4422
4423/* Convenience function that converts a NULL pointer to a "<null>"
4424 string, to pass to print routines. */
4425
4426static const char *
4427string_or_null (const char *str)
4428{
4429 return str != NULL ? str : "<null>";
4430}
4431
4432/* Check if a lookup_name_info built from
4433 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4434 index. EXPECTED_LIST is the list of expected matches, in expected
4435 matching order. If no match expected, then an empty list is
4436 specified. Returns true on success. On failure prints a warning
4437 indicating the file:line that failed, and returns false. */
4438
4439static bool
4440check_match (const char *file, int line,
4441 mock_mapped_index &mock_index,
4442 const char *name, symbol_name_match_type match_type,
4443 bool completion_mode,
4444 std::initializer_list<const char *> expected_list)
4445{
4446 lookup_name_info lookup_name (name, match_type, completion_mode);
4447
4448 bool matched = true;
4449
4450 auto mismatch = [&] (const char *expected_str,
4451 const char *got)
4452 {
4453 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4454 "expected=\"%s\", got=\"%s\"\n"),
4455 file, line,
4456 (match_type == symbol_name_match_type::FULL
4457 ? "FULL" : "WILD"),
4458 name, string_or_null (expected_str), string_or_null (got));
4459 matched = false;
4460 };
4461
4462 auto expected_it = expected_list.begin ();
4463 auto expected_end = expected_list.end ();
4464
4465 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4466 NULL, ALL_DOMAIN,
4467 [&] (offset_type idx)
4468 {
4469 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4470 const char *expected_str
4471 = expected_it == expected_end ? NULL : *expected_it++;
4472
4473 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4474 mismatch (expected_str, matched_name);
4475 });
4476
4477 const char *expected_str
4478 = expected_it == expected_end ? NULL : *expected_it++;
4479 if (expected_str != NULL)
4480 mismatch (expected_str, NULL);
4481
4482 return matched;
4483}
4484
4485/* The symbols added to the mock mapped_index for testing (in
4486 canonical form). */
4487static const char *test_symbols[] = {
4488 "function",
4489 "std::bar",
4490 "std::zfunction",
4491 "std::zfunction2",
4492 "w1::w2",
4493 "ns::foo<char*>",
4494 "ns::foo<int>",
4495 "ns::foo<long>",
4496
4497 /* A name with all sorts of complications. Starts with "z" to make
4498 it easier for the completion tests below. */
4499#define Z_SYM_NAME \
4500 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4501 "::tuple<(anonymous namespace)::ui*, " \
4502 "std::default_delete<(anonymous namespace)::ui>, void>"
4503
4504 Z_SYM_NAME
4505};
4506
4507static void
4508run_test ()
4509{
4510 mock_mapped_index mock_index (test_symbols);
4511
4512 /* We let all tests run until the end even if some fails, for debug
4513 convenience. */
4514 bool any_mismatch = false;
4515
4516 /* Create the expected symbols list (an initializer_list). Needed
4517 because lists have commas, and we need to pass them to CHECK,
4518 which is a macro. */
4519#define EXPECT(...) { __VA_ARGS__ }
4520
4521 /* Wrapper for check_match that passes down the current
4522 __FILE__/__LINE__. */
4523#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4524 any_mismatch |= !check_match (__FILE__, __LINE__, \
4525 mock_index, \
4526 NAME, MATCH_TYPE, COMPLETION_MODE, \
4527 EXPECTED_LIST)
4528
4529 /* Identity checks. */
4530 for (const char *sym : test_symbols)
4531 {
4532 /* Should be able to match all existing symbols. */
4533 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4534 EXPECT (sym));
4535
4536 /* Should be able to match all existing symbols with
4537 parameters. */
4538 std::string with_params = std::string (sym) + "(int)";
4539 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4540 EXPECT (sym));
4541
4542 /* Should be able to match all existing symbols with
4543 parameters and qualifiers. */
4544 with_params = std::string (sym) + " ( int ) const";
4545 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4546 EXPECT (sym));
4547
4548 /* This should really find sym, but cp-name-parser.y doesn't
4549 know about lvalue/rvalue qualifiers yet. */
4550 with_params = std::string (sym) + " ( int ) &&";
4551 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4552 {});
4553 }
4554
4555 /* Check that completion mode works at each prefix of the expected
4556 symbol name. */
4557 {
4558 static const char str[] = "function(int)";
4559 size_t len = strlen (str);
4560 std::string lookup;
4561
4562 for (size_t i = 1; i < len; i++)
4563 {
4564 lookup.assign (str, i);
4565 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4566 EXPECT ("function"));
4567 }
4568 }
4569
4570 /* While "w" is a prefix of both components, the match function
4571 should still only be called once. */
4572 {
4573 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4574 EXPECT ("w1::w2"));
4575 }
4576
4577 /* Same, with a "complicated" symbol. */
4578 {
4579 static const char str[] = Z_SYM_NAME;
4580 size_t len = strlen (str);
4581 std::string lookup;
4582
4583 for (size_t i = 1; i < len; i++)
4584 {
4585 lookup.assign (str, i);
4586 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4587 EXPECT (Z_SYM_NAME));
4588 }
4589 }
4590
4591 /* In FULL mode, an incomplete symbol doesn't match. */
4592 {
4593 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4594 {});
4595 }
4596
4597 /* A complete symbol with parameters matches any overload, since the
4598 index has no overload info. */
4599 {
4600 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4601 EXPECT ("std::zfunction", "std::zfunction2"));
4602 }
4603
4604 /* Check that whitespace is ignored appropriately. A symbol with a
4605 template argument list. */
4606 {
4607 static const char expected[] = "ns::foo<int>";
4608 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4609 EXPECT (expected));
4610 }
4611
4612 /* Check that whitespace is ignored appropriately. A symbol with a
4613 template argument list that includes a pointer. */
4614 {
4615 static const char expected[] = "ns::foo<char*>";
4616 /* Try both completion and non-completion modes. */
4617 static const bool completion_mode[2] = {false, true};
4618 for (size_t i = 0; i < 2; i++)
4619 {
4620 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4621 completion_mode[i], EXPECT (expected));
4622
4623 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4624 completion_mode[i], EXPECT (expected));
4625 }
4626 }
4627
4628 {
4629 /* Check method qualifiers are ignored. */
4630 static const char expected[] = "ns::foo<char*>";
4631 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4632 symbol_name_match_type::FULL, true, EXPECT (expected));
4633 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4634 symbol_name_match_type::FULL, true, EXPECT (expected));
4635 }
4636
4637 /* Test lookup names that don't match anything. */
4638 {
4639 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4640 {});
4641 }
4642
4643 SELF_CHECK (!any_mismatch);
4644
4645#undef EXPECT
4646#undef CHECK_MATCH
4647}
4648
4649}} // namespace selftests::dw2_expand_symtabs_matching
4650
4651#endif /* GDB_SELF_TEST */
4652
3f563c84
PA
4653/* Helper for dw2_expand_matching symtabs. Called on each symbol
4654 matched, to expand corresponding CUs that were marked. IDX is the
4655 index of the symbol name that matched. */
4656
4657static void
4658dw2_expand_marked_cus
4659 (mapped_index &index, offset_type idx,
4660 struct objfile *objfile,
4661 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4662 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4663 search_domain kind)
4664{
4665 const char *name;
4666 offset_type *vec, vec_len, vec_idx;
4667 bool global_seen = false;
4668
61920122
PA
4669 vec = (offset_type *) (index.constant_pool
4670 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4671 vec_len = MAYBE_SWAP (vec[0]);
4672 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4673 {
4674 struct dwarf2_per_cu_data *per_cu;
4675 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4676 /* This value is only valid for index versions >= 7. */
4677 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4678 gdb_index_symbol_kind symbol_kind =
4679 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4680 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4681 /* Only check the symbol attributes if they're present.
4682 Indices prior to version 7 don't record them,
4683 and indices >= 7 may elide them for certain symbols
4684 (gold does this). */
4685 int attrs_valid =
4686 (index.version >= 7
4687 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4688
4689 /* Work around gold/15646. */
4690 if (attrs_valid)
9291a0cd 4691 {
61920122
PA
4692 if (!is_static && global_seen)
4693 continue;
4694 if (!is_static)
4695 global_seen = true;
4696 }
3190f0c6 4697
61920122
PA
4698 /* Only check the symbol's kind if it has one. */
4699 if (attrs_valid)
4700 {
4701 switch (kind)
8943b874 4702 {
61920122
PA
4703 case VARIABLES_DOMAIN:
4704 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4705 continue;
4706 break;
4707 case FUNCTIONS_DOMAIN:
4708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4709 continue;
61920122
PA
4710 break;
4711 case TYPES_DOMAIN:
4712 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4713 continue;
4714 break;
4715 default:
4716 break;
8943b874 4717 }
61920122 4718 }
8943b874 4719
61920122
PA
4720 /* Don't crash on bad data. */
4721 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4722 + dwarf2_per_objfile->n_type_units))
4723 {
4724 complaint (&symfile_complaints,
4725 _(".gdb_index entry has bad CU index"
4726 " [in module %s]"), objfile_name (objfile));
4727 continue;
4728 }
4729
4730 per_cu = dw2_get_cutu (cu_index);
4731 if (file_matcher == NULL || per_cu->v.quick->mark)
4732 {
4733 int symtab_was_null =
4734 (per_cu->v.quick->compunit_symtab == NULL);
4735
4736 dw2_instantiate_symtab (per_cu);
4737
4738 if (expansion_notify != NULL
4739 && symtab_was_null
4740 && per_cu->v.quick->compunit_symtab != NULL)
4741 expansion_notify (per_cu->v.quick->compunit_symtab);
4742 }
4743 }
4744}
4745
4746static void
4747dw2_expand_symtabs_matching
4748 (struct objfile *objfile,
4749 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4750 const lookup_name_info &lookup_name,
4751 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4752 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4753 enum search_domain kind)
4754{
4755 int i;
4756 offset_type iter;
4757
4758 dw2_setup (objfile);
4759
4760 /* index_table is NULL if OBJF_READNOW. */
4761 if (!dwarf2_per_objfile->index_table)
4762 return;
4763
4764 if (file_matcher != NULL)
4765 {
4766 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4767 htab_eq_pointer,
4768 NULL, xcalloc, xfree));
4769 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4770 htab_eq_pointer,
4771 NULL, xcalloc, xfree));
4772
4773 /* The rule is CUs specify all the files, including those used by
4774 any TU, so there's no need to scan TUs here. */
4775
4776 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4777 {
4778 int j;
4779 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4780 struct quick_file_names *file_data;
4781 void **slot;
4782
4783 QUIT;
4784
4785 per_cu->v.quick->mark = 0;
156942c7 4786
61920122
PA
4787 /* We only need to look at symtabs not already expanded. */
4788 if (per_cu->v.quick->compunit_symtab)
4789 continue;
4790
4791 file_data = dw2_get_file_names (per_cu);
4792 if (file_data == NULL)
4793 continue;
4794
4795 if (htab_find (visited_not_found.get (), file_data) != NULL)
4796 continue;
4797 else if (htab_find (visited_found.get (), file_data) != NULL)
3190f0c6 4798 {
61920122 4799 per_cu->v.quick->mark = 1;
3190f0c6
DE
4800 continue;
4801 }
4802
61920122 4803 for (j = 0; j < file_data->num_file_names; ++j)
276d885b 4804 {
61920122 4805 const char *this_real_name;
276d885b 4806
61920122
PA
4807 if (file_matcher (file_data->file_names[j], false))
4808 {
4809 per_cu->v.quick->mark = 1;
4810 break;
4811 }
4812
4813 /* Before we invoke realpath, which can get expensive when many
4814 files are involved, do a quick comparison of the basenames. */
4815 if (!basenames_may_differ
4816 && !file_matcher (lbasename (file_data->file_names[j]),
4817 true))
4818 continue;
276d885b 4819
61920122
PA
4820 this_real_name = dw2_get_real_path (objfile, file_data, j);
4821 if (file_matcher (this_real_name, false))
276d885b 4822 {
61920122
PA
4823 per_cu->v.quick->mark = 1;
4824 break;
276d885b
GB
4825 }
4826 }
61920122
PA
4827
4828 slot = htab_find_slot (per_cu->v.quick->mark
4829 ? visited_found.get ()
4830 : visited_not_found.get (),
4831 file_data, INSERT);
4832 *slot = file_data;
9291a0cd 4833 }
61920122
PA
4834 }
4835
4836 mapped_index &index = *dwarf2_per_objfile->index_table;
4837
4838 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4839 symbol_matcher,
4840 kind, [&] (offset_type idx)
4841 {
4842 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
4843 expansion_notify, kind);
4844 });
9291a0cd
TT
4845}
4846
43f3e411 4847/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4848 symtab. */
4849
43f3e411
DE
4850static struct compunit_symtab *
4851recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4852 CORE_ADDR pc)
9703b513
TT
4853{
4854 int i;
4855
43f3e411
DE
4856 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4857 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4858 return cust;
9703b513 4859
43f3e411 4860 if (cust->includes == NULL)
a3ec0bb1
DE
4861 return NULL;
4862
43f3e411 4863 for (i = 0; cust->includes[i]; ++i)
9703b513 4864 {
43f3e411 4865 struct compunit_symtab *s = cust->includes[i];
9703b513 4866
43f3e411 4867 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4868 if (s != NULL)
4869 return s;
4870 }
4871
4872 return NULL;
4873}
4874
43f3e411
DE
4875static struct compunit_symtab *
4876dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4877 struct bound_minimal_symbol msymbol,
4878 CORE_ADDR pc,
4879 struct obj_section *section,
4880 int warn_if_readin)
9291a0cd
TT
4881{
4882 struct dwarf2_per_cu_data *data;
43f3e411 4883 struct compunit_symtab *result;
9291a0cd
TT
4884
4885 dw2_setup (objfile);
4886
4887 if (!objfile->psymtabs_addrmap)
4888 return NULL;
4889
9a3c8263
SM
4890 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4891 pc);
9291a0cd
TT
4892 if (!data)
4893 return NULL;
4894
43f3e411 4895 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4896 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4897 paddress (get_objfile_arch (objfile), pc));
4898
43f3e411
DE
4899 result
4900 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4901 pc);
9703b513
TT
4902 gdb_assert (result != NULL);
4903 return result;
9291a0cd
TT
4904}
4905
9291a0cd 4906static void
44b13c5a 4907dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4908 void *data, int need_fullname)
9291a0cd 4909{
9291a0cd 4910 dw2_setup (objfile);
ae2de4f8 4911
bbf2f4df 4912 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4913 {
bbf2f4df 4914 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4915
bbf2f4df
PA
4916 htab_up visited (htab_create_alloc (10,
4917 htab_hash_pointer, htab_eq_pointer,
4918 NULL, xcalloc, xfree));
24c79950 4919
bbf2f4df
PA
4920 /* The rule is CUs specify all the files, including those used
4921 by any TU, so there's no need to scan TUs here. We can
4922 ignore file names coming from already-expanded CUs. */
24c79950 4923
bbf2f4df
PA
4924 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4925 {
4926 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4927
bbf2f4df
PA
4928 if (per_cu->v.quick->compunit_symtab)
4929 {
4930 void **slot = htab_find_slot (visited.get (),
4931 per_cu->v.quick->file_names,
4932 INSERT);
9291a0cd 4933
bbf2f4df
PA
4934 *slot = per_cu->v.quick->file_names;
4935 }
24c79950 4936 }
24c79950 4937
bbf2f4df 4938 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4939 {
bbf2f4df
PA
4940 int j;
4941 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4942 struct quick_file_names *file_data;
4943 void **slot;
4944
4945 /* We only need to look at symtabs not already expanded. */
4946 if (per_cu->v.quick->compunit_symtab)
4947 continue;
74e2f255 4948
bbf2f4df
PA
4949 file_data = dw2_get_file_names (per_cu);
4950 if (file_data == NULL)
4951 continue;
4952
4953 slot = htab_find_slot (visited.get (), file_data, INSERT);
4954 if (*slot)
4955 {
4956 /* Already visited. */
4957 continue;
4958 }
4959 *slot = file_data;
4960
4961 for (int j = 0; j < file_data->num_file_names; ++j)
4962 {
4963 const char *filename = file_data->file_names[j];
4964 dwarf2_per_objfile->filenames_cache->seen (filename);
4965 }
9291a0cd
TT
4966 }
4967 }
bbf2f4df
PA
4968
4969 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4970 {
14278e1f 4971 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4972
4973 if (need_fullname)
4974 this_real_name = gdb_realpath (filename);
14278e1f 4975 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4976 });
9291a0cd
TT
4977}
4978
4979static int
4980dw2_has_symbols (struct objfile *objfile)
4981{
4982 return 1;
4983}
4984
4985const struct quick_symbol_functions dwarf2_gdb_index_functions =
4986{
4987 dw2_has_symbols,
4988 dw2_find_last_source_symtab,
4989 dw2_forget_cached_source_info,
f8eba3c6 4990 dw2_map_symtabs_matching_filename,
9291a0cd 4991 dw2_lookup_symbol,
9291a0cd
TT
4992 dw2_print_stats,
4993 dw2_dump,
4994 dw2_relocate,
4995 dw2_expand_symtabs_for_function,
4996 dw2_expand_all_symtabs,
652a8996 4997 dw2_expand_symtabs_with_fullname,
40658b94 4998 dw2_map_matching_symbols,
9291a0cd 4999 dw2_expand_symtabs_matching,
43f3e411 5000 dw2_find_pc_sect_compunit_symtab,
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
c906108c 9236 default:
e7c27a73 9237 new_symbol (die, NULL, cu);
c906108c
SS
9238 break;
9239 }
9240}
ca69b9e6
DE
9241\f
9242/* DWARF name computation. */
c906108c 9243
94af9270
KS
9244/* A helper function for dwarf2_compute_name which determines whether DIE
9245 needs to have the name of the scope prepended to the name listed in the
9246 die. */
9247
9248static int
9249die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9250{
1c809c68
TT
9251 struct attribute *attr;
9252
94af9270
KS
9253 switch (die->tag)
9254 {
9255 case DW_TAG_namespace:
9256 case DW_TAG_typedef:
9257 case DW_TAG_class_type:
9258 case DW_TAG_interface_type:
9259 case DW_TAG_structure_type:
9260 case DW_TAG_union_type:
9261 case DW_TAG_enumeration_type:
9262 case DW_TAG_enumerator:
9263 case DW_TAG_subprogram:
08a76f8a 9264 case DW_TAG_inlined_subroutine:
94af9270 9265 case DW_TAG_member:
74921315 9266 case DW_TAG_imported_declaration:
94af9270
KS
9267 return 1;
9268
9269 case DW_TAG_variable:
c2b0a229 9270 case DW_TAG_constant:
94af9270
KS
9271 /* We only need to prefix "globally" visible variables. These include
9272 any variable marked with DW_AT_external or any variable that
9273 lives in a namespace. [Variables in anonymous namespaces
9274 require prefixing, but they are not DW_AT_external.] */
9275
9276 if (dwarf2_attr (die, DW_AT_specification, cu))
9277 {
9278 struct dwarf2_cu *spec_cu = cu;
9a619af0 9279
94af9270
KS
9280 return die_needs_namespace (die_specification (die, &spec_cu),
9281 spec_cu);
9282 }
9283
1c809c68 9284 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9285 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9286 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9287 return 0;
9288 /* A variable in a lexical block of some kind does not need a
9289 namespace, even though in C++ such variables may be external
9290 and have a mangled name. */
9291 if (die->parent->tag == DW_TAG_lexical_block
9292 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9293 || die->parent->tag == DW_TAG_catch_block
9294 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9295 return 0;
9296 return 1;
94af9270
KS
9297
9298 default:
9299 return 0;
9300 }
9301}
9302
73b9be8b
KS
9303/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9304 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9305 defined for the given DIE. */
9306
9307static struct attribute *
9308dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9309{
9310 struct attribute *attr;
9311
9312 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9313 if (attr == NULL)
9314 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9315
9316 return attr;
9317}
9318
9319/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9320 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9321 defined for the given DIE. */
9322
9323static const char *
9324dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9325{
9326 const char *linkage_name;
9327
9328 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9329 if (linkage_name == NULL)
9330 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9331
9332 return linkage_name;
9333}
9334
94af9270 9335/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9336 compute the physname for the object, which include a method's:
9c37b5ae 9337 - formal parameters (C++),
a766d390 9338 - receiver type (Go),
a766d390
DE
9339
9340 The term "physname" is a bit confusing.
9341 For C++, for example, it is the demangled name.
9342 For Go, for example, it's the mangled name.
94af9270 9343
af6b7be1
JB
9344 For Ada, return the DIE's linkage name rather than the fully qualified
9345 name. PHYSNAME is ignored..
9346
94af9270
KS
9347 The result is allocated on the objfile_obstack and canonicalized. */
9348
9349static const char *
15d034d0
TT
9350dwarf2_compute_name (const char *name,
9351 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9352 int physname)
9353{
bb5ed363
DE
9354 struct objfile *objfile = cu->objfile;
9355
94af9270
KS
9356 if (name == NULL)
9357 name = dwarf2_name (die, cu);
9358
2ee7123e
DE
9359 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9360 but otherwise compute it by typename_concat inside GDB.
9361 FIXME: Actually this is not really true, or at least not always true.
9362 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9363 Fortran names because there is no mangling standard. So new_symbol_full
9364 will set the demangled name to the result of dwarf2_full_name, and it is
9365 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9366 if (cu->language == language_ada
9367 || (cu->language == language_fortran && physname))
9368 {
9369 /* For Ada unit, we prefer the linkage name over the name, as
9370 the former contains the exported name, which the user expects
9371 to be able to reference. Ideally, we want the user to be able
9372 to reference this entity using either natural or linkage name,
9373 but we haven't started looking at this enhancement yet. */
73b9be8b 9374 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9375
2ee7123e
DE
9376 if (linkage_name != NULL)
9377 return linkage_name;
f55ee35c
JK
9378 }
9379
94af9270
KS
9380 /* These are the only languages we know how to qualify names in. */
9381 if (name != NULL
9c37b5ae 9382 && (cu->language == language_cplus
c44af4eb
TT
9383 || cu->language == language_fortran || cu->language == language_d
9384 || cu->language == language_rust))
94af9270
KS
9385 {
9386 if (die_needs_namespace (die, cu))
9387 {
9388 long length;
0d5cff50 9389 const char *prefix;
34a68019 9390 const char *canonical_name = NULL;
94af9270 9391
d7e74731
PA
9392 string_file buf;
9393
94af9270 9394 prefix = determine_prefix (die, cu);
94af9270
KS
9395 if (*prefix != '\0')
9396 {
f55ee35c
JK
9397 char *prefixed_name = typename_concat (NULL, prefix, name,
9398 physname, cu);
9a619af0 9399
d7e74731 9400 buf.puts (prefixed_name);
94af9270
KS
9401 xfree (prefixed_name);
9402 }
9403 else
d7e74731 9404 buf.puts (name);
94af9270 9405
98bfdba5
PA
9406 /* Template parameters may be specified in the DIE's DW_AT_name, or
9407 as children with DW_TAG_template_type_param or
9408 DW_TAG_value_type_param. If the latter, add them to the name
9409 here. If the name already has template parameters, then
9410 skip this step; some versions of GCC emit both, and
9411 it is more efficient to use the pre-computed name.
9412
9413 Something to keep in mind about this process: it is very
9414 unlikely, or in some cases downright impossible, to produce
9415 something that will match the mangled name of a function.
9416 If the definition of the function has the same debug info,
9417 we should be able to match up with it anyway. But fallbacks
9418 using the minimal symbol, for instance to find a method
9419 implemented in a stripped copy of libstdc++, will not work.
9420 If we do not have debug info for the definition, we will have to
9421 match them up some other way.
9422
9423 When we do name matching there is a related problem with function
9424 templates; two instantiated function templates are allowed to
9425 differ only by their return types, which we do not add here. */
9426
9427 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9428 {
9429 struct attribute *attr;
9430 struct die_info *child;
9431 int first = 1;
9432
9433 die->building_fullname = 1;
9434
9435 for (child = die->child; child != NULL; child = child->sibling)
9436 {
9437 struct type *type;
12df843f 9438 LONGEST value;
d521ce57 9439 const gdb_byte *bytes;
98bfdba5
PA
9440 struct dwarf2_locexpr_baton *baton;
9441 struct value *v;
9442
9443 if (child->tag != DW_TAG_template_type_param
9444 && child->tag != DW_TAG_template_value_param)
9445 continue;
9446
9447 if (first)
9448 {
d7e74731 9449 buf.puts ("<");
98bfdba5
PA
9450 first = 0;
9451 }
9452 else
d7e74731 9453 buf.puts (", ");
98bfdba5
PA
9454
9455 attr = dwarf2_attr (child, DW_AT_type, cu);
9456 if (attr == NULL)
9457 {
9458 complaint (&symfile_complaints,
9459 _("template parameter missing DW_AT_type"));
d7e74731 9460 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9461 continue;
9462 }
9463 type = die_type (child, cu);
9464
9465 if (child->tag == DW_TAG_template_type_param)
9466 {
d7e74731 9467 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
9468 continue;
9469 }
9470
9471 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9472 if (attr == NULL)
9473 {
9474 complaint (&symfile_complaints,
3e43a32a
MS
9475 _("template parameter missing "
9476 "DW_AT_const_value"));
d7e74731 9477 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9478 continue;
9479 }
9480
9481 dwarf2_const_value_attr (attr, type, name,
9482 &cu->comp_unit_obstack, cu,
9483 &value, &bytes, &baton);
9484
9485 if (TYPE_NOSIGN (type))
9486 /* GDB prints characters as NUMBER 'CHAR'. If that's
9487 changed, this can use value_print instead. */
d7e74731 9488 c_printchar (value, type, &buf);
98bfdba5
PA
9489 else
9490 {
9491 struct value_print_options opts;
9492
9493 if (baton != NULL)
9494 v = dwarf2_evaluate_loc_desc (type, NULL,
9495 baton->data,
9496 baton->size,
9497 baton->per_cu);
9498 else if (bytes != NULL)
9499 {
9500 v = allocate_value (type);
9501 memcpy (value_contents_writeable (v), bytes,
9502 TYPE_LENGTH (type));
9503 }
9504 else
9505 v = value_from_longest (type, value);
9506
3e43a32a
MS
9507 /* Specify decimal so that we do not depend on
9508 the radix. */
98bfdba5
PA
9509 get_formatted_print_options (&opts, 'd');
9510 opts.raw = 1;
d7e74731 9511 value_print (v, &buf, &opts);
98bfdba5
PA
9512 release_value (v);
9513 value_free (v);
9514 }
9515 }
9516
9517 die->building_fullname = 0;
9518
9519 if (!first)
9520 {
9521 /* Close the argument list, with a space if necessary
9522 (nested templates). */
d7e74731
PA
9523 if (!buf.empty () && buf.string ().back () == '>')
9524 buf.puts (" >");
98bfdba5 9525 else
d7e74731 9526 buf.puts (">");
98bfdba5
PA
9527 }
9528 }
9529
9c37b5ae 9530 /* For C++ methods, append formal parameter type
94af9270 9531 information, if PHYSNAME. */
6e70227d 9532
94af9270 9533 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9534 && cu->language == language_cplus)
94af9270
KS
9535 {
9536 struct type *type = read_type_die (die, cu);
9537
d7e74731 9538 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9539 &type_print_raw_options);
94af9270 9540
9c37b5ae 9541 if (cu->language == language_cplus)
94af9270 9542 {
60430eff
DJ
9543 /* Assume that an artificial first parameter is
9544 "this", but do not crash if it is not. RealView
9545 marks unnamed (and thus unused) parameters as
9546 artificial; there is no way to differentiate
9547 the two cases. */
94af9270
KS
9548 if (TYPE_NFIELDS (type) > 0
9549 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9550 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9551 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9552 0))))
d7e74731 9553 buf.puts (" const");
94af9270
KS
9554 }
9555 }
9556
d7e74731 9557 const std::string &intermediate_name = buf.string ();
94af9270
KS
9558
9559 if (cu->language == language_cplus)
34a68019 9560 canonical_name
322a8516 9561 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9562 &objfile->per_bfd->storage_obstack);
9563
9564 /* If we only computed INTERMEDIATE_NAME, or if
9565 INTERMEDIATE_NAME is already canonical, then we need to
9566 copy it to the appropriate obstack. */
322a8516 9567 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9568 name = ((const char *)
9569 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9570 intermediate_name.c_str (),
9571 intermediate_name.length ()));
34a68019
TT
9572 else
9573 name = canonical_name;
94af9270
KS
9574 }
9575 }
9576
9577 return name;
9578}
9579
0114d602
DJ
9580/* Return the fully qualified name of DIE, based on its DW_AT_name.
9581 If scope qualifiers are appropriate they will be added. The result
34a68019 9582 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9583 not have a name. NAME may either be from a previous call to
9584 dwarf2_name or NULL.
9585
9c37b5ae 9586 The output string will be canonicalized (if C++). */
0114d602
DJ
9587
9588static const char *
15d034d0 9589dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9590{
94af9270
KS
9591 return dwarf2_compute_name (name, die, cu, 0);
9592}
0114d602 9593
94af9270
KS
9594/* Construct a physname for the given DIE in CU. NAME may either be
9595 from a previous call to dwarf2_name or NULL. The result will be
9596 allocated on the objfile_objstack or NULL if the DIE does not have a
9597 name.
0114d602 9598
9c37b5ae 9599 The output string will be canonicalized (if C++). */
0114d602 9600
94af9270 9601static const char *
15d034d0 9602dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9603{
bb5ed363 9604 struct objfile *objfile = cu->objfile;
900e11f9 9605 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9606 int need_copy = 1;
9607
9608 /* In this case dwarf2_compute_name is just a shortcut not building anything
9609 on its own. */
9610 if (!die_needs_namespace (die, cu))
9611 return dwarf2_compute_name (name, die, cu, 1);
9612
73b9be8b 9613 mangled = dw2_linkage_name (die, cu);
900e11f9 9614
e98c9e7c
TT
9615 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9616 See https://github.com/rust-lang/rust/issues/32925. */
9617 if (cu->language == language_rust && mangled != NULL
9618 && strchr (mangled, '{') != NULL)
9619 mangled = NULL;
9620
900e11f9
JK
9621 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9622 has computed. */
791afaa2 9623 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9624 if (mangled != NULL)
900e11f9 9625 {
900e11f9
JK
9626 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9627 type. It is easier for GDB users to search for such functions as
9628 `name(params)' than `long name(params)'. In such case the minimal
9629 symbol names do not match the full symbol names but for template
9630 functions there is never a need to look up their definition from their
9631 declaration so the only disadvantage remains the minimal symbol
9632 variant `long name(params)' does not have the proper inferior type.
9633 */
9634
a766d390
DE
9635 if (cu->language == language_go)
9636 {
9637 /* This is a lie, but we already lie to the caller new_symbol_full.
9638 new_symbol_full assumes we return the mangled name.
9639 This just undoes that lie until things are cleaned up. */
a766d390
DE
9640 }
9641 else
9642 {
791afaa2
TT
9643 demangled.reset (gdb_demangle (mangled,
9644 (DMGL_PARAMS | DMGL_ANSI
9645 | DMGL_RET_DROP)));
a766d390 9646 }
900e11f9 9647 if (demangled)
791afaa2 9648 canon = demangled.get ();
900e11f9
JK
9649 else
9650 {
9651 canon = mangled;
9652 need_copy = 0;
9653 }
9654 }
9655
9656 if (canon == NULL || check_physname)
9657 {
9658 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9659
9660 if (canon != NULL && strcmp (physname, canon) != 0)
9661 {
9662 /* It may not mean a bug in GDB. The compiler could also
9663 compute DW_AT_linkage_name incorrectly. But in such case
9664 GDB would need to be bug-to-bug compatible. */
9665
9666 complaint (&symfile_complaints,
9667 _("Computed physname <%s> does not match demangled <%s> "
9668 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9669 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9670 objfile_name (objfile));
900e11f9
JK
9671
9672 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9673 is available here - over computed PHYSNAME. It is safer
9674 against both buggy GDB and buggy compilers. */
9675
9676 retval = canon;
9677 }
9678 else
9679 {
9680 retval = physname;
9681 need_copy = 0;
9682 }
9683 }
9684 else
9685 retval = canon;
9686
9687 if (need_copy)
224c3ddb
SM
9688 retval = ((const char *)
9689 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9690 retval, strlen (retval)));
900e11f9 9691
900e11f9 9692 return retval;
0114d602
DJ
9693}
9694
74921315
KS
9695/* Inspect DIE in CU for a namespace alias. If one exists, record
9696 a new symbol for it.
9697
9698 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9699
9700static int
9701read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9702{
9703 struct attribute *attr;
9704
9705 /* If the die does not have a name, this is not a namespace
9706 alias. */
9707 attr = dwarf2_attr (die, DW_AT_name, cu);
9708 if (attr != NULL)
9709 {
9710 int num;
9711 struct die_info *d = die;
9712 struct dwarf2_cu *imported_cu = cu;
9713
9714 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9715 keep inspecting DIEs until we hit the underlying import. */
9716#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9717 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9718 {
9719 attr = dwarf2_attr (d, DW_AT_import, cu);
9720 if (attr == NULL)
9721 break;
9722
9723 d = follow_die_ref (d, attr, &imported_cu);
9724 if (d->tag != DW_TAG_imported_declaration)
9725 break;
9726 }
9727
9728 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9729 {
9730 complaint (&symfile_complaints,
9731 _("DIE at 0x%x has too many recursively imported "
9c541725 9732 "declarations"), to_underlying (d->sect_off));
74921315
KS
9733 return 0;
9734 }
9735
9736 if (attr != NULL)
9737 {
9738 struct type *type;
9c541725 9739 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9740
9c541725 9741 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9742 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9743 {
9744 /* This declaration is a global namespace alias. Add
9745 a symbol for it whose type is the aliased namespace. */
9746 new_symbol (die, type, cu);
9747 return 1;
9748 }
9749 }
9750 }
9751
9752 return 0;
9753}
9754
22cee43f
PMR
9755/* Return the using directives repository (global or local?) to use in the
9756 current context for LANGUAGE.
9757
9758 For Ada, imported declarations can materialize renamings, which *may* be
9759 global. However it is impossible (for now?) in DWARF to distinguish
9760 "external" imported declarations and "static" ones. As all imported
9761 declarations seem to be static in all other languages, make them all CU-wide
9762 global only in Ada. */
9763
9764static struct using_direct **
9765using_directives (enum language language)
9766{
9767 if (language == language_ada && context_stack_depth == 0)
9768 return &global_using_directives;
9769 else
9770 return &local_using_directives;
9771}
9772
27aa8d6a
SW
9773/* Read the import statement specified by the given die and record it. */
9774
9775static void
9776read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9777{
bb5ed363 9778 struct objfile *objfile = cu->objfile;
27aa8d6a 9779 struct attribute *import_attr;
32019081 9780 struct die_info *imported_die, *child_die;
de4affc9 9781 struct dwarf2_cu *imported_cu;
27aa8d6a 9782 const char *imported_name;
794684b6 9783 const char *imported_name_prefix;
13387711
SW
9784 const char *canonical_name;
9785 const char *import_alias;
9786 const char *imported_declaration = NULL;
794684b6 9787 const char *import_prefix;
eb1e02fd 9788 std::vector<const char *> excludes;
13387711 9789
27aa8d6a
SW
9790 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9791 if (import_attr == NULL)
9792 {
9793 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9794 dwarf_tag_name (die->tag));
9795 return;
9796 }
9797
de4affc9
CC
9798 imported_cu = cu;
9799 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9800 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9801 if (imported_name == NULL)
9802 {
9803 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9804
9805 The import in the following code:
9806 namespace A
9807 {
9808 typedef int B;
9809 }
9810
9811 int main ()
9812 {
9813 using A::B;
9814 B b;
9815 return b;
9816 }
9817
9818 ...
9819 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9820 <52> DW_AT_decl_file : 1
9821 <53> DW_AT_decl_line : 6
9822 <54> DW_AT_import : <0x75>
9823 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9824 <59> DW_AT_name : B
9825 <5b> DW_AT_decl_file : 1
9826 <5c> DW_AT_decl_line : 2
9827 <5d> DW_AT_type : <0x6e>
9828 ...
9829 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9830 <76> DW_AT_byte_size : 4
9831 <77> DW_AT_encoding : 5 (signed)
9832
9833 imports the wrong die ( 0x75 instead of 0x58 ).
9834 This case will be ignored until the gcc bug is fixed. */
9835 return;
9836 }
9837
82856980
SW
9838 /* Figure out the local name after import. */
9839 import_alias = dwarf2_name (die, cu);
27aa8d6a 9840
794684b6
SW
9841 /* Figure out where the statement is being imported to. */
9842 import_prefix = determine_prefix (die, cu);
9843
9844 /* Figure out what the scope of the imported die is and prepend it
9845 to the name of the imported die. */
de4affc9 9846 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9847
f55ee35c
JK
9848 if (imported_die->tag != DW_TAG_namespace
9849 && imported_die->tag != DW_TAG_module)
794684b6 9850 {
13387711
SW
9851 imported_declaration = imported_name;
9852 canonical_name = imported_name_prefix;
794684b6 9853 }
13387711 9854 else if (strlen (imported_name_prefix) > 0)
12aaed36 9855 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9856 imported_name_prefix,
9857 (cu->language == language_d ? "." : "::"),
9858 imported_name, (char *) NULL);
13387711
SW
9859 else
9860 canonical_name = imported_name;
794684b6 9861
32019081
JK
9862 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9863 for (child_die = die->child; child_die && child_die->tag;
9864 child_die = sibling_die (child_die))
9865 {
9866 /* DWARF-4: A Fortran use statement with a “rename list” may be
9867 represented by an imported module entry with an import attribute
9868 referring to the module and owned entries corresponding to those
9869 entities that are renamed as part of being imported. */
9870
9871 if (child_die->tag != DW_TAG_imported_declaration)
9872 {
9873 complaint (&symfile_complaints,
9874 _("child DW_TAG_imported_declaration expected "
9875 "- DIE at 0x%x [in module %s]"),
9c541725 9876 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9877 continue;
9878 }
9879
9880 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9881 if (import_attr == NULL)
9882 {
9883 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9884 dwarf_tag_name (child_die->tag));
9885 continue;
9886 }
9887
9888 imported_cu = cu;
9889 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9890 &imported_cu);
9891 imported_name = dwarf2_name (imported_die, imported_cu);
9892 if (imported_name == NULL)
9893 {
9894 complaint (&symfile_complaints,
9895 _("child DW_TAG_imported_declaration has unknown "
9896 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9897 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9898 continue;
9899 }
9900
eb1e02fd 9901 excludes.push_back (imported_name);
32019081
JK
9902
9903 process_die (child_die, cu);
9904 }
9905
22cee43f
PMR
9906 add_using_directive (using_directives (cu->language),
9907 import_prefix,
9908 canonical_name,
9909 import_alias,
9910 imported_declaration,
9911 excludes,
9912 0,
9913 &objfile->objfile_obstack);
27aa8d6a
SW
9914}
9915
5230b05a
WT
9916/* ICC<14 does not output the required DW_AT_declaration on incomplete
9917 types, but gives them a size of zero. Starting with version 14,
9918 ICC is compatible with GCC. */
9919
9920static int
9921producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9922{
9923 if (!cu->checked_producer)
9924 check_producer (cu);
9925
9926 return cu->producer_is_icc_lt_14;
9927}
9928
1b80a9fa
JK
9929/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9930 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9931 this, it was first present in GCC release 4.3.0. */
9932
9933static int
9934producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9935{
9936 if (!cu->checked_producer)
9937 check_producer (cu);
9938
9939 return cu->producer_is_gcc_lt_4_3;
9940}
9941
d721ba37
PA
9942static file_and_directory
9943find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9944{
d721ba37
PA
9945 file_and_directory res;
9946
9291a0cd
TT
9947 /* Find the filename. Do not use dwarf2_name here, since the filename
9948 is not a source language identifier. */
d721ba37
PA
9949 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9950 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9951
d721ba37
PA
9952 if (res.comp_dir == NULL
9953 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9954 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9955 {
d721ba37
PA
9956 res.comp_dir_storage = ldirname (res.name);
9957 if (!res.comp_dir_storage.empty ())
9958 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9959 }
d721ba37 9960 if (res.comp_dir != NULL)
9291a0cd
TT
9961 {
9962 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9963 directory, get rid of it. */
d721ba37 9964 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9965
d721ba37
PA
9966 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9967 res.comp_dir = cp + 1;
9291a0cd
TT
9968 }
9969
d721ba37
PA
9970 if (res.name == NULL)
9971 res.name = "<unknown>";
9972
9973 return res;
9291a0cd
TT
9974}
9975
f4dc4d17
DE
9976/* Handle DW_AT_stmt_list for a compilation unit.
9977 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9978 COMP_DIR is the compilation directory. LOWPC is passed to
9979 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9980
9981static void
9982handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9983 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9984{
527f3840 9985 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9986 struct attribute *attr;
527f3840
JK
9987 struct line_header line_header_local;
9988 hashval_t line_header_local_hash;
9989 unsigned u;
9990 void **slot;
9991 int decode_mapping;
2ab95328 9992
f4dc4d17
DE
9993 gdb_assert (! cu->per_cu->is_debug_types);
9994
2ab95328 9995 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9996 if (attr == NULL)
9997 return;
9998
9c541725 9999 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10000
10001 /* The line header hash table is only created if needed (it exists to
10002 prevent redundant reading of the line table for partial_units).
10003 If we're given a partial_unit, we'll need it. If we're given a
10004 compile_unit, then use the line header hash table if it's already
10005 created, but don't create one just yet. */
10006
10007 if (dwarf2_per_objfile->line_header_hash == NULL
10008 && die->tag == DW_TAG_partial_unit)
2ab95328 10009 {
527f3840
JK
10010 dwarf2_per_objfile->line_header_hash
10011 = htab_create_alloc_ex (127, line_header_hash_voidp,
10012 line_header_eq_voidp,
10013 free_line_header_voidp,
10014 &objfile->objfile_obstack,
10015 hashtab_obstack_allocate,
10016 dummy_obstack_deallocate);
10017 }
2ab95328 10018
9c541725 10019 line_header_local.sect_off = line_offset;
527f3840
JK
10020 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10021 line_header_local_hash = line_header_hash (&line_header_local);
10022 if (dwarf2_per_objfile->line_header_hash != NULL)
10023 {
10024 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10025 &line_header_local,
10026 line_header_local_hash, NO_INSERT);
10027
10028 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10029 is not present in *SLOT (since if there is something in *SLOT then
10030 it will be for a partial_unit). */
10031 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10032 {
527f3840 10033 gdb_assert (*slot != NULL);
9a3c8263 10034 cu->line_header = (struct line_header *) *slot;
527f3840 10035 return;
dee91e82 10036 }
2ab95328 10037 }
527f3840
JK
10038
10039 /* dwarf_decode_line_header does not yet provide sufficient information.
10040 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10041 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10042 if (lh == NULL)
527f3840 10043 return;
4c8aa72d
PA
10044
10045 cu->line_header = lh.release ();
10046 cu->line_header_die_owner = die;
527f3840
JK
10047
10048 if (dwarf2_per_objfile->line_header_hash == NULL)
10049 slot = NULL;
10050 else
10051 {
10052 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10053 &line_header_local,
10054 line_header_local_hash, INSERT);
10055 gdb_assert (slot != NULL);
10056 }
10057 if (slot != NULL && *slot == NULL)
10058 {
10059 /* This newly decoded line number information unit will be owned
10060 by line_header_hash hash table. */
10061 *slot = cu->line_header;
4c8aa72d 10062 cu->line_header_die_owner = NULL;
527f3840
JK
10063 }
10064 else
10065 {
10066 /* We cannot free any current entry in (*slot) as that struct line_header
10067 may be already used by multiple CUs. Create only temporary decoded
10068 line_header for this CU - it may happen at most once for each line
10069 number information unit. And if we're not using line_header_hash
10070 then this is what we want as well. */
10071 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10072 }
10073 decode_mapping = (die->tag != DW_TAG_partial_unit);
10074 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10075 decode_mapping);
fff8551c 10076
2ab95328
TT
10077}
10078
95554aad 10079/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10080
c906108c 10081static void
e7c27a73 10082read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10083{
dee91e82 10084 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10086 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10087 CORE_ADDR highpc = ((CORE_ADDR) 0);
10088 struct attribute *attr;
c906108c 10089 struct die_info *child_die;
e142c38c 10090 CORE_ADDR baseaddr;
6e70227d 10091
e142c38c 10092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10093
fae299cd 10094 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10095
10096 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10097 from finish_block. */
2acceee2 10098 if (lowpc == ((CORE_ADDR) -1))
c906108c 10099 lowpc = highpc;
3e29f34a 10100 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10101
d721ba37 10102 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10103
95554aad 10104 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 10105
f4b8a18d
KW
10106 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10107 standardised yet. As a workaround for the language detection we fall
10108 back to the DW_AT_producer string. */
10109 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10110 cu->language = language_opencl;
10111
3019eac3
DE
10112 /* Similar hack for Go. */
10113 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10114 set_cu_language (DW_LANG_Go, cu);
10115
d721ba37 10116 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10117
10118 /* Decode line number information if present. We do this before
10119 processing child DIEs, so that the line header table is available
10120 for DW_AT_decl_file. */
d721ba37 10121 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10122
10123 /* Process all dies in compilation unit. */
10124 if (die->child != NULL)
10125 {
10126 child_die = die->child;
10127 while (child_die && child_die->tag)
10128 {
10129 process_die (child_die, cu);
10130 child_die = sibling_die (child_die);
10131 }
10132 }
10133
10134 /* Decode macro information, if present. Dwarf 2 macro information
10135 refers to information in the line number info statement program
10136 header, so we can only read it if we've read the header
10137 successfully. */
0af92d60
JK
10138 attr = dwarf2_attr (die, DW_AT_macros, cu);
10139 if (attr == NULL)
10140 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10141 if (attr && cu->line_header)
10142 {
10143 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10144 complaint (&symfile_complaints,
0af92d60 10145 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10146
43f3e411 10147 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10148 }
10149 else
10150 {
10151 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10152 if (attr && cu->line_header)
10153 {
10154 unsigned int macro_offset = DW_UNSND (attr);
10155
43f3e411 10156 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10157 }
10158 }
3019eac3
DE
10159}
10160
f4dc4d17
DE
10161/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10162 Create the set of symtabs used by this TU, or if this TU is sharing
10163 symtabs with another TU and the symtabs have already been created
10164 then restore those symtabs in the line header.
10165 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
10166
10167static void
f4dc4d17 10168setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 10169{
f4dc4d17
DE
10170 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10171 struct type_unit_group *tu_group;
10172 int first_time;
3019eac3 10173 struct attribute *attr;
9c541725 10174 unsigned int i;
0186c6a7 10175 struct signatured_type *sig_type;
3019eac3 10176
f4dc4d17 10177 gdb_assert (per_cu->is_debug_types);
0186c6a7 10178 sig_type = (struct signatured_type *) per_cu;
3019eac3 10179
f4dc4d17 10180 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 10181
f4dc4d17 10182 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10183 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
10184 if (sig_type->type_unit_group == NULL)
10185 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10186 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10187
10188 /* If we've already processed this stmt_list there's no real need to
10189 do it again, we could fake it and just recreate the part we need
10190 (file name,index -> symtab mapping). If data shows this optimization
10191 is useful we can do it then. */
43f3e411 10192 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10193
10194 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10195 debug info. */
fff8551c 10196 line_header_up lh;
f4dc4d17 10197 if (attr != NULL)
3019eac3 10198 {
9c541725 10199 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
10200 lh = dwarf_decode_line_header (line_offset, cu);
10201 }
10202 if (lh == NULL)
10203 {
10204 if (first_time)
10205 dwarf2_start_symtab (cu, "", NULL, 0);
10206 else
10207 {
10208 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 10209 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10210 }
f4dc4d17 10211 return;
3019eac3
DE
10212 }
10213
4c8aa72d
PA
10214 cu->line_header = lh.release ();
10215 cu->line_header_die_owner = die;
3019eac3 10216
f4dc4d17
DE
10217 if (first_time)
10218 {
43f3e411 10219 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 10220
1fd60fc0
DE
10221 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10222 still initializing it, and our caller (a few levels up)
10223 process_full_type_unit still needs to know if this is the first
10224 time. */
10225
4c8aa72d
PA
10226 tu_group->num_symtabs = cu->line_header->file_names.size ();
10227 tu_group->symtabs = XNEWVEC (struct symtab *,
10228 cu->line_header->file_names.size ());
3019eac3 10229
4c8aa72d 10230 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10231 {
4c8aa72d 10232 file_entry &fe = cu->line_header->file_names[i];
3019eac3 10233
4c8aa72d 10234 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 10235
f4dc4d17
DE
10236 if (current_subfile->symtab == NULL)
10237 {
4c8aa72d
PA
10238 /* NOTE: start_subfile will recognize when it's been
10239 passed a file it has already seen. So we can't
10240 assume there's a simple mapping from
10241 cu->line_header->file_names to subfiles, plus
10242 cu->line_header->file_names may contain dups. */
43f3e411
DE
10243 current_subfile->symtab
10244 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
10245 }
10246
8c43009f
PA
10247 fe.symtab = current_subfile->symtab;
10248 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10249 }
10250 }
10251 else
3019eac3 10252 {
0ab9ce85 10253 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10254
4c8aa72d 10255 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10256 {
4c8aa72d 10257 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 10258
4c8aa72d 10259 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10260 }
3019eac3
DE
10261 }
10262
f4dc4d17
DE
10263 /* The main symtab is allocated last. Type units don't have DW_AT_name
10264 so they don't have a "real" (so to speak) symtab anyway.
10265 There is later code that will assign the main symtab to all symbols
10266 that don't have one. We need to handle the case of a symbol with a
10267 missing symtab (DW_AT_decl_file) anyway. */
10268}
3019eac3 10269
f4dc4d17
DE
10270/* Process DW_TAG_type_unit.
10271 For TUs we want to skip the first top level sibling if it's not the
10272 actual type being defined by this TU. In this case the first top
10273 level sibling is there to provide context only. */
3019eac3 10274
f4dc4d17
DE
10275static void
10276read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10277{
10278 struct die_info *child_die;
3019eac3 10279
f4dc4d17
DE
10280 prepare_one_comp_unit (cu, die, language_minimal);
10281
10282 /* Initialize (or reinitialize) the machinery for building symtabs.
10283 We do this before processing child DIEs, so that the line header table
10284 is available for DW_AT_decl_file. */
10285 setup_type_unit_groups (die, cu);
10286
10287 if (die->child != NULL)
10288 {
10289 child_die = die->child;
10290 while (child_die && child_die->tag)
10291 {
10292 process_die (child_die, cu);
10293 child_die = sibling_die (child_die);
10294 }
10295 }
3019eac3
DE
10296}
10297\f
80626a55
DE
10298/* DWO/DWP files.
10299
10300 http://gcc.gnu.org/wiki/DebugFission
10301 http://gcc.gnu.org/wiki/DebugFissionDWP
10302
10303 To simplify handling of both DWO files ("object" files with the DWARF info)
10304 and DWP files (a file with the DWOs packaged up into one file), we treat
10305 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10306
10307static hashval_t
10308hash_dwo_file (const void *item)
10309{
9a3c8263 10310 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10311 hashval_t hash;
3019eac3 10312
a2ce51a0
DE
10313 hash = htab_hash_string (dwo_file->dwo_name);
10314 if (dwo_file->comp_dir != NULL)
10315 hash += htab_hash_string (dwo_file->comp_dir);
10316 return hash;
3019eac3
DE
10317}
10318
10319static int
10320eq_dwo_file (const void *item_lhs, const void *item_rhs)
10321{
9a3c8263
SM
10322 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10323 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10324
a2ce51a0
DE
10325 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10326 return 0;
10327 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10328 return lhs->comp_dir == rhs->comp_dir;
10329 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10330}
10331
10332/* Allocate a hash table for DWO files. */
10333
10334static htab_t
10335allocate_dwo_file_hash_table (void)
10336{
10337 struct objfile *objfile = dwarf2_per_objfile->objfile;
10338
10339 return htab_create_alloc_ex (41,
10340 hash_dwo_file,
10341 eq_dwo_file,
10342 NULL,
10343 &objfile->objfile_obstack,
10344 hashtab_obstack_allocate,
10345 dummy_obstack_deallocate);
10346}
10347
80626a55
DE
10348/* Lookup DWO file DWO_NAME. */
10349
10350static void **
0ac5b59e 10351lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
10352{
10353 struct dwo_file find_entry;
10354 void **slot;
10355
10356 if (dwarf2_per_objfile->dwo_files == NULL)
10357 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10358
10359 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
10360 find_entry.dwo_name = dwo_name;
10361 find_entry.comp_dir = comp_dir;
80626a55
DE
10362 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10363
10364 return slot;
10365}
10366
3019eac3
DE
10367static hashval_t
10368hash_dwo_unit (const void *item)
10369{
9a3c8263 10370 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10371
10372 /* This drops the top 32 bits of the id, but is ok for a hash. */
10373 return dwo_unit->signature;
10374}
10375
10376static int
10377eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10378{
9a3c8263
SM
10379 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10380 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10381
10382 /* The signature is assumed to be unique within the DWO file.
10383 So while object file CU dwo_id's always have the value zero,
10384 that's OK, assuming each object file DWO file has only one CU,
10385 and that's the rule for now. */
10386 return lhs->signature == rhs->signature;
10387}
10388
10389/* Allocate a hash table for DWO CUs,TUs.
10390 There is one of these tables for each of CUs,TUs for each DWO file. */
10391
10392static htab_t
10393allocate_dwo_unit_table (struct objfile *objfile)
10394{
10395 /* Start out with a pretty small number.
10396 Generally DWO files contain only one CU and maybe some TUs. */
10397 return htab_create_alloc_ex (3,
10398 hash_dwo_unit,
10399 eq_dwo_unit,
10400 NULL,
10401 &objfile->objfile_obstack,
10402 hashtab_obstack_allocate,
10403 dummy_obstack_deallocate);
10404}
10405
80626a55 10406/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 10407
19c3d4c9 10408struct create_dwo_cu_data
3019eac3
DE
10409{
10410 struct dwo_file *dwo_file;
19c3d4c9 10411 struct dwo_unit dwo_unit;
3019eac3
DE
10412};
10413
19c3d4c9 10414/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10415
10416static void
19c3d4c9
DE
10417create_dwo_cu_reader (const struct die_reader_specs *reader,
10418 const gdb_byte *info_ptr,
10419 struct die_info *comp_unit_die,
10420 int has_children,
10421 void *datap)
3019eac3
DE
10422{
10423 struct dwarf2_cu *cu = reader->cu;
9c541725 10424 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10425 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 10426 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 10427 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 10428 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 10429 struct attribute *attr;
3019eac3
DE
10430
10431 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10432 if (attr == NULL)
10433 {
19c3d4c9
DE
10434 complaint (&symfile_complaints,
10435 _("Dwarf Error: debug entry at offset 0x%x is missing"
10436 " its dwo_id [in module %s]"),
9c541725 10437 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
10438 return;
10439 }
10440
3019eac3
DE
10441 dwo_unit->dwo_file = dwo_file;
10442 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 10443 dwo_unit->section = section;
9c541725 10444 dwo_unit->sect_off = sect_off;
3019eac3
DE
10445 dwo_unit->length = cu->per_cu->length;
10446
b4f54984 10447 if (dwarf_read_debug)
4031ecc5 10448 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
10449 to_underlying (sect_off),
10450 hex_string (dwo_unit->signature));
3019eac3
DE
10451}
10452
33c5cd75 10453/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10454 Note: This function processes DWO files only, not DWP files. */
3019eac3 10455
33c5cd75
DB
10456static void
10457create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10458 htab_t &cus_htab)
3019eac3
DE
10459{
10460 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 10461 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 10462 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10463
33c5cd75
DB
10464 dwarf2_read_section (objfile, &section);
10465 info_ptr = section.buffer;
3019eac3
DE
10466
10467 if (info_ptr == NULL)
33c5cd75 10468 return;
3019eac3 10469
b4f54984 10470 if (dwarf_read_debug)
19c3d4c9
DE
10471 {
10472 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
10473 get_section_name (&section),
10474 get_section_file_name (&section));
19c3d4c9 10475 }
3019eac3 10476
33c5cd75 10477 end_ptr = info_ptr + section.size;
3019eac3
DE
10478 while (info_ptr < end_ptr)
10479 {
10480 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
10481 struct create_dwo_cu_data create_dwo_cu_data;
10482 struct dwo_unit *dwo_unit;
10483 void **slot;
10484 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10485
19c3d4c9
DE
10486 memset (&create_dwo_cu_data.dwo_unit, 0,
10487 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
10488 memset (&per_cu, 0, sizeof (per_cu));
10489 per_cu.objfile = objfile;
10490 per_cu.is_debug_types = 0;
33c5cd75
DB
10491 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10492 per_cu.section = &section;
c5ed0576 10493 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
10494
10495 init_cutu_and_read_dies_no_follow (
10496 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10497 info_ptr += per_cu.length;
10498
10499 // If the unit could not be parsed, skip it.
10500 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10501 continue;
3019eac3 10502
33c5cd75
DB
10503 if (cus_htab == NULL)
10504 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10505
33c5cd75
DB
10506 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10507 *dwo_unit = create_dwo_cu_data.dwo_unit;
10508 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10509 gdb_assert (slot != NULL);
10510 if (*slot != NULL)
19c3d4c9 10511 {
33c5cd75
DB
10512 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10513 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10514
33c5cd75
DB
10515 complaint (&symfile_complaints,
10516 _("debug cu entry at offset 0x%x is duplicate to"
10517 " the entry at offset 0x%x, signature %s"),
10518 to_underlying (sect_off), to_underlying (dup_sect_off),
10519 hex_string (dwo_unit->signature));
19c3d4c9 10520 }
33c5cd75 10521 *slot = (void *)dwo_unit;
3019eac3 10522 }
3019eac3
DE
10523}
10524
80626a55
DE
10525/* DWP file .debug_{cu,tu}_index section format:
10526 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10527
d2415c6c
DE
10528 DWP Version 1:
10529
80626a55
DE
10530 Both index sections have the same format, and serve to map a 64-bit
10531 signature to a set of section numbers. Each section begins with a header,
10532 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10533 indexes, and a pool of 32-bit section numbers. The index sections will be
10534 aligned at 8-byte boundaries in the file.
10535
d2415c6c
DE
10536 The index section header consists of:
10537
10538 V, 32 bit version number
10539 -, 32 bits unused
10540 N, 32 bit number of compilation units or type units in the index
10541 M, 32 bit number of slots in the hash table
80626a55 10542
d2415c6c 10543 Numbers are recorded using the byte order of the application binary.
80626a55 10544
d2415c6c
DE
10545 The hash table begins at offset 16 in the section, and consists of an array
10546 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10547 order of the application binary). Unused slots in the hash table are 0.
10548 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10549
d2415c6c
DE
10550 The parallel table begins immediately after the hash table
10551 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10552 array of 32-bit indexes (using the byte order of the application binary),
10553 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10554 table contains a 32-bit index into the pool of section numbers. For unused
10555 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10556
73869dc2
DE
10557 The pool of section numbers begins immediately following the hash table
10558 (at offset 16 + 12 * M from the beginning of the section). The pool of
10559 section numbers consists of an array of 32-bit words (using the byte order
10560 of the application binary). Each item in the array is indexed starting
10561 from 0. The hash table entry provides the index of the first section
10562 number in the set. Additional section numbers in the set follow, and the
10563 set is terminated by a 0 entry (section number 0 is not used in ELF).
10564
10565 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10566 section must be the first entry in the set, and the .debug_abbrev.dwo must
10567 be the second entry. Other members of the set may follow in any order.
10568
10569 ---
10570
10571 DWP Version 2:
10572
10573 DWP Version 2 combines all the .debug_info, etc. sections into one,
10574 and the entries in the index tables are now offsets into these sections.
10575 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10576 section.
10577
10578 Index Section Contents:
10579 Header
10580 Hash Table of Signatures dwp_hash_table.hash_table
10581 Parallel Table of Indices dwp_hash_table.unit_table
10582 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10583 Table of Section Sizes dwp_hash_table.v2.sizes
10584
10585 The index section header consists of:
10586
10587 V, 32 bit version number
10588 L, 32 bit number of columns in the table of section offsets
10589 N, 32 bit number of compilation units or type units in the index
10590 M, 32 bit number of slots in the hash table
10591
10592 Numbers are recorded using the byte order of the application binary.
10593
10594 The hash table has the same format as version 1.
10595 The parallel table of indices has the same format as version 1,
10596 except that the entries are origin-1 indices into the table of sections
10597 offsets and the table of section sizes.
10598
10599 The table of offsets begins immediately following the parallel table
10600 (at offset 16 + 12 * M from the beginning of the section). The table is
10601 a two-dimensional array of 32-bit words (using the byte order of the
10602 application binary), with L columns and N+1 rows, in row-major order.
10603 Each row in the array is indexed starting from 0. The first row provides
10604 a key to the remaining rows: each column in this row provides an identifier
10605 for a debug section, and the offsets in the same column of subsequent rows
10606 refer to that section. The section identifiers are:
10607
10608 DW_SECT_INFO 1 .debug_info.dwo
10609 DW_SECT_TYPES 2 .debug_types.dwo
10610 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10611 DW_SECT_LINE 4 .debug_line.dwo
10612 DW_SECT_LOC 5 .debug_loc.dwo
10613 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10614 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10615 DW_SECT_MACRO 8 .debug_macro.dwo
10616
10617 The offsets provided by the CU and TU index sections are the base offsets
10618 for the contributions made by each CU or TU to the corresponding section
10619 in the package file. Each CU and TU header contains an abbrev_offset
10620 field, used to find the abbreviations table for that CU or TU within the
10621 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10622 be interpreted as relative to the base offset given in the index section.
10623 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10624 should be interpreted as relative to the base offset for .debug_line.dwo,
10625 and offsets into other debug sections obtained from DWARF attributes should
10626 also be interpreted as relative to the corresponding base offset.
10627
10628 The table of sizes begins immediately following the table of offsets.
10629 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10630 with L columns and N rows, in row-major order. Each row in the array is
10631 indexed starting from 1 (row 0 is shared by the two tables).
10632
10633 ---
10634
10635 Hash table lookup is handled the same in version 1 and 2:
10636
10637 We assume that N and M will not exceed 2^32 - 1.
10638 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10639
d2415c6c
DE
10640 Given a 64-bit compilation unit signature or a type signature S, an entry
10641 in the hash table is located as follows:
80626a55 10642
d2415c6c
DE
10643 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10644 the low-order k bits all set to 1.
80626a55 10645
d2415c6c 10646 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10647
d2415c6c
DE
10648 3) If the hash table entry at index H matches the signature, use that
10649 entry. If the hash table entry at index H is unused (all zeroes),
10650 terminate the search: the signature is not present in the table.
80626a55 10651
d2415c6c 10652 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10653
d2415c6c 10654 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10655 to stop at an unused slot or find the match. */
80626a55
DE
10656
10657/* Create a hash table to map DWO IDs to their CU/TU entry in
10658 .debug_{info,types}.dwo in DWP_FILE.
10659 Returns NULL if there isn't one.
10660 Note: This function processes DWP files only, not DWO files. */
10661
10662static struct dwp_hash_table *
10663create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10664{
10665 struct objfile *objfile = dwarf2_per_objfile->objfile;
10666 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10667 const gdb_byte *index_ptr, *index_end;
80626a55 10668 struct dwarf2_section_info *index;
73869dc2 10669 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10670 struct dwp_hash_table *htab;
10671
10672 if (is_debug_types)
10673 index = &dwp_file->sections.tu_index;
10674 else
10675 index = &dwp_file->sections.cu_index;
10676
10677 if (dwarf2_section_empty_p (index))
10678 return NULL;
10679 dwarf2_read_section (objfile, index);
10680
10681 index_ptr = index->buffer;
10682 index_end = index_ptr + index->size;
10683
10684 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10685 index_ptr += 4;
10686 if (version == 2)
10687 nr_columns = read_4_bytes (dbfd, index_ptr);
10688 else
10689 nr_columns = 0;
10690 index_ptr += 4;
80626a55
DE
10691 nr_units = read_4_bytes (dbfd, index_ptr);
10692 index_ptr += 4;
10693 nr_slots = read_4_bytes (dbfd, index_ptr);
10694 index_ptr += 4;
10695
73869dc2 10696 if (version != 1 && version != 2)
80626a55 10697 {
21aa081e 10698 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10699 " [in module %s]"),
21aa081e 10700 pulongest (version), dwp_file->name);
80626a55
DE
10701 }
10702 if (nr_slots != (nr_slots & -nr_slots))
10703 {
21aa081e 10704 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10705 " is not power of 2 [in module %s]"),
21aa081e 10706 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10707 }
10708
10709 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10710 htab->version = version;
10711 htab->nr_columns = nr_columns;
80626a55
DE
10712 htab->nr_units = nr_units;
10713 htab->nr_slots = nr_slots;
10714 htab->hash_table = index_ptr;
10715 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10716
10717 /* Exit early if the table is empty. */
10718 if (nr_slots == 0 || nr_units == 0
10719 || (version == 2 && nr_columns == 0))
10720 {
10721 /* All must be zero. */
10722 if (nr_slots != 0 || nr_units != 0
10723 || (version == 2 && nr_columns != 0))
10724 {
10725 complaint (&symfile_complaints,
10726 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10727 " all zero [in modules %s]"),
10728 dwp_file->name);
10729 }
10730 return htab;
10731 }
10732
10733 if (version == 1)
10734 {
10735 htab->section_pool.v1.indices =
10736 htab->unit_table + sizeof (uint32_t) * nr_slots;
10737 /* It's harder to decide whether the section is too small in v1.
10738 V1 is deprecated anyway so we punt. */
10739 }
10740 else
10741 {
10742 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10743 int *ids = htab->section_pool.v2.section_ids;
10744 /* Reverse map for error checking. */
10745 int ids_seen[DW_SECT_MAX + 1];
10746 int i;
10747
10748 if (nr_columns < 2)
10749 {
10750 error (_("Dwarf Error: bad DWP hash table, too few columns"
10751 " in section table [in module %s]"),
10752 dwp_file->name);
10753 }
10754 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10755 {
10756 error (_("Dwarf Error: bad DWP hash table, too many columns"
10757 " in section table [in module %s]"),
10758 dwp_file->name);
10759 }
10760 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10761 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10762 for (i = 0; i < nr_columns; ++i)
10763 {
10764 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10765
10766 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10767 {
10768 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10769 " in section table [in module %s]"),
10770 id, dwp_file->name);
10771 }
10772 if (ids_seen[id] != -1)
10773 {
10774 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10775 " id %d in section table [in module %s]"),
10776 id, dwp_file->name);
10777 }
10778 ids_seen[id] = i;
10779 ids[i] = id;
10780 }
10781 /* Must have exactly one info or types section. */
10782 if (((ids_seen[DW_SECT_INFO] != -1)
10783 + (ids_seen[DW_SECT_TYPES] != -1))
10784 != 1)
10785 {
10786 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10787 " DWO info/types section [in module %s]"),
10788 dwp_file->name);
10789 }
10790 /* Must have an abbrev section. */
10791 if (ids_seen[DW_SECT_ABBREV] == -1)
10792 {
10793 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10794 " section [in module %s]"),
10795 dwp_file->name);
10796 }
10797 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10798 htab->section_pool.v2.sizes =
10799 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10800 * nr_units * nr_columns);
10801 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10802 * nr_units * nr_columns))
10803 > index_end)
10804 {
10805 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10806 " [in module %s]"),
10807 dwp_file->name);
10808 }
10809 }
80626a55
DE
10810
10811 return htab;
10812}
10813
10814/* Update SECTIONS with the data from SECTP.
10815
10816 This function is like the other "locate" section routines that are
10817 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10818 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10819
10820 The result is non-zero for success, or zero if an error was found. */
10821
10822static int
73869dc2
DE
10823locate_v1_virtual_dwo_sections (asection *sectp,
10824 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10825{
10826 const struct dwop_section_names *names = &dwop_section_names;
10827
10828 if (section_is_p (sectp->name, &names->abbrev_dwo))
10829 {
10830 /* There can be only one. */
049412e3 10831 if (sections->abbrev.s.section != NULL)
80626a55 10832 return 0;
049412e3 10833 sections->abbrev.s.section = sectp;
80626a55
DE
10834 sections->abbrev.size = bfd_get_section_size (sectp);
10835 }
10836 else if (section_is_p (sectp->name, &names->info_dwo)
10837 || section_is_p (sectp->name, &names->types_dwo))
10838 {
10839 /* There can be only one. */
049412e3 10840 if (sections->info_or_types.s.section != NULL)
80626a55 10841 return 0;
049412e3 10842 sections->info_or_types.s.section = sectp;
80626a55
DE
10843 sections->info_or_types.size = bfd_get_section_size (sectp);
10844 }
10845 else if (section_is_p (sectp->name, &names->line_dwo))
10846 {
10847 /* There can be only one. */
049412e3 10848 if (sections->line.s.section != NULL)
80626a55 10849 return 0;
049412e3 10850 sections->line.s.section = sectp;
80626a55
DE
10851 sections->line.size = bfd_get_section_size (sectp);
10852 }
10853 else if (section_is_p (sectp->name, &names->loc_dwo))
10854 {
10855 /* There can be only one. */
049412e3 10856 if (sections->loc.s.section != NULL)
80626a55 10857 return 0;
049412e3 10858 sections->loc.s.section = sectp;
80626a55
DE
10859 sections->loc.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10862 {
10863 /* There can be only one. */
049412e3 10864 if (sections->macinfo.s.section != NULL)
80626a55 10865 return 0;
049412e3 10866 sections->macinfo.s.section = sectp;
80626a55
DE
10867 sections->macinfo.size = bfd_get_section_size (sectp);
10868 }
10869 else if (section_is_p (sectp->name, &names->macro_dwo))
10870 {
10871 /* There can be only one. */
049412e3 10872 if (sections->macro.s.section != NULL)
80626a55 10873 return 0;
049412e3 10874 sections->macro.s.section = sectp;
80626a55
DE
10875 sections->macro.size = bfd_get_section_size (sectp);
10876 }
10877 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10878 {
10879 /* There can be only one. */
049412e3 10880 if (sections->str_offsets.s.section != NULL)
80626a55 10881 return 0;
049412e3 10882 sections->str_offsets.s.section = sectp;
80626a55
DE
10883 sections->str_offsets.size = bfd_get_section_size (sectp);
10884 }
10885 else
10886 {
10887 /* No other kind of section is valid. */
10888 return 0;
10889 }
10890
10891 return 1;
10892}
10893
73869dc2
DE
10894/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10895 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10896 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10897 This is for DWP version 1 files. */
80626a55
DE
10898
10899static struct dwo_unit *
73869dc2
DE
10900create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10901 uint32_t unit_index,
10902 const char *comp_dir,
10903 ULONGEST signature, int is_debug_types)
80626a55
DE
10904{
10905 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10906 const struct dwp_hash_table *dwp_htab =
10907 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10908 bfd *dbfd = dwp_file->dbfd;
10909 const char *kind = is_debug_types ? "TU" : "CU";
10910 struct dwo_file *dwo_file;
10911 struct dwo_unit *dwo_unit;
73869dc2 10912 struct virtual_v1_dwo_sections sections;
80626a55 10913 void **dwo_file_slot;
80626a55
DE
10914 int i;
10915
73869dc2
DE
10916 gdb_assert (dwp_file->version == 1);
10917
b4f54984 10918 if (dwarf_read_debug)
80626a55 10919 {
73869dc2 10920 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10921 kind,
73869dc2 10922 pulongest (unit_index), hex_string (signature),
80626a55
DE
10923 dwp_file->name);
10924 }
10925
19ac8c2e 10926 /* Fetch the sections of this DWO unit.
80626a55
DE
10927 Put a limit on the number of sections we look for so that bad data
10928 doesn't cause us to loop forever. */
10929
73869dc2 10930#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10931 (1 /* .debug_info or .debug_types */ \
10932 + 1 /* .debug_abbrev */ \
10933 + 1 /* .debug_line */ \
10934 + 1 /* .debug_loc */ \
10935 + 1 /* .debug_str_offsets */ \
19ac8c2e 10936 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10937 + 1 /* trailing zero */)
10938
10939 memset (&sections, 0, sizeof (sections));
80626a55 10940
73869dc2 10941 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10942 {
10943 asection *sectp;
10944 uint32_t section_nr =
10945 read_4_bytes (dbfd,
73869dc2
DE
10946 dwp_htab->section_pool.v1.indices
10947 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10948
10949 if (section_nr == 0)
10950 break;
10951 if (section_nr >= dwp_file->num_sections)
10952 {
10953 error (_("Dwarf Error: bad DWP hash table, section number too large"
10954 " [in module %s]"),
10955 dwp_file->name);
10956 }
10957
10958 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10959 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10960 {
10961 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10962 " [in module %s]"),
10963 dwp_file->name);
10964 }
10965 }
10966
10967 if (i < 2
a32a8923
DE
10968 || dwarf2_section_empty_p (&sections.info_or_types)
10969 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10970 {
10971 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10972 " [in module %s]"),
10973 dwp_file->name);
10974 }
73869dc2 10975 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10976 {
10977 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10978 " [in module %s]"),
10979 dwp_file->name);
10980 }
10981
10982 /* It's easier for the rest of the code if we fake a struct dwo_file and
10983 have dwo_unit "live" in that. At least for now.
10984
10985 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10986 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10987 file, we can combine them back into a virtual DWO file to save space
10988 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10989 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10990
791afaa2
TT
10991 std::string virtual_dwo_name =
10992 string_printf ("virtual-dwo/%d-%d-%d-%d",
10993 get_section_id (&sections.abbrev),
10994 get_section_id (&sections.line),
10995 get_section_id (&sections.loc),
10996 get_section_id (&sections.str_offsets));
80626a55 10997 /* Can we use an existing virtual DWO file? */
791afaa2 10998 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
10999 /* Create one if necessary. */
11000 if (*dwo_file_slot == NULL)
11001 {
b4f54984 11002 if (dwarf_read_debug)
80626a55
DE
11003 {
11004 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11005 virtual_dwo_name.c_str ());
80626a55
DE
11006 }
11007 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11008 dwo_file->dwo_name
11009 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11010 virtual_dwo_name.c_str (),
11011 virtual_dwo_name.size ());
0ac5b59e 11012 dwo_file->comp_dir = comp_dir;
80626a55
DE
11013 dwo_file->sections.abbrev = sections.abbrev;
11014 dwo_file->sections.line = sections.line;
11015 dwo_file->sections.loc = sections.loc;
11016 dwo_file->sections.macinfo = sections.macinfo;
11017 dwo_file->sections.macro = sections.macro;
11018 dwo_file->sections.str_offsets = sections.str_offsets;
11019 /* The "str" section is global to the entire DWP file. */
11020 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11021 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11022 there's no need to record it in dwo_file.
11023 Also, we can't simply record type sections in dwo_file because
11024 we record a pointer into the vector in dwo_unit. As we collect more
11025 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11026 for it, invalidating all copies of pointers into the previous
11027 contents. */
80626a55
DE
11028 *dwo_file_slot = dwo_file;
11029 }
11030 else
11031 {
b4f54984 11032 if (dwarf_read_debug)
80626a55
DE
11033 {
11034 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11035 virtual_dwo_name.c_str ());
80626a55 11036 }
9a3c8263 11037 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11038 }
80626a55
DE
11039
11040 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11041 dwo_unit->dwo_file = dwo_file;
11042 dwo_unit->signature = signature;
8d749320
SM
11043 dwo_unit->section =
11044 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11045 *dwo_unit->section = sections.info_or_types;
57d63ce2 11046 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11047
11048 return dwo_unit;
11049}
11050
73869dc2
DE
11051/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11052 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11053 piece within that section used by a TU/CU, return a virtual section
11054 of just that piece. */
11055
11056static struct dwarf2_section_info
11057create_dwp_v2_section (struct dwarf2_section_info *section,
11058 bfd_size_type offset, bfd_size_type size)
11059{
11060 struct dwarf2_section_info result;
11061 asection *sectp;
11062
11063 gdb_assert (section != NULL);
11064 gdb_assert (!section->is_virtual);
11065
11066 memset (&result, 0, sizeof (result));
11067 result.s.containing_section = section;
11068 result.is_virtual = 1;
11069
11070 if (size == 0)
11071 return result;
11072
11073 sectp = get_section_bfd_section (section);
11074
11075 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11076 bounds of the real section. This is a pretty-rare event, so just
11077 flag an error (easier) instead of a warning and trying to cope. */
11078 if (sectp == NULL
11079 || offset + size > bfd_get_section_size (sectp))
11080 {
11081 bfd *abfd = sectp->owner;
11082
11083 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11084 " in section %s [in module %s]"),
11085 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11086 objfile_name (dwarf2_per_objfile->objfile));
11087 }
11088
11089 result.virtual_offset = offset;
11090 result.size = size;
11091 return result;
11092}
11093
11094/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11095 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11096 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11097 This is for DWP version 2 files. */
11098
11099static struct dwo_unit *
11100create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11101 uint32_t unit_index,
11102 const char *comp_dir,
11103 ULONGEST signature, int is_debug_types)
11104{
11105 struct objfile *objfile = dwarf2_per_objfile->objfile;
11106 const struct dwp_hash_table *dwp_htab =
11107 is_debug_types ? dwp_file->tus : dwp_file->cus;
11108 bfd *dbfd = dwp_file->dbfd;
11109 const char *kind = is_debug_types ? "TU" : "CU";
11110 struct dwo_file *dwo_file;
11111 struct dwo_unit *dwo_unit;
11112 struct virtual_v2_dwo_sections sections;
11113 void **dwo_file_slot;
73869dc2
DE
11114 int i;
11115
11116 gdb_assert (dwp_file->version == 2);
11117
b4f54984 11118 if (dwarf_read_debug)
73869dc2
DE
11119 {
11120 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11121 kind,
11122 pulongest (unit_index), hex_string (signature),
11123 dwp_file->name);
11124 }
11125
11126 /* Fetch the section offsets of this DWO unit. */
11127
11128 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11129
11130 for (i = 0; i < dwp_htab->nr_columns; ++i)
11131 {
11132 uint32_t offset = read_4_bytes (dbfd,
11133 dwp_htab->section_pool.v2.offsets
11134 + (((unit_index - 1) * dwp_htab->nr_columns
11135 + i)
11136 * sizeof (uint32_t)));
11137 uint32_t size = read_4_bytes (dbfd,
11138 dwp_htab->section_pool.v2.sizes
11139 + (((unit_index - 1) * dwp_htab->nr_columns
11140 + i)
11141 * sizeof (uint32_t)));
11142
11143 switch (dwp_htab->section_pool.v2.section_ids[i])
11144 {
11145 case DW_SECT_INFO:
11146 case DW_SECT_TYPES:
11147 sections.info_or_types_offset = offset;
11148 sections.info_or_types_size = size;
11149 break;
11150 case DW_SECT_ABBREV:
11151 sections.abbrev_offset = offset;
11152 sections.abbrev_size = size;
11153 break;
11154 case DW_SECT_LINE:
11155 sections.line_offset = offset;
11156 sections.line_size = size;
11157 break;
11158 case DW_SECT_LOC:
11159 sections.loc_offset = offset;
11160 sections.loc_size = size;
11161 break;
11162 case DW_SECT_STR_OFFSETS:
11163 sections.str_offsets_offset = offset;
11164 sections.str_offsets_size = size;
11165 break;
11166 case DW_SECT_MACINFO:
11167 sections.macinfo_offset = offset;
11168 sections.macinfo_size = size;
11169 break;
11170 case DW_SECT_MACRO:
11171 sections.macro_offset = offset;
11172 sections.macro_size = size;
11173 break;
11174 }
11175 }
11176
11177 /* It's easier for the rest of the code if we fake a struct dwo_file and
11178 have dwo_unit "live" in that. At least for now.
11179
11180 The DWP file can be made up of a random collection of CUs and TUs.
11181 However, for each CU + set of TUs that came from the same original DWO
11182 file, we can combine them back into a virtual DWO file to save space
11183 (fewer struct dwo_file objects to allocate). Remember that for really
11184 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11185
791afaa2
TT
11186 std::string virtual_dwo_name =
11187 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11188 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11189 (long) (sections.line_size ? sections.line_offset : 0),
11190 (long) (sections.loc_size ? sections.loc_offset : 0),
11191 (long) (sections.str_offsets_size
11192 ? sections.str_offsets_offset : 0));
73869dc2 11193 /* Can we use an existing virtual DWO file? */
791afaa2 11194 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
11195 /* Create one if necessary. */
11196 if (*dwo_file_slot == NULL)
11197 {
b4f54984 11198 if (dwarf_read_debug)
73869dc2
DE
11199 {
11200 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11201 virtual_dwo_name.c_str ());
73869dc2
DE
11202 }
11203 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11204 dwo_file->dwo_name
11205 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11206 virtual_dwo_name.c_str (),
11207 virtual_dwo_name.size ());
73869dc2
DE
11208 dwo_file->comp_dir = comp_dir;
11209 dwo_file->sections.abbrev =
11210 create_dwp_v2_section (&dwp_file->sections.abbrev,
11211 sections.abbrev_offset, sections.abbrev_size);
11212 dwo_file->sections.line =
11213 create_dwp_v2_section (&dwp_file->sections.line,
11214 sections.line_offset, sections.line_size);
11215 dwo_file->sections.loc =
11216 create_dwp_v2_section (&dwp_file->sections.loc,
11217 sections.loc_offset, sections.loc_size);
11218 dwo_file->sections.macinfo =
11219 create_dwp_v2_section (&dwp_file->sections.macinfo,
11220 sections.macinfo_offset, sections.macinfo_size);
11221 dwo_file->sections.macro =
11222 create_dwp_v2_section (&dwp_file->sections.macro,
11223 sections.macro_offset, sections.macro_size);
11224 dwo_file->sections.str_offsets =
11225 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11226 sections.str_offsets_offset,
11227 sections.str_offsets_size);
11228 /* The "str" section is global to the entire DWP file. */
11229 dwo_file->sections.str = dwp_file->sections.str;
11230 /* The info or types section is assigned below to dwo_unit,
11231 there's no need to record it in dwo_file.
11232 Also, we can't simply record type sections in dwo_file because
11233 we record a pointer into the vector in dwo_unit. As we collect more
11234 types we'll grow the vector and eventually have to reallocate space
11235 for it, invalidating all copies of pointers into the previous
11236 contents. */
11237 *dwo_file_slot = dwo_file;
11238 }
11239 else
11240 {
b4f54984 11241 if (dwarf_read_debug)
73869dc2
DE
11242 {
11243 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11244 virtual_dwo_name.c_str ());
73869dc2 11245 }
9a3c8263 11246 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11247 }
73869dc2
DE
11248
11249 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11250 dwo_unit->dwo_file = dwo_file;
11251 dwo_unit->signature = signature;
8d749320
SM
11252 dwo_unit->section =
11253 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
11254 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11255 ? &dwp_file->sections.types
11256 : &dwp_file->sections.info,
11257 sections.info_or_types_offset,
11258 sections.info_or_types_size);
11259 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11260
11261 return dwo_unit;
11262}
11263
57d63ce2
DE
11264/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11265 Returns NULL if the signature isn't found. */
80626a55
DE
11266
11267static struct dwo_unit *
57d63ce2
DE
11268lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11269 ULONGEST signature, int is_debug_types)
80626a55 11270{
57d63ce2
DE
11271 const struct dwp_hash_table *dwp_htab =
11272 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 11273 bfd *dbfd = dwp_file->dbfd;
57d63ce2 11274 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11275 uint32_t hash = signature & mask;
11276 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11277 unsigned int i;
11278 void **slot;
870f88f7 11279 struct dwo_unit find_dwo_cu;
80626a55
DE
11280
11281 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11282 find_dwo_cu.signature = signature;
19ac8c2e
DE
11283 slot = htab_find_slot (is_debug_types
11284 ? dwp_file->loaded_tus
11285 : dwp_file->loaded_cus,
11286 &find_dwo_cu, INSERT);
80626a55
DE
11287
11288 if (*slot != NULL)
9a3c8263 11289 return (struct dwo_unit *) *slot;
80626a55
DE
11290
11291 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11292 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11293 {
11294 ULONGEST signature_in_table;
11295
11296 signature_in_table =
57d63ce2 11297 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11298 if (signature_in_table == signature)
11299 {
57d63ce2
DE
11300 uint32_t unit_index =
11301 read_4_bytes (dbfd,
11302 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11303
73869dc2
DE
11304 if (dwp_file->version == 1)
11305 {
11306 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11307 comp_dir, signature,
11308 is_debug_types);
11309 }
11310 else
11311 {
11312 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11313 comp_dir, signature,
11314 is_debug_types);
11315 }
9a3c8263 11316 return (struct dwo_unit *) *slot;
80626a55
DE
11317 }
11318 if (signature_in_table == 0)
11319 return NULL;
11320 hash = (hash + hash2) & mask;
11321 }
11322
11323 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11324 " [in module %s]"),
11325 dwp_file->name);
11326}
11327
ab5088bf 11328/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11329 Open the file specified by FILE_NAME and hand it off to BFD for
11330 preliminary analysis. Return a newly initialized bfd *, which
11331 includes a canonicalized copy of FILE_NAME.
80626a55 11332 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11333 SEARCH_CWD is true if the current directory is to be searched.
11334 It will be searched before debug-file-directory.
13aaf454
DE
11335 If successful, the file is added to the bfd include table of the
11336 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11337 If unable to find/open the file, return NULL.
3019eac3
DE
11338 NOTE: This function is derived from symfile_bfd_open. */
11339
192b62ce 11340static gdb_bfd_ref_ptr
6ac97d4c 11341try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 11342{
80626a55 11343 int desc, flags;
3019eac3 11344 char *absolute_name;
9c02c129
DE
11345 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11346 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11347 to debug_file_directory. */
11348 char *search_path;
11349 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11350
6ac97d4c
DE
11351 if (search_cwd)
11352 {
11353 if (*debug_file_directory != '\0')
11354 search_path = concat (".", dirname_separator_string,
b36cec19 11355 debug_file_directory, (char *) NULL);
6ac97d4c
DE
11356 else
11357 search_path = xstrdup (".");
11358 }
9c02c129 11359 else
6ac97d4c 11360 search_path = xstrdup (debug_file_directory);
3019eac3 11361
492c0ab7 11362 flags = OPF_RETURN_REALPATH;
80626a55
DE
11363 if (is_dwp)
11364 flags |= OPF_SEARCH_IN_PATH;
9c02c129 11365 desc = openp (search_path, flags, file_name,
3019eac3 11366 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 11367 xfree (search_path);
3019eac3
DE
11368 if (desc < 0)
11369 return NULL;
11370
192b62ce 11371 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 11372 xfree (absolute_name);
9c02c129
DE
11373 if (sym_bfd == NULL)
11374 return NULL;
192b62ce 11375 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11376
192b62ce
TT
11377 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11378 return NULL;
3019eac3 11379
13aaf454
DE
11380 /* Success. Record the bfd as having been included by the objfile's bfd.
11381 This is important because things like demangled_names_hash lives in the
11382 objfile's per_bfd space and may have references to things like symbol
11383 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11384 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11385
3019eac3
DE
11386 return sym_bfd;
11387}
11388
ab5088bf 11389/* Try to open DWO file FILE_NAME.
3019eac3
DE
11390 COMP_DIR is the DW_AT_comp_dir attribute.
11391 The result is the bfd handle of the file.
11392 If there is a problem finding or opening the file, return NULL.
11393 Upon success, the canonicalized path of the file is stored in the bfd,
11394 same as symfile_bfd_open. */
11395
192b62ce 11396static gdb_bfd_ref_ptr
ab5088bf 11397open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 11398{
80626a55 11399 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 11400 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11401
11402 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11403
11404 if (comp_dir != NULL)
11405 {
b36cec19
PA
11406 char *path_to_try = concat (comp_dir, SLASH_STRING,
11407 file_name, (char *) NULL);
3019eac3
DE
11408
11409 /* NOTE: If comp_dir is a relative path, this will also try the
11410 search path, which seems useful. */
192b62ce
TT
11411 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11412 1 /*search_cwd*/));
3019eac3
DE
11413 xfree (path_to_try);
11414 if (abfd != NULL)
11415 return abfd;
11416 }
11417
11418 /* That didn't work, try debug-file-directory, which, despite its name,
11419 is a list of paths. */
11420
11421 if (*debug_file_directory == '\0')
11422 return NULL;
11423
6ac97d4c 11424 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
11425}
11426
80626a55
DE
11427/* This function is mapped across the sections and remembers the offset and
11428 size of each of the DWO debugging sections we are interested in. */
11429
11430static void
11431dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11432{
9a3c8263 11433 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
11434 const struct dwop_section_names *names = &dwop_section_names;
11435
11436 if (section_is_p (sectp->name, &names->abbrev_dwo))
11437 {
049412e3 11438 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
11439 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11440 }
11441 else if (section_is_p (sectp->name, &names->info_dwo))
11442 {
049412e3 11443 dwo_sections->info.s.section = sectp;
80626a55
DE
11444 dwo_sections->info.size = bfd_get_section_size (sectp);
11445 }
11446 else if (section_is_p (sectp->name, &names->line_dwo))
11447 {
049412e3 11448 dwo_sections->line.s.section = sectp;
80626a55
DE
11449 dwo_sections->line.size = bfd_get_section_size (sectp);
11450 }
11451 else if (section_is_p (sectp->name, &names->loc_dwo))
11452 {
049412e3 11453 dwo_sections->loc.s.section = sectp;
80626a55
DE
11454 dwo_sections->loc.size = bfd_get_section_size (sectp);
11455 }
11456 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11457 {
049412e3 11458 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
11459 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11460 }
11461 else if (section_is_p (sectp->name, &names->macro_dwo))
11462 {
049412e3 11463 dwo_sections->macro.s.section = sectp;
80626a55
DE
11464 dwo_sections->macro.size = bfd_get_section_size (sectp);
11465 }
11466 else if (section_is_p (sectp->name, &names->str_dwo))
11467 {
049412e3 11468 dwo_sections->str.s.section = sectp;
80626a55
DE
11469 dwo_sections->str.size = bfd_get_section_size (sectp);
11470 }
11471 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11472 {
049412e3 11473 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
11474 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11475 }
11476 else if (section_is_p (sectp->name, &names->types_dwo))
11477 {
11478 struct dwarf2_section_info type_section;
11479
11480 memset (&type_section, 0, sizeof (type_section));
049412e3 11481 type_section.s.section = sectp;
80626a55
DE
11482 type_section.size = bfd_get_section_size (sectp);
11483 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11484 &type_section);
11485 }
11486}
11487
ab5088bf 11488/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 11489 by PER_CU. This is for the non-DWP case.
80626a55 11490 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
11491
11492static struct dwo_file *
0ac5b59e
DE
11493open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11494 const char *dwo_name, const char *comp_dir)
3019eac3
DE
11495{
11496 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 11497 struct dwo_file *dwo_file;
3019eac3
DE
11498 struct cleanup *cleanups;
11499
192b62ce 11500 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
11501 if (dbfd == NULL)
11502 {
b4f54984 11503 if (dwarf_read_debug)
80626a55
DE
11504 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11505 return NULL;
11506 }
11507 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11508 dwo_file->dwo_name = dwo_name;
11509 dwo_file->comp_dir = comp_dir;
192b62ce 11510 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11511
11512 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11513
192b62ce
TT
11514 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11515 &dwo_file->sections);
3019eac3 11516
33c5cd75 11517 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11518
78d4d2c5
JK
11519 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11520 dwo_file->tus);
3019eac3
DE
11521
11522 discard_cleanups (cleanups);
11523
b4f54984 11524 if (dwarf_read_debug)
80626a55
DE
11525 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11526
3019eac3
DE
11527 return dwo_file;
11528}
11529
80626a55 11530/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11531 size of each of the DWP debugging sections common to version 1 and 2 that
11532 we are interested in. */
3019eac3 11533
80626a55 11534static void
73869dc2
DE
11535dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11536 void *dwp_file_ptr)
3019eac3 11537{
9a3c8263 11538 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11539 const struct dwop_section_names *names = &dwop_section_names;
11540 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11541
80626a55 11542 /* Record the ELF section number for later lookup: this is what the
73869dc2 11543 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11544 gdb_assert (elf_section_nr < dwp_file->num_sections);
11545 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11546
80626a55
DE
11547 /* Look for specific sections that we need. */
11548 if (section_is_p (sectp->name, &names->str_dwo))
11549 {
049412e3 11550 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11551 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11552 }
11553 else if (section_is_p (sectp->name, &names->cu_index))
11554 {
049412e3 11555 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11556 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11557 }
11558 else if (section_is_p (sectp->name, &names->tu_index))
11559 {
049412e3 11560 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11561 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11562 }
11563}
3019eac3 11564
73869dc2
DE
11565/* This function is mapped across the sections and remembers the offset and
11566 size of each of the DWP version 2 debugging sections that we are interested
11567 in. This is split into a separate function because we don't know if we
11568 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11569
11570static void
11571dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11572{
9a3c8263 11573 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11574 const struct dwop_section_names *names = &dwop_section_names;
11575 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11576
11577 /* Record the ELF section number for later lookup: this is what the
11578 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11579 gdb_assert (elf_section_nr < dwp_file->num_sections);
11580 dwp_file->elf_sections[elf_section_nr] = sectp;
11581
11582 /* Look for specific sections that we need. */
11583 if (section_is_p (sectp->name, &names->abbrev_dwo))
11584 {
049412e3 11585 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11586 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11587 }
11588 else if (section_is_p (sectp->name, &names->info_dwo))
11589 {
049412e3 11590 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11591 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11592 }
11593 else if (section_is_p (sectp->name, &names->line_dwo))
11594 {
049412e3 11595 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11596 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11597 }
11598 else if (section_is_p (sectp->name, &names->loc_dwo))
11599 {
049412e3 11600 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11601 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11602 }
11603 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11604 {
049412e3 11605 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11606 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11607 }
11608 else if (section_is_p (sectp->name, &names->macro_dwo))
11609 {
049412e3 11610 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11611 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11612 }
11613 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11614 {
049412e3 11615 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11616 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11617 }
11618 else if (section_is_p (sectp->name, &names->types_dwo))
11619 {
049412e3 11620 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11621 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11622 }
11623}
11624
80626a55 11625/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11626
80626a55
DE
11627static hashval_t
11628hash_dwp_loaded_cutus (const void *item)
11629{
9a3c8263 11630 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11631
80626a55
DE
11632 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11633 return dwo_unit->signature;
3019eac3
DE
11634}
11635
80626a55 11636/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11637
80626a55
DE
11638static int
11639eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11640{
9a3c8263
SM
11641 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11642 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11643
80626a55
DE
11644 return dua->signature == dub->signature;
11645}
3019eac3 11646
80626a55 11647/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11648
80626a55
DE
11649static htab_t
11650allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11651{
11652 return htab_create_alloc_ex (3,
11653 hash_dwp_loaded_cutus,
11654 eq_dwp_loaded_cutus,
11655 NULL,
11656 &objfile->objfile_obstack,
11657 hashtab_obstack_allocate,
11658 dummy_obstack_deallocate);
11659}
3019eac3 11660
ab5088bf
DE
11661/* Try to open DWP file FILE_NAME.
11662 The result is the bfd handle of the file.
11663 If there is a problem finding or opening the file, return NULL.
11664 Upon success, the canonicalized path of the file is stored in the bfd,
11665 same as symfile_bfd_open. */
11666
192b62ce 11667static gdb_bfd_ref_ptr
ab5088bf
DE
11668open_dwp_file (const char *file_name)
11669{
192b62ce
TT
11670 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11671 1 /*search_cwd*/));
6ac97d4c
DE
11672 if (abfd != NULL)
11673 return abfd;
11674
11675 /* Work around upstream bug 15652.
11676 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11677 [Whether that's a "bug" is debatable, but it is getting in our way.]
11678 We have no real idea where the dwp file is, because gdb's realpath-ing
11679 of the executable's path may have discarded the needed info.
11680 [IWBN if the dwp file name was recorded in the executable, akin to
11681 .gnu_debuglink, but that doesn't exist yet.]
11682 Strip the directory from FILE_NAME and search again. */
11683 if (*debug_file_directory != '\0')
11684 {
11685 /* Don't implicitly search the current directory here.
11686 If the user wants to search "." to handle this case,
11687 it must be added to debug-file-directory. */
11688 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11689 0 /*search_cwd*/);
11690 }
11691
11692 return NULL;
ab5088bf
DE
11693}
11694
80626a55
DE
11695/* Initialize the use of the DWP file for the current objfile.
11696 By convention the name of the DWP file is ${objfile}.dwp.
11697 The result is NULL if it can't be found. */
a766d390 11698
80626a55 11699static struct dwp_file *
ab5088bf 11700open_and_init_dwp_file (void)
80626a55
DE
11701{
11702 struct objfile *objfile = dwarf2_per_objfile->objfile;
11703 struct dwp_file *dwp_file;
80626a55 11704
82bf32bc
JK
11705 /* Try to find first .dwp for the binary file before any symbolic links
11706 resolving. */
6c447423
DE
11707
11708 /* If the objfile is a debug file, find the name of the real binary
11709 file and get the name of dwp file from there. */
d721ba37 11710 std::string dwp_name;
6c447423
DE
11711 if (objfile->separate_debug_objfile_backlink != NULL)
11712 {
11713 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11714 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11715
d721ba37 11716 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11717 }
11718 else
d721ba37
PA
11719 dwp_name = objfile->original_name;
11720
11721 dwp_name += ".dwp";
80626a55 11722
d721ba37 11723 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11724 if (dbfd == NULL
11725 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11726 {
11727 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11728 dwp_name = objfile_name (objfile);
11729 dwp_name += ".dwp";
11730 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11731 }
11732
80626a55
DE
11733 if (dbfd == NULL)
11734 {
b4f54984 11735 if (dwarf_read_debug)
d721ba37 11736 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11737 return NULL;
3019eac3 11738 }
80626a55 11739 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11740 dwp_file->name = bfd_get_filename (dbfd.get ());
11741 dwp_file->dbfd = dbfd.release ();
c906108c 11742
80626a55 11743 /* +1: section 0 is unused */
192b62ce 11744 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11745 dwp_file->elf_sections =
11746 OBSTACK_CALLOC (&objfile->objfile_obstack,
11747 dwp_file->num_sections, asection *);
11748
192b62ce
TT
11749 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11750 dwp_file);
80626a55
DE
11751
11752 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11753
11754 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11755
73869dc2 11756 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11757 if (dwp_file->cus && dwp_file->tus
11758 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11759 {
11760 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11761 pretty bizarre. We use pulongest here because that's the established
4d65956b 11762 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11763 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11764 " TU version %s [in DWP file %s]"),
11765 pulongest (dwp_file->cus->version),
d721ba37 11766 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11767 }
08302ed2
DE
11768
11769 if (dwp_file->cus)
11770 dwp_file->version = dwp_file->cus->version;
11771 else if (dwp_file->tus)
11772 dwp_file->version = dwp_file->tus->version;
11773 else
11774 dwp_file->version = 2;
73869dc2
DE
11775
11776 if (dwp_file->version == 2)
192b62ce
TT
11777 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11778 dwp_file);
73869dc2 11779
19ac8c2e
DE
11780 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11781 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11782
b4f54984 11783 if (dwarf_read_debug)
80626a55
DE
11784 {
11785 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11786 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11787 " %s CUs, %s TUs\n",
11788 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11789 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11790 }
11791
11792 return dwp_file;
3019eac3 11793}
c906108c 11794
ab5088bf
DE
11795/* Wrapper around open_and_init_dwp_file, only open it once. */
11796
11797static struct dwp_file *
11798get_dwp_file (void)
11799{
11800 if (! dwarf2_per_objfile->dwp_checked)
11801 {
11802 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11803 dwarf2_per_objfile->dwp_checked = 1;
11804 }
11805 return dwarf2_per_objfile->dwp_file;
11806}
11807
80626a55
DE
11808/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11809 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11810 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11811 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11812 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11813
11814 This is called, for example, when wanting to read a variable with a
11815 complex location. Therefore we don't want to do file i/o for every call.
11816 Therefore we don't want to look for a DWO file on every call.
11817 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11818 then we check if we've already seen DWO_NAME, and only THEN do we check
11819 for a DWO file.
11820
1c658ad5 11821 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11822 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11823
3019eac3 11824static struct dwo_unit *
80626a55
DE
11825lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11826 const char *dwo_name, const char *comp_dir,
11827 ULONGEST signature, int is_debug_types)
3019eac3
DE
11828{
11829 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11830 const char *kind = is_debug_types ? "TU" : "CU";
11831 void **dwo_file_slot;
3019eac3 11832 struct dwo_file *dwo_file;
80626a55 11833 struct dwp_file *dwp_file;
cb1df416 11834
6a506a2d
DE
11835 /* First see if there's a DWP file.
11836 If we have a DWP file but didn't find the DWO inside it, don't
11837 look for the original DWO file. It makes gdb behave differently
11838 depending on whether one is debugging in the build tree. */
cf2c3c16 11839
ab5088bf 11840 dwp_file = get_dwp_file ();
80626a55 11841 if (dwp_file != NULL)
cf2c3c16 11842 {
80626a55
DE
11843 const struct dwp_hash_table *dwp_htab =
11844 is_debug_types ? dwp_file->tus : dwp_file->cus;
11845
11846 if (dwp_htab != NULL)
11847 {
11848 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11849 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11850 signature, is_debug_types);
80626a55
DE
11851
11852 if (dwo_cutu != NULL)
11853 {
b4f54984 11854 if (dwarf_read_debug)
80626a55
DE
11855 {
11856 fprintf_unfiltered (gdb_stdlog,
11857 "Virtual DWO %s %s found: @%s\n",
11858 kind, hex_string (signature),
11859 host_address_to_string (dwo_cutu));
11860 }
11861 return dwo_cutu;
11862 }
11863 }
11864 }
6a506a2d 11865 else
80626a55 11866 {
6a506a2d 11867 /* No DWP file, look for the DWO file. */
80626a55 11868
6a506a2d
DE
11869 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11870 if (*dwo_file_slot == NULL)
80626a55 11871 {
6a506a2d
DE
11872 /* Read in the file and build a table of the CUs/TUs it contains. */
11873 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11874 }
6a506a2d 11875 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11876 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11877
6a506a2d 11878 if (dwo_file != NULL)
19c3d4c9 11879 {
6a506a2d
DE
11880 struct dwo_unit *dwo_cutu = NULL;
11881
11882 if (is_debug_types && dwo_file->tus)
11883 {
11884 struct dwo_unit find_dwo_cutu;
11885
11886 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11887 find_dwo_cutu.signature = signature;
9a3c8263
SM
11888 dwo_cutu
11889 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11890 }
33c5cd75 11891 else if (!is_debug_types && dwo_file->cus)
80626a55 11892 {
33c5cd75
DB
11893 struct dwo_unit find_dwo_cutu;
11894
11895 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11896 find_dwo_cutu.signature = signature;
11897 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11898 &find_dwo_cutu);
6a506a2d
DE
11899 }
11900
11901 if (dwo_cutu != NULL)
11902 {
b4f54984 11903 if (dwarf_read_debug)
6a506a2d
DE
11904 {
11905 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11906 kind, dwo_name, hex_string (signature),
11907 host_address_to_string (dwo_cutu));
11908 }
11909 return dwo_cutu;
80626a55
DE
11910 }
11911 }
2e276125 11912 }
9cdd5dbd 11913
80626a55
DE
11914 /* We didn't find it. This could mean a dwo_id mismatch, or
11915 someone deleted the DWO/DWP file, or the search path isn't set up
11916 correctly to find the file. */
11917
b4f54984 11918 if (dwarf_read_debug)
80626a55
DE
11919 {
11920 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11921 kind, dwo_name, hex_string (signature));
11922 }
3019eac3 11923
6656a72d
DE
11924 /* This is a warning and not a complaint because it can be caused by
11925 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11926 {
11927 /* Print the name of the DWP file if we looked there, helps the user
11928 better diagnose the problem. */
791afaa2 11929 std::string dwp_text;
43942612
DE
11930
11931 if (dwp_file != NULL)
791afaa2
TT
11932 dwp_text = string_printf (" [in DWP file %s]",
11933 lbasename (dwp_file->name));
43942612
DE
11934
11935 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11936 " [in module %s]"),
11937 kind, dwo_name, hex_string (signature),
791afaa2 11938 dwp_text.c_str (),
43942612 11939 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11940 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 11941 }
3019eac3 11942 return NULL;
5fb290d7
DJ
11943}
11944
80626a55
DE
11945/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11946 See lookup_dwo_cutu_unit for details. */
11947
11948static struct dwo_unit *
11949lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11950 const char *dwo_name, const char *comp_dir,
11951 ULONGEST signature)
11952{
11953 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11954}
11955
11956/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11957 See lookup_dwo_cutu_unit for details. */
11958
11959static struct dwo_unit *
11960lookup_dwo_type_unit (struct signatured_type *this_tu,
11961 const char *dwo_name, const char *comp_dir)
11962{
11963 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11964}
11965
89e63ee4
DE
11966/* Traversal function for queue_and_load_all_dwo_tus. */
11967
11968static int
11969queue_and_load_dwo_tu (void **slot, void *info)
11970{
11971 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11972 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11973 ULONGEST signature = dwo_unit->signature;
11974 struct signatured_type *sig_type =
11975 lookup_dwo_signatured_type (per_cu->cu, signature);
11976
11977 if (sig_type != NULL)
11978 {
11979 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11980
11981 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11982 a real dependency of PER_CU on SIG_TYPE. That is detected later
11983 while processing PER_CU. */
11984 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11985 load_full_type_unit (sig_cu);
11986 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11987 }
11988
11989 return 1;
11990}
11991
11992/* Queue all TUs contained in the DWO of PER_CU to be read in.
11993 The DWO may have the only definition of the type, though it may not be
11994 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11996
11997static void
11998queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11999{
12000 struct dwo_unit *dwo_unit;
12001 struct dwo_file *dwo_file;
12002
12003 gdb_assert (!per_cu->is_debug_types);
12004 gdb_assert (get_dwp_file () == NULL);
12005 gdb_assert (per_cu->cu != NULL);
12006
12007 dwo_unit = per_cu->cu->dwo_unit;
12008 gdb_assert (dwo_unit != NULL);
12009
12010 dwo_file = dwo_unit->dwo_file;
12011 if (dwo_file->tus != NULL)
12012 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12013}
12014
3019eac3
DE
12015/* Free all resources associated with DWO_FILE.
12016 Close the DWO file and munmap the sections.
12017 All memory should be on the objfile obstack. */
348e048f
DE
12018
12019static void
3019eac3 12020free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 12021{
348e048f 12022
5c6fa7ab 12023 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 12024 gdb_bfd_unref (dwo_file->dbfd);
348e048f 12025
3019eac3
DE
12026 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12027}
348e048f 12028
3019eac3 12029/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 12030
3019eac3
DE
12031static void
12032free_dwo_file_cleanup (void *arg)
12033{
12034 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12035 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 12036
3019eac3
DE
12037 free_dwo_file (dwo_file, objfile);
12038}
348e048f 12039
3019eac3 12040/* Traversal function for free_dwo_files. */
2ab95328 12041
3019eac3
DE
12042static int
12043free_dwo_file_from_slot (void **slot, void *info)
12044{
12045 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12046 struct objfile *objfile = (struct objfile *) info;
348e048f 12047
3019eac3 12048 free_dwo_file (dwo_file, objfile);
348e048f 12049
3019eac3
DE
12050 return 1;
12051}
348e048f 12052
3019eac3 12053/* Free all resources associated with DWO_FILES. */
348e048f 12054
3019eac3
DE
12055static void
12056free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12057{
12058 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 12059}
3019eac3
DE
12060\f
12061/* Read in various DIEs. */
348e048f 12062
d389af10 12063/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12064 Inherit only the children of the DW_AT_abstract_origin DIE not being
12065 already referenced by DW_AT_abstract_origin from the children of the
12066 current DIE. */
d389af10
JK
12067
12068static void
12069inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12070{
12071 struct die_info *child_die;
791afaa2 12072 sect_offset *offsetp;
d389af10
JK
12073 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12074 struct die_info *origin_die;
12075 /* Iterator of the ORIGIN_DIE children. */
12076 struct die_info *origin_child_die;
d389af10 12077 struct attribute *attr;
cd02d79d
PA
12078 struct dwarf2_cu *origin_cu;
12079 struct pending **origin_previous_list_in_scope;
d389af10
JK
12080
12081 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12082 if (!attr)
12083 return;
12084
cd02d79d
PA
12085 /* Note that following die references may follow to a die in a
12086 different cu. */
12087
12088 origin_cu = cu;
12089 origin_die = follow_die_ref (die, attr, &origin_cu);
12090
12091 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12092 symbols in. */
12093 origin_previous_list_in_scope = origin_cu->list_in_scope;
12094 origin_cu->list_in_scope = cu->list_in_scope;
12095
edb3359d
DJ
12096 if (die->tag != origin_die->tag
12097 && !(die->tag == DW_TAG_inlined_subroutine
12098 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12099 complaint (&symfile_complaints,
12100 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
12101 to_underlying (die->sect_off),
12102 to_underlying (origin_die->sect_off));
d389af10 12103
791afaa2 12104 std::vector<sect_offset> offsets;
d389af10 12105
3ea89b92
PMR
12106 for (child_die = die->child;
12107 child_die && child_die->tag;
12108 child_die = sibling_die (child_die))
12109 {
12110 struct die_info *child_origin_die;
12111 struct dwarf2_cu *child_origin_cu;
12112
12113 /* We are trying to process concrete instance entries:
216f72a1 12114 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12115 it's not relevant to our analysis here. i.e. detecting DIEs that are
12116 present in the abstract instance but not referenced in the concrete
12117 one. */
216f72a1
JK
12118 if (child_die->tag == DW_TAG_call_site
12119 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12120 continue;
12121
c38f313d
DJ
12122 /* For each CHILD_DIE, find the corresponding child of
12123 ORIGIN_DIE. If there is more than one layer of
12124 DW_AT_abstract_origin, follow them all; there shouldn't be,
12125 but GCC versions at least through 4.4 generate this (GCC PR
12126 40573). */
3ea89b92
PMR
12127 child_origin_die = child_die;
12128 child_origin_cu = cu;
c38f313d
DJ
12129 while (1)
12130 {
cd02d79d
PA
12131 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12132 child_origin_cu);
c38f313d
DJ
12133 if (attr == NULL)
12134 break;
cd02d79d
PA
12135 child_origin_die = follow_die_ref (child_origin_die, attr,
12136 &child_origin_cu);
c38f313d
DJ
12137 }
12138
d389af10
JK
12139 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12140 counterpart may exist. */
c38f313d 12141 if (child_origin_die != child_die)
d389af10 12142 {
edb3359d
DJ
12143 if (child_die->tag != child_origin_die->tag
12144 && !(child_die->tag == DW_TAG_inlined_subroutine
12145 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12146 complaint (&symfile_complaints,
12147 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12148 "different tags"),
12149 to_underlying (child_die->sect_off),
12150 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
12151 if (child_origin_die->parent != origin_die)
12152 complaint (&symfile_complaints,
12153 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12154 "different parents"),
12155 to_underlying (child_die->sect_off),
12156 to_underlying (child_origin_die->sect_off));
c38f313d 12157 else
791afaa2 12158 offsets.push_back (child_origin_die->sect_off);
d389af10 12159 }
d389af10 12160 }
791afaa2
TT
12161 std::sort (offsets.begin (), offsets.end ());
12162 sect_offset *offsets_end = offsets.data () + offsets.size ();
12163 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12164 if (offsetp[-1] == *offsetp)
3e43a32a
MS
12165 complaint (&symfile_complaints,
12166 _("Multiple children of DIE 0x%x refer "
12167 "to DIE 0x%x as their abstract origin"),
9c541725 12168 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 12169
791afaa2 12170 offsetp = offsets.data ();
d389af10
JK
12171 origin_child_die = origin_die->child;
12172 while (origin_child_die && origin_child_die->tag)
12173 {
12174 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12175 while (offsetp < offsets_end
9c541725 12176 && *offsetp < origin_child_die->sect_off)
d389af10 12177 offsetp++;
b64f50a1 12178 if (offsetp >= offsets_end
9c541725 12179 || *offsetp > origin_child_die->sect_off)
d389af10 12180 {
adde2bff
DE
12181 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12182 Check whether we're already processing ORIGIN_CHILD_DIE.
12183 This can happen with mutually referenced abstract_origins.
12184 PR 16581. */
12185 if (!origin_child_die->in_process)
12186 process_die (origin_child_die, origin_cu);
d389af10
JK
12187 }
12188 origin_child_die = sibling_die (origin_child_die);
12189 }
cd02d79d 12190 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
12191}
12192
c906108c 12193static void
e7c27a73 12194read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12195{
e7c27a73 12196 struct objfile *objfile = cu->objfile;
3e29f34a 12197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12198 struct context_stack *newobj;
c906108c
SS
12199 CORE_ADDR lowpc;
12200 CORE_ADDR highpc;
12201 struct die_info *child_die;
edb3359d 12202 struct attribute *attr, *call_line, *call_file;
15d034d0 12203 const char *name;
e142c38c 12204 CORE_ADDR baseaddr;
801e3a5b 12205 struct block *block;
edb3359d 12206 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
12207 VEC (symbolp) *template_args = NULL;
12208 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12209
12210 if (inlined_func)
12211 {
12212 /* If we do not have call site information, we can't show the
12213 caller of this inlined function. That's too confusing, so
12214 only use the scope for local variables. */
12215 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12216 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12217 if (call_line == NULL || call_file == NULL)
12218 {
12219 read_lexical_block_scope (die, cu);
12220 return;
12221 }
12222 }
c906108c 12223
e142c38c
DJ
12224 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12225
94af9270 12226 name = dwarf2_name (die, cu);
c906108c 12227
e8d05480
JB
12228 /* Ignore functions with missing or empty names. These are actually
12229 illegal according to the DWARF standard. */
12230 if (name == NULL)
12231 {
12232 complaint (&symfile_complaints,
b64f50a1 12233 _("missing name for subprogram DIE at %d"),
9c541725 12234 to_underlying (die->sect_off));
e8d05480
JB
12235 return;
12236 }
12237
12238 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12239 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12240 <= PC_BOUNDS_INVALID)
e8d05480 12241 {
ae4d0c03
PM
12242 attr = dwarf2_attr (die, DW_AT_external, cu);
12243 if (!attr || !DW_UNSND (attr))
12244 complaint (&symfile_complaints,
3e43a32a
MS
12245 _("cannot get low and high bounds "
12246 "for subprogram DIE at %d"),
9c541725 12247 to_underlying (die->sect_off));
e8d05480
JB
12248 return;
12249 }
c906108c 12250
3e29f34a
MR
12251 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12252 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12253
34eaf542
TT
12254 /* If we have any template arguments, then we must allocate a
12255 different sort of symbol. */
12256 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12257 {
12258 if (child_die->tag == DW_TAG_template_type_param
12259 || child_die->tag == DW_TAG_template_value_param)
12260 {
e623cf5d 12261 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
12262 templ_func->base.is_cplus_template_function = 1;
12263 break;
12264 }
12265 }
12266
fe978cb0
PA
12267 newobj = push_context (0, lowpc);
12268 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 12269 (struct symbol *) templ_func);
4c2df51b 12270
4cecd739
DJ
12271 /* If there is a location expression for DW_AT_frame_base, record
12272 it. */
e142c38c 12273 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 12274 if (attr)
fe978cb0 12275 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12276
63e43d3a
PMR
12277 /* If there is a location for the static link, record it. */
12278 newobj->static_link = NULL;
12279 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12280 if (attr)
12281 {
224c3ddb
SM
12282 newobj->static_link
12283 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
12284 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12285 }
12286
e142c38c 12287 cu->list_in_scope = &local_symbols;
c906108c 12288
639d11d3 12289 if (die->child != NULL)
c906108c 12290 {
639d11d3 12291 child_die = die->child;
c906108c
SS
12292 while (child_die && child_die->tag)
12293 {
34eaf542
TT
12294 if (child_die->tag == DW_TAG_template_type_param
12295 || child_die->tag == DW_TAG_template_value_param)
12296 {
12297 struct symbol *arg = new_symbol (child_die, NULL, cu);
12298
f1078f66
DJ
12299 if (arg != NULL)
12300 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12301 }
12302 else
12303 process_die (child_die, cu);
c906108c
SS
12304 child_die = sibling_die (child_die);
12305 }
12306 }
12307
d389af10
JK
12308 inherit_abstract_dies (die, cu);
12309
4a811a97
UW
12310 /* If we have a DW_AT_specification, we might need to import using
12311 directives from the context of the specification DIE. See the
12312 comment in determine_prefix. */
12313 if (cu->language == language_cplus
12314 && dwarf2_attr (die, DW_AT_specification, cu))
12315 {
12316 struct dwarf2_cu *spec_cu = cu;
12317 struct die_info *spec_die = die_specification (die, &spec_cu);
12318
12319 while (spec_die)
12320 {
12321 child_die = spec_die->child;
12322 while (child_die && child_die->tag)
12323 {
12324 if (child_die->tag == DW_TAG_imported_module)
12325 process_die (child_die, spec_cu);
12326 child_die = sibling_die (child_die);
12327 }
12328
12329 /* In some cases, GCC generates specification DIEs that
12330 themselves contain DW_AT_specification attributes. */
12331 spec_die = die_specification (spec_die, &spec_cu);
12332 }
12333 }
12334
fe978cb0 12335 newobj = pop_context ();
c906108c 12336 /* Make a block for the local symbols within. */
fe978cb0 12337 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 12338 newobj->static_link, lowpc, highpc);
801e3a5b 12339
df8a16a1 12340 /* For C++, set the block's scope. */
45280282
IB
12341 if ((cu->language == language_cplus
12342 || cu->language == language_fortran
c44af4eb
TT
12343 || cu->language == language_d
12344 || cu->language == language_rust)
4d4ec4e5 12345 && cu->processing_has_namespace_info)
195a3f6c
TT
12346 block_set_scope (block, determine_prefix (die, cu),
12347 &objfile->objfile_obstack);
df8a16a1 12348
801e3a5b
JB
12349 /* If we have address ranges, record them. */
12350 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12351
fe978cb0 12352 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 12353
34eaf542
TT
12354 /* Attach template arguments to function. */
12355 if (! VEC_empty (symbolp, template_args))
12356 {
12357 gdb_assert (templ_func != NULL);
12358
12359 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
12360 templ_func->template_arguments
8d749320
SM
12361 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12362 templ_func->n_template_arguments);
34eaf542
TT
12363 memcpy (templ_func->template_arguments,
12364 VEC_address (symbolp, template_args),
12365 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12366 VEC_free (symbolp, template_args);
12367 }
12368
208d8187
JB
12369 /* In C++, we can have functions nested inside functions (e.g., when
12370 a function declares a class that has methods). This means that
12371 when we finish processing a function scope, we may need to go
12372 back to building a containing block's symbol lists. */
fe978cb0 12373 local_symbols = newobj->locals;
22cee43f 12374 local_using_directives = newobj->local_using_directives;
208d8187 12375
921e78cf
JB
12376 /* If we've finished processing a top-level function, subsequent
12377 symbols go in the file symbol list. */
12378 if (outermost_context_p ())
e142c38c 12379 cu->list_in_scope = &file_symbols;
c906108c
SS
12380}
12381
12382/* Process all the DIES contained within a lexical block scope. Start
12383 a new scope, process the dies, and then close the scope. */
12384
12385static void
e7c27a73 12386read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12387{
e7c27a73 12388 struct objfile *objfile = cu->objfile;
3e29f34a 12389 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12390 struct context_stack *newobj;
c906108c
SS
12391 CORE_ADDR lowpc, highpc;
12392 struct die_info *child_die;
e142c38c
DJ
12393 CORE_ADDR baseaddr;
12394
12395 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
12396
12397 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12398 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12399 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12400 be nasty. Might be easier to properly extend generic blocks to
af34e669 12401 describe ranges. */
e385593e
JK
12402 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12403 {
12404 case PC_BOUNDS_NOT_PRESENT:
12405 /* DW_TAG_lexical_block has no attributes, process its children as if
12406 there was no wrapping by that DW_TAG_lexical_block.
12407 GCC does no longer produces such DWARF since GCC r224161. */
12408 for (child_die = die->child;
12409 child_die != NULL && child_die->tag;
12410 child_die = sibling_die (child_die))
12411 process_die (child_die, cu);
12412 return;
12413 case PC_BOUNDS_INVALID:
12414 return;
12415 }
3e29f34a
MR
12416 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12417 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
12418
12419 push_context (0, lowpc);
639d11d3 12420 if (die->child != NULL)
c906108c 12421 {
639d11d3 12422 child_die = die->child;
c906108c
SS
12423 while (child_die && child_die->tag)
12424 {
e7c27a73 12425 process_die (child_die, cu);
c906108c
SS
12426 child_die = sibling_die (child_die);
12427 }
12428 }
3ea89b92 12429 inherit_abstract_dies (die, cu);
fe978cb0 12430 newobj = pop_context ();
c906108c 12431
22cee43f 12432 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 12433 {
801e3a5b 12434 struct block *block
63e43d3a 12435 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 12436 newobj->start_addr, highpc);
801e3a5b
JB
12437
12438 /* Note that recording ranges after traversing children, as we
12439 do here, means that recording a parent's ranges entails
12440 walking across all its children's ranges as they appear in
12441 the address map, which is quadratic behavior.
12442
12443 It would be nicer to record the parent's ranges before
12444 traversing its children, simply overriding whatever you find
12445 there. But since we don't even decide whether to create a
12446 block until after we've traversed its children, that's hard
12447 to do. */
12448 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12449 }
fe978cb0 12450 local_symbols = newobj->locals;
22cee43f 12451 local_using_directives = newobj->local_using_directives;
c906108c
SS
12452}
12453
216f72a1 12454/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12455
12456static void
12457read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12458{
12459 struct objfile *objfile = cu->objfile;
12460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12461 CORE_ADDR pc, baseaddr;
12462 struct attribute *attr;
12463 struct call_site *call_site, call_site_local;
12464 void **slot;
12465 int nparams;
12466 struct die_info *child_die;
12467
12468 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12469
216f72a1
JK
12470 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12471 if (attr == NULL)
12472 {
12473 /* This was a pre-DWARF-5 GNU extension alias
12474 for DW_AT_call_return_pc. */
12475 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12476 }
96408a79
SA
12477 if (!attr)
12478 {
12479 complaint (&symfile_complaints,
216f72a1 12480 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 12481 "DIE 0x%x [in module %s]"),
9c541725 12482 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12483 return;
12484 }
31aa7e4e 12485 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 12486 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
12487
12488 if (cu->call_site_htab == NULL)
12489 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12490 NULL, &objfile->objfile_obstack,
12491 hashtab_obstack_allocate, NULL);
12492 call_site_local.pc = pc;
12493 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12494 if (*slot != NULL)
12495 {
12496 complaint (&symfile_complaints,
216f72a1 12497 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 12498 "DIE 0x%x [in module %s]"),
9c541725 12499 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 12500 objfile_name (objfile));
96408a79
SA
12501 return;
12502 }
12503
12504 /* Count parameters at the caller. */
12505
12506 nparams = 0;
12507 for (child_die = die->child; child_die && child_die->tag;
12508 child_die = sibling_die (child_die))
12509 {
216f72a1
JK
12510 if (child_die->tag != DW_TAG_call_site_parameter
12511 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12512 {
12513 complaint (&symfile_complaints,
216f72a1
JK
12514 _("Tag %d is not DW_TAG_call_site_parameter in "
12515 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12516 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12517 objfile_name (objfile));
96408a79
SA
12518 continue;
12519 }
12520
12521 nparams++;
12522 }
12523
224c3ddb
SM
12524 call_site
12525 = ((struct call_site *)
12526 obstack_alloc (&objfile->objfile_obstack,
12527 sizeof (*call_site)
12528 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12529 *slot = call_site;
12530 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12531 call_site->pc = pc;
12532
216f72a1
JK
12533 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12534 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12535 {
12536 struct die_info *func_die;
12537
12538 /* Skip also over DW_TAG_inlined_subroutine. */
12539 for (func_die = die->parent;
12540 func_die && func_die->tag != DW_TAG_subprogram
12541 && func_die->tag != DW_TAG_subroutine_type;
12542 func_die = func_die->parent);
12543
216f72a1
JK
12544 /* DW_AT_call_all_calls is a superset
12545 of DW_AT_call_all_tail_calls. */
96408a79 12546 if (func_die
216f72a1 12547 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12548 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12549 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12550 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12551 {
12552 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12553 not complete. But keep CALL_SITE for look ups via call_site_htab,
12554 both the initial caller containing the real return address PC and
12555 the final callee containing the current PC of a chain of tail
12556 calls do not need to have the tail call list complete. But any
12557 function candidate for a virtual tail call frame searched via
12558 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12559 determined unambiguously. */
12560 }
12561 else
12562 {
12563 struct type *func_type = NULL;
12564
12565 if (func_die)
12566 func_type = get_die_type (func_die, cu);
12567 if (func_type != NULL)
12568 {
12569 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12570
12571 /* Enlist this call site to the function. */
12572 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12573 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12574 }
12575 else
12576 complaint (&symfile_complaints,
216f72a1 12577 _("Cannot find function owning DW_TAG_call_site "
96408a79 12578 "DIE 0x%x [in module %s]"),
9c541725 12579 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12580 }
12581 }
12582
216f72a1
JK
12583 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12584 if (attr == NULL)
12585 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12586 if (attr == NULL)
12587 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12588 if (attr == NULL)
216f72a1
JK
12589 {
12590 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12591 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12592 }
96408a79
SA
12593 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12594 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12595 /* Keep NULL DWARF_BLOCK. */;
12596 else if (attr_form_is_block (attr))
12597 {
12598 struct dwarf2_locexpr_baton *dlbaton;
12599
8d749320 12600 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12601 dlbaton->data = DW_BLOCK (attr)->data;
12602 dlbaton->size = DW_BLOCK (attr)->size;
12603 dlbaton->per_cu = cu->per_cu;
12604
12605 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12606 }
7771576e 12607 else if (attr_form_is_ref (attr))
96408a79 12608 {
96408a79
SA
12609 struct dwarf2_cu *target_cu = cu;
12610 struct die_info *target_die;
12611
ac9ec31b 12612 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12613 gdb_assert (target_cu->objfile == objfile);
12614 if (die_is_declaration (target_die, target_cu))
12615 {
7d45c7c3 12616 const char *target_physname;
9112db09
JK
12617
12618 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12619 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12620 if (target_physname == NULL)
9112db09 12621 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12622 if (target_physname == NULL)
12623 complaint (&symfile_complaints,
216f72a1 12624 _("DW_AT_call_target target DIE has invalid "
96408a79 12625 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12626 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12627 else
7d455152 12628 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12629 }
12630 else
12631 {
12632 CORE_ADDR lowpc;
12633
12634 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12635 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12636 <= PC_BOUNDS_INVALID)
96408a79 12637 complaint (&symfile_complaints,
216f72a1 12638 _("DW_AT_call_target target DIE has invalid "
96408a79 12639 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12640 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12641 else
3e29f34a
MR
12642 {
12643 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12644 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12645 }
96408a79
SA
12646 }
12647 }
12648 else
12649 complaint (&symfile_complaints,
216f72a1 12650 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12651 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12652 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12653
12654 call_site->per_cu = cu->per_cu;
12655
12656 for (child_die = die->child;
12657 child_die && child_die->tag;
12658 child_die = sibling_die (child_die))
12659 {
96408a79 12660 struct call_site_parameter *parameter;
1788b2d3 12661 struct attribute *loc, *origin;
96408a79 12662
216f72a1
JK
12663 if (child_die->tag != DW_TAG_call_site_parameter
12664 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12665 {
12666 /* Already printed the complaint above. */
12667 continue;
12668 }
12669
12670 gdb_assert (call_site->parameter_count < nparams);
12671 parameter = &call_site->parameter[call_site->parameter_count];
12672
1788b2d3
JK
12673 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12674 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12675 register is contained in DW_AT_call_value. */
96408a79 12676
24c5c679 12677 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12678 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12679 if (origin == NULL)
12680 {
12681 /* This was a pre-DWARF-5 GNU extension alias
12682 for DW_AT_call_parameter. */
12683 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12684 }
7771576e 12685 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12686 {
1788b2d3 12687 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12688
12689 sect_offset sect_off
12690 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12691 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12692 {
12693 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12694 binding can be done only inside one CU. Such referenced DIE
12695 therefore cannot be even moved to DW_TAG_partial_unit. */
12696 complaint (&symfile_complaints,
216f72a1
JK
12697 _("DW_AT_call_parameter offset is not in CU for "
12698 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12699 to_underlying (child_die->sect_off),
12700 objfile_name (objfile));
d76b7dbc
JK
12701 continue;
12702 }
9c541725
PA
12703 parameter->u.param_cu_off
12704 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12705 }
12706 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12707 {
12708 complaint (&symfile_complaints,
12709 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12710 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12711 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12712 continue;
12713 }
24c5c679 12714 else
96408a79 12715 {
24c5c679
JK
12716 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12717 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12718 if (parameter->u.dwarf_reg != -1)
12719 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12720 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12721 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12722 &parameter->u.fb_offset))
12723 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12724 else
12725 {
12726 complaint (&symfile_complaints,
12727 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12728 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12729 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12730 "[in module %s]"),
9c541725
PA
12731 to_underlying (child_die->sect_off),
12732 objfile_name (objfile));
24c5c679
JK
12733 continue;
12734 }
96408a79
SA
12735 }
12736
216f72a1
JK
12737 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12738 if (attr == NULL)
12739 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12740 if (!attr_form_is_block (attr))
12741 {
12742 complaint (&symfile_complaints,
216f72a1
JK
12743 _("No DW_FORM_block* DW_AT_call_value for "
12744 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12745 to_underlying (child_die->sect_off),
12746 objfile_name (objfile));
96408a79
SA
12747 continue;
12748 }
12749 parameter->value = DW_BLOCK (attr)->data;
12750 parameter->value_size = DW_BLOCK (attr)->size;
12751
12752 /* Parameters are not pre-cleared by memset above. */
12753 parameter->data_value = NULL;
12754 parameter->data_value_size = 0;
12755 call_site->parameter_count++;
12756
216f72a1
JK
12757 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12758 if (attr == NULL)
12759 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12760 if (attr)
12761 {
12762 if (!attr_form_is_block (attr))
12763 complaint (&symfile_complaints,
216f72a1
JK
12764 _("No DW_FORM_block* DW_AT_call_data_value for "
12765 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12766 to_underlying (child_die->sect_off),
12767 objfile_name (objfile));
96408a79
SA
12768 else
12769 {
12770 parameter->data_value = DW_BLOCK (attr)->data;
12771 parameter->data_value_size = DW_BLOCK (attr)->size;
12772 }
12773 }
12774 }
12775}
12776
43988095
JK
12777/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12778 reading .debug_rnglists.
12779 Callback's type should be:
12780 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12781 Return true if the attributes are present and valid, otherwise,
12782 return false. */
12783
12784template <typename Callback>
12785static bool
12786dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12787 Callback &&callback)
12788{
12789 struct objfile *objfile = cu->objfile;
12790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12791 struct comp_unit_head *cu_header = &cu->header;
12792 bfd *obfd = objfile->obfd;
12793 unsigned int addr_size = cu_header->addr_size;
12794 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12795 /* Base address selection entry. */
12796 CORE_ADDR base;
12797 int found_base;
12798 unsigned int dummy;
12799 const gdb_byte *buffer;
12800 CORE_ADDR low = 0;
12801 CORE_ADDR high = 0;
12802 CORE_ADDR baseaddr;
12803 bool overflow = false;
12804
12805 found_base = cu->base_known;
12806 base = cu->base_address;
12807
12808 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12809 if (offset >= dwarf2_per_objfile->rnglists.size)
12810 {
12811 complaint (&symfile_complaints,
12812 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12813 offset);
12814 return false;
12815 }
12816 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12817
12818 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12819
12820 while (1)
12821 {
7814882a
JK
12822 /* Initialize it due to a false compiler warning. */
12823 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12824 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12825 + dwarf2_per_objfile->rnglists.size);
12826 unsigned int bytes_read;
12827
12828 if (buffer == buf_end)
12829 {
12830 overflow = true;
12831 break;
12832 }
12833 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12834 switch (rlet)
12835 {
12836 case DW_RLE_end_of_list:
12837 break;
12838 case DW_RLE_base_address:
12839 if (buffer + cu->header.addr_size > buf_end)
12840 {
12841 overflow = true;
12842 break;
12843 }
12844 base = read_address (obfd, buffer, cu, &bytes_read);
12845 found_base = 1;
12846 buffer += bytes_read;
12847 break;
12848 case DW_RLE_start_length:
12849 if (buffer + cu->header.addr_size > buf_end)
12850 {
12851 overflow = true;
12852 break;
12853 }
12854 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12855 buffer += bytes_read;
12856 range_end = (range_beginning
12857 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12858 buffer += bytes_read;
12859 if (buffer > buf_end)
12860 {
12861 overflow = true;
12862 break;
12863 }
12864 break;
12865 case DW_RLE_offset_pair:
12866 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12867 buffer += bytes_read;
12868 if (buffer > buf_end)
12869 {
12870 overflow = true;
12871 break;
12872 }
12873 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12874 buffer += bytes_read;
12875 if (buffer > buf_end)
12876 {
12877 overflow = true;
12878 break;
12879 }
12880 break;
12881 case DW_RLE_start_end:
12882 if (buffer + 2 * cu->header.addr_size > buf_end)
12883 {
12884 overflow = true;
12885 break;
12886 }
12887 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12888 buffer += bytes_read;
12889 range_end = read_address (obfd, buffer, cu, &bytes_read);
12890 buffer += bytes_read;
12891 break;
12892 default:
12893 complaint (&symfile_complaints,
12894 _("Invalid .debug_rnglists data (no base address)"));
12895 return false;
12896 }
12897 if (rlet == DW_RLE_end_of_list || overflow)
12898 break;
12899 if (rlet == DW_RLE_base_address)
12900 continue;
12901
12902 if (!found_base)
12903 {
12904 /* We have no valid base address for the ranges
12905 data. */
12906 complaint (&symfile_complaints,
12907 _("Invalid .debug_rnglists data (no base address)"));
12908 return false;
12909 }
12910
12911 if (range_beginning > range_end)
12912 {
12913 /* Inverted range entries are invalid. */
12914 complaint (&symfile_complaints,
12915 _("Invalid .debug_rnglists data (inverted range)"));
12916 return false;
12917 }
12918
12919 /* Empty range entries have no effect. */
12920 if (range_beginning == range_end)
12921 continue;
12922
12923 range_beginning += base;
12924 range_end += base;
12925
12926 /* A not-uncommon case of bad debug info.
12927 Don't pollute the addrmap with bad data. */
12928 if (range_beginning + baseaddr == 0
12929 && !dwarf2_per_objfile->has_section_at_zero)
12930 {
12931 complaint (&symfile_complaints,
12932 _(".debug_rnglists entry has start address of zero"
12933 " [in module %s]"), objfile_name (objfile));
12934 continue;
12935 }
12936
12937 callback (range_beginning, range_end);
12938 }
12939
12940 if (overflow)
12941 {
12942 complaint (&symfile_complaints,
12943 _("Offset %d is not terminated "
12944 "for DW_AT_ranges attribute"),
12945 offset);
12946 return false;
12947 }
12948
12949 return true;
12950}
12951
12952/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12953 Callback's type should be:
12954 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12955 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12956
43988095 12957template <typename Callback>
43039443 12958static int
5f46c5a5 12959dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12960 Callback &&callback)
43039443
JK
12961{
12962 struct objfile *objfile = cu->objfile;
3e29f34a 12963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12964 struct comp_unit_head *cu_header = &cu->header;
12965 bfd *obfd = objfile->obfd;
12966 unsigned int addr_size = cu_header->addr_size;
12967 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12968 /* Base address selection entry. */
12969 CORE_ADDR base;
12970 int found_base;
12971 unsigned int dummy;
d521ce57 12972 const gdb_byte *buffer;
ff013f42 12973 CORE_ADDR baseaddr;
43039443 12974
43988095
JK
12975 if (cu_header->version >= 5)
12976 return dwarf2_rnglists_process (offset, cu, callback);
12977
d00adf39
DE
12978 found_base = cu->base_known;
12979 base = cu->base_address;
43039443 12980
be391dca 12981 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12982 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12983 {
12984 complaint (&symfile_complaints,
12985 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12986 offset);
12987 return 0;
12988 }
dce234bc 12989 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12990
e7030f15 12991 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12992
43039443
JK
12993 while (1)
12994 {
12995 CORE_ADDR range_beginning, range_end;
12996
12997 range_beginning = read_address (obfd, buffer, cu, &dummy);
12998 buffer += addr_size;
12999 range_end = read_address (obfd, buffer, cu, &dummy);
13000 buffer += addr_size;
13001 offset += 2 * addr_size;
13002
13003 /* An end of list marker is a pair of zero addresses. */
13004 if (range_beginning == 0 && range_end == 0)
13005 /* Found the end of list entry. */
13006 break;
13007
13008 /* Each base address selection entry is a pair of 2 values.
13009 The first is the largest possible address, the second is
13010 the base address. Check for a base address here. */
13011 if ((range_beginning & mask) == mask)
13012 {
28d2bfb9
AB
13013 /* If we found the largest possible address, then we already
13014 have the base address in range_end. */
13015 base = range_end;
43039443
JK
13016 found_base = 1;
13017 continue;
13018 }
13019
13020 if (!found_base)
13021 {
13022 /* We have no valid base address for the ranges
13023 data. */
13024 complaint (&symfile_complaints,
13025 _("Invalid .debug_ranges data (no base address)"));
13026 return 0;
13027 }
13028
9277c30c
UW
13029 if (range_beginning > range_end)
13030 {
13031 /* Inverted range entries are invalid. */
13032 complaint (&symfile_complaints,
13033 _("Invalid .debug_ranges data (inverted range)"));
13034 return 0;
13035 }
13036
13037 /* Empty range entries have no effect. */
13038 if (range_beginning == range_end)
13039 continue;
13040
43039443
JK
13041 range_beginning += base;
13042 range_end += base;
13043
01093045
DE
13044 /* A not-uncommon case of bad debug info.
13045 Don't pollute the addrmap with bad data. */
13046 if (range_beginning + baseaddr == 0
13047 && !dwarf2_per_objfile->has_section_at_zero)
13048 {
13049 complaint (&symfile_complaints,
13050 _(".debug_ranges entry has start address of zero"
4262abfb 13051 " [in module %s]"), objfile_name (objfile));
01093045
DE
13052 continue;
13053 }
13054
5f46c5a5
JK
13055 callback (range_beginning, range_end);
13056 }
13057
13058 return 1;
13059}
13060
13061/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13062 Return 1 if the attributes are present and valid, otherwise, return 0.
13063 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13064
13065static int
13066dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13067 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13068 struct partial_symtab *ranges_pst)
13069{
13070 struct objfile *objfile = cu->objfile;
13071 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13072 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13073 SECT_OFF_TEXT (objfile));
13074 int low_set = 0;
13075 CORE_ADDR low = 0;
13076 CORE_ADDR high = 0;
13077 int retval;
13078
13079 retval = dwarf2_ranges_process (offset, cu,
13080 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13081 {
9277c30c 13082 if (ranges_pst != NULL)
3e29f34a
MR
13083 {
13084 CORE_ADDR lowpc;
13085 CORE_ADDR highpc;
13086
13087 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13088 range_beginning + baseaddr);
13089 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13090 range_end + baseaddr);
13091 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13092 ranges_pst);
13093 }
ff013f42 13094
43039443
JK
13095 /* FIXME: This is recording everything as a low-high
13096 segment of consecutive addresses. We should have a
13097 data structure for discontiguous block ranges
13098 instead. */
13099 if (! low_set)
13100 {
13101 low = range_beginning;
13102 high = range_end;
13103 low_set = 1;
13104 }
13105 else
13106 {
13107 if (range_beginning < low)
13108 low = range_beginning;
13109 if (range_end > high)
13110 high = range_end;
13111 }
5f46c5a5
JK
13112 });
13113 if (!retval)
13114 return 0;
43039443
JK
13115
13116 if (! low_set)
13117 /* If the first entry is an end-of-list marker, the range
13118 describes an empty scope, i.e. no instructions. */
13119 return 0;
13120
13121 if (low_return)
13122 *low_return = low;
13123 if (high_return)
13124 *high_return = high;
13125 return 1;
13126}
13127
3a2b436a
JK
13128/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13129 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13130 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13131
3a2b436a 13132static enum pc_bounds_kind
af34e669 13133dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
13134 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13135 struct partial_symtab *pst)
c906108c
SS
13136{
13137 struct attribute *attr;
91da1414 13138 struct attribute *attr_high;
af34e669
DJ
13139 CORE_ADDR low = 0;
13140 CORE_ADDR high = 0;
e385593e 13141 enum pc_bounds_kind ret;
c906108c 13142
91da1414
MW
13143 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13144 if (attr_high)
af34e669 13145 {
e142c38c 13146 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 13147 if (attr)
91da1414 13148 {
31aa7e4e
JB
13149 low = attr_value_as_address (attr);
13150 high = attr_value_as_address (attr_high);
13151 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13152 high += low;
91da1414 13153 }
af34e669
DJ
13154 else
13155 /* Found high w/o low attribute. */
e385593e 13156 return PC_BOUNDS_INVALID;
af34e669
DJ
13157
13158 /* Found consecutive range of addresses. */
3a2b436a 13159 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13160 }
c906108c 13161 else
af34e669 13162 {
e142c38c 13163 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13164 if (attr != NULL)
13165 {
ab435259
DE
13166 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13167 We take advantage of the fact that DW_AT_ranges does not appear
13168 in DW_TAG_compile_unit of DWO files. */
13169 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13170 unsigned int ranges_offset = (DW_UNSND (attr)
13171 + (need_ranges_base
13172 ? cu->ranges_base
13173 : 0));
2e3cf129 13174
af34e669 13175 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13176 .debug_ranges section. */
2e3cf129 13177 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13178 return PC_BOUNDS_INVALID;
43039443 13179 /* Found discontinuous range of addresses. */
3a2b436a 13180 ret = PC_BOUNDS_RANGES;
af34e669 13181 }
e385593e
JK
13182 else
13183 return PC_BOUNDS_NOT_PRESENT;
af34e669 13184 }
c906108c 13185
9373cf26
JK
13186 /* read_partial_die has also the strict LOW < HIGH requirement. */
13187 if (high <= low)
e385593e 13188 return PC_BOUNDS_INVALID;
c906108c
SS
13189
13190 /* When using the GNU linker, .gnu.linkonce. sections are used to
13191 eliminate duplicate copies of functions and vtables and such.
13192 The linker will arbitrarily choose one and discard the others.
13193 The AT_*_pc values for such functions refer to local labels in
13194 these sections. If the section from that file was discarded, the
13195 labels are not in the output, so the relocs get a value of 0.
13196 If this is a discarded function, mark the pc bounds as invalid,
13197 so that GDB will ignore it. */
72dca2f5 13198 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13199 return PC_BOUNDS_INVALID;
c906108c
SS
13200
13201 *lowpc = low;
96408a79
SA
13202 if (highpc)
13203 *highpc = high;
af34e669 13204 return ret;
c906108c
SS
13205}
13206
b084d499
JB
13207/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13208 its low and high PC addresses. Do nothing if these addresses could not
13209 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13210 and HIGHPC to the high address if greater than HIGHPC. */
13211
13212static void
13213dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13214 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13215 struct dwarf2_cu *cu)
13216{
13217 CORE_ADDR low, high;
13218 struct die_info *child = die->child;
13219
e385593e 13220 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13221 {
325fac50
PA
13222 *lowpc = std::min (*lowpc, low);
13223 *highpc = std::max (*highpc, high);
b084d499
JB
13224 }
13225
13226 /* If the language does not allow nested subprograms (either inside
13227 subprograms or lexical blocks), we're done. */
13228 if (cu->language != language_ada)
13229 return;
6e70227d 13230
b084d499
JB
13231 /* Check all the children of the given DIE. If it contains nested
13232 subprograms, then check their pc bounds. Likewise, we need to
13233 check lexical blocks as well, as they may also contain subprogram
13234 definitions. */
13235 while (child && child->tag)
13236 {
13237 if (child->tag == DW_TAG_subprogram
13238 || child->tag == DW_TAG_lexical_block)
13239 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13240 child = sibling_die (child);
13241 }
13242}
13243
fae299cd
DC
13244/* Get the low and high pc's represented by the scope DIE, and store
13245 them in *LOWPC and *HIGHPC. If the correct values can't be
13246 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13247
13248static void
13249get_scope_pc_bounds (struct die_info *die,
13250 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13251 struct dwarf2_cu *cu)
13252{
13253 CORE_ADDR best_low = (CORE_ADDR) -1;
13254 CORE_ADDR best_high = (CORE_ADDR) 0;
13255 CORE_ADDR current_low, current_high;
13256
3a2b436a 13257 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13258 >= PC_BOUNDS_RANGES)
fae299cd
DC
13259 {
13260 best_low = current_low;
13261 best_high = current_high;
13262 }
13263 else
13264 {
13265 struct die_info *child = die->child;
13266
13267 while (child && child->tag)
13268 {
13269 switch (child->tag) {
13270 case DW_TAG_subprogram:
b084d499 13271 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13272 break;
13273 case DW_TAG_namespace:
f55ee35c 13274 case DW_TAG_module:
fae299cd
DC
13275 /* FIXME: carlton/2004-01-16: Should we do this for
13276 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13277 that current GCC's always emit the DIEs corresponding
13278 to definitions of methods of classes as children of a
13279 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13280 the DIEs giving the declarations, which could be
13281 anywhere). But I don't see any reason why the
13282 standards says that they have to be there. */
13283 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13284
13285 if (current_low != ((CORE_ADDR) -1))
13286 {
325fac50
PA
13287 best_low = std::min (best_low, current_low);
13288 best_high = std::max (best_high, current_high);
fae299cd
DC
13289 }
13290 break;
13291 default:
0963b4bd 13292 /* Ignore. */
fae299cd
DC
13293 break;
13294 }
13295
13296 child = sibling_die (child);
13297 }
13298 }
13299
13300 *lowpc = best_low;
13301 *highpc = best_high;
13302}
13303
801e3a5b
JB
13304/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13305 in DIE. */
380bca97 13306
801e3a5b
JB
13307static void
13308dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13309 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13310{
bb5ed363 13311 struct objfile *objfile = cu->objfile;
3e29f34a 13312 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13313 struct attribute *attr;
91da1414 13314 struct attribute *attr_high;
801e3a5b 13315
91da1414
MW
13316 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13317 if (attr_high)
801e3a5b 13318 {
801e3a5b
JB
13319 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13320 if (attr)
13321 {
31aa7e4e
JB
13322 CORE_ADDR low = attr_value_as_address (attr);
13323 CORE_ADDR high = attr_value_as_address (attr_high);
13324
13325 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13326 high += low;
9a619af0 13327
3e29f34a
MR
13328 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13329 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13330 record_block_range (block, low, high - 1);
801e3a5b
JB
13331 }
13332 }
13333
13334 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13335 if (attr)
13336 {
bb5ed363 13337 bfd *obfd = objfile->obfd;
ab435259
DE
13338 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13339 We take advantage of the fact that DW_AT_ranges does not appear
13340 in DW_TAG_compile_unit of DWO files. */
13341 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13342
13343 /* The value of the DW_AT_ranges attribute is the offset of the
13344 address range list in the .debug_ranges section. */
ab435259
DE
13345 unsigned long offset = (DW_UNSND (attr)
13346 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 13347 const gdb_byte *buffer;
801e3a5b
JB
13348
13349 /* For some target architectures, but not others, the
13350 read_address function sign-extends the addresses it returns.
13351 To recognize base address selection entries, we need a
13352 mask. */
13353 unsigned int addr_size = cu->header.addr_size;
13354 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13355
13356 /* The base address, to which the next pair is relative. Note
13357 that this 'base' is a DWARF concept: most entries in a range
13358 list are relative, to reduce the number of relocs against the
13359 debugging information. This is separate from this function's
13360 'baseaddr' argument, which GDB uses to relocate debugging
13361 information from a shared library based on the address at
13362 which the library was loaded. */
d00adf39
DE
13363 CORE_ADDR base = cu->base_address;
13364 int base_known = cu->base_known;
801e3a5b 13365
5f46c5a5
JK
13366 dwarf2_ranges_process (offset, cu,
13367 [&] (CORE_ADDR start, CORE_ADDR end)
13368 {
58fdfd2c
JK
13369 start += baseaddr;
13370 end += baseaddr;
5f46c5a5
JK
13371 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13372 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13373 record_block_range (block, start, end - 1);
13374 });
801e3a5b
JB
13375 }
13376}
13377
685b1105
JK
13378/* Check whether the producer field indicates either of GCC < 4.6, or the
13379 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13380
685b1105
JK
13381static void
13382check_producer (struct dwarf2_cu *cu)
60d5a603 13383{
38360086 13384 int major, minor;
60d5a603
JK
13385
13386 if (cu->producer == NULL)
13387 {
13388 /* For unknown compilers expect their behavior is DWARF version
13389 compliant.
13390
13391 GCC started to support .debug_types sections by -gdwarf-4 since
13392 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13393 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13394 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13395 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13396 }
b1ffba5a 13397 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13398 {
38360086
MW
13399 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13400 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13401 }
5230b05a
WT
13402 else if (producer_is_icc (cu->producer, &major, &minor))
13403 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
13404 else
13405 {
13406 /* For other non-GCC compilers, expect their behavior is DWARF version
13407 compliant. */
60d5a603
JK
13408 }
13409
ba919b58 13410 cu->checked_producer = 1;
685b1105 13411}
ba919b58 13412
685b1105
JK
13413/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13414 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13415 during 4.6.0 experimental. */
13416
13417static int
13418producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13419{
13420 if (!cu->checked_producer)
13421 check_producer (cu);
13422
13423 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13424}
13425
13426/* Return the default accessibility type if it is not overriden by
13427 DW_AT_accessibility. */
13428
13429static enum dwarf_access_attribute
13430dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13431{
13432 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13433 {
13434 /* The default DWARF 2 accessibility for members is public, the default
13435 accessibility for inheritance is private. */
13436
13437 if (die->tag != DW_TAG_inheritance)
13438 return DW_ACCESS_public;
13439 else
13440 return DW_ACCESS_private;
13441 }
13442 else
13443 {
13444 /* DWARF 3+ defines the default accessibility a different way. The same
13445 rules apply now for DW_TAG_inheritance as for the members and it only
13446 depends on the container kind. */
13447
13448 if (die->parent->tag == DW_TAG_class_type)
13449 return DW_ACCESS_private;
13450 else
13451 return DW_ACCESS_public;
13452 }
13453}
13454
74ac6d43
TT
13455/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13456 offset. If the attribute was not found return 0, otherwise return
13457 1. If it was found but could not properly be handled, set *OFFSET
13458 to 0. */
13459
13460static int
13461handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13462 LONGEST *offset)
13463{
13464 struct attribute *attr;
13465
13466 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13467 if (attr != NULL)
13468 {
13469 *offset = 0;
13470
13471 /* Note that we do not check for a section offset first here.
13472 This is because DW_AT_data_member_location is new in DWARF 4,
13473 so if we see it, we can assume that a constant form is really
13474 a constant and not a section offset. */
13475 if (attr_form_is_constant (attr))
13476 *offset = dwarf2_get_attr_constant_value (attr, 0);
13477 else if (attr_form_is_section_offset (attr))
13478 dwarf2_complex_location_expr_complaint ();
13479 else if (attr_form_is_block (attr))
13480 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13481 else
13482 dwarf2_complex_location_expr_complaint ();
13483
13484 return 1;
13485 }
13486
13487 return 0;
13488}
13489
c906108c
SS
13490/* Add an aggregate field to the field list. */
13491
13492static void
107d2387 13493dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 13494 struct dwarf2_cu *cu)
6e70227d 13495{
e7c27a73 13496 struct objfile *objfile = cu->objfile;
5e2b427d 13497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13498 struct nextfield *new_field;
13499 struct attribute *attr;
13500 struct field *fp;
15d034d0 13501 const char *fieldname = "";
c906108c
SS
13502
13503 /* Allocate a new field list entry and link it in. */
8d749320 13504 new_field = XNEW (struct nextfield);
b8c9b27d 13505 make_cleanup (xfree, new_field);
c906108c 13506 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13507
13508 if (die->tag == DW_TAG_inheritance)
13509 {
13510 new_field->next = fip->baseclasses;
13511 fip->baseclasses = new_field;
13512 }
13513 else
13514 {
13515 new_field->next = fip->fields;
13516 fip->fields = new_field;
13517 }
c906108c
SS
13518 fip->nfields++;
13519
e142c38c 13520 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13521 if (attr)
13522 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13523 else
13524 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13525 if (new_field->accessibility != DW_ACCESS_public)
13526 fip->non_public_fields = 1;
60d5a603 13527
e142c38c 13528 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13529 if (attr)
13530 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13531 else
13532 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13533
13534 fp = &new_field->field;
a9a9bd0f 13535
e142c38c 13536 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13537 {
74ac6d43
TT
13538 LONGEST offset;
13539
a9a9bd0f 13540 /* Data member other than a C++ static data member. */
6e70227d 13541
c906108c 13542 /* Get type of field. */
e7c27a73 13543 fp->type = die_type (die, cu);
c906108c 13544
d6a843b5 13545 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13546
c906108c 13547 /* Get bit size of field (zero if none). */
e142c38c 13548 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13549 if (attr)
13550 {
13551 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13552 }
13553 else
13554 {
13555 FIELD_BITSIZE (*fp) = 0;
13556 }
13557
13558 /* Get bit offset of field. */
74ac6d43
TT
13559 if (handle_data_member_location (die, cu, &offset))
13560 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13561 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13562 if (attr)
13563 {
5e2b427d 13564 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13565 {
13566 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13567 additional bit offset from the MSB of the containing
13568 anonymous object to the MSB of the field. We don't
13569 have to do anything special since we don't need to
13570 know the size of the anonymous object. */
f41f5e61 13571 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13572 }
13573 else
13574 {
13575 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13576 MSB of the anonymous object, subtract off the number of
13577 bits from the MSB of the field to the MSB of the
13578 object, and then subtract off the number of bits of
13579 the field itself. The result is the bit offset of
13580 the LSB of the field. */
c906108c
SS
13581 int anonymous_size;
13582 int bit_offset = DW_UNSND (attr);
13583
e142c38c 13584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13585 if (attr)
13586 {
13587 /* The size of the anonymous object containing
13588 the bit field is explicit, so use the
13589 indicated size (in bytes). */
13590 anonymous_size = DW_UNSND (attr);
13591 }
13592 else
13593 {
13594 /* The size of the anonymous object containing
13595 the bit field must be inferred from the type
13596 attribute of the data member containing the
13597 bit field. */
13598 anonymous_size = TYPE_LENGTH (fp->type);
13599 }
f41f5e61
PA
13600 SET_FIELD_BITPOS (*fp,
13601 (FIELD_BITPOS (*fp)
13602 + anonymous_size * bits_per_byte
13603 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13604 }
13605 }
da5b30da
AA
13606 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13607 if (attr != NULL)
13608 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13609 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13610
13611 /* Get name of field. */
39cbfefa
DJ
13612 fieldname = dwarf2_name (die, cu);
13613 if (fieldname == NULL)
13614 fieldname = "";
d8151005
DJ
13615
13616 /* The name is already allocated along with this objfile, so we don't
13617 need to duplicate it for the type. */
13618 fp->name = fieldname;
c906108c
SS
13619
13620 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13621 pointer or virtual base class pointer) to private. */
e142c38c 13622 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13623 {
d48cc9dd 13624 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13625 new_field->accessibility = DW_ACCESS_private;
13626 fip->non_public_fields = 1;
13627 }
13628 }
a9a9bd0f 13629 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13630 {
a9a9bd0f
DC
13631 /* C++ static member. */
13632
13633 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13634 is a declaration, but all versions of G++ as of this writing
13635 (so through at least 3.2.1) incorrectly generate
13636 DW_TAG_variable tags. */
6e70227d 13637
ff355380 13638 const char *physname;
c906108c 13639
a9a9bd0f 13640 /* Get name of field. */
39cbfefa
DJ
13641 fieldname = dwarf2_name (die, cu);
13642 if (fieldname == NULL)
c906108c
SS
13643 return;
13644
254e6b9e 13645 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13646 if (attr
13647 /* Only create a symbol if this is an external value.
13648 new_symbol checks this and puts the value in the global symbol
13649 table, which we want. If it is not external, new_symbol
13650 will try to put the value in cu->list_in_scope which is wrong. */
13651 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13652 {
13653 /* A static const member, not much different than an enum as far as
13654 we're concerned, except that we can support more types. */
13655 new_symbol (die, NULL, cu);
13656 }
13657
2df3850c 13658 /* Get physical name. */
ff355380 13659 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13660
d8151005
DJ
13661 /* The name is already allocated along with this objfile, so we don't
13662 need to duplicate it for the type. */
13663 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13664 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13665 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13666 }
13667 else if (die->tag == DW_TAG_inheritance)
13668 {
74ac6d43 13669 LONGEST offset;
d4b96c9a 13670
74ac6d43
TT
13671 /* C++ base class field. */
13672 if (handle_data_member_location (die, cu, &offset))
13673 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13674 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13675 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13676 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13677 fip->nbaseclasses++;
13678 }
13679}
13680
98751a41
JK
13681/* Add a typedef defined in the scope of the FIP's class. */
13682
13683static void
13684dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13685 struct dwarf2_cu *cu)
6e70227d 13686{
98751a41 13687 struct typedef_field_list *new_field;
98751a41 13688 struct typedef_field *fp;
98751a41
JK
13689
13690 /* Allocate a new field list entry and link it in. */
8d749320 13691 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13692 make_cleanup (xfree, new_field);
13693
13694 gdb_assert (die->tag == DW_TAG_typedef);
13695
13696 fp = &new_field->field;
13697
13698 /* Get name of field. */
13699 fp->name = dwarf2_name (die, cu);
13700 if (fp->name == NULL)
13701 return;
13702
13703 fp->type = read_type_die (die, cu);
13704
c191a687
KS
13705 /* Save accessibility. */
13706 enum dwarf_access_attribute accessibility;
13707 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13708 if (attr != NULL)
13709 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13710 else
13711 accessibility = dwarf2_default_access_attribute (die, cu);
13712 switch (accessibility)
13713 {
13714 case DW_ACCESS_public:
13715 /* The assumed value if neither private nor protected. */
13716 break;
13717 case DW_ACCESS_private:
13718 fp->is_private = 1;
13719 break;
13720 case DW_ACCESS_protected:
13721 fp->is_protected = 1;
13722 break;
13723 default:
37534686
KS
13724 complaint (&symfile_complaints,
13725 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
13726 }
13727
98751a41
JK
13728 new_field->next = fip->typedef_field_list;
13729 fip->typedef_field_list = new_field;
13730 fip->typedef_field_list_count++;
13731}
13732
c906108c
SS
13733/* Create the vector of fields, and attach it to the type. */
13734
13735static void
fba45db2 13736dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13737 struct dwarf2_cu *cu)
c906108c
SS
13738{
13739 int nfields = fip->nfields;
13740
13741 /* Record the field count, allocate space for the array of fields,
13742 and create blank accessibility bitfields if necessary. */
13743 TYPE_NFIELDS (type) = nfields;
13744 TYPE_FIELDS (type) = (struct field *)
13745 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13746 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13747
b4ba55a1 13748 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13749 {
13750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13751
13752 TYPE_FIELD_PRIVATE_BITS (type) =
13753 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13754 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13755
13756 TYPE_FIELD_PROTECTED_BITS (type) =
13757 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13758 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13759
774b6a14
TT
13760 TYPE_FIELD_IGNORE_BITS (type) =
13761 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13762 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13763 }
13764
13765 /* If the type has baseclasses, allocate and clear a bit vector for
13766 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13767 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13768 {
13769 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13770 unsigned char *pointer;
c906108c
SS
13771
13772 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13773 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13774 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13775 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13776 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13777 }
13778
3e43a32a
MS
13779 /* Copy the saved-up fields into the field vector. Start from the head of
13780 the list, adding to the tail of the field array, so that they end up in
13781 the same order in the array in which they were added to the list. */
c906108c
SS
13782 while (nfields-- > 0)
13783 {
7d0ccb61
DJ
13784 struct nextfield *fieldp;
13785
13786 if (fip->fields)
13787 {
13788 fieldp = fip->fields;
13789 fip->fields = fieldp->next;
13790 }
13791 else
13792 {
13793 fieldp = fip->baseclasses;
13794 fip->baseclasses = fieldp->next;
13795 }
13796
13797 TYPE_FIELD (type, nfields) = fieldp->field;
13798 switch (fieldp->accessibility)
c906108c 13799 {
c5aa993b 13800 case DW_ACCESS_private:
b4ba55a1
JB
13801 if (cu->language != language_ada)
13802 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13803 break;
c906108c 13804
c5aa993b 13805 case DW_ACCESS_protected:
b4ba55a1
JB
13806 if (cu->language != language_ada)
13807 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13808 break;
c906108c 13809
c5aa993b
JM
13810 case DW_ACCESS_public:
13811 break;
c906108c 13812
c5aa993b
JM
13813 default:
13814 /* Unknown accessibility. Complain and treat it as public. */
13815 {
e2e0b3e5 13816 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13817 fieldp->accessibility);
c5aa993b
JM
13818 }
13819 break;
c906108c
SS
13820 }
13821 if (nfields < fip->nbaseclasses)
13822 {
7d0ccb61 13823 switch (fieldp->virtuality)
c906108c 13824 {
c5aa993b
JM
13825 case DW_VIRTUALITY_virtual:
13826 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13827 if (cu->language == language_ada)
a73c6dcd 13828 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13829 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13830 break;
c906108c
SS
13831 }
13832 }
c906108c
SS
13833 }
13834}
13835
7d27a96d
TT
13836/* Return true if this member function is a constructor, false
13837 otherwise. */
13838
13839static int
13840dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13841{
13842 const char *fieldname;
fe978cb0 13843 const char *type_name;
7d27a96d
TT
13844 int len;
13845
13846 if (die->parent == NULL)
13847 return 0;
13848
13849 if (die->parent->tag != DW_TAG_structure_type
13850 && die->parent->tag != DW_TAG_union_type
13851 && die->parent->tag != DW_TAG_class_type)
13852 return 0;
13853
13854 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13855 type_name = dwarf2_name (die->parent, cu);
13856 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13857 return 0;
13858
13859 len = strlen (fieldname);
fe978cb0
PA
13860 return (strncmp (fieldname, type_name, len) == 0
13861 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13862}
13863
c906108c
SS
13864/* Add a member function to the proper fieldlist. */
13865
13866static void
107d2387 13867dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13868 struct type *type, struct dwarf2_cu *cu)
c906108c 13869{
e7c27a73 13870 struct objfile *objfile = cu->objfile;
c906108c
SS
13871 struct attribute *attr;
13872 struct fnfieldlist *flp;
13873 int i;
13874 struct fn_field *fnp;
15d034d0 13875 const char *fieldname;
c906108c 13876 struct nextfnfield *new_fnfield;
f792889a 13877 struct type *this_type;
60d5a603 13878 enum dwarf_access_attribute accessibility;
c906108c 13879
b4ba55a1 13880 if (cu->language == language_ada)
a73c6dcd 13881 error (_("unexpected member function in Ada type"));
b4ba55a1 13882
2df3850c 13883 /* Get name of member function. */
39cbfefa
DJ
13884 fieldname = dwarf2_name (die, cu);
13885 if (fieldname == NULL)
2df3850c 13886 return;
c906108c 13887
c906108c
SS
13888 /* Look up member function name in fieldlist. */
13889 for (i = 0; i < fip->nfnfields; i++)
13890 {
27bfe10e 13891 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13892 break;
13893 }
13894
13895 /* Create new list element if necessary. */
13896 if (i < fip->nfnfields)
13897 flp = &fip->fnfieldlists[i];
13898 else
13899 {
13900 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13901 {
13902 fip->fnfieldlists = (struct fnfieldlist *)
13903 xrealloc (fip->fnfieldlists,
13904 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13905 * sizeof (struct fnfieldlist));
c906108c 13906 if (fip->nfnfields == 0)
c13c43fd 13907 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13908 }
13909 flp = &fip->fnfieldlists[fip->nfnfields];
13910 flp->name = fieldname;
13911 flp->length = 0;
13912 flp->head = NULL;
3da10d80 13913 i = fip->nfnfields++;
c906108c
SS
13914 }
13915
13916 /* Create a new member function field and chain it to the field list
0963b4bd 13917 entry. */
8d749320 13918 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13919 make_cleanup (xfree, new_fnfield);
c906108c
SS
13920 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13921 new_fnfield->next = flp->head;
13922 flp->head = new_fnfield;
13923 flp->length++;
13924
13925 /* Fill in the member function field info. */
13926 fnp = &new_fnfield->fnfield;
3da10d80
KS
13927
13928 /* Delay processing of the physname until later. */
9c37b5ae 13929 if (cu->language == language_cplus)
3da10d80
KS
13930 {
13931 add_to_method_list (type, i, flp->length - 1, fieldname,
13932 die, cu);
13933 }
13934 else
13935 {
1d06ead6 13936 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13937 fnp->physname = physname ? physname : "";
13938 }
13939
c906108c 13940 fnp->type = alloc_type (objfile);
f792889a
DJ
13941 this_type = read_type_die (die, cu);
13942 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13943 {
f792889a 13944 int nparams = TYPE_NFIELDS (this_type);
c906108c 13945
f792889a 13946 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13947 of the method itself (TYPE_CODE_METHOD). */
13948 smash_to_method_type (fnp->type, type,
f792889a
DJ
13949 TYPE_TARGET_TYPE (this_type),
13950 TYPE_FIELDS (this_type),
13951 TYPE_NFIELDS (this_type),
13952 TYPE_VARARGS (this_type));
c906108c
SS
13953
13954 /* Handle static member functions.
c5aa993b 13955 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13956 member functions. G++ helps GDB by marking the first
13957 parameter for non-static member functions (which is the this
13958 pointer) as artificial. We obtain this information from
13959 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13960 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13961 fnp->voffset = VOFFSET_STATIC;
13962 }
13963 else
e2e0b3e5 13964 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13965 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13966
13967 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13968 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13969 fnp->fcontext = die_containing_type (die, cu);
c906108c 13970
3e43a32a
MS
13971 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13972 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13973
13974 /* Get accessibility. */
e142c38c 13975 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13976 if (attr)
aead7601 13977 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13978 else
13979 accessibility = dwarf2_default_access_attribute (die, cu);
13980 switch (accessibility)
c906108c 13981 {
60d5a603
JK
13982 case DW_ACCESS_private:
13983 fnp->is_private = 1;
13984 break;
13985 case DW_ACCESS_protected:
13986 fnp->is_protected = 1;
13987 break;
c906108c
SS
13988 }
13989
b02dede2 13990 /* Check for artificial methods. */
e142c38c 13991 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13992 if (attr && DW_UNSND (attr) != 0)
13993 fnp->is_artificial = 1;
13994
7d27a96d
TT
13995 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13996
0d564a31 13997 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13998 function. For older versions of GCC, this is an offset in the
13999 appropriate virtual table, as specified by DW_AT_containing_type.
14000 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14001 to the object address. */
14002
e142c38c 14003 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 14004 if (attr)
8e19ed76 14005 {
aec5aa8b 14006 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 14007 {
aec5aa8b
TT
14008 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14009 {
14010 /* Old-style GCC. */
14011 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14012 }
14013 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14014 || (DW_BLOCK (attr)->size > 1
14015 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14016 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14017 {
aec5aa8b
TT
14018 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14019 if ((fnp->voffset % cu->header.addr_size) != 0)
14020 dwarf2_complex_location_expr_complaint ();
14021 else
14022 fnp->voffset /= cu->header.addr_size;
14023 fnp->voffset += 2;
14024 }
14025 else
14026 dwarf2_complex_location_expr_complaint ();
14027
14028 if (!fnp->fcontext)
7e993ebf
KS
14029 {
14030 /* If there is no `this' field and no DW_AT_containing_type,
14031 we cannot actually find a base class context for the
14032 vtable! */
14033 if (TYPE_NFIELDS (this_type) == 0
14034 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14035 {
14036 complaint (&symfile_complaints,
14037 _("cannot determine context for virtual member "
14038 "function \"%s\" (offset %d)"),
9c541725 14039 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
14040 }
14041 else
14042 {
14043 fnp->fcontext
14044 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14045 }
14046 }
aec5aa8b 14047 }
3690dd37 14048 else if (attr_form_is_section_offset (attr))
8e19ed76 14049 {
4d3c2250 14050 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14051 }
14052 else
14053 {
4d3c2250
KB
14054 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14055 fieldname);
8e19ed76 14056 }
0d564a31 14057 }
d48cc9dd
DJ
14058 else
14059 {
14060 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14061 if (attr && DW_UNSND (attr))
14062 {
14063 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14064 complaint (&symfile_complaints,
3e43a32a
MS
14065 _("Member function \"%s\" (offset %d) is virtual "
14066 "but the vtable offset is not specified"),
9c541725 14067 fieldname, to_underlying (die->sect_off));
9655fd1a 14068 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14069 TYPE_CPLUS_DYNAMIC (type) = 1;
14070 }
14071 }
c906108c
SS
14072}
14073
14074/* Create the vector of member function fields, and attach it to the type. */
14075
14076static void
fba45db2 14077dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14078 struct dwarf2_cu *cu)
c906108c
SS
14079{
14080 struct fnfieldlist *flp;
c906108c
SS
14081 int i;
14082
b4ba55a1 14083 if (cu->language == language_ada)
a73c6dcd 14084 error (_("unexpected member functions in Ada type"));
b4ba55a1 14085
c906108c
SS
14086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14087 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14088 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14089
14090 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14091 {
14092 struct nextfnfield *nfp = flp->head;
14093 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14094 int k;
14095
14096 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14097 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14098 fn_flp->fn_fields = (struct fn_field *)
14099 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14100 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 14101 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
14102 }
14103
14104 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
14105}
14106
1168df01
JB
14107/* Returns non-zero if NAME is the name of a vtable member in CU's
14108 language, zero otherwise. */
14109static int
14110is_vtable_name (const char *name, struct dwarf2_cu *cu)
14111{
14112 static const char vptr[] = "_vptr";
987504bb 14113 static const char vtable[] = "vtable";
1168df01 14114
9c37b5ae
TT
14115 /* Look for the C++ form of the vtable. */
14116 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14117 return 1;
14118
14119 return 0;
14120}
14121
c0dd20ea 14122/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14123 functions, with the ABI-specified layout. If TYPE describes
14124 such a structure, smash it into a member function type.
61049d3b
DJ
14125
14126 GCC shouldn't do this; it should just output pointer to member DIEs.
14127 This is GCC PR debug/28767. */
c0dd20ea 14128
0b92b5bb
TT
14129static void
14130quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14131{
09e2d7c7 14132 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14133
14134 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14135 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14136 return;
c0dd20ea
DJ
14137
14138 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14139 if (TYPE_FIELD_NAME (type, 0) == NULL
14140 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14141 || TYPE_FIELD_NAME (type, 1) == NULL
14142 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14143 return;
c0dd20ea
DJ
14144
14145 /* Find the type of the method. */
0b92b5bb 14146 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14147 if (pfn_type == NULL
14148 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14149 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14150 return;
c0dd20ea
DJ
14151
14152 /* Look for the "this" argument. */
14153 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14154 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14155 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14156 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14157 return;
c0dd20ea 14158
09e2d7c7 14159 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14160 new_type = alloc_type (objfile);
09e2d7c7 14161 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14162 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14163 TYPE_VARARGS (pfn_type));
0b92b5bb 14164 smash_to_methodptr_type (type, new_type);
c0dd20ea 14165}
1168df01 14166
685b1105 14167
c906108c 14168/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14169 (definition) to create a type for the structure or union. Fill in
14170 the type's name and general properties; the members will not be
83655187
DE
14171 processed until process_structure_scope. A symbol table entry for
14172 the type will also not be done until process_structure_scope (assuming
14173 the type has a name).
c906108c 14174
c767944b
DJ
14175 NOTE: we need to call these functions regardless of whether or not the
14176 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14177 structure or union. This gets the type entered into our set of
83655187 14178 user defined types. */
c906108c 14179
f792889a 14180static struct type *
134d01f1 14181read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14182{
e7c27a73 14183 struct objfile *objfile = cu->objfile;
c906108c
SS
14184 struct type *type;
14185 struct attribute *attr;
15d034d0 14186 const char *name;
c906108c 14187
348e048f
DE
14188 /* If the definition of this type lives in .debug_types, read that type.
14189 Don't follow DW_AT_specification though, that will take us back up
14190 the chain and we want to go down. */
45e58e77 14191 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14192 if (attr)
14193 {
ac9ec31b 14194 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14195
ac9ec31b 14196 /* The type's CU may not be the same as CU.
02142a6c 14197 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14198 return set_die_type (die, type, cu);
14199 }
14200
c0dd20ea 14201 type = alloc_type (objfile);
c906108c 14202 INIT_CPLUS_SPECIFIC (type);
93311388 14203
39cbfefa
DJ
14204 name = dwarf2_name (die, cu);
14205 if (name != NULL)
c906108c 14206 {
987504bb 14207 if (cu->language == language_cplus
c44af4eb
TT
14208 || cu->language == language_d
14209 || cu->language == language_rust)
63d06c5c 14210 {
15d034d0 14211 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14212
14213 /* dwarf2_full_name might have already finished building the DIE's
14214 type. If so, there is no need to continue. */
14215 if (get_die_type (die, cu) != NULL)
14216 return get_die_type (die, cu);
14217
14218 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
14219 if (die->tag == DW_TAG_structure_type
14220 || die->tag == DW_TAG_class_type)
14221 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
14222 }
14223 else
14224 {
d8151005
DJ
14225 /* The name is already allocated along with this objfile, so
14226 we don't need to duplicate it for the type. */
7d455152 14227 TYPE_TAG_NAME (type) = name;
94af9270
KS
14228 if (die->tag == DW_TAG_class_type)
14229 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 14230 }
c906108c
SS
14231 }
14232
14233 if (die->tag == DW_TAG_structure_type)
14234 {
14235 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14236 }
14237 else if (die->tag == DW_TAG_union_type)
14238 {
14239 TYPE_CODE (type) = TYPE_CODE_UNION;
14240 }
14241 else
14242 {
4753d33b 14243 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14244 }
14245
0cc2414c
TT
14246 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14247 TYPE_DECLARED_CLASS (type) = 1;
14248
e142c38c 14249 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14250 if (attr)
14251 {
155bfbd3
JB
14252 if (attr_form_is_constant (attr))
14253 TYPE_LENGTH (type) = DW_UNSND (attr);
14254 else
14255 {
14256 /* For the moment, dynamic type sizes are not supported
14257 by GDB's struct type. The actual size is determined
14258 on-demand when resolving the type of a given object,
14259 so set the type's length to zero for now. Otherwise,
14260 we record an expression as the length, and that expression
14261 could lead to a very large value, which could eventually
14262 lead to us trying to allocate that much memory when creating
14263 a value of that type. */
14264 TYPE_LENGTH (type) = 0;
14265 }
c906108c
SS
14266 }
14267 else
14268 {
14269 TYPE_LENGTH (type) = 0;
14270 }
14271
5230b05a 14272 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 14273 {
5230b05a
WT
14274 /* ICC<14 does not output the required DW_AT_declaration on
14275 incomplete types, but gives them a size of zero. */
422b1cb0 14276 TYPE_STUB (type) = 1;
685b1105
JK
14277 }
14278 else
14279 TYPE_STUB_SUPPORTED (type) = 1;
14280
dc718098 14281 if (die_is_declaration (die, cu))
876cecd0 14282 TYPE_STUB (type) = 1;
a6c727b2
DJ
14283 else if (attr == NULL && die->child == NULL
14284 && producer_is_realview (cu->producer))
14285 /* RealView does not output the required DW_AT_declaration
14286 on incomplete types. */
14287 TYPE_STUB (type) = 1;
dc718098 14288
c906108c
SS
14289 /* We need to add the type field to the die immediately so we don't
14290 infinitely recurse when dealing with pointers to the structure
0963b4bd 14291 type within the structure itself. */
1c379e20 14292 set_die_type (die, type, cu);
c906108c 14293
7e314c57
JK
14294 /* set_die_type should be already done. */
14295 set_descriptive_type (type, die, cu);
14296
c767944b
DJ
14297 return type;
14298}
14299
14300/* Finish creating a structure or union type, including filling in
14301 its members and creating a symbol for it. */
14302
14303static void
14304process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14305{
14306 struct objfile *objfile = cu->objfile;
ca040673 14307 struct die_info *child_die;
c767944b
DJ
14308 struct type *type;
14309
14310 type = get_die_type (die, cu);
14311 if (type == NULL)
14312 type = read_structure_type (die, cu);
14313
e142c38c 14314 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
14315 {
14316 struct field_info fi;
34eaf542 14317 VEC (symbolp) *template_args = NULL;
c767944b 14318 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
14319
14320 memset (&fi, 0, sizeof (struct field_info));
14321
639d11d3 14322 child_die = die->child;
c906108c
SS
14323
14324 while (child_die && child_die->tag)
14325 {
a9a9bd0f
DC
14326 if (child_die->tag == DW_TAG_member
14327 || child_die->tag == DW_TAG_variable)
c906108c 14328 {
a9a9bd0f
DC
14329 /* NOTE: carlton/2002-11-05: A C++ static data member
14330 should be a DW_TAG_member that is a declaration, but
14331 all versions of G++ as of this writing (so through at
14332 least 3.2.1) incorrectly generate DW_TAG_variable
14333 tags for them instead. */
e7c27a73 14334 dwarf2_add_field (&fi, child_die, cu);
c906108c 14335 }
8713b1b1 14336 else if (child_die->tag == DW_TAG_subprogram)
c906108c 14337 {
e98c9e7c
TT
14338 /* Rust doesn't have member functions in the C++ sense.
14339 However, it does emit ordinary functions as children
14340 of a struct DIE. */
14341 if (cu->language == language_rust)
14342 read_func_scope (child_die, cu);
14343 else
14344 {
14345 /* C++ member function. */
14346 dwarf2_add_member_fn (&fi, child_die, type, cu);
14347 }
c906108c
SS
14348 }
14349 else if (child_die->tag == DW_TAG_inheritance)
14350 {
14351 /* C++ base class field. */
e7c27a73 14352 dwarf2_add_field (&fi, child_die, cu);
c906108c 14353 }
98751a41
JK
14354 else if (child_die->tag == DW_TAG_typedef)
14355 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
14356 else if (child_die->tag == DW_TAG_template_type_param
14357 || child_die->tag == DW_TAG_template_value_param)
14358 {
14359 struct symbol *arg = new_symbol (child_die, NULL, cu);
14360
f1078f66
DJ
14361 if (arg != NULL)
14362 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
14363 }
14364
c906108c
SS
14365 child_die = sibling_die (child_die);
14366 }
14367
34eaf542
TT
14368 /* Attach template arguments to type. */
14369 if (! VEC_empty (symbolp, template_args))
14370 {
14371 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14372 TYPE_N_TEMPLATE_ARGUMENTS (type)
14373 = VEC_length (symbolp, template_args);
14374 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
14375 = XOBNEWVEC (&objfile->objfile_obstack,
14376 struct symbol *,
14377 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
14378 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14379 VEC_address (symbolp, template_args),
14380 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14381 * sizeof (struct symbol *)));
14382 VEC_free (symbolp, template_args);
14383 }
14384
c906108c
SS
14385 /* Attach fields and member functions to the type. */
14386 if (fi.nfields)
e7c27a73 14387 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
14388 if (fi.nfnfields)
14389 {
e7c27a73 14390 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 14391
c5aa993b 14392 /* Get the type which refers to the base class (possibly this
c906108c 14393 class itself) which contains the vtable pointer for the current
0d564a31
DJ
14394 class from the DW_AT_containing_type attribute. This use of
14395 DW_AT_containing_type is a GNU extension. */
c906108c 14396
e142c38c 14397 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 14398 {
e7c27a73 14399 struct type *t = die_containing_type (die, cu);
c906108c 14400
ae6ae975 14401 set_type_vptr_basetype (type, t);
c906108c
SS
14402 if (type == t)
14403 {
c906108c
SS
14404 int i;
14405
14406 /* Our own class provides vtbl ptr. */
14407 for (i = TYPE_NFIELDS (t) - 1;
14408 i >= TYPE_N_BASECLASSES (t);
14409 --i)
14410 {
0d5cff50 14411 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 14412
1168df01 14413 if (is_vtable_name (fieldname, cu))
c906108c 14414 {
ae6ae975 14415 set_type_vptr_fieldno (type, i);
c906108c
SS
14416 break;
14417 }
14418 }
14419
14420 /* Complain if virtual function table field not found. */
14421 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 14422 complaint (&symfile_complaints,
3e43a32a
MS
14423 _("virtual function table pointer "
14424 "not found when defining class '%s'"),
4d3c2250
KB
14425 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14426 "");
c906108c
SS
14427 }
14428 else
14429 {
ae6ae975 14430 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
14431 }
14432 }
f6235d4c 14433 else if (cu->producer
61012eef 14434 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
14435 {
14436 /* The IBM XLC compiler does not provide direct indication
14437 of the containing type, but the vtable pointer is
14438 always named __vfp. */
14439
14440 int i;
14441
14442 for (i = TYPE_NFIELDS (type) - 1;
14443 i >= TYPE_N_BASECLASSES (type);
14444 --i)
14445 {
14446 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14447 {
ae6ae975
DE
14448 set_type_vptr_fieldno (type, i);
14449 set_type_vptr_basetype (type, type);
f6235d4c
EZ
14450 break;
14451 }
14452 }
14453 }
c906108c 14454 }
98751a41
JK
14455
14456 /* Copy fi.typedef_field_list linked list elements content into the
14457 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14458 if (fi.typedef_field_list)
14459 {
14460 int i = fi.typedef_field_list_count;
14461
a0d7a4ff 14462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 14463 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
14464 = ((struct typedef_field *)
14465 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
14466 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14467
14468 /* Reverse the list order to keep the debug info elements order. */
14469 while (--i >= 0)
14470 {
14471 struct typedef_field *dest, *src;
6e70227d 14472
98751a41
JK
14473 dest = &TYPE_TYPEDEF_FIELD (type, i);
14474 src = &fi.typedef_field_list->field;
14475 fi.typedef_field_list = fi.typedef_field_list->next;
14476 *dest = *src;
14477 }
14478 }
c767944b
DJ
14479
14480 do_cleanups (back_to);
c906108c 14481 }
63d06c5c 14482
bb5ed363 14483 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 14484
90aeadfc
DC
14485 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14486 snapshots) has been known to create a die giving a declaration
14487 for a class that has, as a child, a die giving a definition for a
14488 nested class. So we have to process our children even if the
14489 current die is a declaration. Normally, of course, a declaration
14490 won't have any children at all. */
134d01f1 14491
ca040673
DE
14492 child_die = die->child;
14493
90aeadfc
DC
14494 while (child_die != NULL && child_die->tag)
14495 {
14496 if (child_die->tag == DW_TAG_member
14497 || child_die->tag == DW_TAG_variable
34eaf542
TT
14498 || child_die->tag == DW_TAG_inheritance
14499 || child_die->tag == DW_TAG_template_value_param
14500 || child_die->tag == DW_TAG_template_type_param)
134d01f1 14501 {
90aeadfc 14502 /* Do nothing. */
134d01f1 14503 }
90aeadfc
DC
14504 else
14505 process_die (child_die, cu);
134d01f1 14506
90aeadfc 14507 child_die = sibling_die (child_die);
134d01f1
DJ
14508 }
14509
fa4028e9
JB
14510 /* Do not consider external references. According to the DWARF standard,
14511 these DIEs are identified by the fact that they have no byte_size
14512 attribute, and a declaration attribute. */
14513 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14514 || !die_is_declaration (die, cu))
c767944b 14515 new_symbol (die, type, cu);
134d01f1
DJ
14516}
14517
55426c9d
JB
14518/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14519 update TYPE using some information only available in DIE's children. */
14520
14521static void
14522update_enumeration_type_from_children (struct die_info *die,
14523 struct type *type,
14524 struct dwarf2_cu *cu)
14525{
60f7655a 14526 struct die_info *child_die;
55426c9d
JB
14527 int unsigned_enum = 1;
14528 int flag_enum = 1;
14529 ULONGEST mask = 0;
55426c9d 14530
8268c778 14531 auto_obstack obstack;
55426c9d 14532
60f7655a
DE
14533 for (child_die = die->child;
14534 child_die != NULL && child_die->tag;
14535 child_die = sibling_die (child_die))
55426c9d
JB
14536 {
14537 struct attribute *attr;
14538 LONGEST value;
14539 const gdb_byte *bytes;
14540 struct dwarf2_locexpr_baton *baton;
14541 const char *name;
60f7655a 14542
55426c9d
JB
14543 if (child_die->tag != DW_TAG_enumerator)
14544 continue;
14545
14546 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14547 if (attr == NULL)
14548 continue;
14549
14550 name = dwarf2_name (child_die, cu);
14551 if (name == NULL)
14552 name = "<anonymous enumerator>";
14553
14554 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14555 &value, &bytes, &baton);
14556 if (value < 0)
14557 {
14558 unsigned_enum = 0;
14559 flag_enum = 0;
14560 }
14561 else if ((mask & value) != 0)
14562 flag_enum = 0;
14563 else
14564 mask |= value;
14565
14566 /* If we already know that the enum type is neither unsigned, nor
14567 a flag type, no need to look at the rest of the enumerates. */
14568 if (!unsigned_enum && !flag_enum)
14569 break;
55426c9d
JB
14570 }
14571
14572 if (unsigned_enum)
14573 TYPE_UNSIGNED (type) = 1;
14574 if (flag_enum)
14575 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14576}
14577
134d01f1
DJ
14578/* Given a DW_AT_enumeration_type die, set its type. We do not
14579 complete the type's fields yet, or create any symbols. */
c906108c 14580
f792889a 14581static struct type *
134d01f1 14582read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14583{
e7c27a73 14584 struct objfile *objfile = cu->objfile;
c906108c 14585 struct type *type;
c906108c 14586 struct attribute *attr;
0114d602 14587 const char *name;
134d01f1 14588
348e048f
DE
14589 /* If the definition of this type lives in .debug_types, read that type.
14590 Don't follow DW_AT_specification though, that will take us back up
14591 the chain and we want to go down. */
45e58e77 14592 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14593 if (attr)
14594 {
ac9ec31b 14595 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14596
ac9ec31b 14597 /* The type's CU may not be the same as CU.
02142a6c 14598 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14599 return set_die_type (die, type, cu);
14600 }
14601
c906108c
SS
14602 type = alloc_type (objfile);
14603
14604 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14605 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14606 if (name != NULL)
7d455152 14607 TYPE_TAG_NAME (type) = name;
c906108c 14608
0626fc76
TT
14609 attr = dwarf2_attr (die, DW_AT_type, cu);
14610 if (attr != NULL)
14611 {
14612 struct type *underlying_type = die_type (die, cu);
14613
14614 TYPE_TARGET_TYPE (type) = underlying_type;
14615 }
14616
e142c38c 14617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14618 if (attr)
14619 {
14620 TYPE_LENGTH (type) = DW_UNSND (attr);
14621 }
14622 else
14623 {
14624 TYPE_LENGTH (type) = 0;
14625 }
14626
137033e9
JB
14627 /* The enumeration DIE can be incomplete. In Ada, any type can be
14628 declared as private in the package spec, and then defined only
14629 inside the package body. Such types are known as Taft Amendment
14630 Types. When another package uses such a type, an incomplete DIE
14631 may be generated by the compiler. */
02eb380e 14632 if (die_is_declaration (die, cu))
876cecd0 14633 TYPE_STUB (type) = 1;
02eb380e 14634
0626fc76
TT
14635 /* Finish the creation of this type by using the enum's children.
14636 We must call this even when the underlying type has been provided
14637 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14638 update_enumeration_type_from_children (die, type, cu);
14639
0626fc76
TT
14640 /* If this type has an underlying type that is not a stub, then we
14641 may use its attributes. We always use the "unsigned" attribute
14642 in this situation, because ordinarily we guess whether the type
14643 is unsigned -- but the guess can be wrong and the underlying type
14644 can tell us the reality. However, we defer to a local size
14645 attribute if one exists, because this lets the compiler override
14646 the underlying type if needed. */
14647 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14648 {
14649 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14650 if (TYPE_LENGTH (type) == 0)
14651 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14652 }
14653
3d567982
TT
14654 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14655
f792889a 14656 return set_die_type (die, type, cu);
134d01f1
DJ
14657}
14658
14659/* Given a pointer to a die which begins an enumeration, process all
14660 the dies that define the members of the enumeration, and create the
14661 symbol for the enumeration type.
14662
14663 NOTE: We reverse the order of the element list. */
14664
14665static void
14666process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14667{
f792889a 14668 struct type *this_type;
134d01f1 14669
f792889a
DJ
14670 this_type = get_die_type (die, cu);
14671 if (this_type == NULL)
14672 this_type = read_enumeration_type (die, cu);
9dc481d3 14673
639d11d3 14674 if (die->child != NULL)
c906108c 14675 {
9dc481d3
DE
14676 struct die_info *child_die;
14677 struct symbol *sym;
14678 struct field *fields = NULL;
14679 int num_fields = 0;
15d034d0 14680 const char *name;
9dc481d3 14681
639d11d3 14682 child_die = die->child;
c906108c
SS
14683 while (child_die && child_die->tag)
14684 {
14685 if (child_die->tag != DW_TAG_enumerator)
14686 {
e7c27a73 14687 process_die (child_die, cu);
c906108c
SS
14688 }
14689 else
14690 {
39cbfefa
DJ
14691 name = dwarf2_name (child_die, cu);
14692 if (name)
c906108c 14693 {
f792889a 14694 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14695
14696 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14697 {
14698 fields = (struct field *)
14699 xrealloc (fields,
14700 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14701 * sizeof (struct field));
c906108c
SS
14702 }
14703
3567439c 14704 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14705 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14706 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14707 FIELD_BITSIZE (fields[num_fields]) = 0;
14708
14709 num_fields++;
14710 }
14711 }
14712
14713 child_die = sibling_die (child_die);
14714 }
14715
14716 if (num_fields)
14717 {
f792889a
DJ
14718 TYPE_NFIELDS (this_type) = num_fields;
14719 TYPE_FIELDS (this_type) = (struct field *)
14720 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14721 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14722 sizeof (struct field) * num_fields);
b8c9b27d 14723 xfree (fields);
c906108c 14724 }
c906108c 14725 }
134d01f1 14726
6c83ed52
TT
14727 /* If we are reading an enum from a .debug_types unit, and the enum
14728 is a declaration, and the enum is not the signatured type in the
14729 unit, then we do not want to add a symbol for it. Adding a
14730 symbol would in some cases obscure the true definition of the
14731 enum, giving users an incomplete type when the definition is
14732 actually available. Note that we do not want to do this for all
14733 enums which are just declarations, because C++0x allows forward
14734 enum declarations. */
3019eac3 14735 if (cu->per_cu->is_debug_types
6c83ed52
TT
14736 && die_is_declaration (die, cu))
14737 {
52dc124a 14738 struct signatured_type *sig_type;
6c83ed52 14739
c0f78cd4 14740 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14741 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14742 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14743 return;
14744 }
14745
f792889a 14746 new_symbol (die, this_type, cu);
c906108c
SS
14747}
14748
14749/* Extract all information from a DW_TAG_array_type DIE and put it in
14750 the DIE's type field. For now, this only handles one dimensional
14751 arrays. */
14752
f792889a 14753static struct type *
e7c27a73 14754read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14755{
e7c27a73 14756 struct objfile *objfile = cu->objfile;
c906108c 14757 struct die_info *child_die;
7e314c57 14758 struct type *type;
c906108c 14759 struct type *element_type, *range_type, *index_type;
c906108c 14760 struct attribute *attr;
15d034d0 14761 const char *name;
dc53a7ad 14762 unsigned int bit_stride = 0;
c906108c 14763
e7c27a73 14764 element_type = die_type (die, cu);
c906108c 14765
7e314c57
JK
14766 /* The die_type call above may have already set the type for this DIE. */
14767 type = get_die_type (die, cu);
14768 if (type)
14769 return type;
14770
dc53a7ad
JB
14771 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14772 if (attr != NULL)
14773 bit_stride = DW_UNSND (attr) * 8;
14774
14775 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14776 if (attr != NULL)
14777 bit_stride = DW_UNSND (attr);
14778
c906108c
SS
14779 /* Irix 6.2 native cc creates array types without children for
14780 arrays with unspecified length. */
639d11d3 14781 if (die->child == NULL)
c906108c 14782 {
46bf5051 14783 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14784 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14785 type = create_array_type_with_stride (NULL, element_type, range_type,
14786 bit_stride);
f792889a 14787 return set_die_type (die, type, cu);
c906108c
SS
14788 }
14789
791afaa2 14790 std::vector<struct type *> range_types;
639d11d3 14791 child_die = die->child;
c906108c
SS
14792 while (child_die && child_die->tag)
14793 {
14794 if (child_die->tag == DW_TAG_subrange_type)
14795 {
f792889a 14796 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14797
f792889a 14798 if (child_type != NULL)
a02abb62 14799 {
0963b4bd
MS
14800 /* The range type was succesfully read. Save it for the
14801 array type creation. */
791afaa2 14802 range_types.push_back (child_type);
a02abb62 14803 }
c906108c
SS
14804 }
14805 child_die = sibling_die (child_die);
14806 }
14807
14808 /* Dwarf2 dimensions are output from left to right, create the
14809 necessary array types in backwards order. */
7ca2d3a3 14810
c906108c 14811 type = element_type;
7ca2d3a3
DL
14812
14813 if (read_array_order (die, cu) == DW_ORD_col_major)
14814 {
14815 int i = 0;
9a619af0 14816
791afaa2 14817 while (i < range_types.size ())
dc53a7ad
JB
14818 type = create_array_type_with_stride (NULL, type, range_types[i++],
14819 bit_stride);
7ca2d3a3
DL
14820 }
14821 else
14822 {
791afaa2 14823 size_t ndim = range_types.size ();
7ca2d3a3 14824 while (ndim-- > 0)
dc53a7ad
JB
14825 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14826 bit_stride);
7ca2d3a3 14827 }
c906108c 14828
f5f8a009
EZ
14829 /* Understand Dwarf2 support for vector types (like they occur on
14830 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14831 array type. This is not part of the Dwarf2/3 standard yet, but a
14832 custom vendor extension. The main difference between a regular
14833 array and the vector variant is that vectors are passed by value
14834 to functions. */
e142c38c 14835 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14836 if (attr)
ea37ba09 14837 make_vector_type (type);
f5f8a009 14838
dbc98a8b
KW
14839 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14840 implementation may choose to implement triple vectors using this
14841 attribute. */
14842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14843 if (attr)
14844 {
14845 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14846 TYPE_LENGTH (type) = DW_UNSND (attr);
14847 else
3e43a32a
MS
14848 complaint (&symfile_complaints,
14849 _("DW_AT_byte_size for array type smaller "
14850 "than the total size of elements"));
dbc98a8b
KW
14851 }
14852
39cbfefa
DJ
14853 name = dwarf2_name (die, cu);
14854 if (name)
14855 TYPE_NAME (type) = name;
6e70227d 14856
0963b4bd 14857 /* Install the type in the die. */
7e314c57
JK
14858 set_die_type (die, type, cu);
14859
14860 /* set_die_type should be already done. */
b4ba55a1
JB
14861 set_descriptive_type (type, die, cu);
14862
7e314c57 14863 return type;
c906108c
SS
14864}
14865
7ca2d3a3 14866static enum dwarf_array_dim_ordering
6e70227d 14867read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14868{
14869 struct attribute *attr;
14870
14871 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14872
aead7601
SM
14873 if (attr)
14874 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14875
0963b4bd
MS
14876 /* GNU F77 is a special case, as at 08/2004 array type info is the
14877 opposite order to the dwarf2 specification, but data is still
14878 laid out as per normal fortran.
7ca2d3a3 14879
0963b4bd
MS
14880 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14881 version checking. */
7ca2d3a3 14882
905e0470
PM
14883 if (cu->language == language_fortran
14884 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14885 {
14886 return DW_ORD_row_major;
14887 }
14888
6e70227d 14889 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14890 {
14891 case array_column_major:
14892 return DW_ORD_col_major;
14893 case array_row_major:
14894 default:
14895 return DW_ORD_row_major;
14896 };
14897}
14898
72019c9c 14899/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14900 the DIE's type field. */
72019c9c 14901
f792889a 14902static struct type *
72019c9c
GM
14903read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14904{
7e314c57
JK
14905 struct type *domain_type, *set_type;
14906 struct attribute *attr;
f792889a 14907
7e314c57
JK
14908 domain_type = die_type (die, cu);
14909
14910 /* The die_type call above may have already set the type for this DIE. */
14911 set_type = get_die_type (die, cu);
14912 if (set_type)
14913 return set_type;
14914
14915 set_type = create_set_type (NULL, domain_type);
14916
14917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14918 if (attr)
14919 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14920
f792889a 14921 return set_die_type (die, set_type, cu);
72019c9c 14922}
7ca2d3a3 14923
0971de02
TT
14924/* A helper for read_common_block that creates a locexpr baton.
14925 SYM is the symbol which we are marking as computed.
14926 COMMON_DIE is the DIE for the common block.
14927 COMMON_LOC is the location expression attribute for the common
14928 block itself.
14929 MEMBER_LOC is the location expression attribute for the particular
14930 member of the common block that we are processing.
14931 CU is the CU from which the above come. */
14932
14933static void
14934mark_common_block_symbol_computed (struct symbol *sym,
14935 struct die_info *common_die,
14936 struct attribute *common_loc,
14937 struct attribute *member_loc,
14938 struct dwarf2_cu *cu)
14939{
14940 struct objfile *objfile = dwarf2_per_objfile->objfile;
14941 struct dwarf2_locexpr_baton *baton;
14942 gdb_byte *ptr;
14943 unsigned int cu_off;
14944 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14945 LONGEST offset = 0;
14946
14947 gdb_assert (common_loc && member_loc);
14948 gdb_assert (attr_form_is_block (common_loc));
14949 gdb_assert (attr_form_is_block (member_loc)
14950 || attr_form_is_constant (member_loc));
14951
8d749320 14952 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14953 baton->per_cu = cu->per_cu;
14954 gdb_assert (baton->per_cu);
14955
14956 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14957
14958 if (attr_form_is_constant (member_loc))
14959 {
14960 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14961 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14962 }
14963 else
14964 baton->size += DW_BLOCK (member_loc)->size;
14965
224c3ddb 14966 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14967 baton->data = ptr;
14968
14969 *ptr++ = DW_OP_call4;
9c541725 14970 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14971 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14972 ptr += 4;
14973
14974 if (attr_form_is_constant (member_loc))
14975 {
14976 *ptr++ = DW_OP_addr;
14977 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14978 ptr += cu->header.addr_size;
14979 }
14980 else
14981 {
14982 /* We have to copy the data here, because DW_OP_call4 will only
14983 use a DW_AT_location attribute. */
14984 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14985 ptr += DW_BLOCK (member_loc)->size;
14986 }
14987
14988 *ptr++ = DW_OP_plus;
14989 gdb_assert (ptr - baton->data == baton->size);
14990
0971de02 14991 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14992 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14993}
14994
4357ac6c
TT
14995/* Create appropriate locally-scoped variables for all the
14996 DW_TAG_common_block entries. Also create a struct common_block
14997 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14998 is used to sepate the common blocks name namespace from regular
14999 variable names. */
c906108c
SS
15000
15001static void
e7c27a73 15002read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15003{
0971de02
TT
15004 struct attribute *attr;
15005
15006 attr = dwarf2_attr (die, DW_AT_location, cu);
15007 if (attr)
15008 {
15009 /* Support the .debug_loc offsets. */
15010 if (attr_form_is_block (attr))
15011 {
15012 /* Ok. */
15013 }
15014 else if (attr_form_is_section_offset (attr))
15015 {
15016 dwarf2_complex_location_expr_complaint ();
15017 attr = NULL;
15018 }
15019 else
15020 {
15021 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15022 "common block member");
15023 attr = NULL;
15024 }
15025 }
15026
639d11d3 15027 if (die->child != NULL)
c906108c 15028 {
4357ac6c
TT
15029 struct objfile *objfile = cu->objfile;
15030 struct die_info *child_die;
15031 size_t n_entries = 0, size;
15032 struct common_block *common_block;
15033 struct symbol *sym;
74ac6d43 15034
4357ac6c
TT
15035 for (child_die = die->child;
15036 child_die && child_die->tag;
15037 child_die = sibling_die (child_die))
15038 ++n_entries;
15039
15040 size = (sizeof (struct common_block)
15041 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15042 common_block
15043 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15044 size);
4357ac6c
TT
15045 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15046 common_block->n_entries = 0;
15047
15048 for (child_die = die->child;
15049 child_die && child_die->tag;
15050 child_die = sibling_die (child_die))
15051 {
15052 /* Create the symbol in the DW_TAG_common_block block in the current
15053 symbol scope. */
e7c27a73 15054 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15055 if (sym != NULL)
15056 {
15057 struct attribute *member_loc;
15058
15059 common_block->contents[common_block->n_entries++] = sym;
15060
15061 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15062 cu);
15063 if (member_loc)
15064 {
15065 /* GDB has handled this for a long time, but it is
15066 not specified by DWARF. It seems to have been
15067 emitted by gfortran at least as recently as:
15068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15069 complaint (&symfile_complaints,
15070 _("Variable in common block has "
15071 "DW_AT_data_member_location "
15072 "- DIE at 0x%x [in module %s]"),
9c541725 15073 to_underlying (child_die->sect_off),
4262abfb 15074 objfile_name (cu->objfile));
0971de02
TT
15075
15076 if (attr_form_is_section_offset (member_loc))
15077 dwarf2_complex_location_expr_complaint ();
15078 else if (attr_form_is_constant (member_loc)
15079 || attr_form_is_block (member_loc))
15080 {
15081 if (attr)
15082 mark_common_block_symbol_computed (sym, die, attr,
15083 member_loc, cu);
15084 }
15085 else
15086 dwarf2_complex_location_expr_complaint ();
15087 }
15088 }
c906108c 15089 }
4357ac6c
TT
15090
15091 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15092 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15093 }
15094}
15095
0114d602 15096/* Create a type for a C++ namespace. */
d9fa45fe 15097
0114d602
DJ
15098static struct type *
15099read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15100{
e7c27a73 15101 struct objfile *objfile = cu->objfile;
0114d602 15102 const char *previous_prefix, *name;
9219021c 15103 int is_anonymous;
0114d602
DJ
15104 struct type *type;
15105
15106 /* For extensions, reuse the type of the original namespace. */
15107 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15108 {
15109 struct die_info *ext_die;
15110 struct dwarf2_cu *ext_cu = cu;
9a619af0 15111
0114d602
DJ
15112 ext_die = dwarf2_extension (die, &ext_cu);
15113 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
15114
15115 /* EXT_CU may not be the same as CU.
02142a6c 15116 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
15117 return set_die_type (die, type, cu);
15118 }
9219021c 15119
e142c38c 15120 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
15121
15122 /* Now build the name of the current namespace. */
15123
0114d602
DJ
15124 previous_prefix = determine_prefix (die, cu);
15125 if (previous_prefix[0] != '\0')
15126 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 15127 previous_prefix, name, 0, cu);
0114d602
DJ
15128
15129 /* Create the type. */
19f392bc 15130 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
15131 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15132
60531b24 15133 return set_die_type (die, type, cu);
0114d602
DJ
15134}
15135
22cee43f 15136/* Read a namespace scope. */
0114d602
DJ
15137
15138static void
15139read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15140{
15141 struct objfile *objfile = cu->objfile;
0114d602 15142 int is_anonymous;
9219021c 15143
5c4e30ca
DC
15144 /* Add a symbol associated to this if we haven't seen the namespace
15145 before. Also, add a using directive if it's an anonymous
15146 namespace. */
9219021c 15147
f2f0e013 15148 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
15149 {
15150 struct type *type;
15151
0114d602 15152 type = read_type_die (die, cu);
e7c27a73 15153 new_symbol (die, type, cu);
5c4e30ca 15154
e8e80198 15155 namespace_name (die, &is_anonymous, cu);
5c4e30ca 15156 if (is_anonymous)
0114d602
DJ
15157 {
15158 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 15159
eb1e02fd 15160 std::vector<const char *> excludes;
22cee43f
PMR
15161 add_using_directive (using_directives (cu->language),
15162 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 15163 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 15164 }
5c4e30ca 15165 }
9219021c 15166
639d11d3 15167 if (die->child != NULL)
d9fa45fe 15168 {
639d11d3 15169 struct die_info *child_die = die->child;
6e70227d 15170
d9fa45fe
DC
15171 while (child_die && child_die->tag)
15172 {
e7c27a73 15173 process_die (child_die, cu);
d9fa45fe
DC
15174 child_die = sibling_die (child_die);
15175 }
15176 }
38d518c9
EZ
15177}
15178
f55ee35c
JK
15179/* Read a Fortran module as type. This DIE can be only a declaration used for
15180 imported module. Still we need that type as local Fortran "use ... only"
15181 declaration imports depend on the created type in determine_prefix. */
15182
15183static struct type *
15184read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15185{
15186 struct objfile *objfile = cu->objfile;
15d034d0 15187 const char *module_name;
f55ee35c
JK
15188 struct type *type;
15189
15190 module_name = dwarf2_name (die, cu);
15191 if (!module_name)
3e43a32a
MS
15192 complaint (&symfile_complaints,
15193 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 15194 to_underlying (die->sect_off));
19f392bc 15195 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
15196
15197 /* determine_prefix uses TYPE_TAG_NAME. */
15198 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15199
15200 return set_die_type (die, type, cu);
15201}
15202
5d7cb8df
JK
15203/* Read a Fortran module. */
15204
15205static void
15206read_module (struct die_info *die, struct dwarf2_cu *cu)
15207{
15208 struct die_info *child_die = die->child;
530e8392
KB
15209 struct type *type;
15210
15211 type = read_type_die (die, cu);
15212 new_symbol (die, type, cu);
5d7cb8df 15213
5d7cb8df
JK
15214 while (child_die && child_die->tag)
15215 {
15216 process_die (child_die, cu);
15217 child_die = sibling_die (child_die);
15218 }
15219}
15220
38d518c9
EZ
15221/* Return the name of the namespace represented by DIE. Set
15222 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15223 namespace. */
15224
15225static const char *
e142c38c 15226namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
15227{
15228 struct die_info *current_die;
15229 const char *name = NULL;
15230
15231 /* Loop through the extensions until we find a name. */
15232
15233 for (current_die = die;
15234 current_die != NULL;
f2f0e013 15235 current_die = dwarf2_extension (die, &cu))
38d518c9 15236 {
96553a0c
DE
15237 /* We don't use dwarf2_name here so that we can detect the absence
15238 of a name -> anonymous namespace. */
7d45c7c3 15239 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 15240
38d518c9
EZ
15241 if (name != NULL)
15242 break;
15243 }
15244
15245 /* Is it an anonymous namespace? */
15246
15247 *is_anonymous = (name == NULL);
15248 if (*is_anonymous)
2b1dbab0 15249 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
15250
15251 return name;
d9fa45fe
DC
15252}
15253
c906108c
SS
15254/* Extract all information from a DW_TAG_pointer_type DIE and add to
15255 the user defined type vector. */
15256
f792889a 15257static struct type *
e7c27a73 15258read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15259{
5e2b427d 15260 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 15261 struct comp_unit_head *cu_header = &cu->header;
c906108c 15262 struct type *type;
8b2dbe47
KB
15263 struct attribute *attr_byte_size;
15264 struct attribute *attr_address_class;
15265 int byte_size, addr_class;
7e314c57
JK
15266 struct type *target_type;
15267
15268 target_type = die_type (die, cu);
c906108c 15269
7e314c57
JK
15270 /* The die_type call above may have already set the type for this DIE. */
15271 type = get_die_type (die, cu);
15272 if (type)
15273 return type;
15274
15275 type = lookup_pointer_type (target_type);
8b2dbe47 15276
e142c38c 15277 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
15278 if (attr_byte_size)
15279 byte_size = DW_UNSND (attr_byte_size);
c906108c 15280 else
8b2dbe47
KB
15281 byte_size = cu_header->addr_size;
15282
e142c38c 15283 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
15284 if (attr_address_class)
15285 addr_class = DW_UNSND (attr_address_class);
15286 else
15287 addr_class = DW_ADDR_none;
15288
15289 /* If the pointer size or address class is different than the
15290 default, create a type variant marked as such and set the
15291 length accordingly. */
15292 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 15293 {
5e2b427d 15294 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
15295 {
15296 int type_flags;
15297
849957d9 15298 type_flags = gdbarch_address_class_type_flags
5e2b427d 15299 (gdbarch, byte_size, addr_class);
876cecd0
TT
15300 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15301 == 0);
8b2dbe47
KB
15302 type = make_type_with_address_space (type, type_flags);
15303 }
15304 else if (TYPE_LENGTH (type) != byte_size)
15305 {
3e43a32a
MS
15306 complaint (&symfile_complaints,
15307 _("invalid pointer size %d"), byte_size);
8b2dbe47 15308 }
6e70227d 15309 else
9a619af0
MS
15310 {
15311 /* Should we also complain about unhandled address classes? */
15312 }
c906108c 15313 }
8b2dbe47
KB
15314
15315 TYPE_LENGTH (type) = byte_size;
f792889a 15316 return set_die_type (die, type, cu);
c906108c
SS
15317}
15318
15319/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15320 the user defined type vector. */
15321
f792889a 15322static struct type *
e7c27a73 15323read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
15324{
15325 struct type *type;
15326 struct type *to_type;
15327 struct type *domain;
15328
e7c27a73
DJ
15329 to_type = die_type (die, cu);
15330 domain = die_containing_type (die, cu);
0d5de010 15331
7e314c57
JK
15332 /* The calls above may have already set the type for this DIE. */
15333 type = get_die_type (die, cu);
15334 if (type)
15335 return type;
15336
0d5de010
DJ
15337 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15338 type = lookup_methodptr_type (to_type);
7078baeb
TT
15339 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15340 {
15341 struct type *new_type = alloc_type (cu->objfile);
15342
15343 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15344 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15345 TYPE_VARARGS (to_type));
15346 type = lookup_methodptr_type (new_type);
15347 }
0d5de010
DJ
15348 else
15349 type = lookup_memberptr_type (to_type, domain);
c906108c 15350
f792889a 15351 return set_die_type (die, type, cu);
c906108c
SS
15352}
15353
4297a3f0 15354/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
15355 the user defined type vector. */
15356
f792889a 15357static struct type *
4297a3f0
AV
15358read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15359 enum type_code refcode)
c906108c 15360{
e7c27a73 15361 struct comp_unit_head *cu_header = &cu->header;
7e314c57 15362 struct type *type, *target_type;
c906108c
SS
15363 struct attribute *attr;
15364
4297a3f0
AV
15365 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15366
7e314c57
JK
15367 target_type = die_type (die, cu);
15368
15369 /* The die_type call above may have already set the type for this DIE. */
15370 type = get_die_type (die, cu);
15371 if (type)
15372 return type;
15373
4297a3f0 15374 type = lookup_reference_type (target_type, refcode);
e142c38c 15375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15376 if (attr)
15377 {
15378 TYPE_LENGTH (type) = DW_UNSND (attr);
15379 }
15380 else
15381 {
107d2387 15382 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 15383 }
f792889a 15384 return set_die_type (die, type, cu);
c906108c
SS
15385}
15386
cf363f18
MW
15387/* Add the given cv-qualifiers to the element type of the array. GCC
15388 outputs DWARF type qualifiers that apply to an array, not the
15389 element type. But GDB relies on the array element type to carry
15390 the cv-qualifiers. This mimics section 6.7.3 of the C99
15391 specification. */
15392
15393static struct type *
15394add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15395 struct type *base_type, int cnst, int voltl)
15396{
15397 struct type *el_type, *inner_array;
15398
15399 base_type = copy_type (base_type);
15400 inner_array = base_type;
15401
15402 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15403 {
15404 TYPE_TARGET_TYPE (inner_array) =
15405 copy_type (TYPE_TARGET_TYPE (inner_array));
15406 inner_array = TYPE_TARGET_TYPE (inner_array);
15407 }
15408
15409 el_type = TYPE_TARGET_TYPE (inner_array);
15410 cnst |= TYPE_CONST (el_type);
15411 voltl |= TYPE_VOLATILE (el_type);
15412 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15413
15414 return set_die_type (die, base_type, cu);
15415}
15416
f792889a 15417static struct type *
e7c27a73 15418read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15419{
f792889a 15420 struct type *base_type, *cv_type;
c906108c 15421
e7c27a73 15422 base_type = die_type (die, cu);
7e314c57
JK
15423
15424 /* The die_type call above may have already set the type for this DIE. */
15425 cv_type = get_die_type (die, cu);
15426 if (cv_type)
15427 return cv_type;
15428
2f608a3a
KW
15429 /* In case the const qualifier is applied to an array type, the element type
15430 is so qualified, not the array type (section 6.7.3 of C99). */
15431 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 15432 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 15433
f792889a
DJ
15434 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15435 return set_die_type (die, cv_type, cu);
c906108c
SS
15436}
15437
f792889a 15438static struct type *
e7c27a73 15439read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15440{
f792889a 15441 struct type *base_type, *cv_type;
c906108c 15442
e7c27a73 15443 base_type = die_type (die, cu);
7e314c57
JK
15444
15445 /* The die_type call above may have already set the type for this DIE. */
15446 cv_type = get_die_type (die, cu);
15447 if (cv_type)
15448 return cv_type;
15449
cf363f18
MW
15450 /* In case the volatile qualifier is applied to an array type, the
15451 element type is so qualified, not the array type (section 6.7.3
15452 of C99). */
15453 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15454 return add_array_cv_type (die, cu, base_type, 0, 1);
15455
f792889a
DJ
15456 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15457 return set_die_type (die, cv_type, cu);
c906108c
SS
15458}
15459
06d66ee9
TT
15460/* Handle DW_TAG_restrict_type. */
15461
15462static struct type *
15463read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15464{
15465 struct type *base_type, *cv_type;
15466
15467 base_type = die_type (die, cu);
15468
15469 /* The die_type call above may have already set the type for this DIE. */
15470 cv_type = get_die_type (die, cu);
15471 if (cv_type)
15472 return cv_type;
15473
15474 cv_type = make_restrict_type (base_type);
15475 return set_die_type (die, cv_type, cu);
15476}
15477
a2c2acaf
MW
15478/* Handle DW_TAG_atomic_type. */
15479
15480static struct type *
15481read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15482{
15483 struct type *base_type, *cv_type;
15484
15485 base_type = die_type (die, cu);
15486
15487 /* The die_type call above may have already set the type for this DIE. */
15488 cv_type = get_die_type (die, cu);
15489 if (cv_type)
15490 return cv_type;
15491
15492 cv_type = make_atomic_type (base_type);
15493 return set_die_type (die, cv_type, cu);
15494}
15495
c906108c
SS
15496/* Extract all information from a DW_TAG_string_type DIE and add to
15497 the user defined type vector. It isn't really a user defined type,
15498 but it behaves like one, with other DIE's using an AT_user_def_type
15499 attribute to reference it. */
15500
f792889a 15501static struct type *
e7c27a73 15502read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15503{
e7c27a73 15504 struct objfile *objfile = cu->objfile;
3b7538c0 15505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15506 struct type *type, *range_type, *index_type, *char_type;
15507 struct attribute *attr;
15508 unsigned int length;
15509
e142c38c 15510 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15511 if (attr)
15512 {
15513 length = DW_UNSND (attr);
15514 }
15515 else
15516 {
0963b4bd 15517 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15518 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15519 if (attr)
15520 {
15521 length = DW_UNSND (attr);
15522 }
15523 else
15524 {
15525 length = 1;
15526 }
c906108c 15527 }
6ccb9162 15528
46bf5051 15529 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15530 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15531 char_type = language_string_char_type (cu->language_defn, gdbarch);
15532 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15533
f792889a 15534 return set_die_type (die, type, cu);
c906108c
SS
15535}
15536
4d804846
JB
15537/* Assuming that DIE corresponds to a function, returns nonzero
15538 if the function is prototyped. */
15539
15540static int
15541prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15542{
15543 struct attribute *attr;
15544
15545 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15546 if (attr && (DW_UNSND (attr) != 0))
15547 return 1;
15548
15549 /* The DWARF standard implies that the DW_AT_prototyped attribute
15550 is only meaninful for C, but the concept also extends to other
15551 languages that allow unprototyped functions (Eg: Objective C).
15552 For all other languages, assume that functions are always
15553 prototyped. */
15554 if (cu->language != language_c
15555 && cu->language != language_objc
15556 && cu->language != language_opencl)
15557 return 1;
15558
15559 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15560 prototyped and unprototyped functions; default to prototyped,
15561 since that is more common in modern code (and RealView warns
15562 about unprototyped functions). */
15563 if (producer_is_realview (cu->producer))
15564 return 1;
15565
15566 return 0;
15567}
15568
c906108c
SS
15569/* Handle DIES due to C code like:
15570
15571 struct foo
c5aa993b
JM
15572 {
15573 int (*funcp)(int a, long l);
15574 int b;
15575 };
c906108c 15576
0963b4bd 15577 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15578
f792889a 15579static struct type *
e7c27a73 15580read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15581{
bb5ed363 15582 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15583 struct type *type; /* Type that this function returns. */
15584 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15585 struct attribute *attr;
15586
e7c27a73 15587 type = die_type (die, cu);
7e314c57
JK
15588
15589 /* The die_type call above may have already set the type for this DIE. */
15590 ftype = get_die_type (die, cu);
15591 if (ftype)
15592 return ftype;
15593
0c8b41f1 15594 ftype = lookup_function_type (type);
c906108c 15595
4d804846 15596 if (prototyped_function_p (die, cu))
a6c727b2 15597 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15598
c055b101
CV
15599 /* Store the calling convention in the type if it's available in
15600 the subroutine die. Otherwise set the calling convention to
15601 the default value DW_CC_normal. */
15602 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15603 if (attr)
15604 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15605 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15606 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15607 else
15608 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15609
743649fd
MW
15610 /* Record whether the function returns normally to its caller or not
15611 if the DWARF producer set that information. */
15612 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15613 if (attr && (DW_UNSND (attr) != 0))
15614 TYPE_NO_RETURN (ftype) = 1;
15615
76c10ea2
GM
15616 /* We need to add the subroutine type to the die immediately so
15617 we don't infinitely recurse when dealing with parameters
0963b4bd 15618 declared as the same subroutine type. */
76c10ea2 15619 set_die_type (die, ftype, cu);
6e70227d 15620
639d11d3 15621 if (die->child != NULL)
c906108c 15622 {
bb5ed363 15623 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15624 struct die_info *child_die;
8072405b 15625 int nparams, iparams;
c906108c
SS
15626
15627 /* Count the number of parameters.
15628 FIXME: GDB currently ignores vararg functions, but knows about
15629 vararg member functions. */
8072405b 15630 nparams = 0;
639d11d3 15631 child_die = die->child;
c906108c
SS
15632 while (child_die && child_die->tag)
15633 {
15634 if (child_die->tag == DW_TAG_formal_parameter)
15635 nparams++;
15636 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15637 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15638 child_die = sibling_die (child_die);
15639 }
15640
15641 /* Allocate storage for parameters and fill them in. */
15642 TYPE_NFIELDS (ftype) = nparams;
15643 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15644 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15645
8072405b
JK
15646 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15647 even if we error out during the parameters reading below. */
15648 for (iparams = 0; iparams < nparams; iparams++)
15649 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15650
15651 iparams = 0;
639d11d3 15652 child_die = die->child;
c906108c
SS
15653 while (child_die && child_die->tag)
15654 {
15655 if (child_die->tag == DW_TAG_formal_parameter)
15656 {
3ce3b1ba
PA
15657 struct type *arg_type;
15658
15659 /* DWARF version 2 has no clean way to discern C++
15660 static and non-static member functions. G++ helps
15661 GDB by marking the first parameter for non-static
15662 member functions (which is the this pointer) as
15663 artificial. We pass this information to
15664 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15665
15666 DWARF version 3 added DW_AT_object_pointer, which GCC
15667 4.5 does not yet generate. */
e142c38c 15668 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15669 if (attr)
15670 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15671 else
9c37b5ae 15672 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15673 arg_type = die_type (child_die, cu);
15674
15675 /* RealView does not mark THIS as const, which the testsuite
15676 expects. GCC marks THIS as const in method definitions,
15677 but not in the class specifications (GCC PR 43053). */
15678 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15679 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15680 {
15681 int is_this = 0;
15682 struct dwarf2_cu *arg_cu = cu;
15683 const char *name = dwarf2_name (child_die, cu);
15684
15685 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15686 if (attr)
15687 {
15688 /* If the compiler emits this, use it. */
15689 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15690 is_this = 1;
15691 }
15692 else if (name && strcmp (name, "this") == 0)
15693 /* Function definitions will have the argument names. */
15694 is_this = 1;
15695 else if (name == NULL && iparams == 0)
15696 /* Declarations may not have the names, so like
15697 elsewhere in GDB, assume an artificial first
15698 argument is "this". */
15699 is_this = 1;
15700
15701 if (is_this)
15702 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15703 arg_type, 0);
15704 }
15705
15706 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15707 iparams++;
15708 }
15709 child_die = sibling_die (child_die);
15710 }
15711 }
15712
76c10ea2 15713 return ftype;
c906108c
SS
15714}
15715
f792889a 15716static struct type *
e7c27a73 15717read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15718{
e7c27a73 15719 struct objfile *objfile = cu->objfile;
0114d602 15720 const char *name = NULL;
3c8e0968 15721 struct type *this_type, *target_type;
c906108c 15722
94af9270 15723 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15724 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15725 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15726 set_die_type (die, this_type, cu);
3c8e0968
DE
15727 target_type = die_type (die, cu);
15728 if (target_type != this_type)
15729 TYPE_TARGET_TYPE (this_type) = target_type;
15730 else
15731 {
15732 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15733 spec and cause infinite loops in GDB. */
15734 complaint (&symfile_complaints,
15735 _("Self-referential DW_TAG_typedef "
15736 "- DIE at 0x%x [in module %s]"),
9c541725 15737 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15738 TYPE_TARGET_TYPE (this_type) = NULL;
15739 }
f792889a 15740 return this_type;
c906108c
SS
15741}
15742
9b790ce7
UW
15743/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15744 (which may be different from NAME) to the architecture back-end to allow
15745 it to guess the correct format if necessary. */
15746
15747static struct type *
15748dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15749 const char *name_hint)
15750{
15751 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15752 const struct floatformat **format;
15753 struct type *type;
15754
15755 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15756 if (format)
15757 type = init_float_type (objfile, bits, name, format);
15758 else
77b7c781 15759 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
15760
15761 return type;
15762}
15763
c906108c
SS
15764/* Find a representation of a given base type and install
15765 it in the TYPE field of the die. */
15766
f792889a 15767static struct type *
e7c27a73 15768read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15769{
e7c27a73 15770 struct objfile *objfile = cu->objfile;
c906108c
SS
15771 struct type *type;
15772 struct attribute *attr;
19f392bc 15773 int encoding = 0, bits = 0;
15d034d0 15774 const char *name;
c906108c 15775
e142c38c 15776 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15777 if (attr)
15778 {
15779 encoding = DW_UNSND (attr);
15780 }
e142c38c 15781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15782 if (attr)
15783 {
19f392bc 15784 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15785 }
39cbfefa 15786 name = dwarf2_name (die, cu);
6ccb9162 15787 if (!name)
c906108c 15788 {
6ccb9162
UW
15789 complaint (&symfile_complaints,
15790 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15791 }
6ccb9162
UW
15792
15793 switch (encoding)
c906108c 15794 {
6ccb9162
UW
15795 case DW_ATE_address:
15796 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 15797 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 15798 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15799 break;
15800 case DW_ATE_boolean:
19f392bc 15801 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15802 break;
15803 case DW_ATE_complex_float:
9b790ce7 15804 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15805 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15806 break;
15807 case DW_ATE_decimal_float:
19f392bc 15808 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15809 break;
15810 case DW_ATE_float:
9b790ce7 15811 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15812 break;
15813 case DW_ATE_signed:
19f392bc 15814 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15815 break;
15816 case DW_ATE_unsigned:
3b2b8fea
TT
15817 if (cu->language == language_fortran
15818 && name
61012eef 15819 && startswith (name, "character("))
19f392bc
UW
15820 type = init_character_type (objfile, bits, 1, name);
15821 else
15822 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15823 break;
15824 case DW_ATE_signed_char:
6e70227d 15825 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15826 || cu->language == language_pascal
15827 || cu->language == language_fortran)
19f392bc
UW
15828 type = init_character_type (objfile, bits, 0, name);
15829 else
15830 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15831 break;
15832 case DW_ATE_unsigned_char:
868a0084 15833 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15834 || cu->language == language_pascal
c44af4eb
TT
15835 || cu->language == language_fortran
15836 || cu->language == language_rust)
19f392bc
UW
15837 type = init_character_type (objfile, bits, 1, name);
15838 else
15839 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15840 break;
75079b2b 15841 case DW_ATE_UTF:
53e710ac
PA
15842 {
15843 gdbarch *arch = get_objfile_arch (objfile);
15844
15845 if (bits == 16)
15846 type = builtin_type (arch)->builtin_char16;
15847 else if (bits == 32)
15848 type = builtin_type (arch)->builtin_char32;
15849 else
15850 {
15851 complaint (&symfile_complaints,
15852 _("unsupported DW_ATE_UTF bit size: '%d'"),
15853 bits);
15854 type = init_integer_type (objfile, bits, 1, name);
15855 }
15856 return set_die_type (die, type, cu);
15857 }
75079b2b
TT
15858 break;
15859
6ccb9162
UW
15860 default:
15861 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15862 dwarf_type_encoding_name (encoding));
77b7c781 15863 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 15864 break;
c906108c 15865 }
6ccb9162 15866
0114d602 15867 if (name && strcmp (name, "char") == 0)
876cecd0 15868 TYPE_NOSIGN (type) = 1;
0114d602 15869
f792889a 15870 return set_die_type (die, type, cu);
c906108c
SS
15871}
15872
80180f79
SA
15873/* Parse dwarf attribute if it's a block, reference or constant and put the
15874 resulting value of the attribute into struct bound_prop.
15875 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15876
15877static int
15878attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15879 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15880{
15881 struct dwarf2_property_baton *baton;
15882 struct obstack *obstack = &cu->objfile->objfile_obstack;
15883
15884 if (attr == NULL || prop == NULL)
15885 return 0;
15886
15887 if (attr_form_is_block (attr))
15888 {
8d749320 15889 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15890 baton->referenced_type = NULL;
15891 baton->locexpr.per_cu = cu->per_cu;
15892 baton->locexpr.size = DW_BLOCK (attr)->size;
15893 baton->locexpr.data = DW_BLOCK (attr)->data;
15894 prop->data.baton = baton;
15895 prop->kind = PROP_LOCEXPR;
15896 gdb_assert (prop->data.baton != NULL);
15897 }
15898 else if (attr_form_is_ref (attr))
15899 {
15900 struct dwarf2_cu *target_cu = cu;
15901 struct die_info *target_die;
15902 struct attribute *target_attr;
15903
15904 target_die = follow_die_ref (die, attr, &target_cu);
15905 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15906 if (target_attr == NULL)
15907 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15908 target_cu);
80180f79
SA
15909 if (target_attr == NULL)
15910 return 0;
15911
df25ebbd 15912 switch (target_attr->name)
80180f79 15913 {
df25ebbd
JB
15914 case DW_AT_location:
15915 if (attr_form_is_section_offset (target_attr))
15916 {
8d749320 15917 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15918 baton->referenced_type = die_type (target_die, target_cu);
15919 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15920 prop->data.baton = baton;
15921 prop->kind = PROP_LOCLIST;
15922 gdb_assert (prop->data.baton != NULL);
15923 }
15924 else if (attr_form_is_block (target_attr))
15925 {
8d749320 15926 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15927 baton->referenced_type = die_type (target_die, target_cu);
15928 baton->locexpr.per_cu = cu->per_cu;
15929 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15930 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15931 prop->data.baton = baton;
15932 prop->kind = PROP_LOCEXPR;
15933 gdb_assert (prop->data.baton != NULL);
15934 }
15935 else
15936 {
15937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15938 "dynamic property");
15939 return 0;
15940 }
15941 break;
15942 case DW_AT_data_member_location:
15943 {
15944 LONGEST offset;
15945
15946 if (!handle_data_member_location (target_die, target_cu,
15947 &offset))
15948 return 0;
15949
8d749320 15950 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15951 baton->referenced_type = read_type_die (target_die->parent,
15952 target_cu);
df25ebbd
JB
15953 baton->offset_info.offset = offset;
15954 baton->offset_info.type = die_type (target_die, target_cu);
15955 prop->data.baton = baton;
15956 prop->kind = PROP_ADDR_OFFSET;
15957 break;
15958 }
80180f79
SA
15959 }
15960 }
15961 else if (attr_form_is_constant (attr))
15962 {
15963 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15964 prop->kind = PROP_CONST;
15965 }
15966 else
15967 {
15968 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15969 dwarf2_name (die, cu));
15970 return 0;
15971 }
15972
15973 return 1;
15974}
15975
a02abb62
JB
15976/* Read the given DW_AT_subrange DIE. */
15977
f792889a 15978static struct type *
a02abb62
JB
15979read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15980{
4c9ad8c2 15981 struct type *base_type, *orig_base_type;
a02abb62
JB
15982 struct type *range_type;
15983 struct attribute *attr;
729efb13 15984 struct dynamic_prop low, high;
4fae6e18 15985 int low_default_is_valid;
c451ebe5 15986 int high_bound_is_count = 0;
15d034d0 15987 const char *name;
43bbcdc2 15988 LONGEST negative_mask;
e77813c8 15989
4c9ad8c2
TT
15990 orig_base_type = die_type (die, cu);
15991 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15992 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15993 creating the range type, but we use the result of check_typedef
15994 when examining properties of the type. */
15995 base_type = check_typedef (orig_base_type);
a02abb62 15996
7e314c57
JK
15997 /* The die_type call above may have already set the type for this DIE. */
15998 range_type = get_die_type (die, cu);
15999 if (range_type)
16000 return range_type;
16001
729efb13
SA
16002 low.kind = PROP_CONST;
16003 high.kind = PROP_CONST;
16004 high.data.const_val = 0;
16005
4fae6e18
JK
16006 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16007 omitting DW_AT_lower_bound. */
16008 switch (cu->language)
6e70227d 16009 {
4fae6e18
JK
16010 case language_c:
16011 case language_cplus:
729efb13 16012 low.data.const_val = 0;
4fae6e18
JK
16013 low_default_is_valid = 1;
16014 break;
16015 case language_fortran:
729efb13 16016 low.data.const_val = 1;
4fae6e18
JK
16017 low_default_is_valid = 1;
16018 break;
16019 case language_d:
4fae6e18 16020 case language_objc:
c44af4eb 16021 case language_rust:
729efb13 16022 low.data.const_val = 0;
4fae6e18
JK
16023 low_default_is_valid = (cu->header.version >= 4);
16024 break;
16025 case language_ada:
16026 case language_m2:
16027 case language_pascal:
729efb13 16028 low.data.const_val = 1;
4fae6e18
JK
16029 low_default_is_valid = (cu->header.version >= 4);
16030 break;
16031 default:
729efb13 16032 low.data.const_val = 0;
4fae6e18
JK
16033 low_default_is_valid = 0;
16034 break;
a02abb62
JB
16035 }
16036
e142c38c 16037 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 16038 if (attr)
11c1ba78 16039 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
16040 else if (!low_default_is_valid)
16041 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16042 "- DIE at 0x%x [in module %s]"),
9c541725 16043 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 16044
e142c38c 16045 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 16046 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
16047 {
16048 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 16049 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 16050 {
c451ebe5
SA
16051 /* If bounds are constant do the final calculation here. */
16052 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16053 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16054 else
16055 high_bound_is_count = 1;
c2ff108b 16056 }
e77813c8
PM
16057 }
16058
16059 /* Dwarf-2 specifications explicitly allows to create subrange types
16060 without specifying a base type.
16061 In that case, the base type must be set to the type of
16062 the lower bound, upper bound or count, in that order, if any of these
16063 three attributes references an object that has a type.
16064 If no base type is found, the Dwarf-2 specifications say that
16065 a signed integer type of size equal to the size of an address should
16066 be used.
16067 For the following C code: `extern char gdb_int [];'
16068 GCC produces an empty range DIE.
16069 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 16070 high bound or count are not yet handled by this code. */
e77813c8
PM
16071 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16072 {
16073 struct objfile *objfile = cu->objfile;
16074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16075 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16076 struct type *int_type = objfile_type (objfile)->builtin_int;
16077
16078 /* Test "int", "long int", and "long long int" objfile types,
16079 and select the first one having a size above or equal to the
16080 architecture address size. */
16081 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16082 base_type = int_type;
16083 else
16084 {
16085 int_type = objfile_type (objfile)->builtin_long;
16086 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16087 base_type = int_type;
16088 else
16089 {
16090 int_type = objfile_type (objfile)->builtin_long_long;
16091 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16092 base_type = int_type;
16093 }
16094 }
16095 }
a02abb62 16096
dbb9c2b1
JB
16097 /* Normally, the DWARF producers are expected to use a signed
16098 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16099 But this is unfortunately not always the case, as witnessed
16100 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16101 is used instead. To work around that ambiguity, we treat
16102 the bounds as signed, and thus sign-extend their values, when
16103 the base type is signed. */
6e70227d 16104 negative_mask =
66c6502d 16105 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
16106 if (low.kind == PROP_CONST
16107 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16108 low.data.const_val |= negative_mask;
16109 if (high.kind == PROP_CONST
16110 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16111 high.data.const_val |= negative_mask;
43bbcdc2 16112
729efb13 16113 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 16114
c451ebe5
SA
16115 if (high_bound_is_count)
16116 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16117
c2ff108b
JK
16118 /* Ada expects an empty array on no boundary attributes. */
16119 if (attr == NULL && cu->language != language_ada)
729efb13 16120 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 16121
39cbfefa
DJ
16122 name = dwarf2_name (die, cu);
16123 if (name)
16124 TYPE_NAME (range_type) = name;
6e70227d 16125
e142c38c 16126 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
16127 if (attr)
16128 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16129
7e314c57
JK
16130 set_die_type (die, range_type, cu);
16131
16132 /* set_die_type should be already done. */
b4ba55a1
JB
16133 set_descriptive_type (range_type, die, cu);
16134
7e314c57 16135 return range_type;
a02abb62 16136}
6e70227d 16137
f792889a 16138static struct type *
81a17f79
JB
16139read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16140{
16141 struct type *type;
81a17f79 16142
81a17f79
JB
16143 /* For now, we only support the C meaning of an unspecified type: void. */
16144
19f392bc 16145 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 16146 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 16147
f792889a 16148 return set_die_type (die, type, cu);
81a17f79 16149}
a02abb62 16150
639d11d3
DC
16151/* Read a single die and all its descendents. Set the die's sibling
16152 field to NULL; set other fields in the die correctly, and set all
16153 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16154 location of the info_ptr after reading all of those dies. PARENT
16155 is the parent of the die in question. */
16156
16157static struct die_info *
dee91e82 16158read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
16159 const gdb_byte *info_ptr,
16160 const gdb_byte **new_info_ptr,
dee91e82 16161 struct die_info *parent)
639d11d3
DC
16162{
16163 struct die_info *die;
d521ce57 16164 const gdb_byte *cur_ptr;
639d11d3
DC
16165 int has_children;
16166
bf6af496 16167 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
16168 if (die == NULL)
16169 {
16170 *new_info_ptr = cur_ptr;
16171 return NULL;
16172 }
93311388 16173 store_in_ref_table (die, reader->cu);
639d11d3
DC
16174
16175 if (has_children)
bf6af496 16176 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
16177 else
16178 {
16179 die->child = NULL;
16180 *new_info_ptr = cur_ptr;
16181 }
16182
16183 die->sibling = NULL;
16184 die->parent = parent;
16185 return die;
16186}
16187
16188/* Read a die, all of its descendents, and all of its siblings; set
16189 all of the fields of all of the dies correctly. Arguments are as
16190 in read_die_and_children. */
16191
16192static struct die_info *
bf6af496 16193read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
16194 const gdb_byte *info_ptr,
16195 const gdb_byte **new_info_ptr,
bf6af496 16196 struct die_info *parent)
639d11d3
DC
16197{
16198 struct die_info *first_die, *last_sibling;
d521ce57 16199 const gdb_byte *cur_ptr;
639d11d3 16200
c906108c 16201 cur_ptr = info_ptr;
639d11d3
DC
16202 first_die = last_sibling = NULL;
16203
16204 while (1)
c906108c 16205 {
639d11d3 16206 struct die_info *die
dee91e82 16207 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 16208
1d325ec1 16209 if (die == NULL)
c906108c 16210 {
639d11d3
DC
16211 *new_info_ptr = cur_ptr;
16212 return first_die;
c906108c 16213 }
1d325ec1
DJ
16214
16215 if (!first_die)
16216 first_die = die;
c906108c 16217 else
1d325ec1
DJ
16218 last_sibling->sibling = die;
16219
16220 last_sibling = die;
c906108c 16221 }
c906108c
SS
16222}
16223
bf6af496
DE
16224/* Read a die, all of its descendents, and all of its siblings; set
16225 all of the fields of all of the dies correctly. Arguments are as
16226 in read_die_and_children.
16227 This the main entry point for reading a DIE and all its children. */
16228
16229static struct die_info *
16230read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
16231 const gdb_byte *info_ptr,
16232 const gdb_byte **new_info_ptr,
bf6af496
DE
16233 struct die_info *parent)
16234{
16235 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16236 new_info_ptr, parent);
16237
b4f54984 16238 if (dwarf_die_debug)
bf6af496
DE
16239 {
16240 fprintf_unfiltered (gdb_stdlog,
16241 "Read die from %s@0x%x of %s:\n",
a32a8923 16242 get_section_name (reader->die_section),
bf6af496
DE
16243 (unsigned) (info_ptr - reader->die_section->buffer),
16244 bfd_get_filename (reader->abfd));
b4f54984 16245 dump_die (die, dwarf_die_debug);
bf6af496
DE
16246 }
16247
16248 return die;
16249}
16250
3019eac3
DE
16251/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16252 attributes.
16253 The caller is responsible for filling in the extra attributes
16254 and updating (*DIEP)->num_attrs.
16255 Set DIEP to point to a newly allocated die with its information,
16256 except for its child, sibling, and parent fields.
16257 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 16258
d521ce57 16259static const gdb_byte *
3019eac3 16260read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 16261 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 16262 int *has_children, int num_extra_attrs)
93311388 16263{
b64f50a1 16264 unsigned int abbrev_number, bytes_read, i;
93311388
DE
16265 struct abbrev_info *abbrev;
16266 struct die_info *die;
16267 struct dwarf2_cu *cu = reader->cu;
16268 bfd *abfd = reader->abfd;
16269
9c541725 16270 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
16271 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16272 info_ptr += bytes_read;
16273 if (!abbrev_number)
16274 {
16275 *diep = NULL;
16276 *has_children = 0;
16277 return info_ptr;
16278 }
16279
433df2d4 16280 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 16281 if (!abbrev)
348e048f
DE
16282 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16283 abbrev_number,
16284 bfd_get_filename (abfd));
16285
3019eac3 16286 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 16287 die->sect_off = sect_off;
93311388
DE
16288 die->tag = abbrev->tag;
16289 die->abbrev = abbrev_number;
16290
3019eac3
DE
16291 /* Make the result usable.
16292 The caller needs to update num_attrs after adding the extra
16293 attributes. */
93311388
DE
16294 die->num_attrs = abbrev->num_attrs;
16295
16296 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
16297 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16298 info_ptr);
93311388
DE
16299
16300 *diep = die;
16301 *has_children = abbrev->has_children;
16302 return info_ptr;
16303}
16304
3019eac3
DE
16305/* Read a die and all its attributes.
16306 Set DIEP to point to a newly allocated die with its information,
16307 except for its child, sibling, and parent fields.
16308 Set HAS_CHILDREN to tell whether the die has children or not. */
16309
d521ce57 16310static const gdb_byte *
3019eac3 16311read_full_die (const struct die_reader_specs *reader,
d521ce57 16312 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
16313 int *has_children)
16314{
d521ce57 16315 const gdb_byte *result;
bf6af496
DE
16316
16317 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16318
b4f54984 16319 if (dwarf_die_debug)
bf6af496
DE
16320 {
16321 fprintf_unfiltered (gdb_stdlog,
16322 "Read die from %s@0x%x of %s:\n",
a32a8923 16323 get_section_name (reader->die_section),
bf6af496
DE
16324 (unsigned) (info_ptr - reader->die_section->buffer),
16325 bfd_get_filename (reader->abfd));
b4f54984 16326 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
16327 }
16328
16329 return result;
3019eac3 16330}
433df2d4
DE
16331\f
16332/* Abbreviation tables.
3019eac3 16333
433df2d4 16334 In DWARF version 2, the description of the debugging information is
c906108c
SS
16335 stored in a separate .debug_abbrev section. Before we read any
16336 dies from a section we read in all abbreviations and install them
433df2d4
DE
16337 in a hash table. */
16338
16339/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16340
16341static struct abbrev_info *
16342abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16343{
16344 struct abbrev_info *abbrev;
16345
8d749320 16346 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 16347 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 16348
433df2d4
DE
16349 return abbrev;
16350}
16351
16352/* Add an abbreviation to the table. */
c906108c
SS
16353
16354static void
433df2d4
DE
16355abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16356 unsigned int abbrev_number,
16357 struct abbrev_info *abbrev)
16358{
16359 unsigned int hash_number;
16360
16361 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16362 abbrev->next = abbrev_table->abbrevs[hash_number];
16363 abbrev_table->abbrevs[hash_number] = abbrev;
16364}
dee91e82 16365
433df2d4
DE
16366/* Look up an abbrev in the table.
16367 Returns NULL if the abbrev is not found. */
16368
16369static struct abbrev_info *
16370abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16371 unsigned int abbrev_number)
c906108c 16372{
433df2d4
DE
16373 unsigned int hash_number;
16374 struct abbrev_info *abbrev;
16375
16376 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16377 abbrev = abbrev_table->abbrevs[hash_number];
16378
16379 while (abbrev)
16380 {
16381 if (abbrev->number == abbrev_number)
16382 return abbrev;
16383 abbrev = abbrev->next;
16384 }
16385 return NULL;
16386}
16387
16388/* Read in an abbrev table. */
16389
16390static struct abbrev_table *
16391abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 16392 sect_offset sect_off)
433df2d4
DE
16393{
16394 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 16395 bfd *abfd = get_section_bfd_owner (section);
433df2d4 16396 struct abbrev_table *abbrev_table;
d521ce57 16397 const gdb_byte *abbrev_ptr;
c906108c
SS
16398 struct abbrev_info *cur_abbrev;
16399 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 16400 unsigned int abbrev_form;
f3dd6933
DJ
16401 struct attr_abbrev *cur_attrs;
16402 unsigned int allocated_attrs;
c906108c 16403
70ba0933 16404 abbrev_table = XNEW (struct abbrev_table);
9c541725 16405 abbrev_table->sect_off = sect_off;
433df2d4 16406 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
16407 abbrev_table->abbrevs =
16408 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16409 ABBREV_HASH_SIZE);
433df2d4
DE
16410 memset (abbrev_table->abbrevs, 0,
16411 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 16412
433df2d4 16413 dwarf2_read_section (objfile, section);
9c541725 16414 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
16415 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16416 abbrev_ptr += bytes_read;
16417
f3dd6933 16418 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 16419 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 16420
0963b4bd 16421 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
16422 while (abbrev_number)
16423 {
433df2d4 16424 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
16425
16426 /* read in abbrev header */
16427 cur_abbrev->number = abbrev_number;
aead7601
SM
16428 cur_abbrev->tag
16429 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
16430 abbrev_ptr += bytes_read;
16431 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16432 abbrev_ptr += 1;
16433
16434 /* now read in declarations */
22d2f3ab 16435 for (;;)
c906108c 16436 {
43988095
JK
16437 LONGEST implicit_const;
16438
22d2f3ab
JK
16439 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16440 abbrev_ptr += bytes_read;
16441 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16442 abbrev_ptr += bytes_read;
43988095
JK
16443 if (abbrev_form == DW_FORM_implicit_const)
16444 {
16445 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16446 &bytes_read);
16447 abbrev_ptr += bytes_read;
16448 }
16449 else
16450 {
16451 /* Initialize it due to a false compiler warning. */
16452 implicit_const = -1;
16453 }
22d2f3ab
JK
16454
16455 if (abbrev_name == 0)
16456 break;
16457
f3dd6933 16458 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 16459 {
f3dd6933
DJ
16460 allocated_attrs += ATTR_ALLOC_CHUNK;
16461 cur_attrs
224c3ddb 16462 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 16463 }
ae038cb0 16464
aead7601
SM
16465 cur_attrs[cur_abbrev->num_attrs].name
16466 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 16467 cur_attrs[cur_abbrev->num_attrs].form
aead7601 16468 = (enum dwarf_form) abbrev_form;
43988095 16469 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 16470 ++cur_abbrev->num_attrs;
c906108c
SS
16471 }
16472
8d749320
SM
16473 cur_abbrev->attrs =
16474 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16475 cur_abbrev->num_attrs);
f3dd6933
DJ
16476 memcpy (cur_abbrev->attrs, cur_attrs,
16477 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16478
433df2d4 16479 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
16480
16481 /* Get next abbreviation.
16482 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
16483 always properly terminated with an abbrev number of 0.
16484 Exit loop if we encounter an abbreviation which we have
16485 already read (which means we are about to read the abbreviations
16486 for the next compile unit) or if the end of the abbreviation
16487 table is reached. */
433df2d4 16488 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
16489 break;
16490 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16491 abbrev_ptr += bytes_read;
433df2d4 16492 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
16493 break;
16494 }
f3dd6933
DJ
16495
16496 xfree (cur_attrs);
433df2d4 16497 return abbrev_table;
c906108c
SS
16498}
16499
433df2d4 16500/* Free the resources held by ABBREV_TABLE. */
c906108c 16501
c906108c 16502static void
433df2d4 16503abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16504{
433df2d4
DE
16505 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16506 xfree (abbrev_table);
c906108c
SS
16507}
16508
f4dc4d17
DE
16509/* Same as abbrev_table_free but as a cleanup.
16510 We pass in a pointer to the pointer to the table so that we can
16511 set the pointer to NULL when we're done. It also simplifies
73051182 16512 build_type_psymtabs_1. */
f4dc4d17
DE
16513
16514static void
16515abbrev_table_free_cleanup (void *table_ptr)
16516{
9a3c8263 16517 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16518
16519 if (*abbrev_table_ptr != NULL)
16520 abbrev_table_free (*abbrev_table_ptr);
16521 *abbrev_table_ptr = NULL;
16522}
16523
433df2d4
DE
16524/* Read the abbrev table for CU from ABBREV_SECTION. */
16525
16526static void
16527dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16528 struct dwarf2_section_info *abbrev_section)
c906108c 16529{
433df2d4 16530 cu->abbrev_table =
9c541725 16531 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16532}
c906108c 16533
433df2d4 16534/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16535
433df2d4
DE
16536static void
16537dwarf2_free_abbrev_table (void *ptr_to_cu)
16538{
9a3c8263 16539 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16540
a2ce51a0
DE
16541 if (cu->abbrev_table != NULL)
16542 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16543 /* Set this to NULL so that we SEGV if we try to read it later,
16544 and also because free_comp_unit verifies this is NULL. */
16545 cu->abbrev_table = NULL;
16546}
16547\f
72bf9492
DJ
16548/* Returns nonzero if TAG represents a type that we might generate a partial
16549 symbol for. */
16550
16551static int
16552is_type_tag_for_partial (int tag)
16553{
16554 switch (tag)
16555 {
16556#if 0
16557 /* Some types that would be reasonable to generate partial symbols for,
16558 that we don't at present. */
16559 case DW_TAG_array_type:
16560 case DW_TAG_file_type:
16561 case DW_TAG_ptr_to_member_type:
16562 case DW_TAG_set_type:
16563 case DW_TAG_string_type:
16564 case DW_TAG_subroutine_type:
16565#endif
16566 case DW_TAG_base_type:
16567 case DW_TAG_class_type:
680b30c7 16568 case DW_TAG_interface_type:
72bf9492
DJ
16569 case DW_TAG_enumeration_type:
16570 case DW_TAG_structure_type:
16571 case DW_TAG_subrange_type:
16572 case DW_TAG_typedef:
16573 case DW_TAG_union_type:
16574 return 1;
16575 default:
16576 return 0;
16577 }
16578}
16579
16580/* Load all DIEs that are interesting for partial symbols into memory. */
16581
16582static struct partial_die_info *
dee91e82 16583load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16584 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16585{
dee91e82 16586 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16587 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16588 struct partial_die_info *part_die;
16589 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16590 struct abbrev_info *abbrev;
16591 unsigned int bytes_read;
5afb4e99 16592 unsigned int load_all = 0;
72bf9492
DJ
16593 int nesting_level = 1;
16594
16595 parent_die = NULL;
16596 last_die = NULL;
16597
7adf1e79
DE
16598 gdb_assert (cu->per_cu != NULL);
16599 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16600 load_all = 1;
16601
72bf9492
DJ
16602 cu->partial_dies
16603 = htab_create_alloc_ex (cu->header.length / 12,
16604 partial_die_hash,
16605 partial_die_eq,
16606 NULL,
16607 &cu->comp_unit_obstack,
16608 hashtab_obstack_allocate,
16609 dummy_obstack_deallocate);
16610
8d749320 16611 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16612
16613 while (1)
16614 {
16615 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16616
16617 /* A NULL abbrev means the end of a series of children. */
16618 if (abbrev == NULL)
16619 {
16620 if (--nesting_level == 0)
16621 {
16622 /* PART_DIE was probably the last thing allocated on the
16623 comp_unit_obstack, so we could call obstack_free
16624 here. We don't do that because the waste is small,
16625 and will be cleaned up when we're done with this
16626 compilation unit. This way, we're also more robust
16627 against other users of the comp_unit_obstack. */
16628 return first_die;
16629 }
16630 info_ptr += bytes_read;
16631 last_die = parent_die;
16632 parent_die = parent_die->die_parent;
16633 continue;
16634 }
16635
98bfdba5
PA
16636 /* Check for template arguments. We never save these; if
16637 they're seen, we just mark the parent, and go on our way. */
16638 if (parent_die != NULL
16639 && cu->language == language_cplus
16640 && (abbrev->tag == DW_TAG_template_type_param
16641 || abbrev->tag == DW_TAG_template_value_param))
16642 {
16643 parent_die->has_template_arguments = 1;
16644
16645 if (!load_all)
16646 {
16647 /* We don't need a partial DIE for the template argument. */
dee91e82 16648 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16649 continue;
16650 }
16651 }
16652
0d99eb77 16653 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16654 Skip their other children. */
16655 if (!load_all
16656 && cu->language == language_cplus
16657 && parent_die != NULL
16658 && parent_die->tag == DW_TAG_subprogram)
16659 {
dee91e82 16660 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16661 continue;
16662 }
16663
5afb4e99
DJ
16664 /* Check whether this DIE is interesting enough to save. Normally
16665 we would not be interested in members here, but there may be
16666 later variables referencing them via DW_AT_specification (for
16667 static members). */
16668 if (!load_all
16669 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16670 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16671 && abbrev->tag != DW_TAG_enumerator
16672 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16673 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16674 && abbrev->tag != DW_TAG_variable
5afb4e99 16675 && abbrev->tag != DW_TAG_namespace
f55ee35c 16676 && abbrev->tag != DW_TAG_module
95554aad 16677 && abbrev->tag != DW_TAG_member
74921315
KS
16678 && abbrev->tag != DW_TAG_imported_unit
16679 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16680 {
16681 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16682 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16683 continue;
16684 }
16685
dee91e82
DE
16686 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16687 info_ptr);
72bf9492
DJ
16688
16689 /* This two-pass algorithm for processing partial symbols has a
16690 high cost in cache pressure. Thus, handle some simple cases
16691 here which cover the majority of C partial symbols. DIEs
16692 which neither have specification tags in them, nor could have
16693 specification tags elsewhere pointing at them, can simply be
16694 processed and discarded.
16695
16696 This segment is also optional; scan_partial_symbols and
16697 add_partial_symbol will handle these DIEs if we chain
16698 them in normally. When compilers which do not emit large
16699 quantities of duplicate debug information are more common,
16700 this code can probably be removed. */
16701
16702 /* Any complete simple types at the top level (pretty much all
16703 of them, for a language without namespaces), can be processed
16704 directly. */
16705 if (parent_die == NULL
16706 && part_die->has_specification == 0
16707 && part_die->is_declaration == 0
d8228535 16708 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16709 || part_die->tag == DW_TAG_base_type
16710 || part_die->tag == DW_TAG_subrange_type))
16711 {
16712 if (building_psymtab && part_die->name != NULL)
04a679b8 16713 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16714 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16715 &objfile->static_psymbols,
1762568f 16716 0, cu->language, objfile);
dee91e82 16717 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16718 continue;
16719 }
16720
d8228535
JK
16721 /* The exception for DW_TAG_typedef with has_children above is
16722 a workaround of GCC PR debug/47510. In the case of this complaint
16723 type_name_no_tag_or_error will error on such types later.
16724
16725 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16726 it could not find the child DIEs referenced later, this is checked
16727 above. In correct DWARF DW_TAG_typedef should have no children. */
16728
16729 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16730 complaint (&symfile_complaints,
16731 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16732 "- DIE at 0x%x [in module %s]"),
9c541725 16733 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16734
72bf9492
DJ
16735 /* If we're at the second level, and we're an enumerator, and
16736 our parent has no specification (meaning possibly lives in a
16737 namespace elsewhere), then we can add the partial symbol now
16738 instead of queueing it. */
16739 if (part_die->tag == DW_TAG_enumerator
16740 && parent_die != NULL
16741 && parent_die->die_parent == NULL
16742 && parent_die->tag == DW_TAG_enumeration_type
16743 && parent_die->has_specification == 0)
16744 {
16745 if (part_die->name == NULL)
3e43a32a
MS
16746 complaint (&symfile_complaints,
16747 _("malformed enumerator DIE ignored"));
72bf9492 16748 else if (building_psymtab)
04a679b8 16749 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16750 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16751 cu->language == language_cplus
bb5ed363
DE
16752 ? &objfile->global_psymbols
16753 : &objfile->static_psymbols,
1762568f 16754 0, cu->language, objfile);
72bf9492 16755
dee91e82 16756 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16757 continue;
16758 }
16759
16760 /* We'll save this DIE so link it in. */
16761 part_die->die_parent = parent_die;
16762 part_die->die_sibling = NULL;
16763 part_die->die_child = NULL;
16764
16765 if (last_die && last_die == parent_die)
16766 last_die->die_child = part_die;
16767 else if (last_die)
16768 last_die->die_sibling = part_die;
16769
16770 last_die = part_die;
16771
16772 if (first_die == NULL)
16773 first_die = part_die;
16774
16775 /* Maybe add the DIE to the hash table. Not all DIEs that we
16776 find interesting need to be in the hash table, because we
16777 also have the parent/sibling/child chains; only those that we
16778 might refer to by offset later during partial symbol reading.
16779
16780 For now this means things that might have be the target of a
16781 DW_AT_specification, DW_AT_abstract_origin, or
16782 DW_AT_extension. DW_AT_extension will refer only to
16783 namespaces; DW_AT_abstract_origin refers to functions (and
16784 many things under the function DIE, but we do not recurse
16785 into function DIEs during partial symbol reading) and
16786 possibly variables as well; DW_AT_specification refers to
16787 declarations. Declarations ought to have the DW_AT_declaration
16788 flag. It happens that GCC forgets to put it in sometimes, but
16789 only for functions, not for types.
16790
16791 Adding more things than necessary to the hash table is harmless
16792 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16793 wasted time in find_partial_die, when we reread the compilation
16794 unit with load_all_dies set. */
72bf9492 16795
5afb4e99 16796 if (load_all
72929c62 16797 || abbrev->tag == DW_TAG_constant
5afb4e99 16798 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16799 || abbrev->tag == DW_TAG_variable
16800 || abbrev->tag == DW_TAG_namespace
16801 || part_die->is_declaration)
16802 {
16803 void **slot;
16804
16805 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16806 to_underlying (part_die->sect_off),
16807 INSERT);
72bf9492
DJ
16808 *slot = part_die;
16809 }
16810
8d749320 16811 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16812
16813 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16814 we have no reason to follow the children of structures; for other
98bfdba5
PA
16815 languages we have to, so that we can get at method physnames
16816 to infer fully qualified class names, for DW_AT_specification,
16817 and for C++ template arguments. For C++, we also look one level
16818 inside functions to find template arguments (if the name of the
16819 function does not already contain the template arguments).
bc30ff58
JB
16820
16821 For Ada, we need to scan the children of subprograms and lexical
16822 blocks as well because Ada allows the definition of nested
16823 entities that could be interesting for the debugger, such as
16824 nested subprograms for instance. */
72bf9492 16825 if (last_die->has_children
5afb4e99
DJ
16826 && (load_all
16827 || last_die->tag == DW_TAG_namespace
f55ee35c 16828 || last_die->tag == DW_TAG_module
72bf9492 16829 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16830 || (cu->language == language_cplus
16831 && last_die->tag == DW_TAG_subprogram
16832 && (last_die->name == NULL
16833 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16834 || (cu->language != language_c
16835 && (last_die->tag == DW_TAG_class_type
680b30c7 16836 || last_die->tag == DW_TAG_interface_type
72bf9492 16837 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16838 || last_die->tag == DW_TAG_union_type))
16839 || (cu->language == language_ada
16840 && (last_die->tag == DW_TAG_subprogram
16841 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16842 {
16843 nesting_level++;
16844 parent_die = last_die;
16845 continue;
16846 }
16847
16848 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16849 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16850
16851 /* Back to the top, do it again. */
16852 }
16853}
16854
c906108c
SS
16855/* Read a minimal amount of information into the minimal die structure. */
16856
d521ce57 16857static const gdb_byte *
dee91e82
DE
16858read_partial_die (const struct die_reader_specs *reader,
16859 struct partial_die_info *part_die,
16860 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16861 const gdb_byte *info_ptr)
c906108c 16862{
dee91e82 16863 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16864 struct objfile *objfile = cu->objfile;
d521ce57 16865 const gdb_byte *buffer = reader->buffer;
fa238c03 16866 unsigned int i;
c906108c 16867 struct attribute attr;
c5aa993b 16868 int has_low_pc_attr = 0;
c906108c 16869 int has_high_pc_attr = 0;
91da1414 16870 int high_pc_relative = 0;
c906108c 16871
72bf9492 16872 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16873
9c541725 16874 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16875
16876 info_ptr += abbrev_len;
16877
16878 if (abbrev == NULL)
16879 return info_ptr;
16880
c906108c
SS
16881 part_die->tag = abbrev->tag;
16882 part_die->has_children = abbrev->has_children;
c906108c
SS
16883
16884 for (i = 0; i < abbrev->num_attrs; ++i)
16885 {
dee91e82 16886 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16887
16888 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16889 partial symbol table. */
c906108c
SS
16890 switch (attr.name)
16891 {
16892 case DW_AT_name:
71c25dea
TT
16893 switch (part_die->tag)
16894 {
16895 case DW_TAG_compile_unit:
95554aad 16896 case DW_TAG_partial_unit:
348e048f 16897 case DW_TAG_type_unit:
71c25dea
TT
16898 /* Compilation units have a DW_AT_name that is a filename, not
16899 a source language identifier. */
16900 case DW_TAG_enumeration_type:
16901 case DW_TAG_enumerator:
16902 /* These tags always have simple identifiers already; no need
16903 to canonicalize them. */
16904 part_die->name = DW_STRING (&attr);
16905 break;
16906 default:
16907 part_die->name
16908 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16909 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16910 break;
16911 }
c906108c 16912 break;
31ef98ae 16913 case DW_AT_linkage_name:
c906108c 16914 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16915 /* Note that both forms of linkage name might appear. We
16916 assume they will be the same, and we only store the last
16917 one we see. */
94af9270
KS
16918 if (cu->language == language_ada)
16919 part_die->name = DW_STRING (&attr);
abc72ce4 16920 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16921 break;
16922 case DW_AT_low_pc:
16923 has_low_pc_attr = 1;
31aa7e4e 16924 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16925 break;
16926 case DW_AT_high_pc:
16927 has_high_pc_attr = 1;
31aa7e4e
JB
16928 part_die->highpc = attr_value_as_address (&attr);
16929 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16930 high_pc_relative = 1;
c906108c
SS
16931 break;
16932 case DW_AT_location:
0963b4bd 16933 /* Support the .debug_loc offsets. */
8e19ed76
PS
16934 if (attr_form_is_block (&attr))
16935 {
95554aad 16936 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16937 }
3690dd37 16938 else if (attr_form_is_section_offset (&attr))
8e19ed76 16939 {
4d3c2250 16940 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16941 }
16942 else
16943 {
4d3c2250
KB
16944 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16945 "partial symbol information");
8e19ed76 16946 }
c906108c 16947 break;
c906108c
SS
16948 case DW_AT_external:
16949 part_die->is_external = DW_UNSND (&attr);
16950 break;
16951 case DW_AT_declaration:
16952 part_die->is_declaration = DW_UNSND (&attr);
16953 break;
16954 case DW_AT_type:
16955 part_die->has_type = 1;
16956 break;
16957 case DW_AT_abstract_origin:
16958 case DW_AT_specification:
72bf9492
DJ
16959 case DW_AT_extension:
16960 part_die->has_specification = 1;
c764a876 16961 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16962 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16963 || cu->per_cu->is_dwz);
c906108c
SS
16964 break;
16965 case DW_AT_sibling:
16966 /* Ignore absolute siblings, they might point outside of
16967 the current compile unit. */
16968 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16969 complaint (&symfile_complaints,
16970 _("ignoring absolute DW_AT_sibling"));
c906108c 16971 else
b9502d3f 16972 {
9c541725
PA
16973 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16974 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16975
16976 if (sibling_ptr < info_ptr)
16977 complaint (&symfile_complaints,
16978 _("DW_AT_sibling points backwards"));
22869d73
KS
16979 else if (sibling_ptr > reader->buffer_end)
16980 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16981 else
16982 part_die->sibling = sibling_ptr;
16983 }
c906108c 16984 break;
fa4028e9
JB
16985 case DW_AT_byte_size:
16986 part_die->has_byte_size = 1;
16987 break;
ff908ebf
AW
16988 case DW_AT_const_value:
16989 part_die->has_const_value = 1;
16990 break;
68511cec
CES
16991 case DW_AT_calling_convention:
16992 /* DWARF doesn't provide a way to identify a program's source-level
16993 entry point. DW_AT_calling_convention attributes are only meant
16994 to describe functions' calling conventions.
16995
16996 However, because it's a necessary piece of information in
0c1b455e
TT
16997 Fortran, and before DWARF 4 DW_CC_program was the only
16998 piece of debugging information whose definition refers to
16999 a 'main program' at all, several compilers marked Fortran
17000 main programs with DW_CC_program --- even when those
17001 functions use the standard calling conventions.
17002
17003 Although DWARF now specifies a way to provide this
17004 information, we support this practice for backward
17005 compatibility. */
68511cec 17006 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
17007 && cu->language == language_fortran)
17008 part_die->main_subprogram = 1;
68511cec 17009 break;
481860b3
GB
17010 case DW_AT_inline:
17011 if (DW_UNSND (&attr) == DW_INL_inlined
17012 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17013 part_die->may_be_inlined = 1;
17014 break;
95554aad
TT
17015
17016 case DW_AT_import:
17017 if (part_die->tag == DW_TAG_imported_unit)
36586728 17018 {
9c541725 17019 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17020 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17021 || cu->per_cu->is_dwz);
17022 }
95554aad
TT
17023 break;
17024
0c1b455e
TT
17025 case DW_AT_main_subprogram:
17026 part_die->main_subprogram = DW_UNSND (&attr);
17027 break;
17028
c906108c
SS
17029 default:
17030 break;
17031 }
17032 }
17033
91da1414
MW
17034 if (high_pc_relative)
17035 part_die->highpc += part_die->lowpc;
17036
9373cf26
JK
17037 if (has_low_pc_attr && has_high_pc_attr)
17038 {
17039 /* When using the GNU linker, .gnu.linkonce. sections are used to
17040 eliminate duplicate copies of functions and vtables and such.
17041 The linker will arbitrarily choose one and discard the others.
17042 The AT_*_pc values for such functions refer to local labels in
17043 these sections. If the section from that file was discarded, the
17044 labels are not in the output, so the relocs get a value of 0.
17045 If this is a discarded function, mark the pc bounds as invalid,
17046 so that GDB will ignore it. */
17047 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17048 {
bb5ed363 17049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17050
17051 complaint (&symfile_complaints,
17052 _("DW_AT_low_pc %s is zero "
17053 "for DIE at 0x%x [in module %s]"),
17054 paddress (gdbarch, part_die->lowpc),
9c541725 17055 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
17056 }
17057 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17058 else if (part_die->lowpc >= part_die->highpc)
17059 {
bb5ed363 17060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17061
17062 complaint (&symfile_complaints,
17063 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17064 "for DIE at 0x%x [in module %s]"),
17065 paddress (gdbarch, part_die->lowpc),
17066 paddress (gdbarch, part_die->highpc),
9c541725
PA
17067 to_underlying (part_die->sect_off),
17068 objfile_name (objfile));
9373cf26
JK
17069 }
17070 else
17071 part_die->has_pc_info = 1;
17072 }
85cbf3d3 17073
c906108c
SS
17074 return info_ptr;
17075}
17076
72bf9492
DJ
17077/* Find a cached partial DIE at OFFSET in CU. */
17078
17079static struct partial_die_info *
9c541725 17080find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
17081{
17082 struct partial_die_info *lookup_die = NULL;
17083 struct partial_die_info part_die;
17084
9c541725 17085 part_die.sect_off = sect_off;
9a3c8263
SM
17086 lookup_die = ((struct partial_die_info *)
17087 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 17088 to_underlying (sect_off)));
72bf9492 17089
72bf9492
DJ
17090 return lookup_die;
17091}
17092
348e048f
DE
17093/* Find a partial DIE at OFFSET, which may or may not be in CU,
17094 except in the case of .debug_types DIEs which do not reference
17095 outside their CU (they do however referencing other types via
55f1336d 17096 DW_FORM_ref_sig8). */
72bf9492
DJ
17097
17098static struct partial_die_info *
9c541725 17099find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 17100{
bb5ed363 17101 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
17102 struct dwarf2_per_cu_data *per_cu = NULL;
17103 struct partial_die_info *pd = NULL;
72bf9492 17104
36586728 17105 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 17106 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 17107 {
9c541725 17108 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
17109 if (pd != NULL)
17110 return pd;
0d99eb77
DE
17111 /* We missed recording what we needed.
17112 Load all dies and try again. */
17113 per_cu = cu->per_cu;
5afb4e99 17114 }
0d99eb77
DE
17115 else
17116 {
17117 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 17118 if (cu->per_cu->is_debug_types)
0d99eb77 17119 {
9c541725
PA
17120 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17121 " external reference to offset 0x%x [in module %s].\n"),
17122 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
17123 bfd_get_filename (objfile->obfd));
17124 }
9c541725 17125 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 17126 objfile);
72bf9492 17127
0d99eb77
DE
17128 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17129 load_partial_comp_unit (per_cu);
ae038cb0 17130
0d99eb77 17131 per_cu->cu->last_used = 0;
9c541725 17132 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 17133 }
5afb4e99 17134
dee91e82
DE
17135 /* If we didn't find it, and not all dies have been loaded,
17136 load them all and try again. */
17137
5afb4e99
DJ
17138 if (pd == NULL && per_cu->load_all_dies == 0)
17139 {
5afb4e99 17140 per_cu->load_all_dies = 1;
fd820528
DE
17141
17142 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17143 THIS_CU->cu may already be in use. So we can't just free it and
17144 replace its DIEs with the ones we read in. Instead, we leave those
17145 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17146 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17147 set. */
dee91e82 17148 load_partial_comp_unit (per_cu);
5afb4e99 17149
9c541725 17150 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
17151 }
17152
17153 if (pd == NULL)
17154 internal_error (__FILE__, __LINE__,
3e43a32a
MS
17155 _("could not find partial DIE 0x%x "
17156 "in cache [from module %s]\n"),
9c541725 17157 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 17158 return pd;
72bf9492
DJ
17159}
17160
abc72ce4
DE
17161/* See if we can figure out if the class lives in a namespace. We do
17162 this by looking for a member function; its demangled name will
17163 contain namespace info, if there is any. */
17164
17165static void
17166guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17167 struct dwarf2_cu *cu)
17168{
17169 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17170 what template types look like, because the demangler
17171 frequently doesn't give the same name as the debug info. We
17172 could fix this by only using the demangled name to get the
17173 prefix (but see comment in read_structure_type). */
17174
17175 struct partial_die_info *real_pdi;
17176 struct partial_die_info *child_pdi;
17177
17178 /* If this DIE (this DIE's specification, if any) has a parent, then
17179 we should not do this. We'll prepend the parent's fully qualified
17180 name when we create the partial symbol. */
17181
17182 real_pdi = struct_pdi;
17183 while (real_pdi->has_specification)
36586728
TT
17184 real_pdi = find_partial_die (real_pdi->spec_offset,
17185 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
17186
17187 if (real_pdi->die_parent != NULL)
17188 return;
17189
17190 for (child_pdi = struct_pdi->die_child;
17191 child_pdi != NULL;
17192 child_pdi = child_pdi->die_sibling)
17193 {
17194 if (child_pdi->tag == DW_TAG_subprogram
17195 && child_pdi->linkage_name != NULL)
17196 {
17197 char *actual_class_name
17198 = language_class_name_from_physname (cu->language_defn,
17199 child_pdi->linkage_name);
17200 if (actual_class_name != NULL)
17201 {
17202 struct_pdi->name
224c3ddb
SM
17203 = ((const char *)
17204 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17205 actual_class_name,
17206 strlen (actual_class_name)));
abc72ce4
DE
17207 xfree (actual_class_name);
17208 }
17209 break;
17210 }
17211 }
17212}
17213
72bf9492
DJ
17214/* Adjust PART_DIE before generating a symbol for it. This function
17215 may set the is_external flag or change the DIE's name. */
17216
17217static void
17218fixup_partial_die (struct partial_die_info *part_die,
17219 struct dwarf2_cu *cu)
17220{
abc72ce4
DE
17221 /* Once we've fixed up a die, there's no point in doing so again.
17222 This also avoids a memory leak if we were to call
17223 guess_partial_die_structure_name multiple times. */
17224 if (part_die->fixup_called)
17225 return;
17226
72bf9492
DJ
17227 /* If we found a reference attribute and the DIE has no name, try
17228 to find a name in the referred to DIE. */
17229
17230 if (part_die->name == NULL && part_die->has_specification)
17231 {
17232 struct partial_die_info *spec_die;
72bf9492 17233
36586728
TT
17234 spec_die = find_partial_die (part_die->spec_offset,
17235 part_die->spec_is_dwz, cu);
72bf9492 17236
10b3939b 17237 fixup_partial_die (spec_die, cu);
72bf9492
DJ
17238
17239 if (spec_die->name)
17240 {
17241 part_die->name = spec_die->name;
17242
17243 /* Copy DW_AT_external attribute if it is set. */
17244 if (spec_die->is_external)
17245 part_die->is_external = spec_die->is_external;
17246 }
17247 }
17248
17249 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
17250
17251 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 17252 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 17253
abc72ce4
DE
17254 /* If there is no parent die to provide a namespace, and there are
17255 children, see if we can determine the namespace from their linkage
122d1940 17256 name. */
abc72ce4 17257 if (cu->language == language_cplus
8b70b953 17258 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17259 && part_die->die_parent == NULL
17260 && part_die->has_children
17261 && (part_die->tag == DW_TAG_class_type
17262 || part_die->tag == DW_TAG_structure_type
17263 || part_die->tag == DW_TAG_union_type))
17264 guess_partial_die_structure_name (part_die, cu);
17265
53832f31
TT
17266 /* GCC might emit a nameless struct or union that has a linkage
17267 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17268 if (part_die->name == NULL
96408a79
SA
17269 && (part_die->tag == DW_TAG_class_type
17270 || part_die->tag == DW_TAG_interface_type
17271 || part_die->tag == DW_TAG_structure_type
17272 || part_die->tag == DW_TAG_union_type)
53832f31
TT
17273 && part_die->linkage_name != NULL)
17274 {
17275 char *demangled;
17276
8de20a37 17277 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
17278 if (demangled)
17279 {
96408a79
SA
17280 const char *base;
17281
17282 /* Strip any leading namespaces/classes, keep only the base name.
17283 DW_AT_name for named DIEs does not contain the prefixes. */
17284 base = strrchr (demangled, ':');
17285 if (base && base > demangled && base[-1] == ':')
17286 base++;
17287 else
17288 base = demangled;
17289
34a68019 17290 part_die->name
224c3ddb
SM
17291 = ((const char *)
17292 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17293 base, strlen (base)));
53832f31
TT
17294 xfree (demangled);
17295 }
17296 }
17297
abc72ce4 17298 part_die->fixup_called = 1;
72bf9492
DJ
17299}
17300
a8329558 17301/* Read an attribute value described by an attribute form. */
c906108c 17302
d521ce57 17303static const gdb_byte *
dee91e82
DE
17304read_attribute_value (const struct die_reader_specs *reader,
17305 struct attribute *attr, unsigned form,
43988095 17306 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 17307{
dee91e82 17308 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
17309 struct objfile *objfile = cu->objfile;
17310 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 17311 bfd *abfd = reader->abfd;
e7c27a73 17312 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17313 unsigned int bytes_read;
17314 struct dwarf_block *blk;
17315
aead7601 17316 attr->form = (enum dwarf_form) form;
a8329558 17317 switch (form)
c906108c 17318 {
c906108c 17319 case DW_FORM_ref_addr:
ae411497 17320 if (cu->header.version == 2)
4568ecf9 17321 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 17322 else
4568ecf9
DE
17323 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17324 &cu->header, &bytes_read);
ae411497
TT
17325 info_ptr += bytes_read;
17326 break;
36586728
TT
17327 case DW_FORM_GNU_ref_alt:
17328 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17329 info_ptr += bytes_read;
17330 break;
ae411497 17331 case DW_FORM_addr:
e7c27a73 17332 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 17333 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 17334 info_ptr += bytes_read;
c906108c
SS
17335 break;
17336 case DW_FORM_block2:
7b5a2f43 17337 blk = dwarf_alloc_block (cu);
c906108c
SS
17338 blk->size = read_2_bytes (abfd, info_ptr);
17339 info_ptr += 2;
17340 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17341 info_ptr += blk->size;
17342 DW_BLOCK (attr) = blk;
17343 break;
17344 case DW_FORM_block4:
7b5a2f43 17345 blk = dwarf_alloc_block (cu);
c906108c
SS
17346 blk->size = read_4_bytes (abfd, info_ptr);
17347 info_ptr += 4;
17348 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17349 info_ptr += blk->size;
17350 DW_BLOCK (attr) = blk;
17351 break;
17352 case DW_FORM_data2:
17353 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17354 info_ptr += 2;
17355 break;
17356 case DW_FORM_data4:
17357 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17358 info_ptr += 4;
17359 break;
17360 case DW_FORM_data8:
17361 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17362 info_ptr += 8;
17363 break;
0224619f
JK
17364 case DW_FORM_data16:
17365 blk = dwarf_alloc_block (cu);
17366 blk->size = 16;
17367 blk->data = read_n_bytes (abfd, info_ptr, 16);
17368 info_ptr += 16;
17369 DW_BLOCK (attr) = blk;
17370 break;
2dc7f7b3
TT
17371 case DW_FORM_sec_offset:
17372 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17373 info_ptr += bytes_read;
17374 break;
c906108c 17375 case DW_FORM_string:
9b1c24c8 17376 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 17377 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
17378 info_ptr += bytes_read;
17379 break;
4bdf3d34 17380 case DW_FORM_strp:
36586728
TT
17381 if (!cu->per_cu->is_dwz)
17382 {
17383 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17384 &bytes_read);
17385 DW_STRING_IS_CANONICAL (attr) = 0;
17386 info_ptr += bytes_read;
17387 break;
17388 }
17389 /* FALLTHROUGH */
43988095
JK
17390 case DW_FORM_line_strp:
17391 if (!cu->per_cu->is_dwz)
17392 {
17393 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17394 cu_header, &bytes_read);
17395 DW_STRING_IS_CANONICAL (attr) = 0;
17396 info_ptr += bytes_read;
17397 break;
17398 }
17399 /* FALLTHROUGH */
36586728
TT
17400 case DW_FORM_GNU_strp_alt:
17401 {
17402 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17403 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17404 &bytes_read);
17405
17406 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17407 DW_STRING_IS_CANONICAL (attr) = 0;
17408 info_ptr += bytes_read;
17409 }
4bdf3d34 17410 break;
2dc7f7b3 17411 case DW_FORM_exprloc:
c906108c 17412 case DW_FORM_block:
7b5a2f43 17413 blk = dwarf_alloc_block (cu);
c906108c
SS
17414 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17415 info_ptr += bytes_read;
17416 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17417 info_ptr += blk->size;
17418 DW_BLOCK (attr) = blk;
17419 break;
17420 case DW_FORM_block1:
7b5a2f43 17421 blk = dwarf_alloc_block (cu);
c906108c
SS
17422 blk->size = read_1_byte (abfd, info_ptr);
17423 info_ptr += 1;
17424 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17425 info_ptr += blk->size;
17426 DW_BLOCK (attr) = blk;
17427 break;
17428 case DW_FORM_data1:
17429 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17430 info_ptr += 1;
17431 break;
17432 case DW_FORM_flag:
17433 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17434 info_ptr += 1;
17435 break;
2dc7f7b3
TT
17436 case DW_FORM_flag_present:
17437 DW_UNSND (attr) = 1;
17438 break;
c906108c
SS
17439 case DW_FORM_sdata:
17440 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17441 info_ptr += bytes_read;
17442 break;
17443 case DW_FORM_udata:
17444 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17445 info_ptr += bytes_read;
17446 break;
17447 case DW_FORM_ref1:
9c541725 17448 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17449 + read_1_byte (abfd, info_ptr));
c906108c
SS
17450 info_ptr += 1;
17451 break;
17452 case DW_FORM_ref2:
9c541725 17453 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17454 + read_2_bytes (abfd, info_ptr));
c906108c
SS
17455 info_ptr += 2;
17456 break;
17457 case DW_FORM_ref4:
9c541725 17458 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17459 + read_4_bytes (abfd, info_ptr));
c906108c
SS
17460 info_ptr += 4;
17461 break;
613e1657 17462 case DW_FORM_ref8:
9c541725 17463 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17464 + read_8_bytes (abfd, info_ptr));
613e1657
KB
17465 info_ptr += 8;
17466 break;
55f1336d 17467 case DW_FORM_ref_sig8:
ac9ec31b 17468 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
17469 info_ptr += 8;
17470 break;
c906108c 17471 case DW_FORM_ref_udata:
9c541725 17472 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17473 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
17474 info_ptr += bytes_read;
17475 break;
c906108c 17476 case DW_FORM_indirect:
a8329558
KW
17477 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17478 info_ptr += bytes_read;
43988095
JK
17479 if (form == DW_FORM_implicit_const)
17480 {
17481 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17482 info_ptr += bytes_read;
17483 }
17484 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17485 info_ptr);
17486 break;
17487 case DW_FORM_implicit_const:
17488 DW_SND (attr) = implicit_const;
a8329558 17489 break;
3019eac3
DE
17490 case DW_FORM_GNU_addr_index:
17491 if (reader->dwo_file == NULL)
17492 {
17493 /* For now flag a hard error.
17494 Later we can turn this into a complaint. */
17495 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17496 dwarf_form_name (form),
17497 bfd_get_filename (abfd));
17498 }
17499 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17500 info_ptr += bytes_read;
17501 break;
17502 case DW_FORM_GNU_str_index:
17503 if (reader->dwo_file == NULL)
17504 {
17505 /* For now flag a hard error.
17506 Later we can turn this into a complaint if warranted. */
17507 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17508 dwarf_form_name (form),
17509 bfd_get_filename (abfd));
17510 }
17511 {
17512 ULONGEST str_index =
17513 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17514
342587c4 17515 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17516 DW_STRING_IS_CANONICAL (attr) = 0;
17517 info_ptr += bytes_read;
17518 }
17519 break;
c906108c 17520 default:
8a3fe4f8 17521 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17522 dwarf_form_name (form),
17523 bfd_get_filename (abfd));
c906108c 17524 }
28e94949 17525
36586728 17526 /* Super hack. */
7771576e 17527 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17528 attr->form = DW_FORM_GNU_ref_alt;
17529
28e94949
JB
17530 /* We have seen instances where the compiler tried to emit a byte
17531 size attribute of -1 which ended up being encoded as an unsigned
17532 0xffffffff. Although 0xffffffff is technically a valid size value,
17533 an object of this size seems pretty unlikely so we can relatively
17534 safely treat these cases as if the size attribute was invalid and
17535 treat them as zero by default. */
17536 if (attr->name == DW_AT_byte_size
17537 && form == DW_FORM_data4
17538 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17539 {
17540 complaint
17541 (&symfile_complaints,
43bbcdc2
PH
17542 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17543 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17544 DW_UNSND (attr) = 0;
17545 }
28e94949 17546
c906108c
SS
17547 return info_ptr;
17548}
17549
a8329558
KW
17550/* Read an attribute described by an abbreviated attribute. */
17551
d521ce57 17552static const gdb_byte *
dee91e82
DE
17553read_attribute (const struct die_reader_specs *reader,
17554 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17555 const gdb_byte *info_ptr)
a8329558
KW
17556{
17557 attr->name = abbrev->name;
43988095
JK
17558 return read_attribute_value (reader, attr, abbrev->form,
17559 abbrev->implicit_const, info_ptr);
a8329558
KW
17560}
17561
0963b4bd 17562/* Read dwarf information from a buffer. */
c906108c
SS
17563
17564static unsigned int
a1855c1d 17565read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17566{
fe1b8b76 17567 return bfd_get_8 (abfd, buf);
c906108c
SS
17568}
17569
17570static int
a1855c1d 17571read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17572{
fe1b8b76 17573 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17574}
17575
17576static unsigned int
a1855c1d 17577read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17578{
fe1b8b76 17579 return bfd_get_16 (abfd, buf);
c906108c
SS
17580}
17581
21ae7a4d 17582static int
a1855c1d 17583read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17584{
17585 return bfd_get_signed_16 (abfd, buf);
17586}
17587
c906108c 17588static unsigned int
a1855c1d 17589read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17590{
fe1b8b76 17591 return bfd_get_32 (abfd, buf);
c906108c
SS
17592}
17593
21ae7a4d 17594static int
a1855c1d 17595read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17596{
17597 return bfd_get_signed_32 (abfd, buf);
17598}
17599
93311388 17600static ULONGEST
a1855c1d 17601read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17602{
fe1b8b76 17603 return bfd_get_64 (abfd, buf);
c906108c
SS
17604}
17605
17606static CORE_ADDR
d521ce57 17607read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17608 unsigned int *bytes_read)
c906108c 17609{
e7c27a73 17610 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17611 CORE_ADDR retval = 0;
17612
107d2387 17613 if (cu_header->signed_addr_p)
c906108c 17614 {
107d2387
AC
17615 switch (cu_header->addr_size)
17616 {
17617 case 2:
fe1b8b76 17618 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17619 break;
17620 case 4:
fe1b8b76 17621 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17622 break;
17623 case 8:
fe1b8b76 17624 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17625 break;
17626 default:
8e65ff28 17627 internal_error (__FILE__, __LINE__,
e2e0b3e5 17628 _("read_address: bad switch, signed [in module %s]"),
659b0389 17629 bfd_get_filename (abfd));
107d2387
AC
17630 }
17631 }
17632 else
17633 {
17634 switch (cu_header->addr_size)
17635 {
17636 case 2:
fe1b8b76 17637 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17638 break;
17639 case 4:
fe1b8b76 17640 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17641 break;
17642 case 8:
fe1b8b76 17643 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17644 break;
17645 default:
8e65ff28 17646 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17647 _("read_address: bad switch, "
17648 "unsigned [in module %s]"),
659b0389 17649 bfd_get_filename (abfd));
107d2387 17650 }
c906108c 17651 }
64367e0a 17652
107d2387
AC
17653 *bytes_read = cu_header->addr_size;
17654 return retval;
c906108c
SS
17655}
17656
f7ef9339 17657/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17658 specification allows the initial length to take up either 4 bytes
17659 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17660 bytes describe the length and all offsets will be 8 bytes in length
17661 instead of 4.
17662
f7ef9339
KB
17663 An older, non-standard 64-bit format is also handled by this
17664 function. The older format in question stores the initial length
17665 as an 8-byte quantity without an escape value. Lengths greater
17666 than 2^32 aren't very common which means that the initial 4 bytes
17667 is almost always zero. Since a length value of zero doesn't make
17668 sense for the 32-bit format, this initial zero can be considered to
17669 be an escape value which indicates the presence of the older 64-bit
17670 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17671 greater than 4GB. If it becomes necessary to handle lengths
17672 somewhat larger than 4GB, we could allow other small values (such
17673 as the non-sensical values of 1, 2, and 3) to also be used as
17674 escape values indicating the presence of the old format.
f7ef9339 17675
917c78fc
MK
17676 The value returned via bytes_read should be used to increment the
17677 relevant pointer after calling read_initial_length().
c764a876 17678
613e1657
KB
17679 [ Note: read_initial_length() and read_offset() are based on the
17680 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17681 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17682 from:
17683
f7ef9339 17684 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17685
613e1657
KB
17686 This document is only a draft and is subject to change. (So beware.)
17687
f7ef9339 17688 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17689 determined empirically by examining 64-bit ELF files produced by
17690 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17691
17692 - Kevin, July 16, 2002
613e1657
KB
17693 ] */
17694
17695static LONGEST
d521ce57 17696read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17697{
fe1b8b76 17698 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17699
dd373385 17700 if (length == 0xffffffff)
613e1657 17701 {
fe1b8b76 17702 length = bfd_get_64 (abfd, buf + 4);
613e1657 17703 *bytes_read = 12;
613e1657 17704 }
dd373385 17705 else if (length == 0)
f7ef9339 17706 {
dd373385 17707 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17708 length = bfd_get_64 (abfd, buf);
f7ef9339 17709 *bytes_read = 8;
f7ef9339 17710 }
613e1657
KB
17711 else
17712 {
17713 *bytes_read = 4;
613e1657
KB
17714 }
17715
c764a876
DE
17716 return length;
17717}
dd373385 17718
c764a876
DE
17719/* Cover function for read_initial_length.
17720 Returns the length of the object at BUF, and stores the size of the
17721 initial length in *BYTES_READ and stores the size that offsets will be in
17722 *OFFSET_SIZE.
17723 If the initial length size is not equivalent to that specified in
17724 CU_HEADER then issue a complaint.
17725 This is useful when reading non-comp-unit headers. */
dd373385 17726
c764a876 17727static LONGEST
d521ce57 17728read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17729 const struct comp_unit_head *cu_header,
17730 unsigned int *bytes_read,
17731 unsigned int *offset_size)
17732{
17733 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17734
17735 gdb_assert (cu_header->initial_length_size == 4
17736 || cu_header->initial_length_size == 8
17737 || cu_header->initial_length_size == 12);
17738
17739 if (cu_header->initial_length_size != *bytes_read)
17740 complaint (&symfile_complaints,
17741 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17742
c764a876 17743 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17744 return length;
613e1657
KB
17745}
17746
17747/* Read an offset from the data stream. The size of the offset is
917c78fc 17748 given by cu_header->offset_size. */
613e1657
KB
17749
17750static LONGEST
d521ce57
TT
17751read_offset (bfd *abfd, const gdb_byte *buf,
17752 const struct comp_unit_head *cu_header,
891d2f0b 17753 unsigned int *bytes_read)
c764a876
DE
17754{
17755 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17756
c764a876
DE
17757 *bytes_read = cu_header->offset_size;
17758 return offset;
17759}
17760
17761/* Read an offset from the data stream. */
17762
17763static LONGEST
d521ce57 17764read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17765{
17766 LONGEST retval = 0;
17767
c764a876 17768 switch (offset_size)
613e1657
KB
17769 {
17770 case 4:
fe1b8b76 17771 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17772 break;
17773 case 8:
fe1b8b76 17774 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17775 break;
17776 default:
8e65ff28 17777 internal_error (__FILE__, __LINE__,
c764a876 17778 _("read_offset_1: bad switch [in module %s]"),
659b0389 17779 bfd_get_filename (abfd));
613e1657
KB
17780 }
17781
917c78fc 17782 return retval;
613e1657
KB
17783}
17784
d521ce57
TT
17785static const gdb_byte *
17786read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17787{
17788 /* If the size of a host char is 8 bits, we can return a pointer
17789 to the buffer, otherwise we have to copy the data to a buffer
17790 allocated on the temporary obstack. */
4bdf3d34 17791 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17792 return buf;
c906108c
SS
17793}
17794
d521ce57
TT
17795static const char *
17796read_direct_string (bfd *abfd, const gdb_byte *buf,
17797 unsigned int *bytes_read_ptr)
c906108c
SS
17798{
17799 /* If the size of a host char is 8 bits, we can return a pointer
17800 to the string, otherwise we have to copy the string to a buffer
17801 allocated on the temporary obstack. */
4bdf3d34 17802 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17803 if (*buf == '\0')
17804 {
17805 *bytes_read_ptr = 1;
17806 return NULL;
17807 }
d521ce57
TT
17808 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17809 return (const char *) buf;
4bdf3d34
JJ
17810}
17811
43988095
JK
17812/* Return pointer to string at section SECT offset STR_OFFSET with error
17813 reporting strings FORM_NAME and SECT_NAME. */
17814
d521ce57 17815static const char *
43988095
JK
17816read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17817 struct dwarf2_section_info *sect,
17818 const char *form_name,
17819 const char *sect_name)
17820{
17821 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17822 if (sect->buffer == NULL)
17823 error (_("%s used without %s section [in module %s]"),
17824 form_name, sect_name, bfd_get_filename (abfd));
17825 if (str_offset >= sect->size)
17826 error (_("%s pointing outside of %s section [in module %s]"),
17827 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17828 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17829 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17830 return NULL;
43988095
JK
17831 return (const char *) (sect->buffer + str_offset);
17832}
17833
17834/* Return pointer to string at .debug_str offset STR_OFFSET. */
17835
17836static const char *
17837read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17838{
17839 return read_indirect_string_at_offset_from (abfd, str_offset,
17840 &dwarf2_per_objfile->str,
17841 "DW_FORM_strp", ".debug_str");
17842}
17843
17844/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17845
17846static const char *
17847read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17848{
17849 return read_indirect_string_at_offset_from (abfd, str_offset,
17850 &dwarf2_per_objfile->line_str,
17851 "DW_FORM_line_strp",
17852 ".debug_line_str");
c906108c
SS
17853}
17854
36586728
TT
17855/* Read a string at offset STR_OFFSET in the .debug_str section from
17856 the .dwz file DWZ. Throw an error if the offset is too large. If
17857 the string consists of a single NUL byte, return NULL; otherwise
17858 return a pointer to the string. */
17859
d521ce57 17860static const char *
36586728
TT
17861read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17862{
17863 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17864
17865 if (dwz->str.buffer == NULL)
17866 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17867 "section [in module %s]"),
17868 bfd_get_filename (dwz->dwz_bfd));
17869 if (str_offset >= dwz->str.size)
17870 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17871 ".debug_str section [in module %s]"),
17872 bfd_get_filename (dwz->dwz_bfd));
17873 gdb_assert (HOST_CHAR_BIT == 8);
17874 if (dwz->str.buffer[str_offset] == '\0')
17875 return NULL;
d521ce57 17876 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17877}
17878
43988095
JK
17879/* Return pointer to string at .debug_str offset as read from BUF.
17880 BUF is assumed to be in a compilation unit described by CU_HEADER.
17881 Return *BYTES_READ_PTR count of bytes read from BUF. */
17882
d521ce57
TT
17883static const char *
17884read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17885 const struct comp_unit_head *cu_header,
17886 unsigned int *bytes_read_ptr)
17887{
17888 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17889
17890 return read_indirect_string_at_offset (abfd, str_offset);
17891}
17892
43988095
JK
17893/* Return pointer to string at .debug_line_str offset as read from BUF.
17894 BUF is assumed to be in a compilation unit described by CU_HEADER.
17895 Return *BYTES_READ_PTR count of bytes read from BUF. */
17896
17897static const char *
17898read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17899 const struct comp_unit_head *cu_header,
17900 unsigned int *bytes_read_ptr)
17901{
17902 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17903
17904 return read_indirect_line_string_at_offset (abfd, str_offset);
17905}
17906
17907ULONGEST
d521ce57 17908read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17909 unsigned int *bytes_read_ptr)
c906108c 17910{
12df843f 17911 ULONGEST result;
ce5d95e1 17912 unsigned int num_read;
870f88f7 17913 int shift;
c906108c
SS
17914 unsigned char byte;
17915
17916 result = 0;
17917 shift = 0;
17918 num_read = 0;
c906108c
SS
17919 while (1)
17920 {
fe1b8b76 17921 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17922 buf++;
17923 num_read++;
12df843f 17924 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17925 if ((byte & 128) == 0)
17926 {
17927 break;
17928 }
17929 shift += 7;
17930 }
17931 *bytes_read_ptr = num_read;
17932 return result;
17933}
17934
12df843f 17935static LONGEST
d521ce57
TT
17936read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17937 unsigned int *bytes_read_ptr)
c906108c 17938{
12df843f 17939 LONGEST result;
870f88f7 17940 int shift, num_read;
c906108c
SS
17941 unsigned char byte;
17942
17943 result = 0;
17944 shift = 0;
c906108c 17945 num_read = 0;
c906108c
SS
17946 while (1)
17947 {
fe1b8b76 17948 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17949 buf++;
17950 num_read++;
12df843f 17951 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17952 shift += 7;
17953 if ((byte & 128) == 0)
17954 {
17955 break;
17956 }
17957 }
77e0b926 17958 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17959 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17960 *bytes_read_ptr = num_read;
17961 return result;
17962}
17963
3019eac3
DE
17964/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17965 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17966 ADDR_SIZE is the size of addresses from the CU header. */
17967
17968static CORE_ADDR
17969read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17970{
17971 struct objfile *objfile = dwarf2_per_objfile->objfile;
17972 bfd *abfd = objfile->obfd;
17973 const gdb_byte *info_ptr;
17974
17975 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17976 if (dwarf2_per_objfile->addr.buffer == NULL)
17977 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17978 objfile_name (objfile));
3019eac3
DE
17979 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17980 error (_("DW_FORM_addr_index pointing outside of "
17981 ".debug_addr section [in module %s]"),
4262abfb 17982 objfile_name (objfile));
3019eac3
DE
17983 info_ptr = (dwarf2_per_objfile->addr.buffer
17984 + addr_base + addr_index * addr_size);
17985 if (addr_size == 4)
17986 return bfd_get_32 (abfd, info_ptr);
17987 else
17988 return bfd_get_64 (abfd, info_ptr);
17989}
17990
17991/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17992
17993static CORE_ADDR
17994read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17995{
17996 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17997}
17998
17999/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18000
18001static CORE_ADDR
d521ce57 18002read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18003 unsigned int *bytes_read)
18004{
18005 bfd *abfd = cu->objfile->obfd;
18006 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18007
18008 return read_addr_index (cu, addr_index);
18009}
18010
18011/* Data structure to pass results from dwarf2_read_addr_index_reader
18012 back to dwarf2_read_addr_index. */
18013
18014struct dwarf2_read_addr_index_data
18015{
18016 ULONGEST addr_base;
18017 int addr_size;
18018};
18019
18020/* die_reader_func for dwarf2_read_addr_index. */
18021
18022static void
18023dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 18024 const gdb_byte *info_ptr,
3019eac3
DE
18025 struct die_info *comp_unit_die,
18026 int has_children,
18027 void *data)
18028{
18029 struct dwarf2_cu *cu = reader->cu;
18030 struct dwarf2_read_addr_index_data *aidata =
18031 (struct dwarf2_read_addr_index_data *) data;
18032
18033 aidata->addr_base = cu->addr_base;
18034 aidata->addr_size = cu->header.addr_size;
18035}
18036
18037/* Given an index in .debug_addr, fetch the value.
18038 NOTE: This can be called during dwarf expression evaluation,
18039 long after the debug information has been read, and thus per_cu->cu
18040 may no longer exist. */
18041
18042CORE_ADDR
18043dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18044 unsigned int addr_index)
18045{
18046 struct objfile *objfile = per_cu->objfile;
18047 struct dwarf2_cu *cu = per_cu->cu;
18048 ULONGEST addr_base;
18049 int addr_size;
18050
18051 /* This is intended to be called from outside this file. */
18052 dw2_setup (objfile);
18053
18054 /* We need addr_base and addr_size.
18055 If we don't have PER_CU->cu, we have to get it.
18056 Nasty, but the alternative is storing the needed info in PER_CU,
18057 which at this point doesn't seem justified: it's not clear how frequently
18058 it would get used and it would increase the size of every PER_CU.
18059 Entry points like dwarf2_per_cu_addr_size do a similar thing
18060 so we're not in uncharted territory here.
18061 Alas we need to be a bit more complicated as addr_base is contained
18062 in the DIE.
18063
18064 We don't need to read the entire CU(/TU).
18065 We just need the header and top level die.
a1b64ce1 18066
3019eac3 18067 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18068 For now we skip this optimization. */
3019eac3
DE
18069
18070 if (cu != NULL)
18071 {
18072 addr_base = cu->addr_base;
18073 addr_size = cu->header.addr_size;
18074 }
18075 else
18076 {
18077 struct dwarf2_read_addr_index_data aidata;
18078
a1b64ce1
DE
18079 /* Note: We can't use init_cutu_and_read_dies_simple here,
18080 we need addr_base. */
18081 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18082 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
18083 addr_base = aidata.addr_base;
18084 addr_size = aidata.addr_size;
18085 }
18086
18087 return read_addr_index_1 (addr_index, addr_base, addr_size);
18088}
18089
57d63ce2
DE
18090/* Given a DW_FORM_GNU_str_index, fetch the string.
18091 This is only used by the Fission support. */
3019eac3 18092
d521ce57 18093static const char *
342587c4 18094read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
18095{
18096 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18097 const char *objf_name = objfile_name (objfile);
3019eac3 18098 bfd *abfd = objfile->obfd;
342587c4 18099 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
18100 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18101 struct dwarf2_section_info *str_offsets_section =
18102 &reader->dwo_file->sections.str_offsets;
d521ce57 18103 const gdb_byte *info_ptr;
3019eac3 18104 ULONGEST str_offset;
57d63ce2 18105 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 18106
73869dc2
DE
18107 dwarf2_read_section (objfile, str_section);
18108 dwarf2_read_section (objfile, str_offsets_section);
18109 if (str_section->buffer == NULL)
57d63ce2 18110 error (_("%s used without .debug_str.dwo section"
9c541725
PA
18111 " in CU at offset 0x%x [in module %s]"),
18112 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18113 if (str_offsets_section->buffer == NULL)
57d63ce2 18114 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
18115 " in CU at offset 0x%x [in module %s]"),
18116 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18117 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 18118 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
18119 " section in CU at offset 0x%x [in module %s]"),
18120 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18121 info_ptr = (str_offsets_section->buffer
3019eac3
DE
18122 + str_index * cu->header.offset_size);
18123 if (cu->header.offset_size == 4)
18124 str_offset = bfd_get_32 (abfd, info_ptr);
18125 else
18126 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18127 if (str_offset >= str_section->size)
57d63ce2 18128 error (_("Offset from %s pointing outside of"
9c541725
PA
18129 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18130 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18131 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18132}
18133
3019eac3
DE
18134/* Return the length of an LEB128 number in BUF. */
18135
18136static int
18137leb128_size (const gdb_byte *buf)
18138{
18139 const gdb_byte *begin = buf;
18140 gdb_byte byte;
18141
18142 while (1)
18143 {
18144 byte = *buf++;
18145 if ((byte & 128) == 0)
18146 return buf - begin;
18147 }
18148}
18149
c906108c 18150static void
e142c38c 18151set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18152{
18153 switch (lang)
18154 {
18155 case DW_LANG_C89:
76bee0cc 18156 case DW_LANG_C99:
0cfd832f 18157 case DW_LANG_C11:
c906108c 18158 case DW_LANG_C:
d1be3247 18159 case DW_LANG_UPC:
e142c38c 18160 cu->language = language_c;
c906108c 18161 break;
9c37b5ae 18162 case DW_LANG_Java:
c906108c 18163 case DW_LANG_C_plus_plus:
0cfd832f
MW
18164 case DW_LANG_C_plus_plus_11:
18165 case DW_LANG_C_plus_plus_14:
e142c38c 18166 cu->language = language_cplus;
c906108c 18167 break;
6aecb9c2
JB
18168 case DW_LANG_D:
18169 cu->language = language_d;
18170 break;
c906108c
SS
18171 case DW_LANG_Fortran77:
18172 case DW_LANG_Fortran90:
b21b22e0 18173 case DW_LANG_Fortran95:
f7de9aab
MW
18174 case DW_LANG_Fortran03:
18175 case DW_LANG_Fortran08:
e142c38c 18176 cu->language = language_fortran;
c906108c 18177 break;
a766d390
DE
18178 case DW_LANG_Go:
18179 cu->language = language_go;
18180 break;
c906108c 18181 case DW_LANG_Mips_Assembler:
e142c38c 18182 cu->language = language_asm;
c906108c
SS
18183 break;
18184 case DW_LANG_Ada83:
8aaf0b47 18185 case DW_LANG_Ada95:
bc5f45f8
JB
18186 cu->language = language_ada;
18187 break;
72019c9c
GM
18188 case DW_LANG_Modula2:
18189 cu->language = language_m2;
18190 break;
fe8e67fd
PM
18191 case DW_LANG_Pascal83:
18192 cu->language = language_pascal;
18193 break;
22566fbd
DJ
18194 case DW_LANG_ObjC:
18195 cu->language = language_objc;
18196 break;
c44af4eb
TT
18197 case DW_LANG_Rust:
18198 case DW_LANG_Rust_old:
18199 cu->language = language_rust;
18200 break;
c906108c
SS
18201 case DW_LANG_Cobol74:
18202 case DW_LANG_Cobol85:
c906108c 18203 default:
e142c38c 18204 cu->language = language_minimal;
c906108c
SS
18205 break;
18206 }
e142c38c 18207 cu->language_defn = language_def (cu->language);
c906108c
SS
18208}
18209
18210/* Return the named attribute or NULL if not there. */
18211
18212static struct attribute *
e142c38c 18213dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18214{
a48e046c 18215 for (;;)
c906108c 18216 {
a48e046c
TT
18217 unsigned int i;
18218 struct attribute *spec = NULL;
18219
18220 for (i = 0; i < die->num_attrs; ++i)
18221 {
18222 if (die->attrs[i].name == name)
18223 return &die->attrs[i];
18224 if (die->attrs[i].name == DW_AT_specification
18225 || die->attrs[i].name == DW_AT_abstract_origin)
18226 spec = &die->attrs[i];
18227 }
18228
18229 if (!spec)
18230 break;
c906108c 18231
f2f0e013 18232 die = follow_die_ref (die, spec, &cu);
f2f0e013 18233 }
c5aa993b 18234
c906108c
SS
18235 return NULL;
18236}
18237
348e048f
DE
18238/* Return the named attribute or NULL if not there,
18239 but do not follow DW_AT_specification, etc.
18240 This is for use in contexts where we're reading .debug_types dies.
18241 Following DW_AT_specification, DW_AT_abstract_origin will take us
18242 back up the chain, and we want to go down. */
18243
18244static struct attribute *
45e58e77 18245dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
18246{
18247 unsigned int i;
18248
18249 for (i = 0; i < die->num_attrs; ++i)
18250 if (die->attrs[i].name == name)
18251 return &die->attrs[i];
18252
18253 return NULL;
18254}
18255
7d45c7c3
KB
18256/* Return the string associated with a string-typed attribute, or NULL if it
18257 is either not found or is of an incorrect type. */
18258
18259static const char *
18260dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18261{
18262 struct attribute *attr;
18263 const char *str = NULL;
18264
18265 attr = dwarf2_attr (die, name, cu);
18266
18267 if (attr != NULL)
18268 {
43988095 18269 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
18270 || attr->form == DW_FORM_string
18271 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18272 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18273 str = DW_STRING (attr);
18274 else
18275 complaint (&symfile_complaints,
18276 _("string type expected for attribute %s for "
18277 "DIE at 0x%x in module %s"),
9c541725 18278 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
18279 objfile_name (cu->objfile));
18280 }
18281
18282 return str;
18283}
18284
05cf31d1
JB
18285/* Return non-zero iff the attribute NAME is defined for the given DIE,
18286 and holds a non-zero value. This function should only be used for
2dc7f7b3 18287 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18288
18289static int
18290dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18291{
18292 struct attribute *attr = dwarf2_attr (die, name, cu);
18293
18294 return (attr && DW_UNSND (attr));
18295}
18296
3ca72b44 18297static int
e142c38c 18298die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18299{
05cf31d1
JB
18300 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18301 which value is non-zero. However, we have to be careful with
18302 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18303 (via dwarf2_flag_true_p) follows this attribute. So we may
18304 end up accidently finding a declaration attribute that belongs
18305 to a different DIE referenced by the specification attribute,
18306 even though the given DIE does not have a declaration attribute. */
18307 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18308 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
18309}
18310
63d06c5c 18311/* Return the die giving the specification for DIE, if there is
f2f0e013 18312 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
18313 containing the return value on output. If there is no
18314 specification, but there is an abstract origin, that is
18315 returned. */
63d06c5c
DC
18316
18317static struct die_info *
f2f0e013 18318die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 18319{
f2f0e013
DJ
18320 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18321 *spec_cu);
63d06c5c 18322
edb3359d
DJ
18323 if (spec_attr == NULL)
18324 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18325
63d06c5c
DC
18326 if (spec_attr == NULL)
18327 return NULL;
18328 else
f2f0e013 18329 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 18330}
c906108c 18331
527f3840
JK
18332/* Stub for free_line_header to match void * callback types. */
18333
18334static void
18335free_line_header_voidp (void *arg)
18336{
9a3c8263 18337 struct line_header *lh = (struct line_header *) arg;
527f3840 18338
fff8551c 18339 delete lh;
527f3840
JK
18340}
18341
fff8551c
PA
18342void
18343line_header::add_include_dir (const char *include_dir)
c906108c 18344{
27e0867f 18345 if (dwarf_line_debug >= 2)
fff8551c
PA
18346 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18347 include_dirs.size () + 1, include_dir);
27e0867f 18348
fff8551c 18349 include_dirs.push_back (include_dir);
debd256d 18350}
6e70227d 18351
fff8551c
PA
18352void
18353line_header::add_file_name (const char *name,
ecfb656c 18354 dir_index d_index,
fff8551c
PA
18355 unsigned int mod_time,
18356 unsigned int length)
debd256d 18357{
27e0867f
DE
18358 if (dwarf_line_debug >= 2)
18359 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 18360 (unsigned) file_names.size () + 1, name);
27e0867f 18361
ecfb656c 18362 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 18363}
6e70227d 18364
83769d0b 18365/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
18366
18367static struct dwarf2_section_info *
18368get_debug_line_section (struct dwarf2_cu *cu)
18369{
18370 struct dwarf2_section_info *section;
18371
18372 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18373 DWO file. */
18374 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18375 section = &cu->dwo_unit->dwo_file->sections.line;
18376 else if (cu->per_cu->is_dwz)
18377 {
18378 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18379
18380 section = &dwz->line;
18381 }
18382 else
18383 section = &dwarf2_per_objfile->line;
18384
18385 return section;
18386}
18387
43988095
JK
18388/* Read directory or file name entry format, starting with byte of
18389 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18390 entries count and the entries themselves in the described entry
18391 format. */
18392
18393static void
18394read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18395 struct line_header *lh,
18396 const struct comp_unit_head *cu_header,
18397 void (*callback) (struct line_header *lh,
18398 const char *name,
ecfb656c 18399 dir_index d_index,
43988095
JK
18400 unsigned int mod_time,
18401 unsigned int length))
18402{
18403 gdb_byte format_count, formati;
18404 ULONGEST data_count, datai;
18405 const gdb_byte *buf = *bufp;
18406 const gdb_byte *format_header_data;
18407 int i;
18408 unsigned int bytes_read;
18409
18410 format_count = read_1_byte (abfd, buf);
18411 buf += 1;
18412 format_header_data = buf;
18413 for (formati = 0; formati < format_count; formati++)
18414 {
18415 read_unsigned_leb128 (abfd, buf, &bytes_read);
18416 buf += bytes_read;
18417 read_unsigned_leb128 (abfd, buf, &bytes_read);
18418 buf += bytes_read;
18419 }
18420
18421 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18422 buf += bytes_read;
18423 for (datai = 0; datai < data_count; datai++)
18424 {
18425 const gdb_byte *format = format_header_data;
18426 struct file_entry fe;
18427
43988095
JK
18428 for (formati = 0; formati < format_count; formati++)
18429 {
ecfb656c 18430 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18431 format += bytes_read;
43988095 18432
ecfb656c 18433 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18434 format += bytes_read;
ecfb656c
PA
18435
18436 gdb::optional<const char *> string;
18437 gdb::optional<unsigned int> uint;
18438
43988095
JK
18439 switch (form)
18440 {
18441 case DW_FORM_string:
ecfb656c 18442 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
18443 buf += bytes_read;
18444 break;
18445
18446 case DW_FORM_line_strp:
ecfb656c
PA
18447 string.emplace (read_indirect_line_string (abfd, buf,
18448 cu_header,
18449 &bytes_read));
43988095
JK
18450 buf += bytes_read;
18451 break;
18452
18453 case DW_FORM_data1:
ecfb656c 18454 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
18455 buf += 1;
18456 break;
18457
18458 case DW_FORM_data2:
ecfb656c 18459 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
18460 buf += 2;
18461 break;
18462
18463 case DW_FORM_data4:
ecfb656c 18464 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
18465 buf += 4;
18466 break;
18467
18468 case DW_FORM_data8:
ecfb656c 18469 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
18470 buf += 8;
18471 break;
18472
18473 case DW_FORM_udata:
ecfb656c 18474 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
18475 buf += bytes_read;
18476 break;
18477
18478 case DW_FORM_block:
18479 /* It is valid only for DW_LNCT_timestamp which is ignored by
18480 current GDB. */
18481 break;
18482 }
ecfb656c
PA
18483
18484 switch (content_type)
18485 {
18486 case DW_LNCT_path:
18487 if (string.has_value ())
18488 fe.name = *string;
18489 break;
18490 case DW_LNCT_directory_index:
18491 if (uint.has_value ())
18492 fe.d_index = (dir_index) *uint;
18493 break;
18494 case DW_LNCT_timestamp:
18495 if (uint.has_value ())
18496 fe.mod_time = *uint;
18497 break;
18498 case DW_LNCT_size:
18499 if (uint.has_value ())
18500 fe.length = *uint;
18501 break;
18502 case DW_LNCT_MD5:
18503 break;
18504 default:
18505 complaint (&symfile_complaints,
18506 _("Unknown format content type %s"),
18507 pulongest (content_type));
18508 }
43988095
JK
18509 }
18510
ecfb656c 18511 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18512 }
18513
18514 *bufp = buf;
18515}
18516
debd256d 18517/* Read the statement program header starting at OFFSET in
3019eac3 18518 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18519 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18520 Returns NULL if there is a problem reading the header, e.g., if it
18521 has a version we don't understand.
debd256d
JB
18522
18523 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18524 the returned object point into the dwarf line section buffer,
18525 and must not be freed. */
ae2de4f8 18526
fff8551c 18527static line_header_up
9c541725 18528dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18529{
d521ce57 18530 const gdb_byte *line_ptr;
c764a876 18531 unsigned int bytes_read, offset_size;
debd256d 18532 int i;
d521ce57 18533 const char *cur_dir, *cur_file;
3019eac3
DE
18534 struct dwarf2_section_info *section;
18535 bfd *abfd;
18536
36586728 18537 section = get_debug_line_section (cu);
3019eac3
DE
18538 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18539 if (section->buffer == NULL)
debd256d 18540 {
3019eac3
DE
18541 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18542 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18543 else
18544 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18545 return 0;
18546 }
18547
fceca515
DE
18548 /* We can't do this until we know the section is non-empty.
18549 Only then do we know we have such a section. */
a32a8923 18550 abfd = get_section_bfd_owner (section);
fceca515 18551
a738430d
MK
18552 /* Make sure that at least there's room for the total_length field.
18553 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18554 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18555 {
4d3c2250 18556 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18557 return 0;
18558 }
18559
fff8551c 18560 line_header_up lh (new line_header ());
debd256d 18561
9c541725 18562 lh->sect_off = sect_off;
527f3840
JK
18563 lh->offset_in_dwz = cu->per_cu->is_dwz;
18564
9c541725 18565 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18566
a738430d 18567 /* Read in the header. */
6e70227d 18568 lh->total_length =
c764a876
DE
18569 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18570 &bytes_read, &offset_size);
debd256d 18571 line_ptr += bytes_read;
3019eac3 18572 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18573 {
4d3c2250 18574 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18575 return 0;
18576 }
18577 lh->statement_program_end = line_ptr + lh->total_length;
18578 lh->version = read_2_bytes (abfd, line_ptr);
18579 line_ptr += 2;
43988095 18580 if (lh->version > 5)
cd366ee8
DE
18581 {
18582 /* This is a version we don't understand. The format could have
18583 changed in ways we don't handle properly so just punt. */
18584 complaint (&symfile_complaints,
18585 _("unsupported version in .debug_line section"));
18586 return NULL;
18587 }
43988095
JK
18588 if (lh->version >= 5)
18589 {
18590 gdb_byte segment_selector_size;
18591
18592 /* Skip address size. */
18593 read_1_byte (abfd, line_ptr);
18594 line_ptr += 1;
18595
18596 segment_selector_size = read_1_byte (abfd, line_ptr);
18597 line_ptr += 1;
18598 if (segment_selector_size != 0)
18599 {
18600 complaint (&symfile_complaints,
18601 _("unsupported segment selector size %u "
18602 "in .debug_line section"),
18603 segment_selector_size);
18604 return NULL;
18605 }
18606 }
c764a876
DE
18607 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18608 line_ptr += offset_size;
debd256d
JB
18609 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18610 line_ptr += 1;
2dc7f7b3
TT
18611 if (lh->version >= 4)
18612 {
18613 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18614 line_ptr += 1;
18615 }
18616 else
18617 lh->maximum_ops_per_instruction = 1;
18618
18619 if (lh->maximum_ops_per_instruction == 0)
18620 {
18621 lh->maximum_ops_per_instruction = 1;
18622 complaint (&symfile_complaints,
3e43a32a
MS
18623 _("invalid maximum_ops_per_instruction "
18624 "in `.debug_line' section"));
2dc7f7b3
TT
18625 }
18626
debd256d
JB
18627 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18628 line_ptr += 1;
18629 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18630 line_ptr += 1;
18631 lh->line_range = read_1_byte (abfd, line_ptr);
18632 line_ptr += 1;
18633 lh->opcode_base = read_1_byte (abfd, line_ptr);
18634 line_ptr += 1;
fff8551c 18635 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18636
18637 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18638 for (i = 1; i < lh->opcode_base; ++i)
18639 {
18640 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18641 line_ptr += 1;
18642 }
18643
43988095 18644 if (lh->version >= 5)
debd256d 18645 {
43988095 18646 /* Read directory table. */
fff8551c
PA
18647 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18648 [] (struct line_header *lh, const char *name,
ecfb656c 18649 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18650 unsigned int length)
18651 {
18652 lh->add_include_dir (name);
18653 });
debd256d 18654
43988095 18655 /* Read file name table. */
fff8551c
PA
18656 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18657 [] (struct line_header *lh, const char *name,
ecfb656c 18658 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18659 unsigned int length)
18660 {
ecfb656c 18661 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18662 });
43988095
JK
18663 }
18664 else
debd256d 18665 {
43988095
JK
18666 /* Read directory table. */
18667 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18668 {
18669 line_ptr += bytes_read;
fff8551c 18670 lh->add_include_dir (cur_dir);
43988095 18671 }
debd256d
JB
18672 line_ptr += bytes_read;
18673
43988095
JK
18674 /* Read file name table. */
18675 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18676 {
ecfb656c
PA
18677 unsigned int mod_time, length;
18678 dir_index d_index;
43988095
JK
18679
18680 line_ptr += bytes_read;
ecfb656c 18681 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18682 line_ptr += bytes_read;
18683 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18684 line_ptr += bytes_read;
18685 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18686 line_ptr += bytes_read;
18687
ecfb656c 18688 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18689 }
18690 line_ptr += bytes_read;
debd256d 18691 }
6e70227d 18692 lh->statement_program_start = line_ptr;
debd256d 18693
3019eac3 18694 if (line_ptr > (section->buffer + section->size))
4d3c2250 18695 complaint (&symfile_complaints,
3e43a32a
MS
18696 _("line number info header doesn't "
18697 "fit in `.debug_line' section"));
debd256d 18698
debd256d
JB
18699 return lh;
18700}
c906108c 18701
c6da4cef
DE
18702/* Subroutine of dwarf_decode_lines to simplify it.
18703 Return the file name of the psymtab for included file FILE_INDEX
18704 in line header LH of PST.
18705 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18706 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18707 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18708
18709 The function creates dangling cleanup registration. */
c6da4cef 18710
d521ce57 18711static const char *
c6da4cef
DE
18712psymtab_include_file_name (const struct line_header *lh, int file_index,
18713 const struct partial_symtab *pst,
18714 const char *comp_dir)
18715{
8c43009f 18716 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18717 const char *include_name = fe.name;
18718 const char *include_name_to_compare = include_name;
72b9f47f
TT
18719 const char *pst_filename;
18720 char *copied_name = NULL;
c6da4cef
DE
18721 int file_is_pst;
18722
8c43009f 18723 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18724
18725 if (!IS_ABSOLUTE_PATH (include_name)
18726 && (dir_name != NULL || comp_dir != NULL))
18727 {
18728 /* Avoid creating a duplicate psymtab for PST.
18729 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18730 Before we do the comparison, however, we need to account
18731 for DIR_NAME and COMP_DIR.
18732 First prepend dir_name (if non-NULL). If we still don't
18733 have an absolute path prepend comp_dir (if non-NULL).
18734 However, the directory we record in the include-file's
18735 psymtab does not contain COMP_DIR (to match the
18736 corresponding symtab(s)).
18737
18738 Example:
18739
18740 bash$ cd /tmp
18741 bash$ gcc -g ./hello.c
18742 include_name = "hello.c"
18743 dir_name = "."
18744 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18745 DW_AT_name = "./hello.c"
18746
18747 */
c6da4cef
DE
18748
18749 if (dir_name != NULL)
18750 {
d521ce57
TT
18751 char *tem = concat (dir_name, SLASH_STRING,
18752 include_name, (char *)NULL);
18753
18754 make_cleanup (xfree, tem);
18755 include_name = tem;
c6da4cef 18756 include_name_to_compare = include_name;
c6da4cef
DE
18757 }
18758 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18759 {
d521ce57
TT
18760 char *tem = concat (comp_dir, SLASH_STRING,
18761 include_name, (char *)NULL);
18762
18763 make_cleanup (xfree, tem);
18764 include_name_to_compare = tem;
c6da4cef
DE
18765 }
18766 }
18767
18768 pst_filename = pst->filename;
18769 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18770 {
72b9f47f
TT
18771 copied_name = concat (pst->dirname, SLASH_STRING,
18772 pst_filename, (char *)NULL);
18773 pst_filename = copied_name;
c6da4cef
DE
18774 }
18775
1e3fad37 18776 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18777
72b9f47f
TT
18778 if (copied_name != NULL)
18779 xfree (copied_name);
c6da4cef
DE
18780
18781 if (file_is_pst)
18782 return NULL;
18783 return include_name;
18784}
18785
d9b3de22
DE
18786/* State machine to track the state of the line number program. */
18787
6f77053d 18788class lnp_state_machine
d9b3de22 18789{
6f77053d
PA
18790public:
18791 /* Initialize a machine state for the start of a line number
18792 program. */
18793 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18794
8c43009f
PA
18795 file_entry *current_file ()
18796 {
18797 /* lh->file_names is 0-based, but the file name numbers in the
18798 statement program are 1-based. */
6f77053d
PA
18799 return m_line_header->file_name_at (m_file);
18800 }
18801
18802 /* Record the line in the state machine. END_SEQUENCE is true if
18803 we're processing the end of a sequence. */
18804 void record_line (bool end_sequence);
18805
18806 /* Check address and if invalid nop-out the rest of the lines in this
18807 sequence. */
18808 void check_line_address (struct dwarf2_cu *cu,
18809 const gdb_byte *line_ptr,
18810 CORE_ADDR lowpc, CORE_ADDR address);
18811
18812 void handle_set_discriminator (unsigned int discriminator)
18813 {
18814 m_discriminator = discriminator;
18815 m_line_has_non_zero_discriminator |= discriminator != 0;
18816 }
18817
18818 /* Handle DW_LNE_set_address. */
18819 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18820 {
18821 m_op_index = 0;
18822 address += baseaddr;
18823 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18824 }
18825
18826 /* Handle DW_LNS_advance_pc. */
18827 void handle_advance_pc (CORE_ADDR adjust);
18828
18829 /* Handle a special opcode. */
18830 void handle_special_opcode (unsigned char op_code);
18831
18832 /* Handle DW_LNS_advance_line. */
18833 void handle_advance_line (int line_delta)
18834 {
18835 advance_line (line_delta);
18836 }
18837
18838 /* Handle DW_LNS_set_file. */
18839 void handle_set_file (file_name_index file);
18840
18841 /* Handle DW_LNS_negate_stmt. */
18842 void handle_negate_stmt ()
18843 {
18844 m_is_stmt = !m_is_stmt;
18845 }
18846
18847 /* Handle DW_LNS_const_add_pc. */
18848 void handle_const_add_pc ();
18849
18850 /* Handle DW_LNS_fixed_advance_pc. */
18851 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18852 {
18853 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18854 m_op_index = 0;
18855 }
18856
18857 /* Handle DW_LNS_copy. */
18858 void handle_copy ()
18859 {
18860 record_line (false);
18861 m_discriminator = 0;
18862 }
18863
18864 /* Handle DW_LNE_end_sequence. */
18865 void handle_end_sequence ()
18866 {
18867 m_record_line_callback = ::record_line;
18868 }
18869
18870private:
18871 /* Advance the line by LINE_DELTA. */
18872 void advance_line (int line_delta)
18873 {
18874 m_line += line_delta;
18875
18876 if (line_delta != 0)
18877 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18878 }
18879
6f77053d
PA
18880 gdbarch *m_gdbarch;
18881
18882 /* True if we're recording lines.
18883 Otherwise we're building partial symtabs and are just interested in
18884 finding include files mentioned by the line number program. */
18885 bool m_record_lines_p;
18886
8c43009f 18887 /* The line number header. */
6f77053d 18888 line_header *m_line_header;
8c43009f 18889
6f77053d
PA
18890 /* These are part of the standard DWARF line number state machine,
18891 and initialized according to the DWARF spec. */
d9b3de22 18892
6f77053d 18893 unsigned char m_op_index = 0;
8c43009f 18894 /* The line table index (1-based) of the current file. */
6f77053d
PA
18895 file_name_index m_file = (file_name_index) 1;
18896 unsigned int m_line = 1;
18897
18898 /* These are initialized in the constructor. */
18899
18900 CORE_ADDR m_address;
18901 bool m_is_stmt;
18902 unsigned int m_discriminator;
d9b3de22
DE
18903
18904 /* Additional bits of state we need to track. */
18905
18906 /* The last file that we called dwarf2_start_subfile for.
18907 This is only used for TLLs. */
6f77053d 18908 unsigned int m_last_file = 0;
d9b3de22 18909 /* The last file a line number was recorded for. */
6f77053d 18910 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18911
18912 /* The function to call to record a line. */
6f77053d 18913 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18914
18915 /* The last line number that was recorded, used to coalesce
18916 consecutive entries for the same line. This can happen, for
18917 example, when discriminators are present. PR 17276. */
6f77053d
PA
18918 unsigned int m_last_line = 0;
18919 bool m_line_has_non_zero_discriminator = false;
8c43009f 18920};
d9b3de22 18921
6f77053d
PA
18922void
18923lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18924{
18925 CORE_ADDR addr_adj = (((m_op_index + adjust)
18926 / m_line_header->maximum_ops_per_instruction)
18927 * m_line_header->minimum_instruction_length);
18928 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18929 m_op_index = ((m_op_index + adjust)
18930 % m_line_header->maximum_ops_per_instruction);
18931}
d9b3de22 18932
6f77053d
PA
18933void
18934lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18935{
6f77053d
PA
18936 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18937 CORE_ADDR addr_adj = (((m_op_index
18938 + (adj_opcode / m_line_header->line_range))
18939 / m_line_header->maximum_ops_per_instruction)
18940 * m_line_header->minimum_instruction_length);
18941 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18942 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18943 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18944
6f77053d
PA
18945 int line_delta = (m_line_header->line_base
18946 + (adj_opcode % m_line_header->line_range));
18947 advance_line (line_delta);
18948 record_line (false);
18949 m_discriminator = 0;
18950}
d9b3de22 18951
6f77053d
PA
18952void
18953lnp_state_machine::handle_set_file (file_name_index file)
18954{
18955 m_file = file;
18956
18957 const file_entry *fe = current_file ();
18958 if (fe == NULL)
18959 dwarf2_debug_line_missing_file_complaint ();
18960 else if (m_record_lines_p)
18961 {
18962 const char *dir = fe->include_dir (m_line_header);
18963
18964 m_last_subfile = current_subfile;
18965 m_line_has_non_zero_discriminator = m_discriminator != 0;
18966 dwarf2_start_subfile (fe->name, dir);
18967 }
18968}
18969
18970void
18971lnp_state_machine::handle_const_add_pc ()
18972{
18973 CORE_ADDR adjust
18974 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18975
18976 CORE_ADDR addr_adj
18977 = (((m_op_index + adjust)
18978 / m_line_header->maximum_ops_per_instruction)
18979 * m_line_header->minimum_instruction_length);
18980
18981 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18982 m_op_index = ((m_op_index + adjust)
18983 % m_line_header->maximum_ops_per_instruction);
18984}
d9b3de22 18985
c91513d8
PP
18986/* Ignore this record_line request. */
18987
18988static void
18989noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18990{
18991 return;
18992}
18993
a05a36a5
DE
18994/* Return non-zero if we should add LINE to the line number table.
18995 LINE is the line to add, LAST_LINE is the last line that was added,
18996 LAST_SUBFILE is the subfile for LAST_LINE.
18997 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18998 had a non-zero discriminator.
18999
19000 We have to be careful in the presence of discriminators.
19001 E.g., for this line:
19002
19003 for (i = 0; i < 100000; i++);
19004
19005 clang can emit four line number entries for that one line,
19006 each with a different discriminator.
19007 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19008
19009 However, we want gdb to coalesce all four entries into one.
19010 Otherwise the user could stepi into the middle of the line and
19011 gdb would get confused about whether the pc really was in the
19012 middle of the line.
19013
19014 Things are further complicated by the fact that two consecutive
19015 line number entries for the same line is a heuristic used by gcc
19016 to denote the end of the prologue. So we can't just discard duplicate
19017 entries, we have to be selective about it. The heuristic we use is
19018 that we only collapse consecutive entries for the same line if at least
19019 one of those entries has a non-zero discriminator. PR 17276.
19020
19021 Note: Addresses in the line number state machine can never go backwards
19022 within one sequence, thus this coalescing is ok. */
19023
19024static int
19025dwarf_record_line_p (unsigned int line, unsigned int last_line,
19026 int line_has_non_zero_discriminator,
19027 struct subfile *last_subfile)
19028{
19029 if (current_subfile != last_subfile)
19030 return 1;
19031 if (line != last_line)
19032 return 1;
19033 /* Same line for the same file that we've seen already.
19034 As a last check, for pr 17276, only record the line if the line
19035 has never had a non-zero discriminator. */
19036 if (!line_has_non_zero_discriminator)
19037 return 1;
19038 return 0;
19039}
19040
252a6764
DE
19041/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19042 in the line table of subfile SUBFILE. */
19043
19044static void
d9b3de22
DE
19045dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19046 unsigned int line, CORE_ADDR address,
19047 record_line_ftype p_record_line)
252a6764
DE
19048{
19049 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19050
27e0867f
DE
19051 if (dwarf_line_debug)
19052 {
19053 fprintf_unfiltered (gdb_stdlog,
19054 "Recording line %u, file %s, address %s\n",
19055 line, lbasename (subfile->name),
19056 paddress (gdbarch, address));
19057 }
19058
d5962de5 19059 (*p_record_line) (subfile, line, addr);
252a6764
DE
19060}
19061
19062/* Subroutine of dwarf_decode_lines_1 to simplify it.
19063 Mark the end of a set of line number records.
d9b3de22 19064 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19065 If SUBFILE is NULL the request is ignored. */
19066
19067static void
19068dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19069 CORE_ADDR address, record_line_ftype p_record_line)
19070{
27e0867f
DE
19071 if (subfile == NULL)
19072 return;
19073
19074 if (dwarf_line_debug)
19075 {
19076 fprintf_unfiltered (gdb_stdlog,
19077 "Finishing current line, file %s, address %s\n",
19078 lbasename (subfile->name),
19079 paddress (gdbarch, address));
19080 }
19081
d9b3de22
DE
19082 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19083}
19084
6f77053d
PA
19085void
19086lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19087{
d9b3de22
DE
19088 if (dwarf_line_debug)
19089 {
19090 fprintf_unfiltered (gdb_stdlog,
19091 "Processing actual line %u: file %u,"
19092 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
19093 m_line, to_underlying (m_file),
19094 paddress (m_gdbarch, m_address),
19095 m_is_stmt, m_discriminator);
d9b3de22
DE
19096 }
19097
6f77053d 19098 file_entry *fe = current_file ();
8c43009f
PA
19099
19100 if (fe == NULL)
d9b3de22
DE
19101 dwarf2_debug_line_missing_file_complaint ();
19102 /* For now we ignore lines not starting on an instruction boundary.
19103 But not when processing end_sequence for compatibility with the
19104 previous version of the code. */
6f77053d 19105 else if (m_op_index == 0 || end_sequence)
d9b3de22 19106 {
8c43009f 19107 fe->included_p = 1;
6f77053d 19108 if (m_record_lines_p && m_is_stmt)
d9b3de22 19109 {
6f77053d 19110 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 19111 {
6f77053d
PA
19112 dwarf_finish_line (m_gdbarch, m_last_subfile,
19113 m_address, m_record_line_callback);
d9b3de22
DE
19114 }
19115
19116 if (!end_sequence)
19117 {
6f77053d
PA
19118 if (dwarf_record_line_p (m_line, m_last_line,
19119 m_line_has_non_zero_discriminator,
19120 m_last_subfile))
d9b3de22 19121 {
6f77053d
PA
19122 dwarf_record_line_1 (m_gdbarch, current_subfile,
19123 m_line, m_address,
19124 m_record_line_callback);
d9b3de22 19125 }
6f77053d
PA
19126 m_last_subfile = current_subfile;
19127 m_last_line = m_line;
d9b3de22
DE
19128 }
19129 }
19130 }
19131}
19132
6f77053d
PA
19133lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19134 bool record_lines_p)
d9b3de22 19135{
6f77053d
PA
19136 m_gdbarch = arch;
19137 m_record_lines_p = record_lines_p;
19138 m_line_header = lh;
d9b3de22 19139
6f77053d 19140 m_record_line_callback = ::record_line;
d9b3de22 19141
d9b3de22
DE
19142 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19143 was a line entry for it so that the backend has a chance to adjust it
19144 and also record it in case it needs it. This is currently used by MIPS
19145 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19146 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19147 m_is_stmt = lh->default_is_stmt;
19148 m_discriminator = 0;
252a6764
DE
19149}
19150
6f77053d
PA
19151void
19152lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19153 const gdb_byte *line_ptr,
19154 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
19155{
19156 /* If address < lowpc then it's not a usable value, it's outside the
19157 pc range of the CU. However, we restrict the test to only address
19158 values of zero to preserve GDB's previous behaviour which is to
19159 handle the specific case of a function being GC'd by the linker. */
19160
19161 if (address == 0 && address < lowpc)
19162 {
19163 /* This line table is for a function which has been
19164 GCd by the linker. Ignore it. PR gdb/12528 */
19165
19166 struct objfile *objfile = cu->objfile;
19167 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19168
19169 complaint (&symfile_complaints,
19170 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19171 line_offset, objfile_name (objfile));
6f77053d
PA
19172 m_record_line_callback = noop_record_line;
19173 /* Note: record_line_callback is left as noop_record_line until
19174 we see DW_LNE_end_sequence. */
924c2928
DE
19175 }
19176}
19177
f3f5162e 19178/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19179 Process the line number information in LH.
19180 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19181 program in order to set included_p for every referenced header. */
debd256d 19182
c906108c 19183static void
43f3e411
DE
19184dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19185 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19186{
d521ce57
TT
19187 const gdb_byte *line_ptr, *extended_end;
19188 const gdb_byte *line_end;
a8c50c1f 19189 unsigned int bytes_read, extended_len;
699ca60a 19190 unsigned char op_code, extended_op;
e142c38c
DJ
19191 CORE_ADDR baseaddr;
19192 struct objfile *objfile = cu->objfile;
f3f5162e 19193 bfd *abfd = objfile->obfd;
fbf65064 19194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19195 /* True if we're recording line info (as opposed to building partial
19196 symtabs and just interested in finding include files mentioned by
19197 the line number program). */
19198 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
19199
19200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19201
debd256d
JB
19202 line_ptr = lh->statement_program_start;
19203 line_end = lh->statement_program_end;
c906108c
SS
19204
19205 /* Read the statement sequences until there's nothing left. */
19206 while (line_ptr < line_end)
19207 {
6f77053d
PA
19208 /* The DWARF line number program state machine. Reset the state
19209 machine at the start of each sequence. */
19210 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19211 bool end_sequence = false;
d9b3de22 19212
8c43009f 19213 if (record_lines_p)
c906108c 19214 {
8c43009f
PA
19215 /* Start a subfile for the current file of the state
19216 machine. */
19217 const file_entry *fe = state_machine.current_file ();
19218
19219 if (fe != NULL)
19220 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
19221 }
19222
a738430d 19223 /* Decode the table. */
d9b3de22 19224 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19225 {
19226 op_code = read_1_byte (abfd, line_ptr);
19227 line_ptr += 1;
9aa1fe7e 19228
debd256d 19229 if (op_code >= lh->opcode_base)
6e70227d 19230 {
8e07a239 19231 /* Special opcode. */
6f77053d 19232 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19233 }
19234 else switch (op_code)
c906108c
SS
19235 {
19236 case DW_LNS_extended_op:
3e43a32a
MS
19237 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19238 &bytes_read);
473b7be6 19239 line_ptr += bytes_read;
a8c50c1f 19240 extended_end = line_ptr + extended_len;
c906108c
SS
19241 extended_op = read_1_byte (abfd, line_ptr);
19242 line_ptr += 1;
19243 switch (extended_op)
19244 {
19245 case DW_LNE_end_sequence:
6f77053d
PA
19246 state_machine.handle_end_sequence ();
19247 end_sequence = true;
c906108c
SS
19248 break;
19249 case DW_LNE_set_address:
d9b3de22
DE
19250 {
19251 CORE_ADDR address
19252 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 19253 line_ptr += bytes_read;
6f77053d
PA
19254
19255 state_machine.check_line_address (cu, line_ptr,
19256 lowpc, address);
19257 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19258 }
c906108c
SS
19259 break;
19260 case DW_LNE_define_file:
debd256d 19261 {
d521ce57 19262 const char *cur_file;
ecfb656c
PA
19263 unsigned int mod_time, length;
19264 dir_index dindex;
6e70227d 19265
3e43a32a
MS
19266 cur_file = read_direct_string (abfd, line_ptr,
19267 &bytes_read);
debd256d 19268 line_ptr += bytes_read;
ecfb656c 19269 dindex = (dir_index)
debd256d
JB
19270 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19271 line_ptr += bytes_read;
19272 mod_time =
19273 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19274 line_ptr += bytes_read;
19275 length =
19276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19277 line_ptr += bytes_read;
ecfb656c 19278 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19279 }
c906108c 19280 break;
d0c6ba3d 19281 case DW_LNE_set_discriminator:
6f77053d
PA
19282 {
19283 /* The discriminator is not interesting to the
19284 debugger; just ignore it. We still need to
19285 check its value though:
19286 if there are consecutive entries for the same
19287 (non-prologue) line we want to coalesce them.
19288 PR 17276. */
19289 unsigned int discr
19290 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19291 line_ptr += bytes_read;
19292
19293 state_machine.handle_set_discriminator (discr);
19294 }
d0c6ba3d 19295 break;
c906108c 19296 default:
4d3c2250 19297 complaint (&symfile_complaints,
e2e0b3e5 19298 _("mangled .debug_line section"));
debd256d 19299 return;
c906108c 19300 }
a8c50c1f
DJ
19301 /* Make sure that we parsed the extended op correctly. If e.g.
19302 we expected a different address size than the producer used,
19303 we may have read the wrong number of bytes. */
19304 if (line_ptr != extended_end)
19305 {
19306 complaint (&symfile_complaints,
19307 _("mangled .debug_line section"));
19308 return;
19309 }
c906108c
SS
19310 break;
19311 case DW_LNS_copy:
6f77053d 19312 state_machine.handle_copy ();
c906108c
SS
19313 break;
19314 case DW_LNS_advance_pc:
2dc7f7b3
TT
19315 {
19316 CORE_ADDR adjust
19317 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19318 line_ptr += bytes_read;
6f77053d
PA
19319
19320 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19321 }
c906108c
SS
19322 break;
19323 case DW_LNS_advance_line:
a05a36a5
DE
19324 {
19325 int line_delta
19326 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19327 line_ptr += bytes_read;
6f77053d
PA
19328
19329 state_machine.handle_advance_line (line_delta);
a05a36a5 19330 }
c906108c
SS
19331 break;
19332 case DW_LNS_set_file:
d9b3de22 19333 {
6f77053d 19334 file_name_index file
ecfb656c
PA
19335 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19336 &bytes_read);
d9b3de22 19337 line_ptr += bytes_read;
8c43009f 19338
6f77053d 19339 state_machine.handle_set_file (file);
d9b3de22 19340 }
c906108c
SS
19341 break;
19342 case DW_LNS_set_column:
0ad93d4f 19343 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19344 line_ptr += bytes_read;
19345 break;
19346 case DW_LNS_negate_stmt:
6f77053d 19347 state_machine.handle_negate_stmt ();
c906108c
SS
19348 break;
19349 case DW_LNS_set_basic_block:
c906108c 19350 break;
c2c6d25f
JM
19351 /* Add to the address register of the state machine the
19352 address increment value corresponding to special opcode
a738430d
MK
19353 255. I.e., this value is scaled by the minimum
19354 instruction length since special opcode 255 would have
b021a221 19355 scaled the increment. */
c906108c 19356 case DW_LNS_const_add_pc:
6f77053d 19357 state_machine.handle_const_add_pc ();
c906108c
SS
19358 break;
19359 case DW_LNS_fixed_advance_pc:
3e29f34a 19360 {
6f77053d 19361 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19362 line_ptr += 2;
6f77053d
PA
19363
19364 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19365 }
c906108c 19366 break;
9aa1fe7e 19367 default:
a738430d
MK
19368 {
19369 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19370 int i;
a738430d 19371
debd256d 19372 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19373 {
19374 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19375 line_ptr += bytes_read;
19376 }
19377 }
c906108c
SS
19378 }
19379 }
d9b3de22
DE
19380
19381 if (!end_sequence)
19382 dwarf2_debug_line_missing_end_sequence_complaint ();
19383
19384 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19385 in which case we still finish recording the last line). */
6f77053d 19386 state_machine.record_line (true);
c906108c 19387 }
f3f5162e
DE
19388}
19389
19390/* Decode the Line Number Program (LNP) for the given line_header
19391 structure and CU. The actual information extracted and the type
19392 of structures created from the LNP depends on the value of PST.
19393
19394 1. If PST is NULL, then this procedure uses the data from the program
19395 to create all necessary symbol tables, and their linetables.
19396
19397 2. If PST is not NULL, this procedure reads the program to determine
19398 the list of files included by the unit represented by PST, and
19399 builds all the associated partial symbol tables.
19400
19401 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19402 It is used for relative paths in the line table.
19403 NOTE: When processing partial symtabs (pst != NULL),
19404 comp_dir == pst->dirname.
19405
19406 NOTE: It is important that psymtabs have the same file name (via strcmp)
19407 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19408 symtab we don't use it in the name of the psymtabs we create.
19409 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19410 A good testcase for this is mb-inline.exp.
19411
527f3840
JK
19412 LOWPC is the lowest address in CU (or 0 if not known).
19413
19414 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19415 for its PC<->lines mapping information. Otherwise only the filename
19416 table is read in. */
f3f5162e
DE
19417
19418static void
19419dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 19420 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 19421 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
19422{
19423 struct objfile *objfile = cu->objfile;
19424 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19425
527f3840
JK
19426 if (decode_mapping)
19427 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19428
19429 if (decode_for_pst_p)
19430 {
19431 int file_index;
19432
19433 /* Now that we're done scanning the Line Header Program, we can
19434 create the psymtab of each included file. */
fff8551c 19435 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
19436 if (lh->file_names[file_index].included_p == 1)
19437 {
d521ce57 19438 const char *include_name =
c6da4cef
DE
19439 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19440 if (include_name != NULL)
aaa75496
JB
19441 dwarf2_create_include_psymtab (include_name, pst, objfile);
19442 }
19443 }
cb1df416
DJ
19444 else
19445 {
19446 /* Make sure a symtab is created for every file, even files
19447 which contain only variables (i.e. no code with associated
19448 line numbers). */
43f3e411 19449 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 19450 int i;
cb1df416 19451
fff8551c 19452 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 19453 {
8c43009f 19454 file_entry &fe = lh->file_names[i];
9a619af0 19455
8c43009f 19456 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 19457
cb1df416 19458 if (current_subfile->symtab == NULL)
43f3e411
DE
19459 {
19460 current_subfile->symtab
19461 = allocate_symtab (cust, current_subfile->name);
19462 }
8c43009f 19463 fe.symtab = current_subfile->symtab;
cb1df416
DJ
19464 }
19465 }
c906108c
SS
19466}
19467
19468/* Start a subfile for DWARF. FILENAME is the name of the file and
19469 DIRNAME the name of the source directory which contains FILENAME
4d663531 19470 or NULL if not known.
c906108c
SS
19471 This routine tries to keep line numbers from identical absolute and
19472 relative file names in a common subfile.
19473
19474 Using the `list' example from the GDB testsuite, which resides in
19475 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19476 of /srcdir/list0.c yields the following debugging information for list0.c:
19477
c5aa993b 19478 DW_AT_name: /srcdir/list0.c
4d663531 19479 DW_AT_comp_dir: /compdir
357e46e7 19480 files.files[0].name: list0.h
c5aa993b 19481 files.files[0].dir: /srcdir
357e46e7 19482 files.files[1].name: list0.c
c5aa993b 19483 files.files[1].dir: /srcdir
c906108c
SS
19484
19485 The line number information for list0.c has to end up in a single
4f1520fb
FR
19486 subfile, so that `break /srcdir/list0.c:1' works as expected.
19487 start_subfile will ensure that this happens provided that we pass the
19488 concatenation of files.files[1].dir and files.files[1].name as the
19489 subfile's name. */
c906108c
SS
19490
19491static void
4d663531 19492dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 19493{
d521ce57 19494 char *copy = NULL;
4f1520fb 19495
4d663531 19496 /* In order not to lose the line information directory,
4f1520fb
FR
19497 we concatenate it to the filename when it makes sense.
19498 Note that the Dwarf3 standard says (speaking of filenames in line
19499 information): ``The directory index is ignored for file names
19500 that represent full path names''. Thus ignoring dirname in the
19501 `else' branch below isn't an issue. */
c906108c 19502
d5166ae1 19503 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19504 {
19505 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19506 filename = copy;
19507 }
c906108c 19508
4d663531 19509 start_subfile (filename);
4f1520fb 19510
d521ce57
TT
19511 if (copy != NULL)
19512 xfree (copy);
c906108c
SS
19513}
19514
f4dc4d17
DE
19515/* Start a symtab for DWARF.
19516 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19517
43f3e411 19518static struct compunit_symtab *
f4dc4d17 19519dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19520 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19521{
43f3e411 19522 struct compunit_symtab *cust
5ffa0793 19523 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19524
f4dc4d17
DE
19525 record_debugformat ("DWARF 2");
19526 record_producer (cu->producer);
19527
19528 /* We assume that we're processing GCC output. */
19529 processing_gcc_compilation = 2;
19530
4d4ec4e5 19531 cu->processing_has_namespace_info = 0;
43f3e411
DE
19532
19533 return cust;
f4dc4d17
DE
19534}
19535
4c2df51b
DJ
19536static void
19537var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19538 struct dwarf2_cu *cu)
4c2df51b 19539{
e7c27a73
DJ
19540 struct objfile *objfile = cu->objfile;
19541 struct comp_unit_head *cu_header = &cu->header;
19542
4c2df51b
DJ
19543 /* NOTE drow/2003-01-30: There used to be a comment and some special
19544 code here to turn a symbol with DW_AT_external and a
19545 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19546 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19547 with some versions of binutils) where shared libraries could have
19548 relocations against symbols in their debug information - the
19549 minimal symbol would have the right address, but the debug info
19550 would not. It's no longer necessary, because we will explicitly
19551 apply relocations when we read in the debug information now. */
19552
19553 /* A DW_AT_location attribute with no contents indicates that a
19554 variable has been optimized away. */
19555 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19556 {
f1e6e072 19557 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19558 return;
19559 }
19560
19561 /* Handle one degenerate form of location expression specially, to
19562 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19563 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19564 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19565
19566 if (attr_form_is_block (attr)
3019eac3
DE
19567 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19568 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19569 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19570 && (DW_BLOCK (attr)->size
19571 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19572 {
891d2f0b 19573 unsigned int dummy;
4c2df51b 19574
3019eac3
DE
19575 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19576 SYMBOL_VALUE_ADDRESS (sym) =
19577 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19578 else
19579 SYMBOL_VALUE_ADDRESS (sym) =
19580 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19581 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19582 fixup_symbol_section (sym, objfile);
19583 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19584 SYMBOL_SECTION (sym));
4c2df51b
DJ
19585 return;
19586 }
19587
19588 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19589 expression evaluator, and use LOC_COMPUTED only when necessary
19590 (i.e. when the value of a register or memory location is
19591 referenced, or a thread-local block, etc.). Then again, it might
19592 not be worthwhile. I'm assuming that it isn't unless performance
19593 or memory numbers show me otherwise. */
19594
f1e6e072 19595 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19596
f1e6e072 19597 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19598 cu->has_loclist = 1;
4c2df51b
DJ
19599}
19600
c906108c
SS
19601/* Given a pointer to a DWARF information entry, figure out if we need
19602 to make a symbol table entry for it, and if so, create a new entry
19603 and return a pointer to it.
19604 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19605 used the passed type.
19606 If SPACE is not NULL, use it to hold the new symbol. If it is
19607 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19608
19609static struct symbol *
34eaf542
TT
19610new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19611 struct symbol *space)
c906108c 19612{
e7c27a73 19613 struct objfile *objfile = cu->objfile;
3e29f34a 19614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19615 struct symbol *sym = NULL;
15d034d0 19616 const char *name;
c906108c
SS
19617 struct attribute *attr = NULL;
19618 struct attribute *attr2 = NULL;
e142c38c 19619 CORE_ADDR baseaddr;
e37fd15a
SW
19620 struct pending **list_to_add = NULL;
19621
edb3359d 19622 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19623
19624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19625
94af9270 19626 name = dwarf2_name (die, cu);
c906108c
SS
19627 if (name)
19628 {
94af9270 19629 const char *linkagename;
34eaf542 19630 int suppress_add = 0;
94af9270 19631
34eaf542
TT
19632 if (space)
19633 sym = space;
19634 else
e623cf5d 19635 sym = allocate_symbol (objfile);
c906108c 19636 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19637
19638 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19639 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19640 linkagename = dwarf2_physname (name, die, cu);
19641 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19642
f55ee35c
JK
19643 /* Fortran does not have mangling standard and the mangling does differ
19644 between gfortran, iFort etc. */
19645 if (cu->language == language_fortran
b250c185 19646 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19647 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19648 dwarf2_full_name (name, die, cu),
29df156d 19649 NULL);
f55ee35c 19650
c906108c 19651 /* Default assumptions.
c5aa993b 19652 Use the passed type or decode it from the die. */
176620f1 19653 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19654 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19655 if (type != NULL)
19656 SYMBOL_TYPE (sym) = type;
19657 else
e7c27a73 19658 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19659 attr = dwarf2_attr (die,
19660 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19661 cu);
c906108c
SS
19662 if (attr)
19663 {
19664 SYMBOL_LINE (sym) = DW_UNSND (attr);
19665 }
cb1df416 19666
edb3359d
DJ
19667 attr = dwarf2_attr (die,
19668 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19669 cu);
cb1df416
DJ
19670 if (attr)
19671 {
ecfb656c 19672 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19673 struct file_entry *fe;
9a619af0 19674
ecfb656c
PA
19675 if (cu->line_header != NULL)
19676 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19677 else
19678 fe = NULL;
19679
19680 if (fe == NULL)
cb1df416
DJ
19681 complaint (&symfile_complaints,
19682 _("file index out of range"));
8c43009f
PA
19683 else
19684 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19685 }
19686
c906108c
SS
19687 switch (die->tag)
19688 {
19689 case DW_TAG_label:
e142c38c 19690 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19691 if (attr)
3e29f34a
MR
19692 {
19693 CORE_ADDR addr;
19694
19695 addr = attr_value_as_address (attr);
19696 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19697 SYMBOL_VALUE_ADDRESS (sym) = addr;
19698 }
0f5238ed
TT
19699 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19700 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19701 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19702 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19703 break;
19704 case DW_TAG_subprogram:
19705 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19706 finish_block. */
f1e6e072 19707 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19708 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19709 if ((attr2 && (DW_UNSND (attr2) != 0))
19710 || cu->language == language_ada)
c906108c 19711 {
2cfa0c8d
JB
19712 /* Subprograms marked external are stored as a global symbol.
19713 Ada subprograms, whether marked external or not, are always
19714 stored as a global symbol, because we want to be able to
19715 access them globally. For instance, we want to be able
19716 to break on a nested subprogram without having to
19717 specify the context. */
e37fd15a 19718 list_to_add = &global_symbols;
c906108c
SS
19719 }
19720 else
19721 {
e37fd15a 19722 list_to_add = cu->list_in_scope;
c906108c
SS
19723 }
19724 break;
edb3359d
DJ
19725 case DW_TAG_inlined_subroutine:
19726 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19727 finish_block. */
f1e6e072 19728 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19729 SYMBOL_INLINED (sym) = 1;
481860b3 19730 list_to_add = cu->list_in_scope;
edb3359d 19731 break;
34eaf542
TT
19732 case DW_TAG_template_value_param:
19733 suppress_add = 1;
19734 /* Fall through. */
72929c62 19735 case DW_TAG_constant:
c906108c 19736 case DW_TAG_variable:
254e6b9e 19737 case DW_TAG_member:
0963b4bd
MS
19738 /* Compilation with minimal debug info may result in
19739 variables with missing type entries. Change the
19740 misleading `void' type to something sensible. */
c906108c 19741 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19742 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19743
e142c38c 19744 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19745 /* In the case of DW_TAG_member, we should only be called for
19746 static const members. */
19747 if (die->tag == DW_TAG_member)
19748 {
3863f96c
DE
19749 /* dwarf2_add_field uses die_is_declaration,
19750 so we do the same. */
254e6b9e
DE
19751 gdb_assert (die_is_declaration (die, cu));
19752 gdb_assert (attr);
19753 }
c906108c
SS
19754 if (attr)
19755 {
e7c27a73 19756 dwarf2_const_value (attr, sym, cu);
e142c38c 19757 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19758 if (!suppress_add)
34eaf542
TT
19759 {
19760 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19761 list_to_add = &global_symbols;
34eaf542 19762 else
e37fd15a 19763 list_to_add = cu->list_in_scope;
34eaf542 19764 }
c906108c
SS
19765 break;
19766 }
e142c38c 19767 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19768 if (attr)
19769 {
e7c27a73 19770 var_decode_location (attr, sym, cu);
e142c38c 19771 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19772
19773 /* Fortran explicitly imports any global symbols to the local
19774 scope by DW_TAG_common_block. */
19775 if (cu->language == language_fortran && die->parent
19776 && die->parent->tag == DW_TAG_common_block)
19777 attr2 = NULL;
19778
caac4577
JG
19779 if (SYMBOL_CLASS (sym) == LOC_STATIC
19780 && SYMBOL_VALUE_ADDRESS (sym) == 0
19781 && !dwarf2_per_objfile->has_section_at_zero)
19782 {
19783 /* When a static variable is eliminated by the linker,
19784 the corresponding debug information is not stripped
19785 out, but the variable address is set to null;
19786 do not add such variables into symbol table. */
19787 }
19788 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19789 {
f55ee35c
JK
19790 /* Workaround gfortran PR debug/40040 - it uses
19791 DW_AT_location for variables in -fPIC libraries which may
19792 get overriden by other libraries/executable and get
19793 a different address. Resolve it by the minimal symbol
19794 which may come from inferior's executable using copy
19795 relocation. Make this workaround only for gfortran as for
19796 other compilers GDB cannot guess the minimal symbol
19797 Fortran mangling kind. */
19798 if (cu->language == language_fortran && die->parent
19799 && die->parent->tag == DW_TAG_module
19800 && cu->producer
28586665 19801 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19802 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19803
1c809c68
TT
19804 /* A variable with DW_AT_external is never static,
19805 but it may be block-scoped. */
19806 list_to_add = (cu->list_in_scope == &file_symbols
19807 ? &global_symbols : cu->list_in_scope);
1c809c68 19808 }
c906108c 19809 else
e37fd15a 19810 list_to_add = cu->list_in_scope;
c906108c
SS
19811 }
19812 else
19813 {
19814 /* We do not know the address of this symbol.
c5aa993b
JM
19815 If it is an external symbol and we have type information
19816 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19817 The address of the variable will then be determined from
19818 the minimal symbol table whenever the variable is
19819 referenced. */
e142c38c 19820 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19821
19822 /* Fortran explicitly imports any global symbols to the local
19823 scope by DW_TAG_common_block. */
19824 if (cu->language == language_fortran && die->parent
19825 && die->parent->tag == DW_TAG_common_block)
19826 {
19827 /* SYMBOL_CLASS doesn't matter here because
19828 read_common_block is going to reset it. */
19829 if (!suppress_add)
19830 list_to_add = cu->list_in_scope;
19831 }
19832 else if (attr2 && (DW_UNSND (attr2) != 0)
19833 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19834 {
0fe7935b
DJ
19835 /* A variable with DW_AT_external is never static, but it
19836 may be block-scoped. */
19837 list_to_add = (cu->list_in_scope == &file_symbols
19838 ? &global_symbols : cu->list_in_scope);
19839
f1e6e072 19840 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19841 }
442ddf59
JK
19842 else if (!die_is_declaration (die, cu))
19843 {
19844 /* Use the default LOC_OPTIMIZED_OUT class. */
19845 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19846 if (!suppress_add)
19847 list_to_add = cu->list_in_scope;
442ddf59 19848 }
c906108c
SS
19849 }
19850 break;
19851 case DW_TAG_formal_parameter:
edb3359d
DJ
19852 /* If we are inside a function, mark this as an argument. If
19853 not, we might be looking at an argument to an inlined function
19854 when we do not have enough information to show inlined frames;
19855 pretend it's a local variable in that case so that the user can
19856 still see it. */
19857 if (context_stack_depth > 0
19858 && context_stack[context_stack_depth - 1].name != NULL)
19859 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19860 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19861 if (attr)
19862 {
e7c27a73 19863 var_decode_location (attr, sym, cu);
c906108c 19864 }
e142c38c 19865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19866 if (attr)
19867 {
e7c27a73 19868 dwarf2_const_value (attr, sym, cu);
c906108c 19869 }
f346a30d 19870
e37fd15a 19871 list_to_add = cu->list_in_scope;
c906108c
SS
19872 break;
19873 case DW_TAG_unspecified_parameters:
19874 /* From varargs functions; gdb doesn't seem to have any
19875 interest in this information, so just ignore it for now.
19876 (FIXME?) */
19877 break;
34eaf542
TT
19878 case DW_TAG_template_type_param:
19879 suppress_add = 1;
19880 /* Fall through. */
c906108c 19881 case DW_TAG_class_type:
680b30c7 19882 case DW_TAG_interface_type:
c906108c
SS
19883 case DW_TAG_structure_type:
19884 case DW_TAG_union_type:
72019c9c 19885 case DW_TAG_set_type:
c906108c 19886 case DW_TAG_enumeration_type:
f1e6e072 19887 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19889
63d06c5c 19890 {
9c37b5ae 19891 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19892 really ever be static objects: otherwise, if you try
19893 to, say, break of a class's method and you're in a file
19894 which doesn't mention that class, it won't work unless
19895 the check for all static symbols in lookup_symbol_aux
19896 saves you. See the OtherFileClass tests in
19897 gdb.c++/namespace.exp. */
19898
e37fd15a 19899 if (!suppress_add)
34eaf542 19900 {
34eaf542 19901 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19902 && cu->language == language_cplus
34eaf542 19903 ? &global_symbols : cu->list_in_scope);
63d06c5c 19904
64382290 19905 /* The semantics of C++ state that "struct foo {
9c37b5ae 19906 ... }" also defines a typedef for "foo". */
64382290 19907 if (cu->language == language_cplus
45280282 19908 || cu->language == language_ada
c44af4eb
TT
19909 || cu->language == language_d
19910 || cu->language == language_rust)
64382290
TT
19911 {
19912 /* The symbol's name is already allocated along
19913 with this objfile, so we don't need to
19914 duplicate it for the type. */
19915 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19916 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19917 }
63d06c5c
DC
19918 }
19919 }
c906108c
SS
19920 break;
19921 case DW_TAG_typedef:
f1e6e072 19922 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19923 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19924 list_to_add = cu->list_in_scope;
63d06c5c 19925 break;
c906108c 19926 case DW_TAG_base_type:
a02abb62 19927 case DW_TAG_subrange_type:
f1e6e072 19928 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19929 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19930 list_to_add = cu->list_in_scope;
c906108c
SS
19931 break;
19932 case DW_TAG_enumerator:
e142c38c 19933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19934 if (attr)
19935 {
e7c27a73 19936 dwarf2_const_value (attr, sym, cu);
c906108c 19937 }
63d06c5c
DC
19938 {
19939 /* NOTE: carlton/2003-11-10: See comment above in the
19940 DW_TAG_class_type, etc. block. */
19941
e142c38c 19942 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19943 && cu->language == language_cplus
e142c38c 19944 ? &global_symbols : cu->list_in_scope);
63d06c5c 19945 }
c906108c 19946 break;
74921315 19947 case DW_TAG_imported_declaration:
5c4e30ca 19948 case DW_TAG_namespace:
f1e6e072 19949 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19950 list_to_add = &global_symbols;
5c4e30ca 19951 break;
530e8392
KB
19952 case DW_TAG_module:
19953 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19954 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19955 list_to_add = &global_symbols;
19956 break;
4357ac6c 19957 case DW_TAG_common_block:
f1e6e072 19958 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19959 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19960 add_symbol_to_list (sym, cu->list_in_scope);
19961 break;
c906108c
SS
19962 default:
19963 /* Not a tag we recognize. Hopefully we aren't processing
19964 trash data, but since we must specifically ignore things
19965 we don't recognize, there is nothing else we should do at
0963b4bd 19966 this point. */
e2e0b3e5 19967 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19968 dwarf_tag_name (die->tag));
c906108c
SS
19969 break;
19970 }
df8a16a1 19971
e37fd15a
SW
19972 if (suppress_add)
19973 {
19974 sym->hash_next = objfile->template_symbols;
19975 objfile->template_symbols = sym;
19976 list_to_add = NULL;
19977 }
19978
19979 if (list_to_add != NULL)
19980 add_symbol_to_list (sym, list_to_add);
19981
df8a16a1
DJ
19982 /* For the benefit of old versions of GCC, check for anonymous
19983 namespaces based on the demangled name. */
4d4ec4e5 19984 if (!cu->processing_has_namespace_info
94af9270 19985 && cu->language == language_cplus)
a10964d1 19986 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19987 }
19988 return (sym);
19989}
19990
34eaf542
TT
19991/* A wrapper for new_symbol_full that always allocates a new symbol. */
19992
19993static struct symbol *
19994new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19995{
19996 return new_symbol_full (die, type, cu, NULL);
19997}
19998
98bfdba5
PA
19999/* Given an attr with a DW_FORM_dataN value in host byte order,
20000 zero-extend it as appropriate for the symbol's type. The DWARF
20001 standard (v4) is not entirely clear about the meaning of using
20002 DW_FORM_dataN for a constant with a signed type, where the type is
20003 wider than the data. The conclusion of a discussion on the DWARF
20004 list was that this is unspecified. We choose to always zero-extend
20005 because that is the interpretation long in use by GCC. */
c906108c 20006
98bfdba5 20007static gdb_byte *
ff39bb5e 20008dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20009 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20010{
e7c27a73 20011 struct objfile *objfile = cu->objfile;
e17a4113
UW
20012 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20013 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20014 LONGEST l = DW_UNSND (attr);
20015
20016 if (bits < sizeof (*value) * 8)
20017 {
20018 l &= ((LONGEST) 1 << bits) - 1;
20019 *value = l;
20020 }
20021 else if (bits == sizeof (*value) * 8)
20022 *value = l;
20023 else
20024 {
224c3ddb 20025 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20026 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20027 return bytes;
20028 }
20029
20030 return NULL;
20031}
20032
20033/* Read a constant value from an attribute. Either set *VALUE, or if
20034 the value does not fit in *VALUE, set *BYTES - either already
20035 allocated on the objfile obstack, or newly allocated on OBSTACK,
20036 or, set *BATON, if we translated the constant to a location
20037 expression. */
20038
20039static void
ff39bb5e 20040dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20041 const char *name, struct obstack *obstack,
20042 struct dwarf2_cu *cu,
d521ce57 20043 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20044 struct dwarf2_locexpr_baton **baton)
20045{
20046 struct objfile *objfile = cu->objfile;
20047 struct comp_unit_head *cu_header = &cu->header;
c906108c 20048 struct dwarf_block *blk;
98bfdba5
PA
20049 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20051
20052 *value = 0;
20053 *bytes = NULL;
20054 *baton = NULL;
c906108c
SS
20055
20056 switch (attr->form)
20057 {
20058 case DW_FORM_addr:
3019eac3 20059 case DW_FORM_GNU_addr_index:
ac56253d 20060 {
ac56253d
TT
20061 gdb_byte *data;
20062
98bfdba5
PA
20063 if (TYPE_LENGTH (type) != cu_header->addr_size)
20064 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20065 cu_header->addr_size,
98bfdba5 20066 TYPE_LENGTH (type));
ac56253d
TT
20067 /* Symbols of this form are reasonably rare, so we just
20068 piggyback on the existing location code rather than writing
20069 a new implementation of symbol_computed_ops. */
8d749320 20070 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20071 (*baton)->per_cu = cu->per_cu;
20072 gdb_assert ((*baton)->per_cu);
ac56253d 20073
98bfdba5 20074 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20075 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20076 (*baton)->data = data;
ac56253d
TT
20077
20078 data[0] = DW_OP_addr;
20079 store_unsigned_integer (&data[1], cu_header->addr_size,
20080 byte_order, DW_ADDR (attr));
20081 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20082 }
c906108c 20083 break;
4ac36638 20084 case DW_FORM_string:
93b5768b 20085 case DW_FORM_strp:
3019eac3 20086 case DW_FORM_GNU_str_index:
36586728 20087 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20088 /* DW_STRING is already allocated on the objfile obstack, point
20089 directly to it. */
d521ce57 20090 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20091 break;
c906108c
SS
20092 case DW_FORM_block1:
20093 case DW_FORM_block2:
20094 case DW_FORM_block4:
20095 case DW_FORM_block:
2dc7f7b3 20096 case DW_FORM_exprloc:
0224619f 20097 case DW_FORM_data16:
c906108c 20098 blk = DW_BLOCK (attr);
98bfdba5
PA
20099 if (TYPE_LENGTH (type) != blk->size)
20100 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20101 TYPE_LENGTH (type));
20102 *bytes = blk->data;
c906108c 20103 break;
2df3850c
JM
20104
20105 /* The DW_AT_const_value attributes are supposed to carry the
20106 symbol's value "represented as it would be on the target
20107 architecture." By the time we get here, it's already been
20108 converted to host endianness, so we just need to sign- or
20109 zero-extend it as appropriate. */
20110 case DW_FORM_data1:
3aef2284 20111 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20112 break;
c906108c 20113 case DW_FORM_data2:
3aef2284 20114 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20115 break;
c906108c 20116 case DW_FORM_data4:
3aef2284 20117 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20118 break;
c906108c 20119 case DW_FORM_data8:
3aef2284 20120 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20121 break;
20122
c906108c 20123 case DW_FORM_sdata:
663c44ac 20124 case DW_FORM_implicit_const:
98bfdba5 20125 *value = DW_SND (attr);
2df3850c
JM
20126 break;
20127
c906108c 20128 case DW_FORM_udata:
98bfdba5 20129 *value = DW_UNSND (attr);
c906108c 20130 break;
2df3850c 20131
c906108c 20132 default:
4d3c2250 20133 complaint (&symfile_complaints,
e2e0b3e5 20134 _("unsupported const value attribute form: '%s'"),
4d3c2250 20135 dwarf_form_name (attr->form));
98bfdba5 20136 *value = 0;
c906108c
SS
20137 break;
20138 }
20139}
20140
2df3850c 20141
98bfdba5
PA
20142/* Copy constant value from an attribute to a symbol. */
20143
2df3850c 20144static void
ff39bb5e 20145dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20146 struct dwarf2_cu *cu)
2df3850c 20147{
98bfdba5 20148 struct objfile *objfile = cu->objfile;
12df843f 20149 LONGEST value;
d521ce57 20150 const gdb_byte *bytes;
98bfdba5 20151 struct dwarf2_locexpr_baton *baton;
2df3850c 20152
98bfdba5
PA
20153 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20154 SYMBOL_PRINT_NAME (sym),
20155 &objfile->objfile_obstack, cu,
20156 &value, &bytes, &baton);
2df3850c 20157
98bfdba5
PA
20158 if (baton != NULL)
20159 {
98bfdba5 20160 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20161 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20162 }
20163 else if (bytes != NULL)
20164 {
20165 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20166 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20167 }
20168 else
20169 {
20170 SYMBOL_VALUE (sym) = value;
f1e6e072 20171 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20172 }
2df3850c
JM
20173}
20174
c906108c
SS
20175/* Return the type of the die in question using its DW_AT_type attribute. */
20176
20177static struct type *
e7c27a73 20178die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20179{
c906108c 20180 struct attribute *type_attr;
c906108c 20181
e142c38c 20182 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20183 if (!type_attr)
20184 {
20185 /* A missing DW_AT_type represents a void type. */
46bf5051 20186 return objfile_type (cu->objfile)->builtin_void;
c906108c 20187 }
348e048f 20188
673bfd45 20189 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20190}
20191
b4ba55a1
JB
20192/* True iff CU's producer generates GNAT Ada auxiliary information
20193 that allows to find parallel types through that information instead
20194 of having to do expensive parallel lookups by type name. */
20195
20196static int
20197need_gnat_info (struct dwarf2_cu *cu)
20198{
20199 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20200 of GNAT produces this auxiliary information, without any indication
20201 that it is produced. Part of enhancing the FSF version of GNAT
20202 to produce that information will be to put in place an indicator
20203 that we can use in order to determine whether the descriptive type
20204 info is available or not. One suggestion that has been made is
20205 to use a new attribute, attached to the CU die. For now, assume
20206 that the descriptive type info is not available. */
20207 return 0;
20208}
20209
b4ba55a1
JB
20210/* Return the auxiliary type of the die in question using its
20211 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20212 attribute is not present. */
20213
20214static struct type *
20215die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20216{
b4ba55a1 20217 struct attribute *type_attr;
b4ba55a1
JB
20218
20219 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20220 if (!type_attr)
20221 return NULL;
20222
673bfd45 20223 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20224}
20225
20226/* If DIE has a descriptive_type attribute, then set the TYPE's
20227 descriptive type accordingly. */
20228
20229static void
20230set_descriptive_type (struct type *type, struct die_info *die,
20231 struct dwarf2_cu *cu)
20232{
20233 struct type *descriptive_type = die_descriptive_type (die, cu);
20234
20235 if (descriptive_type)
20236 {
20237 ALLOCATE_GNAT_AUX_TYPE (type);
20238 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20239 }
20240}
20241
c906108c
SS
20242/* Return the containing type of the die in question using its
20243 DW_AT_containing_type attribute. */
20244
20245static struct type *
e7c27a73 20246die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20247{
c906108c 20248 struct attribute *type_attr;
c906108c 20249
e142c38c 20250 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20251 if (!type_attr)
20252 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 20253 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 20254
673bfd45 20255 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20256}
20257
ac9ec31b
DE
20258/* Return an error marker type to use for the ill formed type in DIE/CU. */
20259
20260static struct type *
20261build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20262{
20263 struct objfile *objfile = dwarf2_per_objfile->objfile;
20264 char *message, *saved;
20265
20266 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 20267 objfile_name (objfile),
9c541725
PA
20268 to_underlying (cu->header.sect_off),
20269 to_underlying (die->sect_off));
224c3ddb
SM
20270 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20271 message, strlen (message));
ac9ec31b
DE
20272 xfree (message);
20273
19f392bc 20274 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20275}
20276
673bfd45 20277/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20278 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20279 DW_AT_containing_type.
673bfd45
DE
20280 If there is no type substitute an error marker. */
20281
c906108c 20282static struct type *
ff39bb5e 20283lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20284 struct dwarf2_cu *cu)
c906108c 20285{
bb5ed363 20286 struct objfile *objfile = cu->objfile;
f792889a
DJ
20287 struct type *this_type;
20288
ac9ec31b
DE
20289 gdb_assert (attr->name == DW_AT_type
20290 || attr->name == DW_AT_GNAT_descriptive_type
20291 || attr->name == DW_AT_containing_type);
20292
673bfd45
DE
20293 /* First see if we have it cached. */
20294
36586728
TT
20295 if (attr->form == DW_FORM_GNU_ref_alt)
20296 {
20297 struct dwarf2_per_cu_data *per_cu;
9c541725 20298 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20299
9c541725
PA
20300 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20301 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20302 }
7771576e 20303 else if (attr_form_is_ref (attr))
673bfd45 20304 {
9c541725 20305 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20306
9c541725 20307 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20308 }
55f1336d 20309 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20310 {
ac9ec31b 20311 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20312
ac9ec31b 20313 return get_signatured_type (die, signature, cu);
673bfd45
DE
20314 }
20315 else
20316 {
ac9ec31b
DE
20317 complaint (&symfile_complaints,
20318 _("Dwarf Error: Bad type attribute %s in DIE"
20319 " at 0x%x [in module %s]"),
9c541725 20320 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 20321 objfile_name (objfile));
ac9ec31b 20322 return build_error_marker_type (cu, die);
673bfd45
DE
20323 }
20324
20325 /* If not cached we need to read it in. */
20326
20327 if (this_type == NULL)
20328 {
ac9ec31b 20329 struct die_info *type_die = NULL;
673bfd45
DE
20330 struct dwarf2_cu *type_cu = cu;
20331
7771576e 20332 if (attr_form_is_ref (attr))
ac9ec31b
DE
20333 type_die = follow_die_ref (die, attr, &type_cu);
20334 if (type_die == NULL)
20335 return build_error_marker_type (cu, die);
20336 /* If we find the type now, it's probably because the type came
3019eac3
DE
20337 from an inter-CU reference and the type's CU got expanded before
20338 ours. */
ac9ec31b 20339 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20340 }
20341
20342 /* If we still don't have a type use an error marker. */
20343
20344 if (this_type == NULL)
ac9ec31b 20345 return build_error_marker_type (cu, die);
673bfd45 20346
f792889a 20347 return this_type;
c906108c
SS
20348}
20349
673bfd45
DE
20350/* Return the type in DIE, CU.
20351 Returns NULL for invalid types.
20352
02142a6c 20353 This first does a lookup in die_type_hash,
673bfd45
DE
20354 and only reads the die in if necessary.
20355
20356 NOTE: This can be called when reading in partial or full symbols. */
20357
f792889a 20358static struct type *
e7c27a73 20359read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20360{
f792889a
DJ
20361 struct type *this_type;
20362
20363 this_type = get_die_type (die, cu);
20364 if (this_type)
20365 return this_type;
20366
673bfd45
DE
20367 return read_type_die_1 (die, cu);
20368}
20369
20370/* Read the type in DIE, CU.
20371 Returns NULL for invalid types. */
20372
20373static struct type *
20374read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20375{
20376 struct type *this_type = NULL;
20377
c906108c
SS
20378 switch (die->tag)
20379 {
20380 case DW_TAG_class_type:
680b30c7 20381 case DW_TAG_interface_type:
c906108c
SS
20382 case DW_TAG_structure_type:
20383 case DW_TAG_union_type:
f792889a 20384 this_type = read_structure_type (die, cu);
c906108c
SS
20385 break;
20386 case DW_TAG_enumeration_type:
f792889a 20387 this_type = read_enumeration_type (die, cu);
c906108c
SS
20388 break;
20389 case DW_TAG_subprogram:
20390 case DW_TAG_subroutine_type:
edb3359d 20391 case DW_TAG_inlined_subroutine:
f792889a 20392 this_type = read_subroutine_type (die, cu);
c906108c
SS
20393 break;
20394 case DW_TAG_array_type:
f792889a 20395 this_type = read_array_type (die, cu);
c906108c 20396 break;
72019c9c 20397 case DW_TAG_set_type:
f792889a 20398 this_type = read_set_type (die, cu);
72019c9c 20399 break;
c906108c 20400 case DW_TAG_pointer_type:
f792889a 20401 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20402 break;
20403 case DW_TAG_ptr_to_member_type:
f792889a 20404 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20405 break;
20406 case DW_TAG_reference_type:
4297a3f0
AV
20407 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20408 break;
20409 case DW_TAG_rvalue_reference_type:
20410 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20411 break;
20412 case DW_TAG_const_type:
f792889a 20413 this_type = read_tag_const_type (die, cu);
c906108c
SS
20414 break;
20415 case DW_TAG_volatile_type:
f792889a 20416 this_type = read_tag_volatile_type (die, cu);
c906108c 20417 break;
06d66ee9
TT
20418 case DW_TAG_restrict_type:
20419 this_type = read_tag_restrict_type (die, cu);
20420 break;
c906108c 20421 case DW_TAG_string_type:
f792889a 20422 this_type = read_tag_string_type (die, cu);
c906108c
SS
20423 break;
20424 case DW_TAG_typedef:
f792889a 20425 this_type = read_typedef (die, cu);
c906108c 20426 break;
a02abb62 20427 case DW_TAG_subrange_type:
f792889a 20428 this_type = read_subrange_type (die, cu);
a02abb62 20429 break;
c906108c 20430 case DW_TAG_base_type:
f792889a 20431 this_type = read_base_type (die, cu);
c906108c 20432 break;
81a17f79 20433 case DW_TAG_unspecified_type:
f792889a 20434 this_type = read_unspecified_type (die, cu);
81a17f79 20435 break;
0114d602
DJ
20436 case DW_TAG_namespace:
20437 this_type = read_namespace_type (die, cu);
20438 break;
f55ee35c
JK
20439 case DW_TAG_module:
20440 this_type = read_module_type (die, cu);
20441 break;
a2c2acaf
MW
20442 case DW_TAG_atomic_type:
20443 this_type = read_tag_atomic_type (die, cu);
20444 break;
c906108c 20445 default:
3e43a32a
MS
20446 complaint (&symfile_complaints,
20447 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 20448 dwarf_tag_name (die->tag));
c906108c
SS
20449 break;
20450 }
63d06c5c 20451
f792889a 20452 return this_type;
63d06c5c
DC
20453}
20454
abc72ce4
DE
20455/* See if we can figure out if the class lives in a namespace. We do
20456 this by looking for a member function; its demangled name will
20457 contain namespace info, if there is any.
20458 Return the computed name or NULL.
20459 Space for the result is allocated on the objfile's obstack.
20460 This is the full-die version of guess_partial_die_structure_name.
20461 In this case we know DIE has no useful parent. */
20462
20463static char *
20464guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20465{
20466 struct die_info *spec_die;
20467 struct dwarf2_cu *spec_cu;
20468 struct die_info *child;
20469
20470 spec_cu = cu;
20471 spec_die = die_specification (die, &spec_cu);
20472 if (spec_die != NULL)
20473 {
20474 die = spec_die;
20475 cu = spec_cu;
20476 }
20477
20478 for (child = die->child;
20479 child != NULL;
20480 child = child->sibling)
20481 {
20482 if (child->tag == DW_TAG_subprogram)
20483 {
73b9be8b 20484 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20485
7d45c7c3 20486 if (linkage_name != NULL)
abc72ce4
DE
20487 {
20488 char *actual_name
20489 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 20490 linkage_name);
abc72ce4
DE
20491 char *name = NULL;
20492
20493 if (actual_name != NULL)
20494 {
15d034d0 20495 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20496
20497 if (die_name != NULL
20498 && strcmp (die_name, actual_name) != 0)
20499 {
20500 /* Strip off the class name from the full name.
20501 We want the prefix. */
20502 int die_name_len = strlen (die_name);
20503 int actual_name_len = strlen (actual_name);
20504
20505 /* Test for '::' as a sanity check. */
20506 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20507 && actual_name[actual_name_len
20508 - die_name_len - 1] == ':')
224c3ddb
SM
20509 name = (char *) obstack_copy0 (
20510 &cu->objfile->per_bfd->storage_obstack,
20511 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20512 }
20513 }
20514 xfree (actual_name);
20515 return name;
20516 }
20517 }
20518 }
20519
20520 return NULL;
20521}
20522
96408a79
SA
20523/* GCC might emit a nameless typedef that has a linkage name. Determine the
20524 prefix part in such case. See
20525 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20526
a121b7c1 20527static const char *
96408a79
SA
20528anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20529{
20530 struct attribute *attr;
e6a959d6 20531 const char *base;
96408a79
SA
20532
20533 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20534 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20535 return NULL;
20536
7d45c7c3 20537 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20538 return NULL;
20539
73b9be8b 20540 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20541 if (attr == NULL || DW_STRING (attr) == NULL)
20542 return NULL;
20543
20544 /* dwarf2_name had to be already called. */
20545 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20546
20547 /* Strip the base name, keep any leading namespaces/classes. */
20548 base = strrchr (DW_STRING (attr), ':');
20549 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20550 return "";
20551
224c3ddb
SM
20552 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20553 DW_STRING (attr),
20554 &base[-1] - DW_STRING (attr));
96408a79
SA
20555}
20556
fdde2d81 20557/* Return the name of the namespace/class that DIE is defined within,
0114d602 20558 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20559
0114d602
DJ
20560 For example, if we're within the method foo() in the following
20561 code:
20562
20563 namespace N {
20564 class C {
20565 void foo () {
20566 }
20567 };
20568 }
20569
20570 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20571
0d5cff50 20572static const char *
e142c38c 20573determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20574{
0114d602
DJ
20575 struct die_info *parent, *spec_die;
20576 struct dwarf2_cu *spec_cu;
20577 struct type *parent_type;
a121b7c1 20578 const char *retval;
63d06c5c 20579
9c37b5ae 20580 if (cu->language != language_cplus
c44af4eb
TT
20581 && cu->language != language_fortran && cu->language != language_d
20582 && cu->language != language_rust)
0114d602
DJ
20583 return "";
20584
96408a79
SA
20585 retval = anonymous_struct_prefix (die, cu);
20586 if (retval)
20587 return retval;
20588
0114d602
DJ
20589 /* We have to be careful in the presence of DW_AT_specification.
20590 For example, with GCC 3.4, given the code
20591
20592 namespace N {
20593 void foo() {
20594 // Definition of N::foo.
20595 }
20596 }
20597
20598 then we'll have a tree of DIEs like this:
20599
20600 1: DW_TAG_compile_unit
20601 2: DW_TAG_namespace // N
20602 3: DW_TAG_subprogram // declaration of N::foo
20603 4: DW_TAG_subprogram // definition of N::foo
20604 DW_AT_specification // refers to die #3
20605
20606 Thus, when processing die #4, we have to pretend that we're in
20607 the context of its DW_AT_specification, namely the contex of die
20608 #3. */
20609 spec_cu = cu;
20610 spec_die = die_specification (die, &spec_cu);
20611 if (spec_die == NULL)
20612 parent = die->parent;
20613 else
63d06c5c 20614 {
0114d602
DJ
20615 parent = spec_die->parent;
20616 cu = spec_cu;
63d06c5c 20617 }
0114d602
DJ
20618
20619 if (parent == NULL)
20620 return "";
98bfdba5
PA
20621 else if (parent->building_fullname)
20622 {
20623 const char *name;
20624 const char *parent_name;
20625
20626 /* It has been seen on RealView 2.2 built binaries,
20627 DW_TAG_template_type_param types actually _defined_ as
20628 children of the parent class:
20629
20630 enum E {};
20631 template class <class Enum> Class{};
20632 Class<enum E> class_e;
20633
20634 1: DW_TAG_class_type (Class)
20635 2: DW_TAG_enumeration_type (E)
20636 3: DW_TAG_enumerator (enum1:0)
20637 3: DW_TAG_enumerator (enum2:1)
20638 ...
20639 2: DW_TAG_template_type_param
20640 DW_AT_type DW_FORM_ref_udata (E)
20641
20642 Besides being broken debug info, it can put GDB into an
20643 infinite loop. Consider:
20644
20645 When we're building the full name for Class<E>, we'll start
20646 at Class, and go look over its template type parameters,
20647 finding E. We'll then try to build the full name of E, and
20648 reach here. We're now trying to build the full name of E,
20649 and look over the parent DIE for containing scope. In the
20650 broken case, if we followed the parent DIE of E, we'd again
20651 find Class, and once again go look at its template type
20652 arguments, etc., etc. Simply don't consider such parent die
20653 as source-level parent of this die (it can't be, the language
20654 doesn't allow it), and break the loop here. */
20655 name = dwarf2_name (die, cu);
20656 parent_name = dwarf2_name (parent, cu);
20657 complaint (&symfile_complaints,
20658 _("template param type '%s' defined within parent '%s'"),
20659 name ? name : "<unknown>",
20660 parent_name ? parent_name : "<unknown>");
20661 return "";
20662 }
63d06c5c 20663 else
0114d602
DJ
20664 switch (parent->tag)
20665 {
63d06c5c 20666 case DW_TAG_namespace:
0114d602 20667 parent_type = read_type_die (parent, cu);
acebe513
UW
20668 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20669 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20670 Work around this problem here. */
20671 if (cu->language == language_cplus
20672 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20673 return "";
0114d602
DJ
20674 /* We give a name to even anonymous namespaces. */
20675 return TYPE_TAG_NAME (parent_type);
63d06c5c 20676 case DW_TAG_class_type:
680b30c7 20677 case DW_TAG_interface_type:
63d06c5c 20678 case DW_TAG_structure_type:
0114d602 20679 case DW_TAG_union_type:
f55ee35c 20680 case DW_TAG_module:
0114d602
DJ
20681 parent_type = read_type_die (parent, cu);
20682 if (TYPE_TAG_NAME (parent_type) != NULL)
20683 return TYPE_TAG_NAME (parent_type);
20684 else
20685 /* An anonymous structure is only allowed non-static data
20686 members; no typedefs, no member functions, et cetera.
20687 So it does not need a prefix. */
20688 return "";
abc72ce4 20689 case DW_TAG_compile_unit:
95554aad 20690 case DW_TAG_partial_unit:
abc72ce4
DE
20691 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20692 if (cu->language == language_cplus
8b70b953 20693 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20694 && die->child != NULL
20695 && (die->tag == DW_TAG_class_type
20696 || die->tag == DW_TAG_structure_type
20697 || die->tag == DW_TAG_union_type))
20698 {
20699 char *name = guess_full_die_structure_name (die, cu);
20700 if (name != NULL)
20701 return name;
20702 }
20703 return "";
3d567982
TT
20704 case DW_TAG_enumeration_type:
20705 parent_type = read_type_die (parent, cu);
20706 if (TYPE_DECLARED_CLASS (parent_type))
20707 {
20708 if (TYPE_TAG_NAME (parent_type) != NULL)
20709 return TYPE_TAG_NAME (parent_type);
20710 return "";
20711 }
20712 /* Fall through. */
63d06c5c 20713 default:
8176b9b8 20714 return determine_prefix (parent, cu);
63d06c5c 20715 }
63d06c5c
DC
20716}
20717
3e43a32a
MS
20718/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20719 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20720 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20721 an obconcat, otherwise allocate storage for the result. The CU argument is
20722 used to determine the language and hence, the appropriate separator. */
987504bb 20723
f55ee35c 20724#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20725
20726static char *
f55ee35c
JK
20727typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20728 int physname, struct dwarf2_cu *cu)
63d06c5c 20729{
f55ee35c 20730 const char *lead = "";
5c315b68 20731 const char *sep;
63d06c5c 20732
3e43a32a
MS
20733 if (suffix == NULL || suffix[0] == '\0'
20734 || prefix == NULL || prefix[0] == '\0')
987504bb 20735 sep = "";
45280282
IB
20736 else if (cu->language == language_d)
20737 {
20738 /* For D, the 'main' function could be defined in any module, but it
20739 should never be prefixed. */
20740 if (strcmp (suffix, "D main") == 0)
20741 {
20742 prefix = "";
20743 sep = "";
20744 }
20745 else
20746 sep = ".";
20747 }
f55ee35c
JK
20748 else if (cu->language == language_fortran && physname)
20749 {
20750 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20751 DW_AT_MIPS_linkage_name is preferred and used instead. */
20752
20753 lead = "__";
20754 sep = "_MOD_";
20755 }
987504bb
JJ
20756 else
20757 sep = "::";
63d06c5c 20758
6dd47d34
DE
20759 if (prefix == NULL)
20760 prefix = "";
20761 if (suffix == NULL)
20762 suffix = "";
20763
987504bb
JJ
20764 if (obs == NULL)
20765 {
3e43a32a 20766 char *retval
224c3ddb
SM
20767 = ((char *)
20768 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20769
f55ee35c
JK
20770 strcpy (retval, lead);
20771 strcat (retval, prefix);
6dd47d34
DE
20772 strcat (retval, sep);
20773 strcat (retval, suffix);
63d06c5c
DC
20774 return retval;
20775 }
987504bb
JJ
20776 else
20777 {
20778 /* We have an obstack. */
f55ee35c 20779 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20780 }
63d06c5c
DC
20781}
20782
c906108c
SS
20783/* Return sibling of die, NULL if no sibling. */
20784
f9aca02d 20785static struct die_info *
fba45db2 20786sibling_die (struct die_info *die)
c906108c 20787{
639d11d3 20788 return die->sibling;
c906108c
SS
20789}
20790
71c25dea
TT
20791/* Get name of a die, return NULL if not found. */
20792
15d034d0
TT
20793static const char *
20794dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20795 struct obstack *obstack)
20796{
20797 if (name && cu->language == language_cplus)
20798 {
2f408ecb 20799 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20800
2f408ecb 20801 if (!canon_name.empty ())
71c25dea 20802 {
2f408ecb
PA
20803 if (canon_name != name)
20804 name = (const char *) obstack_copy0 (obstack,
20805 canon_name.c_str (),
20806 canon_name.length ());
71c25dea
TT
20807 }
20808 }
20809
20810 return name;
c906108c
SS
20811}
20812
96553a0c
DE
20813/* Get name of a die, return NULL if not found.
20814 Anonymous namespaces are converted to their magic string. */
9219021c 20815
15d034d0 20816static const char *
e142c38c 20817dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20818{
20819 struct attribute *attr;
20820
e142c38c 20821 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20822 if ((!attr || !DW_STRING (attr))
96553a0c 20823 && die->tag != DW_TAG_namespace
53832f31
TT
20824 && die->tag != DW_TAG_class_type
20825 && die->tag != DW_TAG_interface_type
20826 && die->tag != DW_TAG_structure_type
20827 && die->tag != DW_TAG_union_type)
71c25dea
TT
20828 return NULL;
20829
20830 switch (die->tag)
20831 {
20832 case DW_TAG_compile_unit:
95554aad 20833 case DW_TAG_partial_unit:
71c25dea
TT
20834 /* Compilation units have a DW_AT_name that is a filename, not
20835 a source language identifier. */
20836 case DW_TAG_enumeration_type:
20837 case DW_TAG_enumerator:
20838 /* These tags always have simple identifiers already; no need
20839 to canonicalize them. */
20840 return DW_STRING (attr);
907af001 20841
96553a0c
DE
20842 case DW_TAG_namespace:
20843 if (attr != NULL && DW_STRING (attr) != NULL)
20844 return DW_STRING (attr);
20845 return CP_ANONYMOUS_NAMESPACE_STR;
20846
907af001
UW
20847 case DW_TAG_class_type:
20848 case DW_TAG_interface_type:
20849 case DW_TAG_structure_type:
20850 case DW_TAG_union_type:
20851 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20852 structures or unions. These were of the form "._%d" in GCC 4.1,
20853 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20854 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20855 if (attr && DW_STRING (attr)
61012eef
GB
20856 && (startswith (DW_STRING (attr), "._")
20857 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20858 return NULL;
53832f31
TT
20859
20860 /* GCC might emit a nameless typedef that has a linkage name. See
20861 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20862 if (!attr || DW_STRING (attr) == NULL)
20863 {
df5c6c50 20864 char *demangled = NULL;
53832f31 20865
73b9be8b 20866 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20867 if (attr == NULL || DW_STRING (attr) == NULL)
20868 return NULL;
20869
df5c6c50
JK
20870 /* Avoid demangling DW_STRING (attr) the second time on a second
20871 call for the same DIE. */
20872 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20873 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20874
20875 if (demangled)
20876 {
e6a959d6 20877 const char *base;
96408a79 20878
53832f31 20879 /* FIXME: we already did this for the partial symbol... */
34a68019 20880 DW_STRING (attr)
224c3ddb
SM
20881 = ((const char *)
20882 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20883 demangled, strlen (demangled)));
53832f31
TT
20884 DW_STRING_IS_CANONICAL (attr) = 1;
20885 xfree (demangled);
96408a79
SA
20886
20887 /* Strip any leading namespaces/classes, keep only the base name.
20888 DW_AT_name for named DIEs does not contain the prefixes. */
20889 base = strrchr (DW_STRING (attr), ':');
20890 if (base && base > DW_STRING (attr) && base[-1] == ':')
20891 return &base[1];
20892 else
20893 return DW_STRING (attr);
53832f31
TT
20894 }
20895 }
907af001
UW
20896 break;
20897
71c25dea 20898 default:
907af001
UW
20899 break;
20900 }
20901
20902 if (!DW_STRING_IS_CANONICAL (attr))
20903 {
20904 DW_STRING (attr)
20905 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20906 &cu->objfile->per_bfd->storage_obstack);
907af001 20907 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20908 }
907af001 20909 return DW_STRING (attr);
9219021c
DC
20910}
20911
20912/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20913 is none. *EXT_CU is the CU containing DIE on input, and the CU
20914 containing the return value on output. */
9219021c
DC
20915
20916static struct die_info *
f2f0e013 20917dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20918{
20919 struct attribute *attr;
9219021c 20920
f2f0e013 20921 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20922 if (attr == NULL)
20923 return NULL;
20924
f2f0e013 20925 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20926}
20927
c906108c
SS
20928/* Convert a DIE tag into its string name. */
20929
f39c6ffd 20930static const char *
aa1ee363 20931dwarf_tag_name (unsigned tag)
c906108c 20932{
f39c6ffd
TT
20933 const char *name = get_DW_TAG_name (tag);
20934
20935 if (name == NULL)
20936 return "DW_TAG_<unknown>";
20937
20938 return name;
c906108c
SS
20939}
20940
20941/* Convert a DWARF attribute code into its string name. */
20942
f39c6ffd 20943static const char *
aa1ee363 20944dwarf_attr_name (unsigned attr)
c906108c 20945{
f39c6ffd
TT
20946 const char *name;
20947
c764a876 20948#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20949 if (attr == DW_AT_MIPS_fde)
20950 return "DW_AT_MIPS_fde";
20951#else
20952 if (attr == DW_AT_HP_block_index)
20953 return "DW_AT_HP_block_index";
c764a876 20954#endif
f39c6ffd
TT
20955
20956 name = get_DW_AT_name (attr);
20957
20958 if (name == NULL)
20959 return "DW_AT_<unknown>";
20960
20961 return name;
c906108c
SS
20962}
20963
20964/* Convert a DWARF value form code into its string name. */
20965
f39c6ffd 20966static const char *
aa1ee363 20967dwarf_form_name (unsigned form)
c906108c 20968{
f39c6ffd
TT
20969 const char *name = get_DW_FORM_name (form);
20970
20971 if (name == NULL)
20972 return "DW_FORM_<unknown>";
20973
20974 return name;
c906108c
SS
20975}
20976
a121b7c1 20977static const char *
fba45db2 20978dwarf_bool_name (unsigned mybool)
c906108c
SS
20979{
20980 if (mybool)
20981 return "TRUE";
20982 else
20983 return "FALSE";
20984}
20985
20986/* Convert a DWARF type code into its string name. */
20987
f39c6ffd 20988static const char *
aa1ee363 20989dwarf_type_encoding_name (unsigned enc)
c906108c 20990{
f39c6ffd 20991 const char *name = get_DW_ATE_name (enc);
c906108c 20992
f39c6ffd
TT
20993 if (name == NULL)
20994 return "DW_ATE_<unknown>";
c906108c 20995
f39c6ffd 20996 return name;
c906108c 20997}
c906108c 20998
f9aca02d 20999static void
d97bc12b 21000dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21001{
21002 unsigned int i;
21003
d97bc12b
DE
21004 print_spaces (indent, f);
21005 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
21006 dwarf_tag_name (die->tag), die->abbrev,
21007 to_underlying (die->sect_off));
d97bc12b
DE
21008
21009 if (die->parent != NULL)
21010 {
21011 print_spaces (indent, f);
21012 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 21013 to_underlying (die->parent->sect_off));
d97bc12b
DE
21014 }
21015
21016 print_spaces (indent, f);
21017 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21018 dwarf_bool_name (die->child != NULL));
c906108c 21019
d97bc12b
DE
21020 print_spaces (indent, f);
21021 fprintf_unfiltered (f, " attributes:\n");
21022
c906108c
SS
21023 for (i = 0; i < die->num_attrs; ++i)
21024 {
d97bc12b
DE
21025 print_spaces (indent, f);
21026 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21027 dwarf_attr_name (die->attrs[i].name),
21028 dwarf_form_name (die->attrs[i].form));
d97bc12b 21029
c906108c
SS
21030 switch (die->attrs[i].form)
21031 {
c906108c 21032 case DW_FORM_addr:
3019eac3 21033 case DW_FORM_GNU_addr_index:
d97bc12b 21034 fprintf_unfiltered (f, "address: ");
5af949e3 21035 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21036 break;
21037 case DW_FORM_block2:
21038 case DW_FORM_block4:
21039 case DW_FORM_block:
21040 case DW_FORM_block1:
56eb65bd
SP
21041 fprintf_unfiltered (f, "block: size %s",
21042 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21043 break;
2dc7f7b3 21044 case DW_FORM_exprloc:
56eb65bd
SP
21045 fprintf_unfiltered (f, "expression: size %s",
21046 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21047 break;
0224619f
JK
21048 case DW_FORM_data16:
21049 fprintf_unfiltered (f, "constant of 16 bytes");
21050 break;
4568ecf9
DE
21051 case DW_FORM_ref_addr:
21052 fprintf_unfiltered (f, "ref address: ");
21053 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21054 break;
36586728
TT
21055 case DW_FORM_GNU_ref_alt:
21056 fprintf_unfiltered (f, "alt ref address: ");
21057 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21058 break;
10b3939b
DJ
21059 case DW_FORM_ref1:
21060 case DW_FORM_ref2:
21061 case DW_FORM_ref4:
4568ecf9
DE
21062 case DW_FORM_ref8:
21063 case DW_FORM_ref_udata:
d97bc12b 21064 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21065 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21066 break;
c906108c
SS
21067 case DW_FORM_data1:
21068 case DW_FORM_data2:
21069 case DW_FORM_data4:
ce5d95e1 21070 case DW_FORM_data8:
c906108c
SS
21071 case DW_FORM_udata:
21072 case DW_FORM_sdata:
43bbcdc2
PH
21073 fprintf_unfiltered (f, "constant: %s",
21074 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21075 break;
2dc7f7b3
TT
21076 case DW_FORM_sec_offset:
21077 fprintf_unfiltered (f, "section offset: %s",
21078 pulongest (DW_UNSND (&die->attrs[i])));
21079 break;
55f1336d 21080 case DW_FORM_ref_sig8:
ac9ec31b
DE
21081 fprintf_unfiltered (f, "signature: %s",
21082 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21083 break;
c906108c 21084 case DW_FORM_string:
4bdf3d34 21085 case DW_FORM_strp:
43988095 21086 case DW_FORM_line_strp:
3019eac3 21087 case DW_FORM_GNU_str_index:
36586728 21088 case DW_FORM_GNU_strp_alt:
8285870a 21089 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21090 DW_STRING (&die->attrs[i])
8285870a
JK
21091 ? DW_STRING (&die->attrs[i]) : "",
21092 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21093 break;
21094 case DW_FORM_flag:
21095 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21096 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21097 else
d97bc12b 21098 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21099 break;
2dc7f7b3
TT
21100 case DW_FORM_flag_present:
21101 fprintf_unfiltered (f, "flag: TRUE");
21102 break;
a8329558 21103 case DW_FORM_indirect:
0963b4bd
MS
21104 /* The reader will have reduced the indirect form to
21105 the "base form" so this form should not occur. */
3e43a32a
MS
21106 fprintf_unfiltered (f,
21107 "unexpected attribute form: DW_FORM_indirect");
a8329558 21108 break;
663c44ac
JK
21109 case DW_FORM_implicit_const:
21110 fprintf_unfiltered (f, "constant: %s",
21111 plongest (DW_SND (&die->attrs[i])));
21112 break;
c906108c 21113 default:
d97bc12b 21114 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21115 die->attrs[i].form);
d97bc12b 21116 break;
c906108c 21117 }
d97bc12b 21118 fprintf_unfiltered (f, "\n");
c906108c
SS
21119 }
21120}
21121
f9aca02d 21122static void
d97bc12b 21123dump_die_for_error (struct die_info *die)
c906108c 21124{
d97bc12b
DE
21125 dump_die_shallow (gdb_stderr, 0, die);
21126}
21127
21128static void
21129dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21130{
21131 int indent = level * 4;
21132
21133 gdb_assert (die != NULL);
21134
21135 if (level >= max_level)
21136 return;
21137
21138 dump_die_shallow (f, indent, die);
21139
21140 if (die->child != NULL)
c906108c 21141 {
d97bc12b
DE
21142 print_spaces (indent, f);
21143 fprintf_unfiltered (f, " Children:");
21144 if (level + 1 < max_level)
21145 {
21146 fprintf_unfiltered (f, "\n");
21147 dump_die_1 (f, level + 1, max_level, die->child);
21148 }
21149 else
21150 {
3e43a32a
MS
21151 fprintf_unfiltered (f,
21152 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21153 }
21154 }
21155
21156 if (die->sibling != NULL && level > 0)
21157 {
21158 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21159 }
21160}
21161
d97bc12b
DE
21162/* This is called from the pdie macro in gdbinit.in.
21163 It's not static so gcc will keep a copy callable from gdb. */
21164
21165void
21166dump_die (struct die_info *die, int max_level)
21167{
21168 dump_die_1 (gdb_stdlog, 0, max_level, die);
21169}
21170
f9aca02d 21171static void
51545339 21172store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21173{
51545339 21174 void **slot;
c906108c 21175
9c541725
PA
21176 slot = htab_find_slot_with_hash (cu->die_hash, die,
21177 to_underlying (die->sect_off),
b64f50a1 21178 INSERT);
51545339
DJ
21179
21180 *slot = die;
c906108c
SS
21181}
21182
b64f50a1
JK
21183/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21184 required kind. */
21185
21186static sect_offset
ff39bb5e 21187dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21188{
7771576e 21189 if (attr_form_is_ref (attr))
9c541725 21190 return (sect_offset) DW_UNSND (attr);
93311388
DE
21191
21192 complaint (&symfile_complaints,
21193 _("unsupported die ref attribute form: '%s'"),
21194 dwarf_form_name (attr->form));
9c541725 21195 return {};
c906108c
SS
21196}
21197
43bbcdc2
PH
21198/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21199 * the value held by the attribute is not constant. */
a02abb62 21200
43bbcdc2 21201static LONGEST
ff39bb5e 21202dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21203{
663c44ac 21204 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21205 return DW_SND (attr);
21206 else if (attr->form == DW_FORM_udata
21207 || attr->form == DW_FORM_data1
21208 || attr->form == DW_FORM_data2
21209 || attr->form == DW_FORM_data4
21210 || attr->form == DW_FORM_data8)
21211 return DW_UNSND (attr);
21212 else
21213 {
0224619f 21214 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
21215 complaint (&symfile_complaints,
21216 _("Attribute value is not a constant (%s)"),
a02abb62
JB
21217 dwarf_form_name (attr->form));
21218 return default_value;
21219 }
21220}
21221
348e048f
DE
21222/* Follow reference or signature attribute ATTR of SRC_DIE.
21223 On entry *REF_CU is the CU of SRC_DIE.
21224 On exit *REF_CU is the CU of the result. */
21225
21226static struct die_info *
ff39bb5e 21227follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21228 struct dwarf2_cu **ref_cu)
21229{
21230 struct die_info *die;
21231
7771576e 21232 if (attr_form_is_ref (attr))
348e048f 21233 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21234 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21235 die = follow_die_sig (src_die, attr, ref_cu);
21236 else
21237 {
21238 dump_die_for_error (src_die);
21239 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 21240 objfile_name ((*ref_cu)->objfile));
348e048f
DE
21241 }
21242
21243 return die;
03dd20cc
DJ
21244}
21245
5c631832 21246/* Follow reference OFFSET.
673bfd45
DE
21247 On entry *REF_CU is the CU of the source die referencing OFFSET.
21248 On exit *REF_CU is the CU of the result.
21249 Returns NULL if OFFSET is invalid. */
f504f079 21250
f9aca02d 21251static struct die_info *
9c541725 21252follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21253 struct dwarf2_cu **ref_cu)
c906108c 21254{
10b3939b 21255 struct die_info temp_die;
f2f0e013 21256 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 21257
348e048f
DE
21258 gdb_assert (cu->per_cu != NULL);
21259
98bfdba5
PA
21260 target_cu = cu;
21261
3019eac3 21262 if (cu->per_cu->is_debug_types)
348e048f
DE
21263 {
21264 /* .debug_types CUs cannot reference anything outside their CU.
21265 If they need to, they have to reference a signatured type via
55f1336d 21266 DW_FORM_ref_sig8. */
9c541725 21267 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 21268 return NULL;
348e048f 21269 }
36586728 21270 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 21271 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
21272 {
21273 struct dwarf2_per_cu_data *per_cu;
9a619af0 21274
9c541725 21275 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 21276 cu->objfile);
03dd20cc
DJ
21277
21278 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
21279 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21280 load_full_comp_unit (per_cu, cu->language);
03dd20cc 21281
10b3939b
DJ
21282 target_cu = per_cu->cu;
21283 }
98bfdba5
PA
21284 else if (cu->dies == NULL)
21285 {
21286 /* We're loading full DIEs during partial symbol reading. */
21287 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 21288 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 21289 }
c906108c 21290
f2f0e013 21291 *ref_cu = target_cu;
9c541725 21292 temp_die.sect_off = sect_off;
9a3c8263 21293 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21294 &temp_die,
21295 to_underlying (sect_off));
5c631832 21296}
10b3939b 21297
5c631832
JK
21298/* Follow reference attribute ATTR of SRC_DIE.
21299 On entry *REF_CU is the CU of SRC_DIE.
21300 On exit *REF_CU is the CU of the result. */
21301
21302static struct die_info *
ff39bb5e 21303follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21304 struct dwarf2_cu **ref_cu)
21305{
9c541725 21306 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21307 struct dwarf2_cu *cu = *ref_cu;
21308 struct die_info *die;
21309
9c541725 21310 die = follow_die_offset (sect_off,
36586728
TT
21311 (attr->form == DW_FORM_GNU_ref_alt
21312 || cu->per_cu->is_dwz),
21313 ref_cu);
5c631832
JK
21314 if (!die)
21315 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21316 "at 0x%x [in module %s]"),
9c541725 21317 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 21318 objfile_name (cu->objfile));
348e048f 21319
5c631832
JK
21320 return die;
21321}
21322
9c541725 21323/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
21324 Returned value is intended for DW_OP_call*. Returned
21325 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
21326
21327struct dwarf2_locexpr_baton
9c541725 21328dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
21329 struct dwarf2_per_cu_data *per_cu,
21330 CORE_ADDR (*get_frame_pc) (void *baton),
21331 void *baton)
5c631832 21332{
918dd910 21333 struct dwarf2_cu *cu;
5c631832
JK
21334 struct die_info *die;
21335 struct attribute *attr;
21336 struct dwarf2_locexpr_baton retval;
21337
8cf6f0b1
TT
21338 dw2_setup (per_cu->objfile);
21339
918dd910
JK
21340 if (per_cu->cu == NULL)
21341 load_cu (per_cu);
21342 cu = per_cu->cu;
cc12ce38
DE
21343 if (cu == NULL)
21344 {
21345 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21346 Instead just throw an error, not much else we can do. */
21347 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21348 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21349 }
918dd910 21350
9c541725 21351 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
21352 if (!die)
21353 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21354 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21355
21356 attr = dwarf2_attr (die, DW_AT_location, cu);
21357 if (!attr)
21358 {
e103e986
JK
21359 /* DWARF: "If there is no such attribute, then there is no effect.".
21360 DATA is ignored if SIZE is 0. */
5c631832 21361
e103e986 21362 retval.data = NULL;
5c631832
JK
21363 retval.size = 0;
21364 }
8cf6f0b1
TT
21365 else if (attr_form_is_section_offset (attr))
21366 {
21367 struct dwarf2_loclist_baton loclist_baton;
21368 CORE_ADDR pc = (*get_frame_pc) (baton);
21369 size_t size;
21370
21371 fill_in_loclist_baton (cu, &loclist_baton, attr);
21372
21373 retval.data = dwarf2_find_location_expression (&loclist_baton,
21374 &size, pc);
21375 retval.size = size;
21376 }
5c631832
JK
21377 else
21378 {
21379 if (!attr_form_is_block (attr))
21380 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21381 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 21382 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21383
21384 retval.data = DW_BLOCK (attr)->data;
21385 retval.size = DW_BLOCK (attr)->size;
21386 }
21387 retval.per_cu = cu->per_cu;
918dd910 21388
918dd910
JK
21389 age_cached_comp_units ();
21390
5c631832 21391 return retval;
348e048f
DE
21392}
21393
8b9737bf
TT
21394/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21395 offset. */
21396
21397struct dwarf2_locexpr_baton
21398dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21399 struct dwarf2_per_cu_data *per_cu,
21400 CORE_ADDR (*get_frame_pc) (void *baton),
21401 void *baton)
21402{
9c541725 21403 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21404
9c541725 21405 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21406}
21407
b6807d98
TT
21408/* Write a constant of a given type as target-ordered bytes into
21409 OBSTACK. */
21410
21411static const gdb_byte *
21412write_constant_as_bytes (struct obstack *obstack,
21413 enum bfd_endian byte_order,
21414 struct type *type,
21415 ULONGEST value,
21416 LONGEST *len)
21417{
21418 gdb_byte *result;
21419
21420 *len = TYPE_LENGTH (type);
224c3ddb 21421 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21422 store_unsigned_integer (result, *len, byte_order, value);
21423
21424 return result;
21425}
21426
21427/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21428 pointer to the constant bytes and set LEN to the length of the
21429 data. If memory is needed, allocate it on OBSTACK. If the DIE
21430 does not have a DW_AT_const_value, return NULL. */
21431
21432const gdb_byte *
9c541725 21433dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
21434 struct dwarf2_per_cu_data *per_cu,
21435 struct obstack *obstack,
21436 LONGEST *len)
21437{
21438 struct dwarf2_cu *cu;
21439 struct die_info *die;
21440 struct attribute *attr;
21441 const gdb_byte *result = NULL;
21442 struct type *type;
21443 LONGEST value;
21444 enum bfd_endian byte_order;
21445
21446 dw2_setup (per_cu->objfile);
21447
21448 if (per_cu->cu == NULL)
21449 load_cu (per_cu);
21450 cu = per_cu->cu;
cc12ce38
DE
21451 if (cu == NULL)
21452 {
21453 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21454 Instead just throw an error, not much else we can do. */
21455 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21456 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21457 }
b6807d98 21458
9c541725 21459 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
21460 if (!die)
21461 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21462 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
21463
21464
21465 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21466 if (attr == NULL)
21467 return NULL;
21468
21469 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21470 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21471
21472 switch (attr->form)
21473 {
21474 case DW_FORM_addr:
21475 case DW_FORM_GNU_addr_index:
21476 {
21477 gdb_byte *tem;
21478
21479 *len = cu->header.addr_size;
224c3ddb 21480 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21481 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21482 result = tem;
21483 }
21484 break;
21485 case DW_FORM_string:
21486 case DW_FORM_strp:
21487 case DW_FORM_GNU_str_index:
21488 case DW_FORM_GNU_strp_alt:
21489 /* DW_STRING is already allocated on the objfile obstack, point
21490 directly to it. */
21491 result = (const gdb_byte *) DW_STRING (attr);
21492 *len = strlen (DW_STRING (attr));
21493 break;
21494 case DW_FORM_block1:
21495 case DW_FORM_block2:
21496 case DW_FORM_block4:
21497 case DW_FORM_block:
21498 case DW_FORM_exprloc:
0224619f 21499 case DW_FORM_data16:
b6807d98
TT
21500 result = DW_BLOCK (attr)->data;
21501 *len = DW_BLOCK (attr)->size;
21502 break;
21503
21504 /* The DW_AT_const_value attributes are supposed to carry the
21505 symbol's value "represented as it would be on the target
21506 architecture." By the time we get here, it's already been
21507 converted to host endianness, so we just need to sign- or
21508 zero-extend it as appropriate. */
21509 case DW_FORM_data1:
21510 type = die_type (die, cu);
21511 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21512 if (result == NULL)
21513 result = write_constant_as_bytes (obstack, byte_order,
21514 type, value, len);
21515 break;
21516 case DW_FORM_data2:
21517 type = die_type (die, cu);
21518 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21519 if (result == NULL)
21520 result = write_constant_as_bytes (obstack, byte_order,
21521 type, value, len);
21522 break;
21523 case DW_FORM_data4:
21524 type = die_type (die, cu);
21525 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21526 if (result == NULL)
21527 result = write_constant_as_bytes (obstack, byte_order,
21528 type, value, len);
21529 break;
21530 case DW_FORM_data8:
21531 type = die_type (die, cu);
21532 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21533 if (result == NULL)
21534 result = write_constant_as_bytes (obstack, byte_order,
21535 type, value, len);
21536 break;
21537
21538 case DW_FORM_sdata:
663c44ac 21539 case DW_FORM_implicit_const:
b6807d98
TT
21540 type = die_type (die, cu);
21541 result = write_constant_as_bytes (obstack, byte_order,
21542 type, DW_SND (attr), len);
21543 break;
21544
21545 case DW_FORM_udata:
21546 type = die_type (die, cu);
21547 result = write_constant_as_bytes (obstack, byte_order,
21548 type, DW_UNSND (attr), len);
21549 break;
21550
21551 default:
21552 complaint (&symfile_complaints,
21553 _("unsupported const value attribute form: '%s'"),
21554 dwarf_form_name (attr->form));
21555 break;
21556 }
21557
21558 return result;
21559}
21560
7942e96e
AA
21561/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21562 valid type for this die is found. */
21563
21564struct type *
9c541725 21565dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21566 struct dwarf2_per_cu_data *per_cu)
21567{
21568 struct dwarf2_cu *cu;
21569 struct die_info *die;
21570
21571 dw2_setup (per_cu->objfile);
21572
21573 if (per_cu->cu == NULL)
21574 load_cu (per_cu);
21575 cu = per_cu->cu;
21576 if (!cu)
21577 return NULL;
21578
9c541725 21579 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21580 if (!die)
21581 return NULL;
21582
21583 return die_type (die, cu);
21584}
21585
8a9b8146
TT
21586/* Return the type of the DIE at DIE_OFFSET in the CU named by
21587 PER_CU. */
21588
21589struct type *
b64f50a1 21590dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21591 struct dwarf2_per_cu_data *per_cu)
21592{
8a9b8146 21593 dw2_setup (per_cu->objfile);
b64f50a1 21594
9c541725 21595 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21596 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21597}
21598
ac9ec31b 21599/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21600 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21601 On exit *REF_CU is the CU of the result.
21602 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21603
21604static struct die_info *
ac9ec31b
DE
21605follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21606 struct dwarf2_cu **ref_cu)
348e048f 21607{
348e048f 21608 struct die_info temp_die;
348e048f
DE
21609 struct dwarf2_cu *sig_cu;
21610 struct die_info *die;
21611
ac9ec31b
DE
21612 /* While it might be nice to assert sig_type->type == NULL here,
21613 we can get here for DW_AT_imported_declaration where we need
21614 the DIE not the type. */
348e048f
DE
21615
21616 /* If necessary, add it to the queue and load its DIEs. */
21617
95554aad 21618 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21619 read_signatured_type (sig_type);
348e048f 21620
348e048f 21621 sig_cu = sig_type->per_cu.cu;
69d751e3 21622 gdb_assert (sig_cu != NULL);
9c541725
PA
21623 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21624 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21625 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21626 to_underlying (temp_die.sect_off));
348e048f
DE
21627 if (die)
21628 {
796a7ff8
DE
21629 /* For .gdb_index version 7 keep track of included TUs.
21630 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21631 if (dwarf2_per_objfile->index_table != NULL
21632 && dwarf2_per_objfile->index_table->version <= 7)
21633 {
21634 VEC_safe_push (dwarf2_per_cu_ptr,
21635 (*ref_cu)->per_cu->imported_symtabs,
21636 sig_cu->per_cu);
21637 }
21638
348e048f
DE
21639 *ref_cu = sig_cu;
21640 return die;
21641 }
21642
ac9ec31b
DE
21643 return NULL;
21644}
21645
21646/* Follow signatured type referenced by ATTR in SRC_DIE.
21647 On entry *REF_CU is the CU of SRC_DIE.
21648 On exit *REF_CU is the CU of the result.
21649 The result is the DIE of the type.
21650 If the referenced type cannot be found an error is thrown. */
21651
21652static struct die_info *
ff39bb5e 21653follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21654 struct dwarf2_cu **ref_cu)
21655{
21656 ULONGEST signature = DW_SIGNATURE (attr);
21657 struct signatured_type *sig_type;
21658 struct die_info *die;
21659
21660 gdb_assert (attr->form == DW_FORM_ref_sig8);
21661
a2ce51a0 21662 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21663 /* sig_type will be NULL if the signatured type is missing from
21664 the debug info. */
21665 if (sig_type == NULL)
21666 {
21667 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21668 " from DIE at 0x%x [in module %s]"),
9c541725 21669 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21670 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21671 }
21672
21673 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21674 if (die == NULL)
21675 {
21676 dump_die_for_error (src_die);
21677 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21678 " from DIE at 0x%x [in module %s]"),
9c541725 21679 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21680 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21681 }
21682
21683 return die;
21684}
21685
21686/* Get the type specified by SIGNATURE referenced in DIE/CU,
21687 reading in and processing the type unit if necessary. */
21688
21689static struct type *
21690get_signatured_type (struct die_info *die, ULONGEST signature,
21691 struct dwarf2_cu *cu)
21692{
21693 struct signatured_type *sig_type;
21694 struct dwarf2_cu *type_cu;
21695 struct die_info *type_die;
21696 struct type *type;
21697
a2ce51a0 21698 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21699 /* sig_type will be NULL if the signatured type is missing from
21700 the debug info. */
21701 if (sig_type == NULL)
21702 {
21703 complaint (&symfile_complaints,
21704 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21705 " from DIE at 0x%x [in module %s]"),
9c541725 21706 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21707 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21708 return build_error_marker_type (cu, die);
21709 }
21710
21711 /* If we already know the type we're done. */
21712 if (sig_type->type != NULL)
21713 return sig_type->type;
21714
21715 type_cu = cu;
21716 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21717 if (type_die != NULL)
21718 {
21719 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21720 is created. This is important, for example, because for c++ classes
21721 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21722 type = read_type_die (type_die, type_cu);
21723 if (type == NULL)
21724 {
21725 complaint (&symfile_complaints,
21726 _("Dwarf Error: Cannot build signatured type %s"
21727 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21728 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21729 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21730 type = build_error_marker_type (cu, die);
21731 }
21732 }
21733 else
21734 {
21735 complaint (&symfile_complaints,
21736 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21737 " from DIE at 0x%x [in module %s]"),
9c541725 21738 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21739 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21740 type = build_error_marker_type (cu, die);
21741 }
21742 sig_type->type = type;
21743
21744 return type;
21745}
21746
21747/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21748 reading in and processing the type unit if necessary. */
21749
21750static struct type *
ff39bb5e 21751get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21752 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21753{
21754 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21755 if (attr_form_is_ref (attr))
ac9ec31b
DE
21756 {
21757 struct dwarf2_cu *type_cu = cu;
21758 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21759
21760 return read_type_die (type_die, type_cu);
21761 }
21762 else if (attr->form == DW_FORM_ref_sig8)
21763 {
21764 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21765 }
21766 else
21767 {
21768 complaint (&symfile_complaints,
21769 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21770 " at 0x%x [in module %s]"),
9c541725 21771 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21772 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21773 return build_error_marker_type (cu, die);
21774 }
348e048f
DE
21775}
21776
e5fe5e75 21777/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21778
21779static void
e5fe5e75 21780load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21781{
52dc124a 21782 struct signatured_type *sig_type;
348e048f 21783
f4dc4d17
DE
21784 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21785 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21786
6721b2ec
DE
21787 /* We have the per_cu, but we need the signatured_type.
21788 Fortunately this is an easy translation. */
21789 gdb_assert (per_cu->is_debug_types);
21790 sig_type = (struct signatured_type *) per_cu;
348e048f 21791
6721b2ec 21792 gdb_assert (per_cu->cu == NULL);
348e048f 21793
52dc124a 21794 read_signatured_type (sig_type);
348e048f 21795
6721b2ec 21796 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21797}
21798
dee91e82
DE
21799/* die_reader_func for read_signatured_type.
21800 This is identical to load_full_comp_unit_reader,
21801 but is kept separate for now. */
348e048f
DE
21802
21803static void
dee91e82 21804read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21805 const gdb_byte *info_ptr,
dee91e82
DE
21806 struct die_info *comp_unit_die,
21807 int has_children,
21808 void *data)
348e048f 21809{
dee91e82 21810 struct dwarf2_cu *cu = reader->cu;
348e048f 21811
dee91e82
DE
21812 gdb_assert (cu->die_hash == NULL);
21813 cu->die_hash =
21814 htab_create_alloc_ex (cu->header.length / 12,
21815 die_hash,
21816 die_eq,
21817 NULL,
21818 &cu->comp_unit_obstack,
21819 hashtab_obstack_allocate,
21820 dummy_obstack_deallocate);
348e048f 21821
dee91e82
DE
21822 if (has_children)
21823 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21824 &info_ptr, comp_unit_die);
21825 cu->dies = comp_unit_die;
21826 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21827
21828 /* We try not to read any attributes in this function, because not
9cdd5dbd 21829 all CUs needed for references have been loaded yet, and symbol
348e048f 21830 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21831 or we won't be able to build types correctly.
21832 Similarly, if we do not read the producer, we can not apply
21833 producer-specific interpretation. */
95554aad 21834 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21835}
348e048f 21836
3019eac3
DE
21837/* Read in a signatured type and build its CU and DIEs.
21838 If the type is a stub for the real type in a DWO file,
21839 read in the real type from the DWO file as well. */
dee91e82
DE
21840
21841static void
21842read_signatured_type (struct signatured_type *sig_type)
21843{
21844 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21845
3019eac3 21846 gdb_assert (per_cu->is_debug_types);
dee91e82 21847 gdb_assert (per_cu->cu == NULL);
348e048f 21848
f4dc4d17
DE
21849 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21850 read_signatured_type_reader, NULL);
7ee85ab1 21851 sig_type->per_cu.tu_read = 1;
c906108c
SS
21852}
21853
c906108c
SS
21854/* Decode simple location descriptions.
21855 Given a pointer to a dwarf block that defines a location, compute
21856 the location and return the value.
21857
4cecd739
DJ
21858 NOTE drow/2003-11-18: This function is called in two situations
21859 now: for the address of static or global variables (partial symbols
21860 only) and for offsets into structures which are expected to be
21861 (more or less) constant. The partial symbol case should go away,
21862 and only the constant case should remain. That will let this
21863 function complain more accurately. A few special modes are allowed
21864 without complaint for global variables (for instance, global
21865 register values and thread-local values).
c906108c
SS
21866
21867 A location description containing no operations indicates that the
4cecd739 21868 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21869 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21870 callers will only want a very basic result and this can become a
21ae7a4d
JK
21871 complaint.
21872
21873 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21874
21875static CORE_ADDR
e7c27a73 21876decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21877{
e7c27a73 21878 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21879 size_t i;
21880 size_t size = blk->size;
d521ce57 21881 const gdb_byte *data = blk->data;
21ae7a4d
JK
21882 CORE_ADDR stack[64];
21883 int stacki;
21884 unsigned int bytes_read, unsnd;
21885 gdb_byte op;
c906108c 21886
21ae7a4d
JK
21887 i = 0;
21888 stacki = 0;
21889 stack[stacki] = 0;
21890 stack[++stacki] = 0;
21891
21892 while (i < size)
21893 {
21894 op = data[i++];
21895 switch (op)
21896 {
21897 case DW_OP_lit0:
21898 case DW_OP_lit1:
21899 case DW_OP_lit2:
21900 case DW_OP_lit3:
21901 case DW_OP_lit4:
21902 case DW_OP_lit5:
21903 case DW_OP_lit6:
21904 case DW_OP_lit7:
21905 case DW_OP_lit8:
21906 case DW_OP_lit9:
21907 case DW_OP_lit10:
21908 case DW_OP_lit11:
21909 case DW_OP_lit12:
21910 case DW_OP_lit13:
21911 case DW_OP_lit14:
21912 case DW_OP_lit15:
21913 case DW_OP_lit16:
21914 case DW_OP_lit17:
21915 case DW_OP_lit18:
21916 case DW_OP_lit19:
21917 case DW_OP_lit20:
21918 case DW_OP_lit21:
21919 case DW_OP_lit22:
21920 case DW_OP_lit23:
21921 case DW_OP_lit24:
21922 case DW_OP_lit25:
21923 case DW_OP_lit26:
21924 case DW_OP_lit27:
21925 case DW_OP_lit28:
21926 case DW_OP_lit29:
21927 case DW_OP_lit30:
21928 case DW_OP_lit31:
21929 stack[++stacki] = op - DW_OP_lit0;
21930 break;
f1bea926 21931
21ae7a4d
JK
21932 case DW_OP_reg0:
21933 case DW_OP_reg1:
21934 case DW_OP_reg2:
21935 case DW_OP_reg3:
21936 case DW_OP_reg4:
21937 case DW_OP_reg5:
21938 case DW_OP_reg6:
21939 case DW_OP_reg7:
21940 case DW_OP_reg8:
21941 case DW_OP_reg9:
21942 case DW_OP_reg10:
21943 case DW_OP_reg11:
21944 case DW_OP_reg12:
21945 case DW_OP_reg13:
21946 case DW_OP_reg14:
21947 case DW_OP_reg15:
21948 case DW_OP_reg16:
21949 case DW_OP_reg17:
21950 case DW_OP_reg18:
21951 case DW_OP_reg19:
21952 case DW_OP_reg20:
21953 case DW_OP_reg21:
21954 case DW_OP_reg22:
21955 case DW_OP_reg23:
21956 case DW_OP_reg24:
21957 case DW_OP_reg25:
21958 case DW_OP_reg26:
21959 case DW_OP_reg27:
21960 case DW_OP_reg28:
21961 case DW_OP_reg29:
21962 case DW_OP_reg30:
21963 case DW_OP_reg31:
21964 stack[++stacki] = op - DW_OP_reg0;
21965 if (i < size)
21966 dwarf2_complex_location_expr_complaint ();
21967 break;
c906108c 21968
21ae7a4d
JK
21969 case DW_OP_regx:
21970 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21971 i += bytes_read;
21972 stack[++stacki] = unsnd;
21973 if (i < size)
21974 dwarf2_complex_location_expr_complaint ();
21975 break;
c906108c 21976
21ae7a4d
JK
21977 case DW_OP_addr:
21978 stack[++stacki] = read_address (objfile->obfd, &data[i],
21979 cu, &bytes_read);
21980 i += bytes_read;
21981 break;
d53d4ac5 21982
21ae7a4d
JK
21983 case DW_OP_const1u:
21984 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21985 i += 1;
21986 break;
21987
21988 case DW_OP_const1s:
21989 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21990 i += 1;
21991 break;
21992
21993 case DW_OP_const2u:
21994 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21995 i += 2;
21996 break;
21997
21998 case DW_OP_const2s:
21999 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22000 i += 2;
22001 break;
d53d4ac5 22002
21ae7a4d
JK
22003 case DW_OP_const4u:
22004 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22005 i += 4;
22006 break;
22007
22008 case DW_OP_const4s:
22009 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22010 i += 4;
22011 break;
22012
585861ea
JK
22013 case DW_OP_const8u:
22014 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22015 i += 8;
22016 break;
22017
21ae7a4d
JK
22018 case DW_OP_constu:
22019 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22020 &bytes_read);
22021 i += bytes_read;
22022 break;
22023
22024 case DW_OP_consts:
22025 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22026 i += bytes_read;
22027 break;
22028
22029 case DW_OP_dup:
22030 stack[stacki + 1] = stack[stacki];
22031 stacki++;
22032 break;
22033
22034 case DW_OP_plus:
22035 stack[stacki - 1] += stack[stacki];
22036 stacki--;
22037 break;
22038
22039 case DW_OP_plus_uconst:
22040 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22041 &bytes_read);
22042 i += bytes_read;
22043 break;
22044
22045 case DW_OP_minus:
22046 stack[stacki - 1] -= stack[stacki];
22047 stacki--;
22048 break;
22049
22050 case DW_OP_deref:
22051 /* If we're not the last op, then we definitely can't encode
22052 this using GDB's address_class enum. This is valid for partial
22053 global symbols, although the variable's address will be bogus
22054 in the psymtab. */
22055 if (i < size)
22056 dwarf2_complex_location_expr_complaint ();
22057 break;
22058
22059 case DW_OP_GNU_push_tls_address:
4aa4e28b 22060 case DW_OP_form_tls_address:
21ae7a4d
JK
22061 /* The top of the stack has the offset from the beginning
22062 of the thread control block at which the variable is located. */
22063 /* Nothing should follow this operator, so the top of stack would
22064 be returned. */
22065 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22066 address will be bogus in the psymtab. Make it always at least
22067 non-zero to not look as a variable garbage collected by linker
22068 which have DW_OP_addr 0. */
21ae7a4d
JK
22069 if (i < size)
22070 dwarf2_complex_location_expr_complaint ();
585861ea 22071 stack[stacki]++;
21ae7a4d
JK
22072 break;
22073
22074 case DW_OP_GNU_uninit:
22075 break;
22076
3019eac3 22077 case DW_OP_GNU_addr_index:
49f6c839 22078 case DW_OP_GNU_const_index:
3019eac3
DE
22079 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22080 &bytes_read);
22081 i += bytes_read;
22082 break;
22083
21ae7a4d
JK
22084 default:
22085 {
f39c6ffd 22086 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22087
22088 if (name)
22089 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22090 name);
22091 else
22092 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22093 op);
22094 }
22095
22096 return (stack[stacki]);
d53d4ac5 22097 }
3c6e0cb3 22098
21ae7a4d
JK
22099 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22100 outside of the allocated space. Also enforce minimum>0. */
22101 if (stacki >= ARRAY_SIZE (stack) - 1)
22102 {
22103 complaint (&symfile_complaints,
22104 _("location description stack overflow"));
22105 return 0;
22106 }
22107
22108 if (stacki <= 0)
22109 {
22110 complaint (&symfile_complaints,
22111 _("location description stack underflow"));
22112 return 0;
22113 }
22114 }
22115 return (stack[stacki]);
c906108c
SS
22116}
22117
22118/* memory allocation interface */
22119
c906108c 22120static struct dwarf_block *
7b5a2f43 22121dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22122{
8d749320 22123 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22124}
22125
c906108c 22126static struct die_info *
b60c80d6 22127dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22128{
22129 struct die_info *die;
b60c80d6
DJ
22130 size_t size = sizeof (struct die_info);
22131
22132 if (num_attrs > 1)
22133 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22134
b60c80d6 22135 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22136 memset (die, 0, sizeof (struct die_info));
22137 return (die);
22138}
2e276125
JB
22139
22140\f
22141/* Macro support. */
22142
233d95b5
JK
22143/* Return file name relative to the compilation directory of file number I in
22144 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 22145 responsible for freeing it. */
233d95b5 22146
2e276125 22147static char *
233d95b5 22148file_file_name (int file, struct line_header *lh)
2e276125 22149{
6a83a1e6
EZ
22150 /* Is the file number a valid index into the line header's file name
22151 table? Remember that file numbers start with one, not zero. */
fff8551c 22152 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 22153 {
8c43009f 22154 const file_entry &fe = lh->file_names[file - 1];
6e70227d 22155
8c43009f
PA
22156 if (!IS_ABSOLUTE_PATH (fe.name))
22157 {
22158 const char *dir = fe.include_dir (lh);
22159 if (dir != NULL)
22160 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22161 }
22162 return xstrdup (fe.name);
6a83a1e6 22163 }
2e276125
JB
22164 else
22165 {
6a83a1e6
EZ
22166 /* The compiler produced a bogus file number. We can at least
22167 record the macro definitions made in the file, even if we
22168 won't be able to find the file by name. */
22169 char fake_name[80];
9a619af0 22170
8c042590
PM
22171 xsnprintf (fake_name, sizeof (fake_name),
22172 "<bad macro file number %d>", file);
2e276125 22173
6e70227d 22174 complaint (&symfile_complaints,
6a83a1e6
EZ
22175 _("bad file number in macro information (%d)"),
22176 file);
2e276125 22177
6a83a1e6 22178 return xstrdup (fake_name);
2e276125
JB
22179 }
22180}
22181
233d95b5
JK
22182/* Return the full name of file number I in *LH's file name table.
22183 Use COMP_DIR as the name of the current directory of the
22184 compilation. The result is allocated using xmalloc; the caller is
22185 responsible for freeing it. */
22186static char *
22187file_full_name (int file, struct line_header *lh, const char *comp_dir)
22188{
22189 /* Is the file number a valid index into the line header's file name
22190 table? Remember that file numbers start with one, not zero. */
fff8551c 22191 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
22192 {
22193 char *relative = file_file_name (file, lh);
22194
22195 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22196 return relative;
b36cec19
PA
22197 return reconcat (relative, comp_dir, SLASH_STRING,
22198 relative, (char *) NULL);
233d95b5
JK
22199 }
22200 else
22201 return file_file_name (file, lh);
22202}
22203
2e276125
JB
22204
22205static struct macro_source_file *
22206macro_start_file (int file, int line,
22207 struct macro_source_file *current_file,
43f3e411 22208 struct line_header *lh)
2e276125 22209{
233d95b5
JK
22210 /* File name relative to the compilation directory of this source file. */
22211 char *file_name = file_file_name (file, lh);
2e276125 22212
2e276125 22213 if (! current_file)
abc9d0dc 22214 {
fc474241
DE
22215 /* Note: We don't create a macro table for this compilation unit
22216 at all until we actually get a filename. */
43f3e411 22217 struct macro_table *macro_table = get_macro_table ();
fc474241 22218
abc9d0dc
TT
22219 /* If we have no current file, then this must be the start_file
22220 directive for the compilation unit's main source file. */
fc474241
DE
22221 current_file = macro_set_main (macro_table, file_name);
22222 macro_define_special (macro_table);
abc9d0dc 22223 }
2e276125 22224 else
233d95b5 22225 current_file = macro_include (current_file, line, file_name);
2e276125 22226
233d95b5 22227 xfree (file_name);
6e70227d 22228
2e276125
JB
22229 return current_file;
22230}
22231
2e276125
JB
22232static const char *
22233consume_improper_spaces (const char *p, const char *body)
22234{
22235 if (*p == ' ')
22236 {
4d3c2250 22237 complaint (&symfile_complaints,
3e43a32a
MS
22238 _("macro definition contains spaces "
22239 "in formal argument list:\n`%s'"),
4d3c2250 22240 body);
2e276125
JB
22241
22242 while (*p == ' ')
22243 p++;
22244 }
22245
22246 return p;
22247}
22248
22249
22250static void
22251parse_macro_definition (struct macro_source_file *file, int line,
22252 const char *body)
22253{
22254 const char *p;
22255
22256 /* The body string takes one of two forms. For object-like macro
22257 definitions, it should be:
22258
22259 <macro name> " " <definition>
22260
22261 For function-like macro definitions, it should be:
22262
22263 <macro name> "() " <definition>
22264 or
22265 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22266
22267 Spaces may appear only where explicitly indicated, and in the
22268 <definition>.
22269
22270 The Dwarf 2 spec says that an object-like macro's name is always
22271 followed by a space, but versions of GCC around March 2002 omit
6e70227d 22272 the space when the macro's definition is the empty string.
2e276125
JB
22273
22274 The Dwarf 2 spec says that there should be no spaces between the
22275 formal arguments in a function-like macro's formal argument list,
22276 but versions of GCC around March 2002 include spaces after the
22277 commas. */
22278
22279
22280 /* Find the extent of the macro name. The macro name is terminated
22281 by either a space or null character (for an object-like macro) or
22282 an opening paren (for a function-like macro). */
22283 for (p = body; *p; p++)
22284 if (*p == ' ' || *p == '(')
22285 break;
22286
22287 if (*p == ' ' || *p == '\0')
22288 {
22289 /* It's an object-like macro. */
22290 int name_len = p - body;
3f8a7804 22291 char *name = savestring (body, name_len);
2e276125
JB
22292 const char *replacement;
22293
22294 if (*p == ' ')
22295 replacement = body + name_len + 1;
22296 else
22297 {
4d3c2250 22298 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22299 replacement = body + name_len;
22300 }
6e70227d 22301
2e276125
JB
22302 macro_define_object (file, line, name, replacement);
22303
22304 xfree (name);
22305 }
22306 else if (*p == '(')
22307 {
22308 /* It's a function-like macro. */
3f8a7804 22309 char *name = savestring (body, p - body);
2e276125
JB
22310 int argc = 0;
22311 int argv_size = 1;
8d749320 22312 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
22313
22314 p++;
22315
22316 p = consume_improper_spaces (p, body);
22317
22318 /* Parse the formal argument list. */
22319 while (*p && *p != ')')
22320 {
22321 /* Find the extent of the current argument name. */
22322 const char *arg_start = p;
22323
22324 while (*p && *p != ',' && *p != ')' && *p != ' ')
22325 p++;
22326
22327 if (! *p || p == arg_start)
4d3c2250 22328 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22329 else
22330 {
22331 /* Make sure argv has room for the new argument. */
22332 if (argc >= argv_size)
22333 {
22334 argv_size *= 2;
224c3ddb 22335 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
22336 }
22337
3f8a7804 22338 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
22339 }
22340
22341 p = consume_improper_spaces (p, body);
22342
22343 /* Consume the comma, if present. */
22344 if (*p == ',')
22345 {
22346 p++;
22347
22348 p = consume_improper_spaces (p, body);
22349 }
22350 }
22351
22352 if (*p == ')')
22353 {
22354 p++;
22355
22356 if (*p == ' ')
22357 /* Perfectly formed definition, no complaints. */
22358 macro_define_function (file, line, name,
6e70227d 22359 argc, (const char **) argv,
2e276125
JB
22360 p + 1);
22361 else if (*p == '\0')
22362 {
22363 /* Complain, but do define it. */
4d3c2250 22364 dwarf2_macro_malformed_definition_complaint (body);
2e276125 22365 macro_define_function (file, line, name,
6e70227d 22366 argc, (const char **) argv,
2e276125
JB
22367 p);
22368 }
22369 else
22370 /* Just complain. */
4d3c2250 22371 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22372 }
22373 else
22374 /* Just complain. */
4d3c2250 22375 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22376
22377 xfree (name);
22378 {
22379 int i;
22380
22381 for (i = 0; i < argc; i++)
22382 xfree (argv[i]);
22383 }
22384 xfree (argv);
22385 }
22386 else
4d3c2250 22387 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22388}
22389
cf2c3c16
TT
22390/* Skip some bytes from BYTES according to the form given in FORM.
22391 Returns the new pointer. */
2e276125 22392
d521ce57
TT
22393static const gdb_byte *
22394skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
22395 enum dwarf_form form,
22396 unsigned int offset_size,
22397 struct dwarf2_section_info *section)
2e276125 22398{
cf2c3c16 22399 unsigned int bytes_read;
2e276125 22400
cf2c3c16 22401 switch (form)
2e276125 22402 {
cf2c3c16
TT
22403 case DW_FORM_data1:
22404 case DW_FORM_flag:
22405 ++bytes;
22406 break;
22407
22408 case DW_FORM_data2:
22409 bytes += 2;
22410 break;
22411
22412 case DW_FORM_data4:
22413 bytes += 4;
22414 break;
22415
22416 case DW_FORM_data8:
22417 bytes += 8;
22418 break;
22419
0224619f
JK
22420 case DW_FORM_data16:
22421 bytes += 16;
22422 break;
22423
cf2c3c16
TT
22424 case DW_FORM_string:
22425 read_direct_string (abfd, bytes, &bytes_read);
22426 bytes += bytes_read;
22427 break;
22428
22429 case DW_FORM_sec_offset:
22430 case DW_FORM_strp:
36586728 22431 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
22432 bytes += offset_size;
22433 break;
22434
22435 case DW_FORM_block:
22436 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22437 bytes += bytes_read;
22438 break;
22439
22440 case DW_FORM_block1:
22441 bytes += 1 + read_1_byte (abfd, bytes);
22442 break;
22443 case DW_FORM_block2:
22444 bytes += 2 + read_2_bytes (abfd, bytes);
22445 break;
22446 case DW_FORM_block4:
22447 bytes += 4 + read_4_bytes (abfd, bytes);
22448 break;
22449
22450 case DW_FORM_sdata:
22451 case DW_FORM_udata:
3019eac3
DE
22452 case DW_FORM_GNU_addr_index:
22453 case DW_FORM_GNU_str_index:
d521ce57 22454 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
22455 if (bytes == NULL)
22456 {
22457 dwarf2_section_buffer_overflow_complaint (section);
22458 return NULL;
22459 }
cf2c3c16
TT
22460 break;
22461
663c44ac
JK
22462 case DW_FORM_implicit_const:
22463 break;
22464
cf2c3c16
TT
22465 default:
22466 {
22467 complain:
22468 complaint (&symfile_complaints,
22469 _("invalid form 0x%x in `%s'"),
a32a8923 22470 form, get_section_name (section));
cf2c3c16
TT
22471 return NULL;
22472 }
2e276125
JB
22473 }
22474
cf2c3c16
TT
22475 return bytes;
22476}
757a13d0 22477
cf2c3c16
TT
22478/* A helper for dwarf_decode_macros that handles skipping an unknown
22479 opcode. Returns an updated pointer to the macro data buffer; or,
22480 on error, issues a complaint and returns NULL. */
757a13d0 22481
d521ce57 22482static const gdb_byte *
cf2c3c16 22483skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
22484 const gdb_byte **opcode_definitions,
22485 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
22486 bfd *abfd,
22487 unsigned int offset_size,
22488 struct dwarf2_section_info *section)
22489{
22490 unsigned int bytes_read, i;
22491 unsigned long arg;
d521ce57 22492 const gdb_byte *defn;
2e276125 22493
cf2c3c16 22494 if (opcode_definitions[opcode] == NULL)
2e276125 22495 {
cf2c3c16
TT
22496 complaint (&symfile_complaints,
22497 _("unrecognized DW_MACFINO opcode 0x%x"),
22498 opcode);
22499 return NULL;
22500 }
2e276125 22501
cf2c3c16
TT
22502 defn = opcode_definitions[opcode];
22503 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22504 defn += bytes_read;
2e276125 22505
cf2c3c16
TT
22506 for (i = 0; i < arg; ++i)
22507 {
aead7601
SM
22508 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22509 (enum dwarf_form) defn[i], offset_size,
f664829e 22510 section);
cf2c3c16
TT
22511 if (mac_ptr == NULL)
22512 {
22513 /* skip_form_bytes already issued the complaint. */
22514 return NULL;
22515 }
22516 }
757a13d0 22517
cf2c3c16
TT
22518 return mac_ptr;
22519}
757a13d0 22520
cf2c3c16
TT
22521/* A helper function which parses the header of a macro section.
22522 If the macro section is the extended (for now called "GNU") type,
22523 then this updates *OFFSET_SIZE. Returns a pointer to just after
22524 the header, or issues a complaint and returns NULL on error. */
757a13d0 22525
d521ce57
TT
22526static const gdb_byte *
22527dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22528 bfd *abfd,
d521ce57 22529 const gdb_byte *mac_ptr,
cf2c3c16
TT
22530 unsigned int *offset_size,
22531 int section_is_gnu)
22532{
22533 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22534
cf2c3c16
TT
22535 if (section_is_gnu)
22536 {
22537 unsigned int version, flags;
757a13d0 22538
cf2c3c16 22539 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22540 if (version != 4 && version != 5)
cf2c3c16
TT
22541 {
22542 complaint (&symfile_complaints,
22543 _("unrecognized version `%d' in .debug_macro section"),
22544 version);
22545 return NULL;
22546 }
22547 mac_ptr += 2;
757a13d0 22548
cf2c3c16
TT
22549 flags = read_1_byte (abfd, mac_ptr);
22550 ++mac_ptr;
22551 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22552
cf2c3c16
TT
22553 if ((flags & 2) != 0)
22554 /* We don't need the line table offset. */
22555 mac_ptr += *offset_size;
757a13d0 22556
cf2c3c16
TT
22557 /* Vendor opcode descriptions. */
22558 if ((flags & 4) != 0)
22559 {
22560 unsigned int i, count;
757a13d0 22561
cf2c3c16
TT
22562 count = read_1_byte (abfd, mac_ptr);
22563 ++mac_ptr;
22564 for (i = 0; i < count; ++i)
22565 {
22566 unsigned int opcode, bytes_read;
22567 unsigned long arg;
22568
22569 opcode = read_1_byte (abfd, mac_ptr);
22570 ++mac_ptr;
22571 opcode_definitions[opcode] = mac_ptr;
22572 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22573 mac_ptr += bytes_read;
22574 mac_ptr += arg;
22575 }
757a13d0 22576 }
cf2c3c16 22577 }
757a13d0 22578
cf2c3c16
TT
22579 return mac_ptr;
22580}
757a13d0 22581
cf2c3c16 22582/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22583 including DW_MACRO_import. */
cf2c3c16
TT
22584
22585static void
d521ce57
TT
22586dwarf_decode_macro_bytes (bfd *abfd,
22587 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22588 struct macro_source_file *current_file,
43f3e411 22589 struct line_header *lh,
cf2c3c16 22590 struct dwarf2_section_info *section,
36586728 22591 int section_is_gnu, int section_is_dwz,
cf2c3c16 22592 unsigned int offset_size,
8fc3fc34 22593 htab_t include_hash)
cf2c3c16 22594{
4d663531 22595 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22596 enum dwarf_macro_record_type macinfo_type;
22597 int at_commandline;
d521ce57 22598 const gdb_byte *opcode_definitions[256];
757a13d0 22599
cf2c3c16
TT
22600 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22601 &offset_size, section_is_gnu);
22602 if (mac_ptr == NULL)
22603 {
22604 /* We already issued a complaint. */
22605 return;
22606 }
757a13d0
JK
22607
22608 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22609 GDB is still reading the definitions from command line. First
22610 DW_MACINFO_start_file will need to be ignored as it was already executed
22611 to create CURRENT_FILE for the main source holding also the command line
22612 definitions. On first met DW_MACINFO_start_file this flag is reset to
22613 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22614
22615 at_commandline = 1;
22616
22617 do
22618 {
22619 /* Do we at least have room for a macinfo type byte? */
22620 if (mac_ptr >= mac_end)
22621 {
f664829e 22622 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22623 break;
22624 }
22625
aead7601 22626 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22627 mac_ptr++;
22628
cf2c3c16
TT
22629 /* Note that we rely on the fact that the corresponding GNU and
22630 DWARF constants are the same. */
757a13d0
JK
22631 switch (macinfo_type)
22632 {
22633 /* A zero macinfo type indicates the end of the macro
22634 information. */
22635 case 0:
22636 break;
2e276125 22637
0af92d60
JK
22638 case DW_MACRO_define:
22639 case DW_MACRO_undef:
22640 case DW_MACRO_define_strp:
22641 case DW_MACRO_undef_strp:
22642 case DW_MACRO_define_sup:
22643 case DW_MACRO_undef_sup:
2e276125 22644 {
891d2f0b 22645 unsigned int bytes_read;
2e276125 22646 int line;
d521ce57 22647 const char *body;
cf2c3c16 22648 int is_define;
2e276125 22649
cf2c3c16
TT
22650 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22651 mac_ptr += bytes_read;
22652
0af92d60
JK
22653 if (macinfo_type == DW_MACRO_define
22654 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22655 {
22656 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22657 mac_ptr += bytes_read;
22658 }
22659 else
22660 {
22661 LONGEST str_offset;
22662
22663 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22664 mac_ptr += offset_size;
2e276125 22665
0af92d60
JK
22666 if (macinfo_type == DW_MACRO_define_sup
22667 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22668 || section_is_dwz)
36586728
TT
22669 {
22670 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22671
22672 body = read_indirect_string_from_dwz (dwz, str_offset);
22673 }
22674 else
22675 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22676 }
22677
0af92d60
JK
22678 is_define = (macinfo_type == DW_MACRO_define
22679 || macinfo_type == DW_MACRO_define_strp
22680 || macinfo_type == DW_MACRO_define_sup);
2e276125 22681 if (! current_file)
757a13d0
JK
22682 {
22683 /* DWARF violation as no main source is present. */
22684 complaint (&symfile_complaints,
22685 _("debug info with no main source gives macro %s "
22686 "on line %d: %s"),
cf2c3c16
TT
22687 is_define ? _("definition") : _("undefinition"),
22688 line, body);
757a13d0
JK
22689 break;
22690 }
3e43a32a
MS
22691 if ((line == 0 && !at_commandline)
22692 || (line != 0 && at_commandline))
4d3c2250 22693 complaint (&symfile_complaints,
757a13d0
JK
22694 _("debug info gives %s macro %s with %s line %d: %s"),
22695 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22696 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22697 line == 0 ? _("zero") : _("non-zero"), line, body);
22698
cf2c3c16 22699 if (is_define)
757a13d0 22700 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22701 else
22702 {
0af92d60
JK
22703 gdb_assert (macinfo_type == DW_MACRO_undef
22704 || macinfo_type == DW_MACRO_undef_strp
22705 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22706 macro_undef (current_file, line, body);
22707 }
2e276125
JB
22708 }
22709 break;
22710
0af92d60 22711 case DW_MACRO_start_file:
2e276125 22712 {
891d2f0b 22713 unsigned int bytes_read;
2e276125
JB
22714 int line, file;
22715
22716 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22717 mac_ptr += bytes_read;
22718 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22719 mac_ptr += bytes_read;
22720
3e43a32a
MS
22721 if ((line == 0 && !at_commandline)
22722 || (line != 0 && at_commandline))
757a13d0
JK
22723 complaint (&symfile_complaints,
22724 _("debug info gives source %d included "
22725 "from %s at %s line %d"),
22726 file, at_commandline ? _("command-line") : _("file"),
22727 line == 0 ? _("zero") : _("non-zero"), line);
22728
22729 if (at_commandline)
22730 {
0af92d60 22731 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22732 pass one. */
757a13d0
JK
22733 at_commandline = 0;
22734 }
22735 else
43f3e411 22736 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22737 }
22738 break;
22739
0af92d60 22740 case DW_MACRO_end_file:
2e276125 22741 if (! current_file)
4d3c2250 22742 complaint (&symfile_complaints,
3e43a32a
MS
22743 _("macro debug info has an unmatched "
22744 "`close_file' directive"));
2e276125
JB
22745 else
22746 {
22747 current_file = current_file->included_by;
22748 if (! current_file)
22749 {
cf2c3c16 22750 enum dwarf_macro_record_type next_type;
2e276125
JB
22751
22752 /* GCC circa March 2002 doesn't produce the zero
22753 type byte marking the end of the compilation
22754 unit. Complain if it's not there, but exit no
22755 matter what. */
22756
22757 /* Do we at least have room for a macinfo type byte? */
22758 if (mac_ptr >= mac_end)
22759 {
f664829e 22760 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22761 return;
22762 }
22763
22764 /* We don't increment mac_ptr here, so this is just
22765 a look-ahead. */
aead7601
SM
22766 next_type
22767 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22768 mac_ptr);
2e276125 22769 if (next_type != 0)
4d3c2250 22770 complaint (&symfile_complaints,
3e43a32a
MS
22771 _("no terminating 0-type entry for "
22772 "macros in `.debug_macinfo' section"));
2e276125
JB
22773
22774 return;
22775 }
22776 }
22777 break;
22778
0af92d60
JK
22779 case DW_MACRO_import:
22780 case DW_MACRO_import_sup:
cf2c3c16
TT
22781 {
22782 LONGEST offset;
8fc3fc34 22783 void **slot;
a036ba48
TT
22784 bfd *include_bfd = abfd;
22785 struct dwarf2_section_info *include_section = section;
d521ce57 22786 const gdb_byte *include_mac_end = mac_end;
a036ba48 22787 int is_dwz = section_is_dwz;
d521ce57 22788 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22789
22790 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22791 mac_ptr += offset_size;
22792
0af92d60 22793 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22794 {
22795 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22796
4d663531 22797 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22798
a036ba48 22799 include_section = &dwz->macro;
a32a8923 22800 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22801 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22802 is_dwz = 1;
22803 }
22804
22805 new_mac_ptr = include_section->buffer + offset;
22806 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22807
8fc3fc34
TT
22808 if (*slot != NULL)
22809 {
22810 /* This has actually happened; see
22811 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22812 complaint (&symfile_complaints,
0af92d60 22813 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22814 ".debug_macro section"));
22815 }
22816 else
22817 {
d521ce57 22818 *slot = (void *) new_mac_ptr;
36586728 22819
a036ba48 22820 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22821 include_mac_end, current_file, lh,
36586728 22822 section, section_is_gnu, is_dwz,
4d663531 22823 offset_size, include_hash);
8fc3fc34 22824
d521ce57 22825 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22826 }
cf2c3c16
TT
22827 }
22828 break;
22829
2e276125 22830 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22831 if (!section_is_gnu)
22832 {
22833 unsigned int bytes_read;
2e276125 22834
ac298888
TT
22835 /* This reads the constant, but since we don't recognize
22836 any vendor extensions, we ignore it. */
22837 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22838 mac_ptr += bytes_read;
22839 read_direct_string (abfd, mac_ptr, &bytes_read);
22840 mac_ptr += bytes_read;
2e276125 22841
cf2c3c16
TT
22842 /* We don't recognize any vendor extensions. */
22843 break;
22844 }
22845 /* FALLTHROUGH */
22846
22847 default:
22848 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22849 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22850 section);
22851 if (mac_ptr == NULL)
22852 return;
22853 break;
2e276125 22854 }
757a13d0 22855 } while (macinfo_type != 0);
2e276125 22856}
8e19ed76 22857
cf2c3c16 22858static void
09262596 22859dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22860 int section_is_gnu)
cf2c3c16 22861{
bb5ed363 22862 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22863 struct line_header *lh = cu->line_header;
22864 bfd *abfd;
d521ce57 22865 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22866 struct macro_source_file *current_file = 0;
22867 enum dwarf_macro_record_type macinfo_type;
22868 unsigned int offset_size = cu->header.offset_size;
d521ce57 22869 const gdb_byte *opcode_definitions[256];
8fc3fc34 22870 void **slot;
09262596
DE
22871 struct dwarf2_section_info *section;
22872 const char *section_name;
22873
22874 if (cu->dwo_unit != NULL)
22875 {
22876 if (section_is_gnu)
22877 {
22878 section = &cu->dwo_unit->dwo_file->sections.macro;
22879 section_name = ".debug_macro.dwo";
22880 }
22881 else
22882 {
22883 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22884 section_name = ".debug_macinfo.dwo";
22885 }
22886 }
22887 else
22888 {
22889 if (section_is_gnu)
22890 {
22891 section = &dwarf2_per_objfile->macro;
22892 section_name = ".debug_macro";
22893 }
22894 else
22895 {
22896 section = &dwarf2_per_objfile->macinfo;
22897 section_name = ".debug_macinfo";
22898 }
22899 }
cf2c3c16 22900
bb5ed363 22901 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22902 if (section->buffer == NULL)
22903 {
fceca515 22904 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22905 return;
22906 }
a32a8923 22907 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22908
22909 /* First pass: Find the name of the base filename.
22910 This filename is needed in order to process all macros whose definition
22911 (or undefinition) comes from the command line. These macros are defined
22912 before the first DW_MACINFO_start_file entry, and yet still need to be
22913 associated to the base file.
22914
22915 To determine the base file name, we scan the macro definitions until we
22916 reach the first DW_MACINFO_start_file entry. We then initialize
22917 CURRENT_FILE accordingly so that any macro definition found before the
22918 first DW_MACINFO_start_file can still be associated to the base file. */
22919
22920 mac_ptr = section->buffer + offset;
22921 mac_end = section->buffer + section->size;
22922
22923 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22924 &offset_size, section_is_gnu);
22925 if (mac_ptr == NULL)
22926 {
22927 /* We already issued a complaint. */
22928 return;
22929 }
22930
22931 do
22932 {
22933 /* Do we at least have room for a macinfo type byte? */
22934 if (mac_ptr >= mac_end)
22935 {
22936 /* Complaint is printed during the second pass as GDB will probably
22937 stop the first pass earlier upon finding
22938 DW_MACINFO_start_file. */
22939 break;
22940 }
22941
aead7601 22942 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22943 mac_ptr++;
22944
22945 /* Note that we rely on the fact that the corresponding GNU and
22946 DWARF constants are the same. */
22947 switch (macinfo_type)
22948 {
22949 /* A zero macinfo type indicates the end of the macro
22950 information. */
22951 case 0:
22952 break;
22953
0af92d60
JK
22954 case DW_MACRO_define:
22955 case DW_MACRO_undef:
cf2c3c16
TT
22956 /* Only skip the data by MAC_PTR. */
22957 {
22958 unsigned int bytes_read;
22959
22960 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22961 mac_ptr += bytes_read;
22962 read_direct_string (abfd, mac_ptr, &bytes_read);
22963 mac_ptr += bytes_read;
22964 }
22965 break;
22966
0af92d60 22967 case DW_MACRO_start_file:
cf2c3c16
TT
22968 {
22969 unsigned int bytes_read;
22970 int line, file;
22971
22972 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22973 mac_ptr += bytes_read;
22974 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22975 mac_ptr += bytes_read;
22976
43f3e411 22977 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22978 }
22979 break;
22980
0af92d60 22981 case DW_MACRO_end_file:
cf2c3c16
TT
22982 /* No data to skip by MAC_PTR. */
22983 break;
22984
0af92d60
JK
22985 case DW_MACRO_define_strp:
22986 case DW_MACRO_undef_strp:
22987 case DW_MACRO_define_sup:
22988 case DW_MACRO_undef_sup:
cf2c3c16
TT
22989 {
22990 unsigned int bytes_read;
22991
22992 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22993 mac_ptr += bytes_read;
22994 mac_ptr += offset_size;
22995 }
22996 break;
22997
0af92d60
JK
22998 case DW_MACRO_import:
22999 case DW_MACRO_import_sup:
cf2c3c16 23000 /* Note that, according to the spec, a transparent include
0af92d60 23001 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23002 skip this opcode. */
23003 mac_ptr += offset_size;
23004 break;
23005
23006 case DW_MACINFO_vendor_ext:
23007 /* Only skip the data by MAC_PTR. */
23008 if (!section_is_gnu)
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 /* FALLTHROUGH */
23018
23019 default:
23020 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23021 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23022 section);
23023 if (mac_ptr == NULL)
23024 return;
23025 break;
23026 }
23027 } while (macinfo_type != 0 && current_file == NULL);
23028
23029 /* Second pass: Process all entries.
23030
23031 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23032 command-line macro definitions/undefinitions. This flag is unset when we
23033 reach the first DW_MACINFO_start_file entry. */
23034
fc4007c9
TT
23035 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23036 htab_eq_pointer,
23037 NULL, xcalloc, xfree));
8fc3fc34 23038 mac_ptr = section->buffer + offset;
fc4007c9 23039 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23040 *slot = (void *) mac_ptr;
8fc3fc34 23041 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 23042 current_file, lh, section,
fc4007c9
TT
23043 section_is_gnu, 0, offset_size,
23044 include_hash.get ());
cf2c3c16
TT
23045}
23046
8e19ed76 23047/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 23048 if so return true else false. */
380bca97 23049
8e19ed76 23050static int
6e5a29e1 23051attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
23052{
23053 return (attr == NULL ? 0 :
23054 attr->form == DW_FORM_block1
23055 || attr->form == DW_FORM_block2
23056 || attr->form == DW_FORM_block4
2dc7f7b3
TT
23057 || attr->form == DW_FORM_block
23058 || attr->form == DW_FORM_exprloc);
8e19ed76 23059}
4c2df51b 23060
c6a0999f
JB
23061/* Return non-zero if ATTR's value is a section offset --- classes
23062 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23063 You may use DW_UNSND (attr) to retrieve such offsets.
23064
23065 Section 7.5.4, "Attribute Encodings", explains that no attribute
23066 may have a value that belongs to more than one of these classes; it
23067 would be ambiguous if we did, because we use the same forms for all
23068 of them. */
380bca97 23069
3690dd37 23070static int
6e5a29e1 23071attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
23072{
23073 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
23074 || attr->form == DW_FORM_data8
23075 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
23076}
23077
3690dd37
JB
23078/* Return non-zero if ATTR's value falls in the 'constant' class, or
23079 zero otherwise. When this function returns true, you can apply
23080 dwarf2_get_attr_constant_value to it.
23081
23082 However, note that for some attributes you must check
23083 attr_form_is_section_offset before using this test. DW_FORM_data4
23084 and DW_FORM_data8 are members of both the constant class, and of
23085 the classes that contain offsets into other debug sections
23086 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23087 that, if an attribute's can be either a constant or one of the
23088 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
23089 taken as section offsets, not constants.
23090
23091 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23092 cannot handle that. */
380bca97 23093
3690dd37 23094static int
6e5a29e1 23095attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
23096{
23097 switch (attr->form)
23098 {
23099 case DW_FORM_sdata:
23100 case DW_FORM_udata:
23101 case DW_FORM_data1:
23102 case DW_FORM_data2:
23103 case DW_FORM_data4:
23104 case DW_FORM_data8:
663c44ac 23105 case DW_FORM_implicit_const:
3690dd37
JB
23106 return 1;
23107 default:
23108 return 0;
23109 }
23110}
23111
7771576e
SA
23112
23113/* DW_ADDR is always stored already as sect_offset; despite for the forms
23114 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23115
23116static int
6e5a29e1 23117attr_form_is_ref (const struct attribute *attr)
7771576e
SA
23118{
23119 switch (attr->form)
23120 {
23121 case DW_FORM_ref_addr:
23122 case DW_FORM_ref1:
23123 case DW_FORM_ref2:
23124 case DW_FORM_ref4:
23125 case DW_FORM_ref8:
23126 case DW_FORM_ref_udata:
23127 case DW_FORM_GNU_ref_alt:
23128 return 1;
23129 default:
23130 return 0;
23131 }
23132}
23133
3019eac3
DE
23134/* Return the .debug_loc section to use for CU.
23135 For DWO files use .debug_loc.dwo. */
23136
23137static struct dwarf2_section_info *
23138cu_debug_loc_section (struct dwarf2_cu *cu)
23139{
23140 if (cu->dwo_unit)
43988095
JK
23141 {
23142 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23143
23144 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23145 }
23146 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23147 : &dwarf2_per_objfile->loc);
3019eac3
DE
23148}
23149
8cf6f0b1
TT
23150/* A helper function that fills in a dwarf2_loclist_baton. */
23151
23152static void
23153fill_in_loclist_baton (struct dwarf2_cu *cu,
23154 struct dwarf2_loclist_baton *baton,
ff39bb5e 23155 const struct attribute *attr)
8cf6f0b1 23156{
3019eac3
DE
23157 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23158
23159 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
23160
23161 baton->per_cu = cu->per_cu;
23162 gdb_assert (baton->per_cu);
23163 /* We don't know how long the location list is, but make sure we
23164 don't run off the edge of the section. */
3019eac3
DE
23165 baton->size = section->size - DW_UNSND (attr);
23166 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23167 baton->base_address = cu->base_address;
f664829e 23168 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23169}
23170
4c2df51b 23171static void
ff39bb5e 23172dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23173 struct dwarf2_cu *cu, int is_block)
4c2df51b 23174{
bb5ed363 23175 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23176 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23177
3690dd37 23178 if (attr_form_is_section_offset (attr)
3019eac3 23179 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23180 the section. If so, fall through to the complaint in the
23181 other branch. */
3019eac3 23182 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 23183 {
0d53c4c4 23184 struct dwarf2_loclist_baton *baton;
4c2df51b 23185
8d749320 23186 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23187
8cf6f0b1 23188 fill_in_loclist_baton (cu, baton, attr);
be391dca 23189
d00adf39 23190 if (cu->base_known == 0)
0d53c4c4 23191 complaint (&symfile_complaints,
3e43a32a
MS
23192 _("Location list used without "
23193 "specifying the CU base address."));
4c2df51b 23194
f1e6e072
TT
23195 SYMBOL_ACLASS_INDEX (sym) = (is_block
23196 ? dwarf2_loclist_block_index
23197 : dwarf2_loclist_index);
0d53c4c4
DJ
23198 SYMBOL_LOCATION_BATON (sym) = baton;
23199 }
23200 else
23201 {
23202 struct dwarf2_locexpr_baton *baton;
23203
8d749320 23204 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23205 baton->per_cu = cu->per_cu;
23206 gdb_assert (baton->per_cu);
0d53c4c4
DJ
23207
23208 if (attr_form_is_block (attr))
23209 {
23210 /* Note that we're just copying the block's data pointer
23211 here, not the actual data. We're still pointing into the
6502dd73
DJ
23212 info_buffer for SYM's objfile; right now we never release
23213 that buffer, but when we do clean up properly this may
23214 need to change. */
0d53c4c4
DJ
23215 baton->size = DW_BLOCK (attr)->size;
23216 baton->data = DW_BLOCK (attr)->data;
23217 }
23218 else
23219 {
23220 dwarf2_invalid_attrib_class_complaint ("location description",
23221 SYMBOL_NATURAL_NAME (sym));
23222 baton->size = 0;
0d53c4c4 23223 }
6e70227d 23224
f1e6e072
TT
23225 SYMBOL_ACLASS_INDEX (sym) = (is_block
23226 ? dwarf2_locexpr_block_index
23227 : dwarf2_locexpr_index);
0d53c4c4
DJ
23228 SYMBOL_LOCATION_BATON (sym) = baton;
23229 }
4c2df51b 23230}
6502dd73 23231
9aa1f1e3
TT
23232/* Return the OBJFILE associated with the compilation unit CU. If CU
23233 came from a separate debuginfo file, then the master objfile is
23234 returned. */
ae0d2f24
UW
23235
23236struct objfile *
23237dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23238{
9291a0cd 23239 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
23240
23241 /* Return the master objfile, so that we can report and look up the
23242 correct file containing this variable. */
23243 if (objfile->separate_debug_objfile_backlink)
23244 objfile = objfile->separate_debug_objfile_backlink;
23245
23246 return objfile;
23247}
23248
96408a79
SA
23249/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23250 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23251 CU_HEADERP first. */
23252
23253static const struct comp_unit_head *
23254per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23255 struct dwarf2_per_cu_data *per_cu)
23256{
d521ce57 23257 const gdb_byte *info_ptr;
96408a79
SA
23258
23259 if (per_cu->cu)
23260 return &per_cu->cu->header;
23261
9c541725 23262 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23263
23264 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23265 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23266 rcuh_kind::COMPILE);
96408a79
SA
23267
23268 return cu_headerp;
23269}
23270
ae0d2f24
UW
23271/* Return the address size given in the compilation unit header for CU. */
23272
98714339 23273int
ae0d2f24
UW
23274dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23275{
96408a79
SA
23276 struct comp_unit_head cu_header_local;
23277 const struct comp_unit_head *cu_headerp;
c471e790 23278
96408a79
SA
23279 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23280
23281 return cu_headerp->addr_size;
ae0d2f24
UW
23282}
23283
9eae7c52
TT
23284/* Return the offset size given in the compilation unit header for CU. */
23285
23286int
23287dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23288{
96408a79
SA
23289 struct comp_unit_head cu_header_local;
23290 const struct comp_unit_head *cu_headerp;
9c6c53f7 23291
96408a79
SA
23292 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23293
23294 return cu_headerp->offset_size;
23295}
23296
23297/* See its dwarf2loc.h declaration. */
23298
23299int
23300dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23301{
23302 struct comp_unit_head cu_header_local;
23303 const struct comp_unit_head *cu_headerp;
23304
23305 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23306
23307 if (cu_headerp->version == 2)
23308 return cu_headerp->addr_size;
23309 else
23310 return cu_headerp->offset_size;
181cebd4
JK
23311}
23312
9aa1f1e3
TT
23313/* Return the text offset of the CU. The returned offset comes from
23314 this CU's objfile. If this objfile came from a separate debuginfo
23315 file, then the offset may be different from the corresponding
23316 offset in the parent objfile. */
23317
23318CORE_ADDR
23319dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23320{
bb3fa9d0 23321 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
23322
23323 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23324}
23325
43988095
JK
23326/* Return DWARF version number of PER_CU. */
23327
23328short
23329dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23330{
23331 return per_cu->dwarf_version;
23332}
23333
348e048f
DE
23334/* Locate the .debug_info compilation unit from CU's objfile which contains
23335 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
23336
23337static struct dwarf2_per_cu_data *
9c541725 23338dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 23339 unsigned int offset_in_dwz,
ae038cb0
DJ
23340 struct objfile *objfile)
23341{
23342 struct dwarf2_per_cu_data *this_cu;
23343 int low, high;
36586728 23344 const sect_offset *cu_off;
ae038cb0 23345
ae038cb0
DJ
23346 low = 0;
23347 high = dwarf2_per_objfile->n_comp_units - 1;
23348 while (high > low)
23349 {
36586728 23350 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23351 int mid = low + (high - low) / 2;
9a619af0 23352
36586728 23353 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 23354 cu_off = &mid_cu->sect_off;
36586728 23355 if (mid_cu->is_dwz > offset_in_dwz
9c541725 23356 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
23357 high = mid;
23358 else
23359 low = mid + 1;
23360 }
23361 gdb_assert (low == high);
36586728 23362 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
23363 cu_off = &this_cu->sect_off;
23364 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 23365 {
36586728 23366 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23367 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
23368 "offset 0x%x [in module %s]"),
23369 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 23370
9c541725
PA
23371 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23372 <= sect_off);
ae038cb0
DJ
23373 return dwarf2_per_objfile->all_comp_units[low-1];
23374 }
23375 else
23376 {
23377 this_cu = dwarf2_per_objfile->all_comp_units[low];
23378 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
23379 && sect_off >= this_cu->sect_off + this_cu->length)
23380 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23381 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23382 return this_cu;
23383 }
23384}
23385
23745b47 23386/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23387
9816fde3 23388static void
23745b47 23389init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 23390{
9816fde3 23391 memset (cu, 0, sizeof (*cu));
23745b47
DE
23392 per_cu->cu = cu;
23393 cu->per_cu = per_cu;
23394 cu->objfile = per_cu->objfile;
93311388 23395 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
23396}
23397
23398/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23399
23400static void
95554aad
TT
23401prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23402 enum language pretend_language)
9816fde3
JK
23403{
23404 struct attribute *attr;
23405
23406 /* Set the language we're debugging. */
23407 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23408 if (attr)
23409 set_cu_language (DW_UNSND (attr), cu);
23410 else
9cded63f 23411 {
95554aad 23412 cu->language = pretend_language;
9cded63f
TT
23413 cu->language_defn = language_def (cu->language);
23414 }
dee91e82 23415
7d45c7c3 23416 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23417}
23418
ae038cb0
DJ
23419/* Release one cached compilation unit, CU. We unlink it from the tree
23420 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
23421 the caller is responsible for that.
23422 NOTE: DATA is a void * because this function is also used as a
23423 cleanup routine. */
ae038cb0
DJ
23424
23425static void
68dc6402 23426free_heap_comp_unit (void *data)
ae038cb0 23427{
9a3c8263 23428 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 23429
23745b47
DE
23430 gdb_assert (cu->per_cu != NULL);
23431 cu->per_cu->cu = NULL;
ae038cb0
DJ
23432 cu->per_cu = NULL;
23433
23434 obstack_free (&cu->comp_unit_obstack, NULL);
23435
23436 xfree (cu);
23437}
23438
72bf9492 23439/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 23440 when we're finished with it. We can't free the pointer itself, but be
dee91e82 23441 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
23442
23443static void
23444free_stack_comp_unit (void *data)
23445{
9a3c8263 23446 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 23447
23745b47
DE
23448 gdb_assert (cu->per_cu != NULL);
23449 cu->per_cu->cu = NULL;
23450 cu->per_cu = NULL;
23451
72bf9492
DJ
23452 obstack_free (&cu->comp_unit_obstack, NULL);
23453 cu->partial_dies = NULL;
ae038cb0
DJ
23454}
23455
23456/* Free all cached compilation units. */
23457
23458static void
23459free_cached_comp_units (void *data)
23460{
330cdd98 23461 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
23462}
23463
23464/* Increase the age counter on each cached compilation unit, and free
23465 any that are too old. */
23466
23467static void
23468age_cached_comp_units (void)
23469{
23470 struct dwarf2_per_cu_data *per_cu, **last_chain;
23471
23472 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23473 per_cu = dwarf2_per_objfile->read_in_chain;
23474 while (per_cu != NULL)
23475 {
23476 per_cu->cu->last_used ++;
b4f54984 23477 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23478 dwarf2_mark (per_cu->cu);
23479 per_cu = per_cu->cu->read_in_chain;
23480 }
23481
23482 per_cu = dwarf2_per_objfile->read_in_chain;
23483 last_chain = &dwarf2_per_objfile->read_in_chain;
23484 while (per_cu != NULL)
23485 {
23486 struct dwarf2_per_cu_data *next_cu;
23487
23488 next_cu = per_cu->cu->read_in_chain;
23489
23490 if (!per_cu->cu->mark)
23491 {
68dc6402 23492 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
23493 *last_chain = next_cu;
23494 }
23495 else
23496 last_chain = &per_cu->cu->read_in_chain;
23497
23498 per_cu = next_cu;
23499 }
23500}
23501
23502/* Remove a single compilation unit from the cache. */
23503
23504static void
dee91e82 23505free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23506{
23507 struct dwarf2_per_cu_data *per_cu, **last_chain;
23508
23509 per_cu = dwarf2_per_objfile->read_in_chain;
23510 last_chain = &dwarf2_per_objfile->read_in_chain;
23511 while (per_cu != NULL)
23512 {
23513 struct dwarf2_per_cu_data *next_cu;
23514
23515 next_cu = per_cu->cu->read_in_chain;
23516
dee91e82 23517 if (per_cu == target_per_cu)
ae038cb0 23518 {
68dc6402 23519 free_heap_comp_unit (per_cu->cu);
dee91e82 23520 per_cu->cu = NULL;
ae038cb0
DJ
23521 *last_chain = next_cu;
23522 break;
23523 }
23524 else
23525 last_chain = &per_cu->cu->read_in_chain;
23526
23527 per_cu = next_cu;
23528 }
23529}
23530
fe3e1990
DJ
23531/* Release all extra memory associated with OBJFILE. */
23532
23533void
23534dwarf2_free_objfile (struct objfile *objfile)
23535{
9a3c8263
SM
23536 dwarf2_per_objfile
23537 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23538 dwarf2_objfile_data_key);
fe3e1990
DJ
23539
23540 if (dwarf2_per_objfile == NULL)
23541 return;
23542
330cdd98 23543 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23544}
23545
dee91e82
DE
23546/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23547 We store these in a hash table separate from the DIEs, and preserve them
23548 when the DIEs are flushed out of cache.
23549
23550 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23551 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23552 or the type may come from a DWO file. Furthermore, while it's more logical
23553 to use per_cu->section+offset, with Fission the section with the data is in
23554 the DWO file but we don't know that section at the point we need it.
23555 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23556 because we can enter the lookup routine, get_die_type_at_offset, from
23557 outside this file, and thus won't necessarily have PER_CU->cu.
23558 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23559
dee91e82 23560struct dwarf2_per_cu_offset_and_type
1c379e20 23561{
dee91e82 23562 const struct dwarf2_per_cu_data *per_cu;
9c541725 23563 sect_offset sect_off;
1c379e20
DJ
23564 struct type *type;
23565};
23566
dee91e82 23567/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23568
23569static hashval_t
dee91e82 23570per_cu_offset_and_type_hash (const void *item)
1c379e20 23571{
9a3c8263
SM
23572 const struct dwarf2_per_cu_offset_and_type *ofs
23573 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23574
9c541725 23575 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23576}
23577
dee91e82 23578/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23579
23580static int
dee91e82 23581per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23582{
9a3c8263
SM
23583 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23584 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23585 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23586 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23587
dee91e82 23588 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23589 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23590}
23591
23592/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23593 table if necessary. For convenience, return TYPE.
23594
23595 The DIEs reading must have careful ordering to:
23596 * Not cause infite loops trying to read in DIEs as a prerequisite for
23597 reading current DIE.
23598 * Not trying to dereference contents of still incompletely read in types
23599 while reading in other DIEs.
23600 * Enable referencing still incompletely read in types just by a pointer to
23601 the type without accessing its fields.
23602
23603 Therefore caller should follow these rules:
23604 * Try to fetch any prerequisite types we may need to build this DIE type
23605 before building the type and calling set_die_type.
e71ec853 23606 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23607 possible before fetching more types to complete the current type.
23608 * Make the type as complete as possible before fetching more types. */
1c379e20 23609
f792889a 23610static struct type *
1c379e20
DJ
23611set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23612{
dee91e82 23613 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23614 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23615 struct attribute *attr;
23616 struct dynamic_prop prop;
1c379e20 23617
b4ba55a1
JB
23618 /* For Ada types, make sure that the gnat-specific data is always
23619 initialized (if not already set). There are a few types where
23620 we should not be doing so, because the type-specific area is
23621 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23622 where the type-specific area is used to store the floatformat).
23623 But this is not a problem, because the gnat-specific information
23624 is actually not needed for these types. */
23625 if (need_gnat_info (cu)
23626 && TYPE_CODE (type) != TYPE_CODE_FUNC
23627 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23628 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23629 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23630 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23631 && !HAVE_GNAT_AUX_INFO (type))
23632 INIT_GNAT_SPECIFIC (type);
23633
3f2f83dd
KB
23634 /* Read DW_AT_allocated and set in type. */
23635 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23636 if (attr_form_is_block (attr))
23637 {
23638 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23639 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23640 }
23641 else if (attr != NULL)
23642 {
23643 complaint (&symfile_complaints,
9c541725
PA
23644 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23645 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23646 to_underlying (die->sect_off));
3f2f83dd
KB
23647 }
23648
23649 /* Read DW_AT_associated and set in type. */
23650 attr = dwarf2_attr (die, DW_AT_associated, cu);
23651 if (attr_form_is_block (attr))
23652 {
23653 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23654 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23655 }
23656 else if (attr != NULL)
23657 {
23658 complaint (&symfile_complaints,
9c541725
PA
23659 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23660 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23661 to_underlying (die->sect_off));
3f2f83dd
KB
23662 }
23663
3cdcd0ce
JB
23664 /* Read DW_AT_data_location and set in type. */
23665 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23666 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23667 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23668
dee91e82 23669 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23670 {
dee91e82
DE
23671 dwarf2_per_objfile->die_type_hash =
23672 htab_create_alloc_ex (127,
23673 per_cu_offset_and_type_hash,
23674 per_cu_offset_and_type_eq,
23675 NULL,
23676 &objfile->objfile_obstack,
23677 hashtab_obstack_allocate,
23678 dummy_obstack_deallocate);
f792889a 23679 }
1c379e20 23680
dee91e82 23681 ofs.per_cu = cu->per_cu;
9c541725 23682 ofs.sect_off = die->sect_off;
1c379e20 23683 ofs.type = type;
dee91e82
DE
23684 slot = (struct dwarf2_per_cu_offset_and_type **)
23685 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23686 if (*slot)
23687 complaint (&symfile_complaints,
23688 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23689 to_underlying (die->sect_off));
8d749320
SM
23690 *slot = XOBNEW (&objfile->objfile_obstack,
23691 struct dwarf2_per_cu_offset_and_type);
1c379e20 23692 **slot = ofs;
f792889a 23693 return type;
1c379e20
DJ
23694}
23695
9c541725 23696/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23697 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23698
23699static struct type *
9c541725 23700get_die_type_at_offset (sect_offset sect_off,
673bfd45 23701 struct dwarf2_per_cu_data *per_cu)
1c379e20 23702{
dee91e82 23703 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23704
dee91e82 23705 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23706 return NULL;
1c379e20 23707
dee91e82 23708 ofs.per_cu = per_cu;
9c541725 23709 ofs.sect_off = sect_off;
9a3c8263
SM
23710 slot = ((struct dwarf2_per_cu_offset_and_type *)
23711 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23712 if (slot)
23713 return slot->type;
23714 else
23715 return NULL;
23716}
23717
02142a6c 23718/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23719 or return NULL if DIE does not have a saved type. */
23720
23721static struct type *
23722get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23723{
9c541725 23724 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23725}
23726
10b3939b
DJ
23727/* Add a dependence relationship from CU to REF_PER_CU. */
23728
23729static void
23730dwarf2_add_dependence (struct dwarf2_cu *cu,
23731 struct dwarf2_per_cu_data *ref_per_cu)
23732{
23733 void **slot;
23734
23735 if (cu->dependencies == NULL)
23736 cu->dependencies
23737 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23738 NULL, &cu->comp_unit_obstack,
23739 hashtab_obstack_allocate,
23740 dummy_obstack_deallocate);
23741
23742 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23743 if (*slot == NULL)
23744 *slot = ref_per_cu;
23745}
1c379e20 23746
f504f079
DE
23747/* Subroutine of dwarf2_mark to pass to htab_traverse.
23748 Set the mark field in every compilation unit in the
ae038cb0
DJ
23749 cache that we must keep because we are keeping CU. */
23750
10b3939b
DJ
23751static int
23752dwarf2_mark_helper (void **slot, void *data)
23753{
23754 struct dwarf2_per_cu_data *per_cu;
23755
23756 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23757
23758 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23759 reading of the chain. As such dependencies remain valid it is not much
23760 useful to track and undo them during QUIT cleanups. */
23761 if (per_cu->cu == NULL)
23762 return 1;
23763
10b3939b
DJ
23764 if (per_cu->cu->mark)
23765 return 1;
23766 per_cu->cu->mark = 1;
23767
23768 if (per_cu->cu->dependencies != NULL)
23769 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23770
23771 return 1;
23772}
23773
f504f079
DE
23774/* Set the mark field in CU and in every other compilation unit in the
23775 cache that we must keep because we are keeping CU. */
23776
ae038cb0
DJ
23777static void
23778dwarf2_mark (struct dwarf2_cu *cu)
23779{
23780 if (cu->mark)
23781 return;
23782 cu->mark = 1;
10b3939b
DJ
23783 if (cu->dependencies != NULL)
23784 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23785}
23786
23787static void
23788dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23789{
23790 while (per_cu)
23791 {
23792 per_cu->cu->mark = 0;
23793 per_cu = per_cu->cu->read_in_chain;
23794 }
72bf9492
DJ
23795}
23796
72bf9492
DJ
23797/* Trivial hash function for partial_die_info: the hash value of a DIE
23798 is its offset in .debug_info for this objfile. */
23799
23800static hashval_t
23801partial_die_hash (const void *item)
23802{
9a3c8263
SM
23803 const struct partial_die_info *part_die
23804 = (const struct partial_die_info *) item;
9a619af0 23805
9c541725 23806 return to_underlying (part_die->sect_off);
72bf9492
DJ
23807}
23808
23809/* Trivial comparison function for partial_die_info structures: two DIEs
23810 are equal if they have the same offset. */
23811
23812static int
23813partial_die_eq (const void *item_lhs, const void *item_rhs)
23814{
9a3c8263
SM
23815 const struct partial_die_info *part_die_lhs
23816 = (const struct partial_die_info *) item_lhs;
23817 const struct partial_die_info *part_die_rhs
23818 = (const struct partial_die_info *) item_rhs;
9a619af0 23819
9c541725 23820 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23821}
23822
b4f54984
DE
23823static struct cmd_list_element *set_dwarf_cmdlist;
23824static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23825
23826static void
981a3fb3 23827set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23828{
b4f54984 23829 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23830 gdb_stdout);
ae038cb0
DJ
23831}
23832
23833static void
981a3fb3 23834show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23835{
b4f54984 23836 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23837}
23838
4bf44c1c 23839/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23840
23841static void
c1bd65d0 23842dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23843{
9a3c8263 23844 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23845 int ix;
8b70b953 23846
626f2d1c
TT
23847 /* Make sure we don't accidentally use dwarf2_per_objfile while
23848 cleaning up. */
23849 dwarf2_per_objfile = NULL;
23850
59b0c7c1
JB
23851 for (ix = 0; ix < data->n_comp_units; ++ix)
23852 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23853
59b0c7c1 23854 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23855 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23856 data->all_type_units[ix]->per_cu.imported_symtabs);
23857 xfree (data->all_type_units);
95554aad 23858
8b70b953 23859 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23860
23861 if (data->dwo_files)
23862 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23863 if (data->dwp_file)
23864 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23865
23866 if (data->dwz_file && data->dwz_file->dwz_bfd)
23867 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
23868
23869 if (data->index_table != NULL)
23870 data->index_table->~mapped_index ();
9291a0cd
TT
23871}
23872
23873\f
ae2de4f8 23874/* The "save gdb-index" command. */
9291a0cd 23875
bc8f2430
JK
23876/* In-memory buffer to prepare data to be written later to a file. */
23877class data_buf
9291a0cd 23878{
bc8f2430 23879public:
bc8f2430
JK
23880 /* Copy DATA to the end of the buffer. */
23881 template<typename T>
23882 void append_data (const T &data)
23883 {
23884 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23885 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23886 grow (sizeof (data)));
bc8f2430 23887 }
b89be57b 23888
c2f134ac
PA
23889 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23890 terminating zero is appended too. */
bc8f2430
JK
23891 void append_cstr0 (const char *cstr)
23892 {
23893 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23894 std::copy (cstr, cstr + size, grow (size));
23895 }
23896
23897 /* Accept a host-format integer in VAL and append it to the buffer
23898 as a target-format integer which is LEN bytes long. */
23899 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23900 {
23901 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23902 }
9291a0cd 23903
bc8f2430
JK
23904 /* Return the size of the buffer. */
23905 size_t size () const
23906 {
23907 return m_vec.size ();
23908 }
23909
23910 /* Write the buffer to FILE. */
23911 void file_write (FILE *file) const
23912 {
a81e6d4d
PA
23913 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23914 error (_("couldn't write data to file"));
bc8f2430
JK
23915 }
23916
23917private:
c2f134ac
PA
23918 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23919 the start of the new block. */
23920 gdb_byte *grow (size_t size)
23921 {
23922 m_vec.resize (m_vec.size () + size);
23923 return &*m_vec.end () - size;
23924 }
23925
d5722aa2 23926 gdb::byte_vector m_vec;
bc8f2430 23927};
9291a0cd
TT
23928
23929/* An entry in the symbol table. */
23930struct symtab_index_entry
23931{
23932 /* The name of the symbol. */
23933 const char *name;
23934 /* The offset of the name in the constant pool. */
23935 offset_type index_offset;
23936 /* A sorted vector of the indices of all the CUs that hold an object
23937 of this name. */
bc8f2430 23938 std::vector<offset_type> cu_indices;
9291a0cd
TT
23939};
23940
23941/* The symbol table. This is a power-of-2-sized hash table. */
23942struct mapped_symtab
23943{
bc8f2430
JK
23944 mapped_symtab ()
23945 {
23946 data.resize (1024);
23947 }
b89be57b 23948
bc8f2430 23949 offset_type n_elements = 0;
4b76cda9 23950 std::vector<symtab_index_entry> data;
bc8f2430 23951};
9291a0cd 23952
bc8f2430 23953/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23954 the slot.
23955
23956 Function is used only during write_hash_table so no index format backward
23957 compatibility is needed. */
b89be57b 23958
4b76cda9 23959static symtab_index_entry &
9291a0cd
TT
23960find_slot (struct mapped_symtab *symtab, const char *name)
23961{
559a7a62 23962 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23963
bc8f2430
JK
23964 index = hash & (symtab->data.size () - 1);
23965 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23966
23967 for (;;)
23968 {
4b76cda9
PA
23969 if (symtab->data[index].name == NULL
23970 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23971 return symtab->data[index];
23972 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23973 }
23974}
23975
23976/* Expand SYMTAB's hash table. */
b89be57b 23977
9291a0cd
TT
23978static void
23979hash_expand (struct mapped_symtab *symtab)
23980{
bc8f2430 23981 auto old_entries = std::move (symtab->data);
9291a0cd 23982
bc8f2430
JK
23983 symtab->data.clear ();
23984 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23985
bc8f2430 23986 for (auto &it : old_entries)
4b76cda9 23987 if (it.name != NULL)
bc8f2430 23988 {
4b76cda9 23989 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23990 ref = std::move (it);
23991 }
9291a0cd
TT
23992}
23993
156942c7
DE
23994/* Add an entry to SYMTAB. NAME is the name of the symbol.
23995 CU_INDEX is the index of the CU in which the symbol appears.
23996 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23997
9291a0cd
TT
23998static void
23999add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 24000 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
24001 offset_type cu_index)
24002{
156942c7 24003 offset_type cu_index_and_attrs;
9291a0cd
TT
24004
24005 ++symtab->n_elements;
bc8f2430 24006 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
24007 hash_expand (symtab);
24008
4b76cda9
PA
24009 symtab_index_entry &slot = find_slot (symtab, name);
24010 if (slot.name == NULL)
9291a0cd 24011 {
4b76cda9 24012 slot.name = name;
156942c7 24013 /* index_offset is set later. */
9291a0cd 24014 }
156942c7
DE
24015
24016 cu_index_and_attrs = 0;
24017 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24018 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24019 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24020
24021 /* We don't want to record an index value twice as we want to avoid the
24022 duplication.
24023 We process all global symbols and then all static symbols
24024 (which would allow us to avoid the duplication by only having to check
24025 the last entry pushed), but a symbol could have multiple kinds in one CU.
24026 To keep things simple we don't worry about the duplication here and
24027 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 24028 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
24029}
24030
24031/* Sort and remove duplicates of all symbols' cu_indices lists. */
24032
24033static void
24034uniquify_cu_indices (struct mapped_symtab *symtab)
24035{
4b76cda9 24036 for (auto &entry : symtab->data)
156942c7 24037 {
4b76cda9 24038 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 24039 {
4b76cda9 24040 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
24041 std::sort (cu_indices.begin (), cu_indices.end ());
24042 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24043 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
24044 }
24045 }
9291a0cd
TT
24046}
24047
bc8f2430
JK
24048/* A form of 'const char *' suitable for container keys. Only the
24049 pointer is stored. The strings themselves are compared, not the
24050 pointers. */
24051class c_str_view
9291a0cd 24052{
bc8f2430
JK
24053public:
24054 c_str_view (const char *cstr)
24055 : m_cstr (cstr)
24056 {}
9291a0cd 24057
bc8f2430
JK
24058 bool operator== (const c_str_view &other) const
24059 {
24060 return strcmp (m_cstr, other.m_cstr) == 0;
24061 }
9291a0cd 24062
bc8f2430
JK
24063private:
24064 friend class c_str_view_hasher;
24065 const char *const m_cstr;
24066};
9291a0cd 24067
bc8f2430
JK
24068/* A std::unordered_map::hasher for c_str_view that uses the right
24069 hash function for strings in a mapped index. */
24070class c_str_view_hasher
24071{
24072public:
24073 size_t operator () (const c_str_view &x) const
24074 {
24075 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24076 }
24077};
b89be57b 24078
bc8f2430
JK
24079/* A std::unordered_map::hasher for std::vector<>. */
24080template<typename T>
24081class vector_hasher
9291a0cd 24082{
bc8f2430
JK
24083public:
24084 size_t operator () (const std::vector<T> &key) const
24085 {
24086 return iterative_hash (key.data (),
24087 sizeof (key.front ()) * key.size (), 0);
24088 }
24089};
9291a0cd 24090
bc8f2430
JK
24091/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24092 constant pool entries going into the data buffer CPOOL. */
3876f04e 24093
bc8f2430
JK
24094static void
24095write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24096{
24097 {
24098 /* Elements are sorted vectors of the indices of all the CUs that
24099 hold an object of this name. */
24100 std::unordered_map<std::vector<offset_type>, offset_type,
24101 vector_hasher<offset_type>>
24102 symbol_hash_table;
24103
24104 /* We add all the index vectors to the constant pool first, to
24105 ensure alignment is ok. */
4b76cda9 24106 for (symtab_index_entry &entry : symtab->data)
bc8f2430 24107 {
4b76cda9 24108 if (entry.name == NULL)
bc8f2430 24109 continue;
4b76cda9 24110 gdb_assert (entry.index_offset == 0);
70a1152b
PA
24111
24112 /* Finding before inserting is faster than always trying to
24113 insert, because inserting always allocates a node, does the
24114 lookup, and then destroys the new node if another node
24115 already had the same key. C++17 try_emplace will avoid
24116 this. */
24117 const auto found
4b76cda9 24118 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
24119 if (found != symbol_hash_table.end ())
24120 {
4b76cda9 24121 entry.index_offset = found->second;
70a1152b
PA
24122 continue;
24123 }
24124
4b76cda9
PA
24125 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24126 entry.index_offset = cpool.size ();
24127 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24128 for (const auto index : entry.cu_indices)
24129 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
24130 }
24131 }
9291a0cd
TT
24132
24133 /* Now write out the hash table. */
bc8f2430 24134 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 24135 for (const auto &entry : symtab->data)
9291a0cd
TT
24136 {
24137 offset_type str_off, vec_off;
24138
4b76cda9 24139 if (entry.name != NULL)
9291a0cd 24140 {
4b76cda9 24141 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 24142 if (insertpair.second)
4b76cda9 24143 cpool.append_cstr0 (entry.name);
bc8f2430 24144 str_off = insertpair.first->second;
4b76cda9 24145 vec_off = entry.index_offset;
9291a0cd
TT
24146 }
24147 else
24148 {
24149 /* While 0 is a valid constant pool index, it is not valid
24150 to have 0 for both offsets. */
24151 str_off = 0;
24152 vec_off = 0;
24153 }
24154
bc8f2430
JK
24155 output.append_data (MAYBE_SWAP (str_off));
24156 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 24157 }
9291a0cd
TT
24158}
24159
bc8f2430 24160typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
24161
24162/* Helper struct for building the address table. */
24163struct addrmap_index_data
24164{
bc8f2430
JK
24165 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24166 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24167 {}
24168
0a5429f6 24169 struct objfile *objfile;
bc8f2430
JK
24170 data_buf &addr_vec;
24171 psym_index_map &cu_index_htab;
0a5429f6
DE
24172
24173 /* Non-zero if the previous_* fields are valid.
24174 We can't write an entry until we see the next entry (since it is only then
24175 that we know the end of the entry). */
24176 int previous_valid;
24177 /* Index of the CU in the table of all CUs in the index file. */
24178 unsigned int previous_cu_index;
0963b4bd 24179 /* Start address of the CU. */
0a5429f6
DE
24180 CORE_ADDR previous_cu_start;
24181};
24182
bc8f2430 24183/* Write an address entry to ADDR_VEC. */
b89be57b 24184
9291a0cd 24185static void
bc8f2430 24186add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 24187 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 24188{
9291a0cd
TT
24189 CORE_ADDR baseaddr;
24190
24191 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24192
c2f134ac
PA
24193 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24194 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 24195 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
24196}
24197
24198/* Worker function for traversing an addrmap to build the address table. */
24199
24200static int
24201add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24202{
9a3c8263
SM
24203 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24204 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
24205
24206 if (data->previous_valid)
bc8f2430 24207 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
24208 data->previous_cu_start, start_addr,
24209 data->previous_cu_index);
24210
24211 data->previous_cu_start = start_addr;
24212 if (pst != NULL)
24213 {
bc8f2430
JK
24214 const auto it = data->cu_index_htab.find (pst);
24215 gdb_assert (it != data->cu_index_htab.cend ());
24216 data->previous_cu_index = it->second;
0a5429f6
DE
24217 data->previous_valid = 1;
24218 }
24219 else
bc8f2430 24220 data->previous_valid = 0;
0a5429f6
DE
24221
24222 return 0;
24223}
24224
bc8f2430 24225/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
24226 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24227 in the index file. */
24228
24229static void
bc8f2430
JK
24230write_address_map (struct objfile *objfile, data_buf &addr_vec,
24231 psym_index_map &cu_index_htab)
0a5429f6 24232{
bc8f2430 24233 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
24234
24235 /* When writing the address table, we have to cope with the fact that
24236 the addrmap iterator only provides the start of a region; we have to
24237 wait until the next invocation to get the start of the next region. */
24238
24239 addrmap_index_data.objfile = objfile;
0a5429f6
DE
24240 addrmap_index_data.previous_valid = 0;
24241
24242 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24243 &addrmap_index_data);
24244
24245 /* It's highly unlikely the last entry (end address = 0xff...ff)
24246 is valid, but we should still handle it.
24247 The end address is recorded as the start of the next region, but that
24248 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24249 anyway. */
24250 if (addrmap_index_data.previous_valid)
bc8f2430 24251 add_address_entry (objfile, addr_vec,
0a5429f6
DE
24252 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24253 addrmap_index_data.previous_cu_index);
9291a0cd
TT
24254}
24255
156942c7
DE
24256/* Return the symbol kind of PSYM. */
24257
24258static gdb_index_symbol_kind
24259symbol_kind (struct partial_symbol *psym)
24260{
24261 domain_enum domain = PSYMBOL_DOMAIN (psym);
24262 enum address_class aclass = PSYMBOL_CLASS (psym);
24263
24264 switch (domain)
24265 {
24266 case VAR_DOMAIN:
24267 switch (aclass)
24268 {
24269 case LOC_BLOCK:
24270 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24271 case LOC_TYPEDEF:
24272 return GDB_INDEX_SYMBOL_KIND_TYPE;
24273 case LOC_COMPUTED:
24274 case LOC_CONST_BYTES:
24275 case LOC_OPTIMIZED_OUT:
24276 case LOC_STATIC:
24277 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24278 case LOC_CONST:
24279 /* Note: It's currently impossible to recognize psyms as enum values
24280 short of reading the type info. For now punt. */
24281 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24282 default:
24283 /* There are other LOC_FOO values that one might want to classify
24284 as variables, but dwarf2read.c doesn't currently use them. */
24285 return GDB_INDEX_SYMBOL_KIND_OTHER;
24286 }
24287 case STRUCT_DOMAIN:
24288 return GDB_INDEX_SYMBOL_KIND_TYPE;
24289 default:
24290 return GDB_INDEX_SYMBOL_KIND_OTHER;
24291 }
24292}
24293
9291a0cd 24294/* Add a list of partial symbols to SYMTAB. */
b89be57b 24295
9291a0cd
TT
24296static void
24297write_psymbols (struct mapped_symtab *symtab,
bc8f2430 24298 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
24299 struct partial_symbol **psymp,
24300 int count,
987d643c
TT
24301 offset_type cu_index,
24302 int is_static)
9291a0cd
TT
24303{
24304 for (; count-- > 0; ++psymp)
24305 {
156942c7 24306 struct partial_symbol *psym = *psymp;
987d643c 24307
156942c7 24308 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 24309 error (_("Ada is not currently supported by the index"));
987d643c 24310
987d643c 24311 /* Only add a given psymbol once. */
bc8f2430 24312 if (psyms_seen.insert (psym).second)
987d643c 24313 {
156942c7
DE
24314 gdb_index_symbol_kind kind = symbol_kind (psym);
24315
156942c7
DE
24316 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24317 is_static, kind, cu_index);
987d643c 24318 }
9291a0cd
TT
24319 }
24320}
24321
1fd400ff
TT
24322/* A helper struct used when iterating over debug_types. */
24323struct signatured_type_index_data
24324{
bc8f2430
JK
24325 signatured_type_index_data (data_buf &types_list_,
24326 std::unordered_set<partial_symbol *> &psyms_seen_)
24327 : types_list (types_list_), psyms_seen (psyms_seen_)
24328 {}
24329
1fd400ff
TT
24330 struct objfile *objfile;
24331 struct mapped_symtab *symtab;
bc8f2430
JK
24332 data_buf &types_list;
24333 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
24334 int cu_index;
24335};
24336
24337/* A helper function that writes a single signatured_type to an
24338 obstack. */
b89be57b 24339
1fd400ff
TT
24340static int
24341write_one_signatured_type (void **slot, void *d)
24342{
9a3c8263
SM
24343 struct signatured_type_index_data *info
24344 = (struct signatured_type_index_data *) d;
1fd400ff 24345 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 24346 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
24347
24348 write_psymbols (info->symtab,
987d643c 24349 info->psyms_seen,
af5bf4ad 24350 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
24351 psymtab->n_global_syms, info->cu_index,
24352 0);
1fd400ff 24353 write_psymbols (info->symtab,
987d643c 24354 info->psyms_seen,
af5bf4ad 24355 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
24356 psymtab->n_static_syms, info->cu_index,
24357 1);
1fd400ff 24358
c2f134ac
PA
24359 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24360 to_underlying (entry->per_cu.sect_off));
24361 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24362 to_underlying (entry->type_offset_in_tu));
24363 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
24364
24365 ++info->cu_index;
24366
24367 return 1;
24368}
24369
e8f8bcb3
PA
24370/* Recurse into all "included" dependencies and count their symbols as
24371 if they appeared in this psymtab. */
24372
24373static void
24374recursively_count_psymbols (struct partial_symtab *psymtab,
24375 size_t &psyms_seen)
24376{
24377 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24378 if (psymtab->dependencies[i]->user != NULL)
24379 recursively_count_psymbols (psymtab->dependencies[i],
24380 psyms_seen);
24381
24382 psyms_seen += psymtab->n_global_syms;
24383 psyms_seen += psymtab->n_static_syms;
24384}
24385
95554aad
TT
24386/* Recurse into all "included" dependencies and write their symbols as
24387 if they appeared in this psymtab. */
24388
24389static void
24390recursively_write_psymbols (struct objfile *objfile,
24391 struct partial_symtab *psymtab,
24392 struct mapped_symtab *symtab,
bc8f2430 24393 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
24394 offset_type cu_index)
24395{
24396 int i;
24397
24398 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24399 if (psymtab->dependencies[i]->user != NULL)
24400 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24401 symtab, psyms_seen, cu_index);
24402
24403 write_psymbols (symtab,
24404 psyms_seen,
af5bf4ad 24405 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
24406 psymtab->n_global_syms, cu_index,
24407 0);
24408 write_psymbols (symtab,
24409 psyms_seen,
af5bf4ad 24410 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
24411 psymtab->n_static_syms, cu_index,
24412 1);
24413}
24414
9291a0cd 24415/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 24416
9291a0cd
TT
24417static void
24418write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24419{
9291a0cd
TT
24420 if (dwarf2_per_objfile->using_index)
24421 error (_("Cannot use an index to create the index"));
24422
8b70b953
TT
24423 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24424 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24425
260b681b
DE
24426 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24427 return;
24428
bc8f2430 24429 struct stat st;
4262abfb
JK
24430 if (stat (objfile_name (objfile), &st) < 0)
24431 perror_with_name (objfile_name (objfile));
9291a0cd 24432
bc8f2430
JK
24433 std::string filename (std::string (dir) + SLASH_STRING
24434 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 24435
d419f42d 24436 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 24437 if (!out_file)
bc8f2430 24438 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 24439
16b7a719
PA
24440 /* Order matters here; we want FILE to be closed before FILENAME is
24441 unlinked, because on MS-Windows one cannot delete a file that is
24442 still open. (Don't call anything here that might throw until
24443 file_closer is created.) */
bc8f2430 24444 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 24445 gdb_file_up close_out_file (out_file);
9291a0cd 24446
bc8f2430
JK
24447 mapped_symtab symtab;
24448 data_buf cu_list;
987d643c 24449
0a5429f6
DE
24450 /* While we're scanning CU's create a table that maps a psymtab pointer
24451 (which is what addrmap records) to its index (which is what is recorded
24452 in the index file). This will later be needed to write the address
24453 table. */
bc8f2430
JK
24454 psym_index_map cu_index_htab;
24455 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
24456
24457 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
24458 work here. Also, the debug_types entries do not appear in
24459 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
24460
24461 /* The psyms_seen set is potentially going to be largish (~40k
24462 elements when indexing a -g3 build of GDB itself). Estimate the
24463 number of elements in order to avoid too many rehashes, which
24464 require rebuilding buckets and thus many trips to
24465 malloc/free. */
24466 size_t psyms_count = 0;
24467 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24468 {
24469 struct dwarf2_per_cu_data *per_cu
24470 = dwarf2_per_objfile->all_comp_units[i];
24471 struct partial_symtab *psymtab = per_cu->v.psymtab;
24472
24473 if (psymtab != NULL && psymtab->user == NULL)
24474 recursively_count_psymbols (psymtab, psyms_count);
24475 }
24476 /* Generating an index for gdb itself shows a ratio of
24477 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24478 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 24479 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 24480 {
3e43a32a
MS
24481 struct dwarf2_per_cu_data *per_cu
24482 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 24483 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 24484
92fac807
JK
24485 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24486 It may be referenced from a local scope but in such case it does not
24487 need to be present in .gdb_index. */
24488 if (psymtab == NULL)
24489 continue;
24490
95554aad 24491 if (psymtab->user == NULL)
bc8f2430
JK
24492 recursively_write_psymbols (objfile, psymtab, &symtab,
24493 psyms_seen, i);
9291a0cd 24494
bc8f2430
JK
24495 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24496 gdb_assert (insertpair.second);
9291a0cd 24497
c2f134ac
PA
24498 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24499 to_underlying (per_cu->sect_off));
24500 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
24501 }
24502
0a5429f6 24503 /* Dump the address map. */
bc8f2430
JK
24504 data_buf addr_vec;
24505 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24506
1fd400ff 24507 /* Write out the .debug_type entries, if any. */
bc8f2430 24508 data_buf types_cu_list;
1fd400ff
TT
24509 if (dwarf2_per_objfile->signatured_types)
24510 {
bc8f2430
JK
24511 signatured_type_index_data sig_data (types_cu_list,
24512 psyms_seen);
1fd400ff
TT
24513
24514 sig_data.objfile = objfile;
bc8f2430 24515 sig_data.symtab = &symtab;
1fd400ff
TT
24516 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24517 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24518 write_one_signatured_type, &sig_data);
24519 }
24520
156942c7
DE
24521 /* Now that we've processed all symbols we can shrink their cu_indices
24522 lists. */
bc8f2430 24523 uniquify_cu_indices (&symtab);
156942c7 24524
bc8f2430
JK
24525 data_buf symtab_vec, constant_pool;
24526 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24527
bc8f2430
JK
24528 data_buf contents;
24529 const offset_type size_of_contents = 6 * sizeof (offset_type);
24530 offset_type total_len = size_of_contents;
9291a0cd
TT
24531
24532 /* The version number. */
bc8f2430 24533 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24534
24535 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24536 contents.append_data (MAYBE_SWAP (total_len));
24537 total_len += cu_list.size ();
9291a0cd 24538
1fd400ff 24539 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24540 contents.append_data (MAYBE_SWAP (total_len));
24541 total_len += types_cu_list.size ();
1fd400ff 24542
9291a0cd 24543 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24544 contents.append_data (MAYBE_SWAP (total_len));
24545 total_len += addr_vec.size ();
9291a0cd
TT
24546
24547 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24548 contents.append_data (MAYBE_SWAP (total_len));
24549 total_len += symtab_vec.size ();
9291a0cd
TT
24550
24551 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24552 contents.append_data (MAYBE_SWAP (total_len));
24553 total_len += constant_pool.size ();
9291a0cd 24554
bc8f2430 24555 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24556
bc8f2430
JK
24557 contents.file_write (out_file);
24558 cu_list.file_write (out_file);
24559 types_cu_list.file_write (out_file);
24560 addr_vec.file_write (out_file);
24561 symtab_vec.file_write (out_file);
24562 constant_pool.file_write (out_file);
9291a0cd 24563
bef155c3
TT
24564 /* We want to keep the file. */
24565 unlink_file.keep ();
9291a0cd
TT
24566}
24567
90476074
TT
24568/* Implementation of the `save gdb-index' command.
24569
24570 Note that the file format used by this command is documented in the
24571 GDB manual. Any changes here must be documented there. */
11570e71 24572
9291a0cd 24573static void
8384c356 24574save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24575{
24576 struct objfile *objfile;
24577
24578 if (!arg || !*arg)
96d19272 24579 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24580
24581 ALL_OBJFILES (objfile)
24582 {
24583 struct stat st;
24584
24585 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24586 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24587 continue;
24588
9a3c8263
SM
24589 dwarf2_per_objfile
24590 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24591 dwarf2_objfile_data_key);
9291a0cd
TT
24592 if (dwarf2_per_objfile)
24593 {
9291a0cd 24594
492d29ea 24595 TRY
9291a0cd
TT
24596 {
24597 write_psymtabs_to_index (objfile, arg);
24598 }
492d29ea
PA
24599 CATCH (except, RETURN_MASK_ERROR)
24600 {
24601 exception_fprintf (gdb_stderr, except,
24602 _("Error while writing index for `%s': "),
24603 objfile_name (objfile));
24604 }
24605 END_CATCH
9291a0cd
TT
24606 }
24607 }
dce234bc
PP
24608}
24609
9291a0cd
TT
24610\f
24611
b4f54984 24612int dwarf_always_disassemble;
9eae7c52
TT
24613
24614static void
b4f54984
DE
24615show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24616 struct cmd_list_element *c, const char *value)
9eae7c52 24617{
3e43a32a
MS
24618 fprintf_filtered (file,
24619 _("Whether to always disassemble "
24620 "DWARF expressions is %s.\n"),
9eae7c52
TT
24621 value);
24622}
24623
900e11f9
JK
24624static void
24625show_check_physname (struct ui_file *file, int from_tty,
24626 struct cmd_list_element *c, const char *value)
24627{
24628 fprintf_filtered (file,
24629 _("Whether to check \"physname\" is %s.\n"),
24630 value);
24631}
24632
6502dd73
DJ
24633void
24634_initialize_dwarf2_read (void)
24635{
96d19272
JK
24636 struct cmd_list_element *c;
24637
dce234bc 24638 dwarf2_objfile_data_key
c1bd65d0 24639 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24640
b4f54984
DE
24641 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24642Set DWARF specific variables.\n\
24643Configure DWARF variables such as the cache size"),
24644 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24645 0/*allow-unknown*/, &maintenance_set_cmdlist);
24646
b4f54984
DE
24647 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24648Show DWARF specific variables\n\
24649Show DWARF variables such as the cache size"),
24650 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24651 0/*allow-unknown*/, &maintenance_show_cmdlist);
24652
24653 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24654 &dwarf_max_cache_age, _("\
24655Set the upper bound on the age of cached DWARF compilation units."), _("\
24656Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24657A higher limit means that cached compilation units will be stored\n\
24658in memory longer, and more total memory will be used. Zero disables\n\
24659caching, which can slow down startup."),
2c5b56ce 24660 NULL,
b4f54984
DE
24661 show_dwarf_max_cache_age,
24662 &set_dwarf_cmdlist,
24663 &show_dwarf_cmdlist);
d97bc12b 24664
9eae7c52 24665 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24666 &dwarf_always_disassemble, _("\
9eae7c52
TT
24667Set whether `info address' always disassembles DWARF expressions."), _("\
24668Show whether `info address' always disassembles DWARF expressions."), _("\
24669When enabled, DWARF expressions are always printed in an assembly-like\n\
24670syntax. When disabled, expressions will be printed in a more\n\
24671conversational style, when possible."),
24672 NULL,
b4f54984
DE
24673 show_dwarf_always_disassemble,
24674 &set_dwarf_cmdlist,
24675 &show_dwarf_cmdlist);
24676
24677 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24678Set debugging of the DWARF reader."), _("\
24679Show debugging of the DWARF reader."), _("\
24680When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24681reading and symtab expansion. A value of 1 (one) provides basic\n\
24682information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24683 NULL,
24684 NULL,
24685 &setdebuglist, &showdebuglist);
24686
b4f54984
DE
24687 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24688Set debugging of the DWARF DIE reader."), _("\
24689Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24690When enabled (non-zero), DIEs are dumped after they are read in.\n\
24691The value is the maximum depth to print."),
ccce17b0
YQ
24692 NULL,
24693 NULL,
24694 &setdebuglist, &showdebuglist);
9291a0cd 24695
27e0867f
DE
24696 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24697Set debugging of the dwarf line reader."), _("\
24698Show debugging of the dwarf line reader."), _("\
24699When enabled (non-zero), line number entries are dumped as they are read in.\n\
24700A value of 1 (one) provides basic information.\n\
24701A value greater than 1 provides more verbose information."),
24702 NULL,
24703 NULL,
24704 &setdebuglist, &showdebuglist);
24705
900e11f9
JK
24706 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24707Set cross-checking of \"physname\" code against demangler."), _("\
24708Show cross-checking of \"physname\" code against demangler."), _("\
24709When enabled, GDB's internal \"physname\" code is checked against\n\
24710the demangler."),
24711 NULL, show_check_physname,
24712 &setdebuglist, &showdebuglist);
24713
e615022a
DE
24714 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24715 no_class, &use_deprecated_index_sections, _("\
24716Set whether to use deprecated gdb_index sections."), _("\
24717Show whether to use deprecated gdb_index sections."), _("\
24718When enabled, deprecated .gdb_index sections are used anyway.\n\
24719Normally they are ignored either because of a missing feature or\n\
24720performance issue.\n\
24721Warning: This option must be enabled before gdb reads the file."),
24722 NULL,
24723 NULL,
24724 &setlist, &showlist);
24725
96d19272 24726 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24727 _("\
fc1a9d6e 24728Save a gdb-index file.\n\
11570e71 24729Usage: save gdb-index DIRECTORY"),
96d19272
JK
24730 &save_cmdlist);
24731 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24732
24733 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24734 &dwarf2_locexpr_funcs);
24735 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24736 &dwarf2_loclist_funcs);
24737
24738 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24739 &dwarf2_block_frame_base_locexpr_funcs);
24740 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24741 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24742
24743#if GDB_SELF_TEST
24744 selftests::register_test ("dw2_expand_symtabs_matching",
24745 selftests::dw2_expand_symtabs_matching::run_test);
24746#endif
6502dd73 24747}
This page took 6.562095 seconds and 4 git commands to generate.