x86: Don't check has_non_got_reloc
[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"
c906108c 78#include <fcntl.h>
c906108c 79#include <sys/types.h>
325fac50 80#include <algorithm>
bc8f2430
JK
81#include <unordered_set>
82#include <unordered_map>
d8151005 83
34eaf542
TT
84typedef struct symbol *symbolp;
85DEF_VEC_P (symbolp);
86
73be47f5
DE
87/* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
b4f54984
DE
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90static unsigned int dwarf_read_debug = 0;
45cfd468 91
d97bc12b 92/* When non-zero, dump DIEs after they are read in. */
b4f54984 93static unsigned int dwarf_die_debug = 0;
d97bc12b 94
27e0867f
DE
95/* When non-zero, dump line number entries as they are read in. */
96static unsigned int dwarf_line_debug = 0;
97
900e11f9
JK
98/* When non-zero, cross-check physname against demangler. */
99static int check_physname = 0;
100
481860b3 101/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 102static int use_deprecated_index_sections = 0;
481860b3 103
6502dd73
DJ
104static const struct objfile_data *dwarf2_objfile_data_key;
105
f1e6e072
TT
106/* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108static int dwarf2_locexpr_index;
109static int dwarf2_loclist_index;
110static int dwarf2_locexpr_block_index;
111static int dwarf2_loclist_block_index;
112
73869dc2
DE
113/* A descriptor for dwarf sections.
114
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
119
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
124 input section.
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
128
dce234bc
PP
129struct dwarf2_section_info
130{
73869dc2
DE
131 union
132 {
e5aa3347 133 /* If this is a real section, the bfd section. */
049412e3 134 asection *section;
73869dc2 135 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 136 section. */
73869dc2
DE
137 struct dwarf2_section_info *containing_section;
138 } s;
19ac8c2e 139 /* Pointer to section data, only valid if readin. */
d521ce57 140 const gdb_byte *buffer;
73869dc2 141 /* The size of the section, real or virtual. */
dce234bc 142 bfd_size_type size;
73869dc2
DE
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
be391dca 146 /* True if we have tried to read this section. */
73869dc2
DE
147 char readin;
148 /* True if this is a virtual section, False otherwise.
049412e3 149 This specifies which of s.section and s.containing_section to use. */
73869dc2 150 char is_virtual;
dce234bc
PP
151};
152
8b70b953
TT
153typedef struct dwarf2_section_info dwarf2_section_info_def;
154DEF_VEC_O (dwarf2_section_info_def);
155
9291a0cd
TT
156/* All offsets in the index are of this type. It must be
157 architecture-independent. */
158typedef uint32_t offset_type;
159
160DEF_VEC_I (offset_type);
161
156942c7
DE
162/* Ensure only legit values are used. */
163#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
167 } while (0)
168
169/* Ensure only legit values are used. */
170#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 do { \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177/* Ensure we don't use more than the alloted nuber of bits for the CU. */
178#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 do { \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
182 } while (0)
183
9291a0cd
TT
184/* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186struct mapped_index
187{
559a7a62
JK
188 /* Index data format version. */
189 int version;
190
9291a0cd
TT
191 /* The total length of the buffer. */
192 off_t total_size;
b11b1f88 193
9291a0cd
TT
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
b11b1f88 196
9291a0cd
TT
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
b11b1f88 199
3876f04e
DE
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
b11b1f88 202
9291a0cd 203 /* Size in slots, each slot is 2 offset_types. */
3876f04e 204 offset_type symbol_table_slots;
b11b1f88 205
9291a0cd
TT
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
208};
209
95554aad
TT
210typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211DEF_VEC_P (dwarf2_per_cu_ptr);
212
52059ffd
TT
213struct tu_stats
214{
215 int nr_uniq_abbrev_tables;
216 int nr_symtabs;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
220};
221
9cdd5dbd
DE
222/* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
224
6502dd73
DJ
225struct dwarf2_per_objfile
226{
330cdd98
PA
227 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
228 dwarf2 section names, or is NULL if the standard ELF names are
229 used. */
230 dwarf2_per_objfile (struct objfile *objfile,
231 const dwarf2_debug_sections *names);
ae038cb0 232
330cdd98
PA
233 ~dwarf2_per_objfile ();
234
235 /* Disable copy. */
236 dwarf2_per_objfile (const dwarf2_per_objfile &) = delete;
237 void operator= (const dwarf2_per_objfile &) = delete;
238
239 /* Free all cached compilation units. */
240 void free_cached_comp_units ();
241private:
242 /* This function is mapped across the sections and remembers the
243 offset and size of each of the debugging sections we are
244 interested in. */
245 void locate_sections (bfd *abfd, asection *sectp,
246 const dwarf2_debug_sections &names);
247
248public:
249 dwarf2_section_info info {};
250 dwarf2_section_info abbrev {};
251 dwarf2_section_info line {};
252 dwarf2_section_info loc {};
253 dwarf2_section_info loclists {};
254 dwarf2_section_info macinfo {};
255 dwarf2_section_info macro {};
256 dwarf2_section_info str {};
257 dwarf2_section_info line_str {};
258 dwarf2_section_info ranges {};
259 dwarf2_section_info rnglists {};
260 dwarf2_section_info addr {};
261 dwarf2_section_info frame {};
262 dwarf2_section_info eh_frame {};
263 dwarf2_section_info gdb_index {};
264
265 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 266
be391dca 267 /* Back link. */
330cdd98 268 struct objfile *objfile = NULL;
be391dca 269
d467dd73 270 /* Table of all the compilation units. This is used to locate
10b3939b 271 the target compilation unit of a particular reference. */
330cdd98 272 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
273
274 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 275 int n_comp_units = 0;
ae038cb0 276
1fd400ff 277 /* The number of .debug_types-related CUs. */
330cdd98 278 int n_type_units = 0;
1fd400ff 279
6aa5f3a6
DE
280 /* The number of elements allocated in all_type_units.
281 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 282 int n_allocated_type_units = 0;
6aa5f3a6 283
a2ce51a0
DE
284 /* The .debug_types-related CUs (TUs).
285 This is stored in malloc space because we may realloc it. */
330cdd98 286 struct signatured_type **all_type_units = NULL;
1fd400ff 287
f4dc4d17
DE
288 /* Table of struct type_unit_group objects.
289 The hash key is the DW_AT_stmt_list value. */
330cdd98 290 htab_t type_unit_groups {};
72dca2f5 291
348e048f
DE
292 /* A table mapping .debug_types signatures to its signatured_type entry.
293 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 294 htab_t signatured_types {};
348e048f 295
f4dc4d17
DE
296 /* Type unit statistics, to see how well the scaling improvements
297 are doing. */
330cdd98 298 struct tu_stats tu_stats {};
f4dc4d17
DE
299
300 /* A chain of compilation units that are currently read in, so that
301 they can be freed later. */
330cdd98 302 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 303
3019eac3
DE
304 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
305 This is NULL if the table hasn't been allocated yet. */
330cdd98 306 htab_t dwo_files {};
3019eac3 307
330cdd98
PA
308 /* True if we've checked for whether there is a DWP file. */
309 bool dwp_checked = false;
80626a55
DE
310
311 /* The DWP file if there is one, or NULL. */
330cdd98 312 struct dwp_file *dwp_file = NULL;
80626a55 313
36586728
TT
314 /* The shared '.dwz' file, if one exists. This is used when the
315 original data was compressed using 'dwz -m'. */
330cdd98 316 struct dwz_file *dwz_file = NULL;
36586728 317
330cdd98 318 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 319 VMA of 0. */
330cdd98 320 bool has_section_at_zero = false;
9291a0cd 321
ae2de4f8
DE
322 /* True if we are using the mapped index,
323 or we are faking it for OBJF_READNOW's sake. */
330cdd98 324 bool using_index = false;
9291a0cd 325
ae2de4f8 326 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 327 mapped_index *index_table = NULL;
98bfdba5 328
7b9f3c50 329 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
330 TUs typically share line table entries with a CU, so we maintain a
331 separate table of all line table entries to support the sharing.
332 Note that while there can be way more TUs than CUs, we've already
333 sorted all the TUs into "type unit groups", grouped by their
334 DW_AT_stmt_list value. Therefore the only sharing done here is with a
335 CU and its associated TU group if there is one. */
330cdd98 336 htab_t quick_file_names_table {};
7b9f3c50 337
98bfdba5
PA
338 /* Set during partial symbol reading, to prevent queueing of full
339 symbols. */
330cdd98 340 bool reading_partial_symbols = false;
673bfd45 341
dee91e82 342 /* Table mapping type DIEs to their struct type *.
673bfd45 343 This is NULL if not allocated yet.
02142a6c 344 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 345 htab_t die_type_hash {};
95554aad
TT
346
347 /* The CUs we recently read. */
330cdd98 348 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
349
350 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 351 htab_t line_header_hash {};
bbf2f4df
PA
352
353 /* Table containing all filenames. This is an optional because the
354 table is lazily constructed on first access. */
355 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
356};
357
358static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 359
251d32d9 360/* Default names of the debugging sections. */
c906108c 361
233a11ab
CS
362/* Note that if the debugging section has been compressed, it might
363 have a name like .zdebug_info. */
364
9cdd5dbd
DE
365static const struct dwarf2_debug_sections dwarf2_elf_names =
366{
251d32d9
TG
367 { ".debug_info", ".zdebug_info" },
368 { ".debug_abbrev", ".zdebug_abbrev" },
369 { ".debug_line", ".zdebug_line" },
370 { ".debug_loc", ".zdebug_loc" },
43988095 371 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 372 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 373 { ".debug_macro", ".zdebug_macro" },
251d32d9 374 { ".debug_str", ".zdebug_str" },
43988095 375 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 376 { ".debug_ranges", ".zdebug_ranges" },
43988095 377 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 378 { ".debug_types", ".zdebug_types" },
3019eac3 379 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
380 { ".debug_frame", ".zdebug_frame" },
381 { ".eh_frame", NULL },
24d3216f
TT
382 { ".gdb_index", ".zgdb_index" },
383 23
251d32d9 384};
c906108c 385
80626a55 386/* List of DWO/DWP sections. */
3019eac3 387
80626a55 388static const struct dwop_section_names
3019eac3
DE
389{
390 struct dwarf2_section_names abbrev_dwo;
391 struct dwarf2_section_names info_dwo;
392 struct dwarf2_section_names line_dwo;
393 struct dwarf2_section_names loc_dwo;
43988095 394 struct dwarf2_section_names loclists_dwo;
09262596
DE
395 struct dwarf2_section_names macinfo_dwo;
396 struct dwarf2_section_names macro_dwo;
3019eac3
DE
397 struct dwarf2_section_names str_dwo;
398 struct dwarf2_section_names str_offsets_dwo;
399 struct dwarf2_section_names types_dwo;
80626a55
DE
400 struct dwarf2_section_names cu_index;
401 struct dwarf2_section_names tu_index;
3019eac3 402}
80626a55 403dwop_section_names =
3019eac3
DE
404{
405 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
406 { ".debug_info.dwo", ".zdebug_info.dwo" },
407 { ".debug_line.dwo", ".zdebug_line.dwo" },
408 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 409 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
410 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
411 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
412 { ".debug_str.dwo", ".zdebug_str.dwo" },
413 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
414 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
415 { ".debug_cu_index", ".zdebug_cu_index" },
416 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
417};
418
c906108c
SS
419/* local data types */
420
107d2387
AC
421/* The data in a compilation unit header, after target2host
422 translation, looks like this. */
c906108c 423struct comp_unit_head
a738430d 424{
c764a876 425 unsigned int length;
a738430d 426 short version;
a738430d
MK
427 unsigned char addr_size;
428 unsigned char signed_addr_p;
9c541725 429 sect_offset abbrev_sect_off;
57349743 430
a738430d
MK
431 /* Size of file offsets; either 4 or 8. */
432 unsigned int offset_size;
57349743 433
a738430d
MK
434 /* Size of the length field; either 4 or 12. */
435 unsigned int initial_length_size;
57349743 436
43988095
JK
437 enum dwarf_unit_type unit_type;
438
a738430d
MK
439 /* Offset to the first byte of this compilation unit header in the
440 .debug_info section, for resolving relative reference dies. */
9c541725 441 sect_offset sect_off;
57349743 442
d00adf39
DE
443 /* Offset to first die in this cu from the start of the cu.
444 This will be the first byte following the compilation unit header. */
9c541725 445 cu_offset first_die_cu_offset;
43988095
JK
446
447 /* 64-bit signature of this type unit - it is valid only for
448 UNIT_TYPE DW_UT_type. */
449 ULONGEST signature;
450
451 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 452 cu_offset type_cu_offset_in_tu;
a738430d 453};
c906108c 454
3da10d80
KS
455/* Type used for delaying computation of method physnames.
456 See comments for compute_delayed_physnames. */
457struct delayed_method_info
458{
459 /* The type to which the method is attached, i.e., its parent class. */
460 struct type *type;
461
462 /* The index of the method in the type's function fieldlists. */
463 int fnfield_index;
464
465 /* The index of the method in the fieldlist. */
466 int index;
467
468 /* The name of the DIE. */
469 const char *name;
470
471 /* The DIE associated with this method. */
472 struct die_info *die;
473};
474
475typedef struct delayed_method_info delayed_method_info;
476DEF_VEC_O (delayed_method_info);
477
e7c27a73
DJ
478/* Internal state when decoding a particular compilation unit. */
479struct dwarf2_cu
480{
481 /* The objfile containing this compilation unit. */
482 struct objfile *objfile;
483
d00adf39 484 /* The header of the compilation unit. */
e7c27a73 485 struct comp_unit_head header;
e142c38c 486
d00adf39
DE
487 /* Base address of this compilation unit. */
488 CORE_ADDR base_address;
489
490 /* Non-zero if base_address has been set. */
491 int base_known;
492
e142c38c
DJ
493 /* The language we are debugging. */
494 enum language language;
495 const struct language_defn *language_defn;
496
b0f35d58
DL
497 const char *producer;
498
e142c38c
DJ
499 /* The generic symbol table building routines have separate lists for
500 file scope symbols and all all other scopes (local scopes). So
501 we need to select the right one to pass to add_symbol_to_list().
502 We do it by keeping a pointer to the correct list in list_in_scope.
503
504 FIXME: The original dwarf code just treated the file scope as the
505 first local scope, and all other local scopes as nested local
506 scopes, and worked fine. Check to see if we really need to
507 distinguish these in buildsym.c. */
508 struct pending **list_in_scope;
509
433df2d4
DE
510 /* The abbrev table for this CU.
511 Normally this points to the abbrev table in the objfile.
512 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
513 struct abbrev_table *abbrev_table;
72bf9492 514
b64f50a1
JK
515 /* Hash table holding all the loaded partial DIEs
516 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
517 htab_t partial_dies;
518
519 /* Storage for things with the same lifetime as this read-in compilation
520 unit, including partial DIEs. */
521 struct obstack comp_unit_obstack;
522
ae038cb0
DJ
523 /* When multiple dwarf2_cu structures are living in memory, this field
524 chains them all together, so that they can be released efficiently.
525 We will probably also want a generation counter so that most-recently-used
526 compilation units are cached... */
527 struct dwarf2_per_cu_data *read_in_chain;
528
69d751e3 529 /* Backlink to our per_cu entry. */
ae038cb0
DJ
530 struct dwarf2_per_cu_data *per_cu;
531
532 /* How many compilation units ago was this CU last referenced? */
533 int last_used;
534
b64f50a1
JK
535 /* A hash table of DIE cu_offset for following references with
536 die_info->offset.sect_off as hash. */
51545339 537 htab_t die_hash;
10b3939b
DJ
538
539 /* Full DIEs if read in. */
540 struct die_info *dies;
541
542 /* A set of pointers to dwarf2_per_cu_data objects for compilation
543 units referenced by this one. Only set during full symbol processing;
544 partial symbol tables do not have dependencies. */
545 htab_t dependencies;
546
cb1df416
DJ
547 /* Header data from the line table, during full symbol processing. */
548 struct line_header *line_header;
4c8aa72d
PA
549 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
550 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
551 this is the DW_TAG_compile_unit die for this CU. We'll hold on
552 to the line header as long as this DIE is being processed. See
553 process_die_scope. */
554 die_info *line_header_die_owner;
cb1df416 555
3da10d80
KS
556 /* A list of methods which need to have physnames computed
557 after all type information has been read. */
558 VEC (delayed_method_info) *method_list;
559
96408a79
SA
560 /* To be copied to symtab->call_site_htab. */
561 htab_t call_site_htab;
562
034e5797
DE
563 /* Non-NULL if this CU came from a DWO file.
564 There is an invariant here that is important to remember:
565 Except for attributes copied from the top level DIE in the "main"
566 (or "stub") file in preparation for reading the DWO file
567 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
568 Either there isn't a DWO file (in which case this is NULL and the point
569 is moot), or there is and either we're not going to read it (in which
570 case this is NULL) or there is and we are reading it (in which case this
571 is non-NULL). */
3019eac3
DE
572 struct dwo_unit *dwo_unit;
573
574 /* The DW_AT_addr_base attribute if present, zero otherwise
575 (zero is a valid value though).
1dbab08b 576 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
577 ULONGEST addr_base;
578
2e3cf129
DE
579 /* The DW_AT_ranges_base attribute if present, zero otherwise
580 (zero is a valid value though).
1dbab08b 581 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 582 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
583 be used without needing to know whether DWO files are in use or not.
584 N.B. This does not apply to DW_AT_ranges appearing in
585 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
586 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
587 DW_AT_ranges_base *would* have to be applied, and we'd have to care
588 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
589 ULONGEST ranges_base;
590
ae038cb0
DJ
591 /* Mark used when releasing cached dies. */
592 unsigned int mark : 1;
593
8be455d7
JK
594 /* This CU references .debug_loc. See the symtab->locations_valid field.
595 This test is imperfect as there may exist optimized debug code not using
596 any location list and still facing inlining issues if handled as
597 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 598 unsigned int has_loclist : 1;
ba919b58 599
1b80a9fa
JK
600 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
601 if all the producer_is_* fields are valid. This information is cached
602 because profiling CU expansion showed excessive time spent in
603 producer_is_gxx_lt_4_6. */
ba919b58
TT
604 unsigned int checked_producer : 1;
605 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 606 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 607 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
608
609 /* When set, the file that we're processing is known to have
610 debugging info for C++ namespaces. GCC 3.3.x did not produce
611 this information, but later versions do. */
612
613 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
614};
615
10b3939b
DJ
616/* Persistent data held for a compilation unit, even when not
617 processing it. We put a pointer to this structure in the
28dee7f5 618 read_symtab_private field of the psymtab. */
10b3939b 619
ae038cb0
DJ
620struct dwarf2_per_cu_data
621{
36586728 622 /* The start offset and length of this compilation unit.
45452591 623 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
624 initial_length_size.
625 If the DIE refers to a DWO file, this is always of the original die,
626 not the DWO file. */
9c541725 627 sect_offset sect_off;
36586728 628 unsigned int length;
ae038cb0 629
43988095
JK
630 /* DWARF standard version this data has been read from (such as 4 or 5). */
631 short dwarf_version;
632
ae038cb0
DJ
633 /* Flag indicating this compilation unit will be read in before
634 any of the current compilation units are processed. */
c764a876 635 unsigned int queued : 1;
ae038cb0 636
0d99eb77
DE
637 /* This flag will be set when reading partial DIEs if we need to load
638 absolutely all DIEs for this compilation unit, instead of just the ones
639 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
640 hash table and don't find it. */
641 unsigned int load_all_dies : 1;
642
0186c6a7
DE
643 /* Non-zero if this CU is from .debug_types.
644 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
645 this is non-zero. */
3019eac3
DE
646 unsigned int is_debug_types : 1;
647
36586728
TT
648 /* Non-zero if this CU is from the .dwz file. */
649 unsigned int is_dwz : 1;
650
a2ce51a0
DE
651 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
652 This flag is only valid if is_debug_types is true.
653 We can't read a CU directly from a DWO file: There are required
654 attributes in the stub. */
655 unsigned int reading_dwo_directly : 1;
656
7ee85ab1
DE
657 /* Non-zero if the TU has been read.
658 This is used to assist the "Stay in DWO Optimization" for Fission:
659 When reading a DWO, it's faster to read TUs from the DWO instead of
660 fetching them from random other DWOs (due to comdat folding).
661 If the TU has already been read, the optimization is unnecessary
662 (and unwise - we don't want to change where gdb thinks the TU lives
663 "midflight").
664 This flag is only valid if is_debug_types is true. */
665 unsigned int tu_read : 1;
666
3019eac3
DE
667 /* The section this CU/TU lives in.
668 If the DIE refers to a DWO file, this is always the original die,
669 not the DWO file. */
8a0459fd 670 struct dwarf2_section_info *section;
348e048f 671
17ea53c3 672 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
673 of the CU cache it gets reset to NULL again. This is left as NULL for
674 dummy CUs (a CU header, but nothing else). */
ae038cb0 675 struct dwarf2_cu *cu;
1c379e20 676
9cdd5dbd
DE
677 /* The corresponding objfile.
678 Normally we can get the objfile from dwarf2_per_objfile.
679 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
680 struct objfile *objfile;
681
fffbe6a8
YQ
682 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
683 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
684 union
685 {
686 /* The partial symbol table associated with this compilation unit,
95554aad 687 or NULL for unread partial units. */
9291a0cd
TT
688 struct partial_symtab *psymtab;
689
690 /* Data needed by the "quick" functions. */
691 struct dwarf2_per_cu_quick_data *quick;
692 } v;
95554aad 693
796a7ff8
DE
694 /* The CUs we import using DW_TAG_imported_unit. This is filled in
695 while reading psymtabs, used to compute the psymtab dependencies,
696 and then cleared. Then it is filled in again while reading full
697 symbols, and only deleted when the objfile is destroyed.
698
699 This is also used to work around a difference between the way gold
700 generates .gdb_index version <=7 and the way gdb does. Arguably this
701 is a gold bug. For symbols coming from TUs, gold records in the index
702 the CU that includes the TU instead of the TU itself. This breaks
703 dw2_lookup_symbol: It assumes that if the index says symbol X lives
704 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
705 will find X. Alas TUs live in their own symtab, so after expanding CU Y
706 we need to look in TU Z to find X. Fortunately, this is akin to
707 DW_TAG_imported_unit, so we just use the same mechanism: For
708 .gdb_index version <=7 this also records the TUs that the CU referred
709 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
710 indices so we only pay a price for gold generated indices.
711 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 712 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
713};
714
348e048f
DE
715/* Entry in the signatured_types hash table. */
716
717struct signatured_type
718{
42e7ad6c 719 /* The "per_cu" object of this type.
ac9ec31b 720 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
721 N.B.: This is the first member so that it's easy to convert pointers
722 between them. */
723 struct dwarf2_per_cu_data per_cu;
724
3019eac3 725 /* The type's signature. */
348e048f
DE
726 ULONGEST signature;
727
3019eac3 728 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
729 If this TU is a DWO stub and the definition lives in a DWO file
730 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
731 cu_offset type_offset_in_tu;
732
733 /* Offset in the section of the type's DIE.
734 If the definition lives in a DWO file, this is the offset in the
735 .debug_types.dwo section.
736 The value is zero until the actual value is known.
737 Zero is otherwise not a valid section offset. */
738 sect_offset type_offset_in_section;
0186c6a7
DE
739
740 /* Type units are grouped by their DW_AT_stmt_list entry so that they
741 can share them. This points to the containing symtab. */
742 struct type_unit_group *type_unit_group;
ac9ec31b
DE
743
744 /* The type.
745 The first time we encounter this type we fully read it in and install it
746 in the symbol tables. Subsequent times we only need the type. */
747 struct type *type;
a2ce51a0
DE
748
749 /* Containing DWO unit.
750 This field is valid iff per_cu.reading_dwo_directly. */
751 struct dwo_unit *dwo_unit;
348e048f
DE
752};
753
0186c6a7
DE
754typedef struct signatured_type *sig_type_ptr;
755DEF_VEC_P (sig_type_ptr);
756
094b34ac
DE
757/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
758 This includes type_unit_group and quick_file_names. */
759
760struct stmt_list_hash
761{
762 /* The DWO unit this table is from or NULL if there is none. */
763 struct dwo_unit *dwo_unit;
764
765 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 766 sect_offset line_sect_off;
094b34ac
DE
767};
768
f4dc4d17
DE
769/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
770 an object of this type. */
771
772struct type_unit_group
773{
0186c6a7 774 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
775 To simplify things we create an artificial CU that "includes" all the
776 type units using this stmt_list so that the rest of the code still has
777 a "per_cu" handle on the symtab.
778 This PER_CU is recognized by having no section. */
8a0459fd 779#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
780 struct dwarf2_per_cu_data per_cu;
781
0186c6a7
DE
782 /* The TUs that share this DW_AT_stmt_list entry.
783 This is added to while parsing type units to build partial symtabs,
784 and is deleted afterwards and not used again. */
785 VEC (sig_type_ptr) *tus;
f4dc4d17 786
43f3e411 787 /* The compunit symtab.
094b34ac 788 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
789 so we create an essentially anonymous symtab as the compunit symtab. */
790 struct compunit_symtab *compunit_symtab;
f4dc4d17 791
094b34ac
DE
792 /* The data used to construct the hash key. */
793 struct stmt_list_hash hash;
f4dc4d17
DE
794
795 /* The number of symtabs from the line header.
796 The value here must match line_header.num_file_names. */
797 unsigned int num_symtabs;
798
799 /* The symbol tables for this TU (obtained from the files listed in
800 DW_AT_stmt_list).
801 WARNING: The order of entries here must match the order of entries
802 in the line header. After the first TU using this type_unit_group, the
803 line header for the subsequent TUs is recreated from this. This is done
804 because we need to use the same symtabs for each TU using the same
805 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
806 there's no guarantee the line header doesn't have duplicate entries. */
807 struct symtab **symtabs;
808};
809
73869dc2 810/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
811
812struct dwo_sections
813{
814 struct dwarf2_section_info abbrev;
3019eac3
DE
815 struct dwarf2_section_info line;
816 struct dwarf2_section_info loc;
43988095 817 struct dwarf2_section_info loclists;
09262596
DE
818 struct dwarf2_section_info macinfo;
819 struct dwarf2_section_info macro;
3019eac3
DE
820 struct dwarf2_section_info str;
821 struct dwarf2_section_info str_offsets;
80626a55
DE
822 /* In the case of a virtual DWO file, these two are unused. */
823 struct dwarf2_section_info info;
3019eac3
DE
824 VEC (dwarf2_section_info_def) *types;
825};
826
c88ee1f0 827/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
828
829struct dwo_unit
830{
831 /* Backlink to the containing struct dwo_file. */
832 struct dwo_file *dwo_file;
833
834 /* The "id" that distinguishes this CU/TU.
835 .debug_info calls this "dwo_id", .debug_types calls this "signature".
836 Since signatures came first, we stick with it for consistency. */
837 ULONGEST signature;
838
839 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 840 struct dwarf2_section_info *section;
3019eac3 841
9c541725
PA
842 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
843 sect_offset sect_off;
3019eac3
DE
844 unsigned int length;
845
846 /* For types, offset in the type's DIE of the type defined by this TU. */
847 cu_offset type_offset_in_tu;
848};
849
73869dc2
DE
850/* include/dwarf2.h defines the DWP section codes.
851 It defines a max value but it doesn't define a min value, which we
852 use for error checking, so provide one. */
853
854enum dwp_v2_section_ids
855{
856 DW_SECT_MIN = 1
857};
858
80626a55 859/* Data for one DWO file.
57d63ce2
DE
860
861 This includes virtual DWO files (a virtual DWO file is a DWO file as it
862 appears in a DWP file). DWP files don't really have DWO files per se -
863 comdat folding of types "loses" the DWO file they came from, and from
864 a high level view DWP files appear to contain a mass of random types.
865 However, to maintain consistency with the non-DWP case we pretend DWP
866 files contain virtual DWO files, and we assign each TU with one virtual
867 DWO file (generally based on the line and abbrev section offsets -
868 a heuristic that seems to work in practice). */
3019eac3
DE
869
870struct dwo_file
871{
0ac5b59e 872 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
873 For virtual DWO files the name is constructed from the section offsets
874 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
875 from related CU+TUs. */
0ac5b59e
DE
876 const char *dwo_name;
877
878 /* The DW_AT_comp_dir attribute. */
879 const char *comp_dir;
3019eac3 880
80626a55
DE
881 /* The bfd, when the file is open. Otherwise this is NULL.
882 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
883 bfd *dbfd;
3019eac3 884
73869dc2
DE
885 /* The sections that make up this DWO file.
886 Remember that for virtual DWO files in DWP V2, these are virtual
887 sections (for lack of a better name). */
3019eac3
DE
888 struct dwo_sections sections;
889
33c5cd75
DB
890 /* The CUs in the file.
891 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
892 an extension to handle LLVM's Link Time Optimization output (where
893 multiple source files may be compiled into a single object/dwo pair). */
894 htab_t cus;
3019eac3
DE
895
896 /* Table of TUs in the file.
897 Each element is a struct dwo_unit. */
898 htab_t tus;
899};
900
80626a55
DE
901/* These sections are what may appear in a DWP file. */
902
903struct dwp_sections
904{
73869dc2 905 /* These are used by both DWP version 1 and 2. */
80626a55
DE
906 struct dwarf2_section_info str;
907 struct dwarf2_section_info cu_index;
908 struct dwarf2_section_info tu_index;
73869dc2
DE
909
910 /* These are only used by DWP version 2 files.
911 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
912 sections are referenced by section number, and are not recorded here.
913 In DWP version 2 there is at most one copy of all these sections, each
914 section being (effectively) comprised of the concatenation of all of the
915 individual sections that exist in the version 1 format.
916 To keep the code simple we treat each of these concatenated pieces as a
917 section itself (a virtual section?). */
918 struct dwarf2_section_info abbrev;
919 struct dwarf2_section_info info;
920 struct dwarf2_section_info line;
921 struct dwarf2_section_info loc;
922 struct dwarf2_section_info macinfo;
923 struct dwarf2_section_info macro;
924 struct dwarf2_section_info str_offsets;
925 struct dwarf2_section_info types;
80626a55
DE
926};
927
73869dc2
DE
928/* These sections are what may appear in a virtual DWO file in DWP version 1.
929 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 930
73869dc2 931struct virtual_v1_dwo_sections
80626a55
DE
932{
933 struct dwarf2_section_info abbrev;
934 struct dwarf2_section_info line;
935 struct dwarf2_section_info loc;
936 struct dwarf2_section_info macinfo;
937 struct dwarf2_section_info macro;
938 struct dwarf2_section_info str_offsets;
939 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 940 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
941 struct dwarf2_section_info info_or_types;
942};
943
73869dc2
DE
944/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
945 In version 2, the sections of the DWO files are concatenated together
946 and stored in one section of that name. Thus each ELF section contains
947 several "virtual" sections. */
948
949struct virtual_v2_dwo_sections
950{
951 bfd_size_type abbrev_offset;
952 bfd_size_type abbrev_size;
953
954 bfd_size_type line_offset;
955 bfd_size_type line_size;
956
957 bfd_size_type loc_offset;
958 bfd_size_type loc_size;
959
960 bfd_size_type macinfo_offset;
961 bfd_size_type macinfo_size;
962
963 bfd_size_type macro_offset;
964 bfd_size_type macro_size;
965
966 bfd_size_type str_offsets_offset;
967 bfd_size_type str_offsets_size;
968
969 /* Each DWP hash table entry records one CU or one TU.
970 That is recorded here, and copied to dwo_unit.section. */
971 bfd_size_type info_or_types_offset;
972 bfd_size_type info_or_types_size;
973};
974
80626a55
DE
975/* Contents of DWP hash tables. */
976
977struct dwp_hash_table
978{
73869dc2 979 uint32_t version, nr_columns;
80626a55 980 uint32_t nr_units, nr_slots;
73869dc2
DE
981 const gdb_byte *hash_table, *unit_table;
982 union
983 {
984 struct
985 {
986 const gdb_byte *indices;
987 } v1;
988 struct
989 {
990 /* This is indexed by column number and gives the id of the section
991 in that column. */
992#define MAX_NR_V2_DWO_SECTIONS \
993 (1 /* .debug_info or .debug_types */ \
994 + 1 /* .debug_abbrev */ \
995 + 1 /* .debug_line */ \
996 + 1 /* .debug_loc */ \
997 + 1 /* .debug_str_offsets */ \
998 + 1 /* .debug_macro or .debug_macinfo */)
999 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1000 const gdb_byte *offsets;
1001 const gdb_byte *sizes;
1002 } v2;
1003 } section_pool;
80626a55
DE
1004};
1005
1006/* Data for one DWP file. */
1007
1008struct dwp_file
1009{
1010 /* Name of the file. */
1011 const char *name;
1012
73869dc2
DE
1013 /* File format version. */
1014 int version;
1015
93417882 1016 /* The bfd. */
80626a55
DE
1017 bfd *dbfd;
1018
1019 /* Section info for this file. */
1020 struct dwp_sections sections;
1021
57d63ce2 1022 /* Table of CUs in the file. */
80626a55
DE
1023 const struct dwp_hash_table *cus;
1024
1025 /* Table of TUs in the file. */
1026 const struct dwp_hash_table *tus;
1027
19ac8c2e
DE
1028 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1029 htab_t loaded_cus;
1030 htab_t loaded_tus;
80626a55 1031
73869dc2
DE
1032 /* Table to map ELF section numbers to their sections.
1033 This is only needed for the DWP V1 file format. */
80626a55
DE
1034 unsigned int num_sections;
1035 asection **elf_sections;
1036};
1037
36586728
TT
1038/* This represents a '.dwz' file. */
1039
1040struct dwz_file
1041{
1042 /* A dwz file can only contain a few sections. */
1043 struct dwarf2_section_info abbrev;
1044 struct dwarf2_section_info info;
1045 struct dwarf2_section_info str;
1046 struct dwarf2_section_info line;
1047 struct dwarf2_section_info macro;
2ec9a5e0 1048 struct dwarf2_section_info gdb_index;
36586728
TT
1049
1050 /* The dwz's BFD. */
1051 bfd *dwz_bfd;
1052};
1053
0963b4bd
MS
1054/* Struct used to pass misc. parameters to read_die_and_children, et
1055 al. which are used for both .debug_info and .debug_types dies.
1056 All parameters here are unchanging for the life of the call. This
dee91e82 1057 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1058
1059struct die_reader_specs
1060{
a32a8923 1061 /* The bfd of die_section. */
93311388
DE
1062 bfd* abfd;
1063
1064 /* The CU of the DIE we are parsing. */
1065 struct dwarf2_cu *cu;
1066
80626a55 1067 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1068 struct dwo_file *dwo_file;
1069
dee91e82 1070 /* The section the die comes from.
3019eac3 1071 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1072 struct dwarf2_section_info *die_section;
1073
1074 /* die_section->buffer. */
d521ce57 1075 const gdb_byte *buffer;
f664829e
DE
1076
1077 /* The end of the buffer. */
1078 const gdb_byte *buffer_end;
a2ce51a0
DE
1079
1080 /* The value of the DW_AT_comp_dir attribute. */
1081 const char *comp_dir;
93311388
DE
1082};
1083
fd820528 1084/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1085typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1086 const gdb_byte *info_ptr,
dee91e82
DE
1087 struct die_info *comp_unit_die,
1088 int has_children,
1089 void *data);
1090
ecfb656c
PA
1091/* A 1-based directory index. This is a strong typedef to prevent
1092 accidentally using a directory index as a 0-based index into an
1093 array/vector. */
1094enum class dir_index : unsigned int {};
1095
1096/* Likewise, a 1-based file name index. */
1097enum class file_name_index : unsigned int {};
1098
52059ffd
TT
1099struct file_entry
1100{
fff8551c
PA
1101 file_entry () = default;
1102
ecfb656c 1103 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1104 unsigned int mod_time_, unsigned int length_)
1105 : name (name_),
ecfb656c 1106 d_index (d_index_),
fff8551c
PA
1107 mod_time (mod_time_),
1108 length (length_)
1109 {}
1110
ecfb656c
PA
1111 /* Return the include directory at D_INDEX stored in LH. Returns
1112 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1113 const char *include_dir (const line_header *lh) const;
1114
fff8551c
PA
1115 /* The file name. Note this is an observing pointer. The memory is
1116 owned by debug_line_buffer. */
1117 const char *name {};
1118
8c43009f 1119 /* The directory index (1-based). */
ecfb656c 1120 dir_index d_index {};
fff8551c
PA
1121
1122 unsigned int mod_time {};
1123
1124 unsigned int length {};
1125
1126 /* True if referenced by the Line Number Program. */
1127 bool included_p {};
1128
83769d0b 1129 /* The associated symbol table, if any. */
fff8551c 1130 struct symtab *symtab {};
52059ffd
TT
1131};
1132
debd256d
JB
1133/* The line number information for a compilation unit (found in the
1134 .debug_line section) begins with a "statement program header",
1135 which contains the following information. */
1136struct line_header
1137{
fff8551c
PA
1138 line_header ()
1139 : offset_in_dwz {}
1140 {}
1141
1142 /* Add an entry to the include directory table. */
1143 void add_include_dir (const char *include_dir);
1144
1145 /* Add an entry to the file name table. */
ecfb656c 1146 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1147 unsigned int mod_time, unsigned int length);
1148
ecfb656c 1149 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1150 is out of bounds. */
ecfb656c 1151 const char *include_dir_at (dir_index index) const
8c43009f 1152 {
ecfb656c
PA
1153 /* Convert directory index number (1-based) to vector index
1154 (0-based). */
1155 size_t vec_index = to_underlying (index) - 1;
1156
1157 if (vec_index >= include_dirs.size ())
8c43009f 1158 return NULL;
ecfb656c 1159 return include_dirs[vec_index];
8c43009f
PA
1160 }
1161
ecfb656c 1162 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1163 is out of bounds. */
ecfb656c 1164 file_entry *file_name_at (file_name_index index)
8c43009f 1165 {
ecfb656c
PA
1166 /* Convert file name index number (1-based) to vector index
1167 (0-based). */
1168 size_t vec_index = to_underlying (index) - 1;
1169
1170 if (vec_index >= file_names.size ())
fff8551c 1171 return NULL;
ecfb656c 1172 return &file_names[vec_index];
fff8551c
PA
1173 }
1174
1175 /* Const version of the above. */
1176 const file_entry *file_name_at (unsigned int index) const
1177 {
1178 if (index >= file_names.size ())
8c43009f
PA
1179 return NULL;
1180 return &file_names[index];
1181 }
1182
527f3840 1183 /* Offset of line number information in .debug_line section. */
9c541725 1184 sect_offset sect_off {};
527f3840
JK
1185
1186 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1187 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1188
1189 unsigned int total_length {};
1190 unsigned short version {};
1191 unsigned int header_length {};
1192 unsigned char minimum_instruction_length {};
1193 unsigned char maximum_ops_per_instruction {};
1194 unsigned char default_is_stmt {};
1195 int line_base {};
1196 unsigned char line_range {};
1197 unsigned char opcode_base {};
debd256d
JB
1198
1199 /* standard_opcode_lengths[i] is the number of operands for the
1200 standard opcode whose value is i. This means that
1201 standard_opcode_lengths[0] is unused, and the last meaningful
1202 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1203 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1204
fff8551c
PA
1205 /* The include_directories table. Note these are observing
1206 pointers. The memory is owned by debug_line_buffer. */
1207 std::vector<const char *> include_dirs;
debd256d 1208
fff8551c
PA
1209 /* The file_names table. */
1210 std::vector<file_entry> file_names;
debd256d
JB
1211
1212 /* The start and end of the statement program following this
6502dd73 1213 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1214 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1215};
c906108c 1216
fff8551c
PA
1217typedef std::unique_ptr<line_header> line_header_up;
1218
8c43009f
PA
1219const char *
1220file_entry::include_dir (const line_header *lh) const
1221{
ecfb656c 1222 return lh->include_dir_at (d_index);
8c43009f
PA
1223}
1224
c906108c 1225/* When we construct a partial symbol table entry we only
0963b4bd 1226 need this much information. */
c906108c
SS
1227struct partial_die_info
1228 {
72bf9492 1229 /* Offset of this DIE. */
9c541725 1230 sect_offset sect_off;
72bf9492
DJ
1231
1232 /* DWARF-2 tag for this DIE. */
1233 ENUM_BITFIELD(dwarf_tag) tag : 16;
1234
72bf9492
DJ
1235 /* Assorted flags describing the data found in this DIE. */
1236 unsigned int has_children : 1;
1237 unsigned int is_external : 1;
1238 unsigned int is_declaration : 1;
1239 unsigned int has_type : 1;
1240 unsigned int has_specification : 1;
1241 unsigned int has_pc_info : 1;
481860b3 1242 unsigned int may_be_inlined : 1;
72bf9492 1243
0c1b455e
TT
1244 /* This DIE has been marked DW_AT_main_subprogram. */
1245 unsigned int main_subprogram : 1;
1246
72bf9492
DJ
1247 /* Flag set if the SCOPE field of this structure has been
1248 computed. */
1249 unsigned int scope_set : 1;
1250
fa4028e9
JB
1251 /* Flag set if the DIE has a byte_size attribute. */
1252 unsigned int has_byte_size : 1;
1253
ff908ebf
AW
1254 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1255 unsigned int has_const_value : 1;
1256
98bfdba5
PA
1257 /* Flag set if any of the DIE's children are template arguments. */
1258 unsigned int has_template_arguments : 1;
1259
abc72ce4
DE
1260 /* Flag set if fixup_partial_die has been called on this die. */
1261 unsigned int fixup_called : 1;
1262
36586728
TT
1263 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1264 unsigned int is_dwz : 1;
1265
1266 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1267 unsigned int spec_is_dwz : 1;
1268
72bf9492 1269 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1270 sometimes a default name for unnamed DIEs. */
15d034d0 1271 const char *name;
72bf9492 1272
abc72ce4
DE
1273 /* The linkage name, if present. */
1274 const char *linkage_name;
1275
72bf9492
DJ
1276 /* The scope to prepend to our children. This is generally
1277 allocated on the comp_unit_obstack, so will disappear
1278 when this compilation unit leaves the cache. */
15d034d0 1279 const char *scope;
72bf9492 1280
95554aad
TT
1281 /* Some data associated with the partial DIE. The tag determines
1282 which field is live. */
1283 union
1284 {
1285 /* The location description associated with this DIE, if any. */
1286 struct dwarf_block *locdesc;
1287 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1288 sect_offset sect_off;
95554aad 1289 } d;
72bf9492
DJ
1290
1291 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1292 CORE_ADDR lowpc;
1293 CORE_ADDR highpc;
72bf9492 1294
93311388 1295 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1296 DW_AT_sibling, if any. */
abc72ce4
DE
1297 /* NOTE: This member isn't strictly necessary, read_partial_die could
1298 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1299 const gdb_byte *sibling;
72bf9492
DJ
1300
1301 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1302 DW_AT_specification (or DW_AT_abstract_origin or
1303 DW_AT_extension). */
b64f50a1 1304 sect_offset spec_offset;
72bf9492
DJ
1305
1306 /* Pointers to this DIE's parent, first child, and next sibling,
1307 if any. */
1308 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1309 };
1310
0963b4bd 1311/* This data structure holds the information of an abbrev. */
c906108c
SS
1312struct abbrev_info
1313 {
1314 unsigned int number; /* number identifying abbrev */
1315 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1316 unsigned short has_children; /* boolean */
1317 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1318 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1319 struct abbrev_info *next; /* next in chain */
1320 };
1321
1322struct attr_abbrev
1323 {
9d25dd43
DE
1324 ENUM_BITFIELD(dwarf_attribute) name : 16;
1325 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1326
1327 /* It is valid only if FORM is DW_FORM_implicit_const. */
1328 LONGEST implicit_const;
c906108c
SS
1329 };
1330
433df2d4
DE
1331/* Size of abbrev_table.abbrev_hash_table. */
1332#define ABBREV_HASH_SIZE 121
1333
1334/* Top level data structure to contain an abbreviation table. */
1335
1336struct abbrev_table
1337{
f4dc4d17
DE
1338 /* Where the abbrev table came from.
1339 This is used as a sanity check when the table is used. */
9c541725 1340 sect_offset sect_off;
433df2d4
DE
1341
1342 /* Storage for the abbrev table. */
1343 struct obstack abbrev_obstack;
1344
1345 /* Hash table of abbrevs.
1346 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1347 It could be statically allocated, but the previous code didn't so we
1348 don't either. */
1349 struct abbrev_info **abbrevs;
1350};
1351
0963b4bd 1352/* Attributes have a name and a value. */
b60c80d6
DJ
1353struct attribute
1354 {
9d25dd43 1355 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1356 ENUM_BITFIELD(dwarf_form) form : 15;
1357
1358 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1359 field should be in u.str (existing only for DW_STRING) but it is kept
1360 here for better struct attribute alignment. */
1361 unsigned int string_is_canonical : 1;
1362
b60c80d6
DJ
1363 union
1364 {
15d034d0 1365 const char *str;
b60c80d6 1366 struct dwarf_block *blk;
43bbcdc2
PH
1367 ULONGEST unsnd;
1368 LONGEST snd;
b60c80d6 1369 CORE_ADDR addr;
ac9ec31b 1370 ULONGEST signature;
b60c80d6
DJ
1371 }
1372 u;
1373 };
1374
0963b4bd 1375/* This data structure holds a complete die structure. */
c906108c
SS
1376struct die_info
1377 {
76815b17
DE
1378 /* DWARF-2 tag for this DIE. */
1379 ENUM_BITFIELD(dwarf_tag) tag : 16;
1380
1381 /* Number of attributes */
98bfdba5
PA
1382 unsigned char num_attrs;
1383
1384 /* True if we're presently building the full type name for the
1385 type derived from this DIE. */
1386 unsigned char building_fullname : 1;
76815b17 1387
adde2bff
DE
1388 /* True if this die is in process. PR 16581. */
1389 unsigned char in_process : 1;
1390
76815b17
DE
1391 /* Abbrev number */
1392 unsigned int abbrev;
1393
93311388 1394 /* Offset in .debug_info or .debug_types section. */
9c541725 1395 sect_offset sect_off;
78ba4af6
JB
1396
1397 /* The dies in a compilation unit form an n-ary tree. PARENT
1398 points to this die's parent; CHILD points to the first child of
1399 this node; and all the children of a given node are chained
4950bc1c 1400 together via their SIBLING fields. */
639d11d3
DC
1401 struct die_info *child; /* Its first child, if any. */
1402 struct die_info *sibling; /* Its next sibling, if any. */
1403 struct die_info *parent; /* Its parent, if any. */
c906108c 1404
b60c80d6
DJ
1405 /* An array of attributes, with NUM_ATTRS elements. There may be
1406 zero, but it's not common and zero-sized arrays are not
1407 sufficiently portable C. */
1408 struct attribute attrs[1];
c906108c
SS
1409 };
1410
0963b4bd 1411/* Get at parts of an attribute structure. */
c906108c
SS
1412
1413#define DW_STRING(attr) ((attr)->u.str)
8285870a 1414#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1415#define DW_UNSND(attr) ((attr)->u.unsnd)
1416#define DW_BLOCK(attr) ((attr)->u.blk)
1417#define DW_SND(attr) ((attr)->u.snd)
1418#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1419#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1420
0963b4bd 1421/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1422struct dwarf_block
1423 {
56eb65bd 1424 size_t size;
1d6edc3c
JK
1425
1426 /* Valid only if SIZE is not zero. */
d521ce57 1427 const gdb_byte *data;
c906108c
SS
1428 };
1429
c906108c
SS
1430#ifndef ATTR_ALLOC_CHUNK
1431#define ATTR_ALLOC_CHUNK 4
1432#endif
1433
c906108c
SS
1434/* Allocate fields for structs, unions and enums in this size. */
1435#ifndef DW_FIELD_ALLOC_CHUNK
1436#define DW_FIELD_ALLOC_CHUNK 4
1437#endif
1438
c906108c
SS
1439/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1440 but this would require a corresponding change in unpack_field_as_long
1441 and friends. */
1442static int bits_per_byte = 8;
1443
52059ffd
TT
1444struct nextfield
1445{
1446 struct nextfield *next;
1447 int accessibility;
1448 int virtuality;
1449 struct field field;
1450};
1451
1452struct nextfnfield
1453{
1454 struct nextfnfield *next;
1455 struct fn_field fnfield;
1456};
1457
1458struct fnfieldlist
1459{
1460 const char *name;
1461 int length;
1462 struct nextfnfield *head;
1463};
1464
1465struct typedef_field_list
1466{
1467 struct typedef_field field;
1468 struct typedef_field_list *next;
1469};
1470
c906108c
SS
1471/* The routines that read and process dies for a C struct or C++ class
1472 pass lists of data member fields and lists of member function fields
1473 in an instance of a field_info structure, as defined below. */
1474struct field_info
c5aa993b 1475 {
0963b4bd 1476 /* List of data member and baseclasses fields. */
52059ffd 1477 struct nextfield *fields, *baseclasses;
c906108c 1478
7d0ccb61 1479 /* Number of fields (including baseclasses). */
c5aa993b 1480 int nfields;
c906108c 1481
c5aa993b
JM
1482 /* Number of baseclasses. */
1483 int nbaseclasses;
c906108c 1484
c5aa993b
JM
1485 /* Set if the accesibility of one of the fields is not public. */
1486 int non_public_fields;
c906108c 1487
c5aa993b
JM
1488 /* Member function fieldlist array, contains name of possibly overloaded
1489 member function, number of overloaded member functions and a pointer
1490 to the head of the member function field chain. */
52059ffd 1491 struct fnfieldlist *fnfieldlists;
c906108c 1492
c5aa993b
JM
1493 /* Number of entries in the fnfieldlists array. */
1494 int nfnfields;
98751a41
JK
1495
1496 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1497 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1498 struct typedef_field_list *typedef_field_list;
98751a41 1499 unsigned typedef_field_list_count;
c5aa993b 1500 };
c906108c 1501
10b3939b
DJ
1502/* One item on the queue of compilation units to read in full symbols
1503 for. */
1504struct dwarf2_queue_item
1505{
1506 struct dwarf2_per_cu_data *per_cu;
95554aad 1507 enum language pretend_language;
10b3939b
DJ
1508 struct dwarf2_queue_item *next;
1509};
1510
1511/* The current queue. */
1512static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1513
ae038cb0
DJ
1514/* Loaded secondary compilation units are kept in memory until they
1515 have not been referenced for the processing of this many
1516 compilation units. Set this to zero to disable caching. Cache
1517 sizes of up to at least twenty will improve startup time for
1518 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1519static int dwarf_max_cache_age = 5;
920d2a44 1520static void
b4f54984
DE
1521show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1522 struct cmd_list_element *c, const char *value)
920d2a44 1523{
3e43a32a 1524 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1525 "DWARF compilation units is %s.\n"),
920d2a44
AC
1526 value);
1527}
4390d890 1528\f
c906108c
SS
1529/* local function prototypes */
1530
a32a8923
DE
1531static const char *get_section_name (const struct dwarf2_section_info *);
1532
1533static const char *get_section_file_name (const struct dwarf2_section_info *);
1534
918dd910
JK
1535static void dwarf2_find_base_address (struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
0018ea6f
DE
1538static struct partial_symtab *create_partial_symtab
1539 (struct dwarf2_per_cu_data *per_cu, const char *name);
1540
f1902523
JK
1541static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1542 const gdb_byte *info_ptr,
1543 struct die_info *type_unit_die,
1544 int has_children, void *data);
1545
c67a9c90 1546static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1547
72bf9492
DJ
1548static void scan_partial_symbols (struct partial_die_info *,
1549 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1550 int, struct dwarf2_cu *);
c906108c 1551
72bf9492
DJ
1552static void add_partial_symbol (struct partial_die_info *,
1553 struct dwarf2_cu *);
63d06c5c 1554
72bf9492
DJ
1555static void add_partial_namespace (struct partial_die_info *pdi,
1556 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1557 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1558
5d7cb8df 1559static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1560 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1561 struct dwarf2_cu *cu);
1562
72bf9492
DJ
1563static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1564 struct dwarf2_cu *cu);
91c24f0a 1565
bc30ff58
JB
1566static void add_partial_subprogram (struct partial_die_info *pdi,
1567 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1568 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1569
257e7a09
YQ
1570static void dwarf2_read_symtab (struct partial_symtab *,
1571 struct objfile *);
c906108c 1572
a14ed312 1573static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1574
433df2d4
DE
1575static struct abbrev_info *abbrev_table_lookup_abbrev
1576 (const struct abbrev_table *, unsigned int);
1577
1578static struct abbrev_table *abbrev_table_read_table
1579 (struct dwarf2_section_info *, sect_offset);
1580
1581static void abbrev_table_free (struct abbrev_table *);
1582
f4dc4d17
DE
1583static void abbrev_table_free_cleanup (void *);
1584
dee91e82
DE
1585static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1586 struct dwarf2_section_info *);
c906108c 1587
f3dd6933 1588static void dwarf2_free_abbrev_table (void *);
c906108c 1589
d521ce57 1590static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1591
dee91e82 1592static struct partial_die_info *load_partial_dies
d521ce57 1593 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1594
d521ce57
TT
1595static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1596 struct partial_die_info *,
1597 struct abbrev_info *,
1598 unsigned int,
1599 const gdb_byte *);
c906108c 1600
36586728 1601static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1602 struct dwarf2_cu *);
72bf9492
DJ
1603
1604static void fixup_partial_die (struct partial_die_info *,
1605 struct dwarf2_cu *);
1606
d521ce57
TT
1607static const gdb_byte *read_attribute (const struct die_reader_specs *,
1608 struct attribute *, struct attr_abbrev *,
1609 const gdb_byte *);
a8329558 1610
a1855c1d 1611static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1612
a1855c1d 1613static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1614
a1855c1d 1615static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1616
a1855c1d 1617static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1618
a1855c1d 1619static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1620
d521ce57 1621static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1622 unsigned int *);
c906108c 1623
d521ce57 1624static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1625
1626static LONGEST read_checked_initial_length_and_offset
d521ce57 1627 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1628 unsigned int *, unsigned int *);
613e1657 1629
d521ce57
TT
1630static LONGEST read_offset (bfd *, const gdb_byte *,
1631 const struct comp_unit_head *,
c764a876
DE
1632 unsigned int *);
1633
d521ce57 1634static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1635
f4dc4d17
DE
1636static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1637 sect_offset);
1638
d521ce57 1639static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1640
d521ce57 1641static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1642
d521ce57
TT
1643static const char *read_indirect_string (bfd *, const gdb_byte *,
1644 const struct comp_unit_head *,
1645 unsigned int *);
4bdf3d34 1646
43988095
JK
1647static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1648 const struct comp_unit_head *,
1649 unsigned int *);
36586728 1650
43988095 1651static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1652
d521ce57 1653static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1654
d521ce57
TT
1655static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1656 const gdb_byte *,
3019eac3
DE
1657 unsigned int *);
1658
d521ce57 1659static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1660 ULONGEST str_index);
3019eac3 1661
e142c38c 1662static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1663
e142c38c
DJ
1664static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1665 struct dwarf2_cu *);
c906108c 1666
348e048f 1667static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1668 unsigned int);
348e048f 1669
7d45c7c3
KB
1670static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1671 struct dwarf2_cu *cu);
1672
05cf31d1
JB
1673static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1674 struct dwarf2_cu *cu);
1675
e142c38c 1676static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1677
e142c38c 1678static struct die_info *die_specification (struct die_info *die,
f2f0e013 1679 struct dwarf2_cu **);
63d06c5c 1680
9c541725 1681static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1682 struct dwarf2_cu *cu);
debd256d 1683
f3f5162e 1684static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1685 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1686 CORE_ADDR, int decode_mapping);
c906108c 1687
4d663531 1688static void dwarf2_start_subfile (const char *, const char *);
c906108c 1689
43f3e411
DE
1690static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1691 const char *, const char *,
1692 CORE_ADDR);
f4dc4d17 1693
a14ed312 1694static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1695 struct dwarf2_cu *);
c906108c 1696
34eaf542
TT
1697static struct symbol *new_symbol_full (struct die_info *, struct type *,
1698 struct dwarf2_cu *, struct symbol *);
1699
ff39bb5e 1700static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1701 struct dwarf2_cu *);
c906108c 1702
ff39bb5e 1703static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1704 struct type *type,
1705 const char *name,
1706 struct obstack *obstack,
12df843f 1707 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1708 const gdb_byte **bytes,
98bfdba5 1709 struct dwarf2_locexpr_baton **baton);
2df3850c 1710
e7c27a73 1711static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1712
b4ba55a1
JB
1713static int need_gnat_info (struct dwarf2_cu *);
1714
3e43a32a
MS
1715static struct type *die_descriptive_type (struct die_info *,
1716 struct dwarf2_cu *);
b4ba55a1
JB
1717
1718static void set_descriptive_type (struct type *, struct die_info *,
1719 struct dwarf2_cu *);
1720
e7c27a73
DJ
1721static struct type *die_containing_type (struct die_info *,
1722 struct dwarf2_cu *);
c906108c 1723
ff39bb5e 1724static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1725 struct dwarf2_cu *);
c906108c 1726
f792889a 1727static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1728
673bfd45
DE
1729static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1730
0d5cff50 1731static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1732
6e70227d 1733static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1734 const char *suffix, int physname,
1735 struct dwarf2_cu *cu);
63d06c5c 1736
e7c27a73 1737static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1738
348e048f
DE
1739static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1740
e7c27a73 1741static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1742
e7c27a73 1743static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1744
96408a79
SA
1745static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1746
ff013f42
JK
1747static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1748 struct dwarf2_cu *, struct partial_symtab *);
1749
3a2b436a 1750/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1751 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1752enum pc_bounds_kind
1753{
e385593e 1754 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1755 PC_BOUNDS_NOT_PRESENT,
1756
e385593e
JK
1757 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1758 were present but they do not form a valid range of PC addresses. */
1759 PC_BOUNDS_INVALID,
1760
3a2b436a
JK
1761 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1762 PC_BOUNDS_RANGES,
1763
1764 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1765 PC_BOUNDS_HIGH_LOW,
1766};
1767
1768static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1769 CORE_ADDR *, CORE_ADDR *,
1770 struct dwarf2_cu *,
1771 struct partial_symtab *);
c906108c 1772
fae299cd
DC
1773static void get_scope_pc_bounds (struct die_info *,
1774 CORE_ADDR *, CORE_ADDR *,
1775 struct dwarf2_cu *);
1776
801e3a5b
JB
1777static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1778 CORE_ADDR, struct dwarf2_cu *);
1779
a14ed312 1780static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1781 struct dwarf2_cu *);
c906108c 1782
a14ed312 1783static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1784 struct type *, struct dwarf2_cu *);
c906108c 1785
a14ed312 1786static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1787 struct die_info *, struct type *,
e7c27a73 1788 struct dwarf2_cu *);
c906108c 1789
a14ed312 1790static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1791 struct type *,
1792 struct dwarf2_cu *);
c906108c 1793
134d01f1 1794static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1795
e7c27a73 1796static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1797
e7c27a73 1798static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1799
5d7cb8df
JK
1800static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1801
22cee43f
PMR
1802static struct using_direct **using_directives (enum language);
1803
27aa8d6a
SW
1804static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1805
74921315
KS
1806static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1807
f55ee35c
JK
1808static struct type *read_module_type (struct die_info *die,
1809 struct dwarf2_cu *cu);
1810
38d518c9 1811static const char *namespace_name (struct die_info *die,
e142c38c 1812 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1813
134d01f1 1814static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1815
e7c27a73 1816static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1817
6e70227d 1818static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1819 struct dwarf2_cu *);
1820
bf6af496 1821static struct die_info *read_die_and_siblings_1
d521ce57 1822 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1823 struct die_info *);
639d11d3 1824
dee91e82 1825static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1826 const gdb_byte *info_ptr,
1827 const gdb_byte **new_info_ptr,
639d11d3
DC
1828 struct die_info *parent);
1829
d521ce57
TT
1830static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1831 struct die_info **, const gdb_byte *,
1832 int *, int);
3019eac3 1833
d521ce57
TT
1834static const gdb_byte *read_full_die (const struct die_reader_specs *,
1835 struct die_info **, const gdb_byte *,
1836 int *);
93311388 1837
e7c27a73 1838static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1839
15d034d0
TT
1840static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1841 struct obstack *);
71c25dea 1842
15d034d0 1843static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1844
15d034d0 1845static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1846 struct die_info *die,
1847 struct dwarf2_cu *cu);
1848
ca69b9e6
DE
1849static const char *dwarf2_physname (const char *name, struct die_info *die,
1850 struct dwarf2_cu *cu);
1851
e142c38c 1852static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1853 struct dwarf2_cu **);
9219021c 1854
f39c6ffd 1855static const char *dwarf_tag_name (unsigned int);
c906108c 1856
f39c6ffd 1857static const char *dwarf_attr_name (unsigned int);
c906108c 1858
f39c6ffd 1859static const char *dwarf_form_name (unsigned int);
c906108c 1860
a121b7c1 1861static const char *dwarf_bool_name (unsigned int);
c906108c 1862
f39c6ffd 1863static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1864
f9aca02d 1865static struct die_info *sibling_die (struct die_info *);
c906108c 1866
d97bc12b
DE
1867static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1868
1869static void dump_die_for_error (struct die_info *);
1870
1871static void dump_die_1 (struct ui_file *, int level, int max_level,
1872 struct die_info *);
c906108c 1873
d97bc12b 1874/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1875
51545339 1876static void store_in_ref_table (struct die_info *,
10b3939b 1877 struct dwarf2_cu *);
c906108c 1878
ff39bb5e 1879static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1880
ff39bb5e 1881static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1882
348e048f 1883static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1884 const struct attribute *,
348e048f
DE
1885 struct dwarf2_cu **);
1886
10b3939b 1887static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1888 const struct attribute *,
f2f0e013 1889 struct dwarf2_cu **);
c906108c 1890
348e048f 1891static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1892 const struct attribute *,
348e048f
DE
1893 struct dwarf2_cu **);
1894
ac9ec31b
DE
1895static struct type *get_signatured_type (struct die_info *, ULONGEST,
1896 struct dwarf2_cu *);
1897
1898static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1899 const struct attribute *,
ac9ec31b
DE
1900 struct dwarf2_cu *);
1901
e5fe5e75 1902static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1903
52dc124a 1904static void read_signatured_type (struct signatured_type *);
348e048f 1905
63e43d3a
PMR
1906static int attr_to_dynamic_prop (const struct attribute *attr,
1907 struct die_info *die, struct dwarf2_cu *cu,
1908 struct dynamic_prop *prop);
1909
c906108c
SS
1910/* memory allocation interface */
1911
7b5a2f43 1912static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1913
b60c80d6 1914static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1915
43f3e411 1916static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1917
6e5a29e1 1918static int attr_form_is_block (const struct attribute *);
8e19ed76 1919
6e5a29e1 1920static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1921
6e5a29e1 1922static int attr_form_is_constant (const struct attribute *);
3690dd37 1923
6e5a29e1 1924static int attr_form_is_ref (const struct attribute *);
7771576e 1925
8cf6f0b1
TT
1926static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1927 struct dwarf2_loclist_baton *baton,
ff39bb5e 1928 const struct attribute *attr);
8cf6f0b1 1929
ff39bb5e 1930static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1931 struct symbol *sym,
f1e6e072
TT
1932 struct dwarf2_cu *cu,
1933 int is_block);
4c2df51b 1934
d521ce57
TT
1935static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1936 const gdb_byte *info_ptr,
1937 struct abbrev_info *abbrev);
4bb7a0a7 1938
72bf9492
DJ
1939static void free_stack_comp_unit (void *);
1940
72bf9492
DJ
1941static hashval_t partial_die_hash (const void *item);
1942
1943static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1944
ae038cb0 1945static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1946 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1947
9816fde3 1948static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1949 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1950
1951static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1952 struct die_info *comp_unit_die,
1953 enum language pretend_language);
93311388 1954
68dc6402 1955static void free_heap_comp_unit (void *);
ae038cb0
DJ
1956
1957static void free_cached_comp_units (void *);
1958
1959static void age_cached_comp_units (void);
1960
dee91e82 1961static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1962
f792889a
DJ
1963static struct type *set_die_type (struct die_info *, struct type *,
1964 struct dwarf2_cu *);
1c379e20 1965
ae038cb0
DJ
1966static void create_all_comp_units (struct objfile *);
1967
0e50663e 1968static int create_all_type_units (struct objfile *);
1fd400ff 1969
95554aad
TT
1970static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1971 enum language);
10b3939b 1972
95554aad
TT
1973static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1974 enum language);
10b3939b 1975
f4dc4d17
DE
1976static void process_full_type_unit (struct dwarf2_per_cu_data *,
1977 enum language);
1978
10b3939b
DJ
1979static void dwarf2_add_dependence (struct dwarf2_cu *,
1980 struct dwarf2_per_cu_data *);
1981
ae038cb0
DJ
1982static void dwarf2_mark (struct dwarf2_cu *);
1983
1984static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1985
b64f50a1 1986static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1987 struct dwarf2_per_cu_data *);
673bfd45 1988
f792889a 1989static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1990
9291a0cd
TT
1991static void dwarf2_release_queue (void *dummy);
1992
95554aad
TT
1993static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1994 enum language pretend_language);
1995
a0f42c21 1996static void process_queue (void);
9291a0cd 1997
d721ba37
PA
1998/* The return type of find_file_and_directory. Note, the enclosed
1999 string pointers are only valid while this object is valid. */
2000
2001struct file_and_directory
2002{
2003 /* The filename. This is never NULL. */
2004 const char *name;
2005
2006 /* The compilation directory. NULL if not known. If we needed to
2007 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2008 points directly to the DW_AT_comp_dir string attribute owned by
2009 the obstack that owns the DIE. */
2010 const char *comp_dir;
2011
2012 /* If we needed to build a new string for comp_dir, this is what
2013 owns the storage. */
2014 std::string comp_dir_storage;
2015};
2016
2017static file_and_directory find_file_and_directory (struct die_info *die,
2018 struct dwarf2_cu *cu);
9291a0cd
TT
2019
2020static char *file_full_name (int file, struct line_header *lh,
2021 const char *comp_dir);
2022
43988095
JK
2023/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2024enum class rcuh_kind { COMPILE, TYPE };
2025
d521ce57 2026static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2027 (struct comp_unit_head *header,
2028 struct dwarf2_section_info *section,
d521ce57 2029 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2030 rcuh_kind section_kind);
36586728 2031
fd820528 2032static void init_cutu_and_read_dies
f4dc4d17
DE
2033 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2034 int use_existing_cu, int keep,
3019eac3
DE
2035 die_reader_func_ftype *die_reader_func, void *data);
2036
dee91e82
DE
2037static void init_cutu_and_read_dies_simple
2038 (struct dwarf2_per_cu_data *this_cu,
2039 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2040
673bfd45 2041static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2042
3019eac3
DE
2043static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2044
57d63ce2
DE
2045static struct dwo_unit *lookup_dwo_unit_in_dwp
2046 (struct dwp_file *dwp_file, const char *comp_dir,
2047 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2048
2049static struct dwp_file *get_dwp_file (void);
2050
3019eac3 2051static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2052 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2053
2054static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2055 (struct signatured_type *, const char *, const char *);
3019eac3 2056
89e63ee4
DE
2057static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2058
3019eac3
DE
2059static void free_dwo_file_cleanup (void *);
2060
95554aad
TT
2061static void process_cu_includes (void);
2062
1b80a9fa 2063static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2064
2065static void free_line_header_voidp (void *arg);
4390d890
DE
2066\f
2067/* Various complaints about symbol reading that don't abort the process. */
2068
2069static void
2070dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2071{
2072 complaint (&symfile_complaints,
2073 _("statement list doesn't fit in .debug_line section"));
2074}
2075
2076static void
2077dwarf2_debug_line_missing_file_complaint (void)
2078{
2079 complaint (&symfile_complaints,
2080 _(".debug_line section has line data without a file"));
2081}
2082
2083static void
2084dwarf2_debug_line_missing_end_sequence_complaint (void)
2085{
2086 complaint (&symfile_complaints,
2087 _(".debug_line section has line "
2088 "program sequence without an end"));
2089}
2090
2091static void
2092dwarf2_complex_location_expr_complaint (void)
2093{
2094 complaint (&symfile_complaints, _("location expression too complex"));
2095}
2096
2097static void
2098dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2099 int arg3)
2100{
2101 complaint (&symfile_complaints,
2102 _("const value length mismatch for '%s', got %d, expected %d"),
2103 arg1, arg2, arg3);
2104}
2105
2106static void
2107dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2108{
2109 complaint (&symfile_complaints,
2110 _("debug info runs off end of %s section"
2111 " [in module %s]"),
a32a8923
DE
2112 get_section_name (section),
2113 get_section_file_name (section));
4390d890 2114}
1b80a9fa 2115
4390d890
DE
2116static void
2117dwarf2_macro_malformed_definition_complaint (const char *arg1)
2118{
2119 complaint (&symfile_complaints,
2120 _("macro debug info contains a "
2121 "malformed macro definition:\n`%s'"),
2122 arg1);
2123}
2124
2125static void
2126dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2127{
2128 complaint (&symfile_complaints,
2129 _("invalid attribute class or form for '%s' in '%s'"),
2130 arg1, arg2);
2131}
527f3840
JK
2132
2133/* Hash function for line_header_hash. */
2134
2135static hashval_t
2136line_header_hash (const struct line_header *ofs)
2137{
9c541725 2138 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2139}
2140
2141/* Hash function for htab_create_alloc_ex for line_header_hash. */
2142
2143static hashval_t
2144line_header_hash_voidp (const void *item)
2145{
9a3c8263 2146 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2147
2148 return line_header_hash (ofs);
2149}
2150
2151/* Equality function for line_header_hash. */
2152
2153static int
2154line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2155{
9a3c8263
SM
2156 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2157 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2158
9c541725 2159 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2160 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2161}
2162
4390d890 2163\f
9291a0cd
TT
2164#if WORDS_BIGENDIAN
2165
2166/* Convert VALUE between big- and little-endian. */
2167static offset_type
2168byte_swap (offset_type value)
2169{
2170 offset_type result;
2171
2172 result = (value & 0xff) << 24;
2173 result |= (value & 0xff00) << 8;
2174 result |= (value & 0xff0000) >> 8;
2175 result |= (value & 0xff000000) >> 24;
2176 return result;
2177}
2178
2179#define MAYBE_SWAP(V) byte_swap (V)
2180
2181#else
bc8f2430 2182#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2183#endif /* WORDS_BIGENDIAN */
2184
31aa7e4e
JB
2185/* Read the given attribute value as an address, taking the attribute's
2186 form into account. */
2187
2188static CORE_ADDR
2189attr_value_as_address (struct attribute *attr)
2190{
2191 CORE_ADDR addr;
2192
2193 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2194 {
2195 /* Aside from a few clearly defined exceptions, attributes that
2196 contain an address must always be in DW_FORM_addr form.
2197 Unfortunately, some compilers happen to be violating this
2198 requirement by encoding addresses using other forms, such
2199 as DW_FORM_data4 for example. For those broken compilers,
2200 we try to do our best, without any guarantee of success,
2201 to interpret the address correctly. It would also be nice
2202 to generate a complaint, but that would require us to maintain
2203 a list of legitimate cases where a non-address form is allowed,
2204 as well as update callers to pass in at least the CU's DWARF
2205 version. This is more overhead than what we're willing to
2206 expand for a pretty rare case. */
2207 addr = DW_UNSND (attr);
2208 }
2209 else
2210 addr = DW_ADDR (attr);
2211
2212 return addr;
2213}
2214
9291a0cd
TT
2215/* The suffix for an index file. */
2216#define INDEX_SUFFIX ".gdb-index"
2217
330cdd98
PA
2218/* See declaration. */
2219
2220dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2221 const dwarf2_debug_sections *names)
2222 : objfile (objfile_)
2223{
2224 if (names == NULL)
2225 names = &dwarf2_elf_names;
2226
2227 bfd *obfd = objfile->obfd;
2228
2229 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2230 locate_sections (obfd, sec, *names);
2231}
2232
2233dwarf2_per_objfile::~dwarf2_per_objfile ()
2234{
2235 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2236 free_cached_comp_units ();
2237
2238 if (quick_file_names_table)
2239 htab_delete (quick_file_names_table);
2240
2241 if (line_header_hash)
2242 htab_delete (line_header_hash);
2243
2244 /* Everything else should be on the objfile obstack. */
2245}
2246
2247/* See declaration. */
2248
2249void
2250dwarf2_per_objfile::free_cached_comp_units ()
2251{
2252 dwarf2_per_cu_data *per_cu = read_in_chain;
2253 dwarf2_per_cu_data **last_chain = &read_in_chain;
2254 while (per_cu != NULL)
2255 {
2256 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2257
2258 free_heap_comp_unit (per_cu->cu);
2259 *last_chain = next_cu;
2260 per_cu = next_cu;
2261 }
2262}
2263
c906108c 2264/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2265 information and return true if we have enough to do something.
2266 NAMES points to the dwarf2 section names, or is NULL if the standard
2267 ELF names are used. */
c906108c
SS
2268
2269int
251d32d9
TG
2270dwarf2_has_info (struct objfile *objfile,
2271 const struct dwarf2_debug_sections *names)
c906108c 2272{
9a3c8263
SM
2273 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2274 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2275 if (!dwarf2_per_objfile)
2276 {
2277 /* Initialize per-objfile state. */
2278 struct dwarf2_per_objfile *data
8d749320 2279 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2280
330cdd98
PA
2281 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2282 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2283 }
73869dc2 2284 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2285 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2286 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2287 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2288}
2289
2290/* Return the containing section of virtual section SECTION. */
2291
2292static struct dwarf2_section_info *
2293get_containing_section (const struct dwarf2_section_info *section)
2294{
2295 gdb_assert (section->is_virtual);
2296 return section->s.containing_section;
c906108c
SS
2297}
2298
a32a8923
DE
2299/* Return the bfd owner of SECTION. */
2300
2301static struct bfd *
2302get_section_bfd_owner (const struct dwarf2_section_info *section)
2303{
73869dc2
DE
2304 if (section->is_virtual)
2305 {
2306 section = get_containing_section (section);
2307 gdb_assert (!section->is_virtual);
2308 }
049412e3 2309 return section->s.section->owner;
a32a8923
DE
2310}
2311
2312/* Return the bfd section of SECTION.
2313 Returns NULL if the section is not present. */
2314
2315static asection *
2316get_section_bfd_section (const struct dwarf2_section_info *section)
2317{
73869dc2
DE
2318 if (section->is_virtual)
2319 {
2320 section = get_containing_section (section);
2321 gdb_assert (!section->is_virtual);
2322 }
049412e3 2323 return section->s.section;
a32a8923
DE
2324}
2325
2326/* Return the name of SECTION. */
2327
2328static const char *
2329get_section_name (const struct dwarf2_section_info *section)
2330{
2331 asection *sectp = get_section_bfd_section (section);
2332
2333 gdb_assert (sectp != NULL);
2334 return bfd_section_name (get_section_bfd_owner (section), sectp);
2335}
2336
2337/* Return the name of the file SECTION is in. */
2338
2339static const char *
2340get_section_file_name (const struct dwarf2_section_info *section)
2341{
2342 bfd *abfd = get_section_bfd_owner (section);
2343
2344 return bfd_get_filename (abfd);
2345}
2346
2347/* Return the id of SECTION.
2348 Returns 0 if SECTION doesn't exist. */
2349
2350static int
2351get_section_id (const struct dwarf2_section_info *section)
2352{
2353 asection *sectp = get_section_bfd_section (section);
2354
2355 if (sectp == NULL)
2356 return 0;
2357 return sectp->id;
2358}
2359
2360/* Return the flags of SECTION.
73869dc2 2361 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2362
2363static int
2364get_section_flags (const struct dwarf2_section_info *section)
2365{
2366 asection *sectp = get_section_bfd_section (section);
2367
2368 gdb_assert (sectp != NULL);
2369 return bfd_get_section_flags (sectp->owner, sectp);
2370}
2371
251d32d9
TG
2372/* When loading sections, we look either for uncompressed section or for
2373 compressed section names. */
233a11ab
CS
2374
2375static int
251d32d9
TG
2376section_is_p (const char *section_name,
2377 const struct dwarf2_section_names *names)
233a11ab 2378{
251d32d9
TG
2379 if (names->normal != NULL
2380 && strcmp (section_name, names->normal) == 0)
2381 return 1;
2382 if (names->compressed != NULL
2383 && strcmp (section_name, names->compressed) == 0)
2384 return 1;
2385 return 0;
233a11ab
CS
2386}
2387
330cdd98 2388/* See declaration. */
c906108c 2389
330cdd98
PA
2390void
2391dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2392 const dwarf2_debug_sections &names)
c906108c 2393{
dc7650b8 2394 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2395
dc7650b8
JK
2396 if ((aflag & SEC_HAS_CONTENTS) == 0)
2397 {
2398 }
330cdd98 2399 else if (section_is_p (sectp->name, &names.info))
c906108c 2400 {
330cdd98
PA
2401 this->info.s.section = sectp;
2402 this->info.size = bfd_get_section_size (sectp);
c906108c 2403 }
330cdd98 2404 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2405 {
330cdd98
PA
2406 this->abbrev.s.section = sectp;
2407 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2408 }
330cdd98 2409 else if (section_is_p (sectp->name, &names.line))
c906108c 2410 {
330cdd98
PA
2411 this->line.s.section = sectp;
2412 this->line.size = bfd_get_section_size (sectp);
c906108c 2413 }
330cdd98 2414 else if (section_is_p (sectp->name, &names.loc))
c906108c 2415 {
330cdd98
PA
2416 this->loc.s.section = sectp;
2417 this->loc.size = bfd_get_section_size (sectp);
c906108c 2418 }
330cdd98 2419 else if (section_is_p (sectp->name, &names.loclists))
43988095 2420 {
330cdd98
PA
2421 this->loclists.s.section = sectp;
2422 this->loclists.size = bfd_get_section_size (sectp);
43988095 2423 }
330cdd98 2424 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2425 {
330cdd98
PA
2426 this->macinfo.s.section = sectp;
2427 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2428 }
330cdd98 2429 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2430 {
330cdd98
PA
2431 this->macro.s.section = sectp;
2432 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2433 }
330cdd98 2434 else if (section_is_p (sectp->name, &names.str))
c906108c 2435 {
330cdd98
PA
2436 this->str.s.section = sectp;
2437 this->str.size = bfd_get_section_size (sectp);
c906108c 2438 }
330cdd98 2439 else if (section_is_p (sectp->name, &names.line_str))
43988095 2440 {
330cdd98
PA
2441 this->line_str.s.section = sectp;
2442 this->line_str.size = bfd_get_section_size (sectp);
43988095 2443 }
330cdd98 2444 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2445 {
330cdd98
PA
2446 this->addr.s.section = sectp;
2447 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2448 }
330cdd98 2449 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2450 {
330cdd98
PA
2451 this->frame.s.section = sectp;
2452 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2453 }
330cdd98 2454 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2455 {
330cdd98
PA
2456 this->eh_frame.s.section = sectp;
2457 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2458 }
330cdd98 2459 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2460 {
330cdd98
PA
2461 this->ranges.s.section = sectp;
2462 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2463 }
330cdd98 2464 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2465 {
330cdd98
PA
2466 this->rnglists.s.section = sectp;
2467 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2468 }
330cdd98 2469 else if (section_is_p (sectp->name, &names.types))
348e048f 2470 {
8b70b953
TT
2471 struct dwarf2_section_info type_section;
2472
2473 memset (&type_section, 0, sizeof (type_section));
049412e3 2474 type_section.s.section = sectp;
8b70b953
TT
2475 type_section.size = bfd_get_section_size (sectp);
2476
330cdd98 2477 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2478 &type_section);
348e048f 2479 }
330cdd98 2480 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2481 {
330cdd98
PA
2482 this->gdb_index.s.section = sectp;
2483 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2484 }
dce234bc 2485
b4e1fd61 2486 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2487 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2488 this->has_section_at_zero = true;
c906108c
SS
2489}
2490
fceca515
DE
2491/* A helper function that decides whether a section is empty,
2492 or not present. */
9e0ac564
TT
2493
2494static int
19ac8c2e 2495dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2496{
73869dc2
DE
2497 if (section->is_virtual)
2498 return section->size == 0;
049412e3 2499 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2500}
2501
3019eac3
DE
2502/* Read the contents of the section INFO.
2503 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2504 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2505 of the DWO file.
dce234bc 2506 If the section is compressed, uncompress it before returning. */
c906108c 2507
dce234bc
PP
2508static void
2509dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2510{
a32a8923 2511 asection *sectp;
3019eac3 2512 bfd *abfd;
dce234bc 2513 gdb_byte *buf, *retbuf;
c906108c 2514
be391dca
TT
2515 if (info->readin)
2516 return;
dce234bc 2517 info->buffer = NULL;
be391dca 2518 info->readin = 1;
188dd5d6 2519
9e0ac564 2520 if (dwarf2_section_empty_p (info))
dce234bc 2521 return;
c906108c 2522
a32a8923 2523 sectp = get_section_bfd_section (info);
3019eac3 2524
73869dc2
DE
2525 /* If this is a virtual section we need to read in the real one first. */
2526 if (info->is_virtual)
2527 {
2528 struct dwarf2_section_info *containing_section =
2529 get_containing_section (info);
2530
2531 gdb_assert (sectp != NULL);
2532 if ((sectp->flags & SEC_RELOC) != 0)
2533 {
2534 error (_("Dwarf Error: DWP format V2 with relocations is not"
2535 " supported in section %s [in module %s]"),
2536 get_section_name (info), get_section_file_name (info));
2537 }
2538 dwarf2_read_section (objfile, containing_section);
2539 /* Other code should have already caught virtual sections that don't
2540 fit. */
2541 gdb_assert (info->virtual_offset + info->size
2542 <= containing_section->size);
2543 /* If the real section is empty or there was a problem reading the
2544 section we shouldn't get here. */
2545 gdb_assert (containing_section->buffer != NULL);
2546 info->buffer = containing_section->buffer + info->virtual_offset;
2547 return;
2548 }
2549
4bf44c1c
TT
2550 /* If the section has relocations, we must read it ourselves.
2551 Otherwise we attach it to the BFD. */
2552 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2553 {
d521ce57 2554 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2555 return;
dce234bc 2556 }
dce234bc 2557
224c3ddb 2558 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2559 info->buffer = buf;
dce234bc
PP
2560
2561 /* When debugging .o files, we may need to apply relocations; see
2562 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2563 We never compress sections in .o files, so we only need to
2564 try this when the section is not compressed. */
ac8035ab 2565 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2566 if (retbuf != NULL)
2567 {
2568 info->buffer = retbuf;
2569 return;
2570 }
2571
a32a8923
DE
2572 abfd = get_section_bfd_owner (info);
2573 gdb_assert (abfd != NULL);
2574
dce234bc
PP
2575 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2576 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2577 {
2578 error (_("Dwarf Error: Can't read DWARF data"
2579 " in section %s [in module %s]"),
2580 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2581 }
dce234bc
PP
2582}
2583
9e0ac564
TT
2584/* A helper function that returns the size of a section in a safe way.
2585 If you are positive that the section has been read before using the
2586 size, then it is safe to refer to the dwarf2_section_info object's
2587 "size" field directly. In other cases, you must call this
2588 function, because for compressed sections the size field is not set
2589 correctly until the section has been read. */
2590
2591static bfd_size_type
2592dwarf2_section_size (struct objfile *objfile,
2593 struct dwarf2_section_info *info)
2594{
2595 if (!info->readin)
2596 dwarf2_read_section (objfile, info);
2597 return info->size;
2598}
2599
dce234bc 2600/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2601 SECTION_NAME. */
af34e669 2602
dce234bc 2603void
3017a003
TG
2604dwarf2_get_section_info (struct objfile *objfile,
2605 enum dwarf2_section_enum sect,
d521ce57 2606 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2607 bfd_size_type *sizep)
2608{
2609 struct dwarf2_per_objfile *data
9a3c8263
SM
2610 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2611 dwarf2_objfile_data_key);
dce234bc 2612 struct dwarf2_section_info *info;
a3b2a86b
TT
2613
2614 /* We may see an objfile without any DWARF, in which case we just
2615 return nothing. */
2616 if (data == NULL)
2617 {
2618 *sectp = NULL;
2619 *bufp = NULL;
2620 *sizep = 0;
2621 return;
2622 }
3017a003
TG
2623 switch (sect)
2624 {
2625 case DWARF2_DEBUG_FRAME:
2626 info = &data->frame;
2627 break;
2628 case DWARF2_EH_FRAME:
2629 info = &data->eh_frame;
2630 break;
2631 default:
2632 gdb_assert_not_reached ("unexpected section");
2633 }
dce234bc 2634
9e0ac564 2635 dwarf2_read_section (objfile, info);
dce234bc 2636
a32a8923 2637 *sectp = get_section_bfd_section (info);
dce234bc
PP
2638 *bufp = info->buffer;
2639 *sizep = info->size;
2640}
2641
36586728
TT
2642/* A helper function to find the sections for a .dwz file. */
2643
2644static void
2645locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2646{
9a3c8263 2647 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2648
2649 /* Note that we only support the standard ELF names, because .dwz
2650 is ELF-only (at the time of writing). */
2651 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2652 {
049412e3 2653 dwz_file->abbrev.s.section = sectp;
36586728
TT
2654 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2655 }
2656 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2657 {
049412e3 2658 dwz_file->info.s.section = sectp;
36586728
TT
2659 dwz_file->info.size = bfd_get_section_size (sectp);
2660 }
2661 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2662 {
049412e3 2663 dwz_file->str.s.section = sectp;
36586728
TT
2664 dwz_file->str.size = bfd_get_section_size (sectp);
2665 }
2666 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2667 {
049412e3 2668 dwz_file->line.s.section = sectp;
36586728
TT
2669 dwz_file->line.size = bfd_get_section_size (sectp);
2670 }
2671 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2672 {
049412e3 2673 dwz_file->macro.s.section = sectp;
36586728
TT
2674 dwz_file->macro.size = bfd_get_section_size (sectp);
2675 }
2ec9a5e0
TT
2676 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2677 {
049412e3 2678 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2679 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2680 }
36586728
TT
2681}
2682
4db1a1dc
TT
2683/* Open the separate '.dwz' debug file, if needed. Return NULL if
2684 there is no .gnu_debugaltlink section in the file. Error if there
2685 is such a section but the file cannot be found. */
36586728
TT
2686
2687static struct dwz_file *
2688dwarf2_get_dwz_file (void)
2689{
4db1a1dc 2690 char *data;
36586728
TT
2691 struct cleanup *cleanup;
2692 const char *filename;
2693 struct dwz_file *result;
acd13123 2694 bfd_size_type buildid_len_arg;
dc294be5
TT
2695 size_t buildid_len;
2696 bfd_byte *buildid;
36586728
TT
2697
2698 if (dwarf2_per_objfile->dwz_file != NULL)
2699 return dwarf2_per_objfile->dwz_file;
2700
4db1a1dc
TT
2701 bfd_set_error (bfd_error_no_error);
2702 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2703 &buildid_len_arg, &buildid);
4db1a1dc
TT
2704 if (data == NULL)
2705 {
2706 if (bfd_get_error () == bfd_error_no_error)
2707 return NULL;
2708 error (_("could not read '.gnu_debugaltlink' section: %s"),
2709 bfd_errmsg (bfd_get_error ()));
2710 }
36586728 2711 cleanup = make_cleanup (xfree, data);
dc294be5 2712 make_cleanup (xfree, buildid);
36586728 2713
acd13123
TT
2714 buildid_len = (size_t) buildid_len_arg;
2715
f9d83a0b 2716 filename = (const char *) data;
d721ba37
PA
2717
2718 std::string abs_storage;
36586728
TT
2719 if (!IS_ABSOLUTE_PATH (filename))
2720 {
14278e1f
TT
2721 gdb::unique_xmalloc_ptr<char> abs
2722 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2723
14278e1f 2724 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2725 filename = abs_storage.c_str ();
36586728
TT
2726 }
2727
dc294be5
TT
2728 /* First try the file name given in the section. If that doesn't
2729 work, try to use the build-id instead. */
192b62ce 2730 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2731 if (dwz_bfd != NULL)
36586728 2732 {
192b62ce
TT
2733 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2734 dwz_bfd.release ();
36586728
TT
2735 }
2736
dc294be5
TT
2737 if (dwz_bfd == NULL)
2738 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2739
2740 if (dwz_bfd == NULL)
2741 error (_("could not find '.gnu_debugaltlink' file for %s"),
2742 objfile_name (dwarf2_per_objfile->objfile));
2743
36586728
TT
2744 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2745 struct dwz_file);
192b62ce 2746 result->dwz_bfd = dwz_bfd.release ();
36586728 2747
192b62ce 2748 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2749
2750 do_cleanups (cleanup);
2751
192b62ce 2752 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2753 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2754 return result;
2755}
9291a0cd 2756\f
7b9f3c50
DE
2757/* DWARF quick_symbols_functions support. */
2758
2759/* TUs can share .debug_line entries, and there can be a lot more TUs than
2760 unique line tables, so we maintain a separate table of all .debug_line
2761 derived entries to support the sharing.
2762 All the quick functions need is the list of file names. We discard the
2763 line_header when we're done and don't need to record it here. */
2764struct quick_file_names
2765{
094b34ac
DE
2766 /* The data used to construct the hash key. */
2767 struct stmt_list_hash hash;
7b9f3c50
DE
2768
2769 /* The number of entries in file_names, real_names. */
2770 unsigned int num_file_names;
2771
2772 /* The file names from the line table, after being run through
2773 file_full_name. */
2774 const char **file_names;
2775
2776 /* The file names from the line table after being run through
2777 gdb_realpath. These are computed lazily. */
2778 const char **real_names;
2779};
2780
2781/* When using the index (and thus not using psymtabs), each CU has an
2782 object of this type. This is used to hold information needed by
2783 the various "quick" methods. */
2784struct dwarf2_per_cu_quick_data
2785{
2786 /* The file table. This can be NULL if there was no file table
2787 or it's currently not read in.
2788 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2789 struct quick_file_names *file_names;
2790
2791 /* The corresponding symbol table. This is NULL if symbols for this
2792 CU have not yet been read. */
43f3e411 2793 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2794
2795 /* A temporary mark bit used when iterating over all CUs in
2796 expand_symtabs_matching. */
2797 unsigned int mark : 1;
2798
2799 /* True if we've tried to read the file table and found there isn't one.
2800 There will be no point in trying to read it again next time. */
2801 unsigned int no_file_data : 1;
2802};
2803
094b34ac
DE
2804/* Utility hash function for a stmt_list_hash. */
2805
2806static hashval_t
2807hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2808{
2809 hashval_t v = 0;
2810
2811 if (stmt_list_hash->dwo_unit != NULL)
2812 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2813 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2814 return v;
2815}
2816
2817/* Utility equality function for a stmt_list_hash. */
2818
2819static int
2820eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2821 const struct stmt_list_hash *rhs)
2822{
2823 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2824 return 0;
2825 if (lhs->dwo_unit != NULL
2826 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2827 return 0;
2828
9c541725 2829 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2830}
2831
7b9f3c50
DE
2832/* Hash function for a quick_file_names. */
2833
2834static hashval_t
2835hash_file_name_entry (const void *e)
2836{
9a3c8263
SM
2837 const struct quick_file_names *file_data
2838 = (const struct quick_file_names *) e;
7b9f3c50 2839
094b34ac 2840 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2841}
2842
2843/* Equality function for a quick_file_names. */
2844
2845static int
2846eq_file_name_entry (const void *a, const void *b)
2847{
9a3c8263
SM
2848 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2849 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2850
094b34ac 2851 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2852}
2853
2854/* Delete function for a quick_file_names. */
2855
2856static void
2857delete_file_name_entry (void *e)
2858{
9a3c8263 2859 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2860 int i;
2861
2862 for (i = 0; i < file_data->num_file_names; ++i)
2863 {
2864 xfree ((void*) file_data->file_names[i]);
2865 if (file_data->real_names)
2866 xfree ((void*) file_data->real_names[i]);
2867 }
2868
2869 /* The space for the struct itself lives on objfile_obstack,
2870 so we don't free it here. */
2871}
2872
2873/* Create a quick_file_names hash table. */
2874
2875static htab_t
2876create_quick_file_names_table (unsigned int nr_initial_entries)
2877{
2878 return htab_create_alloc (nr_initial_entries,
2879 hash_file_name_entry, eq_file_name_entry,
2880 delete_file_name_entry, xcalloc, xfree);
2881}
9291a0cd 2882
918dd910
JK
2883/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2884 have to be created afterwards. You should call age_cached_comp_units after
2885 processing PER_CU->CU. dw2_setup must have been already called. */
2886
2887static void
2888load_cu (struct dwarf2_per_cu_data *per_cu)
2889{
3019eac3 2890 if (per_cu->is_debug_types)
e5fe5e75 2891 load_full_type_unit (per_cu);
918dd910 2892 else
95554aad 2893 load_full_comp_unit (per_cu, language_minimal);
918dd910 2894
cc12ce38
DE
2895 if (per_cu->cu == NULL)
2896 return; /* Dummy CU. */
2dc860c0
DE
2897
2898 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2899}
2900
a0f42c21 2901/* Read in the symbols for PER_CU. */
2fdf6df6 2902
9291a0cd 2903static void
a0f42c21 2904dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2905{
2906 struct cleanup *back_to;
2907
f4dc4d17
DE
2908 /* Skip type_unit_groups, reading the type units they contain
2909 is handled elsewhere. */
2910 if (IS_TYPE_UNIT_GROUP (per_cu))
2911 return;
2912
9291a0cd
TT
2913 back_to = make_cleanup (dwarf2_release_queue, NULL);
2914
95554aad 2915 if (dwarf2_per_objfile->using_index
43f3e411 2916 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2917 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2918 {
2919 queue_comp_unit (per_cu, language_minimal);
2920 load_cu (per_cu);
89e63ee4
DE
2921
2922 /* If we just loaded a CU from a DWO, and we're working with an index
2923 that may badly handle TUs, load all the TUs in that DWO as well.
2924 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2925 if (!per_cu->is_debug_types
cc12ce38 2926 && per_cu->cu != NULL
89e63ee4
DE
2927 && per_cu->cu->dwo_unit != NULL
2928 && dwarf2_per_objfile->index_table != NULL
2929 && dwarf2_per_objfile->index_table->version <= 7
2930 /* DWP files aren't supported yet. */
2931 && get_dwp_file () == NULL)
2932 queue_and_load_all_dwo_tus (per_cu);
95554aad 2933 }
9291a0cd 2934
a0f42c21 2935 process_queue ();
9291a0cd
TT
2936
2937 /* Age the cache, releasing compilation units that have not
2938 been used recently. */
2939 age_cached_comp_units ();
2940
2941 do_cleanups (back_to);
2942}
2943
2944/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2945 the objfile from which this CU came. Returns the resulting symbol
2946 table. */
2fdf6df6 2947
43f3e411 2948static struct compunit_symtab *
a0f42c21 2949dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2950{
95554aad 2951 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2952 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2953 {
2954 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2955 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2956 dw2_do_instantiate_symtab (per_cu);
95554aad 2957 process_cu_includes ();
9291a0cd
TT
2958 do_cleanups (back_to);
2959 }
f194fefb 2960
43f3e411 2961 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2962}
2963
8832e7e3 2964/* Return the CU/TU given its index.
f4dc4d17
DE
2965
2966 This is intended for loops like:
2967
2968 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2969 + dwarf2_per_objfile->n_type_units); ++i)
2970 {
8832e7e3 2971 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2972
2973 ...;
2974 }
2975*/
2fdf6df6 2976
1fd400ff 2977static struct dwarf2_per_cu_data *
8832e7e3 2978dw2_get_cutu (int index)
1fd400ff
TT
2979{
2980 if (index >= dwarf2_per_objfile->n_comp_units)
2981 {
f4dc4d17 2982 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2983 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2984 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2985 }
2986
2987 return dwarf2_per_objfile->all_comp_units[index];
2988}
2989
8832e7e3
DE
2990/* Return the CU given its index.
2991 This differs from dw2_get_cutu in that it's for when you know INDEX
2992 refers to a CU. */
f4dc4d17
DE
2993
2994static struct dwarf2_per_cu_data *
8832e7e3 2995dw2_get_cu (int index)
f4dc4d17 2996{
8832e7e3 2997 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2998
1fd400ff
TT
2999 return dwarf2_per_objfile->all_comp_units[index];
3000}
3001
2ec9a5e0
TT
3002/* A helper for create_cus_from_index that handles a given list of
3003 CUs. */
2fdf6df6 3004
74a0d9f6 3005static void
2ec9a5e0
TT
3006create_cus_from_index_list (struct objfile *objfile,
3007 const gdb_byte *cu_list, offset_type n_elements,
3008 struct dwarf2_section_info *section,
3009 int is_dwz,
3010 int base_offset)
9291a0cd
TT
3011{
3012 offset_type i;
9291a0cd 3013
2ec9a5e0 3014 for (i = 0; i < n_elements; i += 2)
9291a0cd 3015 {
74a0d9f6 3016 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3017
3018 sect_offset sect_off
3019 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3020 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3021 cu_list += 2 * 8;
3022
9c541725
PA
3023 dwarf2_per_cu_data *the_cu
3024 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3025 struct dwarf2_per_cu_data);
3026 the_cu->sect_off = sect_off;
9291a0cd
TT
3027 the_cu->length = length;
3028 the_cu->objfile = objfile;
8a0459fd 3029 the_cu->section = section;
9291a0cd
TT
3030 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3031 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3032 the_cu->is_dwz = is_dwz;
3033 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3034 }
9291a0cd
TT
3035}
3036
2ec9a5e0 3037/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3038 the CU objects for this objfile. */
2ec9a5e0 3039
74a0d9f6 3040static void
2ec9a5e0
TT
3041create_cus_from_index (struct objfile *objfile,
3042 const gdb_byte *cu_list, offset_type cu_list_elements,
3043 const gdb_byte *dwz_list, offset_type dwz_elements)
3044{
3045 struct dwz_file *dwz;
3046
3047 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3048 dwarf2_per_objfile->all_comp_units =
3049 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3050 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3051
74a0d9f6
JK
3052 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3053 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3054
3055 if (dwz_elements == 0)
74a0d9f6 3056 return;
2ec9a5e0
TT
3057
3058 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3059 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3060 cu_list_elements / 2);
2ec9a5e0
TT
3061}
3062
1fd400ff 3063/* Create the signatured type hash table from the index. */
673bfd45 3064
74a0d9f6 3065static void
673bfd45 3066create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3067 struct dwarf2_section_info *section,
673bfd45
DE
3068 const gdb_byte *bytes,
3069 offset_type elements)
1fd400ff
TT
3070{
3071 offset_type i;
673bfd45 3072 htab_t sig_types_hash;
1fd400ff 3073
6aa5f3a6
DE
3074 dwarf2_per_objfile->n_type_units
3075 = dwarf2_per_objfile->n_allocated_type_units
3076 = elements / 3;
8d749320
SM
3077 dwarf2_per_objfile->all_type_units =
3078 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3079
673bfd45 3080 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3081
3082 for (i = 0; i < elements; i += 3)
3083 {
52dc124a 3084 struct signatured_type *sig_type;
9c541725 3085 ULONGEST signature;
1fd400ff 3086 void **slot;
9c541725 3087 cu_offset type_offset_in_tu;
1fd400ff 3088
74a0d9f6 3089 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3090 sect_offset sect_off
3091 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3092 type_offset_in_tu
3093 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3094 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3095 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3096 bytes += 3 * 8;
3097
52dc124a 3098 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3099 struct signatured_type);
52dc124a 3100 sig_type->signature = signature;
9c541725 3101 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3102 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3103 sig_type->per_cu.section = section;
9c541725 3104 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3105 sig_type->per_cu.objfile = objfile;
3106 sig_type->per_cu.v.quick
1fd400ff
TT
3107 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3108 struct dwarf2_per_cu_quick_data);
3109
52dc124a
DE
3110 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3111 *slot = sig_type;
1fd400ff 3112
b4dd5633 3113 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3114 }
3115
673bfd45 3116 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3117}
3118
9291a0cd
TT
3119/* Read the address map data from the mapped index, and use it to
3120 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3121
9291a0cd
TT
3122static void
3123create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3124{
3e29f34a 3125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3126 const gdb_byte *iter, *end;
9291a0cd 3127 struct addrmap *mutable_map;
9291a0cd
TT
3128 CORE_ADDR baseaddr;
3129
8268c778
PA
3130 auto_obstack temp_obstack;
3131
9291a0cd
TT
3132 mutable_map = addrmap_create_mutable (&temp_obstack);
3133
3134 iter = index->address_table;
3135 end = iter + index->address_table_size;
3136
3137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3138
3139 while (iter < end)
3140 {
3141 ULONGEST hi, lo, cu_index;
3142 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3143 iter += 8;
3144 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3145 iter += 8;
3146 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3147 iter += 4;
f652bce2 3148
24a55014 3149 if (lo > hi)
f652bce2 3150 {
24a55014
DE
3151 complaint (&symfile_complaints,
3152 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3153 hex_string (lo), hex_string (hi));
24a55014 3154 continue;
f652bce2 3155 }
24a55014
DE
3156
3157 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3158 {
3159 complaint (&symfile_complaints,
3160 _(".gdb_index address table has invalid CU number %u"),
3161 (unsigned) cu_index);
24a55014 3162 continue;
f652bce2 3163 }
24a55014 3164
3e29f34a
MR
3165 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3166 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3167 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3168 }
3169
3170 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3171 &objfile->objfile_obstack);
9291a0cd
TT
3172}
3173
59d7bcaf
JK
3174/* The hash function for strings in the mapped index. This is the same as
3175 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3176 implementation. This is necessary because the hash function is tied to the
3177 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3178 SYMBOL_HASH_NEXT.
3179
3180 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3181
9291a0cd 3182static hashval_t
559a7a62 3183mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3184{
3185 const unsigned char *str = (const unsigned char *) p;
3186 hashval_t r = 0;
3187 unsigned char c;
3188
3189 while ((c = *str++) != 0)
559a7a62
JK
3190 {
3191 if (index_version >= 5)
3192 c = tolower (c);
3193 r = r * 67 + c - 113;
3194 }
9291a0cd
TT
3195
3196 return r;
3197}
3198
3199/* Find a slot in the mapped index INDEX for the object named NAME.
3200 If NAME is found, set *VEC_OUT to point to the CU vector in the
3201 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3202
9291a0cd
TT
3203static int
3204find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3205 offset_type **vec_out)
3206{
0cf03b49
JK
3207 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3208 offset_type hash;
9291a0cd 3209 offset_type slot, step;
559a7a62 3210 int (*cmp) (const char *, const char *);
9291a0cd 3211
0cf03b49 3212 if (current_language->la_language == language_cplus
45280282
IB
3213 || current_language->la_language == language_fortran
3214 || current_language->la_language == language_d)
0cf03b49
JK
3215 {
3216 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3217 not contain any. */
a8719064 3218
72998fb3 3219 if (strchr (name, '(') != NULL)
0cf03b49 3220 {
72998fb3 3221 char *without_params = cp_remove_params (name);
0cf03b49 3222
72998fb3
DE
3223 if (without_params != NULL)
3224 {
3225 make_cleanup (xfree, without_params);
3226 name = without_params;
3227 }
0cf03b49
JK
3228 }
3229 }
3230
559a7a62 3231 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3232 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3233 simulate our NAME being searched is also lowercased. */
3234 hash = mapped_index_string_hash ((index->version == 4
3235 && case_sensitivity == case_sensitive_off
3236 ? 5 : index->version),
3237 name);
3238
3876f04e
DE
3239 slot = hash & (index->symbol_table_slots - 1);
3240 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3241 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3242
3243 for (;;)
3244 {
3245 /* Convert a slot number to an offset into the table. */
3246 offset_type i = 2 * slot;
3247 const char *str;
3876f04e 3248 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3249 {
3250 do_cleanups (back_to);
3251 return 0;
3252 }
9291a0cd 3253
3876f04e 3254 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3255 if (!cmp (name, str))
9291a0cd
TT
3256 {
3257 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3258 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3259 do_cleanups (back_to);
9291a0cd
TT
3260 return 1;
3261 }
3262
3876f04e 3263 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3264 }
3265}
3266
2ec9a5e0
TT
3267/* A helper function that reads the .gdb_index from SECTION and fills
3268 in MAP. FILENAME is the name of the file containing the section;
3269 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3270 ok to use deprecated sections.
3271
3272 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3273 out parameters that are filled in with information about the CU and
3274 TU lists in the section.
3275
3276 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3277
9291a0cd 3278static int
2ec9a5e0
TT
3279read_index_from_section (struct objfile *objfile,
3280 const char *filename,
3281 int deprecated_ok,
3282 struct dwarf2_section_info *section,
3283 struct mapped_index *map,
3284 const gdb_byte **cu_list,
3285 offset_type *cu_list_elements,
3286 const gdb_byte **types_list,
3287 offset_type *types_list_elements)
9291a0cd 3288{
948f8e3d 3289 const gdb_byte *addr;
2ec9a5e0 3290 offset_type version;
b3b272e1 3291 offset_type *metadata;
1fd400ff 3292 int i;
9291a0cd 3293
2ec9a5e0 3294 if (dwarf2_section_empty_p (section))
9291a0cd 3295 return 0;
82430852
JK
3296
3297 /* Older elfutils strip versions could keep the section in the main
3298 executable while splitting it for the separate debug info file. */
a32a8923 3299 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3300 return 0;
3301
2ec9a5e0 3302 dwarf2_read_section (objfile, section);
9291a0cd 3303
2ec9a5e0 3304 addr = section->buffer;
9291a0cd 3305 /* Version check. */
1fd400ff 3306 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3307 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3308 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3309 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3310 indices. */
831adc1f 3311 if (version < 4)
481860b3
GB
3312 {
3313 static int warning_printed = 0;
3314 if (!warning_printed)
3315 {
3316 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3317 filename);
481860b3
GB
3318 warning_printed = 1;
3319 }
3320 return 0;
3321 }
3322 /* Index version 4 uses a different hash function than index version
3323 5 and later.
3324
3325 Versions earlier than 6 did not emit psymbols for inlined
3326 functions. Using these files will cause GDB not to be able to
3327 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3328 indices unless the user has done
3329 "set use-deprecated-index-sections on". */
2ec9a5e0 3330 if (version < 6 && !deprecated_ok)
481860b3
GB
3331 {
3332 static int warning_printed = 0;
3333 if (!warning_printed)
3334 {
e615022a
DE
3335 warning (_("\
3336Skipping deprecated .gdb_index section in %s.\n\
3337Do \"set use-deprecated-index-sections on\" before the file is read\n\
3338to use the section anyway."),
2ec9a5e0 3339 filename);
481860b3
GB
3340 warning_printed = 1;
3341 }
3342 return 0;
3343 }
796a7ff8 3344 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3345 of the TU (for symbols coming from TUs),
3346 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3347 Plus gold-generated indices can have duplicate entries for global symbols,
3348 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3349 These are just performance bugs, and we can't distinguish gdb-generated
3350 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3351
481860b3 3352 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3353 longer backward compatible. */
796a7ff8 3354 if (version > 8)
594e8718 3355 return 0;
9291a0cd 3356
559a7a62 3357 map->version = version;
2ec9a5e0 3358 map->total_size = section->size;
9291a0cd
TT
3359
3360 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3361
3362 i = 0;
2ec9a5e0
TT
3363 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3364 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3365 / 8);
1fd400ff
TT
3366 ++i;
3367
2ec9a5e0
TT
3368 *types_list = addr + MAYBE_SWAP (metadata[i]);
3369 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3370 - MAYBE_SWAP (metadata[i]))
3371 / 8);
987d643c 3372 ++i;
1fd400ff
TT
3373
3374 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3375 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3376 - MAYBE_SWAP (metadata[i]));
3377 ++i;
3378
3876f04e
DE
3379 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3380 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3381 - MAYBE_SWAP (metadata[i]))
3382 / (2 * sizeof (offset_type)));
1fd400ff 3383 ++i;
9291a0cd 3384
f9d83a0b 3385 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3386
2ec9a5e0
TT
3387 return 1;
3388}
3389
3390
3391/* Read the index file. If everything went ok, initialize the "quick"
3392 elements of all the CUs and return 1. Otherwise, return 0. */
3393
3394static int
3395dwarf2_read_index (struct objfile *objfile)
3396{
3397 struct mapped_index local_map, *map;
3398 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3399 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3400 struct dwz_file *dwz;
2ec9a5e0 3401
4262abfb 3402 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3403 use_deprecated_index_sections,
3404 &dwarf2_per_objfile->gdb_index, &local_map,
3405 &cu_list, &cu_list_elements,
3406 &types_list, &types_list_elements))
3407 return 0;
3408
0fefef59 3409 /* Don't use the index if it's empty. */
2ec9a5e0 3410 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3411 return 0;
3412
2ec9a5e0
TT
3413 /* If there is a .dwz file, read it so we can get its CU list as
3414 well. */
4db1a1dc
TT
3415 dwz = dwarf2_get_dwz_file ();
3416 if (dwz != NULL)
2ec9a5e0 3417 {
2ec9a5e0
TT
3418 struct mapped_index dwz_map;
3419 const gdb_byte *dwz_types_ignore;
3420 offset_type dwz_types_elements_ignore;
3421
3422 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3423 1,
3424 &dwz->gdb_index, &dwz_map,
3425 &dwz_list, &dwz_list_elements,
3426 &dwz_types_ignore,
3427 &dwz_types_elements_ignore))
3428 {
3429 warning (_("could not read '.gdb_index' section from %s; skipping"),
3430 bfd_get_filename (dwz->dwz_bfd));
3431 return 0;
3432 }
3433 }
3434
74a0d9f6
JK
3435 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3436 dwz_list_elements);
1fd400ff 3437
8b70b953
TT
3438 if (types_list_elements)
3439 {
3440 struct dwarf2_section_info *section;
3441
3442 /* We can only handle a single .debug_types when we have an
3443 index. */
3444 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3445 return 0;
3446
3447 section = VEC_index (dwarf2_section_info_def,
3448 dwarf2_per_objfile->types, 0);
3449
74a0d9f6
JK
3450 create_signatured_type_table_from_index (objfile, section, types_list,
3451 types_list_elements);
8b70b953 3452 }
9291a0cd 3453
2ec9a5e0
TT
3454 create_addrmap_from_index (objfile, &local_map);
3455
8d749320 3456 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3457 *map = local_map;
9291a0cd
TT
3458
3459 dwarf2_per_objfile->index_table = map;
3460 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3461 dwarf2_per_objfile->quick_file_names_table =
3462 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3463
3464 return 1;
3465}
3466
3467/* A helper for the "quick" functions which sets the global
3468 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3469
9291a0cd
TT
3470static void
3471dw2_setup (struct objfile *objfile)
3472{
9a3c8263
SM
3473 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3474 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3475 gdb_assert (dwarf2_per_objfile);
3476}
3477
dee91e82 3478/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3479
dee91e82
DE
3480static void
3481dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3482 const gdb_byte *info_ptr,
dee91e82
DE
3483 struct die_info *comp_unit_die,
3484 int has_children,
3485 void *data)
9291a0cd 3486{
dee91e82
DE
3487 struct dwarf2_cu *cu = reader->cu;
3488 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3489 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3490 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3491 struct attribute *attr;
dee91e82 3492 int i;
7b9f3c50
DE
3493 void **slot;
3494 struct quick_file_names *qfn;
9291a0cd 3495
0186c6a7
DE
3496 gdb_assert (! this_cu->is_debug_types);
3497
07261596
TT
3498 /* Our callers never want to match partial units -- instead they
3499 will match the enclosing full CU. */
3500 if (comp_unit_die->tag == DW_TAG_partial_unit)
3501 {
3502 this_cu->v.quick->no_file_data = 1;
3503 return;
3504 }
3505
0186c6a7 3506 lh_cu = this_cu;
7b9f3c50 3507 slot = NULL;
dee91e82 3508
fff8551c 3509 line_header_up lh;
9c541725 3510 sect_offset line_offset {};
fff8551c 3511
dee91e82 3512 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3513 if (attr)
3514 {
7b9f3c50
DE
3515 struct quick_file_names find_entry;
3516
9c541725 3517 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3518
3519 /* We may have already read in this line header (TU line header sharing).
3520 If we have we're done. */
094b34ac 3521 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3522 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3523 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3524 &find_entry, INSERT);
3525 if (*slot != NULL)
3526 {
9a3c8263 3527 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3528 return;
7b9f3c50
DE
3529 }
3530
3019eac3 3531 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3532 }
3533 if (lh == NULL)
3534 {
094b34ac 3535 lh_cu->v.quick->no_file_data = 1;
dee91e82 3536 return;
9291a0cd
TT
3537 }
3538
8d749320 3539 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3540 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3541 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3542 gdb_assert (slot != NULL);
3543 *slot = qfn;
9291a0cd 3544
d721ba37 3545 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3546
fff8551c 3547 qfn->num_file_names = lh->file_names.size ();
8d749320 3548 qfn->file_names =
fff8551c
PA
3549 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3550 for (i = 0; i < lh->file_names.size (); ++i)
3551 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3552 qfn->real_names = NULL;
9291a0cd 3553
094b34ac 3554 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3555}
3556
3557/* A helper for the "quick" functions which attempts to read the line
3558 table for THIS_CU. */
3559
3560static struct quick_file_names *
e4a48d9d 3561dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3562{
0186c6a7
DE
3563 /* This should never be called for TUs. */
3564 gdb_assert (! this_cu->is_debug_types);
3565 /* Nor type unit groups. */
3566 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3567
dee91e82
DE
3568 if (this_cu->v.quick->file_names != NULL)
3569 return this_cu->v.quick->file_names;
3570 /* If we know there is no line data, no point in looking again. */
3571 if (this_cu->v.quick->no_file_data)
3572 return NULL;
3573
0186c6a7 3574 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3575
3576 if (this_cu->v.quick->no_file_data)
3577 return NULL;
3578 return this_cu->v.quick->file_names;
9291a0cd
TT
3579}
3580
3581/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3582 real path for a given file name from the line table. */
2fdf6df6 3583
9291a0cd 3584static const char *
7b9f3c50
DE
3585dw2_get_real_path (struct objfile *objfile,
3586 struct quick_file_names *qfn, int index)
9291a0cd 3587{
7b9f3c50
DE
3588 if (qfn->real_names == NULL)
3589 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3590 qfn->num_file_names, const char *);
9291a0cd 3591
7b9f3c50 3592 if (qfn->real_names[index] == NULL)
14278e1f 3593 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3594
7b9f3c50 3595 return qfn->real_names[index];
9291a0cd
TT
3596}
3597
3598static struct symtab *
3599dw2_find_last_source_symtab (struct objfile *objfile)
3600{
43f3e411 3601 struct compunit_symtab *cust;
9291a0cd 3602 int index;
ae2de4f8 3603
9291a0cd
TT
3604 dw2_setup (objfile);
3605 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3606 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3607 if (cust == NULL)
3608 return NULL;
3609 return compunit_primary_filetab (cust);
9291a0cd
TT
3610}
3611
7b9f3c50
DE
3612/* Traversal function for dw2_forget_cached_source_info. */
3613
3614static int
3615dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3616{
7b9f3c50 3617 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3618
7b9f3c50 3619 if (file_data->real_names)
9291a0cd 3620 {
7b9f3c50 3621 int i;
9291a0cd 3622
7b9f3c50 3623 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3624 {
7b9f3c50
DE
3625 xfree ((void*) file_data->real_names[i]);
3626 file_data->real_names[i] = NULL;
9291a0cd
TT
3627 }
3628 }
7b9f3c50
DE
3629
3630 return 1;
3631}
3632
3633static void
3634dw2_forget_cached_source_info (struct objfile *objfile)
3635{
3636 dw2_setup (objfile);
3637
3638 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3639 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3640}
3641
f8eba3c6
TT
3642/* Helper function for dw2_map_symtabs_matching_filename that expands
3643 the symtabs and calls the iterator. */
3644
3645static int
3646dw2_map_expand_apply (struct objfile *objfile,
3647 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3648 const char *name, const char *real_path,
14bc53a8 3649 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3650{
43f3e411 3651 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3652
3653 /* Don't visit already-expanded CUs. */
43f3e411 3654 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3655 return 0;
3656
3657 /* This may expand more than one symtab, and we want to iterate over
3658 all of them. */
a0f42c21 3659 dw2_instantiate_symtab (per_cu);
f8eba3c6 3660
14bc53a8
PA
3661 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3662 last_made, callback);
f8eba3c6
TT
3663}
3664
3665/* Implementation of the map_symtabs_matching_filename method. */
3666
14bc53a8
PA
3667static bool
3668dw2_map_symtabs_matching_filename
3669 (struct objfile *objfile, const char *name, const char *real_path,
3670 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3671{
3672 int i;
c011a4f4 3673 const char *name_basename = lbasename (name);
9291a0cd
TT
3674
3675 dw2_setup (objfile);
ae2de4f8 3676
848e3e78
DE
3677 /* The rule is CUs specify all the files, including those used by
3678 any TU, so there's no need to scan TUs here. */
f4dc4d17 3679
848e3e78 3680 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3681 {
3682 int j;
8832e7e3 3683 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3684 struct quick_file_names *file_data;
9291a0cd 3685
3d7bb9d9 3686 /* We only need to look at symtabs not already expanded. */
43f3e411 3687 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3688 continue;
3689
e4a48d9d 3690 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3691 if (file_data == NULL)
9291a0cd
TT
3692 continue;
3693
7b9f3c50 3694 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3695 {
7b9f3c50 3696 const char *this_name = file_data->file_names[j];
da235a7c 3697 const char *this_real_name;
9291a0cd 3698
af529f8f 3699 if (compare_filenames_for_search (this_name, name))
9291a0cd 3700 {
f5b95b50 3701 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3702 callback))
3703 return true;
288e77a7 3704 continue;
4aac40c8 3705 }
9291a0cd 3706
c011a4f4
DE
3707 /* Before we invoke realpath, which can get expensive when many
3708 files are involved, do a quick comparison of the basenames. */
3709 if (! basenames_may_differ
3710 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3711 continue;
3712
da235a7c
JK
3713 this_real_name = dw2_get_real_path (objfile, file_data, j);
3714 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3715 {
da235a7c 3716 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3717 callback))
3718 return true;
288e77a7 3719 continue;
da235a7c 3720 }
9291a0cd 3721
da235a7c
JK
3722 if (real_path != NULL)
3723 {
af529f8f
JK
3724 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3725 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3726 if (this_real_name != NULL
af529f8f 3727 && FILENAME_CMP (real_path, this_real_name) == 0)
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;
9291a0cd
TT
3733 }
3734 }
3735 }
3736 }
3737
14bc53a8 3738 return false;
9291a0cd
TT
3739}
3740
da51c347
DE
3741/* Struct used to manage iterating over all CUs looking for a symbol. */
3742
3743struct dw2_symtab_iterator
9291a0cd 3744{
da51c347
DE
3745 /* The internalized form of .gdb_index. */
3746 struct mapped_index *index;
3747 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3748 int want_specific_block;
3749 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3750 Unused if !WANT_SPECIFIC_BLOCK. */
3751 int block_index;
3752 /* The kind of symbol we're looking for. */
3753 domain_enum domain;
3754 /* The list of CUs from the index entry of the symbol,
3755 or NULL if not found. */
3756 offset_type *vec;
3757 /* The next element in VEC to look at. */
3758 int next;
3759 /* The number of elements in VEC, or zero if there is no match. */
3760 int length;
8943b874
DE
3761 /* Have we seen a global version of the symbol?
3762 If so we can ignore all further global instances.
3763 This is to work around gold/15646, inefficient gold-generated
3764 indices. */
3765 int global_seen;
da51c347 3766};
9291a0cd 3767
da51c347
DE
3768/* Initialize the index symtab iterator ITER.
3769 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3770 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3771
9291a0cd 3772static void
da51c347
DE
3773dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3774 struct mapped_index *index,
3775 int want_specific_block,
3776 int block_index,
3777 domain_enum domain,
3778 const char *name)
3779{
3780 iter->index = index;
3781 iter->want_specific_block = want_specific_block;
3782 iter->block_index = block_index;
3783 iter->domain = domain;
3784 iter->next = 0;
8943b874 3785 iter->global_seen = 0;
da51c347
DE
3786
3787 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3788 iter->length = MAYBE_SWAP (*iter->vec);
3789 else
3790 {
3791 iter->vec = NULL;
3792 iter->length = 0;
3793 }
3794}
3795
3796/* Return the next matching CU or NULL if there are no more. */
3797
3798static struct dwarf2_per_cu_data *
3799dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3800{
3801 for ( ; iter->next < iter->length; ++iter->next)
3802 {
3803 offset_type cu_index_and_attrs =
3804 MAYBE_SWAP (iter->vec[iter->next + 1]);
3805 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3806 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3807 int want_static = iter->block_index != GLOBAL_BLOCK;
3808 /* This value is only valid for index versions >= 7. */
3809 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3810 gdb_index_symbol_kind symbol_kind =
3811 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3812 /* Only check the symbol attributes if they're present.
3813 Indices prior to version 7 don't record them,
3814 and indices >= 7 may elide them for certain symbols
3815 (gold does this). */
3816 int attrs_valid =
3817 (iter->index->version >= 7
3818 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3819
3190f0c6
DE
3820 /* Don't crash on bad data. */
3821 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3822 + dwarf2_per_objfile->n_type_units))
3823 {
3824 complaint (&symfile_complaints,
3825 _(".gdb_index entry has bad CU index"
4262abfb
JK
3826 " [in module %s]"),
3827 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3828 continue;
3829 }
3830
8832e7e3 3831 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3832
da51c347 3833 /* Skip if already read in. */
43f3e411 3834 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3835 continue;
3836
8943b874
DE
3837 /* Check static vs global. */
3838 if (attrs_valid)
3839 {
3840 if (iter->want_specific_block
3841 && want_static != is_static)
3842 continue;
3843 /* Work around gold/15646. */
3844 if (!is_static && iter->global_seen)
3845 continue;
3846 if (!is_static)
3847 iter->global_seen = 1;
3848 }
da51c347
DE
3849
3850 /* Only check the symbol's kind if it has one. */
3851 if (attrs_valid)
3852 {
3853 switch (iter->domain)
3854 {
3855 case VAR_DOMAIN:
3856 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3857 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3858 /* Some types are also in VAR_DOMAIN. */
3859 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3860 continue;
3861 break;
3862 case STRUCT_DOMAIN:
3863 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3864 continue;
3865 break;
3866 case LABEL_DOMAIN:
3867 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3868 continue;
3869 break;
3870 default:
3871 break;
3872 }
3873 }
3874
3875 ++iter->next;
3876 return per_cu;
3877 }
3878
3879 return NULL;
3880}
3881
43f3e411 3882static struct compunit_symtab *
da51c347
DE
3883dw2_lookup_symbol (struct objfile *objfile, int block_index,
3884 const char *name, domain_enum domain)
9291a0cd 3885{
43f3e411 3886 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3887 struct mapped_index *index;
3888
9291a0cd
TT
3889 dw2_setup (objfile);
3890
156942c7
DE
3891 index = dwarf2_per_objfile->index_table;
3892
da51c347 3893 /* index is NULL if OBJF_READNOW. */
156942c7 3894 if (index)
9291a0cd 3895 {
da51c347
DE
3896 struct dw2_symtab_iterator iter;
3897 struct dwarf2_per_cu_data *per_cu;
3898
3899 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3900
da51c347 3901 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3902 {
b2e2f908 3903 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3904 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3905 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3906 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3907
b2e2f908
DE
3908 sym = block_find_symbol (block, name, domain,
3909 block_find_non_opaque_type_preferred,
3910 &with_opaque);
3911
da51c347
DE
3912 /* Some caution must be observed with overloaded functions
3913 and methods, since the index will not contain any overload
3914 information (but NAME might contain it). */
da51c347 3915
b2e2f908 3916 if (sym != NULL
a778f165 3917 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
3918 return stab;
3919 if (with_opaque != NULL
a778f165 3920 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 3921 stab_best = stab;
da51c347
DE
3922
3923 /* Keep looking through other CUs. */
9291a0cd
TT
3924 }
3925 }
9291a0cd 3926
da51c347 3927 return stab_best;
9291a0cd
TT
3928}
3929
3930static void
3931dw2_print_stats (struct objfile *objfile)
3932{
e4a48d9d 3933 int i, total, count;
9291a0cd
TT
3934
3935 dw2_setup (objfile);
e4a48d9d 3936 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3937 count = 0;
e4a48d9d 3938 for (i = 0; i < total; ++i)
9291a0cd 3939 {
8832e7e3 3940 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3941
43f3e411 3942 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3943 ++count;
3944 }
e4a48d9d 3945 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3946 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3947}
3948
779bd270
DE
3949/* This dumps minimal information about the index.
3950 It is called via "mt print objfiles".
3951 One use is to verify .gdb_index has been loaded by the
3952 gdb.dwarf2/gdb-index.exp testcase. */
3953
9291a0cd
TT
3954static void
3955dw2_dump (struct objfile *objfile)
3956{
779bd270
DE
3957 dw2_setup (objfile);
3958 gdb_assert (dwarf2_per_objfile->using_index);
3959 printf_filtered (".gdb_index:");
3960 if (dwarf2_per_objfile->index_table != NULL)
3961 {
3962 printf_filtered (" version %d\n",
3963 dwarf2_per_objfile->index_table->version);
3964 }
3965 else
3966 printf_filtered (" faked for \"readnow\"\n");
3967 printf_filtered ("\n");
9291a0cd
TT
3968}
3969
3970static void
3189cb12
DE
3971dw2_relocate (struct objfile *objfile,
3972 const struct section_offsets *new_offsets,
3973 const struct section_offsets *delta)
9291a0cd
TT
3974{
3975 /* There's nothing to relocate here. */
3976}
3977
3978static void
3979dw2_expand_symtabs_for_function (struct objfile *objfile,
3980 const char *func_name)
3981{
da51c347
DE
3982 struct mapped_index *index;
3983
3984 dw2_setup (objfile);
3985
3986 index = dwarf2_per_objfile->index_table;
3987
3988 /* index is NULL if OBJF_READNOW. */
3989 if (index)
3990 {
3991 struct dw2_symtab_iterator iter;
3992 struct dwarf2_per_cu_data *per_cu;
3993
3994 /* Note: It doesn't matter what we pass for block_index here. */
3995 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3996 func_name);
3997
3998 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3999 dw2_instantiate_symtab (per_cu);
4000 }
9291a0cd
TT
4001}
4002
4003static void
4004dw2_expand_all_symtabs (struct objfile *objfile)
4005{
4006 int i;
4007
4008 dw2_setup (objfile);
1fd400ff
TT
4009
4010 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4011 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4012 {
8832e7e3 4013 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4014
a0f42c21 4015 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4016 }
4017}
4018
4019static void
652a8996
JK
4020dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4021 const char *fullname)
9291a0cd
TT
4022{
4023 int i;
4024
4025 dw2_setup (objfile);
d4637a04
DE
4026
4027 /* We don't need to consider type units here.
4028 This is only called for examining code, e.g. expand_line_sal.
4029 There can be an order of magnitude (or more) more type units
4030 than comp units, and we avoid them if we can. */
4031
4032 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4033 {
4034 int j;
8832e7e3 4035 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4036 struct quick_file_names *file_data;
9291a0cd 4037
3d7bb9d9 4038 /* We only need to look at symtabs not already expanded. */
43f3e411 4039 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4040 continue;
4041
e4a48d9d 4042 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4043 if (file_data == NULL)
9291a0cd
TT
4044 continue;
4045
7b9f3c50 4046 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4047 {
652a8996
JK
4048 const char *this_fullname = file_data->file_names[j];
4049
4050 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4051 {
a0f42c21 4052 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4053 break;
4054 }
4055 }
4056 }
4057}
4058
9291a0cd 4059static void
ade7ed9e 4060dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4061 const char * name, domain_enum domain,
ade7ed9e 4062 int global,
40658b94
PH
4063 int (*callback) (struct block *,
4064 struct symbol *, void *),
2edb89d3
JK
4065 void *data, symbol_compare_ftype *match,
4066 symbol_compare_ftype *ordered_compare)
9291a0cd 4067{
40658b94 4068 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4069 current language is Ada for a non-Ada objfile using GNU index. As Ada
4070 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4071}
4072
4073static void
f8eba3c6
TT
4074dw2_expand_symtabs_matching
4075 (struct objfile *objfile,
14bc53a8
PA
4076 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4077 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4078 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4079 enum search_domain kind)
9291a0cd
TT
4080{
4081 int i;
4082 offset_type iter;
4b5246aa 4083 struct mapped_index *index;
9291a0cd
TT
4084
4085 dw2_setup (objfile);
ae2de4f8
DE
4086
4087 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4088 if (!dwarf2_per_objfile->index_table)
4089 return;
4b5246aa 4090 index = dwarf2_per_objfile->index_table;
9291a0cd 4091
7b08b9eb 4092 if (file_matcher != NULL)
24c79950 4093 {
fc4007c9
TT
4094 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4095 htab_eq_pointer,
4096 NULL, xcalloc, xfree));
4097 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4098 htab_eq_pointer,
4099 NULL, xcalloc, xfree));
24c79950 4100
848e3e78
DE
4101 /* The rule is CUs specify all the files, including those used by
4102 any TU, so there's no need to scan TUs here. */
4103
4104 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4105 {
4106 int j;
8832e7e3 4107 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4108 struct quick_file_names *file_data;
4109 void **slot;
7b08b9eb 4110
61d96d7e
DE
4111 QUIT;
4112
24c79950 4113 per_cu->v.quick->mark = 0;
3d7bb9d9 4114
24c79950 4115 /* We only need to look at symtabs not already expanded. */
43f3e411 4116 if (per_cu->v.quick->compunit_symtab)
24c79950 4117 continue;
7b08b9eb 4118
e4a48d9d 4119 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4120 if (file_data == NULL)
4121 continue;
7b08b9eb 4122
fc4007c9 4123 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4124 continue;
fc4007c9 4125 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4126 {
4127 per_cu->v.quick->mark = 1;
4128 continue;
4129 }
4130
4131 for (j = 0; j < file_data->num_file_names; ++j)
4132 {
da235a7c
JK
4133 const char *this_real_name;
4134
14bc53a8 4135 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4136 {
4137 per_cu->v.quick->mark = 1;
4138 break;
4139 }
da235a7c
JK
4140
4141 /* Before we invoke realpath, which can get expensive when many
4142 files are involved, do a quick comparison of the basenames. */
4143 if (!basenames_may_differ
4144 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4145 true))
da235a7c
JK
4146 continue;
4147
4148 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4149 if (file_matcher (this_real_name, false))
da235a7c
JK
4150 {
4151 per_cu->v.quick->mark = 1;
4152 break;
4153 }
24c79950
TT
4154 }
4155
4156 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4157 ? visited_found.get ()
4158 : visited_not_found.get (),
24c79950
TT
4159 file_data, INSERT);
4160 *slot = file_data;
4161 }
24c79950 4162 }
9291a0cd 4163
3876f04e 4164 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4165 {
4166 offset_type idx = 2 * iter;
4167 const char *name;
4168 offset_type *vec, vec_len, vec_idx;
8943b874 4169 int global_seen = 0;
9291a0cd 4170
61d96d7e
DE
4171 QUIT;
4172
3876f04e 4173 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4174 continue;
4175
3876f04e 4176 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4177
14bc53a8 4178 if (!symbol_matcher (name))
9291a0cd
TT
4179 continue;
4180
4181 /* The name was matched, now expand corresponding CUs that were
4182 marked. */
4b5246aa 4183 vec = (offset_type *) (index->constant_pool
3876f04e 4184 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4185 vec_len = MAYBE_SWAP (vec[0]);
4186 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4187 {
e254ef6a 4188 struct dwarf2_per_cu_data *per_cu;
156942c7 4189 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4190 /* This value is only valid for index versions >= 7. */
4191 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4192 gdb_index_symbol_kind symbol_kind =
4193 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4194 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4195 /* Only check the symbol attributes if they're present.
4196 Indices prior to version 7 don't record them,
4197 and indices >= 7 may elide them for certain symbols
4198 (gold does this). */
4199 int attrs_valid =
4200 (index->version >= 7
4201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4202
8943b874
DE
4203 /* Work around gold/15646. */
4204 if (attrs_valid)
4205 {
4206 if (!is_static && global_seen)
4207 continue;
4208 if (!is_static)
4209 global_seen = 1;
4210 }
4211
3190f0c6
DE
4212 /* Only check the symbol's kind if it has one. */
4213 if (attrs_valid)
156942c7
DE
4214 {
4215 switch (kind)
4216 {
4217 case VARIABLES_DOMAIN:
4218 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4219 continue;
4220 break;
4221 case FUNCTIONS_DOMAIN:
4222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4223 continue;
4224 break;
4225 case TYPES_DOMAIN:
4226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4227 continue;
4228 break;
4229 default:
4230 break;
4231 }
4232 }
4233
3190f0c6
DE
4234 /* Don't crash on bad data. */
4235 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4236 + dwarf2_per_objfile->n_type_units))
4237 {
4238 complaint (&symfile_complaints,
4239 _(".gdb_index entry has bad CU index"
4262abfb 4240 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4241 continue;
4242 }
4243
8832e7e3 4244 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4245 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4246 {
4247 int symtab_was_null =
4248 (per_cu->v.quick->compunit_symtab == NULL);
4249
4250 dw2_instantiate_symtab (per_cu);
4251
4252 if (expansion_notify != NULL
4253 && symtab_was_null
4254 && per_cu->v.quick->compunit_symtab != NULL)
4255 {
14bc53a8 4256 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4257 }
4258 }
9291a0cd
TT
4259 }
4260 }
4261}
4262
43f3e411 4263/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4264 symtab. */
4265
43f3e411
DE
4266static struct compunit_symtab *
4267recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4268 CORE_ADDR pc)
9703b513
TT
4269{
4270 int i;
4271
43f3e411
DE
4272 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4273 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4274 return cust;
9703b513 4275
43f3e411 4276 if (cust->includes == NULL)
a3ec0bb1
DE
4277 return NULL;
4278
43f3e411 4279 for (i = 0; cust->includes[i]; ++i)
9703b513 4280 {
43f3e411 4281 struct compunit_symtab *s = cust->includes[i];
9703b513 4282
43f3e411 4283 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4284 if (s != NULL)
4285 return s;
4286 }
4287
4288 return NULL;
4289}
4290
43f3e411
DE
4291static struct compunit_symtab *
4292dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4293 struct bound_minimal_symbol msymbol,
4294 CORE_ADDR pc,
4295 struct obj_section *section,
4296 int warn_if_readin)
9291a0cd
TT
4297{
4298 struct dwarf2_per_cu_data *data;
43f3e411 4299 struct compunit_symtab *result;
9291a0cd
TT
4300
4301 dw2_setup (objfile);
4302
4303 if (!objfile->psymtabs_addrmap)
4304 return NULL;
4305
9a3c8263
SM
4306 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4307 pc);
9291a0cd
TT
4308 if (!data)
4309 return NULL;
4310
43f3e411 4311 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4312 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4313 paddress (get_objfile_arch (objfile), pc));
4314
43f3e411
DE
4315 result
4316 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4317 pc);
9703b513
TT
4318 gdb_assert (result != NULL);
4319 return result;
9291a0cd
TT
4320}
4321
9291a0cd 4322static void
44b13c5a 4323dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4324 void *data, int need_fullname)
9291a0cd 4325{
9291a0cd 4326 dw2_setup (objfile);
ae2de4f8 4327
bbf2f4df 4328 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4329 {
bbf2f4df 4330 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4331
bbf2f4df
PA
4332 htab_up visited (htab_create_alloc (10,
4333 htab_hash_pointer, htab_eq_pointer,
4334 NULL, xcalloc, xfree));
24c79950 4335
bbf2f4df
PA
4336 /* The rule is CUs specify all the files, including those used
4337 by any TU, so there's no need to scan TUs here. We can
4338 ignore file names coming from already-expanded CUs. */
24c79950 4339
bbf2f4df
PA
4340 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4341 {
4342 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4343
bbf2f4df
PA
4344 if (per_cu->v.quick->compunit_symtab)
4345 {
4346 void **slot = htab_find_slot (visited.get (),
4347 per_cu->v.quick->file_names,
4348 INSERT);
9291a0cd 4349
bbf2f4df
PA
4350 *slot = per_cu->v.quick->file_names;
4351 }
24c79950 4352 }
24c79950 4353
bbf2f4df 4354 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4355 {
bbf2f4df
PA
4356 int j;
4357 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4358 struct quick_file_names *file_data;
4359 void **slot;
4360
4361 /* We only need to look at symtabs not already expanded. */
4362 if (per_cu->v.quick->compunit_symtab)
4363 continue;
74e2f255 4364
bbf2f4df
PA
4365 file_data = dw2_get_file_names (per_cu);
4366 if (file_data == NULL)
4367 continue;
4368
4369 slot = htab_find_slot (visited.get (), file_data, INSERT);
4370 if (*slot)
4371 {
4372 /* Already visited. */
4373 continue;
4374 }
4375 *slot = file_data;
4376
4377 for (int j = 0; j < file_data->num_file_names; ++j)
4378 {
4379 const char *filename = file_data->file_names[j];
4380 dwarf2_per_objfile->filenames_cache->seen (filename);
4381 }
9291a0cd
TT
4382 }
4383 }
bbf2f4df
PA
4384
4385 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4386 {
14278e1f 4387 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4388
4389 if (need_fullname)
4390 this_real_name = gdb_realpath (filename);
14278e1f 4391 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4392 });
9291a0cd
TT
4393}
4394
4395static int
4396dw2_has_symbols (struct objfile *objfile)
4397{
4398 return 1;
4399}
4400
4401const struct quick_symbol_functions dwarf2_gdb_index_functions =
4402{
4403 dw2_has_symbols,
4404 dw2_find_last_source_symtab,
4405 dw2_forget_cached_source_info,
f8eba3c6 4406 dw2_map_symtabs_matching_filename,
9291a0cd 4407 dw2_lookup_symbol,
9291a0cd
TT
4408 dw2_print_stats,
4409 dw2_dump,
4410 dw2_relocate,
4411 dw2_expand_symtabs_for_function,
4412 dw2_expand_all_symtabs,
652a8996 4413 dw2_expand_symtabs_with_fullname,
40658b94 4414 dw2_map_matching_symbols,
9291a0cd 4415 dw2_expand_symtabs_matching,
43f3e411 4416 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4417 dw2_map_symbol_filenames
4418};
4419
4420/* Initialize for reading DWARF for this objfile. Return 0 if this
4421 file will use psymtabs, or 1 if using the GNU index. */
4422
4423int
4424dwarf2_initialize_objfile (struct objfile *objfile)
4425{
4426 /* If we're about to read full symbols, don't bother with the
4427 indices. In this case we also don't care if some other debug
4428 format is making psymtabs, because they are all about to be
4429 expanded anyway. */
4430 if ((objfile->flags & OBJF_READNOW))
4431 {
4432 int i;
4433
4434 dwarf2_per_objfile->using_index = 1;
4435 create_all_comp_units (objfile);
0e50663e 4436 create_all_type_units (objfile);
7b9f3c50
DE
4437 dwarf2_per_objfile->quick_file_names_table =
4438 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4439
1fd400ff 4440 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4441 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4442 {
8832e7e3 4443 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4444
e254ef6a
DE
4445 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4446 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4447 }
4448
4449 /* Return 1 so that gdb sees the "quick" functions. However,
4450 these functions will be no-ops because we will have expanded
4451 all symtabs. */
4452 return 1;
4453 }
4454
4455 if (dwarf2_read_index (objfile))
4456 return 1;
4457
9291a0cd
TT
4458 return 0;
4459}
4460
4461\f
4462
dce234bc
PP
4463/* Build a partial symbol table. */
4464
4465void
f29dff0a 4466dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4467{
c9bf0622 4468
f29dff0a 4469 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4470 {
4471 init_psymbol_list (objfile, 1024);
4472 }
4473
492d29ea 4474 TRY
c9bf0622
TT
4475 {
4476 /* This isn't really ideal: all the data we allocate on the
4477 objfile's obstack is still uselessly kept around. However,
4478 freeing it seems unsafe. */
906768f9 4479 psymtab_discarder psymtabs (objfile);
c9bf0622 4480 dwarf2_build_psymtabs_hard (objfile);
906768f9 4481 psymtabs.keep ();
c9bf0622 4482 }
492d29ea
PA
4483 CATCH (except, RETURN_MASK_ERROR)
4484 {
4485 exception_print (gdb_stderr, except);
4486 }
4487 END_CATCH
c906108c 4488}
c906108c 4489
1ce1cefd
DE
4490/* Return the total length of the CU described by HEADER. */
4491
4492static unsigned int
4493get_cu_length (const struct comp_unit_head *header)
4494{
4495 return header->initial_length_size + header->length;
4496}
4497
9c541725 4498/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4499
9c541725
PA
4500static inline bool
4501offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4502{
9c541725
PA
4503 sect_offset bottom = cu_header->sect_off;
4504 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4505
9c541725 4506 return sect_off >= bottom && sect_off < top;
45452591
DE
4507}
4508
3b80fe9b
DE
4509/* Find the base address of the compilation unit for range lists and
4510 location lists. It will normally be specified by DW_AT_low_pc.
4511 In DWARF-3 draft 4, the base address could be overridden by
4512 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4513 compilation units with discontinuous ranges. */
4514
4515static void
4516dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4517{
4518 struct attribute *attr;
4519
4520 cu->base_known = 0;
4521 cu->base_address = 0;
4522
4523 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4524 if (attr)
4525 {
31aa7e4e 4526 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4527 cu->base_known = 1;
4528 }
4529 else
4530 {
4531 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4532 if (attr)
4533 {
31aa7e4e 4534 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4535 cu->base_known = 1;
4536 }
4537 }
4538}
4539
93311388 4540/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4541 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4542 NOTE: This leaves members offset, first_die_offset to be filled in
4543 by the caller. */
107d2387 4544
d521ce57 4545static const gdb_byte *
107d2387 4546read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4547 const gdb_byte *info_ptr,
4548 struct dwarf2_section_info *section,
4549 rcuh_kind section_kind)
107d2387
AC
4550{
4551 int signed_addr;
891d2f0b 4552 unsigned int bytes_read;
43988095
JK
4553 const char *filename = get_section_file_name (section);
4554 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4555
4556 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4557 cu_header->initial_length_size = bytes_read;
4558 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4559 info_ptr += bytes_read;
107d2387
AC
4560 cu_header->version = read_2_bytes (abfd, info_ptr);
4561 info_ptr += 2;
43988095
JK
4562 if (cu_header->version < 5)
4563 switch (section_kind)
4564 {
4565 case rcuh_kind::COMPILE:
4566 cu_header->unit_type = DW_UT_compile;
4567 break;
4568 case rcuh_kind::TYPE:
4569 cu_header->unit_type = DW_UT_type;
4570 break;
4571 default:
4572 internal_error (__FILE__, __LINE__,
4573 _("read_comp_unit_head: invalid section_kind"));
4574 }
4575 else
4576 {
4577 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4578 (read_1_byte (abfd, info_ptr));
4579 info_ptr += 1;
4580 switch (cu_header->unit_type)
4581 {
4582 case DW_UT_compile:
4583 if (section_kind != rcuh_kind::COMPILE)
4584 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4585 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4586 filename);
4587 break;
4588 case DW_UT_type:
4589 section_kind = rcuh_kind::TYPE;
4590 break;
4591 default:
4592 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4593 "(is %d, should be %d or %d) [in module %s]"),
4594 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4595 }
4596
4597 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4598 info_ptr += 1;
4599 }
9c541725
PA
4600 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4601 cu_header,
4602 &bytes_read);
613e1657 4603 info_ptr += bytes_read;
43988095
JK
4604 if (cu_header->version < 5)
4605 {
4606 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4607 info_ptr += 1;
4608 }
107d2387
AC
4609 signed_addr = bfd_get_sign_extend_vma (abfd);
4610 if (signed_addr < 0)
8e65ff28 4611 internal_error (__FILE__, __LINE__,
e2e0b3e5 4612 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4613 cu_header->signed_addr_p = signed_addr;
c764a876 4614
43988095
JK
4615 if (section_kind == rcuh_kind::TYPE)
4616 {
4617 LONGEST type_offset;
4618
4619 cu_header->signature = read_8_bytes (abfd, info_ptr);
4620 info_ptr += 8;
4621
4622 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4623 info_ptr += bytes_read;
9c541725
PA
4624 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4625 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4626 error (_("Dwarf Error: Too big type_offset in compilation unit "
4627 "header (is %s) [in module %s]"), plongest (type_offset),
4628 filename);
4629 }
4630
107d2387
AC
4631 return info_ptr;
4632}
4633
36586728
TT
4634/* Helper function that returns the proper abbrev section for
4635 THIS_CU. */
4636
4637static struct dwarf2_section_info *
4638get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4639{
4640 struct dwarf2_section_info *abbrev;
4641
4642 if (this_cu->is_dwz)
4643 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4644 else
4645 abbrev = &dwarf2_per_objfile->abbrev;
4646
4647 return abbrev;
4648}
4649
9ff913ba
DE
4650/* Subroutine of read_and_check_comp_unit_head and
4651 read_and_check_type_unit_head to simplify them.
4652 Perform various error checking on the header. */
4653
4654static void
4655error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4656 struct dwarf2_section_info *section,
4657 struct dwarf2_section_info *abbrev_section)
9ff913ba 4658{
a32a8923 4659 const char *filename = get_section_file_name (section);
9ff913ba 4660
43988095 4661 if (header->version < 2 || header->version > 5)
9ff913ba 4662 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4663 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4664 filename);
4665
9c541725 4666 if (to_underlying (header->abbrev_sect_off)
36586728 4667 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4668 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4669 "(offset 0x%x + 6) [in module %s]"),
4670 to_underlying (header->abbrev_sect_off),
4671 to_underlying (header->sect_off),
9ff913ba
DE
4672 filename);
4673
9c541725 4674 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4675 avoid potential 32-bit overflow. */
9c541725 4676 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4677 > section->size)
9c541725
PA
4678 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4679 "(offset 0x%x + 0) [in module %s]"),
4680 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4681 filename);
4682}
4683
4684/* Read in a CU/TU header and perform some basic error checking.
4685 The contents of the header are stored in HEADER.
4686 The result is a pointer to the start of the first DIE. */
adabb602 4687
d521ce57 4688static const gdb_byte *
9ff913ba
DE
4689read_and_check_comp_unit_head (struct comp_unit_head *header,
4690 struct dwarf2_section_info *section,
4bdcc0c1 4691 struct dwarf2_section_info *abbrev_section,
d521ce57 4692 const gdb_byte *info_ptr,
43988095 4693 rcuh_kind section_kind)
72bf9492 4694{
d521ce57 4695 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4696 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4697
9c541725 4698 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4699
43988095 4700 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4701
9c541725 4702 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4703
4bdcc0c1 4704 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4705
4706 return info_ptr;
348e048f
DE
4707}
4708
f4dc4d17
DE
4709/* Fetch the abbreviation table offset from a comp or type unit header. */
4710
4711static sect_offset
4712read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4713 sect_offset sect_off)
f4dc4d17 4714{
a32a8923 4715 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4716 const gdb_byte *info_ptr;
ac298888 4717 unsigned int initial_length_size, offset_size;
43988095 4718 uint16_t version;
f4dc4d17
DE
4719
4720 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4721 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4722 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4723 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4724 info_ptr += initial_length_size;
4725
4726 version = read_2_bytes (abfd, info_ptr);
4727 info_ptr += 2;
4728 if (version >= 5)
4729 {
4730 /* Skip unit type and address size. */
4731 info_ptr += 2;
4732 }
4733
9c541725 4734 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4735}
4736
aaa75496
JB
4737/* Allocate a new partial symtab for file named NAME and mark this new
4738 partial symtab as being an include of PST. */
4739
4740static void
d521ce57 4741dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4742 struct objfile *objfile)
4743{
4744 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4745
fbd9ab74
JK
4746 if (!IS_ABSOLUTE_PATH (subpst->filename))
4747 {
4748 /* It shares objfile->objfile_obstack. */
4749 subpst->dirname = pst->dirname;
4750 }
4751
aaa75496
JB
4752 subpst->textlow = 0;
4753 subpst->texthigh = 0;
4754
8d749320
SM
4755 subpst->dependencies
4756 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4757 subpst->dependencies[0] = pst;
4758 subpst->number_of_dependencies = 1;
4759
4760 subpst->globals_offset = 0;
4761 subpst->n_global_syms = 0;
4762 subpst->statics_offset = 0;
4763 subpst->n_static_syms = 0;
43f3e411 4764 subpst->compunit_symtab = NULL;
aaa75496
JB
4765 subpst->read_symtab = pst->read_symtab;
4766 subpst->readin = 0;
4767
4768 /* No private part is necessary for include psymtabs. This property
4769 can be used to differentiate between such include psymtabs and
10b3939b 4770 the regular ones. */
58a9656e 4771 subpst->read_symtab_private = NULL;
aaa75496
JB
4772}
4773
4774/* Read the Line Number Program data and extract the list of files
4775 included by the source file represented by PST. Build an include
d85a05f0 4776 partial symtab for each of these included files. */
aaa75496
JB
4777
4778static void
4779dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4780 struct die_info *die,
4781 struct partial_symtab *pst)
aaa75496 4782{
fff8551c 4783 line_header_up lh;
d85a05f0 4784 struct attribute *attr;
aaa75496 4785
d85a05f0
DJ
4786 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4787 if (attr)
9c541725 4788 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4789 if (lh == NULL)
4790 return; /* No linetable, so no includes. */
4791
c6da4cef 4792 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4793 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4794}
4795
348e048f 4796static hashval_t
52dc124a 4797hash_signatured_type (const void *item)
348e048f 4798{
9a3c8263
SM
4799 const struct signatured_type *sig_type
4800 = (const struct signatured_type *) item;
9a619af0 4801
348e048f 4802 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4803 return sig_type->signature;
348e048f
DE
4804}
4805
4806static int
52dc124a 4807eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4808{
9a3c8263
SM
4809 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4810 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4811
348e048f
DE
4812 return lhs->signature == rhs->signature;
4813}
4814
1fd400ff
TT
4815/* Allocate a hash table for signatured types. */
4816
4817static htab_t
673bfd45 4818allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4819{
4820 return htab_create_alloc_ex (41,
52dc124a
DE
4821 hash_signatured_type,
4822 eq_signatured_type,
1fd400ff
TT
4823 NULL,
4824 &objfile->objfile_obstack,
4825 hashtab_obstack_allocate,
4826 dummy_obstack_deallocate);
4827}
4828
d467dd73 4829/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4830
4831static int
d467dd73 4832add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4833{
9a3c8263
SM
4834 struct signatured_type *sigt = (struct signatured_type *) *slot;
4835 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4836
b4dd5633 4837 **datap = sigt;
1fd400ff
TT
4838 ++*datap;
4839
4840 return 1;
4841}
4842
78d4d2c5 4843/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4844 and fill them into TYPES_HTAB. It will process only type units,
4845 therefore DW_UT_type. */
c88ee1f0 4846
78d4d2c5
JK
4847static void
4848create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4849 dwarf2_section_info *section, htab_t &types_htab,
4850 rcuh_kind section_kind)
348e048f 4851{
3019eac3 4852 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4853 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4854 bfd *abfd;
4855 const gdb_byte *info_ptr, *end_ptr;
348e048f 4856
4bdcc0c1
DE
4857 abbrev_section = (dwo_file != NULL
4858 ? &dwo_file->sections.abbrev
4859 : &dwarf2_per_objfile->abbrev);
4860
b4f54984 4861 if (dwarf_read_debug)
43988095
JK
4862 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4863 get_section_name (section),
a32a8923 4864 get_section_file_name (abbrev_section));
09406207 4865
78d4d2c5
JK
4866 dwarf2_read_section (objfile, section);
4867 info_ptr = section->buffer;
348e048f 4868
78d4d2c5
JK
4869 if (info_ptr == NULL)
4870 return;
348e048f 4871
78d4d2c5
JK
4872 /* We can't set abfd until now because the section may be empty or
4873 not present, in which case the bfd is unknown. */
4874 abfd = get_section_bfd_owner (section);
348e048f 4875
78d4d2c5
JK
4876 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4877 because we don't need to read any dies: the signature is in the
4878 header. */
3019eac3 4879
78d4d2c5
JK
4880 end_ptr = info_ptr + section->size;
4881 while (info_ptr < end_ptr)
4882 {
78d4d2c5
JK
4883 struct signatured_type *sig_type;
4884 struct dwo_unit *dwo_tu;
4885 void **slot;
4886 const gdb_byte *ptr = info_ptr;
4887 struct comp_unit_head header;
4888 unsigned int length;
8b70b953 4889
9c541725 4890 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4891
a49dd8dd
JK
4892 /* Initialize it due to a false compiler warning. */
4893 header.signature = -1;
9c541725 4894 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4895
78d4d2c5
JK
4896 /* We need to read the type's signature in order to build the hash
4897 table, but we don't need anything else just yet. */
348e048f 4898
43988095
JK
4899 ptr = read_and_check_comp_unit_head (&header, section,
4900 abbrev_section, ptr, section_kind);
348e048f 4901
78d4d2c5 4902 length = get_cu_length (&header);
6caca83c 4903
78d4d2c5
JK
4904 /* Skip dummy type units. */
4905 if (ptr >= info_ptr + length
43988095
JK
4906 || peek_abbrev_code (abfd, ptr) == 0
4907 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4908 {
4909 info_ptr += length;
4910 continue;
4911 }
dee91e82 4912
78d4d2c5
JK
4913 if (types_htab == NULL)
4914 {
4915 if (dwo_file)
4916 types_htab = allocate_dwo_unit_table (objfile);
4917 else
4918 types_htab = allocate_signatured_type_table (objfile);
4919 }
8b70b953 4920
78d4d2c5
JK
4921 if (dwo_file)
4922 {
4923 sig_type = NULL;
4924 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4925 struct dwo_unit);
4926 dwo_tu->dwo_file = dwo_file;
43988095 4927 dwo_tu->signature = header.signature;
9c541725 4928 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4929 dwo_tu->section = section;
9c541725 4930 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4931 dwo_tu->length = length;
4932 }
4933 else
4934 {
4935 /* N.B.: type_offset is not usable if this type uses a DWO file.
4936 The real type_offset is in the DWO file. */
4937 dwo_tu = NULL;
4938 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4939 struct signatured_type);
43988095 4940 sig_type->signature = header.signature;
9c541725 4941 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4942 sig_type->per_cu.objfile = objfile;
4943 sig_type->per_cu.is_debug_types = 1;
4944 sig_type->per_cu.section = section;
9c541725 4945 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4946 sig_type->per_cu.length = length;
4947 }
4948
4949 slot = htab_find_slot (types_htab,
4950 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4951 INSERT);
4952 gdb_assert (slot != NULL);
4953 if (*slot != NULL)
4954 {
9c541725 4955 sect_offset dup_sect_off;
0349ea22 4956
3019eac3
DE
4957 if (dwo_file)
4958 {
78d4d2c5
JK
4959 const struct dwo_unit *dup_tu
4960 = (const struct dwo_unit *) *slot;
4961
9c541725 4962 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4963 }
4964 else
4965 {
78d4d2c5
JK
4966 const struct signatured_type *dup_tu
4967 = (const struct signatured_type *) *slot;
4968
9c541725 4969 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4970 }
8b70b953 4971
78d4d2c5
JK
4972 complaint (&symfile_complaints,
4973 _("debug type entry at offset 0x%x is duplicate to"
4974 " the entry at offset 0x%x, signature %s"),
9c541725 4975 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4976 hex_string (header.signature));
78d4d2c5
JK
4977 }
4978 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4979
78d4d2c5
JK
4980 if (dwarf_read_debug > 1)
4981 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4982 to_underlying (sect_off),
43988095 4983 hex_string (header.signature));
3019eac3 4984
78d4d2c5
JK
4985 info_ptr += length;
4986 }
4987}
3019eac3 4988
78d4d2c5
JK
4989/* Create the hash table of all entries in the .debug_types
4990 (or .debug_types.dwo) section(s).
4991 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4992 otherwise it is NULL.
b3c8eb43 4993
78d4d2c5 4994 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4995
78d4d2c5 4996 Note: This function processes DWO files only, not DWP files. */
348e048f 4997
78d4d2c5
JK
4998static void
4999create_debug_types_hash_table (struct dwo_file *dwo_file,
5000 VEC (dwarf2_section_info_def) *types,
5001 htab_t &types_htab)
5002{
5003 int ix;
5004 struct dwarf2_section_info *section;
5005
5006 if (VEC_empty (dwarf2_section_info_def, types))
5007 return;
348e048f 5008
78d4d2c5
JK
5009 for (ix = 0;
5010 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5011 ++ix)
43988095
JK
5012 create_debug_type_hash_table (dwo_file, section, types_htab,
5013 rcuh_kind::TYPE);
3019eac3
DE
5014}
5015
5016/* Create the hash table of all entries in the .debug_types section,
5017 and initialize all_type_units.
5018 The result is zero if there is an error (e.g. missing .debug_types section),
5019 otherwise non-zero. */
5020
5021static int
5022create_all_type_units (struct objfile *objfile)
5023{
78d4d2c5 5024 htab_t types_htab = NULL;
b4dd5633 5025 struct signatured_type **iter;
3019eac3 5026
43988095
JK
5027 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5028 rcuh_kind::COMPILE);
78d4d2c5 5029 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5030 if (types_htab == NULL)
5031 {
5032 dwarf2_per_objfile->signatured_types = NULL;
5033 return 0;
5034 }
5035
348e048f
DE
5036 dwarf2_per_objfile->signatured_types = types_htab;
5037
6aa5f3a6
DE
5038 dwarf2_per_objfile->n_type_units
5039 = dwarf2_per_objfile->n_allocated_type_units
5040 = htab_elements (types_htab);
8d749320
SM
5041 dwarf2_per_objfile->all_type_units =
5042 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5043 iter = &dwarf2_per_objfile->all_type_units[0];
5044 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5045 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5046 == dwarf2_per_objfile->n_type_units);
1fd400ff 5047
348e048f
DE
5048 return 1;
5049}
5050
6aa5f3a6
DE
5051/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5052 If SLOT is non-NULL, it is the entry to use in the hash table.
5053 Otherwise we find one. */
5054
5055static struct signatured_type *
5056add_type_unit (ULONGEST sig, void **slot)
5057{
5058 struct objfile *objfile = dwarf2_per_objfile->objfile;
5059 int n_type_units = dwarf2_per_objfile->n_type_units;
5060 struct signatured_type *sig_type;
5061
5062 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5063 ++n_type_units;
5064 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5065 {
5066 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5067 dwarf2_per_objfile->n_allocated_type_units = 1;
5068 dwarf2_per_objfile->n_allocated_type_units *= 2;
5069 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5070 = XRESIZEVEC (struct signatured_type *,
5071 dwarf2_per_objfile->all_type_units,
5072 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5073 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5074 }
5075 dwarf2_per_objfile->n_type_units = n_type_units;
5076
5077 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5078 struct signatured_type);
5079 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5080 sig_type->signature = sig;
5081 sig_type->per_cu.is_debug_types = 1;
5082 if (dwarf2_per_objfile->using_index)
5083 {
5084 sig_type->per_cu.v.quick =
5085 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5086 struct dwarf2_per_cu_quick_data);
5087 }
5088
5089 if (slot == NULL)
5090 {
5091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5092 sig_type, INSERT);
5093 }
5094 gdb_assert (*slot == NULL);
5095 *slot = sig_type;
5096 /* The rest of sig_type must be filled in by the caller. */
5097 return sig_type;
5098}
5099
a2ce51a0
DE
5100/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5101 Fill in SIG_ENTRY with DWO_ENTRY. */
5102
5103static void
5104fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5105 struct signatured_type *sig_entry,
5106 struct dwo_unit *dwo_entry)
5107{
7ee85ab1 5108 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5109 gdb_assert (! sig_entry->per_cu.queued);
5110 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5111 if (dwarf2_per_objfile->using_index)
5112 {
5113 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5114 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5115 }
5116 else
5117 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5118 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5119 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5120 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5121 gdb_assert (sig_entry->dwo_unit == NULL);
5122
5123 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5124 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5125 sig_entry->per_cu.length = dwo_entry->length;
5126 sig_entry->per_cu.reading_dwo_directly = 1;
5127 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5128 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5129 sig_entry->dwo_unit = dwo_entry;
5130}
5131
5132/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5133 If we haven't read the TU yet, create the signatured_type data structure
5134 for a TU to be read in directly from a DWO file, bypassing the stub.
5135 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5136 using .gdb_index, then when reading a CU we want to stay in the DWO file
5137 containing that CU. Otherwise we could end up reading several other DWO
5138 files (due to comdat folding) to process the transitive closure of all the
5139 mentioned TUs, and that can be slow. The current DWO file will have every
5140 type signature that it needs.
a2ce51a0
DE
5141 We only do this for .gdb_index because in the psymtab case we already have
5142 to read all the DWOs to build the type unit groups. */
5143
5144static struct signatured_type *
5145lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5146{
5147 struct objfile *objfile = dwarf2_per_objfile->objfile;
5148 struct dwo_file *dwo_file;
5149 struct dwo_unit find_dwo_entry, *dwo_entry;
5150 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5151 void **slot;
a2ce51a0
DE
5152
5153 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5154
6aa5f3a6
DE
5155 /* If TU skeletons have been removed then we may not have read in any
5156 TUs yet. */
5157 if (dwarf2_per_objfile->signatured_types == NULL)
5158 {
5159 dwarf2_per_objfile->signatured_types
5160 = allocate_signatured_type_table (objfile);
5161 }
a2ce51a0
DE
5162
5163 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5164 Use the global signatured_types array to do our own comdat-folding
5165 of types. If this is the first time we're reading this TU, and
5166 the TU has an entry in .gdb_index, replace the recorded data from
5167 .gdb_index with this TU. */
a2ce51a0 5168
a2ce51a0 5169 find_sig_entry.signature = sig;
6aa5f3a6
DE
5170 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5171 &find_sig_entry, INSERT);
9a3c8263 5172 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5173
5174 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5175 read. Don't reassign the global entry to point to this DWO if that's
5176 the case. Also note that if the TU is already being read, it may not
5177 have come from a DWO, the program may be a mix of Fission-compiled
5178 code and non-Fission-compiled code. */
5179
5180 /* Have we already tried to read this TU?
5181 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5182 needn't exist in the global table yet). */
5183 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5184 return sig_entry;
5185
6aa5f3a6
DE
5186 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5187 dwo_unit of the TU itself. */
5188 dwo_file = cu->dwo_unit->dwo_file;
5189
a2ce51a0
DE
5190 /* Ok, this is the first time we're reading this TU. */
5191 if (dwo_file->tus == NULL)
5192 return NULL;
5193 find_dwo_entry.signature = sig;
9a3c8263 5194 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5195 if (dwo_entry == NULL)
5196 return NULL;
5197
6aa5f3a6
DE
5198 /* If the global table doesn't have an entry for this TU, add one. */
5199 if (sig_entry == NULL)
5200 sig_entry = add_type_unit (sig, slot);
5201
a2ce51a0 5202 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5203 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5204 return sig_entry;
5205}
5206
a2ce51a0
DE
5207/* Subroutine of lookup_signatured_type.
5208 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5209 then try the DWP file. If the TU stub (skeleton) has been removed then
5210 it won't be in .gdb_index. */
a2ce51a0
DE
5211
5212static struct signatured_type *
5213lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5214{
5215 struct objfile *objfile = dwarf2_per_objfile->objfile;
5216 struct dwp_file *dwp_file = get_dwp_file ();
5217 struct dwo_unit *dwo_entry;
5218 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5219 void **slot;
a2ce51a0
DE
5220
5221 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5222 gdb_assert (dwp_file != NULL);
5223
6aa5f3a6
DE
5224 /* If TU skeletons have been removed then we may not have read in any
5225 TUs yet. */
5226 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5227 {
6aa5f3a6
DE
5228 dwarf2_per_objfile->signatured_types
5229 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5230 }
5231
6aa5f3a6
DE
5232 find_sig_entry.signature = sig;
5233 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5234 &find_sig_entry, INSERT);
9a3c8263 5235 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5236
5237 /* Have we already tried to read this TU?
5238 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5239 needn't exist in the global table yet). */
5240 if (sig_entry != NULL)
5241 return sig_entry;
5242
a2ce51a0
DE
5243 if (dwp_file->tus == NULL)
5244 return NULL;
57d63ce2
DE
5245 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5246 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5247 if (dwo_entry == NULL)
5248 return NULL;
5249
6aa5f3a6 5250 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5251 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5252
a2ce51a0
DE
5253 return sig_entry;
5254}
5255
380bca97 5256/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5257 Returns NULL if signature SIG is not present in the table.
5258 It is up to the caller to complain about this. */
348e048f
DE
5259
5260static struct signatured_type *
a2ce51a0 5261lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5262{
a2ce51a0
DE
5263 if (cu->dwo_unit
5264 && dwarf2_per_objfile->using_index)
5265 {
5266 /* We're in a DWO/DWP file, and we're using .gdb_index.
5267 These cases require special processing. */
5268 if (get_dwp_file () == NULL)
5269 return lookup_dwo_signatured_type (cu, sig);
5270 else
5271 return lookup_dwp_signatured_type (cu, sig);
5272 }
5273 else
5274 {
5275 struct signatured_type find_entry, *entry;
348e048f 5276
a2ce51a0
DE
5277 if (dwarf2_per_objfile->signatured_types == NULL)
5278 return NULL;
5279 find_entry.signature = sig;
9a3c8263
SM
5280 entry = ((struct signatured_type *)
5281 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5282 return entry;
5283 }
348e048f 5284}
42e7ad6c
DE
5285\f
5286/* Low level DIE reading support. */
348e048f 5287
d85a05f0
DJ
5288/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5289
5290static void
5291init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5292 struct dwarf2_cu *cu,
3019eac3
DE
5293 struct dwarf2_section_info *section,
5294 struct dwo_file *dwo_file)
d85a05f0 5295{
fceca515 5296 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5297 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5298 reader->cu = cu;
3019eac3 5299 reader->dwo_file = dwo_file;
dee91e82
DE
5300 reader->die_section = section;
5301 reader->buffer = section->buffer;
f664829e 5302 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5303 reader->comp_dir = NULL;
d85a05f0
DJ
5304}
5305
b0c7bfa9
DE
5306/* Subroutine of init_cutu_and_read_dies to simplify it.
5307 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5308 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5309 already.
5310
5311 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5312 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5313 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5314 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5315 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5316 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5317 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5318 are filled in with the info of the DIE from the DWO file.
5319 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5320 provided an abbrev table to use.
5321 The result is non-zero if a valid (non-dummy) DIE was found. */
5322
5323static int
5324read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5325 struct dwo_unit *dwo_unit,
5326 int abbrev_table_provided,
5327 struct die_info *stub_comp_unit_die,
a2ce51a0 5328 const char *stub_comp_dir,
b0c7bfa9 5329 struct die_reader_specs *result_reader,
d521ce57 5330 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5331 struct die_info **result_comp_unit_die,
5332 int *result_has_children)
5333{
5334 struct objfile *objfile = dwarf2_per_objfile->objfile;
5335 struct dwarf2_cu *cu = this_cu->cu;
5336 struct dwarf2_section_info *section;
5337 bfd *abfd;
d521ce57 5338 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5339 ULONGEST signature; /* Or dwo_id. */
5340 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5341 int i,num_extra_attrs;
5342 struct dwarf2_section_info *dwo_abbrev_section;
5343 struct attribute *attr;
5344 struct die_info *comp_unit_die;
5345
b0aeadb3
DE
5346 /* At most one of these may be provided. */
5347 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5348
b0c7bfa9
DE
5349 /* These attributes aren't processed until later:
5350 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5351 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5352 referenced later. However, these attributes are found in the stub
5353 which we won't have later. In order to not impose this complication
5354 on the rest of the code, we read them here and copy them to the
5355 DWO CU/TU die. */
b0c7bfa9
DE
5356
5357 stmt_list = NULL;
5358 low_pc = NULL;
5359 high_pc = NULL;
5360 ranges = NULL;
5361 comp_dir = NULL;
5362
5363 if (stub_comp_unit_die != NULL)
5364 {
5365 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5366 DWO file. */
5367 if (! this_cu->is_debug_types)
5368 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5369 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5370 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5371 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5372 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5373
5374 /* There should be a DW_AT_addr_base attribute here (if needed).
5375 We need the value before we can process DW_FORM_GNU_addr_index. */
5376 cu->addr_base = 0;
5377 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5378 if (attr)
5379 cu->addr_base = DW_UNSND (attr);
5380
5381 /* There should be a DW_AT_ranges_base attribute here (if needed).
5382 We need the value before we can process DW_AT_ranges. */
5383 cu->ranges_base = 0;
5384 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5385 if (attr)
5386 cu->ranges_base = DW_UNSND (attr);
5387 }
a2ce51a0
DE
5388 else if (stub_comp_dir != NULL)
5389 {
5390 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5391 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5392 comp_dir->name = DW_AT_comp_dir;
5393 comp_dir->form = DW_FORM_string;
5394 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5395 DW_STRING (comp_dir) = stub_comp_dir;
5396 }
b0c7bfa9
DE
5397
5398 /* Set up for reading the DWO CU/TU. */
5399 cu->dwo_unit = dwo_unit;
5400 section = dwo_unit->section;
5401 dwarf2_read_section (objfile, section);
a32a8923 5402 abfd = get_section_bfd_owner (section);
9c541725
PA
5403 begin_info_ptr = info_ptr = (section->buffer
5404 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5405 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5406 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5407
5408 if (this_cu->is_debug_types)
5409 {
b0c7bfa9
DE
5410 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5411
43988095 5412 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5413 dwo_abbrev_section,
43988095 5414 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5415 /* This is not an assert because it can be caused by bad debug info. */
43988095 5416 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5417 {
5418 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5419 " TU at offset 0x%x [in module %s]"),
5420 hex_string (sig_type->signature),
43988095 5421 hex_string (cu->header.signature),
9c541725 5422 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5423 bfd_get_filename (abfd));
5424 }
9c541725 5425 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5426 /* For DWOs coming from DWP files, we don't know the CU length
5427 nor the type's offset in the TU until now. */
5428 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5429 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5430
5431 /* Establish the type offset that can be used to lookup the type.
5432 For DWO files, we don't know it until now. */
9c541725
PA
5433 sig_type->type_offset_in_section
5434 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5435 }
5436 else
5437 {
5438 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5439 dwo_abbrev_section,
43988095 5440 info_ptr, rcuh_kind::COMPILE);
9c541725 5441 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5442 /* For DWOs coming from DWP files, we don't know the CU length
5443 until now. */
5444 dwo_unit->length = get_cu_length (&cu->header);
5445 }
5446
02142a6c
DE
5447 /* Replace the CU's original abbrev table with the DWO's.
5448 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5449 if (abbrev_table_provided)
5450 {
5451 /* Don't free the provided abbrev table, the caller of
5452 init_cutu_and_read_dies owns it. */
5453 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5454 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5455 make_cleanup (dwarf2_free_abbrev_table, cu);
5456 }
5457 else
5458 {
5459 dwarf2_free_abbrev_table (cu);
5460 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5461 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5462 }
5463
5464 /* Read in the die, but leave space to copy over the attributes
5465 from the stub. This has the benefit of simplifying the rest of
5466 the code - all the work to maintain the illusion of a single
5467 DW_TAG_{compile,type}_unit DIE is done here. */
5468 num_extra_attrs = ((stmt_list != NULL)
5469 + (low_pc != NULL)
5470 + (high_pc != NULL)
5471 + (ranges != NULL)
5472 + (comp_dir != NULL));
5473 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5474 result_has_children, num_extra_attrs);
5475
5476 /* Copy over the attributes from the stub to the DIE we just read in. */
5477 comp_unit_die = *result_comp_unit_die;
5478 i = comp_unit_die->num_attrs;
5479 if (stmt_list != NULL)
5480 comp_unit_die->attrs[i++] = *stmt_list;
5481 if (low_pc != NULL)
5482 comp_unit_die->attrs[i++] = *low_pc;
5483 if (high_pc != NULL)
5484 comp_unit_die->attrs[i++] = *high_pc;
5485 if (ranges != NULL)
5486 comp_unit_die->attrs[i++] = *ranges;
5487 if (comp_dir != NULL)
5488 comp_unit_die->attrs[i++] = *comp_dir;
5489 comp_unit_die->num_attrs += num_extra_attrs;
5490
b4f54984 5491 if (dwarf_die_debug)
bf6af496
DE
5492 {
5493 fprintf_unfiltered (gdb_stdlog,
5494 "Read die from %s@0x%x of %s:\n",
a32a8923 5495 get_section_name (section),
bf6af496
DE
5496 (unsigned) (begin_info_ptr - section->buffer),
5497 bfd_get_filename (abfd));
b4f54984 5498 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5499 }
5500
a2ce51a0
DE
5501 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5502 TUs by skipping the stub and going directly to the entry in the DWO file.
5503 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5504 to get it via circuitous means. Blech. */
5505 if (comp_dir != NULL)
5506 result_reader->comp_dir = DW_STRING (comp_dir);
5507
b0c7bfa9
DE
5508 /* Skip dummy compilation units. */
5509 if (info_ptr >= begin_info_ptr + dwo_unit->length
5510 || peek_abbrev_code (abfd, info_ptr) == 0)
5511 return 0;
5512
5513 *result_info_ptr = info_ptr;
5514 return 1;
5515}
5516
5517/* Subroutine of init_cutu_and_read_dies to simplify it.
5518 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5519 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5520
5521static struct dwo_unit *
5522lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5523 struct die_info *comp_unit_die)
5524{
5525 struct dwarf2_cu *cu = this_cu->cu;
5526 struct attribute *attr;
5527 ULONGEST signature;
5528 struct dwo_unit *dwo_unit;
5529 const char *comp_dir, *dwo_name;
5530
a2ce51a0
DE
5531 gdb_assert (cu != NULL);
5532
b0c7bfa9 5533 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5534 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5535 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5536
5537 if (this_cu->is_debug_types)
5538 {
5539 struct signatured_type *sig_type;
5540
5541 /* Since this_cu is the first member of struct signatured_type,
5542 we can go from a pointer to one to a pointer to the other. */
5543 sig_type = (struct signatured_type *) this_cu;
5544 signature = sig_type->signature;
5545 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5546 }
5547 else
5548 {
5549 struct attribute *attr;
5550
5551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5552 if (! attr)
5553 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5554 " [in module %s]"),
4262abfb 5555 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5556 signature = DW_UNSND (attr);
5557 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5558 signature);
5559 }
5560
b0c7bfa9
DE
5561 return dwo_unit;
5562}
5563
a2ce51a0 5564/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5565 See it for a description of the parameters.
5566 Read a TU directly from a DWO file, bypassing the stub.
5567
5568 Note: This function could be a little bit simpler if we shared cleanups
5569 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5570 to do, so we keep this function self-contained. Or we could move this
5571 into our caller, but it's complex enough already. */
a2ce51a0
DE
5572
5573static void
6aa5f3a6
DE
5574init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5575 int use_existing_cu, int keep,
a2ce51a0
DE
5576 die_reader_func_ftype *die_reader_func,
5577 void *data)
5578{
5579 struct dwarf2_cu *cu;
5580 struct signatured_type *sig_type;
6aa5f3a6 5581 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5582 struct die_reader_specs reader;
5583 const gdb_byte *info_ptr;
5584 struct die_info *comp_unit_die;
5585 int has_children;
5586
5587 /* Verify we can do the following downcast, and that we have the
5588 data we need. */
5589 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5590 sig_type = (struct signatured_type *) this_cu;
5591 gdb_assert (sig_type->dwo_unit != NULL);
5592
5593 cleanups = make_cleanup (null_cleanup, NULL);
5594
6aa5f3a6
DE
5595 if (use_existing_cu && this_cu->cu != NULL)
5596 {
5597 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5598 cu = this_cu->cu;
5599 /* There's no need to do the rereading_dwo_cu handling that
5600 init_cutu_and_read_dies does since we don't read the stub. */
5601 }
5602 else
5603 {
5604 /* If !use_existing_cu, this_cu->cu must be NULL. */
5605 gdb_assert (this_cu->cu == NULL);
8d749320 5606 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5607 init_one_comp_unit (cu, this_cu);
5608 /* If an error occurs while loading, release our storage. */
5609 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5610 }
5611
5612 /* A future optimization, if needed, would be to use an existing
5613 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5614 could share abbrev tables. */
a2ce51a0
DE
5615
5616 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5617 0 /* abbrev_table_provided */,
5618 NULL /* stub_comp_unit_die */,
5619 sig_type->dwo_unit->dwo_file->comp_dir,
5620 &reader, &info_ptr,
5621 &comp_unit_die, &has_children) == 0)
5622 {
5623 /* Dummy die. */
5624 do_cleanups (cleanups);
5625 return;
5626 }
5627
5628 /* All the "real" work is done here. */
5629 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5630
6aa5f3a6 5631 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5632 but the alternative is making the latter more complex.
5633 This function is only for the special case of using DWO files directly:
5634 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5635 if (free_cu_cleanup != NULL)
a2ce51a0 5636 {
6aa5f3a6
DE
5637 if (keep)
5638 {
5639 /* We've successfully allocated this compilation unit. Let our
5640 caller clean it up when finished with it. */
5641 discard_cleanups (free_cu_cleanup);
a2ce51a0 5642
6aa5f3a6
DE
5643 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5644 So we have to manually free the abbrev table. */
5645 dwarf2_free_abbrev_table (cu);
a2ce51a0 5646
6aa5f3a6
DE
5647 /* Link this CU into read_in_chain. */
5648 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5649 dwarf2_per_objfile->read_in_chain = this_cu;
5650 }
5651 else
5652 do_cleanups (free_cu_cleanup);
a2ce51a0 5653 }
a2ce51a0
DE
5654
5655 do_cleanups (cleanups);
5656}
5657
fd820528 5658/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5659 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5660
f4dc4d17
DE
5661 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5662 Otherwise the table specified in the comp unit header is read in and used.
5663 This is an optimization for when we already have the abbrev table.
5664
dee91e82
DE
5665 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5666 Otherwise, a new CU is allocated with xmalloc.
5667
5668 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5669 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5670
5671 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5672 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5673
70221824 5674static void
fd820528 5675init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5676 struct abbrev_table *abbrev_table,
fd820528
DE
5677 int use_existing_cu, int keep,
5678 die_reader_func_ftype *die_reader_func,
5679 void *data)
c906108c 5680{
dee91e82 5681 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5682 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5683 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5684 struct dwarf2_cu *cu;
d521ce57 5685 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5686 struct die_reader_specs reader;
d85a05f0 5687 struct die_info *comp_unit_die;
dee91e82 5688 int has_children;
d85a05f0 5689 struct attribute *attr;
365156ad 5690 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5691 struct signatured_type *sig_type = NULL;
4bdcc0c1 5692 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5693 /* Non-zero if CU currently points to a DWO file and we need to
5694 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5695 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5696 int rereading_dwo_cu = 0;
c906108c 5697
b4f54984 5698 if (dwarf_die_debug)
09406207
DE
5699 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5700 this_cu->is_debug_types ? "type" : "comp",
9c541725 5701 to_underlying (this_cu->sect_off));
09406207 5702
dee91e82
DE
5703 if (use_existing_cu)
5704 gdb_assert (keep);
23745b47 5705
a2ce51a0
DE
5706 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5707 file (instead of going through the stub), short-circuit all of this. */
5708 if (this_cu->reading_dwo_directly)
5709 {
5710 /* Narrow down the scope of possibilities to have to understand. */
5711 gdb_assert (this_cu->is_debug_types);
5712 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5713 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5714 die_reader_func, data);
a2ce51a0
DE
5715 return;
5716 }
5717
dee91e82
DE
5718 cleanups = make_cleanup (null_cleanup, NULL);
5719
5720 /* This is cheap if the section is already read in. */
5721 dwarf2_read_section (objfile, section);
5722
9c541725 5723 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5724
5725 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5726
5727 if (use_existing_cu && this_cu->cu != NULL)
5728 {
5729 cu = this_cu->cu;
42e7ad6c
DE
5730 /* If this CU is from a DWO file we need to start over, we need to
5731 refetch the attributes from the skeleton CU.
5732 This could be optimized by retrieving those attributes from when we
5733 were here the first time: the previous comp_unit_die was stored in
5734 comp_unit_obstack. But there's no data yet that we need this
5735 optimization. */
5736 if (cu->dwo_unit != NULL)
5737 rereading_dwo_cu = 1;
dee91e82
DE
5738 }
5739 else
5740 {
5741 /* If !use_existing_cu, this_cu->cu must be NULL. */
5742 gdb_assert (this_cu->cu == NULL);
8d749320 5743 cu = XNEW (struct dwarf2_cu);
dee91e82 5744 init_one_comp_unit (cu, this_cu);
dee91e82 5745 /* If an error occurs while loading, release our storage. */
365156ad 5746 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5747 }
dee91e82 5748
b0c7bfa9 5749 /* Get the header. */
9c541725 5750 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5751 {
5752 /* We already have the header, there's no need to read it in again. */
9c541725 5753 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5754 }
5755 else
5756 {
3019eac3 5757 if (this_cu->is_debug_types)
dee91e82 5758 {
43988095 5759 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5760 abbrev_section, info_ptr,
43988095 5761 rcuh_kind::TYPE);
dee91e82 5762
42e7ad6c
DE
5763 /* Since per_cu is the first member of struct signatured_type,
5764 we can go from a pointer to one to a pointer to the other. */
5765 sig_type = (struct signatured_type *) this_cu;
43988095 5766 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5767 gdb_assert (sig_type->type_offset_in_tu
5768 == cu->header.type_cu_offset_in_tu);
5769 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5770
42e7ad6c
DE
5771 /* LENGTH has not been set yet for type units if we're
5772 using .gdb_index. */
1ce1cefd 5773 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5774
5775 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5776 sig_type->type_offset_in_section =
5777 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5778
5779 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5780 }
5781 else
5782 {
4bdcc0c1
DE
5783 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5784 abbrev_section,
43988095
JK
5785 info_ptr,
5786 rcuh_kind::COMPILE);
dee91e82 5787
9c541725 5788 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5789 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5790 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5791 }
5792 }
10b3939b 5793
6caca83c 5794 /* Skip dummy compilation units. */
dee91e82 5795 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5796 || peek_abbrev_code (abfd, info_ptr) == 0)
5797 {
dee91e82 5798 do_cleanups (cleanups);
21b2bd31 5799 return;
6caca83c
CC
5800 }
5801
433df2d4
DE
5802 /* If we don't have them yet, read the abbrevs for this compilation unit.
5803 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5804 done. Note that it's important that if the CU had an abbrev table
5805 on entry we don't free it when we're done: Somewhere up the call stack
5806 it may be in use. */
f4dc4d17
DE
5807 if (abbrev_table != NULL)
5808 {
5809 gdb_assert (cu->abbrev_table == NULL);
9c541725 5810 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5811 cu->abbrev_table = abbrev_table;
5812 }
5813 else if (cu->abbrev_table == NULL)
dee91e82 5814 {
4bdcc0c1 5815 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5816 make_cleanup (dwarf2_free_abbrev_table, cu);
5817 }
42e7ad6c
DE
5818 else if (rereading_dwo_cu)
5819 {
5820 dwarf2_free_abbrev_table (cu);
5821 dwarf2_read_abbrevs (cu, abbrev_section);
5822 }
af703f96 5823
dee91e82 5824 /* Read the top level CU/TU die. */
3019eac3 5825 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5826 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5827
b0c7bfa9
DE
5828 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5829 from the DWO file.
5830 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5831 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5832 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5833 if (attr)
5834 {
3019eac3 5835 struct dwo_unit *dwo_unit;
b0c7bfa9 5836 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5837
5838 if (has_children)
6a506a2d
DE
5839 {
5840 complaint (&symfile_complaints,
5841 _("compilation unit with DW_AT_GNU_dwo_name"
5842 " has children (offset 0x%x) [in module %s]"),
9c541725 5843 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5844 }
b0c7bfa9 5845 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5846 if (dwo_unit != NULL)
3019eac3 5847 {
6a506a2d
DE
5848 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5849 abbrev_table != NULL,
a2ce51a0 5850 comp_unit_die, NULL,
6a506a2d
DE
5851 &reader, &info_ptr,
5852 &dwo_comp_unit_die, &has_children) == 0)
5853 {
5854 /* Dummy die. */
5855 do_cleanups (cleanups);
5856 return;
5857 }
5858 comp_unit_die = dwo_comp_unit_die;
5859 }
5860 else
5861 {
5862 /* Yikes, we couldn't find the rest of the DIE, we only have
5863 the stub. A complaint has already been logged. There's
5864 not much more we can do except pass on the stub DIE to
5865 die_reader_func. We don't want to throw an error on bad
5866 debug info. */
3019eac3
DE
5867 }
5868 }
5869
b0c7bfa9 5870 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5871 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5872
b0c7bfa9 5873 /* Done, clean up. */
365156ad 5874 if (free_cu_cleanup != NULL)
348e048f 5875 {
365156ad
TT
5876 if (keep)
5877 {
5878 /* We've successfully allocated this compilation unit. Let our
5879 caller clean it up when finished with it. */
5880 discard_cleanups (free_cu_cleanup);
dee91e82 5881
365156ad
TT
5882 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5883 So we have to manually free the abbrev table. */
5884 dwarf2_free_abbrev_table (cu);
dee91e82 5885
365156ad
TT
5886 /* Link this CU into read_in_chain. */
5887 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5888 dwarf2_per_objfile->read_in_chain = this_cu;
5889 }
5890 else
5891 do_cleanups (free_cu_cleanup);
348e048f 5892 }
365156ad
TT
5893
5894 do_cleanups (cleanups);
dee91e82
DE
5895}
5896
33e80786
DE
5897/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5898 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5899 to have already done the lookup to find the DWO file).
dee91e82
DE
5900
5901 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5902 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5903
5904 We fill in THIS_CU->length.
5905
5906 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5907 linker) then DIE_READER_FUNC will not get called.
5908
5909 THIS_CU->cu is always freed when done.
3019eac3
DE
5910 This is done in order to not leave THIS_CU->cu in a state where we have
5911 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5912
5913static void
5914init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5915 struct dwo_file *dwo_file,
dee91e82
DE
5916 die_reader_func_ftype *die_reader_func,
5917 void *data)
5918{
5919 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5920 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5921 bfd *abfd = get_section_bfd_owner (section);
33e80786 5922 struct dwarf2_section_info *abbrev_section;
dee91e82 5923 struct dwarf2_cu cu;
d521ce57 5924 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5925 struct die_reader_specs reader;
5926 struct cleanup *cleanups;
5927 struct die_info *comp_unit_die;
5928 int has_children;
5929
b4f54984 5930 if (dwarf_die_debug)
09406207
DE
5931 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5932 this_cu->is_debug_types ? "type" : "comp",
9c541725 5933 to_underlying (this_cu->sect_off));
09406207 5934
dee91e82
DE
5935 gdb_assert (this_cu->cu == NULL);
5936
33e80786
DE
5937 abbrev_section = (dwo_file != NULL
5938 ? &dwo_file->sections.abbrev
5939 : get_abbrev_section_for_cu (this_cu));
5940
dee91e82
DE
5941 /* This is cheap if the section is already read in. */
5942 dwarf2_read_section (objfile, section);
5943
5944 init_one_comp_unit (&cu, this_cu);
5945
5946 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5947
9c541725 5948 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5949 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5950 abbrev_section, info_ptr,
43988095
JK
5951 (this_cu->is_debug_types
5952 ? rcuh_kind::TYPE
5953 : rcuh_kind::COMPILE));
dee91e82 5954
1ce1cefd 5955 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5956
5957 /* Skip dummy compilation units. */
5958 if (info_ptr >= begin_info_ptr + this_cu->length
5959 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5960 {
dee91e82 5961 do_cleanups (cleanups);
21b2bd31 5962 return;
93311388 5963 }
72bf9492 5964
dee91e82
DE
5965 dwarf2_read_abbrevs (&cu, abbrev_section);
5966 make_cleanup (dwarf2_free_abbrev_table, &cu);
5967
3019eac3 5968 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5969 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5970
5971 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5972
5973 do_cleanups (cleanups);
5974}
5975
3019eac3
DE
5976/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5977 does not lookup the specified DWO file.
5978 This cannot be used to read DWO files.
dee91e82
DE
5979
5980 THIS_CU->cu is always freed when done.
3019eac3
DE
5981 This is done in order to not leave THIS_CU->cu in a state where we have
5982 to care whether it refers to the "main" CU or the DWO CU.
5983 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5984
5985static void
5986init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5987 die_reader_func_ftype *die_reader_func,
5988 void *data)
5989{
33e80786 5990 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5991}
0018ea6f
DE
5992\f
5993/* Type Unit Groups.
dee91e82 5994
0018ea6f
DE
5995 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5996 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5997 so that all types coming from the same compilation (.o file) are grouped
5998 together. A future step could be to put the types in the same symtab as
5999 the CU the types ultimately came from. */
ff013f42 6000
f4dc4d17
DE
6001static hashval_t
6002hash_type_unit_group (const void *item)
6003{
9a3c8263
SM
6004 const struct type_unit_group *tu_group
6005 = (const struct type_unit_group *) item;
f4dc4d17 6006
094b34ac 6007 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6008}
348e048f
DE
6009
6010static int
f4dc4d17 6011eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6012{
9a3c8263
SM
6013 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6014 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6015
094b34ac 6016 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6017}
348e048f 6018
f4dc4d17
DE
6019/* Allocate a hash table for type unit groups. */
6020
6021static htab_t
6022allocate_type_unit_groups_table (void)
6023{
6024 return htab_create_alloc_ex (3,
6025 hash_type_unit_group,
6026 eq_type_unit_group,
6027 NULL,
6028 &dwarf2_per_objfile->objfile->objfile_obstack,
6029 hashtab_obstack_allocate,
6030 dummy_obstack_deallocate);
6031}
dee91e82 6032
f4dc4d17
DE
6033/* Type units that don't have DW_AT_stmt_list are grouped into their own
6034 partial symtabs. We combine several TUs per psymtab to not let the size
6035 of any one psymtab grow too big. */
6036#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6037#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6038
094b34ac 6039/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6040 Create the type_unit_group object used to hold one or more TUs. */
6041
6042static struct type_unit_group *
094b34ac 6043create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6044{
6045 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6046 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6047 struct type_unit_group *tu_group;
f4dc4d17
DE
6048
6049 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6050 struct type_unit_group);
094b34ac 6051 per_cu = &tu_group->per_cu;
f4dc4d17 6052 per_cu->objfile = objfile;
f4dc4d17 6053
094b34ac
DE
6054 if (dwarf2_per_objfile->using_index)
6055 {
6056 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6057 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6058 }
6059 else
6060 {
9c541725 6061 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6062 struct partial_symtab *pst;
6063 char *name;
6064
6065 /* Give the symtab a useful name for debug purposes. */
6066 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6067 name = xstrprintf ("<type_units_%d>",
6068 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6069 else
6070 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6071
6072 pst = create_partial_symtab (per_cu, name);
6073 pst->anonymous = 1;
f4dc4d17 6074
094b34ac
DE
6075 xfree (name);
6076 }
f4dc4d17 6077
094b34ac 6078 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6079 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6080
6081 return tu_group;
6082}
6083
094b34ac
DE
6084/* Look up the type_unit_group for type unit CU, and create it if necessary.
6085 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6086
6087static struct type_unit_group *
ff39bb5e 6088get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6089{
6090 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6091 struct type_unit_group *tu_group;
6092 void **slot;
6093 unsigned int line_offset;
6094 struct type_unit_group type_unit_group_for_lookup;
6095
6096 if (dwarf2_per_objfile->type_unit_groups == NULL)
6097 {
6098 dwarf2_per_objfile->type_unit_groups =
6099 allocate_type_unit_groups_table ();
6100 }
6101
6102 /* Do we need to create a new group, or can we use an existing one? */
6103
6104 if (stmt_list)
6105 {
6106 line_offset = DW_UNSND (stmt_list);
6107 ++tu_stats->nr_symtab_sharers;
6108 }
6109 else
6110 {
6111 /* Ugh, no stmt_list. Rare, but we have to handle it.
6112 We can do various things here like create one group per TU or
6113 spread them over multiple groups to split up the expansion work.
6114 To avoid worst case scenarios (too many groups or too large groups)
6115 we, umm, group them in bunches. */
6116 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6117 | (tu_stats->nr_stmt_less_type_units
6118 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6119 ++tu_stats->nr_stmt_less_type_units;
6120 }
6121
094b34ac 6122 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6123 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6124 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6125 &type_unit_group_for_lookup, INSERT);
6126 if (*slot != NULL)
6127 {
9a3c8263 6128 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6129 gdb_assert (tu_group != NULL);
6130 }
6131 else
6132 {
9c541725 6133 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6134 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6135 *slot = tu_group;
6136 ++tu_stats->nr_symtabs;
6137 }
6138
6139 return tu_group;
6140}
0018ea6f
DE
6141\f
6142/* Partial symbol tables. */
6143
6144/* Create a psymtab named NAME and assign it to PER_CU.
6145
6146 The caller must fill in the following details:
6147 dirname, textlow, texthigh. */
6148
6149static struct partial_symtab *
6150create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6151{
6152 struct objfile *objfile = per_cu->objfile;
6153 struct partial_symtab *pst;
6154
18a94d75 6155 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6156 objfile->global_psymbols.next,
6157 objfile->static_psymbols.next);
6158
6159 pst->psymtabs_addrmap_supported = 1;
6160
6161 /* This is the glue that links PST into GDB's symbol API. */
6162 pst->read_symtab_private = per_cu;
6163 pst->read_symtab = dwarf2_read_symtab;
6164 per_cu->v.psymtab = pst;
6165
6166 return pst;
6167}
6168
b93601f3
TT
6169/* The DATA object passed to process_psymtab_comp_unit_reader has this
6170 type. */
6171
6172struct process_psymtab_comp_unit_data
6173{
6174 /* True if we are reading a DW_TAG_partial_unit. */
6175
6176 int want_partial_unit;
6177
6178 /* The "pretend" language that is used if the CU doesn't declare a
6179 language. */
6180
6181 enum language pretend_language;
6182};
6183
0018ea6f
DE
6184/* die_reader_func for process_psymtab_comp_unit. */
6185
6186static void
6187process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6188 const gdb_byte *info_ptr,
0018ea6f
DE
6189 struct die_info *comp_unit_die,
6190 int has_children,
6191 void *data)
6192{
6193 struct dwarf2_cu *cu = reader->cu;
6194 struct objfile *objfile = cu->objfile;
3e29f34a 6195 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6196 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6197 CORE_ADDR baseaddr;
6198 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6199 struct partial_symtab *pst;
3a2b436a 6200 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6201 const char *filename;
9a3c8263
SM
6202 struct process_psymtab_comp_unit_data *info
6203 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6204
b93601f3 6205 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6206 return;
6207
6208 gdb_assert (! per_cu->is_debug_types);
6209
b93601f3 6210 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6211
6212 cu->list_in_scope = &file_symbols;
6213
6214 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6215 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6216 if (filename == NULL)
0018ea6f 6217 filename = "";
0018ea6f
DE
6218
6219 pst = create_partial_symtab (per_cu, filename);
6220
6221 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6222 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6223
6224 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6225
6226 dwarf2_find_base_address (comp_unit_die, cu);
6227
6228 /* Possibly set the default values of LOWPC and HIGHPC from
6229 `DW_AT_ranges'. */
3a2b436a
JK
6230 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6231 &best_highpc, cu, pst);
6232 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6233 /* Store the contiguous range if it is not empty; it can be empty for
6234 CUs with no code. */
6235 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6236 gdbarch_adjust_dwarf2_addr (gdbarch,
6237 best_lowpc + baseaddr),
6238 gdbarch_adjust_dwarf2_addr (gdbarch,
6239 best_highpc + baseaddr) - 1,
6240 pst);
0018ea6f
DE
6241
6242 /* Check if comp unit has_children.
6243 If so, read the rest of the partial symbols from this comp unit.
6244 If not, there's no more debug_info for this comp unit. */
6245 if (has_children)
6246 {
6247 struct partial_die_info *first_die;
6248 CORE_ADDR lowpc, highpc;
6249
6250 lowpc = ((CORE_ADDR) -1);
6251 highpc = ((CORE_ADDR) 0);
6252
6253 first_die = load_partial_dies (reader, info_ptr, 1);
6254
6255 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6256 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6257
6258 /* If we didn't find a lowpc, set it to highpc to avoid
6259 complaints from `maint check'. */
6260 if (lowpc == ((CORE_ADDR) -1))
6261 lowpc = highpc;
6262
6263 /* If the compilation unit didn't have an explicit address range,
6264 then use the information extracted from its child dies. */
e385593e 6265 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6266 {
6267 best_lowpc = lowpc;
6268 best_highpc = highpc;
6269 }
6270 }
3e29f34a
MR
6271 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6272 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6273
8763cede 6274 end_psymtab_common (objfile, pst);
0018ea6f
DE
6275
6276 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6277 {
6278 int i;
6279 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6280 struct dwarf2_per_cu_data *iter;
6281
6282 /* Fill in 'dependencies' here; we fill in 'users' in a
6283 post-pass. */
6284 pst->number_of_dependencies = len;
8d749320
SM
6285 pst->dependencies =
6286 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6287 for (i = 0;
6288 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6289 i, iter);
6290 ++i)
6291 pst->dependencies[i] = iter->v.psymtab;
6292
6293 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6294 }
6295
6296 /* Get the list of files included in the current compilation unit,
6297 and build a psymtab for each of them. */
6298 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6299
b4f54984 6300 if (dwarf_read_debug)
0018ea6f
DE
6301 {
6302 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6303
6304 fprintf_unfiltered (gdb_stdlog,
6305 "Psymtab for %s unit @0x%x: %s - %s"
6306 ", %d global, %d static syms\n",
6307 per_cu->is_debug_types ? "type" : "comp",
9c541725 6308 to_underlying (per_cu->sect_off),
0018ea6f
DE
6309 paddress (gdbarch, pst->textlow),
6310 paddress (gdbarch, pst->texthigh),
6311 pst->n_global_syms, pst->n_static_syms);
6312 }
6313}
6314
6315/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6316 Process compilation unit THIS_CU for a psymtab. */
6317
6318static void
6319process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6320 int want_partial_unit,
6321 enum language pretend_language)
0018ea6f
DE
6322{
6323 /* If this compilation unit was already read in, free the
6324 cached copy in order to read it in again. This is
6325 necessary because we skipped some symbols when we first
6326 read in the compilation unit (see load_partial_dies).
6327 This problem could be avoided, but the benefit is unclear. */
6328 if (this_cu->cu != NULL)
6329 free_one_cached_comp_unit (this_cu);
6330
f1902523
JK
6331 if (this_cu->is_debug_types)
6332 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6333 NULL);
6334 else
6335 {
6336 process_psymtab_comp_unit_data info;
6337 info.want_partial_unit = want_partial_unit;
6338 info.pretend_language = pretend_language;
6339 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6340 process_psymtab_comp_unit_reader, &info);
6341 }
0018ea6f
DE
6342
6343 /* Age out any secondary CUs. */
6344 age_cached_comp_units ();
6345}
f4dc4d17
DE
6346
6347/* Reader function for build_type_psymtabs. */
6348
6349static void
6350build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6351 const gdb_byte *info_ptr,
f4dc4d17
DE
6352 struct die_info *type_unit_die,
6353 int has_children,
6354 void *data)
6355{
6356 struct objfile *objfile = dwarf2_per_objfile->objfile;
6357 struct dwarf2_cu *cu = reader->cu;
6358 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6359 struct signatured_type *sig_type;
f4dc4d17
DE
6360 struct type_unit_group *tu_group;
6361 struct attribute *attr;
6362 struct partial_die_info *first_die;
6363 CORE_ADDR lowpc, highpc;
6364 struct partial_symtab *pst;
6365
6366 gdb_assert (data == NULL);
0186c6a7
DE
6367 gdb_assert (per_cu->is_debug_types);
6368 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6369
6370 if (! has_children)
6371 return;
6372
6373 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6374 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6375
0186c6a7 6376 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6377
6378 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6379 cu->list_in_scope = &file_symbols;
6380 pst = create_partial_symtab (per_cu, "");
6381 pst->anonymous = 1;
6382
6383 first_die = load_partial_dies (reader, info_ptr, 1);
6384
6385 lowpc = (CORE_ADDR) -1;
6386 highpc = (CORE_ADDR) 0;
6387 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6388
8763cede 6389 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6390}
6391
73051182
DE
6392/* Struct used to sort TUs by their abbreviation table offset. */
6393
6394struct tu_abbrev_offset
6395{
6396 struct signatured_type *sig_type;
6397 sect_offset abbrev_offset;
6398};
6399
6400/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6401
6402static int
6403sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6404{
9a3c8263
SM
6405 const struct tu_abbrev_offset * const *a
6406 = (const struct tu_abbrev_offset * const*) ap;
6407 const struct tu_abbrev_offset * const *b
6408 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6409 sect_offset aoff = (*a)->abbrev_offset;
6410 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6411
6412 return (aoff > boff) - (aoff < boff);
6413}
6414
6415/* Efficiently read all the type units.
6416 This does the bulk of the work for build_type_psymtabs.
6417
6418 The efficiency is because we sort TUs by the abbrev table they use and
6419 only read each abbrev table once. In one program there are 200K TUs
6420 sharing 8K abbrev tables.
6421
6422 The main purpose of this function is to support building the
6423 dwarf2_per_objfile->type_unit_groups table.
6424 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6425 can collapse the search space by grouping them by stmt_list.
6426 The savings can be significant, in the same program from above the 200K TUs
6427 share 8K stmt_list tables.
6428
6429 FUNC is expected to call get_type_unit_group, which will create the
6430 struct type_unit_group if necessary and add it to
6431 dwarf2_per_objfile->type_unit_groups. */
6432
6433static void
6434build_type_psymtabs_1 (void)
6435{
73051182
DE
6436 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6437 struct cleanup *cleanups;
6438 struct abbrev_table *abbrev_table;
6439 sect_offset abbrev_offset;
6440 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6441 int i;
6442
6443 /* It's up to the caller to not call us multiple times. */
6444 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6445
6446 if (dwarf2_per_objfile->n_type_units == 0)
6447 return;
6448
6449 /* TUs typically share abbrev tables, and there can be way more TUs than
6450 abbrev tables. Sort by abbrev table to reduce the number of times we
6451 read each abbrev table in.
6452 Alternatives are to punt or to maintain a cache of abbrev tables.
6453 This is simpler and efficient enough for now.
6454
6455 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6456 symtab to use). Typically TUs with the same abbrev offset have the same
6457 stmt_list value too so in practice this should work well.
6458
6459 The basic algorithm here is:
6460
6461 sort TUs by abbrev table
6462 for each TU with same abbrev table:
6463 read abbrev table if first user
6464 read TU top level DIE
6465 [IWBN if DWO skeletons had DW_AT_stmt_list]
6466 call FUNC */
6467
b4f54984 6468 if (dwarf_read_debug)
73051182
DE
6469 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6470
6471 /* Sort in a separate table to maintain the order of all_type_units
6472 for .gdb_index: TU indices directly index all_type_units. */
6473 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6474 dwarf2_per_objfile->n_type_units);
6475 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6476 {
6477 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6478
6479 sorted_by_abbrev[i].sig_type = sig_type;
6480 sorted_by_abbrev[i].abbrev_offset =
6481 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6482 sig_type->per_cu.sect_off);
73051182
DE
6483 }
6484 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6485 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6486 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6487
9c541725 6488 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6489 abbrev_table = NULL;
6490 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6491
6492 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6493 {
6494 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6495
6496 /* Switch to the next abbrev table if necessary. */
6497 if (abbrev_table == NULL
9c541725 6498 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6499 {
6500 if (abbrev_table != NULL)
6501 {
6502 abbrev_table_free (abbrev_table);
6503 /* Reset to NULL in case abbrev_table_read_table throws
6504 an error: abbrev_table_free_cleanup will get called. */
6505 abbrev_table = NULL;
6506 }
6507 abbrev_offset = tu->abbrev_offset;
6508 abbrev_table =
6509 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6510 abbrev_offset);
6511 ++tu_stats->nr_uniq_abbrev_tables;
6512 }
6513
6514 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6515 build_type_psymtabs_reader, NULL);
6516 }
6517
73051182 6518 do_cleanups (cleanups);
6aa5f3a6 6519}
73051182 6520
6aa5f3a6
DE
6521/* Print collected type unit statistics. */
6522
6523static void
6524print_tu_stats (void)
6525{
6526 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6527
6528 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6529 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6530 dwarf2_per_objfile->n_type_units);
6531 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6532 tu_stats->nr_uniq_abbrev_tables);
6533 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6534 tu_stats->nr_symtabs);
6535 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6536 tu_stats->nr_symtab_sharers);
6537 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6538 tu_stats->nr_stmt_less_type_units);
6539 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6540 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6541}
6542
f4dc4d17
DE
6543/* Traversal function for build_type_psymtabs. */
6544
6545static int
6546build_type_psymtab_dependencies (void **slot, void *info)
6547{
6548 struct objfile *objfile = dwarf2_per_objfile->objfile;
6549 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6550 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6551 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6552 int len = VEC_length (sig_type_ptr, tu_group->tus);
6553 struct signatured_type *iter;
f4dc4d17
DE
6554 int i;
6555
6556 gdb_assert (len > 0);
0186c6a7 6557 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6558
6559 pst->number_of_dependencies = len;
8d749320
SM
6560 pst->dependencies =
6561 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6562 for (i = 0;
0186c6a7 6563 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6564 ++i)
6565 {
0186c6a7
DE
6566 gdb_assert (iter->per_cu.is_debug_types);
6567 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6568 iter->type_unit_group = tu_group;
f4dc4d17
DE
6569 }
6570
0186c6a7 6571 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6572
6573 return 1;
6574}
6575
6576/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6577 Build partial symbol tables for the .debug_types comp-units. */
6578
6579static void
6580build_type_psymtabs (struct objfile *objfile)
6581{
0e50663e 6582 if (! create_all_type_units (objfile))
348e048f
DE
6583 return;
6584
73051182 6585 build_type_psymtabs_1 ();
6aa5f3a6 6586}
f4dc4d17 6587
6aa5f3a6
DE
6588/* Traversal function for process_skeletonless_type_unit.
6589 Read a TU in a DWO file and build partial symbols for it. */
6590
6591static int
6592process_skeletonless_type_unit (void **slot, void *info)
6593{
6594 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6595 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6596 struct signatured_type find_entry, *entry;
6597
6598 /* If this TU doesn't exist in the global table, add it and read it in. */
6599
6600 if (dwarf2_per_objfile->signatured_types == NULL)
6601 {
6602 dwarf2_per_objfile->signatured_types
6603 = allocate_signatured_type_table (objfile);
6604 }
6605
6606 find_entry.signature = dwo_unit->signature;
6607 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6608 INSERT);
6609 /* If we've already seen this type there's nothing to do. What's happening
6610 is we're doing our own version of comdat-folding here. */
6611 if (*slot != NULL)
6612 return 1;
6613
6614 /* This does the job that create_all_type_units would have done for
6615 this TU. */
6616 entry = add_type_unit (dwo_unit->signature, slot);
6617 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6618 *slot = entry;
6619
6620 /* This does the job that build_type_psymtabs_1 would have done. */
6621 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6622 build_type_psymtabs_reader, NULL);
6623
6624 return 1;
6625}
6626
6627/* Traversal function for process_skeletonless_type_units. */
6628
6629static int
6630process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6631{
6632 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6633
6634 if (dwo_file->tus != NULL)
6635 {
6636 htab_traverse_noresize (dwo_file->tus,
6637 process_skeletonless_type_unit, info);
6638 }
6639
6640 return 1;
6641}
6642
6643/* Scan all TUs of DWO files, verifying we've processed them.
6644 This is needed in case a TU was emitted without its skeleton.
6645 Note: This can't be done until we know what all the DWO files are. */
6646
6647static void
6648process_skeletonless_type_units (struct objfile *objfile)
6649{
6650 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6651 if (get_dwp_file () == NULL
6652 && dwarf2_per_objfile->dwo_files != NULL)
6653 {
6654 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6655 process_dwo_file_for_skeletonless_type_units,
6656 objfile);
6657 }
348e048f
DE
6658}
6659
60606b2c
TT
6660/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6661
6662static void
6663psymtabs_addrmap_cleanup (void *o)
6664{
9a3c8263 6665 struct objfile *objfile = (struct objfile *) o;
ec61707d 6666
60606b2c
TT
6667 objfile->psymtabs_addrmap = NULL;
6668}
6669
95554aad
TT
6670/* Compute the 'user' field for each psymtab in OBJFILE. */
6671
6672static void
6673set_partial_user (struct objfile *objfile)
6674{
6675 int i;
6676
6677 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6678 {
8832e7e3 6679 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6680 struct partial_symtab *pst = per_cu->v.psymtab;
6681 int j;
6682
36586728
TT
6683 if (pst == NULL)
6684 continue;
6685
95554aad
TT
6686 for (j = 0; j < pst->number_of_dependencies; ++j)
6687 {
6688 /* Set the 'user' field only if it is not already set. */
6689 if (pst->dependencies[j]->user == NULL)
6690 pst->dependencies[j]->user = pst;
6691 }
6692 }
6693}
6694
93311388
DE
6695/* Build the partial symbol table by doing a quick pass through the
6696 .debug_info and .debug_abbrev sections. */
72bf9492 6697
93311388 6698static void
c67a9c90 6699dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6700{
60606b2c 6701 struct cleanup *back_to, *addrmap_cleanup;
21b2bd31 6702 int i;
93311388 6703
b4f54984 6704 if (dwarf_read_debug)
45cfd468
DE
6705 {
6706 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6707 objfile_name (objfile));
45cfd468
DE
6708 }
6709
98bfdba5
PA
6710 dwarf2_per_objfile->reading_partial_symbols = 1;
6711
be391dca 6712 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6713
93311388
DE
6714 /* Any cached compilation units will be linked by the per-objfile
6715 read_in_chain. Make sure to free them when we're done. */
6716 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6717
348e048f
DE
6718 build_type_psymtabs (objfile);
6719
93311388 6720 create_all_comp_units (objfile);
c906108c 6721
60606b2c
TT
6722 /* Create a temporary address map on a temporary obstack. We later
6723 copy this to the final obstack. */
8268c778 6724 auto_obstack temp_obstack;
60606b2c
TT
6725 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6726 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6727
21b2bd31 6728 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6729 {
8832e7e3 6730 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6731
b93601f3 6732 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6733 }
ff013f42 6734
6aa5f3a6
DE
6735 /* This has to wait until we read the CUs, we need the list of DWOs. */
6736 process_skeletonless_type_units (objfile);
6737
6738 /* Now that all TUs have been processed we can fill in the dependencies. */
6739 if (dwarf2_per_objfile->type_unit_groups != NULL)
6740 {
6741 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6742 build_type_psymtab_dependencies, NULL);
6743 }
6744
b4f54984 6745 if (dwarf_read_debug)
6aa5f3a6
DE
6746 print_tu_stats ();
6747
95554aad
TT
6748 set_partial_user (objfile);
6749
ff013f42
JK
6750 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6751 &objfile->objfile_obstack);
60606b2c 6752 discard_cleanups (addrmap_cleanup);
ff013f42 6753
ae038cb0 6754 do_cleanups (back_to);
45cfd468 6755
b4f54984 6756 if (dwarf_read_debug)
45cfd468 6757 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6758 objfile_name (objfile));
ae038cb0
DJ
6759}
6760
3019eac3 6761/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6762
6763static void
dee91e82 6764load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6765 const gdb_byte *info_ptr,
dee91e82
DE
6766 struct die_info *comp_unit_die,
6767 int has_children,
6768 void *data)
ae038cb0 6769{
dee91e82 6770 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6771
95554aad 6772 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6773
ae038cb0
DJ
6774 /* Check if comp unit has_children.
6775 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6776 If not, there's no more debug_info for this comp unit. */
d85a05f0 6777 if (has_children)
dee91e82
DE
6778 load_partial_dies (reader, info_ptr, 0);
6779}
98bfdba5 6780
dee91e82
DE
6781/* Load the partial DIEs for a secondary CU into memory.
6782 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6783
dee91e82
DE
6784static void
6785load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6786{
f4dc4d17
DE
6787 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6788 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6789}
6790
ae038cb0 6791static void
36586728
TT
6792read_comp_units_from_section (struct objfile *objfile,
6793 struct dwarf2_section_info *section,
f1902523 6794 struct dwarf2_section_info *abbrev_section,
36586728
TT
6795 unsigned int is_dwz,
6796 int *n_allocated,
6797 int *n_comp_units,
6798 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6799{
d521ce57 6800 const gdb_byte *info_ptr;
a32a8923 6801 bfd *abfd = get_section_bfd_owner (section);
be391dca 6802
b4f54984 6803 if (dwarf_read_debug)
bf6af496 6804 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6805 get_section_name (section),
6806 get_section_file_name (section));
bf6af496 6807
36586728 6808 dwarf2_read_section (objfile, section);
ae038cb0 6809
36586728 6810 info_ptr = section->buffer;
6e70227d 6811
36586728 6812 while (info_ptr < section->buffer + section->size)
ae038cb0 6813 {
ae038cb0 6814 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6815
9c541725 6816 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6817
f1902523
JK
6818 comp_unit_head cu_header;
6819 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6820 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6821
6822 /* Save the compilation unit for later lookup. */
f1902523
JK
6823 if (cu_header.unit_type != DW_UT_type)
6824 {
6825 this_cu = XOBNEW (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_data);
6827 memset (this_cu, 0, sizeof (*this_cu));
6828 }
6829 else
6830 {
6831 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6832 struct signatured_type);
6833 memset (sig_type, 0, sizeof (*sig_type));
6834 sig_type->signature = cu_header.signature;
6835 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6836 this_cu = &sig_type->per_cu;
6837 }
6838 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6839 this_cu->sect_off = sect_off;
f1902523 6840 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6841 this_cu->is_dwz = is_dwz;
9291a0cd 6842 this_cu->objfile = objfile;
8a0459fd 6843 this_cu->section = section;
ae038cb0 6844
36586728 6845 if (*n_comp_units == *n_allocated)
ae038cb0 6846 {
36586728 6847 *n_allocated *= 2;
224c3ddb
SM
6848 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6849 *all_comp_units, *n_allocated);
ae038cb0 6850 }
36586728
TT
6851 (*all_comp_units)[*n_comp_units] = this_cu;
6852 ++*n_comp_units;
ae038cb0
DJ
6853
6854 info_ptr = info_ptr + this_cu->length;
6855 }
36586728
TT
6856}
6857
6858/* Create a list of all compilation units in OBJFILE.
6859 This is only done for -readnow and building partial symtabs. */
6860
6861static void
6862create_all_comp_units (struct objfile *objfile)
6863{
6864 int n_allocated;
6865 int n_comp_units;
6866 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6867 struct dwz_file *dwz;
36586728
TT
6868
6869 n_comp_units = 0;
6870 n_allocated = 10;
8d749320 6871 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6872
f1902523
JK
6873 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6874 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6875 &n_allocated, &n_comp_units, &all_comp_units);
6876
4db1a1dc
TT
6877 dwz = dwarf2_get_dwz_file ();
6878 if (dwz != NULL)
f1902523 6879 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6880 &n_allocated, &n_comp_units,
6881 &all_comp_units);
ae038cb0 6882
8d749320
SM
6883 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6884 struct dwarf2_per_cu_data *,
6885 n_comp_units);
ae038cb0
DJ
6886 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6887 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6888 xfree (all_comp_units);
6889 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6890}
6891
5734ee8b 6892/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6893 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6894 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6895 DW_AT_ranges). See the comments of add_partial_subprogram on how
6896 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6897
72bf9492
DJ
6898static void
6899scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6900 CORE_ADDR *highpc, int set_addrmap,
6901 struct dwarf2_cu *cu)
c906108c 6902{
72bf9492 6903 struct partial_die_info *pdi;
c906108c 6904
91c24f0a
DC
6905 /* Now, march along the PDI's, descending into ones which have
6906 interesting children but skipping the children of the other ones,
6907 until we reach the end of the compilation unit. */
c906108c 6908
72bf9492 6909 pdi = first_die;
91c24f0a 6910
72bf9492
DJ
6911 while (pdi != NULL)
6912 {
6913 fixup_partial_die (pdi, cu);
c906108c 6914
f55ee35c 6915 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6916 children, so we need to look at them. Ditto for anonymous
6917 enums. */
933c6fe4 6918
72bf9492 6919 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6920 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6921 || pdi->tag == DW_TAG_imported_unit)
c906108c 6922 {
72bf9492 6923 switch (pdi->tag)
c906108c
SS
6924 {
6925 case DW_TAG_subprogram:
cdc07690 6926 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6927 break;
72929c62 6928 case DW_TAG_constant:
c906108c
SS
6929 case DW_TAG_variable:
6930 case DW_TAG_typedef:
91c24f0a 6931 case DW_TAG_union_type:
72bf9492 6932 if (!pdi->is_declaration)
63d06c5c 6933 {
72bf9492 6934 add_partial_symbol (pdi, cu);
63d06c5c
DC
6935 }
6936 break;
c906108c 6937 case DW_TAG_class_type:
680b30c7 6938 case DW_TAG_interface_type:
c906108c 6939 case DW_TAG_structure_type:
72bf9492 6940 if (!pdi->is_declaration)
c906108c 6941 {
72bf9492 6942 add_partial_symbol (pdi, cu);
c906108c 6943 }
e98c9e7c
TT
6944 if (cu->language == language_rust && pdi->has_children)
6945 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6946 set_addrmap, cu);
c906108c 6947 break;
91c24f0a 6948 case DW_TAG_enumeration_type:
72bf9492
DJ
6949 if (!pdi->is_declaration)
6950 add_partial_enumeration (pdi, cu);
c906108c
SS
6951 break;
6952 case DW_TAG_base_type:
a02abb62 6953 case DW_TAG_subrange_type:
c906108c 6954 /* File scope base type definitions are added to the partial
c5aa993b 6955 symbol table. */
72bf9492 6956 add_partial_symbol (pdi, cu);
c906108c 6957 break;
d9fa45fe 6958 case DW_TAG_namespace:
cdc07690 6959 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6960 break;
5d7cb8df 6961 case DW_TAG_module:
cdc07690 6962 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6963 break;
95554aad
TT
6964 case DW_TAG_imported_unit:
6965 {
6966 struct dwarf2_per_cu_data *per_cu;
6967
f4dc4d17
DE
6968 /* For now we don't handle imported units in type units. */
6969 if (cu->per_cu->is_debug_types)
6970 {
6971 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6972 " supported in type units [in module %s]"),
4262abfb 6973 objfile_name (cu->objfile));
f4dc4d17
DE
6974 }
6975
9c541725 6976 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6977 pdi->is_dwz,
95554aad
TT
6978 cu->objfile);
6979
6980 /* Go read the partial unit, if needed. */
6981 if (per_cu->v.psymtab == NULL)
b93601f3 6982 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6983
f4dc4d17 6984 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6985 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6986 }
6987 break;
74921315
KS
6988 case DW_TAG_imported_declaration:
6989 add_partial_symbol (pdi, cu);
6990 break;
c906108c
SS
6991 default:
6992 break;
6993 }
6994 }
6995
72bf9492
DJ
6996 /* If the die has a sibling, skip to the sibling. */
6997
6998 pdi = pdi->die_sibling;
6999 }
7000}
7001
7002/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7003
72bf9492 7004 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7005 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7006 Enumerators are an exception; they use the scope of their parent
7007 enumeration type, i.e. the name of the enumeration type is not
7008 prepended to the enumerator.
91c24f0a 7009
72bf9492
DJ
7010 There are two complexities. One is DW_AT_specification; in this
7011 case "parent" means the parent of the target of the specification,
7012 instead of the direct parent of the DIE. The other is compilers
7013 which do not emit DW_TAG_namespace; in this case we try to guess
7014 the fully qualified name of structure types from their members'
7015 linkage names. This must be done using the DIE's children rather
7016 than the children of any DW_AT_specification target. We only need
7017 to do this for structures at the top level, i.e. if the target of
7018 any DW_AT_specification (if any; otherwise the DIE itself) does not
7019 have a parent. */
7020
7021/* Compute the scope prefix associated with PDI's parent, in
7022 compilation unit CU. The result will be allocated on CU's
7023 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7024 field. NULL is returned if no prefix is necessary. */
15d034d0 7025static const char *
72bf9492
DJ
7026partial_die_parent_scope (struct partial_die_info *pdi,
7027 struct dwarf2_cu *cu)
7028{
15d034d0 7029 const char *grandparent_scope;
72bf9492 7030 struct partial_die_info *parent, *real_pdi;
91c24f0a 7031
72bf9492
DJ
7032 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7033 then this means the parent of the specification DIE. */
7034
7035 real_pdi = pdi;
72bf9492 7036 while (real_pdi->has_specification)
36586728
TT
7037 real_pdi = find_partial_die (real_pdi->spec_offset,
7038 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7039
7040 parent = real_pdi->die_parent;
7041 if (parent == NULL)
7042 return NULL;
7043
7044 if (parent->scope_set)
7045 return parent->scope;
7046
7047 fixup_partial_die (parent, cu);
7048
10b3939b 7049 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7050
acebe513
UW
7051 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7052 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7053 Work around this problem here. */
7054 if (cu->language == language_cplus
6e70227d 7055 && parent->tag == DW_TAG_namespace
acebe513
UW
7056 && strcmp (parent->name, "::") == 0
7057 && grandparent_scope == NULL)
7058 {
7059 parent->scope = NULL;
7060 parent->scope_set = 1;
7061 return NULL;
7062 }
7063
9c6c53f7
SA
7064 if (pdi->tag == DW_TAG_enumerator)
7065 /* Enumerators should not get the name of the enumeration as a prefix. */
7066 parent->scope = grandparent_scope;
7067 else if (parent->tag == DW_TAG_namespace
f55ee35c 7068 || parent->tag == DW_TAG_module
72bf9492
DJ
7069 || parent->tag == DW_TAG_structure_type
7070 || parent->tag == DW_TAG_class_type
680b30c7 7071 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7072 || parent->tag == DW_TAG_union_type
7073 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7074 {
7075 if (grandparent_scope == NULL)
7076 parent->scope = parent->name;
7077 else
3e43a32a
MS
7078 parent->scope = typename_concat (&cu->comp_unit_obstack,
7079 grandparent_scope,
f55ee35c 7080 parent->name, 0, cu);
72bf9492 7081 }
72bf9492
DJ
7082 else
7083 {
7084 /* FIXME drow/2004-04-01: What should we be doing with
7085 function-local names? For partial symbols, we should probably be
7086 ignoring them. */
7087 complaint (&symfile_complaints,
e2e0b3e5 7088 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7089 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7090 parent->scope = grandparent_scope;
c906108c
SS
7091 }
7092
72bf9492
DJ
7093 parent->scope_set = 1;
7094 return parent->scope;
7095}
7096
7097/* Return the fully scoped name associated with PDI, from compilation unit
7098 CU. The result will be allocated with malloc. */
4568ecf9 7099
72bf9492
DJ
7100static char *
7101partial_die_full_name (struct partial_die_info *pdi,
7102 struct dwarf2_cu *cu)
7103{
15d034d0 7104 const char *parent_scope;
72bf9492 7105
98bfdba5
PA
7106 /* If this is a template instantiation, we can not work out the
7107 template arguments from partial DIEs. So, unfortunately, we have
7108 to go through the full DIEs. At least any work we do building
7109 types here will be reused if full symbols are loaded later. */
7110 if (pdi->has_template_arguments)
7111 {
7112 fixup_partial_die (pdi, cu);
7113
7114 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7115 {
7116 struct die_info *die;
7117 struct attribute attr;
7118 struct dwarf2_cu *ref_cu = cu;
7119
b64f50a1 7120 /* DW_FORM_ref_addr is using section offset. */
b4069958 7121 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7122 attr.form = DW_FORM_ref_addr;
9c541725 7123 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7124 die = follow_die_ref (NULL, &attr, &ref_cu);
7125
7126 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7127 }
7128 }
7129
72bf9492
DJ
7130 parent_scope = partial_die_parent_scope (pdi, cu);
7131 if (parent_scope == NULL)
7132 return NULL;
7133 else
f55ee35c 7134 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7135}
7136
7137static void
72bf9492 7138add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7139{
e7c27a73 7140 struct objfile *objfile = cu->objfile;
3e29f34a 7141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7142 CORE_ADDR addr = 0;
15d034d0 7143 const char *actual_name = NULL;
e142c38c 7144 CORE_ADDR baseaddr;
15d034d0 7145 char *built_actual_name;
e142c38c
DJ
7146
7147 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7148
15d034d0
TT
7149 built_actual_name = partial_die_full_name (pdi, cu);
7150 if (built_actual_name != NULL)
7151 actual_name = built_actual_name;
63d06c5c 7152
72bf9492
DJ
7153 if (actual_name == NULL)
7154 actual_name = pdi->name;
7155
c906108c
SS
7156 switch (pdi->tag)
7157 {
7158 case DW_TAG_subprogram:
3e29f34a 7159 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7160 if (pdi->is_external || cu->language == language_ada)
c906108c 7161 {
2cfa0c8d
JB
7162 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7163 of the global scope. But in Ada, we want to be able to access
7164 nested procedures globally. So all Ada subprograms are stored
7165 in the global scope. */
f47fb265 7166 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7167 built_actual_name != NULL,
f47fb265
MS
7168 VAR_DOMAIN, LOC_BLOCK,
7169 &objfile->global_psymbols,
1762568f 7170 addr, cu->language, objfile);
c906108c
SS
7171 }
7172 else
7173 {
f47fb265 7174 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7175 built_actual_name != NULL,
f47fb265
MS
7176 VAR_DOMAIN, LOC_BLOCK,
7177 &objfile->static_psymbols,
1762568f 7178 addr, cu->language, objfile);
c906108c 7179 }
0c1b455e
TT
7180
7181 if (pdi->main_subprogram && actual_name != NULL)
7182 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7183 break;
72929c62
JB
7184 case DW_TAG_constant:
7185 {
7186 struct psymbol_allocation_list *list;
7187
7188 if (pdi->is_external)
7189 list = &objfile->global_psymbols;
7190 else
7191 list = &objfile->static_psymbols;
f47fb265 7192 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7193 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7194 list, 0, cu->language, objfile);
72929c62
JB
7195 }
7196 break;
c906108c 7197 case DW_TAG_variable:
95554aad
TT
7198 if (pdi->d.locdesc)
7199 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7200
95554aad 7201 if (pdi->d.locdesc
caac4577
JG
7202 && addr == 0
7203 && !dwarf2_per_objfile->has_section_at_zero)
7204 {
7205 /* A global or static variable may also have been stripped
7206 out by the linker if unused, in which case its address
7207 will be nullified; do not add such variables into partial
7208 symbol table then. */
7209 }
7210 else if (pdi->is_external)
c906108c
SS
7211 {
7212 /* Global Variable.
7213 Don't enter into the minimal symbol tables as there is
7214 a minimal symbol table entry from the ELF symbols already.
7215 Enter into partial symbol table if it has a location
7216 descriptor or a type.
7217 If the location descriptor is missing, new_symbol will create
7218 a LOC_UNRESOLVED symbol, the address of the variable will then
7219 be determined from the minimal symbol table whenever the variable
7220 is referenced.
7221 The address for the partial symbol table entry is not
7222 used by GDB, but it comes in handy for debugging partial symbol
7223 table building. */
7224
95554aad 7225 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7226 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7227 built_actual_name != NULL,
f47fb265
MS
7228 VAR_DOMAIN, LOC_STATIC,
7229 &objfile->global_psymbols,
1762568f 7230 addr + baseaddr,
f47fb265 7231 cu->language, objfile);
c906108c
SS
7232 }
7233 else
7234 {
ff908ebf
AW
7235 int has_loc = pdi->d.locdesc != NULL;
7236
7237 /* Static Variable. Skip symbols whose value we cannot know (those
7238 without location descriptors or constant values). */
7239 if (!has_loc && !pdi->has_const_value)
decbce07 7240 {
15d034d0 7241 xfree (built_actual_name);
decbce07
MS
7242 return;
7243 }
ff908ebf 7244
f47fb265 7245 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7246 built_actual_name != NULL,
f47fb265
MS
7247 VAR_DOMAIN, LOC_STATIC,
7248 &objfile->static_psymbols,
ff908ebf 7249 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7250 cu->language, objfile);
c906108c
SS
7251 }
7252 break;
7253 case DW_TAG_typedef:
7254 case DW_TAG_base_type:
a02abb62 7255 case DW_TAG_subrange_type:
38d518c9 7256 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7257 built_actual_name != NULL,
176620f1 7258 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7259 &objfile->static_psymbols,
1762568f 7260 0, cu->language, objfile);
c906108c 7261 break;
74921315 7262 case DW_TAG_imported_declaration:
72bf9492
DJ
7263 case DW_TAG_namespace:
7264 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7265 built_actual_name != NULL,
72bf9492
DJ
7266 VAR_DOMAIN, LOC_TYPEDEF,
7267 &objfile->global_psymbols,
1762568f 7268 0, cu->language, objfile);
72bf9492 7269 break;
530e8392
KB
7270 case DW_TAG_module:
7271 add_psymbol_to_list (actual_name, strlen (actual_name),
7272 built_actual_name != NULL,
7273 MODULE_DOMAIN, LOC_TYPEDEF,
7274 &objfile->global_psymbols,
1762568f 7275 0, cu->language, objfile);
530e8392 7276 break;
c906108c 7277 case DW_TAG_class_type:
680b30c7 7278 case DW_TAG_interface_type:
c906108c
SS
7279 case DW_TAG_structure_type:
7280 case DW_TAG_union_type:
7281 case DW_TAG_enumeration_type:
fa4028e9
JB
7282 /* Skip external references. The DWARF standard says in the section
7283 about "Structure, Union, and Class Type Entries": "An incomplete
7284 structure, union or class type is represented by a structure,
7285 union or class entry that does not have a byte size attribute
7286 and that has a DW_AT_declaration attribute." */
7287 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7288 {
15d034d0 7289 xfree (built_actual_name);
decbce07
MS
7290 return;
7291 }
fa4028e9 7292
63d06c5c
DC
7293 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7294 static vs. global. */
38d518c9 7295 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7296 built_actual_name != NULL,
176620f1 7297 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7298 cu->language == language_cplus
63d06c5c
DC
7299 ? &objfile->global_psymbols
7300 : &objfile->static_psymbols,
1762568f 7301 0, cu->language, objfile);
c906108c 7302
c906108c
SS
7303 break;
7304 case DW_TAG_enumerator:
38d518c9 7305 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7306 built_actual_name != NULL,
176620f1 7307 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7308 cu->language == language_cplus
f6fe98ef
DJ
7309 ? &objfile->global_psymbols
7310 : &objfile->static_psymbols,
1762568f 7311 0, cu->language, objfile);
c906108c
SS
7312 break;
7313 default:
7314 break;
7315 }
5c4e30ca 7316
15d034d0 7317 xfree (built_actual_name);
c906108c
SS
7318}
7319
5c4e30ca
DC
7320/* Read a partial die corresponding to a namespace; also, add a symbol
7321 corresponding to that namespace to the symbol table. NAMESPACE is
7322 the name of the enclosing namespace. */
91c24f0a 7323
72bf9492
DJ
7324static void
7325add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7327 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7328{
72bf9492 7329 /* Add a symbol for the namespace. */
e7c27a73 7330
72bf9492 7331 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7332
7333 /* Now scan partial symbols in that namespace. */
7334
91c24f0a 7335 if (pdi->has_children)
cdc07690 7336 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7337}
7338
5d7cb8df
JK
7339/* Read a partial die corresponding to a Fortran module. */
7340
7341static void
7342add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7343 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7344{
530e8392
KB
7345 /* Add a symbol for the namespace. */
7346
7347 add_partial_symbol (pdi, cu);
7348
f55ee35c 7349 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7350
7351 if (pdi->has_children)
cdc07690 7352 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7353}
7354
bc30ff58
JB
7355/* Read a partial die corresponding to a subprogram and create a partial
7356 symbol for that subprogram. When the CU language allows it, this
7357 routine also defines a partial symbol for each nested subprogram
cdc07690 7358 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7359 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7360 and highest PC values found in PDI.
6e70227d 7361
cdc07690
YQ
7362 PDI may also be a lexical block, in which case we simply search
7363 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7364 Again, this is only performed when the CU language allows this
7365 type of definitions. */
7366
7367static void
7368add_partial_subprogram (struct partial_die_info *pdi,
7369 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7370 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7371{
7372 if (pdi->tag == DW_TAG_subprogram)
7373 {
7374 if (pdi->has_pc_info)
7375 {
7376 if (pdi->lowpc < *lowpc)
7377 *lowpc = pdi->lowpc;
7378 if (pdi->highpc > *highpc)
7379 *highpc = pdi->highpc;
cdc07690 7380 if (set_addrmap)
5734ee8b 7381 {
5734ee8b 7382 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7384 CORE_ADDR baseaddr;
7385 CORE_ADDR highpc;
7386 CORE_ADDR lowpc;
5734ee8b
DJ
7387
7388 baseaddr = ANOFFSET (objfile->section_offsets,
7389 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7390 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7391 pdi->lowpc + baseaddr);
7392 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7393 pdi->highpc + baseaddr);
7394 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7395 cu->per_cu->v.psymtab);
5734ee8b 7396 }
481860b3
GB
7397 }
7398
7399 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7400 {
bc30ff58 7401 if (!pdi->is_declaration)
e8d05480
JB
7402 /* Ignore subprogram DIEs that do not have a name, they are
7403 illegal. Do not emit a complaint at this point, we will
7404 do so when we convert this psymtab into a symtab. */
7405 if (pdi->name)
7406 add_partial_symbol (pdi, cu);
bc30ff58
JB
7407 }
7408 }
6e70227d 7409
bc30ff58
JB
7410 if (! pdi->has_children)
7411 return;
7412
7413 if (cu->language == language_ada)
7414 {
7415 pdi = pdi->die_child;
7416 while (pdi != NULL)
7417 {
7418 fixup_partial_die (pdi, cu);
7419 if (pdi->tag == DW_TAG_subprogram
7420 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7421 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7422 pdi = pdi->die_sibling;
7423 }
7424 }
7425}
7426
91c24f0a
DC
7427/* Read a partial die corresponding to an enumeration type. */
7428
72bf9492
DJ
7429static void
7430add_partial_enumeration (struct partial_die_info *enum_pdi,
7431 struct dwarf2_cu *cu)
91c24f0a 7432{
72bf9492 7433 struct partial_die_info *pdi;
91c24f0a
DC
7434
7435 if (enum_pdi->name != NULL)
72bf9492
DJ
7436 add_partial_symbol (enum_pdi, cu);
7437
7438 pdi = enum_pdi->die_child;
7439 while (pdi)
91c24f0a 7440 {
72bf9492 7441 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7442 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7443 else
72bf9492
DJ
7444 add_partial_symbol (pdi, cu);
7445 pdi = pdi->die_sibling;
91c24f0a 7446 }
91c24f0a
DC
7447}
7448
6caca83c
CC
7449/* Return the initial uleb128 in the die at INFO_PTR. */
7450
7451static unsigned int
d521ce57 7452peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7453{
7454 unsigned int bytes_read;
7455
7456 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7457}
7458
4bb7a0a7
DJ
7459/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7460 Return the corresponding abbrev, or NULL if the number is zero (indicating
7461 an empty DIE). In either case *BYTES_READ will be set to the length of
7462 the initial number. */
7463
7464static struct abbrev_info *
d521ce57 7465peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7466 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7467{
7468 bfd *abfd = cu->objfile->obfd;
7469 unsigned int abbrev_number;
7470 struct abbrev_info *abbrev;
7471
7472 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7473
7474 if (abbrev_number == 0)
7475 return NULL;
7476
433df2d4 7477 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7478 if (!abbrev)
7479 {
422b9917
DE
7480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7481 " at offset 0x%x [in module %s]"),
7482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7483 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7484 }
7485
7486 return abbrev;
7487}
7488
93311388
DE
7489/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7490 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7491 DIE. Any children of the skipped DIEs will also be skipped. */
7492
d521ce57
TT
7493static const gdb_byte *
7494skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7495{
dee91e82 7496 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7497 struct abbrev_info *abbrev;
7498 unsigned int bytes_read;
7499
7500 while (1)
7501 {
7502 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7503 if (abbrev == NULL)
7504 return info_ptr + bytes_read;
7505 else
dee91e82 7506 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7507 }
7508}
7509
93311388
DE
7510/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7511 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7512 abbrev corresponding to that skipped uleb128 should be passed in
7513 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7514 children. */
7515
d521ce57
TT
7516static const gdb_byte *
7517skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7518 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7519{
7520 unsigned int bytes_read;
7521 struct attribute attr;
dee91e82
DE
7522 bfd *abfd = reader->abfd;
7523 struct dwarf2_cu *cu = reader->cu;
d521ce57 7524 const gdb_byte *buffer = reader->buffer;
f664829e 7525 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7526 unsigned int form, i;
7527
7528 for (i = 0; i < abbrev->num_attrs; i++)
7529 {
7530 /* The only abbrev we care about is DW_AT_sibling. */
7531 if (abbrev->attrs[i].name == DW_AT_sibling)
7532 {
dee91e82 7533 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7534 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7535 complaint (&symfile_complaints,
7536 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7537 else
b9502d3f 7538 {
9c541725
PA
7539 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7540 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7541
7542 if (sibling_ptr < info_ptr)
7543 complaint (&symfile_complaints,
7544 _("DW_AT_sibling points backwards"));
22869d73
KS
7545 else if (sibling_ptr > reader->buffer_end)
7546 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7547 else
7548 return sibling_ptr;
7549 }
4bb7a0a7
DJ
7550 }
7551
7552 /* If it isn't DW_AT_sibling, skip this attribute. */
7553 form = abbrev->attrs[i].form;
7554 skip_attribute:
7555 switch (form)
7556 {
4bb7a0a7 7557 case DW_FORM_ref_addr:
ae411497
TT
7558 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7559 and later it is offset sized. */
7560 if (cu->header.version == 2)
7561 info_ptr += cu->header.addr_size;
7562 else
7563 info_ptr += cu->header.offset_size;
7564 break;
36586728
TT
7565 case DW_FORM_GNU_ref_alt:
7566 info_ptr += cu->header.offset_size;
7567 break;
ae411497 7568 case DW_FORM_addr:
4bb7a0a7
DJ
7569 info_ptr += cu->header.addr_size;
7570 break;
7571 case DW_FORM_data1:
7572 case DW_FORM_ref1:
7573 case DW_FORM_flag:
7574 info_ptr += 1;
7575 break;
2dc7f7b3 7576 case DW_FORM_flag_present:
43988095 7577 case DW_FORM_implicit_const:
2dc7f7b3 7578 break;
4bb7a0a7
DJ
7579 case DW_FORM_data2:
7580 case DW_FORM_ref2:
7581 info_ptr += 2;
7582 break;
7583 case DW_FORM_data4:
7584 case DW_FORM_ref4:
7585 info_ptr += 4;
7586 break;
7587 case DW_FORM_data8:
7588 case DW_FORM_ref8:
55f1336d 7589 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7590 info_ptr += 8;
7591 break;
0224619f
JK
7592 case DW_FORM_data16:
7593 info_ptr += 16;
7594 break;
4bb7a0a7 7595 case DW_FORM_string:
9b1c24c8 7596 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7597 info_ptr += bytes_read;
7598 break;
2dc7f7b3 7599 case DW_FORM_sec_offset:
4bb7a0a7 7600 case DW_FORM_strp:
36586728 7601 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7602 info_ptr += cu->header.offset_size;
7603 break;
2dc7f7b3 7604 case DW_FORM_exprloc:
4bb7a0a7
DJ
7605 case DW_FORM_block:
7606 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7607 info_ptr += bytes_read;
7608 break;
7609 case DW_FORM_block1:
7610 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7611 break;
7612 case DW_FORM_block2:
7613 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7614 break;
7615 case DW_FORM_block4:
7616 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7617 break;
7618 case DW_FORM_sdata:
7619 case DW_FORM_udata:
7620 case DW_FORM_ref_udata:
3019eac3
DE
7621 case DW_FORM_GNU_addr_index:
7622 case DW_FORM_GNU_str_index:
d521ce57 7623 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7624 break;
7625 case DW_FORM_indirect:
7626 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7627 info_ptr += bytes_read;
7628 /* We need to continue parsing from here, so just go back to
7629 the top. */
7630 goto skip_attribute;
7631
7632 default:
3e43a32a
MS
7633 error (_("Dwarf Error: Cannot handle %s "
7634 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7635 dwarf_form_name (form),
7636 bfd_get_filename (abfd));
7637 }
7638 }
7639
7640 if (abbrev->has_children)
dee91e82 7641 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7642 else
7643 return info_ptr;
7644}
7645
93311388 7646/* Locate ORIG_PDI's sibling.
dee91e82 7647 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7648
d521ce57 7649static const gdb_byte *
dee91e82
DE
7650locate_pdi_sibling (const struct die_reader_specs *reader,
7651 struct partial_die_info *orig_pdi,
d521ce57 7652 const gdb_byte *info_ptr)
91c24f0a
DC
7653{
7654 /* Do we know the sibling already? */
72bf9492 7655
91c24f0a
DC
7656 if (orig_pdi->sibling)
7657 return orig_pdi->sibling;
7658
7659 /* Are there any children to deal with? */
7660
7661 if (!orig_pdi->has_children)
7662 return info_ptr;
7663
4bb7a0a7 7664 /* Skip the children the long way. */
91c24f0a 7665
dee91e82 7666 return skip_children (reader, info_ptr);
91c24f0a
DC
7667}
7668
257e7a09 7669/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7670 not NULL. */
c906108c
SS
7671
7672static void
257e7a09
YQ
7673dwarf2_read_symtab (struct partial_symtab *self,
7674 struct objfile *objfile)
c906108c 7675{
257e7a09 7676 if (self->readin)
c906108c 7677 {
442e4d9c 7678 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7679 self->filename);
442e4d9c
YQ
7680 }
7681 else
7682 {
7683 if (info_verbose)
c906108c 7684 {
442e4d9c 7685 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7686 self->filename);
442e4d9c 7687 gdb_flush (gdb_stdout);
c906108c 7688 }
c906108c 7689
442e4d9c 7690 /* Restore our global data. */
9a3c8263
SM
7691 dwarf2_per_objfile
7692 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7693 dwarf2_objfile_data_key);
10b3939b 7694
442e4d9c
YQ
7695 /* If this psymtab is constructed from a debug-only objfile, the
7696 has_section_at_zero flag will not necessarily be correct. We
7697 can get the correct value for this flag by looking at the data
7698 associated with the (presumably stripped) associated objfile. */
7699 if (objfile->separate_debug_objfile_backlink)
7700 {
7701 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7702 = ((struct dwarf2_per_objfile *)
7703 objfile_data (objfile->separate_debug_objfile_backlink,
7704 dwarf2_objfile_data_key));
9a619af0 7705
442e4d9c
YQ
7706 dwarf2_per_objfile->has_section_at_zero
7707 = dpo_backlink->has_section_at_zero;
7708 }
b2ab525c 7709
442e4d9c 7710 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7711
257e7a09 7712 psymtab_to_symtab_1 (self);
c906108c 7713
442e4d9c
YQ
7714 /* Finish up the debug error message. */
7715 if (info_verbose)
7716 printf_filtered (_("done.\n"));
c906108c 7717 }
95554aad
TT
7718
7719 process_cu_includes ();
c906108c 7720}
9cdd5dbd
DE
7721\f
7722/* Reading in full CUs. */
c906108c 7723
10b3939b
DJ
7724/* Add PER_CU to the queue. */
7725
7726static void
95554aad
TT
7727queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7728 enum language pretend_language)
10b3939b
DJ
7729{
7730 struct dwarf2_queue_item *item;
7731
7732 per_cu->queued = 1;
8d749320 7733 item = XNEW (struct dwarf2_queue_item);
10b3939b 7734 item->per_cu = per_cu;
95554aad 7735 item->pretend_language = pretend_language;
10b3939b
DJ
7736 item->next = NULL;
7737
7738 if (dwarf2_queue == NULL)
7739 dwarf2_queue = item;
7740 else
7741 dwarf2_queue_tail->next = item;
7742
7743 dwarf2_queue_tail = item;
7744}
7745
89e63ee4
DE
7746/* If PER_CU is not yet queued, add it to the queue.
7747 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7748 dependency.
0907af0c 7749 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7750 meaning either PER_CU is already queued or it is already loaded.
7751
7752 N.B. There is an invariant here that if a CU is queued then it is loaded.
7753 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7754
7755static int
89e63ee4 7756maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7757 struct dwarf2_per_cu_data *per_cu,
7758 enum language pretend_language)
7759{
7760 /* We may arrive here during partial symbol reading, if we need full
7761 DIEs to process an unusual case (e.g. template arguments). Do
7762 not queue PER_CU, just tell our caller to load its DIEs. */
7763 if (dwarf2_per_objfile->reading_partial_symbols)
7764 {
7765 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7766 return 1;
7767 return 0;
7768 }
7769
7770 /* Mark the dependence relation so that we don't flush PER_CU
7771 too early. */
89e63ee4
DE
7772 if (dependent_cu != NULL)
7773 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7774
7775 /* If it's already on the queue, we have nothing to do. */
7776 if (per_cu->queued)
7777 return 0;
7778
7779 /* If the compilation unit is already loaded, just mark it as
7780 used. */
7781 if (per_cu->cu != NULL)
7782 {
7783 per_cu->cu->last_used = 0;
7784 return 0;
7785 }
7786
7787 /* Add it to the queue. */
7788 queue_comp_unit (per_cu, pretend_language);
7789
7790 return 1;
7791}
7792
10b3939b
DJ
7793/* Process the queue. */
7794
7795static void
a0f42c21 7796process_queue (void)
10b3939b
DJ
7797{
7798 struct dwarf2_queue_item *item, *next_item;
7799
b4f54984 7800 if (dwarf_read_debug)
45cfd468
DE
7801 {
7802 fprintf_unfiltered (gdb_stdlog,
7803 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7804 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7805 }
7806
03dd20cc
DJ
7807 /* The queue starts out with one item, but following a DIE reference
7808 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7809 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7810 {
cc12ce38
DE
7811 if ((dwarf2_per_objfile->using_index
7812 ? !item->per_cu->v.quick->compunit_symtab
7813 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7814 /* Skip dummy CUs. */
7815 && item->per_cu->cu != NULL)
f4dc4d17
DE
7816 {
7817 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7818 unsigned int debug_print_threshold;
247f5c4f 7819 char buf[100];
f4dc4d17 7820
247f5c4f 7821 if (per_cu->is_debug_types)
f4dc4d17 7822 {
247f5c4f
DE
7823 struct signatured_type *sig_type =
7824 (struct signatured_type *) per_cu;
7825
7826 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7827 hex_string (sig_type->signature),
9c541725 7828 to_underlying (per_cu->sect_off));
73be47f5
DE
7829 /* There can be 100s of TUs.
7830 Only print them in verbose mode. */
7831 debug_print_threshold = 2;
f4dc4d17 7832 }
247f5c4f 7833 else
73be47f5 7834 {
9c541725
PA
7835 sprintf (buf, "CU at offset 0x%x",
7836 to_underlying (per_cu->sect_off));
73be47f5
DE
7837 debug_print_threshold = 1;
7838 }
247f5c4f 7839
b4f54984 7840 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7841 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7842
7843 if (per_cu->is_debug_types)
7844 process_full_type_unit (per_cu, item->pretend_language);
7845 else
7846 process_full_comp_unit (per_cu, item->pretend_language);
7847
b4f54984 7848 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7849 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7850 }
10b3939b
DJ
7851
7852 item->per_cu->queued = 0;
7853 next_item = item->next;
7854 xfree (item);
7855 }
7856
7857 dwarf2_queue_tail = NULL;
45cfd468 7858
b4f54984 7859 if (dwarf_read_debug)
45cfd468
DE
7860 {
7861 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7862 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7863 }
10b3939b
DJ
7864}
7865
7866/* Free all allocated queue entries. This function only releases anything if
7867 an error was thrown; if the queue was processed then it would have been
7868 freed as we went along. */
7869
7870static void
7871dwarf2_release_queue (void *dummy)
7872{
7873 struct dwarf2_queue_item *item, *last;
7874
7875 item = dwarf2_queue;
7876 while (item)
7877 {
7878 /* Anything still marked queued is likely to be in an
7879 inconsistent state, so discard it. */
7880 if (item->per_cu->queued)
7881 {
7882 if (item->per_cu->cu != NULL)
dee91e82 7883 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7884 item->per_cu->queued = 0;
7885 }
7886
7887 last = item;
7888 item = item->next;
7889 xfree (last);
7890 }
7891
7892 dwarf2_queue = dwarf2_queue_tail = NULL;
7893}
7894
7895/* Read in full symbols for PST, and anything it depends on. */
7896
c906108c 7897static void
fba45db2 7898psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7899{
10b3939b 7900 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7901 int i;
7902
95554aad
TT
7903 if (pst->readin)
7904 return;
7905
aaa75496 7906 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7907 if (!pst->dependencies[i]->readin
7908 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7909 {
7910 /* Inform about additional files that need to be read in. */
7911 if (info_verbose)
7912 {
a3f17187 7913 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7914 fputs_filtered (" ", gdb_stdout);
7915 wrap_here ("");
7916 fputs_filtered ("and ", gdb_stdout);
7917 wrap_here ("");
7918 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7919 wrap_here (""); /* Flush output. */
aaa75496
JB
7920 gdb_flush (gdb_stdout);
7921 }
7922 psymtab_to_symtab_1 (pst->dependencies[i]);
7923 }
7924
9a3c8263 7925 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7926
7927 if (per_cu == NULL)
aaa75496
JB
7928 {
7929 /* It's an include file, no symbols to read for it.
7930 Everything is in the parent symtab. */
7931 pst->readin = 1;
7932 return;
7933 }
c906108c 7934
a0f42c21 7935 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7936}
7937
dee91e82
DE
7938/* Trivial hash function for die_info: the hash value of a DIE
7939 is its offset in .debug_info for this objfile. */
10b3939b 7940
dee91e82
DE
7941static hashval_t
7942die_hash (const void *item)
10b3939b 7943{
9a3c8263 7944 const struct die_info *die = (const struct die_info *) item;
6502dd73 7945
9c541725 7946 return to_underlying (die->sect_off);
dee91e82 7947}
63d06c5c 7948
dee91e82
DE
7949/* Trivial comparison function for die_info structures: two DIEs
7950 are equal if they have the same offset. */
98bfdba5 7951
dee91e82
DE
7952static int
7953die_eq (const void *item_lhs, const void *item_rhs)
7954{
9a3c8263
SM
7955 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7956 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7957
9c541725 7958 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7959}
c906108c 7960
dee91e82
DE
7961/* die_reader_func for load_full_comp_unit.
7962 This is identical to read_signatured_type_reader,
7963 but is kept separate for now. */
c906108c 7964
dee91e82
DE
7965static void
7966load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7967 const gdb_byte *info_ptr,
dee91e82
DE
7968 struct die_info *comp_unit_die,
7969 int has_children,
7970 void *data)
7971{
7972 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7973 enum language *language_ptr = (enum language *) data;
6caca83c 7974
dee91e82
DE
7975 gdb_assert (cu->die_hash == NULL);
7976 cu->die_hash =
7977 htab_create_alloc_ex (cu->header.length / 12,
7978 die_hash,
7979 die_eq,
7980 NULL,
7981 &cu->comp_unit_obstack,
7982 hashtab_obstack_allocate,
7983 dummy_obstack_deallocate);
e142c38c 7984
dee91e82
DE
7985 if (has_children)
7986 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7987 &info_ptr, comp_unit_die);
7988 cu->dies = comp_unit_die;
7989 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7990
7991 /* We try not to read any attributes in this function, because not
9cdd5dbd 7992 all CUs needed for references have been loaded yet, and symbol
10b3939b 7993 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7994 or we won't be able to build types correctly.
7995 Similarly, if we do not read the producer, we can not apply
7996 producer-specific interpretation. */
95554aad 7997 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7998}
10b3939b 7999
dee91e82 8000/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8001
dee91e82 8002static void
95554aad
TT
8003load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8004 enum language pretend_language)
dee91e82 8005{
3019eac3 8006 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8007
f4dc4d17
DE
8008 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8009 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8010}
8011
3da10d80
KS
8012/* Add a DIE to the delayed physname list. */
8013
8014static void
8015add_to_method_list (struct type *type, int fnfield_index, int index,
8016 const char *name, struct die_info *die,
8017 struct dwarf2_cu *cu)
8018{
8019 struct delayed_method_info mi;
8020 mi.type = type;
8021 mi.fnfield_index = fnfield_index;
8022 mi.index = index;
8023 mi.name = name;
8024 mi.die = die;
8025 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8026}
8027
8028/* A cleanup for freeing the delayed method list. */
8029
8030static void
8031free_delayed_list (void *ptr)
8032{
8033 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8034 if (cu->method_list != NULL)
8035 {
8036 VEC_free (delayed_method_info, cu->method_list);
8037 cu->method_list = NULL;
8038 }
8039}
8040
3693fdb3
PA
8041/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8042 "const" / "volatile". If so, decrements LEN by the length of the
8043 modifier and return true. Otherwise return false. */
8044
8045template<size_t N>
8046static bool
8047check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8048{
8049 size_t mod_len = sizeof (mod) - 1;
8050 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8051 {
8052 len -= mod_len;
8053 return true;
8054 }
8055 return false;
8056}
8057
3da10d80
KS
8058/* Compute the physnames of any methods on the CU's method list.
8059
8060 The computation of method physnames is delayed in order to avoid the
8061 (bad) condition that one of the method's formal parameters is of an as yet
8062 incomplete type. */
8063
8064static void
8065compute_delayed_physnames (struct dwarf2_cu *cu)
8066{
8067 int i;
8068 struct delayed_method_info *mi;
3693fdb3
PA
8069
8070 /* Only C++ delays computing physnames. */
8071 if (VEC_empty (delayed_method_info, cu->method_list))
8072 return;
8073 gdb_assert (cu->language == language_cplus);
8074
3da10d80
KS
8075 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8076 {
1d06ead6 8077 const char *physname;
3da10d80
KS
8078 struct fn_fieldlist *fn_flp
8079 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8080 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8081 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8082 = physname ? physname : "";
3693fdb3
PA
8083
8084 /* Since there's no tag to indicate whether a method is a
8085 const/volatile overload, extract that information out of the
8086 demangled name. */
8087 if (physname != NULL)
8088 {
8089 size_t len = strlen (physname);
8090
8091 while (1)
8092 {
8093 if (physname[len] == ')') /* shortcut */
8094 break;
8095 else if (check_modifier (physname, len, " const"))
8096 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8097 else if (check_modifier (physname, len, " volatile"))
8098 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8099 else
8100 break;
8101 }
8102 }
3da10d80
KS
8103 }
8104}
8105
a766d390
DE
8106/* Go objects should be embedded in a DW_TAG_module DIE,
8107 and it's not clear if/how imported objects will appear.
8108 To keep Go support simple until that's worked out,
8109 go back through what we've read and create something usable.
8110 We could do this while processing each DIE, and feels kinda cleaner,
8111 but that way is more invasive.
8112 This is to, for example, allow the user to type "p var" or "b main"
8113 without having to specify the package name, and allow lookups
8114 of module.object to work in contexts that use the expression
8115 parser. */
8116
8117static void
8118fixup_go_packaging (struct dwarf2_cu *cu)
8119{
8120 char *package_name = NULL;
8121 struct pending *list;
8122 int i;
8123
8124 for (list = global_symbols; list != NULL; list = list->next)
8125 {
8126 for (i = 0; i < list->nsyms; ++i)
8127 {
8128 struct symbol *sym = list->symbol[i];
8129
8130 if (SYMBOL_LANGUAGE (sym) == language_go
8131 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8132 {
8133 char *this_package_name = go_symbol_package_name (sym);
8134
8135 if (this_package_name == NULL)
8136 continue;
8137 if (package_name == NULL)
8138 package_name = this_package_name;
8139 else
8140 {
8141 if (strcmp (package_name, this_package_name) != 0)
8142 complaint (&symfile_complaints,
8143 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8144 (symbol_symtab (sym) != NULL
8145 ? symtab_to_filename_for_display
8146 (symbol_symtab (sym))
4262abfb 8147 : objfile_name (cu->objfile)),
a766d390
DE
8148 this_package_name, package_name);
8149 xfree (this_package_name);
8150 }
8151 }
8152 }
8153 }
8154
8155 if (package_name != NULL)
8156 {
8157 struct objfile *objfile = cu->objfile;
34a68019 8158 const char *saved_package_name
224c3ddb
SM
8159 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8160 package_name,
8161 strlen (package_name));
19f392bc
UW
8162 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8163 saved_package_name);
a766d390
DE
8164 struct symbol *sym;
8165
8166 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8167
e623cf5d 8168 sym = allocate_symbol (objfile);
f85f34ed 8169 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8170 SYMBOL_SET_NAMES (sym, saved_package_name,
8171 strlen (saved_package_name), 0, objfile);
a766d390
DE
8172 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8173 e.g., "main" finds the "main" module and not C's main(). */
8174 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8176 SYMBOL_TYPE (sym) = type;
8177
8178 add_symbol_to_list (sym, &global_symbols);
8179
8180 xfree (package_name);
8181 }
8182}
8183
95554aad
TT
8184/* Return the symtab for PER_CU. This works properly regardless of
8185 whether we're using the index or psymtabs. */
8186
43f3e411
DE
8187static struct compunit_symtab *
8188get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8189{
8190 return (dwarf2_per_objfile->using_index
43f3e411
DE
8191 ? per_cu->v.quick->compunit_symtab
8192 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8193}
8194
8195/* A helper function for computing the list of all symbol tables
8196 included by PER_CU. */
8197
8198static void
43f3e411 8199recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8200 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8201 struct dwarf2_per_cu_data *per_cu,
43f3e411 8202 struct compunit_symtab *immediate_parent)
95554aad
TT
8203{
8204 void **slot;
8205 int ix;
43f3e411 8206 struct compunit_symtab *cust;
95554aad
TT
8207 struct dwarf2_per_cu_data *iter;
8208
8209 slot = htab_find_slot (all_children, per_cu, INSERT);
8210 if (*slot != NULL)
8211 {
8212 /* This inclusion and its children have been processed. */
8213 return;
8214 }
8215
8216 *slot = per_cu;
8217 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8218 cust = get_compunit_symtab (per_cu);
8219 if (cust != NULL)
ec94af83
DE
8220 {
8221 /* If this is a type unit only add its symbol table if we haven't
8222 seen it yet (type unit per_cu's can share symtabs). */
8223 if (per_cu->is_debug_types)
8224 {
43f3e411 8225 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8226 if (*slot == NULL)
8227 {
43f3e411
DE
8228 *slot = cust;
8229 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8230 if (cust->user == NULL)
8231 cust->user = immediate_parent;
ec94af83
DE
8232 }
8233 }
8234 else
f9125b6c 8235 {
43f3e411
DE
8236 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8237 if (cust->user == NULL)
8238 cust->user = immediate_parent;
f9125b6c 8239 }
ec94af83 8240 }
95554aad
TT
8241
8242 for (ix = 0;
796a7ff8 8243 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8244 ++ix)
ec94af83
DE
8245 {
8246 recursively_compute_inclusions (result, all_children,
43f3e411 8247 all_type_symtabs, iter, cust);
ec94af83 8248 }
95554aad
TT
8249}
8250
43f3e411 8251/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8252 PER_CU. */
8253
8254static void
43f3e411 8255compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8256{
f4dc4d17
DE
8257 gdb_assert (! per_cu->is_debug_types);
8258
796a7ff8 8259 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8260 {
8261 int ix, len;
ec94af83 8262 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8263 struct compunit_symtab *compunit_symtab_iter;
8264 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8265 htab_t all_children, all_type_symtabs;
43f3e411 8266 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8267
8268 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8269 if (cust == NULL)
95554aad
TT
8270 return;
8271
8272 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8273 NULL, xcalloc, xfree);
ec94af83
DE
8274 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8275 NULL, xcalloc, xfree);
95554aad
TT
8276
8277 for (ix = 0;
796a7ff8 8278 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8279 ix, per_cu_iter);
95554aad 8280 ++ix)
ec94af83
DE
8281 {
8282 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8283 all_type_symtabs, per_cu_iter,
43f3e411 8284 cust);
ec94af83 8285 }
95554aad 8286
ec94af83 8287 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8288 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8289 cust->includes
8d749320
SM
8290 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8291 struct compunit_symtab *, len + 1);
95554aad 8292 for (ix = 0;
43f3e411
DE
8293 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8294 compunit_symtab_iter);
95554aad 8295 ++ix)
43f3e411
DE
8296 cust->includes[ix] = compunit_symtab_iter;
8297 cust->includes[len] = NULL;
95554aad 8298
43f3e411 8299 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8300 htab_delete (all_children);
ec94af83 8301 htab_delete (all_type_symtabs);
95554aad
TT
8302 }
8303}
8304
8305/* Compute the 'includes' field for the symtabs of all the CUs we just
8306 read. */
8307
8308static void
8309process_cu_includes (void)
8310{
8311 int ix;
8312 struct dwarf2_per_cu_data *iter;
8313
8314 for (ix = 0;
8315 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8316 ix, iter);
8317 ++ix)
f4dc4d17
DE
8318 {
8319 if (! iter->is_debug_types)
43f3e411 8320 compute_compunit_symtab_includes (iter);
f4dc4d17 8321 }
95554aad
TT
8322
8323 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8324}
8325
9cdd5dbd 8326/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8327 already been loaded into memory. */
8328
8329static void
95554aad
TT
8330process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8331 enum language pretend_language)
10b3939b 8332{
10b3939b 8333 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8334 struct objfile *objfile = per_cu->objfile;
3e29f34a 8335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8336 CORE_ADDR lowpc, highpc;
43f3e411 8337 struct compunit_symtab *cust;
3da10d80 8338 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8339 CORE_ADDR baseaddr;
4359dff1 8340 struct block *static_block;
3e29f34a 8341 CORE_ADDR addr;
10b3939b
DJ
8342
8343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8344
10b3939b
DJ
8345 buildsym_init ();
8346 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8347 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8348
8349 cu->list_in_scope = &file_symbols;
c906108c 8350
95554aad
TT
8351 cu->language = pretend_language;
8352 cu->language_defn = language_def (cu->language);
8353
c906108c 8354 /* Do line number decoding in read_file_scope () */
10b3939b 8355 process_die (cu->dies, cu);
c906108c 8356
a766d390
DE
8357 /* For now fudge the Go package. */
8358 if (cu->language == language_go)
8359 fixup_go_packaging (cu);
8360
3da10d80
KS
8361 /* Now that we have processed all the DIEs in the CU, all the types
8362 should be complete, and it should now be safe to compute all of the
8363 physnames. */
8364 compute_delayed_physnames (cu);
8365 do_cleanups (delayed_list_cleanup);
8366
fae299cd
DC
8367 /* Some compilers don't define a DW_AT_high_pc attribute for the
8368 compilation unit. If the DW_AT_high_pc is missing, synthesize
8369 it, by scanning the DIE's below the compilation unit. */
10b3939b 8370 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8371
3e29f34a
MR
8372 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8373 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8374
8375 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8376 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8377 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8378 addrmap to help ensure it has an accurate map of pc values belonging to
8379 this comp unit. */
8380 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8381
43f3e411
DE
8382 cust = end_symtab_from_static_block (static_block,
8383 SECT_OFF_TEXT (objfile), 0);
c906108c 8384
43f3e411 8385 if (cust != NULL)
c906108c 8386 {
df15bd07 8387 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8388
8be455d7
JK
8389 /* Set symtab language to language from DW_AT_language. If the
8390 compilation is from a C file generated by language preprocessors, do
8391 not set the language if it was already deduced by start_subfile. */
43f3e411 8392 if (!(cu->language == language_c
40e3ad0e 8393 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8394 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8395
8396 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8397 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8398 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8399 there were bugs in prologue debug info, fixed later in GCC-4.5
8400 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8401
8402 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8403 needed, it would be wrong due to missing DW_AT_producer there.
8404
8405 Still one can confuse GDB by using non-standard GCC compilation
8406 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8407 */
ab260dad 8408 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8409 cust->locations_valid = 1;
e0d00bc7
JK
8410
8411 if (gcc_4_minor >= 5)
43f3e411 8412 cust->epilogue_unwind_valid = 1;
96408a79 8413
43f3e411 8414 cust->call_site_htab = cu->call_site_htab;
c906108c 8415 }
9291a0cd
TT
8416
8417 if (dwarf2_per_objfile->using_index)
43f3e411 8418 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8419 else
8420 {
8421 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8422 pst->compunit_symtab = cust;
9291a0cd
TT
8423 pst->readin = 1;
8424 }
c906108c 8425
95554aad
TT
8426 /* Push it for inclusion processing later. */
8427 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8428
c906108c 8429 do_cleanups (back_to);
f4dc4d17 8430}
45cfd468 8431
f4dc4d17
DE
8432/* Generate full symbol information for type unit PER_CU, whose DIEs have
8433 already been loaded into memory. */
8434
8435static void
8436process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8437 enum language pretend_language)
8438{
8439 struct dwarf2_cu *cu = per_cu->cu;
8440 struct objfile *objfile = per_cu->objfile;
43f3e411 8441 struct compunit_symtab *cust;
f4dc4d17 8442 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8443 struct signatured_type *sig_type;
8444
8445 gdb_assert (per_cu->is_debug_types);
8446 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8447
8448 buildsym_init ();
8449 back_to = make_cleanup (really_free_pendings, NULL);
8450 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8451
8452 cu->list_in_scope = &file_symbols;
8453
8454 cu->language = pretend_language;
8455 cu->language_defn = language_def (cu->language);
8456
8457 /* The symbol tables are set up in read_type_unit_scope. */
8458 process_die (cu->dies, cu);
8459
8460 /* For now fudge the Go package. */
8461 if (cu->language == language_go)
8462 fixup_go_packaging (cu);
8463
8464 /* Now that we have processed all the DIEs in the CU, all the types
8465 should be complete, and it should now be safe to compute all of the
8466 physnames. */
8467 compute_delayed_physnames (cu);
8468 do_cleanups (delayed_list_cleanup);
8469
8470 /* TUs share symbol tables.
8471 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8472 of it with end_expandable_symtab. Otherwise, complete the addition of
8473 this TU's symbols to the existing symtab. */
43f3e411 8474 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8475 {
43f3e411
DE
8476 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8477 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8478
43f3e411 8479 if (cust != NULL)
f4dc4d17
DE
8480 {
8481 /* Set symtab language to language from DW_AT_language. If the
8482 compilation is from a C file generated by language preprocessors,
8483 do not set the language if it was already deduced by
8484 start_subfile. */
43f3e411
DE
8485 if (!(cu->language == language_c
8486 && COMPUNIT_FILETABS (cust)->language != language_c))
8487 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8488 }
8489 }
8490 else
8491 {
0ab9ce85 8492 augment_type_symtab ();
43f3e411 8493 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8494 }
8495
8496 if (dwarf2_per_objfile->using_index)
43f3e411 8497 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8498 else
8499 {
8500 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8501 pst->compunit_symtab = cust;
f4dc4d17 8502 pst->readin = 1;
45cfd468 8503 }
f4dc4d17
DE
8504
8505 do_cleanups (back_to);
c906108c
SS
8506}
8507
95554aad
TT
8508/* Process an imported unit DIE. */
8509
8510static void
8511process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8512{
8513 struct attribute *attr;
8514
f4dc4d17
DE
8515 /* For now we don't handle imported units in type units. */
8516 if (cu->per_cu->is_debug_types)
8517 {
8518 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8519 " supported in type units [in module %s]"),
4262abfb 8520 objfile_name (cu->objfile));
f4dc4d17
DE
8521 }
8522
95554aad
TT
8523 attr = dwarf2_attr (die, DW_AT_import, cu);
8524 if (attr != NULL)
8525 {
9c541725
PA
8526 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8527 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8528 dwarf2_per_cu_data *per_cu
8529 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8530
69d751e3 8531 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8532 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8533 load_full_comp_unit (per_cu, cu->language);
8534
796a7ff8 8535 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8536 per_cu);
8537 }
8538}
8539
4c8aa72d
PA
8540/* RAII object that represents a process_die scope: i.e.,
8541 starts/finishes processing a DIE. */
8542class process_die_scope
adde2bff 8543{
4c8aa72d
PA
8544public:
8545 process_die_scope (die_info *die, dwarf2_cu *cu)
8546 : m_die (die), m_cu (cu)
8547 {
8548 /* We should only be processing DIEs not already in process. */
8549 gdb_assert (!m_die->in_process);
8550 m_die->in_process = true;
8551 }
8c3cb9fa 8552
4c8aa72d
PA
8553 ~process_die_scope ()
8554 {
8555 m_die->in_process = false;
8556
8557 /* If we're done processing the DIE for the CU that owns the line
8558 header, we don't need the line header anymore. */
8559 if (m_cu->line_header_die_owner == m_die)
8560 {
8561 delete m_cu->line_header;
8562 m_cu->line_header = NULL;
8563 m_cu->line_header_die_owner = NULL;
8564 }
8565 }
8566
8567private:
8568 die_info *m_die;
8569 dwarf2_cu *m_cu;
8570};
adde2bff 8571
c906108c
SS
8572/* Process a die and its children. */
8573
8574static void
e7c27a73 8575process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8576{
4c8aa72d 8577 process_die_scope scope (die, cu);
adde2bff 8578
c906108c
SS
8579 switch (die->tag)
8580 {
8581 case DW_TAG_padding:
8582 break;
8583 case DW_TAG_compile_unit:
95554aad 8584 case DW_TAG_partial_unit:
e7c27a73 8585 read_file_scope (die, cu);
c906108c 8586 break;
348e048f
DE
8587 case DW_TAG_type_unit:
8588 read_type_unit_scope (die, cu);
8589 break;
c906108c 8590 case DW_TAG_subprogram:
c906108c 8591 case DW_TAG_inlined_subroutine:
edb3359d 8592 read_func_scope (die, cu);
c906108c
SS
8593 break;
8594 case DW_TAG_lexical_block:
14898363
L
8595 case DW_TAG_try_block:
8596 case DW_TAG_catch_block:
e7c27a73 8597 read_lexical_block_scope (die, cu);
c906108c 8598 break;
216f72a1 8599 case DW_TAG_call_site:
96408a79
SA
8600 case DW_TAG_GNU_call_site:
8601 read_call_site_scope (die, cu);
8602 break;
c906108c 8603 case DW_TAG_class_type:
680b30c7 8604 case DW_TAG_interface_type:
c906108c
SS
8605 case DW_TAG_structure_type:
8606 case DW_TAG_union_type:
134d01f1 8607 process_structure_scope (die, cu);
c906108c
SS
8608 break;
8609 case DW_TAG_enumeration_type:
134d01f1 8610 process_enumeration_scope (die, cu);
c906108c 8611 break;
134d01f1 8612
f792889a
DJ
8613 /* These dies have a type, but processing them does not create
8614 a symbol or recurse to process the children. Therefore we can
8615 read them on-demand through read_type_die. */
c906108c 8616 case DW_TAG_subroutine_type:
72019c9c 8617 case DW_TAG_set_type:
c906108c 8618 case DW_TAG_array_type:
c906108c 8619 case DW_TAG_pointer_type:
c906108c 8620 case DW_TAG_ptr_to_member_type:
c906108c 8621 case DW_TAG_reference_type:
4297a3f0 8622 case DW_TAG_rvalue_reference_type:
c906108c 8623 case DW_TAG_string_type:
c906108c 8624 break;
134d01f1 8625
c906108c 8626 case DW_TAG_base_type:
a02abb62 8627 case DW_TAG_subrange_type:
cb249c71 8628 case DW_TAG_typedef:
134d01f1
DJ
8629 /* Add a typedef symbol for the type definition, if it has a
8630 DW_AT_name. */
f792889a 8631 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8632 break;
c906108c 8633 case DW_TAG_common_block:
e7c27a73 8634 read_common_block (die, cu);
c906108c
SS
8635 break;
8636 case DW_TAG_common_inclusion:
8637 break;
d9fa45fe 8638 case DW_TAG_namespace:
4d4ec4e5 8639 cu->processing_has_namespace_info = 1;
e7c27a73 8640 read_namespace (die, cu);
d9fa45fe 8641 break;
5d7cb8df 8642 case DW_TAG_module:
4d4ec4e5 8643 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8644 read_module (die, cu);
8645 break;
d9fa45fe 8646 case DW_TAG_imported_declaration:
74921315
KS
8647 cu->processing_has_namespace_info = 1;
8648 if (read_namespace_alias (die, cu))
8649 break;
8650 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8651 case DW_TAG_imported_module:
4d4ec4e5 8652 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8653 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8654 || cu->language != language_fortran))
8655 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8656 dwarf_tag_name (die->tag));
8657 read_import_statement (die, cu);
d9fa45fe 8658 break;
95554aad
TT
8659
8660 case DW_TAG_imported_unit:
8661 process_imported_unit_die (die, cu);
8662 break;
8663
c906108c 8664 default:
e7c27a73 8665 new_symbol (die, NULL, cu);
c906108c
SS
8666 break;
8667 }
8668}
ca69b9e6
DE
8669\f
8670/* DWARF name computation. */
c906108c 8671
94af9270
KS
8672/* A helper function for dwarf2_compute_name which determines whether DIE
8673 needs to have the name of the scope prepended to the name listed in the
8674 die. */
8675
8676static int
8677die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8678{
1c809c68
TT
8679 struct attribute *attr;
8680
94af9270
KS
8681 switch (die->tag)
8682 {
8683 case DW_TAG_namespace:
8684 case DW_TAG_typedef:
8685 case DW_TAG_class_type:
8686 case DW_TAG_interface_type:
8687 case DW_TAG_structure_type:
8688 case DW_TAG_union_type:
8689 case DW_TAG_enumeration_type:
8690 case DW_TAG_enumerator:
8691 case DW_TAG_subprogram:
08a76f8a 8692 case DW_TAG_inlined_subroutine:
94af9270 8693 case DW_TAG_member:
74921315 8694 case DW_TAG_imported_declaration:
94af9270
KS
8695 return 1;
8696
8697 case DW_TAG_variable:
c2b0a229 8698 case DW_TAG_constant:
94af9270
KS
8699 /* We only need to prefix "globally" visible variables. These include
8700 any variable marked with DW_AT_external or any variable that
8701 lives in a namespace. [Variables in anonymous namespaces
8702 require prefixing, but they are not DW_AT_external.] */
8703
8704 if (dwarf2_attr (die, DW_AT_specification, cu))
8705 {
8706 struct dwarf2_cu *spec_cu = cu;
9a619af0 8707
94af9270
KS
8708 return die_needs_namespace (die_specification (die, &spec_cu),
8709 spec_cu);
8710 }
8711
1c809c68 8712 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8713 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8714 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8715 return 0;
8716 /* A variable in a lexical block of some kind does not need a
8717 namespace, even though in C++ such variables may be external
8718 and have a mangled name. */
8719 if (die->parent->tag == DW_TAG_lexical_block
8720 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8721 || die->parent->tag == DW_TAG_catch_block
8722 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8723 return 0;
8724 return 1;
94af9270
KS
8725
8726 default:
8727 return 0;
8728 }
8729}
8730
73b9be8b
KS
8731/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8732 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8733 defined for the given DIE. */
8734
8735static struct attribute *
8736dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8737{
8738 struct attribute *attr;
8739
8740 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8741 if (attr == NULL)
8742 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8743
8744 return attr;
8745}
8746
8747/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8748 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8749 defined for the given DIE. */
8750
8751static const char *
8752dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8753{
8754 const char *linkage_name;
8755
8756 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8757 if (linkage_name == NULL)
8758 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8759
8760 return linkage_name;
8761}
8762
94af9270 8763/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8764 compute the physname for the object, which include a method's:
9c37b5ae 8765 - formal parameters (C++),
a766d390 8766 - receiver type (Go),
a766d390
DE
8767
8768 The term "physname" is a bit confusing.
8769 For C++, for example, it is the demangled name.
8770 For Go, for example, it's the mangled name.
94af9270 8771
af6b7be1
JB
8772 For Ada, return the DIE's linkage name rather than the fully qualified
8773 name. PHYSNAME is ignored..
8774
94af9270
KS
8775 The result is allocated on the objfile_obstack and canonicalized. */
8776
8777static const char *
15d034d0
TT
8778dwarf2_compute_name (const char *name,
8779 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8780 int physname)
8781{
bb5ed363
DE
8782 struct objfile *objfile = cu->objfile;
8783
94af9270
KS
8784 if (name == NULL)
8785 name = dwarf2_name (die, cu);
8786
2ee7123e
DE
8787 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8788 but otherwise compute it by typename_concat inside GDB.
8789 FIXME: Actually this is not really true, or at least not always true.
8790 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8791 Fortran names because there is no mangling standard. So new_symbol_full
8792 will set the demangled name to the result of dwarf2_full_name, and it is
8793 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8794 if (cu->language == language_ada
8795 || (cu->language == language_fortran && physname))
8796 {
8797 /* For Ada unit, we prefer the linkage name over the name, as
8798 the former contains the exported name, which the user expects
8799 to be able to reference. Ideally, we want the user to be able
8800 to reference this entity using either natural or linkage name,
8801 but we haven't started looking at this enhancement yet. */
73b9be8b 8802 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 8803
2ee7123e
DE
8804 if (linkage_name != NULL)
8805 return linkage_name;
f55ee35c
JK
8806 }
8807
94af9270
KS
8808 /* These are the only languages we know how to qualify names in. */
8809 if (name != NULL
9c37b5ae 8810 && (cu->language == language_cplus
c44af4eb
TT
8811 || cu->language == language_fortran || cu->language == language_d
8812 || cu->language == language_rust))
94af9270
KS
8813 {
8814 if (die_needs_namespace (die, cu))
8815 {
8816 long length;
0d5cff50 8817 const char *prefix;
34a68019 8818 const char *canonical_name = NULL;
94af9270 8819
d7e74731
PA
8820 string_file buf;
8821
94af9270 8822 prefix = determine_prefix (die, cu);
94af9270
KS
8823 if (*prefix != '\0')
8824 {
f55ee35c
JK
8825 char *prefixed_name = typename_concat (NULL, prefix, name,
8826 physname, cu);
9a619af0 8827
d7e74731 8828 buf.puts (prefixed_name);
94af9270
KS
8829 xfree (prefixed_name);
8830 }
8831 else
d7e74731 8832 buf.puts (name);
94af9270 8833
98bfdba5
PA
8834 /* Template parameters may be specified in the DIE's DW_AT_name, or
8835 as children with DW_TAG_template_type_param or
8836 DW_TAG_value_type_param. If the latter, add them to the name
8837 here. If the name already has template parameters, then
8838 skip this step; some versions of GCC emit both, and
8839 it is more efficient to use the pre-computed name.
8840
8841 Something to keep in mind about this process: it is very
8842 unlikely, or in some cases downright impossible, to produce
8843 something that will match the mangled name of a function.
8844 If the definition of the function has the same debug info,
8845 we should be able to match up with it anyway. But fallbacks
8846 using the minimal symbol, for instance to find a method
8847 implemented in a stripped copy of libstdc++, will not work.
8848 If we do not have debug info for the definition, we will have to
8849 match them up some other way.
8850
8851 When we do name matching there is a related problem with function
8852 templates; two instantiated function templates are allowed to
8853 differ only by their return types, which we do not add here. */
8854
8855 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8856 {
8857 struct attribute *attr;
8858 struct die_info *child;
8859 int first = 1;
8860
8861 die->building_fullname = 1;
8862
8863 for (child = die->child; child != NULL; child = child->sibling)
8864 {
8865 struct type *type;
12df843f 8866 LONGEST value;
d521ce57 8867 const gdb_byte *bytes;
98bfdba5
PA
8868 struct dwarf2_locexpr_baton *baton;
8869 struct value *v;
8870
8871 if (child->tag != DW_TAG_template_type_param
8872 && child->tag != DW_TAG_template_value_param)
8873 continue;
8874
8875 if (first)
8876 {
d7e74731 8877 buf.puts ("<");
98bfdba5
PA
8878 first = 0;
8879 }
8880 else
d7e74731 8881 buf.puts (", ");
98bfdba5
PA
8882
8883 attr = dwarf2_attr (child, DW_AT_type, cu);
8884 if (attr == NULL)
8885 {
8886 complaint (&symfile_complaints,
8887 _("template parameter missing DW_AT_type"));
d7e74731 8888 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8889 continue;
8890 }
8891 type = die_type (child, cu);
8892
8893 if (child->tag == DW_TAG_template_type_param)
8894 {
d7e74731 8895 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8896 continue;
8897 }
8898
8899 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8900 if (attr == NULL)
8901 {
8902 complaint (&symfile_complaints,
3e43a32a
MS
8903 _("template parameter missing "
8904 "DW_AT_const_value"));
d7e74731 8905 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8906 continue;
8907 }
8908
8909 dwarf2_const_value_attr (attr, type, name,
8910 &cu->comp_unit_obstack, cu,
8911 &value, &bytes, &baton);
8912
8913 if (TYPE_NOSIGN (type))
8914 /* GDB prints characters as NUMBER 'CHAR'. If that's
8915 changed, this can use value_print instead. */
d7e74731 8916 c_printchar (value, type, &buf);
98bfdba5
PA
8917 else
8918 {
8919 struct value_print_options opts;
8920
8921 if (baton != NULL)
8922 v = dwarf2_evaluate_loc_desc (type, NULL,
8923 baton->data,
8924 baton->size,
8925 baton->per_cu);
8926 else if (bytes != NULL)
8927 {
8928 v = allocate_value (type);
8929 memcpy (value_contents_writeable (v), bytes,
8930 TYPE_LENGTH (type));
8931 }
8932 else
8933 v = value_from_longest (type, value);
8934
3e43a32a
MS
8935 /* Specify decimal so that we do not depend on
8936 the radix. */
98bfdba5
PA
8937 get_formatted_print_options (&opts, 'd');
8938 opts.raw = 1;
d7e74731 8939 value_print (v, &buf, &opts);
98bfdba5
PA
8940 release_value (v);
8941 value_free (v);
8942 }
8943 }
8944
8945 die->building_fullname = 0;
8946
8947 if (!first)
8948 {
8949 /* Close the argument list, with a space if necessary
8950 (nested templates). */
d7e74731
PA
8951 if (!buf.empty () && buf.string ().back () == '>')
8952 buf.puts (" >");
98bfdba5 8953 else
d7e74731 8954 buf.puts (">");
98bfdba5
PA
8955 }
8956 }
8957
9c37b5ae 8958 /* For C++ methods, append formal parameter type
94af9270 8959 information, if PHYSNAME. */
6e70227d 8960
94af9270 8961 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8962 && cu->language == language_cplus)
94af9270
KS
8963 {
8964 struct type *type = read_type_die (die, cu);
8965
d7e74731 8966 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8967 &type_print_raw_options);
94af9270 8968
9c37b5ae 8969 if (cu->language == language_cplus)
94af9270 8970 {
60430eff
DJ
8971 /* Assume that an artificial first parameter is
8972 "this", but do not crash if it is not. RealView
8973 marks unnamed (and thus unused) parameters as
8974 artificial; there is no way to differentiate
8975 the two cases. */
94af9270
KS
8976 if (TYPE_NFIELDS (type) > 0
8977 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8978 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8979 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8980 0))))
d7e74731 8981 buf.puts (" const");
94af9270
KS
8982 }
8983 }
8984
d7e74731 8985 const std::string &intermediate_name = buf.string ();
94af9270
KS
8986
8987 if (cu->language == language_cplus)
34a68019 8988 canonical_name
322a8516 8989 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8990 &objfile->per_bfd->storage_obstack);
8991
8992 /* If we only computed INTERMEDIATE_NAME, or if
8993 INTERMEDIATE_NAME is already canonical, then we need to
8994 copy it to the appropriate obstack. */
322a8516 8995 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8996 name = ((const char *)
8997 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8998 intermediate_name.c_str (),
8999 intermediate_name.length ()));
34a68019
TT
9000 else
9001 name = canonical_name;
94af9270
KS
9002 }
9003 }
9004
9005 return name;
9006}
9007
0114d602
DJ
9008/* Return the fully qualified name of DIE, based on its DW_AT_name.
9009 If scope qualifiers are appropriate they will be added. The result
34a68019 9010 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9011 not have a name. NAME may either be from a previous call to
9012 dwarf2_name or NULL.
9013
9c37b5ae 9014 The output string will be canonicalized (if C++). */
0114d602
DJ
9015
9016static const char *
15d034d0 9017dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9018{
94af9270
KS
9019 return dwarf2_compute_name (name, die, cu, 0);
9020}
0114d602 9021
94af9270
KS
9022/* Construct a physname for the given DIE in CU. NAME may either be
9023 from a previous call to dwarf2_name or NULL. The result will be
9024 allocated on the objfile_objstack or NULL if the DIE does not have a
9025 name.
0114d602 9026
9c37b5ae 9027 The output string will be canonicalized (if C++). */
0114d602 9028
94af9270 9029static const char *
15d034d0 9030dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9031{
bb5ed363 9032 struct objfile *objfile = cu->objfile;
900e11f9
JK
9033 const char *retval, *mangled = NULL, *canon = NULL;
9034 struct cleanup *back_to;
9035 int need_copy = 1;
9036
9037 /* In this case dwarf2_compute_name is just a shortcut not building anything
9038 on its own. */
9039 if (!die_needs_namespace (die, cu))
9040 return dwarf2_compute_name (name, die, cu, 1);
9041
9042 back_to = make_cleanup (null_cleanup, NULL);
9043
73b9be8b 9044 mangled = dw2_linkage_name (die, cu);
900e11f9 9045
e98c9e7c
TT
9046 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9047 See https://github.com/rust-lang/rust/issues/32925. */
9048 if (cu->language == language_rust && mangled != NULL
9049 && strchr (mangled, '{') != NULL)
9050 mangled = NULL;
9051
900e11f9
JK
9052 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9053 has computed. */
7d45c7c3 9054 if (mangled != NULL)
900e11f9
JK
9055 {
9056 char *demangled;
9057
900e11f9
JK
9058 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9059 type. It is easier for GDB users to search for such functions as
9060 `name(params)' than `long name(params)'. In such case the minimal
9061 symbol names do not match the full symbol names but for template
9062 functions there is never a need to look up their definition from their
9063 declaration so the only disadvantage remains the minimal symbol
9064 variant `long name(params)' does not have the proper inferior type.
9065 */
9066
a766d390
DE
9067 if (cu->language == language_go)
9068 {
9069 /* This is a lie, but we already lie to the caller new_symbol_full.
9070 new_symbol_full assumes we return the mangled name.
9071 This just undoes that lie until things are cleaned up. */
9072 demangled = NULL;
9073 }
9074 else
9075 {
8de20a37 9076 demangled = gdb_demangle (mangled,
9c37b5ae 9077 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 9078 }
900e11f9
JK
9079 if (demangled)
9080 {
9081 make_cleanup (xfree, demangled);
9082 canon = demangled;
9083 }
9084 else
9085 {
9086 canon = mangled;
9087 need_copy = 0;
9088 }
9089 }
9090
9091 if (canon == NULL || check_physname)
9092 {
9093 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9094
9095 if (canon != NULL && strcmp (physname, canon) != 0)
9096 {
9097 /* It may not mean a bug in GDB. The compiler could also
9098 compute DW_AT_linkage_name incorrectly. But in such case
9099 GDB would need to be bug-to-bug compatible. */
9100
9101 complaint (&symfile_complaints,
9102 _("Computed physname <%s> does not match demangled <%s> "
9103 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9104 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9105 objfile_name (objfile));
900e11f9
JK
9106
9107 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9108 is available here - over computed PHYSNAME. It is safer
9109 against both buggy GDB and buggy compilers. */
9110
9111 retval = canon;
9112 }
9113 else
9114 {
9115 retval = physname;
9116 need_copy = 0;
9117 }
9118 }
9119 else
9120 retval = canon;
9121
9122 if (need_copy)
224c3ddb
SM
9123 retval = ((const char *)
9124 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9125 retval, strlen (retval)));
900e11f9
JK
9126
9127 do_cleanups (back_to);
9128 return retval;
0114d602
DJ
9129}
9130
74921315
KS
9131/* Inspect DIE in CU for a namespace alias. If one exists, record
9132 a new symbol for it.
9133
9134 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9135
9136static int
9137read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9138{
9139 struct attribute *attr;
9140
9141 /* If the die does not have a name, this is not a namespace
9142 alias. */
9143 attr = dwarf2_attr (die, DW_AT_name, cu);
9144 if (attr != NULL)
9145 {
9146 int num;
9147 struct die_info *d = die;
9148 struct dwarf2_cu *imported_cu = cu;
9149
9150 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9151 keep inspecting DIEs until we hit the underlying import. */
9152#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9153 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9154 {
9155 attr = dwarf2_attr (d, DW_AT_import, cu);
9156 if (attr == NULL)
9157 break;
9158
9159 d = follow_die_ref (d, attr, &imported_cu);
9160 if (d->tag != DW_TAG_imported_declaration)
9161 break;
9162 }
9163
9164 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9165 {
9166 complaint (&symfile_complaints,
9167 _("DIE at 0x%x has too many recursively imported "
9c541725 9168 "declarations"), to_underlying (d->sect_off));
74921315
KS
9169 return 0;
9170 }
9171
9172 if (attr != NULL)
9173 {
9174 struct type *type;
9c541725 9175 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9176
9c541725 9177 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9178 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9179 {
9180 /* This declaration is a global namespace alias. Add
9181 a symbol for it whose type is the aliased namespace. */
9182 new_symbol (die, type, cu);
9183 return 1;
9184 }
9185 }
9186 }
9187
9188 return 0;
9189}
9190
22cee43f
PMR
9191/* Return the using directives repository (global or local?) to use in the
9192 current context for LANGUAGE.
9193
9194 For Ada, imported declarations can materialize renamings, which *may* be
9195 global. However it is impossible (for now?) in DWARF to distinguish
9196 "external" imported declarations and "static" ones. As all imported
9197 declarations seem to be static in all other languages, make them all CU-wide
9198 global only in Ada. */
9199
9200static struct using_direct **
9201using_directives (enum language language)
9202{
9203 if (language == language_ada && context_stack_depth == 0)
9204 return &global_using_directives;
9205 else
9206 return &local_using_directives;
9207}
9208
27aa8d6a
SW
9209/* Read the import statement specified by the given die and record it. */
9210
9211static void
9212read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9213{
bb5ed363 9214 struct objfile *objfile = cu->objfile;
27aa8d6a 9215 struct attribute *import_attr;
32019081 9216 struct die_info *imported_die, *child_die;
de4affc9 9217 struct dwarf2_cu *imported_cu;
27aa8d6a 9218 const char *imported_name;
794684b6 9219 const char *imported_name_prefix;
13387711
SW
9220 const char *canonical_name;
9221 const char *import_alias;
9222 const char *imported_declaration = NULL;
794684b6 9223 const char *import_prefix;
32019081
JK
9224 VEC (const_char_ptr) *excludes = NULL;
9225 struct cleanup *cleanups;
13387711 9226
27aa8d6a
SW
9227 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9228 if (import_attr == NULL)
9229 {
9230 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9231 dwarf_tag_name (die->tag));
9232 return;
9233 }
9234
de4affc9
CC
9235 imported_cu = cu;
9236 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9237 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9238 if (imported_name == NULL)
9239 {
9240 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9241
9242 The import in the following code:
9243 namespace A
9244 {
9245 typedef int B;
9246 }
9247
9248 int main ()
9249 {
9250 using A::B;
9251 B b;
9252 return b;
9253 }
9254
9255 ...
9256 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9257 <52> DW_AT_decl_file : 1
9258 <53> DW_AT_decl_line : 6
9259 <54> DW_AT_import : <0x75>
9260 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9261 <59> DW_AT_name : B
9262 <5b> DW_AT_decl_file : 1
9263 <5c> DW_AT_decl_line : 2
9264 <5d> DW_AT_type : <0x6e>
9265 ...
9266 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9267 <76> DW_AT_byte_size : 4
9268 <77> DW_AT_encoding : 5 (signed)
9269
9270 imports the wrong die ( 0x75 instead of 0x58 ).
9271 This case will be ignored until the gcc bug is fixed. */
9272 return;
9273 }
9274
82856980
SW
9275 /* Figure out the local name after import. */
9276 import_alias = dwarf2_name (die, cu);
27aa8d6a 9277
794684b6
SW
9278 /* Figure out where the statement is being imported to. */
9279 import_prefix = determine_prefix (die, cu);
9280
9281 /* Figure out what the scope of the imported die is and prepend it
9282 to the name of the imported die. */
de4affc9 9283 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9284
f55ee35c
JK
9285 if (imported_die->tag != DW_TAG_namespace
9286 && imported_die->tag != DW_TAG_module)
794684b6 9287 {
13387711
SW
9288 imported_declaration = imported_name;
9289 canonical_name = imported_name_prefix;
794684b6 9290 }
13387711 9291 else if (strlen (imported_name_prefix) > 0)
12aaed36 9292 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9293 imported_name_prefix,
9294 (cu->language == language_d ? "." : "::"),
9295 imported_name, (char *) NULL);
13387711
SW
9296 else
9297 canonical_name = imported_name;
794684b6 9298
32019081
JK
9299 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9300
9301 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9302 for (child_die = die->child; child_die && child_die->tag;
9303 child_die = sibling_die (child_die))
9304 {
9305 /* DWARF-4: A Fortran use statement with a “rename list” may be
9306 represented by an imported module entry with an import attribute
9307 referring to the module and owned entries corresponding to those
9308 entities that are renamed as part of being imported. */
9309
9310 if (child_die->tag != DW_TAG_imported_declaration)
9311 {
9312 complaint (&symfile_complaints,
9313 _("child DW_TAG_imported_declaration expected "
9314 "- DIE at 0x%x [in module %s]"),
9c541725 9315 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9316 continue;
9317 }
9318
9319 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9320 if (import_attr == NULL)
9321 {
9322 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9323 dwarf_tag_name (child_die->tag));
9324 continue;
9325 }
9326
9327 imported_cu = cu;
9328 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9329 &imported_cu);
9330 imported_name = dwarf2_name (imported_die, imported_cu);
9331 if (imported_name == NULL)
9332 {
9333 complaint (&symfile_complaints,
9334 _("child DW_TAG_imported_declaration has unknown "
9335 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9336 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9337 continue;
9338 }
9339
9340 VEC_safe_push (const_char_ptr, excludes, imported_name);
9341
9342 process_die (child_die, cu);
9343 }
9344
22cee43f
PMR
9345 add_using_directive (using_directives (cu->language),
9346 import_prefix,
9347 canonical_name,
9348 import_alias,
9349 imported_declaration,
9350 excludes,
9351 0,
9352 &objfile->objfile_obstack);
32019081
JK
9353
9354 do_cleanups (cleanups);
27aa8d6a
SW
9355}
9356
1b80a9fa
JK
9357/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9358 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9359 this, it was first present in GCC release 4.3.0. */
9360
9361static int
9362producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9363{
9364 if (!cu->checked_producer)
9365 check_producer (cu);
9366
9367 return cu->producer_is_gcc_lt_4_3;
9368}
9369
d721ba37
PA
9370static file_and_directory
9371find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9372{
d721ba37
PA
9373 file_and_directory res;
9374
9291a0cd
TT
9375 /* Find the filename. Do not use dwarf2_name here, since the filename
9376 is not a source language identifier. */
d721ba37
PA
9377 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9378 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9379
d721ba37
PA
9380 if (res.comp_dir == NULL
9381 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9382 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9383 {
d721ba37
PA
9384 res.comp_dir_storage = ldirname (res.name);
9385 if (!res.comp_dir_storage.empty ())
9386 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9387 }
d721ba37 9388 if (res.comp_dir != NULL)
9291a0cd
TT
9389 {
9390 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9391 directory, get rid of it. */
d721ba37 9392 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9393
d721ba37
PA
9394 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9395 res.comp_dir = cp + 1;
9291a0cd
TT
9396 }
9397
d721ba37
PA
9398 if (res.name == NULL)
9399 res.name = "<unknown>";
9400
9401 return res;
9291a0cd
TT
9402}
9403
f4dc4d17
DE
9404/* Handle DW_AT_stmt_list for a compilation unit.
9405 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9406 COMP_DIR is the compilation directory. LOWPC is passed to
9407 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9408
9409static void
9410handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9411 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9412{
527f3840 9413 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9414 struct attribute *attr;
527f3840
JK
9415 struct line_header line_header_local;
9416 hashval_t line_header_local_hash;
9417 unsigned u;
9418 void **slot;
9419 int decode_mapping;
2ab95328 9420
f4dc4d17
DE
9421 gdb_assert (! cu->per_cu->is_debug_types);
9422
2ab95328 9423 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9424 if (attr == NULL)
9425 return;
9426
9c541725 9427 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9428
9429 /* The line header hash table is only created if needed (it exists to
9430 prevent redundant reading of the line table for partial_units).
9431 If we're given a partial_unit, we'll need it. If we're given a
9432 compile_unit, then use the line header hash table if it's already
9433 created, but don't create one just yet. */
9434
9435 if (dwarf2_per_objfile->line_header_hash == NULL
9436 && die->tag == DW_TAG_partial_unit)
2ab95328 9437 {
527f3840
JK
9438 dwarf2_per_objfile->line_header_hash
9439 = htab_create_alloc_ex (127, line_header_hash_voidp,
9440 line_header_eq_voidp,
9441 free_line_header_voidp,
9442 &objfile->objfile_obstack,
9443 hashtab_obstack_allocate,
9444 dummy_obstack_deallocate);
9445 }
2ab95328 9446
9c541725 9447 line_header_local.sect_off = line_offset;
527f3840
JK
9448 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9449 line_header_local_hash = line_header_hash (&line_header_local);
9450 if (dwarf2_per_objfile->line_header_hash != NULL)
9451 {
9452 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9453 &line_header_local,
9454 line_header_local_hash, NO_INSERT);
9455
9456 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9457 is not present in *SLOT (since if there is something in *SLOT then
9458 it will be for a partial_unit). */
9459 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9460 {
527f3840 9461 gdb_assert (*slot != NULL);
9a3c8263 9462 cu->line_header = (struct line_header *) *slot;
527f3840 9463 return;
dee91e82 9464 }
2ab95328 9465 }
527f3840
JK
9466
9467 /* dwarf_decode_line_header does not yet provide sufficient information.
9468 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9469 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9470 if (lh == NULL)
527f3840 9471 return;
4c8aa72d
PA
9472
9473 cu->line_header = lh.release ();
9474 cu->line_header_die_owner = die;
527f3840
JK
9475
9476 if (dwarf2_per_objfile->line_header_hash == NULL)
9477 slot = NULL;
9478 else
9479 {
9480 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9481 &line_header_local,
9482 line_header_local_hash, INSERT);
9483 gdb_assert (slot != NULL);
9484 }
9485 if (slot != NULL && *slot == NULL)
9486 {
9487 /* This newly decoded line number information unit will be owned
9488 by line_header_hash hash table. */
9489 *slot = cu->line_header;
4c8aa72d 9490 cu->line_header_die_owner = NULL;
527f3840
JK
9491 }
9492 else
9493 {
9494 /* We cannot free any current entry in (*slot) as that struct line_header
9495 may be already used by multiple CUs. Create only temporary decoded
9496 line_header for this CU - it may happen at most once for each line
9497 number information unit. And if we're not using line_header_hash
9498 then this is what we want as well. */
9499 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9500 }
9501 decode_mapping = (die->tag != DW_TAG_partial_unit);
9502 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9503 decode_mapping);
fff8551c 9504
2ab95328
TT
9505}
9506
95554aad 9507/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9508
c906108c 9509static void
e7c27a73 9510read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9511{
dee91e82 9512 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9514 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9515 CORE_ADDR highpc = ((CORE_ADDR) 0);
9516 struct attribute *attr;
c906108c 9517 struct die_info *child_die;
e142c38c 9518 CORE_ADDR baseaddr;
6e70227d 9519
e142c38c 9520 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9521
fae299cd 9522 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9523
9524 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9525 from finish_block. */
2acceee2 9526 if (lowpc == ((CORE_ADDR) -1))
c906108c 9527 lowpc = highpc;
3e29f34a 9528 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9529
d721ba37 9530 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9531
95554aad 9532 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9533
f4b8a18d
KW
9534 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9535 standardised yet. As a workaround for the language detection we fall
9536 back to the DW_AT_producer string. */
9537 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9538 cu->language = language_opencl;
9539
3019eac3
DE
9540 /* Similar hack for Go. */
9541 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9542 set_cu_language (DW_LANG_Go, cu);
9543
d721ba37 9544 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9545
9546 /* Decode line number information if present. We do this before
9547 processing child DIEs, so that the line header table is available
9548 for DW_AT_decl_file. */
d721ba37 9549 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9550
9551 /* Process all dies in compilation unit. */
9552 if (die->child != NULL)
9553 {
9554 child_die = die->child;
9555 while (child_die && child_die->tag)
9556 {
9557 process_die (child_die, cu);
9558 child_die = sibling_die (child_die);
9559 }
9560 }
9561
9562 /* Decode macro information, if present. Dwarf 2 macro information
9563 refers to information in the line number info statement program
9564 header, so we can only read it if we've read the header
9565 successfully. */
0af92d60
JK
9566 attr = dwarf2_attr (die, DW_AT_macros, cu);
9567 if (attr == NULL)
9568 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9569 if (attr && cu->line_header)
9570 {
9571 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9572 complaint (&symfile_complaints,
0af92d60 9573 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9574
43f3e411 9575 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9576 }
9577 else
9578 {
9579 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9580 if (attr && cu->line_header)
9581 {
9582 unsigned int macro_offset = DW_UNSND (attr);
9583
43f3e411 9584 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9585 }
9586 }
3019eac3
DE
9587}
9588
f4dc4d17
DE
9589/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9590 Create the set of symtabs used by this TU, or if this TU is sharing
9591 symtabs with another TU and the symtabs have already been created
9592 then restore those symtabs in the line header.
9593 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9594
9595static void
f4dc4d17 9596setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9597{
f4dc4d17
DE
9598 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9599 struct type_unit_group *tu_group;
9600 int first_time;
3019eac3 9601 struct attribute *attr;
9c541725 9602 unsigned int i;
0186c6a7 9603 struct signatured_type *sig_type;
3019eac3 9604
f4dc4d17 9605 gdb_assert (per_cu->is_debug_types);
0186c6a7 9606 sig_type = (struct signatured_type *) per_cu;
3019eac3 9607
f4dc4d17 9608 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9609
f4dc4d17 9610 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9611 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9612 if (sig_type->type_unit_group == NULL)
9613 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9614 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9615
9616 /* If we've already processed this stmt_list there's no real need to
9617 do it again, we could fake it and just recreate the part we need
9618 (file name,index -> symtab mapping). If data shows this optimization
9619 is useful we can do it then. */
43f3e411 9620 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9621
9622 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9623 debug info. */
fff8551c 9624 line_header_up lh;
f4dc4d17 9625 if (attr != NULL)
3019eac3 9626 {
9c541725 9627 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9628 lh = dwarf_decode_line_header (line_offset, cu);
9629 }
9630 if (lh == NULL)
9631 {
9632 if (first_time)
9633 dwarf2_start_symtab (cu, "", NULL, 0);
9634 else
9635 {
9636 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9637 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9638 }
f4dc4d17 9639 return;
3019eac3
DE
9640 }
9641
4c8aa72d
PA
9642 cu->line_header = lh.release ();
9643 cu->line_header_die_owner = die;
3019eac3 9644
f4dc4d17
DE
9645 if (first_time)
9646 {
43f3e411 9647 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9648
1fd60fc0
DE
9649 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9650 still initializing it, and our caller (a few levels up)
9651 process_full_type_unit still needs to know if this is the first
9652 time. */
9653
4c8aa72d
PA
9654 tu_group->num_symtabs = cu->line_header->file_names.size ();
9655 tu_group->symtabs = XNEWVEC (struct symtab *,
9656 cu->line_header->file_names.size ());
3019eac3 9657
4c8aa72d 9658 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9659 {
4c8aa72d 9660 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9661
4c8aa72d 9662 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9663
f4dc4d17
DE
9664 if (current_subfile->symtab == NULL)
9665 {
4c8aa72d
PA
9666 /* NOTE: start_subfile will recognize when it's been
9667 passed a file it has already seen. So we can't
9668 assume there's a simple mapping from
9669 cu->line_header->file_names to subfiles, plus
9670 cu->line_header->file_names may contain dups. */
43f3e411
DE
9671 current_subfile->symtab
9672 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9673 }
9674
8c43009f
PA
9675 fe.symtab = current_subfile->symtab;
9676 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9677 }
9678 }
9679 else
3019eac3 9680 {
0ab9ce85 9681 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9682
4c8aa72d 9683 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9684 {
4c8aa72d 9685 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 9686
4c8aa72d 9687 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9688 }
3019eac3
DE
9689 }
9690
f4dc4d17
DE
9691 /* The main symtab is allocated last. Type units don't have DW_AT_name
9692 so they don't have a "real" (so to speak) symtab anyway.
9693 There is later code that will assign the main symtab to all symbols
9694 that don't have one. We need to handle the case of a symbol with a
9695 missing symtab (DW_AT_decl_file) anyway. */
9696}
3019eac3 9697
f4dc4d17
DE
9698/* Process DW_TAG_type_unit.
9699 For TUs we want to skip the first top level sibling if it's not the
9700 actual type being defined by this TU. In this case the first top
9701 level sibling is there to provide context only. */
3019eac3 9702
f4dc4d17
DE
9703static void
9704read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9705{
9706 struct die_info *child_die;
3019eac3 9707
f4dc4d17
DE
9708 prepare_one_comp_unit (cu, die, language_minimal);
9709
9710 /* Initialize (or reinitialize) the machinery for building symtabs.
9711 We do this before processing child DIEs, so that the line header table
9712 is available for DW_AT_decl_file. */
9713 setup_type_unit_groups (die, cu);
9714
9715 if (die->child != NULL)
9716 {
9717 child_die = die->child;
9718 while (child_die && child_die->tag)
9719 {
9720 process_die (child_die, cu);
9721 child_die = sibling_die (child_die);
9722 }
9723 }
3019eac3
DE
9724}
9725\f
80626a55
DE
9726/* DWO/DWP files.
9727
9728 http://gcc.gnu.org/wiki/DebugFission
9729 http://gcc.gnu.org/wiki/DebugFissionDWP
9730
9731 To simplify handling of both DWO files ("object" files with the DWARF info)
9732 and DWP files (a file with the DWOs packaged up into one file), we treat
9733 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9734
9735static hashval_t
9736hash_dwo_file (const void *item)
9737{
9a3c8263 9738 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9739 hashval_t hash;
3019eac3 9740
a2ce51a0
DE
9741 hash = htab_hash_string (dwo_file->dwo_name);
9742 if (dwo_file->comp_dir != NULL)
9743 hash += htab_hash_string (dwo_file->comp_dir);
9744 return hash;
3019eac3
DE
9745}
9746
9747static int
9748eq_dwo_file (const void *item_lhs, const void *item_rhs)
9749{
9a3c8263
SM
9750 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9751 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9752
a2ce51a0
DE
9753 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9754 return 0;
9755 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9756 return lhs->comp_dir == rhs->comp_dir;
9757 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9758}
9759
9760/* Allocate a hash table for DWO files. */
9761
9762static htab_t
9763allocate_dwo_file_hash_table (void)
9764{
9765 struct objfile *objfile = dwarf2_per_objfile->objfile;
9766
9767 return htab_create_alloc_ex (41,
9768 hash_dwo_file,
9769 eq_dwo_file,
9770 NULL,
9771 &objfile->objfile_obstack,
9772 hashtab_obstack_allocate,
9773 dummy_obstack_deallocate);
9774}
9775
80626a55
DE
9776/* Lookup DWO file DWO_NAME. */
9777
9778static void **
0ac5b59e 9779lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9780{
9781 struct dwo_file find_entry;
9782 void **slot;
9783
9784 if (dwarf2_per_objfile->dwo_files == NULL)
9785 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9786
9787 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9788 find_entry.dwo_name = dwo_name;
9789 find_entry.comp_dir = comp_dir;
80626a55
DE
9790 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9791
9792 return slot;
9793}
9794
3019eac3
DE
9795static hashval_t
9796hash_dwo_unit (const void *item)
9797{
9a3c8263 9798 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9799
9800 /* This drops the top 32 bits of the id, but is ok for a hash. */
9801 return dwo_unit->signature;
9802}
9803
9804static int
9805eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9806{
9a3c8263
SM
9807 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9808 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9809
9810 /* The signature is assumed to be unique within the DWO file.
9811 So while object file CU dwo_id's always have the value zero,
9812 that's OK, assuming each object file DWO file has only one CU,
9813 and that's the rule for now. */
9814 return lhs->signature == rhs->signature;
9815}
9816
9817/* Allocate a hash table for DWO CUs,TUs.
9818 There is one of these tables for each of CUs,TUs for each DWO file. */
9819
9820static htab_t
9821allocate_dwo_unit_table (struct objfile *objfile)
9822{
9823 /* Start out with a pretty small number.
9824 Generally DWO files contain only one CU and maybe some TUs. */
9825 return htab_create_alloc_ex (3,
9826 hash_dwo_unit,
9827 eq_dwo_unit,
9828 NULL,
9829 &objfile->objfile_obstack,
9830 hashtab_obstack_allocate,
9831 dummy_obstack_deallocate);
9832}
9833
80626a55 9834/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9835
19c3d4c9 9836struct create_dwo_cu_data
3019eac3
DE
9837{
9838 struct dwo_file *dwo_file;
19c3d4c9 9839 struct dwo_unit dwo_unit;
3019eac3
DE
9840};
9841
19c3d4c9 9842/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9843
9844static void
19c3d4c9
DE
9845create_dwo_cu_reader (const struct die_reader_specs *reader,
9846 const gdb_byte *info_ptr,
9847 struct die_info *comp_unit_die,
9848 int has_children,
9849 void *datap)
3019eac3
DE
9850{
9851 struct dwarf2_cu *cu = reader->cu;
9c541725 9852 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9853 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9854 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9855 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9856 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9857 struct attribute *attr;
3019eac3
DE
9858
9859 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9860 if (attr == NULL)
9861 {
19c3d4c9
DE
9862 complaint (&symfile_complaints,
9863 _("Dwarf Error: debug entry at offset 0x%x is missing"
9864 " its dwo_id [in module %s]"),
9c541725 9865 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9866 return;
9867 }
9868
3019eac3
DE
9869 dwo_unit->dwo_file = dwo_file;
9870 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9871 dwo_unit->section = section;
9c541725 9872 dwo_unit->sect_off = sect_off;
3019eac3
DE
9873 dwo_unit->length = cu->per_cu->length;
9874
b4f54984 9875 if (dwarf_read_debug)
4031ecc5 9876 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9877 to_underlying (sect_off),
9878 hex_string (dwo_unit->signature));
3019eac3
DE
9879}
9880
33c5cd75 9881/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9882 Note: This function processes DWO files only, not DWP files. */
3019eac3 9883
33c5cd75
DB
9884static void
9885create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9886 htab_t &cus_htab)
3019eac3
DE
9887{
9888 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9889 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9890 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9891
33c5cd75
DB
9892 dwarf2_read_section (objfile, &section);
9893 info_ptr = section.buffer;
3019eac3
DE
9894
9895 if (info_ptr == NULL)
33c5cd75 9896 return;
3019eac3 9897
b4f54984 9898 if (dwarf_read_debug)
19c3d4c9
DE
9899 {
9900 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9901 get_section_name (&section),
9902 get_section_file_name (&section));
19c3d4c9 9903 }
3019eac3 9904
33c5cd75 9905 end_ptr = info_ptr + section.size;
3019eac3
DE
9906 while (info_ptr < end_ptr)
9907 {
9908 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9909 struct create_dwo_cu_data create_dwo_cu_data;
9910 struct dwo_unit *dwo_unit;
9911 void **slot;
9912 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9913
19c3d4c9
DE
9914 memset (&create_dwo_cu_data.dwo_unit, 0,
9915 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9916 memset (&per_cu, 0, sizeof (per_cu));
9917 per_cu.objfile = objfile;
9918 per_cu.is_debug_types = 0;
33c5cd75
DB
9919 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9920 per_cu.section = &section;
c5ed0576 9921 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9922
9923 init_cutu_and_read_dies_no_follow (
9924 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9925 info_ptr += per_cu.length;
9926
9927 // If the unit could not be parsed, skip it.
9928 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9929 continue;
3019eac3 9930
33c5cd75
DB
9931 if (cus_htab == NULL)
9932 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 9933
33c5cd75
DB
9934 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9935 *dwo_unit = create_dwo_cu_data.dwo_unit;
9936 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9937 gdb_assert (slot != NULL);
9938 if (*slot != NULL)
19c3d4c9 9939 {
33c5cd75
DB
9940 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9941 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 9942
33c5cd75
DB
9943 complaint (&symfile_complaints,
9944 _("debug cu entry at offset 0x%x is duplicate to"
9945 " the entry at offset 0x%x, signature %s"),
9946 to_underlying (sect_off), to_underlying (dup_sect_off),
9947 hex_string (dwo_unit->signature));
19c3d4c9 9948 }
33c5cd75 9949 *slot = (void *)dwo_unit;
3019eac3 9950 }
3019eac3
DE
9951}
9952
80626a55
DE
9953/* DWP file .debug_{cu,tu}_index section format:
9954 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9955
d2415c6c
DE
9956 DWP Version 1:
9957
80626a55
DE
9958 Both index sections have the same format, and serve to map a 64-bit
9959 signature to a set of section numbers. Each section begins with a header,
9960 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9961 indexes, and a pool of 32-bit section numbers. The index sections will be
9962 aligned at 8-byte boundaries in the file.
9963
d2415c6c
DE
9964 The index section header consists of:
9965
9966 V, 32 bit version number
9967 -, 32 bits unused
9968 N, 32 bit number of compilation units or type units in the index
9969 M, 32 bit number of slots in the hash table
80626a55 9970
d2415c6c 9971 Numbers are recorded using the byte order of the application binary.
80626a55 9972
d2415c6c
DE
9973 The hash table begins at offset 16 in the section, and consists of an array
9974 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9975 order of the application binary). Unused slots in the hash table are 0.
9976 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9977
d2415c6c
DE
9978 The parallel table begins immediately after the hash table
9979 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9980 array of 32-bit indexes (using the byte order of the application binary),
9981 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9982 table contains a 32-bit index into the pool of section numbers. For unused
9983 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9984
73869dc2
DE
9985 The pool of section numbers begins immediately following the hash table
9986 (at offset 16 + 12 * M from the beginning of the section). The pool of
9987 section numbers consists of an array of 32-bit words (using the byte order
9988 of the application binary). Each item in the array is indexed starting
9989 from 0. The hash table entry provides the index of the first section
9990 number in the set. Additional section numbers in the set follow, and the
9991 set is terminated by a 0 entry (section number 0 is not used in ELF).
9992
9993 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9994 section must be the first entry in the set, and the .debug_abbrev.dwo must
9995 be the second entry. Other members of the set may follow in any order.
9996
9997 ---
9998
9999 DWP Version 2:
10000
10001 DWP Version 2 combines all the .debug_info, etc. sections into one,
10002 and the entries in the index tables are now offsets into these sections.
10003 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10004 section.
10005
10006 Index Section Contents:
10007 Header
10008 Hash Table of Signatures dwp_hash_table.hash_table
10009 Parallel Table of Indices dwp_hash_table.unit_table
10010 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10011 Table of Section Sizes dwp_hash_table.v2.sizes
10012
10013 The index section header consists of:
10014
10015 V, 32 bit version number
10016 L, 32 bit number of columns in the table of section offsets
10017 N, 32 bit number of compilation units or type units in the index
10018 M, 32 bit number of slots in the hash table
10019
10020 Numbers are recorded using the byte order of the application binary.
10021
10022 The hash table has the same format as version 1.
10023 The parallel table of indices has the same format as version 1,
10024 except that the entries are origin-1 indices into the table of sections
10025 offsets and the table of section sizes.
10026
10027 The table of offsets begins immediately following the parallel table
10028 (at offset 16 + 12 * M from the beginning of the section). The table is
10029 a two-dimensional array of 32-bit words (using the byte order of the
10030 application binary), with L columns and N+1 rows, in row-major order.
10031 Each row in the array is indexed starting from 0. The first row provides
10032 a key to the remaining rows: each column in this row provides an identifier
10033 for a debug section, and the offsets in the same column of subsequent rows
10034 refer to that section. The section identifiers are:
10035
10036 DW_SECT_INFO 1 .debug_info.dwo
10037 DW_SECT_TYPES 2 .debug_types.dwo
10038 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10039 DW_SECT_LINE 4 .debug_line.dwo
10040 DW_SECT_LOC 5 .debug_loc.dwo
10041 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10042 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10043 DW_SECT_MACRO 8 .debug_macro.dwo
10044
10045 The offsets provided by the CU and TU index sections are the base offsets
10046 for the contributions made by each CU or TU to the corresponding section
10047 in the package file. Each CU and TU header contains an abbrev_offset
10048 field, used to find the abbreviations table for that CU or TU within the
10049 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10050 be interpreted as relative to the base offset given in the index section.
10051 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10052 should be interpreted as relative to the base offset for .debug_line.dwo,
10053 and offsets into other debug sections obtained from DWARF attributes should
10054 also be interpreted as relative to the corresponding base offset.
10055
10056 The table of sizes begins immediately following the table of offsets.
10057 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10058 with L columns and N rows, in row-major order. Each row in the array is
10059 indexed starting from 1 (row 0 is shared by the two tables).
10060
10061 ---
10062
10063 Hash table lookup is handled the same in version 1 and 2:
10064
10065 We assume that N and M will not exceed 2^32 - 1.
10066 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10067
d2415c6c
DE
10068 Given a 64-bit compilation unit signature or a type signature S, an entry
10069 in the hash table is located as follows:
80626a55 10070
d2415c6c
DE
10071 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10072 the low-order k bits all set to 1.
80626a55 10073
d2415c6c 10074 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10075
d2415c6c
DE
10076 3) If the hash table entry at index H matches the signature, use that
10077 entry. If the hash table entry at index H is unused (all zeroes),
10078 terminate the search: the signature is not present in the table.
80626a55 10079
d2415c6c 10080 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10081
d2415c6c 10082 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10083 to stop at an unused slot or find the match. */
80626a55
DE
10084
10085/* Create a hash table to map DWO IDs to their CU/TU entry in
10086 .debug_{info,types}.dwo in DWP_FILE.
10087 Returns NULL if there isn't one.
10088 Note: This function processes DWP files only, not DWO files. */
10089
10090static struct dwp_hash_table *
10091create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10092{
10093 struct objfile *objfile = dwarf2_per_objfile->objfile;
10094 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10095 const gdb_byte *index_ptr, *index_end;
80626a55 10096 struct dwarf2_section_info *index;
73869dc2 10097 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10098 struct dwp_hash_table *htab;
10099
10100 if (is_debug_types)
10101 index = &dwp_file->sections.tu_index;
10102 else
10103 index = &dwp_file->sections.cu_index;
10104
10105 if (dwarf2_section_empty_p (index))
10106 return NULL;
10107 dwarf2_read_section (objfile, index);
10108
10109 index_ptr = index->buffer;
10110 index_end = index_ptr + index->size;
10111
10112 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10113 index_ptr += 4;
10114 if (version == 2)
10115 nr_columns = read_4_bytes (dbfd, index_ptr);
10116 else
10117 nr_columns = 0;
10118 index_ptr += 4;
80626a55
DE
10119 nr_units = read_4_bytes (dbfd, index_ptr);
10120 index_ptr += 4;
10121 nr_slots = read_4_bytes (dbfd, index_ptr);
10122 index_ptr += 4;
10123
73869dc2 10124 if (version != 1 && version != 2)
80626a55 10125 {
21aa081e 10126 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10127 " [in module %s]"),
21aa081e 10128 pulongest (version), dwp_file->name);
80626a55
DE
10129 }
10130 if (nr_slots != (nr_slots & -nr_slots))
10131 {
21aa081e 10132 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10133 " is not power of 2 [in module %s]"),
21aa081e 10134 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10135 }
10136
10137 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10138 htab->version = version;
10139 htab->nr_columns = nr_columns;
80626a55
DE
10140 htab->nr_units = nr_units;
10141 htab->nr_slots = nr_slots;
10142 htab->hash_table = index_ptr;
10143 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10144
10145 /* Exit early if the table is empty. */
10146 if (nr_slots == 0 || nr_units == 0
10147 || (version == 2 && nr_columns == 0))
10148 {
10149 /* All must be zero. */
10150 if (nr_slots != 0 || nr_units != 0
10151 || (version == 2 && nr_columns != 0))
10152 {
10153 complaint (&symfile_complaints,
10154 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10155 " all zero [in modules %s]"),
10156 dwp_file->name);
10157 }
10158 return htab;
10159 }
10160
10161 if (version == 1)
10162 {
10163 htab->section_pool.v1.indices =
10164 htab->unit_table + sizeof (uint32_t) * nr_slots;
10165 /* It's harder to decide whether the section is too small in v1.
10166 V1 is deprecated anyway so we punt. */
10167 }
10168 else
10169 {
10170 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10171 int *ids = htab->section_pool.v2.section_ids;
10172 /* Reverse map for error checking. */
10173 int ids_seen[DW_SECT_MAX + 1];
10174 int i;
10175
10176 if (nr_columns < 2)
10177 {
10178 error (_("Dwarf Error: bad DWP hash table, too few columns"
10179 " in section table [in module %s]"),
10180 dwp_file->name);
10181 }
10182 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10183 {
10184 error (_("Dwarf Error: bad DWP hash table, too many columns"
10185 " in section table [in module %s]"),
10186 dwp_file->name);
10187 }
10188 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10189 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10190 for (i = 0; i < nr_columns; ++i)
10191 {
10192 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10193
10194 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10195 {
10196 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10197 " in section table [in module %s]"),
10198 id, dwp_file->name);
10199 }
10200 if (ids_seen[id] != -1)
10201 {
10202 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10203 " id %d in section table [in module %s]"),
10204 id, dwp_file->name);
10205 }
10206 ids_seen[id] = i;
10207 ids[i] = id;
10208 }
10209 /* Must have exactly one info or types section. */
10210 if (((ids_seen[DW_SECT_INFO] != -1)
10211 + (ids_seen[DW_SECT_TYPES] != -1))
10212 != 1)
10213 {
10214 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10215 " DWO info/types section [in module %s]"),
10216 dwp_file->name);
10217 }
10218 /* Must have an abbrev section. */
10219 if (ids_seen[DW_SECT_ABBREV] == -1)
10220 {
10221 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10222 " section [in module %s]"),
10223 dwp_file->name);
10224 }
10225 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10226 htab->section_pool.v2.sizes =
10227 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10228 * nr_units * nr_columns);
10229 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10230 * nr_units * nr_columns))
10231 > index_end)
10232 {
10233 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10234 " [in module %s]"),
10235 dwp_file->name);
10236 }
10237 }
80626a55
DE
10238
10239 return htab;
10240}
10241
10242/* Update SECTIONS with the data from SECTP.
10243
10244 This function is like the other "locate" section routines that are
10245 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10246 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10247
10248 The result is non-zero for success, or zero if an error was found. */
10249
10250static int
73869dc2
DE
10251locate_v1_virtual_dwo_sections (asection *sectp,
10252 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10253{
10254 const struct dwop_section_names *names = &dwop_section_names;
10255
10256 if (section_is_p (sectp->name, &names->abbrev_dwo))
10257 {
10258 /* There can be only one. */
049412e3 10259 if (sections->abbrev.s.section != NULL)
80626a55 10260 return 0;
049412e3 10261 sections->abbrev.s.section = sectp;
80626a55
DE
10262 sections->abbrev.size = bfd_get_section_size (sectp);
10263 }
10264 else if (section_is_p (sectp->name, &names->info_dwo)
10265 || section_is_p (sectp->name, &names->types_dwo))
10266 {
10267 /* There can be only one. */
049412e3 10268 if (sections->info_or_types.s.section != NULL)
80626a55 10269 return 0;
049412e3 10270 sections->info_or_types.s.section = sectp;
80626a55
DE
10271 sections->info_or_types.size = bfd_get_section_size (sectp);
10272 }
10273 else if (section_is_p (sectp->name, &names->line_dwo))
10274 {
10275 /* There can be only one. */
049412e3 10276 if (sections->line.s.section != NULL)
80626a55 10277 return 0;
049412e3 10278 sections->line.s.section = sectp;
80626a55
DE
10279 sections->line.size = bfd_get_section_size (sectp);
10280 }
10281 else if (section_is_p (sectp->name, &names->loc_dwo))
10282 {
10283 /* There can be only one. */
049412e3 10284 if (sections->loc.s.section != NULL)
80626a55 10285 return 0;
049412e3 10286 sections->loc.s.section = sectp;
80626a55
DE
10287 sections->loc.size = bfd_get_section_size (sectp);
10288 }
10289 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10290 {
10291 /* There can be only one. */
049412e3 10292 if (sections->macinfo.s.section != NULL)
80626a55 10293 return 0;
049412e3 10294 sections->macinfo.s.section = sectp;
80626a55
DE
10295 sections->macinfo.size = bfd_get_section_size (sectp);
10296 }
10297 else if (section_is_p (sectp->name, &names->macro_dwo))
10298 {
10299 /* There can be only one. */
049412e3 10300 if (sections->macro.s.section != NULL)
80626a55 10301 return 0;
049412e3 10302 sections->macro.s.section = sectp;
80626a55
DE
10303 sections->macro.size = bfd_get_section_size (sectp);
10304 }
10305 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10306 {
10307 /* There can be only one. */
049412e3 10308 if (sections->str_offsets.s.section != NULL)
80626a55 10309 return 0;
049412e3 10310 sections->str_offsets.s.section = sectp;
80626a55
DE
10311 sections->str_offsets.size = bfd_get_section_size (sectp);
10312 }
10313 else
10314 {
10315 /* No other kind of section is valid. */
10316 return 0;
10317 }
10318
10319 return 1;
10320}
10321
73869dc2
DE
10322/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10323 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10324 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10325 This is for DWP version 1 files. */
80626a55
DE
10326
10327static struct dwo_unit *
73869dc2
DE
10328create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10329 uint32_t unit_index,
10330 const char *comp_dir,
10331 ULONGEST signature, int is_debug_types)
80626a55
DE
10332{
10333 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10334 const struct dwp_hash_table *dwp_htab =
10335 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10336 bfd *dbfd = dwp_file->dbfd;
10337 const char *kind = is_debug_types ? "TU" : "CU";
10338 struct dwo_file *dwo_file;
10339 struct dwo_unit *dwo_unit;
73869dc2 10340 struct virtual_v1_dwo_sections sections;
80626a55
DE
10341 void **dwo_file_slot;
10342 char *virtual_dwo_name;
80626a55
DE
10343 struct cleanup *cleanups;
10344 int i;
10345
73869dc2
DE
10346 gdb_assert (dwp_file->version == 1);
10347
b4f54984 10348 if (dwarf_read_debug)
80626a55 10349 {
73869dc2 10350 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10351 kind,
73869dc2 10352 pulongest (unit_index), hex_string (signature),
80626a55
DE
10353 dwp_file->name);
10354 }
10355
19ac8c2e 10356 /* Fetch the sections of this DWO unit.
80626a55
DE
10357 Put a limit on the number of sections we look for so that bad data
10358 doesn't cause us to loop forever. */
10359
73869dc2 10360#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10361 (1 /* .debug_info or .debug_types */ \
10362 + 1 /* .debug_abbrev */ \
10363 + 1 /* .debug_line */ \
10364 + 1 /* .debug_loc */ \
10365 + 1 /* .debug_str_offsets */ \
19ac8c2e 10366 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10367 + 1 /* trailing zero */)
10368
10369 memset (&sections, 0, sizeof (sections));
10370 cleanups = make_cleanup (null_cleanup, 0);
10371
73869dc2 10372 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10373 {
10374 asection *sectp;
10375 uint32_t section_nr =
10376 read_4_bytes (dbfd,
73869dc2
DE
10377 dwp_htab->section_pool.v1.indices
10378 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10379
10380 if (section_nr == 0)
10381 break;
10382 if (section_nr >= dwp_file->num_sections)
10383 {
10384 error (_("Dwarf Error: bad DWP hash table, section number too large"
10385 " [in module %s]"),
10386 dwp_file->name);
10387 }
10388
10389 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10390 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10391 {
10392 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10393 " [in module %s]"),
10394 dwp_file->name);
10395 }
10396 }
10397
10398 if (i < 2
a32a8923
DE
10399 || dwarf2_section_empty_p (&sections.info_or_types)
10400 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10401 {
10402 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10403 " [in module %s]"),
10404 dwp_file->name);
10405 }
73869dc2 10406 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10407 {
10408 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10409 " [in module %s]"),
10410 dwp_file->name);
10411 }
10412
10413 /* It's easier for the rest of the code if we fake a struct dwo_file and
10414 have dwo_unit "live" in that. At least for now.
10415
10416 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10417 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10418 file, we can combine them back into a virtual DWO file to save space
10419 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10420 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10421
2792b94d
PM
10422 virtual_dwo_name =
10423 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10424 get_section_id (&sections.abbrev),
10425 get_section_id (&sections.line),
10426 get_section_id (&sections.loc),
10427 get_section_id (&sections.str_offsets));
80626a55
DE
10428 make_cleanup (xfree, virtual_dwo_name);
10429 /* Can we use an existing virtual DWO file? */
0ac5b59e 10430 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10431 /* Create one if necessary. */
10432 if (*dwo_file_slot == NULL)
10433 {
b4f54984 10434 if (dwarf_read_debug)
80626a55
DE
10435 {
10436 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10437 virtual_dwo_name);
10438 }
10439 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10440 dwo_file->dwo_name
10441 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10442 virtual_dwo_name,
10443 strlen (virtual_dwo_name));
0ac5b59e 10444 dwo_file->comp_dir = comp_dir;
80626a55
DE
10445 dwo_file->sections.abbrev = sections.abbrev;
10446 dwo_file->sections.line = sections.line;
10447 dwo_file->sections.loc = sections.loc;
10448 dwo_file->sections.macinfo = sections.macinfo;
10449 dwo_file->sections.macro = sections.macro;
10450 dwo_file->sections.str_offsets = sections.str_offsets;
10451 /* The "str" section is global to the entire DWP file. */
10452 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10453 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10454 there's no need to record it in dwo_file.
10455 Also, we can't simply record type sections in dwo_file because
10456 we record a pointer into the vector in dwo_unit. As we collect more
10457 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10458 for it, invalidating all copies of pointers into the previous
10459 contents. */
80626a55
DE
10460 *dwo_file_slot = dwo_file;
10461 }
10462 else
10463 {
b4f54984 10464 if (dwarf_read_debug)
80626a55
DE
10465 {
10466 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10467 virtual_dwo_name);
10468 }
9a3c8263 10469 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10470 }
10471 do_cleanups (cleanups);
10472
10473 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10474 dwo_unit->dwo_file = dwo_file;
10475 dwo_unit->signature = signature;
8d749320
SM
10476 dwo_unit->section =
10477 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10478 *dwo_unit->section = sections.info_or_types;
57d63ce2 10479 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10480
10481 return dwo_unit;
10482}
10483
73869dc2
DE
10484/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10485 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10486 piece within that section used by a TU/CU, return a virtual section
10487 of just that piece. */
10488
10489static struct dwarf2_section_info
10490create_dwp_v2_section (struct dwarf2_section_info *section,
10491 bfd_size_type offset, bfd_size_type size)
10492{
10493 struct dwarf2_section_info result;
10494 asection *sectp;
10495
10496 gdb_assert (section != NULL);
10497 gdb_assert (!section->is_virtual);
10498
10499 memset (&result, 0, sizeof (result));
10500 result.s.containing_section = section;
10501 result.is_virtual = 1;
10502
10503 if (size == 0)
10504 return result;
10505
10506 sectp = get_section_bfd_section (section);
10507
10508 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10509 bounds of the real section. This is a pretty-rare event, so just
10510 flag an error (easier) instead of a warning and trying to cope. */
10511 if (sectp == NULL
10512 || offset + size > bfd_get_section_size (sectp))
10513 {
10514 bfd *abfd = sectp->owner;
10515
10516 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10517 " in section %s [in module %s]"),
10518 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10519 objfile_name (dwarf2_per_objfile->objfile));
10520 }
10521
10522 result.virtual_offset = offset;
10523 result.size = size;
10524 return result;
10525}
10526
10527/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10528 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10529 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10530 This is for DWP version 2 files. */
10531
10532static struct dwo_unit *
10533create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10534 uint32_t unit_index,
10535 const char *comp_dir,
10536 ULONGEST signature, int is_debug_types)
10537{
10538 struct objfile *objfile = dwarf2_per_objfile->objfile;
10539 const struct dwp_hash_table *dwp_htab =
10540 is_debug_types ? dwp_file->tus : dwp_file->cus;
10541 bfd *dbfd = dwp_file->dbfd;
10542 const char *kind = is_debug_types ? "TU" : "CU";
10543 struct dwo_file *dwo_file;
10544 struct dwo_unit *dwo_unit;
10545 struct virtual_v2_dwo_sections sections;
10546 void **dwo_file_slot;
10547 char *virtual_dwo_name;
73869dc2
DE
10548 struct cleanup *cleanups;
10549 int i;
10550
10551 gdb_assert (dwp_file->version == 2);
10552
b4f54984 10553 if (dwarf_read_debug)
73869dc2
DE
10554 {
10555 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10556 kind,
10557 pulongest (unit_index), hex_string (signature),
10558 dwp_file->name);
10559 }
10560
10561 /* Fetch the section offsets of this DWO unit. */
10562
10563 memset (&sections, 0, sizeof (sections));
10564 cleanups = make_cleanup (null_cleanup, 0);
10565
10566 for (i = 0; i < dwp_htab->nr_columns; ++i)
10567 {
10568 uint32_t offset = read_4_bytes (dbfd,
10569 dwp_htab->section_pool.v2.offsets
10570 + (((unit_index - 1) * dwp_htab->nr_columns
10571 + i)
10572 * sizeof (uint32_t)));
10573 uint32_t size = read_4_bytes (dbfd,
10574 dwp_htab->section_pool.v2.sizes
10575 + (((unit_index - 1) * dwp_htab->nr_columns
10576 + i)
10577 * sizeof (uint32_t)));
10578
10579 switch (dwp_htab->section_pool.v2.section_ids[i])
10580 {
10581 case DW_SECT_INFO:
10582 case DW_SECT_TYPES:
10583 sections.info_or_types_offset = offset;
10584 sections.info_or_types_size = size;
10585 break;
10586 case DW_SECT_ABBREV:
10587 sections.abbrev_offset = offset;
10588 sections.abbrev_size = size;
10589 break;
10590 case DW_SECT_LINE:
10591 sections.line_offset = offset;
10592 sections.line_size = size;
10593 break;
10594 case DW_SECT_LOC:
10595 sections.loc_offset = offset;
10596 sections.loc_size = size;
10597 break;
10598 case DW_SECT_STR_OFFSETS:
10599 sections.str_offsets_offset = offset;
10600 sections.str_offsets_size = size;
10601 break;
10602 case DW_SECT_MACINFO:
10603 sections.macinfo_offset = offset;
10604 sections.macinfo_size = size;
10605 break;
10606 case DW_SECT_MACRO:
10607 sections.macro_offset = offset;
10608 sections.macro_size = size;
10609 break;
10610 }
10611 }
10612
10613 /* It's easier for the rest of the code if we fake a struct dwo_file and
10614 have dwo_unit "live" in that. At least for now.
10615
10616 The DWP file can be made up of a random collection of CUs and TUs.
10617 However, for each CU + set of TUs that came from the same original DWO
10618 file, we can combine them back into a virtual DWO file to save space
10619 (fewer struct dwo_file objects to allocate). Remember that for really
10620 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10621
10622 virtual_dwo_name =
10623 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10624 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10625 (long) (sections.line_size ? sections.line_offset : 0),
10626 (long) (sections.loc_size ? sections.loc_offset : 0),
10627 (long) (sections.str_offsets_size
10628 ? sections.str_offsets_offset : 0));
10629 make_cleanup (xfree, virtual_dwo_name);
10630 /* Can we use an existing virtual DWO file? */
10631 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10632 /* Create one if necessary. */
10633 if (*dwo_file_slot == NULL)
10634 {
b4f54984 10635 if (dwarf_read_debug)
73869dc2
DE
10636 {
10637 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10638 virtual_dwo_name);
10639 }
10640 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10641 dwo_file->dwo_name
10642 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10643 virtual_dwo_name,
10644 strlen (virtual_dwo_name));
73869dc2
DE
10645 dwo_file->comp_dir = comp_dir;
10646 dwo_file->sections.abbrev =
10647 create_dwp_v2_section (&dwp_file->sections.abbrev,
10648 sections.abbrev_offset, sections.abbrev_size);
10649 dwo_file->sections.line =
10650 create_dwp_v2_section (&dwp_file->sections.line,
10651 sections.line_offset, sections.line_size);
10652 dwo_file->sections.loc =
10653 create_dwp_v2_section (&dwp_file->sections.loc,
10654 sections.loc_offset, sections.loc_size);
10655 dwo_file->sections.macinfo =
10656 create_dwp_v2_section (&dwp_file->sections.macinfo,
10657 sections.macinfo_offset, sections.macinfo_size);
10658 dwo_file->sections.macro =
10659 create_dwp_v2_section (&dwp_file->sections.macro,
10660 sections.macro_offset, sections.macro_size);
10661 dwo_file->sections.str_offsets =
10662 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10663 sections.str_offsets_offset,
10664 sections.str_offsets_size);
10665 /* The "str" section is global to the entire DWP file. */
10666 dwo_file->sections.str = dwp_file->sections.str;
10667 /* The info or types section is assigned below to dwo_unit,
10668 there's no need to record it in dwo_file.
10669 Also, we can't simply record type sections in dwo_file because
10670 we record a pointer into the vector in dwo_unit. As we collect more
10671 types we'll grow the vector and eventually have to reallocate space
10672 for it, invalidating all copies of pointers into the previous
10673 contents. */
10674 *dwo_file_slot = dwo_file;
10675 }
10676 else
10677 {
b4f54984 10678 if (dwarf_read_debug)
73869dc2
DE
10679 {
10680 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10681 virtual_dwo_name);
10682 }
9a3c8263 10683 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10684 }
10685 do_cleanups (cleanups);
10686
10687 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10688 dwo_unit->dwo_file = dwo_file;
10689 dwo_unit->signature = signature;
8d749320
SM
10690 dwo_unit->section =
10691 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10692 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10693 ? &dwp_file->sections.types
10694 : &dwp_file->sections.info,
10695 sections.info_or_types_offset,
10696 sections.info_or_types_size);
10697 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10698
10699 return dwo_unit;
10700}
10701
57d63ce2
DE
10702/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10703 Returns NULL if the signature isn't found. */
80626a55
DE
10704
10705static struct dwo_unit *
57d63ce2
DE
10706lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10707 ULONGEST signature, int is_debug_types)
80626a55 10708{
57d63ce2
DE
10709 const struct dwp_hash_table *dwp_htab =
10710 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10711 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10712 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10713 uint32_t hash = signature & mask;
10714 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10715 unsigned int i;
10716 void **slot;
870f88f7 10717 struct dwo_unit find_dwo_cu;
80626a55
DE
10718
10719 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10720 find_dwo_cu.signature = signature;
19ac8c2e
DE
10721 slot = htab_find_slot (is_debug_types
10722 ? dwp_file->loaded_tus
10723 : dwp_file->loaded_cus,
10724 &find_dwo_cu, INSERT);
80626a55
DE
10725
10726 if (*slot != NULL)
9a3c8263 10727 return (struct dwo_unit *) *slot;
80626a55
DE
10728
10729 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10730 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10731 {
10732 ULONGEST signature_in_table;
10733
10734 signature_in_table =
57d63ce2 10735 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10736 if (signature_in_table == signature)
10737 {
57d63ce2
DE
10738 uint32_t unit_index =
10739 read_4_bytes (dbfd,
10740 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10741
73869dc2
DE
10742 if (dwp_file->version == 1)
10743 {
10744 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10745 comp_dir, signature,
10746 is_debug_types);
10747 }
10748 else
10749 {
10750 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10751 comp_dir, signature,
10752 is_debug_types);
10753 }
9a3c8263 10754 return (struct dwo_unit *) *slot;
80626a55
DE
10755 }
10756 if (signature_in_table == 0)
10757 return NULL;
10758 hash = (hash + hash2) & mask;
10759 }
10760
10761 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10762 " [in module %s]"),
10763 dwp_file->name);
10764}
10765
ab5088bf 10766/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10767 Open the file specified by FILE_NAME and hand it off to BFD for
10768 preliminary analysis. Return a newly initialized bfd *, which
10769 includes a canonicalized copy of FILE_NAME.
80626a55 10770 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10771 SEARCH_CWD is true if the current directory is to be searched.
10772 It will be searched before debug-file-directory.
13aaf454
DE
10773 If successful, the file is added to the bfd include table of the
10774 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10775 If unable to find/open the file, return NULL.
3019eac3
DE
10776 NOTE: This function is derived from symfile_bfd_open. */
10777
192b62ce 10778static gdb_bfd_ref_ptr
6ac97d4c 10779try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10780{
80626a55 10781 int desc, flags;
3019eac3 10782 char *absolute_name;
9c02c129
DE
10783 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10784 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10785 to debug_file_directory. */
10786 char *search_path;
10787 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10788
6ac97d4c
DE
10789 if (search_cwd)
10790 {
10791 if (*debug_file_directory != '\0')
10792 search_path = concat (".", dirname_separator_string,
b36cec19 10793 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10794 else
10795 search_path = xstrdup (".");
10796 }
9c02c129 10797 else
6ac97d4c 10798 search_path = xstrdup (debug_file_directory);
3019eac3 10799
492c0ab7 10800 flags = OPF_RETURN_REALPATH;
80626a55
DE
10801 if (is_dwp)
10802 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10803 desc = openp (search_path, flags, file_name,
3019eac3 10804 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10805 xfree (search_path);
3019eac3
DE
10806 if (desc < 0)
10807 return NULL;
10808
192b62ce 10809 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10810 xfree (absolute_name);
9c02c129
DE
10811 if (sym_bfd == NULL)
10812 return NULL;
192b62ce 10813 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10814
192b62ce
TT
10815 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10816 return NULL;
3019eac3 10817
13aaf454
DE
10818 /* Success. Record the bfd as having been included by the objfile's bfd.
10819 This is important because things like demangled_names_hash lives in the
10820 objfile's per_bfd space and may have references to things like symbol
10821 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10822 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10823
3019eac3
DE
10824 return sym_bfd;
10825}
10826
ab5088bf 10827/* Try to open DWO file FILE_NAME.
3019eac3
DE
10828 COMP_DIR is the DW_AT_comp_dir attribute.
10829 The result is the bfd handle of the file.
10830 If there is a problem finding or opening the file, return NULL.
10831 Upon success, the canonicalized path of the file is stored in the bfd,
10832 same as symfile_bfd_open. */
10833
192b62ce 10834static gdb_bfd_ref_ptr
ab5088bf 10835open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10836{
80626a55 10837 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10838 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10839
10840 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10841
10842 if (comp_dir != NULL)
10843 {
b36cec19
PA
10844 char *path_to_try = concat (comp_dir, SLASH_STRING,
10845 file_name, (char *) NULL);
3019eac3
DE
10846
10847 /* NOTE: If comp_dir is a relative path, this will also try the
10848 search path, which seems useful. */
192b62ce
TT
10849 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10850 1 /*search_cwd*/));
3019eac3
DE
10851 xfree (path_to_try);
10852 if (abfd != NULL)
10853 return abfd;
10854 }
10855
10856 /* That didn't work, try debug-file-directory, which, despite its name,
10857 is a list of paths. */
10858
10859 if (*debug_file_directory == '\0')
10860 return NULL;
10861
6ac97d4c 10862 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10863}
10864
80626a55
DE
10865/* This function is mapped across the sections and remembers the offset and
10866 size of each of the DWO debugging sections we are interested in. */
10867
10868static void
10869dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10870{
9a3c8263 10871 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10872 const struct dwop_section_names *names = &dwop_section_names;
10873
10874 if (section_is_p (sectp->name, &names->abbrev_dwo))
10875 {
049412e3 10876 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10877 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10878 }
10879 else if (section_is_p (sectp->name, &names->info_dwo))
10880 {
049412e3 10881 dwo_sections->info.s.section = sectp;
80626a55
DE
10882 dwo_sections->info.size = bfd_get_section_size (sectp);
10883 }
10884 else if (section_is_p (sectp->name, &names->line_dwo))
10885 {
049412e3 10886 dwo_sections->line.s.section = sectp;
80626a55
DE
10887 dwo_sections->line.size = bfd_get_section_size (sectp);
10888 }
10889 else if (section_is_p (sectp->name, &names->loc_dwo))
10890 {
049412e3 10891 dwo_sections->loc.s.section = sectp;
80626a55
DE
10892 dwo_sections->loc.size = bfd_get_section_size (sectp);
10893 }
10894 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10895 {
049412e3 10896 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10897 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10898 }
10899 else if (section_is_p (sectp->name, &names->macro_dwo))
10900 {
049412e3 10901 dwo_sections->macro.s.section = sectp;
80626a55
DE
10902 dwo_sections->macro.size = bfd_get_section_size (sectp);
10903 }
10904 else if (section_is_p (sectp->name, &names->str_dwo))
10905 {
049412e3 10906 dwo_sections->str.s.section = sectp;
80626a55
DE
10907 dwo_sections->str.size = bfd_get_section_size (sectp);
10908 }
10909 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10910 {
049412e3 10911 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10912 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10913 }
10914 else if (section_is_p (sectp->name, &names->types_dwo))
10915 {
10916 struct dwarf2_section_info type_section;
10917
10918 memset (&type_section, 0, sizeof (type_section));
049412e3 10919 type_section.s.section = sectp;
80626a55
DE
10920 type_section.size = bfd_get_section_size (sectp);
10921 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10922 &type_section);
10923 }
10924}
10925
ab5088bf 10926/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10927 by PER_CU. This is for the non-DWP case.
80626a55 10928 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10929
10930static struct dwo_file *
0ac5b59e
DE
10931open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10932 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10933{
10934 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10935 struct dwo_file *dwo_file;
3019eac3
DE
10936 struct cleanup *cleanups;
10937
192b62ce 10938 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10939 if (dbfd == NULL)
10940 {
b4f54984 10941 if (dwarf_read_debug)
80626a55
DE
10942 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10943 return NULL;
10944 }
10945 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10946 dwo_file->dwo_name = dwo_name;
10947 dwo_file->comp_dir = comp_dir;
192b62ce 10948 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10949
10950 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10951
192b62ce
TT
10952 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10953 &dwo_file->sections);
3019eac3 10954
33c5cd75 10955 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 10956
78d4d2c5
JK
10957 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10958 dwo_file->tus);
3019eac3
DE
10959
10960 discard_cleanups (cleanups);
10961
b4f54984 10962 if (dwarf_read_debug)
80626a55
DE
10963 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10964
3019eac3
DE
10965 return dwo_file;
10966}
10967
80626a55 10968/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10969 size of each of the DWP debugging sections common to version 1 and 2 that
10970 we are interested in. */
3019eac3 10971
80626a55 10972static void
73869dc2
DE
10973dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10974 void *dwp_file_ptr)
3019eac3 10975{
9a3c8263 10976 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10977 const struct dwop_section_names *names = &dwop_section_names;
10978 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10979
80626a55 10980 /* Record the ELF section number for later lookup: this is what the
73869dc2 10981 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10982 gdb_assert (elf_section_nr < dwp_file->num_sections);
10983 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10984
80626a55
DE
10985 /* Look for specific sections that we need. */
10986 if (section_is_p (sectp->name, &names->str_dwo))
10987 {
049412e3 10988 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10989 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10990 }
10991 else if (section_is_p (sectp->name, &names->cu_index))
10992 {
049412e3 10993 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10994 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10995 }
10996 else if (section_is_p (sectp->name, &names->tu_index))
10997 {
049412e3 10998 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10999 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11000 }
11001}
3019eac3 11002
73869dc2
DE
11003/* This function is mapped across the sections and remembers the offset and
11004 size of each of the DWP version 2 debugging sections that we are interested
11005 in. This is split into a separate function because we don't know if we
11006 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11007
11008static void
11009dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11010{
9a3c8263 11011 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11012 const struct dwop_section_names *names = &dwop_section_names;
11013 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11014
11015 /* Record the ELF section number for later lookup: this is what the
11016 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11017 gdb_assert (elf_section_nr < dwp_file->num_sections);
11018 dwp_file->elf_sections[elf_section_nr] = sectp;
11019
11020 /* Look for specific sections that we need. */
11021 if (section_is_p (sectp->name, &names->abbrev_dwo))
11022 {
049412e3 11023 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11024 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11025 }
11026 else if (section_is_p (sectp->name, &names->info_dwo))
11027 {
049412e3 11028 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11029 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11030 }
11031 else if (section_is_p (sectp->name, &names->line_dwo))
11032 {
049412e3 11033 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11034 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11035 }
11036 else if (section_is_p (sectp->name, &names->loc_dwo))
11037 {
049412e3 11038 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11039 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11040 }
11041 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11042 {
049412e3 11043 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11044 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11045 }
11046 else if (section_is_p (sectp->name, &names->macro_dwo))
11047 {
049412e3 11048 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11049 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11050 }
11051 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11052 {
049412e3 11053 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11054 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11055 }
11056 else if (section_is_p (sectp->name, &names->types_dwo))
11057 {
049412e3 11058 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11059 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11060 }
11061}
11062
80626a55 11063/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11064
80626a55
DE
11065static hashval_t
11066hash_dwp_loaded_cutus (const void *item)
11067{
9a3c8263 11068 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11069
80626a55
DE
11070 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11071 return dwo_unit->signature;
3019eac3
DE
11072}
11073
80626a55 11074/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11075
80626a55
DE
11076static int
11077eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11078{
9a3c8263
SM
11079 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11080 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11081
80626a55
DE
11082 return dua->signature == dub->signature;
11083}
3019eac3 11084
80626a55 11085/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11086
80626a55
DE
11087static htab_t
11088allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11089{
11090 return htab_create_alloc_ex (3,
11091 hash_dwp_loaded_cutus,
11092 eq_dwp_loaded_cutus,
11093 NULL,
11094 &objfile->objfile_obstack,
11095 hashtab_obstack_allocate,
11096 dummy_obstack_deallocate);
11097}
3019eac3 11098
ab5088bf
DE
11099/* Try to open DWP file FILE_NAME.
11100 The result is the bfd handle of the file.
11101 If there is a problem finding or opening the file, return NULL.
11102 Upon success, the canonicalized path of the file is stored in the bfd,
11103 same as symfile_bfd_open. */
11104
192b62ce 11105static gdb_bfd_ref_ptr
ab5088bf
DE
11106open_dwp_file (const char *file_name)
11107{
192b62ce
TT
11108 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11109 1 /*search_cwd*/));
6ac97d4c
DE
11110 if (abfd != NULL)
11111 return abfd;
11112
11113 /* Work around upstream bug 15652.
11114 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11115 [Whether that's a "bug" is debatable, but it is getting in our way.]
11116 We have no real idea where the dwp file is, because gdb's realpath-ing
11117 of the executable's path may have discarded the needed info.
11118 [IWBN if the dwp file name was recorded in the executable, akin to
11119 .gnu_debuglink, but that doesn't exist yet.]
11120 Strip the directory from FILE_NAME and search again. */
11121 if (*debug_file_directory != '\0')
11122 {
11123 /* Don't implicitly search the current directory here.
11124 If the user wants to search "." to handle this case,
11125 it must be added to debug-file-directory. */
11126 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11127 0 /*search_cwd*/);
11128 }
11129
11130 return NULL;
ab5088bf
DE
11131}
11132
80626a55
DE
11133/* Initialize the use of the DWP file for the current objfile.
11134 By convention the name of the DWP file is ${objfile}.dwp.
11135 The result is NULL if it can't be found. */
a766d390 11136
80626a55 11137static struct dwp_file *
ab5088bf 11138open_and_init_dwp_file (void)
80626a55
DE
11139{
11140 struct objfile *objfile = dwarf2_per_objfile->objfile;
11141 struct dwp_file *dwp_file;
80626a55 11142
82bf32bc
JK
11143 /* Try to find first .dwp for the binary file before any symbolic links
11144 resolving. */
6c447423
DE
11145
11146 /* If the objfile is a debug file, find the name of the real binary
11147 file and get the name of dwp file from there. */
d721ba37 11148 std::string dwp_name;
6c447423
DE
11149 if (objfile->separate_debug_objfile_backlink != NULL)
11150 {
11151 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11152 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11153
d721ba37 11154 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11155 }
11156 else
d721ba37
PA
11157 dwp_name = objfile->original_name;
11158
11159 dwp_name += ".dwp";
80626a55 11160
d721ba37 11161 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11162 if (dbfd == NULL
11163 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11164 {
11165 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11166 dwp_name = objfile_name (objfile);
11167 dwp_name += ".dwp";
11168 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11169 }
11170
80626a55
DE
11171 if (dbfd == NULL)
11172 {
b4f54984 11173 if (dwarf_read_debug)
d721ba37 11174 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11175 return NULL;
3019eac3 11176 }
80626a55 11177 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11178 dwp_file->name = bfd_get_filename (dbfd.get ());
11179 dwp_file->dbfd = dbfd.release ();
c906108c 11180
80626a55 11181 /* +1: section 0 is unused */
192b62ce 11182 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11183 dwp_file->elf_sections =
11184 OBSTACK_CALLOC (&objfile->objfile_obstack,
11185 dwp_file->num_sections, asection *);
11186
192b62ce
TT
11187 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11188 dwp_file);
80626a55
DE
11189
11190 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11191
11192 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11193
73869dc2
DE
11194 /* The DWP file version is stored in the hash table. Oh well. */
11195 if (dwp_file->cus->version != dwp_file->tus->version)
11196 {
11197 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11198 pretty bizarre. We use pulongest here because that's the established
4d65956b 11199 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11200 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11201 " TU version %s [in DWP file %s]"),
11202 pulongest (dwp_file->cus->version),
d721ba37 11203 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11204 }
11205 dwp_file->version = dwp_file->cus->version;
11206
11207 if (dwp_file->version == 2)
192b62ce
TT
11208 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11209 dwp_file);
73869dc2 11210
19ac8c2e
DE
11211 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11212 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11213
b4f54984 11214 if (dwarf_read_debug)
80626a55
DE
11215 {
11216 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11217 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11218 " %s CUs, %s TUs\n",
11219 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11220 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11221 }
11222
11223 return dwp_file;
3019eac3 11224}
c906108c 11225
ab5088bf
DE
11226/* Wrapper around open_and_init_dwp_file, only open it once. */
11227
11228static struct dwp_file *
11229get_dwp_file (void)
11230{
11231 if (! dwarf2_per_objfile->dwp_checked)
11232 {
11233 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11234 dwarf2_per_objfile->dwp_checked = 1;
11235 }
11236 return dwarf2_per_objfile->dwp_file;
11237}
11238
80626a55
DE
11239/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11240 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11241 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11242 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11243 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11244
11245 This is called, for example, when wanting to read a variable with a
11246 complex location. Therefore we don't want to do file i/o for every call.
11247 Therefore we don't want to look for a DWO file on every call.
11248 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11249 then we check if we've already seen DWO_NAME, and only THEN do we check
11250 for a DWO file.
11251
1c658ad5 11252 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11253 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11254
3019eac3 11255static struct dwo_unit *
80626a55
DE
11256lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11257 const char *dwo_name, const char *comp_dir,
11258 ULONGEST signature, int is_debug_types)
3019eac3
DE
11259{
11260 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11261 const char *kind = is_debug_types ? "TU" : "CU";
11262 void **dwo_file_slot;
3019eac3 11263 struct dwo_file *dwo_file;
80626a55 11264 struct dwp_file *dwp_file;
cb1df416 11265
6a506a2d
DE
11266 /* First see if there's a DWP file.
11267 If we have a DWP file but didn't find the DWO inside it, don't
11268 look for the original DWO file. It makes gdb behave differently
11269 depending on whether one is debugging in the build tree. */
cf2c3c16 11270
ab5088bf 11271 dwp_file = get_dwp_file ();
80626a55 11272 if (dwp_file != NULL)
cf2c3c16 11273 {
80626a55
DE
11274 const struct dwp_hash_table *dwp_htab =
11275 is_debug_types ? dwp_file->tus : dwp_file->cus;
11276
11277 if (dwp_htab != NULL)
11278 {
11279 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11280 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11281 signature, is_debug_types);
80626a55
DE
11282
11283 if (dwo_cutu != NULL)
11284 {
b4f54984 11285 if (dwarf_read_debug)
80626a55
DE
11286 {
11287 fprintf_unfiltered (gdb_stdlog,
11288 "Virtual DWO %s %s found: @%s\n",
11289 kind, hex_string (signature),
11290 host_address_to_string (dwo_cutu));
11291 }
11292 return dwo_cutu;
11293 }
11294 }
11295 }
6a506a2d 11296 else
80626a55 11297 {
6a506a2d 11298 /* No DWP file, look for the DWO file. */
80626a55 11299
6a506a2d
DE
11300 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11301 if (*dwo_file_slot == NULL)
80626a55 11302 {
6a506a2d
DE
11303 /* Read in the file and build a table of the CUs/TUs it contains. */
11304 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11305 }
6a506a2d 11306 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11307 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11308
6a506a2d 11309 if (dwo_file != NULL)
19c3d4c9 11310 {
6a506a2d
DE
11311 struct dwo_unit *dwo_cutu = NULL;
11312
11313 if (is_debug_types && dwo_file->tus)
11314 {
11315 struct dwo_unit find_dwo_cutu;
11316
11317 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11318 find_dwo_cutu.signature = signature;
9a3c8263
SM
11319 dwo_cutu
11320 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11321 }
33c5cd75 11322 else if (!is_debug_types && dwo_file->cus)
80626a55 11323 {
33c5cd75
DB
11324 struct dwo_unit find_dwo_cutu;
11325
11326 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11327 find_dwo_cutu.signature = signature;
11328 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11329 &find_dwo_cutu);
6a506a2d
DE
11330 }
11331
11332 if (dwo_cutu != NULL)
11333 {
b4f54984 11334 if (dwarf_read_debug)
6a506a2d
DE
11335 {
11336 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11337 kind, dwo_name, hex_string (signature),
11338 host_address_to_string (dwo_cutu));
11339 }
11340 return dwo_cutu;
80626a55
DE
11341 }
11342 }
2e276125 11343 }
9cdd5dbd 11344
80626a55
DE
11345 /* We didn't find it. This could mean a dwo_id mismatch, or
11346 someone deleted the DWO/DWP file, or the search path isn't set up
11347 correctly to find the file. */
11348
b4f54984 11349 if (dwarf_read_debug)
80626a55
DE
11350 {
11351 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11352 kind, dwo_name, hex_string (signature));
11353 }
3019eac3 11354
6656a72d
DE
11355 /* This is a warning and not a complaint because it can be caused by
11356 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11357 {
11358 /* Print the name of the DWP file if we looked there, helps the user
11359 better diagnose the problem. */
11360 char *dwp_text = NULL;
11361 struct cleanup *cleanups;
11362
11363 if (dwp_file != NULL)
11364 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11365 cleanups = make_cleanup (xfree, dwp_text);
11366
11367 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11368 " [in module %s]"),
11369 kind, dwo_name, hex_string (signature),
11370 dwp_text != NULL ? dwp_text : "",
11371 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11372 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11373
11374 do_cleanups (cleanups);
11375 }
3019eac3 11376 return NULL;
5fb290d7
DJ
11377}
11378
80626a55
DE
11379/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11380 See lookup_dwo_cutu_unit for details. */
11381
11382static struct dwo_unit *
11383lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11384 const char *dwo_name, const char *comp_dir,
11385 ULONGEST signature)
11386{
11387 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11388}
11389
11390/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11391 See lookup_dwo_cutu_unit for details. */
11392
11393static struct dwo_unit *
11394lookup_dwo_type_unit (struct signatured_type *this_tu,
11395 const char *dwo_name, const char *comp_dir)
11396{
11397 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11398}
11399
89e63ee4
DE
11400/* Traversal function for queue_and_load_all_dwo_tus. */
11401
11402static int
11403queue_and_load_dwo_tu (void **slot, void *info)
11404{
11405 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11406 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11407 ULONGEST signature = dwo_unit->signature;
11408 struct signatured_type *sig_type =
11409 lookup_dwo_signatured_type (per_cu->cu, signature);
11410
11411 if (sig_type != NULL)
11412 {
11413 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11414
11415 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11416 a real dependency of PER_CU on SIG_TYPE. That is detected later
11417 while processing PER_CU. */
11418 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11419 load_full_type_unit (sig_cu);
11420 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11421 }
11422
11423 return 1;
11424}
11425
11426/* Queue all TUs contained in the DWO of PER_CU to be read in.
11427 The DWO may have the only definition of the type, though it may not be
11428 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11429 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11430
11431static void
11432queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11433{
11434 struct dwo_unit *dwo_unit;
11435 struct dwo_file *dwo_file;
11436
11437 gdb_assert (!per_cu->is_debug_types);
11438 gdb_assert (get_dwp_file () == NULL);
11439 gdb_assert (per_cu->cu != NULL);
11440
11441 dwo_unit = per_cu->cu->dwo_unit;
11442 gdb_assert (dwo_unit != NULL);
11443
11444 dwo_file = dwo_unit->dwo_file;
11445 if (dwo_file->tus != NULL)
11446 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11447}
11448
3019eac3
DE
11449/* Free all resources associated with DWO_FILE.
11450 Close the DWO file and munmap the sections.
11451 All memory should be on the objfile obstack. */
348e048f
DE
11452
11453static void
3019eac3 11454free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11455{
348e048f 11456
5c6fa7ab 11457 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11458 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11459
3019eac3
DE
11460 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11461}
348e048f 11462
3019eac3 11463/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11464
3019eac3
DE
11465static void
11466free_dwo_file_cleanup (void *arg)
11467{
11468 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11469 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11470
3019eac3
DE
11471 free_dwo_file (dwo_file, objfile);
11472}
348e048f 11473
3019eac3 11474/* Traversal function for free_dwo_files. */
2ab95328 11475
3019eac3
DE
11476static int
11477free_dwo_file_from_slot (void **slot, void *info)
11478{
11479 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11480 struct objfile *objfile = (struct objfile *) info;
348e048f 11481
3019eac3 11482 free_dwo_file (dwo_file, objfile);
348e048f 11483
3019eac3
DE
11484 return 1;
11485}
348e048f 11486
3019eac3 11487/* Free all resources associated with DWO_FILES. */
348e048f 11488
3019eac3
DE
11489static void
11490free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11491{
11492 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11493}
3019eac3
DE
11494\f
11495/* Read in various DIEs. */
348e048f 11496
d389af10
JK
11497/* qsort helper for inherit_abstract_dies. */
11498
11499static int
11500unsigned_int_compar (const void *ap, const void *bp)
11501{
11502 unsigned int a = *(unsigned int *) ap;
11503 unsigned int b = *(unsigned int *) bp;
11504
11505 return (a > b) - (b > a);
11506}
11507
11508/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11509 Inherit only the children of the DW_AT_abstract_origin DIE not being
11510 already referenced by DW_AT_abstract_origin from the children of the
11511 current DIE. */
d389af10
JK
11512
11513static void
11514inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11515{
11516 struct die_info *child_die;
11517 unsigned die_children_count;
11518 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11519 sect_offset *offsets;
11520 sect_offset *offsets_end, *offsetp;
d389af10
JK
11521 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11522 struct die_info *origin_die;
11523 /* Iterator of the ORIGIN_DIE children. */
11524 struct die_info *origin_child_die;
11525 struct cleanup *cleanups;
11526 struct attribute *attr;
cd02d79d
PA
11527 struct dwarf2_cu *origin_cu;
11528 struct pending **origin_previous_list_in_scope;
d389af10
JK
11529
11530 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11531 if (!attr)
11532 return;
11533
cd02d79d
PA
11534 /* Note that following die references may follow to a die in a
11535 different cu. */
11536
11537 origin_cu = cu;
11538 origin_die = follow_die_ref (die, attr, &origin_cu);
11539
11540 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11541 symbols in. */
11542 origin_previous_list_in_scope = origin_cu->list_in_scope;
11543 origin_cu->list_in_scope = cu->list_in_scope;
11544
edb3359d
DJ
11545 if (die->tag != origin_die->tag
11546 && !(die->tag == DW_TAG_inlined_subroutine
11547 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11548 complaint (&symfile_complaints,
11549 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11550 to_underlying (die->sect_off),
11551 to_underlying (origin_die->sect_off));
d389af10
JK
11552
11553 child_die = die->child;
11554 die_children_count = 0;
11555 while (child_die && child_die->tag)
11556 {
11557 child_die = sibling_die (child_die);
11558 die_children_count++;
11559 }
8d749320 11560 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11561 cleanups = make_cleanup (xfree, offsets);
11562
11563 offsets_end = offsets;
3ea89b92
PMR
11564 for (child_die = die->child;
11565 child_die && child_die->tag;
11566 child_die = sibling_die (child_die))
11567 {
11568 struct die_info *child_origin_die;
11569 struct dwarf2_cu *child_origin_cu;
11570
11571 /* We are trying to process concrete instance entries:
216f72a1 11572 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11573 it's not relevant to our analysis here. i.e. detecting DIEs that are
11574 present in the abstract instance but not referenced in the concrete
11575 one. */
216f72a1
JK
11576 if (child_die->tag == DW_TAG_call_site
11577 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11578 continue;
11579
c38f313d
DJ
11580 /* For each CHILD_DIE, find the corresponding child of
11581 ORIGIN_DIE. If there is more than one layer of
11582 DW_AT_abstract_origin, follow them all; there shouldn't be,
11583 but GCC versions at least through 4.4 generate this (GCC PR
11584 40573). */
3ea89b92
PMR
11585 child_origin_die = child_die;
11586 child_origin_cu = cu;
c38f313d
DJ
11587 while (1)
11588 {
cd02d79d
PA
11589 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11590 child_origin_cu);
c38f313d
DJ
11591 if (attr == NULL)
11592 break;
cd02d79d
PA
11593 child_origin_die = follow_die_ref (child_origin_die, attr,
11594 &child_origin_cu);
c38f313d
DJ
11595 }
11596
d389af10
JK
11597 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11598 counterpart may exist. */
c38f313d 11599 if (child_origin_die != child_die)
d389af10 11600 {
edb3359d
DJ
11601 if (child_die->tag != child_origin_die->tag
11602 && !(child_die->tag == DW_TAG_inlined_subroutine
11603 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11604 complaint (&symfile_complaints,
11605 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11606 "different tags"),
11607 to_underlying (child_die->sect_off),
11608 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11609 if (child_origin_die->parent != origin_die)
11610 complaint (&symfile_complaints,
11611 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11612 "different parents"),
11613 to_underlying (child_die->sect_off),
11614 to_underlying (child_origin_die->sect_off));
c38f313d 11615 else
9c541725 11616 *offsets_end++ = child_origin_die->sect_off;
d389af10 11617 }
d389af10
JK
11618 }
11619 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11620 unsigned_int_compar);
11621 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11622 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11623 complaint (&symfile_complaints,
11624 _("Multiple children of DIE 0x%x refer "
11625 "to DIE 0x%x as their abstract origin"),
9c541725 11626 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11627
11628 offsetp = offsets;
11629 origin_child_die = origin_die->child;
11630 while (origin_child_die && origin_child_die->tag)
11631 {
11632 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11633 while (offsetp < offsets_end
9c541725 11634 && *offsetp < origin_child_die->sect_off)
d389af10 11635 offsetp++;
b64f50a1 11636 if (offsetp >= offsets_end
9c541725 11637 || *offsetp > origin_child_die->sect_off)
d389af10 11638 {
adde2bff
DE
11639 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11640 Check whether we're already processing ORIGIN_CHILD_DIE.
11641 This can happen with mutually referenced abstract_origins.
11642 PR 16581. */
11643 if (!origin_child_die->in_process)
11644 process_die (origin_child_die, origin_cu);
d389af10
JK
11645 }
11646 origin_child_die = sibling_die (origin_child_die);
11647 }
cd02d79d 11648 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11649
11650 do_cleanups (cleanups);
11651}
11652
c906108c 11653static void
e7c27a73 11654read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11655{
e7c27a73 11656 struct objfile *objfile = cu->objfile;
3e29f34a 11657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11658 struct context_stack *newobj;
c906108c
SS
11659 CORE_ADDR lowpc;
11660 CORE_ADDR highpc;
11661 struct die_info *child_die;
edb3359d 11662 struct attribute *attr, *call_line, *call_file;
15d034d0 11663 const char *name;
e142c38c 11664 CORE_ADDR baseaddr;
801e3a5b 11665 struct block *block;
edb3359d 11666 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11667 VEC (symbolp) *template_args = NULL;
11668 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11669
11670 if (inlined_func)
11671 {
11672 /* If we do not have call site information, we can't show the
11673 caller of this inlined function. That's too confusing, so
11674 only use the scope for local variables. */
11675 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11676 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11677 if (call_line == NULL || call_file == NULL)
11678 {
11679 read_lexical_block_scope (die, cu);
11680 return;
11681 }
11682 }
c906108c 11683
e142c38c
DJ
11684 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11685
94af9270 11686 name = dwarf2_name (die, cu);
c906108c 11687
e8d05480
JB
11688 /* Ignore functions with missing or empty names. These are actually
11689 illegal according to the DWARF standard. */
11690 if (name == NULL)
11691 {
11692 complaint (&symfile_complaints,
b64f50a1 11693 _("missing name for subprogram DIE at %d"),
9c541725 11694 to_underlying (die->sect_off));
e8d05480
JB
11695 return;
11696 }
11697
11698 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11699 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11700 <= PC_BOUNDS_INVALID)
e8d05480 11701 {
ae4d0c03
PM
11702 attr = dwarf2_attr (die, DW_AT_external, cu);
11703 if (!attr || !DW_UNSND (attr))
11704 complaint (&symfile_complaints,
3e43a32a
MS
11705 _("cannot get low and high bounds "
11706 "for subprogram DIE at %d"),
9c541725 11707 to_underlying (die->sect_off));
e8d05480
JB
11708 return;
11709 }
c906108c 11710
3e29f34a
MR
11711 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11712 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11713
34eaf542
TT
11714 /* If we have any template arguments, then we must allocate a
11715 different sort of symbol. */
11716 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11717 {
11718 if (child_die->tag == DW_TAG_template_type_param
11719 || child_die->tag == DW_TAG_template_value_param)
11720 {
e623cf5d 11721 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11722 templ_func->base.is_cplus_template_function = 1;
11723 break;
11724 }
11725 }
11726
fe978cb0
PA
11727 newobj = push_context (0, lowpc);
11728 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11729 (struct symbol *) templ_func);
4c2df51b 11730
4cecd739
DJ
11731 /* If there is a location expression for DW_AT_frame_base, record
11732 it. */
e142c38c 11733 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11734 if (attr)
fe978cb0 11735 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11736
63e43d3a
PMR
11737 /* If there is a location for the static link, record it. */
11738 newobj->static_link = NULL;
11739 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11740 if (attr)
11741 {
224c3ddb
SM
11742 newobj->static_link
11743 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11744 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11745 }
11746
e142c38c 11747 cu->list_in_scope = &local_symbols;
c906108c 11748
639d11d3 11749 if (die->child != NULL)
c906108c 11750 {
639d11d3 11751 child_die = die->child;
c906108c
SS
11752 while (child_die && child_die->tag)
11753 {
34eaf542
TT
11754 if (child_die->tag == DW_TAG_template_type_param
11755 || child_die->tag == DW_TAG_template_value_param)
11756 {
11757 struct symbol *arg = new_symbol (child_die, NULL, cu);
11758
f1078f66
DJ
11759 if (arg != NULL)
11760 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11761 }
11762 else
11763 process_die (child_die, cu);
c906108c
SS
11764 child_die = sibling_die (child_die);
11765 }
11766 }
11767
d389af10
JK
11768 inherit_abstract_dies (die, cu);
11769
4a811a97
UW
11770 /* If we have a DW_AT_specification, we might need to import using
11771 directives from the context of the specification DIE. See the
11772 comment in determine_prefix. */
11773 if (cu->language == language_cplus
11774 && dwarf2_attr (die, DW_AT_specification, cu))
11775 {
11776 struct dwarf2_cu *spec_cu = cu;
11777 struct die_info *spec_die = die_specification (die, &spec_cu);
11778
11779 while (spec_die)
11780 {
11781 child_die = spec_die->child;
11782 while (child_die && child_die->tag)
11783 {
11784 if (child_die->tag == DW_TAG_imported_module)
11785 process_die (child_die, spec_cu);
11786 child_die = sibling_die (child_die);
11787 }
11788
11789 /* In some cases, GCC generates specification DIEs that
11790 themselves contain DW_AT_specification attributes. */
11791 spec_die = die_specification (spec_die, &spec_cu);
11792 }
11793 }
11794
fe978cb0 11795 newobj = pop_context ();
c906108c 11796 /* Make a block for the local symbols within. */
fe978cb0 11797 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11798 newobj->static_link, lowpc, highpc);
801e3a5b 11799
df8a16a1 11800 /* For C++, set the block's scope. */
45280282
IB
11801 if ((cu->language == language_cplus
11802 || cu->language == language_fortran
c44af4eb
TT
11803 || cu->language == language_d
11804 || cu->language == language_rust)
4d4ec4e5 11805 && cu->processing_has_namespace_info)
195a3f6c
TT
11806 block_set_scope (block, determine_prefix (die, cu),
11807 &objfile->objfile_obstack);
df8a16a1 11808
801e3a5b
JB
11809 /* If we have address ranges, record them. */
11810 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11811
fe978cb0 11812 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11813
34eaf542
TT
11814 /* Attach template arguments to function. */
11815 if (! VEC_empty (symbolp, template_args))
11816 {
11817 gdb_assert (templ_func != NULL);
11818
11819 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11820 templ_func->template_arguments
8d749320
SM
11821 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11822 templ_func->n_template_arguments);
34eaf542
TT
11823 memcpy (templ_func->template_arguments,
11824 VEC_address (symbolp, template_args),
11825 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11826 VEC_free (symbolp, template_args);
11827 }
11828
208d8187
JB
11829 /* In C++, we can have functions nested inside functions (e.g., when
11830 a function declares a class that has methods). This means that
11831 when we finish processing a function scope, we may need to go
11832 back to building a containing block's symbol lists. */
fe978cb0 11833 local_symbols = newobj->locals;
22cee43f 11834 local_using_directives = newobj->local_using_directives;
208d8187 11835
921e78cf
JB
11836 /* If we've finished processing a top-level function, subsequent
11837 symbols go in the file symbol list. */
11838 if (outermost_context_p ())
e142c38c 11839 cu->list_in_scope = &file_symbols;
c906108c
SS
11840}
11841
11842/* Process all the DIES contained within a lexical block scope. Start
11843 a new scope, process the dies, and then close the scope. */
11844
11845static void
e7c27a73 11846read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11847{
e7c27a73 11848 struct objfile *objfile = cu->objfile;
3e29f34a 11849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11850 struct context_stack *newobj;
c906108c
SS
11851 CORE_ADDR lowpc, highpc;
11852 struct die_info *child_die;
e142c38c
DJ
11853 CORE_ADDR baseaddr;
11854
11855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11856
11857 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11858 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11859 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11860 be nasty. Might be easier to properly extend generic blocks to
af34e669 11861 describe ranges. */
e385593e
JK
11862 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11863 {
11864 case PC_BOUNDS_NOT_PRESENT:
11865 /* DW_TAG_lexical_block has no attributes, process its children as if
11866 there was no wrapping by that DW_TAG_lexical_block.
11867 GCC does no longer produces such DWARF since GCC r224161. */
11868 for (child_die = die->child;
11869 child_die != NULL && child_die->tag;
11870 child_die = sibling_die (child_die))
11871 process_die (child_die, cu);
11872 return;
11873 case PC_BOUNDS_INVALID:
11874 return;
11875 }
3e29f34a
MR
11876 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11877 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11878
11879 push_context (0, lowpc);
639d11d3 11880 if (die->child != NULL)
c906108c 11881 {
639d11d3 11882 child_die = die->child;
c906108c
SS
11883 while (child_die && child_die->tag)
11884 {
e7c27a73 11885 process_die (child_die, cu);
c906108c
SS
11886 child_die = sibling_die (child_die);
11887 }
11888 }
3ea89b92 11889 inherit_abstract_dies (die, cu);
fe978cb0 11890 newobj = pop_context ();
c906108c 11891
22cee43f 11892 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11893 {
801e3a5b 11894 struct block *block
63e43d3a 11895 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11896 newobj->start_addr, highpc);
801e3a5b
JB
11897
11898 /* Note that recording ranges after traversing children, as we
11899 do here, means that recording a parent's ranges entails
11900 walking across all its children's ranges as they appear in
11901 the address map, which is quadratic behavior.
11902
11903 It would be nicer to record the parent's ranges before
11904 traversing its children, simply overriding whatever you find
11905 there. But since we don't even decide whether to create a
11906 block until after we've traversed its children, that's hard
11907 to do. */
11908 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11909 }
fe978cb0 11910 local_symbols = newobj->locals;
22cee43f 11911 local_using_directives = newobj->local_using_directives;
c906108c
SS
11912}
11913
216f72a1 11914/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11915
11916static void
11917read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11918{
11919 struct objfile *objfile = cu->objfile;
11920 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11921 CORE_ADDR pc, baseaddr;
11922 struct attribute *attr;
11923 struct call_site *call_site, call_site_local;
11924 void **slot;
11925 int nparams;
11926 struct die_info *child_die;
11927
11928 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11929
216f72a1
JK
11930 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11931 if (attr == NULL)
11932 {
11933 /* This was a pre-DWARF-5 GNU extension alias
11934 for DW_AT_call_return_pc. */
11935 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11936 }
96408a79
SA
11937 if (!attr)
11938 {
11939 complaint (&symfile_complaints,
216f72a1 11940 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11941 "DIE 0x%x [in module %s]"),
9c541725 11942 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11943 return;
11944 }
31aa7e4e 11945 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11946 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11947
11948 if (cu->call_site_htab == NULL)
11949 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11950 NULL, &objfile->objfile_obstack,
11951 hashtab_obstack_allocate, NULL);
11952 call_site_local.pc = pc;
11953 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11954 if (*slot != NULL)
11955 {
11956 complaint (&symfile_complaints,
216f72a1 11957 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11958 "DIE 0x%x [in module %s]"),
9c541725 11959 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11960 objfile_name (objfile));
96408a79
SA
11961 return;
11962 }
11963
11964 /* Count parameters at the caller. */
11965
11966 nparams = 0;
11967 for (child_die = die->child; child_die && child_die->tag;
11968 child_die = sibling_die (child_die))
11969 {
216f72a1
JK
11970 if (child_die->tag != DW_TAG_call_site_parameter
11971 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11972 {
11973 complaint (&symfile_complaints,
216f72a1
JK
11974 _("Tag %d is not DW_TAG_call_site_parameter in "
11975 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11976 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11977 objfile_name (objfile));
96408a79
SA
11978 continue;
11979 }
11980
11981 nparams++;
11982 }
11983
224c3ddb
SM
11984 call_site
11985 = ((struct call_site *)
11986 obstack_alloc (&objfile->objfile_obstack,
11987 sizeof (*call_site)
11988 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11989 *slot = call_site;
11990 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11991 call_site->pc = pc;
11992
216f72a1
JK
11993 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11994 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11995 {
11996 struct die_info *func_die;
11997
11998 /* Skip also over DW_TAG_inlined_subroutine. */
11999 for (func_die = die->parent;
12000 func_die && func_die->tag != DW_TAG_subprogram
12001 && func_die->tag != DW_TAG_subroutine_type;
12002 func_die = func_die->parent);
12003
216f72a1
JK
12004 /* DW_AT_call_all_calls is a superset
12005 of DW_AT_call_all_tail_calls. */
96408a79 12006 if (func_die
216f72a1 12007 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12008 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12009 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12010 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12011 {
12012 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12013 not complete. But keep CALL_SITE for look ups via call_site_htab,
12014 both the initial caller containing the real return address PC and
12015 the final callee containing the current PC of a chain of tail
12016 calls do not need to have the tail call list complete. But any
12017 function candidate for a virtual tail call frame searched via
12018 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12019 determined unambiguously. */
12020 }
12021 else
12022 {
12023 struct type *func_type = NULL;
12024
12025 if (func_die)
12026 func_type = get_die_type (func_die, cu);
12027 if (func_type != NULL)
12028 {
12029 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12030
12031 /* Enlist this call site to the function. */
12032 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12033 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12034 }
12035 else
12036 complaint (&symfile_complaints,
216f72a1 12037 _("Cannot find function owning DW_TAG_call_site "
96408a79 12038 "DIE 0x%x [in module %s]"),
9c541725 12039 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12040 }
12041 }
12042
216f72a1
JK
12043 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12044 if (attr == NULL)
12045 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12046 if (attr == NULL)
12047 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12048 if (attr == NULL)
216f72a1
JK
12049 {
12050 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12051 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12052 }
96408a79
SA
12053 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12054 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12055 /* Keep NULL DWARF_BLOCK. */;
12056 else if (attr_form_is_block (attr))
12057 {
12058 struct dwarf2_locexpr_baton *dlbaton;
12059
8d749320 12060 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12061 dlbaton->data = DW_BLOCK (attr)->data;
12062 dlbaton->size = DW_BLOCK (attr)->size;
12063 dlbaton->per_cu = cu->per_cu;
12064
12065 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12066 }
7771576e 12067 else if (attr_form_is_ref (attr))
96408a79 12068 {
96408a79
SA
12069 struct dwarf2_cu *target_cu = cu;
12070 struct die_info *target_die;
12071
ac9ec31b 12072 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12073 gdb_assert (target_cu->objfile == objfile);
12074 if (die_is_declaration (target_die, target_cu))
12075 {
7d45c7c3 12076 const char *target_physname;
9112db09
JK
12077
12078 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12079 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12080 if (target_physname == NULL)
9112db09 12081 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12082 if (target_physname == NULL)
12083 complaint (&symfile_complaints,
216f72a1 12084 _("DW_AT_call_target target DIE has invalid "
96408a79 12085 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12086 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12087 else
7d455152 12088 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12089 }
12090 else
12091 {
12092 CORE_ADDR lowpc;
12093
12094 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12095 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12096 <= PC_BOUNDS_INVALID)
96408a79 12097 complaint (&symfile_complaints,
216f72a1 12098 _("DW_AT_call_target target DIE has invalid "
96408a79 12099 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12100 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12101 else
3e29f34a
MR
12102 {
12103 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12104 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12105 }
96408a79
SA
12106 }
12107 }
12108 else
12109 complaint (&symfile_complaints,
216f72a1 12110 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12111 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12112 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12113
12114 call_site->per_cu = cu->per_cu;
12115
12116 for (child_die = die->child;
12117 child_die && child_die->tag;
12118 child_die = sibling_die (child_die))
12119 {
96408a79 12120 struct call_site_parameter *parameter;
1788b2d3 12121 struct attribute *loc, *origin;
96408a79 12122
216f72a1
JK
12123 if (child_die->tag != DW_TAG_call_site_parameter
12124 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12125 {
12126 /* Already printed the complaint above. */
12127 continue;
12128 }
12129
12130 gdb_assert (call_site->parameter_count < nparams);
12131 parameter = &call_site->parameter[call_site->parameter_count];
12132
1788b2d3
JK
12133 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12134 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12135 register is contained in DW_AT_call_value. */
96408a79 12136
24c5c679 12137 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12138 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12139 if (origin == NULL)
12140 {
12141 /* This was a pre-DWARF-5 GNU extension alias
12142 for DW_AT_call_parameter. */
12143 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12144 }
7771576e 12145 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12146 {
1788b2d3 12147 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12148
12149 sect_offset sect_off
12150 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12151 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12152 {
12153 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12154 binding can be done only inside one CU. Such referenced DIE
12155 therefore cannot be even moved to DW_TAG_partial_unit. */
12156 complaint (&symfile_complaints,
216f72a1
JK
12157 _("DW_AT_call_parameter offset is not in CU for "
12158 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12159 to_underlying (child_die->sect_off),
12160 objfile_name (objfile));
d76b7dbc
JK
12161 continue;
12162 }
9c541725
PA
12163 parameter->u.param_cu_off
12164 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12165 }
12166 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12167 {
12168 complaint (&symfile_complaints,
12169 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12170 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12171 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12172 continue;
12173 }
24c5c679 12174 else
96408a79 12175 {
24c5c679
JK
12176 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12177 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12178 if (parameter->u.dwarf_reg != -1)
12179 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12180 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12181 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12182 &parameter->u.fb_offset))
12183 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12184 else
12185 {
12186 complaint (&symfile_complaints,
12187 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12188 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12189 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12190 "[in module %s]"),
9c541725
PA
12191 to_underlying (child_die->sect_off),
12192 objfile_name (objfile));
24c5c679
JK
12193 continue;
12194 }
96408a79
SA
12195 }
12196
216f72a1
JK
12197 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12198 if (attr == NULL)
12199 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12200 if (!attr_form_is_block (attr))
12201 {
12202 complaint (&symfile_complaints,
216f72a1
JK
12203 _("No DW_FORM_block* DW_AT_call_value for "
12204 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12205 to_underlying (child_die->sect_off),
12206 objfile_name (objfile));
96408a79
SA
12207 continue;
12208 }
12209 parameter->value = DW_BLOCK (attr)->data;
12210 parameter->value_size = DW_BLOCK (attr)->size;
12211
12212 /* Parameters are not pre-cleared by memset above. */
12213 parameter->data_value = NULL;
12214 parameter->data_value_size = 0;
12215 call_site->parameter_count++;
12216
216f72a1
JK
12217 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12218 if (attr == NULL)
12219 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12220 if (attr)
12221 {
12222 if (!attr_form_is_block (attr))
12223 complaint (&symfile_complaints,
216f72a1
JK
12224 _("No DW_FORM_block* DW_AT_call_data_value for "
12225 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12226 to_underlying (child_die->sect_off),
12227 objfile_name (objfile));
96408a79
SA
12228 else
12229 {
12230 parameter->data_value = DW_BLOCK (attr)->data;
12231 parameter->data_value_size = DW_BLOCK (attr)->size;
12232 }
12233 }
12234 }
12235}
12236
43988095
JK
12237/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12238 reading .debug_rnglists.
12239 Callback's type should be:
12240 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12241 Return true if the attributes are present and valid, otherwise,
12242 return false. */
12243
12244template <typename Callback>
12245static bool
12246dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12247 Callback &&callback)
12248{
12249 struct objfile *objfile = cu->objfile;
12250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12251 struct comp_unit_head *cu_header = &cu->header;
12252 bfd *obfd = objfile->obfd;
12253 unsigned int addr_size = cu_header->addr_size;
12254 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12255 /* Base address selection entry. */
12256 CORE_ADDR base;
12257 int found_base;
12258 unsigned int dummy;
12259 const gdb_byte *buffer;
12260 CORE_ADDR low = 0;
12261 CORE_ADDR high = 0;
12262 CORE_ADDR baseaddr;
12263 bool overflow = false;
12264
12265 found_base = cu->base_known;
12266 base = cu->base_address;
12267
12268 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12269 if (offset >= dwarf2_per_objfile->rnglists.size)
12270 {
12271 complaint (&symfile_complaints,
12272 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12273 offset);
12274 return false;
12275 }
12276 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12277
12278 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12279
12280 while (1)
12281 {
7814882a
JK
12282 /* Initialize it due to a false compiler warning. */
12283 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12284 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12285 + dwarf2_per_objfile->rnglists.size);
12286 unsigned int bytes_read;
12287
12288 if (buffer == buf_end)
12289 {
12290 overflow = true;
12291 break;
12292 }
12293 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12294 switch (rlet)
12295 {
12296 case DW_RLE_end_of_list:
12297 break;
12298 case DW_RLE_base_address:
12299 if (buffer + cu->header.addr_size > buf_end)
12300 {
12301 overflow = true;
12302 break;
12303 }
12304 base = read_address (obfd, buffer, cu, &bytes_read);
12305 found_base = 1;
12306 buffer += bytes_read;
12307 break;
12308 case DW_RLE_start_length:
12309 if (buffer + cu->header.addr_size > buf_end)
12310 {
12311 overflow = true;
12312 break;
12313 }
12314 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12315 buffer += bytes_read;
12316 range_end = (range_beginning
12317 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12318 buffer += bytes_read;
12319 if (buffer > buf_end)
12320 {
12321 overflow = true;
12322 break;
12323 }
12324 break;
12325 case DW_RLE_offset_pair:
12326 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12327 buffer += bytes_read;
12328 if (buffer > buf_end)
12329 {
12330 overflow = true;
12331 break;
12332 }
12333 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12334 buffer += bytes_read;
12335 if (buffer > buf_end)
12336 {
12337 overflow = true;
12338 break;
12339 }
12340 break;
12341 case DW_RLE_start_end:
12342 if (buffer + 2 * cu->header.addr_size > buf_end)
12343 {
12344 overflow = true;
12345 break;
12346 }
12347 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12348 buffer += bytes_read;
12349 range_end = read_address (obfd, buffer, cu, &bytes_read);
12350 buffer += bytes_read;
12351 break;
12352 default:
12353 complaint (&symfile_complaints,
12354 _("Invalid .debug_rnglists data (no base address)"));
12355 return false;
12356 }
12357 if (rlet == DW_RLE_end_of_list || overflow)
12358 break;
12359 if (rlet == DW_RLE_base_address)
12360 continue;
12361
12362 if (!found_base)
12363 {
12364 /* We have no valid base address for the ranges
12365 data. */
12366 complaint (&symfile_complaints,
12367 _("Invalid .debug_rnglists data (no base address)"));
12368 return false;
12369 }
12370
12371 if (range_beginning > range_end)
12372 {
12373 /* Inverted range entries are invalid. */
12374 complaint (&symfile_complaints,
12375 _("Invalid .debug_rnglists data (inverted range)"));
12376 return false;
12377 }
12378
12379 /* Empty range entries have no effect. */
12380 if (range_beginning == range_end)
12381 continue;
12382
12383 range_beginning += base;
12384 range_end += base;
12385
12386 /* A not-uncommon case of bad debug info.
12387 Don't pollute the addrmap with bad data. */
12388 if (range_beginning + baseaddr == 0
12389 && !dwarf2_per_objfile->has_section_at_zero)
12390 {
12391 complaint (&symfile_complaints,
12392 _(".debug_rnglists entry has start address of zero"
12393 " [in module %s]"), objfile_name (objfile));
12394 continue;
12395 }
12396
12397 callback (range_beginning, range_end);
12398 }
12399
12400 if (overflow)
12401 {
12402 complaint (&symfile_complaints,
12403 _("Offset %d is not terminated "
12404 "for DW_AT_ranges attribute"),
12405 offset);
12406 return false;
12407 }
12408
12409 return true;
12410}
12411
12412/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12413 Callback's type should be:
12414 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12415 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12416
43988095 12417template <typename Callback>
43039443 12418static int
5f46c5a5 12419dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12420 Callback &&callback)
43039443
JK
12421{
12422 struct objfile *objfile = cu->objfile;
3e29f34a 12423 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12424 struct comp_unit_head *cu_header = &cu->header;
12425 bfd *obfd = objfile->obfd;
12426 unsigned int addr_size = cu_header->addr_size;
12427 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12428 /* Base address selection entry. */
12429 CORE_ADDR base;
12430 int found_base;
12431 unsigned int dummy;
d521ce57 12432 const gdb_byte *buffer;
ff013f42 12433 CORE_ADDR baseaddr;
43039443 12434
43988095
JK
12435 if (cu_header->version >= 5)
12436 return dwarf2_rnglists_process (offset, cu, callback);
12437
d00adf39
DE
12438 found_base = cu->base_known;
12439 base = cu->base_address;
43039443 12440
be391dca 12441 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12442 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12443 {
12444 complaint (&symfile_complaints,
12445 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12446 offset);
12447 return 0;
12448 }
dce234bc 12449 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12450
e7030f15 12451 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12452
43039443
JK
12453 while (1)
12454 {
12455 CORE_ADDR range_beginning, range_end;
12456
12457 range_beginning = read_address (obfd, buffer, cu, &dummy);
12458 buffer += addr_size;
12459 range_end = read_address (obfd, buffer, cu, &dummy);
12460 buffer += addr_size;
12461 offset += 2 * addr_size;
12462
12463 /* An end of list marker is a pair of zero addresses. */
12464 if (range_beginning == 0 && range_end == 0)
12465 /* Found the end of list entry. */
12466 break;
12467
12468 /* Each base address selection entry is a pair of 2 values.
12469 The first is the largest possible address, the second is
12470 the base address. Check for a base address here. */
12471 if ((range_beginning & mask) == mask)
12472 {
28d2bfb9
AB
12473 /* If we found the largest possible address, then we already
12474 have the base address in range_end. */
12475 base = range_end;
43039443
JK
12476 found_base = 1;
12477 continue;
12478 }
12479
12480 if (!found_base)
12481 {
12482 /* We have no valid base address for the ranges
12483 data. */
12484 complaint (&symfile_complaints,
12485 _("Invalid .debug_ranges data (no base address)"));
12486 return 0;
12487 }
12488
9277c30c
UW
12489 if (range_beginning > range_end)
12490 {
12491 /* Inverted range entries are invalid. */
12492 complaint (&symfile_complaints,
12493 _("Invalid .debug_ranges data (inverted range)"));
12494 return 0;
12495 }
12496
12497 /* Empty range entries have no effect. */
12498 if (range_beginning == range_end)
12499 continue;
12500
43039443
JK
12501 range_beginning += base;
12502 range_end += base;
12503
01093045
DE
12504 /* A not-uncommon case of bad debug info.
12505 Don't pollute the addrmap with bad data. */
12506 if (range_beginning + baseaddr == 0
12507 && !dwarf2_per_objfile->has_section_at_zero)
12508 {
12509 complaint (&symfile_complaints,
12510 _(".debug_ranges entry has start address of zero"
4262abfb 12511 " [in module %s]"), objfile_name (objfile));
01093045
DE
12512 continue;
12513 }
12514
5f46c5a5
JK
12515 callback (range_beginning, range_end);
12516 }
12517
12518 return 1;
12519}
12520
12521/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12522 Return 1 if the attributes are present and valid, otherwise, return 0.
12523 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12524
12525static int
12526dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12527 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12528 struct partial_symtab *ranges_pst)
12529{
12530 struct objfile *objfile = cu->objfile;
12531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12532 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12533 SECT_OFF_TEXT (objfile));
12534 int low_set = 0;
12535 CORE_ADDR low = 0;
12536 CORE_ADDR high = 0;
12537 int retval;
12538
12539 retval = dwarf2_ranges_process (offset, cu,
12540 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12541 {
9277c30c 12542 if (ranges_pst != NULL)
3e29f34a
MR
12543 {
12544 CORE_ADDR lowpc;
12545 CORE_ADDR highpc;
12546
12547 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12548 range_beginning + baseaddr);
12549 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12550 range_end + baseaddr);
12551 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12552 ranges_pst);
12553 }
ff013f42 12554
43039443
JK
12555 /* FIXME: This is recording everything as a low-high
12556 segment of consecutive addresses. We should have a
12557 data structure for discontiguous block ranges
12558 instead. */
12559 if (! low_set)
12560 {
12561 low = range_beginning;
12562 high = range_end;
12563 low_set = 1;
12564 }
12565 else
12566 {
12567 if (range_beginning < low)
12568 low = range_beginning;
12569 if (range_end > high)
12570 high = range_end;
12571 }
5f46c5a5
JK
12572 });
12573 if (!retval)
12574 return 0;
43039443
JK
12575
12576 if (! low_set)
12577 /* If the first entry is an end-of-list marker, the range
12578 describes an empty scope, i.e. no instructions. */
12579 return 0;
12580
12581 if (low_return)
12582 *low_return = low;
12583 if (high_return)
12584 *high_return = high;
12585 return 1;
12586}
12587
3a2b436a
JK
12588/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12589 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12590 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12591
3a2b436a 12592static enum pc_bounds_kind
af34e669 12593dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12594 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12595 struct partial_symtab *pst)
c906108c
SS
12596{
12597 struct attribute *attr;
91da1414 12598 struct attribute *attr_high;
af34e669
DJ
12599 CORE_ADDR low = 0;
12600 CORE_ADDR high = 0;
e385593e 12601 enum pc_bounds_kind ret;
c906108c 12602
91da1414
MW
12603 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12604 if (attr_high)
af34e669 12605 {
e142c38c 12606 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12607 if (attr)
91da1414 12608 {
31aa7e4e
JB
12609 low = attr_value_as_address (attr);
12610 high = attr_value_as_address (attr_high);
12611 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12612 high += low;
91da1414 12613 }
af34e669
DJ
12614 else
12615 /* Found high w/o low attribute. */
e385593e 12616 return PC_BOUNDS_INVALID;
af34e669
DJ
12617
12618 /* Found consecutive range of addresses. */
3a2b436a 12619 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12620 }
c906108c 12621 else
af34e669 12622 {
e142c38c 12623 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12624 if (attr != NULL)
12625 {
ab435259
DE
12626 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12627 We take advantage of the fact that DW_AT_ranges does not appear
12628 in DW_TAG_compile_unit of DWO files. */
12629 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12630 unsigned int ranges_offset = (DW_UNSND (attr)
12631 + (need_ranges_base
12632 ? cu->ranges_base
12633 : 0));
2e3cf129 12634
af34e669 12635 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12636 .debug_ranges section. */
2e3cf129 12637 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12638 return PC_BOUNDS_INVALID;
43039443 12639 /* Found discontinuous range of addresses. */
3a2b436a 12640 ret = PC_BOUNDS_RANGES;
af34e669 12641 }
e385593e
JK
12642 else
12643 return PC_BOUNDS_NOT_PRESENT;
af34e669 12644 }
c906108c 12645
9373cf26
JK
12646 /* read_partial_die has also the strict LOW < HIGH requirement. */
12647 if (high <= low)
e385593e 12648 return PC_BOUNDS_INVALID;
c906108c
SS
12649
12650 /* When using the GNU linker, .gnu.linkonce. sections are used to
12651 eliminate duplicate copies of functions and vtables and such.
12652 The linker will arbitrarily choose one and discard the others.
12653 The AT_*_pc values for such functions refer to local labels in
12654 these sections. If the section from that file was discarded, the
12655 labels are not in the output, so the relocs get a value of 0.
12656 If this is a discarded function, mark the pc bounds as invalid,
12657 so that GDB will ignore it. */
72dca2f5 12658 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12659 return PC_BOUNDS_INVALID;
c906108c
SS
12660
12661 *lowpc = low;
96408a79
SA
12662 if (highpc)
12663 *highpc = high;
af34e669 12664 return ret;
c906108c
SS
12665}
12666
b084d499
JB
12667/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12668 its low and high PC addresses. Do nothing if these addresses could not
12669 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12670 and HIGHPC to the high address if greater than HIGHPC. */
12671
12672static void
12673dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12674 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12675 struct dwarf2_cu *cu)
12676{
12677 CORE_ADDR low, high;
12678 struct die_info *child = die->child;
12679
e385593e 12680 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12681 {
325fac50
PA
12682 *lowpc = std::min (*lowpc, low);
12683 *highpc = std::max (*highpc, high);
b084d499
JB
12684 }
12685
12686 /* If the language does not allow nested subprograms (either inside
12687 subprograms or lexical blocks), we're done. */
12688 if (cu->language != language_ada)
12689 return;
6e70227d 12690
b084d499
JB
12691 /* Check all the children of the given DIE. If it contains nested
12692 subprograms, then check their pc bounds. Likewise, we need to
12693 check lexical blocks as well, as they may also contain subprogram
12694 definitions. */
12695 while (child && child->tag)
12696 {
12697 if (child->tag == DW_TAG_subprogram
12698 || child->tag == DW_TAG_lexical_block)
12699 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12700 child = sibling_die (child);
12701 }
12702}
12703
fae299cd
DC
12704/* Get the low and high pc's represented by the scope DIE, and store
12705 them in *LOWPC and *HIGHPC. If the correct values can't be
12706 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12707
12708static void
12709get_scope_pc_bounds (struct die_info *die,
12710 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12711 struct dwarf2_cu *cu)
12712{
12713 CORE_ADDR best_low = (CORE_ADDR) -1;
12714 CORE_ADDR best_high = (CORE_ADDR) 0;
12715 CORE_ADDR current_low, current_high;
12716
3a2b436a 12717 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12718 >= PC_BOUNDS_RANGES)
fae299cd
DC
12719 {
12720 best_low = current_low;
12721 best_high = current_high;
12722 }
12723 else
12724 {
12725 struct die_info *child = die->child;
12726
12727 while (child && child->tag)
12728 {
12729 switch (child->tag) {
12730 case DW_TAG_subprogram:
b084d499 12731 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12732 break;
12733 case DW_TAG_namespace:
f55ee35c 12734 case DW_TAG_module:
fae299cd
DC
12735 /* FIXME: carlton/2004-01-16: Should we do this for
12736 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12737 that current GCC's always emit the DIEs corresponding
12738 to definitions of methods of classes as children of a
12739 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12740 the DIEs giving the declarations, which could be
12741 anywhere). But I don't see any reason why the
12742 standards says that they have to be there. */
12743 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12744
12745 if (current_low != ((CORE_ADDR) -1))
12746 {
325fac50
PA
12747 best_low = std::min (best_low, current_low);
12748 best_high = std::max (best_high, current_high);
fae299cd
DC
12749 }
12750 break;
12751 default:
0963b4bd 12752 /* Ignore. */
fae299cd
DC
12753 break;
12754 }
12755
12756 child = sibling_die (child);
12757 }
12758 }
12759
12760 *lowpc = best_low;
12761 *highpc = best_high;
12762}
12763
801e3a5b
JB
12764/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12765 in DIE. */
380bca97 12766
801e3a5b
JB
12767static void
12768dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12769 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12770{
bb5ed363 12771 struct objfile *objfile = cu->objfile;
3e29f34a 12772 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12773 struct attribute *attr;
91da1414 12774 struct attribute *attr_high;
801e3a5b 12775
91da1414
MW
12776 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12777 if (attr_high)
801e3a5b 12778 {
801e3a5b
JB
12779 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12780 if (attr)
12781 {
31aa7e4e
JB
12782 CORE_ADDR low = attr_value_as_address (attr);
12783 CORE_ADDR high = attr_value_as_address (attr_high);
12784
12785 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12786 high += low;
9a619af0 12787
3e29f34a
MR
12788 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12789 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12790 record_block_range (block, low, high - 1);
801e3a5b
JB
12791 }
12792 }
12793
12794 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12795 if (attr)
12796 {
bb5ed363 12797 bfd *obfd = objfile->obfd;
ab435259
DE
12798 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12799 We take advantage of the fact that DW_AT_ranges does not appear
12800 in DW_TAG_compile_unit of DWO files. */
12801 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12802
12803 /* The value of the DW_AT_ranges attribute is the offset of the
12804 address range list in the .debug_ranges section. */
ab435259
DE
12805 unsigned long offset = (DW_UNSND (attr)
12806 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12807 const gdb_byte *buffer;
801e3a5b
JB
12808
12809 /* For some target architectures, but not others, the
12810 read_address function sign-extends the addresses it returns.
12811 To recognize base address selection entries, we need a
12812 mask. */
12813 unsigned int addr_size = cu->header.addr_size;
12814 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12815
12816 /* The base address, to which the next pair is relative. Note
12817 that this 'base' is a DWARF concept: most entries in a range
12818 list are relative, to reduce the number of relocs against the
12819 debugging information. This is separate from this function's
12820 'baseaddr' argument, which GDB uses to relocate debugging
12821 information from a shared library based on the address at
12822 which the library was loaded. */
d00adf39
DE
12823 CORE_ADDR base = cu->base_address;
12824 int base_known = cu->base_known;
801e3a5b 12825
5f46c5a5
JK
12826 dwarf2_ranges_process (offset, cu,
12827 [&] (CORE_ADDR start, CORE_ADDR end)
12828 {
58fdfd2c
JK
12829 start += baseaddr;
12830 end += baseaddr;
5f46c5a5
JK
12831 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12832 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12833 record_block_range (block, start, end - 1);
12834 });
801e3a5b
JB
12835 }
12836}
12837
685b1105
JK
12838/* Check whether the producer field indicates either of GCC < 4.6, or the
12839 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12840
685b1105
JK
12841static void
12842check_producer (struct dwarf2_cu *cu)
60d5a603 12843{
38360086 12844 int major, minor;
60d5a603
JK
12845
12846 if (cu->producer == NULL)
12847 {
12848 /* For unknown compilers expect their behavior is DWARF version
12849 compliant.
12850
12851 GCC started to support .debug_types sections by -gdwarf-4 since
12852 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12853 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12854 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12855 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12856 }
b1ffba5a 12857 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12858 {
38360086
MW
12859 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12860 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12861 }
61012eef 12862 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12863 cu->producer_is_icc = 1;
12864 else
12865 {
12866 /* For other non-GCC compilers, expect their behavior is DWARF version
12867 compliant. */
60d5a603
JK
12868 }
12869
ba919b58 12870 cu->checked_producer = 1;
685b1105 12871}
ba919b58 12872
685b1105
JK
12873/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12874 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12875 during 4.6.0 experimental. */
12876
12877static int
12878producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12879{
12880 if (!cu->checked_producer)
12881 check_producer (cu);
12882
12883 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12884}
12885
12886/* Return the default accessibility type if it is not overriden by
12887 DW_AT_accessibility. */
12888
12889static enum dwarf_access_attribute
12890dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12891{
12892 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12893 {
12894 /* The default DWARF 2 accessibility for members is public, the default
12895 accessibility for inheritance is private. */
12896
12897 if (die->tag != DW_TAG_inheritance)
12898 return DW_ACCESS_public;
12899 else
12900 return DW_ACCESS_private;
12901 }
12902 else
12903 {
12904 /* DWARF 3+ defines the default accessibility a different way. The same
12905 rules apply now for DW_TAG_inheritance as for the members and it only
12906 depends on the container kind. */
12907
12908 if (die->parent->tag == DW_TAG_class_type)
12909 return DW_ACCESS_private;
12910 else
12911 return DW_ACCESS_public;
12912 }
12913}
12914
74ac6d43
TT
12915/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12916 offset. If the attribute was not found return 0, otherwise return
12917 1. If it was found but could not properly be handled, set *OFFSET
12918 to 0. */
12919
12920static int
12921handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12922 LONGEST *offset)
12923{
12924 struct attribute *attr;
12925
12926 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12927 if (attr != NULL)
12928 {
12929 *offset = 0;
12930
12931 /* Note that we do not check for a section offset first here.
12932 This is because DW_AT_data_member_location is new in DWARF 4,
12933 so if we see it, we can assume that a constant form is really
12934 a constant and not a section offset. */
12935 if (attr_form_is_constant (attr))
12936 *offset = dwarf2_get_attr_constant_value (attr, 0);
12937 else if (attr_form_is_section_offset (attr))
12938 dwarf2_complex_location_expr_complaint ();
12939 else if (attr_form_is_block (attr))
12940 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12941 else
12942 dwarf2_complex_location_expr_complaint ();
12943
12944 return 1;
12945 }
12946
12947 return 0;
12948}
12949
c906108c
SS
12950/* Add an aggregate field to the field list. */
12951
12952static void
107d2387 12953dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12954 struct dwarf2_cu *cu)
6e70227d 12955{
e7c27a73 12956 struct objfile *objfile = cu->objfile;
5e2b427d 12957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12958 struct nextfield *new_field;
12959 struct attribute *attr;
12960 struct field *fp;
15d034d0 12961 const char *fieldname = "";
c906108c
SS
12962
12963 /* Allocate a new field list entry and link it in. */
8d749320 12964 new_field = XNEW (struct nextfield);
b8c9b27d 12965 make_cleanup (xfree, new_field);
c906108c 12966 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12967
12968 if (die->tag == DW_TAG_inheritance)
12969 {
12970 new_field->next = fip->baseclasses;
12971 fip->baseclasses = new_field;
12972 }
12973 else
12974 {
12975 new_field->next = fip->fields;
12976 fip->fields = new_field;
12977 }
c906108c
SS
12978 fip->nfields++;
12979
e142c38c 12980 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12981 if (attr)
12982 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12983 else
12984 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12985 if (new_field->accessibility != DW_ACCESS_public)
12986 fip->non_public_fields = 1;
60d5a603 12987
e142c38c 12988 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12989 if (attr)
12990 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12991 else
12992 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12993
12994 fp = &new_field->field;
a9a9bd0f 12995
e142c38c 12996 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12997 {
74ac6d43
TT
12998 LONGEST offset;
12999
a9a9bd0f 13000 /* Data member other than a C++ static data member. */
6e70227d 13001
c906108c 13002 /* Get type of field. */
e7c27a73 13003 fp->type = die_type (die, cu);
c906108c 13004
d6a843b5 13005 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13006
c906108c 13007 /* Get bit size of field (zero if none). */
e142c38c 13008 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13009 if (attr)
13010 {
13011 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13012 }
13013 else
13014 {
13015 FIELD_BITSIZE (*fp) = 0;
13016 }
13017
13018 /* Get bit offset of field. */
74ac6d43
TT
13019 if (handle_data_member_location (die, cu, &offset))
13020 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13021 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13022 if (attr)
13023 {
5e2b427d 13024 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13025 {
13026 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13027 additional bit offset from the MSB of the containing
13028 anonymous object to the MSB of the field. We don't
13029 have to do anything special since we don't need to
13030 know the size of the anonymous object. */
f41f5e61 13031 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13032 }
13033 else
13034 {
13035 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13036 MSB of the anonymous object, subtract off the number of
13037 bits from the MSB of the field to the MSB of the
13038 object, and then subtract off the number of bits of
13039 the field itself. The result is the bit offset of
13040 the LSB of the field. */
c906108c
SS
13041 int anonymous_size;
13042 int bit_offset = DW_UNSND (attr);
13043
e142c38c 13044 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13045 if (attr)
13046 {
13047 /* The size of the anonymous object containing
13048 the bit field is explicit, so use the
13049 indicated size (in bytes). */
13050 anonymous_size = DW_UNSND (attr);
13051 }
13052 else
13053 {
13054 /* The size of the anonymous object containing
13055 the bit field must be inferred from the type
13056 attribute of the data member containing the
13057 bit field. */
13058 anonymous_size = TYPE_LENGTH (fp->type);
13059 }
f41f5e61
PA
13060 SET_FIELD_BITPOS (*fp,
13061 (FIELD_BITPOS (*fp)
13062 + anonymous_size * bits_per_byte
13063 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13064 }
13065 }
da5b30da
AA
13066 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13067 if (attr != NULL)
13068 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13069 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13070
13071 /* Get name of field. */
39cbfefa
DJ
13072 fieldname = dwarf2_name (die, cu);
13073 if (fieldname == NULL)
13074 fieldname = "";
d8151005
DJ
13075
13076 /* The name is already allocated along with this objfile, so we don't
13077 need to duplicate it for the type. */
13078 fp->name = fieldname;
c906108c
SS
13079
13080 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13081 pointer or virtual base class pointer) to private. */
e142c38c 13082 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13083 {
d48cc9dd 13084 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13085 new_field->accessibility = DW_ACCESS_private;
13086 fip->non_public_fields = 1;
13087 }
13088 }
a9a9bd0f 13089 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13090 {
a9a9bd0f
DC
13091 /* C++ static member. */
13092
13093 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13094 is a declaration, but all versions of G++ as of this writing
13095 (so through at least 3.2.1) incorrectly generate
13096 DW_TAG_variable tags. */
6e70227d 13097
ff355380 13098 const char *physname;
c906108c 13099
a9a9bd0f 13100 /* Get name of field. */
39cbfefa
DJ
13101 fieldname = dwarf2_name (die, cu);
13102 if (fieldname == NULL)
c906108c
SS
13103 return;
13104
254e6b9e 13105 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13106 if (attr
13107 /* Only create a symbol if this is an external value.
13108 new_symbol checks this and puts the value in the global symbol
13109 table, which we want. If it is not external, new_symbol
13110 will try to put the value in cu->list_in_scope which is wrong. */
13111 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13112 {
13113 /* A static const member, not much different than an enum as far as
13114 we're concerned, except that we can support more types. */
13115 new_symbol (die, NULL, cu);
13116 }
13117
2df3850c 13118 /* Get physical name. */
ff355380 13119 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13120
d8151005
DJ
13121 /* The name is already allocated along with this objfile, so we don't
13122 need to duplicate it for the type. */
13123 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13124 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13125 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13126 }
13127 else if (die->tag == DW_TAG_inheritance)
13128 {
74ac6d43 13129 LONGEST offset;
d4b96c9a 13130
74ac6d43
TT
13131 /* C++ base class field. */
13132 if (handle_data_member_location (die, cu, &offset))
13133 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13134 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13135 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13136 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13137 fip->nbaseclasses++;
13138 }
13139}
13140
98751a41
JK
13141/* Add a typedef defined in the scope of the FIP's class. */
13142
13143static void
13144dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13145 struct dwarf2_cu *cu)
6e70227d 13146{
98751a41 13147 struct typedef_field_list *new_field;
98751a41 13148 struct typedef_field *fp;
98751a41
JK
13149
13150 /* Allocate a new field list entry and link it in. */
8d749320 13151 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13152 make_cleanup (xfree, new_field);
13153
13154 gdb_assert (die->tag == DW_TAG_typedef);
13155
13156 fp = &new_field->field;
13157
13158 /* Get name of field. */
13159 fp->name = dwarf2_name (die, cu);
13160 if (fp->name == NULL)
13161 return;
13162
13163 fp->type = read_type_die (die, cu);
13164
13165 new_field->next = fip->typedef_field_list;
13166 fip->typedef_field_list = new_field;
13167 fip->typedef_field_list_count++;
13168}
13169
c906108c
SS
13170/* Create the vector of fields, and attach it to the type. */
13171
13172static void
fba45db2 13173dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13174 struct dwarf2_cu *cu)
c906108c
SS
13175{
13176 int nfields = fip->nfields;
13177
13178 /* Record the field count, allocate space for the array of fields,
13179 and create blank accessibility bitfields if necessary. */
13180 TYPE_NFIELDS (type) = nfields;
13181 TYPE_FIELDS (type) = (struct field *)
13182 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13183 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13184
b4ba55a1 13185 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13186 {
13187 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13188
13189 TYPE_FIELD_PRIVATE_BITS (type) =
13190 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13191 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13192
13193 TYPE_FIELD_PROTECTED_BITS (type) =
13194 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13195 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13196
774b6a14
TT
13197 TYPE_FIELD_IGNORE_BITS (type) =
13198 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13199 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13200 }
13201
13202 /* If the type has baseclasses, allocate and clear a bit vector for
13203 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13204 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13205 {
13206 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13207 unsigned char *pointer;
c906108c
SS
13208
13209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13210 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13211 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13212 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13213 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13214 }
13215
3e43a32a
MS
13216 /* Copy the saved-up fields into the field vector. Start from the head of
13217 the list, adding to the tail of the field array, so that they end up in
13218 the same order in the array in which they were added to the list. */
c906108c
SS
13219 while (nfields-- > 0)
13220 {
7d0ccb61
DJ
13221 struct nextfield *fieldp;
13222
13223 if (fip->fields)
13224 {
13225 fieldp = fip->fields;
13226 fip->fields = fieldp->next;
13227 }
13228 else
13229 {
13230 fieldp = fip->baseclasses;
13231 fip->baseclasses = fieldp->next;
13232 }
13233
13234 TYPE_FIELD (type, nfields) = fieldp->field;
13235 switch (fieldp->accessibility)
c906108c 13236 {
c5aa993b 13237 case DW_ACCESS_private:
b4ba55a1
JB
13238 if (cu->language != language_ada)
13239 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13240 break;
c906108c 13241
c5aa993b 13242 case DW_ACCESS_protected:
b4ba55a1
JB
13243 if (cu->language != language_ada)
13244 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13245 break;
c906108c 13246
c5aa993b
JM
13247 case DW_ACCESS_public:
13248 break;
c906108c 13249
c5aa993b
JM
13250 default:
13251 /* Unknown accessibility. Complain and treat it as public. */
13252 {
e2e0b3e5 13253 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13254 fieldp->accessibility);
c5aa993b
JM
13255 }
13256 break;
c906108c
SS
13257 }
13258 if (nfields < fip->nbaseclasses)
13259 {
7d0ccb61 13260 switch (fieldp->virtuality)
c906108c 13261 {
c5aa993b
JM
13262 case DW_VIRTUALITY_virtual:
13263 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13264 if (cu->language == language_ada)
a73c6dcd 13265 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13266 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13267 break;
c906108c
SS
13268 }
13269 }
c906108c
SS
13270 }
13271}
13272
7d27a96d
TT
13273/* Return true if this member function is a constructor, false
13274 otherwise. */
13275
13276static int
13277dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13278{
13279 const char *fieldname;
fe978cb0 13280 const char *type_name;
7d27a96d
TT
13281 int len;
13282
13283 if (die->parent == NULL)
13284 return 0;
13285
13286 if (die->parent->tag != DW_TAG_structure_type
13287 && die->parent->tag != DW_TAG_union_type
13288 && die->parent->tag != DW_TAG_class_type)
13289 return 0;
13290
13291 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13292 type_name = dwarf2_name (die->parent, cu);
13293 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13294 return 0;
13295
13296 len = strlen (fieldname);
fe978cb0
PA
13297 return (strncmp (fieldname, type_name, len) == 0
13298 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13299}
13300
c906108c
SS
13301/* Add a member function to the proper fieldlist. */
13302
13303static void
107d2387 13304dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13305 struct type *type, struct dwarf2_cu *cu)
c906108c 13306{
e7c27a73 13307 struct objfile *objfile = cu->objfile;
c906108c
SS
13308 struct attribute *attr;
13309 struct fnfieldlist *flp;
13310 int i;
13311 struct fn_field *fnp;
15d034d0 13312 const char *fieldname;
c906108c 13313 struct nextfnfield *new_fnfield;
f792889a 13314 struct type *this_type;
60d5a603 13315 enum dwarf_access_attribute accessibility;
c906108c 13316
b4ba55a1 13317 if (cu->language == language_ada)
a73c6dcd 13318 error (_("unexpected member function in Ada type"));
b4ba55a1 13319
2df3850c 13320 /* Get name of member function. */
39cbfefa
DJ
13321 fieldname = dwarf2_name (die, cu);
13322 if (fieldname == NULL)
2df3850c 13323 return;
c906108c 13324
c906108c
SS
13325 /* Look up member function name in fieldlist. */
13326 for (i = 0; i < fip->nfnfields; i++)
13327 {
27bfe10e 13328 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13329 break;
13330 }
13331
13332 /* Create new list element if necessary. */
13333 if (i < fip->nfnfields)
13334 flp = &fip->fnfieldlists[i];
13335 else
13336 {
13337 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13338 {
13339 fip->fnfieldlists = (struct fnfieldlist *)
13340 xrealloc (fip->fnfieldlists,
13341 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13342 * sizeof (struct fnfieldlist));
c906108c 13343 if (fip->nfnfields == 0)
c13c43fd 13344 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13345 }
13346 flp = &fip->fnfieldlists[fip->nfnfields];
13347 flp->name = fieldname;
13348 flp->length = 0;
13349 flp->head = NULL;
3da10d80 13350 i = fip->nfnfields++;
c906108c
SS
13351 }
13352
13353 /* Create a new member function field and chain it to the field list
0963b4bd 13354 entry. */
8d749320 13355 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13356 make_cleanup (xfree, new_fnfield);
c906108c
SS
13357 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13358 new_fnfield->next = flp->head;
13359 flp->head = new_fnfield;
13360 flp->length++;
13361
13362 /* Fill in the member function field info. */
13363 fnp = &new_fnfield->fnfield;
3da10d80
KS
13364
13365 /* Delay processing of the physname until later. */
9c37b5ae 13366 if (cu->language == language_cplus)
3da10d80
KS
13367 {
13368 add_to_method_list (type, i, flp->length - 1, fieldname,
13369 die, cu);
13370 }
13371 else
13372 {
1d06ead6 13373 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13374 fnp->physname = physname ? physname : "";
13375 }
13376
c906108c 13377 fnp->type = alloc_type (objfile);
f792889a
DJ
13378 this_type = read_type_die (die, cu);
13379 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13380 {
f792889a 13381 int nparams = TYPE_NFIELDS (this_type);
c906108c 13382
f792889a 13383 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13384 of the method itself (TYPE_CODE_METHOD). */
13385 smash_to_method_type (fnp->type, type,
f792889a
DJ
13386 TYPE_TARGET_TYPE (this_type),
13387 TYPE_FIELDS (this_type),
13388 TYPE_NFIELDS (this_type),
13389 TYPE_VARARGS (this_type));
c906108c
SS
13390
13391 /* Handle static member functions.
c5aa993b 13392 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13393 member functions. G++ helps GDB by marking the first
13394 parameter for non-static member functions (which is the this
13395 pointer) as artificial. We obtain this information from
13396 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13397 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13398 fnp->voffset = VOFFSET_STATIC;
13399 }
13400 else
e2e0b3e5 13401 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13402 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13403
13404 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13405 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13406 fnp->fcontext = die_containing_type (die, cu);
c906108c 13407
3e43a32a
MS
13408 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13409 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13410
13411 /* Get accessibility. */
e142c38c 13412 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13413 if (attr)
aead7601 13414 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13415 else
13416 accessibility = dwarf2_default_access_attribute (die, cu);
13417 switch (accessibility)
c906108c 13418 {
60d5a603
JK
13419 case DW_ACCESS_private:
13420 fnp->is_private = 1;
13421 break;
13422 case DW_ACCESS_protected:
13423 fnp->is_protected = 1;
13424 break;
c906108c
SS
13425 }
13426
b02dede2 13427 /* Check for artificial methods. */
e142c38c 13428 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13429 if (attr && DW_UNSND (attr) != 0)
13430 fnp->is_artificial = 1;
13431
7d27a96d
TT
13432 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13433
0d564a31 13434 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13435 function. For older versions of GCC, this is an offset in the
13436 appropriate virtual table, as specified by DW_AT_containing_type.
13437 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13438 to the object address. */
13439
e142c38c 13440 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13441 if (attr)
8e19ed76 13442 {
aec5aa8b 13443 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13444 {
aec5aa8b
TT
13445 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13446 {
13447 /* Old-style GCC. */
13448 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13449 }
13450 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13451 || (DW_BLOCK (attr)->size > 1
13452 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13453 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13454 {
aec5aa8b
TT
13455 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13456 if ((fnp->voffset % cu->header.addr_size) != 0)
13457 dwarf2_complex_location_expr_complaint ();
13458 else
13459 fnp->voffset /= cu->header.addr_size;
13460 fnp->voffset += 2;
13461 }
13462 else
13463 dwarf2_complex_location_expr_complaint ();
13464
13465 if (!fnp->fcontext)
7e993ebf
KS
13466 {
13467 /* If there is no `this' field and no DW_AT_containing_type,
13468 we cannot actually find a base class context for the
13469 vtable! */
13470 if (TYPE_NFIELDS (this_type) == 0
13471 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13472 {
13473 complaint (&symfile_complaints,
13474 _("cannot determine context for virtual member "
13475 "function \"%s\" (offset %d)"),
9c541725 13476 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13477 }
13478 else
13479 {
13480 fnp->fcontext
13481 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13482 }
13483 }
aec5aa8b 13484 }
3690dd37 13485 else if (attr_form_is_section_offset (attr))
8e19ed76 13486 {
4d3c2250 13487 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13488 }
13489 else
13490 {
4d3c2250
KB
13491 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13492 fieldname);
8e19ed76 13493 }
0d564a31 13494 }
d48cc9dd
DJ
13495 else
13496 {
13497 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13498 if (attr && DW_UNSND (attr))
13499 {
13500 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13501 complaint (&symfile_complaints,
3e43a32a
MS
13502 _("Member function \"%s\" (offset %d) is virtual "
13503 "but the vtable offset is not specified"),
9c541725 13504 fieldname, to_underlying (die->sect_off));
9655fd1a 13505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13506 TYPE_CPLUS_DYNAMIC (type) = 1;
13507 }
13508 }
c906108c
SS
13509}
13510
13511/* Create the vector of member function fields, and attach it to the type. */
13512
13513static void
fba45db2 13514dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13515 struct dwarf2_cu *cu)
c906108c
SS
13516{
13517 struct fnfieldlist *flp;
c906108c
SS
13518 int i;
13519
b4ba55a1 13520 if (cu->language == language_ada)
a73c6dcd 13521 error (_("unexpected member functions in Ada type"));
b4ba55a1 13522
c906108c
SS
13523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13524 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13525 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13526
13527 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13528 {
13529 struct nextfnfield *nfp = flp->head;
13530 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13531 int k;
13532
13533 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13534 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13535 fn_flp->fn_fields = (struct fn_field *)
13536 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13537 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13538 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13539 }
13540
13541 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13542}
13543
1168df01
JB
13544/* Returns non-zero if NAME is the name of a vtable member in CU's
13545 language, zero otherwise. */
13546static int
13547is_vtable_name (const char *name, struct dwarf2_cu *cu)
13548{
13549 static const char vptr[] = "_vptr";
987504bb 13550 static const char vtable[] = "vtable";
1168df01 13551
9c37b5ae
TT
13552 /* Look for the C++ form of the vtable. */
13553 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13554 return 1;
13555
13556 return 0;
13557}
13558
c0dd20ea 13559/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13560 functions, with the ABI-specified layout. If TYPE describes
13561 such a structure, smash it into a member function type.
61049d3b
DJ
13562
13563 GCC shouldn't do this; it should just output pointer to member DIEs.
13564 This is GCC PR debug/28767. */
c0dd20ea 13565
0b92b5bb
TT
13566static void
13567quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13568{
09e2d7c7 13569 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13570
13571 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13572 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13573 return;
c0dd20ea
DJ
13574
13575 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13576 if (TYPE_FIELD_NAME (type, 0) == NULL
13577 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13578 || TYPE_FIELD_NAME (type, 1) == NULL
13579 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13580 return;
c0dd20ea
DJ
13581
13582 /* Find the type of the method. */
0b92b5bb 13583 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13584 if (pfn_type == NULL
13585 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13586 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13587 return;
c0dd20ea
DJ
13588
13589 /* Look for the "this" argument. */
13590 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13591 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13592 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13593 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13594 return;
c0dd20ea 13595
09e2d7c7 13596 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13597 new_type = alloc_type (objfile);
09e2d7c7 13598 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13599 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13600 TYPE_VARARGS (pfn_type));
0b92b5bb 13601 smash_to_methodptr_type (type, new_type);
c0dd20ea 13602}
1168df01 13603
685b1105
JK
13604/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13605 (icc). */
13606
13607static int
13608producer_is_icc (struct dwarf2_cu *cu)
13609{
13610 if (!cu->checked_producer)
13611 check_producer (cu);
13612
13613 return cu->producer_is_icc;
13614}
13615
c906108c 13616/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13617 (definition) to create a type for the structure or union. Fill in
13618 the type's name and general properties; the members will not be
83655187
DE
13619 processed until process_structure_scope. A symbol table entry for
13620 the type will also not be done until process_structure_scope (assuming
13621 the type has a name).
c906108c 13622
c767944b
DJ
13623 NOTE: we need to call these functions regardless of whether or not the
13624 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13625 structure or union. This gets the type entered into our set of
83655187 13626 user defined types. */
c906108c 13627
f792889a 13628static struct type *
134d01f1 13629read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13630{
e7c27a73 13631 struct objfile *objfile = cu->objfile;
c906108c
SS
13632 struct type *type;
13633 struct attribute *attr;
15d034d0 13634 const char *name;
c906108c 13635
348e048f
DE
13636 /* If the definition of this type lives in .debug_types, read that type.
13637 Don't follow DW_AT_specification though, that will take us back up
13638 the chain and we want to go down. */
45e58e77 13639 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13640 if (attr)
13641 {
ac9ec31b 13642 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13643
ac9ec31b 13644 /* The type's CU may not be the same as CU.
02142a6c 13645 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13646 return set_die_type (die, type, cu);
13647 }
13648
c0dd20ea 13649 type = alloc_type (objfile);
c906108c 13650 INIT_CPLUS_SPECIFIC (type);
93311388 13651
39cbfefa
DJ
13652 name = dwarf2_name (die, cu);
13653 if (name != NULL)
c906108c 13654 {
987504bb 13655 if (cu->language == language_cplus
c44af4eb
TT
13656 || cu->language == language_d
13657 || cu->language == language_rust)
63d06c5c 13658 {
15d034d0 13659 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13660
13661 /* dwarf2_full_name might have already finished building the DIE's
13662 type. If so, there is no need to continue. */
13663 if (get_die_type (die, cu) != NULL)
13664 return get_die_type (die, cu);
13665
13666 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13667 if (die->tag == DW_TAG_structure_type
13668 || die->tag == DW_TAG_class_type)
13669 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13670 }
13671 else
13672 {
d8151005
DJ
13673 /* The name is already allocated along with this objfile, so
13674 we don't need to duplicate it for the type. */
7d455152 13675 TYPE_TAG_NAME (type) = name;
94af9270
KS
13676 if (die->tag == DW_TAG_class_type)
13677 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13678 }
c906108c
SS
13679 }
13680
13681 if (die->tag == DW_TAG_structure_type)
13682 {
13683 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13684 }
13685 else if (die->tag == DW_TAG_union_type)
13686 {
13687 TYPE_CODE (type) = TYPE_CODE_UNION;
13688 }
13689 else
13690 {
4753d33b 13691 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13692 }
13693
0cc2414c
TT
13694 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13695 TYPE_DECLARED_CLASS (type) = 1;
13696
e142c38c 13697 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13698 if (attr)
13699 {
155bfbd3
JB
13700 if (attr_form_is_constant (attr))
13701 TYPE_LENGTH (type) = DW_UNSND (attr);
13702 else
13703 {
13704 /* For the moment, dynamic type sizes are not supported
13705 by GDB's struct type. The actual size is determined
13706 on-demand when resolving the type of a given object,
13707 so set the type's length to zero for now. Otherwise,
13708 we record an expression as the length, and that expression
13709 could lead to a very large value, which could eventually
13710 lead to us trying to allocate that much memory when creating
13711 a value of that type. */
13712 TYPE_LENGTH (type) = 0;
13713 }
c906108c
SS
13714 }
13715 else
13716 {
13717 TYPE_LENGTH (type) = 0;
13718 }
13719
422b1cb0 13720 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13721 {
13722 /* ICC does not output the required DW_AT_declaration
13723 on incomplete types, but gives them a size of zero. */
422b1cb0 13724 TYPE_STUB (type) = 1;
685b1105
JK
13725 }
13726 else
13727 TYPE_STUB_SUPPORTED (type) = 1;
13728
dc718098 13729 if (die_is_declaration (die, cu))
876cecd0 13730 TYPE_STUB (type) = 1;
a6c727b2
DJ
13731 else if (attr == NULL && die->child == NULL
13732 && producer_is_realview (cu->producer))
13733 /* RealView does not output the required DW_AT_declaration
13734 on incomplete types. */
13735 TYPE_STUB (type) = 1;
dc718098 13736
c906108c
SS
13737 /* We need to add the type field to the die immediately so we don't
13738 infinitely recurse when dealing with pointers to the structure
0963b4bd 13739 type within the structure itself. */
1c379e20 13740 set_die_type (die, type, cu);
c906108c 13741
7e314c57
JK
13742 /* set_die_type should be already done. */
13743 set_descriptive_type (type, die, cu);
13744
c767944b
DJ
13745 return type;
13746}
13747
13748/* Finish creating a structure or union type, including filling in
13749 its members and creating a symbol for it. */
13750
13751static void
13752process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13753{
13754 struct objfile *objfile = cu->objfile;
ca040673 13755 struct die_info *child_die;
c767944b
DJ
13756 struct type *type;
13757
13758 type = get_die_type (die, cu);
13759 if (type == NULL)
13760 type = read_structure_type (die, cu);
13761
e142c38c 13762 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13763 {
13764 struct field_info fi;
34eaf542 13765 VEC (symbolp) *template_args = NULL;
c767944b 13766 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13767
13768 memset (&fi, 0, sizeof (struct field_info));
13769
639d11d3 13770 child_die = die->child;
c906108c
SS
13771
13772 while (child_die && child_die->tag)
13773 {
a9a9bd0f
DC
13774 if (child_die->tag == DW_TAG_member
13775 || child_die->tag == DW_TAG_variable)
c906108c 13776 {
a9a9bd0f
DC
13777 /* NOTE: carlton/2002-11-05: A C++ static data member
13778 should be a DW_TAG_member that is a declaration, but
13779 all versions of G++ as of this writing (so through at
13780 least 3.2.1) incorrectly generate DW_TAG_variable
13781 tags for them instead. */
e7c27a73 13782 dwarf2_add_field (&fi, child_die, cu);
c906108c 13783 }
8713b1b1 13784 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13785 {
e98c9e7c
TT
13786 /* Rust doesn't have member functions in the C++ sense.
13787 However, it does emit ordinary functions as children
13788 of a struct DIE. */
13789 if (cu->language == language_rust)
13790 read_func_scope (child_die, cu);
13791 else
13792 {
13793 /* C++ member function. */
13794 dwarf2_add_member_fn (&fi, child_die, type, cu);
13795 }
c906108c
SS
13796 }
13797 else if (child_die->tag == DW_TAG_inheritance)
13798 {
13799 /* C++ base class field. */
e7c27a73 13800 dwarf2_add_field (&fi, child_die, cu);
c906108c 13801 }
98751a41
JK
13802 else if (child_die->tag == DW_TAG_typedef)
13803 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13804 else if (child_die->tag == DW_TAG_template_type_param
13805 || child_die->tag == DW_TAG_template_value_param)
13806 {
13807 struct symbol *arg = new_symbol (child_die, NULL, cu);
13808
f1078f66
DJ
13809 if (arg != NULL)
13810 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13811 }
13812
c906108c
SS
13813 child_die = sibling_die (child_die);
13814 }
13815
34eaf542
TT
13816 /* Attach template arguments to type. */
13817 if (! VEC_empty (symbolp, template_args))
13818 {
13819 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13820 TYPE_N_TEMPLATE_ARGUMENTS (type)
13821 = VEC_length (symbolp, template_args);
13822 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13823 = XOBNEWVEC (&objfile->objfile_obstack,
13824 struct symbol *,
13825 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13826 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13827 VEC_address (symbolp, template_args),
13828 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13829 * sizeof (struct symbol *)));
13830 VEC_free (symbolp, template_args);
13831 }
13832
c906108c
SS
13833 /* Attach fields and member functions to the type. */
13834 if (fi.nfields)
e7c27a73 13835 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13836 if (fi.nfnfields)
13837 {
e7c27a73 13838 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13839
c5aa993b 13840 /* Get the type which refers to the base class (possibly this
c906108c 13841 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13842 class from the DW_AT_containing_type attribute. This use of
13843 DW_AT_containing_type is a GNU extension. */
c906108c 13844
e142c38c 13845 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13846 {
e7c27a73 13847 struct type *t = die_containing_type (die, cu);
c906108c 13848
ae6ae975 13849 set_type_vptr_basetype (type, t);
c906108c
SS
13850 if (type == t)
13851 {
c906108c
SS
13852 int i;
13853
13854 /* Our own class provides vtbl ptr. */
13855 for (i = TYPE_NFIELDS (t) - 1;
13856 i >= TYPE_N_BASECLASSES (t);
13857 --i)
13858 {
0d5cff50 13859 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13860
1168df01 13861 if (is_vtable_name (fieldname, cu))
c906108c 13862 {
ae6ae975 13863 set_type_vptr_fieldno (type, i);
c906108c
SS
13864 break;
13865 }
13866 }
13867
13868 /* Complain if virtual function table field not found. */
13869 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13870 complaint (&symfile_complaints,
3e43a32a
MS
13871 _("virtual function table pointer "
13872 "not found when defining class '%s'"),
4d3c2250
KB
13873 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13874 "");
c906108c
SS
13875 }
13876 else
13877 {
ae6ae975 13878 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13879 }
13880 }
f6235d4c 13881 else if (cu->producer
61012eef 13882 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13883 {
13884 /* The IBM XLC compiler does not provide direct indication
13885 of the containing type, but the vtable pointer is
13886 always named __vfp. */
13887
13888 int i;
13889
13890 for (i = TYPE_NFIELDS (type) - 1;
13891 i >= TYPE_N_BASECLASSES (type);
13892 --i)
13893 {
13894 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13895 {
ae6ae975
DE
13896 set_type_vptr_fieldno (type, i);
13897 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13898 break;
13899 }
13900 }
13901 }
c906108c 13902 }
98751a41
JK
13903
13904 /* Copy fi.typedef_field_list linked list elements content into the
13905 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13906 if (fi.typedef_field_list)
13907 {
13908 int i = fi.typedef_field_list_count;
13909
a0d7a4ff 13910 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13911 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13912 = ((struct typedef_field *)
13913 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13914 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13915
13916 /* Reverse the list order to keep the debug info elements order. */
13917 while (--i >= 0)
13918 {
13919 struct typedef_field *dest, *src;
6e70227d 13920
98751a41
JK
13921 dest = &TYPE_TYPEDEF_FIELD (type, i);
13922 src = &fi.typedef_field_list->field;
13923 fi.typedef_field_list = fi.typedef_field_list->next;
13924 *dest = *src;
13925 }
13926 }
c767944b
DJ
13927
13928 do_cleanups (back_to);
c906108c 13929 }
63d06c5c 13930
bb5ed363 13931 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13932
90aeadfc
DC
13933 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13934 snapshots) has been known to create a die giving a declaration
13935 for a class that has, as a child, a die giving a definition for a
13936 nested class. So we have to process our children even if the
13937 current die is a declaration. Normally, of course, a declaration
13938 won't have any children at all. */
134d01f1 13939
ca040673
DE
13940 child_die = die->child;
13941
90aeadfc
DC
13942 while (child_die != NULL && child_die->tag)
13943 {
13944 if (child_die->tag == DW_TAG_member
13945 || child_die->tag == DW_TAG_variable
34eaf542
TT
13946 || child_die->tag == DW_TAG_inheritance
13947 || child_die->tag == DW_TAG_template_value_param
13948 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13949 {
90aeadfc 13950 /* Do nothing. */
134d01f1 13951 }
90aeadfc
DC
13952 else
13953 process_die (child_die, cu);
134d01f1 13954
90aeadfc 13955 child_die = sibling_die (child_die);
134d01f1
DJ
13956 }
13957
fa4028e9
JB
13958 /* Do not consider external references. According to the DWARF standard,
13959 these DIEs are identified by the fact that they have no byte_size
13960 attribute, and a declaration attribute. */
13961 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13962 || !die_is_declaration (die, cu))
c767944b 13963 new_symbol (die, type, cu);
134d01f1
DJ
13964}
13965
55426c9d
JB
13966/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13967 update TYPE using some information only available in DIE's children. */
13968
13969static void
13970update_enumeration_type_from_children (struct die_info *die,
13971 struct type *type,
13972 struct dwarf2_cu *cu)
13973{
60f7655a 13974 struct die_info *child_die;
55426c9d
JB
13975 int unsigned_enum = 1;
13976 int flag_enum = 1;
13977 ULONGEST mask = 0;
55426c9d 13978
8268c778 13979 auto_obstack obstack;
55426c9d 13980
60f7655a
DE
13981 for (child_die = die->child;
13982 child_die != NULL && child_die->tag;
13983 child_die = sibling_die (child_die))
55426c9d
JB
13984 {
13985 struct attribute *attr;
13986 LONGEST value;
13987 const gdb_byte *bytes;
13988 struct dwarf2_locexpr_baton *baton;
13989 const char *name;
60f7655a 13990
55426c9d
JB
13991 if (child_die->tag != DW_TAG_enumerator)
13992 continue;
13993
13994 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13995 if (attr == NULL)
13996 continue;
13997
13998 name = dwarf2_name (child_die, cu);
13999 if (name == NULL)
14000 name = "<anonymous enumerator>";
14001
14002 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14003 &value, &bytes, &baton);
14004 if (value < 0)
14005 {
14006 unsigned_enum = 0;
14007 flag_enum = 0;
14008 }
14009 else if ((mask & value) != 0)
14010 flag_enum = 0;
14011 else
14012 mask |= value;
14013
14014 /* If we already know that the enum type is neither unsigned, nor
14015 a flag type, no need to look at the rest of the enumerates. */
14016 if (!unsigned_enum && !flag_enum)
14017 break;
55426c9d
JB
14018 }
14019
14020 if (unsigned_enum)
14021 TYPE_UNSIGNED (type) = 1;
14022 if (flag_enum)
14023 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14024}
14025
134d01f1
DJ
14026/* Given a DW_AT_enumeration_type die, set its type. We do not
14027 complete the type's fields yet, or create any symbols. */
c906108c 14028
f792889a 14029static struct type *
134d01f1 14030read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14031{
e7c27a73 14032 struct objfile *objfile = cu->objfile;
c906108c 14033 struct type *type;
c906108c 14034 struct attribute *attr;
0114d602 14035 const char *name;
134d01f1 14036
348e048f
DE
14037 /* If the definition of this type lives in .debug_types, read that type.
14038 Don't follow DW_AT_specification though, that will take us back up
14039 the chain and we want to go down. */
45e58e77 14040 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14041 if (attr)
14042 {
ac9ec31b 14043 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14044
ac9ec31b 14045 /* The type's CU may not be the same as CU.
02142a6c 14046 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14047 return set_die_type (die, type, cu);
14048 }
14049
c906108c
SS
14050 type = alloc_type (objfile);
14051
14052 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14053 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14054 if (name != NULL)
7d455152 14055 TYPE_TAG_NAME (type) = name;
c906108c 14056
0626fc76
TT
14057 attr = dwarf2_attr (die, DW_AT_type, cu);
14058 if (attr != NULL)
14059 {
14060 struct type *underlying_type = die_type (die, cu);
14061
14062 TYPE_TARGET_TYPE (type) = underlying_type;
14063 }
14064
e142c38c 14065 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14066 if (attr)
14067 {
14068 TYPE_LENGTH (type) = DW_UNSND (attr);
14069 }
14070 else
14071 {
14072 TYPE_LENGTH (type) = 0;
14073 }
14074
137033e9
JB
14075 /* The enumeration DIE can be incomplete. In Ada, any type can be
14076 declared as private in the package spec, and then defined only
14077 inside the package body. Such types are known as Taft Amendment
14078 Types. When another package uses such a type, an incomplete DIE
14079 may be generated by the compiler. */
02eb380e 14080 if (die_is_declaration (die, cu))
876cecd0 14081 TYPE_STUB (type) = 1;
02eb380e 14082
0626fc76
TT
14083 /* Finish the creation of this type by using the enum's children.
14084 We must call this even when the underlying type has been provided
14085 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14086 update_enumeration_type_from_children (die, type, cu);
14087
0626fc76
TT
14088 /* If this type has an underlying type that is not a stub, then we
14089 may use its attributes. We always use the "unsigned" attribute
14090 in this situation, because ordinarily we guess whether the type
14091 is unsigned -- but the guess can be wrong and the underlying type
14092 can tell us the reality. However, we defer to a local size
14093 attribute if one exists, because this lets the compiler override
14094 the underlying type if needed. */
14095 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14096 {
14097 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14098 if (TYPE_LENGTH (type) == 0)
14099 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14100 }
14101
3d567982
TT
14102 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14103
f792889a 14104 return set_die_type (die, type, cu);
134d01f1
DJ
14105}
14106
14107/* Given a pointer to a die which begins an enumeration, process all
14108 the dies that define the members of the enumeration, and create the
14109 symbol for the enumeration type.
14110
14111 NOTE: We reverse the order of the element list. */
14112
14113static void
14114process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14115{
f792889a 14116 struct type *this_type;
134d01f1 14117
f792889a
DJ
14118 this_type = get_die_type (die, cu);
14119 if (this_type == NULL)
14120 this_type = read_enumeration_type (die, cu);
9dc481d3 14121
639d11d3 14122 if (die->child != NULL)
c906108c 14123 {
9dc481d3
DE
14124 struct die_info *child_die;
14125 struct symbol *sym;
14126 struct field *fields = NULL;
14127 int num_fields = 0;
15d034d0 14128 const char *name;
9dc481d3 14129
639d11d3 14130 child_die = die->child;
c906108c
SS
14131 while (child_die && child_die->tag)
14132 {
14133 if (child_die->tag != DW_TAG_enumerator)
14134 {
e7c27a73 14135 process_die (child_die, cu);
c906108c
SS
14136 }
14137 else
14138 {
39cbfefa
DJ
14139 name = dwarf2_name (child_die, cu);
14140 if (name)
c906108c 14141 {
f792889a 14142 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14143
14144 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14145 {
14146 fields = (struct field *)
14147 xrealloc (fields,
14148 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14149 * sizeof (struct field));
c906108c
SS
14150 }
14151
3567439c 14152 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14153 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14154 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14155 FIELD_BITSIZE (fields[num_fields]) = 0;
14156
14157 num_fields++;
14158 }
14159 }
14160
14161 child_die = sibling_die (child_die);
14162 }
14163
14164 if (num_fields)
14165 {
f792889a
DJ
14166 TYPE_NFIELDS (this_type) = num_fields;
14167 TYPE_FIELDS (this_type) = (struct field *)
14168 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14169 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14170 sizeof (struct field) * num_fields);
b8c9b27d 14171 xfree (fields);
c906108c 14172 }
c906108c 14173 }
134d01f1 14174
6c83ed52
TT
14175 /* If we are reading an enum from a .debug_types unit, and the enum
14176 is a declaration, and the enum is not the signatured type in the
14177 unit, then we do not want to add a symbol for it. Adding a
14178 symbol would in some cases obscure the true definition of the
14179 enum, giving users an incomplete type when the definition is
14180 actually available. Note that we do not want to do this for all
14181 enums which are just declarations, because C++0x allows forward
14182 enum declarations. */
3019eac3 14183 if (cu->per_cu->is_debug_types
6c83ed52
TT
14184 && die_is_declaration (die, cu))
14185 {
52dc124a 14186 struct signatured_type *sig_type;
6c83ed52 14187
c0f78cd4 14188 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14189 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14190 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14191 return;
14192 }
14193
f792889a 14194 new_symbol (die, this_type, cu);
c906108c
SS
14195}
14196
14197/* Extract all information from a DW_TAG_array_type DIE and put it in
14198 the DIE's type field. For now, this only handles one dimensional
14199 arrays. */
14200
f792889a 14201static struct type *
e7c27a73 14202read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14203{
e7c27a73 14204 struct objfile *objfile = cu->objfile;
c906108c 14205 struct die_info *child_die;
7e314c57 14206 struct type *type;
c906108c
SS
14207 struct type *element_type, *range_type, *index_type;
14208 struct type **range_types = NULL;
14209 struct attribute *attr;
14210 int ndim = 0;
14211 struct cleanup *back_to;
15d034d0 14212 const char *name;
dc53a7ad 14213 unsigned int bit_stride = 0;
c906108c 14214
e7c27a73 14215 element_type = die_type (die, cu);
c906108c 14216
7e314c57
JK
14217 /* The die_type call above may have already set the type for this DIE. */
14218 type = get_die_type (die, cu);
14219 if (type)
14220 return type;
14221
dc53a7ad
JB
14222 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14223 if (attr != NULL)
14224 bit_stride = DW_UNSND (attr) * 8;
14225
14226 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14227 if (attr != NULL)
14228 bit_stride = DW_UNSND (attr);
14229
c906108c
SS
14230 /* Irix 6.2 native cc creates array types without children for
14231 arrays with unspecified length. */
639d11d3 14232 if (die->child == NULL)
c906108c 14233 {
46bf5051 14234 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14235 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14236 type = create_array_type_with_stride (NULL, element_type, range_type,
14237 bit_stride);
f792889a 14238 return set_die_type (die, type, cu);
c906108c
SS
14239 }
14240
14241 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14242 child_die = die->child;
c906108c
SS
14243 while (child_die && child_die->tag)
14244 {
14245 if (child_die->tag == DW_TAG_subrange_type)
14246 {
f792889a 14247 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14248
f792889a 14249 if (child_type != NULL)
a02abb62 14250 {
0963b4bd
MS
14251 /* The range type was succesfully read. Save it for the
14252 array type creation. */
a02abb62
JB
14253 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14254 {
14255 range_types = (struct type **)
14256 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14257 * sizeof (struct type *));
14258 if (ndim == 0)
14259 make_cleanup (free_current_contents, &range_types);
14260 }
f792889a 14261 range_types[ndim++] = child_type;
a02abb62 14262 }
c906108c
SS
14263 }
14264 child_die = sibling_die (child_die);
14265 }
14266
14267 /* Dwarf2 dimensions are output from left to right, create the
14268 necessary array types in backwards order. */
7ca2d3a3 14269
c906108c 14270 type = element_type;
7ca2d3a3
DL
14271
14272 if (read_array_order (die, cu) == DW_ORD_col_major)
14273 {
14274 int i = 0;
9a619af0 14275
7ca2d3a3 14276 while (i < ndim)
dc53a7ad
JB
14277 type = create_array_type_with_stride (NULL, type, range_types[i++],
14278 bit_stride);
7ca2d3a3
DL
14279 }
14280 else
14281 {
14282 while (ndim-- > 0)
dc53a7ad
JB
14283 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14284 bit_stride);
7ca2d3a3 14285 }
c906108c 14286
f5f8a009
EZ
14287 /* Understand Dwarf2 support for vector types (like they occur on
14288 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14289 array type. This is not part of the Dwarf2/3 standard yet, but a
14290 custom vendor extension. The main difference between a regular
14291 array and the vector variant is that vectors are passed by value
14292 to functions. */
e142c38c 14293 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14294 if (attr)
ea37ba09 14295 make_vector_type (type);
f5f8a009 14296
dbc98a8b
KW
14297 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14298 implementation may choose to implement triple vectors using this
14299 attribute. */
14300 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14301 if (attr)
14302 {
14303 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14304 TYPE_LENGTH (type) = DW_UNSND (attr);
14305 else
3e43a32a
MS
14306 complaint (&symfile_complaints,
14307 _("DW_AT_byte_size for array type smaller "
14308 "than the total size of elements"));
dbc98a8b
KW
14309 }
14310
39cbfefa
DJ
14311 name = dwarf2_name (die, cu);
14312 if (name)
14313 TYPE_NAME (type) = name;
6e70227d 14314
0963b4bd 14315 /* Install the type in the die. */
7e314c57
JK
14316 set_die_type (die, type, cu);
14317
14318 /* set_die_type should be already done. */
b4ba55a1
JB
14319 set_descriptive_type (type, die, cu);
14320
c906108c
SS
14321 do_cleanups (back_to);
14322
7e314c57 14323 return type;
c906108c
SS
14324}
14325
7ca2d3a3 14326static enum dwarf_array_dim_ordering
6e70227d 14327read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14328{
14329 struct attribute *attr;
14330
14331 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14332
aead7601
SM
14333 if (attr)
14334 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14335
0963b4bd
MS
14336 /* GNU F77 is a special case, as at 08/2004 array type info is the
14337 opposite order to the dwarf2 specification, but data is still
14338 laid out as per normal fortran.
7ca2d3a3 14339
0963b4bd
MS
14340 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14341 version checking. */
7ca2d3a3 14342
905e0470
PM
14343 if (cu->language == language_fortran
14344 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14345 {
14346 return DW_ORD_row_major;
14347 }
14348
6e70227d 14349 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14350 {
14351 case array_column_major:
14352 return DW_ORD_col_major;
14353 case array_row_major:
14354 default:
14355 return DW_ORD_row_major;
14356 };
14357}
14358
72019c9c 14359/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14360 the DIE's type field. */
72019c9c 14361
f792889a 14362static struct type *
72019c9c
GM
14363read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14364{
7e314c57
JK
14365 struct type *domain_type, *set_type;
14366 struct attribute *attr;
f792889a 14367
7e314c57
JK
14368 domain_type = die_type (die, cu);
14369
14370 /* The die_type call above may have already set the type for this DIE. */
14371 set_type = get_die_type (die, cu);
14372 if (set_type)
14373 return set_type;
14374
14375 set_type = create_set_type (NULL, domain_type);
14376
14377 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14378 if (attr)
14379 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14380
f792889a 14381 return set_die_type (die, set_type, cu);
72019c9c 14382}
7ca2d3a3 14383
0971de02
TT
14384/* A helper for read_common_block that creates a locexpr baton.
14385 SYM is the symbol which we are marking as computed.
14386 COMMON_DIE is the DIE for the common block.
14387 COMMON_LOC is the location expression attribute for the common
14388 block itself.
14389 MEMBER_LOC is the location expression attribute for the particular
14390 member of the common block that we are processing.
14391 CU is the CU from which the above come. */
14392
14393static void
14394mark_common_block_symbol_computed (struct symbol *sym,
14395 struct die_info *common_die,
14396 struct attribute *common_loc,
14397 struct attribute *member_loc,
14398 struct dwarf2_cu *cu)
14399{
14400 struct objfile *objfile = dwarf2_per_objfile->objfile;
14401 struct dwarf2_locexpr_baton *baton;
14402 gdb_byte *ptr;
14403 unsigned int cu_off;
14404 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14405 LONGEST offset = 0;
14406
14407 gdb_assert (common_loc && member_loc);
14408 gdb_assert (attr_form_is_block (common_loc));
14409 gdb_assert (attr_form_is_block (member_loc)
14410 || attr_form_is_constant (member_loc));
14411
8d749320 14412 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14413 baton->per_cu = cu->per_cu;
14414 gdb_assert (baton->per_cu);
14415
14416 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14417
14418 if (attr_form_is_constant (member_loc))
14419 {
14420 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14421 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14422 }
14423 else
14424 baton->size += DW_BLOCK (member_loc)->size;
14425
224c3ddb 14426 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14427 baton->data = ptr;
14428
14429 *ptr++ = DW_OP_call4;
9c541725 14430 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14431 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14432 ptr += 4;
14433
14434 if (attr_form_is_constant (member_loc))
14435 {
14436 *ptr++ = DW_OP_addr;
14437 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14438 ptr += cu->header.addr_size;
14439 }
14440 else
14441 {
14442 /* We have to copy the data here, because DW_OP_call4 will only
14443 use a DW_AT_location attribute. */
14444 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14445 ptr += DW_BLOCK (member_loc)->size;
14446 }
14447
14448 *ptr++ = DW_OP_plus;
14449 gdb_assert (ptr - baton->data == baton->size);
14450
0971de02 14451 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14452 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14453}
14454
4357ac6c
TT
14455/* Create appropriate locally-scoped variables for all the
14456 DW_TAG_common_block entries. Also create a struct common_block
14457 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14458 is used to sepate the common blocks name namespace from regular
14459 variable names. */
c906108c
SS
14460
14461static void
e7c27a73 14462read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14463{
0971de02
TT
14464 struct attribute *attr;
14465
14466 attr = dwarf2_attr (die, DW_AT_location, cu);
14467 if (attr)
14468 {
14469 /* Support the .debug_loc offsets. */
14470 if (attr_form_is_block (attr))
14471 {
14472 /* Ok. */
14473 }
14474 else if (attr_form_is_section_offset (attr))
14475 {
14476 dwarf2_complex_location_expr_complaint ();
14477 attr = NULL;
14478 }
14479 else
14480 {
14481 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14482 "common block member");
14483 attr = NULL;
14484 }
14485 }
14486
639d11d3 14487 if (die->child != NULL)
c906108c 14488 {
4357ac6c
TT
14489 struct objfile *objfile = cu->objfile;
14490 struct die_info *child_die;
14491 size_t n_entries = 0, size;
14492 struct common_block *common_block;
14493 struct symbol *sym;
74ac6d43 14494
4357ac6c
TT
14495 for (child_die = die->child;
14496 child_die && child_die->tag;
14497 child_die = sibling_die (child_die))
14498 ++n_entries;
14499
14500 size = (sizeof (struct common_block)
14501 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14502 common_block
14503 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14504 size);
4357ac6c
TT
14505 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14506 common_block->n_entries = 0;
14507
14508 for (child_die = die->child;
14509 child_die && child_die->tag;
14510 child_die = sibling_die (child_die))
14511 {
14512 /* Create the symbol in the DW_TAG_common_block block in the current
14513 symbol scope. */
e7c27a73 14514 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14515 if (sym != NULL)
14516 {
14517 struct attribute *member_loc;
14518
14519 common_block->contents[common_block->n_entries++] = sym;
14520
14521 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14522 cu);
14523 if (member_loc)
14524 {
14525 /* GDB has handled this for a long time, but it is
14526 not specified by DWARF. It seems to have been
14527 emitted by gfortran at least as recently as:
14528 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14529 complaint (&symfile_complaints,
14530 _("Variable in common block has "
14531 "DW_AT_data_member_location "
14532 "- DIE at 0x%x [in module %s]"),
9c541725 14533 to_underlying (child_die->sect_off),
4262abfb 14534 objfile_name (cu->objfile));
0971de02
TT
14535
14536 if (attr_form_is_section_offset (member_loc))
14537 dwarf2_complex_location_expr_complaint ();
14538 else if (attr_form_is_constant (member_loc)
14539 || attr_form_is_block (member_loc))
14540 {
14541 if (attr)
14542 mark_common_block_symbol_computed (sym, die, attr,
14543 member_loc, cu);
14544 }
14545 else
14546 dwarf2_complex_location_expr_complaint ();
14547 }
14548 }
c906108c 14549 }
4357ac6c
TT
14550
14551 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14552 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14553 }
14554}
14555
0114d602 14556/* Create a type for a C++ namespace. */
d9fa45fe 14557
0114d602
DJ
14558static struct type *
14559read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14560{
e7c27a73 14561 struct objfile *objfile = cu->objfile;
0114d602 14562 const char *previous_prefix, *name;
9219021c 14563 int is_anonymous;
0114d602
DJ
14564 struct type *type;
14565
14566 /* For extensions, reuse the type of the original namespace. */
14567 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14568 {
14569 struct die_info *ext_die;
14570 struct dwarf2_cu *ext_cu = cu;
9a619af0 14571
0114d602
DJ
14572 ext_die = dwarf2_extension (die, &ext_cu);
14573 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14574
14575 /* EXT_CU may not be the same as CU.
02142a6c 14576 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14577 return set_die_type (die, type, cu);
14578 }
9219021c 14579
e142c38c 14580 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14581
14582 /* Now build the name of the current namespace. */
14583
0114d602
DJ
14584 previous_prefix = determine_prefix (die, cu);
14585 if (previous_prefix[0] != '\0')
14586 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14587 previous_prefix, name, 0, cu);
0114d602
DJ
14588
14589 /* Create the type. */
19f392bc 14590 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14591 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14592
60531b24 14593 return set_die_type (die, type, cu);
0114d602
DJ
14594}
14595
22cee43f 14596/* Read a namespace scope. */
0114d602
DJ
14597
14598static void
14599read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14600{
14601 struct objfile *objfile = cu->objfile;
0114d602 14602 int is_anonymous;
9219021c 14603
5c4e30ca
DC
14604 /* Add a symbol associated to this if we haven't seen the namespace
14605 before. Also, add a using directive if it's an anonymous
14606 namespace. */
9219021c 14607
f2f0e013 14608 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14609 {
14610 struct type *type;
14611
0114d602 14612 type = read_type_die (die, cu);
e7c27a73 14613 new_symbol (die, type, cu);
5c4e30ca 14614
e8e80198 14615 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14616 if (is_anonymous)
0114d602
DJ
14617 {
14618 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14619
22cee43f
PMR
14620 add_using_directive (using_directives (cu->language),
14621 previous_prefix, TYPE_NAME (type), NULL,
14622 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14623 }
5c4e30ca 14624 }
9219021c 14625
639d11d3 14626 if (die->child != NULL)
d9fa45fe 14627 {
639d11d3 14628 struct die_info *child_die = die->child;
6e70227d 14629
d9fa45fe
DC
14630 while (child_die && child_die->tag)
14631 {
e7c27a73 14632 process_die (child_die, cu);
d9fa45fe
DC
14633 child_die = sibling_die (child_die);
14634 }
14635 }
38d518c9
EZ
14636}
14637
f55ee35c
JK
14638/* Read a Fortran module as type. This DIE can be only a declaration used for
14639 imported module. Still we need that type as local Fortran "use ... only"
14640 declaration imports depend on the created type in determine_prefix. */
14641
14642static struct type *
14643read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14644{
14645 struct objfile *objfile = cu->objfile;
15d034d0 14646 const char *module_name;
f55ee35c
JK
14647 struct type *type;
14648
14649 module_name = dwarf2_name (die, cu);
14650 if (!module_name)
3e43a32a
MS
14651 complaint (&symfile_complaints,
14652 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14653 to_underlying (die->sect_off));
19f392bc 14654 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14655
14656 /* determine_prefix uses TYPE_TAG_NAME. */
14657 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14658
14659 return set_die_type (die, type, cu);
14660}
14661
5d7cb8df
JK
14662/* Read a Fortran module. */
14663
14664static void
14665read_module (struct die_info *die, struct dwarf2_cu *cu)
14666{
14667 struct die_info *child_die = die->child;
530e8392
KB
14668 struct type *type;
14669
14670 type = read_type_die (die, cu);
14671 new_symbol (die, type, cu);
5d7cb8df 14672
5d7cb8df
JK
14673 while (child_die && child_die->tag)
14674 {
14675 process_die (child_die, cu);
14676 child_die = sibling_die (child_die);
14677 }
14678}
14679
38d518c9
EZ
14680/* Return the name of the namespace represented by DIE. Set
14681 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14682 namespace. */
14683
14684static const char *
e142c38c 14685namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14686{
14687 struct die_info *current_die;
14688 const char *name = NULL;
14689
14690 /* Loop through the extensions until we find a name. */
14691
14692 for (current_die = die;
14693 current_die != NULL;
f2f0e013 14694 current_die = dwarf2_extension (die, &cu))
38d518c9 14695 {
96553a0c
DE
14696 /* We don't use dwarf2_name here so that we can detect the absence
14697 of a name -> anonymous namespace. */
7d45c7c3 14698 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14699
38d518c9
EZ
14700 if (name != NULL)
14701 break;
14702 }
14703
14704 /* Is it an anonymous namespace? */
14705
14706 *is_anonymous = (name == NULL);
14707 if (*is_anonymous)
2b1dbab0 14708 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14709
14710 return name;
d9fa45fe
DC
14711}
14712
c906108c
SS
14713/* Extract all information from a DW_TAG_pointer_type DIE and add to
14714 the user defined type vector. */
14715
f792889a 14716static struct type *
e7c27a73 14717read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14718{
5e2b427d 14719 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14720 struct comp_unit_head *cu_header = &cu->header;
c906108c 14721 struct type *type;
8b2dbe47
KB
14722 struct attribute *attr_byte_size;
14723 struct attribute *attr_address_class;
14724 int byte_size, addr_class;
7e314c57
JK
14725 struct type *target_type;
14726
14727 target_type = die_type (die, cu);
c906108c 14728
7e314c57
JK
14729 /* The die_type call above may have already set the type for this DIE. */
14730 type = get_die_type (die, cu);
14731 if (type)
14732 return type;
14733
14734 type = lookup_pointer_type (target_type);
8b2dbe47 14735
e142c38c 14736 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14737 if (attr_byte_size)
14738 byte_size = DW_UNSND (attr_byte_size);
c906108c 14739 else
8b2dbe47
KB
14740 byte_size = cu_header->addr_size;
14741
e142c38c 14742 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14743 if (attr_address_class)
14744 addr_class = DW_UNSND (attr_address_class);
14745 else
14746 addr_class = DW_ADDR_none;
14747
14748 /* If the pointer size or address class is different than the
14749 default, create a type variant marked as such and set the
14750 length accordingly. */
14751 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14752 {
5e2b427d 14753 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14754 {
14755 int type_flags;
14756
849957d9 14757 type_flags = gdbarch_address_class_type_flags
5e2b427d 14758 (gdbarch, byte_size, addr_class);
876cecd0
TT
14759 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14760 == 0);
8b2dbe47
KB
14761 type = make_type_with_address_space (type, type_flags);
14762 }
14763 else if (TYPE_LENGTH (type) != byte_size)
14764 {
3e43a32a
MS
14765 complaint (&symfile_complaints,
14766 _("invalid pointer size %d"), byte_size);
8b2dbe47 14767 }
6e70227d 14768 else
9a619af0
MS
14769 {
14770 /* Should we also complain about unhandled address classes? */
14771 }
c906108c 14772 }
8b2dbe47
KB
14773
14774 TYPE_LENGTH (type) = byte_size;
f792889a 14775 return set_die_type (die, type, cu);
c906108c
SS
14776}
14777
14778/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14779 the user defined type vector. */
14780
f792889a 14781static struct type *
e7c27a73 14782read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14783{
14784 struct type *type;
14785 struct type *to_type;
14786 struct type *domain;
14787
e7c27a73
DJ
14788 to_type = die_type (die, cu);
14789 domain = die_containing_type (die, cu);
0d5de010 14790
7e314c57
JK
14791 /* The calls above may have already set the type for this DIE. */
14792 type = get_die_type (die, cu);
14793 if (type)
14794 return type;
14795
0d5de010
DJ
14796 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14797 type = lookup_methodptr_type (to_type);
7078baeb
TT
14798 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14799 {
14800 struct type *new_type = alloc_type (cu->objfile);
14801
14802 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14803 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14804 TYPE_VARARGS (to_type));
14805 type = lookup_methodptr_type (new_type);
14806 }
0d5de010
DJ
14807 else
14808 type = lookup_memberptr_type (to_type, domain);
c906108c 14809
f792889a 14810 return set_die_type (die, type, cu);
c906108c
SS
14811}
14812
4297a3f0 14813/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14814 the user defined type vector. */
14815
f792889a 14816static struct type *
4297a3f0
AV
14817read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14818 enum type_code refcode)
c906108c 14819{
e7c27a73 14820 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14821 struct type *type, *target_type;
c906108c
SS
14822 struct attribute *attr;
14823
4297a3f0
AV
14824 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14825
7e314c57
JK
14826 target_type = die_type (die, cu);
14827
14828 /* The die_type call above may have already set the type for this DIE. */
14829 type = get_die_type (die, cu);
14830 if (type)
14831 return type;
14832
4297a3f0 14833 type = lookup_reference_type (target_type, refcode);
e142c38c 14834 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14835 if (attr)
14836 {
14837 TYPE_LENGTH (type) = DW_UNSND (attr);
14838 }
14839 else
14840 {
107d2387 14841 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14842 }
f792889a 14843 return set_die_type (die, type, cu);
c906108c
SS
14844}
14845
cf363f18
MW
14846/* Add the given cv-qualifiers to the element type of the array. GCC
14847 outputs DWARF type qualifiers that apply to an array, not the
14848 element type. But GDB relies on the array element type to carry
14849 the cv-qualifiers. This mimics section 6.7.3 of the C99
14850 specification. */
14851
14852static struct type *
14853add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14854 struct type *base_type, int cnst, int voltl)
14855{
14856 struct type *el_type, *inner_array;
14857
14858 base_type = copy_type (base_type);
14859 inner_array = base_type;
14860
14861 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14862 {
14863 TYPE_TARGET_TYPE (inner_array) =
14864 copy_type (TYPE_TARGET_TYPE (inner_array));
14865 inner_array = TYPE_TARGET_TYPE (inner_array);
14866 }
14867
14868 el_type = TYPE_TARGET_TYPE (inner_array);
14869 cnst |= TYPE_CONST (el_type);
14870 voltl |= TYPE_VOLATILE (el_type);
14871 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14872
14873 return set_die_type (die, base_type, cu);
14874}
14875
f792889a 14876static struct type *
e7c27a73 14877read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14878{
f792889a 14879 struct type *base_type, *cv_type;
c906108c 14880
e7c27a73 14881 base_type = die_type (die, cu);
7e314c57
JK
14882
14883 /* The die_type call above may have already set the type for this DIE. */
14884 cv_type = get_die_type (die, cu);
14885 if (cv_type)
14886 return cv_type;
14887
2f608a3a
KW
14888 /* In case the const qualifier is applied to an array type, the element type
14889 is so qualified, not the array type (section 6.7.3 of C99). */
14890 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14891 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14892
f792889a
DJ
14893 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14894 return set_die_type (die, cv_type, cu);
c906108c
SS
14895}
14896
f792889a 14897static struct type *
e7c27a73 14898read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14899{
f792889a 14900 struct type *base_type, *cv_type;
c906108c 14901
e7c27a73 14902 base_type = die_type (die, cu);
7e314c57
JK
14903
14904 /* The die_type call above may have already set the type for this DIE. */
14905 cv_type = get_die_type (die, cu);
14906 if (cv_type)
14907 return cv_type;
14908
cf363f18
MW
14909 /* In case the volatile qualifier is applied to an array type, the
14910 element type is so qualified, not the array type (section 6.7.3
14911 of C99). */
14912 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14913 return add_array_cv_type (die, cu, base_type, 0, 1);
14914
f792889a
DJ
14915 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14916 return set_die_type (die, cv_type, cu);
c906108c
SS
14917}
14918
06d66ee9
TT
14919/* Handle DW_TAG_restrict_type. */
14920
14921static struct type *
14922read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14923{
14924 struct type *base_type, *cv_type;
14925
14926 base_type = die_type (die, cu);
14927
14928 /* The die_type call above may have already set the type for this DIE. */
14929 cv_type = get_die_type (die, cu);
14930 if (cv_type)
14931 return cv_type;
14932
14933 cv_type = make_restrict_type (base_type);
14934 return set_die_type (die, cv_type, cu);
14935}
14936
a2c2acaf
MW
14937/* Handle DW_TAG_atomic_type. */
14938
14939static struct type *
14940read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14941{
14942 struct type *base_type, *cv_type;
14943
14944 base_type = die_type (die, cu);
14945
14946 /* The die_type call above may have already set the type for this DIE. */
14947 cv_type = get_die_type (die, cu);
14948 if (cv_type)
14949 return cv_type;
14950
14951 cv_type = make_atomic_type (base_type);
14952 return set_die_type (die, cv_type, cu);
14953}
14954
c906108c
SS
14955/* Extract all information from a DW_TAG_string_type DIE and add to
14956 the user defined type vector. It isn't really a user defined type,
14957 but it behaves like one, with other DIE's using an AT_user_def_type
14958 attribute to reference it. */
14959
f792889a 14960static struct type *
e7c27a73 14961read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14962{
e7c27a73 14963 struct objfile *objfile = cu->objfile;
3b7538c0 14964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14965 struct type *type, *range_type, *index_type, *char_type;
14966 struct attribute *attr;
14967 unsigned int length;
14968
e142c38c 14969 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14970 if (attr)
14971 {
14972 length = DW_UNSND (attr);
14973 }
14974 else
14975 {
0963b4bd 14976 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14978 if (attr)
14979 {
14980 length = DW_UNSND (attr);
14981 }
14982 else
14983 {
14984 length = 1;
14985 }
c906108c 14986 }
6ccb9162 14987
46bf5051 14988 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14989 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14990 char_type = language_string_char_type (cu->language_defn, gdbarch);
14991 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14992
f792889a 14993 return set_die_type (die, type, cu);
c906108c
SS
14994}
14995
4d804846
JB
14996/* Assuming that DIE corresponds to a function, returns nonzero
14997 if the function is prototyped. */
14998
14999static int
15000prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15001{
15002 struct attribute *attr;
15003
15004 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15005 if (attr && (DW_UNSND (attr) != 0))
15006 return 1;
15007
15008 /* The DWARF standard implies that the DW_AT_prototyped attribute
15009 is only meaninful for C, but the concept also extends to other
15010 languages that allow unprototyped functions (Eg: Objective C).
15011 For all other languages, assume that functions are always
15012 prototyped. */
15013 if (cu->language != language_c
15014 && cu->language != language_objc
15015 && cu->language != language_opencl)
15016 return 1;
15017
15018 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15019 prototyped and unprototyped functions; default to prototyped,
15020 since that is more common in modern code (and RealView warns
15021 about unprototyped functions). */
15022 if (producer_is_realview (cu->producer))
15023 return 1;
15024
15025 return 0;
15026}
15027
c906108c
SS
15028/* Handle DIES due to C code like:
15029
15030 struct foo
c5aa993b
JM
15031 {
15032 int (*funcp)(int a, long l);
15033 int b;
15034 };
c906108c 15035
0963b4bd 15036 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15037
f792889a 15038static struct type *
e7c27a73 15039read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15040{
bb5ed363 15041 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15042 struct type *type; /* Type that this function returns. */
15043 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15044 struct attribute *attr;
15045
e7c27a73 15046 type = die_type (die, cu);
7e314c57
JK
15047
15048 /* The die_type call above may have already set the type for this DIE. */
15049 ftype = get_die_type (die, cu);
15050 if (ftype)
15051 return ftype;
15052
0c8b41f1 15053 ftype = lookup_function_type (type);
c906108c 15054
4d804846 15055 if (prototyped_function_p (die, cu))
a6c727b2 15056 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15057
c055b101
CV
15058 /* Store the calling convention in the type if it's available in
15059 the subroutine die. Otherwise set the calling convention to
15060 the default value DW_CC_normal. */
15061 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15062 if (attr)
15063 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15064 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15065 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15066 else
15067 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15068
743649fd
MW
15069 /* Record whether the function returns normally to its caller or not
15070 if the DWARF producer set that information. */
15071 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15072 if (attr && (DW_UNSND (attr) != 0))
15073 TYPE_NO_RETURN (ftype) = 1;
15074
76c10ea2
GM
15075 /* We need to add the subroutine type to the die immediately so
15076 we don't infinitely recurse when dealing with parameters
0963b4bd 15077 declared as the same subroutine type. */
76c10ea2 15078 set_die_type (die, ftype, cu);
6e70227d 15079
639d11d3 15080 if (die->child != NULL)
c906108c 15081 {
bb5ed363 15082 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15083 struct die_info *child_die;
8072405b 15084 int nparams, iparams;
c906108c
SS
15085
15086 /* Count the number of parameters.
15087 FIXME: GDB currently ignores vararg functions, but knows about
15088 vararg member functions. */
8072405b 15089 nparams = 0;
639d11d3 15090 child_die = die->child;
c906108c
SS
15091 while (child_die && child_die->tag)
15092 {
15093 if (child_die->tag == DW_TAG_formal_parameter)
15094 nparams++;
15095 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15096 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15097 child_die = sibling_die (child_die);
15098 }
15099
15100 /* Allocate storage for parameters and fill them in. */
15101 TYPE_NFIELDS (ftype) = nparams;
15102 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15103 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15104
8072405b
JK
15105 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15106 even if we error out during the parameters reading below. */
15107 for (iparams = 0; iparams < nparams; iparams++)
15108 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15109
15110 iparams = 0;
639d11d3 15111 child_die = die->child;
c906108c
SS
15112 while (child_die && child_die->tag)
15113 {
15114 if (child_die->tag == DW_TAG_formal_parameter)
15115 {
3ce3b1ba
PA
15116 struct type *arg_type;
15117
15118 /* DWARF version 2 has no clean way to discern C++
15119 static and non-static member functions. G++ helps
15120 GDB by marking the first parameter for non-static
15121 member functions (which is the this pointer) as
15122 artificial. We pass this information to
15123 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15124
15125 DWARF version 3 added DW_AT_object_pointer, which GCC
15126 4.5 does not yet generate. */
e142c38c 15127 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15128 if (attr)
15129 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15130 else
9c37b5ae 15131 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15132 arg_type = die_type (child_die, cu);
15133
15134 /* RealView does not mark THIS as const, which the testsuite
15135 expects. GCC marks THIS as const in method definitions,
15136 but not in the class specifications (GCC PR 43053). */
15137 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15138 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15139 {
15140 int is_this = 0;
15141 struct dwarf2_cu *arg_cu = cu;
15142 const char *name = dwarf2_name (child_die, cu);
15143
15144 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15145 if (attr)
15146 {
15147 /* If the compiler emits this, use it. */
15148 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15149 is_this = 1;
15150 }
15151 else if (name && strcmp (name, "this") == 0)
15152 /* Function definitions will have the argument names. */
15153 is_this = 1;
15154 else if (name == NULL && iparams == 0)
15155 /* Declarations may not have the names, so like
15156 elsewhere in GDB, assume an artificial first
15157 argument is "this". */
15158 is_this = 1;
15159
15160 if (is_this)
15161 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15162 arg_type, 0);
15163 }
15164
15165 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15166 iparams++;
15167 }
15168 child_die = sibling_die (child_die);
15169 }
15170 }
15171
76c10ea2 15172 return ftype;
c906108c
SS
15173}
15174
f792889a 15175static struct type *
e7c27a73 15176read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15177{
e7c27a73 15178 struct objfile *objfile = cu->objfile;
0114d602 15179 const char *name = NULL;
3c8e0968 15180 struct type *this_type, *target_type;
c906108c 15181
94af9270 15182 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15183 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15184 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15185 set_die_type (die, this_type, cu);
3c8e0968
DE
15186 target_type = die_type (die, cu);
15187 if (target_type != this_type)
15188 TYPE_TARGET_TYPE (this_type) = target_type;
15189 else
15190 {
15191 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15192 spec and cause infinite loops in GDB. */
15193 complaint (&symfile_complaints,
15194 _("Self-referential DW_TAG_typedef "
15195 "- DIE at 0x%x [in module %s]"),
9c541725 15196 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15197 TYPE_TARGET_TYPE (this_type) = NULL;
15198 }
f792889a 15199 return this_type;
c906108c
SS
15200}
15201
9b790ce7
UW
15202/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15203 (which may be different from NAME) to the architecture back-end to allow
15204 it to guess the correct format if necessary. */
15205
15206static struct type *
15207dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15208 const char *name_hint)
15209{
15210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15211 const struct floatformat **format;
15212 struct type *type;
15213
15214 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15215 if (format)
15216 type = init_float_type (objfile, bits, name, format);
15217 else
15218 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15219
15220 return type;
15221}
15222
c906108c
SS
15223/* Find a representation of a given base type and install
15224 it in the TYPE field of the die. */
15225
f792889a 15226static struct type *
e7c27a73 15227read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15228{
e7c27a73 15229 struct objfile *objfile = cu->objfile;
c906108c
SS
15230 struct type *type;
15231 struct attribute *attr;
19f392bc 15232 int encoding = 0, bits = 0;
15d034d0 15233 const char *name;
c906108c 15234
e142c38c 15235 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15236 if (attr)
15237 {
15238 encoding = DW_UNSND (attr);
15239 }
e142c38c 15240 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15241 if (attr)
15242 {
19f392bc 15243 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15244 }
39cbfefa 15245 name = dwarf2_name (die, cu);
6ccb9162 15246 if (!name)
c906108c 15247 {
6ccb9162
UW
15248 complaint (&symfile_complaints,
15249 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15250 }
6ccb9162
UW
15251
15252 switch (encoding)
c906108c 15253 {
6ccb9162
UW
15254 case DW_ATE_address:
15255 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15256 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15257 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15258 break;
15259 case DW_ATE_boolean:
19f392bc 15260 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15261 break;
15262 case DW_ATE_complex_float:
9b790ce7 15263 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15264 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15265 break;
15266 case DW_ATE_decimal_float:
19f392bc 15267 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15268 break;
15269 case DW_ATE_float:
9b790ce7 15270 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15271 break;
15272 case DW_ATE_signed:
19f392bc 15273 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15274 break;
15275 case DW_ATE_unsigned:
3b2b8fea
TT
15276 if (cu->language == language_fortran
15277 && name
61012eef 15278 && startswith (name, "character("))
19f392bc
UW
15279 type = init_character_type (objfile, bits, 1, name);
15280 else
15281 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15282 break;
15283 case DW_ATE_signed_char:
6e70227d 15284 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15285 || cu->language == language_pascal
15286 || cu->language == language_fortran)
19f392bc
UW
15287 type = init_character_type (objfile, bits, 0, name);
15288 else
15289 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15290 break;
15291 case DW_ATE_unsigned_char:
868a0084 15292 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15293 || cu->language == language_pascal
c44af4eb
TT
15294 || cu->language == language_fortran
15295 || cu->language == language_rust)
19f392bc
UW
15296 type = init_character_type (objfile, bits, 1, name);
15297 else
15298 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15299 break;
75079b2b 15300 case DW_ATE_UTF:
53e710ac
PA
15301 {
15302 gdbarch *arch = get_objfile_arch (objfile);
15303
15304 if (bits == 16)
15305 type = builtin_type (arch)->builtin_char16;
15306 else if (bits == 32)
15307 type = builtin_type (arch)->builtin_char32;
15308 else
15309 {
15310 complaint (&symfile_complaints,
15311 _("unsupported DW_ATE_UTF bit size: '%d'"),
15312 bits);
15313 type = init_integer_type (objfile, bits, 1, name);
15314 }
15315 return set_die_type (die, type, cu);
15316 }
75079b2b
TT
15317 break;
15318
6ccb9162
UW
15319 default:
15320 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15321 dwarf_type_encoding_name (encoding));
19f392bc
UW
15322 type = init_type (objfile, TYPE_CODE_ERROR,
15323 bits / TARGET_CHAR_BIT, name);
6ccb9162 15324 break;
c906108c 15325 }
6ccb9162 15326
0114d602 15327 if (name && strcmp (name, "char") == 0)
876cecd0 15328 TYPE_NOSIGN (type) = 1;
0114d602 15329
f792889a 15330 return set_die_type (die, type, cu);
c906108c
SS
15331}
15332
80180f79
SA
15333/* Parse dwarf attribute if it's a block, reference or constant and put the
15334 resulting value of the attribute into struct bound_prop.
15335 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15336
15337static int
15338attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15339 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15340{
15341 struct dwarf2_property_baton *baton;
15342 struct obstack *obstack = &cu->objfile->objfile_obstack;
15343
15344 if (attr == NULL || prop == NULL)
15345 return 0;
15346
15347 if (attr_form_is_block (attr))
15348 {
8d749320 15349 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15350 baton->referenced_type = NULL;
15351 baton->locexpr.per_cu = cu->per_cu;
15352 baton->locexpr.size = DW_BLOCK (attr)->size;
15353 baton->locexpr.data = DW_BLOCK (attr)->data;
15354 prop->data.baton = baton;
15355 prop->kind = PROP_LOCEXPR;
15356 gdb_assert (prop->data.baton != NULL);
15357 }
15358 else if (attr_form_is_ref (attr))
15359 {
15360 struct dwarf2_cu *target_cu = cu;
15361 struct die_info *target_die;
15362 struct attribute *target_attr;
15363
15364 target_die = follow_die_ref (die, attr, &target_cu);
15365 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15366 if (target_attr == NULL)
15367 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15368 target_cu);
80180f79
SA
15369 if (target_attr == NULL)
15370 return 0;
15371
df25ebbd 15372 switch (target_attr->name)
80180f79 15373 {
df25ebbd
JB
15374 case DW_AT_location:
15375 if (attr_form_is_section_offset (target_attr))
15376 {
8d749320 15377 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15378 baton->referenced_type = die_type (target_die, target_cu);
15379 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15380 prop->data.baton = baton;
15381 prop->kind = PROP_LOCLIST;
15382 gdb_assert (prop->data.baton != NULL);
15383 }
15384 else if (attr_form_is_block (target_attr))
15385 {
8d749320 15386 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15387 baton->referenced_type = die_type (target_die, target_cu);
15388 baton->locexpr.per_cu = cu->per_cu;
15389 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15390 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15391 prop->data.baton = baton;
15392 prop->kind = PROP_LOCEXPR;
15393 gdb_assert (prop->data.baton != NULL);
15394 }
15395 else
15396 {
15397 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15398 "dynamic property");
15399 return 0;
15400 }
15401 break;
15402 case DW_AT_data_member_location:
15403 {
15404 LONGEST offset;
15405
15406 if (!handle_data_member_location (target_die, target_cu,
15407 &offset))
15408 return 0;
15409
8d749320 15410 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15411 baton->referenced_type = read_type_die (target_die->parent,
15412 target_cu);
df25ebbd
JB
15413 baton->offset_info.offset = offset;
15414 baton->offset_info.type = die_type (target_die, target_cu);
15415 prop->data.baton = baton;
15416 prop->kind = PROP_ADDR_OFFSET;
15417 break;
15418 }
80180f79
SA
15419 }
15420 }
15421 else if (attr_form_is_constant (attr))
15422 {
15423 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15424 prop->kind = PROP_CONST;
15425 }
15426 else
15427 {
15428 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15429 dwarf2_name (die, cu));
15430 return 0;
15431 }
15432
15433 return 1;
15434}
15435
a02abb62
JB
15436/* Read the given DW_AT_subrange DIE. */
15437
f792889a 15438static struct type *
a02abb62
JB
15439read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15440{
4c9ad8c2 15441 struct type *base_type, *orig_base_type;
a02abb62
JB
15442 struct type *range_type;
15443 struct attribute *attr;
729efb13 15444 struct dynamic_prop low, high;
4fae6e18 15445 int low_default_is_valid;
c451ebe5 15446 int high_bound_is_count = 0;
15d034d0 15447 const char *name;
43bbcdc2 15448 LONGEST negative_mask;
e77813c8 15449
4c9ad8c2
TT
15450 orig_base_type = die_type (die, cu);
15451 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15452 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15453 creating the range type, but we use the result of check_typedef
15454 when examining properties of the type. */
15455 base_type = check_typedef (orig_base_type);
a02abb62 15456
7e314c57
JK
15457 /* The die_type call above may have already set the type for this DIE. */
15458 range_type = get_die_type (die, cu);
15459 if (range_type)
15460 return range_type;
15461
729efb13
SA
15462 low.kind = PROP_CONST;
15463 high.kind = PROP_CONST;
15464 high.data.const_val = 0;
15465
4fae6e18
JK
15466 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15467 omitting DW_AT_lower_bound. */
15468 switch (cu->language)
6e70227d 15469 {
4fae6e18
JK
15470 case language_c:
15471 case language_cplus:
729efb13 15472 low.data.const_val = 0;
4fae6e18
JK
15473 low_default_is_valid = 1;
15474 break;
15475 case language_fortran:
729efb13 15476 low.data.const_val = 1;
4fae6e18
JK
15477 low_default_is_valid = 1;
15478 break;
15479 case language_d:
4fae6e18 15480 case language_objc:
c44af4eb 15481 case language_rust:
729efb13 15482 low.data.const_val = 0;
4fae6e18
JK
15483 low_default_is_valid = (cu->header.version >= 4);
15484 break;
15485 case language_ada:
15486 case language_m2:
15487 case language_pascal:
729efb13 15488 low.data.const_val = 1;
4fae6e18
JK
15489 low_default_is_valid = (cu->header.version >= 4);
15490 break;
15491 default:
729efb13 15492 low.data.const_val = 0;
4fae6e18
JK
15493 low_default_is_valid = 0;
15494 break;
a02abb62
JB
15495 }
15496
e142c38c 15497 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15498 if (attr)
11c1ba78 15499 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15500 else if (!low_default_is_valid)
15501 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15502 "- DIE at 0x%x [in module %s]"),
9c541725 15503 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15504
e142c38c 15505 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15506 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15507 {
15508 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15509 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15510 {
c451ebe5
SA
15511 /* If bounds are constant do the final calculation here. */
15512 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15513 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15514 else
15515 high_bound_is_count = 1;
c2ff108b 15516 }
e77813c8
PM
15517 }
15518
15519 /* Dwarf-2 specifications explicitly allows to create subrange types
15520 without specifying a base type.
15521 In that case, the base type must be set to the type of
15522 the lower bound, upper bound or count, in that order, if any of these
15523 three attributes references an object that has a type.
15524 If no base type is found, the Dwarf-2 specifications say that
15525 a signed integer type of size equal to the size of an address should
15526 be used.
15527 For the following C code: `extern char gdb_int [];'
15528 GCC produces an empty range DIE.
15529 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15530 high bound or count are not yet handled by this code. */
e77813c8
PM
15531 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15532 {
15533 struct objfile *objfile = cu->objfile;
15534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15535 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15536 struct type *int_type = objfile_type (objfile)->builtin_int;
15537
15538 /* Test "int", "long int", and "long long int" objfile types,
15539 and select the first one having a size above or equal to the
15540 architecture address size. */
15541 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15542 base_type = int_type;
15543 else
15544 {
15545 int_type = objfile_type (objfile)->builtin_long;
15546 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15547 base_type = int_type;
15548 else
15549 {
15550 int_type = objfile_type (objfile)->builtin_long_long;
15551 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15552 base_type = int_type;
15553 }
15554 }
15555 }
a02abb62 15556
dbb9c2b1
JB
15557 /* Normally, the DWARF producers are expected to use a signed
15558 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15559 But this is unfortunately not always the case, as witnessed
15560 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15561 is used instead. To work around that ambiguity, we treat
15562 the bounds as signed, and thus sign-extend their values, when
15563 the base type is signed. */
6e70227d 15564 negative_mask =
66c6502d 15565 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15566 if (low.kind == PROP_CONST
15567 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15568 low.data.const_val |= negative_mask;
15569 if (high.kind == PROP_CONST
15570 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15571 high.data.const_val |= negative_mask;
43bbcdc2 15572
729efb13 15573 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15574
c451ebe5
SA
15575 if (high_bound_is_count)
15576 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15577
c2ff108b
JK
15578 /* Ada expects an empty array on no boundary attributes. */
15579 if (attr == NULL && cu->language != language_ada)
729efb13 15580 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15581
39cbfefa
DJ
15582 name = dwarf2_name (die, cu);
15583 if (name)
15584 TYPE_NAME (range_type) = name;
6e70227d 15585
e142c38c 15586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15587 if (attr)
15588 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15589
7e314c57
JK
15590 set_die_type (die, range_type, cu);
15591
15592 /* set_die_type should be already done. */
b4ba55a1
JB
15593 set_descriptive_type (range_type, die, cu);
15594
7e314c57 15595 return range_type;
a02abb62 15596}
6e70227d 15597
f792889a 15598static struct type *
81a17f79
JB
15599read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15600{
15601 struct type *type;
81a17f79 15602
81a17f79
JB
15603 /* For now, we only support the C meaning of an unspecified type: void. */
15604
19f392bc 15605 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15606 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15607
f792889a 15608 return set_die_type (die, type, cu);
81a17f79 15609}
a02abb62 15610
639d11d3
DC
15611/* Read a single die and all its descendents. Set the die's sibling
15612 field to NULL; set other fields in the die correctly, and set all
15613 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15614 location of the info_ptr after reading all of those dies. PARENT
15615 is the parent of the die in question. */
15616
15617static struct die_info *
dee91e82 15618read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15619 const gdb_byte *info_ptr,
15620 const gdb_byte **new_info_ptr,
dee91e82 15621 struct die_info *parent)
639d11d3
DC
15622{
15623 struct die_info *die;
d521ce57 15624 const gdb_byte *cur_ptr;
639d11d3
DC
15625 int has_children;
15626
bf6af496 15627 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15628 if (die == NULL)
15629 {
15630 *new_info_ptr = cur_ptr;
15631 return NULL;
15632 }
93311388 15633 store_in_ref_table (die, reader->cu);
639d11d3
DC
15634
15635 if (has_children)
bf6af496 15636 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15637 else
15638 {
15639 die->child = NULL;
15640 *new_info_ptr = cur_ptr;
15641 }
15642
15643 die->sibling = NULL;
15644 die->parent = parent;
15645 return die;
15646}
15647
15648/* Read a die, all of its descendents, and all of its siblings; set
15649 all of the fields of all of the dies correctly. Arguments are as
15650 in read_die_and_children. */
15651
15652static struct die_info *
bf6af496 15653read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15654 const gdb_byte *info_ptr,
15655 const gdb_byte **new_info_ptr,
bf6af496 15656 struct die_info *parent)
639d11d3
DC
15657{
15658 struct die_info *first_die, *last_sibling;
d521ce57 15659 const gdb_byte *cur_ptr;
639d11d3 15660
c906108c 15661 cur_ptr = info_ptr;
639d11d3
DC
15662 first_die = last_sibling = NULL;
15663
15664 while (1)
c906108c 15665 {
639d11d3 15666 struct die_info *die
dee91e82 15667 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15668
1d325ec1 15669 if (die == NULL)
c906108c 15670 {
639d11d3
DC
15671 *new_info_ptr = cur_ptr;
15672 return first_die;
c906108c 15673 }
1d325ec1
DJ
15674
15675 if (!first_die)
15676 first_die = die;
c906108c 15677 else
1d325ec1
DJ
15678 last_sibling->sibling = die;
15679
15680 last_sibling = die;
c906108c 15681 }
c906108c
SS
15682}
15683
bf6af496
DE
15684/* Read a die, all of its descendents, and all of its siblings; set
15685 all of the fields of all of the dies correctly. Arguments are as
15686 in read_die_and_children.
15687 This the main entry point for reading a DIE and all its children. */
15688
15689static struct die_info *
15690read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15691 const gdb_byte *info_ptr,
15692 const gdb_byte **new_info_ptr,
bf6af496
DE
15693 struct die_info *parent)
15694{
15695 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15696 new_info_ptr, parent);
15697
b4f54984 15698 if (dwarf_die_debug)
bf6af496
DE
15699 {
15700 fprintf_unfiltered (gdb_stdlog,
15701 "Read die from %s@0x%x of %s:\n",
a32a8923 15702 get_section_name (reader->die_section),
bf6af496
DE
15703 (unsigned) (info_ptr - reader->die_section->buffer),
15704 bfd_get_filename (reader->abfd));
b4f54984 15705 dump_die (die, dwarf_die_debug);
bf6af496
DE
15706 }
15707
15708 return die;
15709}
15710
3019eac3
DE
15711/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15712 attributes.
15713 The caller is responsible for filling in the extra attributes
15714 and updating (*DIEP)->num_attrs.
15715 Set DIEP to point to a newly allocated die with its information,
15716 except for its child, sibling, and parent fields.
15717 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15718
d521ce57 15719static const gdb_byte *
3019eac3 15720read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15721 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15722 int *has_children, int num_extra_attrs)
93311388 15723{
b64f50a1 15724 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15725 struct abbrev_info *abbrev;
15726 struct die_info *die;
15727 struct dwarf2_cu *cu = reader->cu;
15728 bfd *abfd = reader->abfd;
15729
9c541725 15730 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15731 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15732 info_ptr += bytes_read;
15733 if (!abbrev_number)
15734 {
15735 *diep = NULL;
15736 *has_children = 0;
15737 return info_ptr;
15738 }
15739
433df2d4 15740 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15741 if (!abbrev)
348e048f
DE
15742 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15743 abbrev_number,
15744 bfd_get_filename (abfd));
15745
3019eac3 15746 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15747 die->sect_off = sect_off;
93311388
DE
15748 die->tag = abbrev->tag;
15749 die->abbrev = abbrev_number;
15750
3019eac3
DE
15751 /* Make the result usable.
15752 The caller needs to update num_attrs after adding the extra
15753 attributes. */
93311388
DE
15754 die->num_attrs = abbrev->num_attrs;
15755
15756 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15757 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15758 info_ptr);
93311388
DE
15759
15760 *diep = die;
15761 *has_children = abbrev->has_children;
15762 return info_ptr;
15763}
15764
3019eac3
DE
15765/* Read a die and all its attributes.
15766 Set DIEP to point to a newly allocated die with its information,
15767 except for its child, sibling, and parent fields.
15768 Set HAS_CHILDREN to tell whether the die has children or not. */
15769
d521ce57 15770static const gdb_byte *
3019eac3 15771read_full_die (const struct die_reader_specs *reader,
d521ce57 15772 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15773 int *has_children)
15774{
d521ce57 15775 const gdb_byte *result;
bf6af496
DE
15776
15777 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15778
b4f54984 15779 if (dwarf_die_debug)
bf6af496
DE
15780 {
15781 fprintf_unfiltered (gdb_stdlog,
15782 "Read die from %s@0x%x of %s:\n",
a32a8923 15783 get_section_name (reader->die_section),
bf6af496
DE
15784 (unsigned) (info_ptr - reader->die_section->buffer),
15785 bfd_get_filename (reader->abfd));
b4f54984 15786 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15787 }
15788
15789 return result;
3019eac3 15790}
433df2d4
DE
15791\f
15792/* Abbreviation tables.
3019eac3 15793
433df2d4 15794 In DWARF version 2, the description of the debugging information is
c906108c
SS
15795 stored in a separate .debug_abbrev section. Before we read any
15796 dies from a section we read in all abbreviations and install them
433df2d4
DE
15797 in a hash table. */
15798
15799/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15800
15801static struct abbrev_info *
15802abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15803{
15804 struct abbrev_info *abbrev;
15805
8d749320 15806 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15807 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15808
433df2d4
DE
15809 return abbrev;
15810}
15811
15812/* Add an abbreviation to the table. */
c906108c
SS
15813
15814static void
433df2d4
DE
15815abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15816 unsigned int abbrev_number,
15817 struct abbrev_info *abbrev)
15818{
15819 unsigned int hash_number;
15820
15821 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15822 abbrev->next = abbrev_table->abbrevs[hash_number];
15823 abbrev_table->abbrevs[hash_number] = abbrev;
15824}
dee91e82 15825
433df2d4
DE
15826/* Look up an abbrev in the table.
15827 Returns NULL if the abbrev is not found. */
15828
15829static struct abbrev_info *
15830abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15831 unsigned int abbrev_number)
c906108c 15832{
433df2d4
DE
15833 unsigned int hash_number;
15834 struct abbrev_info *abbrev;
15835
15836 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15837 abbrev = abbrev_table->abbrevs[hash_number];
15838
15839 while (abbrev)
15840 {
15841 if (abbrev->number == abbrev_number)
15842 return abbrev;
15843 abbrev = abbrev->next;
15844 }
15845 return NULL;
15846}
15847
15848/* Read in an abbrev table. */
15849
15850static struct abbrev_table *
15851abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15852 sect_offset sect_off)
433df2d4
DE
15853{
15854 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15855 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15856 struct abbrev_table *abbrev_table;
d521ce57 15857 const gdb_byte *abbrev_ptr;
c906108c
SS
15858 struct abbrev_info *cur_abbrev;
15859 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15860 unsigned int abbrev_form;
f3dd6933
DJ
15861 struct attr_abbrev *cur_attrs;
15862 unsigned int allocated_attrs;
c906108c 15863
70ba0933 15864 abbrev_table = XNEW (struct abbrev_table);
9c541725 15865 abbrev_table->sect_off = sect_off;
433df2d4 15866 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15867 abbrev_table->abbrevs =
15868 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15869 ABBREV_HASH_SIZE);
433df2d4
DE
15870 memset (abbrev_table->abbrevs, 0,
15871 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15872
433df2d4 15873 dwarf2_read_section (objfile, section);
9c541725 15874 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15875 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15876 abbrev_ptr += bytes_read;
15877
f3dd6933 15878 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15879 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15880
0963b4bd 15881 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15882 while (abbrev_number)
15883 {
433df2d4 15884 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15885
15886 /* read in abbrev header */
15887 cur_abbrev->number = abbrev_number;
aead7601
SM
15888 cur_abbrev->tag
15889 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15890 abbrev_ptr += bytes_read;
15891 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15892 abbrev_ptr += 1;
15893
15894 /* now read in declarations */
22d2f3ab 15895 for (;;)
c906108c 15896 {
43988095
JK
15897 LONGEST implicit_const;
15898
22d2f3ab
JK
15899 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15900 abbrev_ptr += bytes_read;
15901 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15902 abbrev_ptr += bytes_read;
43988095
JK
15903 if (abbrev_form == DW_FORM_implicit_const)
15904 {
15905 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15906 &bytes_read);
15907 abbrev_ptr += bytes_read;
15908 }
15909 else
15910 {
15911 /* Initialize it due to a false compiler warning. */
15912 implicit_const = -1;
15913 }
22d2f3ab
JK
15914
15915 if (abbrev_name == 0)
15916 break;
15917
f3dd6933 15918 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15919 {
f3dd6933
DJ
15920 allocated_attrs += ATTR_ALLOC_CHUNK;
15921 cur_attrs
224c3ddb 15922 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15923 }
ae038cb0 15924
aead7601
SM
15925 cur_attrs[cur_abbrev->num_attrs].name
15926 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15927 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15928 = (enum dwarf_form) abbrev_form;
43988095 15929 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15930 ++cur_abbrev->num_attrs;
c906108c
SS
15931 }
15932
8d749320
SM
15933 cur_abbrev->attrs =
15934 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15935 cur_abbrev->num_attrs);
f3dd6933
DJ
15936 memcpy (cur_abbrev->attrs, cur_attrs,
15937 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15938
433df2d4 15939 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15940
15941 /* Get next abbreviation.
15942 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15943 always properly terminated with an abbrev number of 0.
15944 Exit loop if we encounter an abbreviation which we have
15945 already read (which means we are about to read the abbreviations
15946 for the next compile unit) or if the end of the abbreviation
15947 table is reached. */
433df2d4 15948 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15949 break;
15950 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15951 abbrev_ptr += bytes_read;
433df2d4 15952 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15953 break;
15954 }
f3dd6933
DJ
15955
15956 xfree (cur_attrs);
433df2d4 15957 return abbrev_table;
c906108c
SS
15958}
15959
433df2d4 15960/* Free the resources held by ABBREV_TABLE. */
c906108c 15961
c906108c 15962static void
433df2d4 15963abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15964{
433df2d4
DE
15965 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15966 xfree (abbrev_table);
c906108c
SS
15967}
15968
f4dc4d17
DE
15969/* Same as abbrev_table_free but as a cleanup.
15970 We pass in a pointer to the pointer to the table so that we can
15971 set the pointer to NULL when we're done. It also simplifies
73051182 15972 build_type_psymtabs_1. */
f4dc4d17
DE
15973
15974static void
15975abbrev_table_free_cleanup (void *table_ptr)
15976{
9a3c8263 15977 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15978
15979 if (*abbrev_table_ptr != NULL)
15980 abbrev_table_free (*abbrev_table_ptr);
15981 *abbrev_table_ptr = NULL;
15982}
15983
433df2d4
DE
15984/* Read the abbrev table for CU from ABBREV_SECTION. */
15985
15986static void
15987dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15988 struct dwarf2_section_info *abbrev_section)
c906108c 15989{
433df2d4 15990 cu->abbrev_table =
9c541725 15991 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15992}
c906108c 15993
433df2d4 15994/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15995
433df2d4
DE
15996static void
15997dwarf2_free_abbrev_table (void *ptr_to_cu)
15998{
9a3c8263 15999 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16000
a2ce51a0
DE
16001 if (cu->abbrev_table != NULL)
16002 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16003 /* Set this to NULL so that we SEGV if we try to read it later,
16004 and also because free_comp_unit verifies this is NULL. */
16005 cu->abbrev_table = NULL;
16006}
16007\f
72bf9492
DJ
16008/* Returns nonzero if TAG represents a type that we might generate a partial
16009 symbol for. */
16010
16011static int
16012is_type_tag_for_partial (int tag)
16013{
16014 switch (tag)
16015 {
16016#if 0
16017 /* Some types that would be reasonable to generate partial symbols for,
16018 that we don't at present. */
16019 case DW_TAG_array_type:
16020 case DW_TAG_file_type:
16021 case DW_TAG_ptr_to_member_type:
16022 case DW_TAG_set_type:
16023 case DW_TAG_string_type:
16024 case DW_TAG_subroutine_type:
16025#endif
16026 case DW_TAG_base_type:
16027 case DW_TAG_class_type:
680b30c7 16028 case DW_TAG_interface_type:
72bf9492
DJ
16029 case DW_TAG_enumeration_type:
16030 case DW_TAG_structure_type:
16031 case DW_TAG_subrange_type:
16032 case DW_TAG_typedef:
16033 case DW_TAG_union_type:
16034 return 1;
16035 default:
16036 return 0;
16037 }
16038}
16039
16040/* Load all DIEs that are interesting for partial symbols into memory. */
16041
16042static struct partial_die_info *
dee91e82 16043load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16044 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16045{
dee91e82 16046 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16047 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16048 struct partial_die_info *part_die;
16049 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16050 struct abbrev_info *abbrev;
16051 unsigned int bytes_read;
5afb4e99 16052 unsigned int load_all = 0;
72bf9492
DJ
16053 int nesting_level = 1;
16054
16055 parent_die = NULL;
16056 last_die = NULL;
16057
7adf1e79
DE
16058 gdb_assert (cu->per_cu != NULL);
16059 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16060 load_all = 1;
16061
72bf9492
DJ
16062 cu->partial_dies
16063 = htab_create_alloc_ex (cu->header.length / 12,
16064 partial_die_hash,
16065 partial_die_eq,
16066 NULL,
16067 &cu->comp_unit_obstack,
16068 hashtab_obstack_allocate,
16069 dummy_obstack_deallocate);
16070
8d749320 16071 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16072
16073 while (1)
16074 {
16075 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16076
16077 /* A NULL abbrev means the end of a series of children. */
16078 if (abbrev == NULL)
16079 {
16080 if (--nesting_level == 0)
16081 {
16082 /* PART_DIE was probably the last thing allocated on the
16083 comp_unit_obstack, so we could call obstack_free
16084 here. We don't do that because the waste is small,
16085 and will be cleaned up when we're done with this
16086 compilation unit. This way, we're also more robust
16087 against other users of the comp_unit_obstack. */
16088 return first_die;
16089 }
16090 info_ptr += bytes_read;
16091 last_die = parent_die;
16092 parent_die = parent_die->die_parent;
16093 continue;
16094 }
16095
98bfdba5
PA
16096 /* Check for template arguments. We never save these; if
16097 they're seen, we just mark the parent, and go on our way. */
16098 if (parent_die != NULL
16099 && cu->language == language_cplus
16100 && (abbrev->tag == DW_TAG_template_type_param
16101 || abbrev->tag == DW_TAG_template_value_param))
16102 {
16103 parent_die->has_template_arguments = 1;
16104
16105 if (!load_all)
16106 {
16107 /* We don't need a partial DIE for the template argument. */
dee91e82 16108 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16109 continue;
16110 }
16111 }
16112
0d99eb77 16113 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16114 Skip their other children. */
16115 if (!load_all
16116 && cu->language == language_cplus
16117 && parent_die != NULL
16118 && parent_die->tag == DW_TAG_subprogram)
16119 {
dee91e82 16120 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16121 continue;
16122 }
16123
5afb4e99
DJ
16124 /* Check whether this DIE is interesting enough to save. Normally
16125 we would not be interested in members here, but there may be
16126 later variables referencing them via DW_AT_specification (for
16127 static members). */
16128 if (!load_all
16129 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16130 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16131 && abbrev->tag != DW_TAG_enumerator
16132 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16133 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16134 && abbrev->tag != DW_TAG_variable
5afb4e99 16135 && abbrev->tag != DW_TAG_namespace
f55ee35c 16136 && abbrev->tag != DW_TAG_module
95554aad 16137 && abbrev->tag != DW_TAG_member
74921315
KS
16138 && abbrev->tag != DW_TAG_imported_unit
16139 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16140 {
16141 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16142 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16143 continue;
16144 }
16145
dee91e82
DE
16146 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16147 info_ptr);
72bf9492
DJ
16148
16149 /* This two-pass algorithm for processing partial symbols has a
16150 high cost in cache pressure. Thus, handle some simple cases
16151 here which cover the majority of C partial symbols. DIEs
16152 which neither have specification tags in them, nor could have
16153 specification tags elsewhere pointing at them, can simply be
16154 processed and discarded.
16155
16156 This segment is also optional; scan_partial_symbols and
16157 add_partial_symbol will handle these DIEs if we chain
16158 them in normally. When compilers which do not emit large
16159 quantities of duplicate debug information are more common,
16160 this code can probably be removed. */
16161
16162 /* Any complete simple types at the top level (pretty much all
16163 of them, for a language without namespaces), can be processed
16164 directly. */
16165 if (parent_die == NULL
16166 && part_die->has_specification == 0
16167 && part_die->is_declaration == 0
d8228535 16168 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16169 || part_die->tag == DW_TAG_base_type
16170 || part_die->tag == DW_TAG_subrange_type))
16171 {
16172 if (building_psymtab && part_die->name != NULL)
04a679b8 16173 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16174 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16175 &objfile->static_psymbols,
1762568f 16176 0, cu->language, objfile);
dee91e82 16177 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16178 continue;
16179 }
16180
d8228535
JK
16181 /* The exception for DW_TAG_typedef with has_children above is
16182 a workaround of GCC PR debug/47510. In the case of this complaint
16183 type_name_no_tag_or_error will error on such types later.
16184
16185 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16186 it could not find the child DIEs referenced later, this is checked
16187 above. In correct DWARF DW_TAG_typedef should have no children. */
16188
16189 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16190 complaint (&symfile_complaints,
16191 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16192 "- DIE at 0x%x [in module %s]"),
9c541725 16193 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16194
72bf9492
DJ
16195 /* If we're at the second level, and we're an enumerator, and
16196 our parent has no specification (meaning possibly lives in a
16197 namespace elsewhere), then we can add the partial symbol now
16198 instead of queueing it. */
16199 if (part_die->tag == DW_TAG_enumerator
16200 && parent_die != NULL
16201 && parent_die->die_parent == NULL
16202 && parent_die->tag == DW_TAG_enumeration_type
16203 && parent_die->has_specification == 0)
16204 {
16205 if (part_die->name == NULL)
3e43a32a
MS
16206 complaint (&symfile_complaints,
16207 _("malformed enumerator DIE ignored"));
72bf9492 16208 else if (building_psymtab)
04a679b8 16209 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16210 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16211 cu->language == language_cplus
bb5ed363
DE
16212 ? &objfile->global_psymbols
16213 : &objfile->static_psymbols,
1762568f 16214 0, cu->language, objfile);
72bf9492 16215
dee91e82 16216 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16217 continue;
16218 }
16219
16220 /* We'll save this DIE so link it in. */
16221 part_die->die_parent = parent_die;
16222 part_die->die_sibling = NULL;
16223 part_die->die_child = NULL;
16224
16225 if (last_die && last_die == parent_die)
16226 last_die->die_child = part_die;
16227 else if (last_die)
16228 last_die->die_sibling = part_die;
16229
16230 last_die = part_die;
16231
16232 if (first_die == NULL)
16233 first_die = part_die;
16234
16235 /* Maybe add the DIE to the hash table. Not all DIEs that we
16236 find interesting need to be in the hash table, because we
16237 also have the parent/sibling/child chains; only those that we
16238 might refer to by offset later during partial symbol reading.
16239
16240 For now this means things that might have be the target of a
16241 DW_AT_specification, DW_AT_abstract_origin, or
16242 DW_AT_extension. DW_AT_extension will refer only to
16243 namespaces; DW_AT_abstract_origin refers to functions (and
16244 many things under the function DIE, but we do not recurse
16245 into function DIEs during partial symbol reading) and
16246 possibly variables as well; DW_AT_specification refers to
16247 declarations. Declarations ought to have the DW_AT_declaration
16248 flag. It happens that GCC forgets to put it in sometimes, but
16249 only for functions, not for types.
16250
16251 Adding more things than necessary to the hash table is harmless
16252 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16253 wasted time in find_partial_die, when we reread the compilation
16254 unit with load_all_dies set. */
72bf9492 16255
5afb4e99 16256 if (load_all
72929c62 16257 || abbrev->tag == DW_TAG_constant
5afb4e99 16258 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16259 || abbrev->tag == DW_TAG_variable
16260 || abbrev->tag == DW_TAG_namespace
16261 || part_die->is_declaration)
16262 {
16263 void **slot;
16264
16265 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16266 to_underlying (part_die->sect_off),
16267 INSERT);
72bf9492
DJ
16268 *slot = part_die;
16269 }
16270
8d749320 16271 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16272
16273 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16274 we have no reason to follow the children of structures; for other
98bfdba5
PA
16275 languages we have to, so that we can get at method physnames
16276 to infer fully qualified class names, for DW_AT_specification,
16277 and for C++ template arguments. For C++, we also look one level
16278 inside functions to find template arguments (if the name of the
16279 function does not already contain the template arguments).
bc30ff58
JB
16280
16281 For Ada, we need to scan the children of subprograms and lexical
16282 blocks as well because Ada allows the definition of nested
16283 entities that could be interesting for the debugger, such as
16284 nested subprograms for instance. */
72bf9492 16285 if (last_die->has_children
5afb4e99
DJ
16286 && (load_all
16287 || last_die->tag == DW_TAG_namespace
f55ee35c 16288 || last_die->tag == DW_TAG_module
72bf9492 16289 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16290 || (cu->language == language_cplus
16291 && last_die->tag == DW_TAG_subprogram
16292 && (last_die->name == NULL
16293 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16294 || (cu->language != language_c
16295 && (last_die->tag == DW_TAG_class_type
680b30c7 16296 || last_die->tag == DW_TAG_interface_type
72bf9492 16297 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16298 || last_die->tag == DW_TAG_union_type))
16299 || (cu->language == language_ada
16300 && (last_die->tag == DW_TAG_subprogram
16301 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16302 {
16303 nesting_level++;
16304 parent_die = last_die;
16305 continue;
16306 }
16307
16308 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16309 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16310
16311 /* Back to the top, do it again. */
16312 }
16313}
16314
c906108c
SS
16315/* Read a minimal amount of information into the minimal die structure. */
16316
d521ce57 16317static const gdb_byte *
dee91e82
DE
16318read_partial_die (const struct die_reader_specs *reader,
16319 struct partial_die_info *part_die,
16320 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16321 const gdb_byte *info_ptr)
c906108c 16322{
dee91e82 16323 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16324 struct objfile *objfile = cu->objfile;
d521ce57 16325 const gdb_byte *buffer = reader->buffer;
fa238c03 16326 unsigned int i;
c906108c 16327 struct attribute attr;
c5aa993b 16328 int has_low_pc_attr = 0;
c906108c 16329 int has_high_pc_attr = 0;
91da1414 16330 int high_pc_relative = 0;
c906108c 16331
72bf9492 16332 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16333
9c541725 16334 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16335
16336 info_ptr += abbrev_len;
16337
16338 if (abbrev == NULL)
16339 return info_ptr;
16340
c906108c
SS
16341 part_die->tag = abbrev->tag;
16342 part_die->has_children = abbrev->has_children;
c906108c
SS
16343
16344 for (i = 0; i < abbrev->num_attrs; ++i)
16345 {
dee91e82 16346 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16347
16348 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16349 partial symbol table. */
c906108c
SS
16350 switch (attr.name)
16351 {
16352 case DW_AT_name:
71c25dea
TT
16353 switch (part_die->tag)
16354 {
16355 case DW_TAG_compile_unit:
95554aad 16356 case DW_TAG_partial_unit:
348e048f 16357 case DW_TAG_type_unit:
71c25dea
TT
16358 /* Compilation units have a DW_AT_name that is a filename, not
16359 a source language identifier. */
16360 case DW_TAG_enumeration_type:
16361 case DW_TAG_enumerator:
16362 /* These tags always have simple identifiers already; no need
16363 to canonicalize them. */
16364 part_die->name = DW_STRING (&attr);
16365 break;
16366 default:
16367 part_die->name
16368 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16369 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16370 break;
16371 }
c906108c 16372 break;
31ef98ae 16373 case DW_AT_linkage_name:
c906108c 16374 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16375 /* Note that both forms of linkage name might appear. We
16376 assume they will be the same, and we only store the last
16377 one we see. */
94af9270
KS
16378 if (cu->language == language_ada)
16379 part_die->name = DW_STRING (&attr);
abc72ce4 16380 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16381 break;
16382 case DW_AT_low_pc:
16383 has_low_pc_attr = 1;
31aa7e4e 16384 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16385 break;
16386 case DW_AT_high_pc:
16387 has_high_pc_attr = 1;
31aa7e4e
JB
16388 part_die->highpc = attr_value_as_address (&attr);
16389 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16390 high_pc_relative = 1;
c906108c
SS
16391 break;
16392 case DW_AT_location:
0963b4bd 16393 /* Support the .debug_loc offsets. */
8e19ed76
PS
16394 if (attr_form_is_block (&attr))
16395 {
95554aad 16396 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16397 }
3690dd37 16398 else if (attr_form_is_section_offset (&attr))
8e19ed76 16399 {
4d3c2250 16400 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16401 }
16402 else
16403 {
4d3c2250
KB
16404 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16405 "partial symbol information");
8e19ed76 16406 }
c906108c 16407 break;
c906108c
SS
16408 case DW_AT_external:
16409 part_die->is_external = DW_UNSND (&attr);
16410 break;
16411 case DW_AT_declaration:
16412 part_die->is_declaration = DW_UNSND (&attr);
16413 break;
16414 case DW_AT_type:
16415 part_die->has_type = 1;
16416 break;
16417 case DW_AT_abstract_origin:
16418 case DW_AT_specification:
72bf9492
DJ
16419 case DW_AT_extension:
16420 part_die->has_specification = 1;
c764a876 16421 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16422 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16423 || cu->per_cu->is_dwz);
c906108c
SS
16424 break;
16425 case DW_AT_sibling:
16426 /* Ignore absolute siblings, they might point outside of
16427 the current compile unit. */
16428 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16429 complaint (&symfile_complaints,
16430 _("ignoring absolute DW_AT_sibling"));
c906108c 16431 else
b9502d3f 16432 {
9c541725
PA
16433 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16434 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16435
16436 if (sibling_ptr < info_ptr)
16437 complaint (&symfile_complaints,
16438 _("DW_AT_sibling points backwards"));
22869d73
KS
16439 else if (sibling_ptr > reader->buffer_end)
16440 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16441 else
16442 part_die->sibling = sibling_ptr;
16443 }
c906108c 16444 break;
fa4028e9
JB
16445 case DW_AT_byte_size:
16446 part_die->has_byte_size = 1;
16447 break;
ff908ebf
AW
16448 case DW_AT_const_value:
16449 part_die->has_const_value = 1;
16450 break;
68511cec
CES
16451 case DW_AT_calling_convention:
16452 /* DWARF doesn't provide a way to identify a program's source-level
16453 entry point. DW_AT_calling_convention attributes are only meant
16454 to describe functions' calling conventions.
16455
16456 However, because it's a necessary piece of information in
0c1b455e
TT
16457 Fortran, and before DWARF 4 DW_CC_program was the only
16458 piece of debugging information whose definition refers to
16459 a 'main program' at all, several compilers marked Fortran
16460 main programs with DW_CC_program --- even when those
16461 functions use the standard calling conventions.
16462
16463 Although DWARF now specifies a way to provide this
16464 information, we support this practice for backward
16465 compatibility. */
68511cec 16466 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16467 && cu->language == language_fortran)
16468 part_die->main_subprogram = 1;
68511cec 16469 break;
481860b3
GB
16470 case DW_AT_inline:
16471 if (DW_UNSND (&attr) == DW_INL_inlined
16472 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16473 part_die->may_be_inlined = 1;
16474 break;
95554aad
TT
16475
16476 case DW_AT_import:
16477 if (part_die->tag == DW_TAG_imported_unit)
36586728 16478 {
9c541725 16479 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16480 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16481 || cu->per_cu->is_dwz);
16482 }
95554aad
TT
16483 break;
16484
0c1b455e
TT
16485 case DW_AT_main_subprogram:
16486 part_die->main_subprogram = DW_UNSND (&attr);
16487 break;
16488
c906108c
SS
16489 default:
16490 break;
16491 }
16492 }
16493
91da1414
MW
16494 if (high_pc_relative)
16495 part_die->highpc += part_die->lowpc;
16496
9373cf26
JK
16497 if (has_low_pc_attr && has_high_pc_attr)
16498 {
16499 /* When using the GNU linker, .gnu.linkonce. sections are used to
16500 eliminate duplicate copies of functions and vtables and such.
16501 The linker will arbitrarily choose one and discard the others.
16502 The AT_*_pc values for such functions refer to local labels in
16503 these sections. If the section from that file was discarded, the
16504 labels are not in the output, so the relocs get a value of 0.
16505 If this is a discarded function, mark the pc bounds as invalid,
16506 so that GDB will ignore it. */
16507 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16508 {
bb5ed363 16509 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16510
16511 complaint (&symfile_complaints,
16512 _("DW_AT_low_pc %s is zero "
16513 "for DIE at 0x%x [in module %s]"),
16514 paddress (gdbarch, part_die->lowpc),
9c541725 16515 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16516 }
16517 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16518 else if (part_die->lowpc >= part_die->highpc)
16519 {
bb5ed363 16520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16521
16522 complaint (&symfile_complaints,
16523 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16524 "for DIE at 0x%x [in module %s]"),
16525 paddress (gdbarch, part_die->lowpc),
16526 paddress (gdbarch, part_die->highpc),
9c541725
PA
16527 to_underlying (part_die->sect_off),
16528 objfile_name (objfile));
9373cf26
JK
16529 }
16530 else
16531 part_die->has_pc_info = 1;
16532 }
85cbf3d3 16533
c906108c
SS
16534 return info_ptr;
16535}
16536
72bf9492
DJ
16537/* Find a cached partial DIE at OFFSET in CU. */
16538
16539static struct partial_die_info *
9c541725 16540find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16541{
16542 struct partial_die_info *lookup_die = NULL;
16543 struct partial_die_info part_die;
16544
9c541725 16545 part_die.sect_off = sect_off;
9a3c8263
SM
16546 lookup_die = ((struct partial_die_info *)
16547 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16548 to_underlying (sect_off)));
72bf9492 16549
72bf9492
DJ
16550 return lookup_die;
16551}
16552
348e048f
DE
16553/* Find a partial DIE at OFFSET, which may or may not be in CU,
16554 except in the case of .debug_types DIEs which do not reference
16555 outside their CU (they do however referencing other types via
55f1336d 16556 DW_FORM_ref_sig8). */
72bf9492
DJ
16557
16558static struct partial_die_info *
9c541725 16559find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16560{
bb5ed363 16561 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16562 struct dwarf2_per_cu_data *per_cu = NULL;
16563 struct partial_die_info *pd = NULL;
72bf9492 16564
36586728 16565 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16566 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16567 {
9c541725 16568 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16569 if (pd != NULL)
16570 return pd;
0d99eb77
DE
16571 /* We missed recording what we needed.
16572 Load all dies and try again. */
16573 per_cu = cu->per_cu;
5afb4e99 16574 }
0d99eb77
DE
16575 else
16576 {
16577 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16578 if (cu->per_cu->is_debug_types)
0d99eb77 16579 {
9c541725
PA
16580 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16581 " external reference to offset 0x%x [in module %s].\n"),
16582 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16583 bfd_get_filename (objfile->obfd));
16584 }
9c541725 16585 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16586 objfile);
72bf9492 16587
0d99eb77
DE
16588 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16589 load_partial_comp_unit (per_cu);
ae038cb0 16590
0d99eb77 16591 per_cu->cu->last_used = 0;
9c541725 16592 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16593 }
5afb4e99 16594
dee91e82
DE
16595 /* If we didn't find it, and not all dies have been loaded,
16596 load them all and try again. */
16597
5afb4e99
DJ
16598 if (pd == NULL && per_cu->load_all_dies == 0)
16599 {
5afb4e99 16600 per_cu->load_all_dies = 1;
fd820528
DE
16601
16602 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16603 THIS_CU->cu may already be in use. So we can't just free it and
16604 replace its DIEs with the ones we read in. Instead, we leave those
16605 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16606 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16607 set. */
dee91e82 16608 load_partial_comp_unit (per_cu);
5afb4e99 16609
9c541725 16610 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16611 }
16612
16613 if (pd == NULL)
16614 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16615 _("could not find partial DIE 0x%x "
16616 "in cache [from module %s]\n"),
9c541725 16617 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16618 return pd;
72bf9492
DJ
16619}
16620
abc72ce4
DE
16621/* See if we can figure out if the class lives in a namespace. We do
16622 this by looking for a member function; its demangled name will
16623 contain namespace info, if there is any. */
16624
16625static void
16626guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16627 struct dwarf2_cu *cu)
16628{
16629 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16630 what template types look like, because the demangler
16631 frequently doesn't give the same name as the debug info. We
16632 could fix this by only using the demangled name to get the
16633 prefix (but see comment in read_structure_type). */
16634
16635 struct partial_die_info *real_pdi;
16636 struct partial_die_info *child_pdi;
16637
16638 /* If this DIE (this DIE's specification, if any) has a parent, then
16639 we should not do this. We'll prepend the parent's fully qualified
16640 name when we create the partial symbol. */
16641
16642 real_pdi = struct_pdi;
16643 while (real_pdi->has_specification)
36586728
TT
16644 real_pdi = find_partial_die (real_pdi->spec_offset,
16645 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16646
16647 if (real_pdi->die_parent != NULL)
16648 return;
16649
16650 for (child_pdi = struct_pdi->die_child;
16651 child_pdi != NULL;
16652 child_pdi = child_pdi->die_sibling)
16653 {
16654 if (child_pdi->tag == DW_TAG_subprogram
16655 && child_pdi->linkage_name != NULL)
16656 {
16657 char *actual_class_name
16658 = language_class_name_from_physname (cu->language_defn,
16659 child_pdi->linkage_name);
16660 if (actual_class_name != NULL)
16661 {
16662 struct_pdi->name
224c3ddb
SM
16663 = ((const char *)
16664 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16665 actual_class_name,
16666 strlen (actual_class_name)));
abc72ce4
DE
16667 xfree (actual_class_name);
16668 }
16669 break;
16670 }
16671 }
16672}
16673
72bf9492
DJ
16674/* Adjust PART_DIE before generating a symbol for it. This function
16675 may set the is_external flag or change the DIE's name. */
16676
16677static void
16678fixup_partial_die (struct partial_die_info *part_die,
16679 struct dwarf2_cu *cu)
16680{
abc72ce4
DE
16681 /* Once we've fixed up a die, there's no point in doing so again.
16682 This also avoids a memory leak if we were to call
16683 guess_partial_die_structure_name multiple times. */
16684 if (part_die->fixup_called)
16685 return;
16686
72bf9492
DJ
16687 /* If we found a reference attribute and the DIE has no name, try
16688 to find a name in the referred to DIE. */
16689
16690 if (part_die->name == NULL && part_die->has_specification)
16691 {
16692 struct partial_die_info *spec_die;
72bf9492 16693
36586728
TT
16694 spec_die = find_partial_die (part_die->spec_offset,
16695 part_die->spec_is_dwz, cu);
72bf9492 16696
10b3939b 16697 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16698
16699 if (spec_die->name)
16700 {
16701 part_die->name = spec_die->name;
16702
16703 /* Copy DW_AT_external attribute if it is set. */
16704 if (spec_die->is_external)
16705 part_die->is_external = spec_die->is_external;
16706 }
16707 }
16708
16709 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16710
16711 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16712 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16713
abc72ce4
DE
16714 /* If there is no parent die to provide a namespace, and there are
16715 children, see if we can determine the namespace from their linkage
122d1940 16716 name. */
abc72ce4 16717 if (cu->language == language_cplus
8b70b953 16718 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16719 && part_die->die_parent == NULL
16720 && part_die->has_children
16721 && (part_die->tag == DW_TAG_class_type
16722 || part_die->tag == DW_TAG_structure_type
16723 || part_die->tag == DW_TAG_union_type))
16724 guess_partial_die_structure_name (part_die, cu);
16725
53832f31
TT
16726 /* GCC might emit a nameless struct or union that has a linkage
16727 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16728 if (part_die->name == NULL
96408a79
SA
16729 && (part_die->tag == DW_TAG_class_type
16730 || part_die->tag == DW_TAG_interface_type
16731 || part_die->tag == DW_TAG_structure_type
16732 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16733 && part_die->linkage_name != NULL)
16734 {
16735 char *demangled;
16736
8de20a37 16737 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16738 if (demangled)
16739 {
96408a79
SA
16740 const char *base;
16741
16742 /* Strip any leading namespaces/classes, keep only the base name.
16743 DW_AT_name for named DIEs does not contain the prefixes. */
16744 base = strrchr (demangled, ':');
16745 if (base && base > demangled && base[-1] == ':')
16746 base++;
16747 else
16748 base = demangled;
16749
34a68019 16750 part_die->name
224c3ddb
SM
16751 = ((const char *)
16752 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16753 base, strlen (base)));
53832f31
TT
16754 xfree (demangled);
16755 }
16756 }
16757
abc72ce4 16758 part_die->fixup_called = 1;
72bf9492
DJ
16759}
16760
a8329558 16761/* Read an attribute value described by an attribute form. */
c906108c 16762
d521ce57 16763static const gdb_byte *
dee91e82
DE
16764read_attribute_value (const struct die_reader_specs *reader,
16765 struct attribute *attr, unsigned form,
43988095 16766 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16767{
dee91e82 16768 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16769 struct objfile *objfile = cu->objfile;
16770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16771 bfd *abfd = reader->abfd;
e7c27a73 16772 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16773 unsigned int bytes_read;
16774 struct dwarf_block *blk;
16775
aead7601 16776 attr->form = (enum dwarf_form) form;
a8329558 16777 switch (form)
c906108c 16778 {
c906108c 16779 case DW_FORM_ref_addr:
ae411497 16780 if (cu->header.version == 2)
4568ecf9 16781 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16782 else
4568ecf9
DE
16783 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16784 &cu->header, &bytes_read);
ae411497
TT
16785 info_ptr += bytes_read;
16786 break;
36586728
TT
16787 case DW_FORM_GNU_ref_alt:
16788 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16789 info_ptr += bytes_read;
16790 break;
ae411497 16791 case DW_FORM_addr:
e7c27a73 16792 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16793 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16794 info_ptr += bytes_read;
c906108c
SS
16795 break;
16796 case DW_FORM_block2:
7b5a2f43 16797 blk = dwarf_alloc_block (cu);
c906108c
SS
16798 blk->size = read_2_bytes (abfd, info_ptr);
16799 info_ptr += 2;
16800 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16801 info_ptr += blk->size;
16802 DW_BLOCK (attr) = blk;
16803 break;
16804 case DW_FORM_block4:
7b5a2f43 16805 blk = dwarf_alloc_block (cu);
c906108c
SS
16806 blk->size = read_4_bytes (abfd, info_ptr);
16807 info_ptr += 4;
16808 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16809 info_ptr += blk->size;
16810 DW_BLOCK (attr) = blk;
16811 break;
16812 case DW_FORM_data2:
16813 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16814 info_ptr += 2;
16815 break;
16816 case DW_FORM_data4:
16817 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16818 info_ptr += 4;
16819 break;
16820 case DW_FORM_data8:
16821 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16822 info_ptr += 8;
16823 break;
0224619f
JK
16824 case DW_FORM_data16:
16825 blk = dwarf_alloc_block (cu);
16826 blk->size = 16;
16827 blk->data = read_n_bytes (abfd, info_ptr, 16);
16828 info_ptr += 16;
16829 DW_BLOCK (attr) = blk;
16830 break;
2dc7f7b3
TT
16831 case DW_FORM_sec_offset:
16832 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16833 info_ptr += bytes_read;
16834 break;
c906108c 16835 case DW_FORM_string:
9b1c24c8 16836 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16837 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16838 info_ptr += bytes_read;
16839 break;
4bdf3d34 16840 case DW_FORM_strp:
36586728
TT
16841 if (!cu->per_cu->is_dwz)
16842 {
16843 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16844 &bytes_read);
16845 DW_STRING_IS_CANONICAL (attr) = 0;
16846 info_ptr += bytes_read;
16847 break;
16848 }
16849 /* FALLTHROUGH */
43988095
JK
16850 case DW_FORM_line_strp:
16851 if (!cu->per_cu->is_dwz)
16852 {
16853 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16854 cu_header, &bytes_read);
16855 DW_STRING_IS_CANONICAL (attr) = 0;
16856 info_ptr += bytes_read;
16857 break;
16858 }
16859 /* FALLTHROUGH */
36586728
TT
16860 case DW_FORM_GNU_strp_alt:
16861 {
16862 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16863 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16864 &bytes_read);
16865
16866 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16867 DW_STRING_IS_CANONICAL (attr) = 0;
16868 info_ptr += bytes_read;
16869 }
4bdf3d34 16870 break;
2dc7f7b3 16871 case DW_FORM_exprloc:
c906108c 16872 case DW_FORM_block:
7b5a2f43 16873 blk = dwarf_alloc_block (cu);
c906108c
SS
16874 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16875 info_ptr += bytes_read;
16876 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16877 info_ptr += blk->size;
16878 DW_BLOCK (attr) = blk;
16879 break;
16880 case DW_FORM_block1:
7b5a2f43 16881 blk = dwarf_alloc_block (cu);
c906108c
SS
16882 blk->size = read_1_byte (abfd, info_ptr);
16883 info_ptr += 1;
16884 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16885 info_ptr += blk->size;
16886 DW_BLOCK (attr) = blk;
16887 break;
16888 case DW_FORM_data1:
16889 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16890 info_ptr += 1;
16891 break;
16892 case DW_FORM_flag:
16893 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16894 info_ptr += 1;
16895 break;
2dc7f7b3
TT
16896 case DW_FORM_flag_present:
16897 DW_UNSND (attr) = 1;
16898 break;
c906108c
SS
16899 case DW_FORM_sdata:
16900 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16901 info_ptr += bytes_read;
16902 break;
16903 case DW_FORM_udata:
16904 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16905 info_ptr += bytes_read;
16906 break;
16907 case DW_FORM_ref1:
9c541725 16908 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16909 + read_1_byte (abfd, info_ptr));
c906108c
SS
16910 info_ptr += 1;
16911 break;
16912 case DW_FORM_ref2:
9c541725 16913 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16914 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16915 info_ptr += 2;
16916 break;
16917 case DW_FORM_ref4:
9c541725 16918 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16919 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16920 info_ptr += 4;
16921 break;
613e1657 16922 case DW_FORM_ref8:
9c541725 16923 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16924 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16925 info_ptr += 8;
16926 break;
55f1336d 16927 case DW_FORM_ref_sig8:
ac9ec31b 16928 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16929 info_ptr += 8;
16930 break;
c906108c 16931 case DW_FORM_ref_udata:
9c541725 16932 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16933 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16934 info_ptr += bytes_read;
16935 break;
c906108c 16936 case DW_FORM_indirect:
a8329558
KW
16937 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16938 info_ptr += bytes_read;
43988095
JK
16939 if (form == DW_FORM_implicit_const)
16940 {
16941 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16942 info_ptr += bytes_read;
16943 }
16944 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16945 info_ptr);
16946 break;
16947 case DW_FORM_implicit_const:
16948 DW_SND (attr) = implicit_const;
a8329558 16949 break;
3019eac3
DE
16950 case DW_FORM_GNU_addr_index:
16951 if (reader->dwo_file == NULL)
16952 {
16953 /* For now flag a hard error.
16954 Later we can turn this into a complaint. */
16955 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16956 dwarf_form_name (form),
16957 bfd_get_filename (abfd));
16958 }
16959 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16960 info_ptr += bytes_read;
16961 break;
16962 case DW_FORM_GNU_str_index:
16963 if (reader->dwo_file == NULL)
16964 {
16965 /* For now flag a hard error.
16966 Later we can turn this into a complaint if warranted. */
16967 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16968 dwarf_form_name (form),
16969 bfd_get_filename (abfd));
16970 }
16971 {
16972 ULONGEST str_index =
16973 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16974
342587c4 16975 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16976 DW_STRING_IS_CANONICAL (attr) = 0;
16977 info_ptr += bytes_read;
16978 }
16979 break;
c906108c 16980 default:
8a3fe4f8 16981 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16982 dwarf_form_name (form),
16983 bfd_get_filename (abfd));
c906108c 16984 }
28e94949 16985
36586728 16986 /* Super hack. */
7771576e 16987 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16988 attr->form = DW_FORM_GNU_ref_alt;
16989
28e94949
JB
16990 /* We have seen instances where the compiler tried to emit a byte
16991 size attribute of -1 which ended up being encoded as an unsigned
16992 0xffffffff. Although 0xffffffff is technically a valid size value,
16993 an object of this size seems pretty unlikely so we can relatively
16994 safely treat these cases as if the size attribute was invalid and
16995 treat them as zero by default. */
16996 if (attr->name == DW_AT_byte_size
16997 && form == DW_FORM_data4
16998 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16999 {
17000 complaint
17001 (&symfile_complaints,
43bbcdc2
PH
17002 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17003 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17004 DW_UNSND (attr) = 0;
17005 }
28e94949 17006
c906108c
SS
17007 return info_ptr;
17008}
17009
a8329558
KW
17010/* Read an attribute described by an abbreviated attribute. */
17011
d521ce57 17012static const gdb_byte *
dee91e82
DE
17013read_attribute (const struct die_reader_specs *reader,
17014 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17015 const gdb_byte *info_ptr)
a8329558
KW
17016{
17017 attr->name = abbrev->name;
43988095
JK
17018 return read_attribute_value (reader, attr, abbrev->form,
17019 abbrev->implicit_const, info_ptr);
a8329558
KW
17020}
17021
0963b4bd 17022/* Read dwarf information from a buffer. */
c906108c
SS
17023
17024static unsigned int
a1855c1d 17025read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17026{
fe1b8b76 17027 return bfd_get_8 (abfd, buf);
c906108c
SS
17028}
17029
17030static int
a1855c1d 17031read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17032{
fe1b8b76 17033 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17034}
17035
17036static unsigned int
a1855c1d 17037read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17038{
fe1b8b76 17039 return bfd_get_16 (abfd, buf);
c906108c
SS
17040}
17041
21ae7a4d 17042static int
a1855c1d 17043read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17044{
17045 return bfd_get_signed_16 (abfd, buf);
17046}
17047
c906108c 17048static unsigned int
a1855c1d 17049read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17050{
fe1b8b76 17051 return bfd_get_32 (abfd, buf);
c906108c
SS
17052}
17053
21ae7a4d 17054static int
a1855c1d 17055read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17056{
17057 return bfd_get_signed_32 (abfd, buf);
17058}
17059
93311388 17060static ULONGEST
a1855c1d 17061read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17062{
fe1b8b76 17063 return bfd_get_64 (abfd, buf);
c906108c
SS
17064}
17065
17066static CORE_ADDR
d521ce57 17067read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17068 unsigned int *bytes_read)
c906108c 17069{
e7c27a73 17070 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17071 CORE_ADDR retval = 0;
17072
107d2387 17073 if (cu_header->signed_addr_p)
c906108c 17074 {
107d2387
AC
17075 switch (cu_header->addr_size)
17076 {
17077 case 2:
fe1b8b76 17078 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17079 break;
17080 case 4:
fe1b8b76 17081 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17082 break;
17083 case 8:
fe1b8b76 17084 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17085 break;
17086 default:
8e65ff28 17087 internal_error (__FILE__, __LINE__,
e2e0b3e5 17088 _("read_address: bad switch, signed [in module %s]"),
659b0389 17089 bfd_get_filename (abfd));
107d2387
AC
17090 }
17091 }
17092 else
17093 {
17094 switch (cu_header->addr_size)
17095 {
17096 case 2:
fe1b8b76 17097 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17098 break;
17099 case 4:
fe1b8b76 17100 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17101 break;
17102 case 8:
fe1b8b76 17103 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17104 break;
17105 default:
8e65ff28 17106 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17107 _("read_address: bad switch, "
17108 "unsigned [in module %s]"),
659b0389 17109 bfd_get_filename (abfd));
107d2387 17110 }
c906108c 17111 }
64367e0a 17112
107d2387
AC
17113 *bytes_read = cu_header->addr_size;
17114 return retval;
c906108c
SS
17115}
17116
f7ef9339 17117/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17118 specification allows the initial length to take up either 4 bytes
17119 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17120 bytes describe the length and all offsets will be 8 bytes in length
17121 instead of 4.
17122
f7ef9339
KB
17123 An older, non-standard 64-bit format is also handled by this
17124 function. The older format in question stores the initial length
17125 as an 8-byte quantity without an escape value. Lengths greater
17126 than 2^32 aren't very common which means that the initial 4 bytes
17127 is almost always zero. Since a length value of zero doesn't make
17128 sense for the 32-bit format, this initial zero can be considered to
17129 be an escape value which indicates the presence of the older 64-bit
17130 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17131 greater than 4GB. If it becomes necessary to handle lengths
17132 somewhat larger than 4GB, we could allow other small values (such
17133 as the non-sensical values of 1, 2, and 3) to also be used as
17134 escape values indicating the presence of the old format.
f7ef9339 17135
917c78fc
MK
17136 The value returned via bytes_read should be used to increment the
17137 relevant pointer after calling read_initial_length().
c764a876 17138
613e1657
KB
17139 [ Note: read_initial_length() and read_offset() are based on the
17140 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17141 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17142 from:
17143
f7ef9339 17144 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17145
613e1657
KB
17146 This document is only a draft and is subject to change. (So beware.)
17147
f7ef9339 17148 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17149 determined empirically by examining 64-bit ELF files produced by
17150 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17151
17152 - Kevin, July 16, 2002
613e1657
KB
17153 ] */
17154
17155static LONGEST
d521ce57 17156read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17157{
fe1b8b76 17158 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17159
dd373385 17160 if (length == 0xffffffff)
613e1657 17161 {
fe1b8b76 17162 length = bfd_get_64 (abfd, buf + 4);
613e1657 17163 *bytes_read = 12;
613e1657 17164 }
dd373385 17165 else if (length == 0)
f7ef9339 17166 {
dd373385 17167 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17168 length = bfd_get_64 (abfd, buf);
f7ef9339 17169 *bytes_read = 8;
f7ef9339 17170 }
613e1657
KB
17171 else
17172 {
17173 *bytes_read = 4;
613e1657
KB
17174 }
17175
c764a876
DE
17176 return length;
17177}
dd373385 17178
c764a876
DE
17179/* Cover function for read_initial_length.
17180 Returns the length of the object at BUF, and stores the size of the
17181 initial length in *BYTES_READ and stores the size that offsets will be in
17182 *OFFSET_SIZE.
17183 If the initial length size is not equivalent to that specified in
17184 CU_HEADER then issue a complaint.
17185 This is useful when reading non-comp-unit headers. */
dd373385 17186
c764a876 17187static LONGEST
d521ce57 17188read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17189 const struct comp_unit_head *cu_header,
17190 unsigned int *bytes_read,
17191 unsigned int *offset_size)
17192{
17193 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17194
17195 gdb_assert (cu_header->initial_length_size == 4
17196 || cu_header->initial_length_size == 8
17197 || cu_header->initial_length_size == 12);
17198
17199 if (cu_header->initial_length_size != *bytes_read)
17200 complaint (&symfile_complaints,
17201 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17202
c764a876 17203 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17204 return length;
613e1657
KB
17205}
17206
17207/* Read an offset from the data stream. The size of the offset is
917c78fc 17208 given by cu_header->offset_size. */
613e1657
KB
17209
17210static LONGEST
d521ce57
TT
17211read_offset (bfd *abfd, const gdb_byte *buf,
17212 const struct comp_unit_head *cu_header,
891d2f0b 17213 unsigned int *bytes_read)
c764a876
DE
17214{
17215 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17216
c764a876
DE
17217 *bytes_read = cu_header->offset_size;
17218 return offset;
17219}
17220
17221/* Read an offset from the data stream. */
17222
17223static LONGEST
d521ce57 17224read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17225{
17226 LONGEST retval = 0;
17227
c764a876 17228 switch (offset_size)
613e1657
KB
17229 {
17230 case 4:
fe1b8b76 17231 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17232 break;
17233 case 8:
fe1b8b76 17234 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17235 break;
17236 default:
8e65ff28 17237 internal_error (__FILE__, __LINE__,
c764a876 17238 _("read_offset_1: bad switch [in module %s]"),
659b0389 17239 bfd_get_filename (abfd));
613e1657
KB
17240 }
17241
917c78fc 17242 return retval;
613e1657
KB
17243}
17244
d521ce57
TT
17245static const gdb_byte *
17246read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17247{
17248 /* If the size of a host char is 8 bits, we can return a pointer
17249 to the buffer, otherwise we have to copy the data to a buffer
17250 allocated on the temporary obstack. */
4bdf3d34 17251 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17252 return buf;
c906108c
SS
17253}
17254
d521ce57
TT
17255static const char *
17256read_direct_string (bfd *abfd, const gdb_byte *buf,
17257 unsigned int *bytes_read_ptr)
c906108c
SS
17258{
17259 /* If the size of a host char is 8 bits, we can return a pointer
17260 to the string, otherwise we have to copy the string to a buffer
17261 allocated on the temporary obstack. */
4bdf3d34 17262 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17263 if (*buf == '\0')
17264 {
17265 *bytes_read_ptr = 1;
17266 return NULL;
17267 }
d521ce57
TT
17268 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17269 return (const char *) buf;
4bdf3d34
JJ
17270}
17271
43988095
JK
17272/* Return pointer to string at section SECT offset STR_OFFSET with error
17273 reporting strings FORM_NAME and SECT_NAME. */
17274
d521ce57 17275static const char *
43988095
JK
17276read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17277 struct dwarf2_section_info *sect,
17278 const char *form_name,
17279 const char *sect_name)
17280{
17281 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17282 if (sect->buffer == NULL)
17283 error (_("%s used without %s section [in module %s]"),
17284 form_name, sect_name, bfd_get_filename (abfd));
17285 if (str_offset >= sect->size)
17286 error (_("%s pointing outside of %s section [in module %s]"),
17287 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17288 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17289 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17290 return NULL;
43988095
JK
17291 return (const char *) (sect->buffer + str_offset);
17292}
17293
17294/* Return pointer to string at .debug_str offset STR_OFFSET. */
17295
17296static const char *
17297read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17298{
17299 return read_indirect_string_at_offset_from (abfd, str_offset,
17300 &dwarf2_per_objfile->str,
17301 "DW_FORM_strp", ".debug_str");
17302}
17303
17304/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17305
17306static const char *
17307read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17308{
17309 return read_indirect_string_at_offset_from (abfd, str_offset,
17310 &dwarf2_per_objfile->line_str,
17311 "DW_FORM_line_strp",
17312 ".debug_line_str");
c906108c
SS
17313}
17314
36586728
TT
17315/* Read a string at offset STR_OFFSET in the .debug_str section from
17316 the .dwz file DWZ. Throw an error if the offset is too large. If
17317 the string consists of a single NUL byte, return NULL; otherwise
17318 return a pointer to the string. */
17319
d521ce57 17320static const char *
36586728
TT
17321read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17322{
17323 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17324
17325 if (dwz->str.buffer == NULL)
17326 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17327 "section [in module %s]"),
17328 bfd_get_filename (dwz->dwz_bfd));
17329 if (str_offset >= dwz->str.size)
17330 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17331 ".debug_str section [in module %s]"),
17332 bfd_get_filename (dwz->dwz_bfd));
17333 gdb_assert (HOST_CHAR_BIT == 8);
17334 if (dwz->str.buffer[str_offset] == '\0')
17335 return NULL;
d521ce57 17336 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17337}
17338
43988095
JK
17339/* Return pointer to string at .debug_str offset as read from BUF.
17340 BUF is assumed to be in a compilation unit described by CU_HEADER.
17341 Return *BYTES_READ_PTR count of bytes read from BUF. */
17342
d521ce57
TT
17343static const char *
17344read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17345 const struct comp_unit_head *cu_header,
17346 unsigned int *bytes_read_ptr)
17347{
17348 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17349
17350 return read_indirect_string_at_offset (abfd, str_offset);
17351}
17352
43988095
JK
17353/* Return pointer to string at .debug_line_str offset as read from BUF.
17354 BUF is assumed to be in a compilation unit described by CU_HEADER.
17355 Return *BYTES_READ_PTR count of bytes read from BUF. */
17356
17357static const char *
17358read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17359 const struct comp_unit_head *cu_header,
17360 unsigned int *bytes_read_ptr)
17361{
17362 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17363
17364 return read_indirect_line_string_at_offset (abfd, str_offset);
17365}
17366
17367ULONGEST
d521ce57 17368read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17369 unsigned int *bytes_read_ptr)
c906108c 17370{
12df843f 17371 ULONGEST result;
ce5d95e1 17372 unsigned int num_read;
870f88f7 17373 int shift;
c906108c
SS
17374 unsigned char byte;
17375
17376 result = 0;
17377 shift = 0;
17378 num_read = 0;
c906108c
SS
17379 while (1)
17380 {
fe1b8b76 17381 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17382 buf++;
17383 num_read++;
12df843f 17384 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17385 if ((byte & 128) == 0)
17386 {
17387 break;
17388 }
17389 shift += 7;
17390 }
17391 *bytes_read_ptr = num_read;
17392 return result;
17393}
17394
12df843f 17395static LONGEST
d521ce57
TT
17396read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17397 unsigned int *bytes_read_ptr)
c906108c 17398{
12df843f 17399 LONGEST result;
870f88f7 17400 int shift, num_read;
c906108c
SS
17401 unsigned char byte;
17402
17403 result = 0;
17404 shift = 0;
c906108c 17405 num_read = 0;
c906108c
SS
17406 while (1)
17407 {
fe1b8b76 17408 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17409 buf++;
17410 num_read++;
12df843f 17411 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17412 shift += 7;
17413 if ((byte & 128) == 0)
17414 {
17415 break;
17416 }
17417 }
77e0b926 17418 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17419 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17420 *bytes_read_ptr = num_read;
17421 return result;
17422}
17423
3019eac3
DE
17424/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17425 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17426 ADDR_SIZE is the size of addresses from the CU header. */
17427
17428static CORE_ADDR
17429read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17430{
17431 struct objfile *objfile = dwarf2_per_objfile->objfile;
17432 bfd *abfd = objfile->obfd;
17433 const gdb_byte *info_ptr;
17434
17435 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17436 if (dwarf2_per_objfile->addr.buffer == NULL)
17437 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17438 objfile_name (objfile));
3019eac3
DE
17439 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17440 error (_("DW_FORM_addr_index pointing outside of "
17441 ".debug_addr section [in module %s]"),
4262abfb 17442 objfile_name (objfile));
3019eac3
DE
17443 info_ptr = (dwarf2_per_objfile->addr.buffer
17444 + addr_base + addr_index * addr_size);
17445 if (addr_size == 4)
17446 return bfd_get_32 (abfd, info_ptr);
17447 else
17448 return bfd_get_64 (abfd, info_ptr);
17449}
17450
17451/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17452
17453static CORE_ADDR
17454read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17455{
17456 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17457}
17458
17459/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17460
17461static CORE_ADDR
d521ce57 17462read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17463 unsigned int *bytes_read)
17464{
17465 bfd *abfd = cu->objfile->obfd;
17466 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17467
17468 return read_addr_index (cu, addr_index);
17469}
17470
17471/* Data structure to pass results from dwarf2_read_addr_index_reader
17472 back to dwarf2_read_addr_index. */
17473
17474struct dwarf2_read_addr_index_data
17475{
17476 ULONGEST addr_base;
17477 int addr_size;
17478};
17479
17480/* die_reader_func for dwarf2_read_addr_index. */
17481
17482static void
17483dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17484 const gdb_byte *info_ptr,
3019eac3
DE
17485 struct die_info *comp_unit_die,
17486 int has_children,
17487 void *data)
17488{
17489 struct dwarf2_cu *cu = reader->cu;
17490 struct dwarf2_read_addr_index_data *aidata =
17491 (struct dwarf2_read_addr_index_data *) data;
17492
17493 aidata->addr_base = cu->addr_base;
17494 aidata->addr_size = cu->header.addr_size;
17495}
17496
17497/* Given an index in .debug_addr, fetch the value.
17498 NOTE: This can be called during dwarf expression evaluation,
17499 long after the debug information has been read, and thus per_cu->cu
17500 may no longer exist. */
17501
17502CORE_ADDR
17503dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17504 unsigned int addr_index)
17505{
17506 struct objfile *objfile = per_cu->objfile;
17507 struct dwarf2_cu *cu = per_cu->cu;
17508 ULONGEST addr_base;
17509 int addr_size;
17510
17511 /* This is intended to be called from outside this file. */
17512 dw2_setup (objfile);
17513
17514 /* We need addr_base and addr_size.
17515 If we don't have PER_CU->cu, we have to get it.
17516 Nasty, but the alternative is storing the needed info in PER_CU,
17517 which at this point doesn't seem justified: it's not clear how frequently
17518 it would get used and it would increase the size of every PER_CU.
17519 Entry points like dwarf2_per_cu_addr_size do a similar thing
17520 so we're not in uncharted territory here.
17521 Alas we need to be a bit more complicated as addr_base is contained
17522 in the DIE.
17523
17524 We don't need to read the entire CU(/TU).
17525 We just need the header and top level die.
a1b64ce1 17526
3019eac3 17527 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17528 For now we skip this optimization. */
3019eac3
DE
17529
17530 if (cu != NULL)
17531 {
17532 addr_base = cu->addr_base;
17533 addr_size = cu->header.addr_size;
17534 }
17535 else
17536 {
17537 struct dwarf2_read_addr_index_data aidata;
17538
a1b64ce1
DE
17539 /* Note: We can't use init_cutu_and_read_dies_simple here,
17540 we need addr_base. */
17541 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17542 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17543 addr_base = aidata.addr_base;
17544 addr_size = aidata.addr_size;
17545 }
17546
17547 return read_addr_index_1 (addr_index, addr_base, addr_size);
17548}
17549
57d63ce2
DE
17550/* Given a DW_FORM_GNU_str_index, fetch the string.
17551 This is only used by the Fission support. */
3019eac3 17552
d521ce57 17553static const char *
342587c4 17554read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17555{
17556 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17557 const char *objf_name = objfile_name (objfile);
3019eac3 17558 bfd *abfd = objfile->obfd;
342587c4 17559 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17560 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17561 struct dwarf2_section_info *str_offsets_section =
17562 &reader->dwo_file->sections.str_offsets;
d521ce57 17563 const gdb_byte *info_ptr;
3019eac3 17564 ULONGEST str_offset;
57d63ce2 17565 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17566
73869dc2
DE
17567 dwarf2_read_section (objfile, str_section);
17568 dwarf2_read_section (objfile, str_offsets_section);
17569 if (str_section->buffer == NULL)
57d63ce2 17570 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17571 " in CU at offset 0x%x [in module %s]"),
17572 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17573 if (str_offsets_section->buffer == NULL)
57d63ce2 17574 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17575 " in CU at offset 0x%x [in module %s]"),
17576 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17577 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17578 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17579 " section in CU at offset 0x%x [in module %s]"),
17580 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17581 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17582 + str_index * cu->header.offset_size);
17583 if (cu->header.offset_size == 4)
17584 str_offset = bfd_get_32 (abfd, info_ptr);
17585 else
17586 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17587 if (str_offset >= str_section->size)
57d63ce2 17588 error (_("Offset from %s pointing outside of"
9c541725
PA
17589 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17590 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17591 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17592}
17593
3019eac3
DE
17594/* Return the length of an LEB128 number in BUF. */
17595
17596static int
17597leb128_size (const gdb_byte *buf)
17598{
17599 const gdb_byte *begin = buf;
17600 gdb_byte byte;
17601
17602 while (1)
17603 {
17604 byte = *buf++;
17605 if ((byte & 128) == 0)
17606 return buf - begin;
17607 }
17608}
17609
c906108c 17610static void
e142c38c 17611set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17612{
17613 switch (lang)
17614 {
17615 case DW_LANG_C89:
76bee0cc 17616 case DW_LANG_C99:
0cfd832f 17617 case DW_LANG_C11:
c906108c 17618 case DW_LANG_C:
d1be3247 17619 case DW_LANG_UPC:
e142c38c 17620 cu->language = language_c;
c906108c 17621 break;
9c37b5ae 17622 case DW_LANG_Java:
c906108c 17623 case DW_LANG_C_plus_plus:
0cfd832f
MW
17624 case DW_LANG_C_plus_plus_11:
17625 case DW_LANG_C_plus_plus_14:
e142c38c 17626 cu->language = language_cplus;
c906108c 17627 break;
6aecb9c2
JB
17628 case DW_LANG_D:
17629 cu->language = language_d;
17630 break;
c906108c
SS
17631 case DW_LANG_Fortran77:
17632 case DW_LANG_Fortran90:
b21b22e0 17633 case DW_LANG_Fortran95:
f7de9aab
MW
17634 case DW_LANG_Fortran03:
17635 case DW_LANG_Fortran08:
e142c38c 17636 cu->language = language_fortran;
c906108c 17637 break;
a766d390
DE
17638 case DW_LANG_Go:
17639 cu->language = language_go;
17640 break;
c906108c 17641 case DW_LANG_Mips_Assembler:
e142c38c 17642 cu->language = language_asm;
c906108c
SS
17643 break;
17644 case DW_LANG_Ada83:
8aaf0b47 17645 case DW_LANG_Ada95:
bc5f45f8
JB
17646 cu->language = language_ada;
17647 break;
72019c9c
GM
17648 case DW_LANG_Modula2:
17649 cu->language = language_m2;
17650 break;
fe8e67fd
PM
17651 case DW_LANG_Pascal83:
17652 cu->language = language_pascal;
17653 break;
22566fbd
DJ
17654 case DW_LANG_ObjC:
17655 cu->language = language_objc;
17656 break;
c44af4eb
TT
17657 case DW_LANG_Rust:
17658 case DW_LANG_Rust_old:
17659 cu->language = language_rust;
17660 break;
c906108c
SS
17661 case DW_LANG_Cobol74:
17662 case DW_LANG_Cobol85:
c906108c 17663 default:
e142c38c 17664 cu->language = language_minimal;
c906108c
SS
17665 break;
17666 }
e142c38c 17667 cu->language_defn = language_def (cu->language);
c906108c
SS
17668}
17669
17670/* Return the named attribute or NULL if not there. */
17671
17672static struct attribute *
e142c38c 17673dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17674{
a48e046c 17675 for (;;)
c906108c 17676 {
a48e046c
TT
17677 unsigned int i;
17678 struct attribute *spec = NULL;
17679
17680 for (i = 0; i < die->num_attrs; ++i)
17681 {
17682 if (die->attrs[i].name == name)
17683 return &die->attrs[i];
17684 if (die->attrs[i].name == DW_AT_specification
17685 || die->attrs[i].name == DW_AT_abstract_origin)
17686 spec = &die->attrs[i];
17687 }
17688
17689 if (!spec)
17690 break;
c906108c 17691
f2f0e013 17692 die = follow_die_ref (die, spec, &cu);
f2f0e013 17693 }
c5aa993b 17694
c906108c
SS
17695 return NULL;
17696}
17697
348e048f
DE
17698/* Return the named attribute or NULL if not there,
17699 but do not follow DW_AT_specification, etc.
17700 This is for use in contexts where we're reading .debug_types dies.
17701 Following DW_AT_specification, DW_AT_abstract_origin will take us
17702 back up the chain, and we want to go down. */
17703
17704static struct attribute *
45e58e77 17705dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17706{
17707 unsigned int i;
17708
17709 for (i = 0; i < die->num_attrs; ++i)
17710 if (die->attrs[i].name == name)
17711 return &die->attrs[i];
17712
17713 return NULL;
17714}
17715
7d45c7c3
KB
17716/* Return the string associated with a string-typed attribute, or NULL if it
17717 is either not found or is of an incorrect type. */
17718
17719static const char *
17720dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17721{
17722 struct attribute *attr;
17723 const char *str = NULL;
17724
17725 attr = dwarf2_attr (die, name, cu);
17726
17727 if (attr != NULL)
17728 {
43988095 17729 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17730 || attr->form == DW_FORM_string
17731 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17732 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17733 str = DW_STRING (attr);
17734 else
17735 complaint (&symfile_complaints,
17736 _("string type expected for attribute %s for "
17737 "DIE at 0x%x in module %s"),
9c541725 17738 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17739 objfile_name (cu->objfile));
17740 }
17741
17742 return str;
17743}
17744
05cf31d1
JB
17745/* Return non-zero iff the attribute NAME is defined for the given DIE,
17746 and holds a non-zero value. This function should only be used for
2dc7f7b3 17747 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17748
17749static int
17750dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17751{
17752 struct attribute *attr = dwarf2_attr (die, name, cu);
17753
17754 return (attr && DW_UNSND (attr));
17755}
17756
3ca72b44 17757static int
e142c38c 17758die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17759{
05cf31d1
JB
17760 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17761 which value is non-zero. However, we have to be careful with
17762 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17763 (via dwarf2_flag_true_p) follows this attribute. So we may
17764 end up accidently finding a declaration attribute that belongs
17765 to a different DIE referenced by the specification attribute,
17766 even though the given DIE does not have a declaration attribute. */
17767 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17768 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17769}
17770
63d06c5c 17771/* Return the die giving the specification for DIE, if there is
f2f0e013 17772 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17773 containing the return value on output. If there is no
17774 specification, but there is an abstract origin, that is
17775 returned. */
63d06c5c
DC
17776
17777static struct die_info *
f2f0e013 17778die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17779{
f2f0e013
DJ
17780 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17781 *spec_cu);
63d06c5c 17782
edb3359d
DJ
17783 if (spec_attr == NULL)
17784 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17785
63d06c5c
DC
17786 if (spec_attr == NULL)
17787 return NULL;
17788 else
f2f0e013 17789 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17790}
c906108c 17791
527f3840
JK
17792/* Stub for free_line_header to match void * callback types. */
17793
17794static void
17795free_line_header_voidp (void *arg)
17796{
9a3c8263 17797 struct line_header *lh = (struct line_header *) arg;
527f3840 17798
fff8551c 17799 delete lh;
527f3840
JK
17800}
17801
fff8551c
PA
17802void
17803line_header::add_include_dir (const char *include_dir)
c906108c 17804{
27e0867f 17805 if (dwarf_line_debug >= 2)
fff8551c
PA
17806 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17807 include_dirs.size () + 1, include_dir);
27e0867f 17808
fff8551c 17809 include_dirs.push_back (include_dir);
debd256d 17810}
6e70227d 17811
fff8551c
PA
17812void
17813line_header::add_file_name (const char *name,
ecfb656c 17814 dir_index d_index,
fff8551c
PA
17815 unsigned int mod_time,
17816 unsigned int length)
debd256d 17817{
27e0867f
DE
17818 if (dwarf_line_debug >= 2)
17819 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17820 (unsigned) file_names.size () + 1, name);
27e0867f 17821
ecfb656c 17822 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17823}
6e70227d 17824
83769d0b 17825/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17826
17827static struct dwarf2_section_info *
17828get_debug_line_section (struct dwarf2_cu *cu)
17829{
17830 struct dwarf2_section_info *section;
17831
17832 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17833 DWO file. */
17834 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17835 section = &cu->dwo_unit->dwo_file->sections.line;
17836 else if (cu->per_cu->is_dwz)
17837 {
17838 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17839
17840 section = &dwz->line;
17841 }
17842 else
17843 section = &dwarf2_per_objfile->line;
17844
17845 return section;
17846}
17847
43988095
JK
17848/* Read directory or file name entry format, starting with byte of
17849 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17850 entries count and the entries themselves in the described entry
17851 format. */
17852
17853static void
17854read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17855 struct line_header *lh,
17856 const struct comp_unit_head *cu_header,
17857 void (*callback) (struct line_header *lh,
17858 const char *name,
ecfb656c 17859 dir_index d_index,
43988095
JK
17860 unsigned int mod_time,
17861 unsigned int length))
17862{
17863 gdb_byte format_count, formati;
17864 ULONGEST data_count, datai;
17865 const gdb_byte *buf = *bufp;
17866 const gdb_byte *format_header_data;
17867 int i;
17868 unsigned int bytes_read;
17869
17870 format_count = read_1_byte (abfd, buf);
17871 buf += 1;
17872 format_header_data = buf;
17873 for (formati = 0; formati < format_count; formati++)
17874 {
17875 read_unsigned_leb128 (abfd, buf, &bytes_read);
17876 buf += bytes_read;
17877 read_unsigned_leb128 (abfd, buf, &bytes_read);
17878 buf += bytes_read;
17879 }
17880
17881 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17882 buf += bytes_read;
17883 for (datai = 0; datai < data_count; datai++)
17884 {
17885 const gdb_byte *format = format_header_data;
17886 struct file_entry fe;
17887
43988095
JK
17888 for (formati = 0; formati < format_count; formati++)
17889 {
ecfb656c 17890 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17891 format += bytes_read;
43988095 17892
ecfb656c 17893 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17894 format += bytes_read;
ecfb656c
PA
17895
17896 gdb::optional<const char *> string;
17897 gdb::optional<unsigned int> uint;
17898
43988095
JK
17899 switch (form)
17900 {
17901 case DW_FORM_string:
ecfb656c 17902 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17903 buf += bytes_read;
17904 break;
17905
17906 case DW_FORM_line_strp:
ecfb656c
PA
17907 string.emplace (read_indirect_line_string (abfd, buf,
17908 cu_header,
17909 &bytes_read));
43988095
JK
17910 buf += bytes_read;
17911 break;
17912
17913 case DW_FORM_data1:
ecfb656c 17914 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17915 buf += 1;
17916 break;
17917
17918 case DW_FORM_data2:
ecfb656c 17919 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17920 buf += 2;
17921 break;
17922
17923 case DW_FORM_data4:
ecfb656c 17924 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17925 buf += 4;
17926 break;
17927
17928 case DW_FORM_data8:
ecfb656c 17929 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17930 buf += 8;
17931 break;
17932
17933 case DW_FORM_udata:
ecfb656c 17934 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17935 buf += bytes_read;
17936 break;
17937
17938 case DW_FORM_block:
17939 /* It is valid only for DW_LNCT_timestamp which is ignored by
17940 current GDB. */
17941 break;
17942 }
ecfb656c
PA
17943
17944 switch (content_type)
17945 {
17946 case DW_LNCT_path:
17947 if (string.has_value ())
17948 fe.name = *string;
17949 break;
17950 case DW_LNCT_directory_index:
17951 if (uint.has_value ())
17952 fe.d_index = (dir_index) *uint;
17953 break;
17954 case DW_LNCT_timestamp:
17955 if (uint.has_value ())
17956 fe.mod_time = *uint;
17957 break;
17958 case DW_LNCT_size:
17959 if (uint.has_value ())
17960 fe.length = *uint;
17961 break;
17962 case DW_LNCT_MD5:
17963 break;
17964 default:
17965 complaint (&symfile_complaints,
17966 _("Unknown format content type %s"),
17967 pulongest (content_type));
17968 }
43988095
JK
17969 }
17970
ecfb656c 17971 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17972 }
17973
17974 *bufp = buf;
17975}
17976
debd256d 17977/* Read the statement program header starting at OFFSET in
3019eac3 17978 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17979 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17980 Returns NULL if there is a problem reading the header, e.g., if it
17981 has a version we don't understand.
debd256d
JB
17982
17983 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17984 the returned object point into the dwarf line section buffer,
17985 and must not be freed. */
ae2de4f8 17986
fff8551c 17987static line_header_up
9c541725 17988dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17989{
d521ce57 17990 const gdb_byte *line_ptr;
c764a876 17991 unsigned int bytes_read, offset_size;
debd256d 17992 int i;
d521ce57 17993 const char *cur_dir, *cur_file;
3019eac3
DE
17994 struct dwarf2_section_info *section;
17995 bfd *abfd;
17996
36586728 17997 section = get_debug_line_section (cu);
3019eac3
DE
17998 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17999 if (section->buffer == NULL)
debd256d 18000 {
3019eac3
DE
18001 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18002 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18003 else
18004 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18005 return 0;
18006 }
18007
fceca515
DE
18008 /* We can't do this until we know the section is non-empty.
18009 Only then do we know we have such a section. */
a32a8923 18010 abfd = get_section_bfd_owner (section);
fceca515 18011
a738430d
MK
18012 /* Make sure that at least there's room for the total_length field.
18013 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18014 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18015 {
4d3c2250 18016 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18017 return 0;
18018 }
18019
fff8551c 18020 line_header_up lh (new line_header ());
debd256d 18021
9c541725 18022 lh->sect_off = sect_off;
527f3840
JK
18023 lh->offset_in_dwz = cu->per_cu->is_dwz;
18024
9c541725 18025 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18026
a738430d 18027 /* Read in the header. */
6e70227d 18028 lh->total_length =
c764a876
DE
18029 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18030 &bytes_read, &offset_size);
debd256d 18031 line_ptr += bytes_read;
3019eac3 18032 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18033 {
4d3c2250 18034 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18035 return 0;
18036 }
18037 lh->statement_program_end = line_ptr + lh->total_length;
18038 lh->version = read_2_bytes (abfd, line_ptr);
18039 line_ptr += 2;
43988095 18040 if (lh->version > 5)
cd366ee8
DE
18041 {
18042 /* This is a version we don't understand. The format could have
18043 changed in ways we don't handle properly so just punt. */
18044 complaint (&symfile_complaints,
18045 _("unsupported version in .debug_line section"));
18046 return NULL;
18047 }
43988095
JK
18048 if (lh->version >= 5)
18049 {
18050 gdb_byte segment_selector_size;
18051
18052 /* Skip address size. */
18053 read_1_byte (abfd, line_ptr);
18054 line_ptr += 1;
18055
18056 segment_selector_size = read_1_byte (abfd, line_ptr);
18057 line_ptr += 1;
18058 if (segment_selector_size != 0)
18059 {
18060 complaint (&symfile_complaints,
18061 _("unsupported segment selector size %u "
18062 "in .debug_line section"),
18063 segment_selector_size);
18064 return NULL;
18065 }
18066 }
c764a876
DE
18067 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18068 line_ptr += offset_size;
debd256d
JB
18069 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18070 line_ptr += 1;
2dc7f7b3
TT
18071 if (lh->version >= 4)
18072 {
18073 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18074 line_ptr += 1;
18075 }
18076 else
18077 lh->maximum_ops_per_instruction = 1;
18078
18079 if (lh->maximum_ops_per_instruction == 0)
18080 {
18081 lh->maximum_ops_per_instruction = 1;
18082 complaint (&symfile_complaints,
3e43a32a
MS
18083 _("invalid maximum_ops_per_instruction "
18084 "in `.debug_line' section"));
2dc7f7b3
TT
18085 }
18086
debd256d
JB
18087 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18088 line_ptr += 1;
18089 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18090 line_ptr += 1;
18091 lh->line_range = read_1_byte (abfd, line_ptr);
18092 line_ptr += 1;
18093 lh->opcode_base = read_1_byte (abfd, line_ptr);
18094 line_ptr += 1;
fff8551c 18095 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18096
18097 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18098 for (i = 1; i < lh->opcode_base; ++i)
18099 {
18100 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18101 line_ptr += 1;
18102 }
18103
43988095 18104 if (lh->version >= 5)
debd256d 18105 {
43988095 18106 /* Read directory table. */
fff8551c
PA
18107 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18108 [] (struct line_header *lh, const char *name,
ecfb656c 18109 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18110 unsigned int length)
18111 {
18112 lh->add_include_dir (name);
18113 });
debd256d 18114
43988095 18115 /* Read file name table. */
fff8551c
PA
18116 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18117 [] (struct line_header *lh, const char *name,
ecfb656c 18118 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18119 unsigned int length)
18120 {
ecfb656c 18121 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18122 });
43988095
JK
18123 }
18124 else
debd256d 18125 {
43988095
JK
18126 /* Read directory table. */
18127 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18128 {
18129 line_ptr += bytes_read;
fff8551c 18130 lh->add_include_dir (cur_dir);
43988095 18131 }
debd256d
JB
18132 line_ptr += bytes_read;
18133
43988095
JK
18134 /* Read file name table. */
18135 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18136 {
ecfb656c
PA
18137 unsigned int mod_time, length;
18138 dir_index d_index;
43988095
JK
18139
18140 line_ptr += bytes_read;
ecfb656c 18141 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18142 line_ptr += bytes_read;
18143 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18144 line_ptr += bytes_read;
18145 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18146 line_ptr += bytes_read;
18147
ecfb656c 18148 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18149 }
18150 line_ptr += bytes_read;
debd256d 18151 }
6e70227d 18152 lh->statement_program_start = line_ptr;
debd256d 18153
3019eac3 18154 if (line_ptr > (section->buffer + section->size))
4d3c2250 18155 complaint (&symfile_complaints,
3e43a32a
MS
18156 _("line number info header doesn't "
18157 "fit in `.debug_line' section"));
debd256d 18158
debd256d
JB
18159 return lh;
18160}
c906108c 18161
c6da4cef
DE
18162/* Subroutine of dwarf_decode_lines to simplify it.
18163 Return the file name of the psymtab for included file FILE_INDEX
18164 in line header LH of PST.
18165 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18166 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18167 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18168
18169 The function creates dangling cleanup registration. */
c6da4cef 18170
d521ce57 18171static const char *
c6da4cef
DE
18172psymtab_include_file_name (const struct line_header *lh, int file_index,
18173 const struct partial_symtab *pst,
18174 const char *comp_dir)
18175{
8c43009f 18176 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18177 const char *include_name = fe.name;
18178 const char *include_name_to_compare = include_name;
72b9f47f
TT
18179 const char *pst_filename;
18180 char *copied_name = NULL;
c6da4cef
DE
18181 int file_is_pst;
18182
8c43009f 18183 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18184
18185 if (!IS_ABSOLUTE_PATH (include_name)
18186 && (dir_name != NULL || comp_dir != NULL))
18187 {
18188 /* Avoid creating a duplicate psymtab for PST.
18189 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18190 Before we do the comparison, however, we need to account
18191 for DIR_NAME and COMP_DIR.
18192 First prepend dir_name (if non-NULL). If we still don't
18193 have an absolute path prepend comp_dir (if non-NULL).
18194 However, the directory we record in the include-file's
18195 psymtab does not contain COMP_DIR (to match the
18196 corresponding symtab(s)).
18197
18198 Example:
18199
18200 bash$ cd /tmp
18201 bash$ gcc -g ./hello.c
18202 include_name = "hello.c"
18203 dir_name = "."
18204 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18205 DW_AT_name = "./hello.c"
18206
18207 */
c6da4cef
DE
18208
18209 if (dir_name != NULL)
18210 {
d521ce57
TT
18211 char *tem = concat (dir_name, SLASH_STRING,
18212 include_name, (char *)NULL);
18213
18214 make_cleanup (xfree, tem);
18215 include_name = tem;
c6da4cef 18216 include_name_to_compare = include_name;
c6da4cef
DE
18217 }
18218 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18219 {
d521ce57
TT
18220 char *tem = concat (comp_dir, SLASH_STRING,
18221 include_name, (char *)NULL);
18222
18223 make_cleanup (xfree, tem);
18224 include_name_to_compare = tem;
c6da4cef
DE
18225 }
18226 }
18227
18228 pst_filename = pst->filename;
18229 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18230 {
72b9f47f
TT
18231 copied_name = concat (pst->dirname, SLASH_STRING,
18232 pst_filename, (char *)NULL);
18233 pst_filename = copied_name;
c6da4cef
DE
18234 }
18235
1e3fad37 18236 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18237
72b9f47f
TT
18238 if (copied_name != NULL)
18239 xfree (copied_name);
c6da4cef
DE
18240
18241 if (file_is_pst)
18242 return NULL;
18243 return include_name;
18244}
18245
d9b3de22
DE
18246/* State machine to track the state of the line number program. */
18247
6f77053d 18248class lnp_state_machine
d9b3de22 18249{
6f77053d
PA
18250public:
18251 /* Initialize a machine state for the start of a line number
18252 program. */
18253 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18254
8c43009f
PA
18255 file_entry *current_file ()
18256 {
18257 /* lh->file_names is 0-based, but the file name numbers in the
18258 statement program are 1-based. */
6f77053d
PA
18259 return m_line_header->file_name_at (m_file);
18260 }
18261
18262 /* Record the line in the state machine. END_SEQUENCE is true if
18263 we're processing the end of a sequence. */
18264 void record_line (bool end_sequence);
18265
18266 /* Check address and if invalid nop-out the rest of the lines in this
18267 sequence. */
18268 void check_line_address (struct dwarf2_cu *cu,
18269 const gdb_byte *line_ptr,
18270 CORE_ADDR lowpc, CORE_ADDR address);
18271
18272 void handle_set_discriminator (unsigned int discriminator)
18273 {
18274 m_discriminator = discriminator;
18275 m_line_has_non_zero_discriminator |= discriminator != 0;
18276 }
18277
18278 /* Handle DW_LNE_set_address. */
18279 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18280 {
18281 m_op_index = 0;
18282 address += baseaddr;
18283 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18284 }
18285
18286 /* Handle DW_LNS_advance_pc. */
18287 void handle_advance_pc (CORE_ADDR adjust);
18288
18289 /* Handle a special opcode. */
18290 void handle_special_opcode (unsigned char op_code);
18291
18292 /* Handle DW_LNS_advance_line. */
18293 void handle_advance_line (int line_delta)
18294 {
18295 advance_line (line_delta);
18296 }
18297
18298 /* Handle DW_LNS_set_file. */
18299 void handle_set_file (file_name_index file);
18300
18301 /* Handle DW_LNS_negate_stmt. */
18302 void handle_negate_stmt ()
18303 {
18304 m_is_stmt = !m_is_stmt;
18305 }
18306
18307 /* Handle DW_LNS_const_add_pc. */
18308 void handle_const_add_pc ();
18309
18310 /* Handle DW_LNS_fixed_advance_pc. */
18311 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18312 {
18313 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18314 m_op_index = 0;
18315 }
18316
18317 /* Handle DW_LNS_copy. */
18318 void handle_copy ()
18319 {
18320 record_line (false);
18321 m_discriminator = 0;
18322 }
18323
18324 /* Handle DW_LNE_end_sequence. */
18325 void handle_end_sequence ()
18326 {
18327 m_record_line_callback = ::record_line;
18328 }
18329
18330private:
18331 /* Advance the line by LINE_DELTA. */
18332 void advance_line (int line_delta)
18333 {
18334 m_line += line_delta;
18335
18336 if (line_delta != 0)
18337 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18338 }
18339
6f77053d
PA
18340 gdbarch *m_gdbarch;
18341
18342 /* True if we're recording lines.
18343 Otherwise we're building partial symtabs and are just interested in
18344 finding include files mentioned by the line number program. */
18345 bool m_record_lines_p;
18346
8c43009f 18347 /* The line number header. */
6f77053d 18348 line_header *m_line_header;
8c43009f 18349
6f77053d
PA
18350 /* These are part of the standard DWARF line number state machine,
18351 and initialized according to the DWARF spec. */
d9b3de22 18352
6f77053d 18353 unsigned char m_op_index = 0;
8c43009f 18354 /* The line table index (1-based) of the current file. */
6f77053d
PA
18355 file_name_index m_file = (file_name_index) 1;
18356 unsigned int m_line = 1;
18357
18358 /* These are initialized in the constructor. */
18359
18360 CORE_ADDR m_address;
18361 bool m_is_stmt;
18362 unsigned int m_discriminator;
d9b3de22
DE
18363
18364 /* Additional bits of state we need to track. */
18365
18366 /* The last file that we called dwarf2_start_subfile for.
18367 This is only used for TLLs. */
6f77053d 18368 unsigned int m_last_file = 0;
d9b3de22 18369 /* The last file a line number was recorded for. */
6f77053d 18370 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18371
18372 /* The function to call to record a line. */
6f77053d 18373 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18374
18375 /* The last line number that was recorded, used to coalesce
18376 consecutive entries for the same line. This can happen, for
18377 example, when discriminators are present. PR 17276. */
6f77053d
PA
18378 unsigned int m_last_line = 0;
18379 bool m_line_has_non_zero_discriminator = false;
8c43009f 18380};
d9b3de22 18381
6f77053d
PA
18382void
18383lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18384{
18385 CORE_ADDR addr_adj = (((m_op_index + adjust)
18386 / m_line_header->maximum_ops_per_instruction)
18387 * m_line_header->minimum_instruction_length);
18388 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18389 m_op_index = ((m_op_index + adjust)
18390 % m_line_header->maximum_ops_per_instruction);
18391}
d9b3de22 18392
6f77053d
PA
18393void
18394lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18395{
6f77053d
PA
18396 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18397 CORE_ADDR addr_adj = (((m_op_index
18398 + (adj_opcode / m_line_header->line_range))
18399 / m_line_header->maximum_ops_per_instruction)
18400 * m_line_header->minimum_instruction_length);
18401 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18402 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18403 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18404
6f77053d
PA
18405 int line_delta = (m_line_header->line_base
18406 + (adj_opcode % m_line_header->line_range));
18407 advance_line (line_delta);
18408 record_line (false);
18409 m_discriminator = 0;
18410}
d9b3de22 18411
6f77053d
PA
18412void
18413lnp_state_machine::handle_set_file (file_name_index file)
18414{
18415 m_file = file;
18416
18417 const file_entry *fe = current_file ();
18418 if (fe == NULL)
18419 dwarf2_debug_line_missing_file_complaint ();
18420 else if (m_record_lines_p)
18421 {
18422 const char *dir = fe->include_dir (m_line_header);
18423
18424 m_last_subfile = current_subfile;
18425 m_line_has_non_zero_discriminator = m_discriminator != 0;
18426 dwarf2_start_subfile (fe->name, dir);
18427 }
18428}
18429
18430void
18431lnp_state_machine::handle_const_add_pc ()
18432{
18433 CORE_ADDR adjust
18434 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18435
18436 CORE_ADDR addr_adj
18437 = (((m_op_index + adjust)
18438 / m_line_header->maximum_ops_per_instruction)
18439 * m_line_header->minimum_instruction_length);
18440
18441 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18442 m_op_index = ((m_op_index + adjust)
18443 % m_line_header->maximum_ops_per_instruction);
18444}
d9b3de22 18445
c91513d8
PP
18446/* Ignore this record_line request. */
18447
18448static void
18449noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18450{
18451 return;
18452}
18453
a05a36a5
DE
18454/* Return non-zero if we should add LINE to the line number table.
18455 LINE is the line to add, LAST_LINE is the last line that was added,
18456 LAST_SUBFILE is the subfile for LAST_LINE.
18457 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18458 had a non-zero discriminator.
18459
18460 We have to be careful in the presence of discriminators.
18461 E.g., for this line:
18462
18463 for (i = 0; i < 100000; i++);
18464
18465 clang can emit four line number entries for that one line,
18466 each with a different discriminator.
18467 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18468
18469 However, we want gdb to coalesce all four entries into one.
18470 Otherwise the user could stepi into the middle of the line and
18471 gdb would get confused about whether the pc really was in the
18472 middle of the line.
18473
18474 Things are further complicated by the fact that two consecutive
18475 line number entries for the same line is a heuristic used by gcc
18476 to denote the end of the prologue. So we can't just discard duplicate
18477 entries, we have to be selective about it. The heuristic we use is
18478 that we only collapse consecutive entries for the same line if at least
18479 one of those entries has a non-zero discriminator. PR 17276.
18480
18481 Note: Addresses in the line number state machine can never go backwards
18482 within one sequence, thus this coalescing is ok. */
18483
18484static int
18485dwarf_record_line_p (unsigned int line, unsigned int last_line,
18486 int line_has_non_zero_discriminator,
18487 struct subfile *last_subfile)
18488{
18489 if (current_subfile != last_subfile)
18490 return 1;
18491 if (line != last_line)
18492 return 1;
18493 /* Same line for the same file that we've seen already.
18494 As a last check, for pr 17276, only record the line if the line
18495 has never had a non-zero discriminator. */
18496 if (!line_has_non_zero_discriminator)
18497 return 1;
18498 return 0;
18499}
18500
252a6764
DE
18501/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18502 in the line table of subfile SUBFILE. */
18503
18504static void
d9b3de22
DE
18505dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18506 unsigned int line, CORE_ADDR address,
18507 record_line_ftype p_record_line)
252a6764
DE
18508{
18509 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18510
27e0867f
DE
18511 if (dwarf_line_debug)
18512 {
18513 fprintf_unfiltered (gdb_stdlog,
18514 "Recording line %u, file %s, address %s\n",
18515 line, lbasename (subfile->name),
18516 paddress (gdbarch, address));
18517 }
18518
d5962de5 18519 (*p_record_line) (subfile, line, addr);
252a6764
DE
18520}
18521
18522/* Subroutine of dwarf_decode_lines_1 to simplify it.
18523 Mark the end of a set of line number records.
d9b3de22 18524 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18525 If SUBFILE is NULL the request is ignored. */
18526
18527static void
18528dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18529 CORE_ADDR address, record_line_ftype p_record_line)
18530{
27e0867f
DE
18531 if (subfile == NULL)
18532 return;
18533
18534 if (dwarf_line_debug)
18535 {
18536 fprintf_unfiltered (gdb_stdlog,
18537 "Finishing current line, file %s, address %s\n",
18538 lbasename (subfile->name),
18539 paddress (gdbarch, address));
18540 }
18541
d9b3de22
DE
18542 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18543}
18544
6f77053d
PA
18545void
18546lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18547{
d9b3de22
DE
18548 if (dwarf_line_debug)
18549 {
18550 fprintf_unfiltered (gdb_stdlog,
18551 "Processing actual line %u: file %u,"
18552 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18553 m_line, to_underlying (m_file),
18554 paddress (m_gdbarch, m_address),
18555 m_is_stmt, m_discriminator);
d9b3de22
DE
18556 }
18557
6f77053d 18558 file_entry *fe = current_file ();
8c43009f
PA
18559
18560 if (fe == NULL)
d9b3de22
DE
18561 dwarf2_debug_line_missing_file_complaint ();
18562 /* For now we ignore lines not starting on an instruction boundary.
18563 But not when processing end_sequence for compatibility with the
18564 previous version of the code. */
6f77053d 18565 else if (m_op_index == 0 || end_sequence)
d9b3de22 18566 {
8c43009f 18567 fe->included_p = 1;
6f77053d 18568 if (m_record_lines_p && m_is_stmt)
d9b3de22 18569 {
6f77053d 18570 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18571 {
6f77053d
PA
18572 dwarf_finish_line (m_gdbarch, m_last_subfile,
18573 m_address, m_record_line_callback);
d9b3de22
DE
18574 }
18575
18576 if (!end_sequence)
18577 {
6f77053d
PA
18578 if (dwarf_record_line_p (m_line, m_last_line,
18579 m_line_has_non_zero_discriminator,
18580 m_last_subfile))
d9b3de22 18581 {
6f77053d
PA
18582 dwarf_record_line_1 (m_gdbarch, current_subfile,
18583 m_line, m_address,
18584 m_record_line_callback);
d9b3de22 18585 }
6f77053d
PA
18586 m_last_subfile = current_subfile;
18587 m_last_line = m_line;
d9b3de22
DE
18588 }
18589 }
18590 }
18591}
18592
6f77053d
PA
18593lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18594 bool record_lines_p)
d9b3de22 18595{
6f77053d
PA
18596 m_gdbarch = arch;
18597 m_record_lines_p = record_lines_p;
18598 m_line_header = lh;
d9b3de22 18599
6f77053d 18600 m_record_line_callback = ::record_line;
d9b3de22 18601
d9b3de22
DE
18602 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18603 was a line entry for it so that the backend has a chance to adjust it
18604 and also record it in case it needs it. This is currently used by MIPS
18605 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18606 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18607 m_is_stmt = lh->default_is_stmt;
18608 m_discriminator = 0;
252a6764
DE
18609}
18610
6f77053d
PA
18611void
18612lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18613 const gdb_byte *line_ptr,
18614 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18615{
18616 /* If address < lowpc then it's not a usable value, it's outside the
18617 pc range of the CU. However, we restrict the test to only address
18618 values of zero to preserve GDB's previous behaviour which is to
18619 handle the specific case of a function being GC'd by the linker. */
18620
18621 if (address == 0 && address < lowpc)
18622 {
18623 /* This line table is for a function which has been
18624 GCd by the linker. Ignore it. PR gdb/12528 */
18625
18626 struct objfile *objfile = cu->objfile;
18627 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18628
18629 complaint (&symfile_complaints,
18630 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18631 line_offset, objfile_name (objfile));
6f77053d
PA
18632 m_record_line_callback = noop_record_line;
18633 /* Note: record_line_callback is left as noop_record_line until
18634 we see DW_LNE_end_sequence. */
924c2928
DE
18635 }
18636}
18637
f3f5162e 18638/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18639 Process the line number information in LH.
18640 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18641 program in order to set included_p for every referenced header. */
debd256d 18642
c906108c 18643static void
43f3e411
DE
18644dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18645 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18646{
d521ce57
TT
18647 const gdb_byte *line_ptr, *extended_end;
18648 const gdb_byte *line_end;
a8c50c1f 18649 unsigned int bytes_read, extended_len;
699ca60a 18650 unsigned char op_code, extended_op;
e142c38c
DJ
18651 CORE_ADDR baseaddr;
18652 struct objfile *objfile = cu->objfile;
f3f5162e 18653 bfd *abfd = objfile->obfd;
fbf65064 18654 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18655 /* True if we're recording line info (as opposed to building partial
18656 symtabs and just interested in finding include files mentioned by
18657 the line number program). */
18658 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18659
18660 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18661
debd256d
JB
18662 line_ptr = lh->statement_program_start;
18663 line_end = lh->statement_program_end;
c906108c
SS
18664
18665 /* Read the statement sequences until there's nothing left. */
18666 while (line_ptr < line_end)
18667 {
6f77053d
PA
18668 /* The DWARF line number program state machine. Reset the state
18669 machine at the start of each sequence. */
18670 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18671 bool end_sequence = false;
d9b3de22 18672
8c43009f 18673 if (record_lines_p)
c906108c 18674 {
8c43009f
PA
18675 /* Start a subfile for the current file of the state
18676 machine. */
18677 const file_entry *fe = state_machine.current_file ();
18678
18679 if (fe != NULL)
18680 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18681 }
18682
a738430d 18683 /* Decode the table. */
d9b3de22 18684 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18685 {
18686 op_code = read_1_byte (abfd, line_ptr);
18687 line_ptr += 1;
9aa1fe7e 18688
debd256d 18689 if (op_code >= lh->opcode_base)
6e70227d 18690 {
8e07a239 18691 /* Special opcode. */
6f77053d 18692 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18693 }
18694 else switch (op_code)
c906108c
SS
18695 {
18696 case DW_LNS_extended_op:
3e43a32a
MS
18697 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18698 &bytes_read);
473b7be6 18699 line_ptr += bytes_read;
a8c50c1f 18700 extended_end = line_ptr + extended_len;
c906108c
SS
18701 extended_op = read_1_byte (abfd, line_ptr);
18702 line_ptr += 1;
18703 switch (extended_op)
18704 {
18705 case DW_LNE_end_sequence:
6f77053d
PA
18706 state_machine.handle_end_sequence ();
18707 end_sequence = true;
c906108c
SS
18708 break;
18709 case DW_LNE_set_address:
d9b3de22
DE
18710 {
18711 CORE_ADDR address
18712 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18713 line_ptr += bytes_read;
6f77053d
PA
18714
18715 state_machine.check_line_address (cu, line_ptr,
18716 lowpc, address);
18717 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18718 }
c906108c
SS
18719 break;
18720 case DW_LNE_define_file:
debd256d 18721 {
d521ce57 18722 const char *cur_file;
ecfb656c
PA
18723 unsigned int mod_time, length;
18724 dir_index dindex;
6e70227d 18725
3e43a32a
MS
18726 cur_file = read_direct_string (abfd, line_ptr,
18727 &bytes_read);
debd256d 18728 line_ptr += bytes_read;
ecfb656c 18729 dindex = (dir_index)
debd256d
JB
18730 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18731 line_ptr += bytes_read;
18732 mod_time =
18733 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18734 line_ptr += bytes_read;
18735 length =
18736 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18737 line_ptr += bytes_read;
ecfb656c 18738 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18739 }
c906108c 18740 break;
d0c6ba3d 18741 case DW_LNE_set_discriminator:
6f77053d
PA
18742 {
18743 /* The discriminator is not interesting to the
18744 debugger; just ignore it. We still need to
18745 check its value though:
18746 if there are consecutive entries for the same
18747 (non-prologue) line we want to coalesce them.
18748 PR 17276. */
18749 unsigned int discr
18750 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18751 line_ptr += bytes_read;
18752
18753 state_machine.handle_set_discriminator (discr);
18754 }
d0c6ba3d 18755 break;
c906108c 18756 default:
4d3c2250 18757 complaint (&symfile_complaints,
e2e0b3e5 18758 _("mangled .debug_line section"));
debd256d 18759 return;
c906108c 18760 }
a8c50c1f
DJ
18761 /* Make sure that we parsed the extended op correctly. If e.g.
18762 we expected a different address size than the producer used,
18763 we may have read the wrong number of bytes. */
18764 if (line_ptr != extended_end)
18765 {
18766 complaint (&symfile_complaints,
18767 _("mangled .debug_line section"));
18768 return;
18769 }
c906108c
SS
18770 break;
18771 case DW_LNS_copy:
6f77053d 18772 state_machine.handle_copy ();
c906108c
SS
18773 break;
18774 case DW_LNS_advance_pc:
2dc7f7b3
TT
18775 {
18776 CORE_ADDR adjust
18777 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18778 line_ptr += bytes_read;
6f77053d
PA
18779
18780 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18781 }
c906108c
SS
18782 break;
18783 case DW_LNS_advance_line:
a05a36a5
DE
18784 {
18785 int line_delta
18786 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18787 line_ptr += bytes_read;
6f77053d
PA
18788
18789 state_machine.handle_advance_line (line_delta);
a05a36a5 18790 }
c906108c
SS
18791 break;
18792 case DW_LNS_set_file:
d9b3de22 18793 {
6f77053d 18794 file_name_index file
ecfb656c
PA
18795 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18796 &bytes_read);
d9b3de22 18797 line_ptr += bytes_read;
8c43009f 18798
6f77053d 18799 state_machine.handle_set_file (file);
d9b3de22 18800 }
c906108c
SS
18801 break;
18802 case DW_LNS_set_column:
0ad93d4f 18803 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18804 line_ptr += bytes_read;
18805 break;
18806 case DW_LNS_negate_stmt:
6f77053d 18807 state_machine.handle_negate_stmt ();
c906108c
SS
18808 break;
18809 case DW_LNS_set_basic_block:
c906108c 18810 break;
c2c6d25f
JM
18811 /* Add to the address register of the state machine the
18812 address increment value corresponding to special opcode
a738430d
MK
18813 255. I.e., this value is scaled by the minimum
18814 instruction length since special opcode 255 would have
b021a221 18815 scaled the increment. */
c906108c 18816 case DW_LNS_const_add_pc:
6f77053d 18817 state_machine.handle_const_add_pc ();
c906108c
SS
18818 break;
18819 case DW_LNS_fixed_advance_pc:
3e29f34a 18820 {
6f77053d 18821 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18822 line_ptr += 2;
6f77053d
PA
18823
18824 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18825 }
c906108c 18826 break;
9aa1fe7e 18827 default:
a738430d
MK
18828 {
18829 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18830 int i;
a738430d 18831
debd256d 18832 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18833 {
18834 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18835 line_ptr += bytes_read;
18836 }
18837 }
c906108c
SS
18838 }
18839 }
d9b3de22
DE
18840
18841 if (!end_sequence)
18842 dwarf2_debug_line_missing_end_sequence_complaint ();
18843
18844 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18845 in which case we still finish recording the last line). */
6f77053d 18846 state_machine.record_line (true);
c906108c 18847 }
f3f5162e
DE
18848}
18849
18850/* Decode the Line Number Program (LNP) for the given line_header
18851 structure and CU. The actual information extracted and the type
18852 of structures created from the LNP depends on the value of PST.
18853
18854 1. If PST is NULL, then this procedure uses the data from the program
18855 to create all necessary symbol tables, and their linetables.
18856
18857 2. If PST is not NULL, this procedure reads the program to determine
18858 the list of files included by the unit represented by PST, and
18859 builds all the associated partial symbol tables.
18860
18861 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18862 It is used for relative paths in the line table.
18863 NOTE: When processing partial symtabs (pst != NULL),
18864 comp_dir == pst->dirname.
18865
18866 NOTE: It is important that psymtabs have the same file name (via strcmp)
18867 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18868 symtab we don't use it in the name of the psymtabs we create.
18869 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18870 A good testcase for this is mb-inline.exp.
18871
527f3840
JK
18872 LOWPC is the lowest address in CU (or 0 if not known).
18873
18874 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18875 for its PC<->lines mapping information. Otherwise only the filename
18876 table is read in. */
f3f5162e
DE
18877
18878static void
18879dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18880 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18881 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18882{
18883 struct objfile *objfile = cu->objfile;
18884 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18885
527f3840
JK
18886 if (decode_mapping)
18887 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18888
18889 if (decode_for_pst_p)
18890 {
18891 int file_index;
18892
18893 /* Now that we're done scanning the Line Header Program, we can
18894 create the psymtab of each included file. */
fff8551c 18895 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18896 if (lh->file_names[file_index].included_p == 1)
18897 {
d521ce57 18898 const char *include_name =
c6da4cef
DE
18899 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18900 if (include_name != NULL)
aaa75496
JB
18901 dwarf2_create_include_psymtab (include_name, pst, objfile);
18902 }
18903 }
cb1df416
DJ
18904 else
18905 {
18906 /* Make sure a symtab is created for every file, even files
18907 which contain only variables (i.e. no code with associated
18908 line numbers). */
43f3e411 18909 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18910 int i;
cb1df416 18911
fff8551c 18912 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18913 {
8c43009f 18914 file_entry &fe = lh->file_names[i];
9a619af0 18915
8c43009f 18916 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18917
cb1df416 18918 if (current_subfile->symtab == NULL)
43f3e411
DE
18919 {
18920 current_subfile->symtab
18921 = allocate_symtab (cust, current_subfile->name);
18922 }
8c43009f 18923 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18924 }
18925 }
c906108c
SS
18926}
18927
18928/* Start a subfile for DWARF. FILENAME is the name of the file and
18929 DIRNAME the name of the source directory which contains FILENAME
4d663531 18930 or NULL if not known.
c906108c
SS
18931 This routine tries to keep line numbers from identical absolute and
18932 relative file names in a common subfile.
18933
18934 Using the `list' example from the GDB testsuite, which resides in
18935 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18936 of /srcdir/list0.c yields the following debugging information for list0.c:
18937
c5aa993b 18938 DW_AT_name: /srcdir/list0.c
4d663531 18939 DW_AT_comp_dir: /compdir
357e46e7 18940 files.files[0].name: list0.h
c5aa993b 18941 files.files[0].dir: /srcdir
357e46e7 18942 files.files[1].name: list0.c
c5aa993b 18943 files.files[1].dir: /srcdir
c906108c
SS
18944
18945 The line number information for list0.c has to end up in a single
4f1520fb
FR
18946 subfile, so that `break /srcdir/list0.c:1' works as expected.
18947 start_subfile will ensure that this happens provided that we pass the
18948 concatenation of files.files[1].dir and files.files[1].name as the
18949 subfile's name. */
c906108c
SS
18950
18951static void
4d663531 18952dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18953{
d521ce57 18954 char *copy = NULL;
4f1520fb 18955
4d663531 18956 /* In order not to lose the line information directory,
4f1520fb
FR
18957 we concatenate it to the filename when it makes sense.
18958 Note that the Dwarf3 standard says (speaking of filenames in line
18959 information): ``The directory index is ignored for file names
18960 that represent full path names''. Thus ignoring dirname in the
18961 `else' branch below isn't an issue. */
c906108c 18962
d5166ae1 18963 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18964 {
18965 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18966 filename = copy;
18967 }
c906108c 18968
4d663531 18969 start_subfile (filename);
4f1520fb 18970
d521ce57
TT
18971 if (copy != NULL)
18972 xfree (copy);
c906108c
SS
18973}
18974
f4dc4d17
DE
18975/* Start a symtab for DWARF.
18976 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18977
43f3e411 18978static struct compunit_symtab *
f4dc4d17 18979dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18980 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18981{
43f3e411
DE
18982 struct compunit_symtab *cust
18983 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18984
f4dc4d17
DE
18985 record_debugformat ("DWARF 2");
18986 record_producer (cu->producer);
18987
18988 /* We assume that we're processing GCC output. */
18989 processing_gcc_compilation = 2;
18990
4d4ec4e5 18991 cu->processing_has_namespace_info = 0;
43f3e411
DE
18992
18993 return cust;
f4dc4d17
DE
18994}
18995
4c2df51b
DJ
18996static void
18997var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18998 struct dwarf2_cu *cu)
4c2df51b 18999{
e7c27a73
DJ
19000 struct objfile *objfile = cu->objfile;
19001 struct comp_unit_head *cu_header = &cu->header;
19002
4c2df51b
DJ
19003 /* NOTE drow/2003-01-30: There used to be a comment and some special
19004 code here to turn a symbol with DW_AT_external and a
19005 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19006 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19007 with some versions of binutils) where shared libraries could have
19008 relocations against symbols in their debug information - the
19009 minimal symbol would have the right address, but the debug info
19010 would not. It's no longer necessary, because we will explicitly
19011 apply relocations when we read in the debug information now. */
19012
19013 /* A DW_AT_location attribute with no contents indicates that a
19014 variable has been optimized away. */
19015 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19016 {
f1e6e072 19017 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19018 return;
19019 }
19020
19021 /* Handle one degenerate form of location expression specially, to
19022 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19023 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19024 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19025
19026 if (attr_form_is_block (attr)
3019eac3
DE
19027 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19028 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19029 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19030 && (DW_BLOCK (attr)->size
19031 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19032 {
891d2f0b 19033 unsigned int dummy;
4c2df51b 19034
3019eac3
DE
19035 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19036 SYMBOL_VALUE_ADDRESS (sym) =
19037 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19038 else
19039 SYMBOL_VALUE_ADDRESS (sym) =
19040 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19041 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19042 fixup_symbol_section (sym, objfile);
19043 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19044 SYMBOL_SECTION (sym));
4c2df51b
DJ
19045 return;
19046 }
19047
19048 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19049 expression evaluator, and use LOC_COMPUTED only when necessary
19050 (i.e. when the value of a register or memory location is
19051 referenced, or a thread-local block, etc.). Then again, it might
19052 not be worthwhile. I'm assuming that it isn't unless performance
19053 or memory numbers show me otherwise. */
19054
f1e6e072 19055 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19056
f1e6e072 19057 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19058 cu->has_loclist = 1;
4c2df51b
DJ
19059}
19060
c906108c
SS
19061/* Given a pointer to a DWARF information entry, figure out if we need
19062 to make a symbol table entry for it, and if so, create a new entry
19063 and return a pointer to it.
19064 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19065 used the passed type.
19066 If SPACE is not NULL, use it to hold the new symbol. If it is
19067 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19068
19069static struct symbol *
34eaf542
TT
19070new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19071 struct symbol *space)
c906108c 19072{
e7c27a73 19073 struct objfile *objfile = cu->objfile;
3e29f34a 19074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19075 struct symbol *sym = NULL;
15d034d0 19076 const char *name;
c906108c
SS
19077 struct attribute *attr = NULL;
19078 struct attribute *attr2 = NULL;
e142c38c 19079 CORE_ADDR baseaddr;
e37fd15a
SW
19080 struct pending **list_to_add = NULL;
19081
edb3359d 19082 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19083
19084 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19085
94af9270 19086 name = dwarf2_name (die, cu);
c906108c
SS
19087 if (name)
19088 {
94af9270 19089 const char *linkagename;
34eaf542 19090 int suppress_add = 0;
94af9270 19091
34eaf542
TT
19092 if (space)
19093 sym = space;
19094 else
e623cf5d 19095 sym = allocate_symbol (objfile);
c906108c 19096 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19097
19098 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19099 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19100 linkagename = dwarf2_physname (name, die, cu);
19101 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19102
f55ee35c
JK
19103 /* Fortran does not have mangling standard and the mangling does differ
19104 between gfortran, iFort etc. */
19105 if (cu->language == language_fortran
b250c185 19106 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19107 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19108 dwarf2_full_name (name, die, cu),
29df156d 19109 NULL);
f55ee35c 19110
c906108c 19111 /* Default assumptions.
c5aa993b 19112 Use the passed type or decode it from the die. */
176620f1 19113 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19114 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19115 if (type != NULL)
19116 SYMBOL_TYPE (sym) = type;
19117 else
e7c27a73 19118 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19119 attr = dwarf2_attr (die,
19120 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19121 cu);
c906108c
SS
19122 if (attr)
19123 {
19124 SYMBOL_LINE (sym) = DW_UNSND (attr);
19125 }
cb1df416 19126
edb3359d
DJ
19127 attr = dwarf2_attr (die,
19128 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19129 cu);
cb1df416
DJ
19130 if (attr)
19131 {
ecfb656c 19132 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19133 struct file_entry *fe;
9a619af0 19134
ecfb656c
PA
19135 if (cu->line_header != NULL)
19136 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19137 else
19138 fe = NULL;
19139
19140 if (fe == NULL)
cb1df416
DJ
19141 complaint (&symfile_complaints,
19142 _("file index out of range"));
8c43009f
PA
19143 else
19144 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19145 }
19146
c906108c
SS
19147 switch (die->tag)
19148 {
19149 case DW_TAG_label:
e142c38c 19150 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19151 if (attr)
3e29f34a
MR
19152 {
19153 CORE_ADDR addr;
19154
19155 addr = attr_value_as_address (attr);
19156 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19157 SYMBOL_VALUE_ADDRESS (sym) = addr;
19158 }
0f5238ed
TT
19159 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19160 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19161 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19162 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19163 break;
19164 case DW_TAG_subprogram:
19165 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19166 finish_block. */
f1e6e072 19167 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19168 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19169 if ((attr2 && (DW_UNSND (attr2) != 0))
19170 || cu->language == language_ada)
c906108c 19171 {
2cfa0c8d
JB
19172 /* Subprograms marked external are stored as a global symbol.
19173 Ada subprograms, whether marked external or not, are always
19174 stored as a global symbol, because we want to be able to
19175 access them globally. For instance, we want to be able
19176 to break on a nested subprogram without having to
19177 specify the context. */
e37fd15a 19178 list_to_add = &global_symbols;
c906108c
SS
19179 }
19180 else
19181 {
e37fd15a 19182 list_to_add = cu->list_in_scope;
c906108c
SS
19183 }
19184 break;
edb3359d
DJ
19185 case DW_TAG_inlined_subroutine:
19186 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19187 finish_block. */
f1e6e072 19188 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19189 SYMBOL_INLINED (sym) = 1;
481860b3 19190 list_to_add = cu->list_in_scope;
edb3359d 19191 break;
34eaf542
TT
19192 case DW_TAG_template_value_param:
19193 suppress_add = 1;
19194 /* Fall through. */
72929c62 19195 case DW_TAG_constant:
c906108c 19196 case DW_TAG_variable:
254e6b9e 19197 case DW_TAG_member:
0963b4bd
MS
19198 /* Compilation with minimal debug info may result in
19199 variables with missing type entries. Change the
19200 misleading `void' type to something sensible. */
c906108c 19201 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19202 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19203
e142c38c 19204 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19205 /* In the case of DW_TAG_member, we should only be called for
19206 static const members. */
19207 if (die->tag == DW_TAG_member)
19208 {
3863f96c
DE
19209 /* dwarf2_add_field uses die_is_declaration,
19210 so we do the same. */
254e6b9e
DE
19211 gdb_assert (die_is_declaration (die, cu));
19212 gdb_assert (attr);
19213 }
c906108c
SS
19214 if (attr)
19215 {
e7c27a73 19216 dwarf2_const_value (attr, sym, cu);
e142c38c 19217 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19218 if (!suppress_add)
34eaf542
TT
19219 {
19220 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19221 list_to_add = &global_symbols;
34eaf542 19222 else
e37fd15a 19223 list_to_add = cu->list_in_scope;
34eaf542 19224 }
c906108c
SS
19225 break;
19226 }
e142c38c 19227 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19228 if (attr)
19229 {
e7c27a73 19230 var_decode_location (attr, sym, cu);
e142c38c 19231 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19232
19233 /* Fortran explicitly imports any global symbols to the local
19234 scope by DW_TAG_common_block. */
19235 if (cu->language == language_fortran && die->parent
19236 && die->parent->tag == DW_TAG_common_block)
19237 attr2 = NULL;
19238
caac4577
JG
19239 if (SYMBOL_CLASS (sym) == LOC_STATIC
19240 && SYMBOL_VALUE_ADDRESS (sym) == 0
19241 && !dwarf2_per_objfile->has_section_at_zero)
19242 {
19243 /* When a static variable is eliminated by the linker,
19244 the corresponding debug information is not stripped
19245 out, but the variable address is set to null;
19246 do not add such variables into symbol table. */
19247 }
19248 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19249 {
f55ee35c
JK
19250 /* Workaround gfortran PR debug/40040 - it uses
19251 DW_AT_location for variables in -fPIC libraries which may
19252 get overriden by other libraries/executable and get
19253 a different address. Resolve it by the minimal symbol
19254 which may come from inferior's executable using copy
19255 relocation. Make this workaround only for gfortran as for
19256 other compilers GDB cannot guess the minimal symbol
19257 Fortran mangling kind. */
19258 if (cu->language == language_fortran && die->parent
19259 && die->parent->tag == DW_TAG_module
19260 && cu->producer
28586665 19261 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19262 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19263
1c809c68
TT
19264 /* A variable with DW_AT_external is never static,
19265 but it may be block-scoped. */
19266 list_to_add = (cu->list_in_scope == &file_symbols
19267 ? &global_symbols : cu->list_in_scope);
1c809c68 19268 }
c906108c 19269 else
e37fd15a 19270 list_to_add = cu->list_in_scope;
c906108c
SS
19271 }
19272 else
19273 {
19274 /* We do not know the address of this symbol.
c5aa993b
JM
19275 If it is an external symbol and we have type information
19276 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19277 The address of the variable will then be determined from
19278 the minimal symbol table whenever the variable is
19279 referenced. */
e142c38c 19280 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19281
19282 /* Fortran explicitly imports any global symbols to the local
19283 scope by DW_TAG_common_block. */
19284 if (cu->language == language_fortran && die->parent
19285 && die->parent->tag == DW_TAG_common_block)
19286 {
19287 /* SYMBOL_CLASS doesn't matter here because
19288 read_common_block is going to reset it. */
19289 if (!suppress_add)
19290 list_to_add = cu->list_in_scope;
19291 }
19292 else if (attr2 && (DW_UNSND (attr2) != 0)
19293 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19294 {
0fe7935b
DJ
19295 /* A variable with DW_AT_external is never static, but it
19296 may be block-scoped. */
19297 list_to_add = (cu->list_in_scope == &file_symbols
19298 ? &global_symbols : cu->list_in_scope);
19299
f1e6e072 19300 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19301 }
442ddf59
JK
19302 else if (!die_is_declaration (die, cu))
19303 {
19304 /* Use the default LOC_OPTIMIZED_OUT class. */
19305 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19306 if (!suppress_add)
19307 list_to_add = cu->list_in_scope;
442ddf59 19308 }
c906108c
SS
19309 }
19310 break;
19311 case DW_TAG_formal_parameter:
edb3359d
DJ
19312 /* If we are inside a function, mark this as an argument. If
19313 not, we might be looking at an argument to an inlined function
19314 when we do not have enough information to show inlined frames;
19315 pretend it's a local variable in that case so that the user can
19316 still see it. */
19317 if (context_stack_depth > 0
19318 && context_stack[context_stack_depth - 1].name != NULL)
19319 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19320 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19321 if (attr)
19322 {
e7c27a73 19323 var_decode_location (attr, sym, cu);
c906108c 19324 }
e142c38c 19325 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19326 if (attr)
19327 {
e7c27a73 19328 dwarf2_const_value (attr, sym, cu);
c906108c 19329 }
f346a30d 19330
e37fd15a 19331 list_to_add = cu->list_in_scope;
c906108c
SS
19332 break;
19333 case DW_TAG_unspecified_parameters:
19334 /* From varargs functions; gdb doesn't seem to have any
19335 interest in this information, so just ignore it for now.
19336 (FIXME?) */
19337 break;
34eaf542
TT
19338 case DW_TAG_template_type_param:
19339 suppress_add = 1;
19340 /* Fall through. */
c906108c 19341 case DW_TAG_class_type:
680b30c7 19342 case DW_TAG_interface_type:
c906108c
SS
19343 case DW_TAG_structure_type:
19344 case DW_TAG_union_type:
72019c9c 19345 case DW_TAG_set_type:
c906108c 19346 case DW_TAG_enumeration_type:
f1e6e072 19347 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19348 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19349
63d06c5c 19350 {
9c37b5ae 19351 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19352 really ever be static objects: otherwise, if you try
19353 to, say, break of a class's method and you're in a file
19354 which doesn't mention that class, it won't work unless
19355 the check for all static symbols in lookup_symbol_aux
19356 saves you. See the OtherFileClass tests in
19357 gdb.c++/namespace.exp. */
19358
e37fd15a 19359 if (!suppress_add)
34eaf542 19360 {
34eaf542 19361 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19362 && cu->language == language_cplus
34eaf542 19363 ? &global_symbols : cu->list_in_scope);
63d06c5c 19364
64382290 19365 /* The semantics of C++ state that "struct foo {
9c37b5ae 19366 ... }" also defines a typedef for "foo". */
64382290 19367 if (cu->language == language_cplus
45280282 19368 || cu->language == language_ada
c44af4eb
TT
19369 || cu->language == language_d
19370 || cu->language == language_rust)
64382290
TT
19371 {
19372 /* The symbol's name is already allocated along
19373 with this objfile, so we don't need to
19374 duplicate it for the type. */
19375 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19376 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19377 }
63d06c5c
DC
19378 }
19379 }
c906108c
SS
19380 break;
19381 case DW_TAG_typedef:
f1e6e072 19382 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19383 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19384 list_to_add = cu->list_in_scope;
63d06c5c 19385 break;
c906108c 19386 case DW_TAG_base_type:
a02abb62 19387 case DW_TAG_subrange_type:
f1e6e072 19388 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19389 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19390 list_to_add = cu->list_in_scope;
c906108c
SS
19391 break;
19392 case DW_TAG_enumerator:
e142c38c 19393 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19394 if (attr)
19395 {
e7c27a73 19396 dwarf2_const_value (attr, sym, cu);
c906108c 19397 }
63d06c5c
DC
19398 {
19399 /* NOTE: carlton/2003-11-10: See comment above in the
19400 DW_TAG_class_type, etc. block. */
19401
e142c38c 19402 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19403 && cu->language == language_cplus
e142c38c 19404 ? &global_symbols : cu->list_in_scope);
63d06c5c 19405 }
c906108c 19406 break;
74921315 19407 case DW_TAG_imported_declaration:
5c4e30ca 19408 case DW_TAG_namespace:
f1e6e072 19409 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19410 list_to_add = &global_symbols;
5c4e30ca 19411 break;
530e8392
KB
19412 case DW_TAG_module:
19413 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19414 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19415 list_to_add = &global_symbols;
19416 break;
4357ac6c 19417 case DW_TAG_common_block:
f1e6e072 19418 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19419 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19420 add_symbol_to_list (sym, cu->list_in_scope);
19421 break;
c906108c
SS
19422 default:
19423 /* Not a tag we recognize. Hopefully we aren't processing
19424 trash data, but since we must specifically ignore things
19425 we don't recognize, there is nothing else we should do at
0963b4bd 19426 this point. */
e2e0b3e5 19427 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19428 dwarf_tag_name (die->tag));
c906108c
SS
19429 break;
19430 }
df8a16a1 19431
e37fd15a
SW
19432 if (suppress_add)
19433 {
19434 sym->hash_next = objfile->template_symbols;
19435 objfile->template_symbols = sym;
19436 list_to_add = NULL;
19437 }
19438
19439 if (list_to_add != NULL)
19440 add_symbol_to_list (sym, list_to_add);
19441
df8a16a1
DJ
19442 /* For the benefit of old versions of GCC, check for anonymous
19443 namespaces based on the demangled name. */
4d4ec4e5 19444 if (!cu->processing_has_namespace_info
94af9270 19445 && cu->language == language_cplus)
a10964d1 19446 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19447 }
19448 return (sym);
19449}
19450
34eaf542
TT
19451/* A wrapper for new_symbol_full that always allocates a new symbol. */
19452
19453static struct symbol *
19454new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19455{
19456 return new_symbol_full (die, type, cu, NULL);
19457}
19458
98bfdba5
PA
19459/* Given an attr with a DW_FORM_dataN value in host byte order,
19460 zero-extend it as appropriate for the symbol's type. The DWARF
19461 standard (v4) is not entirely clear about the meaning of using
19462 DW_FORM_dataN for a constant with a signed type, where the type is
19463 wider than the data. The conclusion of a discussion on the DWARF
19464 list was that this is unspecified. We choose to always zero-extend
19465 because that is the interpretation long in use by GCC. */
c906108c 19466
98bfdba5 19467static gdb_byte *
ff39bb5e 19468dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19469 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19470{
e7c27a73 19471 struct objfile *objfile = cu->objfile;
e17a4113
UW
19472 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19473 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19474 LONGEST l = DW_UNSND (attr);
19475
19476 if (bits < sizeof (*value) * 8)
19477 {
19478 l &= ((LONGEST) 1 << bits) - 1;
19479 *value = l;
19480 }
19481 else if (bits == sizeof (*value) * 8)
19482 *value = l;
19483 else
19484 {
224c3ddb 19485 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19486 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19487 return bytes;
19488 }
19489
19490 return NULL;
19491}
19492
19493/* Read a constant value from an attribute. Either set *VALUE, or if
19494 the value does not fit in *VALUE, set *BYTES - either already
19495 allocated on the objfile obstack, or newly allocated on OBSTACK,
19496 or, set *BATON, if we translated the constant to a location
19497 expression. */
19498
19499static void
ff39bb5e 19500dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19501 const char *name, struct obstack *obstack,
19502 struct dwarf2_cu *cu,
d521ce57 19503 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19504 struct dwarf2_locexpr_baton **baton)
19505{
19506 struct objfile *objfile = cu->objfile;
19507 struct comp_unit_head *cu_header = &cu->header;
c906108c 19508 struct dwarf_block *blk;
98bfdba5
PA
19509 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19510 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19511
19512 *value = 0;
19513 *bytes = NULL;
19514 *baton = NULL;
c906108c
SS
19515
19516 switch (attr->form)
19517 {
19518 case DW_FORM_addr:
3019eac3 19519 case DW_FORM_GNU_addr_index:
ac56253d 19520 {
ac56253d
TT
19521 gdb_byte *data;
19522
98bfdba5
PA
19523 if (TYPE_LENGTH (type) != cu_header->addr_size)
19524 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19525 cu_header->addr_size,
98bfdba5 19526 TYPE_LENGTH (type));
ac56253d
TT
19527 /* Symbols of this form are reasonably rare, so we just
19528 piggyback on the existing location code rather than writing
19529 a new implementation of symbol_computed_ops. */
8d749320 19530 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19531 (*baton)->per_cu = cu->per_cu;
19532 gdb_assert ((*baton)->per_cu);
ac56253d 19533
98bfdba5 19534 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19535 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19536 (*baton)->data = data;
ac56253d
TT
19537
19538 data[0] = DW_OP_addr;
19539 store_unsigned_integer (&data[1], cu_header->addr_size,
19540 byte_order, DW_ADDR (attr));
19541 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19542 }
c906108c 19543 break;
4ac36638 19544 case DW_FORM_string:
93b5768b 19545 case DW_FORM_strp:
3019eac3 19546 case DW_FORM_GNU_str_index:
36586728 19547 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19548 /* DW_STRING is already allocated on the objfile obstack, point
19549 directly to it. */
d521ce57 19550 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19551 break;
c906108c
SS
19552 case DW_FORM_block1:
19553 case DW_FORM_block2:
19554 case DW_FORM_block4:
19555 case DW_FORM_block:
2dc7f7b3 19556 case DW_FORM_exprloc:
0224619f 19557 case DW_FORM_data16:
c906108c 19558 blk = DW_BLOCK (attr);
98bfdba5
PA
19559 if (TYPE_LENGTH (type) != blk->size)
19560 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19561 TYPE_LENGTH (type));
19562 *bytes = blk->data;
c906108c 19563 break;
2df3850c
JM
19564
19565 /* The DW_AT_const_value attributes are supposed to carry the
19566 symbol's value "represented as it would be on the target
19567 architecture." By the time we get here, it's already been
19568 converted to host endianness, so we just need to sign- or
19569 zero-extend it as appropriate. */
19570 case DW_FORM_data1:
3aef2284 19571 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19572 break;
c906108c 19573 case DW_FORM_data2:
3aef2284 19574 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19575 break;
c906108c 19576 case DW_FORM_data4:
3aef2284 19577 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19578 break;
c906108c 19579 case DW_FORM_data8:
3aef2284 19580 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19581 break;
19582
c906108c 19583 case DW_FORM_sdata:
663c44ac 19584 case DW_FORM_implicit_const:
98bfdba5 19585 *value = DW_SND (attr);
2df3850c
JM
19586 break;
19587
c906108c 19588 case DW_FORM_udata:
98bfdba5 19589 *value = DW_UNSND (attr);
c906108c 19590 break;
2df3850c 19591
c906108c 19592 default:
4d3c2250 19593 complaint (&symfile_complaints,
e2e0b3e5 19594 _("unsupported const value attribute form: '%s'"),
4d3c2250 19595 dwarf_form_name (attr->form));
98bfdba5 19596 *value = 0;
c906108c
SS
19597 break;
19598 }
19599}
19600
2df3850c 19601
98bfdba5
PA
19602/* Copy constant value from an attribute to a symbol. */
19603
2df3850c 19604static void
ff39bb5e 19605dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19606 struct dwarf2_cu *cu)
2df3850c 19607{
98bfdba5 19608 struct objfile *objfile = cu->objfile;
12df843f 19609 LONGEST value;
d521ce57 19610 const gdb_byte *bytes;
98bfdba5 19611 struct dwarf2_locexpr_baton *baton;
2df3850c 19612
98bfdba5
PA
19613 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19614 SYMBOL_PRINT_NAME (sym),
19615 &objfile->objfile_obstack, cu,
19616 &value, &bytes, &baton);
2df3850c 19617
98bfdba5
PA
19618 if (baton != NULL)
19619 {
98bfdba5 19620 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19621 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19622 }
19623 else if (bytes != NULL)
19624 {
19625 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19626 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19627 }
19628 else
19629 {
19630 SYMBOL_VALUE (sym) = value;
f1e6e072 19631 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19632 }
2df3850c
JM
19633}
19634
c906108c
SS
19635/* Return the type of the die in question using its DW_AT_type attribute. */
19636
19637static struct type *
e7c27a73 19638die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19639{
c906108c 19640 struct attribute *type_attr;
c906108c 19641
e142c38c 19642 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19643 if (!type_attr)
19644 {
19645 /* A missing DW_AT_type represents a void type. */
46bf5051 19646 return objfile_type (cu->objfile)->builtin_void;
c906108c 19647 }
348e048f 19648
673bfd45 19649 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19650}
19651
b4ba55a1
JB
19652/* True iff CU's producer generates GNAT Ada auxiliary information
19653 that allows to find parallel types through that information instead
19654 of having to do expensive parallel lookups by type name. */
19655
19656static int
19657need_gnat_info (struct dwarf2_cu *cu)
19658{
19659 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19660 of GNAT produces this auxiliary information, without any indication
19661 that it is produced. Part of enhancing the FSF version of GNAT
19662 to produce that information will be to put in place an indicator
19663 that we can use in order to determine whether the descriptive type
19664 info is available or not. One suggestion that has been made is
19665 to use a new attribute, attached to the CU die. For now, assume
19666 that the descriptive type info is not available. */
19667 return 0;
19668}
19669
b4ba55a1
JB
19670/* Return the auxiliary type of the die in question using its
19671 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19672 attribute is not present. */
19673
19674static struct type *
19675die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19676{
b4ba55a1 19677 struct attribute *type_attr;
b4ba55a1
JB
19678
19679 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19680 if (!type_attr)
19681 return NULL;
19682
673bfd45 19683 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19684}
19685
19686/* If DIE has a descriptive_type attribute, then set the TYPE's
19687 descriptive type accordingly. */
19688
19689static void
19690set_descriptive_type (struct type *type, struct die_info *die,
19691 struct dwarf2_cu *cu)
19692{
19693 struct type *descriptive_type = die_descriptive_type (die, cu);
19694
19695 if (descriptive_type)
19696 {
19697 ALLOCATE_GNAT_AUX_TYPE (type);
19698 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19699 }
19700}
19701
c906108c
SS
19702/* Return the containing type of the die in question using its
19703 DW_AT_containing_type attribute. */
19704
19705static struct type *
e7c27a73 19706die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19707{
c906108c 19708 struct attribute *type_attr;
c906108c 19709
e142c38c 19710 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19711 if (!type_attr)
19712 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19713 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19714
673bfd45 19715 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19716}
19717
ac9ec31b
DE
19718/* Return an error marker type to use for the ill formed type in DIE/CU. */
19719
19720static struct type *
19721build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19722{
19723 struct objfile *objfile = dwarf2_per_objfile->objfile;
19724 char *message, *saved;
19725
19726 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19727 objfile_name (objfile),
9c541725
PA
19728 to_underlying (cu->header.sect_off),
19729 to_underlying (die->sect_off));
224c3ddb
SM
19730 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19731 message, strlen (message));
ac9ec31b
DE
19732 xfree (message);
19733
19f392bc 19734 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19735}
19736
673bfd45 19737/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19738 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19739 DW_AT_containing_type.
673bfd45
DE
19740 If there is no type substitute an error marker. */
19741
c906108c 19742static struct type *
ff39bb5e 19743lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19744 struct dwarf2_cu *cu)
c906108c 19745{
bb5ed363 19746 struct objfile *objfile = cu->objfile;
f792889a
DJ
19747 struct type *this_type;
19748
ac9ec31b
DE
19749 gdb_assert (attr->name == DW_AT_type
19750 || attr->name == DW_AT_GNAT_descriptive_type
19751 || attr->name == DW_AT_containing_type);
19752
673bfd45
DE
19753 /* First see if we have it cached. */
19754
36586728
TT
19755 if (attr->form == DW_FORM_GNU_ref_alt)
19756 {
19757 struct dwarf2_per_cu_data *per_cu;
9c541725 19758 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19759
9c541725
PA
19760 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19761 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19762 }
7771576e 19763 else if (attr_form_is_ref (attr))
673bfd45 19764 {
9c541725 19765 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19766
9c541725 19767 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19768 }
55f1336d 19769 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19770 {
ac9ec31b 19771 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19772
ac9ec31b 19773 return get_signatured_type (die, signature, cu);
673bfd45
DE
19774 }
19775 else
19776 {
ac9ec31b
DE
19777 complaint (&symfile_complaints,
19778 _("Dwarf Error: Bad type attribute %s in DIE"
19779 " at 0x%x [in module %s]"),
9c541725 19780 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19781 objfile_name (objfile));
ac9ec31b 19782 return build_error_marker_type (cu, die);
673bfd45
DE
19783 }
19784
19785 /* If not cached we need to read it in. */
19786
19787 if (this_type == NULL)
19788 {
ac9ec31b 19789 struct die_info *type_die = NULL;
673bfd45
DE
19790 struct dwarf2_cu *type_cu = cu;
19791
7771576e 19792 if (attr_form_is_ref (attr))
ac9ec31b
DE
19793 type_die = follow_die_ref (die, attr, &type_cu);
19794 if (type_die == NULL)
19795 return build_error_marker_type (cu, die);
19796 /* If we find the type now, it's probably because the type came
3019eac3
DE
19797 from an inter-CU reference and the type's CU got expanded before
19798 ours. */
ac9ec31b 19799 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19800 }
19801
19802 /* If we still don't have a type use an error marker. */
19803
19804 if (this_type == NULL)
ac9ec31b 19805 return build_error_marker_type (cu, die);
673bfd45 19806
f792889a 19807 return this_type;
c906108c
SS
19808}
19809
673bfd45
DE
19810/* Return the type in DIE, CU.
19811 Returns NULL for invalid types.
19812
02142a6c 19813 This first does a lookup in die_type_hash,
673bfd45
DE
19814 and only reads the die in if necessary.
19815
19816 NOTE: This can be called when reading in partial or full symbols. */
19817
f792889a 19818static struct type *
e7c27a73 19819read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19820{
f792889a
DJ
19821 struct type *this_type;
19822
19823 this_type = get_die_type (die, cu);
19824 if (this_type)
19825 return this_type;
19826
673bfd45
DE
19827 return read_type_die_1 (die, cu);
19828}
19829
19830/* Read the type in DIE, CU.
19831 Returns NULL for invalid types. */
19832
19833static struct type *
19834read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19835{
19836 struct type *this_type = NULL;
19837
c906108c
SS
19838 switch (die->tag)
19839 {
19840 case DW_TAG_class_type:
680b30c7 19841 case DW_TAG_interface_type:
c906108c
SS
19842 case DW_TAG_structure_type:
19843 case DW_TAG_union_type:
f792889a 19844 this_type = read_structure_type (die, cu);
c906108c
SS
19845 break;
19846 case DW_TAG_enumeration_type:
f792889a 19847 this_type = read_enumeration_type (die, cu);
c906108c
SS
19848 break;
19849 case DW_TAG_subprogram:
19850 case DW_TAG_subroutine_type:
edb3359d 19851 case DW_TAG_inlined_subroutine:
f792889a 19852 this_type = read_subroutine_type (die, cu);
c906108c
SS
19853 break;
19854 case DW_TAG_array_type:
f792889a 19855 this_type = read_array_type (die, cu);
c906108c 19856 break;
72019c9c 19857 case DW_TAG_set_type:
f792889a 19858 this_type = read_set_type (die, cu);
72019c9c 19859 break;
c906108c 19860 case DW_TAG_pointer_type:
f792889a 19861 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19862 break;
19863 case DW_TAG_ptr_to_member_type:
f792889a 19864 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19865 break;
19866 case DW_TAG_reference_type:
4297a3f0
AV
19867 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19868 break;
19869 case DW_TAG_rvalue_reference_type:
19870 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19871 break;
19872 case DW_TAG_const_type:
f792889a 19873 this_type = read_tag_const_type (die, cu);
c906108c
SS
19874 break;
19875 case DW_TAG_volatile_type:
f792889a 19876 this_type = read_tag_volatile_type (die, cu);
c906108c 19877 break;
06d66ee9
TT
19878 case DW_TAG_restrict_type:
19879 this_type = read_tag_restrict_type (die, cu);
19880 break;
c906108c 19881 case DW_TAG_string_type:
f792889a 19882 this_type = read_tag_string_type (die, cu);
c906108c
SS
19883 break;
19884 case DW_TAG_typedef:
f792889a 19885 this_type = read_typedef (die, cu);
c906108c 19886 break;
a02abb62 19887 case DW_TAG_subrange_type:
f792889a 19888 this_type = read_subrange_type (die, cu);
a02abb62 19889 break;
c906108c 19890 case DW_TAG_base_type:
f792889a 19891 this_type = read_base_type (die, cu);
c906108c 19892 break;
81a17f79 19893 case DW_TAG_unspecified_type:
f792889a 19894 this_type = read_unspecified_type (die, cu);
81a17f79 19895 break;
0114d602
DJ
19896 case DW_TAG_namespace:
19897 this_type = read_namespace_type (die, cu);
19898 break;
f55ee35c
JK
19899 case DW_TAG_module:
19900 this_type = read_module_type (die, cu);
19901 break;
a2c2acaf
MW
19902 case DW_TAG_atomic_type:
19903 this_type = read_tag_atomic_type (die, cu);
19904 break;
c906108c 19905 default:
3e43a32a
MS
19906 complaint (&symfile_complaints,
19907 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19908 dwarf_tag_name (die->tag));
c906108c
SS
19909 break;
19910 }
63d06c5c 19911
f792889a 19912 return this_type;
63d06c5c
DC
19913}
19914
abc72ce4
DE
19915/* See if we can figure out if the class lives in a namespace. We do
19916 this by looking for a member function; its demangled name will
19917 contain namespace info, if there is any.
19918 Return the computed name or NULL.
19919 Space for the result is allocated on the objfile's obstack.
19920 This is the full-die version of guess_partial_die_structure_name.
19921 In this case we know DIE has no useful parent. */
19922
19923static char *
19924guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19925{
19926 struct die_info *spec_die;
19927 struct dwarf2_cu *spec_cu;
19928 struct die_info *child;
19929
19930 spec_cu = cu;
19931 spec_die = die_specification (die, &spec_cu);
19932 if (spec_die != NULL)
19933 {
19934 die = spec_die;
19935 cu = spec_cu;
19936 }
19937
19938 for (child = die->child;
19939 child != NULL;
19940 child = child->sibling)
19941 {
19942 if (child->tag == DW_TAG_subprogram)
19943 {
73b9be8b 19944 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 19945
7d45c7c3 19946 if (linkage_name != NULL)
abc72ce4
DE
19947 {
19948 char *actual_name
19949 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19950 linkage_name);
abc72ce4
DE
19951 char *name = NULL;
19952
19953 if (actual_name != NULL)
19954 {
15d034d0 19955 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19956
19957 if (die_name != NULL
19958 && strcmp (die_name, actual_name) != 0)
19959 {
19960 /* Strip off the class name from the full name.
19961 We want the prefix. */
19962 int die_name_len = strlen (die_name);
19963 int actual_name_len = strlen (actual_name);
19964
19965 /* Test for '::' as a sanity check. */
19966 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19967 && actual_name[actual_name_len
19968 - die_name_len - 1] == ':')
224c3ddb
SM
19969 name = (char *) obstack_copy0 (
19970 &cu->objfile->per_bfd->storage_obstack,
19971 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19972 }
19973 }
19974 xfree (actual_name);
19975 return name;
19976 }
19977 }
19978 }
19979
19980 return NULL;
19981}
19982
96408a79
SA
19983/* GCC might emit a nameless typedef that has a linkage name. Determine the
19984 prefix part in such case. See
19985 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19986
a121b7c1 19987static const char *
96408a79
SA
19988anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19989{
19990 struct attribute *attr;
e6a959d6 19991 const char *base;
96408a79
SA
19992
19993 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19994 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19995 return NULL;
19996
7d45c7c3 19997 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19998 return NULL;
19999
73b9be8b 20000 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20001 if (attr == NULL || DW_STRING (attr) == NULL)
20002 return NULL;
20003
20004 /* dwarf2_name had to be already called. */
20005 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20006
20007 /* Strip the base name, keep any leading namespaces/classes. */
20008 base = strrchr (DW_STRING (attr), ':');
20009 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20010 return "";
20011
224c3ddb
SM
20012 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20013 DW_STRING (attr),
20014 &base[-1] - DW_STRING (attr));
96408a79
SA
20015}
20016
fdde2d81 20017/* Return the name of the namespace/class that DIE is defined within,
0114d602 20018 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20019
0114d602
DJ
20020 For example, if we're within the method foo() in the following
20021 code:
20022
20023 namespace N {
20024 class C {
20025 void foo () {
20026 }
20027 };
20028 }
20029
20030 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20031
0d5cff50 20032static const char *
e142c38c 20033determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20034{
0114d602
DJ
20035 struct die_info *parent, *spec_die;
20036 struct dwarf2_cu *spec_cu;
20037 struct type *parent_type;
a121b7c1 20038 const char *retval;
63d06c5c 20039
9c37b5ae 20040 if (cu->language != language_cplus
c44af4eb
TT
20041 && cu->language != language_fortran && cu->language != language_d
20042 && cu->language != language_rust)
0114d602
DJ
20043 return "";
20044
96408a79
SA
20045 retval = anonymous_struct_prefix (die, cu);
20046 if (retval)
20047 return retval;
20048
0114d602
DJ
20049 /* We have to be careful in the presence of DW_AT_specification.
20050 For example, with GCC 3.4, given the code
20051
20052 namespace N {
20053 void foo() {
20054 // Definition of N::foo.
20055 }
20056 }
20057
20058 then we'll have a tree of DIEs like this:
20059
20060 1: DW_TAG_compile_unit
20061 2: DW_TAG_namespace // N
20062 3: DW_TAG_subprogram // declaration of N::foo
20063 4: DW_TAG_subprogram // definition of N::foo
20064 DW_AT_specification // refers to die #3
20065
20066 Thus, when processing die #4, we have to pretend that we're in
20067 the context of its DW_AT_specification, namely the contex of die
20068 #3. */
20069 spec_cu = cu;
20070 spec_die = die_specification (die, &spec_cu);
20071 if (spec_die == NULL)
20072 parent = die->parent;
20073 else
63d06c5c 20074 {
0114d602
DJ
20075 parent = spec_die->parent;
20076 cu = spec_cu;
63d06c5c 20077 }
0114d602
DJ
20078
20079 if (parent == NULL)
20080 return "";
98bfdba5
PA
20081 else if (parent->building_fullname)
20082 {
20083 const char *name;
20084 const char *parent_name;
20085
20086 /* It has been seen on RealView 2.2 built binaries,
20087 DW_TAG_template_type_param types actually _defined_ as
20088 children of the parent class:
20089
20090 enum E {};
20091 template class <class Enum> Class{};
20092 Class<enum E> class_e;
20093
20094 1: DW_TAG_class_type (Class)
20095 2: DW_TAG_enumeration_type (E)
20096 3: DW_TAG_enumerator (enum1:0)
20097 3: DW_TAG_enumerator (enum2:1)
20098 ...
20099 2: DW_TAG_template_type_param
20100 DW_AT_type DW_FORM_ref_udata (E)
20101
20102 Besides being broken debug info, it can put GDB into an
20103 infinite loop. Consider:
20104
20105 When we're building the full name for Class<E>, we'll start
20106 at Class, and go look over its template type parameters,
20107 finding E. We'll then try to build the full name of E, and
20108 reach here. We're now trying to build the full name of E,
20109 and look over the parent DIE for containing scope. In the
20110 broken case, if we followed the parent DIE of E, we'd again
20111 find Class, and once again go look at its template type
20112 arguments, etc., etc. Simply don't consider such parent die
20113 as source-level parent of this die (it can't be, the language
20114 doesn't allow it), and break the loop here. */
20115 name = dwarf2_name (die, cu);
20116 parent_name = dwarf2_name (parent, cu);
20117 complaint (&symfile_complaints,
20118 _("template param type '%s' defined within parent '%s'"),
20119 name ? name : "<unknown>",
20120 parent_name ? parent_name : "<unknown>");
20121 return "";
20122 }
63d06c5c 20123 else
0114d602
DJ
20124 switch (parent->tag)
20125 {
63d06c5c 20126 case DW_TAG_namespace:
0114d602 20127 parent_type = read_type_die (parent, cu);
acebe513
UW
20128 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20129 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20130 Work around this problem here. */
20131 if (cu->language == language_cplus
20132 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20133 return "";
0114d602
DJ
20134 /* We give a name to even anonymous namespaces. */
20135 return TYPE_TAG_NAME (parent_type);
63d06c5c 20136 case DW_TAG_class_type:
680b30c7 20137 case DW_TAG_interface_type:
63d06c5c 20138 case DW_TAG_structure_type:
0114d602 20139 case DW_TAG_union_type:
f55ee35c 20140 case DW_TAG_module:
0114d602
DJ
20141 parent_type = read_type_die (parent, cu);
20142 if (TYPE_TAG_NAME (parent_type) != NULL)
20143 return TYPE_TAG_NAME (parent_type);
20144 else
20145 /* An anonymous structure is only allowed non-static data
20146 members; no typedefs, no member functions, et cetera.
20147 So it does not need a prefix. */
20148 return "";
abc72ce4 20149 case DW_TAG_compile_unit:
95554aad 20150 case DW_TAG_partial_unit:
abc72ce4
DE
20151 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20152 if (cu->language == language_cplus
8b70b953 20153 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20154 && die->child != NULL
20155 && (die->tag == DW_TAG_class_type
20156 || die->tag == DW_TAG_structure_type
20157 || die->tag == DW_TAG_union_type))
20158 {
20159 char *name = guess_full_die_structure_name (die, cu);
20160 if (name != NULL)
20161 return name;
20162 }
20163 return "";
3d567982
TT
20164 case DW_TAG_enumeration_type:
20165 parent_type = read_type_die (parent, cu);
20166 if (TYPE_DECLARED_CLASS (parent_type))
20167 {
20168 if (TYPE_TAG_NAME (parent_type) != NULL)
20169 return TYPE_TAG_NAME (parent_type);
20170 return "";
20171 }
20172 /* Fall through. */
63d06c5c 20173 default:
8176b9b8 20174 return determine_prefix (parent, cu);
63d06c5c 20175 }
63d06c5c
DC
20176}
20177
3e43a32a
MS
20178/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20179 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20180 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20181 an obconcat, otherwise allocate storage for the result. The CU argument is
20182 used to determine the language and hence, the appropriate separator. */
987504bb 20183
f55ee35c 20184#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20185
20186static char *
f55ee35c
JK
20187typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20188 int physname, struct dwarf2_cu *cu)
63d06c5c 20189{
f55ee35c 20190 const char *lead = "";
5c315b68 20191 const char *sep;
63d06c5c 20192
3e43a32a
MS
20193 if (suffix == NULL || suffix[0] == '\0'
20194 || prefix == NULL || prefix[0] == '\0')
987504bb 20195 sep = "";
45280282
IB
20196 else if (cu->language == language_d)
20197 {
20198 /* For D, the 'main' function could be defined in any module, but it
20199 should never be prefixed. */
20200 if (strcmp (suffix, "D main") == 0)
20201 {
20202 prefix = "";
20203 sep = "";
20204 }
20205 else
20206 sep = ".";
20207 }
f55ee35c
JK
20208 else if (cu->language == language_fortran && physname)
20209 {
20210 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20211 DW_AT_MIPS_linkage_name is preferred and used instead. */
20212
20213 lead = "__";
20214 sep = "_MOD_";
20215 }
987504bb
JJ
20216 else
20217 sep = "::";
63d06c5c 20218
6dd47d34
DE
20219 if (prefix == NULL)
20220 prefix = "";
20221 if (suffix == NULL)
20222 suffix = "";
20223
987504bb
JJ
20224 if (obs == NULL)
20225 {
3e43a32a 20226 char *retval
224c3ddb
SM
20227 = ((char *)
20228 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20229
f55ee35c
JK
20230 strcpy (retval, lead);
20231 strcat (retval, prefix);
6dd47d34
DE
20232 strcat (retval, sep);
20233 strcat (retval, suffix);
63d06c5c
DC
20234 return retval;
20235 }
987504bb
JJ
20236 else
20237 {
20238 /* We have an obstack. */
f55ee35c 20239 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20240 }
63d06c5c
DC
20241}
20242
c906108c
SS
20243/* Return sibling of die, NULL if no sibling. */
20244
f9aca02d 20245static struct die_info *
fba45db2 20246sibling_die (struct die_info *die)
c906108c 20247{
639d11d3 20248 return die->sibling;
c906108c
SS
20249}
20250
71c25dea
TT
20251/* Get name of a die, return NULL if not found. */
20252
15d034d0
TT
20253static const char *
20254dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20255 struct obstack *obstack)
20256{
20257 if (name && cu->language == language_cplus)
20258 {
2f408ecb 20259 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20260
2f408ecb 20261 if (!canon_name.empty ())
71c25dea 20262 {
2f408ecb
PA
20263 if (canon_name != name)
20264 name = (const char *) obstack_copy0 (obstack,
20265 canon_name.c_str (),
20266 canon_name.length ());
71c25dea
TT
20267 }
20268 }
20269
20270 return name;
c906108c
SS
20271}
20272
96553a0c
DE
20273/* Get name of a die, return NULL if not found.
20274 Anonymous namespaces are converted to their magic string. */
9219021c 20275
15d034d0 20276static const char *
e142c38c 20277dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20278{
20279 struct attribute *attr;
20280
e142c38c 20281 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20282 if ((!attr || !DW_STRING (attr))
96553a0c 20283 && die->tag != DW_TAG_namespace
53832f31
TT
20284 && die->tag != DW_TAG_class_type
20285 && die->tag != DW_TAG_interface_type
20286 && die->tag != DW_TAG_structure_type
20287 && die->tag != DW_TAG_union_type)
71c25dea
TT
20288 return NULL;
20289
20290 switch (die->tag)
20291 {
20292 case DW_TAG_compile_unit:
95554aad 20293 case DW_TAG_partial_unit:
71c25dea
TT
20294 /* Compilation units have a DW_AT_name that is a filename, not
20295 a source language identifier. */
20296 case DW_TAG_enumeration_type:
20297 case DW_TAG_enumerator:
20298 /* These tags always have simple identifiers already; no need
20299 to canonicalize them. */
20300 return DW_STRING (attr);
907af001 20301
96553a0c
DE
20302 case DW_TAG_namespace:
20303 if (attr != NULL && DW_STRING (attr) != NULL)
20304 return DW_STRING (attr);
20305 return CP_ANONYMOUS_NAMESPACE_STR;
20306
907af001
UW
20307 case DW_TAG_class_type:
20308 case DW_TAG_interface_type:
20309 case DW_TAG_structure_type:
20310 case DW_TAG_union_type:
20311 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20312 structures or unions. These were of the form "._%d" in GCC 4.1,
20313 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20314 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20315 if (attr && DW_STRING (attr)
61012eef
GB
20316 && (startswith (DW_STRING (attr), "._")
20317 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20318 return NULL;
53832f31
TT
20319
20320 /* GCC might emit a nameless typedef that has a linkage name. See
20321 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20322 if (!attr || DW_STRING (attr) == NULL)
20323 {
df5c6c50 20324 char *demangled = NULL;
53832f31 20325
73b9be8b 20326 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20327 if (attr == NULL || DW_STRING (attr) == NULL)
20328 return NULL;
20329
df5c6c50
JK
20330 /* Avoid demangling DW_STRING (attr) the second time on a second
20331 call for the same DIE. */
20332 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20333 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20334
20335 if (demangled)
20336 {
e6a959d6 20337 const char *base;
96408a79 20338
53832f31 20339 /* FIXME: we already did this for the partial symbol... */
34a68019 20340 DW_STRING (attr)
224c3ddb
SM
20341 = ((const char *)
20342 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20343 demangled, strlen (demangled)));
53832f31
TT
20344 DW_STRING_IS_CANONICAL (attr) = 1;
20345 xfree (demangled);
96408a79
SA
20346
20347 /* Strip any leading namespaces/classes, keep only the base name.
20348 DW_AT_name for named DIEs does not contain the prefixes. */
20349 base = strrchr (DW_STRING (attr), ':');
20350 if (base && base > DW_STRING (attr) && base[-1] == ':')
20351 return &base[1];
20352 else
20353 return DW_STRING (attr);
53832f31
TT
20354 }
20355 }
907af001
UW
20356 break;
20357
71c25dea 20358 default:
907af001
UW
20359 break;
20360 }
20361
20362 if (!DW_STRING_IS_CANONICAL (attr))
20363 {
20364 DW_STRING (attr)
20365 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20366 &cu->objfile->per_bfd->storage_obstack);
907af001 20367 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20368 }
907af001 20369 return DW_STRING (attr);
9219021c
DC
20370}
20371
20372/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20373 is none. *EXT_CU is the CU containing DIE on input, and the CU
20374 containing the return value on output. */
9219021c
DC
20375
20376static struct die_info *
f2f0e013 20377dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20378{
20379 struct attribute *attr;
9219021c 20380
f2f0e013 20381 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20382 if (attr == NULL)
20383 return NULL;
20384
f2f0e013 20385 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20386}
20387
c906108c
SS
20388/* Convert a DIE tag into its string name. */
20389
f39c6ffd 20390static const char *
aa1ee363 20391dwarf_tag_name (unsigned tag)
c906108c 20392{
f39c6ffd
TT
20393 const char *name = get_DW_TAG_name (tag);
20394
20395 if (name == NULL)
20396 return "DW_TAG_<unknown>";
20397
20398 return name;
c906108c
SS
20399}
20400
20401/* Convert a DWARF attribute code into its string name. */
20402
f39c6ffd 20403static const char *
aa1ee363 20404dwarf_attr_name (unsigned attr)
c906108c 20405{
f39c6ffd
TT
20406 const char *name;
20407
c764a876 20408#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20409 if (attr == DW_AT_MIPS_fde)
20410 return "DW_AT_MIPS_fde";
20411#else
20412 if (attr == DW_AT_HP_block_index)
20413 return "DW_AT_HP_block_index";
c764a876 20414#endif
f39c6ffd
TT
20415
20416 name = get_DW_AT_name (attr);
20417
20418 if (name == NULL)
20419 return "DW_AT_<unknown>";
20420
20421 return name;
c906108c
SS
20422}
20423
20424/* Convert a DWARF value form code into its string name. */
20425
f39c6ffd 20426static const char *
aa1ee363 20427dwarf_form_name (unsigned form)
c906108c 20428{
f39c6ffd
TT
20429 const char *name = get_DW_FORM_name (form);
20430
20431 if (name == NULL)
20432 return "DW_FORM_<unknown>";
20433
20434 return name;
c906108c
SS
20435}
20436
a121b7c1 20437static const char *
fba45db2 20438dwarf_bool_name (unsigned mybool)
c906108c
SS
20439{
20440 if (mybool)
20441 return "TRUE";
20442 else
20443 return "FALSE";
20444}
20445
20446/* Convert a DWARF type code into its string name. */
20447
f39c6ffd 20448static const char *
aa1ee363 20449dwarf_type_encoding_name (unsigned enc)
c906108c 20450{
f39c6ffd 20451 const char *name = get_DW_ATE_name (enc);
c906108c 20452
f39c6ffd
TT
20453 if (name == NULL)
20454 return "DW_ATE_<unknown>";
c906108c 20455
f39c6ffd 20456 return name;
c906108c 20457}
c906108c 20458
f9aca02d 20459static void
d97bc12b 20460dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20461{
20462 unsigned int i;
20463
d97bc12b
DE
20464 print_spaces (indent, f);
20465 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20466 dwarf_tag_name (die->tag), die->abbrev,
20467 to_underlying (die->sect_off));
d97bc12b
DE
20468
20469 if (die->parent != NULL)
20470 {
20471 print_spaces (indent, f);
20472 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20473 to_underlying (die->parent->sect_off));
d97bc12b
DE
20474 }
20475
20476 print_spaces (indent, f);
20477 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20478 dwarf_bool_name (die->child != NULL));
c906108c 20479
d97bc12b
DE
20480 print_spaces (indent, f);
20481 fprintf_unfiltered (f, " attributes:\n");
20482
c906108c
SS
20483 for (i = 0; i < die->num_attrs; ++i)
20484 {
d97bc12b
DE
20485 print_spaces (indent, f);
20486 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20487 dwarf_attr_name (die->attrs[i].name),
20488 dwarf_form_name (die->attrs[i].form));
d97bc12b 20489
c906108c
SS
20490 switch (die->attrs[i].form)
20491 {
c906108c 20492 case DW_FORM_addr:
3019eac3 20493 case DW_FORM_GNU_addr_index:
d97bc12b 20494 fprintf_unfiltered (f, "address: ");
5af949e3 20495 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20496 break;
20497 case DW_FORM_block2:
20498 case DW_FORM_block4:
20499 case DW_FORM_block:
20500 case DW_FORM_block1:
56eb65bd
SP
20501 fprintf_unfiltered (f, "block: size %s",
20502 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20503 break;
2dc7f7b3 20504 case DW_FORM_exprloc:
56eb65bd
SP
20505 fprintf_unfiltered (f, "expression: size %s",
20506 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20507 break;
0224619f
JK
20508 case DW_FORM_data16:
20509 fprintf_unfiltered (f, "constant of 16 bytes");
20510 break;
4568ecf9
DE
20511 case DW_FORM_ref_addr:
20512 fprintf_unfiltered (f, "ref address: ");
20513 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20514 break;
36586728
TT
20515 case DW_FORM_GNU_ref_alt:
20516 fprintf_unfiltered (f, "alt ref address: ");
20517 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20518 break;
10b3939b
DJ
20519 case DW_FORM_ref1:
20520 case DW_FORM_ref2:
20521 case DW_FORM_ref4:
4568ecf9
DE
20522 case DW_FORM_ref8:
20523 case DW_FORM_ref_udata:
d97bc12b 20524 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20525 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20526 break;
c906108c
SS
20527 case DW_FORM_data1:
20528 case DW_FORM_data2:
20529 case DW_FORM_data4:
ce5d95e1 20530 case DW_FORM_data8:
c906108c
SS
20531 case DW_FORM_udata:
20532 case DW_FORM_sdata:
43bbcdc2
PH
20533 fprintf_unfiltered (f, "constant: %s",
20534 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20535 break;
2dc7f7b3
TT
20536 case DW_FORM_sec_offset:
20537 fprintf_unfiltered (f, "section offset: %s",
20538 pulongest (DW_UNSND (&die->attrs[i])));
20539 break;
55f1336d 20540 case DW_FORM_ref_sig8:
ac9ec31b
DE
20541 fprintf_unfiltered (f, "signature: %s",
20542 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20543 break;
c906108c 20544 case DW_FORM_string:
4bdf3d34 20545 case DW_FORM_strp:
43988095 20546 case DW_FORM_line_strp:
3019eac3 20547 case DW_FORM_GNU_str_index:
36586728 20548 case DW_FORM_GNU_strp_alt:
8285870a 20549 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20550 DW_STRING (&die->attrs[i])
8285870a
JK
20551 ? DW_STRING (&die->attrs[i]) : "",
20552 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20553 break;
20554 case DW_FORM_flag:
20555 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20556 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20557 else
d97bc12b 20558 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20559 break;
2dc7f7b3
TT
20560 case DW_FORM_flag_present:
20561 fprintf_unfiltered (f, "flag: TRUE");
20562 break;
a8329558 20563 case DW_FORM_indirect:
0963b4bd
MS
20564 /* The reader will have reduced the indirect form to
20565 the "base form" so this form should not occur. */
3e43a32a
MS
20566 fprintf_unfiltered (f,
20567 "unexpected attribute form: DW_FORM_indirect");
a8329558 20568 break;
663c44ac
JK
20569 case DW_FORM_implicit_const:
20570 fprintf_unfiltered (f, "constant: %s",
20571 plongest (DW_SND (&die->attrs[i])));
20572 break;
c906108c 20573 default:
d97bc12b 20574 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20575 die->attrs[i].form);
d97bc12b 20576 break;
c906108c 20577 }
d97bc12b 20578 fprintf_unfiltered (f, "\n");
c906108c
SS
20579 }
20580}
20581
f9aca02d 20582static void
d97bc12b 20583dump_die_for_error (struct die_info *die)
c906108c 20584{
d97bc12b
DE
20585 dump_die_shallow (gdb_stderr, 0, die);
20586}
20587
20588static void
20589dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20590{
20591 int indent = level * 4;
20592
20593 gdb_assert (die != NULL);
20594
20595 if (level >= max_level)
20596 return;
20597
20598 dump_die_shallow (f, indent, die);
20599
20600 if (die->child != NULL)
c906108c 20601 {
d97bc12b
DE
20602 print_spaces (indent, f);
20603 fprintf_unfiltered (f, " Children:");
20604 if (level + 1 < max_level)
20605 {
20606 fprintf_unfiltered (f, "\n");
20607 dump_die_1 (f, level + 1, max_level, die->child);
20608 }
20609 else
20610 {
3e43a32a
MS
20611 fprintf_unfiltered (f,
20612 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20613 }
20614 }
20615
20616 if (die->sibling != NULL && level > 0)
20617 {
20618 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20619 }
20620}
20621
d97bc12b
DE
20622/* This is called from the pdie macro in gdbinit.in.
20623 It's not static so gcc will keep a copy callable from gdb. */
20624
20625void
20626dump_die (struct die_info *die, int max_level)
20627{
20628 dump_die_1 (gdb_stdlog, 0, max_level, die);
20629}
20630
f9aca02d 20631static void
51545339 20632store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20633{
51545339 20634 void **slot;
c906108c 20635
9c541725
PA
20636 slot = htab_find_slot_with_hash (cu->die_hash, die,
20637 to_underlying (die->sect_off),
b64f50a1 20638 INSERT);
51545339
DJ
20639
20640 *slot = die;
c906108c
SS
20641}
20642
b64f50a1
JK
20643/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20644 required kind. */
20645
20646static sect_offset
ff39bb5e 20647dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20648{
7771576e 20649 if (attr_form_is_ref (attr))
9c541725 20650 return (sect_offset) DW_UNSND (attr);
93311388
DE
20651
20652 complaint (&symfile_complaints,
20653 _("unsupported die ref attribute form: '%s'"),
20654 dwarf_form_name (attr->form));
9c541725 20655 return {};
c906108c
SS
20656}
20657
43bbcdc2
PH
20658/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20659 * the value held by the attribute is not constant. */
a02abb62 20660
43bbcdc2 20661static LONGEST
ff39bb5e 20662dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20663{
663c44ac 20664 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20665 return DW_SND (attr);
20666 else if (attr->form == DW_FORM_udata
20667 || attr->form == DW_FORM_data1
20668 || attr->form == DW_FORM_data2
20669 || attr->form == DW_FORM_data4
20670 || attr->form == DW_FORM_data8)
20671 return DW_UNSND (attr);
20672 else
20673 {
0224619f 20674 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20675 complaint (&symfile_complaints,
20676 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20677 dwarf_form_name (attr->form));
20678 return default_value;
20679 }
20680}
20681
348e048f
DE
20682/* Follow reference or signature attribute ATTR of SRC_DIE.
20683 On entry *REF_CU is the CU of SRC_DIE.
20684 On exit *REF_CU is the CU of the result. */
20685
20686static struct die_info *
ff39bb5e 20687follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20688 struct dwarf2_cu **ref_cu)
20689{
20690 struct die_info *die;
20691
7771576e 20692 if (attr_form_is_ref (attr))
348e048f 20693 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20694 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20695 die = follow_die_sig (src_die, attr, ref_cu);
20696 else
20697 {
20698 dump_die_for_error (src_die);
20699 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20700 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20701 }
20702
20703 return die;
03dd20cc
DJ
20704}
20705
5c631832 20706/* Follow reference OFFSET.
673bfd45
DE
20707 On entry *REF_CU is the CU of the source die referencing OFFSET.
20708 On exit *REF_CU is the CU of the result.
20709 Returns NULL if OFFSET is invalid. */
f504f079 20710
f9aca02d 20711static struct die_info *
9c541725 20712follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20713 struct dwarf2_cu **ref_cu)
c906108c 20714{
10b3939b 20715 struct die_info temp_die;
f2f0e013 20716 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20717
348e048f
DE
20718 gdb_assert (cu->per_cu != NULL);
20719
98bfdba5
PA
20720 target_cu = cu;
20721
3019eac3 20722 if (cu->per_cu->is_debug_types)
348e048f
DE
20723 {
20724 /* .debug_types CUs cannot reference anything outside their CU.
20725 If they need to, they have to reference a signatured type via
55f1336d 20726 DW_FORM_ref_sig8. */
9c541725 20727 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20728 return NULL;
348e048f 20729 }
36586728 20730 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20731 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20732 {
20733 struct dwarf2_per_cu_data *per_cu;
9a619af0 20734
9c541725 20735 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20736 cu->objfile);
03dd20cc
DJ
20737
20738 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20739 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20740 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20741
10b3939b
DJ
20742 target_cu = per_cu->cu;
20743 }
98bfdba5
PA
20744 else if (cu->dies == NULL)
20745 {
20746 /* We're loading full DIEs during partial symbol reading. */
20747 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20748 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20749 }
c906108c 20750
f2f0e013 20751 *ref_cu = target_cu;
9c541725 20752 temp_die.sect_off = sect_off;
9a3c8263 20753 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20754 &temp_die,
20755 to_underlying (sect_off));
5c631832 20756}
10b3939b 20757
5c631832
JK
20758/* Follow reference attribute ATTR of SRC_DIE.
20759 On entry *REF_CU is the CU of SRC_DIE.
20760 On exit *REF_CU is the CU of the result. */
20761
20762static struct die_info *
ff39bb5e 20763follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20764 struct dwarf2_cu **ref_cu)
20765{
9c541725 20766 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20767 struct dwarf2_cu *cu = *ref_cu;
20768 struct die_info *die;
20769
9c541725 20770 die = follow_die_offset (sect_off,
36586728
TT
20771 (attr->form == DW_FORM_GNU_ref_alt
20772 || cu->per_cu->is_dwz),
20773 ref_cu);
5c631832
JK
20774 if (!die)
20775 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20776 "at 0x%x [in module %s]"),
9c541725 20777 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20778 objfile_name (cu->objfile));
348e048f 20779
5c631832
JK
20780 return die;
20781}
20782
9c541725 20783/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20784 Returned value is intended for DW_OP_call*. Returned
20785 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20786
20787struct dwarf2_locexpr_baton
9c541725 20788dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20789 struct dwarf2_per_cu_data *per_cu,
20790 CORE_ADDR (*get_frame_pc) (void *baton),
20791 void *baton)
5c631832 20792{
918dd910 20793 struct dwarf2_cu *cu;
5c631832
JK
20794 struct die_info *die;
20795 struct attribute *attr;
20796 struct dwarf2_locexpr_baton retval;
20797
8cf6f0b1
TT
20798 dw2_setup (per_cu->objfile);
20799
918dd910
JK
20800 if (per_cu->cu == NULL)
20801 load_cu (per_cu);
20802 cu = per_cu->cu;
cc12ce38
DE
20803 if (cu == NULL)
20804 {
20805 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20806 Instead just throw an error, not much else we can do. */
20807 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20808 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20809 }
918dd910 20810
9c541725 20811 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20812 if (!die)
20813 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20814 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20815
20816 attr = dwarf2_attr (die, DW_AT_location, cu);
20817 if (!attr)
20818 {
e103e986
JK
20819 /* DWARF: "If there is no such attribute, then there is no effect.".
20820 DATA is ignored if SIZE is 0. */
5c631832 20821
e103e986 20822 retval.data = NULL;
5c631832
JK
20823 retval.size = 0;
20824 }
8cf6f0b1
TT
20825 else if (attr_form_is_section_offset (attr))
20826 {
20827 struct dwarf2_loclist_baton loclist_baton;
20828 CORE_ADDR pc = (*get_frame_pc) (baton);
20829 size_t size;
20830
20831 fill_in_loclist_baton (cu, &loclist_baton, attr);
20832
20833 retval.data = dwarf2_find_location_expression (&loclist_baton,
20834 &size, pc);
20835 retval.size = size;
20836 }
5c631832
JK
20837 else
20838 {
20839 if (!attr_form_is_block (attr))
20840 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20841 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20842 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20843
20844 retval.data = DW_BLOCK (attr)->data;
20845 retval.size = DW_BLOCK (attr)->size;
20846 }
20847 retval.per_cu = cu->per_cu;
918dd910 20848
918dd910
JK
20849 age_cached_comp_units ();
20850
5c631832 20851 return retval;
348e048f
DE
20852}
20853
8b9737bf
TT
20854/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20855 offset. */
20856
20857struct dwarf2_locexpr_baton
20858dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20859 struct dwarf2_per_cu_data *per_cu,
20860 CORE_ADDR (*get_frame_pc) (void *baton),
20861 void *baton)
20862{
9c541725 20863 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20864
9c541725 20865 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20866}
20867
b6807d98
TT
20868/* Write a constant of a given type as target-ordered bytes into
20869 OBSTACK. */
20870
20871static const gdb_byte *
20872write_constant_as_bytes (struct obstack *obstack,
20873 enum bfd_endian byte_order,
20874 struct type *type,
20875 ULONGEST value,
20876 LONGEST *len)
20877{
20878 gdb_byte *result;
20879
20880 *len = TYPE_LENGTH (type);
224c3ddb 20881 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20882 store_unsigned_integer (result, *len, byte_order, value);
20883
20884 return result;
20885}
20886
20887/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20888 pointer to the constant bytes and set LEN to the length of the
20889 data. If memory is needed, allocate it on OBSTACK. If the DIE
20890 does not have a DW_AT_const_value, return NULL. */
20891
20892const gdb_byte *
9c541725 20893dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20894 struct dwarf2_per_cu_data *per_cu,
20895 struct obstack *obstack,
20896 LONGEST *len)
20897{
20898 struct dwarf2_cu *cu;
20899 struct die_info *die;
20900 struct attribute *attr;
20901 const gdb_byte *result = NULL;
20902 struct type *type;
20903 LONGEST value;
20904 enum bfd_endian byte_order;
20905
20906 dw2_setup (per_cu->objfile);
20907
20908 if (per_cu->cu == NULL)
20909 load_cu (per_cu);
20910 cu = per_cu->cu;
cc12ce38
DE
20911 if (cu == NULL)
20912 {
20913 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20914 Instead just throw an error, not much else we can do. */
20915 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20916 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20917 }
b6807d98 20918
9c541725 20919 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20920 if (!die)
20921 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20922 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20923
20924
20925 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20926 if (attr == NULL)
20927 return NULL;
20928
20929 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20930 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20931
20932 switch (attr->form)
20933 {
20934 case DW_FORM_addr:
20935 case DW_FORM_GNU_addr_index:
20936 {
20937 gdb_byte *tem;
20938
20939 *len = cu->header.addr_size;
224c3ddb 20940 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20941 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20942 result = tem;
20943 }
20944 break;
20945 case DW_FORM_string:
20946 case DW_FORM_strp:
20947 case DW_FORM_GNU_str_index:
20948 case DW_FORM_GNU_strp_alt:
20949 /* DW_STRING is already allocated on the objfile obstack, point
20950 directly to it. */
20951 result = (const gdb_byte *) DW_STRING (attr);
20952 *len = strlen (DW_STRING (attr));
20953 break;
20954 case DW_FORM_block1:
20955 case DW_FORM_block2:
20956 case DW_FORM_block4:
20957 case DW_FORM_block:
20958 case DW_FORM_exprloc:
0224619f 20959 case DW_FORM_data16:
b6807d98
TT
20960 result = DW_BLOCK (attr)->data;
20961 *len = DW_BLOCK (attr)->size;
20962 break;
20963
20964 /* The DW_AT_const_value attributes are supposed to carry the
20965 symbol's value "represented as it would be on the target
20966 architecture." By the time we get here, it's already been
20967 converted to host endianness, so we just need to sign- or
20968 zero-extend it as appropriate. */
20969 case DW_FORM_data1:
20970 type = die_type (die, cu);
20971 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20972 if (result == NULL)
20973 result = write_constant_as_bytes (obstack, byte_order,
20974 type, value, len);
20975 break;
20976 case DW_FORM_data2:
20977 type = die_type (die, cu);
20978 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20979 if (result == NULL)
20980 result = write_constant_as_bytes (obstack, byte_order,
20981 type, value, len);
20982 break;
20983 case DW_FORM_data4:
20984 type = die_type (die, cu);
20985 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20986 if (result == NULL)
20987 result = write_constant_as_bytes (obstack, byte_order,
20988 type, value, len);
20989 break;
20990 case DW_FORM_data8:
20991 type = die_type (die, cu);
20992 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20993 if (result == NULL)
20994 result = write_constant_as_bytes (obstack, byte_order,
20995 type, value, len);
20996 break;
20997
20998 case DW_FORM_sdata:
663c44ac 20999 case DW_FORM_implicit_const:
b6807d98
TT
21000 type = die_type (die, cu);
21001 result = write_constant_as_bytes (obstack, byte_order,
21002 type, DW_SND (attr), len);
21003 break;
21004
21005 case DW_FORM_udata:
21006 type = die_type (die, cu);
21007 result = write_constant_as_bytes (obstack, byte_order,
21008 type, DW_UNSND (attr), len);
21009 break;
21010
21011 default:
21012 complaint (&symfile_complaints,
21013 _("unsupported const value attribute form: '%s'"),
21014 dwarf_form_name (attr->form));
21015 break;
21016 }
21017
21018 return result;
21019}
21020
7942e96e
AA
21021/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21022 valid type for this die is found. */
21023
21024struct type *
9c541725 21025dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21026 struct dwarf2_per_cu_data *per_cu)
21027{
21028 struct dwarf2_cu *cu;
21029 struct die_info *die;
21030
21031 dw2_setup (per_cu->objfile);
21032
21033 if (per_cu->cu == NULL)
21034 load_cu (per_cu);
21035 cu = per_cu->cu;
21036 if (!cu)
21037 return NULL;
21038
9c541725 21039 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21040 if (!die)
21041 return NULL;
21042
21043 return die_type (die, cu);
21044}
21045
8a9b8146
TT
21046/* Return the type of the DIE at DIE_OFFSET in the CU named by
21047 PER_CU. */
21048
21049struct type *
b64f50a1 21050dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21051 struct dwarf2_per_cu_data *per_cu)
21052{
8a9b8146 21053 dw2_setup (per_cu->objfile);
b64f50a1 21054
9c541725 21055 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21056 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21057}
21058
ac9ec31b 21059/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21060 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21061 On exit *REF_CU is the CU of the result.
21062 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21063
21064static struct die_info *
ac9ec31b
DE
21065follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21066 struct dwarf2_cu **ref_cu)
348e048f 21067{
348e048f 21068 struct die_info temp_die;
348e048f
DE
21069 struct dwarf2_cu *sig_cu;
21070 struct die_info *die;
21071
ac9ec31b
DE
21072 /* While it might be nice to assert sig_type->type == NULL here,
21073 we can get here for DW_AT_imported_declaration where we need
21074 the DIE not the type. */
348e048f
DE
21075
21076 /* If necessary, add it to the queue and load its DIEs. */
21077
95554aad 21078 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21079 read_signatured_type (sig_type);
348e048f 21080
348e048f 21081 sig_cu = sig_type->per_cu.cu;
69d751e3 21082 gdb_assert (sig_cu != NULL);
9c541725
PA
21083 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21084 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21085 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21086 to_underlying (temp_die.sect_off));
348e048f
DE
21087 if (die)
21088 {
796a7ff8
DE
21089 /* For .gdb_index version 7 keep track of included TUs.
21090 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21091 if (dwarf2_per_objfile->index_table != NULL
21092 && dwarf2_per_objfile->index_table->version <= 7)
21093 {
21094 VEC_safe_push (dwarf2_per_cu_ptr,
21095 (*ref_cu)->per_cu->imported_symtabs,
21096 sig_cu->per_cu);
21097 }
21098
348e048f
DE
21099 *ref_cu = sig_cu;
21100 return die;
21101 }
21102
ac9ec31b
DE
21103 return NULL;
21104}
21105
21106/* Follow signatured type referenced by ATTR in SRC_DIE.
21107 On entry *REF_CU is the CU of SRC_DIE.
21108 On exit *REF_CU is the CU of the result.
21109 The result is the DIE of the type.
21110 If the referenced type cannot be found an error is thrown. */
21111
21112static struct die_info *
ff39bb5e 21113follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21114 struct dwarf2_cu **ref_cu)
21115{
21116 ULONGEST signature = DW_SIGNATURE (attr);
21117 struct signatured_type *sig_type;
21118 struct die_info *die;
21119
21120 gdb_assert (attr->form == DW_FORM_ref_sig8);
21121
a2ce51a0 21122 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21123 /* sig_type will be NULL if the signatured type is missing from
21124 the debug info. */
21125 if (sig_type == NULL)
21126 {
21127 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21128 " from DIE at 0x%x [in module %s]"),
9c541725 21129 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21130 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21131 }
21132
21133 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21134 if (die == NULL)
21135 {
21136 dump_die_for_error (src_die);
21137 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21138 " from DIE at 0x%x [in module %s]"),
9c541725 21139 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21140 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21141 }
21142
21143 return die;
21144}
21145
21146/* Get the type specified by SIGNATURE referenced in DIE/CU,
21147 reading in and processing the type unit if necessary. */
21148
21149static struct type *
21150get_signatured_type (struct die_info *die, ULONGEST signature,
21151 struct dwarf2_cu *cu)
21152{
21153 struct signatured_type *sig_type;
21154 struct dwarf2_cu *type_cu;
21155 struct die_info *type_die;
21156 struct type *type;
21157
a2ce51a0 21158 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21159 /* sig_type will be NULL if the signatured type is missing from
21160 the debug info. */
21161 if (sig_type == NULL)
21162 {
21163 complaint (&symfile_complaints,
21164 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21165 " from DIE at 0x%x [in module %s]"),
9c541725 21166 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21167 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21168 return build_error_marker_type (cu, die);
21169 }
21170
21171 /* If we already know the type we're done. */
21172 if (sig_type->type != NULL)
21173 return sig_type->type;
21174
21175 type_cu = cu;
21176 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21177 if (type_die != NULL)
21178 {
21179 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21180 is created. This is important, for example, because for c++ classes
21181 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21182 type = read_type_die (type_die, type_cu);
21183 if (type == NULL)
21184 {
21185 complaint (&symfile_complaints,
21186 _("Dwarf Error: Cannot build signatured type %s"
21187 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21188 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21189 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21190 type = build_error_marker_type (cu, die);
21191 }
21192 }
21193 else
21194 {
21195 complaint (&symfile_complaints,
21196 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21197 " from DIE at 0x%x [in module %s]"),
9c541725 21198 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21199 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21200 type = build_error_marker_type (cu, die);
21201 }
21202 sig_type->type = type;
21203
21204 return type;
21205}
21206
21207/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21208 reading in and processing the type unit if necessary. */
21209
21210static struct type *
ff39bb5e 21211get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21212 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21213{
21214 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21215 if (attr_form_is_ref (attr))
ac9ec31b
DE
21216 {
21217 struct dwarf2_cu *type_cu = cu;
21218 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21219
21220 return read_type_die (type_die, type_cu);
21221 }
21222 else if (attr->form == DW_FORM_ref_sig8)
21223 {
21224 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21225 }
21226 else
21227 {
21228 complaint (&symfile_complaints,
21229 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21230 " at 0x%x [in module %s]"),
9c541725 21231 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21232 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21233 return build_error_marker_type (cu, die);
21234 }
348e048f
DE
21235}
21236
e5fe5e75 21237/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21238
21239static void
e5fe5e75 21240load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21241{
52dc124a 21242 struct signatured_type *sig_type;
348e048f 21243
f4dc4d17
DE
21244 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21245 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21246
6721b2ec
DE
21247 /* We have the per_cu, but we need the signatured_type.
21248 Fortunately this is an easy translation. */
21249 gdb_assert (per_cu->is_debug_types);
21250 sig_type = (struct signatured_type *) per_cu;
348e048f 21251
6721b2ec 21252 gdb_assert (per_cu->cu == NULL);
348e048f 21253
52dc124a 21254 read_signatured_type (sig_type);
348e048f 21255
6721b2ec 21256 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21257}
21258
dee91e82
DE
21259/* die_reader_func for read_signatured_type.
21260 This is identical to load_full_comp_unit_reader,
21261 but is kept separate for now. */
348e048f
DE
21262
21263static void
dee91e82 21264read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21265 const gdb_byte *info_ptr,
dee91e82
DE
21266 struct die_info *comp_unit_die,
21267 int has_children,
21268 void *data)
348e048f 21269{
dee91e82 21270 struct dwarf2_cu *cu = reader->cu;
348e048f 21271
dee91e82
DE
21272 gdb_assert (cu->die_hash == NULL);
21273 cu->die_hash =
21274 htab_create_alloc_ex (cu->header.length / 12,
21275 die_hash,
21276 die_eq,
21277 NULL,
21278 &cu->comp_unit_obstack,
21279 hashtab_obstack_allocate,
21280 dummy_obstack_deallocate);
348e048f 21281
dee91e82
DE
21282 if (has_children)
21283 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21284 &info_ptr, comp_unit_die);
21285 cu->dies = comp_unit_die;
21286 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21287
21288 /* We try not to read any attributes in this function, because not
9cdd5dbd 21289 all CUs needed for references have been loaded yet, and symbol
348e048f 21290 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21291 or we won't be able to build types correctly.
21292 Similarly, if we do not read the producer, we can not apply
21293 producer-specific interpretation. */
95554aad 21294 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21295}
348e048f 21296
3019eac3
DE
21297/* Read in a signatured type and build its CU and DIEs.
21298 If the type is a stub for the real type in a DWO file,
21299 read in the real type from the DWO file as well. */
dee91e82
DE
21300
21301static void
21302read_signatured_type (struct signatured_type *sig_type)
21303{
21304 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21305
3019eac3 21306 gdb_assert (per_cu->is_debug_types);
dee91e82 21307 gdb_assert (per_cu->cu == NULL);
348e048f 21308
f4dc4d17
DE
21309 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21310 read_signatured_type_reader, NULL);
7ee85ab1 21311 sig_type->per_cu.tu_read = 1;
c906108c
SS
21312}
21313
c906108c
SS
21314/* Decode simple location descriptions.
21315 Given a pointer to a dwarf block that defines a location, compute
21316 the location and return the value.
21317
4cecd739
DJ
21318 NOTE drow/2003-11-18: This function is called in two situations
21319 now: for the address of static or global variables (partial symbols
21320 only) and for offsets into structures which are expected to be
21321 (more or less) constant. The partial symbol case should go away,
21322 and only the constant case should remain. That will let this
21323 function complain more accurately. A few special modes are allowed
21324 without complaint for global variables (for instance, global
21325 register values and thread-local values).
c906108c
SS
21326
21327 A location description containing no operations indicates that the
4cecd739 21328 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21329 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21330 callers will only want a very basic result and this can become a
21ae7a4d
JK
21331 complaint.
21332
21333 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21334
21335static CORE_ADDR
e7c27a73 21336decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21337{
e7c27a73 21338 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21339 size_t i;
21340 size_t size = blk->size;
d521ce57 21341 const gdb_byte *data = blk->data;
21ae7a4d
JK
21342 CORE_ADDR stack[64];
21343 int stacki;
21344 unsigned int bytes_read, unsnd;
21345 gdb_byte op;
c906108c 21346
21ae7a4d
JK
21347 i = 0;
21348 stacki = 0;
21349 stack[stacki] = 0;
21350 stack[++stacki] = 0;
21351
21352 while (i < size)
21353 {
21354 op = data[i++];
21355 switch (op)
21356 {
21357 case DW_OP_lit0:
21358 case DW_OP_lit1:
21359 case DW_OP_lit2:
21360 case DW_OP_lit3:
21361 case DW_OP_lit4:
21362 case DW_OP_lit5:
21363 case DW_OP_lit6:
21364 case DW_OP_lit7:
21365 case DW_OP_lit8:
21366 case DW_OP_lit9:
21367 case DW_OP_lit10:
21368 case DW_OP_lit11:
21369 case DW_OP_lit12:
21370 case DW_OP_lit13:
21371 case DW_OP_lit14:
21372 case DW_OP_lit15:
21373 case DW_OP_lit16:
21374 case DW_OP_lit17:
21375 case DW_OP_lit18:
21376 case DW_OP_lit19:
21377 case DW_OP_lit20:
21378 case DW_OP_lit21:
21379 case DW_OP_lit22:
21380 case DW_OP_lit23:
21381 case DW_OP_lit24:
21382 case DW_OP_lit25:
21383 case DW_OP_lit26:
21384 case DW_OP_lit27:
21385 case DW_OP_lit28:
21386 case DW_OP_lit29:
21387 case DW_OP_lit30:
21388 case DW_OP_lit31:
21389 stack[++stacki] = op - DW_OP_lit0;
21390 break;
f1bea926 21391
21ae7a4d
JK
21392 case DW_OP_reg0:
21393 case DW_OP_reg1:
21394 case DW_OP_reg2:
21395 case DW_OP_reg3:
21396 case DW_OP_reg4:
21397 case DW_OP_reg5:
21398 case DW_OP_reg6:
21399 case DW_OP_reg7:
21400 case DW_OP_reg8:
21401 case DW_OP_reg9:
21402 case DW_OP_reg10:
21403 case DW_OP_reg11:
21404 case DW_OP_reg12:
21405 case DW_OP_reg13:
21406 case DW_OP_reg14:
21407 case DW_OP_reg15:
21408 case DW_OP_reg16:
21409 case DW_OP_reg17:
21410 case DW_OP_reg18:
21411 case DW_OP_reg19:
21412 case DW_OP_reg20:
21413 case DW_OP_reg21:
21414 case DW_OP_reg22:
21415 case DW_OP_reg23:
21416 case DW_OP_reg24:
21417 case DW_OP_reg25:
21418 case DW_OP_reg26:
21419 case DW_OP_reg27:
21420 case DW_OP_reg28:
21421 case DW_OP_reg29:
21422 case DW_OP_reg30:
21423 case DW_OP_reg31:
21424 stack[++stacki] = op - DW_OP_reg0;
21425 if (i < size)
21426 dwarf2_complex_location_expr_complaint ();
21427 break;
c906108c 21428
21ae7a4d
JK
21429 case DW_OP_regx:
21430 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21431 i += bytes_read;
21432 stack[++stacki] = unsnd;
21433 if (i < size)
21434 dwarf2_complex_location_expr_complaint ();
21435 break;
c906108c 21436
21ae7a4d
JK
21437 case DW_OP_addr:
21438 stack[++stacki] = read_address (objfile->obfd, &data[i],
21439 cu, &bytes_read);
21440 i += bytes_read;
21441 break;
d53d4ac5 21442
21ae7a4d
JK
21443 case DW_OP_const1u:
21444 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21445 i += 1;
21446 break;
21447
21448 case DW_OP_const1s:
21449 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21450 i += 1;
21451 break;
21452
21453 case DW_OP_const2u:
21454 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21455 i += 2;
21456 break;
21457
21458 case DW_OP_const2s:
21459 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21460 i += 2;
21461 break;
d53d4ac5 21462
21ae7a4d
JK
21463 case DW_OP_const4u:
21464 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21465 i += 4;
21466 break;
21467
21468 case DW_OP_const4s:
21469 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21470 i += 4;
21471 break;
21472
585861ea
JK
21473 case DW_OP_const8u:
21474 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21475 i += 8;
21476 break;
21477
21ae7a4d
JK
21478 case DW_OP_constu:
21479 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21480 &bytes_read);
21481 i += bytes_read;
21482 break;
21483
21484 case DW_OP_consts:
21485 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21486 i += bytes_read;
21487 break;
21488
21489 case DW_OP_dup:
21490 stack[stacki + 1] = stack[stacki];
21491 stacki++;
21492 break;
21493
21494 case DW_OP_plus:
21495 stack[stacki - 1] += stack[stacki];
21496 stacki--;
21497 break;
21498
21499 case DW_OP_plus_uconst:
21500 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21501 &bytes_read);
21502 i += bytes_read;
21503 break;
21504
21505 case DW_OP_minus:
21506 stack[stacki - 1] -= stack[stacki];
21507 stacki--;
21508 break;
21509
21510 case DW_OP_deref:
21511 /* If we're not the last op, then we definitely can't encode
21512 this using GDB's address_class enum. This is valid for partial
21513 global symbols, although the variable's address will be bogus
21514 in the psymtab. */
21515 if (i < size)
21516 dwarf2_complex_location_expr_complaint ();
21517 break;
21518
21519 case DW_OP_GNU_push_tls_address:
4aa4e28b 21520 case DW_OP_form_tls_address:
21ae7a4d
JK
21521 /* The top of the stack has the offset from the beginning
21522 of the thread control block at which the variable is located. */
21523 /* Nothing should follow this operator, so the top of stack would
21524 be returned. */
21525 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21526 address will be bogus in the psymtab. Make it always at least
21527 non-zero to not look as a variable garbage collected by linker
21528 which have DW_OP_addr 0. */
21ae7a4d
JK
21529 if (i < size)
21530 dwarf2_complex_location_expr_complaint ();
585861ea 21531 stack[stacki]++;
21ae7a4d
JK
21532 break;
21533
21534 case DW_OP_GNU_uninit:
21535 break;
21536
3019eac3 21537 case DW_OP_GNU_addr_index:
49f6c839 21538 case DW_OP_GNU_const_index:
3019eac3
DE
21539 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21540 &bytes_read);
21541 i += bytes_read;
21542 break;
21543
21ae7a4d
JK
21544 default:
21545 {
f39c6ffd 21546 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21547
21548 if (name)
21549 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21550 name);
21551 else
21552 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21553 op);
21554 }
21555
21556 return (stack[stacki]);
d53d4ac5 21557 }
3c6e0cb3 21558
21ae7a4d
JK
21559 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21560 outside of the allocated space. Also enforce minimum>0. */
21561 if (stacki >= ARRAY_SIZE (stack) - 1)
21562 {
21563 complaint (&symfile_complaints,
21564 _("location description stack overflow"));
21565 return 0;
21566 }
21567
21568 if (stacki <= 0)
21569 {
21570 complaint (&symfile_complaints,
21571 _("location description stack underflow"));
21572 return 0;
21573 }
21574 }
21575 return (stack[stacki]);
c906108c
SS
21576}
21577
21578/* memory allocation interface */
21579
c906108c 21580static struct dwarf_block *
7b5a2f43 21581dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21582{
8d749320 21583 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21584}
21585
c906108c 21586static struct die_info *
b60c80d6 21587dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21588{
21589 struct die_info *die;
b60c80d6
DJ
21590 size_t size = sizeof (struct die_info);
21591
21592 if (num_attrs > 1)
21593 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21594
b60c80d6 21595 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21596 memset (die, 0, sizeof (struct die_info));
21597 return (die);
21598}
2e276125
JB
21599
21600\f
21601/* Macro support. */
21602
233d95b5
JK
21603/* Return file name relative to the compilation directory of file number I in
21604 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21605 responsible for freeing it. */
233d95b5 21606
2e276125 21607static char *
233d95b5 21608file_file_name (int file, struct line_header *lh)
2e276125 21609{
6a83a1e6
EZ
21610 /* Is the file number a valid index into the line header's file name
21611 table? Remember that file numbers start with one, not zero. */
fff8551c 21612 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21613 {
8c43009f 21614 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21615
8c43009f
PA
21616 if (!IS_ABSOLUTE_PATH (fe.name))
21617 {
21618 const char *dir = fe.include_dir (lh);
21619 if (dir != NULL)
21620 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21621 }
21622 return xstrdup (fe.name);
6a83a1e6 21623 }
2e276125
JB
21624 else
21625 {
6a83a1e6
EZ
21626 /* The compiler produced a bogus file number. We can at least
21627 record the macro definitions made in the file, even if we
21628 won't be able to find the file by name. */
21629 char fake_name[80];
9a619af0 21630
8c042590
PM
21631 xsnprintf (fake_name, sizeof (fake_name),
21632 "<bad macro file number %d>", file);
2e276125 21633
6e70227d 21634 complaint (&symfile_complaints,
6a83a1e6
EZ
21635 _("bad file number in macro information (%d)"),
21636 file);
2e276125 21637
6a83a1e6 21638 return xstrdup (fake_name);
2e276125
JB
21639 }
21640}
21641
233d95b5
JK
21642/* Return the full name of file number I in *LH's file name table.
21643 Use COMP_DIR as the name of the current directory of the
21644 compilation. The result is allocated using xmalloc; the caller is
21645 responsible for freeing it. */
21646static char *
21647file_full_name (int file, struct line_header *lh, const char *comp_dir)
21648{
21649 /* Is the file number a valid index into the line header's file name
21650 table? Remember that file numbers start with one, not zero. */
fff8551c 21651 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21652 {
21653 char *relative = file_file_name (file, lh);
21654
21655 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21656 return relative;
b36cec19
PA
21657 return reconcat (relative, comp_dir, SLASH_STRING,
21658 relative, (char *) NULL);
233d95b5
JK
21659 }
21660 else
21661 return file_file_name (file, lh);
21662}
21663
2e276125
JB
21664
21665static struct macro_source_file *
21666macro_start_file (int file, int line,
21667 struct macro_source_file *current_file,
43f3e411 21668 struct line_header *lh)
2e276125 21669{
233d95b5
JK
21670 /* File name relative to the compilation directory of this source file. */
21671 char *file_name = file_file_name (file, lh);
2e276125 21672
2e276125 21673 if (! current_file)
abc9d0dc 21674 {
fc474241
DE
21675 /* Note: We don't create a macro table for this compilation unit
21676 at all until we actually get a filename. */
43f3e411 21677 struct macro_table *macro_table = get_macro_table ();
fc474241 21678
abc9d0dc
TT
21679 /* If we have no current file, then this must be the start_file
21680 directive for the compilation unit's main source file. */
fc474241
DE
21681 current_file = macro_set_main (macro_table, file_name);
21682 macro_define_special (macro_table);
abc9d0dc 21683 }
2e276125 21684 else
233d95b5 21685 current_file = macro_include (current_file, line, file_name);
2e276125 21686
233d95b5 21687 xfree (file_name);
6e70227d 21688
2e276125
JB
21689 return current_file;
21690}
21691
21692
21693/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21694 followed by a null byte. */
21695static char *
21696copy_string (const char *buf, int len)
21697{
224c3ddb 21698 char *s = (char *) xmalloc (len + 1);
9a619af0 21699
2e276125
JB
21700 memcpy (s, buf, len);
21701 s[len] = '\0';
2e276125
JB
21702 return s;
21703}
21704
21705
21706static const char *
21707consume_improper_spaces (const char *p, const char *body)
21708{
21709 if (*p == ' ')
21710 {
4d3c2250 21711 complaint (&symfile_complaints,
3e43a32a
MS
21712 _("macro definition contains spaces "
21713 "in formal argument list:\n`%s'"),
4d3c2250 21714 body);
2e276125
JB
21715
21716 while (*p == ' ')
21717 p++;
21718 }
21719
21720 return p;
21721}
21722
21723
21724static void
21725parse_macro_definition (struct macro_source_file *file, int line,
21726 const char *body)
21727{
21728 const char *p;
21729
21730 /* The body string takes one of two forms. For object-like macro
21731 definitions, it should be:
21732
21733 <macro name> " " <definition>
21734
21735 For function-like macro definitions, it should be:
21736
21737 <macro name> "() " <definition>
21738 or
21739 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21740
21741 Spaces may appear only where explicitly indicated, and in the
21742 <definition>.
21743
21744 The Dwarf 2 spec says that an object-like macro's name is always
21745 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21746 the space when the macro's definition is the empty string.
2e276125
JB
21747
21748 The Dwarf 2 spec says that there should be no spaces between the
21749 formal arguments in a function-like macro's formal argument list,
21750 but versions of GCC around March 2002 include spaces after the
21751 commas. */
21752
21753
21754 /* Find the extent of the macro name. The macro name is terminated
21755 by either a space or null character (for an object-like macro) or
21756 an opening paren (for a function-like macro). */
21757 for (p = body; *p; p++)
21758 if (*p == ' ' || *p == '(')
21759 break;
21760
21761 if (*p == ' ' || *p == '\0')
21762 {
21763 /* It's an object-like macro. */
21764 int name_len = p - body;
21765 char *name = copy_string (body, name_len);
21766 const char *replacement;
21767
21768 if (*p == ' ')
21769 replacement = body + name_len + 1;
21770 else
21771 {
4d3c2250 21772 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21773 replacement = body + name_len;
21774 }
6e70227d 21775
2e276125
JB
21776 macro_define_object (file, line, name, replacement);
21777
21778 xfree (name);
21779 }
21780 else if (*p == '(')
21781 {
21782 /* It's a function-like macro. */
21783 char *name = copy_string (body, p - body);
21784 int argc = 0;
21785 int argv_size = 1;
8d749320 21786 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21787
21788 p++;
21789
21790 p = consume_improper_spaces (p, body);
21791
21792 /* Parse the formal argument list. */
21793 while (*p && *p != ')')
21794 {
21795 /* Find the extent of the current argument name. */
21796 const char *arg_start = p;
21797
21798 while (*p && *p != ',' && *p != ')' && *p != ' ')
21799 p++;
21800
21801 if (! *p || p == arg_start)
4d3c2250 21802 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21803 else
21804 {
21805 /* Make sure argv has room for the new argument. */
21806 if (argc >= argv_size)
21807 {
21808 argv_size *= 2;
224c3ddb 21809 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21810 }
21811
21812 argv[argc++] = copy_string (arg_start, p - arg_start);
21813 }
21814
21815 p = consume_improper_spaces (p, body);
21816
21817 /* Consume the comma, if present. */
21818 if (*p == ',')
21819 {
21820 p++;
21821
21822 p = consume_improper_spaces (p, body);
21823 }
21824 }
21825
21826 if (*p == ')')
21827 {
21828 p++;
21829
21830 if (*p == ' ')
21831 /* Perfectly formed definition, no complaints. */
21832 macro_define_function (file, line, name,
6e70227d 21833 argc, (const char **) argv,
2e276125
JB
21834 p + 1);
21835 else if (*p == '\0')
21836 {
21837 /* Complain, but do define it. */
4d3c2250 21838 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21839 macro_define_function (file, line, name,
6e70227d 21840 argc, (const char **) argv,
2e276125
JB
21841 p);
21842 }
21843 else
21844 /* Just complain. */
4d3c2250 21845 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21846 }
21847 else
21848 /* Just complain. */
4d3c2250 21849 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21850
21851 xfree (name);
21852 {
21853 int i;
21854
21855 for (i = 0; i < argc; i++)
21856 xfree (argv[i]);
21857 }
21858 xfree (argv);
21859 }
21860 else
4d3c2250 21861 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21862}
21863
cf2c3c16
TT
21864/* Skip some bytes from BYTES according to the form given in FORM.
21865 Returns the new pointer. */
2e276125 21866
d521ce57
TT
21867static const gdb_byte *
21868skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21869 enum dwarf_form form,
21870 unsigned int offset_size,
21871 struct dwarf2_section_info *section)
2e276125 21872{
cf2c3c16 21873 unsigned int bytes_read;
2e276125 21874
cf2c3c16 21875 switch (form)
2e276125 21876 {
cf2c3c16
TT
21877 case DW_FORM_data1:
21878 case DW_FORM_flag:
21879 ++bytes;
21880 break;
21881
21882 case DW_FORM_data2:
21883 bytes += 2;
21884 break;
21885
21886 case DW_FORM_data4:
21887 bytes += 4;
21888 break;
21889
21890 case DW_FORM_data8:
21891 bytes += 8;
21892 break;
21893
0224619f
JK
21894 case DW_FORM_data16:
21895 bytes += 16;
21896 break;
21897
cf2c3c16
TT
21898 case DW_FORM_string:
21899 read_direct_string (abfd, bytes, &bytes_read);
21900 bytes += bytes_read;
21901 break;
21902
21903 case DW_FORM_sec_offset:
21904 case DW_FORM_strp:
36586728 21905 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21906 bytes += offset_size;
21907 break;
21908
21909 case DW_FORM_block:
21910 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21911 bytes += bytes_read;
21912 break;
21913
21914 case DW_FORM_block1:
21915 bytes += 1 + read_1_byte (abfd, bytes);
21916 break;
21917 case DW_FORM_block2:
21918 bytes += 2 + read_2_bytes (abfd, bytes);
21919 break;
21920 case DW_FORM_block4:
21921 bytes += 4 + read_4_bytes (abfd, bytes);
21922 break;
21923
21924 case DW_FORM_sdata:
21925 case DW_FORM_udata:
3019eac3
DE
21926 case DW_FORM_GNU_addr_index:
21927 case DW_FORM_GNU_str_index:
d521ce57 21928 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21929 if (bytes == NULL)
21930 {
21931 dwarf2_section_buffer_overflow_complaint (section);
21932 return NULL;
21933 }
cf2c3c16
TT
21934 break;
21935
663c44ac
JK
21936 case DW_FORM_implicit_const:
21937 break;
21938
cf2c3c16
TT
21939 default:
21940 {
21941 complain:
21942 complaint (&symfile_complaints,
21943 _("invalid form 0x%x in `%s'"),
a32a8923 21944 form, get_section_name (section));
cf2c3c16
TT
21945 return NULL;
21946 }
2e276125
JB
21947 }
21948
cf2c3c16
TT
21949 return bytes;
21950}
757a13d0 21951
cf2c3c16
TT
21952/* A helper for dwarf_decode_macros that handles skipping an unknown
21953 opcode. Returns an updated pointer to the macro data buffer; or,
21954 on error, issues a complaint and returns NULL. */
757a13d0 21955
d521ce57 21956static const gdb_byte *
cf2c3c16 21957skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21958 const gdb_byte **opcode_definitions,
21959 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21960 bfd *abfd,
21961 unsigned int offset_size,
21962 struct dwarf2_section_info *section)
21963{
21964 unsigned int bytes_read, i;
21965 unsigned long arg;
d521ce57 21966 const gdb_byte *defn;
2e276125 21967
cf2c3c16 21968 if (opcode_definitions[opcode] == NULL)
2e276125 21969 {
cf2c3c16
TT
21970 complaint (&symfile_complaints,
21971 _("unrecognized DW_MACFINO opcode 0x%x"),
21972 opcode);
21973 return NULL;
21974 }
2e276125 21975
cf2c3c16
TT
21976 defn = opcode_definitions[opcode];
21977 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21978 defn += bytes_read;
2e276125 21979
cf2c3c16
TT
21980 for (i = 0; i < arg; ++i)
21981 {
aead7601
SM
21982 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21983 (enum dwarf_form) defn[i], offset_size,
f664829e 21984 section);
cf2c3c16
TT
21985 if (mac_ptr == NULL)
21986 {
21987 /* skip_form_bytes already issued the complaint. */
21988 return NULL;
21989 }
21990 }
757a13d0 21991
cf2c3c16
TT
21992 return mac_ptr;
21993}
757a13d0 21994
cf2c3c16
TT
21995/* A helper function which parses the header of a macro section.
21996 If the macro section is the extended (for now called "GNU") type,
21997 then this updates *OFFSET_SIZE. Returns a pointer to just after
21998 the header, or issues a complaint and returns NULL on error. */
757a13d0 21999
d521ce57
TT
22000static const gdb_byte *
22001dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22002 bfd *abfd,
d521ce57 22003 const gdb_byte *mac_ptr,
cf2c3c16
TT
22004 unsigned int *offset_size,
22005 int section_is_gnu)
22006{
22007 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22008
cf2c3c16
TT
22009 if (section_is_gnu)
22010 {
22011 unsigned int version, flags;
757a13d0 22012
cf2c3c16 22013 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22014 if (version != 4 && version != 5)
cf2c3c16
TT
22015 {
22016 complaint (&symfile_complaints,
22017 _("unrecognized version `%d' in .debug_macro section"),
22018 version);
22019 return NULL;
22020 }
22021 mac_ptr += 2;
757a13d0 22022
cf2c3c16
TT
22023 flags = read_1_byte (abfd, mac_ptr);
22024 ++mac_ptr;
22025 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22026
cf2c3c16
TT
22027 if ((flags & 2) != 0)
22028 /* We don't need the line table offset. */
22029 mac_ptr += *offset_size;
757a13d0 22030
cf2c3c16
TT
22031 /* Vendor opcode descriptions. */
22032 if ((flags & 4) != 0)
22033 {
22034 unsigned int i, count;
757a13d0 22035
cf2c3c16
TT
22036 count = read_1_byte (abfd, mac_ptr);
22037 ++mac_ptr;
22038 for (i = 0; i < count; ++i)
22039 {
22040 unsigned int opcode, bytes_read;
22041 unsigned long arg;
22042
22043 opcode = read_1_byte (abfd, mac_ptr);
22044 ++mac_ptr;
22045 opcode_definitions[opcode] = mac_ptr;
22046 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22047 mac_ptr += bytes_read;
22048 mac_ptr += arg;
22049 }
757a13d0 22050 }
cf2c3c16 22051 }
757a13d0 22052
cf2c3c16
TT
22053 return mac_ptr;
22054}
757a13d0 22055
cf2c3c16 22056/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22057 including DW_MACRO_import. */
cf2c3c16
TT
22058
22059static void
d521ce57
TT
22060dwarf_decode_macro_bytes (bfd *abfd,
22061 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22062 struct macro_source_file *current_file,
43f3e411 22063 struct line_header *lh,
cf2c3c16 22064 struct dwarf2_section_info *section,
36586728 22065 int section_is_gnu, int section_is_dwz,
cf2c3c16 22066 unsigned int offset_size,
8fc3fc34 22067 htab_t include_hash)
cf2c3c16 22068{
4d663531 22069 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22070 enum dwarf_macro_record_type macinfo_type;
22071 int at_commandline;
d521ce57 22072 const gdb_byte *opcode_definitions[256];
757a13d0 22073
cf2c3c16
TT
22074 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22075 &offset_size, section_is_gnu);
22076 if (mac_ptr == NULL)
22077 {
22078 /* We already issued a complaint. */
22079 return;
22080 }
757a13d0
JK
22081
22082 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22083 GDB is still reading the definitions from command line. First
22084 DW_MACINFO_start_file will need to be ignored as it was already executed
22085 to create CURRENT_FILE for the main source holding also the command line
22086 definitions. On first met DW_MACINFO_start_file this flag is reset to
22087 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22088
22089 at_commandline = 1;
22090
22091 do
22092 {
22093 /* Do we at least have room for a macinfo type byte? */
22094 if (mac_ptr >= mac_end)
22095 {
f664829e 22096 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22097 break;
22098 }
22099
aead7601 22100 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22101 mac_ptr++;
22102
cf2c3c16
TT
22103 /* Note that we rely on the fact that the corresponding GNU and
22104 DWARF constants are the same. */
757a13d0
JK
22105 switch (macinfo_type)
22106 {
22107 /* A zero macinfo type indicates the end of the macro
22108 information. */
22109 case 0:
22110 break;
2e276125 22111
0af92d60
JK
22112 case DW_MACRO_define:
22113 case DW_MACRO_undef:
22114 case DW_MACRO_define_strp:
22115 case DW_MACRO_undef_strp:
22116 case DW_MACRO_define_sup:
22117 case DW_MACRO_undef_sup:
2e276125 22118 {
891d2f0b 22119 unsigned int bytes_read;
2e276125 22120 int line;
d521ce57 22121 const char *body;
cf2c3c16 22122 int is_define;
2e276125 22123
cf2c3c16
TT
22124 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22125 mac_ptr += bytes_read;
22126
0af92d60
JK
22127 if (macinfo_type == DW_MACRO_define
22128 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22129 {
22130 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22131 mac_ptr += bytes_read;
22132 }
22133 else
22134 {
22135 LONGEST str_offset;
22136
22137 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22138 mac_ptr += offset_size;
2e276125 22139
0af92d60
JK
22140 if (macinfo_type == DW_MACRO_define_sup
22141 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22142 || section_is_dwz)
36586728
TT
22143 {
22144 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22145
22146 body = read_indirect_string_from_dwz (dwz, str_offset);
22147 }
22148 else
22149 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22150 }
22151
0af92d60
JK
22152 is_define = (macinfo_type == DW_MACRO_define
22153 || macinfo_type == DW_MACRO_define_strp
22154 || macinfo_type == DW_MACRO_define_sup);
2e276125 22155 if (! current_file)
757a13d0
JK
22156 {
22157 /* DWARF violation as no main source is present. */
22158 complaint (&symfile_complaints,
22159 _("debug info with no main source gives macro %s "
22160 "on line %d: %s"),
cf2c3c16
TT
22161 is_define ? _("definition") : _("undefinition"),
22162 line, body);
757a13d0
JK
22163 break;
22164 }
3e43a32a
MS
22165 if ((line == 0 && !at_commandline)
22166 || (line != 0 && at_commandline))
4d3c2250 22167 complaint (&symfile_complaints,
757a13d0
JK
22168 _("debug info gives %s macro %s with %s line %d: %s"),
22169 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22170 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22171 line == 0 ? _("zero") : _("non-zero"), line, body);
22172
cf2c3c16 22173 if (is_define)
757a13d0 22174 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22175 else
22176 {
0af92d60
JK
22177 gdb_assert (macinfo_type == DW_MACRO_undef
22178 || macinfo_type == DW_MACRO_undef_strp
22179 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22180 macro_undef (current_file, line, body);
22181 }
2e276125
JB
22182 }
22183 break;
22184
0af92d60 22185 case DW_MACRO_start_file:
2e276125 22186 {
891d2f0b 22187 unsigned int bytes_read;
2e276125
JB
22188 int line, file;
22189
22190 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22191 mac_ptr += bytes_read;
22192 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22193 mac_ptr += bytes_read;
22194
3e43a32a
MS
22195 if ((line == 0 && !at_commandline)
22196 || (line != 0 && at_commandline))
757a13d0
JK
22197 complaint (&symfile_complaints,
22198 _("debug info gives source %d included "
22199 "from %s at %s line %d"),
22200 file, at_commandline ? _("command-line") : _("file"),
22201 line == 0 ? _("zero") : _("non-zero"), line);
22202
22203 if (at_commandline)
22204 {
0af92d60 22205 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22206 pass one. */
757a13d0
JK
22207 at_commandline = 0;
22208 }
22209 else
43f3e411 22210 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22211 }
22212 break;
22213
0af92d60 22214 case DW_MACRO_end_file:
2e276125 22215 if (! current_file)
4d3c2250 22216 complaint (&symfile_complaints,
3e43a32a
MS
22217 _("macro debug info has an unmatched "
22218 "`close_file' directive"));
2e276125
JB
22219 else
22220 {
22221 current_file = current_file->included_by;
22222 if (! current_file)
22223 {
cf2c3c16 22224 enum dwarf_macro_record_type next_type;
2e276125
JB
22225
22226 /* GCC circa March 2002 doesn't produce the zero
22227 type byte marking the end of the compilation
22228 unit. Complain if it's not there, but exit no
22229 matter what. */
22230
22231 /* Do we at least have room for a macinfo type byte? */
22232 if (mac_ptr >= mac_end)
22233 {
f664829e 22234 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22235 return;
22236 }
22237
22238 /* We don't increment mac_ptr here, so this is just
22239 a look-ahead. */
aead7601
SM
22240 next_type
22241 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22242 mac_ptr);
2e276125 22243 if (next_type != 0)
4d3c2250 22244 complaint (&symfile_complaints,
3e43a32a
MS
22245 _("no terminating 0-type entry for "
22246 "macros in `.debug_macinfo' section"));
2e276125
JB
22247
22248 return;
22249 }
22250 }
22251 break;
22252
0af92d60
JK
22253 case DW_MACRO_import:
22254 case DW_MACRO_import_sup:
cf2c3c16
TT
22255 {
22256 LONGEST offset;
8fc3fc34 22257 void **slot;
a036ba48
TT
22258 bfd *include_bfd = abfd;
22259 struct dwarf2_section_info *include_section = section;
d521ce57 22260 const gdb_byte *include_mac_end = mac_end;
a036ba48 22261 int is_dwz = section_is_dwz;
d521ce57 22262 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22263
22264 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22265 mac_ptr += offset_size;
22266
0af92d60 22267 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22268 {
22269 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22270
4d663531 22271 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22272
a036ba48 22273 include_section = &dwz->macro;
a32a8923 22274 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22275 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22276 is_dwz = 1;
22277 }
22278
22279 new_mac_ptr = include_section->buffer + offset;
22280 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22281
8fc3fc34
TT
22282 if (*slot != NULL)
22283 {
22284 /* This has actually happened; see
22285 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22286 complaint (&symfile_complaints,
0af92d60 22287 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22288 ".debug_macro section"));
22289 }
22290 else
22291 {
d521ce57 22292 *slot = (void *) new_mac_ptr;
36586728 22293
a036ba48 22294 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22295 include_mac_end, current_file, lh,
36586728 22296 section, section_is_gnu, is_dwz,
4d663531 22297 offset_size, include_hash);
8fc3fc34 22298
d521ce57 22299 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22300 }
cf2c3c16
TT
22301 }
22302 break;
22303
2e276125 22304 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22305 if (!section_is_gnu)
22306 {
22307 unsigned int bytes_read;
2e276125 22308
ac298888
TT
22309 /* This reads the constant, but since we don't recognize
22310 any vendor extensions, we ignore it. */
22311 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22312 mac_ptr += bytes_read;
22313 read_direct_string (abfd, mac_ptr, &bytes_read);
22314 mac_ptr += bytes_read;
2e276125 22315
cf2c3c16
TT
22316 /* We don't recognize any vendor extensions. */
22317 break;
22318 }
22319 /* FALLTHROUGH */
22320
22321 default:
22322 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22323 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22324 section);
22325 if (mac_ptr == NULL)
22326 return;
22327 break;
2e276125 22328 }
757a13d0 22329 } while (macinfo_type != 0);
2e276125 22330}
8e19ed76 22331
cf2c3c16 22332static void
09262596 22333dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22334 int section_is_gnu)
cf2c3c16 22335{
bb5ed363 22336 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22337 struct line_header *lh = cu->line_header;
22338 bfd *abfd;
d521ce57 22339 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22340 struct macro_source_file *current_file = 0;
22341 enum dwarf_macro_record_type macinfo_type;
22342 unsigned int offset_size = cu->header.offset_size;
d521ce57 22343 const gdb_byte *opcode_definitions[256];
8fc3fc34 22344 struct cleanup *cleanup;
8fc3fc34 22345 void **slot;
09262596
DE
22346 struct dwarf2_section_info *section;
22347 const char *section_name;
22348
22349 if (cu->dwo_unit != NULL)
22350 {
22351 if (section_is_gnu)
22352 {
22353 section = &cu->dwo_unit->dwo_file->sections.macro;
22354 section_name = ".debug_macro.dwo";
22355 }
22356 else
22357 {
22358 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22359 section_name = ".debug_macinfo.dwo";
22360 }
22361 }
22362 else
22363 {
22364 if (section_is_gnu)
22365 {
22366 section = &dwarf2_per_objfile->macro;
22367 section_name = ".debug_macro";
22368 }
22369 else
22370 {
22371 section = &dwarf2_per_objfile->macinfo;
22372 section_name = ".debug_macinfo";
22373 }
22374 }
cf2c3c16 22375
bb5ed363 22376 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22377 if (section->buffer == NULL)
22378 {
fceca515 22379 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22380 return;
22381 }
a32a8923 22382 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22383
22384 /* First pass: Find the name of the base filename.
22385 This filename is needed in order to process all macros whose definition
22386 (or undefinition) comes from the command line. These macros are defined
22387 before the first DW_MACINFO_start_file entry, and yet still need to be
22388 associated to the base file.
22389
22390 To determine the base file name, we scan the macro definitions until we
22391 reach the first DW_MACINFO_start_file entry. We then initialize
22392 CURRENT_FILE accordingly so that any macro definition found before the
22393 first DW_MACINFO_start_file can still be associated to the base file. */
22394
22395 mac_ptr = section->buffer + offset;
22396 mac_end = section->buffer + section->size;
22397
22398 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22399 &offset_size, section_is_gnu);
22400 if (mac_ptr == NULL)
22401 {
22402 /* We already issued a complaint. */
22403 return;
22404 }
22405
22406 do
22407 {
22408 /* Do we at least have room for a macinfo type byte? */
22409 if (mac_ptr >= mac_end)
22410 {
22411 /* Complaint is printed during the second pass as GDB will probably
22412 stop the first pass earlier upon finding
22413 DW_MACINFO_start_file. */
22414 break;
22415 }
22416
aead7601 22417 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22418 mac_ptr++;
22419
22420 /* Note that we rely on the fact that the corresponding GNU and
22421 DWARF constants are the same. */
22422 switch (macinfo_type)
22423 {
22424 /* A zero macinfo type indicates the end of the macro
22425 information. */
22426 case 0:
22427 break;
22428
0af92d60
JK
22429 case DW_MACRO_define:
22430 case DW_MACRO_undef:
cf2c3c16
TT
22431 /* Only skip the data by MAC_PTR. */
22432 {
22433 unsigned int bytes_read;
22434
22435 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22436 mac_ptr += bytes_read;
22437 read_direct_string (abfd, mac_ptr, &bytes_read);
22438 mac_ptr += bytes_read;
22439 }
22440 break;
22441
0af92d60 22442 case DW_MACRO_start_file:
cf2c3c16
TT
22443 {
22444 unsigned int bytes_read;
22445 int line, file;
22446
22447 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22448 mac_ptr += bytes_read;
22449 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22450 mac_ptr += bytes_read;
22451
43f3e411 22452 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22453 }
22454 break;
22455
0af92d60 22456 case DW_MACRO_end_file:
cf2c3c16
TT
22457 /* No data to skip by MAC_PTR. */
22458 break;
22459
0af92d60
JK
22460 case DW_MACRO_define_strp:
22461 case DW_MACRO_undef_strp:
22462 case DW_MACRO_define_sup:
22463 case DW_MACRO_undef_sup:
cf2c3c16
TT
22464 {
22465 unsigned int bytes_read;
22466
22467 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22468 mac_ptr += bytes_read;
22469 mac_ptr += offset_size;
22470 }
22471 break;
22472
0af92d60
JK
22473 case DW_MACRO_import:
22474 case DW_MACRO_import_sup:
cf2c3c16 22475 /* Note that, according to the spec, a transparent include
0af92d60 22476 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22477 skip this opcode. */
22478 mac_ptr += offset_size;
22479 break;
22480
22481 case DW_MACINFO_vendor_ext:
22482 /* Only skip the data by MAC_PTR. */
22483 if (!section_is_gnu)
22484 {
22485 unsigned int bytes_read;
22486
22487 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22488 mac_ptr += bytes_read;
22489 read_direct_string (abfd, mac_ptr, &bytes_read);
22490 mac_ptr += bytes_read;
22491 }
22492 /* FALLTHROUGH */
22493
22494 default:
22495 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22496 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22497 section);
22498 if (mac_ptr == NULL)
22499 return;
22500 break;
22501 }
22502 } while (macinfo_type != 0 && current_file == NULL);
22503
22504 /* Second pass: Process all entries.
22505
22506 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22507 command-line macro definitions/undefinitions. This flag is unset when we
22508 reach the first DW_MACINFO_start_file entry. */
22509
fc4007c9
TT
22510 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22511 htab_eq_pointer,
22512 NULL, xcalloc, xfree));
8fc3fc34 22513 mac_ptr = section->buffer + offset;
fc4007c9 22514 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22515 *slot = (void *) mac_ptr;
8fc3fc34 22516 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22517 current_file, lh, section,
fc4007c9
TT
22518 section_is_gnu, 0, offset_size,
22519 include_hash.get ());
cf2c3c16
TT
22520}
22521
8e19ed76 22522/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22523 if so return true else false. */
380bca97 22524
8e19ed76 22525static int
6e5a29e1 22526attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22527{
22528 return (attr == NULL ? 0 :
22529 attr->form == DW_FORM_block1
22530 || attr->form == DW_FORM_block2
22531 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22532 || attr->form == DW_FORM_block
22533 || attr->form == DW_FORM_exprloc);
8e19ed76 22534}
4c2df51b 22535
c6a0999f
JB
22536/* Return non-zero if ATTR's value is a section offset --- classes
22537 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22538 You may use DW_UNSND (attr) to retrieve such offsets.
22539
22540 Section 7.5.4, "Attribute Encodings", explains that no attribute
22541 may have a value that belongs to more than one of these classes; it
22542 would be ambiguous if we did, because we use the same forms for all
22543 of them. */
380bca97 22544
3690dd37 22545static int
6e5a29e1 22546attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22547{
22548 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22549 || attr->form == DW_FORM_data8
22550 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22551}
22552
3690dd37
JB
22553/* Return non-zero if ATTR's value falls in the 'constant' class, or
22554 zero otherwise. When this function returns true, you can apply
22555 dwarf2_get_attr_constant_value to it.
22556
22557 However, note that for some attributes you must check
22558 attr_form_is_section_offset before using this test. DW_FORM_data4
22559 and DW_FORM_data8 are members of both the constant class, and of
22560 the classes that contain offsets into other debug sections
22561 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22562 that, if an attribute's can be either a constant or one of the
22563 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22564 taken as section offsets, not constants.
22565
22566 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22567 cannot handle that. */
380bca97 22568
3690dd37 22569static int
6e5a29e1 22570attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22571{
22572 switch (attr->form)
22573 {
22574 case DW_FORM_sdata:
22575 case DW_FORM_udata:
22576 case DW_FORM_data1:
22577 case DW_FORM_data2:
22578 case DW_FORM_data4:
22579 case DW_FORM_data8:
663c44ac 22580 case DW_FORM_implicit_const:
3690dd37
JB
22581 return 1;
22582 default:
22583 return 0;
22584 }
22585}
22586
7771576e
SA
22587
22588/* DW_ADDR is always stored already as sect_offset; despite for the forms
22589 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22590
22591static int
6e5a29e1 22592attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22593{
22594 switch (attr->form)
22595 {
22596 case DW_FORM_ref_addr:
22597 case DW_FORM_ref1:
22598 case DW_FORM_ref2:
22599 case DW_FORM_ref4:
22600 case DW_FORM_ref8:
22601 case DW_FORM_ref_udata:
22602 case DW_FORM_GNU_ref_alt:
22603 return 1;
22604 default:
22605 return 0;
22606 }
22607}
22608
3019eac3
DE
22609/* Return the .debug_loc section to use for CU.
22610 For DWO files use .debug_loc.dwo. */
22611
22612static struct dwarf2_section_info *
22613cu_debug_loc_section (struct dwarf2_cu *cu)
22614{
22615 if (cu->dwo_unit)
43988095
JK
22616 {
22617 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22618
22619 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22620 }
22621 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22622 : &dwarf2_per_objfile->loc);
3019eac3
DE
22623}
22624
8cf6f0b1
TT
22625/* A helper function that fills in a dwarf2_loclist_baton. */
22626
22627static void
22628fill_in_loclist_baton (struct dwarf2_cu *cu,
22629 struct dwarf2_loclist_baton *baton,
ff39bb5e 22630 const struct attribute *attr)
8cf6f0b1 22631{
3019eac3
DE
22632 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22633
22634 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22635
22636 baton->per_cu = cu->per_cu;
22637 gdb_assert (baton->per_cu);
22638 /* We don't know how long the location list is, but make sure we
22639 don't run off the edge of the section. */
3019eac3
DE
22640 baton->size = section->size - DW_UNSND (attr);
22641 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22642 baton->base_address = cu->base_address;
f664829e 22643 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22644}
22645
4c2df51b 22646static void
ff39bb5e 22647dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22648 struct dwarf2_cu *cu, int is_block)
4c2df51b 22649{
bb5ed363 22650 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22651 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22652
3690dd37 22653 if (attr_form_is_section_offset (attr)
3019eac3 22654 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22655 the section. If so, fall through to the complaint in the
22656 other branch. */
3019eac3 22657 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22658 {
0d53c4c4 22659 struct dwarf2_loclist_baton *baton;
4c2df51b 22660
8d749320 22661 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22662
8cf6f0b1 22663 fill_in_loclist_baton (cu, baton, attr);
be391dca 22664
d00adf39 22665 if (cu->base_known == 0)
0d53c4c4 22666 complaint (&symfile_complaints,
3e43a32a
MS
22667 _("Location list used without "
22668 "specifying the CU base address."));
4c2df51b 22669
f1e6e072
TT
22670 SYMBOL_ACLASS_INDEX (sym) = (is_block
22671 ? dwarf2_loclist_block_index
22672 : dwarf2_loclist_index);
0d53c4c4
DJ
22673 SYMBOL_LOCATION_BATON (sym) = baton;
22674 }
22675 else
22676 {
22677 struct dwarf2_locexpr_baton *baton;
22678
8d749320 22679 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22680 baton->per_cu = cu->per_cu;
22681 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22682
22683 if (attr_form_is_block (attr))
22684 {
22685 /* Note that we're just copying the block's data pointer
22686 here, not the actual data. We're still pointing into the
6502dd73
DJ
22687 info_buffer for SYM's objfile; right now we never release
22688 that buffer, but when we do clean up properly this may
22689 need to change. */
0d53c4c4
DJ
22690 baton->size = DW_BLOCK (attr)->size;
22691 baton->data = DW_BLOCK (attr)->data;
22692 }
22693 else
22694 {
22695 dwarf2_invalid_attrib_class_complaint ("location description",
22696 SYMBOL_NATURAL_NAME (sym));
22697 baton->size = 0;
0d53c4c4 22698 }
6e70227d 22699
f1e6e072
TT
22700 SYMBOL_ACLASS_INDEX (sym) = (is_block
22701 ? dwarf2_locexpr_block_index
22702 : dwarf2_locexpr_index);
0d53c4c4
DJ
22703 SYMBOL_LOCATION_BATON (sym) = baton;
22704 }
4c2df51b 22705}
6502dd73 22706
9aa1f1e3
TT
22707/* Return the OBJFILE associated with the compilation unit CU. If CU
22708 came from a separate debuginfo file, then the master objfile is
22709 returned. */
ae0d2f24
UW
22710
22711struct objfile *
22712dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22713{
9291a0cd 22714 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22715
22716 /* Return the master objfile, so that we can report and look up the
22717 correct file containing this variable. */
22718 if (objfile->separate_debug_objfile_backlink)
22719 objfile = objfile->separate_debug_objfile_backlink;
22720
22721 return objfile;
22722}
22723
96408a79
SA
22724/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22725 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22726 CU_HEADERP first. */
22727
22728static const struct comp_unit_head *
22729per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22730 struct dwarf2_per_cu_data *per_cu)
22731{
d521ce57 22732 const gdb_byte *info_ptr;
96408a79
SA
22733
22734 if (per_cu->cu)
22735 return &per_cu->cu->header;
22736
9c541725 22737 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22738
22739 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22740 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22741 rcuh_kind::COMPILE);
96408a79
SA
22742
22743 return cu_headerp;
22744}
22745
ae0d2f24
UW
22746/* Return the address size given in the compilation unit header for CU. */
22747
98714339 22748int
ae0d2f24
UW
22749dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22750{
96408a79
SA
22751 struct comp_unit_head cu_header_local;
22752 const struct comp_unit_head *cu_headerp;
c471e790 22753
96408a79
SA
22754 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22755
22756 return cu_headerp->addr_size;
ae0d2f24
UW
22757}
22758
9eae7c52
TT
22759/* Return the offset size given in the compilation unit header for CU. */
22760
22761int
22762dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22763{
96408a79
SA
22764 struct comp_unit_head cu_header_local;
22765 const struct comp_unit_head *cu_headerp;
9c6c53f7 22766
96408a79
SA
22767 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22768
22769 return cu_headerp->offset_size;
22770}
22771
22772/* See its dwarf2loc.h declaration. */
22773
22774int
22775dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22776{
22777 struct comp_unit_head cu_header_local;
22778 const struct comp_unit_head *cu_headerp;
22779
22780 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22781
22782 if (cu_headerp->version == 2)
22783 return cu_headerp->addr_size;
22784 else
22785 return cu_headerp->offset_size;
181cebd4
JK
22786}
22787
9aa1f1e3
TT
22788/* Return the text offset of the CU. The returned offset comes from
22789 this CU's objfile. If this objfile came from a separate debuginfo
22790 file, then the offset may be different from the corresponding
22791 offset in the parent objfile. */
22792
22793CORE_ADDR
22794dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22795{
bb3fa9d0 22796 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22797
22798 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22799}
22800
43988095
JK
22801/* Return DWARF version number of PER_CU. */
22802
22803short
22804dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22805{
22806 return per_cu->dwarf_version;
22807}
22808
348e048f
DE
22809/* Locate the .debug_info compilation unit from CU's objfile which contains
22810 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22811
22812static struct dwarf2_per_cu_data *
9c541725 22813dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22814 unsigned int offset_in_dwz,
ae038cb0
DJ
22815 struct objfile *objfile)
22816{
22817 struct dwarf2_per_cu_data *this_cu;
22818 int low, high;
36586728 22819 const sect_offset *cu_off;
ae038cb0 22820
ae038cb0
DJ
22821 low = 0;
22822 high = dwarf2_per_objfile->n_comp_units - 1;
22823 while (high > low)
22824 {
36586728 22825 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22826 int mid = low + (high - low) / 2;
9a619af0 22827
36586728 22828 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22829 cu_off = &mid_cu->sect_off;
36586728 22830 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22831 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22832 high = mid;
22833 else
22834 low = mid + 1;
22835 }
22836 gdb_assert (low == high);
36586728 22837 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22838 cu_off = &this_cu->sect_off;
22839 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22840 {
36586728 22841 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22842 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22843 "offset 0x%x [in module %s]"),
22844 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22845
9c541725
PA
22846 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22847 <= sect_off);
ae038cb0
DJ
22848 return dwarf2_per_objfile->all_comp_units[low-1];
22849 }
22850 else
22851 {
22852 this_cu = dwarf2_per_objfile->all_comp_units[low];
22853 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22854 && sect_off >= this_cu->sect_off + this_cu->length)
22855 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22856 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22857 return this_cu;
22858 }
22859}
22860
23745b47 22861/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22862
9816fde3 22863static void
23745b47 22864init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22865{
9816fde3 22866 memset (cu, 0, sizeof (*cu));
23745b47
DE
22867 per_cu->cu = cu;
22868 cu->per_cu = per_cu;
22869 cu->objfile = per_cu->objfile;
93311388 22870 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22871}
22872
22873/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22874
22875static void
95554aad
TT
22876prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22877 enum language pretend_language)
9816fde3
JK
22878{
22879 struct attribute *attr;
22880
22881 /* Set the language we're debugging. */
22882 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22883 if (attr)
22884 set_cu_language (DW_UNSND (attr), cu);
22885 else
9cded63f 22886 {
95554aad 22887 cu->language = pretend_language;
9cded63f
TT
22888 cu->language_defn = language_def (cu->language);
22889 }
dee91e82 22890
7d45c7c3 22891 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22892}
22893
ae038cb0
DJ
22894/* Release one cached compilation unit, CU. We unlink it from the tree
22895 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22896 the caller is responsible for that.
22897 NOTE: DATA is a void * because this function is also used as a
22898 cleanup routine. */
ae038cb0
DJ
22899
22900static void
68dc6402 22901free_heap_comp_unit (void *data)
ae038cb0 22902{
9a3c8263 22903 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22904
23745b47
DE
22905 gdb_assert (cu->per_cu != NULL);
22906 cu->per_cu->cu = NULL;
ae038cb0
DJ
22907 cu->per_cu = NULL;
22908
22909 obstack_free (&cu->comp_unit_obstack, NULL);
22910
22911 xfree (cu);
22912}
22913
72bf9492 22914/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22915 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22916 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22917
22918static void
22919free_stack_comp_unit (void *data)
22920{
9a3c8263 22921 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22922
23745b47
DE
22923 gdb_assert (cu->per_cu != NULL);
22924 cu->per_cu->cu = NULL;
22925 cu->per_cu = NULL;
22926
72bf9492
DJ
22927 obstack_free (&cu->comp_unit_obstack, NULL);
22928 cu->partial_dies = NULL;
ae038cb0
DJ
22929}
22930
22931/* Free all cached compilation units. */
22932
22933static void
22934free_cached_comp_units (void *data)
22935{
330cdd98 22936 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22937}
22938
22939/* Increase the age counter on each cached compilation unit, and free
22940 any that are too old. */
22941
22942static void
22943age_cached_comp_units (void)
22944{
22945 struct dwarf2_per_cu_data *per_cu, **last_chain;
22946
22947 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22948 per_cu = dwarf2_per_objfile->read_in_chain;
22949 while (per_cu != NULL)
22950 {
22951 per_cu->cu->last_used ++;
b4f54984 22952 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22953 dwarf2_mark (per_cu->cu);
22954 per_cu = per_cu->cu->read_in_chain;
22955 }
22956
22957 per_cu = dwarf2_per_objfile->read_in_chain;
22958 last_chain = &dwarf2_per_objfile->read_in_chain;
22959 while (per_cu != NULL)
22960 {
22961 struct dwarf2_per_cu_data *next_cu;
22962
22963 next_cu = per_cu->cu->read_in_chain;
22964
22965 if (!per_cu->cu->mark)
22966 {
68dc6402 22967 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22968 *last_chain = next_cu;
22969 }
22970 else
22971 last_chain = &per_cu->cu->read_in_chain;
22972
22973 per_cu = next_cu;
22974 }
22975}
22976
22977/* Remove a single compilation unit from the cache. */
22978
22979static void
dee91e82 22980free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22981{
22982 struct dwarf2_per_cu_data *per_cu, **last_chain;
22983
22984 per_cu = dwarf2_per_objfile->read_in_chain;
22985 last_chain = &dwarf2_per_objfile->read_in_chain;
22986 while (per_cu != NULL)
22987 {
22988 struct dwarf2_per_cu_data *next_cu;
22989
22990 next_cu = per_cu->cu->read_in_chain;
22991
dee91e82 22992 if (per_cu == target_per_cu)
ae038cb0 22993 {
68dc6402 22994 free_heap_comp_unit (per_cu->cu);
dee91e82 22995 per_cu->cu = NULL;
ae038cb0
DJ
22996 *last_chain = next_cu;
22997 break;
22998 }
22999 else
23000 last_chain = &per_cu->cu->read_in_chain;
23001
23002 per_cu = next_cu;
23003 }
23004}
23005
fe3e1990
DJ
23006/* Release all extra memory associated with OBJFILE. */
23007
23008void
23009dwarf2_free_objfile (struct objfile *objfile)
23010{
9a3c8263
SM
23011 dwarf2_per_objfile
23012 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23013 dwarf2_objfile_data_key);
fe3e1990
DJ
23014
23015 if (dwarf2_per_objfile == NULL)
23016 return;
23017
330cdd98 23018 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23019}
23020
dee91e82
DE
23021/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23022 We store these in a hash table separate from the DIEs, and preserve them
23023 when the DIEs are flushed out of cache.
23024
23025 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23026 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23027 or the type may come from a DWO file. Furthermore, while it's more logical
23028 to use per_cu->section+offset, with Fission the section with the data is in
23029 the DWO file but we don't know that section at the point we need it.
23030 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23031 because we can enter the lookup routine, get_die_type_at_offset, from
23032 outside this file, and thus won't necessarily have PER_CU->cu.
23033 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23034
dee91e82 23035struct dwarf2_per_cu_offset_and_type
1c379e20 23036{
dee91e82 23037 const struct dwarf2_per_cu_data *per_cu;
9c541725 23038 sect_offset sect_off;
1c379e20
DJ
23039 struct type *type;
23040};
23041
dee91e82 23042/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23043
23044static hashval_t
dee91e82 23045per_cu_offset_and_type_hash (const void *item)
1c379e20 23046{
9a3c8263
SM
23047 const struct dwarf2_per_cu_offset_and_type *ofs
23048 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23049
9c541725 23050 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23051}
23052
dee91e82 23053/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23054
23055static int
dee91e82 23056per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23057{
9a3c8263
SM
23058 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23059 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23060 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23061 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23062
dee91e82 23063 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23064 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23065}
23066
23067/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23068 table if necessary. For convenience, return TYPE.
23069
23070 The DIEs reading must have careful ordering to:
23071 * Not cause infite loops trying to read in DIEs as a prerequisite for
23072 reading current DIE.
23073 * Not trying to dereference contents of still incompletely read in types
23074 while reading in other DIEs.
23075 * Enable referencing still incompletely read in types just by a pointer to
23076 the type without accessing its fields.
23077
23078 Therefore caller should follow these rules:
23079 * Try to fetch any prerequisite types we may need to build this DIE type
23080 before building the type and calling set_die_type.
e71ec853 23081 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23082 possible before fetching more types to complete the current type.
23083 * Make the type as complete as possible before fetching more types. */
1c379e20 23084
f792889a 23085static struct type *
1c379e20
DJ
23086set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23087{
dee91e82 23088 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23089 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23090 struct attribute *attr;
23091 struct dynamic_prop prop;
1c379e20 23092
b4ba55a1
JB
23093 /* For Ada types, make sure that the gnat-specific data is always
23094 initialized (if not already set). There are a few types where
23095 we should not be doing so, because the type-specific area is
23096 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23097 where the type-specific area is used to store the floatformat).
23098 But this is not a problem, because the gnat-specific information
23099 is actually not needed for these types. */
23100 if (need_gnat_info (cu)
23101 && TYPE_CODE (type) != TYPE_CODE_FUNC
23102 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23103 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23104 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23105 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23106 && !HAVE_GNAT_AUX_INFO (type))
23107 INIT_GNAT_SPECIFIC (type);
23108
3f2f83dd
KB
23109 /* Read DW_AT_allocated and set in type. */
23110 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23111 if (attr_form_is_block (attr))
23112 {
23113 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23114 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23115 }
23116 else if (attr != NULL)
23117 {
23118 complaint (&symfile_complaints,
9c541725
PA
23119 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23120 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23121 to_underlying (die->sect_off));
3f2f83dd
KB
23122 }
23123
23124 /* Read DW_AT_associated and set in type. */
23125 attr = dwarf2_attr (die, DW_AT_associated, cu);
23126 if (attr_form_is_block (attr))
23127 {
23128 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23129 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23130 }
23131 else if (attr != NULL)
23132 {
23133 complaint (&symfile_complaints,
9c541725
PA
23134 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23135 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23136 to_underlying (die->sect_off));
3f2f83dd
KB
23137 }
23138
3cdcd0ce
JB
23139 /* Read DW_AT_data_location and set in type. */
23140 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23141 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23142 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23143
dee91e82 23144 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23145 {
dee91e82
DE
23146 dwarf2_per_objfile->die_type_hash =
23147 htab_create_alloc_ex (127,
23148 per_cu_offset_and_type_hash,
23149 per_cu_offset_and_type_eq,
23150 NULL,
23151 &objfile->objfile_obstack,
23152 hashtab_obstack_allocate,
23153 dummy_obstack_deallocate);
f792889a 23154 }
1c379e20 23155
dee91e82 23156 ofs.per_cu = cu->per_cu;
9c541725 23157 ofs.sect_off = die->sect_off;
1c379e20 23158 ofs.type = type;
dee91e82
DE
23159 slot = (struct dwarf2_per_cu_offset_and_type **)
23160 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23161 if (*slot)
23162 complaint (&symfile_complaints,
23163 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23164 to_underlying (die->sect_off));
8d749320
SM
23165 *slot = XOBNEW (&objfile->objfile_obstack,
23166 struct dwarf2_per_cu_offset_and_type);
1c379e20 23167 **slot = ofs;
f792889a 23168 return type;
1c379e20
DJ
23169}
23170
9c541725 23171/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23172 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23173
23174static struct type *
9c541725 23175get_die_type_at_offset (sect_offset sect_off,
673bfd45 23176 struct dwarf2_per_cu_data *per_cu)
1c379e20 23177{
dee91e82 23178 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23179
dee91e82 23180 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23181 return NULL;
1c379e20 23182
dee91e82 23183 ofs.per_cu = per_cu;
9c541725 23184 ofs.sect_off = sect_off;
9a3c8263
SM
23185 slot = ((struct dwarf2_per_cu_offset_and_type *)
23186 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23187 if (slot)
23188 return slot->type;
23189 else
23190 return NULL;
23191}
23192
02142a6c 23193/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23194 or return NULL if DIE does not have a saved type. */
23195
23196static struct type *
23197get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23198{
9c541725 23199 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23200}
23201
10b3939b
DJ
23202/* Add a dependence relationship from CU to REF_PER_CU. */
23203
23204static void
23205dwarf2_add_dependence (struct dwarf2_cu *cu,
23206 struct dwarf2_per_cu_data *ref_per_cu)
23207{
23208 void **slot;
23209
23210 if (cu->dependencies == NULL)
23211 cu->dependencies
23212 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23213 NULL, &cu->comp_unit_obstack,
23214 hashtab_obstack_allocate,
23215 dummy_obstack_deallocate);
23216
23217 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23218 if (*slot == NULL)
23219 *slot = ref_per_cu;
23220}
1c379e20 23221
f504f079
DE
23222/* Subroutine of dwarf2_mark to pass to htab_traverse.
23223 Set the mark field in every compilation unit in the
ae038cb0
DJ
23224 cache that we must keep because we are keeping CU. */
23225
10b3939b
DJ
23226static int
23227dwarf2_mark_helper (void **slot, void *data)
23228{
23229 struct dwarf2_per_cu_data *per_cu;
23230
23231 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23232
23233 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23234 reading of the chain. As such dependencies remain valid it is not much
23235 useful to track and undo them during QUIT cleanups. */
23236 if (per_cu->cu == NULL)
23237 return 1;
23238
10b3939b
DJ
23239 if (per_cu->cu->mark)
23240 return 1;
23241 per_cu->cu->mark = 1;
23242
23243 if (per_cu->cu->dependencies != NULL)
23244 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23245
23246 return 1;
23247}
23248
f504f079
DE
23249/* Set the mark field in CU and in every other compilation unit in the
23250 cache that we must keep because we are keeping CU. */
23251
ae038cb0
DJ
23252static void
23253dwarf2_mark (struct dwarf2_cu *cu)
23254{
23255 if (cu->mark)
23256 return;
23257 cu->mark = 1;
10b3939b
DJ
23258 if (cu->dependencies != NULL)
23259 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23260}
23261
23262static void
23263dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23264{
23265 while (per_cu)
23266 {
23267 per_cu->cu->mark = 0;
23268 per_cu = per_cu->cu->read_in_chain;
23269 }
72bf9492
DJ
23270}
23271
72bf9492
DJ
23272/* Trivial hash function for partial_die_info: the hash value of a DIE
23273 is its offset in .debug_info for this objfile. */
23274
23275static hashval_t
23276partial_die_hash (const void *item)
23277{
9a3c8263
SM
23278 const struct partial_die_info *part_die
23279 = (const struct partial_die_info *) item;
9a619af0 23280
9c541725 23281 return to_underlying (part_die->sect_off);
72bf9492
DJ
23282}
23283
23284/* Trivial comparison function for partial_die_info structures: two DIEs
23285 are equal if they have the same offset. */
23286
23287static int
23288partial_die_eq (const void *item_lhs, const void *item_rhs)
23289{
9a3c8263
SM
23290 const struct partial_die_info *part_die_lhs
23291 = (const struct partial_die_info *) item_lhs;
23292 const struct partial_die_info *part_die_rhs
23293 = (const struct partial_die_info *) item_rhs;
9a619af0 23294
9c541725 23295 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23296}
23297
b4f54984
DE
23298static struct cmd_list_element *set_dwarf_cmdlist;
23299static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23300
23301static void
b4f54984 23302set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23303{
b4f54984 23304 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23305 gdb_stdout);
ae038cb0
DJ
23306}
23307
23308static void
b4f54984 23309show_dwarf_cmd (char *args, int from_tty)
6e70227d 23310{
b4f54984 23311 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23312}
23313
4bf44c1c 23314/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23315
23316static void
c1bd65d0 23317dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23318{
9a3c8263 23319 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23320 int ix;
8b70b953 23321
626f2d1c
TT
23322 /* Make sure we don't accidentally use dwarf2_per_objfile while
23323 cleaning up. */
23324 dwarf2_per_objfile = NULL;
23325
59b0c7c1
JB
23326 for (ix = 0; ix < data->n_comp_units; ++ix)
23327 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23328
59b0c7c1 23329 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23330 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23331 data->all_type_units[ix]->per_cu.imported_symtabs);
23332 xfree (data->all_type_units);
95554aad 23333
8b70b953 23334 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23335
23336 if (data->dwo_files)
23337 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23338 if (data->dwp_file)
23339 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23340
23341 if (data->dwz_file && data->dwz_file->dwz_bfd)
23342 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23343}
23344
23345\f
ae2de4f8 23346/* The "save gdb-index" command. */
9291a0cd 23347
bc8f2430
JK
23348/* In-memory buffer to prepare data to be written later to a file. */
23349class data_buf
9291a0cd 23350{
bc8f2430 23351public:
bc8f2430
JK
23352 /* Copy DATA to the end of the buffer. */
23353 template<typename T>
23354 void append_data (const T &data)
23355 {
23356 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23357 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23358 grow (sizeof (data)));
bc8f2430 23359 }
b89be57b 23360
c2f134ac
PA
23361 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23362 terminating zero is appended too. */
bc8f2430
JK
23363 void append_cstr0 (const char *cstr)
23364 {
23365 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23366 std::copy (cstr, cstr + size, grow (size));
23367 }
23368
23369 /* Accept a host-format integer in VAL and append it to the buffer
23370 as a target-format integer which is LEN bytes long. */
23371 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23372 {
23373 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23374 }
9291a0cd 23375
bc8f2430
JK
23376 /* Return the size of the buffer. */
23377 size_t size () const
23378 {
23379 return m_vec.size ();
23380 }
23381
23382 /* Write the buffer to FILE. */
23383 void file_write (FILE *file) const
23384 {
a81e6d4d
PA
23385 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23386 error (_("couldn't write data to file"));
bc8f2430
JK
23387 }
23388
23389private:
c2f134ac
PA
23390 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23391 the start of the new block. */
23392 gdb_byte *grow (size_t size)
23393 {
23394 m_vec.resize (m_vec.size () + size);
23395 return &*m_vec.end () - size;
23396 }
23397
d5722aa2 23398 gdb::byte_vector m_vec;
bc8f2430 23399};
9291a0cd
TT
23400
23401/* An entry in the symbol table. */
23402struct symtab_index_entry
23403{
23404 /* The name of the symbol. */
23405 const char *name;
23406 /* The offset of the name in the constant pool. */
23407 offset_type index_offset;
23408 /* A sorted vector of the indices of all the CUs that hold an object
23409 of this name. */
bc8f2430 23410 std::vector<offset_type> cu_indices;
9291a0cd
TT
23411};
23412
23413/* The symbol table. This is a power-of-2-sized hash table. */
23414struct mapped_symtab
23415{
bc8f2430
JK
23416 mapped_symtab ()
23417 {
23418 data.resize (1024);
23419 }
b89be57b 23420
bc8f2430 23421 offset_type n_elements = 0;
4b76cda9 23422 std::vector<symtab_index_entry> data;
bc8f2430 23423};
9291a0cd 23424
bc8f2430 23425/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23426 the slot.
23427
23428 Function is used only during write_hash_table so no index format backward
23429 compatibility is needed. */
b89be57b 23430
4b76cda9 23431static symtab_index_entry &
9291a0cd
TT
23432find_slot (struct mapped_symtab *symtab, const char *name)
23433{
559a7a62 23434 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23435
bc8f2430
JK
23436 index = hash & (symtab->data.size () - 1);
23437 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23438
23439 for (;;)
23440 {
4b76cda9
PA
23441 if (symtab->data[index].name == NULL
23442 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23443 return symtab->data[index];
23444 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23445 }
23446}
23447
23448/* Expand SYMTAB's hash table. */
b89be57b 23449
9291a0cd
TT
23450static void
23451hash_expand (struct mapped_symtab *symtab)
23452{
bc8f2430 23453 auto old_entries = std::move (symtab->data);
9291a0cd 23454
bc8f2430
JK
23455 symtab->data.clear ();
23456 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23457
bc8f2430 23458 for (auto &it : old_entries)
4b76cda9 23459 if (it.name != NULL)
bc8f2430 23460 {
4b76cda9 23461 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23462 ref = std::move (it);
23463 }
9291a0cd
TT
23464}
23465
156942c7
DE
23466/* Add an entry to SYMTAB. NAME is the name of the symbol.
23467 CU_INDEX is the index of the CU in which the symbol appears.
23468 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23469
9291a0cd
TT
23470static void
23471add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23472 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23473 offset_type cu_index)
23474{
156942c7 23475 offset_type cu_index_and_attrs;
9291a0cd
TT
23476
23477 ++symtab->n_elements;
bc8f2430 23478 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23479 hash_expand (symtab);
23480
4b76cda9
PA
23481 symtab_index_entry &slot = find_slot (symtab, name);
23482 if (slot.name == NULL)
9291a0cd 23483 {
4b76cda9 23484 slot.name = name;
156942c7 23485 /* index_offset is set later. */
9291a0cd 23486 }
156942c7
DE
23487
23488 cu_index_and_attrs = 0;
23489 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23490 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23491 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23492
23493 /* We don't want to record an index value twice as we want to avoid the
23494 duplication.
23495 We process all global symbols and then all static symbols
23496 (which would allow us to avoid the duplication by only having to check
23497 the last entry pushed), but a symbol could have multiple kinds in one CU.
23498 To keep things simple we don't worry about the duplication here and
23499 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23500 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23501}
23502
23503/* Sort and remove duplicates of all symbols' cu_indices lists. */
23504
23505static void
23506uniquify_cu_indices (struct mapped_symtab *symtab)
23507{
4b76cda9 23508 for (auto &entry : symtab->data)
156942c7 23509 {
4b76cda9 23510 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23511 {
4b76cda9 23512 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23513 std::sort (cu_indices.begin (), cu_indices.end ());
23514 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23515 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23516 }
23517 }
9291a0cd
TT
23518}
23519
bc8f2430
JK
23520/* A form of 'const char *' suitable for container keys. Only the
23521 pointer is stored. The strings themselves are compared, not the
23522 pointers. */
23523class c_str_view
9291a0cd 23524{
bc8f2430
JK
23525public:
23526 c_str_view (const char *cstr)
23527 : m_cstr (cstr)
23528 {}
9291a0cd 23529
bc8f2430
JK
23530 bool operator== (const c_str_view &other) const
23531 {
23532 return strcmp (m_cstr, other.m_cstr) == 0;
23533 }
9291a0cd 23534
bc8f2430
JK
23535private:
23536 friend class c_str_view_hasher;
23537 const char *const m_cstr;
23538};
9291a0cd 23539
bc8f2430
JK
23540/* A std::unordered_map::hasher for c_str_view that uses the right
23541 hash function for strings in a mapped index. */
23542class c_str_view_hasher
23543{
23544public:
23545 size_t operator () (const c_str_view &x) const
23546 {
23547 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23548 }
23549};
b89be57b 23550
bc8f2430
JK
23551/* A std::unordered_map::hasher for std::vector<>. */
23552template<typename T>
23553class vector_hasher
9291a0cd 23554{
bc8f2430
JK
23555public:
23556 size_t operator () (const std::vector<T> &key) const
23557 {
23558 return iterative_hash (key.data (),
23559 sizeof (key.front ()) * key.size (), 0);
23560 }
23561};
9291a0cd 23562
bc8f2430
JK
23563/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23564 constant pool entries going into the data buffer CPOOL. */
3876f04e 23565
bc8f2430
JK
23566static void
23567write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23568{
23569 {
23570 /* Elements are sorted vectors of the indices of all the CUs that
23571 hold an object of this name. */
23572 std::unordered_map<std::vector<offset_type>, offset_type,
23573 vector_hasher<offset_type>>
23574 symbol_hash_table;
23575
23576 /* We add all the index vectors to the constant pool first, to
23577 ensure alignment is ok. */
4b76cda9 23578 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23579 {
4b76cda9 23580 if (entry.name == NULL)
bc8f2430 23581 continue;
4b76cda9 23582 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23583
23584 /* Finding before inserting is faster than always trying to
23585 insert, because inserting always allocates a node, does the
23586 lookup, and then destroys the new node if another node
23587 already had the same key. C++17 try_emplace will avoid
23588 this. */
23589 const auto found
4b76cda9 23590 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23591 if (found != symbol_hash_table.end ())
23592 {
4b76cda9 23593 entry.index_offset = found->second;
70a1152b
PA
23594 continue;
23595 }
23596
4b76cda9
PA
23597 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23598 entry.index_offset = cpool.size ();
23599 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23600 for (const auto index : entry.cu_indices)
23601 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23602 }
23603 }
9291a0cd
TT
23604
23605 /* Now write out the hash table. */
bc8f2430 23606 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23607 for (const auto &entry : symtab->data)
9291a0cd
TT
23608 {
23609 offset_type str_off, vec_off;
23610
4b76cda9 23611 if (entry.name != NULL)
9291a0cd 23612 {
4b76cda9 23613 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23614 if (insertpair.second)
4b76cda9 23615 cpool.append_cstr0 (entry.name);
bc8f2430 23616 str_off = insertpair.first->second;
4b76cda9 23617 vec_off = entry.index_offset;
9291a0cd
TT
23618 }
23619 else
23620 {
23621 /* While 0 is a valid constant pool index, it is not valid
23622 to have 0 for both offsets. */
23623 str_off = 0;
23624 vec_off = 0;
23625 }
23626
bc8f2430
JK
23627 output.append_data (MAYBE_SWAP (str_off));
23628 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23629 }
9291a0cd
TT
23630}
23631
bc8f2430 23632typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23633
23634/* Helper struct for building the address table. */
23635struct addrmap_index_data
23636{
bc8f2430
JK
23637 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23638 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23639 {}
23640
0a5429f6 23641 struct objfile *objfile;
bc8f2430
JK
23642 data_buf &addr_vec;
23643 psym_index_map &cu_index_htab;
0a5429f6
DE
23644
23645 /* Non-zero if the previous_* fields are valid.
23646 We can't write an entry until we see the next entry (since it is only then
23647 that we know the end of the entry). */
23648 int previous_valid;
23649 /* Index of the CU in the table of all CUs in the index file. */
23650 unsigned int previous_cu_index;
0963b4bd 23651 /* Start address of the CU. */
0a5429f6
DE
23652 CORE_ADDR previous_cu_start;
23653};
23654
bc8f2430 23655/* Write an address entry to ADDR_VEC. */
b89be57b 23656
9291a0cd 23657static void
bc8f2430 23658add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23659 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23660{
9291a0cd
TT
23661 CORE_ADDR baseaddr;
23662
23663 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23664
c2f134ac
PA
23665 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23666 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23667 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23668}
23669
23670/* Worker function for traversing an addrmap to build the address table. */
23671
23672static int
23673add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23674{
9a3c8263
SM
23675 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23676 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23677
23678 if (data->previous_valid)
bc8f2430 23679 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23680 data->previous_cu_start, start_addr,
23681 data->previous_cu_index);
23682
23683 data->previous_cu_start = start_addr;
23684 if (pst != NULL)
23685 {
bc8f2430
JK
23686 const auto it = data->cu_index_htab.find (pst);
23687 gdb_assert (it != data->cu_index_htab.cend ());
23688 data->previous_cu_index = it->second;
0a5429f6
DE
23689 data->previous_valid = 1;
23690 }
23691 else
bc8f2430 23692 data->previous_valid = 0;
0a5429f6
DE
23693
23694 return 0;
23695}
23696
bc8f2430 23697/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23698 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23699 in the index file. */
23700
23701static void
bc8f2430
JK
23702write_address_map (struct objfile *objfile, data_buf &addr_vec,
23703 psym_index_map &cu_index_htab)
0a5429f6 23704{
bc8f2430 23705 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23706
23707 /* When writing the address table, we have to cope with the fact that
23708 the addrmap iterator only provides the start of a region; we have to
23709 wait until the next invocation to get the start of the next region. */
23710
23711 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23712 addrmap_index_data.previous_valid = 0;
23713
23714 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23715 &addrmap_index_data);
23716
23717 /* It's highly unlikely the last entry (end address = 0xff...ff)
23718 is valid, but we should still handle it.
23719 The end address is recorded as the start of the next region, but that
23720 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23721 anyway. */
23722 if (addrmap_index_data.previous_valid)
bc8f2430 23723 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23724 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23725 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23726}
23727
156942c7
DE
23728/* Return the symbol kind of PSYM. */
23729
23730static gdb_index_symbol_kind
23731symbol_kind (struct partial_symbol *psym)
23732{
23733 domain_enum domain = PSYMBOL_DOMAIN (psym);
23734 enum address_class aclass = PSYMBOL_CLASS (psym);
23735
23736 switch (domain)
23737 {
23738 case VAR_DOMAIN:
23739 switch (aclass)
23740 {
23741 case LOC_BLOCK:
23742 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23743 case LOC_TYPEDEF:
23744 return GDB_INDEX_SYMBOL_KIND_TYPE;
23745 case LOC_COMPUTED:
23746 case LOC_CONST_BYTES:
23747 case LOC_OPTIMIZED_OUT:
23748 case LOC_STATIC:
23749 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23750 case LOC_CONST:
23751 /* Note: It's currently impossible to recognize psyms as enum values
23752 short of reading the type info. For now punt. */
23753 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23754 default:
23755 /* There are other LOC_FOO values that one might want to classify
23756 as variables, but dwarf2read.c doesn't currently use them. */
23757 return GDB_INDEX_SYMBOL_KIND_OTHER;
23758 }
23759 case STRUCT_DOMAIN:
23760 return GDB_INDEX_SYMBOL_KIND_TYPE;
23761 default:
23762 return GDB_INDEX_SYMBOL_KIND_OTHER;
23763 }
23764}
23765
9291a0cd 23766/* Add a list of partial symbols to SYMTAB. */
b89be57b 23767
9291a0cd
TT
23768static void
23769write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23770 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23771 struct partial_symbol **psymp,
23772 int count,
987d643c
TT
23773 offset_type cu_index,
23774 int is_static)
9291a0cd
TT
23775{
23776 for (; count-- > 0; ++psymp)
23777 {
156942c7 23778 struct partial_symbol *psym = *psymp;
987d643c 23779
156942c7 23780 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23781 error (_("Ada is not currently supported by the index"));
987d643c 23782
987d643c 23783 /* Only add a given psymbol once. */
bc8f2430 23784 if (psyms_seen.insert (psym).second)
987d643c 23785 {
156942c7
DE
23786 gdb_index_symbol_kind kind = symbol_kind (psym);
23787
156942c7
DE
23788 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23789 is_static, kind, cu_index);
987d643c 23790 }
9291a0cd
TT
23791 }
23792}
23793
1fd400ff
TT
23794/* A helper struct used when iterating over debug_types. */
23795struct signatured_type_index_data
23796{
bc8f2430
JK
23797 signatured_type_index_data (data_buf &types_list_,
23798 std::unordered_set<partial_symbol *> &psyms_seen_)
23799 : types_list (types_list_), psyms_seen (psyms_seen_)
23800 {}
23801
1fd400ff
TT
23802 struct objfile *objfile;
23803 struct mapped_symtab *symtab;
bc8f2430
JK
23804 data_buf &types_list;
23805 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23806 int cu_index;
23807};
23808
23809/* A helper function that writes a single signatured_type to an
23810 obstack. */
b89be57b 23811
1fd400ff
TT
23812static int
23813write_one_signatured_type (void **slot, void *d)
23814{
9a3c8263
SM
23815 struct signatured_type_index_data *info
23816 = (struct signatured_type_index_data *) d;
1fd400ff 23817 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23818 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23819
23820 write_psymbols (info->symtab,
987d643c 23821 info->psyms_seen,
3e43a32a
MS
23822 info->objfile->global_psymbols.list
23823 + psymtab->globals_offset,
987d643c
TT
23824 psymtab->n_global_syms, info->cu_index,
23825 0);
1fd400ff 23826 write_psymbols (info->symtab,
987d643c 23827 info->psyms_seen,
3e43a32a
MS
23828 info->objfile->static_psymbols.list
23829 + psymtab->statics_offset,
987d643c
TT
23830 psymtab->n_static_syms, info->cu_index,
23831 1);
1fd400ff 23832
c2f134ac
PA
23833 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23834 to_underlying (entry->per_cu.sect_off));
23835 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23836 to_underlying (entry->type_offset_in_tu));
23837 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23838
23839 ++info->cu_index;
23840
23841 return 1;
23842}
23843
e8f8bcb3
PA
23844/* Recurse into all "included" dependencies and count their symbols as
23845 if they appeared in this psymtab. */
23846
23847static void
23848recursively_count_psymbols (struct partial_symtab *psymtab,
23849 size_t &psyms_seen)
23850{
23851 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23852 if (psymtab->dependencies[i]->user != NULL)
23853 recursively_count_psymbols (psymtab->dependencies[i],
23854 psyms_seen);
23855
23856 psyms_seen += psymtab->n_global_syms;
23857 psyms_seen += psymtab->n_static_syms;
23858}
23859
95554aad
TT
23860/* Recurse into all "included" dependencies and write their symbols as
23861 if they appeared in this psymtab. */
23862
23863static void
23864recursively_write_psymbols (struct objfile *objfile,
23865 struct partial_symtab *psymtab,
23866 struct mapped_symtab *symtab,
bc8f2430 23867 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23868 offset_type cu_index)
23869{
23870 int i;
23871
23872 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23873 if (psymtab->dependencies[i]->user != NULL)
23874 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23875 symtab, psyms_seen, cu_index);
23876
23877 write_psymbols (symtab,
23878 psyms_seen,
23879 objfile->global_psymbols.list + psymtab->globals_offset,
23880 psymtab->n_global_syms, cu_index,
23881 0);
23882 write_psymbols (symtab,
23883 psyms_seen,
23884 objfile->static_psymbols.list + psymtab->statics_offset,
23885 psymtab->n_static_syms, cu_index,
23886 1);
23887}
23888
9291a0cd 23889/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23890
9291a0cd
TT
23891static void
23892write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23893{
9291a0cd
TT
23894 if (dwarf2_per_objfile->using_index)
23895 error (_("Cannot use an index to create the index"));
23896
8b70b953
TT
23897 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23898 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23899
260b681b
DE
23900 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23901 return;
23902
bc8f2430 23903 struct stat st;
4262abfb
JK
23904 if (stat (objfile_name (objfile), &st) < 0)
23905 perror_with_name (objfile_name (objfile));
9291a0cd 23906
bc8f2430
JK
23907 std::string filename (std::string (dir) + SLASH_STRING
23908 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23909
d419f42d 23910 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23911 if (!out_file)
bc8f2430 23912 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23913
16b7a719
PA
23914 /* Order matters here; we want FILE to be closed before FILENAME is
23915 unlinked, because on MS-Windows one cannot delete a file that is
23916 still open. (Don't call anything here that might throw until
23917 file_closer is created.) */
bc8f2430 23918 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23919 gdb_file_up close_out_file (out_file);
9291a0cd 23920
bc8f2430
JK
23921 mapped_symtab symtab;
23922 data_buf cu_list;
987d643c 23923
0a5429f6
DE
23924 /* While we're scanning CU's create a table that maps a psymtab pointer
23925 (which is what addrmap records) to its index (which is what is recorded
23926 in the index file). This will later be needed to write the address
23927 table. */
bc8f2430
JK
23928 psym_index_map cu_index_htab;
23929 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23930
23931 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23932 work here. Also, the debug_types entries do not appear in
23933 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23934
23935 /* The psyms_seen set is potentially going to be largish (~40k
23936 elements when indexing a -g3 build of GDB itself). Estimate the
23937 number of elements in order to avoid too many rehashes, which
23938 require rebuilding buckets and thus many trips to
23939 malloc/free. */
23940 size_t psyms_count = 0;
23941 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23942 {
23943 struct dwarf2_per_cu_data *per_cu
23944 = dwarf2_per_objfile->all_comp_units[i];
23945 struct partial_symtab *psymtab = per_cu->v.psymtab;
23946
23947 if (psymtab != NULL && psymtab->user == NULL)
23948 recursively_count_psymbols (psymtab, psyms_count);
23949 }
23950 /* Generating an index for gdb itself shows a ratio of
23951 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23952 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23953 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23954 {
3e43a32a
MS
23955 struct dwarf2_per_cu_data *per_cu
23956 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23957 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23958
92fac807
JK
23959 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23960 It may be referenced from a local scope but in such case it does not
23961 need to be present in .gdb_index. */
23962 if (psymtab == NULL)
23963 continue;
23964
95554aad 23965 if (psymtab->user == NULL)
bc8f2430
JK
23966 recursively_write_psymbols (objfile, psymtab, &symtab,
23967 psyms_seen, i);
9291a0cd 23968
bc8f2430
JK
23969 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23970 gdb_assert (insertpair.second);
9291a0cd 23971
c2f134ac
PA
23972 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23973 to_underlying (per_cu->sect_off));
23974 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23975 }
23976
0a5429f6 23977 /* Dump the address map. */
bc8f2430
JK
23978 data_buf addr_vec;
23979 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23980
1fd400ff 23981 /* Write out the .debug_type entries, if any. */
bc8f2430 23982 data_buf types_cu_list;
1fd400ff
TT
23983 if (dwarf2_per_objfile->signatured_types)
23984 {
bc8f2430
JK
23985 signatured_type_index_data sig_data (types_cu_list,
23986 psyms_seen);
1fd400ff
TT
23987
23988 sig_data.objfile = objfile;
bc8f2430 23989 sig_data.symtab = &symtab;
1fd400ff
TT
23990 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23991 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23992 write_one_signatured_type, &sig_data);
23993 }
23994
156942c7
DE
23995 /* Now that we've processed all symbols we can shrink their cu_indices
23996 lists. */
bc8f2430 23997 uniquify_cu_indices (&symtab);
156942c7 23998
bc8f2430
JK
23999 data_buf symtab_vec, constant_pool;
24000 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24001
bc8f2430
JK
24002 data_buf contents;
24003 const offset_type size_of_contents = 6 * sizeof (offset_type);
24004 offset_type total_len = size_of_contents;
9291a0cd
TT
24005
24006 /* The version number. */
bc8f2430 24007 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24008
24009 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24010 contents.append_data (MAYBE_SWAP (total_len));
24011 total_len += cu_list.size ();
9291a0cd 24012
1fd400ff 24013 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24014 contents.append_data (MAYBE_SWAP (total_len));
24015 total_len += types_cu_list.size ();
1fd400ff 24016
9291a0cd 24017 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24018 contents.append_data (MAYBE_SWAP (total_len));
24019 total_len += addr_vec.size ();
9291a0cd
TT
24020
24021 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24022 contents.append_data (MAYBE_SWAP (total_len));
24023 total_len += symtab_vec.size ();
9291a0cd
TT
24024
24025 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24026 contents.append_data (MAYBE_SWAP (total_len));
24027 total_len += constant_pool.size ();
9291a0cd 24028
bc8f2430 24029 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24030
bc8f2430
JK
24031 contents.file_write (out_file);
24032 cu_list.file_write (out_file);
24033 types_cu_list.file_write (out_file);
24034 addr_vec.file_write (out_file);
24035 symtab_vec.file_write (out_file);
24036 constant_pool.file_write (out_file);
9291a0cd 24037
bef155c3
TT
24038 /* We want to keep the file. */
24039 unlink_file.keep ();
9291a0cd
TT
24040}
24041
90476074
TT
24042/* Implementation of the `save gdb-index' command.
24043
24044 Note that the file format used by this command is documented in the
24045 GDB manual. Any changes here must be documented there. */
11570e71 24046
9291a0cd
TT
24047static void
24048save_gdb_index_command (char *arg, int from_tty)
24049{
24050 struct objfile *objfile;
24051
24052 if (!arg || !*arg)
96d19272 24053 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24054
24055 ALL_OBJFILES (objfile)
24056 {
24057 struct stat st;
24058
24059 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24060 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24061 continue;
24062
9a3c8263
SM
24063 dwarf2_per_objfile
24064 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24065 dwarf2_objfile_data_key);
9291a0cd
TT
24066 if (dwarf2_per_objfile)
24067 {
9291a0cd 24068
492d29ea 24069 TRY
9291a0cd
TT
24070 {
24071 write_psymtabs_to_index (objfile, arg);
24072 }
492d29ea
PA
24073 CATCH (except, RETURN_MASK_ERROR)
24074 {
24075 exception_fprintf (gdb_stderr, except,
24076 _("Error while writing index for `%s': "),
24077 objfile_name (objfile));
24078 }
24079 END_CATCH
9291a0cd
TT
24080 }
24081 }
dce234bc
PP
24082}
24083
9291a0cd
TT
24084\f
24085
b4f54984 24086int dwarf_always_disassemble;
9eae7c52
TT
24087
24088static void
b4f54984
DE
24089show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24090 struct cmd_list_element *c, const char *value)
9eae7c52 24091{
3e43a32a
MS
24092 fprintf_filtered (file,
24093 _("Whether to always disassemble "
24094 "DWARF expressions is %s.\n"),
9eae7c52
TT
24095 value);
24096}
24097
900e11f9
JK
24098static void
24099show_check_physname (struct ui_file *file, int from_tty,
24100 struct cmd_list_element *c, const char *value)
24101{
24102 fprintf_filtered (file,
24103 _("Whether to check \"physname\" is %s.\n"),
24104 value);
24105}
24106
6502dd73
DJ
24107void _initialize_dwarf2_read (void);
24108
24109void
24110_initialize_dwarf2_read (void)
24111{
96d19272
JK
24112 struct cmd_list_element *c;
24113
dce234bc 24114 dwarf2_objfile_data_key
c1bd65d0 24115 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24116
b4f54984
DE
24117 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24118Set DWARF specific variables.\n\
24119Configure DWARF variables such as the cache size"),
24120 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24121 0/*allow-unknown*/, &maintenance_set_cmdlist);
24122
b4f54984
DE
24123 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24124Show DWARF specific variables\n\
24125Show DWARF variables such as the cache size"),
24126 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24127 0/*allow-unknown*/, &maintenance_show_cmdlist);
24128
24129 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24130 &dwarf_max_cache_age, _("\
24131Set the upper bound on the age of cached DWARF compilation units."), _("\
24132Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24133A higher limit means that cached compilation units will be stored\n\
24134in memory longer, and more total memory will be used. Zero disables\n\
24135caching, which can slow down startup."),
2c5b56ce 24136 NULL,
b4f54984
DE
24137 show_dwarf_max_cache_age,
24138 &set_dwarf_cmdlist,
24139 &show_dwarf_cmdlist);
d97bc12b 24140
9eae7c52 24141 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24142 &dwarf_always_disassemble, _("\
9eae7c52
TT
24143Set whether `info address' always disassembles DWARF expressions."), _("\
24144Show whether `info address' always disassembles DWARF expressions."), _("\
24145When enabled, DWARF expressions are always printed in an assembly-like\n\
24146syntax. When disabled, expressions will be printed in a more\n\
24147conversational style, when possible."),
24148 NULL,
b4f54984
DE
24149 show_dwarf_always_disassemble,
24150 &set_dwarf_cmdlist,
24151 &show_dwarf_cmdlist);
24152
24153 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24154Set debugging of the DWARF reader."), _("\
24155Show debugging of the DWARF reader."), _("\
24156When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24157reading and symtab expansion. A value of 1 (one) provides basic\n\
24158information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24159 NULL,
24160 NULL,
24161 &setdebuglist, &showdebuglist);
24162
b4f54984
DE
24163 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24164Set debugging of the DWARF DIE reader."), _("\
24165Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24166When enabled (non-zero), DIEs are dumped after they are read in.\n\
24167The value is the maximum depth to print."),
ccce17b0
YQ
24168 NULL,
24169 NULL,
24170 &setdebuglist, &showdebuglist);
9291a0cd 24171
27e0867f
DE
24172 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24173Set debugging of the dwarf line reader."), _("\
24174Show debugging of the dwarf line reader."), _("\
24175When enabled (non-zero), line number entries are dumped as they are read in.\n\
24176A value of 1 (one) provides basic information.\n\
24177A value greater than 1 provides more verbose information."),
24178 NULL,
24179 NULL,
24180 &setdebuglist, &showdebuglist);
24181
900e11f9
JK
24182 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24183Set cross-checking of \"physname\" code against demangler."), _("\
24184Show cross-checking of \"physname\" code against demangler."), _("\
24185When enabled, GDB's internal \"physname\" code is checked against\n\
24186the demangler."),
24187 NULL, show_check_physname,
24188 &setdebuglist, &showdebuglist);
24189
e615022a
DE
24190 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24191 no_class, &use_deprecated_index_sections, _("\
24192Set whether to use deprecated gdb_index sections."), _("\
24193Show whether to use deprecated gdb_index sections."), _("\
24194When enabled, deprecated .gdb_index sections are used anyway.\n\
24195Normally they are ignored either because of a missing feature or\n\
24196performance issue.\n\
24197Warning: This option must be enabled before gdb reads the file."),
24198 NULL,
24199 NULL,
24200 &setlist, &showlist);
24201
96d19272 24202 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24203 _("\
fc1a9d6e 24204Save a gdb-index file.\n\
11570e71 24205Usage: save gdb-index DIRECTORY"),
96d19272
JK
24206 &save_cmdlist);
24207 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24208
24209 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24210 &dwarf2_locexpr_funcs);
24211 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24212 &dwarf2_loclist_funcs);
24213
24214 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24215 &dwarf2_block_frame_base_locexpr_funcs);
24216 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24217 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24218}
This page took 3.989737 seconds and 4 git commands to generate.