Fix aarch64-freebsd build failure
[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
d6541620 235 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
236
237 /* Free all cached compilation units. */
238 void free_cached_comp_units ();
239private:
240 /* This function is mapped across the sections and remembers the
241 offset and size of each of the debugging sections we are
242 interested in. */
243 void locate_sections (bfd *abfd, asection *sectp,
244 const dwarf2_debug_sections &names);
245
246public:
247 dwarf2_section_info info {};
248 dwarf2_section_info abbrev {};
249 dwarf2_section_info line {};
250 dwarf2_section_info loc {};
251 dwarf2_section_info loclists {};
252 dwarf2_section_info macinfo {};
253 dwarf2_section_info macro {};
254 dwarf2_section_info str {};
255 dwarf2_section_info line_str {};
256 dwarf2_section_info ranges {};
257 dwarf2_section_info rnglists {};
258 dwarf2_section_info addr {};
259 dwarf2_section_info frame {};
260 dwarf2_section_info eh_frame {};
261 dwarf2_section_info gdb_index {};
262
263 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 264
be391dca 265 /* Back link. */
330cdd98 266 struct objfile *objfile = NULL;
be391dca 267
d467dd73 268 /* Table of all the compilation units. This is used to locate
10b3939b 269 the target compilation unit of a particular reference. */
330cdd98 270 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
271
272 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 273 int n_comp_units = 0;
ae038cb0 274
1fd400ff 275 /* The number of .debug_types-related CUs. */
330cdd98 276 int n_type_units = 0;
1fd400ff 277
6aa5f3a6
DE
278 /* The number of elements allocated in all_type_units.
279 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 280 int n_allocated_type_units = 0;
6aa5f3a6 281
a2ce51a0
DE
282 /* The .debug_types-related CUs (TUs).
283 This is stored in malloc space because we may realloc it. */
330cdd98 284 struct signatured_type **all_type_units = NULL;
1fd400ff 285
f4dc4d17
DE
286 /* Table of struct type_unit_group objects.
287 The hash key is the DW_AT_stmt_list value. */
330cdd98 288 htab_t type_unit_groups {};
72dca2f5 289
348e048f
DE
290 /* A table mapping .debug_types signatures to its signatured_type entry.
291 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 292 htab_t signatured_types {};
348e048f 293
f4dc4d17
DE
294 /* Type unit statistics, to see how well the scaling improvements
295 are doing. */
330cdd98 296 struct tu_stats tu_stats {};
f4dc4d17
DE
297
298 /* A chain of compilation units that are currently read in, so that
299 they can be freed later. */
330cdd98 300 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 301
3019eac3
DE
302 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
303 This is NULL if the table hasn't been allocated yet. */
330cdd98 304 htab_t dwo_files {};
3019eac3 305
330cdd98
PA
306 /* True if we've checked for whether there is a DWP file. */
307 bool dwp_checked = false;
80626a55
DE
308
309 /* The DWP file if there is one, or NULL. */
330cdd98 310 struct dwp_file *dwp_file = NULL;
80626a55 311
36586728
TT
312 /* The shared '.dwz' file, if one exists. This is used when the
313 original data was compressed using 'dwz -m'. */
330cdd98 314 struct dwz_file *dwz_file = NULL;
36586728 315
330cdd98 316 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 317 VMA of 0. */
330cdd98 318 bool has_section_at_zero = false;
9291a0cd 319
ae2de4f8
DE
320 /* True if we are using the mapped index,
321 or we are faking it for OBJF_READNOW's sake. */
330cdd98 322 bool using_index = false;
9291a0cd 323
ae2de4f8 324 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 325 mapped_index *index_table = NULL;
98bfdba5 326
7b9f3c50 327 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
328 TUs typically share line table entries with a CU, so we maintain a
329 separate table of all line table entries to support the sharing.
330 Note that while there can be way more TUs than CUs, we've already
331 sorted all the TUs into "type unit groups", grouped by their
332 DW_AT_stmt_list value. Therefore the only sharing done here is with a
333 CU and its associated TU group if there is one. */
330cdd98 334 htab_t quick_file_names_table {};
7b9f3c50 335
98bfdba5
PA
336 /* Set during partial symbol reading, to prevent queueing of full
337 symbols. */
330cdd98 338 bool reading_partial_symbols = false;
673bfd45 339
dee91e82 340 /* Table mapping type DIEs to their struct type *.
673bfd45 341 This is NULL if not allocated yet.
02142a6c 342 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 343 htab_t die_type_hash {};
95554aad
TT
344
345 /* The CUs we recently read. */
330cdd98 346 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
347
348 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 349 htab_t line_header_hash {};
bbf2f4df
PA
350
351 /* Table containing all filenames. This is an optional because the
352 table is lazily constructed on first access. */
353 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
354};
355
356static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 357
251d32d9 358/* Default names of the debugging sections. */
c906108c 359
233a11ab
CS
360/* Note that if the debugging section has been compressed, it might
361 have a name like .zdebug_info. */
362
9cdd5dbd
DE
363static const struct dwarf2_debug_sections dwarf2_elf_names =
364{
251d32d9
TG
365 { ".debug_info", ".zdebug_info" },
366 { ".debug_abbrev", ".zdebug_abbrev" },
367 { ".debug_line", ".zdebug_line" },
368 { ".debug_loc", ".zdebug_loc" },
43988095 369 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 370 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 371 { ".debug_macro", ".zdebug_macro" },
251d32d9 372 { ".debug_str", ".zdebug_str" },
43988095 373 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 374 { ".debug_ranges", ".zdebug_ranges" },
43988095 375 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 376 { ".debug_types", ".zdebug_types" },
3019eac3 377 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
378 { ".debug_frame", ".zdebug_frame" },
379 { ".eh_frame", NULL },
24d3216f
TT
380 { ".gdb_index", ".zgdb_index" },
381 23
251d32d9 382};
c906108c 383
80626a55 384/* List of DWO/DWP sections. */
3019eac3 385
80626a55 386static const struct dwop_section_names
3019eac3
DE
387{
388 struct dwarf2_section_names abbrev_dwo;
389 struct dwarf2_section_names info_dwo;
390 struct dwarf2_section_names line_dwo;
391 struct dwarf2_section_names loc_dwo;
43988095 392 struct dwarf2_section_names loclists_dwo;
09262596
DE
393 struct dwarf2_section_names macinfo_dwo;
394 struct dwarf2_section_names macro_dwo;
3019eac3
DE
395 struct dwarf2_section_names str_dwo;
396 struct dwarf2_section_names str_offsets_dwo;
397 struct dwarf2_section_names types_dwo;
80626a55
DE
398 struct dwarf2_section_names cu_index;
399 struct dwarf2_section_names tu_index;
3019eac3 400}
80626a55 401dwop_section_names =
3019eac3
DE
402{
403 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
404 { ".debug_info.dwo", ".zdebug_info.dwo" },
405 { ".debug_line.dwo", ".zdebug_line.dwo" },
406 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 407 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
408 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
409 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
410 { ".debug_str.dwo", ".zdebug_str.dwo" },
411 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
412 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
413 { ".debug_cu_index", ".zdebug_cu_index" },
414 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
415};
416
c906108c
SS
417/* local data types */
418
107d2387
AC
419/* The data in a compilation unit header, after target2host
420 translation, looks like this. */
c906108c 421struct comp_unit_head
a738430d 422{
c764a876 423 unsigned int length;
a738430d 424 short version;
a738430d
MK
425 unsigned char addr_size;
426 unsigned char signed_addr_p;
9c541725 427 sect_offset abbrev_sect_off;
57349743 428
a738430d
MK
429 /* Size of file offsets; either 4 or 8. */
430 unsigned int offset_size;
57349743 431
a738430d
MK
432 /* Size of the length field; either 4 or 12. */
433 unsigned int initial_length_size;
57349743 434
43988095
JK
435 enum dwarf_unit_type unit_type;
436
a738430d
MK
437 /* Offset to the first byte of this compilation unit header in the
438 .debug_info section, for resolving relative reference dies. */
9c541725 439 sect_offset sect_off;
57349743 440
d00adf39
DE
441 /* Offset to first die in this cu from the start of the cu.
442 This will be the first byte following the compilation unit header. */
9c541725 443 cu_offset first_die_cu_offset;
43988095
JK
444
445 /* 64-bit signature of this type unit - it is valid only for
446 UNIT_TYPE DW_UT_type. */
447 ULONGEST signature;
448
449 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 450 cu_offset type_cu_offset_in_tu;
a738430d 451};
c906108c 452
3da10d80
KS
453/* Type used for delaying computation of method physnames.
454 See comments for compute_delayed_physnames. */
455struct delayed_method_info
456{
457 /* The type to which the method is attached, i.e., its parent class. */
458 struct type *type;
459
460 /* The index of the method in the type's function fieldlists. */
461 int fnfield_index;
462
463 /* The index of the method in the fieldlist. */
464 int index;
465
466 /* The name of the DIE. */
467 const char *name;
468
469 /* The DIE associated with this method. */
470 struct die_info *die;
471};
472
473typedef struct delayed_method_info delayed_method_info;
474DEF_VEC_O (delayed_method_info);
475
e7c27a73
DJ
476/* Internal state when decoding a particular compilation unit. */
477struct dwarf2_cu
478{
479 /* The objfile containing this compilation unit. */
480 struct objfile *objfile;
481
d00adf39 482 /* The header of the compilation unit. */
e7c27a73 483 struct comp_unit_head header;
e142c38c 484
d00adf39
DE
485 /* Base address of this compilation unit. */
486 CORE_ADDR base_address;
487
488 /* Non-zero if base_address has been set. */
489 int base_known;
490
e142c38c
DJ
491 /* The language we are debugging. */
492 enum language language;
493 const struct language_defn *language_defn;
494
b0f35d58
DL
495 const char *producer;
496
e142c38c
DJ
497 /* The generic symbol table building routines have separate lists for
498 file scope symbols and all all other scopes (local scopes). So
499 we need to select the right one to pass to add_symbol_to_list().
500 We do it by keeping a pointer to the correct list in list_in_scope.
501
502 FIXME: The original dwarf code just treated the file scope as the
503 first local scope, and all other local scopes as nested local
504 scopes, and worked fine. Check to see if we really need to
505 distinguish these in buildsym.c. */
506 struct pending **list_in_scope;
507
433df2d4
DE
508 /* The abbrev table for this CU.
509 Normally this points to the abbrev table in the objfile.
510 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
511 struct abbrev_table *abbrev_table;
72bf9492 512
b64f50a1
JK
513 /* Hash table holding all the loaded partial DIEs
514 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
515 htab_t partial_dies;
516
517 /* Storage for things with the same lifetime as this read-in compilation
518 unit, including partial DIEs. */
519 struct obstack comp_unit_obstack;
520
ae038cb0
DJ
521 /* When multiple dwarf2_cu structures are living in memory, this field
522 chains them all together, so that they can be released efficiently.
523 We will probably also want a generation counter so that most-recently-used
524 compilation units are cached... */
525 struct dwarf2_per_cu_data *read_in_chain;
526
69d751e3 527 /* Backlink to our per_cu entry. */
ae038cb0
DJ
528 struct dwarf2_per_cu_data *per_cu;
529
530 /* How many compilation units ago was this CU last referenced? */
531 int last_used;
532
b64f50a1
JK
533 /* A hash table of DIE cu_offset for following references with
534 die_info->offset.sect_off as hash. */
51545339 535 htab_t die_hash;
10b3939b
DJ
536
537 /* Full DIEs if read in. */
538 struct die_info *dies;
539
540 /* A set of pointers to dwarf2_per_cu_data objects for compilation
541 units referenced by this one. Only set during full symbol processing;
542 partial symbol tables do not have dependencies. */
543 htab_t dependencies;
544
cb1df416
DJ
545 /* Header data from the line table, during full symbol processing. */
546 struct line_header *line_header;
4c8aa72d
PA
547 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
548 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
549 this is the DW_TAG_compile_unit die for this CU. We'll hold on
550 to the line header as long as this DIE is being processed. See
551 process_die_scope. */
552 die_info *line_header_die_owner;
cb1df416 553
3da10d80
KS
554 /* A list of methods which need to have physnames computed
555 after all type information has been read. */
556 VEC (delayed_method_info) *method_list;
557
96408a79
SA
558 /* To be copied to symtab->call_site_htab. */
559 htab_t call_site_htab;
560
034e5797
DE
561 /* Non-NULL if this CU came from a DWO file.
562 There is an invariant here that is important to remember:
563 Except for attributes copied from the top level DIE in the "main"
564 (or "stub") file in preparation for reading the DWO file
565 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
566 Either there isn't a DWO file (in which case this is NULL and the point
567 is moot), or there is and either we're not going to read it (in which
568 case this is NULL) or there is and we are reading it (in which case this
569 is non-NULL). */
3019eac3
DE
570 struct dwo_unit *dwo_unit;
571
572 /* The DW_AT_addr_base attribute if present, zero otherwise
573 (zero is a valid value though).
1dbab08b 574 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
575 ULONGEST addr_base;
576
2e3cf129
DE
577 /* The DW_AT_ranges_base attribute if present, zero otherwise
578 (zero is a valid value though).
1dbab08b 579 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 580 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
581 be used without needing to know whether DWO files are in use or not.
582 N.B. This does not apply to DW_AT_ranges appearing in
583 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
584 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
585 DW_AT_ranges_base *would* have to be applied, and we'd have to care
586 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
587 ULONGEST ranges_base;
588
ae038cb0
DJ
589 /* Mark used when releasing cached dies. */
590 unsigned int mark : 1;
591
8be455d7
JK
592 /* This CU references .debug_loc. See the symtab->locations_valid field.
593 This test is imperfect as there may exist optimized debug code not using
594 any location list and still facing inlining issues if handled as
595 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 596 unsigned int has_loclist : 1;
ba919b58 597
1b80a9fa
JK
598 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
599 if all the producer_is_* fields are valid. This information is cached
600 because profiling CU expansion showed excessive time spent in
601 producer_is_gxx_lt_4_6. */
ba919b58
TT
602 unsigned int checked_producer : 1;
603 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 604 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 605 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
606
607 /* When set, the file that we're processing is known to have
608 debugging info for C++ namespaces. GCC 3.3.x did not produce
609 this information, but later versions do. */
610
611 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
612};
613
10b3939b
DJ
614/* Persistent data held for a compilation unit, even when not
615 processing it. We put a pointer to this structure in the
28dee7f5 616 read_symtab_private field of the psymtab. */
10b3939b 617
ae038cb0
DJ
618struct dwarf2_per_cu_data
619{
36586728 620 /* The start offset and length of this compilation unit.
45452591 621 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
622 initial_length_size.
623 If the DIE refers to a DWO file, this is always of the original die,
624 not the DWO file. */
9c541725 625 sect_offset sect_off;
36586728 626 unsigned int length;
ae038cb0 627
43988095
JK
628 /* DWARF standard version this data has been read from (such as 4 or 5). */
629 short dwarf_version;
630
ae038cb0
DJ
631 /* Flag indicating this compilation unit will be read in before
632 any of the current compilation units are processed. */
c764a876 633 unsigned int queued : 1;
ae038cb0 634
0d99eb77
DE
635 /* This flag will be set when reading partial DIEs if we need to load
636 absolutely all DIEs for this compilation unit, instead of just the ones
637 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
638 hash table and don't find it. */
639 unsigned int load_all_dies : 1;
640
0186c6a7
DE
641 /* Non-zero if this CU is from .debug_types.
642 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
643 this is non-zero. */
3019eac3
DE
644 unsigned int is_debug_types : 1;
645
36586728
TT
646 /* Non-zero if this CU is from the .dwz file. */
647 unsigned int is_dwz : 1;
648
a2ce51a0
DE
649 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
650 This flag is only valid if is_debug_types is true.
651 We can't read a CU directly from a DWO file: There are required
652 attributes in the stub. */
653 unsigned int reading_dwo_directly : 1;
654
7ee85ab1
DE
655 /* Non-zero if the TU has been read.
656 This is used to assist the "Stay in DWO Optimization" for Fission:
657 When reading a DWO, it's faster to read TUs from the DWO instead of
658 fetching them from random other DWOs (due to comdat folding).
659 If the TU has already been read, the optimization is unnecessary
660 (and unwise - we don't want to change where gdb thinks the TU lives
661 "midflight").
662 This flag is only valid if is_debug_types is true. */
663 unsigned int tu_read : 1;
664
3019eac3
DE
665 /* The section this CU/TU lives in.
666 If the DIE refers to a DWO file, this is always the original die,
667 not the DWO file. */
8a0459fd 668 struct dwarf2_section_info *section;
348e048f 669
17ea53c3 670 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
671 of the CU cache it gets reset to NULL again. This is left as NULL for
672 dummy CUs (a CU header, but nothing else). */
ae038cb0 673 struct dwarf2_cu *cu;
1c379e20 674
9cdd5dbd
DE
675 /* The corresponding objfile.
676 Normally we can get the objfile from dwarf2_per_objfile.
677 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
678 struct objfile *objfile;
679
fffbe6a8
YQ
680 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
681 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
682 union
683 {
684 /* The partial symbol table associated with this compilation unit,
95554aad 685 or NULL for unread partial units. */
9291a0cd
TT
686 struct partial_symtab *psymtab;
687
688 /* Data needed by the "quick" functions. */
689 struct dwarf2_per_cu_quick_data *quick;
690 } v;
95554aad 691
796a7ff8
DE
692 /* The CUs we import using DW_TAG_imported_unit. This is filled in
693 while reading psymtabs, used to compute the psymtab dependencies,
694 and then cleared. Then it is filled in again while reading full
695 symbols, and only deleted when the objfile is destroyed.
696
697 This is also used to work around a difference between the way gold
698 generates .gdb_index version <=7 and the way gdb does. Arguably this
699 is a gold bug. For symbols coming from TUs, gold records in the index
700 the CU that includes the TU instead of the TU itself. This breaks
701 dw2_lookup_symbol: It assumes that if the index says symbol X lives
702 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
703 will find X. Alas TUs live in their own symtab, so after expanding CU Y
704 we need to look in TU Z to find X. Fortunately, this is akin to
705 DW_TAG_imported_unit, so we just use the same mechanism: For
706 .gdb_index version <=7 this also records the TUs that the CU referred
707 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
708 indices so we only pay a price for gold generated indices.
709 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 710 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
711};
712
348e048f
DE
713/* Entry in the signatured_types hash table. */
714
715struct signatured_type
716{
42e7ad6c 717 /* The "per_cu" object of this type.
ac9ec31b 718 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
719 N.B.: This is the first member so that it's easy to convert pointers
720 between them. */
721 struct dwarf2_per_cu_data per_cu;
722
3019eac3 723 /* The type's signature. */
348e048f
DE
724 ULONGEST signature;
725
3019eac3 726 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
727 If this TU is a DWO stub and the definition lives in a DWO file
728 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
729 cu_offset type_offset_in_tu;
730
731 /* Offset in the section of the type's DIE.
732 If the definition lives in a DWO file, this is the offset in the
733 .debug_types.dwo section.
734 The value is zero until the actual value is known.
735 Zero is otherwise not a valid section offset. */
736 sect_offset type_offset_in_section;
0186c6a7
DE
737
738 /* Type units are grouped by their DW_AT_stmt_list entry so that they
739 can share them. This points to the containing symtab. */
740 struct type_unit_group *type_unit_group;
ac9ec31b
DE
741
742 /* The type.
743 The first time we encounter this type we fully read it in and install it
744 in the symbol tables. Subsequent times we only need the type. */
745 struct type *type;
a2ce51a0
DE
746
747 /* Containing DWO unit.
748 This field is valid iff per_cu.reading_dwo_directly. */
749 struct dwo_unit *dwo_unit;
348e048f
DE
750};
751
0186c6a7
DE
752typedef struct signatured_type *sig_type_ptr;
753DEF_VEC_P (sig_type_ptr);
754
094b34ac
DE
755/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
756 This includes type_unit_group and quick_file_names. */
757
758struct stmt_list_hash
759{
760 /* The DWO unit this table is from or NULL if there is none. */
761 struct dwo_unit *dwo_unit;
762
763 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 764 sect_offset line_sect_off;
094b34ac
DE
765};
766
f4dc4d17
DE
767/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
768 an object of this type. */
769
770struct type_unit_group
771{
0186c6a7 772 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
773 To simplify things we create an artificial CU that "includes" all the
774 type units using this stmt_list so that the rest of the code still has
775 a "per_cu" handle on the symtab.
776 This PER_CU is recognized by having no section. */
8a0459fd 777#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
778 struct dwarf2_per_cu_data per_cu;
779
0186c6a7
DE
780 /* The TUs that share this DW_AT_stmt_list entry.
781 This is added to while parsing type units to build partial symtabs,
782 and is deleted afterwards and not used again. */
783 VEC (sig_type_ptr) *tus;
f4dc4d17 784
43f3e411 785 /* The compunit symtab.
094b34ac 786 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
787 so we create an essentially anonymous symtab as the compunit symtab. */
788 struct compunit_symtab *compunit_symtab;
f4dc4d17 789
094b34ac
DE
790 /* The data used to construct the hash key. */
791 struct stmt_list_hash hash;
f4dc4d17
DE
792
793 /* The number of symtabs from the line header.
794 The value here must match line_header.num_file_names. */
795 unsigned int num_symtabs;
796
797 /* The symbol tables for this TU (obtained from the files listed in
798 DW_AT_stmt_list).
799 WARNING: The order of entries here must match the order of entries
800 in the line header. After the first TU using this type_unit_group, the
801 line header for the subsequent TUs is recreated from this. This is done
802 because we need to use the same symtabs for each TU using the same
803 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
804 there's no guarantee the line header doesn't have duplicate entries. */
805 struct symtab **symtabs;
806};
807
73869dc2 808/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
809
810struct dwo_sections
811{
812 struct dwarf2_section_info abbrev;
3019eac3
DE
813 struct dwarf2_section_info line;
814 struct dwarf2_section_info loc;
43988095 815 struct dwarf2_section_info loclists;
09262596
DE
816 struct dwarf2_section_info macinfo;
817 struct dwarf2_section_info macro;
3019eac3
DE
818 struct dwarf2_section_info str;
819 struct dwarf2_section_info str_offsets;
80626a55
DE
820 /* In the case of a virtual DWO file, these two are unused. */
821 struct dwarf2_section_info info;
3019eac3
DE
822 VEC (dwarf2_section_info_def) *types;
823};
824
c88ee1f0 825/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
826
827struct dwo_unit
828{
829 /* Backlink to the containing struct dwo_file. */
830 struct dwo_file *dwo_file;
831
832 /* The "id" that distinguishes this CU/TU.
833 .debug_info calls this "dwo_id", .debug_types calls this "signature".
834 Since signatures came first, we stick with it for consistency. */
835 ULONGEST signature;
836
837 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 838 struct dwarf2_section_info *section;
3019eac3 839
9c541725
PA
840 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
841 sect_offset sect_off;
3019eac3
DE
842 unsigned int length;
843
844 /* For types, offset in the type's DIE of the type defined by this TU. */
845 cu_offset type_offset_in_tu;
846};
847
73869dc2
DE
848/* include/dwarf2.h defines the DWP section codes.
849 It defines a max value but it doesn't define a min value, which we
850 use for error checking, so provide one. */
851
852enum dwp_v2_section_ids
853{
854 DW_SECT_MIN = 1
855};
856
80626a55 857/* Data for one DWO file.
57d63ce2
DE
858
859 This includes virtual DWO files (a virtual DWO file is a DWO file as it
860 appears in a DWP file). DWP files don't really have DWO files per se -
861 comdat folding of types "loses" the DWO file they came from, and from
862 a high level view DWP files appear to contain a mass of random types.
863 However, to maintain consistency with the non-DWP case we pretend DWP
864 files contain virtual DWO files, and we assign each TU with one virtual
865 DWO file (generally based on the line and abbrev section offsets -
866 a heuristic that seems to work in practice). */
3019eac3
DE
867
868struct dwo_file
869{
0ac5b59e 870 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
871 For virtual DWO files the name is constructed from the section offsets
872 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
873 from related CU+TUs. */
0ac5b59e
DE
874 const char *dwo_name;
875
876 /* The DW_AT_comp_dir attribute. */
877 const char *comp_dir;
3019eac3 878
80626a55
DE
879 /* The bfd, when the file is open. Otherwise this is NULL.
880 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
881 bfd *dbfd;
3019eac3 882
73869dc2
DE
883 /* The sections that make up this DWO file.
884 Remember that for virtual DWO files in DWP V2, these are virtual
885 sections (for lack of a better name). */
3019eac3
DE
886 struct dwo_sections sections;
887
33c5cd75
DB
888 /* The CUs in the file.
889 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
890 an extension to handle LLVM's Link Time Optimization output (where
891 multiple source files may be compiled into a single object/dwo pair). */
892 htab_t cus;
3019eac3
DE
893
894 /* Table of TUs in the file.
895 Each element is a struct dwo_unit. */
896 htab_t tus;
897};
898
80626a55
DE
899/* These sections are what may appear in a DWP file. */
900
901struct dwp_sections
902{
73869dc2 903 /* These are used by both DWP version 1 and 2. */
80626a55
DE
904 struct dwarf2_section_info str;
905 struct dwarf2_section_info cu_index;
906 struct dwarf2_section_info tu_index;
73869dc2
DE
907
908 /* These are only used by DWP version 2 files.
909 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
910 sections are referenced by section number, and are not recorded here.
911 In DWP version 2 there is at most one copy of all these sections, each
912 section being (effectively) comprised of the concatenation of all of the
913 individual sections that exist in the version 1 format.
914 To keep the code simple we treat each of these concatenated pieces as a
915 section itself (a virtual section?). */
916 struct dwarf2_section_info abbrev;
917 struct dwarf2_section_info info;
918 struct dwarf2_section_info line;
919 struct dwarf2_section_info loc;
920 struct dwarf2_section_info macinfo;
921 struct dwarf2_section_info macro;
922 struct dwarf2_section_info str_offsets;
923 struct dwarf2_section_info types;
80626a55
DE
924};
925
73869dc2
DE
926/* These sections are what may appear in a virtual DWO file in DWP version 1.
927 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 928
73869dc2 929struct virtual_v1_dwo_sections
80626a55
DE
930{
931 struct dwarf2_section_info abbrev;
932 struct dwarf2_section_info line;
933 struct dwarf2_section_info loc;
934 struct dwarf2_section_info macinfo;
935 struct dwarf2_section_info macro;
936 struct dwarf2_section_info str_offsets;
937 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 938 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
939 struct dwarf2_section_info info_or_types;
940};
941
73869dc2
DE
942/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
943 In version 2, the sections of the DWO files are concatenated together
944 and stored in one section of that name. Thus each ELF section contains
945 several "virtual" sections. */
946
947struct virtual_v2_dwo_sections
948{
949 bfd_size_type abbrev_offset;
950 bfd_size_type abbrev_size;
951
952 bfd_size_type line_offset;
953 bfd_size_type line_size;
954
955 bfd_size_type loc_offset;
956 bfd_size_type loc_size;
957
958 bfd_size_type macinfo_offset;
959 bfd_size_type macinfo_size;
960
961 bfd_size_type macro_offset;
962 bfd_size_type macro_size;
963
964 bfd_size_type str_offsets_offset;
965 bfd_size_type str_offsets_size;
966
967 /* Each DWP hash table entry records one CU or one TU.
968 That is recorded here, and copied to dwo_unit.section. */
969 bfd_size_type info_or_types_offset;
970 bfd_size_type info_or_types_size;
971};
972
80626a55
DE
973/* Contents of DWP hash tables. */
974
975struct dwp_hash_table
976{
73869dc2 977 uint32_t version, nr_columns;
80626a55 978 uint32_t nr_units, nr_slots;
73869dc2
DE
979 const gdb_byte *hash_table, *unit_table;
980 union
981 {
982 struct
983 {
984 const gdb_byte *indices;
985 } v1;
986 struct
987 {
988 /* This is indexed by column number and gives the id of the section
989 in that column. */
990#define MAX_NR_V2_DWO_SECTIONS \
991 (1 /* .debug_info or .debug_types */ \
992 + 1 /* .debug_abbrev */ \
993 + 1 /* .debug_line */ \
994 + 1 /* .debug_loc */ \
995 + 1 /* .debug_str_offsets */ \
996 + 1 /* .debug_macro or .debug_macinfo */)
997 int section_ids[MAX_NR_V2_DWO_SECTIONS];
998 const gdb_byte *offsets;
999 const gdb_byte *sizes;
1000 } v2;
1001 } section_pool;
80626a55
DE
1002};
1003
1004/* Data for one DWP file. */
1005
1006struct dwp_file
1007{
1008 /* Name of the file. */
1009 const char *name;
1010
73869dc2
DE
1011 /* File format version. */
1012 int version;
1013
93417882 1014 /* The bfd. */
80626a55
DE
1015 bfd *dbfd;
1016
1017 /* Section info for this file. */
1018 struct dwp_sections sections;
1019
57d63ce2 1020 /* Table of CUs in the file. */
80626a55
DE
1021 const struct dwp_hash_table *cus;
1022
1023 /* Table of TUs in the file. */
1024 const struct dwp_hash_table *tus;
1025
19ac8c2e
DE
1026 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1027 htab_t loaded_cus;
1028 htab_t loaded_tus;
80626a55 1029
73869dc2
DE
1030 /* Table to map ELF section numbers to their sections.
1031 This is only needed for the DWP V1 file format. */
80626a55
DE
1032 unsigned int num_sections;
1033 asection **elf_sections;
1034};
1035
36586728
TT
1036/* This represents a '.dwz' file. */
1037
1038struct dwz_file
1039{
1040 /* A dwz file can only contain a few sections. */
1041 struct dwarf2_section_info abbrev;
1042 struct dwarf2_section_info info;
1043 struct dwarf2_section_info str;
1044 struct dwarf2_section_info line;
1045 struct dwarf2_section_info macro;
2ec9a5e0 1046 struct dwarf2_section_info gdb_index;
36586728
TT
1047
1048 /* The dwz's BFD. */
1049 bfd *dwz_bfd;
1050};
1051
0963b4bd
MS
1052/* Struct used to pass misc. parameters to read_die_and_children, et
1053 al. which are used for both .debug_info and .debug_types dies.
1054 All parameters here are unchanging for the life of the call. This
dee91e82 1055 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1056
1057struct die_reader_specs
1058{
a32a8923 1059 /* The bfd of die_section. */
93311388
DE
1060 bfd* abfd;
1061
1062 /* The CU of the DIE we are parsing. */
1063 struct dwarf2_cu *cu;
1064
80626a55 1065 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1066 struct dwo_file *dwo_file;
1067
dee91e82 1068 /* The section the die comes from.
3019eac3 1069 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1070 struct dwarf2_section_info *die_section;
1071
1072 /* die_section->buffer. */
d521ce57 1073 const gdb_byte *buffer;
f664829e
DE
1074
1075 /* The end of the buffer. */
1076 const gdb_byte *buffer_end;
a2ce51a0
DE
1077
1078 /* The value of the DW_AT_comp_dir attribute. */
1079 const char *comp_dir;
93311388
DE
1080};
1081
fd820528 1082/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1083typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1084 const gdb_byte *info_ptr,
dee91e82
DE
1085 struct die_info *comp_unit_die,
1086 int has_children,
1087 void *data);
1088
ecfb656c
PA
1089/* A 1-based directory index. This is a strong typedef to prevent
1090 accidentally using a directory index as a 0-based index into an
1091 array/vector. */
1092enum class dir_index : unsigned int {};
1093
1094/* Likewise, a 1-based file name index. */
1095enum class file_name_index : unsigned int {};
1096
52059ffd
TT
1097struct file_entry
1098{
fff8551c
PA
1099 file_entry () = default;
1100
ecfb656c 1101 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1102 unsigned int mod_time_, unsigned int length_)
1103 : name (name_),
ecfb656c 1104 d_index (d_index_),
fff8551c
PA
1105 mod_time (mod_time_),
1106 length (length_)
1107 {}
1108
ecfb656c
PA
1109 /* Return the include directory at D_INDEX stored in LH. Returns
1110 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1111 const char *include_dir (const line_header *lh) const;
1112
fff8551c
PA
1113 /* The file name. Note this is an observing pointer. The memory is
1114 owned by debug_line_buffer. */
1115 const char *name {};
1116
8c43009f 1117 /* The directory index (1-based). */
ecfb656c 1118 dir_index d_index {};
fff8551c
PA
1119
1120 unsigned int mod_time {};
1121
1122 unsigned int length {};
1123
1124 /* True if referenced by the Line Number Program. */
1125 bool included_p {};
1126
83769d0b 1127 /* The associated symbol table, if any. */
fff8551c 1128 struct symtab *symtab {};
52059ffd
TT
1129};
1130
debd256d
JB
1131/* The line number information for a compilation unit (found in the
1132 .debug_line section) begins with a "statement program header",
1133 which contains the following information. */
1134struct line_header
1135{
fff8551c
PA
1136 line_header ()
1137 : offset_in_dwz {}
1138 {}
1139
1140 /* Add an entry to the include directory table. */
1141 void add_include_dir (const char *include_dir);
1142
1143 /* Add an entry to the file name table. */
ecfb656c 1144 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1145 unsigned int mod_time, unsigned int length);
1146
ecfb656c 1147 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1148 is out of bounds. */
ecfb656c 1149 const char *include_dir_at (dir_index index) const
8c43009f 1150 {
ecfb656c
PA
1151 /* Convert directory index number (1-based) to vector index
1152 (0-based). */
1153 size_t vec_index = to_underlying (index) - 1;
1154
1155 if (vec_index >= include_dirs.size ())
8c43009f 1156 return NULL;
ecfb656c 1157 return include_dirs[vec_index];
8c43009f
PA
1158 }
1159
ecfb656c 1160 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1161 is out of bounds. */
ecfb656c 1162 file_entry *file_name_at (file_name_index index)
8c43009f 1163 {
ecfb656c
PA
1164 /* Convert file name index number (1-based) to vector index
1165 (0-based). */
1166 size_t vec_index = to_underlying (index) - 1;
1167
1168 if (vec_index >= file_names.size ())
fff8551c 1169 return NULL;
ecfb656c 1170 return &file_names[vec_index];
fff8551c
PA
1171 }
1172
1173 /* Const version of the above. */
1174 const file_entry *file_name_at (unsigned int index) const
1175 {
1176 if (index >= file_names.size ())
8c43009f
PA
1177 return NULL;
1178 return &file_names[index];
1179 }
1180
527f3840 1181 /* Offset of line number information in .debug_line section. */
9c541725 1182 sect_offset sect_off {};
527f3840
JK
1183
1184 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1185 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1186
1187 unsigned int total_length {};
1188 unsigned short version {};
1189 unsigned int header_length {};
1190 unsigned char minimum_instruction_length {};
1191 unsigned char maximum_ops_per_instruction {};
1192 unsigned char default_is_stmt {};
1193 int line_base {};
1194 unsigned char line_range {};
1195 unsigned char opcode_base {};
debd256d
JB
1196
1197 /* standard_opcode_lengths[i] is the number of operands for the
1198 standard opcode whose value is i. This means that
1199 standard_opcode_lengths[0] is unused, and the last meaningful
1200 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1201 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1202
fff8551c
PA
1203 /* The include_directories table. Note these are observing
1204 pointers. The memory is owned by debug_line_buffer. */
1205 std::vector<const char *> include_dirs;
debd256d 1206
fff8551c
PA
1207 /* The file_names table. */
1208 std::vector<file_entry> file_names;
debd256d
JB
1209
1210 /* The start and end of the statement program following this
6502dd73 1211 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1212 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1213};
c906108c 1214
fff8551c
PA
1215typedef std::unique_ptr<line_header> line_header_up;
1216
8c43009f
PA
1217const char *
1218file_entry::include_dir (const line_header *lh) const
1219{
ecfb656c 1220 return lh->include_dir_at (d_index);
8c43009f
PA
1221}
1222
c906108c 1223/* When we construct a partial symbol table entry we only
0963b4bd 1224 need this much information. */
c906108c
SS
1225struct partial_die_info
1226 {
72bf9492 1227 /* Offset of this DIE. */
9c541725 1228 sect_offset sect_off;
72bf9492
DJ
1229
1230 /* DWARF-2 tag for this DIE. */
1231 ENUM_BITFIELD(dwarf_tag) tag : 16;
1232
72bf9492
DJ
1233 /* Assorted flags describing the data found in this DIE. */
1234 unsigned int has_children : 1;
1235 unsigned int is_external : 1;
1236 unsigned int is_declaration : 1;
1237 unsigned int has_type : 1;
1238 unsigned int has_specification : 1;
1239 unsigned int has_pc_info : 1;
481860b3 1240 unsigned int may_be_inlined : 1;
72bf9492 1241
0c1b455e
TT
1242 /* This DIE has been marked DW_AT_main_subprogram. */
1243 unsigned int main_subprogram : 1;
1244
72bf9492
DJ
1245 /* Flag set if the SCOPE field of this structure has been
1246 computed. */
1247 unsigned int scope_set : 1;
1248
fa4028e9
JB
1249 /* Flag set if the DIE has a byte_size attribute. */
1250 unsigned int has_byte_size : 1;
1251
ff908ebf
AW
1252 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1253 unsigned int has_const_value : 1;
1254
98bfdba5
PA
1255 /* Flag set if any of the DIE's children are template arguments. */
1256 unsigned int has_template_arguments : 1;
1257
abc72ce4
DE
1258 /* Flag set if fixup_partial_die has been called on this die. */
1259 unsigned int fixup_called : 1;
1260
36586728
TT
1261 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1262 unsigned int is_dwz : 1;
1263
1264 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1265 unsigned int spec_is_dwz : 1;
1266
72bf9492 1267 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1268 sometimes a default name for unnamed DIEs. */
15d034d0 1269 const char *name;
72bf9492 1270
abc72ce4
DE
1271 /* The linkage name, if present. */
1272 const char *linkage_name;
1273
72bf9492
DJ
1274 /* The scope to prepend to our children. This is generally
1275 allocated on the comp_unit_obstack, so will disappear
1276 when this compilation unit leaves the cache. */
15d034d0 1277 const char *scope;
72bf9492 1278
95554aad
TT
1279 /* Some data associated with the partial DIE. The tag determines
1280 which field is live. */
1281 union
1282 {
1283 /* The location description associated with this DIE, if any. */
1284 struct dwarf_block *locdesc;
1285 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1286 sect_offset sect_off;
95554aad 1287 } d;
72bf9492
DJ
1288
1289 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1290 CORE_ADDR lowpc;
1291 CORE_ADDR highpc;
72bf9492 1292
93311388 1293 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1294 DW_AT_sibling, if any. */
abc72ce4
DE
1295 /* NOTE: This member isn't strictly necessary, read_partial_die could
1296 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1297 const gdb_byte *sibling;
72bf9492
DJ
1298
1299 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1300 DW_AT_specification (or DW_AT_abstract_origin or
1301 DW_AT_extension). */
b64f50a1 1302 sect_offset spec_offset;
72bf9492
DJ
1303
1304 /* Pointers to this DIE's parent, first child, and next sibling,
1305 if any. */
1306 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1307 };
1308
0963b4bd 1309/* This data structure holds the information of an abbrev. */
c906108c
SS
1310struct abbrev_info
1311 {
1312 unsigned int number; /* number identifying abbrev */
1313 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1314 unsigned short has_children; /* boolean */
1315 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1316 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1317 struct abbrev_info *next; /* next in chain */
1318 };
1319
1320struct attr_abbrev
1321 {
9d25dd43
DE
1322 ENUM_BITFIELD(dwarf_attribute) name : 16;
1323 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1324
1325 /* It is valid only if FORM is DW_FORM_implicit_const. */
1326 LONGEST implicit_const;
c906108c
SS
1327 };
1328
433df2d4
DE
1329/* Size of abbrev_table.abbrev_hash_table. */
1330#define ABBREV_HASH_SIZE 121
1331
1332/* Top level data structure to contain an abbreviation table. */
1333
1334struct abbrev_table
1335{
f4dc4d17
DE
1336 /* Where the abbrev table came from.
1337 This is used as a sanity check when the table is used. */
9c541725 1338 sect_offset sect_off;
433df2d4
DE
1339
1340 /* Storage for the abbrev table. */
1341 struct obstack abbrev_obstack;
1342
1343 /* Hash table of abbrevs.
1344 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1345 It could be statically allocated, but the previous code didn't so we
1346 don't either. */
1347 struct abbrev_info **abbrevs;
1348};
1349
0963b4bd 1350/* Attributes have a name and a value. */
b60c80d6
DJ
1351struct attribute
1352 {
9d25dd43 1353 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1354 ENUM_BITFIELD(dwarf_form) form : 15;
1355
1356 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1357 field should be in u.str (existing only for DW_STRING) but it is kept
1358 here for better struct attribute alignment. */
1359 unsigned int string_is_canonical : 1;
1360
b60c80d6
DJ
1361 union
1362 {
15d034d0 1363 const char *str;
b60c80d6 1364 struct dwarf_block *blk;
43bbcdc2
PH
1365 ULONGEST unsnd;
1366 LONGEST snd;
b60c80d6 1367 CORE_ADDR addr;
ac9ec31b 1368 ULONGEST signature;
b60c80d6
DJ
1369 }
1370 u;
1371 };
1372
0963b4bd 1373/* This data structure holds a complete die structure. */
c906108c
SS
1374struct die_info
1375 {
76815b17
DE
1376 /* DWARF-2 tag for this DIE. */
1377 ENUM_BITFIELD(dwarf_tag) tag : 16;
1378
1379 /* Number of attributes */
98bfdba5
PA
1380 unsigned char num_attrs;
1381
1382 /* True if we're presently building the full type name for the
1383 type derived from this DIE. */
1384 unsigned char building_fullname : 1;
76815b17 1385
adde2bff
DE
1386 /* True if this die is in process. PR 16581. */
1387 unsigned char in_process : 1;
1388
76815b17
DE
1389 /* Abbrev number */
1390 unsigned int abbrev;
1391
93311388 1392 /* Offset in .debug_info or .debug_types section. */
9c541725 1393 sect_offset sect_off;
78ba4af6
JB
1394
1395 /* The dies in a compilation unit form an n-ary tree. PARENT
1396 points to this die's parent; CHILD points to the first child of
1397 this node; and all the children of a given node are chained
4950bc1c 1398 together via their SIBLING fields. */
639d11d3
DC
1399 struct die_info *child; /* Its first child, if any. */
1400 struct die_info *sibling; /* Its next sibling, if any. */
1401 struct die_info *parent; /* Its parent, if any. */
c906108c 1402
b60c80d6
DJ
1403 /* An array of attributes, with NUM_ATTRS elements. There may be
1404 zero, but it's not common and zero-sized arrays are not
1405 sufficiently portable C. */
1406 struct attribute attrs[1];
c906108c
SS
1407 };
1408
0963b4bd 1409/* Get at parts of an attribute structure. */
c906108c
SS
1410
1411#define DW_STRING(attr) ((attr)->u.str)
8285870a 1412#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1413#define DW_UNSND(attr) ((attr)->u.unsnd)
1414#define DW_BLOCK(attr) ((attr)->u.blk)
1415#define DW_SND(attr) ((attr)->u.snd)
1416#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1417#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1418
0963b4bd 1419/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1420struct dwarf_block
1421 {
56eb65bd 1422 size_t size;
1d6edc3c
JK
1423
1424 /* Valid only if SIZE is not zero. */
d521ce57 1425 const gdb_byte *data;
c906108c
SS
1426 };
1427
c906108c
SS
1428#ifndef ATTR_ALLOC_CHUNK
1429#define ATTR_ALLOC_CHUNK 4
1430#endif
1431
c906108c
SS
1432/* Allocate fields for structs, unions and enums in this size. */
1433#ifndef DW_FIELD_ALLOC_CHUNK
1434#define DW_FIELD_ALLOC_CHUNK 4
1435#endif
1436
c906108c
SS
1437/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1438 but this would require a corresponding change in unpack_field_as_long
1439 and friends. */
1440static int bits_per_byte = 8;
1441
52059ffd
TT
1442struct nextfield
1443{
1444 struct nextfield *next;
1445 int accessibility;
1446 int virtuality;
1447 struct field field;
1448};
1449
1450struct nextfnfield
1451{
1452 struct nextfnfield *next;
1453 struct fn_field fnfield;
1454};
1455
1456struct fnfieldlist
1457{
1458 const char *name;
1459 int length;
1460 struct nextfnfield *head;
1461};
1462
1463struct typedef_field_list
1464{
1465 struct typedef_field field;
1466 struct typedef_field_list *next;
1467};
1468
c906108c
SS
1469/* The routines that read and process dies for a C struct or C++ class
1470 pass lists of data member fields and lists of member function fields
1471 in an instance of a field_info structure, as defined below. */
1472struct field_info
c5aa993b 1473 {
0963b4bd 1474 /* List of data member and baseclasses fields. */
52059ffd 1475 struct nextfield *fields, *baseclasses;
c906108c 1476
7d0ccb61 1477 /* Number of fields (including baseclasses). */
c5aa993b 1478 int nfields;
c906108c 1479
c5aa993b
JM
1480 /* Number of baseclasses. */
1481 int nbaseclasses;
c906108c 1482
c5aa993b
JM
1483 /* Set if the accesibility of one of the fields is not public. */
1484 int non_public_fields;
c906108c 1485
c5aa993b
JM
1486 /* Member function fieldlist array, contains name of possibly overloaded
1487 member function, number of overloaded member functions and a pointer
1488 to the head of the member function field chain. */
52059ffd 1489 struct fnfieldlist *fnfieldlists;
c906108c 1490
c5aa993b
JM
1491 /* Number of entries in the fnfieldlists array. */
1492 int nfnfields;
98751a41
JK
1493
1494 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1495 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1496 struct typedef_field_list *typedef_field_list;
98751a41 1497 unsigned typedef_field_list_count;
c5aa993b 1498 };
c906108c 1499
10b3939b
DJ
1500/* One item on the queue of compilation units to read in full symbols
1501 for. */
1502struct dwarf2_queue_item
1503{
1504 struct dwarf2_per_cu_data *per_cu;
95554aad 1505 enum language pretend_language;
10b3939b
DJ
1506 struct dwarf2_queue_item *next;
1507};
1508
1509/* The current queue. */
1510static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1511
ae038cb0
DJ
1512/* Loaded secondary compilation units are kept in memory until they
1513 have not been referenced for the processing of this many
1514 compilation units. Set this to zero to disable caching. Cache
1515 sizes of up to at least twenty will improve startup time for
1516 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1517static int dwarf_max_cache_age = 5;
920d2a44 1518static void
b4f54984
DE
1519show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1520 struct cmd_list_element *c, const char *value)
920d2a44 1521{
3e43a32a 1522 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1523 "DWARF compilation units is %s.\n"),
920d2a44
AC
1524 value);
1525}
4390d890 1526\f
c906108c
SS
1527/* local function prototypes */
1528
a32a8923
DE
1529static const char *get_section_name (const struct dwarf2_section_info *);
1530
1531static const char *get_section_file_name (const struct dwarf2_section_info *);
1532
918dd910
JK
1533static void dwarf2_find_base_address (struct die_info *die,
1534 struct dwarf2_cu *cu);
1535
0018ea6f
DE
1536static struct partial_symtab *create_partial_symtab
1537 (struct dwarf2_per_cu_data *per_cu, const char *name);
1538
f1902523
JK
1539static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1540 const gdb_byte *info_ptr,
1541 struct die_info *type_unit_die,
1542 int has_children, void *data);
1543
c67a9c90 1544static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1545
72bf9492
DJ
1546static void scan_partial_symbols (struct partial_die_info *,
1547 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1548 int, struct dwarf2_cu *);
c906108c 1549
72bf9492
DJ
1550static void add_partial_symbol (struct partial_die_info *,
1551 struct dwarf2_cu *);
63d06c5c 1552
72bf9492
DJ
1553static void add_partial_namespace (struct partial_die_info *pdi,
1554 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1555 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1556
5d7cb8df 1557static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1558 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1559 struct dwarf2_cu *cu);
1560
72bf9492
DJ
1561static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1562 struct dwarf2_cu *cu);
91c24f0a 1563
bc30ff58
JB
1564static void add_partial_subprogram (struct partial_die_info *pdi,
1565 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1566 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1567
257e7a09
YQ
1568static void dwarf2_read_symtab (struct partial_symtab *,
1569 struct objfile *);
c906108c 1570
a14ed312 1571static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1572
433df2d4
DE
1573static struct abbrev_info *abbrev_table_lookup_abbrev
1574 (const struct abbrev_table *, unsigned int);
1575
1576static struct abbrev_table *abbrev_table_read_table
1577 (struct dwarf2_section_info *, sect_offset);
1578
1579static void abbrev_table_free (struct abbrev_table *);
1580
f4dc4d17
DE
1581static void abbrev_table_free_cleanup (void *);
1582
dee91e82
DE
1583static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1584 struct dwarf2_section_info *);
c906108c 1585
f3dd6933 1586static void dwarf2_free_abbrev_table (void *);
c906108c 1587
d521ce57 1588static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1589
dee91e82 1590static struct partial_die_info *load_partial_dies
d521ce57 1591 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1592
d521ce57
TT
1593static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1594 struct partial_die_info *,
1595 struct abbrev_info *,
1596 unsigned int,
1597 const gdb_byte *);
c906108c 1598
36586728 1599static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1600 struct dwarf2_cu *);
72bf9492
DJ
1601
1602static void fixup_partial_die (struct partial_die_info *,
1603 struct dwarf2_cu *);
1604
d521ce57
TT
1605static const gdb_byte *read_attribute (const struct die_reader_specs *,
1606 struct attribute *, struct attr_abbrev *,
1607 const gdb_byte *);
a8329558 1608
a1855c1d 1609static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1610
a1855c1d 1611static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1612
a1855c1d 1613static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1614
a1855c1d 1615static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1616
a1855c1d 1617static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1618
d521ce57 1619static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1620 unsigned int *);
c906108c 1621
d521ce57 1622static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1623
1624static LONGEST read_checked_initial_length_and_offset
d521ce57 1625 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1626 unsigned int *, unsigned int *);
613e1657 1627
d521ce57
TT
1628static LONGEST read_offset (bfd *, const gdb_byte *,
1629 const struct comp_unit_head *,
c764a876
DE
1630 unsigned int *);
1631
d521ce57 1632static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1633
f4dc4d17
DE
1634static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1635 sect_offset);
1636
d521ce57 1637static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1638
d521ce57 1639static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1640
d521ce57
TT
1641static const char *read_indirect_string (bfd *, const gdb_byte *,
1642 const struct comp_unit_head *,
1643 unsigned int *);
4bdf3d34 1644
43988095
JK
1645static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1646 const struct comp_unit_head *,
1647 unsigned int *);
36586728 1648
43988095 1649static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1650
d521ce57 1651static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1652
d521ce57
TT
1653static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1654 const gdb_byte *,
3019eac3
DE
1655 unsigned int *);
1656
d521ce57 1657static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1658 ULONGEST str_index);
3019eac3 1659
e142c38c 1660static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1661
e142c38c
DJ
1662static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1663 struct dwarf2_cu *);
c906108c 1664
348e048f 1665static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1666 unsigned int);
348e048f 1667
7d45c7c3
KB
1668static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1669 struct dwarf2_cu *cu);
1670
05cf31d1
JB
1671static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1672 struct dwarf2_cu *cu);
1673
e142c38c 1674static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1675
e142c38c 1676static struct die_info *die_specification (struct die_info *die,
f2f0e013 1677 struct dwarf2_cu **);
63d06c5c 1678
9c541725 1679static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1680 struct dwarf2_cu *cu);
debd256d 1681
f3f5162e 1682static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1683 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1684 CORE_ADDR, int decode_mapping);
c906108c 1685
4d663531 1686static void dwarf2_start_subfile (const char *, const char *);
c906108c 1687
43f3e411
DE
1688static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1689 const char *, const char *,
1690 CORE_ADDR);
f4dc4d17 1691
a14ed312 1692static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1693 struct dwarf2_cu *);
c906108c 1694
34eaf542
TT
1695static struct symbol *new_symbol_full (struct die_info *, struct type *,
1696 struct dwarf2_cu *, struct symbol *);
1697
ff39bb5e 1698static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1699 struct dwarf2_cu *);
c906108c 1700
ff39bb5e 1701static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1702 struct type *type,
1703 const char *name,
1704 struct obstack *obstack,
12df843f 1705 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1706 const gdb_byte **bytes,
98bfdba5 1707 struct dwarf2_locexpr_baton **baton);
2df3850c 1708
e7c27a73 1709static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1710
b4ba55a1
JB
1711static int need_gnat_info (struct dwarf2_cu *);
1712
3e43a32a
MS
1713static struct type *die_descriptive_type (struct die_info *,
1714 struct dwarf2_cu *);
b4ba55a1
JB
1715
1716static void set_descriptive_type (struct type *, struct die_info *,
1717 struct dwarf2_cu *);
1718
e7c27a73
DJ
1719static struct type *die_containing_type (struct die_info *,
1720 struct dwarf2_cu *);
c906108c 1721
ff39bb5e 1722static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1723 struct dwarf2_cu *);
c906108c 1724
f792889a 1725static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1726
673bfd45
DE
1727static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1728
0d5cff50 1729static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1730
6e70227d 1731static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1732 const char *suffix, int physname,
1733 struct dwarf2_cu *cu);
63d06c5c 1734
e7c27a73 1735static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1736
348e048f
DE
1737static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1738
e7c27a73 1739static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1740
e7c27a73 1741static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1742
96408a79
SA
1743static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1744
ff013f42
JK
1745static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1746 struct dwarf2_cu *, struct partial_symtab *);
1747
3a2b436a 1748/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1749 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1750enum pc_bounds_kind
1751{
e385593e 1752 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1753 PC_BOUNDS_NOT_PRESENT,
1754
e385593e
JK
1755 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1756 were present but they do not form a valid range of PC addresses. */
1757 PC_BOUNDS_INVALID,
1758
3a2b436a
JK
1759 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1760 PC_BOUNDS_RANGES,
1761
1762 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1763 PC_BOUNDS_HIGH_LOW,
1764};
1765
1766static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1767 CORE_ADDR *, CORE_ADDR *,
1768 struct dwarf2_cu *,
1769 struct partial_symtab *);
c906108c 1770
fae299cd
DC
1771static void get_scope_pc_bounds (struct die_info *,
1772 CORE_ADDR *, CORE_ADDR *,
1773 struct dwarf2_cu *);
1774
801e3a5b
JB
1775static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1776 CORE_ADDR, struct dwarf2_cu *);
1777
a14ed312 1778static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1779 struct dwarf2_cu *);
c906108c 1780
a14ed312 1781static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1782 struct type *, struct dwarf2_cu *);
c906108c 1783
a14ed312 1784static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1785 struct die_info *, struct type *,
e7c27a73 1786 struct dwarf2_cu *);
c906108c 1787
a14ed312 1788static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1789 struct type *,
1790 struct dwarf2_cu *);
c906108c 1791
134d01f1 1792static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1793
e7c27a73 1794static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1795
e7c27a73 1796static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1797
5d7cb8df
JK
1798static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1799
22cee43f
PMR
1800static struct using_direct **using_directives (enum language);
1801
27aa8d6a
SW
1802static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1803
74921315
KS
1804static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1805
f55ee35c
JK
1806static struct type *read_module_type (struct die_info *die,
1807 struct dwarf2_cu *cu);
1808
38d518c9 1809static const char *namespace_name (struct die_info *die,
e142c38c 1810 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1811
134d01f1 1812static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1813
e7c27a73 1814static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1815
6e70227d 1816static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1817 struct dwarf2_cu *);
1818
bf6af496 1819static struct die_info *read_die_and_siblings_1
d521ce57 1820 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1821 struct die_info *);
639d11d3 1822
dee91e82 1823static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1824 const gdb_byte *info_ptr,
1825 const gdb_byte **new_info_ptr,
639d11d3
DC
1826 struct die_info *parent);
1827
d521ce57
TT
1828static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1829 struct die_info **, const gdb_byte *,
1830 int *, int);
3019eac3 1831
d521ce57
TT
1832static const gdb_byte *read_full_die (const struct die_reader_specs *,
1833 struct die_info **, const gdb_byte *,
1834 int *);
93311388 1835
e7c27a73 1836static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1837
15d034d0
TT
1838static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1839 struct obstack *);
71c25dea 1840
15d034d0 1841static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1842
15d034d0 1843static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1844 struct die_info *die,
1845 struct dwarf2_cu *cu);
1846
ca69b9e6
DE
1847static const char *dwarf2_physname (const char *name, struct die_info *die,
1848 struct dwarf2_cu *cu);
1849
e142c38c 1850static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1851 struct dwarf2_cu **);
9219021c 1852
f39c6ffd 1853static const char *dwarf_tag_name (unsigned int);
c906108c 1854
f39c6ffd 1855static const char *dwarf_attr_name (unsigned int);
c906108c 1856
f39c6ffd 1857static const char *dwarf_form_name (unsigned int);
c906108c 1858
a121b7c1 1859static const char *dwarf_bool_name (unsigned int);
c906108c 1860
f39c6ffd 1861static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1862
f9aca02d 1863static struct die_info *sibling_die (struct die_info *);
c906108c 1864
d97bc12b
DE
1865static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1866
1867static void dump_die_for_error (struct die_info *);
1868
1869static void dump_die_1 (struct ui_file *, int level, int max_level,
1870 struct die_info *);
c906108c 1871
d97bc12b 1872/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1873
51545339 1874static void store_in_ref_table (struct die_info *,
10b3939b 1875 struct dwarf2_cu *);
c906108c 1876
ff39bb5e 1877static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1878
ff39bb5e 1879static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1880
348e048f 1881static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1882 const struct attribute *,
348e048f
DE
1883 struct dwarf2_cu **);
1884
10b3939b 1885static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1886 const struct attribute *,
f2f0e013 1887 struct dwarf2_cu **);
c906108c 1888
348e048f 1889static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1890 const struct attribute *,
348e048f
DE
1891 struct dwarf2_cu **);
1892
ac9ec31b
DE
1893static struct type *get_signatured_type (struct die_info *, ULONGEST,
1894 struct dwarf2_cu *);
1895
1896static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1897 const struct attribute *,
ac9ec31b
DE
1898 struct dwarf2_cu *);
1899
e5fe5e75 1900static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1901
52dc124a 1902static void read_signatured_type (struct signatured_type *);
348e048f 1903
63e43d3a
PMR
1904static int attr_to_dynamic_prop (const struct attribute *attr,
1905 struct die_info *die, struct dwarf2_cu *cu,
1906 struct dynamic_prop *prop);
1907
c906108c
SS
1908/* memory allocation interface */
1909
7b5a2f43 1910static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1911
b60c80d6 1912static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1913
43f3e411 1914static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1915
6e5a29e1 1916static int attr_form_is_block (const struct attribute *);
8e19ed76 1917
6e5a29e1 1918static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1919
6e5a29e1 1920static int attr_form_is_constant (const struct attribute *);
3690dd37 1921
6e5a29e1 1922static int attr_form_is_ref (const struct attribute *);
7771576e 1923
8cf6f0b1
TT
1924static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1925 struct dwarf2_loclist_baton *baton,
ff39bb5e 1926 const struct attribute *attr);
8cf6f0b1 1927
ff39bb5e 1928static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1929 struct symbol *sym,
f1e6e072
TT
1930 struct dwarf2_cu *cu,
1931 int is_block);
4c2df51b 1932
d521ce57
TT
1933static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1934 const gdb_byte *info_ptr,
1935 struct abbrev_info *abbrev);
4bb7a0a7 1936
72bf9492
DJ
1937static void free_stack_comp_unit (void *);
1938
72bf9492
DJ
1939static hashval_t partial_die_hash (const void *item);
1940
1941static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1942
ae038cb0 1943static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1944 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1945
9816fde3 1946static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1947 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1948
1949static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1950 struct die_info *comp_unit_die,
1951 enum language pretend_language);
93311388 1952
68dc6402 1953static void free_heap_comp_unit (void *);
ae038cb0
DJ
1954
1955static void free_cached_comp_units (void *);
1956
1957static void age_cached_comp_units (void);
1958
dee91e82 1959static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1960
f792889a
DJ
1961static struct type *set_die_type (struct die_info *, struct type *,
1962 struct dwarf2_cu *);
1c379e20 1963
ae038cb0
DJ
1964static void create_all_comp_units (struct objfile *);
1965
0e50663e 1966static int create_all_type_units (struct objfile *);
1fd400ff 1967
95554aad
TT
1968static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1969 enum language);
10b3939b 1970
95554aad
TT
1971static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1972 enum language);
10b3939b 1973
f4dc4d17
DE
1974static void process_full_type_unit (struct dwarf2_per_cu_data *,
1975 enum language);
1976
10b3939b
DJ
1977static void dwarf2_add_dependence (struct dwarf2_cu *,
1978 struct dwarf2_per_cu_data *);
1979
ae038cb0
DJ
1980static void dwarf2_mark (struct dwarf2_cu *);
1981
1982static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1983
b64f50a1 1984static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1985 struct dwarf2_per_cu_data *);
673bfd45 1986
f792889a 1987static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1988
9291a0cd
TT
1989static void dwarf2_release_queue (void *dummy);
1990
95554aad
TT
1991static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1992 enum language pretend_language);
1993
a0f42c21 1994static void process_queue (void);
9291a0cd 1995
d721ba37
PA
1996/* The return type of find_file_and_directory. Note, the enclosed
1997 string pointers are only valid while this object is valid. */
1998
1999struct file_and_directory
2000{
2001 /* The filename. This is never NULL. */
2002 const char *name;
2003
2004 /* The compilation directory. NULL if not known. If we needed to
2005 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2006 points directly to the DW_AT_comp_dir string attribute owned by
2007 the obstack that owns the DIE. */
2008 const char *comp_dir;
2009
2010 /* If we needed to build a new string for comp_dir, this is what
2011 owns the storage. */
2012 std::string comp_dir_storage;
2013};
2014
2015static file_and_directory find_file_and_directory (struct die_info *die,
2016 struct dwarf2_cu *cu);
9291a0cd
TT
2017
2018static char *file_full_name (int file, struct line_header *lh,
2019 const char *comp_dir);
2020
43988095
JK
2021/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2022enum class rcuh_kind { COMPILE, TYPE };
2023
d521ce57 2024static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2025 (struct comp_unit_head *header,
2026 struct dwarf2_section_info *section,
d521ce57 2027 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2028 rcuh_kind section_kind);
36586728 2029
fd820528 2030static void init_cutu_and_read_dies
f4dc4d17
DE
2031 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2032 int use_existing_cu, int keep,
3019eac3
DE
2033 die_reader_func_ftype *die_reader_func, void *data);
2034
dee91e82
DE
2035static void init_cutu_and_read_dies_simple
2036 (struct dwarf2_per_cu_data *this_cu,
2037 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2038
673bfd45 2039static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2040
3019eac3
DE
2041static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2042
57d63ce2
DE
2043static struct dwo_unit *lookup_dwo_unit_in_dwp
2044 (struct dwp_file *dwp_file, const char *comp_dir,
2045 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2046
2047static struct dwp_file *get_dwp_file (void);
2048
3019eac3 2049static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2050 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2051
2052static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2053 (struct signatured_type *, const char *, const char *);
3019eac3 2054
89e63ee4
DE
2055static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2056
3019eac3
DE
2057static void free_dwo_file_cleanup (void *);
2058
95554aad
TT
2059static void process_cu_includes (void);
2060
1b80a9fa 2061static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2062
2063static void free_line_header_voidp (void *arg);
4390d890
DE
2064\f
2065/* Various complaints about symbol reading that don't abort the process. */
2066
2067static void
2068dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2069{
2070 complaint (&symfile_complaints,
2071 _("statement list doesn't fit in .debug_line section"));
2072}
2073
2074static void
2075dwarf2_debug_line_missing_file_complaint (void)
2076{
2077 complaint (&symfile_complaints,
2078 _(".debug_line section has line data without a file"));
2079}
2080
2081static void
2082dwarf2_debug_line_missing_end_sequence_complaint (void)
2083{
2084 complaint (&symfile_complaints,
2085 _(".debug_line section has line "
2086 "program sequence without an end"));
2087}
2088
2089static void
2090dwarf2_complex_location_expr_complaint (void)
2091{
2092 complaint (&symfile_complaints, _("location expression too complex"));
2093}
2094
2095static void
2096dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2097 int arg3)
2098{
2099 complaint (&symfile_complaints,
2100 _("const value length mismatch for '%s', got %d, expected %d"),
2101 arg1, arg2, arg3);
2102}
2103
2104static void
2105dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2106{
2107 complaint (&symfile_complaints,
2108 _("debug info runs off end of %s section"
2109 " [in module %s]"),
a32a8923
DE
2110 get_section_name (section),
2111 get_section_file_name (section));
4390d890 2112}
1b80a9fa 2113
4390d890
DE
2114static void
2115dwarf2_macro_malformed_definition_complaint (const char *arg1)
2116{
2117 complaint (&symfile_complaints,
2118 _("macro debug info contains a "
2119 "malformed macro definition:\n`%s'"),
2120 arg1);
2121}
2122
2123static void
2124dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2125{
2126 complaint (&symfile_complaints,
2127 _("invalid attribute class or form for '%s' in '%s'"),
2128 arg1, arg2);
2129}
527f3840
JK
2130
2131/* Hash function for line_header_hash. */
2132
2133static hashval_t
2134line_header_hash (const struct line_header *ofs)
2135{
9c541725 2136 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2137}
2138
2139/* Hash function for htab_create_alloc_ex for line_header_hash. */
2140
2141static hashval_t
2142line_header_hash_voidp (const void *item)
2143{
9a3c8263 2144 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2145
2146 return line_header_hash (ofs);
2147}
2148
2149/* Equality function for line_header_hash. */
2150
2151static int
2152line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2153{
9a3c8263
SM
2154 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2155 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2156
9c541725 2157 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2158 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2159}
2160
4390d890 2161\f
9291a0cd
TT
2162#if WORDS_BIGENDIAN
2163
2164/* Convert VALUE between big- and little-endian. */
2165static offset_type
2166byte_swap (offset_type value)
2167{
2168 offset_type result;
2169
2170 result = (value & 0xff) << 24;
2171 result |= (value & 0xff00) << 8;
2172 result |= (value & 0xff0000) >> 8;
2173 result |= (value & 0xff000000) >> 24;
2174 return result;
2175}
2176
2177#define MAYBE_SWAP(V) byte_swap (V)
2178
2179#else
bc8f2430 2180#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2181#endif /* WORDS_BIGENDIAN */
2182
31aa7e4e
JB
2183/* Read the given attribute value as an address, taking the attribute's
2184 form into account. */
2185
2186static CORE_ADDR
2187attr_value_as_address (struct attribute *attr)
2188{
2189 CORE_ADDR addr;
2190
2191 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2192 {
2193 /* Aside from a few clearly defined exceptions, attributes that
2194 contain an address must always be in DW_FORM_addr form.
2195 Unfortunately, some compilers happen to be violating this
2196 requirement by encoding addresses using other forms, such
2197 as DW_FORM_data4 for example. For those broken compilers,
2198 we try to do our best, without any guarantee of success,
2199 to interpret the address correctly. It would also be nice
2200 to generate a complaint, but that would require us to maintain
2201 a list of legitimate cases where a non-address form is allowed,
2202 as well as update callers to pass in at least the CU's DWARF
2203 version. This is more overhead than what we're willing to
2204 expand for a pretty rare case. */
2205 addr = DW_UNSND (attr);
2206 }
2207 else
2208 addr = DW_ADDR (attr);
2209
2210 return addr;
2211}
2212
9291a0cd
TT
2213/* The suffix for an index file. */
2214#define INDEX_SUFFIX ".gdb-index"
2215
330cdd98
PA
2216/* See declaration. */
2217
2218dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2219 const dwarf2_debug_sections *names)
2220 : objfile (objfile_)
2221{
2222 if (names == NULL)
2223 names = &dwarf2_elf_names;
2224
2225 bfd *obfd = objfile->obfd;
2226
2227 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2228 locate_sections (obfd, sec, *names);
2229}
2230
2231dwarf2_per_objfile::~dwarf2_per_objfile ()
2232{
2233 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2234 free_cached_comp_units ();
2235
2236 if (quick_file_names_table)
2237 htab_delete (quick_file_names_table);
2238
2239 if (line_header_hash)
2240 htab_delete (line_header_hash);
2241
2242 /* Everything else should be on the objfile obstack. */
2243}
2244
2245/* See declaration. */
2246
2247void
2248dwarf2_per_objfile::free_cached_comp_units ()
2249{
2250 dwarf2_per_cu_data *per_cu = read_in_chain;
2251 dwarf2_per_cu_data **last_chain = &read_in_chain;
2252 while (per_cu != NULL)
2253 {
2254 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2255
2256 free_heap_comp_unit (per_cu->cu);
2257 *last_chain = next_cu;
2258 per_cu = next_cu;
2259 }
2260}
2261
c906108c 2262/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2263 information and return true if we have enough to do something.
2264 NAMES points to the dwarf2 section names, or is NULL if the standard
2265 ELF names are used. */
c906108c
SS
2266
2267int
251d32d9
TG
2268dwarf2_has_info (struct objfile *objfile,
2269 const struct dwarf2_debug_sections *names)
c906108c 2270{
9a3c8263
SM
2271 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2272 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2273 if (!dwarf2_per_objfile)
2274 {
2275 /* Initialize per-objfile state. */
2276 struct dwarf2_per_objfile *data
8d749320 2277 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2278
330cdd98
PA
2279 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2280 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2281 }
73869dc2 2282 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2283 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2284 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2285 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2286}
2287
2288/* Return the containing section of virtual section SECTION. */
2289
2290static struct dwarf2_section_info *
2291get_containing_section (const struct dwarf2_section_info *section)
2292{
2293 gdb_assert (section->is_virtual);
2294 return section->s.containing_section;
c906108c
SS
2295}
2296
a32a8923
DE
2297/* Return the bfd owner of SECTION. */
2298
2299static struct bfd *
2300get_section_bfd_owner (const struct dwarf2_section_info *section)
2301{
73869dc2
DE
2302 if (section->is_virtual)
2303 {
2304 section = get_containing_section (section);
2305 gdb_assert (!section->is_virtual);
2306 }
049412e3 2307 return section->s.section->owner;
a32a8923
DE
2308}
2309
2310/* Return the bfd section of SECTION.
2311 Returns NULL if the section is not present. */
2312
2313static asection *
2314get_section_bfd_section (const struct dwarf2_section_info *section)
2315{
73869dc2
DE
2316 if (section->is_virtual)
2317 {
2318 section = get_containing_section (section);
2319 gdb_assert (!section->is_virtual);
2320 }
049412e3 2321 return section->s.section;
a32a8923
DE
2322}
2323
2324/* Return the name of SECTION. */
2325
2326static const char *
2327get_section_name (const struct dwarf2_section_info *section)
2328{
2329 asection *sectp = get_section_bfd_section (section);
2330
2331 gdb_assert (sectp != NULL);
2332 return bfd_section_name (get_section_bfd_owner (section), sectp);
2333}
2334
2335/* Return the name of the file SECTION is in. */
2336
2337static const char *
2338get_section_file_name (const struct dwarf2_section_info *section)
2339{
2340 bfd *abfd = get_section_bfd_owner (section);
2341
2342 return bfd_get_filename (abfd);
2343}
2344
2345/* Return the id of SECTION.
2346 Returns 0 if SECTION doesn't exist. */
2347
2348static int
2349get_section_id (const struct dwarf2_section_info *section)
2350{
2351 asection *sectp = get_section_bfd_section (section);
2352
2353 if (sectp == NULL)
2354 return 0;
2355 return sectp->id;
2356}
2357
2358/* Return the flags of SECTION.
73869dc2 2359 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2360
2361static int
2362get_section_flags (const struct dwarf2_section_info *section)
2363{
2364 asection *sectp = get_section_bfd_section (section);
2365
2366 gdb_assert (sectp != NULL);
2367 return bfd_get_section_flags (sectp->owner, sectp);
2368}
2369
251d32d9
TG
2370/* When loading sections, we look either for uncompressed section or for
2371 compressed section names. */
233a11ab
CS
2372
2373static int
251d32d9
TG
2374section_is_p (const char *section_name,
2375 const struct dwarf2_section_names *names)
233a11ab 2376{
251d32d9
TG
2377 if (names->normal != NULL
2378 && strcmp (section_name, names->normal) == 0)
2379 return 1;
2380 if (names->compressed != NULL
2381 && strcmp (section_name, names->compressed) == 0)
2382 return 1;
2383 return 0;
233a11ab
CS
2384}
2385
330cdd98 2386/* See declaration. */
c906108c 2387
330cdd98
PA
2388void
2389dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2390 const dwarf2_debug_sections &names)
c906108c 2391{
dc7650b8 2392 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2393
dc7650b8
JK
2394 if ((aflag & SEC_HAS_CONTENTS) == 0)
2395 {
2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.info))
c906108c 2398 {
330cdd98
PA
2399 this->info.s.section = sectp;
2400 this->info.size = bfd_get_section_size (sectp);
c906108c 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2403 {
330cdd98
PA
2404 this->abbrev.s.section = sectp;
2405 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.line))
c906108c 2408 {
330cdd98
PA
2409 this->line.s.section = sectp;
2410 this->line.size = bfd_get_section_size (sectp);
c906108c 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.loc))
c906108c 2413 {
330cdd98
PA
2414 this->loc.s.section = sectp;
2415 this->loc.size = bfd_get_section_size (sectp);
c906108c 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.loclists))
43988095 2418 {
330cdd98
PA
2419 this->loclists.s.section = sectp;
2420 this->loclists.size = bfd_get_section_size (sectp);
43988095 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2423 {
330cdd98
PA
2424 this->macinfo.s.section = sectp;
2425 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2426 }
330cdd98 2427 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2428 {
330cdd98
PA
2429 this->macro.s.section = sectp;
2430 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2431 }
330cdd98 2432 else if (section_is_p (sectp->name, &names.str))
c906108c 2433 {
330cdd98
PA
2434 this->str.s.section = sectp;
2435 this->str.size = bfd_get_section_size (sectp);
c906108c 2436 }
330cdd98 2437 else if (section_is_p (sectp->name, &names.line_str))
43988095 2438 {
330cdd98
PA
2439 this->line_str.s.section = sectp;
2440 this->line_str.size = bfd_get_section_size (sectp);
43988095 2441 }
330cdd98 2442 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2443 {
330cdd98
PA
2444 this->addr.s.section = sectp;
2445 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2446 }
330cdd98 2447 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2448 {
330cdd98
PA
2449 this->frame.s.section = sectp;
2450 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2451 }
330cdd98 2452 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2453 {
330cdd98
PA
2454 this->eh_frame.s.section = sectp;
2455 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2456 }
330cdd98 2457 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2458 {
330cdd98
PA
2459 this->ranges.s.section = sectp;
2460 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2461 }
330cdd98 2462 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2463 {
330cdd98
PA
2464 this->rnglists.s.section = sectp;
2465 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2466 }
330cdd98 2467 else if (section_is_p (sectp->name, &names.types))
348e048f 2468 {
8b70b953
TT
2469 struct dwarf2_section_info type_section;
2470
2471 memset (&type_section, 0, sizeof (type_section));
049412e3 2472 type_section.s.section = sectp;
8b70b953
TT
2473 type_section.size = bfd_get_section_size (sectp);
2474
330cdd98 2475 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2476 &type_section);
348e048f 2477 }
330cdd98 2478 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2479 {
330cdd98
PA
2480 this->gdb_index.s.section = sectp;
2481 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2482 }
dce234bc 2483
b4e1fd61 2484 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2485 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2486 this->has_section_at_zero = true;
c906108c
SS
2487}
2488
fceca515
DE
2489/* A helper function that decides whether a section is empty,
2490 or not present. */
9e0ac564
TT
2491
2492static int
19ac8c2e 2493dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2494{
73869dc2
DE
2495 if (section->is_virtual)
2496 return section->size == 0;
049412e3 2497 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2498}
2499
3019eac3
DE
2500/* Read the contents of the section INFO.
2501 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2502 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2503 of the DWO file.
dce234bc 2504 If the section is compressed, uncompress it before returning. */
c906108c 2505
dce234bc
PP
2506static void
2507dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2508{
a32a8923 2509 asection *sectp;
3019eac3 2510 bfd *abfd;
dce234bc 2511 gdb_byte *buf, *retbuf;
c906108c 2512
be391dca
TT
2513 if (info->readin)
2514 return;
dce234bc 2515 info->buffer = NULL;
be391dca 2516 info->readin = 1;
188dd5d6 2517
9e0ac564 2518 if (dwarf2_section_empty_p (info))
dce234bc 2519 return;
c906108c 2520
a32a8923 2521 sectp = get_section_bfd_section (info);
3019eac3 2522
73869dc2
DE
2523 /* If this is a virtual section we need to read in the real one first. */
2524 if (info->is_virtual)
2525 {
2526 struct dwarf2_section_info *containing_section =
2527 get_containing_section (info);
2528
2529 gdb_assert (sectp != NULL);
2530 if ((sectp->flags & SEC_RELOC) != 0)
2531 {
2532 error (_("Dwarf Error: DWP format V2 with relocations is not"
2533 " supported in section %s [in module %s]"),
2534 get_section_name (info), get_section_file_name (info));
2535 }
2536 dwarf2_read_section (objfile, containing_section);
2537 /* Other code should have already caught virtual sections that don't
2538 fit. */
2539 gdb_assert (info->virtual_offset + info->size
2540 <= containing_section->size);
2541 /* If the real section is empty or there was a problem reading the
2542 section we shouldn't get here. */
2543 gdb_assert (containing_section->buffer != NULL);
2544 info->buffer = containing_section->buffer + info->virtual_offset;
2545 return;
2546 }
2547
4bf44c1c
TT
2548 /* If the section has relocations, we must read it ourselves.
2549 Otherwise we attach it to the BFD. */
2550 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2551 {
d521ce57 2552 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2553 return;
dce234bc 2554 }
dce234bc 2555
224c3ddb 2556 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2557 info->buffer = buf;
dce234bc
PP
2558
2559 /* When debugging .o files, we may need to apply relocations; see
2560 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2561 We never compress sections in .o files, so we only need to
2562 try this when the section is not compressed. */
ac8035ab 2563 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2564 if (retbuf != NULL)
2565 {
2566 info->buffer = retbuf;
2567 return;
2568 }
2569
a32a8923
DE
2570 abfd = get_section_bfd_owner (info);
2571 gdb_assert (abfd != NULL);
2572
dce234bc
PP
2573 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2574 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2575 {
2576 error (_("Dwarf Error: Can't read DWARF data"
2577 " in section %s [in module %s]"),
2578 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2579 }
dce234bc
PP
2580}
2581
9e0ac564
TT
2582/* A helper function that returns the size of a section in a safe way.
2583 If you are positive that the section has been read before using the
2584 size, then it is safe to refer to the dwarf2_section_info object's
2585 "size" field directly. In other cases, you must call this
2586 function, because for compressed sections the size field is not set
2587 correctly until the section has been read. */
2588
2589static bfd_size_type
2590dwarf2_section_size (struct objfile *objfile,
2591 struct dwarf2_section_info *info)
2592{
2593 if (!info->readin)
2594 dwarf2_read_section (objfile, info);
2595 return info->size;
2596}
2597
dce234bc 2598/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2599 SECTION_NAME. */
af34e669 2600
dce234bc 2601void
3017a003
TG
2602dwarf2_get_section_info (struct objfile *objfile,
2603 enum dwarf2_section_enum sect,
d521ce57 2604 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2605 bfd_size_type *sizep)
2606{
2607 struct dwarf2_per_objfile *data
9a3c8263
SM
2608 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2609 dwarf2_objfile_data_key);
dce234bc 2610 struct dwarf2_section_info *info;
a3b2a86b
TT
2611
2612 /* We may see an objfile without any DWARF, in which case we just
2613 return nothing. */
2614 if (data == NULL)
2615 {
2616 *sectp = NULL;
2617 *bufp = NULL;
2618 *sizep = 0;
2619 return;
2620 }
3017a003
TG
2621 switch (sect)
2622 {
2623 case DWARF2_DEBUG_FRAME:
2624 info = &data->frame;
2625 break;
2626 case DWARF2_EH_FRAME:
2627 info = &data->eh_frame;
2628 break;
2629 default:
2630 gdb_assert_not_reached ("unexpected section");
2631 }
dce234bc 2632
9e0ac564 2633 dwarf2_read_section (objfile, info);
dce234bc 2634
a32a8923 2635 *sectp = get_section_bfd_section (info);
dce234bc
PP
2636 *bufp = info->buffer;
2637 *sizep = info->size;
2638}
2639
36586728
TT
2640/* A helper function to find the sections for a .dwz file. */
2641
2642static void
2643locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2644{
9a3c8263 2645 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2646
2647 /* Note that we only support the standard ELF names, because .dwz
2648 is ELF-only (at the time of writing). */
2649 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2650 {
049412e3 2651 dwz_file->abbrev.s.section = sectp;
36586728
TT
2652 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2653 }
2654 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2655 {
049412e3 2656 dwz_file->info.s.section = sectp;
36586728
TT
2657 dwz_file->info.size = bfd_get_section_size (sectp);
2658 }
2659 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2660 {
049412e3 2661 dwz_file->str.s.section = sectp;
36586728
TT
2662 dwz_file->str.size = bfd_get_section_size (sectp);
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2665 {
049412e3 2666 dwz_file->line.s.section = sectp;
36586728
TT
2667 dwz_file->line.size = bfd_get_section_size (sectp);
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2670 {
049412e3 2671 dwz_file->macro.s.section = sectp;
36586728
TT
2672 dwz_file->macro.size = bfd_get_section_size (sectp);
2673 }
2ec9a5e0
TT
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2675 {
049412e3 2676 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2677 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2678 }
36586728
TT
2679}
2680
4db1a1dc
TT
2681/* Open the separate '.dwz' debug file, if needed. Return NULL if
2682 there is no .gnu_debugaltlink section in the file. Error if there
2683 is such a section but the file cannot be found. */
36586728
TT
2684
2685static struct dwz_file *
2686dwarf2_get_dwz_file (void)
2687{
4db1a1dc 2688 char *data;
36586728
TT
2689 struct cleanup *cleanup;
2690 const char *filename;
2691 struct dwz_file *result;
acd13123 2692 bfd_size_type buildid_len_arg;
dc294be5
TT
2693 size_t buildid_len;
2694 bfd_byte *buildid;
36586728
TT
2695
2696 if (dwarf2_per_objfile->dwz_file != NULL)
2697 return dwarf2_per_objfile->dwz_file;
2698
4db1a1dc
TT
2699 bfd_set_error (bfd_error_no_error);
2700 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2701 &buildid_len_arg, &buildid);
4db1a1dc
TT
2702 if (data == NULL)
2703 {
2704 if (bfd_get_error () == bfd_error_no_error)
2705 return NULL;
2706 error (_("could not read '.gnu_debugaltlink' section: %s"),
2707 bfd_errmsg (bfd_get_error ()));
2708 }
36586728 2709 cleanup = make_cleanup (xfree, data);
dc294be5 2710 make_cleanup (xfree, buildid);
36586728 2711
acd13123
TT
2712 buildid_len = (size_t) buildid_len_arg;
2713
f9d83a0b 2714 filename = (const char *) data;
d721ba37
PA
2715
2716 std::string abs_storage;
36586728
TT
2717 if (!IS_ABSOLUTE_PATH (filename))
2718 {
14278e1f
TT
2719 gdb::unique_xmalloc_ptr<char> abs
2720 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2721
14278e1f 2722 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2723 filename = abs_storage.c_str ();
36586728
TT
2724 }
2725
dc294be5
TT
2726 /* First try the file name given in the section. If that doesn't
2727 work, try to use the build-id instead. */
192b62ce 2728 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2729 if (dwz_bfd != NULL)
36586728 2730 {
192b62ce
TT
2731 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2732 dwz_bfd.release ();
36586728
TT
2733 }
2734
dc294be5
TT
2735 if (dwz_bfd == NULL)
2736 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2737
2738 if (dwz_bfd == NULL)
2739 error (_("could not find '.gnu_debugaltlink' file for %s"),
2740 objfile_name (dwarf2_per_objfile->objfile));
2741
36586728
TT
2742 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2743 struct dwz_file);
192b62ce 2744 result->dwz_bfd = dwz_bfd.release ();
36586728 2745
192b62ce 2746 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2747
2748 do_cleanups (cleanup);
2749
192b62ce 2750 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2751 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2752 return result;
2753}
9291a0cd 2754\f
7b9f3c50
DE
2755/* DWARF quick_symbols_functions support. */
2756
2757/* TUs can share .debug_line entries, and there can be a lot more TUs than
2758 unique line tables, so we maintain a separate table of all .debug_line
2759 derived entries to support the sharing.
2760 All the quick functions need is the list of file names. We discard the
2761 line_header when we're done and don't need to record it here. */
2762struct quick_file_names
2763{
094b34ac
DE
2764 /* The data used to construct the hash key. */
2765 struct stmt_list_hash hash;
7b9f3c50
DE
2766
2767 /* The number of entries in file_names, real_names. */
2768 unsigned int num_file_names;
2769
2770 /* The file names from the line table, after being run through
2771 file_full_name. */
2772 const char **file_names;
2773
2774 /* The file names from the line table after being run through
2775 gdb_realpath. These are computed lazily. */
2776 const char **real_names;
2777};
2778
2779/* When using the index (and thus not using psymtabs), each CU has an
2780 object of this type. This is used to hold information needed by
2781 the various "quick" methods. */
2782struct dwarf2_per_cu_quick_data
2783{
2784 /* The file table. This can be NULL if there was no file table
2785 or it's currently not read in.
2786 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2787 struct quick_file_names *file_names;
2788
2789 /* The corresponding symbol table. This is NULL if symbols for this
2790 CU have not yet been read. */
43f3e411 2791 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2792
2793 /* A temporary mark bit used when iterating over all CUs in
2794 expand_symtabs_matching. */
2795 unsigned int mark : 1;
2796
2797 /* True if we've tried to read the file table and found there isn't one.
2798 There will be no point in trying to read it again next time. */
2799 unsigned int no_file_data : 1;
2800};
2801
094b34ac
DE
2802/* Utility hash function for a stmt_list_hash. */
2803
2804static hashval_t
2805hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2806{
2807 hashval_t v = 0;
2808
2809 if (stmt_list_hash->dwo_unit != NULL)
2810 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2811 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2812 return v;
2813}
2814
2815/* Utility equality function for a stmt_list_hash. */
2816
2817static int
2818eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2819 const struct stmt_list_hash *rhs)
2820{
2821 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2822 return 0;
2823 if (lhs->dwo_unit != NULL
2824 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2825 return 0;
2826
9c541725 2827 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2828}
2829
7b9f3c50
DE
2830/* Hash function for a quick_file_names. */
2831
2832static hashval_t
2833hash_file_name_entry (const void *e)
2834{
9a3c8263
SM
2835 const struct quick_file_names *file_data
2836 = (const struct quick_file_names *) e;
7b9f3c50 2837
094b34ac 2838 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2839}
2840
2841/* Equality function for a quick_file_names. */
2842
2843static int
2844eq_file_name_entry (const void *a, const void *b)
2845{
9a3c8263
SM
2846 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2847 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2848
094b34ac 2849 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2850}
2851
2852/* Delete function for a quick_file_names. */
2853
2854static void
2855delete_file_name_entry (void *e)
2856{
9a3c8263 2857 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2858 int i;
2859
2860 for (i = 0; i < file_data->num_file_names; ++i)
2861 {
2862 xfree ((void*) file_data->file_names[i]);
2863 if (file_data->real_names)
2864 xfree ((void*) file_data->real_names[i]);
2865 }
2866
2867 /* The space for the struct itself lives on objfile_obstack,
2868 so we don't free it here. */
2869}
2870
2871/* Create a quick_file_names hash table. */
2872
2873static htab_t
2874create_quick_file_names_table (unsigned int nr_initial_entries)
2875{
2876 return htab_create_alloc (nr_initial_entries,
2877 hash_file_name_entry, eq_file_name_entry,
2878 delete_file_name_entry, xcalloc, xfree);
2879}
9291a0cd 2880
918dd910
JK
2881/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2882 have to be created afterwards. You should call age_cached_comp_units after
2883 processing PER_CU->CU. dw2_setup must have been already called. */
2884
2885static void
2886load_cu (struct dwarf2_per_cu_data *per_cu)
2887{
3019eac3 2888 if (per_cu->is_debug_types)
e5fe5e75 2889 load_full_type_unit (per_cu);
918dd910 2890 else
95554aad 2891 load_full_comp_unit (per_cu, language_minimal);
918dd910 2892
cc12ce38
DE
2893 if (per_cu->cu == NULL)
2894 return; /* Dummy CU. */
2dc860c0
DE
2895
2896 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2897}
2898
a0f42c21 2899/* Read in the symbols for PER_CU. */
2fdf6df6 2900
9291a0cd 2901static void
a0f42c21 2902dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2903{
2904 struct cleanup *back_to;
2905
f4dc4d17
DE
2906 /* Skip type_unit_groups, reading the type units they contain
2907 is handled elsewhere. */
2908 if (IS_TYPE_UNIT_GROUP (per_cu))
2909 return;
2910
9291a0cd
TT
2911 back_to = make_cleanup (dwarf2_release_queue, NULL);
2912
95554aad 2913 if (dwarf2_per_objfile->using_index
43f3e411 2914 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2915 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2916 {
2917 queue_comp_unit (per_cu, language_minimal);
2918 load_cu (per_cu);
89e63ee4
DE
2919
2920 /* If we just loaded a CU from a DWO, and we're working with an index
2921 that may badly handle TUs, load all the TUs in that DWO as well.
2922 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2923 if (!per_cu->is_debug_types
cc12ce38 2924 && per_cu->cu != NULL
89e63ee4
DE
2925 && per_cu->cu->dwo_unit != NULL
2926 && dwarf2_per_objfile->index_table != NULL
2927 && dwarf2_per_objfile->index_table->version <= 7
2928 /* DWP files aren't supported yet. */
2929 && get_dwp_file () == NULL)
2930 queue_and_load_all_dwo_tus (per_cu);
95554aad 2931 }
9291a0cd 2932
a0f42c21 2933 process_queue ();
9291a0cd
TT
2934
2935 /* Age the cache, releasing compilation units that have not
2936 been used recently. */
2937 age_cached_comp_units ();
2938
2939 do_cleanups (back_to);
2940}
2941
2942/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2943 the objfile from which this CU came. Returns the resulting symbol
2944 table. */
2fdf6df6 2945
43f3e411 2946static struct compunit_symtab *
a0f42c21 2947dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2948{
95554aad 2949 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2950 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2951 {
2952 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2953 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2954 dw2_do_instantiate_symtab (per_cu);
95554aad 2955 process_cu_includes ();
9291a0cd
TT
2956 do_cleanups (back_to);
2957 }
f194fefb 2958
43f3e411 2959 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2960}
2961
8832e7e3 2962/* Return the CU/TU given its index.
f4dc4d17
DE
2963
2964 This is intended for loops like:
2965
2966 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2967 + dwarf2_per_objfile->n_type_units); ++i)
2968 {
8832e7e3 2969 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2970
2971 ...;
2972 }
2973*/
2fdf6df6 2974
1fd400ff 2975static struct dwarf2_per_cu_data *
8832e7e3 2976dw2_get_cutu (int index)
1fd400ff
TT
2977{
2978 if (index >= dwarf2_per_objfile->n_comp_units)
2979 {
f4dc4d17 2980 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2981 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2982 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2983 }
2984
2985 return dwarf2_per_objfile->all_comp_units[index];
2986}
2987
8832e7e3
DE
2988/* Return the CU given its index.
2989 This differs from dw2_get_cutu in that it's for when you know INDEX
2990 refers to a CU. */
f4dc4d17
DE
2991
2992static struct dwarf2_per_cu_data *
8832e7e3 2993dw2_get_cu (int index)
f4dc4d17 2994{
8832e7e3 2995 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2996
1fd400ff
TT
2997 return dwarf2_per_objfile->all_comp_units[index];
2998}
2999
2ec9a5e0
TT
3000/* A helper for create_cus_from_index that handles a given list of
3001 CUs. */
2fdf6df6 3002
74a0d9f6 3003static void
2ec9a5e0
TT
3004create_cus_from_index_list (struct objfile *objfile,
3005 const gdb_byte *cu_list, offset_type n_elements,
3006 struct dwarf2_section_info *section,
3007 int is_dwz,
3008 int base_offset)
9291a0cd
TT
3009{
3010 offset_type i;
9291a0cd 3011
2ec9a5e0 3012 for (i = 0; i < n_elements; i += 2)
9291a0cd 3013 {
74a0d9f6 3014 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3015
3016 sect_offset sect_off
3017 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3018 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3019 cu_list += 2 * 8;
3020
9c541725
PA
3021 dwarf2_per_cu_data *the_cu
3022 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3023 struct dwarf2_per_cu_data);
3024 the_cu->sect_off = sect_off;
9291a0cd
TT
3025 the_cu->length = length;
3026 the_cu->objfile = objfile;
8a0459fd 3027 the_cu->section = section;
9291a0cd
TT
3028 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3029 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3030 the_cu->is_dwz = is_dwz;
3031 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3032 }
9291a0cd
TT
3033}
3034
2ec9a5e0 3035/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3036 the CU objects for this objfile. */
2ec9a5e0 3037
74a0d9f6 3038static void
2ec9a5e0
TT
3039create_cus_from_index (struct objfile *objfile,
3040 const gdb_byte *cu_list, offset_type cu_list_elements,
3041 const gdb_byte *dwz_list, offset_type dwz_elements)
3042{
3043 struct dwz_file *dwz;
3044
3045 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3046 dwarf2_per_objfile->all_comp_units =
3047 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3048 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3049
74a0d9f6
JK
3050 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3051 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3052
3053 if (dwz_elements == 0)
74a0d9f6 3054 return;
2ec9a5e0
TT
3055
3056 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3057 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3058 cu_list_elements / 2);
2ec9a5e0
TT
3059}
3060
1fd400ff 3061/* Create the signatured type hash table from the index. */
673bfd45 3062
74a0d9f6 3063static void
673bfd45 3064create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3065 struct dwarf2_section_info *section,
673bfd45
DE
3066 const gdb_byte *bytes,
3067 offset_type elements)
1fd400ff
TT
3068{
3069 offset_type i;
673bfd45 3070 htab_t sig_types_hash;
1fd400ff 3071
6aa5f3a6
DE
3072 dwarf2_per_objfile->n_type_units
3073 = dwarf2_per_objfile->n_allocated_type_units
3074 = elements / 3;
8d749320
SM
3075 dwarf2_per_objfile->all_type_units =
3076 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3077
673bfd45 3078 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3079
3080 for (i = 0; i < elements; i += 3)
3081 {
52dc124a 3082 struct signatured_type *sig_type;
9c541725 3083 ULONGEST signature;
1fd400ff 3084 void **slot;
9c541725 3085 cu_offset type_offset_in_tu;
1fd400ff 3086
74a0d9f6 3087 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3088 sect_offset sect_off
3089 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3090 type_offset_in_tu
3091 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3092 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3093 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3094 bytes += 3 * 8;
3095
52dc124a 3096 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3097 struct signatured_type);
52dc124a 3098 sig_type->signature = signature;
9c541725 3099 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3100 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3101 sig_type->per_cu.section = section;
9c541725 3102 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3103 sig_type->per_cu.objfile = objfile;
3104 sig_type->per_cu.v.quick
1fd400ff
TT
3105 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3106 struct dwarf2_per_cu_quick_data);
3107
52dc124a
DE
3108 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3109 *slot = sig_type;
1fd400ff 3110
b4dd5633 3111 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3112 }
3113
673bfd45 3114 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3115}
3116
9291a0cd
TT
3117/* Read the address map data from the mapped index, and use it to
3118 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3119
9291a0cd
TT
3120static void
3121create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3122{
3e29f34a 3123 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3124 const gdb_byte *iter, *end;
9291a0cd 3125 struct addrmap *mutable_map;
9291a0cd
TT
3126 CORE_ADDR baseaddr;
3127
8268c778
PA
3128 auto_obstack temp_obstack;
3129
9291a0cd
TT
3130 mutable_map = addrmap_create_mutable (&temp_obstack);
3131
3132 iter = index->address_table;
3133 end = iter + index->address_table_size;
3134
3135 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3136
3137 while (iter < end)
3138 {
3139 ULONGEST hi, lo, cu_index;
3140 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3141 iter += 8;
3142 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3143 iter += 8;
3144 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3145 iter += 4;
f652bce2 3146
24a55014 3147 if (lo > hi)
f652bce2 3148 {
24a55014
DE
3149 complaint (&symfile_complaints,
3150 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3151 hex_string (lo), hex_string (hi));
24a55014 3152 continue;
f652bce2 3153 }
24a55014
DE
3154
3155 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3156 {
3157 complaint (&symfile_complaints,
3158 _(".gdb_index address table has invalid CU number %u"),
3159 (unsigned) cu_index);
24a55014 3160 continue;
f652bce2 3161 }
24a55014 3162
3e29f34a
MR
3163 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3164 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3165 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3166 }
3167
3168 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3169 &objfile->objfile_obstack);
9291a0cd
TT
3170}
3171
59d7bcaf
JK
3172/* The hash function for strings in the mapped index. This is the same as
3173 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3174 implementation. This is necessary because the hash function is tied to the
3175 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3176 SYMBOL_HASH_NEXT.
3177
3178 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3179
9291a0cd 3180static hashval_t
559a7a62 3181mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3182{
3183 const unsigned char *str = (const unsigned char *) p;
3184 hashval_t r = 0;
3185 unsigned char c;
3186
3187 while ((c = *str++) != 0)
559a7a62
JK
3188 {
3189 if (index_version >= 5)
3190 c = tolower (c);
3191 r = r * 67 + c - 113;
3192 }
9291a0cd
TT
3193
3194 return r;
3195}
3196
3197/* Find a slot in the mapped index INDEX for the object named NAME.
3198 If NAME is found, set *VEC_OUT to point to the CU vector in the
3199 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3200
9291a0cd
TT
3201static int
3202find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3203 offset_type **vec_out)
3204{
0cf03b49
JK
3205 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3206 offset_type hash;
9291a0cd 3207 offset_type slot, step;
559a7a62 3208 int (*cmp) (const char *, const char *);
9291a0cd 3209
0cf03b49 3210 if (current_language->la_language == language_cplus
45280282
IB
3211 || current_language->la_language == language_fortran
3212 || current_language->la_language == language_d)
0cf03b49
JK
3213 {
3214 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3215 not contain any. */
a8719064 3216
72998fb3 3217 if (strchr (name, '(') != NULL)
0cf03b49 3218 {
72998fb3 3219 char *without_params = cp_remove_params (name);
0cf03b49 3220
72998fb3
DE
3221 if (without_params != NULL)
3222 {
3223 make_cleanup (xfree, without_params);
3224 name = without_params;
3225 }
0cf03b49
JK
3226 }
3227 }
3228
559a7a62 3229 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3230 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3231 simulate our NAME being searched is also lowercased. */
3232 hash = mapped_index_string_hash ((index->version == 4
3233 && case_sensitivity == case_sensitive_off
3234 ? 5 : index->version),
3235 name);
3236
3876f04e
DE
3237 slot = hash & (index->symbol_table_slots - 1);
3238 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3239 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3240
3241 for (;;)
3242 {
3243 /* Convert a slot number to an offset into the table. */
3244 offset_type i = 2 * slot;
3245 const char *str;
3876f04e 3246 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3247 {
3248 do_cleanups (back_to);
3249 return 0;
3250 }
9291a0cd 3251
3876f04e 3252 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3253 if (!cmp (name, str))
9291a0cd
TT
3254 {
3255 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3256 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3257 do_cleanups (back_to);
9291a0cd
TT
3258 return 1;
3259 }
3260
3876f04e 3261 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3262 }
3263}
3264
2ec9a5e0
TT
3265/* A helper function that reads the .gdb_index from SECTION and fills
3266 in MAP. FILENAME is the name of the file containing the section;
3267 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3268 ok to use deprecated sections.
3269
3270 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3271 out parameters that are filled in with information about the CU and
3272 TU lists in the section.
3273
3274 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3275
9291a0cd 3276static int
2ec9a5e0
TT
3277read_index_from_section (struct objfile *objfile,
3278 const char *filename,
3279 int deprecated_ok,
3280 struct dwarf2_section_info *section,
3281 struct mapped_index *map,
3282 const gdb_byte **cu_list,
3283 offset_type *cu_list_elements,
3284 const gdb_byte **types_list,
3285 offset_type *types_list_elements)
9291a0cd 3286{
948f8e3d 3287 const gdb_byte *addr;
2ec9a5e0 3288 offset_type version;
b3b272e1 3289 offset_type *metadata;
1fd400ff 3290 int i;
9291a0cd 3291
2ec9a5e0 3292 if (dwarf2_section_empty_p (section))
9291a0cd 3293 return 0;
82430852
JK
3294
3295 /* Older elfutils strip versions could keep the section in the main
3296 executable while splitting it for the separate debug info file. */
a32a8923 3297 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3298 return 0;
3299
2ec9a5e0 3300 dwarf2_read_section (objfile, section);
9291a0cd 3301
2ec9a5e0 3302 addr = section->buffer;
9291a0cd 3303 /* Version check. */
1fd400ff 3304 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3305 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3306 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3307 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3308 indices. */
831adc1f 3309 if (version < 4)
481860b3
GB
3310 {
3311 static int warning_printed = 0;
3312 if (!warning_printed)
3313 {
3314 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3315 filename);
481860b3
GB
3316 warning_printed = 1;
3317 }
3318 return 0;
3319 }
3320 /* Index version 4 uses a different hash function than index version
3321 5 and later.
3322
3323 Versions earlier than 6 did not emit psymbols for inlined
3324 functions. Using these files will cause GDB not to be able to
3325 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3326 indices unless the user has done
3327 "set use-deprecated-index-sections on". */
2ec9a5e0 3328 if (version < 6 && !deprecated_ok)
481860b3
GB
3329 {
3330 static int warning_printed = 0;
3331 if (!warning_printed)
3332 {
e615022a
DE
3333 warning (_("\
3334Skipping deprecated .gdb_index section in %s.\n\
3335Do \"set use-deprecated-index-sections on\" before the file is read\n\
3336to use the section anyway."),
2ec9a5e0 3337 filename);
481860b3
GB
3338 warning_printed = 1;
3339 }
3340 return 0;
3341 }
796a7ff8 3342 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3343 of the TU (for symbols coming from TUs),
3344 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3345 Plus gold-generated indices can have duplicate entries for global symbols,
3346 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3347 These are just performance bugs, and we can't distinguish gdb-generated
3348 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3349
481860b3 3350 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3351 longer backward compatible. */
796a7ff8 3352 if (version > 8)
594e8718 3353 return 0;
9291a0cd 3354
559a7a62 3355 map->version = version;
2ec9a5e0 3356 map->total_size = section->size;
9291a0cd
TT
3357
3358 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3359
3360 i = 0;
2ec9a5e0
TT
3361 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3362 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3363 / 8);
1fd400ff
TT
3364 ++i;
3365
2ec9a5e0
TT
3366 *types_list = addr + MAYBE_SWAP (metadata[i]);
3367 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3368 - MAYBE_SWAP (metadata[i]))
3369 / 8);
987d643c 3370 ++i;
1fd400ff
TT
3371
3372 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3373 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3374 - MAYBE_SWAP (metadata[i]));
3375 ++i;
3376
3876f04e
DE
3377 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3378 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3379 - MAYBE_SWAP (metadata[i]))
3380 / (2 * sizeof (offset_type)));
1fd400ff 3381 ++i;
9291a0cd 3382
f9d83a0b 3383 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3384
2ec9a5e0
TT
3385 return 1;
3386}
3387
3388
3389/* Read the index file. If everything went ok, initialize the "quick"
3390 elements of all the CUs and return 1. Otherwise, return 0. */
3391
3392static int
3393dwarf2_read_index (struct objfile *objfile)
3394{
3395 struct mapped_index local_map, *map;
3396 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3397 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3398 struct dwz_file *dwz;
2ec9a5e0 3399
4262abfb 3400 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3401 use_deprecated_index_sections,
3402 &dwarf2_per_objfile->gdb_index, &local_map,
3403 &cu_list, &cu_list_elements,
3404 &types_list, &types_list_elements))
3405 return 0;
3406
0fefef59 3407 /* Don't use the index if it's empty. */
2ec9a5e0 3408 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3409 return 0;
3410
2ec9a5e0
TT
3411 /* If there is a .dwz file, read it so we can get its CU list as
3412 well. */
4db1a1dc
TT
3413 dwz = dwarf2_get_dwz_file ();
3414 if (dwz != NULL)
2ec9a5e0 3415 {
2ec9a5e0
TT
3416 struct mapped_index dwz_map;
3417 const gdb_byte *dwz_types_ignore;
3418 offset_type dwz_types_elements_ignore;
3419
3420 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3421 1,
3422 &dwz->gdb_index, &dwz_map,
3423 &dwz_list, &dwz_list_elements,
3424 &dwz_types_ignore,
3425 &dwz_types_elements_ignore))
3426 {
3427 warning (_("could not read '.gdb_index' section from %s; skipping"),
3428 bfd_get_filename (dwz->dwz_bfd));
3429 return 0;
3430 }
3431 }
3432
74a0d9f6
JK
3433 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3434 dwz_list_elements);
1fd400ff 3435
8b70b953
TT
3436 if (types_list_elements)
3437 {
3438 struct dwarf2_section_info *section;
3439
3440 /* We can only handle a single .debug_types when we have an
3441 index. */
3442 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3443 return 0;
3444
3445 section = VEC_index (dwarf2_section_info_def,
3446 dwarf2_per_objfile->types, 0);
3447
74a0d9f6
JK
3448 create_signatured_type_table_from_index (objfile, section, types_list,
3449 types_list_elements);
8b70b953 3450 }
9291a0cd 3451
2ec9a5e0
TT
3452 create_addrmap_from_index (objfile, &local_map);
3453
8d749320 3454 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3455 *map = local_map;
9291a0cd
TT
3456
3457 dwarf2_per_objfile->index_table = map;
3458 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3459 dwarf2_per_objfile->quick_file_names_table =
3460 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3461
3462 return 1;
3463}
3464
3465/* A helper for the "quick" functions which sets the global
3466 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3467
9291a0cd
TT
3468static void
3469dw2_setup (struct objfile *objfile)
3470{
9a3c8263
SM
3471 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3472 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3473 gdb_assert (dwarf2_per_objfile);
3474}
3475
dee91e82 3476/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3477
dee91e82
DE
3478static void
3479dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3480 const gdb_byte *info_ptr,
dee91e82
DE
3481 struct die_info *comp_unit_die,
3482 int has_children,
3483 void *data)
9291a0cd 3484{
dee91e82
DE
3485 struct dwarf2_cu *cu = reader->cu;
3486 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3487 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3488 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3489 struct attribute *attr;
dee91e82 3490 int i;
7b9f3c50
DE
3491 void **slot;
3492 struct quick_file_names *qfn;
9291a0cd 3493
0186c6a7
DE
3494 gdb_assert (! this_cu->is_debug_types);
3495
07261596
TT
3496 /* Our callers never want to match partial units -- instead they
3497 will match the enclosing full CU. */
3498 if (comp_unit_die->tag == DW_TAG_partial_unit)
3499 {
3500 this_cu->v.quick->no_file_data = 1;
3501 return;
3502 }
3503
0186c6a7 3504 lh_cu = this_cu;
7b9f3c50 3505 slot = NULL;
dee91e82 3506
fff8551c 3507 line_header_up lh;
9c541725 3508 sect_offset line_offset {};
fff8551c 3509
dee91e82 3510 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3511 if (attr)
3512 {
7b9f3c50
DE
3513 struct quick_file_names find_entry;
3514
9c541725 3515 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3516
3517 /* We may have already read in this line header (TU line header sharing).
3518 If we have we're done. */
094b34ac 3519 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3520 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3521 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3522 &find_entry, INSERT);
3523 if (*slot != NULL)
3524 {
9a3c8263 3525 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3526 return;
7b9f3c50
DE
3527 }
3528
3019eac3 3529 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3530 }
3531 if (lh == NULL)
3532 {
094b34ac 3533 lh_cu->v.quick->no_file_data = 1;
dee91e82 3534 return;
9291a0cd
TT
3535 }
3536
8d749320 3537 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3538 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3539 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3540 gdb_assert (slot != NULL);
3541 *slot = qfn;
9291a0cd 3542
d721ba37 3543 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3544
fff8551c 3545 qfn->num_file_names = lh->file_names.size ();
8d749320 3546 qfn->file_names =
fff8551c
PA
3547 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3548 for (i = 0; i < lh->file_names.size (); ++i)
3549 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3550 qfn->real_names = NULL;
9291a0cd 3551
094b34ac 3552 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3553}
3554
3555/* A helper for the "quick" functions which attempts to read the line
3556 table for THIS_CU. */
3557
3558static struct quick_file_names *
e4a48d9d 3559dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3560{
0186c6a7
DE
3561 /* This should never be called for TUs. */
3562 gdb_assert (! this_cu->is_debug_types);
3563 /* Nor type unit groups. */
3564 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3565
dee91e82
DE
3566 if (this_cu->v.quick->file_names != NULL)
3567 return this_cu->v.quick->file_names;
3568 /* If we know there is no line data, no point in looking again. */
3569 if (this_cu->v.quick->no_file_data)
3570 return NULL;
3571
0186c6a7 3572 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3573
3574 if (this_cu->v.quick->no_file_data)
3575 return NULL;
3576 return this_cu->v.quick->file_names;
9291a0cd
TT
3577}
3578
3579/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3580 real path for a given file name from the line table. */
2fdf6df6 3581
9291a0cd 3582static const char *
7b9f3c50
DE
3583dw2_get_real_path (struct objfile *objfile,
3584 struct quick_file_names *qfn, int index)
9291a0cd 3585{
7b9f3c50
DE
3586 if (qfn->real_names == NULL)
3587 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3588 qfn->num_file_names, const char *);
9291a0cd 3589
7b9f3c50 3590 if (qfn->real_names[index] == NULL)
14278e1f 3591 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3592
7b9f3c50 3593 return qfn->real_names[index];
9291a0cd
TT
3594}
3595
3596static struct symtab *
3597dw2_find_last_source_symtab (struct objfile *objfile)
3598{
43f3e411 3599 struct compunit_symtab *cust;
9291a0cd 3600 int index;
ae2de4f8 3601
9291a0cd
TT
3602 dw2_setup (objfile);
3603 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3604 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3605 if (cust == NULL)
3606 return NULL;
3607 return compunit_primary_filetab (cust);
9291a0cd
TT
3608}
3609
7b9f3c50
DE
3610/* Traversal function for dw2_forget_cached_source_info. */
3611
3612static int
3613dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3614{
7b9f3c50 3615 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3616
7b9f3c50 3617 if (file_data->real_names)
9291a0cd 3618 {
7b9f3c50 3619 int i;
9291a0cd 3620
7b9f3c50 3621 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3622 {
7b9f3c50
DE
3623 xfree ((void*) file_data->real_names[i]);
3624 file_data->real_names[i] = NULL;
9291a0cd
TT
3625 }
3626 }
7b9f3c50
DE
3627
3628 return 1;
3629}
3630
3631static void
3632dw2_forget_cached_source_info (struct objfile *objfile)
3633{
3634 dw2_setup (objfile);
3635
3636 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3637 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3638}
3639
f8eba3c6
TT
3640/* Helper function for dw2_map_symtabs_matching_filename that expands
3641 the symtabs and calls the iterator. */
3642
3643static int
3644dw2_map_expand_apply (struct objfile *objfile,
3645 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3646 const char *name, const char *real_path,
14bc53a8 3647 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3648{
43f3e411 3649 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3650
3651 /* Don't visit already-expanded CUs. */
43f3e411 3652 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3653 return 0;
3654
3655 /* This may expand more than one symtab, and we want to iterate over
3656 all of them. */
a0f42c21 3657 dw2_instantiate_symtab (per_cu);
f8eba3c6 3658
14bc53a8
PA
3659 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3660 last_made, callback);
f8eba3c6
TT
3661}
3662
3663/* Implementation of the map_symtabs_matching_filename method. */
3664
14bc53a8
PA
3665static bool
3666dw2_map_symtabs_matching_filename
3667 (struct objfile *objfile, const char *name, const char *real_path,
3668 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3669{
3670 int i;
c011a4f4 3671 const char *name_basename = lbasename (name);
9291a0cd
TT
3672
3673 dw2_setup (objfile);
ae2de4f8 3674
848e3e78
DE
3675 /* The rule is CUs specify all the files, including those used by
3676 any TU, so there's no need to scan TUs here. */
f4dc4d17 3677
848e3e78 3678 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3679 {
3680 int j;
8832e7e3 3681 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3682 struct quick_file_names *file_data;
9291a0cd 3683
3d7bb9d9 3684 /* We only need to look at symtabs not already expanded. */
43f3e411 3685 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3686 continue;
3687
e4a48d9d 3688 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3689 if (file_data == NULL)
9291a0cd
TT
3690 continue;
3691
7b9f3c50 3692 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3693 {
7b9f3c50 3694 const char *this_name = file_data->file_names[j];
da235a7c 3695 const char *this_real_name;
9291a0cd 3696
af529f8f 3697 if (compare_filenames_for_search (this_name, name))
9291a0cd 3698 {
f5b95b50 3699 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3700 callback))
3701 return true;
288e77a7 3702 continue;
4aac40c8 3703 }
9291a0cd 3704
c011a4f4
DE
3705 /* Before we invoke realpath, which can get expensive when many
3706 files are involved, do a quick comparison of the basenames. */
3707 if (! basenames_may_differ
3708 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3709 continue;
3710
da235a7c
JK
3711 this_real_name = dw2_get_real_path (objfile, file_data, j);
3712 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3713 {
da235a7c 3714 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3715 callback))
3716 return true;
288e77a7 3717 continue;
da235a7c 3718 }
9291a0cd 3719
da235a7c
JK
3720 if (real_path != NULL)
3721 {
af529f8f
JK
3722 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3723 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3724 if (this_real_name != NULL
af529f8f 3725 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3726 {
f5b95b50 3727 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3728 callback))
3729 return true;
288e77a7 3730 continue;
9291a0cd
TT
3731 }
3732 }
3733 }
3734 }
3735
14bc53a8 3736 return false;
9291a0cd
TT
3737}
3738
da51c347
DE
3739/* Struct used to manage iterating over all CUs looking for a symbol. */
3740
3741struct dw2_symtab_iterator
9291a0cd 3742{
da51c347
DE
3743 /* The internalized form of .gdb_index. */
3744 struct mapped_index *index;
3745 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3746 int want_specific_block;
3747 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3748 Unused if !WANT_SPECIFIC_BLOCK. */
3749 int block_index;
3750 /* The kind of symbol we're looking for. */
3751 domain_enum domain;
3752 /* The list of CUs from the index entry of the symbol,
3753 or NULL if not found. */
3754 offset_type *vec;
3755 /* The next element in VEC to look at. */
3756 int next;
3757 /* The number of elements in VEC, or zero if there is no match. */
3758 int length;
8943b874
DE
3759 /* Have we seen a global version of the symbol?
3760 If so we can ignore all further global instances.
3761 This is to work around gold/15646, inefficient gold-generated
3762 indices. */
3763 int global_seen;
da51c347 3764};
9291a0cd 3765
da51c347
DE
3766/* Initialize the index symtab iterator ITER.
3767 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3768 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3769
9291a0cd 3770static void
da51c347
DE
3771dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3772 struct mapped_index *index,
3773 int want_specific_block,
3774 int block_index,
3775 domain_enum domain,
3776 const char *name)
3777{
3778 iter->index = index;
3779 iter->want_specific_block = want_specific_block;
3780 iter->block_index = block_index;
3781 iter->domain = domain;
3782 iter->next = 0;
8943b874 3783 iter->global_seen = 0;
da51c347
DE
3784
3785 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3786 iter->length = MAYBE_SWAP (*iter->vec);
3787 else
3788 {
3789 iter->vec = NULL;
3790 iter->length = 0;
3791 }
3792}
3793
3794/* Return the next matching CU or NULL if there are no more. */
3795
3796static struct dwarf2_per_cu_data *
3797dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3798{
3799 for ( ; iter->next < iter->length; ++iter->next)
3800 {
3801 offset_type cu_index_and_attrs =
3802 MAYBE_SWAP (iter->vec[iter->next + 1]);
3803 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3804 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3805 int want_static = iter->block_index != GLOBAL_BLOCK;
3806 /* This value is only valid for index versions >= 7. */
3807 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3808 gdb_index_symbol_kind symbol_kind =
3809 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3810 /* Only check the symbol attributes if they're present.
3811 Indices prior to version 7 don't record them,
3812 and indices >= 7 may elide them for certain symbols
3813 (gold does this). */
3814 int attrs_valid =
3815 (iter->index->version >= 7
3816 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3817
3190f0c6
DE
3818 /* Don't crash on bad data. */
3819 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3820 + dwarf2_per_objfile->n_type_units))
3821 {
3822 complaint (&symfile_complaints,
3823 _(".gdb_index entry has bad CU index"
4262abfb
JK
3824 " [in module %s]"),
3825 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3826 continue;
3827 }
3828
8832e7e3 3829 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3830
da51c347 3831 /* Skip if already read in. */
43f3e411 3832 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3833 continue;
3834
8943b874
DE
3835 /* Check static vs global. */
3836 if (attrs_valid)
3837 {
3838 if (iter->want_specific_block
3839 && want_static != is_static)
3840 continue;
3841 /* Work around gold/15646. */
3842 if (!is_static && iter->global_seen)
3843 continue;
3844 if (!is_static)
3845 iter->global_seen = 1;
3846 }
da51c347
DE
3847
3848 /* Only check the symbol's kind if it has one. */
3849 if (attrs_valid)
3850 {
3851 switch (iter->domain)
3852 {
3853 case VAR_DOMAIN:
3854 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3855 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3856 /* Some types are also in VAR_DOMAIN. */
3857 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3858 continue;
3859 break;
3860 case STRUCT_DOMAIN:
3861 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3862 continue;
3863 break;
3864 case LABEL_DOMAIN:
3865 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3866 continue;
3867 break;
3868 default:
3869 break;
3870 }
3871 }
3872
3873 ++iter->next;
3874 return per_cu;
3875 }
3876
3877 return NULL;
3878}
3879
43f3e411 3880static struct compunit_symtab *
da51c347
DE
3881dw2_lookup_symbol (struct objfile *objfile, int block_index,
3882 const char *name, domain_enum domain)
9291a0cd 3883{
43f3e411 3884 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3885 struct mapped_index *index;
3886
9291a0cd
TT
3887 dw2_setup (objfile);
3888
156942c7
DE
3889 index = dwarf2_per_objfile->index_table;
3890
da51c347 3891 /* index is NULL if OBJF_READNOW. */
156942c7 3892 if (index)
9291a0cd 3893 {
da51c347
DE
3894 struct dw2_symtab_iterator iter;
3895 struct dwarf2_per_cu_data *per_cu;
3896
3897 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3898
da51c347 3899 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3900 {
b2e2f908 3901 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3902 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3903 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3904 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3905
b2e2f908
DE
3906 sym = block_find_symbol (block, name, domain,
3907 block_find_non_opaque_type_preferred,
3908 &with_opaque);
3909
da51c347
DE
3910 /* Some caution must be observed with overloaded functions
3911 and methods, since the index will not contain any overload
3912 information (but NAME might contain it). */
da51c347 3913
b2e2f908 3914 if (sym != NULL
a778f165 3915 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
3916 return stab;
3917 if (with_opaque != NULL
a778f165 3918 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 3919 stab_best = stab;
da51c347
DE
3920
3921 /* Keep looking through other CUs. */
9291a0cd
TT
3922 }
3923 }
9291a0cd 3924
da51c347 3925 return stab_best;
9291a0cd
TT
3926}
3927
3928static void
3929dw2_print_stats (struct objfile *objfile)
3930{
e4a48d9d 3931 int i, total, count;
9291a0cd
TT
3932
3933 dw2_setup (objfile);
e4a48d9d 3934 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3935 count = 0;
e4a48d9d 3936 for (i = 0; i < total; ++i)
9291a0cd 3937 {
8832e7e3 3938 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3939
43f3e411 3940 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3941 ++count;
3942 }
e4a48d9d 3943 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3944 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3945}
3946
779bd270
DE
3947/* This dumps minimal information about the index.
3948 It is called via "mt print objfiles".
3949 One use is to verify .gdb_index has been loaded by the
3950 gdb.dwarf2/gdb-index.exp testcase. */
3951
9291a0cd
TT
3952static void
3953dw2_dump (struct objfile *objfile)
3954{
779bd270
DE
3955 dw2_setup (objfile);
3956 gdb_assert (dwarf2_per_objfile->using_index);
3957 printf_filtered (".gdb_index:");
3958 if (dwarf2_per_objfile->index_table != NULL)
3959 {
3960 printf_filtered (" version %d\n",
3961 dwarf2_per_objfile->index_table->version);
3962 }
3963 else
3964 printf_filtered (" faked for \"readnow\"\n");
3965 printf_filtered ("\n");
9291a0cd
TT
3966}
3967
3968static void
3189cb12
DE
3969dw2_relocate (struct objfile *objfile,
3970 const struct section_offsets *new_offsets,
3971 const struct section_offsets *delta)
9291a0cd
TT
3972{
3973 /* There's nothing to relocate here. */
3974}
3975
3976static void
3977dw2_expand_symtabs_for_function (struct objfile *objfile,
3978 const char *func_name)
3979{
da51c347
DE
3980 struct mapped_index *index;
3981
3982 dw2_setup (objfile);
3983
3984 index = dwarf2_per_objfile->index_table;
3985
3986 /* index is NULL if OBJF_READNOW. */
3987 if (index)
3988 {
3989 struct dw2_symtab_iterator iter;
3990 struct dwarf2_per_cu_data *per_cu;
3991
3992 /* Note: It doesn't matter what we pass for block_index here. */
3993 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3994 func_name);
3995
3996 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3997 dw2_instantiate_symtab (per_cu);
3998 }
9291a0cd
TT
3999}
4000
4001static void
4002dw2_expand_all_symtabs (struct objfile *objfile)
4003{
4004 int i;
4005
4006 dw2_setup (objfile);
1fd400ff
TT
4007
4008 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4009 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4010 {
8832e7e3 4011 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4012
a0f42c21 4013 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4014 }
4015}
4016
4017static void
652a8996
JK
4018dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4019 const char *fullname)
9291a0cd
TT
4020{
4021 int i;
4022
4023 dw2_setup (objfile);
d4637a04
DE
4024
4025 /* We don't need to consider type units here.
4026 This is only called for examining code, e.g. expand_line_sal.
4027 There can be an order of magnitude (or more) more type units
4028 than comp units, and we avoid them if we can. */
4029
4030 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4031 {
4032 int j;
8832e7e3 4033 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4034 struct quick_file_names *file_data;
9291a0cd 4035
3d7bb9d9 4036 /* We only need to look at symtabs not already expanded. */
43f3e411 4037 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4038 continue;
4039
e4a48d9d 4040 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4041 if (file_data == NULL)
9291a0cd
TT
4042 continue;
4043
7b9f3c50 4044 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4045 {
652a8996
JK
4046 const char *this_fullname = file_data->file_names[j];
4047
4048 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4049 {
a0f42c21 4050 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4051 break;
4052 }
4053 }
4054 }
4055}
4056
9291a0cd 4057static void
ade7ed9e 4058dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4059 const char * name, domain_enum domain,
ade7ed9e 4060 int global,
40658b94
PH
4061 int (*callback) (struct block *,
4062 struct symbol *, void *),
2edb89d3
JK
4063 void *data, symbol_compare_ftype *match,
4064 symbol_compare_ftype *ordered_compare)
9291a0cd 4065{
40658b94 4066 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4067 current language is Ada for a non-Ada objfile using GNU index. As Ada
4068 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4069}
4070
4071static void
f8eba3c6
TT
4072dw2_expand_symtabs_matching
4073 (struct objfile *objfile,
14bc53a8
PA
4074 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4075 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4076 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4077 enum search_domain kind)
9291a0cd
TT
4078{
4079 int i;
4080 offset_type iter;
4b5246aa 4081 struct mapped_index *index;
9291a0cd
TT
4082
4083 dw2_setup (objfile);
ae2de4f8
DE
4084
4085 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4086 if (!dwarf2_per_objfile->index_table)
4087 return;
4b5246aa 4088 index = dwarf2_per_objfile->index_table;
9291a0cd 4089
7b08b9eb 4090 if (file_matcher != NULL)
24c79950 4091 {
fc4007c9
TT
4092 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4093 htab_eq_pointer,
4094 NULL, xcalloc, xfree));
4095 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4096 htab_eq_pointer,
4097 NULL, xcalloc, xfree));
24c79950 4098
848e3e78
DE
4099 /* The rule is CUs specify all the files, including those used by
4100 any TU, so there's no need to scan TUs here. */
4101
4102 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4103 {
4104 int j;
8832e7e3 4105 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4106 struct quick_file_names *file_data;
4107 void **slot;
7b08b9eb 4108
61d96d7e
DE
4109 QUIT;
4110
24c79950 4111 per_cu->v.quick->mark = 0;
3d7bb9d9 4112
24c79950 4113 /* We only need to look at symtabs not already expanded. */
43f3e411 4114 if (per_cu->v.quick->compunit_symtab)
24c79950 4115 continue;
7b08b9eb 4116
e4a48d9d 4117 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4118 if (file_data == NULL)
4119 continue;
7b08b9eb 4120
fc4007c9 4121 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4122 continue;
fc4007c9 4123 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4124 {
4125 per_cu->v.quick->mark = 1;
4126 continue;
4127 }
4128
4129 for (j = 0; j < file_data->num_file_names; ++j)
4130 {
da235a7c
JK
4131 const char *this_real_name;
4132
14bc53a8 4133 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4134 {
4135 per_cu->v.quick->mark = 1;
4136 break;
4137 }
da235a7c
JK
4138
4139 /* Before we invoke realpath, which can get expensive when many
4140 files are involved, do a quick comparison of the basenames. */
4141 if (!basenames_may_differ
4142 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4143 true))
da235a7c
JK
4144 continue;
4145
4146 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4147 if (file_matcher (this_real_name, false))
da235a7c
JK
4148 {
4149 per_cu->v.quick->mark = 1;
4150 break;
4151 }
24c79950
TT
4152 }
4153
4154 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4155 ? visited_found.get ()
4156 : visited_not_found.get (),
24c79950
TT
4157 file_data, INSERT);
4158 *slot = file_data;
4159 }
24c79950 4160 }
9291a0cd 4161
3876f04e 4162 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4163 {
4164 offset_type idx = 2 * iter;
4165 const char *name;
4166 offset_type *vec, vec_len, vec_idx;
8943b874 4167 int global_seen = 0;
9291a0cd 4168
61d96d7e
DE
4169 QUIT;
4170
3876f04e 4171 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4172 continue;
4173
3876f04e 4174 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4175
14bc53a8 4176 if (!symbol_matcher (name))
9291a0cd
TT
4177 continue;
4178
4179 /* The name was matched, now expand corresponding CUs that were
4180 marked. */
4b5246aa 4181 vec = (offset_type *) (index->constant_pool
3876f04e 4182 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4183 vec_len = MAYBE_SWAP (vec[0]);
4184 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4185 {
e254ef6a 4186 struct dwarf2_per_cu_data *per_cu;
156942c7 4187 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4188 /* This value is only valid for index versions >= 7. */
4189 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4190 gdb_index_symbol_kind symbol_kind =
4191 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4192 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4193 /* Only check the symbol attributes if they're present.
4194 Indices prior to version 7 don't record them,
4195 and indices >= 7 may elide them for certain symbols
4196 (gold does this). */
4197 int attrs_valid =
4198 (index->version >= 7
4199 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4200
8943b874
DE
4201 /* Work around gold/15646. */
4202 if (attrs_valid)
4203 {
4204 if (!is_static && global_seen)
4205 continue;
4206 if (!is_static)
4207 global_seen = 1;
4208 }
4209
3190f0c6
DE
4210 /* Only check the symbol's kind if it has one. */
4211 if (attrs_valid)
156942c7
DE
4212 {
4213 switch (kind)
4214 {
4215 case VARIABLES_DOMAIN:
4216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4217 continue;
4218 break;
4219 case FUNCTIONS_DOMAIN:
4220 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4221 continue;
4222 break;
4223 case TYPES_DOMAIN:
4224 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4225 continue;
4226 break;
4227 default:
4228 break;
4229 }
4230 }
4231
3190f0c6
DE
4232 /* Don't crash on bad data. */
4233 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4234 + dwarf2_per_objfile->n_type_units))
4235 {
4236 complaint (&symfile_complaints,
4237 _(".gdb_index entry has bad CU index"
4262abfb 4238 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4239 continue;
4240 }
4241
8832e7e3 4242 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4243 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4244 {
4245 int symtab_was_null =
4246 (per_cu->v.quick->compunit_symtab == NULL);
4247
4248 dw2_instantiate_symtab (per_cu);
4249
4250 if (expansion_notify != NULL
4251 && symtab_was_null
4252 && per_cu->v.quick->compunit_symtab != NULL)
4253 {
14bc53a8 4254 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4255 }
4256 }
9291a0cd
TT
4257 }
4258 }
4259}
4260
43f3e411 4261/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4262 symtab. */
4263
43f3e411
DE
4264static struct compunit_symtab *
4265recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4266 CORE_ADDR pc)
9703b513
TT
4267{
4268 int i;
4269
43f3e411
DE
4270 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4271 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4272 return cust;
9703b513 4273
43f3e411 4274 if (cust->includes == NULL)
a3ec0bb1
DE
4275 return NULL;
4276
43f3e411 4277 for (i = 0; cust->includes[i]; ++i)
9703b513 4278 {
43f3e411 4279 struct compunit_symtab *s = cust->includes[i];
9703b513 4280
43f3e411 4281 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4282 if (s != NULL)
4283 return s;
4284 }
4285
4286 return NULL;
4287}
4288
43f3e411
DE
4289static struct compunit_symtab *
4290dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4291 struct bound_minimal_symbol msymbol,
4292 CORE_ADDR pc,
4293 struct obj_section *section,
4294 int warn_if_readin)
9291a0cd
TT
4295{
4296 struct dwarf2_per_cu_data *data;
43f3e411 4297 struct compunit_symtab *result;
9291a0cd
TT
4298
4299 dw2_setup (objfile);
4300
4301 if (!objfile->psymtabs_addrmap)
4302 return NULL;
4303
9a3c8263
SM
4304 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4305 pc);
9291a0cd
TT
4306 if (!data)
4307 return NULL;
4308
43f3e411 4309 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4310 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4311 paddress (get_objfile_arch (objfile), pc));
4312
43f3e411
DE
4313 result
4314 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4315 pc);
9703b513
TT
4316 gdb_assert (result != NULL);
4317 return result;
9291a0cd
TT
4318}
4319
9291a0cd 4320static void
44b13c5a 4321dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4322 void *data, int need_fullname)
9291a0cd 4323{
9291a0cd 4324 dw2_setup (objfile);
ae2de4f8 4325
bbf2f4df 4326 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4327 {
bbf2f4df 4328 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4329
bbf2f4df
PA
4330 htab_up visited (htab_create_alloc (10,
4331 htab_hash_pointer, htab_eq_pointer,
4332 NULL, xcalloc, xfree));
24c79950 4333
bbf2f4df
PA
4334 /* The rule is CUs specify all the files, including those used
4335 by any TU, so there's no need to scan TUs here. We can
4336 ignore file names coming from already-expanded CUs. */
24c79950 4337
bbf2f4df
PA
4338 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4339 {
4340 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4341
bbf2f4df
PA
4342 if (per_cu->v.quick->compunit_symtab)
4343 {
4344 void **slot = htab_find_slot (visited.get (),
4345 per_cu->v.quick->file_names,
4346 INSERT);
9291a0cd 4347
bbf2f4df
PA
4348 *slot = per_cu->v.quick->file_names;
4349 }
24c79950 4350 }
24c79950 4351
bbf2f4df 4352 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4353 {
bbf2f4df
PA
4354 int j;
4355 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4356 struct quick_file_names *file_data;
4357 void **slot;
4358
4359 /* We only need to look at symtabs not already expanded. */
4360 if (per_cu->v.quick->compunit_symtab)
4361 continue;
74e2f255 4362
bbf2f4df
PA
4363 file_data = dw2_get_file_names (per_cu);
4364 if (file_data == NULL)
4365 continue;
4366
4367 slot = htab_find_slot (visited.get (), file_data, INSERT);
4368 if (*slot)
4369 {
4370 /* Already visited. */
4371 continue;
4372 }
4373 *slot = file_data;
4374
4375 for (int j = 0; j < file_data->num_file_names; ++j)
4376 {
4377 const char *filename = file_data->file_names[j];
4378 dwarf2_per_objfile->filenames_cache->seen (filename);
4379 }
9291a0cd
TT
4380 }
4381 }
bbf2f4df
PA
4382
4383 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4384 {
14278e1f 4385 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4386
4387 if (need_fullname)
4388 this_real_name = gdb_realpath (filename);
14278e1f 4389 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4390 });
9291a0cd
TT
4391}
4392
4393static int
4394dw2_has_symbols (struct objfile *objfile)
4395{
4396 return 1;
4397}
4398
4399const struct quick_symbol_functions dwarf2_gdb_index_functions =
4400{
4401 dw2_has_symbols,
4402 dw2_find_last_source_symtab,
4403 dw2_forget_cached_source_info,
f8eba3c6 4404 dw2_map_symtabs_matching_filename,
9291a0cd 4405 dw2_lookup_symbol,
9291a0cd
TT
4406 dw2_print_stats,
4407 dw2_dump,
4408 dw2_relocate,
4409 dw2_expand_symtabs_for_function,
4410 dw2_expand_all_symtabs,
652a8996 4411 dw2_expand_symtabs_with_fullname,
40658b94 4412 dw2_map_matching_symbols,
9291a0cd 4413 dw2_expand_symtabs_matching,
43f3e411 4414 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4415 dw2_map_symbol_filenames
4416};
4417
4418/* Initialize for reading DWARF for this objfile. Return 0 if this
4419 file will use psymtabs, or 1 if using the GNU index. */
4420
4421int
4422dwarf2_initialize_objfile (struct objfile *objfile)
4423{
4424 /* If we're about to read full symbols, don't bother with the
4425 indices. In this case we also don't care if some other debug
4426 format is making psymtabs, because they are all about to be
4427 expanded anyway. */
4428 if ((objfile->flags & OBJF_READNOW))
4429 {
4430 int i;
4431
4432 dwarf2_per_objfile->using_index = 1;
4433 create_all_comp_units (objfile);
0e50663e 4434 create_all_type_units (objfile);
7b9f3c50
DE
4435 dwarf2_per_objfile->quick_file_names_table =
4436 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4437
1fd400ff 4438 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4439 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4440 {
8832e7e3 4441 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4442
e254ef6a
DE
4443 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4444 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4445 }
4446
4447 /* Return 1 so that gdb sees the "quick" functions. However,
4448 these functions will be no-ops because we will have expanded
4449 all symtabs. */
4450 return 1;
4451 }
4452
4453 if (dwarf2_read_index (objfile))
4454 return 1;
4455
9291a0cd
TT
4456 return 0;
4457}
4458
4459\f
4460
dce234bc
PP
4461/* Build a partial symbol table. */
4462
4463void
f29dff0a 4464dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4465{
c9bf0622 4466
f29dff0a 4467 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4468 {
4469 init_psymbol_list (objfile, 1024);
4470 }
4471
492d29ea 4472 TRY
c9bf0622
TT
4473 {
4474 /* This isn't really ideal: all the data we allocate on the
4475 objfile's obstack is still uselessly kept around. However,
4476 freeing it seems unsafe. */
906768f9 4477 psymtab_discarder psymtabs (objfile);
c9bf0622 4478 dwarf2_build_psymtabs_hard (objfile);
906768f9 4479 psymtabs.keep ();
c9bf0622 4480 }
492d29ea
PA
4481 CATCH (except, RETURN_MASK_ERROR)
4482 {
4483 exception_print (gdb_stderr, except);
4484 }
4485 END_CATCH
c906108c 4486}
c906108c 4487
1ce1cefd
DE
4488/* Return the total length of the CU described by HEADER. */
4489
4490static unsigned int
4491get_cu_length (const struct comp_unit_head *header)
4492{
4493 return header->initial_length_size + header->length;
4494}
4495
9c541725 4496/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4497
9c541725
PA
4498static inline bool
4499offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4500{
9c541725
PA
4501 sect_offset bottom = cu_header->sect_off;
4502 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4503
9c541725 4504 return sect_off >= bottom && sect_off < top;
45452591
DE
4505}
4506
3b80fe9b
DE
4507/* Find the base address of the compilation unit for range lists and
4508 location lists. It will normally be specified by DW_AT_low_pc.
4509 In DWARF-3 draft 4, the base address could be overridden by
4510 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4511 compilation units with discontinuous ranges. */
4512
4513static void
4514dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4515{
4516 struct attribute *attr;
4517
4518 cu->base_known = 0;
4519 cu->base_address = 0;
4520
4521 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4522 if (attr)
4523 {
31aa7e4e 4524 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4525 cu->base_known = 1;
4526 }
4527 else
4528 {
4529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4530 if (attr)
4531 {
31aa7e4e 4532 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4533 cu->base_known = 1;
4534 }
4535 }
4536}
4537
93311388 4538/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4539 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4540 NOTE: This leaves members offset, first_die_offset to be filled in
4541 by the caller. */
107d2387 4542
d521ce57 4543static const gdb_byte *
107d2387 4544read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4545 const gdb_byte *info_ptr,
4546 struct dwarf2_section_info *section,
4547 rcuh_kind section_kind)
107d2387
AC
4548{
4549 int signed_addr;
891d2f0b 4550 unsigned int bytes_read;
43988095
JK
4551 const char *filename = get_section_file_name (section);
4552 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4553
4554 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4555 cu_header->initial_length_size = bytes_read;
4556 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4557 info_ptr += bytes_read;
107d2387
AC
4558 cu_header->version = read_2_bytes (abfd, info_ptr);
4559 info_ptr += 2;
43988095
JK
4560 if (cu_header->version < 5)
4561 switch (section_kind)
4562 {
4563 case rcuh_kind::COMPILE:
4564 cu_header->unit_type = DW_UT_compile;
4565 break;
4566 case rcuh_kind::TYPE:
4567 cu_header->unit_type = DW_UT_type;
4568 break;
4569 default:
4570 internal_error (__FILE__, __LINE__,
4571 _("read_comp_unit_head: invalid section_kind"));
4572 }
4573 else
4574 {
4575 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4576 (read_1_byte (abfd, info_ptr));
4577 info_ptr += 1;
4578 switch (cu_header->unit_type)
4579 {
4580 case DW_UT_compile:
4581 if (section_kind != rcuh_kind::COMPILE)
4582 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4583 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4584 filename);
4585 break;
4586 case DW_UT_type:
4587 section_kind = rcuh_kind::TYPE;
4588 break;
4589 default:
4590 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4591 "(is %d, should be %d or %d) [in module %s]"),
4592 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4593 }
4594
4595 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4596 info_ptr += 1;
4597 }
9c541725
PA
4598 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4599 cu_header,
4600 &bytes_read);
613e1657 4601 info_ptr += bytes_read;
43988095
JK
4602 if (cu_header->version < 5)
4603 {
4604 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4605 info_ptr += 1;
4606 }
107d2387
AC
4607 signed_addr = bfd_get_sign_extend_vma (abfd);
4608 if (signed_addr < 0)
8e65ff28 4609 internal_error (__FILE__, __LINE__,
e2e0b3e5 4610 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4611 cu_header->signed_addr_p = signed_addr;
c764a876 4612
43988095
JK
4613 if (section_kind == rcuh_kind::TYPE)
4614 {
4615 LONGEST type_offset;
4616
4617 cu_header->signature = read_8_bytes (abfd, info_ptr);
4618 info_ptr += 8;
4619
4620 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4621 info_ptr += bytes_read;
9c541725
PA
4622 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4623 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4624 error (_("Dwarf Error: Too big type_offset in compilation unit "
4625 "header (is %s) [in module %s]"), plongest (type_offset),
4626 filename);
4627 }
4628
107d2387
AC
4629 return info_ptr;
4630}
4631
36586728
TT
4632/* Helper function that returns the proper abbrev section for
4633 THIS_CU. */
4634
4635static struct dwarf2_section_info *
4636get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4637{
4638 struct dwarf2_section_info *abbrev;
4639
4640 if (this_cu->is_dwz)
4641 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4642 else
4643 abbrev = &dwarf2_per_objfile->abbrev;
4644
4645 return abbrev;
4646}
4647
9ff913ba
DE
4648/* Subroutine of read_and_check_comp_unit_head and
4649 read_and_check_type_unit_head to simplify them.
4650 Perform various error checking on the header. */
4651
4652static void
4653error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4654 struct dwarf2_section_info *section,
4655 struct dwarf2_section_info *abbrev_section)
9ff913ba 4656{
a32a8923 4657 const char *filename = get_section_file_name (section);
9ff913ba 4658
43988095 4659 if (header->version < 2 || header->version > 5)
9ff913ba 4660 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4661 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4662 filename);
4663
9c541725 4664 if (to_underlying (header->abbrev_sect_off)
36586728 4665 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4666 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4667 "(offset 0x%x + 6) [in module %s]"),
4668 to_underlying (header->abbrev_sect_off),
4669 to_underlying (header->sect_off),
9ff913ba
DE
4670 filename);
4671
9c541725 4672 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4673 avoid potential 32-bit overflow. */
9c541725 4674 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4675 > section->size)
9c541725
PA
4676 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4677 "(offset 0x%x + 0) [in module %s]"),
4678 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4679 filename);
4680}
4681
4682/* Read in a CU/TU header and perform some basic error checking.
4683 The contents of the header are stored in HEADER.
4684 The result is a pointer to the start of the first DIE. */
adabb602 4685
d521ce57 4686static const gdb_byte *
9ff913ba
DE
4687read_and_check_comp_unit_head (struct comp_unit_head *header,
4688 struct dwarf2_section_info *section,
4bdcc0c1 4689 struct dwarf2_section_info *abbrev_section,
d521ce57 4690 const gdb_byte *info_ptr,
43988095 4691 rcuh_kind section_kind)
72bf9492 4692{
d521ce57 4693 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4694 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4695
9c541725 4696 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4697
43988095 4698 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4699
9c541725 4700 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4701
4bdcc0c1 4702 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4703
4704 return info_ptr;
348e048f
DE
4705}
4706
f4dc4d17
DE
4707/* Fetch the abbreviation table offset from a comp or type unit header. */
4708
4709static sect_offset
4710read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4711 sect_offset sect_off)
f4dc4d17 4712{
a32a8923 4713 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4714 const gdb_byte *info_ptr;
ac298888 4715 unsigned int initial_length_size, offset_size;
43988095 4716 uint16_t version;
f4dc4d17
DE
4717
4718 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4719 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4720 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4721 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4722 info_ptr += initial_length_size;
4723
4724 version = read_2_bytes (abfd, info_ptr);
4725 info_ptr += 2;
4726 if (version >= 5)
4727 {
4728 /* Skip unit type and address size. */
4729 info_ptr += 2;
4730 }
4731
9c541725 4732 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4733}
4734
aaa75496
JB
4735/* Allocate a new partial symtab for file named NAME and mark this new
4736 partial symtab as being an include of PST. */
4737
4738static void
d521ce57 4739dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4740 struct objfile *objfile)
4741{
4742 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4743
fbd9ab74
JK
4744 if (!IS_ABSOLUTE_PATH (subpst->filename))
4745 {
4746 /* It shares objfile->objfile_obstack. */
4747 subpst->dirname = pst->dirname;
4748 }
4749
aaa75496
JB
4750 subpst->textlow = 0;
4751 subpst->texthigh = 0;
4752
8d749320
SM
4753 subpst->dependencies
4754 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4755 subpst->dependencies[0] = pst;
4756 subpst->number_of_dependencies = 1;
4757
4758 subpst->globals_offset = 0;
4759 subpst->n_global_syms = 0;
4760 subpst->statics_offset = 0;
4761 subpst->n_static_syms = 0;
43f3e411 4762 subpst->compunit_symtab = NULL;
aaa75496
JB
4763 subpst->read_symtab = pst->read_symtab;
4764 subpst->readin = 0;
4765
4766 /* No private part is necessary for include psymtabs. This property
4767 can be used to differentiate between such include psymtabs and
10b3939b 4768 the regular ones. */
58a9656e 4769 subpst->read_symtab_private = NULL;
aaa75496
JB
4770}
4771
4772/* Read the Line Number Program data and extract the list of files
4773 included by the source file represented by PST. Build an include
d85a05f0 4774 partial symtab for each of these included files. */
aaa75496
JB
4775
4776static void
4777dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4778 struct die_info *die,
4779 struct partial_symtab *pst)
aaa75496 4780{
fff8551c 4781 line_header_up lh;
d85a05f0 4782 struct attribute *attr;
aaa75496 4783
d85a05f0
DJ
4784 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4785 if (attr)
9c541725 4786 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4787 if (lh == NULL)
4788 return; /* No linetable, so no includes. */
4789
c6da4cef 4790 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4791 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4792}
4793
348e048f 4794static hashval_t
52dc124a 4795hash_signatured_type (const void *item)
348e048f 4796{
9a3c8263
SM
4797 const struct signatured_type *sig_type
4798 = (const struct signatured_type *) item;
9a619af0 4799
348e048f 4800 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4801 return sig_type->signature;
348e048f
DE
4802}
4803
4804static int
52dc124a 4805eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4806{
9a3c8263
SM
4807 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4808 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4809
348e048f
DE
4810 return lhs->signature == rhs->signature;
4811}
4812
1fd400ff
TT
4813/* Allocate a hash table for signatured types. */
4814
4815static htab_t
673bfd45 4816allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4817{
4818 return htab_create_alloc_ex (41,
52dc124a
DE
4819 hash_signatured_type,
4820 eq_signatured_type,
1fd400ff
TT
4821 NULL,
4822 &objfile->objfile_obstack,
4823 hashtab_obstack_allocate,
4824 dummy_obstack_deallocate);
4825}
4826
d467dd73 4827/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4828
4829static int
d467dd73 4830add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4831{
9a3c8263
SM
4832 struct signatured_type *sigt = (struct signatured_type *) *slot;
4833 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4834
b4dd5633 4835 **datap = sigt;
1fd400ff
TT
4836 ++*datap;
4837
4838 return 1;
4839}
4840
78d4d2c5 4841/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4842 and fill them into TYPES_HTAB. It will process only type units,
4843 therefore DW_UT_type. */
c88ee1f0 4844
78d4d2c5
JK
4845static void
4846create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4847 dwarf2_section_info *section, htab_t &types_htab,
4848 rcuh_kind section_kind)
348e048f 4849{
3019eac3 4850 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4851 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4852 bfd *abfd;
4853 const gdb_byte *info_ptr, *end_ptr;
348e048f 4854
4bdcc0c1
DE
4855 abbrev_section = (dwo_file != NULL
4856 ? &dwo_file->sections.abbrev
4857 : &dwarf2_per_objfile->abbrev);
4858
b4f54984 4859 if (dwarf_read_debug)
43988095
JK
4860 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4861 get_section_name (section),
a32a8923 4862 get_section_file_name (abbrev_section));
09406207 4863
78d4d2c5
JK
4864 dwarf2_read_section (objfile, section);
4865 info_ptr = section->buffer;
348e048f 4866
78d4d2c5
JK
4867 if (info_ptr == NULL)
4868 return;
348e048f 4869
78d4d2c5
JK
4870 /* We can't set abfd until now because the section may be empty or
4871 not present, in which case the bfd is unknown. */
4872 abfd = get_section_bfd_owner (section);
348e048f 4873
78d4d2c5
JK
4874 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4875 because we don't need to read any dies: the signature is in the
4876 header. */
3019eac3 4877
78d4d2c5
JK
4878 end_ptr = info_ptr + section->size;
4879 while (info_ptr < end_ptr)
4880 {
78d4d2c5
JK
4881 struct signatured_type *sig_type;
4882 struct dwo_unit *dwo_tu;
4883 void **slot;
4884 const gdb_byte *ptr = info_ptr;
4885 struct comp_unit_head header;
4886 unsigned int length;
8b70b953 4887
9c541725 4888 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4889
a49dd8dd
JK
4890 /* Initialize it due to a false compiler warning. */
4891 header.signature = -1;
9c541725 4892 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4893
78d4d2c5
JK
4894 /* We need to read the type's signature in order to build the hash
4895 table, but we don't need anything else just yet. */
348e048f 4896
43988095
JK
4897 ptr = read_and_check_comp_unit_head (&header, section,
4898 abbrev_section, ptr, section_kind);
348e048f 4899
78d4d2c5 4900 length = get_cu_length (&header);
6caca83c 4901
78d4d2c5
JK
4902 /* Skip dummy type units. */
4903 if (ptr >= info_ptr + length
43988095
JK
4904 || peek_abbrev_code (abfd, ptr) == 0
4905 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4906 {
4907 info_ptr += length;
4908 continue;
4909 }
dee91e82 4910
78d4d2c5
JK
4911 if (types_htab == NULL)
4912 {
4913 if (dwo_file)
4914 types_htab = allocate_dwo_unit_table (objfile);
4915 else
4916 types_htab = allocate_signatured_type_table (objfile);
4917 }
8b70b953 4918
78d4d2c5
JK
4919 if (dwo_file)
4920 {
4921 sig_type = NULL;
4922 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4923 struct dwo_unit);
4924 dwo_tu->dwo_file = dwo_file;
43988095 4925 dwo_tu->signature = header.signature;
9c541725 4926 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4927 dwo_tu->section = section;
9c541725 4928 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4929 dwo_tu->length = length;
4930 }
4931 else
4932 {
4933 /* N.B.: type_offset is not usable if this type uses a DWO file.
4934 The real type_offset is in the DWO file. */
4935 dwo_tu = NULL;
4936 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4937 struct signatured_type);
43988095 4938 sig_type->signature = header.signature;
9c541725 4939 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4940 sig_type->per_cu.objfile = objfile;
4941 sig_type->per_cu.is_debug_types = 1;
4942 sig_type->per_cu.section = section;
9c541725 4943 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4944 sig_type->per_cu.length = length;
4945 }
4946
4947 slot = htab_find_slot (types_htab,
4948 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4949 INSERT);
4950 gdb_assert (slot != NULL);
4951 if (*slot != NULL)
4952 {
9c541725 4953 sect_offset dup_sect_off;
0349ea22 4954
3019eac3
DE
4955 if (dwo_file)
4956 {
78d4d2c5
JK
4957 const struct dwo_unit *dup_tu
4958 = (const struct dwo_unit *) *slot;
4959
9c541725 4960 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4961 }
4962 else
4963 {
78d4d2c5
JK
4964 const struct signatured_type *dup_tu
4965 = (const struct signatured_type *) *slot;
4966
9c541725 4967 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4968 }
8b70b953 4969
78d4d2c5
JK
4970 complaint (&symfile_complaints,
4971 _("debug type entry at offset 0x%x is duplicate to"
4972 " the entry at offset 0x%x, signature %s"),
9c541725 4973 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4974 hex_string (header.signature));
78d4d2c5
JK
4975 }
4976 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4977
78d4d2c5
JK
4978 if (dwarf_read_debug > 1)
4979 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4980 to_underlying (sect_off),
43988095 4981 hex_string (header.signature));
3019eac3 4982
78d4d2c5
JK
4983 info_ptr += length;
4984 }
4985}
3019eac3 4986
78d4d2c5
JK
4987/* Create the hash table of all entries in the .debug_types
4988 (or .debug_types.dwo) section(s).
4989 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4990 otherwise it is NULL.
b3c8eb43 4991
78d4d2c5 4992 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4993
78d4d2c5 4994 Note: This function processes DWO files only, not DWP files. */
348e048f 4995
78d4d2c5
JK
4996static void
4997create_debug_types_hash_table (struct dwo_file *dwo_file,
4998 VEC (dwarf2_section_info_def) *types,
4999 htab_t &types_htab)
5000{
5001 int ix;
5002 struct dwarf2_section_info *section;
5003
5004 if (VEC_empty (dwarf2_section_info_def, types))
5005 return;
348e048f 5006
78d4d2c5
JK
5007 for (ix = 0;
5008 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5009 ++ix)
43988095
JK
5010 create_debug_type_hash_table (dwo_file, section, types_htab,
5011 rcuh_kind::TYPE);
3019eac3
DE
5012}
5013
5014/* Create the hash table of all entries in the .debug_types section,
5015 and initialize all_type_units.
5016 The result is zero if there is an error (e.g. missing .debug_types section),
5017 otherwise non-zero. */
5018
5019static int
5020create_all_type_units (struct objfile *objfile)
5021{
78d4d2c5 5022 htab_t types_htab = NULL;
b4dd5633 5023 struct signatured_type **iter;
3019eac3 5024
43988095
JK
5025 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5026 rcuh_kind::COMPILE);
78d4d2c5 5027 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5028 if (types_htab == NULL)
5029 {
5030 dwarf2_per_objfile->signatured_types = NULL;
5031 return 0;
5032 }
5033
348e048f
DE
5034 dwarf2_per_objfile->signatured_types = types_htab;
5035
6aa5f3a6
DE
5036 dwarf2_per_objfile->n_type_units
5037 = dwarf2_per_objfile->n_allocated_type_units
5038 = htab_elements (types_htab);
8d749320
SM
5039 dwarf2_per_objfile->all_type_units =
5040 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5041 iter = &dwarf2_per_objfile->all_type_units[0];
5042 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5043 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5044 == dwarf2_per_objfile->n_type_units);
1fd400ff 5045
348e048f
DE
5046 return 1;
5047}
5048
6aa5f3a6
DE
5049/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5050 If SLOT is non-NULL, it is the entry to use in the hash table.
5051 Otherwise we find one. */
5052
5053static struct signatured_type *
5054add_type_unit (ULONGEST sig, void **slot)
5055{
5056 struct objfile *objfile = dwarf2_per_objfile->objfile;
5057 int n_type_units = dwarf2_per_objfile->n_type_units;
5058 struct signatured_type *sig_type;
5059
5060 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5061 ++n_type_units;
5062 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5063 {
5064 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5065 dwarf2_per_objfile->n_allocated_type_units = 1;
5066 dwarf2_per_objfile->n_allocated_type_units *= 2;
5067 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5068 = XRESIZEVEC (struct signatured_type *,
5069 dwarf2_per_objfile->all_type_units,
5070 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5071 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5072 }
5073 dwarf2_per_objfile->n_type_units = n_type_units;
5074
5075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5076 struct signatured_type);
5077 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5078 sig_type->signature = sig;
5079 sig_type->per_cu.is_debug_types = 1;
5080 if (dwarf2_per_objfile->using_index)
5081 {
5082 sig_type->per_cu.v.quick =
5083 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5084 struct dwarf2_per_cu_quick_data);
5085 }
5086
5087 if (slot == NULL)
5088 {
5089 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5090 sig_type, INSERT);
5091 }
5092 gdb_assert (*slot == NULL);
5093 *slot = sig_type;
5094 /* The rest of sig_type must be filled in by the caller. */
5095 return sig_type;
5096}
5097
a2ce51a0
DE
5098/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5099 Fill in SIG_ENTRY with DWO_ENTRY. */
5100
5101static void
5102fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5103 struct signatured_type *sig_entry,
5104 struct dwo_unit *dwo_entry)
5105{
7ee85ab1 5106 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5107 gdb_assert (! sig_entry->per_cu.queued);
5108 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5109 if (dwarf2_per_objfile->using_index)
5110 {
5111 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5112 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5113 }
5114 else
5115 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5116 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5117 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5118 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5119 gdb_assert (sig_entry->dwo_unit == NULL);
5120
5121 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5122 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5123 sig_entry->per_cu.length = dwo_entry->length;
5124 sig_entry->per_cu.reading_dwo_directly = 1;
5125 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5126 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5127 sig_entry->dwo_unit = dwo_entry;
5128}
5129
5130/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5131 If we haven't read the TU yet, create the signatured_type data structure
5132 for a TU to be read in directly from a DWO file, bypassing the stub.
5133 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5134 using .gdb_index, then when reading a CU we want to stay in the DWO file
5135 containing that CU. Otherwise we could end up reading several other DWO
5136 files (due to comdat folding) to process the transitive closure of all the
5137 mentioned TUs, and that can be slow. The current DWO file will have every
5138 type signature that it needs.
a2ce51a0
DE
5139 We only do this for .gdb_index because in the psymtab case we already have
5140 to read all the DWOs to build the type unit groups. */
5141
5142static struct signatured_type *
5143lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5144{
5145 struct objfile *objfile = dwarf2_per_objfile->objfile;
5146 struct dwo_file *dwo_file;
5147 struct dwo_unit find_dwo_entry, *dwo_entry;
5148 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5149 void **slot;
a2ce51a0
DE
5150
5151 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5152
6aa5f3a6
DE
5153 /* If TU skeletons have been removed then we may not have read in any
5154 TUs yet. */
5155 if (dwarf2_per_objfile->signatured_types == NULL)
5156 {
5157 dwarf2_per_objfile->signatured_types
5158 = allocate_signatured_type_table (objfile);
5159 }
a2ce51a0
DE
5160
5161 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5162 Use the global signatured_types array to do our own comdat-folding
5163 of types. If this is the first time we're reading this TU, and
5164 the TU has an entry in .gdb_index, replace the recorded data from
5165 .gdb_index with this TU. */
a2ce51a0 5166
a2ce51a0 5167 find_sig_entry.signature = sig;
6aa5f3a6
DE
5168 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5169 &find_sig_entry, INSERT);
9a3c8263 5170 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5171
5172 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5173 read. Don't reassign the global entry to point to this DWO if that's
5174 the case. Also note that if the TU is already being read, it may not
5175 have come from a DWO, the program may be a mix of Fission-compiled
5176 code and non-Fission-compiled code. */
5177
5178 /* Have we already tried to read this TU?
5179 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5180 needn't exist in the global table yet). */
5181 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5182 return sig_entry;
5183
6aa5f3a6
DE
5184 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5185 dwo_unit of the TU itself. */
5186 dwo_file = cu->dwo_unit->dwo_file;
5187
a2ce51a0
DE
5188 /* Ok, this is the first time we're reading this TU. */
5189 if (dwo_file->tus == NULL)
5190 return NULL;
5191 find_dwo_entry.signature = sig;
9a3c8263 5192 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5193 if (dwo_entry == NULL)
5194 return NULL;
5195
6aa5f3a6
DE
5196 /* If the global table doesn't have an entry for this TU, add one. */
5197 if (sig_entry == NULL)
5198 sig_entry = add_type_unit (sig, slot);
5199
a2ce51a0 5200 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5201 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5202 return sig_entry;
5203}
5204
a2ce51a0
DE
5205/* Subroutine of lookup_signatured_type.
5206 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5207 then try the DWP file. If the TU stub (skeleton) has been removed then
5208 it won't be in .gdb_index. */
a2ce51a0
DE
5209
5210static struct signatured_type *
5211lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5212{
5213 struct objfile *objfile = dwarf2_per_objfile->objfile;
5214 struct dwp_file *dwp_file = get_dwp_file ();
5215 struct dwo_unit *dwo_entry;
5216 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5217 void **slot;
a2ce51a0
DE
5218
5219 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5220 gdb_assert (dwp_file != NULL);
5221
6aa5f3a6
DE
5222 /* If TU skeletons have been removed then we may not have read in any
5223 TUs yet. */
5224 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5225 {
6aa5f3a6
DE
5226 dwarf2_per_objfile->signatured_types
5227 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5228 }
5229
6aa5f3a6
DE
5230 find_sig_entry.signature = sig;
5231 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5232 &find_sig_entry, INSERT);
9a3c8263 5233 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5234
5235 /* Have we already tried to read this TU?
5236 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5237 needn't exist in the global table yet). */
5238 if (sig_entry != NULL)
5239 return sig_entry;
5240
a2ce51a0
DE
5241 if (dwp_file->tus == NULL)
5242 return NULL;
57d63ce2
DE
5243 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5244 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5245 if (dwo_entry == NULL)
5246 return NULL;
5247
6aa5f3a6 5248 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5249 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5250
a2ce51a0
DE
5251 return sig_entry;
5252}
5253
380bca97 5254/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5255 Returns NULL if signature SIG is not present in the table.
5256 It is up to the caller to complain about this. */
348e048f
DE
5257
5258static struct signatured_type *
a2ce51a0 5259lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5260{
a2ce51a0
DE
5261 if (cu->dwo_unit
5262 && dwarf2_per_objfile->using_index)
5263 {
5264 /* We're in a DWO/DWP file, and we're using .gdb_index.
5265 These cases require special processing. */
5266 if (get_dwp_file () == NULL)
5267 return lookup_dwo_signatured_type (cu, sig);
5268 else
5269 return lookup_dwp_signatured_type (cu, sig);
5270 }
5271 else
5272 {
5273 struct signatured_type find_entry, *entry;
348e048f 5274
a2ce51a0
DE
5275 if (dwarf2_per_objfile->signatured_types == NULL)
5276 return NULL;
5277 find_entry.signature = sig;
9a3c8263
SM
5278 entry = ((struct signatured_type *)
5279 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5280 return entry;
5281 }
348e048f 5282}
42e7ad6c
DE
5283\f
5284/* Low level DIE reading support. */
348e048f 5285
d85a05f0
DJ
5286/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5287
5288static void
5289init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5290 struct dwarf2_cu *cu,
3019eac3
DE
5291 struct dwarf2_section_info *section,
5292 struct dwo_file *dwo_file)
d85a05f0 5293{
fceca515 5294 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5295 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5296 reader->cu = cu;
3019eac3 5297 reader->dwo_file = dwo_file;
dee91e82
DE
5298 reader->die_section = section;
5299 reader->buffer = section->buffer;
f664829e 5300 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5301 reader->comp_dir = NULL;
d85a05f0
DJ
5302}
5303
b0c7bfa9
DE
5304/* Subroutine of init_cutu_and_read_dies to simplify it.
5305 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5306 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5307 already.
5308
5309 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5310 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5311 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5312 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5313 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5314 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5315 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5316 are filled in with the info of the DIE from the DWO file.
5317 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5318 provided an abbrev table to use.
5319 The result is non-zero if a valid (non-dummy) DIE was found. */
5320
5321static int
5322read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5323 struct dwo_unit *dwo_unit,
5324 int abbrev_table_provided,
5325 struct die_info *stub_comp_unit_die,
a2ce51a0 5326 const char *stub_comp_dir,
b0c7bfa9 5327 struct die_reader_specs *result_reader,
d521ce57 5328 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5329 struct die_info **result_comp_unit_die,
5330 int *result_has_children)
5331{
5332 struct objfile *objfile = dwarf2_per_objfile->objfile;
5333 struct dwarf2_cu *cu = this_cu->cu;
5334 struct dwarf2_section_info *section;
5335 bfd *abfd;
d521ce57 5336 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5337 ULONGEST signature; /* Or dwo_id. */
5338 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5339 int i,num_extra_attrs;
5340 struct dwarf2_section_info *dwo_abbrev_section;
5341 struct attribute *attr;
5342 struct die_info *comp_unit_die;
5343
b0aeadb3
DE
5344 /* At most one of these may be provided. */
5345 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5346
b0c7bfa9
DE
5347 /* These attributes aren't processed until later:
5348 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5349 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5350 referenced later. However, these attributes are found in the stub
5351 which we won't have later. In order to not impose this complication
5352 on the rest of the code, we read them here and copy them to the
5353 DWO CU/TU die. */
b0c7bfa9
DE
5354
5355 stmt_list = NULL;
5356 low_pc = NULL;
5357 high_pc = NULL;
5358 ranges = NULL;
5359 comp_dir = NULL;
5360
5361 if (stub_comp_unit_die != NULL)
5362 {
5363 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5364 DWO file. */
5365 if (! this_cu->is_debug_types)
5366 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5367 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5368 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5369 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5370 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5371
5372 /* There should be a DW_AT_addr_base attribute here (if needed).
5373 We need the value before we can process DW_FORM_GNU_addr_index. */
5374 cu->addr_base = 0;
5375 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5376 if (attr)
5377 cu->addr_base = DW_UNSND (attr);
5378
5379 /* There should be a DW_AT_ranges_base attribute here (if needed).
5380 We need the value before we can process DW_AT_ranges. */
5381 cu->ranges_base = 0;
5382 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5383 if (attr)
5384 cu->ranges_base = DW_UNSND (attr);
5385 }
a2ce51a0
DE
5386 else if (stub_comp_dir != NULL)
5387 {
5388 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5389 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5390 comp_dir->name = DW_AT_comp_dir;
5391 comp_dir->form = DW_FORM_string;
5392 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5393 DW_STRING (comp_dir) = stub_comp_dir;
5394 }
b0c7bfa9
DE
5395
5396 /* Set up for reading the DWO CU/TU. */
5397 cu->dwo_unit = dwo_unit;
5398 section = dwo_unit->section;
5399 dwarf2_read_section (objfile, section);
a32a8923 5400 abfd = get_section_bfd_owner (section);
9c541725
PA
5401 begin_info_ptr = info_ptr = (section->buffer
5402 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5403 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5404 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5405
5406 if (this_cu->is_debug_types)
5407 {
b0c7bfa9
DE
5408 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5409
43988095 5410 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5411 dwo_abbrev_section,
43988095 5412 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5413 /* This is not an assert because it can be caused by bad debug info. */
43988095 5414 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5415 {
5416 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5417 " TU at offset 0x%x [in module %s]"),
5418 hex_string (sig_type->signature),
43988095 5419 hex_string (cu->header.signature),
9c541725 5420 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5421 bfd_get_filename (abfd));
5422 }
9c541725 5423 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5424 /* For DWOs coming from DWP files, we don't know the CU length
5425 nor the type's offset in the TU until now. */
5426 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5427 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5428
5429 /* Establish the type offset that can be used to lookup the type.
5430 For DWO files, we don't know it until now. */
9c541725
PA
5431 sig_type->type_offset_in_section
5432 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5433 }
5434 else
5435 {
5436 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5437 dwo_abbrev_section,
43988095 5438 info_ptr, rcuh_kind::COMPILE);
9c541725 5439 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5440 /* For DWOs coming from DWP files, we don't know the CU length
5441 until now. */
5442 dwo_unit->length = get_cu_length (&cu->header);
5443 }
5444
02142a6c
DE
5445 /* Replace the CU's original abbrev table with the DWO's.
5446 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5447 if (abbrev_table_provided)
5448 {
5449 /* Don't free the provided abbrev table, the caller of
5450 init_cutu_and_read_dies owns it. */
5451 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5452 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5453 make_cleanup (dwarf2_free_abbrev_table, cu);
5454 }
5455 else
5456 {
5457 dwarf2_free_abbrev_table (cu);
5458 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5459 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5460 }
5461
5462 /* Read in the die, but leave space to copy over the attributes
5463 from the stub. This has the benefit of simplifying the rest of
5464 the code - all the work to maintain the illusion of a single
5465 DW_TAG_{compile,type}_unit DIE is done here. */
5466 num_extra_attrs = ((stmt_list != NULL)
5467 + (low_pc != NULL)
5468 + (high_pc != NULL)
5469 + (ranges != NULL)
5470 + (comp_dir != NULL));
5471 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5472 result_has_children, num_extra_attrs);
5473
5474 /* Copy over the attributes from the stub to the DIE we just read in. */
5475 comp_unit_die = *result_comp_unit_die;
5476 i = comp_unit_die->num_attrs;
5477 if (stmt_list != NULL)
5478 comp_unit_die->attrs[i++] = *stmt_list;
5479 if (low_pc != NULL)
5480 comp_unit_die->attrs[i++] = *low_pc;
5481 if (high_pc != NULL)
5482 comp_unit_die->attrs[i++] = *high_pc;
5483 if (ranges != NULL)
5484 comp_unit_die->attrs[i++] = *ranges;
5485 if (comp_dir != NULL)
5486 comp_unit_die->attrs[i++] = *comp_dir;
5487 comp_unit_die->num_attrs += num_extra_attrs;
5488
b4f54984 5489 if (dwarf_die_debug)
bf6af496
DE
5490 {
5491 fprintf_unfiltered (gdb_stdlog,
5492 "Read die from %s@0x%x of %s:\n",
a32a8923 5493 get_section_name (section),
bf6af496
DE
5494 (unsigned) (begin_info_ptr - section->buffer),
5495 bfd_get_filename (abfd));
b4f54984 5496 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5497 }
5498
a2ce51a0
DE
5499 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5500 TUs by skipping the stub and going directly to the entry in the DWO file.
5501 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5502 to get it via circuitous means. Blech. */
5503 if (comp_dir != NULL)
5504 result_reader->comp_dir = DW_STRING (comp_dir);
5505
b0c7bfa9
DE
5506 /* Skip dummy compilation units. */
5507 if (info_ptr >= begin_info_ptr + dwo_unit->length
5508 || peek_abbrev_code (abfd, info_ptr) == 0)
5509 return 0;
5510
5511 *result_info_ptr = info_ptr;
5512 return 1;
5513}
5514
5515/* Subroutine of init_cutu_and_read_dies to simplify it.
5516 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5517 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5518
5519static struct dwo_unit *
5520lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5521 struct die_info *comp_unit_die)
5522{
5523 struct dwarf2_cu *cu = this_cu->cu;
5524 struct attribute *attr;
5525 ULONGEST signature;
5526 struct dwo_unit *dwo_unit;
5527 const char *comp_dir, *dwo_name;
5528
a2ce51a0
DE
5529 gdb_assert (cu != NULL);
5530
b0c7bfa9 5531 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5532 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5533 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5534
5535 if (this_cu->is_debug_types)
5536 {
5537 struct signatured_type *sig_type;
5538
5539 /* Since this_cu is the first member of struct signatured_type,
5540 we can go from a pointer to one to a pointer to the other. */
5541 sig_type = (struct signatured_type *) this_cu;
5542 signature = sig_type->signature;
5543 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5544 }
5545 else
5546 {
5547 struct attribute *attr;
5548
5549 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5550 if (! attr)
5551 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5552 " [in module %s]"),
4262abfb 5553 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5554 signature = DW_UNSND (attr);
5555 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5556 signature);
5557 }
5558
b0c7bfa9
DE
5559 return dwo_unit;
5560}
5561
a2ce51a0 5562/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5563 See it for a description of the parameters.
5564 Read a TU directly from a DWO file, bypassing the stub.
5565
5566 Note: This function could be a little bit simpler if we shared cleanups
5567 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5568 to do, so we keep this function self-contained. Or we could move this
5569 into our caller, but it's complex enough already. */
a2ce51a0
DE
5570
5571static void
6aa5f3a6
DE
5572init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5573 int use_existing_cu, int keep,
a2ce51a0
DE
5574 die_reader_func_ftype *die_reader_func,
5575 void *data)
5576{
5577 struct dwarf2_cu *cu;
5578 struct signatured_type *sig_type;
6aa5f3a6 5579 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5580 struct die_reader_specs reader;
5581 const gdb_byte *info_ptr;
5582 struct die_info *comp_unit_die;
5583 int has_children;
5584
5585 /* Verify we can do the following downcast, and that we have the
5586 data we need. */
5587 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5588 sig_type = (struct signatured_type *) this_cu;
5589 gdb_assert (sig_type->dwo_unit != NULL);
5590
5591 cleanups = make_cleanup (null_cleanup, NULL);
5592
6aa5f3a6
DE
5593 if (use_existing_cu && this_cu->cu != NULL)
5594 {
5595 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5596 cu = this_cu->cu;
5597 /* There's no need to do the rereading_dwo_cu handling that
5598 init_cutu_and_read_dies does since we don't read the stub. */
5599 }
5600 else
5601 {
5602 /* If !use_existing_cu, this_cu->cu must be NULL. */
5603 gdb_assert (this_cu->cu == NULL);
8d749320 5604 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5605 init_one_comp_unit (cu, this_cu);
5606 /* If an error occurs while loading, release our storage. */
5607 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5608 }
5609
5610 /* A future optimization, if needed, would be to use an existing
5611 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5612 could share abbrev tables. */
a2ce51a0
DE
5613
5614 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5615 0 /* abbrev_table_provided */,
5616 NULL /* stub_comp_unit_die */,
5617 sig_type->dwo_unit->dwo_file->comp_dir,
5618 &reader, &info_ptr,
5619 &comp_unit_die, &has_children) == 0)
5620 {
5621 /* Dummy die. */
5622 do_cleanups (cleanups);
5623 return;
5624 }
5625
5626 /* All the "real" work is done here. */
5627 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5628
6aa5f3a6 5629 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5630 but the alternative is making the latter more complex.
5631 This function is only for the special case of using DWO files directly:
5632 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5633 if (free_cu_cleanup != NULL)
a2ce51a0 5634 {
6aa5f3a6
DE
5635 if (keep)
5636 {
5637 /* We've successfully allocated this compilation unit. Let our
5638 caller clean it up when finished with it. */
5639 discard_cleanups (free_cu_cleanup);
a2ce51a0 5640
6aa5f3a6
DE
5641 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5642 So we have to manually free the abbrev table. */
5643 dwarf2_free_abbrev_table (cu);
a2ce51a0 5644
6aa5f3a6
DE
5645 /* Link this CU into read_in_chain. */
5646 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5647 dwarf2_per_objfile->read_in_chain = this_cu;
5648 }
5649 else
5650 do_cleanups (free_cu_cleanup);
a2ce51a0 5651 }
a2ce51a0
DE
5652
5653 do_cleanups (cleanups);
5654}
5655
fd820528 5656/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5657 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5658
f4dc4d17
DE
5659 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5660 Otherwise the table specified in the comp unit header is read in and used.
5661 This is an optimization for when we already have the abbrev table.
5662
dee91e82
DE
5663 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5664 Otherwise, a new CU is allocated with xmalloc.
5665
5666 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5667 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5668
5669 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5670 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5671
70221824 5672static void
fd820528 5673init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5674 struct abbrev_table *abbrev_table,
fd820528
DE
5675 int use_existing_cu, int keep,
5676 die_reader_func_ftype *die_reader_func,
5677 void *data)
c906108c 5678{
dee91e82 5679 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5680 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5681 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5682 struct dwarf2_cu *cu;
d521ce57 5683 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5684 struct die_reader_specs reader;
d85a05f0 5685 struct die_info *comp_unit_die;
dee91e82 5686 int has_children;
d85a05f0 5687 struct attribute *attr;
365156ad 5688 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5689 struct signatured_type *sig_type = NULL;
4bdcc0c1 5690 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5691 /* Non-zero if CU currently points to a DWO file and we need to
5692 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5693 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5694 int rereading_dwo_cu = 0;
c906108c 5695
b4f54984 5696 if (dwarf_die_debug)
09406207
DE
5697 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5698 this_cu->is_debug_types ? "type" : "comp",
9c541725 5699 to_underlying (this_cu->sect_off));
09406207 5700
dee91e82
DE
5701 if (use_existing_cu)
5702 gdb_assert (keep);
23745b47 5703
a2ce51a0
DE
5704 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5705 file (instead of going through the stub), short-circuit all of this. */
5706 if (this_cu->reading_dwo_directly)
5707 {
5708 /* Narrow down the scope of possibilities to have to understand. */
5709 gdb_assert (this_cu->is_debug_types);
5710 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5711 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5712 die_reader_func, data);
a2ce51a0
DE
5713 return;
5714 }
5715
dee91e82
DE
5716 cleanups = make_cleanup (null_cleanup, NULL);
5717
5718 /* This is cheap if the section is already read in. */
5719 dwarf2_read_section (objfile, section);
5720
9c541725 5721 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5722
5723 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5724
5725 if (use_existing_cu && this_cu->cu != NULL)
5726 {
5727 cu = this_cu->cu;
42e7ad6c
DE
5728 /* If this CU is from a DWO file we need to start over, we need to
5729 refetch the attributes from the skeleton CU.
5730 This could be optimized by retrieving those attributes from when we
5731 were here the first time: the previous comp_unit_die was stored in
5732 comp_unit_obstack. But there's no data yet that we need this
5733 optimization. */
5734 if (cu->dwo_unit != NULL)
5735 rereading_dwo_cu = 1;
dee91e82
DE
5736 }
5737 else
5738 {
5739 /* If !use_existing_cu, this_cu->cu must be NULL. */
5740 gdb_assert (this_cu->cu == NULL);
8d749320 5741 cu = XNEW (struct dwarf2_cu);
dee91e82 5742 init_one_comp_unit (cu, this_cu);
dee91e82 5743 /* If an error occurs while loading, release our storage. */
365156ad 5744 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5745 }
dee91e82 5746
b0c7bfa9 5747 /* Get the header. */
9c541725 5748 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5749 {
5750 /* We already have the header, there's no need to read it in again. */
9c541725 5751 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5752 }
5753 else
5754 {
3019eac3 5755 if (this_cu->is_debug_types)
dee91e82 5756 {
43988095 5757 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5758 abbrev_section, info_ptr,
43988095 5759 rcuh_kind::TYPE);
dee91e82 5760
42e7ad6c
DE
5761 /* Since per_cu is the first member of struct signatured_type,
5762 we can go from a pointer to one to a pointer to the other. */
5763 sig_type = (struct signatured_type *) this_cu;
43988095 5764 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5765 gdb_assert (sig_type->type_offset_in_tu
5766 == cu->header.type_cu_offset_in_tu);
5767 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5768
42e7ad6c
DE
5769 /* LENGTH has not been set yet for type units if we're
5770 using .gdb_index. */
1ce1cefd 5771 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5772
5773 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5774 sig_type->type_offset_in_section =
5775 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5776
5777 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5778 }
5779 else
5780 {
4bdcc0c1
DE
5781 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5782 abbrev_section,
43988095
JK
5783 info_ptr,
5784 rcuh_kind::COMPILE);
dee91e82 5785
9c541725 5786 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5787 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5788 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5789 }
5790 }
10b3939b 5791
6caca83c 5792 /* Skip dummy compilation units. */
dee91e82 5793 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5794 || peek_abbrev_code (abfd, info_ptr) == 0)
5795 {
dee91e82 5796 do_cleanups (cleanups);
21b2bd31 5797 return;
6caca83c
CC
5798 }
5799
433df2d4
DE
5800 /* If we don't have them yet, read the abbrevs for this compilation unit.
5801 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5802 done. Note that it's important that if the CU had an abbrev table
5803 on entry we don't free it when we're done: Somewhere up the call stack
5804 it may be in use. */
f4dc4d17
DE
5805 if (abbrev_table != NULL)
5806 {
5807 gdb_assert (cu->abbrev_table == NULL);
9c541725 5808 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5809 cu->abbrev_table = abbrev_table;
5810 }
5811 else if (cu->abbrev_table == NULL)
dee91e82 5812 {
4bdcc0c1 5813 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5814 make_cleanup (dwarf2_free_abbrev_table, cu);
5815 }
42e7ad6c
DE
5816 else if (rereading_dwo_cu)
5817 {
5818 dwarf2_free_abbrev_table (cu);
5819 dwarf2_read_abbrevs (cu, abbrev_section);
5820 }
af703f96 5821
dee91e82 5822 /* Read the top level CU/TU die. */
3019eac3 5823 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5824 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5825
b0c7bfa9
DE
5826 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5827 from the DWO file.
5828 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5829 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5830 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5831 if (attr)
5832 {
3019eac3 5833 struct dwo_unit *dwo_unit;
b0c7bfa9 5834 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5835
5836 if (has_children)
6a506a2d
DE
5837 {
5838 complaint (&symfile_complaints,
5839 _("compilation unit with DW_AT_GNU_dwo_name"
5840 " has children (offset 0x%x) [in module %s]"),
9c541725 5841 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5842 }
b0c7bfa9 5843 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5844 if (dwo_unit != NULL)
3019eac3 5845 {
6a506a2d
DE
5846 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5847 abbrev_table != NULL,
a2ce51a0 5848 comp_unit_die, NULL,
6a506a2d
DE
5849 &reader, &info_ptr,
5850 &dwo_comp_unit_die, &has_children) == 0)
5851 {
5852 /* Dummy die. */
5853 do_cleanups (cleanups);
5854 return;
5855 }
5856 comp_unit_die = dwo_comp_unit_die;
5857 }
5858 else
5859 {
5860 /* Yikes, we couldn't find the rest of the DIE, we only have
5861 the stub. A complaint has already been logged. There's
5862 not much more we can do except pass on the stub DIE to
5863 die_reader_func. We don't want to throw an error on bad
5864 debug info. */
3019eac3
DE
5865 }
5866 }
5867
b0c7bfa9 5868 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5869 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5870
b0c7bfa9 5871 /* Done, clean up. */
365156ad 5872 if (free_cu_cleanup != NULL)
348e048f 5873 {
365156ad
TT
5874 if (keep)
5875 {
5876 /* We've successfully allocated this compilation unit. Let our
5877 caller clean it up when finished with it. */
5878 discard_cleanups (free_cu_cleanup);
dee91e82 5879
365156ad
TT
5880 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5881 So we have to manually free the abbrev table. */
5882 dwarf2_free_abbrev_table (cu);
dee91e82 5883
365156ad
TT
5884 /* Link this CU into read_in_chain. */
5885 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5886 dwarf2_per_objfile->read_in_chain = this_cu;
5887 }
5888 else
5889 do_cleanups (free_cu_cleanup);
348e048f 5890 }
365156ad
TT
5891
5892 do_cleanups (cleanups);
dee91e82
DE
5893}
5894
33e80786
DE
5895/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5896 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5897 to have already done the lookup to find the DWO file).
dee91e82
DE
5898
5899 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5900 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5901
5902 We fill in THIS_CU->length.
5903
5904 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5905 linker) then DIE_READER_FUNC will not get called.
5906
5907 THIS_CU->cu is always freed when done.
3019eac3
DE
5908 This is done in order to not leave THIS_CU->cu in a state where we have
5909 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5910
5911static void
5912init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5913 struct dwo_file *dwo_file,
dee91e82
DE
5914 die_reader_func_ftype *die_reader_func,
5915 void *data)
5916{
5917 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5918 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5919 bfd *abfd = get_section_bfd_owner (section);
33e80786 5920 struct dwarf2_section_info *abbrev_section;
dee91e82 5921 struct dwarf2_cu cu;
d521ce57 5922 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5923 struct die_reader_specs reader;
5924 struct cleanup *cleanups;
5925 struct die_info *comp_unit_die;
5926 int has_children;
5927
b4f54984 5928 if (dwarf_die_debug)
09406207
DE
5929 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5930 this_cu->is_debug_types ? "type" : "comp",
9c541725 5931 to_underlying (this_cu->sect_off));
09406207 5932
dee91e82
DE
5933 gdb_assert (this_cu->cu == NULL);
5934
33e80786
DE
5935 abbrev_section = (dwo_file != NULL
5936 ? &dwo_file->sections.abbrev
5937 : get_abbrev_section_for_cu (this_cu));
5938
dee91e82
DE
5939 /* This is cheap if the section is already read in. */
5940 dwarf2_read_section (objfile, section);
5941
5942 init_one_comp_unit (&cu, this_cu);
5943
5944 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5945
9c541725 5946 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5947 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5948 abbrev_section, info_ptr,
43988095
JK
5949 (this_cu->is_debug_types
5950 ? rcuh_kind::TYPE
5951 : rcuh_kind::COMPILE));
dee91e82 5952
1ce1cefd 5953 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5954
5955 /* Skip dummy compilation units. */
5956 if (info_ptr >= begin_info_ptr + this_cu->length
5957 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5958 {
dee91e82 5959 do_cleanups (cleanups);
21b2bd31 5960 return;
93311388 5961 }
72bf9492 5962
dee91e82
DE
5963 dwarf2_read_abbrevs (&cu, abbrev_section);
5964 make_cleanup (dwarf2_free_abbrev_table, &cu);
5965
3019eac3 5966 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5967 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5968
5969 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5970
5971 do_cleanups (cleanups);
5972}
5973
3019eac3
DE
5974/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5975 does not lookup the specified DWO file.
5976 This cannot be used to read DWO files.
dee91e82
DE
5977
5978 THIS_CU->cu is always freed when done.
3019eac3
DE
5979 This is done in order to not leave THIS_CU->cu in a state where we have
5980 to care whether it refers to the "main" CU or the DWO CU.
5981 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5982
5983static void
5984init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5985 die_reader_func_ftype *die_reader_func,
5986 void *data)
5987{
33e80786 5988 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5989}
0018ea6f
DE
5990\f
5991/* Type Unit Groups.
dee91e82 5992
0018ea6f
DE
5993 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5994 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5995 so that all types coming from the same compilation (.o file) are grouped
5996 together. A future step could be to put the types in the same symtab as
5997 the CU the types ultimately came from. */
ff013f42 5998
f4dc4d17
DE
5999static hashval_t
6000hash_type_unit_group (const void *item)
6001{
9a3c8263
SM
6002 const struct type_unit_group *tu_group
6003 = (const struct type_unit_group *) item;
f4dc4d17 6004
094b34ac 6005 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6006}
348e048f
DE
6007
6008static int
f4dc4d17 6009eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6010{
9a3c8263
SM
6011 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6012 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6013
094b34ac 6014 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6015}
348e048f 6016
f4dc4d17
DE
6017/* Allocate a hash table for type unit groups. */
6018
6019static htab_t
6020allocate_type_unit_groups_table (void)
6021{
6022 return htab_create_alloc_ex (3,
6023 hash_type_unit_group,
6024 eq_type_unit_group,
6025 NULL,
6026 &dwarf2_per_objfile->objfile->objfile_obstack,
6027 hashtab_obstack_allocate,
6028 dummy_obstack_deallocate);
6029}
dee91e82 6030
f4dc4d17
DE
6031/* Type units that don't have DW_AT_stmt_list are grouped into their own
6032 partial symtabs. We combine several TUs per psymtab to not let the size
6033 of any one psymtab grow too big. */
6034#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6035#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6036
094b34ac 6037/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6038 Create the type_unit_group object used to hold one or more TUs. */
6039
6040static struct type_unit_group *
094b34ac 6041create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6042{
6043 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6044 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6045 struct type_unit_group *tu_group;
f4dc4d17
DE
6046
6047 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6048 struct type_unit_group);
094b34ac 6049 per_cu = &tu_group->per_cu;
f4dc4d17 6050 per_cu->objfile = objfile;
f4dc4d17 6051
094b34ac
DE
6052 if (dwarf2_per_objfile->using_index)
6053 {
6054 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6055 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6056 }
6057 else
6058 {
9c541725 6059 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6060 struct partial_symtab *pst;
6061 char *name;
6062
6063 /* Give the symtab a useful name for debug purposes. */
6064 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6065 name = xstrprintf ("<type_units_%d>",
6066 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6067 else
6068 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6069
6070 pst = create_partial_symtab (per_cu, name);
6071 pst->anonymous = 1;
f4dc4d17 6072
094b34ac
DE
6073 xfree (name);
6074 }
f4dc4d17 6075
094b34ac 6076 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6077 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6078
6079 return tu_group;
6080}
6081
094b34ac
DE
6082/* Look up the type_unit_group for type unit CU, and create it if necessary.
6083 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6084
6085static struct type_unit_group *
ff39bb5e 6086get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6087{
6088 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6089 struct type_unit_group *tu_group;
6090 void **slot;
6091 unsigned int line_offset;
6092 struct type_unit_group type_unit_group_for_lookup;
6093
6094 if (dwarf2_per_objfile->type_unit_groups == NULL)
6095 {
6096 dwarf2_per_objfile->type_unit_groups =
6097 allocate_type_unit_groups_table ();
6098 }
6099
6100 /* Do we need to create a new group, or can we use an existing one? */
6101
6102 if (stmt_list)
6103 {
6104 line_offset = DW_UNSND (stmt_list);
6105 ++tu_stats->nr_symtab_sharers;
6106 }
6107 else
6108 {
6109 /* Ugh, no stmt_list. Rare, but we have to handle it.
6110 We can do various things here like create one group per TU or
6111 spread them over multiple groups to split up the expansion work.
6112 To avoid worst case scenarios (too many groups or too large groups)
6113 we, umm, group them in bunches. */
6114 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6115 | (tu_stats->nr_stmt_less_type_units
6116 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6117 ++tu_stats->nr_stmt_less_type_units;
6118 }
6119
094b34ac 6120 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6121 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6122 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6123 &type_unit_group_for_lookup, INSERT);
6124 if (*slot != NULL)
6125 {
9a3c8263 6126 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6127 gdb_assert (tu_group != NULL);
6128 }
6129 else
6130 {
9c541725 6131 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6132 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6133 *slot = tu_group;
6134 ++tu_stats->nr_symtabs;
6135 }
6136
6137 return tu_group;
6138}
0018ea6f
DE
6139\f
6140/* Partial symbol tables. */
6141
6142/* Create a psymtab named NAME and assign it to PER_CU.
6143
6144 The caller must fill in the following details:
6145 dirname, textlow, texthigh. */
6146
6147static struct partial_symtab *
6148create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6149{
6150 struct objfile *objfile = per_cu->objfile;
6151 struct partial_symtab *pst;
6152
18a94d75 6153 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6154 objfile->global_psymbols.next,
6155 objfile->static_psymbols.next);
6156
6157 pst->psymtabs_addrmap_supported = 1;
6158
6159 /* This is the glue that links PST into GDB's symbol API. */
6160 pst->read_symtab_private = per_cu;
6161 pst->read_symtab = dwarf2_read_symtab;
6162 per_cu->v.psymtab = pst;
6163
6164 return pst;
6165}
6166
b93601f3
TT
6167/* The DATA object passed to process_psymtab_comp_unit_reader has this
6168 type. */
6169
6170struct process_psymtab_comp_unit_data
6171{
6172 /* True if we are reading a DW_TAG_partial_unit. */
6173
6174 int want_partial_unit;
6175
6176 /* The "pretend" language that is used if the CU doesn't declare a
6177 language. */
6178
6179 enum language pretend_language;
6180};
6181
0018ea6f
DE
6182/* die_reader_func for process_psymtab_comp_unit. */
6183
6184static void
6185process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6186 const gdb_byte *info_ptr,
0018ea6f
DE
6187 struct die_info *comp_unit_die,
6188 int has_children,
6189 void *data)
6190{
6191 struct dwarf2_cu *cu = reader->cu;
6192 struct objfile *objfile = cu->objfile;
3e29f34a 6193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6194 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6195 CORE_ADDR baseaddr;
6196 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6197 struct partial_symtab *pst;
3a2b436a 6198 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6199 const char *filename;
9a3c8263
SM
6200 struct process_psymtab_comp_unit_data *info
6201 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6202
b93601f3 6203 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6204 return;
6205
6206 gdb_assert (! per_cu->is_debug_types);
6207
b93601f3 6208 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6209
6210 cu->list_in_scope = &file_symbols;
6211
6212 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6213 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6214 if (filename == NULL)
0018ea6f 6215 filename = "";
0018ea6f
DE
6216
6217 pst = create_partial_symtab (per_cu, filename);
6218
6219 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6220 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6221
6222 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6223
6224 dwarf2_find_base_address (comp_unit_die, cu);
6225
6226 /* Possibly set the default values of LOWPC and HIGHPC from
6227 `DW_AT_ranges'. */
3a2b436a
JK
6228 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6229 &best_highpc, cu, pst);
6230 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6231 /* Store the contiguous range if it is not empty; it can be empty for
6232 CUs with no code. */
6233 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6234 gdbarch_adjust_dwarf2_addr (gdbarch,
6235 best_lowpc + baseaddr),
6236 gdbarch_adjust_dwarf2_addr (gdbarch,
6237 best_highpc + baseaddr) - 1,
6238 pst);
0018ea6f
DE
6239
6240 /* Check if comp unit has_children.
6241 If so, read the rest of the partial symbols from this comp unit.
6242 If not, there's no more debug_info for this comp unit. */
6243 if (has_children)
6244 {
6245 struct partial_die_info *first_die;
6246 CORE_ADDR lowpc, highpc;
6247
6248 lowpc = ((CORE_ADDR) -1);
6249 highpc = ((CORE_ADDR) 0);
6250
6251 first_die = load_partial_dies (reader, info_ptr, 1);
6252
6253 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6254 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6255
6256 /* If we didn't find a lowpc, set it to highpc to avoid
6257 complaints from `maint check'. */
6258 if (lowpc == ((CORE_ADDR) -1))
6259 lowpc = highpc;
6260
6261 /* If the compilation unit didn't have an explicit address range,
6262 then use the information extracted from its child dies. */
e385593e 6263 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6264 {
6265 best_lowpc = lowpc;
6266 best_highpc = highpc;
6267 }
6268 }
3e29f34a
MR
6269 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6270 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6271
8763cede 6272 end_psymtab_common (objfile, pst);
0018ea6f
DE
6273
6274 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6275 {
6276 int i;
6277 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6278 struct dwarf2_per_cu_data *iter;
6279
6280 /* Fill in 'dependencies' here; we fill in 'users' in a
6281 post-pass. */
6282 pst->number_of_dependencies = len;
8d749320
SM
6283 pst->dependencies =
6284 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6285 for (i = 0;
6286 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6287 i, iter);
6288 ++i)
6289 pst->dependencies[i] = iter->v.psymtab;
6290
6291 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6292 }
6293
6294 /* Get the list of files included in the current compilation unit,
6295 and build a psymtab for each of them. */
6296 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6297
b4f54984 6298 if (dwarf_read_debug)
0018ea6f
DE
6299 {
6300 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6301
6302 fprintf_unfiltered (gdb_stdlog,
6303 "Psymtab for %s unit @0x%x: %s - %s"
6304 ", %d global, %d static syms\n",
6305 per_cu->is_debug_types ? "type" : "comp",
9c541725 6306 to_underlying (per_cu->sect_off),
0018ea6f
DE
6307 paddress (gdbarch, pst->textlow),
6308 paddress (gdbarch, pst->texthigh),
6309 pst->n_global_syms, pst->n_static_syms);
6310 }
6311}
6312
6313/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6314 Process compilation unit THIS_CU for a psymtab. */
6315
6316static void
6317process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6318 int want_partial_unit,
6319 enum language pretend_language)
0018ea6f
DE
6320{
6321 /* If this compilation unit was already read in, free the
6322 cached copy in order to read it in again. This is
6323 necessary because we skipped some symbols when we first
6324 read in the compilation unit (see load_partial_dies).
6325 This problem could be avoided, but the benefit is unclear. */
6326 if (this_cu->cu != NULL)
6327 free_one_cached_comp_unit (this_cu);
6328
f1902523
JK
6329 if (this_cu->is_debug_types)
6330 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6331 NULL);
6332 else
6333 {
6334 process_psymtab_comp_unit_data info;
6335 info.want_partial_unit = want_partial_unit;
6336 info.pretend_language = pretend_language;
6337 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6338 process_psymtab_comp_unit_reader, &info);
6339 }
0018ea6f
DE
6340
6341 /* Age out any secondary CUs. */
6342 age_cached_comp_units ();
6343}
f4dc4d17
DE
6344
6345/* Reader function for build_type_psymtabs. */
6346
6347static void
6348build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6349 const gdb_byte *info_ptr,
f4dc4d17
DE
6350 struct die_info *type_unit_die,
6351 int has_children,
6352 void *data)
6353{
6354 struct objfile *objfile = dwarf2_per_objfile->objfile;
6355 struct dwarf2_cu *cu = reader->cu;
6356 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6357 struct signatured_type *sig_type;
f4dc4d17
DE
6358 struct type_unit_group *tu_group;
6359 struct attribute *attr;
6360 struct partial_die_info *first_die;
6361 CORE_ADDR lowpc, highpc;
6362 struct partial_symtab *pst;
6363
6364 gdb_assert (data == NULL);
0186c6a7
DE
6365 gdb_assert (per_cu->is_debug_types);
6366 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6367
6368 if (! has_children)
6369 return;
6370
6371 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6372 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6373
0186c6a7 6374 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6375
6376 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6377 cu->list_in_scope = &file_symbols;
6378 pst = create_partial_symtab (per_cu, "");
6379 pst->anonymous = 1;
6380
6381 first_die = load_partial_dies (reader, info_ptr, 1);
6382
6383 lowpc = (CORE_ADDR) -1;
6384 highpc = (CORE_ADDR) 0;
6385 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6386
8763cede 6387 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6388}
6389
73051182
DE
6390/* Struct used to sort TUs by their abbreviation table offset. */
6391
6392struct tu_abbrev_offset
6393{
6394 struct signatured_type *sig_type;
6395 sect_offset abbrev_offset;
6396};
6397
6398/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6399
6400static int
6401sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6402{
9a3c8263
SM
6403 const struct tu_abbrev_offset * const *a
6404 = (const struct tu_abbrev_offset * const*) ap;
6405 const struct tu_abbrev_offset * const *b
6406 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6407 sect_offset aoff = (*a)->abbrev_offset;
6408 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6409
6410 return (aoff > boff) - (aoff < boff);
6411}
6412
6413/* Efficiently read all the type units.
6414 This does the bulk of the work for build_type_psymtabs.
6415
6416 The efficiency is because we sort TUs by the abbrev table they use and
6417 only read each abbrev table once. In one program there are 200K TUs
6418 sharing 8K abbrev tables.
6419
6420 The main purpose of this function is to support building the
6421 dwarf2_per_objfile->type_unit_groups table.
6422 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6423 can collapse the search space by grouping them by stmt_list.
6424 The savings can be significant, in the same program from above the 200K TUs
6425 share 8K stmt_list tables.
6426
6427 FUNC is expected to call get_type_unit_group, which will create the
6428 struct type_unit_group if necessary and add it to
6429 dwarf2_per_objfile->type_unit_groups. */
6430
6431static void
6432build_type_psymtabs_1 (void)
6433{
73051182
DE
6434 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6435 struct cleanup *cleanups;
6436 struct abbrev_table *abbrev_table;
6437 sect_offset abbrev_offset;
6438 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6439 int i;
6440
6441 /* It's up to the caller to not call us multiple times. */
6442 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6443
6444 if (dwarf2_per_objfile->n_type_units == 0)
6445 return;
6446
6447 /* TUs typically share abbrev tables, and there can be way more TUs than
6448 abbrev tables. Sort by abbrev table to reduce the number of times we
6449 read each abbrev table in.
6450 Alternatives are to punt or to maintain a cache of abbrev tables.
6451 This is simpler and efficient enough for now.
6452
6453 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6454 symtab to use). Typically TUs with the same abbrev offset have the same
6455 stmt_list value too so in practice this should work well.
6456
6457 The basic algorithm here is:
6458
6459 sort TUs by abbrev table
6460 for each TU with same abbrev table:
6461 read abbrev table if first user
6462 read TU top level DIE
6463 [IWBN if DWO skeletons had DW_AT_stmt_list]
6464 call FUNC */
6465
b4f54984 6466 if (dwarf_read_debug)
73051182
DE
6467 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6468
6469 /* Sort in a separate table to maintain the order of all_type_units
6470 for .gdb_index: TU indices directly index all_type_units. */
6471 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6472 dwarf2_per_objfile->n_type_units);
6473 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6474 {
6475 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6476
6477 sorted_by_abbrev[i].sig_type = sig_type;
6478 sorted_by_abbrev[i].abbrev_offset =
6479 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6480 sig_type->per_cu.sect_off);
73051182
DE
6481 }
6482 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6483 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6484 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6485
9c541725 6486 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6487 abbrev_table = NULL;
6488 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6489
6490 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6491 {
6492 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6493
6494 /* Switch to the next abbrev table if necessary. */
6495 if (abbrev_table == NULL
9c541725 6496 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6497 {
6498 if (abbrev_table != NULL)
6499 {
6500 abbrev_table_free (abbrev_table);
6501 /* Reset to NULL in case abbrev_table_read_table throws
6502 an error: abbrev_table_free_cleanup will get called. */
6503 abbrev_table = NULL;
6504 }
6505 abbrev_offset = tu->abbrev_offset;
6506 abbrev_table =
6507 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6508 abbrev_offset);
6509 ++tu_stats->nr_uniq_abbrev_tables;
6510 }
6511
6512 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6513 build_type_psymtabs_reader, NULL);
6514 }
6515
73051182 6516 do_cleanups (cleanups);
6aa5f3a6 6517}
73051182 6518
6aa5f3a6
DE
6519/* Print collected type unit statistics. */
6520
6521static void
6522print_tu_stats (void)
6523{
6524 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6525
6526 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6527 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6528 dwarf2_per_objfile->n_type_units);
6529 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6530 tu_stats->nr_uniq_abbrev_tables);
6531 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6532 tu_stats->nr_symtabs);
6533 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6534 tu_stats->nr_symtab_sharers);
6535 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6536 tu_stats->nr_stmt_less_type_units);
6537 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6538 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6539}
6540
f4dc4d17
DE
6541/* Traversal function for build_type_psymtabs. */
6542
6543static int
6544build_type_psymtab_dependencies (void **slot, void *info)
6545{
6546 struct objfile *objfile = dwarf2_per_objfile->objfile;
6547 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6548 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6549 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6550 int len = VEC_length (sig_type_ptr, tu_group->tus);
6551 struct signatured_type *iter;
f4dc4d17
DE
6552 int i;
6553
6554 gdb_assert (len > 0);
0186c6a7 6555 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6556
6557 pst->number_of_dependencies = len;
8d749320
SM
6558 pst->dependencies =
6559 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6560 for (i = 0;
0186c6a7 6561 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6562 ++i)
6563 {
0186c6a7
DE
6564 gdb_assert (iter->per_cu.is_debug_types);
6565 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6566 iter->type_unit_group = tu_group;
f4dc4d17
DE
6567 }
6568
0186c6a7 6569 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6570
6571 return 1;
6572}
6573
6574/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6575 Build partial symbol tables for the .debug_types comp-units. */
6576
6577static void
6578build_type_psymtabs (struct objfile *objfile)
6579{
0e50663e 6580 if (! create_all_type_units (objfile))
348e048f
DE
6581 return;
6582
73051182 6583 build_type_psymtabs_1 ();
6aa5f3a6 6584}
f4dc4d17 6585
6aa5f3a6
DE
6586/* Traversal function for process_skeletonless_type_unit.
6587 Read a TU in a DWO file and build partial symbols for it. */
6588
6589static int
6590process_skeletonless_type_unit (void **slot, void *info)
6591{
6592 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6593 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6594 struct signatured_type find_entry, *entry;
6595
6596 /* If this TU doesn't exist in the global table, add it and read it in. */
6597
6598 if (dwarf2_per_objfile->signatured_types == NULL)
6599 {
6600 dwarf2_per_objfile->signatured_types
6601 = allocate_signatured_type_table (objfile);
6602 }
6603
6604 find_entry.signature = dwo_unit->signature;
6605 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6606 INSERT);
6607 /* If we've already seen this type there's nothing to do. What's happening
6608 is we're doing our own version of comdat-folding here. */
6609 if (*slot != NULL)
6610 return 1;
6611
6612 /* This does the job that create_all_type_units would have done for
6613 this TU. */
6614 entry = add_type_unit (dwo_unit->signature, slot);
6615 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6616 *slot = entry;
6617
6618 /* This does the job that build_type_psymtabs_1 would have done. */
6619 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6620 build_type_psymtabs_reader, NULL);
6621
6622 return 1;
6623}
6624
6625/* Traversal function for process_skeletonless_type_units. */
6626
6627static int
6628process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6629{
6630 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6631
6632 if (dwo_file->tus != NULL)
6633 {
6634 htab_traverse_noresize (dwo_file->tus,
6635 process_skeletonless_type_unit, info);
6636 }
6637
6638 return 1;
6639}
6640
6641/* Scan all TUs of DWO files, verifying we've processed them.
6642 This is needed in case a TU was emitted without its skeleton.
6643 Note: This can't be done until we know what all the DWO files are. */
6644
6645static void
6646process_skeletonless_type_units (struct objfile *objfile)
6647{
6648 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6649 if (get_dwp_file () == NULL
6650 && dwarf2_per_objfile->dwo_files != NULL)
6651 {
6652 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6653 process_dwo_file_for_skeletonless_type_units,
6654 objfile);
6655 }
348e048f
DE
6656}
6657
60606b2c
TT
6658/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6659
6660static void
6661psymtabs_addrmap_cleanup (void *o)
6662{
9a3c8263 6663 struct objfile *objfile = (struct objfile *) o;
ec61707d 6664
60606b2c
TT
6665 objfile->psymtabs_addrmap = NULL;
6666}
6667
95554aad
TT
6668/* Compute the 'user' field for each psymtab in OBJFILE. */
6669
6670static void
6671set_partial_user (struct objfile *objfile)
6672{
6673 int i;
6674
6675 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6676 {
8832e7e3 6677 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6678 struct partial_symtab *pst = per_cu->v.psymtab;
6679 int j;
6680
36586728
TT
6681 if (pst == NULL)
6682 continue;
6683
95554aad
TT
6684 for (j = 0; j < pst->number_of_dependencies; ++j)
6685 {
6686 /* Set the 'user' field only if it is not already set. */
6687 if (pst->dependencies[j]->user == NULL)
6688 pst->dependencies[j]->user = pst;
6689 }
6690 }
6691}
6692
93311388
DE
6693/* Build the partial symbol table by doing a quick pass through the
6694 .debug_info and .debug_abbrev sections. */
72bf9492 6695
93311388 6696static void
c67a9c90 6697dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6698{
60606b2c 6699 struct cleanup *back_to, *addrmap_cleanup;
21b2bd31 6700 int i;
93311388 6701
b4f54984 6702 if (dwarf_read_debug)
45cfd468
DE
6703 {
6704 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6705 objfile_name (objfile));
45cfd468
DE
6706 }
6707
98bfdba5
PA
6708 dwarf2_per_objfile->reading_partial_symbols = 1;
6709
be391dca 6710 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6711
93311388
DE
6712 /* Any cached compilation units will be linked by the per-objfile
6713 read_in_chain. Make sure to free them when we're done. */
6714 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6715
348e048f
DE
6716 build_type_psymtabs (objfile);
6717
93311388 6718 create_all_comp_units (objfile);
c906108c 6719
60606b2c
TT
6720 /* Create a temporary address map on a temporary obstack. We later
6721 copy this to the final obstack. */
8268c778 6722 auto_obstack temp_obstack;
60606b2c
TT
6723 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6724 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6725
21b2bd31 6726 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6727 {
8832e7e3 6728 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6729
b93601f3 6730 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6731 }
ff013f42 6732
6aa5f3a6
DE
6733 /* This has to wait until we read the CUs, we need the list of DWOs. */
6734 process_skeletonless_type_units (objfile);
6735
6736 /* Now that all TUs have been processed we can fill in the dependencies. */
6737 if (dwarf2_per_objfile->type_unit_groups != NULL)
6738 {
6739 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6740 build_type_psymtab_dependencies, NULL);
6741 }
6742
b4f54984 6743 if (dwarf_read_debug)
6aa5f3a6
DE
6744 print_tu_stats ();
6745
95554aad
TT
6746 set_partial_user (objfile);
6747
ff013f42
JK
6748 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6749 &objfile->objfile_obstack);
60606b2c 6750 discard_cleanups (addrmap_cleanup);
ff013f42 6751
ae038cb0 6752 do_cleanups (back_to);
45cfd468 6753
b4f54984 6754 if (dwarf_read_debug)
45cfd468 6755 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6756 objfile_name (objfile));
ae038cb0
DJ
6757}
6758
3019eac3 6759/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6760
6761static void
dee91e82 6762load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6763 const gdb_byte *info_ptr,
dee91e82
DE
6764 struct die_info *comp_unit_die,
6765 int has_children,
6766 void *data)
ae038cb0 6767{
dee91e82 6768 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6769
95554aad 6770 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6771
ae038cb0
DJ
6772 /* Check if comp unit has_children.
6773 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6774 If not, there's no more debug_info for this comp unit. */
d85a05f0 6775 if (has_children)
dee91e82
DE
6776 load_partial_dies (reader, info_ptr, 0);
6777}
98bfdba5 6778
dee91e82
DE
6779/* Load the partial DIEs for a secondary CU into memory.
6780 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6781
dee91e82
DE
6782static void
6783load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6784{
f4dc4d17
DE
6785 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6786 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6787}
6788
ae038cb0 6789static void
36586728
TT
6790read_comp_units_from_section (struct objfile *objfile,
6791 struct dwarf2_section_info *section,
f1902523 6792 struct dwarf2_section_info *abbrev_section,
36586728
TT
6793 unsigned int is_dwz,
6794 int *n_allocated,
6795 int *n_comp_units,
6796 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6797{
d521ce57 6798 const gdb_byte *info_ptr;
a32a8923 6799 bfd *abfd = get_section_bfd_owner (section);
be391dca 6800
b4f54984 6801 if (dwarf_read_debug)
bf6af496 6802 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6803 get_section_name (section),
6804 get_section_file_name (section));
bf6af496 6805
36586728 6806 dwarf2_read_section (objfile, section);
ae038cb0 6807
36586728 6808 info_ptr = section->buffer;
6e70227d 6809
36586728 6810 while (info_ptr < section->buffer + section->size)
ae038cb0 6811 {
ae038cb0 6812 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6813
9c541725 6814 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6815
f1902523
JK
6816 comp_unit_head cu_header;
6817 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6818 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6819
6820 /* Save the compilation unit for later lookup. */
f1902523
JK
6821 if (cu_header.unit_type != DW_UT_type)
6822 {
6823 this_cu = XOBNEW (&objfile->objfile_obstack,
6824 struct dwarf2_per_cu_data);
6825 memset (this_cu, 0, sizeof (*this_cu));
6826 }
6827 else
6828 {
6829 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6830 struct signatured_type);
6831 memset (sig_type, 0, sizeof (*sig_type));
6832 sig_type->signature = cu_header.signature;
6833 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6834 this_cu = &sig_type->per_cu;
6835 }
6836 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6837 this_cu->sect_off = sect_off;
f1902523 6838 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6839 this_cu->is_dwz = is_dwz;
9291a0cd 6840 this_cu->objfile = objfile;
8a0459fd 6841 this_cu->section = section;
ae038cb0 6842
36586728 6843 if (*n_comp_units == *n_allocated)
ae038cb0 6844 {
36586728 6845 *n_allocated *= 2;
224c3ddb
SM
6846 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6847 *all_comp_units, *n_allocated);
ae038cb0 6848 }
36586728
TT
6849 (*all_comp_units)[*n_comp_units] = this_cu;
6850 ++*n_comp_units;
ae038cb0
DJ
6851
6852 info_ptr = info_ptr + this_cu->length;
6853 }
36586728
TT
6854}
6855
6856/* Create a list of all compilation units in OBJFILE.
6857 This is only done for -readnow and building partial symtabs. */
6858
6859static void
6860create_all_comp_units (struct objfile *objfile)
6861{
6862 int n_allocated;
6863 int n_comp_units;
6864 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6865 struct dwz_file *dwz;
36586728
TT
6866
6867 n_comp_units = 0;
6868 n_allocated = 10;
8d749320 6869 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6870
f1902523
JK
6871 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6872 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6873 &n_allocated, &n_comp_units, &all_comp_units);
6874
4db1a1dc
TT
6875 dwz = dwarf2_get_dwz_file ();
6876 if (dwz != NULL)
f1902523 6877 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6878 &n_allocated, &n_comp_units,
6879 &all_comp_units);
ae038cb0 6880
8d749320
SM
6881 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6882 struct dwarf2_per_cu_data *,
6883 n_comp_units);
ae038cb0
DJ
6884 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6885 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6886 xfree (all_comp_units);
6887 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6888}
6889
5734ee8b 6890/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6891 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6892 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6893 DW_AT_ranges). See the comments of add_partial_subprogram on how
6894 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6895
72bf9492
DJ
6896static void
6897scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6898 CORE_ADDR *highpc, int set_addrmap,
6899 struct dwarf2_cu *cu)
c906108c 6900{
72bf9492 6901 struct partial_die_info *pdi;
c906108c 6902
91c24f0a
DC
6903 /* Now, march along the PDI's, descending into ones which have
6904 interesting children but skipping the children of the other ones,
6905 until we reach the end of the compilation unit. */
c906108c 6906
72bf9492 6907 pdi = first_die;
91c24f0a 6908
72bf9492
DJ
6909 while (pdi != NULL)
6910 {
6911 fixup_partial_die (pdi, cu);
c906108c 6912
f55ee35c 6913 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6914 children, so we need to look at them. Ditto for anonymous
6915 enums. */
933c6fe4 6916
72bf9492 6917 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6918 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6919 || pdi->tag == DW_TAG_imported_unit)
c906108c 6920 {
72bf9492 6921 switch (pdi->tag)
c906108c
SS
6922 {
6923 case DW_TAG_subprogram:
cdc07690 6924 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6925 break;
72929c62 6926 case DW_TAG_constant:
c906108c
SS
6927 case DW_TAG_variable:
6928 case DW_TAG_typedef:
91c24f0a 6929 case DW_TAG_union_type:
72bf9492 6930 if (!pdi->is_declaration)
63d06c5c 6931 {
72bf9492 6932 add_partial_symbol (pdi, cu);
63d06c5c
DC
6933 }
6934 break;
c906108c 6935 case DW_TAG_class_type:
680b30c7 6936 case DW_TAG_interface_type:
c906108c 6937 case DW_TAG_structure_type:
72bf9492 6938 if (!pdi->is_declaration)
c906108c 6939 {
72bf9492 6940 add_partial_symbol (pdi, cu);
c906108c 6941 }
e98c9e7c
TT
6942 if (cu->language == language_rust && pdi->has_children)
6943 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6944 set_addrmap, cu);
c906108c 6945 break;
91c24f0a 6946 case DW_TAG_enumeration_type:
72bf9492
DJ
6947 if (!pdi->is_declaration)
6948 add_partial_enumeration (pdi, cu);
c906108c
SS
6949 break;
6950 case DW_TAG_base_type:
a02abb62 6951 case DW_TAG_subrange_type:
c906108c 6952 /* File scope base type definitions are added to the partial
c5aa993b 6953 symbol table. */
72bf9492 6954 add_partial_symbol (pdi, cu);
c906108c 6955 break;
d9fa45fe 6956 case DW_TAG_namespace:
cdc07690 6957 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6958 break;
5d7cb8df 6959 case DW_TAG_module:
cdc07690 6960 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6961 break;
95554aad
TT
6962 case DW_TAG_imported_unit:
6963 {
6964 struct dwarf2_per_cu_data *per_cu;
6965
f4dc4d17
DE
6966 /* For now we don't handle imported units in type units. */
6967 if (cu->per_cu->is_debug_types)
6968 {
6969 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6970 " supported in type units [in module %s]"),
4262abfb 6971 objfile_name (cu->objfile));
f4dc4d17
DE
6972 }
6973
9c541725 6974 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6975 pdi->is_dwz,
95554aad
TT
6976 cu->objfile);
6977
6978 /* Go read the partial unit, if needed. */
6979 if (per_cu->v.psymtab == NULL)
b93601f3 6980 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6981
f4dc4d17 6982 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6983 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6984 }
6985 break;
74921315
KS
6986 case DW_TAG_imported_declaration:
6987 add_partial_symbol (pdi, cu);
6988 break;
c906108c
SS
6989 default:
6990 break;
6991 }
6992 }
6993
72bf9492
DJ
6994 /* If the die has a sibling, skip to the sibling. */
6995
6996 pdi = pdi->die_sibling;
6997 }
6998}
6999
7000/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7001
72bf9492 7002 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7003 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7004 Enumerators are an exception; they use the scope of their parent
7005 enumeration type, i.e. the name of the enumeration type is not
7006 prepended to the enumerator.
91c24f0a 7007
72bf9492
DJ
7008 There are two complexities. One is DW_AT_specification; in this
7009 case "parent" means the parent of the target of the specification,
7010 instead of the direct parent of the DIE. The other is compilers
7011 which do not emit DW_TAG_namespace; in this case we try to guess
7012 the fully qualified name of structure types from their members'
7013 linkage names. This must be done using the DIE's children rather
7014 than the children of any DW_AT_specification target. We only need
7015 to do this for structures at the top level, i.e. if the target of
7016 any DW_AT_specification (if any; otherwise the DIE itself) does not
7017 have a parent. */
7018
7019/* Compute the scope prefix associated with PDI's parent, in
7020 compilation unit CU. The result will be allocated on CU's
7021 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7022 field. NULL is returned if no prefix is necessary. */
15d034d0 7023static const char *
72bf9492
DJ
7024partial_die_parent_scope (struct partial_die_info *pdi,
7025 struct dwarf2_cu *cu)
7026{
15d034d0 7027 const char *grandparent_scope;
72bf9492 7028 struct partial_die_info *parent, *real_pdi;
91c24f0a 7029
72bf9492
DJ
7030 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7031 then this means the parent of the specification DIE. */
7032
7033 real_pdi = pdi;
72bf9492 7034 while (real_pdi->has_specification)
36586728
TT
7035 real_pdi = find_partial_die (real_pdi->spec_offset,
7036 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7037
7038 parent = real_pdi->die_parent;
7039 if (parent == NULL)
7040 return NULL;
7041
7042 if (parent->scope_set)
7043 return parent->scope;
7044
7045 fixup_partial_die (parent, cu);
7046
10b3939b 7047 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7048
acebe513
UW
7049 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7050 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7051 Work around this problem here. */
7052 if (cu->language == language_cplus
6e70227d 7053 && parent->tag == DW_TAG_namespace
acebe513
UW
7054 && strcmp (parent->name, "::") == 0
7055 && grandparent_scope == NULL)
7056 {
7057 parent->scope = NULL;
7058 parent->scope_set = 1;
7059 return NULL;
7060 }
7061
9c6c53f7
SA
7062 if (pdi->tag == DW_TAG_enumerator)
7063 /* Enumerators should not get the name of the enumeration as a prefix. */
7064 parent->scope = grandparent_scope;
7065 else if (parent->tag == DW_TAG_namespace
f55ee35c 7066 || parent->tag == DW_TAG_module
72bf9492
DJ
7067 || parent->tag == DW_TAG_structure_type
7068 || parent->tag == DW_TAG_class_type
680b30c7 7069 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7070 || parent->tag == DW_TAG_union_type
7071 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7072 {
7073 if (grandparent_scope == NULL)
7074 parent->scope = parent->name;
7075 else
3e43a32a
MS
7076 parent->scope = typename_concat (&cu->comp_unit_obstack,
7077 grandparent_scope,
f55ee35c 7078 parent->name, 0, cu);
72bf9492 7079 }
72bf9492
DJ
7080 else
7081 {
7082 /* FIXME drow/2004-04-01: What should we be doing with
7083 function-local names? For partial symbols, we should probably be
7084 ignoring them. */
7085 complaint (&symfile_complaints,
e2e0b3e5 7086 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7087 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7088 parent->scope = grandparent_scope;
c906108c
SS
7089 }
7090
72bf9492
DJ
7091 parent->scope_set = 1;
7092 return parent->scope;
7093}
7094
7095/* Return the fully scoped name associated with PDI, from compilation unit
7096 CU. The result will be allocated with malloc. */
4568ecf9 7097
72bf9492
DJ
7098static char *
7099partial_die_full_name (struct partial_die_info *pdi,
7100 struct dwarf2_cu *cu)
7101{
15d034d0 7102 const char *parent_scope;
72bf9492 7103
98bfdba5
PA
7104 /* If this is a template instantiation, we can not work out the
7105 template arguments from partial DIEs. So, unfortunately, we have
7106 to go through the full DIEs. At least any work we do building
7107 types here will be reused if full symbols are loaded later. */
7108 if (pdi->has_template_arguments)
7109 {
7110 fixup_partial_die (pdi, cu);
7111
7112 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7113 {
7114 struct die_info *die;
7115 struct attribute attr;
7116 struct dwarf2_cu *ref_cu = cu;
7117
b64f50a1 7118 /* DW_FORM_ref_addr is using section offset. */
b4069958 7119 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7120 attr.form = DW_FORM_ref_addr;
9c541725 7121 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7122 die = follow_die_ref (NULL, &attr, &ref_cu);
7123
7124 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7125 }
7126 }
7127
72bf9492
DJ
7128 parent_scope = partial_die_parent_scope (pdi, cu);
7129 if (parent_scope == NULL)
7130 return NULL;
7131 else
f55ee35c 7132 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7133}
7134
7135static void
72bf9492 7136add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7137{
e7c27a73 7138 struct objfile *objfile = cu->objfile;
3e29f34a 7139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7140 CORE_ADDR addr = 0;
15d034d0 7141 const char *actual_name = NULL;
e142c38c 7142 CORE_ADDR baseaddr;
15d034d0 7143 char *built_actual_name;
e142c38c
DJ
7144
7145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7146
15d034d0
TT
7147 built_actual_name = partial_die_full_name (pdi, cu);
7148 if (built_actual_name != NULL)
7149 actual_name = built_actual_name;
63d06c5c 7150
72bf9492
DJ
7151 if (actual_name == NULL)
7152 actual_name = pdi->name;
7153
c906108c
SS
7154 switch (pdi->tag)
7155 {
7156 case DW_TAG_subprogram:
3e29f34a 7157 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7158 if (pdi->is_external || cu->language == language_ada)
c906108c 7159 {
2cfa0c8d
JB
7160 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7161 of the global scope. But in Ada, we want to be able to access
7162 nested procedures globally. So all Ada subprograms are stored
7163 in the global scope. */
f47fb265 7164 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7165 built_actual_name != NULL,
f47fb265
MS
7166 VAR_DOMAIN, LOC_BLOCK,
7167 &objfile->global_psymbols,
1762568f 7168 addr, cu->language, objfile);
c906108c
SS
7169 }
7170 else
7171 {
f47fb265 7172 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7173 built_actual_name != NULL,
f47fb265
MS
7174 VAR_DOMAIN, LOC_BLOCK,
7175 &objfile->static_psymbols,
1762568f 7176 addr, cu->language, objfile);
c906108c 7177 }
0c1b455e
TT
7178
7179 if (pdi->main_subprogram && actual_name != NULL)
7180 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7181 break;
72929c62
JB
7182 case DW_TAG_constant:
7183 {
7184 struct psymbol_allocation_list *list;
7185
7186 if (pdi->is_external)
7187 list = &objfile->global_psymbols;
7188 else
7189 list = &objfile->static_psymbols;
f47fb265 7190 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7191 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7192 list, 0, cu->language, objfile);
72929c62
JB
7193 }
7194 break;
c906108c 7195 case DW_TAG_variable:
95554aad
TT
7196 if (pdi->d.locdesc)
7197 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7198
95554aad 7199 if (pdi->d.locdesc
caac4577
JG
7200 && addr == 0
7201 && !dwarf2_per_objfile->has_section_at_zero)
7202 {
7203 /* A global or static variable may also have been stripped
7204 out by the linker if unused, in which case its address
7205 will be nullified; do not add such variables into partial
7206 symbol table then. */
7207 }
7208 else if (pdi->is_external)
c906108c
SS
7209 {
7210 /* Global Variable.
7211 Don't enter into the minimal symbol tables as there is
7212 a minimal symbol table entry from the ELF symbols already.
7213 Enter into partial symbol table if it has a location
7214 descriptor or a type.
7215 If the location descriptor is missing, new_symbol will create
7216 a LOC_UNRESOLVED symbol, the address of the variable will then
7217 be determined from the minimal symbol table whenever the variable
7218 is referenced.
7219 The address for the partial symbol table entry is not
7220 used by GDB, but it comes in handy for debugging partial symbol
7221 table building. */
7222
95554aad 7223 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7224 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7225 built_actual_name != NULL,
f47fb265
MS
7226 VAR_DOMAIN, LOC_STATIC,
7227 &objfile->global_psymbols,
1762568f 7228 addr + baseaddr,
f47fb265 7229 cu->language, objfile);
c906108c
SS
7230 }
7231 else
7232 {
ff908ebf
AW
7233 int has_loc = pdi->d.locdesc != NULL;
7234
7235 /* Static Variable. Skip symbols whose value we cannot know (those
7236 without location descriptors or constant values). */
7237 if (!has_loc && !pdi->has_const_value)
decbce07 7238 {
15d034d0 7239 xfree (built_actual_name);
decbce07
MS
7240 return;
7241 }
ff908ebf 7242
f47fb265 7243 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7244 built_actual_name != NULL,
f47fb265
MS
7245 VAR_DOMAIN, LOC_STATIC,
7246 &objfile->static_psymbols,
ff908ebf 7247 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7248 cu->language, objfile);
c906108c
SS
7249 }
7250 break;
7251 case DW_TAG_typedef:
7252 case DW_TAG_base_type:
a02abb62 7253 case DW_TAG_subrange_type:
38d518c9 7254 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7255 built_actual_name != NULL,
176620f1 7256 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7257 &objfile->static_psymbols,
1762568f 7258 0, cu->language, objfile);
c906108c 7259 break;
74921315 7260 case DW_TAG_imported_declaration:
72bf9492
DJ
7261 case DW_TAG_namespace:
7262 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7263 built_actual_name != NULL,
72bf9492
DJ
7264 VAR_DOMAIN, LOC_TYPEDEF,
7265 &objfile->global_psymbols,
1762568f 7266 0, cu->language, objfile);
72bf9492 7267 break;
530e8392
KB
7268 case DW_TAG_module:
7269 add_psymbol_to_list (actual_name, strlen (actual_name),
7270 built_actual_name != NULL,
7271 MODULE_DOMAIN, LOC_TYPEDEF,
7272 &objfile->global_psymbols,
1762568f 7273 0, cu->language, objfile);
530e8392 7274 break;
c906108c 7275 case DW_TAG_class_type:
680b30c7 7276 case DW_TAG_interface_type:
c906108c
SS
7277 case DW_TAG_structure_type:
7278 case DW_TAG_union_type:
7279 case DW_TAG_enumeration_type:
fa4028e9
JB
7280 /* Skip external references. The DWARF standard says in the section
7281 about "Structure, Union, and Class Type Entries": "An incomplete
7282 structure, union or class type is represented by a structure,
7283 union or class entry that does not have a byte size attribute
7284 and that has a DW_AT_declaration attribute." */
7285 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7286 {
15d034d0 7287 xfree (built_actual_name);
decbce07
MS
7288 return;
7289 }
fa4028e9 7290
63d06c5c
DC
7291 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7292 static vs. global. */
38d518c9 7293 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7294 built_actual_name != NULL,
176620f1 7295 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7296 cu->language == language_cplus
63d06c5c
DC
7297 ? &objfile->global_psymbols
7298 : &objfile->static_psymbols,
1762568f 7299 0, cu->language, objfile);
c906108c 7300
c906108c
SS
7301 break;
7302 case DW_TAG_enumerator:
38d518c9 7303 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7304 built_actual_name != NULL,
176620f1 7305 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7306 cu->language == language_cplus
f6fe98ef
DJ
7307 ? &objfile->global_psymbols
7308 : &objfile->static_psymbols,
1762568f 7309 0, cu->language, objfile);
c906108c
SS
7310 break;
7311 default:
7312 break;
7313 }
5c4e30ca 7314
15d034d0 7315 xfree (built_actual_name);
c906108c
SS
7316}
7317
5c4e30ca
DC
7318/* Read a partial die corresponding to a namespace; also, add a symbol
7319 corresponding to that namespace to the symbol table. NAMESPACE is
7320 the name of the enclosing namespace. */
91c24f0a 7321
72bf9492
DJ
7322static void
7323add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7324 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7325 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7326{
72bf9492 7327 /* Add a symbol for the namespace. */
e7c27a73 7328
72bf9492 7329 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7330
7331 /* Now scan partial symbols in that namespace. */
7332
91c24f0a 7333 if (pdi->has_children)
cdc07690 7334 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7335}
7336
5d7cb8df
JK
7337/* Read a partial die corresponding to a Fortran module. */
7338
7339static void
7340add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7341 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7342{
530e8392
KB
7343 /* Add a symbol for the namespace. */
7344
7345 add_partial_symbol (pdi, cu);
7346
f55ee35c 7347 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7348
7349 if (pdi->has_children)
cdc07690 7350 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7351}
7352
bc30ff58
JB
7353/* Read a partial die corresponding to a subprogram and create a partial
7354 symbol for that subprogram. When the CU language allows it, this
7355 routine also defines a partial symbol for each nested subprogram
cdc07690 7356 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7357 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7358 and highest PC values found in PDI.
6e70227d 7359
cdc07690
YQ
7360 PDI may also be a lexical block, in which case we simply search
7361 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7362 Again, this is only performed when the CU language allows this
7363 type of definitions. */
7364
7365static void
7366add_partial_subprogram (struct partial_die_info *pdi,
7367 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7368 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7369{
7370 if (pdi->tag == DW_TAG_subprogram)
7371 {
7372 if (pdi->has_pc_info)
7373 {
7374 if (pdi->lowpc < *lowpc)
7375 *lowpc = pdi->lowpc;
7376 if (pdi->highpc > *highpc)
7377 *highpc = pdi->highpc;
cdc07690 7378 if (set_addrmap)
5734ee8b 7379 {
5734ee8b 7380 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7381 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7382 CORE_ADDR baseaddr;
7383 CORE_ADDR highpc;
7384 CORE_ADDR lowpc;
5734ee8b
DJ
7385
7386 baseaddr = ANOFFSET (objfile->section_offsets,
7387 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7388 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7389 pdi->lowpc + baseaddr);
7390 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7391 pdi->highpc + baseaddr);
7392 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7393 cu->per_cu->v.psymtab);
5734ee8b 7394 }
481860b3
GB
7395 }
7396
7397 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7398 {
bc30ff58 7399 if (!pdi->is_declaration)
e8d05480
JB
7400 /* Ignore subprogram DIEs that do not have a name, they are
7401 illegal. Do not emit a complaint at this point, we will
7402 do so when we convert this psymtab into a symtab. */
7403 if (pdi->name)
7404 add_partial_symbol (pdi, cu);
bc30ff58
JB
7405 }
7406 }
6e70227d 7407
bc30ff58
JB
7408 if (! pdi->has_children)
7409 return;
7410
7411 if (cu->language == language_ada)
7412 {
7413 pdi = pdi->die_child;
7414 while (pdi != NULL)
7415 {
7416 fixup_partial_die (pdi, cu);
7417 if (pdi->tag == DW_TAG_subprogram
7418 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7419 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7420 pdi = pdi->die_sibling;
7421 }
7422 }
7423}
7424
91c24f0a
DC
7425/* Read a partial die corresponding to an enumeration type. */
7426
72bf9492
DJ
7427static void
7428add_partial_enumeration (struct partial_die_info *enum_pdi,
7429 struct dwarf2_cu *cu)
91c24f0a 7430{
72bf9492 7431 struct partial_die_info *pdi;
91c24f0a
DC
7432
7433 if (enum_pdi->name != NULL)
72bf9492
DJ
7434 add_partial_symbol (enum_pdi, cu);
7435
7436 pdi = enum_pdi->die_child;
7437 while (pdi)
91c24f0a 7438 {
72bf9492 7439 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7440 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7441 else
72bf9492
DJ
7442 add_partial_symbol (pdi, cu);
7443 pdi = pdi->die_sibling;
91c24f0a 7444 }
91c24f0a
DC
7445}
7446
6caca83c
CC
7447/* Return the initial uleb128 in the die at INFO_PTR. */
7448
7449static unsigned int
d521ce57 7450peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7451{
7452 unsigned int bytes_read;
7453
7454 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7455}
7456
4bb7a0a7
DJ
7457/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7458 Return the corresponding abbrev, or NULL if the number is zero (indicating
7459 an empty DIE). In either case *BYTES_READ will be set to the length of
7460 the initial number. */
7461
7462static struct abbrev_info *
d521ce57 7463peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7464 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7465{
7466 bfd *abfd = cu->objfile->obfd;
7467 unsigned int abbrev_number;
7468 struct abbrev_info *abbrev;
7469
7470 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7471
7472 if (abbrev_number == 0)
7473 return NULL;
7474
433df2d4 7475 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7476 if (!abbrev)
7477 {
422b9917
DE
7478 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7479 " at offset 0x%x [in module %s]"),
7480 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7481 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7482 }
7483
7484 return abbrev;
7485}
7486
93311388
DE
7487/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7488 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7489 DIE. Any children of the skipped DIEs will also be skipped. */
7490
d521ce57
TT
7491static const gdb_byte *
7492skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7493{
dee91e82 7494 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7495 struct abbrev_info *abbrev;
7496 unsigned int bytes_read;
7497
7498 while (1)
7499 {
7500 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7501 if (abbrev == NULL)
7502 return info_ptr + bytes_read;
7503 else
dee91e82 7504 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7505 }
7506}
7507
93311388
DE
7508/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7509 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7510 abbrev corresponding to that skipped uleb128 should be passed in
7511 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7512 children. */
7513
d521ce57
TT
7514static const gdb_byte *
7515skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7516 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7517{
7518 unsigned int bytes_read;
7519 struct attribute attr;
dee91e82
DE
7520 bfd *abfd = reader->abfd;
7521 struct dwarf2_cu *cu = reader->cu;
d521ce57 7522 const gdb_byte *buffer = reader->buffer;
f664829e 7523 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7524 unsigned int form, i;
7525
7526 for (i = 0; i < abbrev->num_attrs; i++)
7527 {
7528 /* The only abbrev we care about is DW_AT_sibling. */
7529 if (abbrev->attrs[i].name == DW_AT_sibling)
7530 {
dee91e82 7531 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7532 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7533 complaint (&symfile_complaints,
7534 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7535 else
b9502d3f 7536 {
9c541725
PA
7537 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7538 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7539
7540 if (sibling_ptr < info_ptr)
7541 complaint (&symfile_complaints,
7542 _("DW_AT_sibling points backwards"));
22869d73
KS
7543 else if (sibling_ptr > reader->buffer_end)
7544 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7545 else
7546 return sibling_ptr;
7547 }
4bb7a0a7
DJ
7548 }
7549
7550 /* If it isn't DW_AT_sibling, skip this attribute. */
7551 form = abbrev->attrs[i].form;
7552 skip_attribute:
7553 switch (form)
7554 {
4bb7a0a7 7555 case DW_FORM_ref_addr:
ae411497
TT
7556 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7557 and later it is offset sized. */
7558 if (cu->header.version == 2)
7559 info_ptr += cu->header.addr_size;
7560 else
7561 info_ptr += cu->header.offset_size;
7562 break;
36586728
TT
7563 case DW_FORM_GNU_ref_alt:
7564 info_ptr += cu->header.offset_size;
7565 break;
ae411497 7566 case DW_FORM_addr:
4bb7a0a7
DJ
7567 info_ptr += cu->header.addr_size;
7568 break;
7569 case DW_FORM_data1:
7570 case DW_FORM_ref1:
7571 case DW_FORM_flag:
7572 info_ptr += 1;
7573 break;
2dc7f7b3 7574 case DW_FORM_flag_present:
43988095 7575 case DW_FORM_implicit_const:
2dc7f7b3 7576 break;
4bb7a0a7
DJ
7577 case DW_FORM_data2:
7578 case DW_FORM_ref2:
7579 info_ptr += 2;
7580 break;
7581 case DW_FORM_data4:
7582 case DW_FORM_ref4:
7583 info_ptr += 4;
7584 break;
7585 case DW_FORM_data8:
7586 case DW_FORM_ref8:
55f1336d 7587 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7588 info_ptr += 8;
7589 break;
0224619f
JK
7590 case DW_FORM_data16:
7591 info_ptr += 16;
7592 break;
4bb7a0a7 7593 case DW_FORM_string:
9b1c24c8 7594 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7595 info_ptr += bytes_read;
7596 break;
2dc7f7b3 7597 case DW_FORM_sec_offset:
4bb7a0a7 7598 case DW_FORM_strp:
36586728 7599 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7600 info_ptr += cu->header.offset_size;
7601 break;
2dc7f7b3 7602 case DW_FORM_exprloc:
4bb7a0a7
DJ
7603 case DW_FORM_block:
7604 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7605 info_ptr += bytes_read;
7606 break;
7607 case DW_FORM_block1:
7608 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7609 break;
7610 case DW_FORM_block2:
7611 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7612 break;
7613 case DW_FORM_block4:
7614 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7615 break;
7616 case DW_FORM_sdata:
7617 case DW_FORM_udata:
7618 case DW_FORM_ref_udata:
3019eac3
DE
7619 case DW_FORM_GNU_addr_index:
7620 case DW_FORM_GNU_str_index:
d521ce57 7621 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7622 break;
7623 case DW_FORM_indirect:
7624 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7625 info_ptr += bytes_read;
7626 /* We need to continue parsing from here, so just go back to
7627 the top. */
7628 goto skip_attribute;
7629
7630 default:
3e43a32a
MS
7631 error (_("Dwarf Error: Cannot handle %s "
7632 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7633 dwarf_form_name (form),
7634 bfd_get_filename (abfd));
7635 }
7636 }
7637
7638 if (abbrev->has_children)
dee91e82 7639 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7640 else
7641 return info_ptr;
7642}
7643
93311388 7644/* Locate ORIG_PDI's sibling.
dee91e82 7645 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7646
d521ce57 7647static const gdb_byte *
dee91e82
DE
7648locate_pdi_sibling (const struct die_reader_specs *reader,
7649 struct partial_die_info *orig_pdi,
d521ce57 7650 const gdb_byte *info_ptr)
91c24f0a
DC
7651{
7652 /* Do we know the sibling already? */
72bf9492 7653
91c24f0a
DC
7654 if (orig_pdi->sibling)
7655 return orig_pdi->sibling;
7656
7657 /* Are there any children to deal with? */
7658
7659 if (!orig_pdi->has_children)
7660 return info_ptr;
7661
4bb7a0a7 7662 /* Skip the children the long way. */
91c24f0a 7663
dee91e82 7664 return skip_children (reader, info_ptr);
91c24f0a
DC
7665}
7666
257e7a09 7667/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7668 not NULL. */
c906108c
SS
7669
7670static void
257e7a09
YQ
7671dwarf2_read_symtab (struct partial_symtab *self,
7672 struct objfile *objfile)
c906108c 7673{
257e7a09 7674 if (self->readin)
c906108c 7675 {
442e4d9c 7676 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7677 self->filename);
442e4d9c
YQ
7678 }
7679 else
7680 {
7681 if (info_verbose)
c906108c 7682 {
442e4d9c 7683 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7684 self->filename);
442e4d9c 7685 gdb_flush (gdb_stdout);
c906108c 7686 }
c906108c 7687
442e4d9c 7688 /* Restore our global data. */
9a3c8263
SM
7689 dwarf2_per_objfile
7690 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7691 dwarf2_objfile_data_key);
10b3939b 7692
442e4d9c
YQ
7693 /* If this psymtab is constructed from a debug-only objfile, the
7694 has_section_at_zero flag will not necessarily be correct. We
7695 can get the correct value for this flag by looking at the data
7696 associated with the (presumably stripped) associated objfile. */
7697 if (objfile->separate_debug_objfile_backlink)
7698 {
7699 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7700 = ((struct dwarf2_per_objfile *)
7701 objfile_data (objfile->separate_debug_objfile_backlink,
7702 dwarf2_objfile_data_key));
9a619af0 7703
442e4d9c
YQ
7704 dwarf2_per_objfile->has_section_at_zero
7705 = dpo_backlink->has_section_at_zero;
7706 }
b2ab525c 7707
442e4d9c 7708 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7709
257e7a09 7710 psymtab_to_symtab_1 (self);
c906108c 7711
442e4d9c
YQ
7712 /* Finish up the debug error message. */
7713 if (info_verbose)
7714 printf_filtered (_("done.\n"));
c906108c 7715 }
95554aad
TT
7716
7717 process_cu_includes ();
c906108c 7718}
9cdd5dbd
DE
7719\f
7720/* Reading in full CUs. */
c906108c 7721
10b3939b
DJ
7722/* Add PER_CU to the queue. */
7723
7724static void
95554aad
TT
7725queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7726 enum language pretend_language)
10b3939b
DJ
7727{
7728 struct dwarf2_queue_item *item;
7729
7730 per_cu->queued = 1;
8d749320 7731 item = XNEW (struct dwarf2_queue_item);
10b3939b 7732 item->per_cu = per_cu;
95554aad 7733 item->pretend_language = pretend_language;
10b3939b
DJ
7734 item->next = NULL;
7735
7736 if (dwarf2_queue == NULL)
7737 dwarf2_queue = item;
7738 else
7739 dwarf2_queue_tail->next = item;
7740
7741 dwarf2_queue_tail = item;
7742}
7743
89e63ee4
DE
7744/* If PER_CU is not yet queued, add it to the queue.
7745 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7746 dependency.
0907af0c 7747 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7748 meaning either PER_CU is already queued or it is already loaded.
7749
7750 N.B. There is an invariant here that if a CU is queued then it is loaded.
7751 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7752
7753static int
89e63ee4 7754maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7755 struct dwarf2_per_cu_data *per_cu,
7756 enum language pretend_language)
7757{
7758 /* We may arrive here during partial symbol reading, if we need full
7759 DIEs to process an unusual case (e.g. template arguments). Do
7760 not queue PER_CU, just tell our caller to load its DIEs. */
7761 if (dwarf2_per_objfile->reading_partial_symbols)
7762 {
7763 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7764 return 1;
7765 return 0;
7766 }
7767
7768 /* Mark the dependence relation so that we don't flush PER_CU
7769 too early. */
89e63ee4
DE
7770 if (dependent_cu != NULL)
7771 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7772
7773 /* If it's already on the queue, we have nothing to do. */
7774 if (per_cu->queued)
7775 return 0;
7776
7777 /* If the compilation unit is already loaded, just mark it as
7778 used. */
7779 if (per_cu->cu != NULL)
7780 {
7781 per_cu->cu->last_used = 0;
7782 return 0;
7783 }
7784
7785 /* Add it to the queue. */
7786 queue_comp_unit (per_cu, pretend_language);
7787
7788 return 1;
7789}
7790
10b3939b
DJ
7791/* Process the queue. */
7792
7793static void
a0f42c21 7794process_queue (void)
10b3939b
DJ
7795{
7796 struct dwarf2_queue_item *item, *next_item;
7797
b4f54984 7798 if (dwarf_read_debug)
45cfd468
DE
7799 {
7800 fprintf_unfiltered (gdb_stdlog,
7801 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7802 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7803 }
7804
03dd20cc
DJ
7805 /* The queue starts out with one item, but following a DIE reference
7806 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7807 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7808 {
cc12ce38
DE
7809 if ((dwarf2_per_objfile->using_index
7810 ? !item->per_cu->v.quick->compunit_symtab
7811 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7812 /* Skip dummy CUs. */
7813 && item->per_cu->cu != NULL)
f4dc4d17
DE
7814 {
7815 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7816 unsigned int debug_print_threshold;
247f5c4f 7817 char buf[100];
f4dc4d17 7818
247f5c4f 7819 if (per_cu->is_debug_types)
f4dc4d17 7820 {
247f5c4f
DE
7821 struct signatured_type *sig_type =
7822 (struct signatured_type *) per_cu;
7823
7824 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7825 hex_string (sig_type->signature),
9c541725 7826 to_underlying (per_cu->sect_off));
73be47f5
DE
7827 /* There can be 100s of TUs.
7828 Only print them in verbose mode. */
7829 debug_print_threshold = 2;
f4dc4d17 7830 }
247f5c4f 7831 else
73be47f5 7832 {
9c541725
PA
7833 sprintf (buf, "CU at offset 0x%x",
7834 to_underlying (per_cu->sect_off));
73be47f5
DE
7835 debug_print_threshold = 1;
7836 }
247f5c4f 7837
b4f54984 7838 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7839 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7840
7841 if (per_cu->is_debug_types)
7842 process_full_type_unit (per_cu, item->pretend_language);
7843 else
7844 process_full_comp_unit (per_cu, item->pretend_language);
7845
b4f54984 7846 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7847 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7848 }
10b3939b
DJ
7849
7850 item->per_cu->queued = 0;
7851 next_item = item->next;
7852 xfree (item);
7853 }
7854
7855 dwarf2_queue_tail = NULL;
45cfd468 7856
b4f54984 7857 if (dwarf_read_debug)
45cfd468
DE
7858 {
7859 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7860 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7861 }
10b3939b
DJ
7862}
7863
7864/* Free all allocated queue entries. This function only releases anything if
7865 an error was thrown; if the queue was processed then it would have been
7866 freed as we went along. */
7867
7868static void
7869dwarf2_release_queue (void *dummy)
7870{
7871 struct dwarf2_queue_item *item, *last;
7872
7873 item = dwarf2_queue;
7874 while (item)
7875 {
7876 /* Anything still marked queued is likely to be in an
7877 inconsistent state, so discard it. */
7878 if (item->per_cu->queued)
7879 {
7880 if (item->per_cu->cu != NULL)
dee91e82 7881 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7882 item->per_cu->queued = 0;
7883 }
7884
7885 last = item;
7886 item = item->next;
7887 xfree (last);
7888 }
7889
7890 dwarf2_queue = dwarf2_queue_tail = NULL;
7891}
7892
7893/* Read in full symbols for PST, and anything it depends on. */
7894
c906108c 7895static void
fba45db2 7896psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7897{
10b3939b 7898 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7899 int i;
7900
95554aad
TT
7901 if (pst->readin)
7902 return;
7903
aaa75496 7904 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7905 if (!pst->dependencies[i]->readin
7906 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7907 {
7908 /* Inform about additional files that need to be read in. */
7909 if (info_verbose)
7910 {
a3f17187 7911 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7912 fputs_filtered (" ", gdb_stdout);
7913 wrap_here ("");
7914 fputs_filtered ("and ", gdb_stdout);
7915 wrap_here ("");
7916 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7917 wrap_here (""); /* Flush output. */
aaa75496
JB
7918 gdb_flush (gdb_stdout);
7919 }
7920 psymtab_to_symtab_1 (pst->dependencies[i]);
7921 }
7922
9a3c8263 7923 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7924
7925 if (per_cu == NULL)
aaa75496
JB
7926 {
7927 /* It's an include file, no symbols to read for it.
7928 Everything is in the parent symtab. */
7929 pst->readin = 1;
7930 return;
7931 }
c906108c 7932
a0f42c21 7933 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7934}
7935
dee91e82
DE
7936/* Trivial hash function for die_info: the hash value of a DIE
7937 is its offset in .debug_info for this objfile. */
10b3939b 7938
dee91e82
DE
7939static hashval_t
7940die_hash (const void *item)
10b3939b 7941{
9a3c8263 7942 const struct die_info *die = (const struct die_info *) item;
6502dd73 7943
9c541725 7944 return to_underlying (die->sect_off);
dee91e82 7945}
63d06c5c 7946
dee91e82
DE
7947/* Trivial comparison function for die_info structures: two DIEs
7948 are equal if they have the same offset. */
98bfdba5 7949
dee91e82
DE
7950static int
7951die_eq (const void *item_lhs, const void *item_rhs)
7952{
9a3c8263
SM
7953 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7954 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7955
9c541725 7956 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7957}
c906108c 7958
dee91e82
DE
7959/* die_reader_func for load_full_comp_unit.
7960 This is identical to read_signatured_type_reader,
7961 but is kept separate for now. */
c906108c 7962
dee91e82
DE
7963static void
7964load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7965 const gdb_byte *info_ptr,
dee91e82
DE
7966 struct die_info *comp_unit_die,
7967 int has_children,
7968 void *data)
7969{
7970 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7971 enum language *language_ptr = (enum language *) data;
6caca83c 7972
dee91e82
DE
7973 gdb_assert (cu->die_hash == NULL);
7974 cu->die_hash =
7975 htab_create_alloc_ex (cu->header.length / 12,
7976 die_hash,
7977 die_eq,
7978 NULL,
7979 &cu->comp_unit_obstack,
7980 hashtab_obstack_allocate,
7981 dummy_obstack_deallocate);
e142c38c 7982
dee91e82
DE
7983 if (has_children)
7984 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7985 &info_ptr, comp_unit_die);
7986 cu->dies = comp_unit_die;
7987 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7988
7989 /* We try not to read any attributes in this function, because not
9cdd5dbd 7990 all CUs needed for references have been loaded yet, and symbol
10b3939b 7991 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7992 or we won't be able to build types correctly.
7993 Similarly, if we do not read the producer, we can not apply
7994 producer-specific interpretation. */
95554aad 7995 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7996}
10b3939b 7997
dee91e82 7998/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7999
dee91e82 8000static void
95554aad
TT
8001load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8002 enum language pretend_language)
dee91e82 8003{
3019eac3 8004 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8005
f4dc4d17
DE
8006 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8007 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8008}
8009
3da10d80
KS
8010/* Add a DIE to the delayed physname list. */
8011
8012static void
8013add_to_method_list (struct type *type, int fnfield_index, int index,
8014 const char *name, struct die_info *die,
8015 struct dwarf2_cu *cu)
8016{
8017 struct delayed_method_info mi;
8018 mi.type = type;
8019 mi.fnfield_index = fnfield_index;
8020 mi.index = index;
8021 mi.name = name;
8022 mi.die = die;
8023 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8024}
8025
8026/* A cleanup for freeing the delayed method list. */
8027
8028static void
8029free_delayed_list (void *ptr)
8030{
8031 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8032 if (cu->method_list != NULL)
8033 {
8034 VEC_free (delayed_method_info, cu->method_list);
8035 cu->method_list = NULL;
8036 }
8037}
8038
3693fdb3
PA
8039/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8040 "const" / "volatile". If so, decrements LEN by the length of the
8041 modifier and return true. Otherwise return false. */
8042
8043template<size_t N>
8044static bool
8045check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8046{
8047 size_t mod_len = sizeof (mod) - 1;
8048 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8049 {
8050 len -= mod_len;
8051 return true;
8052 }
8053 return false;
8054}
8055
3da10d80
KS
8056/* Compute the physnames of any methods on the CU's method list.
8057
8058 The computation of method physnames is delayed in order to avoid the
8059 (bad) condition that one of the method's formal parameters is of an as yet
8060 incomplete type. */
8061
8062static void
8063compute_delayed_physnames (struct dwarf2_cu *cu)
8064{
8065 int i;
8066 struct delayed_method_info *mi;
3693fdb3
PA
8067
8068 /* Only C++ delays computing physnames. */
8069 if (VEC_empty (delayed_method_info, cu->method_list))
8070 return;
8071 gdb_assert (cu->language == language_cplus);
8072
3da10d80
KS
8073 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8074 {
1d06ead6 8075 const char *physname;
3da10d80
KS
8076 struct fn_fieldlist *fn_flp
8077 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8078 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8079 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8080 = physname ? physname : "";
3693fdb3
PA
8081
8082 /* Since there's no tag to indicate whether a method is a
8083 const/volatile overload, extract that information out of the
8084 demangled name. */
8085 if (physname != NULL)
8086 {
8087 size_t len = strlen (physname);
8088
8089 while (1)
8090 {
8091 if (physname[len] == ')') /* shortcut */
8092 break;
8093 else if (check_modifier (physname, len, " const"))
8094 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8095 else if (check_modifier (physname, len, " volatile"))
8096 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8097 else
8098 break;
8099 }
8100 }
3da10d80
KS
8101 }
8102}
8103
a766d390
DE
8104/* Go objects should be embedded in a DW_TAG_module DIE,
8105 and it's not clear if/how imported objects will appear.
8106 To keep Go support simple until that's worked out,
8107 go back through what we've read and create something usable.
8108 We could do this while processing each DIE, and feels kinda cleaner,
8109 but that way is more invasive.
8110 This is to, for example, allow the user to type "p var" or "b main"
8111 without having to specify the package name, and allow lookups
8112 of module.object to work in contexts that use the expression
8113 parser. */
8114
8115static void
8116fixup_go_packaging (struct dwarf2_cu *cu)
8117{
8118 char *package_name = NULL;
8119 struct pending *list;
8120 int i;
8121
8122 for (list = global_symbols; list != NULL; list = list->next)
8123 {
8124 for (i = 0; i < list->nsyms; ++i)
8125 {
8126 struct symbol *sym = list->symbol[i];
8127
8128 if (SYMBOL_LANGUAGE (sym) == language_go
8129 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8130 {
8131 char *this_package_name = go_symbol_package_name (sym);
8132
8133 if (this_package_name == NULL)
8134 continue;
8135 if (package_name == NULL)
8136 package_name = this_package_name;
8137 else
8138 {
8139 if (strcmp (package_name, this_package_name) != 0)
8140 complaint (&symfile_complaints,
8141 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8142 (symbol_symtab (sym) != NULL
8143 ? symtab_to_filename_for_display
8144 (symbol_symtab (sym))
4262abfb 8145 : objfile_name (cu->objfile)),
a766d390
DE
8146 this_package_name, package_name);
8147 xfree (this_package_name);
8148 }
8149 }
8150 }
8151 }
8152
8153 if (package_name != NULL)
8154 {
8155 struct objfile *objfile = cu->objfile;
34a68019 8156 const char *saved_package_name
224c3ddb
SM
8157 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8158 package_name,
8159 strlen (package_name));
19f392bc
UW
8160 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8161 saved_package_name);
a766d390
DE
8162 struct symbol *sym;
8163
8164 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8165
e623cf5d 8166 sym = allocate_symbol (objfile);
f85f34ed 8167 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8168 SYMBOL_SET_NAMES (sym, saved_package_name,
8169 strlen (saved_package_name), 0, objfile);
a766d390
DE
8170 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8171 e.g., "main" finds the "main" module and not C's main(). */
8172 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8173 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8174 SYMBOL_TYPE (sym) = type;
8175
8176 add_symbol_to_list (sym, &global_symbols);
8177
8178 xfree (package_name);
8179 }
8180}
8181
95554aad
TT
8182/* Return the symtab for PER_CU. This works properly regardless of
8183 whether we're using the index or psymtabs. */
8184
43f3e411
DE
8185static struct compunit_symtab *
8186get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8187{
8188 return (dwarf2_per_objfile->using_index
43f3e411
DE
8189 ? per_cu->v.quick->compunit_symtab
8190 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8191}
8192
8193/* A helper function for computing the list of all symbol tables
8194 included by PER_CU. */
8195
8196static void
43f3e411 8197recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8198 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8199 struct dwarf2_per_cu_data *per_cu,
43f3e411 8200 struct compunit_symtab *immediate_parent)
95554aad
TT
8201{
8202 void **slot;
8203 int ix;
43f3e411 8204 struct compunit_symtab *cust;
95554aad
TT
8205 struct dwarf2_per_cu_data *iter;
8206
8207 slot = htab_find_slot (all_children, per_cu, INSERT);
8208 if (*slot != NULL)
8209 {
8210 /* This inclusion and its children have been processed. */
8211 return;
8212 }
8213
8214 *slot = per_cu;
8215 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8216 cust = get_compunit_symtab (per_cu);
8217 if (cust != NULL)
ec94af83
DE
8218 {
8219 /* If this is a type unit only add its symbol table if we haven't
8220 seen it yet (type unit per_cu's can share symtabs). */
8221 if (per_cu->is_debug_types)
8222 {
43f3e411 8223 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8224 if (*slot == NULL)
8225 {
43f3e411
DE
8226 *slot = cust;
8227 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8228 if (cust->user == NULL)
8229 cust->user = immediate_parent;
ec94af83
DE
8230 }
8231 }
8232 else
f9125b6c 8233 {
43f3e411
DE
8234 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8235 if (cust->user == NULL)
8236 cust->user = immediate_parent;
f9125b6c 8237 }
ec94af83 8238 }
95554aad
TT
8239
8240 for (ix = 0;
796a7ff8 8241 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8242 ++ix)
ec94af83
DE
8243 {
8244 recursively_compute_inclusions (result, all_children,
43f3e411 8245 all_type_symtabs, iter, cust);
ec94af83 8246 }
95554aad
TT
8247}
8248
43f3e411 8249/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8250 PER_CU. */
8251
8252static void
43f3e411 8253compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8254{
f4dc4d17
DE
8255 gdb_assert (! per_cu->is_debug_types);
8256
796a7ff8 8257 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8258 {
8259 int ix, len;
ec94af83 8260 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8261 struct compunit_symtab *compunit_symtab_iter;
8262 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8263 htab_t all_children, all_type_symtabs;
43f3e411 8264 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8265
8266 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8267 if (cust == NULL)
95554aad
TT
8268 return;
8269
8270 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8271 NULL, xcalloc, xfree);
ec94af83
DE
8272 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8273 NULL, xcalloc, xfree);
95554aad
TT
8274
8275 for (ix = 0;
796a7ff8 8276 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8277 ix, per_cu_iter);
95554aad 8278 ++ix)
ec94af83
DE
8279 {
8280 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8281 all_type_symtabs, per_cu_iter,
43f3e411 8282 cust);
ec94af83 8283 }
95554aad 8284
ec94af83 8285 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8286 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8287 cust->includes
8d749320
SM
8288 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8289 struct compunit_symtab *, len + 1);
95554aad 8290 for (ix = 0;
43f3e411
DE
8291 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8292 compunit_symtab_iter);
95554aad 8293 ++ix)
43f3e411
DE
8294 cust->includes[ix] = compunit_symtab_iter;
8295 cust->includes[len] = NULL;
95554aad 8296
43f3e411 8297 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8298 htab_delete (all_children);
ec94af83 8299 htab_delete (all_type_symtabs);
95554aad
TT
8300 }
8301}
8302
8303/* Compute the 'includes' field for the symtabs of all the CUs we just
8304 read. */
8305
8306static void
8307process_cu_includes (void)
8308{
8309 int ix;
8310 struct dwarf2_per_cu_data *iter;
8311
8312 for (ix = 0;
8313 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8314 ix, iter);
8315 ++ix)
f4dc4d17
DE
8316 {
8317 if (! iter->is_debug_types)
43f3e411 8318 compute_compunit_symtab_includes (iter);
f4dc4d17 8319 }
95554aad
TT
8320
8321 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8322}
8323
9cdd5dbd 8324/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8325 already been loaded into memory. */
8326
8327static void
95554aad
TT
8328process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8329 enum language pretend_language)
10b3939b 8330{
10b3939b 8331 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8332 struct objfile *objfile = per_cu->objfile;
3e29f34a 8333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8334 CORE_ADDR lowpc, highpc;
43f3e411 8335 struct compunit_symtab *cust;
3da10d80 8336 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8337 CORE_ADDR baseaddr;
4359dff1 8338 struct block *static_block;
3e29f34a 8339 CORE_ADDR addr;
10b3939b
DJ
8340
8341 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8342
10b3939b
DJ
8343 buildsym_init ();
8344 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8345 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8346
8347 cu->list_in_scope = &file_symbols;
c906108c 8348
95554aad
TT
8349 cu->language = pretend_language;
8350 cu->language_defn = language_def (cu->language);
8351
c906108c 8352 /* Do line number decoding in read_file_scope () */
10b3939b 8353 process_die (cu->dies, cu);
c906108c 8354
a766d390
DE
8355 /* For now fudge the Go package. */
8356 if (cu->language == language_go)
8357 fixup_go_packaging (cu);
8358
3da10d80
KS
8359 /* Now that we have processed all the DIEs in the CU, all the types
8360 should be complete, and it should now be safe to compute all of the
8361 physnames. */
8362 compute_delayed_physnames (cu);
8363 do_cleanups (delayed_list_cleanup);
8364
fae299cd
DC
8365 /* Some compilers don't define a DW_AT_high_pc attribute for the
8366 compilation unit. If the DW_AT_high_pc is missing, synthesize
8367 it, by scanning the DIE's below the compilation unit. */
10b3939b 8368 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8369
3e29f34a
MR
8370 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8371 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8372
8373 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8374 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8375 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8376 addrmap to help ensure it has an accurate map of pc values belonging to
8377 this comp unit. */
8378 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8379
43f3e411
DE
8380 cust = end_symtab_from_static_block (static_block,
8381 SECT_OFF_TEXT (objfile), 0);
c906108c 8382
43f3e411 8383 if (cust != NULL)
c906108c 8384 {
df15bd07 8385 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8386
8be455d7
JK
8387 /* Set symtab language to language from DW_AT_language. If the
8388 compilation is from a C file generated by language preprocessors, do
8389 not set the language if it was already deduced by start_subfile. */
43f3e411 8390 if (!(cu->language == language_c
40e3ad0e 8391 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8392 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8393
8394 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8395 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8396 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8397 there were bugs in prologue debug info, fixed later in GCC-4.5
8398 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8399
8400 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8401 needed, it would be wrong due to missing DW_AT_producer there.
8402
8403 Still one can confuse GDB by using non-standard GCC compilation
8404 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8405 */
ab260dad 8406 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8407 cust->locations_valid = 1;
e0d00bc7
JK
8408
8409 if (gcc_4_minor >= 5)
43f3e411 8410 cust->epilogue_unwind_valid = 1;
96408a79 8411
43f3e411 8412 cust->call_site_htab = cu->call_site_htab;
c906108c 8413 }
9291a0cd
TT
8414
8415 if (dwarf2_per_objfile->using_index)
43f3e411 8416 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8417 else
8418 {
8419 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8420 pst->compunit_symtab = cust;
9291a0cd
TT
8421 pst->readin = 1;
8422 }
c906108c 8423
95554aad
TT
8424 /* Push it for inclusion processing later. */
8425 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8426
c906108c 8427 do_cleanups (back_to);
f4dc4d17 8428}
45cfd468 8429
f4dc4d17
DE
8430/* Generate full symbol information for type unit PER_CU, whose DIEs have
8431 already been loaded into memory. */
8432
8433static void
8434process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8435 enum language pretend_language)
8436{
8437 struct dwarf2_cu *cu = per_cu->cu;
8438 struct objfile *objfile = per_cu->objfile;
43f3e411 8439 struct compunit_symtab *cust;
f4dc4d17 8440 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8441 struct signatured_type *sig_type;
8442
8443 gdb_assert (per_cu->is_debug_types);
8444 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8445
8446 buildsym_init ();
8447 back_to = make_cleanup (really_free_pendings, NULL);
8448 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8449
8450 cu->list_in_scope = &file_symbols;
8451
8452 cu->language = pretend_language;
8453 cu->language_defn = language_def (cu->language);
8454
8455 /* The symbol tables are set up in read_type_unit_scope. */
8456 process_die (cu->dies, cu);
8457
8458 /* For now fudge the Go package. */
8459 if (cu->language == language_go)
8460 fixup_go_packaging (cu);
8461
8462 /* Now that we have processed all the DIEs in the CU, all the types
8463 should be complete, and it should now be safe to compute all of the
8464 physnames. */
8465 compute_delayed_physnames (cu);
8466 do_cleanups (delayed_list_cleanup);
8467
8468 /* TUs share symbol tables.
8469 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8470 of it with end_expandable_symtab. Otherwise, complete the addition of
8471 this TU's symbols to the existing symtab. */
43f3e411 8472 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8473 {
43f3e411
DE
8474 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8475 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8476
43f3e411 8477 if (cust != NULL)
f4dc4d17
DE
8478 {
8479 /* Set symtab language to language from DW_AT_language. If the
8480 compilation is from a C file generated by language preprocessors,
8481 do not set the language if it was already deduced by
8482 start_subfile. */
43f3e411
DE
8483 if (!(cu->language == language_c
8484 && COMPUNIT_FILETABS (cust)->language != language_c))
8485 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8486 }
8487 }
8488 else
8489 {
0ab9ce85 8490 augment_type_symtab ();
43f3e411 8491 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8492 }
8493
8494 if (dwarf2_per_objfile->using_index)
43f3e411 8495 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8496 else
8497 {
8498 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8499 pst->compunit_symtab = cust;
f4dc4d17 8500 pst->readin = 1;
45cfd468 8501 }
f4dc4d17
DE
8502
8503 do_cleanups (back_to);
c906108c
SS
8504}
8505
95554aad
TT
8506/* Process an imported unit DIE. */
8507
8508static void
8509process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8510{
8511 struct attribute *attr;
8512
f4dc4d17
DE
8513 /* For now we don't handle imported units in type units. */
8514 if (cu->per_cu->is_debug_types)
8515 {
8516 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8517 " supported in type units [in module %s]"),
4262abfb 8518 objfile_name (cu->objfile));
f4dc4d17
DE
8519 }
8520
95554aad
TT
8521 attr = dwarf2_attr (die, DW_AT_import, cu);
8522 if (attr != NULL)
8523 {
9c541725
PA
8524 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8525 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8526 dwarf2_per_cu_data *per_cu
8527 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8528
69d751e3 8529 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8530 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8531 load_full_comp_unit (per_cu, cu->language);
8532
796a7ff8 8533 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8534 per_cu);
8535 }
8536}
8537
4c8aa72d
PA
8538/* RAII object that represents a process_die scope: i.e.,
8539 starts/finishes processing a DIE. */
8540class process_die_scope
adde2bff 8541{
4c8aa72d
PA
8542public:
8543 process_die_scope (die_info *die, dwarf2_cu *cu)
8544 : m_die (die), m_cu (cu)
8545 {
8546 /* We should only be processing DIEs not already in process. */
8547 gdb_assert (!m_die->in_process);
8548 m_die->in_process = true;
8549 }
8c3cb9fa 8550
4c8aa72d
PA
8551 ~process_die_scope ()
8552 {
8553 m_die->in_process = false;
8554
8555 /* If we're done processing the DIE for the CU that owns the line
8556 header, we don't need the line header anymore. */
8557 if (m_cu->line_header_die_owner == m_die)
8558 {
8559 delete m_cu->line_header;
8560 m_cu->line_header = NULL;
8561 m_cu->line_header_die_owner = NULL;
8562 }
8563 }
8564
8565private:
8566 die_info *m_die;
8567 dwarf2_cu *m_cu;
8568};
adde2bff 8569
c906108c
SS
8570/* Process a die and its children. */
8571
8572static void
e7c27a73 8573process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8574{
4c8aa72d 8575 process_die_scope scope (die, cu);
adde2bff 8576
c906108c
SS
8577 switch (die->tag)
8578 {
8579 case DW_TAG_padding:
8580 break;
8581 case DW_TAG_compile_unit:
95554aad 8582 case DW_TAG_partial_unit:
e7c27a73 8583 read_file_scope (die, cu);
c906108c 8584 break;
348e048f
DE
8585 case DW_TAG_type_unit:
8586 read_type_unit_scope (die, cu);
8587 break;
c906108c 8588 case DW_TAG_subprogram:
c906108c 8589 case DW_TAG_inlined_subroutine:
edb3359d 8590 read_func_scope (die, cu);
c906108c
SS
8591 break;
8592 case DW_TAG_lexical_block:
14898363
L
8593 case DW_TAG_try_block:
8594 case DW_TAG_catch_block:
e7c27a73 8595 read_lexical_block_scope (die, cu);
c906108c 8596 break;
216f72a1 8597 case DW_TAG_call_site:
96408a79
SA
8598 case DW_TAG_GNU_call_site:
8599 read_call_site_scope (die, cu);
8600 break;
c906108c 8601 case DW_TAG_class_type:
680b30c7 8602 case DW_TAG_interface_type:
c906108c
SS
8603 case DW_TAG_structure_type:
8604 case DW_TAG_union_type:
134d01f1 8605 process_structure_scope (die, cu);
c906108c
SS
8606 break;
8607 case DW_TAG_enumeration_type:
134d01f1 8608 process_enumeration_scope (die, cu);
c906108c 8609 break;
134d01f1 8610
f792889a
DJ
8611 /* These dies have a type, but processing them does not create
8612 a symbol or recurse to process the children. Therefore we can
8613 read them on-demand through read_type_die. */
c906108c 8614 case DW_TAG_subroutine_type:
72019c9c 8615 case DW_TAG_set_type:
c906108c 8616 case DW_TAG_array_type:
c906108c 8617 case DW_TAG_pointer_type:
c906108c 8618 case DW_TAG_ptr_to_member_type:
c906108c 8619 case DW_TAG_reference_type:
4297a3f0 8620 case DW_TAG_rvalue_reference_type:
c906108c 8621 case DW_TAG_string_type:
c906108c 8622 break;
134d01f1 8623
c906108c 8624 case DW_TAG_base_type:
a02abb62 8625 case DW_TAG_subrange_type:
cb249c71 8626 case DW_TAG_typedef:
134d01f1
DJ
8627 /* Add a typedef symbol for the type definition, if it has a
8628 DW_AT_name. */
f792889a 8629 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8630 break;
c906108c 8631 case DW_TAG_common_block:
e7c27a73 8632 read_common_block (die, cu);
c906108c
SS
8633 break;
8634 case DW_TAG_common_inclusion:
8635 break;
d9fa45fe 8636 case DW_TAG_namespace:
4d4ec4e5 8637 cu->processing_has_namespace_info = 1;
e7c27a73 8638 read_namespace (die, cu);
d9fa45fe 8639 break;
5d7cb8df 8640 case DW_TAG_module:
4d4ec4e5 8641 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8642 read_module (die, cu);
8643 break;
d9fa45fe 8644 case DW_TAG_imported_declaration:
74921315
KS
8645 cu->processing_has_namespace_info = 1;
8646 if (read_namespace_alias (die, cu))
8647 break;
8648 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8649 case DW_TAG_imported_module:
4d4ec4e5 8650 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8651 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8652 || cu->language != language_fortran))
8653 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8654 dwarf_tag_name (die->tag));
8655 read_import_statement (die, cu);
d9fa45fe 8656 break;
95554aad
TT
8657
8658 case DW_TAG_imported_unit:
8659 process_imported_unit_die (die, cu);
8660 break;
8661
c906108c 8662 default:
e7c27a73 8663 new_symbol (die, NULL, cu);
c906108c
SS
8664 break;
8665 }
8666}
ca69b9e6
DE
8667\f
8668/* DWARF name computation. */
c906108c 8669
94af9270
KS
8670/* A helper function for dwarf2_compute_name which determines whether DIE
8671 needs to have the name of the scope prepended to the name listed in the
8672 die. */
8673
8674static int
8675die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8676{
1c809c68
TT
8677 struct attribute *attr;
8678
94af9270
KS
8679 switch (die->tag)
8680 {
8681 case DW_TAG_namespace:
8682 case DW_TAG_typedef:
8683 case DW_TAG_class_type:
8684 case DW_TAG_interface_type:
8685 case DW_TAG_structure_type:
8686 case DW_TAG_union_type:
8687 case DW_TAG_enumeration_type:
8688 case DW_TAG_enumerator:
8689 case DW_TAG_subprogram:
08a76f8a 8690 case DW_TAG_inlined_subroutine:
94af9270 8691 case DW_TAG_member:
74921315 8692 case DW_TAG_imported_declaration:
94af9270
KS
8693 return 1;
8694
8695 case DW_TAG_variable:
c2b0a229 8696 case DW_TAG_constant:
94af9270
KS
8697 /* We only need to prefix "globally" visible variables. These include
8698 any variable marked with DW_AT_external or any variable that
8699 lives in a namespace. [Variables in anonymous namespaces
8700 require prefixing, but they are not DW_AT_external.] */
8701
8702 if (dwarf2_attr (die, DW_AT_specification, cu))
8703 {
8704 struct dwarf2_cu *spec_cu = cu;
9a619af0 8705
94af9270
KS
8706 return die_needs_namespace (die_specification (die, &spec_cu),
8707 spec_cu);
8708 }
8709
1c809c68 8710 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8711 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8712 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8713 return 0;
8714 /* A variable in a lexical block of some kind does not need a
8715 namespace, even though in C++ such variables may be external
8716 and have a mangled name. */
8717 if (die->parent->tag == DW_TAG_lexical_block
8718 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8719 || die->parent->tag == DW_TAG_catch_block
8720 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8721 return 0;
8722 return 1;
94af9270
KS
8723
8724 default:
8725 return 0;
8726 }
8727}
8728
73b9be8b
KS
8729/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8730 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8731 defined for the given DIE. */
8732
8733static struct attribute *
8734dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8735{
8736 struct attribute *attr;
8737
8738 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8739 if (attr == NULL)
8740 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8741
8742 return attr;
8743}
8744
8745/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8746 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8747 defined for the given DIE. */
8748
8749static const char *
8750dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8751{
8752 const char *linkage_name;
8753
8754 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8755 if (linkage_name == NULL)
8756 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8757
8758 return linkage_name;
8759}
8760
94af9270 8761/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8762 compute the physname for the object, which include a method's:
9c37b5ae 8763 - formal parameters (C++),
a766d390 8764 - receiver type (Go),
a766d390
DE
8765
8766 The term "physname" is a bit confusing.
8767 For C++, for example, it is the demangled name.
8768 For Go, for example, it's the mangled name.
94af9270 8769
af6b7be1
JB
8770 For Ada, return the DIE's linkage name rather than the fully qualified
8771 name. PHYSNAME is ignored..
8772
94af9270
KS
8773 The result is allocated on the objfile_obstack and canonicalized. */
8774
8775static const char *
15d034d0
TT
8776dwarf2_compute_name (const char *name,
8777 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8778 int physname)
8779{
bb5ed363
DE
8780 struct objfile *objfile = cu->objfile;
8781
94af9270
KS
8782 if (name == NULL)
8783 name = dwarf2_name (die, cu);
8784
2ee7123e
DE
8785 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8786 but otherwise compute it by typename_concat inside GDB.
8787 FIXME: Actually this is not really true, or at least not always true.
8788 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8789 Fortran names because there is no mangling standard. So new_symbol_full
8790 will set the demangled name to the result of dwarf2_full_name, and it is
8791 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8792 if (cu->language == language_ada
8793 || (cu->language == language_fortran && physname))
8794 {
8795 /* For Ada unit, we prefer the linkage name over the name, as
8796 the former contains the exported name, which the user expects
8797 to be able to reference. Ideally, we want the user to be able
8798 to reference this entity using either natural or linkage name,
8799 but we haven't started looking at this enhancement yet. */
73b9be8b 8800 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 8801
2ee7123e
DE
8802 if (linkage_name != NULL)
8803 return linkage_name;
f55ee35c
JK
8804 }
8805
94af9270
KS
8806 /* These are the only languages we know how to qualify names in. */
8807 if (name != NULL
9c37b5ae 8808 && (cu->language == language_cplus
c44af4eb
TT
8809 || cu->language == language_fortran || cu->language == language_d
8810 || cu->language == language_rust))
94af9270
KS
8811 {
8812 if (die_needs_namespace (die, cu))
8813 {
8814 long length;
0d5cff50 8815 const char *prefix;
34a68019 8816 const char *canonical_name = NULL;
94af9270 8817
d7e74731
PA
8818 string_file buf;
8819
94af9270 8820 prefix = determine_prefix (die, cu);
94af9270
KS
8821 if (*prefix != '\0')
8822 {
f55ee35c
JK
8823 char *prefixed_name = typename_concat (NULL, prefix, name,
8824 physname, cu);
9a619af0 8825
d7e74731 8826 buf.puts (prefixed_name);
94af9270
KS
8827 xfree (prefixed_name);
8828 }
8829 else
d7e74731 8830 buf.puts (name);
94af9270 8831
98bfdba5
PA
8832 /* Template parameters may be specified in the DIE's DW_AT_name, or
8833 as children with DW_TAG_template_type_param or
8834 DW_TAG_value_type_param. If the latter, add them to the name
8835 here. If the name already has template parameters, then
8836 skip this step; some versions of GCC emit both, and
8837 it is more efficient to use the pre-computed name.
8838
8839 Something to keep in mind about this process: it is very
8840 unlikely, or in some cases downright impossible, to produce
8841 something that will match the mangled name of a function.
8842 If the definition of the function has the same debug info,
8843 we should be able to match up with it anyway. But fallbacks
8844 using the minimal symbol, for instance to find a method
8845 implemented in a stripped copy of libstdc++, will not work.
8846 If we do not have debug info for the definition, we will have to
8847 match them up some other way.
8848
8849 When we do name matching there is a related problem with function
8850 templates; two instantiated function templates are allowed to
8851 differ only by their return types, which we do not add here. */
8852
8853 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8854 {
8855 struct attribute *attr;
8856 struct die_info *child;
8857 int first = 1;
8858
8859 die->building_fullname = 1;
8860
8861 for (child = die->child; child != NULL; child = child->sibling)
8862 {
8863 struct type *type;
12df843f 8864 LONGEST value;
d521ce57 8865 const gdb_byte *bytes;
98bfdba5
PA
8866 struct dwarf2_locexpr_baton *baton;
8867 struct value *v;
8868
8869 if (child->tag != DW_TAG_template_type_param
8870 && child->tag != DW_TAG_template_value_param)
8871 continue;
8872
8873 if (first)
8874 {
d7e74731 8875 buf.puts ("<");
98bfdba5
PA
8876 first = 0;
8877 }
8878 else
d7e74731 8879 buf.puts (", ");
98bfdba5
PA
8880
8881 attr = dwarf2_attr (child, DW_AT_type, cu);
8882 if (attr == NULL)
8883 {
8884 complaint (&symfile_complaints,
8885 _("template parameter missing DW_AT_type"));
d7e74731 8886 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8887 continue;
8888 }
8889 type = die_type (child, cu);
8890
8891 if (child->tag == DW_TAG_template_type_param)
8892 {
d7e74731 8893 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8894 continue;
8895 }
8896
8897 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8898 if (attr == NULL)
8899 {
8900 complaint (&symfile_complaints,
3e43a32a
MS
8901 _("template parameter missing "
8902 "DW_AT_const_value"));
d7e74731 8903 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8904 continue;
8905 }
8906
8907 dwarf2_const_value_attr (attr, type, name,
8908 &cu->comp_unit_obstack, cu,
8909 &value, &bytes, &baton);
8910
8911 if (TYPE_NOSIGN (type))
8912 /* GDB prints characters as NUMBER 'CHAR'. If that's
8913 changed, this can use value_print instead. */
d7e74731 8914 c_printchar (value, type, &buf);
98bfdba5
PA
8915 else
8916 {
8917 struct value_print_options opts;
8918
8919 if (baton != NULL)
8920 v = dwarf2_evaluate_loc_desc (type, NULL,
8921 baton->data,
8922 baton->size,
8923 baton->per_cu);
8924 else if (bytes != NULL)
8925 {
8926 v = allocate_value (type);
8927 memcpy (value_contents_writeable (v), bytes,
8928 TYPE_LENGTH (type));
8929 }
8930 else
8931 v = value_from_longest (type, value);
8932
3e43a32a
MS
8933 /* Specify decimal so that we do not depend on
8934 the radix. */
98bfdba5
PA
8935 get_formatted_print_options (&opts, 'd');
8936 opts.raw = 1;
d7e74731 8937 value_print (v, &buf, &opts);
98bfdba5
PA
8938 release_value (v);
8939 value_free (v);
8940 }
8941 }
8942
8943 die->building_fullname = 0;
8944
8945 if (!first)
8946 {
8947 /* Close the argument list, with a space if necessary
8948 (nested templates). */
d7e74731
PA
8949 if (!buf.empty () && buf.string ().back () == '>')
8950 buf.puts (" >");
98bfdba5 8951 else
d7e74731 8952 buf.puts (">");
98bfdba5
PA
8953 }
8954 }
8955
9c37b5ae 8956 /* For C++ methods, append formal parameter type
94af9270 8957 information, if PHYSNAME. */
6e70227d 8958
94af9270 8959 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8960 && cu->language == language_cplus)
94af9270
KS
8961 {
8962 struct type *type = read_type_die (die, cu);
8963
d7e74731 8964 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8965 &type_print_raw_options);
94af9270 8966
9c37b5ae 8967 if (cu->language == language_cplus)
94af9270 8968 {
60430eff
DJ
8969 /* Assume that an artificial first parameter is
8970 "this", but do not crash if it is not. RealView
8971 marks unnamed (and thus unused) parameters as
8972 artificial; there is no way to differentiate
8973 the two cases. */
94af9270
KS
8974 if (TYPE_NFIELDS (type) > 0
8975 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8976 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8977 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8978 0))))
d7e74731 8979 buf.puts (" const");
94af9270
KS
8980 }
8981 }
8982
d7e74731 8983 const std::string &intermediate_name = buf.string ();
94af9270
KS
8984
8985 if (cu->language == language_cplus)
34a68019 8986 canonical_name
322a8516 8987 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8988 &objfile->per_bfd->storage_obstack);
8989
8990 /* If we only computed INTERMEDIATE_NAME, or if
8991 INTERMEDIATE_NAME is already canonical, then we need to
8992 copy it to the appropriate obstack. */
322a8516 8993 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8994 name = ((const char *)
8995 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8996 intermediate_name.c_str (),
8997 intermediate_name.length ()));
34a68019
TT
8998 else
8999 name = canonical_name;
94af9270
KS
9000 }
9001 }
9002
9003 return name;
9004}
9005
0114d602
DJ
9006/* Return the fully qualified name of DIE, based on its DW_AT_name.
9007 If scope qualifiers are appropriate they will be added. The result
34a68019 9008 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9009 not have a name. NAME may either be from a previous call to
9010 dwarf2_name or NULL.
9011
9c37b5ae 9012 The output string will be canonicalized (if C++). */
0114d602
DJ
9013
9014static const char *
15d034d0 9015dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9016{
94af9270
KS
9017 return dwarf2_compute_name (name, die, cu, 0);
9018}
0114d602 9019
94af9270
KS
9020/* Construct a physname for the given DIE in CU. NAME may either be
9021 from a previous call to dwarf2_name or NULL. The result will be
9022 allocated on the objfile_objstack or NULL if the DIE does not have a
9023 name.
0114d602 9024
9c37b5ae 9025 The output string will be canonicalized (if C++). */
0114d602 9026
94af9270 9027static const char *
15d034d0 9028dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9029{
bb5ed363 9030 struct objfile *objfile = cu->objfile;
900e11f9
JK
9031 const char *retval, *mangled = NULL, *canon = NULL;
9032 struct cleanup *back_to;
9033 int need_copy = 1;
9034
9035 /* In this case dwarf2_compute_name is just a shortcut not building anything
9036 on its own. */
9037 if (!die_needs_namespace (die, cu))
9038 return dwarf2_compute_name (name, die, cu, 1);
9039
9040 back_to = make_cleanup (null_cleanup, NULL);
9041
73b9be8b 9042 mangled = dw2_linkage_name (die, cu);
900e11f9 9043
e98c9e7c
TT
9044 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9045 See https://github.com/rust-lang/rust/issues/32925. */
9046 if (cu->language == language_rust && mangled != NULL
9047 && strchr (mangled, '{') != NULL)
9048 mangled = NULL;
9049
900e11f9
JK
9050 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9051 has computed. */
7d45c7c3 9052 if (mangled != NULL)
900e11f9
JK
9053 {
9054 char *demangled;
9055
900e11f9
JK
9056 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9057 type. It is easier for GDB users to search for such functions as
9058 `name(params)' than `long name(params)'. In such case the minimal
9059 symbol names do not match the full symbol names but for template
9060 functions there is never a need to look up their definition from their
9061 declaration so the only disadvantage remains the minimal symbol
9062 variant `long name(params)' does not have the proper inferior type.
9063 */
9064
a766d390
DE
9065 if (cu->language == language_go)
9066 {
9067 /* This is a lie, but we already lie to the caller new_symbol_full.
9068 new_symbol_full assumes we return the mangled name.
9069 This just undoes that lie until things are cleaned up. */
9070 demangled = NULL;
9071 }
9072 else
9073 {
8de20a37 9074 demangled = gdb_demangle (mangled,
9c37b5ae 9075 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 9076 }
900e11f9
JK
9077 if (demangled)
9078 {
9079 make_cleanup (xfree, demangled);
9080 canon = demangled;
9081 }
9082 else
9083 {
9084 canon = mangled;
9085 need_copy = 0;
9086 }
9087 }
9088
9089 if (canon == NULL || check_physname)
9090 {
9091 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9092
9093 if (canon != NULL && strcmp (physname, canon) != 0)
9094 {
9095 /* It may not mean a bug in GDB. The compiler could also
9096 compute DW_AT_linkage_name incorrectly. But in such case
9097 GDB would need to be bug-to-bug compatible. */
9098
9099 complaint (&symfile_complaints,
9100 _("Computed physname <%s> does not match demangled <%s> "
9101 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9102 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9103 objfile_name (objfile));
900e11f9
JK
9104
9105 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9106 is available here - over computed PHYSNAME. It is safer
9107 against both buggy GDB and buggy compilers. */
9108
9109 retval = canon;
9110 }
9111 else
9112 {
9113 retval = physname;
9114 need_copy = 0;
9115 }
9116 }
9117 else
9118 retval = canon;
9119
9120 if (need_copy)
224c3ddb
SM
9121 retval = ((const char *)
9122 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9123 retval, strlen (retval)));
900e11f9
JK
9124
9125 do_cleanups (back_to);
9126 return retval;
0114d602
DJ
9127}
9128
74921315
KS
9129/* Inspect DIE in CU for a namespace alias. If one exists, record
9130 a new symbol for it.
9131
9132 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9133
9134static int
9135read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9136{
9137 struct attribute *attr;
9138
9139 /* If the die does not have a name, this is not a namespace
9140 alias. */
9141 attr = dwarf2_attr (die, DW_AT_name, cu);
9142 if (attr != NULL)
9143 {
9144 int num;
9145 struct die_info *d = die;
9146 struct dwarf2_cu *imported_cu = cu;
9147
9148 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9149 keep inspecting DIEs until we hit the underlying import. */
9150#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9151 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9152 {
9153 attr = dwarf2_attr (d, DW_AT_import, cu);
9154 if (attr == NULL)
9155 break;
9156
9157 d = follow_die_ref (d, attr, &imported_cu);
9158 if (d->tag != DW_TAG_imported_declaration)
9159 break;
9160 }
9161
9162 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9163 {
9164 complaint (&symfile_complaints,
9165 _("DIE at 0x%x has too many recursively imported "
9c541725 9166 "declarations"), to_underlying (d->sect_off));
74921315
KS
9167 return 0;
9168 }
9169
9170 if (attr != NULL)
9171 {
9172 struct type *type;
9c541725 9173 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9174
9c541725 9175 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9176 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9177 {
9178 /* This declaration is a global namespace alias. Add
9179 a symbol for it whose type is the aliased namespace. */
9180 new_symbol (die, type, cu);
9181 return 1;
9182 }
9183 }
9184 }
9185
9186 return 0;
9187}
9188
22cee43f
PMR
9189/* Return the using directives repository (global or local?) to use in the
9190 current context for LANGUAGE.
9191
9192 For Ada, imported declarations can materialize renamings, which *may* be
9193 global. However it is impossible (for now?) in DWARF to distinguish
9194 "external" imported declarations and "static" ones. As all imported
9195 declarations seem to be static in all other languages, make them all CU-wide
9196 global only in Ada. */
9197
9198static struct using_direct **
9199using_directives (enum language language)
9200{
9201 if (language == language_ada && context_stack_depth == 0)
9202 return &global_using_directives;
9203 else
9204 return &local_using_directives;
9205}
9206
27aa8d6a
SW
9207/* Read the import statement specified by the given die and record it. */
9208
9209static void
9210read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9211{
bb5ed363 9212 struct objfile *objfile = cu->objfile;
27aa8d6a 9213 struct attribute *import_attr;
32019081 9214 struct die_info *imported_die, *child_die;
de4affc9 9215 struct dwarf2_cu *imported_cu;
27aa8d6a 9216 const char *imported_name;
794684b6 9217 const char *imported_name_prefix;
13387711
SW
9218 const char *canonical_name;
9219 const char *import_alias;
9220 const char *imported_declaration = NULL;
794684b6 9221 const char *import_prefix;
eb1e02fd 9222 std::vector<const char *> excludes;
13387711 9223
27aa8d6a
SW
9224 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9225 if (import_attr == NULL)
9226 {
9227 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9228 dwarf_tag_name (die->tag));
9229 return;
9230 }
9231
de4affc9
CC
9232 imported_cu = cu;
9233 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9234 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9235 if (imported_name == NULL)
9236 {
9237 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9238
9239 The import in the following code:
9240 namespace A
9241 {
9242 typedef int B;
9243 }
9244
9245 int main ()
9246 {
9247 using A::B;
9248 B b;
9249 return b;
9250 }
9251
9252 ...
9253 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9254 <52> DW_AT_decl_file : 1
9255 <53> DW_AT_decl_line : 6
9256 <54> DW_AT_import : <0x75>
9257 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9258 <59> DW_AT_name : B
9259 <5b> DW_AT_decl_file : 1
9260 <5c> DW_AT_decl_line : 2
9261 <5d> DW_AT_type : <0x6e>
9262 ...
9263 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9264 <76> DW_AT_byte_size : 4
9265 <77> DW_AT_encoding : 5 (signed)
9266
9267 imports the wrong die ( 0x75 instead of 0x58 ).
9268 This case will be ignored until the gcc bug is fixed. */
9269 return;
9270 }
9271
82856980
SW
9272 /* Figure out the local name after import. */
9273 import_alias = dwarf2_name (die, cu);
27aa8d6a 9274
794684b6
SW
9275 /* Figure out where the statement is being imported to. */
9276 import_prefix = determine_prefix (die, cu);
9277
9278 /* Figure out what the scope of the imported die is and prepend it
9279 to the name of the imported die. */
de4affc9 9280 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9281
f55ee35c
JK
9282 if (imported_die->tag != DW_TAG_namespace
9283 && imported_die->tag != DW_TAG_module)
794684b6 9284 {
13387711
SW
9285 imported_declaration = imported_name;
9286 canonical_name = imported_name_prefix;
794684b6 9287 }
13387711 9288 else if (strlen (imported_name_prefix) > 0)
12aaed36 9289 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9290 imported_name_prefix,
9291 (cu->language == language_d ? "." : "::"),
9292 imported_name, (char *) NULL);
13387711
SW
9293 else
9294 canonical_name = imported_name;
794684b6 9295
32019081
JK
9296 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9297 for (child_die = die->child; child_die && child_die->tag;
9298 child_die = sibling_die (child_die))
9299 {
9300 /* DWARF-4: A Fortran use statement with a “rename list” may be
9301 represented by an imported module entry with an import attribute
9302 referring to the module and owned entries corresponding to those
9303 entities that are renamed as part of being imported. */
9304
9305 if (child_die->tag != DW_TAG_imported_declaration)
9306 {
9307 complaint (&symfile_complaints,
9308 _("child DW_TAG_imported_declaration expected "
9309 "- DIE at 0x%x [in module %s]"),
9c541725 9310 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9311 continue;
9312 }
9313
9314 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9315 if (import_attr == NULL)
9316 {
9317 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9318 dwarf_tag_name (child_die->tag));
9319 continue;
9320 }
9321
9322 imported_cu = cu;
9323 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9324 &imported_cu);
9325 imported_name = dwarf2_name (imported_die, imported_cu);
9326 if (imported_name == NULL)
9327 {
9328 complaint (&symfile_complaints,
9329 _("child DW_TAG_imported_declaration has unknown "
9330 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9331 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9332 continue;
9333 }
9334
eb1e02fd 9335 excludes.push_back (imported_name);
32019081
JK
9336
9337 process_die (child_die, cu);
9338 }
9339
22cee43f
PMR
9340 add_using_directive (using_directives (cu->language),
9341 import_prefix,
9342 canonical_name,
9343 import_alias,
9344 imported_declaration,
9345 excludes,
9346 0,
9347 &objfile->objfile_obstack);
27aa8d6a
SW
9348}
9349
1b80a9fa
JK
9350/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9351 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9352 this, it was first present in GCC release 4.3.0. */
9353
9354static int
9355producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9356{
9357 if (!cu->checked_producer)
9358 check_producer (cu);
9359
9360 return cu->producer_is_gcc_lt_4_3;
9361}
9362
d721ba37
PA
9363static file_and_directory
9364find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9365{
d721ba37
PA
9366 file_and_directory res;
9367
9291a0cd
TT
9368 /* Find the filename. Do not use dwarf2_name here, since the filename
9369 is not a source language identifier. */
d721ba37
PA
9370 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9371 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9372
d721ba37
PA
9373 if (res.comp_dir == NULL
9374 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9375 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9376 {
d721ba37
PA
9377 res.comp_dir_storage = ldirname (res.name);
9378 if (!res.comp_dir_storage.empty ())
9379 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9380 }
d721ba37 9381 if (res.comp_dir != NULL)
9291a0cd
TT
9382 {
9383 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9384 directory, get rid of it. */
d721ba37 9385 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9386
d721ba37
PA
9387 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9388 res.comp_dir = cp + 1;
9291a0cd
TT
9389 }
9390
d721ba37
PA
9391 if (res.name == NULL)
9392 res.name = "<unknown>";
9393
9394 return res;
9291a0cd
TT
9395}
9396
f4dc4d17
DE
9397/* Handle DW_AT_stmt_list for a compilation unit.
9398 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9399 COMP_DIR is the compilation directory. LOWPC is passed to
9400 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9401
9402static void
9403handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9404 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9405{
527f3840 9406 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9407 struct attribute *attr;
527f3840
JK
9408 struct line_header line_header_local;
9409 hashval_t line_header_local_hash;
9410 unsigned u;
9411 void **slot;
9412 int decode_mapping;
2ab95328 9413
f4dc4d17
DE
9414 gdb_assert (! cu->per_cu->is_debug_types);
9415
2ab95328 9416 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9417 if (attr == NULL)
9418 return;
9419
9c541725 9420 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9421
9422 /* The line header hash table is only created if needed (it exists to
9423 prevent redundant reading of the line table for partial_units).
9424 If we're given a partial_unit, we'll need it. If we're given a
9425 compile_unit, then use the line header hash table if it's already
9426 created, but don't create one just yet. */
9427
9428 if (dwarf2_per_objfile->line_header_hash == NULL
9429 && die->tag == DW_TAG_partial_unit)
2ab95328 9430 {
527f3840
JK
9431 dwarf2_per_objfile->line_header_hash
9432 = htab_create_alloc_ex (127, line_header_hash_voidp,
9433 line_header_eq_voidp,
9434 free_line_header_voidp,
9435 &objfile->objfile_obstack,
9436 hashtab_obstack_allocate,
9437 dummy_obstack_deallocate);
9438 }
2ab95328 9439
9c541725 9440 line_header_local.sect_off = line_offset;
527f3840
JK
9441 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9442 line_header_local_hash = line_header_hash (&line_header_local);
9443 if (dwarf2_per_objfile->line_header_hash != NULL)
9444 {
9445 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9446 &line_header_local,
9447 line_header_local_hash, NO_INSERT);
9448
9449 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9450 is not present in *SLOT (since if there is something in *SLOT then
9451 it will be for a partial_unit). */
9452 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9453 {
527f3840 9454 gdb_assert (*slot != NULL);
9a3c8263 9455 cu->line_header = (struct line_header *) *slot;
527f3840 9456 return;
dee91e82 9457 }
2ab95328 9458 }
527f3840
JK
9459
9460 /* dwarf_decode_line_header does not yet provide sufficient information.
9461 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9462 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9463 if (lh == NULL)
527f3840 9464 return;
4c8aa72d
PA
9465
9466 cu->line_header = lh.release ();
9467 cu->line_header_die_owner = die;
527f3840
JK
9468
9469 if (dwarf2_per_objfile->line_header_hash == NULL)
9470 slot = NULL;
9471 else
9472 {
9473 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9474 &line_header_local,
9475 line_header_local_hash, INSERT);
9476 gdb_assert (slot != NULL);
9477 }
9478 if (slot != NULL && *slot == NULL)
9479 {
9480 /* This newly decoded line number information unit will be owned
9481 by line_header_hash hash table. */
9482 *slot = cu->line_header;
4c8aa72d 9483 cu->line_header_die_owner = NULL;
527f3840
JK
9484 }
9485 else
9486 {
9487 /* We cannot free any current entry in (*slot) as that struct line_header
9488 may be already used by multiple CUs. Create only temporary decoded
9489 line_header for this CU - it may happen at most once for each line
9490 number information unit. And if we're not using line_header_hash
9491 then this is what we want as well. */
9492 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9493 }
9494 decode_mapping = (die->tag != DW_TAG_partial_unit);
9495 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9496 decode_mapping);
fff8551c 9497
2ab95328
TT
9498}
9499
95554aad 9500/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9501
c906108c 9502static void
e7c27a73 9503read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9504{
dee91e82 9505 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9507 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9508 CORE_ADDR highpc = ((CORE_ADDR) 0);
9509 struct attribute *attr;
c906108c 9510 struct die_info *child_die;
e142c38c 9511 CORE_ADDR baseaddr;
6e70227d 9512
e142c38c 9513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9514
fae299cd 9515 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9516
9517 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9518 from finish_block. */
2acceee2 9519 if (lowpc == ((CORE_ADDR) -1))
c906108c 9520 lowpc = highpc;
3e29f34a 9521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9522
d721ba37 9523 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9524
95554aad 9525 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9526
f4b8a18d
KW
9527 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9528 standardised yet. As a workaround for the language detection we fall
9529 back to the DW_AT_producer string. */
9530 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9531 cu->language = language_opencl;
9532
3019eac3
DE
9533 /* Similar hack for Go. */
9534 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9535 set_cu_language (DW_LANG_Go, cu);
9536
d721ba37 9537 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9538
9539 /* Decode line number information if present. We do this before
9540 processing child DIEs, so that the line header table is available
9541 for DW_AT_decl_file. */
d721ba37 9542 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9543
9544 /* Process all dies in compilation unit. */
9545 if (die->child != NULL)
9546 {
9547 child_die = die->child;
9548 while (child_die && child_die->tag)
9549 {
9550 process_die (child_die, cu);
9551 child_die = sibling_die (child_die);
9552 }
9553 }
9554
9555 /* Decode macro information, if present. Dwarf 2 macro information
9556 refers to information in the line number info statement program
9557 header, so we can only read it if we've read the header
9558 successfully. */
0af92d60
JK
9559 attr = dwarf2_attr (die, DW_AT_macros, cu);
9560 if (attr == NULL)
9561 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9562 if (attr && cu->line_header)
9563 {
9564 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9565 complaint (&symfile_complaints,
0af92d60 9566 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9567
43f3e411 9568 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9569 }
9570 else
9571 {
9572 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9573 if (attr && cu->line_header)
9574 {
9575 unsigned int macro_offset = DW_UNSND (attr);
9576
43f3e411 9577 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9578 }
9579 }
3019eac3
DE
9580}
9581
f4dc4d17
DE
9582/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9583 Create the set of symtabs used by this TU, or if this TU is sharing
9584 symtabs with another TU and the symtabs have already been created
9585 then restore those symtabs in the line header.
9586 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9587
9588static void
f4dc4d17 9589setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9590{
f4dc4d17
DE
9591 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9592 struct type_unit_group *tu_group;
9593 int first_time;
3019eac3 9594 struct attribute *attr;
9c541725 9595 unsigned int i;
0186c6a7 9596 struct signatured_type *sig_type;
3019eac3 9597
f4dc4d17 9598 gdb_assert (per_cu->is_debug_types);
0186c6a7 9599 sig_type = (struct signatured_type *) per_cu;
3019eac3 9600
f4dc4d17 9601 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9602
f4dc4d17 9603 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9604 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9605 if (sig_type->type_unit_group == NULL)
9606 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9607 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9608
9609 /* If we've already processed this stmt_list there's no real need to
9610 do it again, we could fake it and just recreate the part we need
9611 (file name,index -> symtab mapping). If data shows this optimization
9612 is useful we can do it then. */
43f3e411 9613 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9614
9615 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9616 debug info. */
fff8551c 9617 line_header_up lh;
f4dc4d17 9618 if (attr != NULL)
3019eac3 9619 {
9c541725 9620 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9621 lh = dwarf_decode_line_header (line_offset, cu);
9622 }
9623 if (lh == NULL)
9624 {
9625 if (first_time)
9626 dwarf2_start_symtab (cu, "", NULL, 0);
9627 else
9628 {
9629 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9630 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9631 }
f4dc4d17 9632 return;
3019eac3
DE
9633 }
9634
4c8aa72d
PA
9635 cu->line_header = lh.release ();
9636 cu->line_header_die_owner = die;
3019eac3 9637
f4dc4d17
DE
9638 if (first_time)
9639 {
43f3e411 9640 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9641
1fd60fc0
DE
9642 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9643 still initializing it, and our caller (a few levels up)
9644 process_full_type_unit still needs to know if this is the first
9645 time. */
9646
4c8aa72d
PA
9647 tu_group->num_symtabs = cu->line_header->file_names.size ();
9648 tu_group->symtabs = XNEWVEC (struct symtab *,
9649 cu->line_header->file_names.size ());
3019eac3 9650
4c8aa72d 9651 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9652 {
4c8aa72d 9653 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9654
4c8aa72d 9655 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9656
f4dc4d17
DE
9657 if (current_subfile->symtab == NULL)
9658 {
4c8aa72d
PA
9659 /* NOTE: start_subfile will recognize when it's been
9660 passed a file it has already seen. So we can't
9661 assume there's a simple mapping from
9662 cu->line_header->file_names to subfiles, plus
9663 cu->line_header->file_names may contain dups. */
43f3e411
DE
9664 current_subfile->symtab
9665 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9666 }
9667
8c43009f
PA
9668 fe.symtab = current_subfile->symtab;
9669 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9670 }
9671 }
9672 else
3019eac3 9673 {
0ab9ce85 9674 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9675
4c8aa72d 9676 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9677 {
4c8aa72d 9678 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 9679
4c8aa72d 9680 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9681 }
3019eac3
DE
9682 }
9683
f4dc4d17
DE
9684 /* The main symtab is allocated last. Type units don't have DW_AT_name
9685 so they don't have a "real" (so to speak) symtab anyway.
9686 There is later code that will assign the main symtab to all symbols
9687 that don't have one. We need to handle the case of a symbol with a
9688 missing symtab (DW_AT_decl_file) anyway. */
9689}
3019eac3 9690
f4dc4d17
DE
9691/* Process DW_TAG_type_unit.
9692 For TUs we want to skip the first top level sibling if it's not the
9693 actual type being defined by this TU. In this case the first top
9694 level sibling is there to provide context only. */
3019eac3 9695
f4dc4d17
DE
9696static void
9697read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9698{
9699 struct die_info *child_die;
3019eac3 9700
f4dc4d17
DE
9701 prepare_one_comp_unit (cu, die, language_minimal);
9702
9703 /* Initialize (or reinitialize) the machinery for building symtabs.
9704 We do this before processing child DIEs, so that the line header table
9705 is available for DW_AT_decl_file. */
9706 setup_type_unit_groups (die, cu);
9707
9708 if (die->child != NULL)
9709 {
9710 child_die = die->child;
9711 while (child_die && child_die->tag)
9712 {
9713 process_die (child_die, cu);
9714 child_die = sibling_die (child_die);
9715 }
9716 }
3019eac3
DE
9717}
9718\f
80626a55
DE
9719/* DWO/DWP files.
9720
9721 http://gcc.gnu.org/wiki/DebugFission
9722 http://gcc.gnu.org/wiki/DebugFissionDWP
9723
9724 To simplify handling of both DWO files ("object" files with the DWARF info)
9725 and DWP files (a file with the DWOs packaged up into one file), we treat
9726 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9727
9728static hashval_t
9729hash_dwo_file (const void *item)
9730{
9a3c8263 9731 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9732 hashval_t hash;
3019eac3 9733
a2ce51a0
DE
9734 hash = htab_hash_string (dwo_file->dwo_name);
9735 if (dwo_file->comp_dir != NULL)
9736 hash += htab_hash_string (dwo_file->comp_dir);
9737 return hash;
3019eac3
DE
9738}
9739
9740static int
9741eq_dwo_file (const void *item_lhs, const void *item_rhs)
9742{
9a3c8263
SM
9743 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9744 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9745
a2ce51a0
DE
9746 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9747 return 0;
9748 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9749 return lhs->comp_dir == rhs->comp_dir;
9750 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9751}
9752
9753/* Allocate a hash table for DWO files. */
9754
9755static htab_t
9756allocate_dwo_file_hash_table (void)
9757{
9758 struct objfile *objfile = dwarf2_per_objfile->objfile;
9759
9760 return htab_create_alloc_ex (41,
9761 hash_dwo_file,
9762 eq_dwo_file,
9763 NULL,
9764 &objfile->objfile_obstack,
9765 hashtab_obstack_allocate,
9766 dummy_obstack_deallocate);
9767}
9768
80626a55
DE
9769/* Lookup DWO file DWO_NAME. */
9770
9771static void **
0ac5b59e 9772lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9773{
9774 struct dwo_file find_entry;
9775 void **slot;
9776
9777 if (dwarf2_per_objfile->dwo_files == NULL)
9778 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9779
9780 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9781 find_entry.dwo_name = dwo_name;
9782 find_entry.comp_dir = comp_dir;
80626a55
DE
9783 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9784
9785 return slot;
9786}
9787
3019eac3
DE
9788static hashval_t
9789hash_dwo_unit (const void *item)
9790{
9a3c8263 9791 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9792
9793 /* This drops the top 32 bits of the id, but is ok for a hash. */
9794 return dwo_unit->signature;
9795}
9796
9797static int
9798eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9799{
9a3c8263
SM
9800 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9801 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9802
9803 /* The signature is assumed to be unique within the DWO file.
9804 So while object file CU dwo_id's always have the value zero,
9805 that's OK, assuming each object file DWO file has only one CU,
9806 and that's the rule for now. */
9807 return lhs->signature == rhs->signature;
9808}
9809
9810/* Allocate a hash table for DWO CUs,TUs.
9811 There is one of these tables for each of CUs,TUs for each DWO file. */
9812
9813static htab_t
9814allocate_dwo_unit_table (struct objfile *objfile)
9815{
9816 /* Start out with a pretty small number.
9817 Generally DWO files contain only one CU and maybe some TUs. */
9818 return htab_create_alloc_ex (3,
9819 hash_dwo_unit,
9820 eq_dwo_unit,
9821 NULL,
9822 &objfile->objfile_obstack,
9823 hashtab_obstack_allocate,
9824 dummy_obstack_deallocate);
9825}
9826
80626a55 9827/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9828
19c3d4c9 9829struct create_dwo_cu_data
3019eac3
DE
9830{
9831 struct dwo_file *dwo_file;
19c3d4c9 9832 struct dwo_unit dwo_unit;
3019eac3
DE
9833};
9834
19c3d4c9 9835/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9836
9837static void
19c3d4c9
DE
9838create_dwo_cu_reader (const struct die_reader_specs *reader,
9839 const gdb_byte *info_ptr,
9840 struct die_info *comp_unit_die,
9841 int has_children,
9842 void *datap)
3019eac3
DE
9843{
9844 struct dwarf2_cu *cu = reader->cu;
9c541725 9845 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9846 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9847 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9848 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9849 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9850 struct attribute *attr;
3019eac3
DE
9851
9852 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9853 if (attr == NULL)
9854 {
19c3d4c9
DE
9855 complaint (&symfile_complaints,
9856 _("Dwarf Error: debug entry at offset 0x%x is missing"
9857 " its dwo_id [in module %s]"),
9c541725 9858 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9859 return;
9860 }
9861
3019eac3
DE
9862 dwo_unit->dwo_file = dwo_file;
9863 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9864 dwo_unit->section = section;
9c541725 9865 dwo_unit->sect_off = sect_off;
3019eac3
DE
9866 dwo_unit->length = cu->per_cu->length;
9867
b4f54984 9868 if (dwarf_read_debug)
4031ecc5 9869 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9870 to_underlying (sect_off),
9871 hex_string (dwo_unit->signature));
3019eac3
DE
9872}
9873
33c5cd75 9874/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9875 Note: This function processes DWO files only, not DWP files. */
3019eac3 9876
33c5cd75
DB
9877static void
9878create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9879 htab_t &cus_htab)
3019eac3
DE
9880{
9881 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9882 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9883 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9884
33c5cd75
DB
9885 dwarf2_read_section (objfile, &section);
9886 info_ptr = section.buffer;
3019eac3
DE
9887
9888 if (info_ptr == NULL)
33c5cd75 9889 return;
3019eac3 9890
b4f54984 9891 if (dwarf_read_debug)
19c3d4c9
DE
9892 {
9893 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9894 get_section_name (&section),
9895 get_section_file_name (&section));
19c3d4c9 9896 }
3019eac3 9897
33c5cd75 9898 end_ptr = info_ptr + section.size;
3019eac3
DE
9899 while (info_ptr < end_ptr)
9900 {
9901 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9902 struct create_dwo_cu_data create_dwo_cu_data;
9903 struct dwo_unit *dwo_unit;
9904 void **slot;
9905 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9906
19c3d4c9
DE
9907 memset (&create_dwo_cu_data.dwo_unit, 0,
9908 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9909 memset (&per_cu, 0, sizeof (per_cu));
9910 per_cu.objfile = objfile;
9911 per_cu.is_debug_types = 0;
33c5cd75
DB
9912 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9913 per_cu.section = &section;
c5ed0576 9914 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9915
9916 init_cutu_and_read_dies_no_follow (
9917 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9918 info_ptr += per_cu.length;
9919
9920 // If the unit could not be parsed, skip it.
9921 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9922 continue;
3019eac3 9923
33c5cd75
DB
9924 if (cus_htab == NULL)
9925 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 9926
33c5cd75
DB
9927 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9928 *dwo_unit = create_dwo_cu_data.dwo_unit;
9929 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9930 gdb_assert (slot != NULL);
9931 if (*slot != NULL)
19c3d4c9 9932 {
33c5cd75
DB
9933 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9934 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 9935
33c5cd75
DB
9936 complaint (&symfile_complaints,
9937 _("debug cu entry at offset 0x%x is duplicate to"
9938 " the entry at offset 0x%x, signature %s"),
9939 to_underlying (sect_off), to_underlying (dup_sect_off),
9940 hex_string (dwo_unit->signature));
19c3d4c9 9941 }
33c5cd75 9942 *slot = (void *)dwo_unit;
3019eac3 9943 }
3019eac3
DE
9944}
9945
80626a55
DE
9946/* DWP file .debug_{cu,tu}_index section format:
9947 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9948
d2415c6c
DE
9949 DWP Version 1:
9950
80626a55
DE
9951 Both index sections have the same format, and serve to map a 64-bit
9952 signature to a set of section numbers. Each section begins with a header,
9953 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9954 indexes, and a pool of 32-bit section numbers. The index sections will be
9955 aligned at 8-byte boundaries in the file.
9956
d2415c6c
DE
9957 The index section header consists of:
9958
9959 V, 32 bit version number
9960 -, 32 bits unused
9961 N, 32 bit number of compilation units or type units in the index
9962 M, 32 bit number of slots in the hash table
80626a55 9963
d2415c6c 9964 Numbers are recorded using the byte order of the application binary.
80626a55 9965
d2415c6c
DE
9966 The hash table begins at offset 16 in the section, and consists of an array
9967 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9968 order of the application binary). Unused slots in the hash table are 0.
9969 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9970
d2415c6c
DE
9971 The parallel table begins immediately after the hash table
9972 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9973 array of 32-bit indexes (using the byte order of the application binary),
9974 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9975 table contains a 32-bit index into the pool of section numbers. For unused
9976 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9977
73869dc2
DE
9978 The pool of section numbers begins immediately following the hash table
9979 (at offset 16 + 12 * M from the beginning of the section). The pool of
9980 section numbers consists of an array of 32-bit words (using the byte order
9981 of the application binary). Each item in the array is indexed starting
9982 from 0. The hash table entry provides the index of the first section
9983 number in the set. Additional section numbers in the set follow, and the
9984 set is terminated by a 0 entry (section number 0 is not used in ELF).
9985
9986 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9987 section must be the first entry in the set, and the .debug_abbrev.dwo must
9988 be the second entry. Other members of the set may follow in any order.
9989
9990 ---
9991
9992 DWP Version 2:
9993
9994 DWP Version 2 combines all the .debug_info, etc. sections into one,
9995 and the entries in the index tables are now offsets into these sections.
9996 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9997 section.
9998
9999 Index Section Contents:
10000 Header
10001 Hash Table of Signatures dwp_hash_table.hash_table
10002 Parallel Table of Indices dwp_hash_table.unit_table
10003 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10004 Table of Section Sizes dwp_hash_table.v2.sizes
10005
10006 The index section header consists of:
10007
10008 V, 32 bit version number
10009 L, 32 bit number of columns in the table of section offsets
10010 N, 32 bit number of compilation units or type units in the index
10011 M, 32 bit number of slots in the hash table
10012
10013 Numbers are recorded using the byte order of the application binary.
10014
10015 The hash table has the same format as version 1.
10016 The parallel table of indices has the same format as version 1,
10017 except that the entries are origin-1 indices into the table of sections
10018 offsets and the table of section sizes.
10019
10020 The table of offsets begins immediately following the parallel table
10021 (at offset 16 + 12 * M from the beginning of the section). The table is
10022 a two-dimensional array of 32-bit words (using the byte order of the
10023 application binary), with L columns and N+1 rows, in row-major order.
10024 Each row in the array is indexed starting from 0. The first row provides
10025 a key to the remaining rows: each column in this row provides an identifier
10026 for a debug section, and the offsets in the same column of subsequent rows
10027 refer to that section. The section identifiers are:
10028
10029 DW_SECT_INFO 1 .debug_info.dwo
10030 DW_SECT_TYPES 2 .debug_types.dwo
10031 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10032 DW_SECT_LINE 4 .debug_line.dwo
10033 DW_SECT_LOC 5 .debug_loc.dwo
10034 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10035 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10036 DW_SECT_MACRO 8 .debug_macro.dwo
10037
10038 The offsets provided by the CU and TU index sections are the base offsets
10039 for the contributions made by each CU or TU to the corresponding section
10040 in the package file. Each CU and TU header contains an abbrev_offset
10041 field, used to find the abbreviations table for that CU or TU within the
10042 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10043 be interpreted as relative to the base offset given in the index section.
10044 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10045 should be interpreted as relative to the base offset for .debug_line.dwo,
10046 and offsets into other debug sections obtained from DWARF attributes should
10047 also be interpreted as relative to the corresponding base offset.
10048
10049 The table of sizes begins immediately following the table of offsets.
10050 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10051 with L columns and N rows, in row-major order. Each row in the array is
10052 indexed starting from 1 (row 0 is shared by the two tables).
10053
10054 ---
10055
10056 Hash table lookup is handled the same in version 1 and 2:
10057
10058 We assume that N and M will not exceed 2^32 - 1.
10059 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10060
d2415c6c
DE
10061 Given a 64-bit compilation unit signature or a type signature S, an entry
10062 in the hash table is located as follows:
80626a55 10063
d2415c6c
DE
10064 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10065 the low-order k bits all set to 1.
80626a55 10066
d2415c6c 10067 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10068
d2415c6c
DE
10069 3) If the hash table entry at index H matches the signature, use that
10070 entry. If the hash table entry at index H is unused (all zeroes),
10071 terminate the search: the signature is not present in the table.
80626a55 10072
d2415c6c 10073 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10074
d2415c6c 10075 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10076 to stop at an unused slot or find the match. */
80626a55
DE
10077
10078/* Create a hash table to map DWO IDs to their CU/TU entry in
10079 .debug_{info,types}.dwo in DWP_FILE.
10080 Returns NULL if there isn't one.
10081 Note: This function processes DWP files only, not DWO files. */
10082
10083static struct dwp_hash_table *
10084create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10085{
10086 struct objfile *objfile = dwarf2_per_objfile->objfile;
10087 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10088 const gdb_byte *index_ptr, *index_end;
80626a55 10089 struct dwarf2_section_info *index;
73869dc2 10090 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10091 struct dwp_hash_table *htab;
10092
10093 if (is_debug_types)
10094 index = &dwp_file->sections.tu_index;
10095 else
10096 index = &dwp_file->sections.cu_index;
10097
10098 if (dwarf2_section_empty_p (index))
10099 return NULL;
10100 dwarf2_read_section (objfile, index);
10101
10102 index_ptr = index->buffer;
10103 index_end = index_ptr + index->size;
10104
10105 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10106 index_ptr += 4;
10107 if (version == 2)
10108 nr_columns = read_4_bytes (dbfd, index_ptr);
10109 else
10110 nr_columns = 0;
10111 index_ptr += 4;
80626a55
DE
10112 nr_units = read_4_bytes (dbfd, index_ptr);
10113 index_ptr += 4;
10114 nr_slots = read_4_bytes (dbfd, index_ptr);
10115 index_ptr += 4;
10116
73869dc2 10117 if (version != 1 && version != 2)
80626a55 10118 {
21aa081e 10119 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10120 " [in module %s]"),
21aa081e 10121 pulongest (version), dwp_file->name);
80626a55
DE
10122 }
10123 if (nr_slots != (nr_slots & -nr_slots))
10124 {
21aa081e 10125 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10126 " is not power of 2 [in module %s]"),
21aa081e 10127 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10128 }
10129
10130 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10131 htab->version = version;
10132 htab->nr_columns = nr_columns;
80626a55
DE
10133 htab->nr_units = nr_units;
10134 htab->nr_slots = nr_slots;
10135 htab->hash_table = index_ptr;
10136 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10137
10138 /* Exit early if the table is empty. */
10139 if (nr_slots == 0 || nr_units == 0
10140 || (version == 2 && nr_columns == 0))
10141 {
10142 /* All must be zero. */
10143 if (nr_slots != 0 || nr_units != 0
10144 || (version == 2 && nr_columns != 0))
10145 {
10146 complaint (&symfile_complaints,
10147 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10148 " all zero [in modules %s]"),
10149 dwp_file->name);
10150 }
10151 return htab;
10152 }
10153
10154 if (version == 1)
10155 {
10156 htab->section_pool.v1.indices =
10157 htab->unit_table + sizeof (uint32_t) * nr_slots;
10158 /* It's harder to decide whether the section is too small in v1.
10159 V1 is deprecated anyway so we punt. */
10160 }
10161 else
10162 {
10163 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10164 int *ids = htab->section_pool.v2.section_ids;
10165 /* Reverse map for error checking. */
10166 int ids_seen[DW_SECT_MAX + 1];
10167 int i;
10168
10169 if (nr_columns < 2)
10170 {
10171 error (_("Dwarf Error: bad DWP hash table, too few columns"
10172 " in section table [in module %s]"),
10173 dwp_file->name);
10174 }
10175 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10176 {
10177 error (_("Dwarf Error: bad DWP hash table, too many columns"
10178 " in section table [in module %s]"),
10179 dwp_file->name);
10180 }
10181 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10182 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10183 for (i = 0; i < nr_columns; ++i)
10184 {
10185 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10186
10187 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10188 {
10189 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10190 " in section table [in module %s]"),
10191 id, dwp_file->name);
10192 }
10193 if (ids_seen[id] != -1)
10194 {
10195 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10196 " id %d in section table [in module %s]"),
10197 id, dwp_file->name);
10198 }
10199 ids_seen[id] = i;
10200 ids[i] = id;
10201 }
10202 /* Must have exactly one info or types section. */
10203 if (((ids_seen[DW_SECT_INFO] != -1)
10204 + (ids_seen[DW_SECT_TYPES] != -1))
10205 != 1)
10206 {
10207 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10208 " DWO info/types section [in module %s]"),
10209 dwp_file->name);
10210 }
10211 /* Must have an abbrev section. */
10212 if (ids_seen[DW_SECT_ABBREV] == -1)
10213 {
10214 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10215 " section [in module %s]"),
10216 dwp_file->name);
10217 }
10218 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10219 htab->section_pool.v2.sizes =
10220 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10221 * nr_units * nr_columns);
10222 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10223 * nr_units * nr_columns))
10224 > index_end)
10225 {
10226 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10227 " [in module %s]"),
10228 dwp_file->name);
10229 }
10230 }
80626a55
DE
10231
10232 return htab;
10233}
10234
10235/* Update SECTIONS with the data from SECTP.
10236
10237 This function is like the other "locate" section routines that are
10238 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10239 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10240
10241 The result is non-zero for success, or zero if an error was found. */
10242
10243static int
73869dc2
DE
10244locate_v1_virtual_dwo_sections (asection *sectp,
10245 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10246{
10247 const struct dwop_section_names *names = &dwop_section_names;
10248
10249 if (section_is_p (sectp->name, &names->abbrev_dwo))
10250 {
10251 /* There can be only one. */
049412e3 10252 if (sections->abbrev.s.section != NULL)
80626a55 10253 return 0;
049412e3 10254 sections->abbrev.s.section = sectp;
80626a55
DE
10255 sections->abbrev.size = bfd_get_section_size (sectp);
10256 }
10257 else if (section_is_p (sectp->name, &names->info_dwo)
10258 || section_is_p (sectp->name, &names->types_dwo))
10259 {
10260 /* There can be only one. */
049412e3 10261 if (sections->info_or_types.s.section != NULL)
80626a55 10262 return 0;
049412e3 10263 sections->info_or_types.s.section = sectp;
80626a55
DE
10264 sections->info_or_types.size = bfd_get_section_size (sectp);
10265 }
10266 else if (section_is_p (sectp->name, &names->line_dwo))
10267 {
10268 /* There can be only one. */
049412e3 10269 if (sections->line.s.section != NULL)
80626a55 10270 return 0;
049412e3 10271 sections->line.s.section = sectp;
80626a55
DE
10272 sections->line.size = bfd_get_section_size (sectp);
10273 }
10274 else if (section_is_p (sectp->name, &names->loc_dwo))
10275 {
10276 /* There can be only one. */
049412e3 10277 if (sections->loc.s.section != NULL)
80626a55 10278 return 0;
049412e3 10279 sections->loc.s.section = sectp;
80626a55
DE
10280 sections->loc.size = bfd_get_section_size (sectp);
10281 }
10282 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10283 {
10284 /* There can be only one. */
049412e3 10285 if (sections->macinfo.s.section != NULL)
80626a55 10286 return 0;
049412e3 10287 sections->macinfo.s.section = sectp;
80626a55
DE
10288 sections->macinfo.size = bfd_get_section_size (sectp);
10289 }
10290 else if (section_is_p (sectp->name, &names->macro_dwo))
10291 {
10292 /* There can be only one. */
049412e3 10293 if (sections->macro.s.section != NULL)
80626a55 10294 return 0;
049412e3 10295 sections->macro.s.section = sectp;
80626a55
DE
10296 sections->macro.size = bfd_get_section_size (sectp);
10297 }
10298 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10299 {
10300 /* There can be only one. */
049412e3 10301 if (sections->str_offsets.s.section != NULL)
80626a55 10302 return 0;
049412e3 10303 sections->str_offsets.s.section = sectp;
80626a55
DE
10304 sections->str_offsets.size = bfd_get_section_size (sectp);
10305 }
10306 else
10307 {
10308 /* No other kind of section is valid. */
10309 return 0;
10310 }
10311
10312 return 1;
10313}
10314
73869dc2
DE
10315/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10316 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10317 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10318 This is for DWP version 1 files. */
80626a55
DE
10319
10320static struct dwo_unit *
73869dc2
DE
10321create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10322 uint32_t unit_index,
10323 const char *comp_dir,
10324 ULONGEST signature, int is_debug_types)
80626a55
DE
10325{
10326 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10327 const struct dwp_hash_table *dwp_htab =
10328 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10329 bfd *dbfd = dwp_file->dbfd;
10330 const char *kind = is_debug_types ? "TU" : "CU";
10331 struct dwo_file *dwo_file;
10332 struct dwo_unit *dwo_unit;
73869dc2 10333 struct virtual_v1_dwo_sections sections;
80626a55
DE
10334 void **dwo_file_slot;
10335 char *virtual_dwo_name;
80626a55
DE
10336 struct cleanup *cleanups;
10337 int i;
10338
73869dc2
DE
10339 gdb_assert (dwp_file->version == 1);
10340
b4f54984 10341 if (dwarf_read_debug)
80626a55 10342 {
73869dc2 10343 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10344 kind,
73869dc2 10345 pulongest (unit_index), hex_string (signature),
80626a55
DE
10346 dwp_file->name);
10347 }
10348
19ac8c2e 10349 /* Fetch the sections of this DWO unit.
80626a55
DE
10350 Put a limit on the number of sections we look for so that bad data
10351 doesn't cause us to loop forever. */
10352
73869dc2 10353#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10354 (1 /* .debug_info or .debug_types */ \
10355 + 1 /* .debug_abbrev */ \
10356 + 1 /* .debug_line */ \
10357 + 1 /* .debug_loc */ \
10358 + 1 /* .debug_str_offsets */ \
19ac8c2e 10359 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10360 + 1 /* trailing zero */)
10361
10362 memset (&sections, 0, sizeof (sections));
10363 cleanups = make_cleanup (null_cleanup, 0);
10364
73869dc2 10365 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10366 {
10367 asection *sectp;
10368 uint32_t section_nr =
10369 read_4_bytes (dbfd,
73869dc2
DE
10370 dwp_htab->section_pool.v1.indices
10371 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10372
10373 if (section_nr == 0)
10374 break;
10375 if (section_nr >= dwp_file->num_sections)
10376 {
10377 error (_("Dwarf Error: bad DWP hash table, section number too large"
10378 " [in module %s]"),
10379 dwp_file->name);
10380 }
10381
10382 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10383 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10384 {
10385 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10386 " [in module %s]"),
10387 dwp_file->name);
10388 }
10389 }
10390
10391 if (i < 2
a32a8923
DE
10392 || dwarf2_section_empty_p (&sections.info_or_types)
10393 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10394 {
10395 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10396 " [in module %s]"),
10397 dwp_file->name);
10398 }
73869dc2 10399 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10400 {
10401 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10402 " [in module %s]"),
10403 dwp_file->name);
10404 }
10405
10406 /* It's easier for the rest of the code if we fake a struct dwo_file and
10407 have dwo_unit "live" in that. At least for now.
10408
10409 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10410 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10411 file, we can combine them back into a virtual DWO file to save space
10412 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10413 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10414
2792b94d
PM
10415 virtual_dwo_name =
10416 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10417 get_section_id (&sections.abbrev),
10418 get_section_id (&sections.line),
10419 get_section_id (&sections.loc),
10420 get_section_id (&sections.str_offsets));
80626a55
DE
10421 make_cleanup (xfree, virtual_dwo_name);
10422 /* Can we use an existing virtual DWO file? */
0ac5b59e 10423 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10424 /* Create one if necessary. */
10425 if (*dwo_file_slot == NULL)
10426 {
b4f54984 10427 if (dwarf_read_debug)
80626a55
DE
10428 {
10429 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10430 virtual_dwo_name);
10431 }
10432 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10433 dwo_file->dwo_name
10434 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10435 virtual_dwo_name,
10436 strlen (virtual_dwo_name));
0ac5b59e 10437 dwo_file->comp_dir = comp_dir;
80626a55
DE
10438 dwo_file->sections.abbrev = sections.abbrev;
10439 dwo_file->sections.line = sections.line;
10440 dwo_file->sections.loc = sections.loc;
10441 dwo_file->sections.macinfo = sections.macinfo;
10442 dwo_file->sections.macro = sections.macro;
10443 dwo_file->sections.str_offsets = sections.str_offsets;
10444 /* The "str" section is global to the entire DWP file. */
10445 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10446 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10447 there's no need to record it in dwo_file.
10448 Also, we can't simply record type sections in dwo_file because
10449 we record a pointer into the vector in dwo_unit. As we collect more
10450 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10451 for it, invalidating all copies of pointers into the previous
10452 contents. */
80626a55
DE
10453 *dwo_file_slot = dwo_file;
10454 }
10455 else
10456 {
b4f54984 10457 if (dwarf_read_debug)
80626a55
DE
10458 {
10459 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10460 virtual_dwo_name);
10461 }
9a3c8263 10462 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10463 }
10464 do_cleanups (cleanups);
10465
10466 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10467 dwo_unit->dwo_file = dwo_file;
10468 dwo_unit->signature = signature;
8d749320
SM
10469 dwo_unit->section =
10470 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10471 *dwo_unit->section = sections.info_or_types;
57d63ce2 10472 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10473
10474 return dwo_unit;
10475}
10476
73869dc2
DE
10477/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10478 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10479 piece within that section used by a TU/CU, return a virtual section
10480 of just that piece. */
10481
10482static struct dwarf2_section_info
10483create_dwp_v2_section (struct dwarf2_section_info *section,
10484 bfd_size_type offset, bfd_size_type size)
10485{
10486 struct dwarf2_section_info result;
10487 asection *sectp;
10488
10489 gdb_assert (section != NULL);
10490 gdb_assert (!section->is_virtual);
10491
10492 memset (&result, 0, sizeof (result));
10493 result.s.containing_section = section;
10494 result.is_virtual = 1;
10495
10496 if (size == 0)
10497 return result;
10498
10499 sectp = get_section_bfd_section (section);
10500
10501 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10502 bounds of the real section. This is a pretty-rare event, so just
10503 flag an error (easier) instead of a warning and trying to cope. */
10504 if (sectp == NULL
10505 || offset + size > bfd_get_section_size (sectp))
10506 {
10507 bfd *abfd = sectp->owner;
10508
10509 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10510 " in section %s [in module %s]"),
10511 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10512 objfile_name (dwarf2_per_objfile->objfile));
10513 }
10514
10515 result.virtual_offset = offset;
10516 result.size = size;
10517 return result;
10518}
10519
10520/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10521 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10522 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10523 This is for DWP version 2 files. */
10524
10525static struct dwo_unit *
10526create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10527 uint32_t unit_index,
10528 const char *comp_dir,
10529 ULONGEST signature, int is_debug_types)
10530{
10531 struct objfile *objfile = dwarf2_per_objfile->objfile;
10532 const struct dwp_hash_table *dwp_htab =
10533 is_debug_types ? dwp_file->tus : dwp_file->cus;
10534 bfd *dbfd = dwp_file->dbfd;
10535 const char *kind = is_debug_types ? "TU" : "CU";
10536 struct dwo_file *dwo_file;
10537 struct dwo_unit *dwo_unit;
10538 struct virtual_v2_dwo_sections sections;
10539 void **dwo_file_slot;
10540 char *virtual_dwo_name;
73869dc2
DE
10541 struct cleanup *cleanups;
10542 int i;
10543
10544 gdb_assert (dwp_file->version == 2);
10545
b4f54984 10546 if (dwarf_read_debug)
73869dc2
DE
10547 {
10548 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10549 kind,
10550 pulongest (unit_index), hex_string (signature),
10551 dwp_file->name);
10552 }
10553
10554 /* Fetch the section offsets of this DWO unit. */
10555
10556 memset (&sections, 0, sizeof (sections));
10557 cleanups = make_cleanup (null_cleanup, 0);
10558
10559 for (i = 0; i < dwp_htab->nr_columns; ++i)
10560 {
10561 uint32_t offset = read_4_bytes (dbfd,
10562 dwp_htab->section_pool.v2.offsets
10563 + (((unit_index - 1) * dwp_htab->nr_columns
10564 + i)
10565 * sizeof (uint32_t)));
10566 uint32_t size = read_4_bytes (dbfd,
10567 dwp_htab->section_pool.v2.sizes
10568 + (((unit_index - 1) * dwp_htab->nr_columns
10569 + i)
10570 * sizeof (uint32_t)));
10571
10572 switch (dwp_htab->section_pool.v2.section_ids[i])
10573 {
10574 case DW_SECT_INFO:
10575 case DW_SECT_TYPES:
10576 sections.info_or_types_offset = offset;
10577 sections.info_or_types_size = size;
10578 break;
10579 case DW_SECT_ABBREV:
10580 sections.abbrev_offset = offset;
10581 sections.abbrev_size = size;
10582 break;
10583 case DW_SECT_LINE:
10584 sections.line_offset = offset;
10585 sections.line_size = size;
10586 break;
10587 case DW_SECT_LOC:
10588 sections.loc_offset = offset;
10589 sections.loc_size = size;
10590 break;
10591 case DW_SECT_STR_OFFSETS:
10592 sections.str_offsets_offset = offset;
10593 sections.str_offsets_size = size;
10594 break;
10595 case DW_SECT_MACINFO:
10596 sections.macinfo_offset = offset;
10597 sections.macinfo_size = size;
10598 break;
10599 case DW_SECT_MACRO:
10600 sections.macro_offset = offset;
10601 sections.macro_size = size;
10602 break;
10603 }
10604 }
10605
10606 /* It's easier for the rest of the code if we fake a struct dwo_file and
10607 have dwo_unit "live" in that. At least for now.
10608
10609 The DWP file can be made up of a random collection of CUs and TUs.
10610 However, for each CU + set of TUs that came from the same original DWO
10611 file, we can combine them back into a virtual DWO file to save space
10612 (fewer struct dwo_file objects to allocate). Remember that for really
10613 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10614
10615 virtual_dwo_name =
10616 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10617 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10618 (long) (sections.line_size ? sections.line_offset : 0),
10619 (long) (sections.loc_size ? sections.loc_offset : 0),
10620 (long) (sections.str_offsets_size
10621 ? sections.str_offsets_offset : 0));
10622 make_cleanup (xfree, virtual_dwo_name);
10623 /* Can we use an existing virtual DWO file? */
10624 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10625 /* Create one if necessary. */
10626 if (*dwo_file_slot == NULL)
10627 {
b4f54984 10628 if (dwarf_read_debug)
73869dc2
DE
10629 {
10630 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10631 virtual_dwo_name);
10632 }
10633 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10634 dwo_file->dwo_name
10635 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10636 virtual_dwo_name,
10637 strlen (virtual_dwo_name));
73869dc2
DE
10638 dwo_file->comp_dir = comp_dir;
10639 dwo_file->sections.abbrev =
10640 create_dwp_v2_section (&dwp_file->sections.abbrev,
10641 sections.abbrev_offset, sections.abbrev_size);
10642 dwo_file->sections.line =
10643 create_dwp_v2_section (&dwp_file->sections.line,
10644 sections.line_offset, sections.line_size);
10645 dwo_file->sections.loc =
10646 create_dwp_v2_section (&dwp_file->sections.loc,
10647 sections.loc_offset, sections.loc_size);
10648 dwo_file->sections.macinfo =
10649 create_dwp_v2_section (&dwp_file->sections.macinfo,
10650 sections.macinfo_offset, sections.macinfo_size);
10651 dwo_file->sections.macro =
10652 create_dwp_v2_section (&dwp_file->sections.macro,
10653 sections.macro_offset, sections.macro_size);
10654 dwo_file->sections.str_offsets =
10655 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10656 sections.str_offsets_offset,
10657 sections.str_offsets_size);
10658 /* The "str" section is global to the entire DWP file. */
10659 dwo_file->sections.str = dwp_file->sections.str;
10660 /* The info or types section is assigned below to dwo_unit,
10661 there's no need to record it in dwo_file.
10662 Also, we can't simply record type sections in dwo_file because
10663 we record a pointer into the vector in dwo_unit. As we collect more
10664 types we'll grow the vector and eventually have to reallocate space
10665 for it, invalidating all copies of pointers into the previous
10666 contents. */
10667 *dwo_file_slot = dwo_file;
10668 }
10669 else
10670 {
b4f54984 10671 if (dwarf_read_debug)
73869dc2
DE
10672 {
10673 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10674 virtual_dwo_name);
10675 }
9a3c8263 10676 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10677 }
10678 do_cleanups (cleanups);
10679
10680 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10681 dwo_unit->dwo_file = dwo_file;
10682 dwo_unit->signature = signature;
8d749320
SM
10683 dwo_unit->section =
10684 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10685 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10686 ? &dwp_file->sections.types
10687 : &dwp_file->sections.info,
10688 sections.info_or_types_offset,
10689 sections.info_or_types_size);
10690 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10691
10692 return dwo_unit;
10693}
10694
57d63ce2
DE
10695/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10696 Returns NULL if the signature isn't found. */
80626a55
DE
10697
10698static struct dwo_unit *
57d63ce2
DE
10699lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10700 ULONGEST signature, int is_debug_types)
80626a55 10701{
57d63ce2
DE
10702 const struct dwp_hash_table *dwp_htab =
10703 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10704 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10705 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10706 uint32_t hash = signature & mask;
10707 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10708 unsigned int i;
10709 void **slot;
870f88f7 10710 struct dwo_unit find_dwo_cu;
80626a55
DE
10711
10712 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10713 find_dwo_cu.signature = signature;
19ac8c2e
DE
10714 slot = htab_find_slot (is_debug_types
10715 ? dwp_file->loaded_tus
10716 : dwp_file->loaded_cus,
10717 &find_dwo_cu, INSERT);
80626a55
DE
10718
10719 if (*slot != NULL)
9a3c8263 10720 return (struct dwo_unit *) *slot;
80626a55
DE
10721
10722 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10723 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10724 {
10725 ULONGEST signature_in_table;
10726
10727 signature_in_table =
57d63ce2 10728 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10729 if (signature_in_table == signature)
10730 {
57d63ce2
DE
10731 uint32_t unit_index =
10732 read_4_bytes (dbfd,
10733 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10734
73869dc2
DE
10735 if (dwp_file->version == 1)
10736 {
10737 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10738 comp_dir, signature,
10739 is_debug_types);
10740 }
10741 else
10742 {
10743 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10744 comp_dir, signature,
10745 is_debug_types);
10746 }
9a3c8263 10747 return (struct dwo_unit *) *slot;
80626a55
DE
10748 }
10749 if (signature_in_table == 0)
10750 return NULL;
10751 hash = (hash + hash2) & mask;
10752 }
10753
10754 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10755 " [in module %s]"),
10756 dwp_file->name);
10757}
10758
ab5088bf 10759/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10760 Open the file specified by FILE_NAME and hand it off to BFD for
10761 preliminary analysis. Return a newly initialized bfd *, which
10762 includes a canonicalized copy of FILE_NAME.
80626a55 10763 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10764 SEARCH_CWD is true if the current directory is to be searched.
10765 It will be searched before debug-file-directory.
13aaf454
DE
10766 If successful, the file is added to the bfd include table of the
10767 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10768 If unable to find/open the file, return NULL.
3019eac3
DE
10769 NOTE: This function is derived from symfile_bfd_open. */
10770
192b62ce 10771static gdb_bfd_ref_ptr
6ac97d4c 10772try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10773{
80626a55 10774 int desc, flags;
3019eac3 10775 char *absolute_name;
9c02c129
DE
10776 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10777 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10778 to debug_file_directory. */
10779 char *search_path;
10780 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10781
6ac97d4c
DE
10782 if (search_cwd)
10783 {
10784 if (*debug_file_directory != '\0')
10785 search_path = concat (".", dirname_separator_string,
b36cec19 10786 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10787 else
10788 search_path = xstrdup (".");
10789 }
9c02c129 10790 else
6ac97d4c 10791 search_path = xstrdup (debug_file_directory);
3019eac3 10792
492c0ab7 10793 flags = OPF_RETURN_REALPATH;
80626a55
DE
10794 if (is_dwp)
10795 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10796 desc = openp (search_path, flags, file_name,
3019eac3 10797 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10798 xfree (search_path);
3019eac3
DE
10799 if (desc < 0)
10800 return NULL;
10801
192b62ce 10802 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10803 xfree (absolute_name);
9c02c129
DE
10804 if (sym_bfd == NULL)
10805 return NULL;
192b62ce 10806 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10807
192b62ce
TT
10808 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10809 return NULL;
3019eac3 10810
13aaf454
DE
10811 /* Success. Record the bfd as having been included by the objfile's bfd.
10812 This is important because things like demangled_names_hash lives in the
10813 objfile's per_bfd space and may have references to things like symbol
10814 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10815 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10816
3019eac3
DE
10817 return sym_bfd;
10818}
10819
ab5088bf 10820/* Try to open DWO file FILE_NAME.
3019eac3
DE
10821 COMP_DIR is the DW_AT_comp_dir attribute.
10822 The result is the bfd handle of the file.
10823 If there is a problem finding or opening the file, return NULL.
10824 Upon success, the canonicalized path of the file is stored in the bfd,
10825 same as symfile_bfd_open. */
10826
192b62ce 10827static gdb_bfd_ref_ptr
ab5088bf 10828open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10829{
80626a55 10830 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10831 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10832
10833 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10834
10835 if (comp_dir != NULL)
10836 {
b36cec19
PA
10837 char *path_to_try = concat (comp_dir, SLASH_STRING,
10838 file_name, (char *) NULL);
3019eac3
DE
10839
10840 /* NOTE: If comp_dir is a relative path, this will also try the
10841 search path, which seems useful. */
192b62ce
TT
10842 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10843 1 /*search_cwd*/));
3019eac3
DE
10844 xfree (path_to_try);
10845 if (abfd != NULL)
10846 return abfd;
10847 }
10848
10849 /* That didn't work, try debug-file-directory, which, despite its name,
10850 is a list of paths. */
10851
10852 if (*debug_file_directory == '\0')
10853 return NULL;
10854
6ac97d4c 10855 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10856}
10857
80626a55
DE
10858/* This function is mapped across the sections and remembers the offset and
10859 size of each of the DWO debugging sections we are interested in. */
10860
10861static void
10862dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10863{
9a3c8263 10864 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10865 const struct dwop_section_names *names = &dwop_section_names;
10866
10867 if (section_is_p (sectp->name, &names->abbrev_dwo))
10868 {
049412e3 10869 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10870 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10871 }
10872 else if (section_is_p (sectp->name, &names->info_dwo))
10873 {
049412e3 10874 dwo_sections->info.s.section = sectp;
80626a55
DE
10875 dwo_sections->info.size = bfd_get_section_size (sectp);
10876 }
10877 else if (section_is_p (sectp->name, &names->line_dwo))
10878 {
049412e3 10879 dwo_sections->line.s.section = sectp;
80626a55
DE
10880 dwo_sections->line.size = bfd_get_section_size (sectp);
10881 }
10882 else if (section_is_p (sectp->name, &names->loc_dwo))
10883 {
049412e3 10884 dwo_sections->loc.s.section = sectp;
80626a55
DE
10885 dwo_sections->loc.size = bfd_get_section_size (sectp);
10886 }
10887 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10888 {
049412e3 10889 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10890 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10891 }
10892 else if (section_is_p (sectp->name, &names->macro_dwo))
10893 {
049412e3 10894 dwo_sections->macro.s.section = sectp;
80626a55
DE
10895 dwo_sections->macro.size = bfd_get_section_size (sectp);
10896 }
10897 else if (section_is_p (sectp->name, &names->str_dwo))
10898 {
049412e3 10899 dwo_sections->str.s.section = sectp;
80626a55
DE
10900 dwo_sections->str.size = bfd_get_section_size (sectp);
10901 }
10902 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10903 {
049412e3 10904 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10905 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10906 }
10907 else if (section_is_p (sectp->name, &names->types_dwo))
10908 {
10909 struct dwarf2_section_info type_section;
10910
10911 memset (&type_section, 0, sizeof (type_section));
049412e3 10912 type_section.s.section = sectp;
80626a55
DE
10913 type_section.size = bfd_get_section_size (sectp);
10914 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10915 &type_section);
10916 }
10917}
10918
ab5088bf 10919/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10920 by PER_CU. This is for the non-DWP case.
80626a55 10921 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10922
10923static struct dwo_file *
0ac5b59e
DE
10924open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10925 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10926{
10927 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10928 struct dwo_file *dwo_file;
3019eac3
DE
10929 struct cleanup *cleanups;
10930
192b62ce 10931 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10932 if (dbfd == NULL)
10933 {
b4f54984 10934 if (dwarf_read_debug)
80626a55
DE
10935 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10936 return NULL;
10937 }
10938 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10939 dwo_file->dwo_name = dwo_name;
10940 dwo_file->comp_dir = comp_dir;
192b62ce 10941 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10942
10943 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10944
192b62ce
TT
10945 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10946 &dwo_file->sections);
3019eac3 10947
33c5cd75 10948 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 10949
78d4d2c5
JK
10950 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10951 dwo_file->tus);
3019eac3
DE
10952
10953 discard_cleanups (cleanups);
10954
b4f54984 10955 if (dwarf_read_debug)
80626a55
DE
10956 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10957
3019eac3
DE
10958 return dwo_file;
10959}
10960
80626a55 10961/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10962 size of each of the DWP debugging sections common to version 1 and 2 that
10963 we are interested in. */
3019eac3 10964
80626a55 10965static void
73869dc2
DE
10966dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10967 void *dwp_file_ptr)
3019eac3 10968{
9a3c8263 10969 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10970 const struct dwop_section_names *names = &dwop_section_names;
10971 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10972
80626a55 10973 /* Record the ELF section number for later lookup: this is what the
73869dc2 10974 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10975 gdb_assert (elf_section_nr < dwp_file->num_sections);
10976 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10977
80626a55
DE
10978 /* Look for specific sections that we need. */
10979 if (section_is_p (sectp->name, &names->str_dwo))
10980 {
049412e3 10981 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10982 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10983 }
10984 else if (section_is_p (sectp->name, &names->cu_index))
10985 {
049412e3 10986 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10987 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10988 }
10989 else if (section_is_p (sectp->name, &names->tu_index))
10990 {
049412e3 10991 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10992 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10993 }
10994}
3019eac3 10995
73869dc2
DE
10996/* This function is mapped across the sections and remembers the offset and
10997 size of each of the DWP version 2 debugging sections that we are interested
10998 in. This is split into a separate function because we don't know if we
10999 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11000
11001static void
11002dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11003{
9a3c8263 11004 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11005 const struct dwop_section_names *names = &dwop_section_names;
11006 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11007
11008 /* Record the ELF section number for later lookup: this is what the
11009 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11010 gdb_assert (elf_section_nr < dwp_file->num_sections);
11011 dwp_file->elf_sections[elf_section_nr] = sectp;
11012
11013 /* Look for specific sections that we need. */
11014 if (section_is_p (sectp->name, &names->abbrev_dwo))
11015 {
049412e3 11016 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11017 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11018 }
11019 else if (section_is_p (sectp->name, &names->info_dwo))
11020 {
049412e3 11021 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11022 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11023 }
11024 else if (section_is_p (sectp->name, &names->line_dwo))
11025 {
049412e3 11026 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11027 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11028 }
11029 else if (section_is_p (sectp->name, &names->loc_dwo))
11030 {
049412e3 11031 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11032 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11033 }
11034 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11035 {
049412e3 11036 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11037 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11038 }
11039 else if (section_is_p (sectp->name, &names->macro_dwo))
11040 {
049412e3 11041 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11042 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11043 }
11044 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11045 {
049412e3 11046 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11047 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11048 }
11049 else if (section_is_p (sectp->name, &names->types_dwo))
11050 {
049412e3 11051 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11052 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11053 }
11054}
11055
80626a55 11056/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11057
80626a55
DE
11058static hashval_t
11059hash_dwp_loaded_cutus (const void *item)
11060{
9a3c8263 11061 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11062
80626a55
DE
11063 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11064 return dwo_unit->signature;
3019eac3
DE
11065}
11066
80626a55 11067/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11068
80626a55
DE
11069static int
11070eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11071{
9a3c8263
SM
11072 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11073 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11074
80626a55
DE
11075 return dua->signature == dub->signature;
11076}
3019eac3 11077
80626a55 11078/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11079
80626a55
DE
11080static htab_t
11081allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11082{
11083 return htab_create_alloc_ex (3,
11084 hash_dwp_loaded_cutus,
11085 eq_dwp_loaded_cutus,
11086 NULL,
11087 &objfile->objfile_obstack,
11088 hashtab_obstack_allocate,
11089 dummy_obstack_deallocate);
11090}
3019eac3 11091
ab5088bf
DE
11092/* Try to open DWP file FILE_NAME.
11093 The result is the bfd handle of the file.
11094 If there is a problem finding or opening the file, return NULL.
11095 Upon success, the canonicalized path of the file is stored in the bfd,
11096 same as symfile_bfd_open. */
11097
192b62ce 11098static gdb_bfd_ref_ptr
ab5088bf
DE
11099open_dwp_file (const char *file_name)
11100{
192b62ce
TT
11101 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11102 1 /*search_cwd*/));
6ac97d4c
DE
11103 if (abfd != NULL)
11104 return abfd;
11105
11106 /* Work around upstream bug 15652.
11107 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11108 [Whether that's a "bug" is debatable, but it is getting in our way.]
11109 We have no real idea where the dwp file is, because gdb's realpath-ing
11110 of the executable's path may have discarded the needed info.
11111 [IWBN if the dwp file name was recorded in the executable, akin to
11112 .gnu_debuglink, but that doesn't exist yet.]
11113 Strip the directory from FILE_NAME and search again. */
11114 if (*debug_file_directory != '\0')
11115 {
11116 /* Don't implicitly search the current directory here.
11117 If the user wants to search "." to handle this case,
11118 it must be added to debug-file-directory. */
11119 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11120 0 /*search_cwd*/);
11121 }
11122
11123 return NULL;
ab5088bf
DE
11124}
11125
80626a55
DE
11126/* Initialize the use of the DWP file for the current objfile.
11127 By convention the name of the DWP file is ${objfile}.dwp.
11128 The result is NULL if it can't be found. */
a766d390 11129
80626a55 11130static struct dwp_file *
ab5088bf 11131open_and_init_dwp_file (void)
80626a55
DE
11132{
11133 struct objfile *objfile = dwarf2_per_objfile->objfile;
11134 struct dwp_file *dwp_file;
80626a55 11135
82bf32bc
JK
11136 /* Try to find first .dwp for the binary file before any symbolic links
11137 resolving. */
6c447423
DE
11138
11139 /* If the objfile is a debug file, find the name of the real binary
11140 file and get the name of dwp file from there. */
d721ba37 11141 std::string dwp_name;
6c447423
DE
11142 if (objfile->separate_debug_objfile_backlink != NULL)
11143 {
11144 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11145 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11146
d721ba37 11147 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11148 }
11149 else
d721ba37
PA
11150 dwp_name = objfile->original_name;
11151
11152 dwp_name += ".dwp";
80626a55 11153
d721ba37 11154 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11155 if (dbfd == NULL
11156 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11157 {
11158 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11159 dwp_name = objfile_name (objfile);
11160 dwp_name += ".dwp";
11161 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11162 }
11163
80626a55
DE
11164 if (dbfd == NULL)
11165 {
b4f54984 11166 if (dwarf_read_debug)
d721ba37 11167 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11168 return NULL;
3019eac3 11169 }
80626a55 11170 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11171 dwp_file->name = bfd_get_filename (dbfd.get ());
11172 dwp_file->dbfd = dbfd.release ();
c906108c 11173
80626a55 11174 /* +1: section 0 is unused */
192b62ce 11175 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11176 dwp_file->elf_sections =
11177 OBSTACK_CALLOC (&objfile->objfile_obstack,
11178 dwp_file->num_sections, asection *);
11179
192b62ce
TT
11180 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11181 dwp_file);
80626a55
DE
11182
11183 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11184
11185 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11186
73869dc2
DE
11187 /* The DWP file version is stored in the hash table. Oh well. */
11188 if (dwp_file->cus->version != dwp_file->tus->version)
11189 {
11190 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11191 pretty bizarre. We use pulongest here because that's the established
4d65956b 11192 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11193 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11194 " TU version %s [in DWP file %s]"),
11195 pulongest (dwp_file->cus->version),
d721ba37 11196 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11197 }
11198 dwp_file->version = dwp_file->cus->version;
11199
11200 if (dwp_file->version == 2)
192b62ce
TT
11201 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11202 dwp_file);
73869dc2 11203
19ac8c2e
DE
11204 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11205 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11206
b4f54984 11207 if (dwarf_read_debug)
80626a55
DE
11208 {
11209 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11210 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11211 " %s CUs, %s TUs\n",
11212 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11213 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11214 }
11215
11216 return dwp_file;
3019eac3 11217}
c906108c 11218
ab5088bf
DE
11219/* Wrapper around open_and_init_dwp_file, only open it once. */
11220
11221static struct dwp_file *
11222get_dwp_file (void)
11223{
11224 if (! dwarf2_per_objfile->dwp_checked)
11225 {
11226 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11227 dwarf2_per_objfile->dwp_checked = 1;
11228 }
11229 return dwarf2_per_objfile->dwp_file;
11230}
11231
80626a55
DE
11232/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11233 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11234 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11235 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11236 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11237
11238 This is called, for example, when wanting to read a variable with a
11239 complex location. Therefore we don't want to do file i/o for every call.
11240 Therefore we don't want to look for a DWO file on every call.
11241 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11242 then we check if we've already seen DWO_NAME, and only THEN do we check
11243 for a DWO file.
11244
1c658ad5 11245 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11246 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11247
3019eac3 11248static struct dwo_unit *
80626a55
DE
11249lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11250 const char *dwo_name, const char *comp_dir,
11251 ULONGEST signature, int is_debug_types)
3019eac3
DE
11252{
11253 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11254 const char *kind = is_debug_types ? "TU" : "CU";
11255 void **dwo_file_slot;
3019eac3 11256 struct dwo_file *dwo_file;
80626a55 11257 struct dwp_file *dwp_file;
cb1df416 11258
6a506a2d
DE
11259 /* First see if there's a DWP file.
11260 If we have a DWP file but didn't find the DWO inside it, don't
11261 look for the original DWO file. It makes gdb behave differently
11262 depending on whether one is debugging in the build tree. */
cf2c3c16 11263
ab5088bf 11264 dwp_file = get_dwp_file ();
80626a55 11265 if (dwp_file != NULL)
cf2c3c16 11266 {
80626a55
DE
11267 const struct dwp_hash_table *dwp_htab =
11268 is_debug_types ? dwp_file->tus : dwp_file->cus;
11269
11270 if (dwp_htab != NULL)
11271 {
11272 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11273 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11274 signature, is_debug_types);
80626a55
DE
11275
11276 if (dwo_cutu != NULL)
11277 {
b4f54984 11278 if (dwarf_read_debug)
80626a55
DE
11279 {
11280 fprintf_unfiltered (gdb_stdlog,
11281 "Virtual DWO %s %s found: @%s\n",
11282 kind, hex_string (signature),
11283 host_address_to_string (dwo_cutu));
11284 }
11285 return dwo_cutu;
11286 }
11287 }
11288 }
6a506a2d 11289 else
80626a55 11290 {
6a506a2d 11291 /* No DWP file, look for the DWO file. */
80626a55 11292
6a506a2d
DE
11293 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11294 if (*dwo_file_slot == NULL)
80626a55 11295 {
6a506a2d
DE
11296 /* Read in the file and build a table of the CUs/TUs it contains. */
11297 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11298 }
6a506a2d 11299 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11300 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11301
6a506a2d 11302 if (dwo_file != NULL)
19c3d4c9 11303 {
6a506a2d
DE
11304 struct dwo_unit *dwo_cutu = NULL;
11305
11306 if (is_debug_types && dwo_file->tus)
11307 {
11308 struct dwo_unit find_dwo_cutu;
11309
11310 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11311 find_dwo_cutu.signature = signature;
9a3c8263
SM
11312 dwo_cutu
11313 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11314 }
33c5cd75 11315 else if (!is_debug_types && dwo_file->cus)
80626a55 11316 {
33c5cd75
DB
11317 struct dwo_unit find_dwo_cutu;
11318
11319 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11320 find_dwo_cutu.signature = signature;
11321 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11322 &find_dwo_cutu);
6a506a2d
DE
11323 }
11324
11325 if (dwo_cutu != NULL)
11326 {
b4f54984 11327 if (dwarf_read_debug)
6a506a2d
DE
11328 {
11329 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11330 kind, dwo_name, hex_string (signature),
11331 host_address_to_string (dwo_cutu));
11332 }
11333 return dwo_cutu;
80626a55
DE
11334 }
11335 }
2e276125 11336 }
9cdd5dbd 11337
80626a55
DE
11338 /* We didn't find it. This could mean a dwo_id mismatch, or
11339 someone deleted the DWO/DWP file, or the search path isn't set up
11340 correctly to find the file. */
11341
b4f54984 11342 if (dwarf_read_debug)
80626a55
DE
11343 {
11344 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11345 kind, dwo_name, hex_string (signature));
11346 }
3019eac3 11347
6656a72d
DE
11348 /* This is a warning and not a complaint because it can be caused by
11349 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11350 {
11351 /* Print the name of the DWP file if we looked there, helps the user
11352 better diagnose the problem. */
11353 char *dwp_text = NULL;
11354 struct cleanup *cleanups;
11355
11356 if (dwp_file != NULL)
11357 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11358 cleanups = make_cleanup (xfree, dwp_text);
11359
11360 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11361 " [in module %s]"),
11362 kind, dwo_name, hex_string (signature),
11363 dwp_text != NULL ? dwp_text : "",
11364 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11365 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11366
11367 do_cleanups (cleanups);
11368 }
3019eac3 11369 return NULL;
5fb290d7
DJ
11370}
11371
80626a55
DE
11372/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11373 See lookup_dwo_cutu_unit for details. */
11374
11375static struct dwo_unit *
11376lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11377 const char *dwo_name, const char *comp_dir,
11378 ULONGEST signature)
11379{
11380 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11381}
11382
11383/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11384 See lookup_dwo_cutu_unit for details. */
11385
11386static struct dwo_unit *
11387lookup_dwo_type_unit (struct signatured_type *this_tu,
11388 const char *dwo_name, const char *comp_dir)
11389{
11390 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11391}
11392
89e63ee4
DE
11393/* Traversal function for queue_and_load_all_dwo_tus. */
11394
11395static int
11396queue_and_load_dwo_tu (void **slot, void *info)
11397{
11398 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11399 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11400 ULONGEST signature = dwo_unit->signature;
11401 struct signatured_type *sig_type =
11402 lookup_dwo_signatured_type (per_cu->cu, signature);
11403
11404 if (sig_type != NULL)
11405 {
11406 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11407
11408 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11409 a real dependency of PER_CU on SIG_TYPE. That is detected later
11410 while processing PER_CU. */
11411 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11412 load_full_type_unit (sig_cu);
11413 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11414 }
11415
11416 return 1;
11417}
11418
11419/* Queue all TUs contained in the DWO of PER_CU to be read in.
11420 The DWO may have the only definition of the type, though it may not be
11421 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11422 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11423
11424static void
11425queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11426{
11427 struct dwo_unit *dwo_unit;
11428 struct dwo_file *dwo_file;
11429
11430 gdb_assert (!per_cu->is_debug_types);
11431 gdb_assert (get_dwp_file () == NULL);
11432 gdb_assert (per_cu->cu != NULL);
11433
11434 dwo_unit = per_cu->cu->dwo_unit;
11435 gdb_assert (dwo_unit != NULL);
11436
11437 dwo_file = dwo_unit->dwo_file;
11438 if (dwo_file->tus != NULL)
11439 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11440}
11441
3019eac3
DE
11442/* Free all resources associated with DWO_FILE.
11443 Close the DWO file and munmap the sections.
11444 All memory should be on the objfile obstack. */
348e048f
DE
11445
11446static void
3019eac3 11447free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11448{
348e048f 11449
5c6fa7ab 11450 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11451 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11452
3019eac3
DE
11453 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11454}
348e048f 11455
3019eac3 11456/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11457
3019eac3
DE
11458static void
11459free_dwo_file_cleanup (void *arg)
11460{
11461 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11462 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11463
3019eac3
DE
11464 free_dwo_file (dwo_file, objfile);
11465}
348e048f 11466
3019eac3 11467/* Traversal function for free_dwo_files. */
2ab95328 11468
3019eac3
DE
11469static int
11470free_dwo_file_from_slot (void **slot, void *info)
11471{
11472 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11473 struct objfile *objfile = (struct objfile *) info;
348e048f 11474
3019eac3 11475 free_dwo_file (dwo_file, objfile);
348e048f 11476
3019eac3
DE
11477 return 1;
11478}
348e048f 11479
3019eac3 11480/* Free all resources associated with DWO_FILES. */
348e048f 11481
3019eac3
DE
11482static void
11483free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11484{
11485 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11486}
3019eac3
DE
11487\f
11488/* Read in various DIEs. */
348e048f 11489
d389af10
JK
11490/* qsort helper for inherit_abstract_dies. */
11491
11492static int
11493unsigned_int_compar (const void *ap, const void *bp)
11494{
11495 unsigned int a = *(unsigned int *) ap;
11496 unsigned int b = *(unsigned int *) bp;
11497
11498 return (a > b) - (b > a);
11499}
11500
11501/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11502 Inherit only the children of the DW_AT_abstract_origin DIE not being
11503 already referenced by DW_AT_abstract_origin from the children of the
11504 current DIE. */
d389af10
JK
11505
11506static void
11507inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11508{
11509 struct die_info *child_die;
11510 unsigned die_children_count;
11511 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11512 sect_offset *offsets;
11513 sect_offset *offsets_end, *offsetp;
d389af10
JK
11514 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11515 struct die_info *origin_die;
11516 /* Iterator of the ORIGIN_DIE children. */
11517 struct die_info *origin_child_die;
11518 struct cleanup *cleanups;
11519 struct attribute *attr;
cd02d79d
PA
11520 struct dwarf2_cu *origin_cu;
11521 struct pending **origin_previous_list_in_scope;
d389af10
JK
11522
11523 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11524 if (!attr)
11525 return;
11526
cd02d79d
PA
11527 /* Note that following die references may follow to a die in a
11528 different cu. */
11529
11530 origin_cu = cu;
11531 origin_die = follow_die_ref (die, attr, &origin_cu);
11532
11533 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11534 symbols in. */
11535 origin_previous_list_in_scope = origin_cu->list_in_scope;
11536 origin_cu->list_in_scope = cu->list_in_scope;
11537
edb3359d
DJ
11538 if (die->tag != origin_die->tag
11539 && !(die->tag == DW_TAG_inlined_subroutine
11540 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11541 complaint (&symfile_complaints,
11542 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11543 to_underlying (die->sect_off),
11544 to_underlying (origin_die->sect_off));
d389af10
JK
11545
11546 child_die = die->child;
11547 die_children_count = 0;
11548 while (child_die && child_die->tag)
11549 {
11550 child_die = sibling_die (child_die);
11551 die_children_count++;
11552 }
8d749320 11553 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11554 cleanups = make_cleanup (xfree, offsets);
11555
11556 offsets_end = offsets;
3ea89b92
PMR
11557 for (child_die = die->child;
11558 child_die && child_die->tag;
11559 child_die = sibling_die (child_die))
11560 {
11561 struct die_info *child_origin_die;
11562 struct dwarf2_cu *child_origin_cu;
11563
11564 /* We are trying to process concrete instance entries:
216f72a1 11565 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11566 it's not relevant to our analysis here. i.e. detecting DIEs that are
11567 present in the abstract instance but not referenced in the concrete
11568 one. */
216f72a1
JK
11569 if (child_die->tag == DW_TAG_call_site
11570 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11571 continue;
11572
c38f313d
DJ
11573 /* For each CHILD_DIE, find the corresponding child of
11574 ORIGIN_DIE. If there is more than one layer of
11575 DW_AT_abstract_origin, follow them all; there shouldn't be,
11576 but GCC versions at least through 4.4 generate this (GCC PR
11577 40573). */
3ea89b92
PMR
11578 child_origin_die = child_die;
11579 child_origin_cu = cu;
c38f313d
DJ
11580 while (1)
11581 {
cd02d79d
PA
11582 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11583 child_origin_cu);
c38f313d
DJ
11584 if (attr == NULL)
11585 break;
cd02d79d
PA
11586 child_origin_die = follow_die_ref (child_origin_die, attr,
11587 &child_origin_cu);
c38f313d
DJ
11588 }
11589
d389af10
JK
11590 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11591 counterpart may exist. */
c38f313d 11592 if (child_origin_die != child_die)
d389af10 11593 {
edb3359d
DJ
11594 if (child_die->tag != child_origin_die->tag
11595 && !(child_die->tag == DW_TAG_inlined_subroutine
11596 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11597 complaint (&symfile_complaints,
11598 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11599 "different tags"),
11600 to_underlying (child_die->sect_off),
11601 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11602 if (child_origin_die->parent != origin_die)
11603 complaint (&symfile_complaints,
11604 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11605 "different parents"),
11606 to_underlying (child_die->sect_off),
11607 to_underlying (child_origin_die->sect_off));
c38f313d 11608 else
9c541725 11609 *offsets_end++ = child_origin_die->sect_off;
d389af10 11610 }
d389af10
JK
11611 }
11612 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11613 unsigned_int_compar);
11614 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11615 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11616 complaint (&symfile_complaints,
11617 _("Multiple children of DIE 0x%x refer "
11618 "to DIE 0x%x as their abstract origin"),
9c541725 11619 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11620
11621 offsetp = offsets;
11622 origin_child_die = origin_die->child;
11623 while (origin_child_die && origin_child_die->tag)
11624 {
11625 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11626 while (offsetp < offsets_end
9c541725 11627 && *offsetp < origin_child_die->sect_off)
d389af10 11628 offsetp++;
b64f50a1 11629 if (offsetp >= offsets_end
9c541725 11630 || *offsetp > origin_child_die->sect_off)
d389af10 11631 {
adde2bff
DE
11632 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11633 Check whether we're already processing ORIGIN_CHILD_DIE.
11634 This can happen with mutually referenced abstract_origins.
11635 PR 16581. */
11636 if (!origin_child_die->in_process)
11637 process_die (origin_child_die, origin_cu);
d389af10
JK
11638 }
11639 origin_child_die = sibling_die (origin_child_die);
11640 }
cd02d79d 11641 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11642
11643 do_cleanups (cleanups);
11644}
11645
c906108c 11646static void
e7c27a73 11647read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11648{
e7c27a73 11649 struct objfile *objfile = cu->objfile;
3e29f34a 11650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11651 struct context_stack *newobj;
c906108c
SS
11652 CORE_ADDR lowpc;
11653 CORE_ADDR highpc;
11654 struct die_info *child_die;
edb3359d 11655 struct attribute *attr, *call_line, *call_file;
15d034d0 11656 const char *name;
e142c38c 11657 CORE_ADDR baseaddr;
801e3a5b 11658 struct block *block;
edb3359d 11659 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11660 VEC (symbolp) *template_args = NULL;
11661 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11662
11663 if (inlined_func)
11664 {
11665 /* If we do not have call site information, we can't show the
11666 caller of this inlined function. That's too confusing, so
11667 only use the scope for local variables. */
11668 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11669 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11670 if (call_line == NULL || call_file == NULL)
11671 {
11672 read_lexical_block_scope (die, cu);
11673 return;
11674 }
11675 }
c906108c 11676
e142c38c
DJ
11677 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11678
94af9270 11679 name = dwarf2_name (die, cu);
c906108c 11680
e8d05480
JB
11681 /* Ignore functions with missing or empty names. These are actually
11682 illegal according to the DWARF standard. */
11683 if (name == NULL)
11684 {
11685 complaint (&symfile_complaints,
b64f50a1 11686 _("missing name for subprogram DIE at %d"),
9c541725 11687 to_underlying (die->sect_off));
e8d05480
JB
11688 return;
11689 }
11690
11691 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11692 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11693 <= PC_BOUNDS_INVALID)
e8d05480 11694 {
ae4d0c03
PM
11695 attr = dwarf2_attr (die, DW_AT_external, cu);
11696 if (!attr || !DW_UNSND (attr))
11697 complaint (&symfile_complaints,
3e43a32a
MS
11698 _("cannot get low and high bounds "
11699 "for subprogram DIE at %d"),
9c541725 11700 to_underlying (die->sect_off));
e8d05480
JB
11701 return;
11702 }
c906108c 11703
3e29f34a
MR
11704 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11705 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11706
34eaf542
TT
11707 /* If we have any template arguments, then we must allocate a
11708 different sort of symbol. */
11709 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11710 {
11711 if (child_die->tag == DW_TAG_template_type_param
11712 || child_die->tag == DW_TAG_template_value_param)
11713 {
e623cf5d 11714 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11715 templ_func->base.is_cplus_template_function = 1;
11716 break;
11717 }
11718 }
11719
fe978cb0
PA
11720 newobj = push_context (0, lowpc);
11721 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11722 (struct symbol *) templ_func);
4c2df51b 11723
4cecd739
DJ
11724 /* If there is a location expression for DW_AT_frame_base, record
11725 it. */
e142c38c 11726 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11727 if (attr)
fe978cb0 11728 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11729
63e43d3a
PMR
11730 /* If there is a location for the static link, record it. */
11731 newobj->static_link = NULL;
11732 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11733 if (attr)
11734 {
224c3ddb
SM
11735 newobj->static_link
11736 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11737 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11738 }
11739
e142c38c 11740 cu->list_in_scope = &local_symbols;
c906108c 11741
639d11d3 11742 if (die->child != NULL)
c906108c 11743 {
639d11d3 11744 child_die = die->child;
c906108c
SS
11745 while (child_die && child_die->tag)
11746 {
34eaf542
TT
11747 if (child_die->tag == DW_TAG_template_type_param
11748 || child_die->tag == DW_TAG_template_value_param)
11749 {
11750 struct symbol *arg = new_symbol (child_die, NULL, cu);
11751
f1078f66
DJ
11752 if (arg != NULL)
11753 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11754 }
11755 else
11756 process_die (child_die, cu);
c906108c
SS
11757 child_die = sibling_die (child_die);
11758 }
11759 }
11760
d389af10
JK
11761 inherit_abstract_dies (die, cu);
11762
4a811a97
UW
11763 /* If we have a DW_AT_specification, we might need to import using
11764 directives from the context of the specification DIE. See the
11765 comment in determine_prefix. */
11766 if (cu->language == language_cplus
11767 && dwarf2_attr (die, DW_AT_specification, cu))
11768 {
11769 struct dwarf2_cu *spec_cu = cu;
11770 struct die_info *spec_die = die_specification (die, &spec_cu);
11771
11772 while (spec_die)
11773 {
11774 child_die = spec_die->child;
11775 while (child_die && child_die->tag)
11776 {
11777 if (child_die->tag == DW_TAG_imported_module)
11778 process_die (child_die, spec_cu);
11779 child_die = sibling_die (child_die);
11780 }
11781
11782 /* In some cases, GCC generates specification DIEs that
11783 themselves contain DW_AT_specification attributes. */
11784 spec_die = die_specification (spec_die, &spec_cu);
11785 }
11786 }
11787
fe978cb0 11788 newobj = pop_context ();
c906108c 11789 /* Make a block for the local symbols within. */
fe978cb0 11790 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11791 newobj->static_link, lowpc, highpc);
801e3a5b 11792
df8a16a1 11793 /* For C++, set the block's scope. */
45280282
IB
11794 if ((cu->language == language_cplus
11795 || cu->language == language_fortran
c44af4eb
TT
11796 || cu->language == language_d
11797 || cu->language == language_rust)
4d4ec4e5 11798 && cu->processing_has_namespace_info)
195a3f6c
TT
11799 block_set_scope (block, determine_prefix (die, cu),
11800 &objfile->objfile_obstack);
df8a16a1 11801
801e3a5b
JB
11802 /* If we have address ranges, record them. */
11803 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11804
fe978cb0 11805 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11806
34eaf542
TT
11807 /* Attach template arguments to function. */
11808 if (! VEC_empty (symbolp, template_args))
11809 {
11810 gdb_assert (templ_func != NULL);
11811
11812 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11813 templ_func->template_arguments
8d749320
SM
11814 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11815 templ_func->n_template_arguments);
34eaf542
TT
11816 memcpy (templ_func->template_arguments,
11817 VEC_address (symbolp, template_args),
11818 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11819 VEC_free (symbolp, template_args);
11820 }
11821
208d8187
JB
11822 /* In C++, we can have functions nested inside functions (e.g., when
11823 a function declares a class that has methods). This means that
11824 when we finish processing a function scope, we may need to go
11825 back to building a containing block's symbol lists. */
fe978cb0 11826 local_symbols = newobj->locals;
22cee43f 11827 local_using_directives = newobj->local_using_directives;
208d8187 11828
921e78cf
JB
11829 /* If we've finished processing a top-level function, subsequent
11830 symbols go in the file symbol list. */
11831 if (outermost_context_p ())
e142c38c 11832 cu->list_in_scope = &file_symbols;
c906108c
SS
11833}
11834
11835/* Process all the DIES contained within a lexical block scope. Start
11836 a new scope, process the dies, and then close the scope. */
11837
11838static void
e7c27a73 11839read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11840{
e7c27a73 11841 struct objfile *objfile = cu->objfile;
3e29f34a 11842 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11843 struct context_stack *newobj;
c906108c
SS
11844 CORE_ADDR lowpc, highpc;
11845 struct die_info *child_die;
e142c38c
DJ
11846 CORE_ADDR baseaddr;
11847
11848 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11849
11850 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11851 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11852 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11853 be nasty. Might be easier to properly extend generic blocks to
af34e669 11854 describe ranges. */
e385593e
JK
11855 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11856 {
11857 case PC_BOUNDS_NOT_PRESENT:
11858 /* DW_TAG_lexical_block has no attributes, process its children as if
11859 there was no wrapping by that DW_TAG_lexical_block.
11860 GCC does no longer produces such DWARF since GCC r224161. */
11861 for (child_die = die->child;
11862 child_die != NULL && child_die->tag;
11863 child_die = sibling_die (child_die))
11864 process_die (child_die, cu);
11865 return;
11866 case PC_BOUNDS_INVALID:
11867 return;
11868 }
3e29f34a
MR
11869 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11870 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11871
11872 push_context (0, lowpc);
639d11d3 11873 if (die->child != NULL)
c906108c 11874 {
639d11d3 11875 child_die = die->child;
c906108c
SS
11876 while (child_die && child_die->tag)
11877 {
e7c27a73 11878 process_die (child_die, cu);
c906108c
SS
11879 child_die = sibling_die (child_die);
11880 }
11881 }
3ea89b92 11882 inherit_abstract_dies (die, cu);
fe978cb0 11883 newobj = pop_context ();
c906108c 11884
22cee43f 11885 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11886 {
801e3a5b 11887 struct block *block
63e43d3a 11888 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11889 newobj->start_addr, highpc);
801e3a5b
JB
11890
11891 /* Note that recording ranges after traversing children, as we
11892 do here, means that recording a parent's ranges entails
11893 walking across all its children's ranges as they appear in
11894 the address map, which is quadratic behavior.
11895
11896 It would be nicer to record the parent's ranges before
11897 traversing its children, simply overriding whatever you find
11898 there. But since we don't even decide whether to create a
11899 block until after we've traversed its children, that's hard
11900 to do. */
11901 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11902 }
fe978cb0 11903 local_symbols = newobj->locals;
22cee43f 11904 local_using_directives = newobj->local_using_directives;
c906108c
SS
11905}
11906
216f72a1 11907/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11908
11909static void
11910read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11911{
11912 struct objfile *objfile = cu->objfile;
11913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11914 CORE_ADDR pc, baseaddr;
11915 struct attribute *attr;
11916 struct call_site *call_site, call_site_local;
11917 void **slot;
11918 int nparams;
11919 struct die_info *child_die;
11920
11921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11922
216f72a1
JK
11923 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11924 if (attr == NULL)
11925 {
11926 /* This was a pre-DWARF-5 GNU extension alias
11927 for DW_AT_call_return_pc. */
11928 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11929 }
96408a79
SA
11930 if (!attr)
11931 {
11932 complaint (&symfile_complaints,
216f72a1 11933 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11934 "DIE 0x%x [in module %s]"),
9c541725 11935 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11936 return;
11937 }
31aa7e4e 11938 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11939 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11940
11941 if (cu->call_site_htab == NULL)
11942 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11943 NULL, &objfile->objfile_obstack,
11944 hashtab_obstack_allocate, NULL);
11945 call_site_local.pc = pc;
11946 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11947 if (*slot != NULL)
11948 {
11949 complaint (&symfile_complaints,
216f72a1 11950 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11951 "DIE 0x%x [in module %s]"),
9c541725 11952 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11953 objfile_name (objfile));
96408a79
SA
11954 return;
11955 }
11956
11957 /* Count parameters at the caller. */
11958
11959 nparams = 0;
11960 for (child_die = die->child; child_die && child_die->tag;
11961 child_die = sibling_die (child_die))
11962 {
216f72a1
JK
11963 if (child_die->tag != DW_TAG_call_site_parameter
11964 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11965 {
11966 complaint (&symfile_complaints,
216f72a1
JK
11967 _("Tag %d is not DW_TAG_call_site_parameter in "
11968 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11969 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11970 objfile_name (objfile));
96408a79
SA
11971 continue;
11972 }
11973
11974 nparams++;
11975 }
11976
224c3ddb
SM
11977 call_site
11978 = ((struct call_site *)
11979 obstack_alloc (&objfile->objfile_obstack,
11980 sizeof (*call_site)
11981 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11982 *slot = call_site;
11983 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11984 call_site->pc = pc;
11985
216f72a1
JK
11986 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11987 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11988 {
11989 struct die_info *func_die;
11990
11991 /* Skip also over DW_TAG_inlined_subroutine. */
11992 for (func_die = die->parent;
11993 func_die && func_die->tag != DW_TAG_subprogram
11994 && func_die->tag != DW_TAG_subroutine_type;
11995 func_die = func_die->parent);
11996
216f72a1
JK
11997 /* DW_AT_call_all_calls is a superset
11998 of DW_AT_call_all_tail_calls. */
96408a79 11999 if (func_die
216f72a1 12000 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12001 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12002 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12003 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12004 {
12005 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12006 not complete. But keep CALL_SITE for look ups via call_site_htab,
12007 both the initial caller containing the real return address PC and
12008 the final callee containing the current PC of a chain of tail
12009 calls do not need to have the tail call list complete. But any
12010 function candidate for a virtual tail call frame searched via
12011 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12012 determined unambiguously. */
12013 }
12014 else
12015 {
12016 struct type *func_type = NULL;
12017
12018 if (func_die)
12019 func_type = get_die_type (func_die, cu);
12020 if (func_type != NULL)
12021 {
12022 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12023
12024 /* Enlist this call site to the function. */
12025 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12026 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12027 }
12028 else
12029 complaint (&symfile_complaints,
216f72a1 12030 _("Cannot find function owning DW_TAG_call_site "
96408a79 12031 "DIE 0x%x [in module %s]"),
9c541725 12032 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12033 }
12034 }
12035
216f72a1
JK
12036 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12037 if (attr == NULL)
12038 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12039 if (attr == NULL)
12040 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12041 if (attr == NULL)
216f72a1
JK
12042 {
12043 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12044 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12045 }
96408a79
SA
12046 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12047 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12048 /* Keep NULL DWARF_BLOCK. */;
12049 else if (attr_form_is_block (attr))
12050 {
12051 struct dwarf2_locexpr_baton *dlbaton;
12052
8d749320 12053 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12054 dlbaton->data = DW_BLOCK (attr)->data;
12055 dlbaton->size = DW_BLOCK (attr)->size;
12056 dlbaton->per_cu = cu->per_cu;
12057
12058 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12059 }
7771576e 12060 else if (attr_form_is_ref (attr))
96408a79 12061 {
96408a79
SA
12062 struct dwarf2_cu *target_cu = cu;
12063 struct die_info *target_die;
12064
ac9ec31b 12065 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12066 gdb_assert (target_cu->objfile == objfile);
12067 if (die_is_declaration (target_die, target_cu))
12068 {
7d45c7c3 12069 const char *target_physname;
9112db09
JK
12070
12071 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12072 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12073 if (target_physname == NULL)
9112db09 12074 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12075 if (target_physname == NULL)
12076 complaint (&symfile_complaints,
216f72a1 12077 _("DW_AT_call_target target DIE has invalid "
96408a79 12078 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12079 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12080 else
7d455152 12081 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12082 }
12083 else
12084 {
12085 CORE_ADDR lowpc;
12086
12087 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12088 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12089 <= PC_BOUNDS_INVALID)
96408a79 12090 complaint (&symfile_complaints,
216f72a1 12091 _("DW_AT_call_target target DIE has invalid "
96408a79 12092 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12093 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12094 else
3e29f34a
MR
12095 {
12096 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12097 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12098 }
96408a79
SA
12099 }
12100 }
12101 else
12102 complaint (&symfile_complaints,
216f72a1 12103 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12104 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12105 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12106
12107 call_site->per_cu = cu->per_cu;
12108
12109 for (child_die = die->child;
12110 child_die && child_die->tag;
12111 child_die = sibling_die (child_die))
12112 {
96408a79 12113 struct call_site_parameter *parameter;
1788b2d3 12114 struct attribute *loc, *origin;
96408a79 12115
216f72a1
JK
12116 if (child_die->tag != DW_TAG_call_site_parameter
12117 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12118 {
12119 /* Already printed the complaint above. */
12120 continue;
12121 }
12122
12123 gdb_assert (call_site->parameter_count < nparams);
12124 parameter = &call_site->parameter[call_site->parameter_count];
12125
1788b2d3
JK
12126 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12127 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12128 register is contained in DW_AT_call_value. */
96408a79 12129
24c5c679 12130 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12131 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12132 if (origin == NULL)
12133 {
12134 /* This was a pre-DWARF-5 GNU extension alias
12135 for DW_AT_call_parameter. */
12136 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12137 }
7771576e 12138 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12139 {
1788b2d3 12140 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12141
12142 sect_offset sect_off
12143 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12144 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12145 {
12146 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12147 binding can be done only inside one CU. Such referenced DIE
12148 therefore cannot be even moved to DW_TAG_partial_unit. */
12149 complaint (&symfile_complaints,
216f72a1
JK
12150 _("DW_AT_call_parameter offset is not in CU for "
12151 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12152 to_underlying (child_die->sect_off),
12153 objfile_name (objfile));
d76b7dbc
JK
12154 continue;
12155 }
9c541725
PA
12156 parameter->u.param_cu_off
12157 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12158 }
12159 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12160 {
12161 complaint (&symfile_complaints,
12162 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12163 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12164 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12165 continue;
12166 }
24c5c679 12167 else
96408a79 12168 {
24c5c679
JK
12169 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12170 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12171 if (parameter->u.dwarf_reg != -1)
12172 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12173 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12174 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12175 &parameter->u.fb_offset))
12176 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12177 else
12178 {
12179 complaint (&symfile_complaints,
12180 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12181 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12182 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12183 "[in module %s]"),
9c541725
PA
12184 to_underlying (child_die->sect_off),
12185 objfile_name (objfile));
24c5c679
JK
12186 continue;
12187 }
96408a79
SA
12188 }
12189
216f72a1
JK
12190 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12191 if (attr == NULL)
12192 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12193 if (!attr_form_is_block (attr))
12194 {
12195 complaint (&symfile_complaints,
216f72a1
JK
12196 _("No DW_FORM_block* DW_AT_call_value for "
12197 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12198 to_underlying (child_die->sect_off),
12199 objfile_name (objfile));
96408a79
SA
12200 continue;
12201 }
12202 parameter->value = DW_BLOCK (attr)->data;
12203 parameter->value_size = DW_BLOCK (attr)->size;
12204
12205 /* Parameters are not pre-cleared by memset above. */
12206 parameter->data_value = NULL;
12207 parameter->data_value_size = 0;
12208 call_site->parameter_count++;
12209
216f72a1
JK
12210 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12211 if (attr == NULL)
12212 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12213 if (attr)
12214 {
12215 if (!attr_form_is_block (attr))
12216 complaint (&symfile_complaints,
216f72a1
JK
12217 _("No DW_FORM_block* DW_AT_call_data_value for "
12218 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12219 to_underlying (child_die->sect_off),
12220 objfile_name (objfile));
96408a79
SA
12221 else
12222 {
12223 parameter->data_value = DW_BLOCK (attr)->data;
12224 parameter->data_value_size = DW_BLOCK (attr)->size;
12225 }
12226 }
12227 }
12228}
12229
43988095
JK
12230/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12231 reading .debug_rnglists.
12232 Callback's type should be:
12233 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12234 Return true if the attributes are present and valid, otherwise,
12235 return false. */
12236
12237template <typename Callback>
12238static bool
12239dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12240 Callback &&callback)
12241{
12242 struct objfile *objfile = cu->objfile;
12243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12244 struct comp_unit_head *cu_header = &cu->header;
12245 bfd *obfd = objfile->obfd;
12246 unsigned int addr_size = cu_header->addr_size;
12247 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12248 /* Base address selection entry. */
12249 CORE_ADDR base;
12250 int found_base;
12251 unsigned int dummy;
12252 const gdb_byte *buffer;
12253 CORE_ADDR low = 0;
12254 CORE_ADDR high = 0;
12255 CORE_ADDR baseaddr;
12256 bool overflow = false;
12257
12258 found_base = cu->base_known;
12259 base = cu->base_address;
12260
12261 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12262 if (offset >= dwarf2_per_objfile->rnglists.size)
12263 {
12264 complaint (&symfile_complaints,
12265 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12266 offset);
12267 return false;
12268 }
12269 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12270
12271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12272
12273 while (1)
12274 {
7814882a
JK
12275 /* Initialize it due to a false compiler warning. */
12276 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12277 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12278 + dwarf2_per_objfile->rnglists.size);
12279 unsigned int bytes_read;
12280
12281 if (buffer == buf_end)
12282 {
12283 overflow = true;
12284 break;
12285 }
12286 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12287 switch (rlet)
12288 {
12289 case DW_RLE_end_of_list:
12290 break;
12291 case DW_RLE_base_address:
12292 if (buffer + cu->header.addr_size > buf_end)
12293 {
12294 overflow = true;
12295 break;
12296 }
12297 base = read_address (obfd, buffer, cu, &bytes_read);
12298 found_base = 1;
12299 buffer += bytes_read;
12300 break;
12301 case DW_RLE_start_length:
12302 if (buffer + cu->header.addr_size > buf_end)
12303 {
12304 overflow = true;
12305 break;
12306 }
12307 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12308 buffer += bytes_read;
12309 range_end = (range_beginning
12310 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12311 buffer += bytes_read;
12312 if (buffer > buf_end)
12313 {
12314 overflow = true;
12315 break;
12316 }
12317 break;
12318 case DW_RLE_offset_pair:
12319 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12320 buffer += bytes_read;
12321 if (buffer > buf_end)
12322 {
12323 overflow = true;
12324 break;
12325 }
12326 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12327 buffer += bytes_read;
12328 if (buffer > buf_end)
12329 {
12330 overflow = true;
12331 break;
12332 }
12333 break;
12334 case DW_RLE_start_end:
12335 if (buffer + 2 * cu->header.addr_size > buf_end)
12336 {
12337 overflow = true;
12338 break;
12339 }
12340 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12341 buffer += bytes_read;
12342 range_end = read_address (obfd, buffer, cu, &bytes_read);
12343 buffer += bytes_read;
12344 break;
12345 default:
12346 complaint (&symfile_complaints,
12347 _("Invalid .debug_rnglists data (no base address)"));
12348 return false;
12349 }
12350 if (rlet == DW_RLE_end_of_list || overflow)
12351 break;
12352 if (rlet == DW_RLE_base_address)
12353 continue;
12354
12355 if (!found_base)
12356 {
12357 /* We have no valid base address for the ranges
12358 data. */
12359 complaint (&symfile_complaints,
12360 _("Invalid .debug_rnglists data (no base address)"));
12361 return false;
12362 }
12363
12364 if (range_beginning > range_end)
12365 {
12366 /* Inverted range entries are invalid. */
12367 complaint (&symfile_complaints,
12368 _("Invalid .debug_rnglists data (inverted range)"));
12369 return false;
12370 }
12371
12372 /* Empty range entries have no effect. */
12373 if (range_beginning == range_end)
12374 continue;
12375
12376 range_beginning += base;
12377 range_end += base;
12378
12379 /* A not-uncommon case of bad debug info.
12380 Don't pollute the addrmap with bad data. */
12381 if (range_beginning + baseaddr == 0
12382 && !dwarf2_per_objfile->has_section_at_zero)
12383 {
12384 complaint (&symfile_complaints,
12385 _(".debug_rnglists entry has start address of zero"
12386 " [in module %s]"), objfile_name (objfile));
12387 continue;
12388 }
12389
12390 callback (range_beginning, range_end);
12391 }
12392
12393 if (overflow)
12394 {
12395 complaint (&symfile_complaints,
12396 _("Offset %d is not terminated "
12397 "for DW_AT_ranges attribute"),
12398 offset);
12399 return false;
12400 }
12401
12402 return true;
12403}
12404
12405/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12406 Callback's type should be:
12407 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12408 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12409
43988095 12410template <typename Callback>
43039443 12411static int
5f46c5a5 12412dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12413 Callback &&callback)
43039443
JK
12414{
12415 struct objfile *objfile = cu->objfile;
3e29f34a 12416 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12417 struct comp_unit_head *cu_header = &cu->header;
12418 bfd *obfd = objfile->obfd;
12419 unsigned int addr_size = cu_header->addr_size;
12420 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12421 /* Base address selection entry. */
12422 CORE_ADDR base;
12423 int found_base;
12424 unsigned int dummy;
d521ce57 12425 const gdb_byte *buffer;
ff013f42 12426 CORE_ADDR baseaddr;
43039443 12427
43988095
JK
12428 if (cu_header->version >= 5)
12429 return dwarf2_rnglists_process (offset, cu, callback);
12430
d00adf39
DE
12431 found_base = cu->base_known;
12432 base = cu->base_address;
43039443 12433
be391dca 12434 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12435 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12436 {
12437 complaint (&symfile_complaints,
12438 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12439 offset);
12440 return 0;
12441 }
dce234bc 12442 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12443
e7030f15 12444 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12445
43039443
JK
12446 while (1)
12447 {
12448 CORE_ADDR range_beginning, range_end;
12449
12450 range_beginning = read_address (obfd, buffer, cu, &dummy);
12451 buffer += addr_size;
12452 range_end = read_address (obfd, buffer, cu, &dummy);
12453 buffer += addr_size;
12454 offset += 2 * addr_size;
12455
12456 /* An end of list marker is a pair of zero addresses. */
12457 if (range_beginning == 0 && range_end == 0)
12458 /* Found the end of list entry. */
12459 break;
12460
12461 /* Each base address selection entry is a pair of 2 values.
12462 The first is the largest possible address, the second is
12463 the base address. Check for a base address here. */
12464 if ((range_beginning & mask) == mask)
12465 {
28d2bfb9
AB
12466 /* If we found the largest possible address, then we already
12467 have the base address in range_end. */
12468 base = range_end;
43039443
JK
12469 found_base = 1;
12470 continue;
12471 }
12472
12473 if (!found_base)
12474 {
12475 /* We have no valid base address for the ranges
12476 data. */
12477 complaint (&symfile_complaints,
12478 _("Invalid .debug_ranges data (no base address)"));
12479 return 0;
12480 }
12481
9277c30c
UW
12482 if (range_beginning > range_end)
12483 {
12484 /* Inverted range entries are invalid. */
12485 complaint (&symfile_complaints,
12486 _("Invalid .debug_ranges data (inverted range)"));
12487 return 0;
12488 }
12489
12490 /* Empty range entries have no effect. */
12491 if (range_beginning == range_end)
12492 continue;
12493
43039443
JK
12494 range_beginning += base;
12495 range_end += base;
12496
01093045
DE
12497 /* A not-uncommon case of bad debug info.
12498 Don't pollute the addrmap with bad data. */
12499 if (range_beginning + baseaddr == 0
12500 && !dwarf2_per_objfile->has_section_at_zero)
12501 {
12502 complaint (&symfile_complaints,
12503 _(".debug_ranges entry has start address of zero"
4262abfb 12504 " [in module %s]"), objfile_name (objfile));
01093045
DE
12505 continue;
12506 }
12507
5f46c5a5
JK
12508 callback (range_beginning, range_end);
12509 }
12510
12511 return 1;
12512}
12513
12514/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12515 Return 1 if the attributes are present and valid, otherwise, return 0.
12516 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12517
12518static int
12519dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12520 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12521 struct partial_symtab *ranges_pst)
12522{
12523 struct objfile *objfile = cu->objfile;
12524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12525 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12526 SECT_OFF_TEXT (objfile));
12527 int low_set = 0;
12528 CORE_ADDR low = 0;
12529 CORE_ADDR high = 0;
12530 int retval;
12531
12532 retval = dwarf2_ranges_process (offset, cu,
12533 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12534 {
9277c30c 12535 if (ranges_pst != NULL)
3e29f34a
MR
12536 {
12537 CORE_ADDR lowpc;
12538 CORE_ADDR highpc;
12539
12540 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12541 range_beginning + baseaddr);
12542 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12543 range_end + baseaddr);
12544 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12545 ranges_pst);
12546 }
ff013f42 12547
43039443
JK
12548 /* FIXME: This is recording everything as a low-high
12549 segment of consecutive addresses. We should have a
12550 data structure for discontiguous block ranges
12551 instead. */
12552 if (! low_set)
12553 {
12554 low = range_beginning;
12555 high = range_end;
12556 low_set = 1;
12557 }
12558 else
12559 {
12560 if (range_beginning < low)
12561 low = range_beginning;
12562 if (range_end > high)
12563 high = range_end;
12564 }
5f46c5a5
JK
12565 });
12566 if (!retval)
12567 return 0;
43039443
JK
12568
12569 if (! low_set)
12570 /* If the first entry is an end-of-list marker, the range
12571 describes an empty scope, i.e. no instructions. */
12572 return 0;
12573
12574 if (low_return)
12575 *low_return = low;
12576 if (high_return)
12577 *high_return = high;
12578 return 1;
12579}
12580
3a2b436a
JK
12581/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12582 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12583 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12584
3a2b436a 12585static enum pc_bounds_kind
af34e669 12586dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12587 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12588 struct partial_symtab *pst)
c906108c
SS
12589{
12590 struct attribute *attr;
91da1414 12591 struct attribute *attr_high;
af34e669
DJ
12592 CORE_ADDR low = 0;
12593 CORE_ADDR high = 0;
e385593e 12594 enum pc_bounds_kind ret;
c906108c 12595
91da1414
MW
12596 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12597 if (attr_high)
af34e669 12598 {
e142c38c 12599 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12600 if (attr)
91da1414 12601 {
31aa7e4e
JB
12602 low = attr_value_as_address (attr);
12603 high = attr_value_as_address (attr_high);
12604 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12605 high += low;
91da1414 12606 }
af34e669
DJ
12607 else
12608 /* Found high w/o low attribute. */
e385593e 12609 return PC_BOUNDS_INVALID;
af34e669
DJ
12610
12611 /* Found consecutive range of addresses. */
3a2b436a 12612 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12613 }
c906108c 12614 else
af34e669 12615 {
e142c38c 12616 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12617 if (attr != NULL)
12618 {
ab435259
DE
12619 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12620 We take advantage of the fact that DW_AT_ranges does not appear
12621 in DW_TAG_compile_unit of DWO files. */
12622 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12623 unsigned int ranges_offset = (DW_UNSND (attr)
12624 + (need_ranges_base
12625 ? cu->ranges_base
12626 : 0));
2e3cf129 12627
af34e669 12628 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12629 .debug_ranges section. */
2e3cf129 12630 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12631 return PC_BOUNDS_INVALID;
43039443 12632 /* Found discontinuous range of addresses. */
3a2b436a 12633 ret = PC_BOUNDS_RANGES;
af34e669 12634 }
e385593e
JK
12635 else
12636 return PC_BOUNDS_NOT_PRESENT;
af34e669 12637 }
c906108c 12638
9373cf26
JK
12639 /* read_partial_die has also the strict LOW < HIGH requirement. */
12640 if (high <= low)
e385593e 12641 return PC_BOUNDS_INVALID;
c906108c
SS
12642
12643 /* When using the GNU linker, .gnu.linkonce. sections are used to
12644 eliminate duplicate copies of functions and vtables and such.
12645 The linker will arbitrarily choose one and discard the others.
12646 The AT_*_pc values for such functions refer to local labels in
12647 these sections. If the section from that file was discarded, the
12648 labels are not in the output, so the relocs get a value of 0.
12649 If this is a discarded function, mark the pc bounds as invalid,
12650 so that GDB will ignore it. */
72dca2f5 12651 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12652 return PC_BOUNDS_INVALID;
c906108c
SS
12653
12654 *lowpc = low;
96408a79
SA
12655 if (highpc)
12656 *highpc = high;
af34e669 12657 return ret;
c906108c
SS
12658}
12659
b084d499
JB
12660/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12661 its low and high PC addresses. Do nothing if these addresses could not
12662 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12663 and HIGHPC to the high address if greater than HIGHPC. */
12664
12665static void
12666dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12667 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12668 struct dwarf2_cu *cu)
12669{
12670 CORE_ADDR low, high;
12671 struct die_info *child = die->child;
12672
e385593e 12673 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12674 {
325fac50
PA
12675 *lowpc = std::min (*lowpc, low);
12676 *highpc = std::max (*highpc, high);
b084d499
JB
12677 }
12678
12679 /* If the language does not allow nested subprograms (either inside
12680 subprograms or lexical blocks), we're done. */
12681 if (cu->language != language_ada)
12682 return;
6e70227d 12683
b084d499
JB
12684 /* Check all the children of the given DIE. If it contains nested
12685 subprograms, then check their pc bounds. Likewise, we need to
12686 check lexical blocks as well, as they may also contain subprogram
12687 definitions. */
12688 while (child && child->tag)
12689 {
12690 if (child->tag == DW_TAG_subprogram
12691 || child->tag == DW_TAG_lexical_block)
12692 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12693 child = sibling_die (child);
12694 }
12695}
12696
fae299cd
DC
12697/* Get the low and high pc's represented by the scope DIE, and store
12698 them in *LOWPC and *HIGHPC. If the correct values can't be
12699 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12700
12701static void
12702get_scope_pc_bounds (struct die_info *die,
12703 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12704 struct dwarf2_cu *cu)
12705{
12706 CORE_ADDR best_low = (CORE_ADDR) -1;
12707 CORE_ADDR best_high = (CORE_ADDR) 0;
12708 CORE_ADDR current_low, current_high;
12709
3a2b436a 12710 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12711 >= PC_BOUNDS_RANGES)
fae299cd
DC
12712 {
12713 best_low = current_low;
12714 best_high = current_high;
12715 }
12716 else
12717 {
12718 struct die_info *child = die->child;
12719
12720 while (child && child->tag)
12721 {
12722 switch (child->tag) {
12723 case DW_TAG_subprogram:
b084d499 12724 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12725 break;
12726 case DW_TAG_namespace:
f55ee35c 12727 case DW_TAG_module:
fae299cd
DC
12728 /* FIXME: carlton/2004-01-16: Should we do this for
12729 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12730 that current GCC's always emit the DIEs corresponding
12731 to definitions of methods of classes as children of a
12732 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12733 the DIEs giving the declarations, which could be
12734 anywhere). But I don't see any reason why the
12735 standards says that they have to be there. */
12736 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12737
12738 if (current_low != ((CORE_ADDR) -1))
12739 {
325fac50
PA
12740 best_low = std::min (best_low, current_low);
12741 best_high = std::max (best_high, current_high);
fae299cd
DC
12742 }
12743 break;
12744 default:
0963b4bd 12745 /* Ignore. */
fae299cd
DC
12746 break;
12747 }
12748
12749 child = sibling_die (child);
12750 }
12751 }
12752
12753 *lowpc = best_low;
12754 *highpc = best_high;
12755}
12756
801e3a5b
JB
12757/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12758 in DIE. */
380bca97 12759
801e3a5b
JB
12760static void
12761dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12762 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12763{
bb5ed363 12764 struct objfile *objfile = cu->objfile;
3e29f34a 12765 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12766 struct attribute *attr;
91da1414 12767 struct attribute *attr_high;
801e3a5b 12768
91da1414
MW
12769 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12770 if (attr_high)
801e3a5b 12771 {
801e3a5b
JB
12772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12773 if (attr)
12774 {
31aa7e4e
JB
12775 CORE_ADDR low = attr_value_as_address (attr);
12776 CORE_ADDR high = attr_value_as_address (attr_high);
12777
12778 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12779 high += low;
9a619af0 12780
3e29f34a
MR
12781 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12782 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12783 record_block_range (block, low, high - 1);
801e3a5b
JB
12784 }
12785 }
12786
12787 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12788 if (attr)
12789 {
bb5ed363 12790 bfd *obfd = objfile->obfd;
ab435259
DE
12791 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12792 We take advantage of the fact that DW_AT_ranges does not appear
12793 in DW_TAG_compile_unit of DWO files. */
12794 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12795
12796 /* The value of the DW_AT_ranges attribute is the offset of the
12797 address range list in the .debug_ranges section. */
ab435259
DE
12798 unsigned long offset = (DW_UNSND (attr)
12799 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12800 const gdb_byte *buffer;
801e3a5b
JB
12801
12802 /* For some target architectures, but not others, the
12803 read_address function sign-extends the addresses it returns.
12804 To recognize base address selection entries, we need a
12805 mask. */
12806 unsigned int addr_size = cu->header.addr_size;
12807 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12808
12809 /* The base address, to which the next pair is relative. Note
12810 that this 'base' is a DWARF concept: most entries in a range
12811 list are relative, to reduce the number of relocs against the
12812 debugging information. This is separate from this function's
12813 'baseaddr' argument, which GDB uses to relocate debugging
12814 information from a shared library based on the address at
12815 which the library was loaded. */
d00adf39
DE
12816 CORE_ADDR base = cu->base_address;
12817 int base_known = cu->base_known;
801e3a5b 12818
5f46c5a5
JK
12819 dwarf2_ranges_process (offset, cu,
12820 [&] (CORE_ADDR start, CORE_ADDR end)
12821 {
58fdfd2c
JK
12822 start += baseaddr;
12823 end += baseaddr;
5f46c5a5
JK
12824 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12825 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12826 record_block_range (block, start, end - 1);
12827 });
801e3a5b
JB
12828 }
12829}
12830
685b1105
JK
12831/* Check whether the producer field indicates either of GCC < 4.6, or the
12832 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12833
685b1105
JK
12834static void
12835check_producer (struct dwarf2_cu *cu)
60d5a603 12836{
38360086 12837 int major, minor;
60d5a603
JK
12838
12839 if (cu->producer == NULL)
12840 {
12841 /* For unknown compilers expect their behavior is DWARF version
12842 compliant.
12843
12844 GCC started to support .debug_types sections by -gdwarf-4 since
12845 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12846 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12847 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12848 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12849 }
b1ffba5a 12850 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12851 {
38360086
MW
12852 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12853 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12854 }
61012eef 12855 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12856 cu->producer_is_icc = 1;
12857 else
12858 {
12859 /* For other non-GCC compilers, expect their behavior is DWARF version
12860 compliant. */
60d5a603
JK
12861 }
12862
ba919b58 12863 cu->checked_producer = 1;
685b1105 12864}
ba919b58 12865
685b1105
JK
12866/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12867 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12868 during 4.6.0 experimental. */
12869
12870static int
12871producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12872{
12873 if (!cu->checked_producer)
12874 check_producer (cu);
12875
12876 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12877}
12878
12879/* Return the default accessibility type if it is not overriden by
12880 DW_AT_accessibility. */
12881
12882static enum dwarf_access_attribute
12883dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12884{
12885 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12886 {
12887 /* The default DWARF 2 accessibility for members is public, the default
12888 accessibility for inheritance is private. */
12889
12890 if (die->tag != DW_TAG_inheritance)
12891 return DW_ACCESS_public;
12892 else
12893 return DW_ACCESS_private;
12894 }
12895 else
12896 {
12897 /* DWARF 3+ defines the default accessibility a different way. The same
12898 rules apply now for DW_TAG_inheritance as for the members and it only
12899 depends on the container kind. */
12900
12901 if (die->parent->tag == DW_TAG_class_type)
12902 return DW_ACCESS_private;
12903 else
12904 return DW_ACCESS_public;
12905 }
12906}
12907
74ac6d43
TT
12908/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12909 offset. If the attribute was not found return 0, otherwise return
12910 1. If it was found but could not properly be handled, set *OFFSET
12911 to 0. */
12912
12913static int
12914handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12915 LONGEST *offset)
12916{
12917 struct attribute *attr;
12918
12919 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12920 if (attr != NULL)
12921 {
12922 *offset = 0;
12923
12924 /* Note that we do not check for a section offset first here.
12925 This is because DW_AT_data_member_location is new in DWARF 4,
12926 so if we see it, we can assume that a constant form is really
12927 a constant and not a section offset. */
12928 if (attr_form_is_constant (attr))
12929 *offset = dwarf2_get_attr_constant_value (attr, 0);
12930 else if (attr_form_is_section_offset (attr))
12931 dwarf2_complex_location_expr_complaint ();
12932 else if (attr_form_is_block (attr))
12933 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12934 else
12935 dwarf2_complex_location_expr_complaint ();
12936
12937 return 1;
12938 }
12939
12940 return 0;
12941}
12942
c906108c
SS
12943/* Add an aggregate field to the field list. */
12944
12945static void
107d2387 12946dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12947 struct dwarf2_cu *cu)
6e70227d 12948{
e7c27a73 12949 struct objfile *objfile = cu->objfile;
5e2b427d 12950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12951 struct nextfield *new_field;
12952 struct attribute *attr;
12953 struct field *fp;
15d034d0 12954 const char *fieldname = "";
c906108c
SS
12955
12956 /* Allocate a new field list entry and link it in. */
8d749320 12957 new_field = XNEW (struct nextfield);
b8c9b27d 12958 make_cleanup (xfree, new_field);
c906108c 12959 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12960
12961 if (die->tag == DW_TAG_inheritance)
12962 {
12963 new_field->next = fip->baseclasses;
12964 fip->baseclasses = new_field;
12965 }
12966 else
12967 {
12968 new_field->next = fip->fields;
12969 fip->fields = new_field;
12970 }
c906108c
SS
12971 fip->nfields++;
12972
e142c38c 12973 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12974 if (attr)
12975 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12976 else
12977 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12978 if (new_field->accessibility != DW_ACCESS_public)
12979 fip->non_public_fields = 1;
60d5a603 12980
e142c38c 12981 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12982 if (attr)
12983 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12984 else
12985 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12986
12987 fp = &new_field->field;
a9a9bd0f 12988
e142c38c 12989 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12990 {
74ac6d43
TT
12991 LONGEST offset;
12992
a9a9bd0f 12993 /* Data member other than a C++ static data member. */
6e70227d 12994
c906108c 12995 /* Get type of field. */
e7c27a73 12996 fp->type = die_type (die, cu);
c906108c 12997
d6a843b5 12998 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12999
c906108c 13000 /* Get bit size of field (zero if none). */
e142c38c 13001 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13002 if (attr)
13003 {
13004 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13005 }
13006 else
13007 {
13008 FIELD_BITSIZE (*fp) = 0;
13009 }
13010
13011 /* Get bit offset of field. */
74ac6d43
TT
13012 if (handle_data_member_location (die, cu, &offset))
13013 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13014 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13015 if (attr)
13016 {
5e2b427d 13017 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13018 {
13019 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13020 additional bit offset from the MSB of the containing
13021 anonymous object to the MSB of the field. We don't
13022 have to do anything special since we don't need to
13023 know the size of the anonymous object. */
f41f5e61 13024 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13025 }
13026 else
13027 {
13028 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13029 MSB of the anonymous object, subtract off the number of
13030 bits from the MSB of the field to the MSB of the
13031 object, and then subtract off the number of bits of
13032 the field itself. The result is the bit offset of
13033 the LSB of the field. */
c906108c
SS
13034 int anonymous_size;
13035 int bit_offset = DW_UNSND (attr);
13036
e142c38c 13037 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13038 if (attr)
13039 {
13040 /* The size of the anonymous object containing
13041 the bit field is explicit, so use the
13042 indicated size (in bytes). */
13043 anonymous_size = DW_UNSND (attr);
13044 }
13045 else
13046 {
13047 /* The size of the anonymous object containing
13048 the bit field must be inferred from the type
13049 attribute of the data member containing the
13050 bit field. */
13051 anonymous_size = TYPE_LENGTH (fp->type);
13052 }
f41f5e61
PA
13053 SET_FIELD_BITPOS (*fp,
13054 (FIELD_BITPOS (*fp)
13055 + anonymous_size * bits_per_byte
13056 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13057 }
13058 }
da5b30da
AA
13059 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13060 if (attr != NULL)
13061 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13062 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13063
13064 /* Get name of field. */
39cbfefa
DJ
13065 fieldname = dwarf2_name (die, cu);
13066 if (fieldname == NULL)
13067 fieldname = "";
d8151005
DJ
13068
13069 /* The name is already allocated along with this objfile, so we don't
13070 need to duplicate it for the type. */
13071 fp->name = fieldname;
c906108c
SS
13072
13073 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13074 pointer or virtual base class pointer) to private. */
e142c38c 13075 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13076 {
d48cc9dd 13077 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13078 new_field->accessibility = DW_ACCESS_private;
13079 fip->non_public_fields = 1;
13080 }
13081 }
a9a9bd0f 13082 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13083 {
a9a9bd0f
DC
13084 /* C++ static member. */
13085
13086 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13087 is a declaration, but all versions of G++ as of this writing
13088 (so through at least 3.2.1) incorrectly generate
13089 DW_TAG_variable tags. */
6e70227d 13090
ff355380 13091 const char *physname;
c906108c 13092
a9a9bd0f 13093 /* Get name of field. */
39cbfefa
DJ
13094 fieldname = dwarf2_name (die, cu);
13095 if (fieldname == NULL)
c906108c
SS
13096 return;
13097
254e6b9e 13098 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13099 if (attr
13100 /* Only create a symbol if this is an external value.
13101 new_symbol checks this and puts the value in the global symbol
13102 table, which we want. If it is not external, new_symbol
13103 will try to put the value in cu->list_in_scope which is wrong. */
13104 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13105 {
13106 /* A static const member, not much different than an enum as far as
13107 we're concerned, except that we can support more types. */
13108 new_symbol (die, NULL, cu);
13109 }
13110
2df3850c 13111 /* Get physical name. */
ff355380 13112 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13113
d8151005
DJ
13114 /* The name is already allocated along with this objfile, so we don't
13115 need to duplicate it for the type. */
13116 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13117 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13118 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13119 }
13120 else if (die->tag == DW_TAG_inheritance)
13121 {
74ac6d43 13122 LONGEST offset;
d4b96c9a 13123
74ac6d43
TT
13124 /* C++ base class field. */
13125 if (handle_data_member_location (die, cu, &offset))
13126 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13127 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13128 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13129 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13130 fip->nbaseclasses++;
13131 }
13132}
13133
98751a41
JK
13134/* Add a typedef defined in the scope of the FIP's class. */
13135
13136static void
13137dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13138 struct dwarf2_cu *cu)
6e70227d 13139{
98751a41 13140 struct typedef_field_list *new_field;
98751a41 13141 struct typedef_field *fp;
98751a41
JK
13142
13143 /* Allocate a new field list entry and link it in. */
8d749320 13144 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13145 make_cleanup (xfree, new_field);
13146
13147 gdb_assert (die->tag == DW_TAG_typedef);
13148
13149 fp = &new_field->field;
13150
13151 /* Get name of field. */
13152 fp->name = dwarf2_name (die, cu);
13153 if (fp->name == NULL)
13154 return;
13155
13156 fp->type = read_type_die (die, cu);
13157
13158 new_field->next = fip->typedef_field_list;
13159 fip->typedef_field_list = new_field;
13160 fip->typedef_field_list_count++;
13161}
13162
c906108c
SS
13163/* Create the vector of fields, and attach it to the type. */
13164
13165static void
fba45db2 13166dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13167 struct dwarf2_cu *cu)
c906108c
SS
13168{
13169 int nfields = fip->nfields;
13170
13171 /* Record the field count, allocate space for the array of fields,
13172 and create blank accessibility bitfields if necessary. */
13173 TYPE_NFIELDS (type) = nfields;
13174 TYPE_FIELDS (type) = (struct field *)
13175 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13176 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13177
b4ba55a1 13178 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13179 {
13180 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13181
13182 TYPE_FIELD_PRIVATE_BITS (type) =
13183 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13184 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13185
13186 TYPE_FIELD_PROTECTED_BITS (type) =
13187 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13188 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13189
774b6a14
TT
13190 TYPE_FIELD_IGNORE_BITS (type) =
13191 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13192 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13193 }
13194
13195 /* If the type has baseclasses, allocate and clear a bit vector for
13196 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13197 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13198 {
13199 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13200 unsigned char *pointer;
c906108c
SS
13201
13202 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13203 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13204 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13205 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13206 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13207 }
13208
3e43a32a
MS
13209 /* Copy the saved-up fields into the field vector. Start from the head of
13210 the list, adding to the tail of the field array, so that they end up in
13211 the same order in the array in which they were added to the list. */
c906108c
SS
13212 while (nfields-- > 0)
13213 {
7d0ccb61
DJ
13214 struct nextfield *fieldp;
13215
13216 if (fip->fields)
13217 {
13218 fieldp = fip->fields;
13219 fip->fields = fieldp->next;
13220 }
13221 else
13222 {
13223 fieldp = fip->baseclasses;
13224 fip->baseclasses = fieldp->next;
13225 }
13226
13227 TYPE_FIELD (type, nfields) = fieldp->field;
13228 switch (fieldp->accessibility)
c906108c 13229 {
c5aa993b 13230 case DW_ACCESS_private:
b4ba55a1
JB
13231 if (cu->language != language_ada)
13232 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13233 break;
c906108c 13234
c5aa993b 13235 case DW_ACCESS_protected:
b4ba55a1
JB
13236 if (cu->language != language_ada)
13237 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13238 break;
c906108c 13239
c5aa993b
JM
13240 case DW_ACCESS_public:
13241 break;
c906108c 13242
c5aa993b
JM
13243 default:
13244 /* Unknown accessibility. Complain and treat it as public. */
13245 {
e2e0b3e5 13246 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13247 fieldp->accessibility);
c5aa993b
JM
13248 }
13249 break;
c906108c
SS
13250 }
13251 if (nfields < fip->nbaseclasses)
13252 {
7d0ccb61 13253 switch (fieldp->virtuality)
c906108c 13254 {
c5aa993b
JM
13255 case DW_VIRTUALITY_virtual:
13256 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13257 if (cu->language == language_ada)
a73c6dcd 13258 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13259 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13260 break;
c906108c
SS
13261 }
13262 }
c906108c
SS
13263 }
13264}
13265
7d27a96d
TT
13266/* Return true if this member function is a constructor, false
13267 otherwise. */
13268
13269static int
13270dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13271{
13272 const char *fieldname;
fe978cb0 13273 const char *type_name;
7d27a96d
TT
13274 int len;
13275
13276 if (die->parent == NULL)
13277 return 0;
13278
13279 if (die->parent->tag != DW_TAG_structure_type
13280 && die->parent->tag != DW_TAG_union_type
13281 && die->parent->tag != DW_TAG_class_type)
13282 return 0;
13283
13284 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13285 type_name = dwarf2_name (die->parent, cu);
13286 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13287 return 0;
13288
13289 len = strlen (fieldname);
fe978cb0
PA
13290 return (strncmp (fieldname, type_name, len) == 0
13291 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13292}
13293
c906108c
SS
13294/* Add a member function to the proper fieldlist. */
13295
13296static void
107d2387 13297dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13298 struct type *type, struct dwarf2_cu *cu)
c906108c 13299{
e7c27a73 13300 struct objfile *objfile = cu->objfile;
c906108c
SS
13301 struct attribute *attr;
13302 struct fnfieldlist *flp;
13303 int i;
13304 struct fn_field *fnp;
15d034d0 13305 const char *fieldname;
c906108c 13306 struct nextfnfield *new_fnfield;
f792889a 13307 struct type *this_type;
60d5a603 13308 enum dwarf_access_attribute accessibility;
c906108c 13309
b4ba55a1 13310 if (cu->language == language_ada)
a73c6dcd 13311 error (_("unexpected member function in Ada type"));
b4ba55a1 13312
2df3850c 13313 /* Get name of member function. */
39cbfefa
DJ
13314 fieldname = dwarf2_name (die, cu);
13315 if (fieldname == NULL)
2df3850c 13316 return;
c906108c 13317
c906108c
SS
13318 /* Look up member function name in fieldlist. */
13319 for (i = 0; i < fip->nfnfields; i++)
13320 {
27bfe10e 13321 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13322 break;
13323 }
13324
13325 /* Create new list element if necessary. */
13326 if (i < fip->nfnfields)
13327 flp = &fip->fnfieldlists[i];
13328 else
13329 {
13330 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13331 {
13332 fip->fnfieldlists = (struct fnfieldlist *)
13333 xrealloc (fip->fnfieldlists,
13334 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13335 * sizeof (struct fnfieldlist));
c906108c 13336 if (fip->nfnfields == 0)
c13c43fd 13337 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13338 }
13339 flp = &fip->fnfieldlists[fip->nfnfields];
13340 flp->name = fieldname;
13341 flp->length = 0;
13342 flp->head = NULL;
3da10d80 13343 i = fip->nfnfields++;
c906108c
SS
13344 }
13345
13346 /* Create a new member function field and chain it to the field list
0963b4bd 13347 entry. */
8d749320 13348 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13349 make_cleanup (xfree, new_fnfield);
c906108c
SS
13350 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13351 new_fnfield->next = flp->head;
13352 flp->head = new_fnfield;
13353 flp->length++;
13354
13355 /* Fill in the member function field info. */
13356 fnp = &new_fnfield->fnfield;
3da10d80
KS
13357
13358 /* Delay processing of the physname until later. */
9c37b5ae 13359 if (cu->language == language_cplus)
3da10d80
KS
13360 {
13361 add_to_method_list (type, i, flp->length - 1, fieldname,
13362 die, cu);
13363 }
13364 else
13365 {
1d06ead6 13366 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13367 fnp->physname = physname ? physname : "";
13368 }
13369
c906108c 13370 fnp->type = alloc_type (objfile);
f792889a
DJ
13371 this_type = read_type_die (die, cu);
13372 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13373 {
f792889a 13374 int nparams = TYPE_NFIELDS (this_type);
c906108c 13375
f792889a 13376 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13377 of the method itself (TYPE_CODE_METHOD). */
13378 smash_to_method_type (fnp->type, type,
f792889a
DJ
13379 TYPE_TARGET_TYPE (this_type),
13380 TYPE_FIELDS (this_type),
13381 TYPE_NFIELDS (this_type),
13382 TYPE_VARARGS (this_type));
c906108c
SS
13383
13384 /* Handle static member functions.
c5aa993b 13385 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13386 member functions. G++ helps GDB by marking the first
13387 parameter for non-static member functions (which is the this
13388 pointer) as artificial. We obtain this information from
13389 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13390 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13391 fnp->voffset = VOFFSET_STATIC;
13392 }
13393 else
e2e0b3e5 13394 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13395 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13396
13397 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13398 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13399 fnp->fcontext = die_containing_type (die, cu);
c906108c 13400
3e43a32a
MS
13401 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13402 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13403
13404 /* Get accessibility. */
e142c38c 13405 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13406 if (attr)
aead7601 13407 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13408 else
13409 accessibility = dwarf2_default_access_attribute (die, cu);
13410 switch (accessibility)
c906108c 13411 {
60d5a603
JK
13412 case DW_ACCESS_private:
13413 fnp->is_private = 1;
13414 break;
13415 case DW_ACCESS_protected:
13416 fnp->is_protected = 1;
13417 break;
c906108c
SS
13418 }
13419
b02dede2 13420 /* Check for artificial methods. */
e142c38c 13421 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13422 if (attr && DW_UNSND (attr) != 0)
13423 fnp->is_artificial = 1;
13424
7d27a96d
TT
13425 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13426
0d564a31 13427 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13428 function. For older versions of GCC, this is an offset in the
13429 appropriate virtual table, as specified by DW_AT_containing_type.
13430 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13431 to the object address. */
13432
e142c38c 13433 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13434 if (attr)
8e19ed76 13435 {
aec5aa8b 13436 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13437 {
aec5aa8b
TT
13438 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13439 {
13440 /* Old-style GCC. */
13441 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13442 }
13443 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13444 || (DW_BLOCK (attr)->size > 1
13445 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13446 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13447 {
aec5aa8b
TT
13448 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13449 if ((fnp->voffset % cu->header.addr_size) != 0)
13450 dwarf2_complex_location_expr_complaint ();
13451 else
13452 fnp->voffset /= cu->header.addr_size;
13453 fnp->voffset += 2;
13454 }
13455 else
13456 dwarf2_complex_location_expr_complaint ();
13457
13458 if (!fnp->fcontext)
7e993ebf
KS
13459 {
13460 /* If there is no `this' field and no DW_AT_containing_type,
13461 we cannot actually find a base class context for the
13462 vtable! */
13463 if (TYPE_NFIELDS (this_type) == 0
13464 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13465 {
13466 complaint (&symfile_complaints,
13467 _("cannot determine context for virtual member "
13468 "function \"%s\" (offset %d)"),
9c541725 13469 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13470 }
13471 else
13472 {
13473 fnp->fcontext
13474 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13475 }
13476 }
aec5aa8b 13477 }
3690dd37 13478 else if (attr_form_is_section_offset (attr))
8e19ed76 13479 {
4d3c2250 13480 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13481 }
13482 else
13483 {
4d3c2250
KB
13484 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13485 fieldname);
8e19ed76 13486 }
0d564a31 13487 }
d48cc9dd
DJ
13488 else
13489 {
13490 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13491 if (attr && DW_UNSND (attr))
13492 {
13493 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13494 complaint (&symfile_complaints,
3e43a32a
MS
13495 _("Member function \"%s\" (offset %d) is virtual "
13496 "but the vtable offset is not specified"),
9c541725 13497 fieldname, to_underlying (die->sect_off));
9655fd1a 13498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13499 TYPE_CPLUS_DYNAMIC (type) = 1;
13500 }
13501 }
c906108c
SS
13502}
13503
13504/* Create the vector of member function fields, and attach it to the type. */
13505
13506static void
fba45db2 13507dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13508 struct dwarf2_cu *cu)
c906108c
SS
13509{
13510 struct fnfieldlist *flp;
c906108c
SS
13511 int i;
13512
b4ba55a1 13513 if (cu->language == language_ada)
a73c6dcd 13514 error (_("unexpected member functions in Ada type"));
b4ba55a1 13515
c906108c
SS
13516 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13517 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13518 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13519
13520 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13521 {
13522 struct nextfnfield *nfp = flp->head;
13523 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13524 int k;
13525
13526 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13527 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13528 fn_flp->fn_fields = (struct fn_field *)
13529 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13530 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13531 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13532 }
13533
13534 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13535}
13536
1168df01
JB
13537/* Returns non-zero if NAME is the name of a vtable member in CU's
13538 language, zero otherwise. */
13539static int
13540is_vtable_name (const char *name, struct dwarf2_cu *cu)
13541{
13542 static const char vptr[] = "_vptr";
987504bb 13543 static const char vtable[] = "vtable";
1168df01 13544
9c37b5ae
TT
13545 /* Look for the C++ form of the vtable. */
13546 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13547 return 1;
13548
13549 return 0;
13550}
13551
c0dd20ea 13552/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13553 functions, with the ABI-specified layout. If TYPE describes
13554 such a structure, smash it into a member function type.
61049d3b
DJ
13555
13556 GCC shouldn't do this; it should just output pointer to member DIEs.
13557 This is GCC PR debug/28767. */
c0dd20ea 13558
0b92b5bb
TT
13559static void
13560quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13561{
09e2d7c7 13562 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13563
13564 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13565 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13566 return;
c0dd20ea
DJ
13567
13568 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13569 if (TYPE_FIELD_NAME (type, 0) == NULL
13570 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13571 || TYPE_FIELD_NAME (type, 1) == NULL
13572 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13573 return;
c0dd20ea
DJ
13574
13575 /* Find the type of the method. */
0b92b5bb 13576 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13577 if (pfn_type == NULL
13578 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13579 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13580 return;
c0dd20ea
DJ
13581
13582 /* Look for the "this" argument. */
13583 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13584 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13585 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13586 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13587 return;
c0dd20ea 13588
09e2d7c7 13589 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13590 new_type = alloc_type (objfile);
09e2d7c7 13591 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13592 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13593 TYPE_VARARGS (pfn_type));
0b92b5bb 13594 smash_to_methodptr_type (type, new_type);
c0dd20ea 13595}
1168df01 13596
685b1105
JK
13597/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13598 (icc). */
13599
13600static int
13601producer_is_icc (struct dwarf2_cu *cu)
13602{
13603 if (!cu->checked_producer)
13604 check_producer (cu);
13605
13606 return cu->producer_is_icc;
13607}
13608
c906108c 13609/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13610 (definition) to create a type for the structure or union. Fill in
13611 the type's name and general properties; the members will not be
83655187
DE
13612 processed until process_structure_scope. A symbol table entry for
13613 the type will also not be done until process_structure_scope (assuming
13614 the type has a name).
c906108c 13615
c767944b
DJ
13616 NOTE: we need to call these functions regardless of whether or not the
13617 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13618 structure or union. This gets the type entered into our set of
83655187 13619 user defined types. */
c906108c 13620
f792889a 13621static struct type *
134d01f1 13622read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13623{
e7c27a73 13624 struct objfile *objfile = cu->objfile;
c906108c
SS
13625 struct type *type;
13626 struct attribute *attr;
15d034d0 13627 const char *name;
c906108c 13628
348e048f
DE
13629 /* If the definition of this type lives in .debug_types, read that type.
13630 Don't follow DW_AT_specification though, that will take us back up
13631 the chain and we want to go down. */
45e58e77 13632 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13633 if (attr)
13634 {
ac9ec31b 13635 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13636
ac9ec31b 13637 /* The type's CU may not be the same as CU.
02142a6c 13638 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13639 return set_die_type (die, type, cu);
13640 }
13641
c0dd20ea 13642 type = alloc_type (objfile);
c906108c 13643 INIT_CPLUS_SPECIFIC (type);
93311388 13644
39cbfefa
DJ
13645 name = dwarf2_name (die, cu);
13646 if (name != NULL)
c906108c 13647 {
987504bb 13648 if (cu->language == language_cplus
c44af4eb
TT
13649 || cu->language == language_d
13650 || cu->language == language_rust)
63d06c5c 13651 {
15d034d0 13652 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13653
13654 /* dwarf2_full_name might have already finished building the DIE's
13655 type. If so, there is no need to continue. */
13656 if (get_die_type (die, cu) != NULL)
13657 return get_die_type (die, cu);
13658
13659 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13660 if (die->tag == DW_TAG_structure_type
13661 || die->tag == DW_TAG_class_type)
13662 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13663 }
13664 else
13665 {
d8151005
DJ
13666 /* The name is already allocated along with this objfile, so
13667 we don't need to duplicate it for the type. */
7d455152 13668 TYPE_TAG_NAME (type) = name;
94af9270
KS
13669 if (die->tag == DW_TAG_class_type)
13670 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13671 }
c906108c
SS
13672 }
13673
13674 if (die->tag == DW_TAG_structure_type)
13675 {
13676 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13677 }
13678 else if (die->tag == DW_TAG_union_type)
13679 {
13680 TYPE_CODE (type) = TYPE_CODE_UNION;
13681 }
13682 else
13683 {
4753d33b 13684 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13685 }
13686
0cc2414c
TT
13687 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13688 TYPE_DECLARED_CLASS (type) = 1;
13689
e142c38c 13690 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13691 if (attr)
13692 {
155bfbd3
JB
13693 if (attr_form_is_constant (attr))
13694 TYPE_LENGTH (type) = DW_UNSND (attr);
13695 else
13696 {
13697 /* For the moment, dynamic type sizes are not supported
13698 by GDB's struct type. The actual size is determined
13699 on-demand when resolving the type of a given object,
13700 so set the type's length to zero for now. Otherwise,
13701 we record an expression as the length, and that expression
13702 could lead to a very large value, which could eventually
13703 lead to us trying to allocate that much memory when creating
13704 a value of that type. */
13705 TYPE_LENGTH (type) = 0;
13706 }
c906108c
SS
13707 }
13708 else
13709 {
13710 TYPE_LENGTH (type) = 0;
13711 }
13712
422b1cb0 13713 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13714 {
13715 /* ICC does not output the required DW_AT_declaration
13716 on incomplete types, but gives them a size of zero. */
422b1cb0 13717 TYPE_STUB (type) = 1;
685b1105
JK
13718 }
13719 else
13720 TYPE_STUB_SUPPORTED (type) = 1;
13721
dc718098 13722 if (die_is_declaration (die, cu))
876cecd0 13723 TYPE_STUB (type) = 1;
a6c727b2
DJ
13724 else if (attr == NULL && die->child == NULL
13725 && producer_is_realview (cu->producer))
13726 /* RealView does not output the required DW_AT_declaration
13727 on incomplete types. */
13728 TYPE_STUB (type) = 1;
dc718098 13729
c906108c
SS
13730 /* We need to add the type field to the die immediately so we don't
13731 infinitely recurse when dealing with pointers to the structure
0963b4bd 13732 type within the structure itself. */
1c379e20 13733 set_die_type (die, type, cu);
c906108c 13734
7e314c57
JK
13735 /* set_die_type should be already done. */
13736 set_descriptive_type (type, die, cu);
13737
c767944b
DJ
13738 return type;
13739}
13740
13741/* Finish creating a structure or union type, including filling in
13742 its members and creating a symbol for it. */
13743
13744static void
13745process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13746{
13747 struct objfile *objfile = cu->objfile;
ca040673 13748 struct die_info *child_die;
c767944b
DJ
13749 struct type *type;
13750
13751 type = get_die_type (die, cu);
13752 if (type == NULL)
13753 type = read_structure_type (die, cu);
13754
e142c38c 13755 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13756 {
13757 struct field_info fi;
34eaf542 13758 VEC (symbolp) *template_args = NULL;
c767944b 13759 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13760
13761 memset (&fi, 0, sizeof (struct field_info));
13762
639d11d3 13763 child_die = die->child;
c906108c
SS
13764
13765 while (child_die && child_die->tag)
13766 {
a9a9bd0f
DC
13767 if (child_die->tag == DW_TAG_member
13768 || child_die->tag == DW_TAG_variable)
c906108c 13769 {
a9a9bd0f
DC
13770 /* NOTE: carlton/2002-11-05: A C++ static data member
13771 should be a DW_TAG_member that is a declaration, but
13772 all versions of G++ as of this writing (so through at
13773 least 3.2.1) incorrectly generate DW_TAG_variable
13774 tags for them instead. */
e7c27a73 13775 dwarf2_add_field (&fi, child_die, cu);
c906108c 13776 }
8713b1b1 13777 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13778 {
e98c9e7c
TT
13779 /* Rust doesn't have member functions in the C++ sense.
13780 However, it does emit ordinary functions as children
13781 of a struct DIE. */
13782 if (cu->language == language_rust)
13783 read_func_scope (child_die, cu);
13784 else
13785 {
13786 /* C++ member function. */
13787 dwarf2_add_member_fn (&fi, child_die, type, cu);
13788 }
c906108c
SS
13789 }
13790 else if (child_die->tag == DW_TAG_inheritance)
13791 {
13792 /* C++ base class field. */
e7c27a73 13793 dwarf2_add_field (&fi, child_die, cu);
c906108c 13794 }
98751a41
JK
13795 else if (child_die->tag == DW_TAG_typedef)
13796 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13797 else if (child_die->tag == DW_TAG_template_type_param
13798 || child_die->tag == DW_TAG_template_value_param)
13799 {
13800 struct symbol *arg = new_symbol (child_die, NULL, cu);
13801
f1078f66
DJ
13802 if (arg != NULL)
13803 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13804 }
13805
c906108c
SS
13806 child_die = sibling_die (child_die);
13807 }
13808
34eaf542
TT
13809 /* Attach template arguments to type. */
13810 if (! VEC_empty (symbolp, template_args))
13811 {
13812 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13813 TYPE_N_TEMPLATE_ARGUMENTS (type)
13814 = VEC_length (symbolp, template_args);
13815 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13816 = XOBNEWVEC (&objfile->objfile_obstack,
13817 struct symbol *,
13818 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13819 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13820 VEC_address (symbolp, template_args),
13821 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13822 * sizeof (struct symbol *)));
13823 VEC_free (symbolp, template_args);
13824 }
13825
c906108c
SS
13826 /* Attach fields and member functions to the type. */
13827 if (fi.nfields)
e7c27a73 13828 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13829 if (fi.nfnfields)
13830 {
e7c27a73 13831 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13832
c5aa993b 13833 /* Get the type which refers to the base class (possibly this
c906108c 13834 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13835 class from the DW_AT_containing_type attribute. This use of
13836 DW_AT_containing_type is a GNU extension. */
c906108c 13837
e142c38c 13838 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13839 {
e7c27a73 13840 struct type *t = die_containing_type (die, cu);
c906108c 13841
ae6ae975 13842 set_type_vptr_basetype (type, t);
c906108c
SS
13843 if (type == t)
13844 {
c906108c
SS
13845 int i;
13846
13847 /* Our own class provides vtbl ptr. */
13848 for (i = TYPE_NFIELDS (t) - 1;
13849 i >= TYPE_N_BASECLASSES (t);
13850 --i)
13851 {
0d5cff50 13852 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13853
1168df01 13854 if (is_vtable_name (fieldname, cu))
c906108c 13855 {
ae6ae975 13856 set_type_vptr_fieldno (type, i);
c906108c
SS
13857 break;
13858 }
13859 }
13860
13861 /* Complain if virtual function table field not found. */
13862 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13863 complaint (&symfile_complaints,
3e43a32a
MS
13864 _("virtual function table pointer "
13865 "not found when defining class '%s'"),
4d3c2250
KB
13866 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13867 "");
c906108c
SS
13868 }
13869 else
13870 {
ae6ae975 13871 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13872 }
13873 }
f6235d4c 13874 else if (cu->producer
61012eef 13875 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13876 {
13877 /* The IBM XLC compiler does not provide direct indication
13878 of the containing type, but the vtable pointer is
13879 always named __vfp. */
13880
13881 int i;
13882
13883 for (i = TYPE_NFIELDS (type) - 1;
13884 i >= TYPE_N_BASECLASSES (type);
13885 --i)
13886 {
13887 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13888 {
ae6ae975
DE
13889 set_type_vptr_fieldno (type, i);
13890 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13891 break;
13892 }
13893 }
13894 }
c906108c 13895 }
98751a41
JK
13896
13897 /* Copy fi.typedef_field_list linked list elements content into the
13898 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13899 if (fi.typedef_field_list)
13900 {
13901 int i = fi.typedef_field_list_count;
13902
a0d7a4ff 13903 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13904 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13905 = ((struct typedef_field *)
13906 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13907 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13908
13909 /* Reverse the list order to keep the debug info elements order. */
13910 while (--i >= 0)
13911 {
13912 struct typedef_field *dest, *src;
6e70227d 13913
98751a41
JK
13914 dest = &TYPE_TYPEDEF_FIELD (type, i);
13915 src = &fi.typedef_field_list->field;
13916 fi.typedef_field_list = fi.typedef_field_list->next;
13917 *dest = *src;
13918 }
13919 }
c767944b
DJ
13920
13921 do_cleanups (back_to);
c906108c 13922 }
63d06c5c 13923
bb5ed363 13924 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13925
90aeadfc
DC
13926 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13927 snapshots) has been known to create a die giving a declaration
13928 for a class that has, as a child, a die giving a definition for a
13929 nested class. So we have to process our children even if the
13930 current die is a declaration. Normally, of course, a declaration
13931 won't have any children at all. */
134d01f1 13932
ca040673
DE
13933 child_die = die->child;
13934
90aeadfc
DC
13935 while (child_die != NULL && child_die->tag)
13936 {
13937 if (child_die->tag == DW_TAG_member
13938 || child_die->tag == DW_TAG_variable
34eaf542
TT
13939 || child_die->tag == DW_TAG_inheritance
13940 || child_die->tag == DW_TAG_template_value_param
13941 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13942 {
90aeadfc 13943 /* Do nothing. */
134d01f1 13944 }
90aeadfc
DC
13945 else
13946 process_die (child_die, cu);
134d01f1 13947
90aeadfc 13948 child_die = sibling_die (child_die);
134d01f1
DJ
13949 }
13950
fa4028e9
JB
13951 /* Do not consider external references. According to the DWARF standard,
13952 these DIEs are identified by the fact that they have no byte_size
13953 attribute, and a declaration attribute. */
13954 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13955 || !die_is_declaration (die, cu))
c767944b 13956 new_symbol (die, type, cu);
134d01f1
DJ
13957}
13958
55426c9d
JB
13959/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13960 update TYPE using some information only available in DIE's children. */
13961
13962static void
13963update_enumeration_type_from_children (struct die_info *die,
13964 struct type *type,
13965 struct dwarf2_cu *cu)
13966{
60f7655a 13967 struct die_info *child_die;
55426c9d
JB
13968 int unsigned_enum = 1;
13969 int flag_enum = 1;
13970 ULONGEST mask = 0;
55426c9d 13971
8268c778 13972 auto_obstack obstack;
55426c9d 13973
60f7655a
DE
13974 for (child_die = die->child;
13975 child_die != NULL && child_die->tag;
13976 child_die = sibling_die (child_die))
55426c9d
JB
13977 {
13978 struct attribute *attr;
13979 LONGEST value;
13980 const gdb_byte *bytes;
13981 struct dwarf2_locexpr_baton *baton;
13982 const char *name;
60f7655a 13983
55426c9d
JB
13984 if (child_die->tag != DW_TAG_enumerator)
13985 continue;
13986
13987 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13988 if (attr == NULL)
13989 continue;
13990
13991 name = dwarf2_name (child_die, cu);
13992 if (name == NULL)
13993 name = "<anonymous enumerator>";
13994
13995 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13996 &value, &bytes, &baton);
13997 if (value < 0)
13998 {
13999 unsigned_enum = 0;
14000 flag_enum = 0;
14001 }
14002 else if ((mask & value) != 0)
14003 flag_enum = 0;
14004 else
14005 mask |= value;
14006
14007 /* If we already know that the enum type is neither unsigned, nor
14008 a flag type, no need to look at the rest of the enumerates. */
14009 if (!unsigned_enum && !flag_enum)
14010 break;
55426c9d
JB
14011 }
14012
14013 if (unsigned_enum)
14014 TYPE_UNSIGNED (type) = 1;
14015 if (flag_enum)
14016 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14017}
14018
134d01f1
DJ
14019/* Given a DW_AT_enumeration_type die, set its type. We do not
14020 complete the type's fields yet, or create any symbols. */
c906108c 14021
f792889a 14022static struct type *
134d01f1 14023read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14024{
e7c27a73 14025 struct objfile *objfile = cu->objfile;
c906108c 14026 struct type *type;
c906108c 14027 struct attribute *attr;
0114d602 14028 const char *name;
134d01f1 14029
348e048f
DE
14030 /* If the definition of this type lives in .debug_types, read that type.
14031 Don't follow DW_AT_specification though, that will take us back up
14032 the chain and we want to go down. */
45e58e77 14033 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14034 if (attr)
14035 {
ac9ec31b 14036 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14037
ac9ec31b 14038 /* The type's CU may not be the same as CU.
02142a6c 14039 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14040 return set_die_type (die, type, cu);
14041 }
14042
c906108c
SS
14043 type = alloc_type (objfile);
14044
14045 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14046 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14047 if (name != NULL)
7d455152 14048 TYPE_TAG_NAME (type) = name;
c906108c 14049
0626fc76
TT
14050 attr = dwarf2_attr (die, DW_AT_type, cu);
14051 if (attr != NULL)
14052 {
14053 struct type *underlying_type = die_type (die, cu);
14054
14055 TYPE_TARGET_TYPE (type) = underlying_type;
14056 }
14057
e142c38c 14058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14059 if (attr)
14060 {
14061 TYPE_LENGTH (type) = DW_UNSND (attr);
14062 }
14063 else
14064 {
14065 TYPE_LENGTH (type) = 0;
14066 }
14067
137033e9
JB
14068 /* The enumeration DIE can be incomplete. In Ada, any type can be
14069 declared as private in the package spec, and then defined only
14070 inside the package body. Such types are known as Taft Amendment
14071 Types. When another package uses such a type, an incomplete DIE
14072 may be generated by the compiler. */
02eb380e 14073 if (die_is_declaration (die, cu))
876cecd0 14074 TYPE_STUB (type) = 1;
02eb380e 14075
0626fc76
TT
14076 /* Finish the creation of this type by using the enum's children.
14077 We must call this even when the underlying type has been provided
14078 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14079 update_enumeration_type_from_children (die, type, cu);
14080
0626fc76
TT
14081 /* If this type has an underlying type that is not a stub, then we
14082 may use its attributes. We always use the "unsigned" attribute
14083 in this situation, because ordinarily we guess whether the type
14084 is unsigned -- but the guess can be wrong and the underlying type
14085 can tell us the reality. However, we defer to a local size
14086 attribute if one exists, because this lets the compiler override
14087 the underlying type if needed. */
14088 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14089 {
14090 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14091 if (TYPE_LENGTH (type) == 0)
14092 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14093 }
14094
3d567982
TT
14095 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14096
f792889a 14097 return set_die_type (die, type, cu);
134d01f1
DJ
14098}
14099
14100/* Given a pointer to a die which begins an enumeration, process all
14101 the dies that define the members of the enumeration, and create the
14102 symbol for the enumeration type.
14103
14104 NOTE: We reverse the order of the element list. */
14105
14106static void
14107process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14108{
f792889a 14109 struct type *this_type;
134d01f1 14110
f792889a
DJ
14111 this_type = get_die_type (die, cu);
14112 if (this_type == NULL)
14113 this_type = read_enumeration_type (die, cu);
9dc481d3 14114
639d11d3 14115 if (die->child != NULL)
c906108c 14116 {
9dc481d3
DE
14117 struct die_info *child_die;
14118 struct symbol *sym;
14119 struct field *fields = NULL;
14120 int num_fields = 0;
15d034d0 14121 const char *name;
9dc481d3 14122
639d11d3 14123 child_die = die->child;
c906108c
SS
14124 while (child_die && child_die->tag)
14125 {
14126 if (child_die->tag != DW_TAG_enumerator)
14127 {
e7c27a73 14128 process_die (child_die, cu);
c906108c
SS
14129 }
14130 else
14131 {
39cbfefa
DJ
14132 name = dwarf2_name (child_die, cu);
14133 if (name)
c906108c 14134 {
f792889a 14135 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14136
14137 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14138 {
14139 fields = (struct field *)
14140 xrealloc (fields,
14141 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14142 * sizeof (struct field));
c906108c
SS
14143 }
14144
3567439c 14145 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14146 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14147 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14148 FIELD_BITSIZE (fields[num_fields]) = 0;
14149
14150 num_fields++;
14151 }
14152 }
14153
14154 child_die = sibling_die (child_die);
14155 }
14156
14157 if (num_fields)
14158 {
f792889a
DJ
14159 TYPE_NFIELDS (this_type) = num_fields;
14160 TYPE_FIELDS (this_type) = (struct field *)
14161 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14162 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14163 sizeof (struct field) * num_fields);
b8c9b27d 14164 xfree (fields);
c906108c 14165 }
c906108c 14166 }
134d01f1 14167
6c83ed52
TT
14168 /* If we are reading an enum from a .debug_types unit, and the enum
14169 is a declaration, and the enum is not the signatured type in the
14170 unit, then we do not want to add a symbol for it. Adding a
14171 symbol would in some cases obscure the true definition of the
14172 enum, giving users an incomplete type when the definition is
14173 actually available. Note that we do not want to do this for all
14174 enums which are just declarations, because C++0x allows forward
14175 enum declarations. */
3019eac3 14176 if (cu->per_cu->is_debug_types
6c83ed52
TT
14177 && die_is_declaration (die, cu))
14178 {
52dc124a 14179 struct signatured_type *sig_type;
6c83ed52 14180
c0f78cd4 14181 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14182 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14183 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14184 return;
14185 }
14186
f792889a 14187 new_symbol (die, this_type, cu);
c906108c
SS
14188}
14189
14190/* Extract all information from a DW_TAG_array_type DIE and put it in
14191 the DIE's type field. For now, this only handles one dimensional
14192 arrays. */
14193
f792889a 14194static struct type *
e7c27a73 14195read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14196{
e7c27a73 14197 struct objfile *objfile = cu->objfile;
c906108c 14198 struct die_info *child_die;
7e314c57 14199 struct type *type;
c906108c
SS
14200 struct type *element_type, *range_type, *index_type;
14201 struct type **range_types = NULL;
14202 struct attribute *attr;
14203 int ndim = 0;
14204 struct cleanup *back_to;
15d034d0 14205 const char *name;
dc53a7ad 14206 unsigned int bit_stride = 0;
c906108c 14207
e7c27a73 14208 element_type = die_type (die, cu);
c906108c 14209
7e314c57
JK
14210 /* The die_type call above may have already set the type for this DIE. */
14211 type = get_die_type (die, cu);
14212 if (type)
14213 return type;
14214
dc53a7ad
JB
14215 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14216 if (attr != NULL)
14217 bit_stride = DW_UNSND (attr) * 8;
14218
14219 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14220 if (attr != NULL)
14221 bit_stride = DW_UNSND (attr);
14222
c906108c
SS
14223 /* Irix 6.2 native cc creates array types without children for
14224 arrays with unspecified length. */
639d11d3 14225 if (die->child == NULL)
c906108c 14226 {
46bf5051 14227 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14228 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14229 type = create_array_type_with_stride (NULL, element_type, range_type,
14230 bit_stride);
f792889a 14231 return set_die_type (die, type, cu);
c906108c
SS
14232 }
14233
14234 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14235 child_die = die->child;
c906108c
SS
14236 while (child_die && child_die->tag)
14237 {
14238 if (child_die->tag == DW_TAG_subrange_type)
14239 {
f792889a 14240 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14241
f792889a 14242 if (child_type != NULL)
a02abb62 14243 {
0963b4bd
MS
14244 /* The range type was succesfully read. Save it for the
14245 array type creation. */
a02abb62
JB
14246 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14247 {
14248 range_types = (struct type **)
14249 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14250 * sizeof (struct type *));
14251 if (ndim == 0)
14252 make_cleanup (free_current_contents, &range_types);
14253 }
f792889a 14254 range_types[ndim++] = child_type;
a02abb62 14255 }
c906108c
SS
14256 }
14257 child_die = sibling_die (child_die);
14258 }
14259
14260 /* Dwarf2 dimensions are output from left to right, create the
14261 necessary array types in backwards order. */
7ca2d3a3 14262
c906108c 14263 type = element_type;
7ca2d3a3
DL
14264
14265 if (read_array_order (die, cu) == DW_ORD_col_major)
14266 {
14267 int i = 0;
9a619af0 14268
7ca2d3a3 14269 while (i < ndim)
dc53a7ad
JB
14270 type = create_array_type_with_stride (NULL, type, range_types[i++],
14271 bit_stride);
7ca2d3a3
DL
14272 }
14273 else
14274 {
14275 while (ndim-- > 0)
dc53a7ad
JB
14276 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14277 bit_stride);
7ca2d3a3 14278 }
c906108c 14279
f5f8a009
EZ
14280 /* Understand Dwarf2 support for vector types (like they occur on
14281 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14282 array type. This is not part of the Dwarf2/3 standard yet, but a
14283 custom vendor extension. The main difference between a regular
14284 array and the vector variant is that vectors are passed by value
14285 to functions. */
e142c38c 14286 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14287 if (attr)
ea37ba09 14288 make_vector_type (type);
f5f8a009 14289
dbc98a8b
KW
14290 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14291 implementation may choose to implement triple vectors using this
14292 attribute. */
14293 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14294 if (attr)
14295 {
14296 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14297 TYPE_LENGTH (type) = DW_UNSND (attr);
14298 else
3e43a32a
MS
14299 complaint (&symfile_complaints,
14300 _("DW_AT_byte_size for array type smaller "
14301 "than the total size of elements"));
dbc98a8b
KW
14302 }
14303
39cbfefa
DJ
14304 name = dwarf2_name (die, cu);
14305 if (name)
14306 TYPE_NAME (type) = name;
6e70227d 14307
0963b4bd 14308 /* Install the type in the die. */
7e314c57
JK
14309 set_die_type (die, type, cu);
14310
14311 /* set_die_type should be already done. */
b4ba55a1
JB
14312 set_descriptive_type (type, die, cu);
14313
c906108c
SS
14314 do_cleanups (back_to);
14315
7e314c57 14316 return type;
c906108c
SS
14317}
14318
7ca2d3a3 14319static enum dwarf_array_dim_ordering
6e70227d 14320read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14321{
14322 struct attribute *attr;
14323
14324 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14325
aead7601
SM
14326 if (attr)
14327 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14328
0963b4bd
MS
14329 /* GNU F77 is a special case, as at 08/2004 array type info is the
14330 opposite order to the dwarf2 specification, but data is still
14331 laid out as per normal fortran.
7ca2d3a3 14332
0963b4bd
MS
14333 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14334 version checking. */
7ca2d3a3 14335
905e0470
PM
14336 if (cu->language == language_fortran
14337 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14338 {
14339 return DW_ORD_row_major;
14340 }
14341
6e70227d 14342 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14343 {
14344 case array_column_major:
14345 return DW_ORD_col_major;
14346 case array_row_major:
14347 default:
14348 return DW_ORD_row_major;
14349 };
14350}
14351
72019c9c 14352/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14353 the DIE's type field. */
72019c9c 14354
f792889a 14355static struct type *
72019c9c
GM
14356read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14357{
7e314c57
JK
14358 struct type *domain_type, *set_type;
14359 struct attribute *attr;
f792889a 14360
7e314c57
JK
14361 domain_type = die_type (die, cu);
14362
14363 /* The die_type call above may have already set the type for this DIE. */
14364 set_type = get_die_type (die, cu);
14365 if (set_type)
14366 return set_type;
14367
14368 set_type = create_set_type (NULL, domain_type);
14369
14370 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14371 if (attr)
14372 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14373
f792889a 14374 return set_die_type (die, set_type, cu);
72019c9c 14375}
7ca2d3a3 14376
0971de02
TT
14377/* A helper for read_common_block that creates a locexpr baton.
14378 SYM is the symbol which we are marking as computed.
14379 COMMON_DIE is the DIE for the common block.
14380 COMMON_LOC is the location expression attribute for the common
14381 block itself.
14382 MEMBER_LOC is the location expression attribute for the particular
14383 member of the common block that we are processing.
14384 CU is the CU from which the above come. */
14385
14386static void
14387mark_common_block_symbol_computed (struct symbol *sym,
14388 struct die_info *common_die,
14389 struct attribute *common_loc,
14390 struct attribute *member_loc,
14391 struct dwarf2_cu *cu)
14392{
14393 struct objfile *objfile = dwarf2_per_objfile->objfile;
14394 struct dwarf2_locexpr_baton *baton;
14395 gdb_byte *ptr;
14396 unsigned int cu_off;
14397 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14398 LONGEST offset = 0;
14399
14400 gdb_assert (common_loc && member_loc);
14401 gdb_assert (attr_form_is_block (common_loc));
14402 gdb_assert (attr_form_is_block (member_loc)
14403 || attr_form_is_constant (member_loc));
14404
8d749320 14405 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14406 baton->per_cu = cu->per_cu;
14407 gdb_assert (baton->per_cu);
14408
14409 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14410
14411 if (attr_form_is_constant (member_loc))
14412 {
14413 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14414 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14415 }
14416 else
14417 baton->size += DW_BLOCK (member_loc)->size;
14418
224c3ddb 14419 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14420 baton->data = ptr;
14421
14422 *ptr++ = DW_OP_call4;
9c541725 14423 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14424 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14425 ptr += 4;
14426
14427 if (attr_form_is_constant (member_loc))
14428 {
14429 *ptr++ = DW_OP_addr;
14430 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14431 ptr += cu->header.addr_size;
14432 }
14433 else
14434 {
14435 /* We have to copy the data here, because DW_OP_call4 will only
14436 use a DW_AT_location attribute. */
14437 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14438 ptr += DW_BLOCK (member_loc)->size;
14439 }
14440
14441 *ptr++ = DW_OP_plus;
14442 gdb_assert (ptr - baton->data == baton->size);
14443
0971de02 14444 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14445 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14446}
14447
4357ac6c
TT
14448/* Create appropriate locally-scoped variables for all the
14449 DW_TAG_common_block entries. Also create a struct common_block
14450 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14451 is used to sepate the common blocks name namespace from regular
14452 variable names. */
c906108c
SS
14453
14454static void
e7c27a73 14455read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14456{
0971de02
TT
14457 struct attribute *attr;
14458
14459 attr = dwarf2_attr (die, DW_AT_location, cu);
14460 if (attr)
14461 {
14462 /* Support the .debug_loc offsets. */
14463 if (attr_form_is_block (attr))
14464 {
14465 /* Ok. */
14466 }
14467 else if (attr_form_is_section_offset (attr))
14468 {
14469 dwarf2_complex_location_expr_complaint ();
14470 attr = NULL;
14471 }
14472 else
14473 {
14474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14475 "common block member");
14476 attr = NULL;
14477 }
14478 }
14479
639d11d3 14480 if (die->child != NULL)
c906108c 14481 {
4357ac6c
TT
14482 struct objfile *objfile = cu->objfile;
14483 struct die_info *child_die;
14484 size_t n_entries = 0, size;
14485 struct common_block *common_block;
14486 struct symbol *sym;
74ac6d43 14487
4357ac6c
TT
14488 for (child_die = die->child;
14489 child_die && child_die->tag;
14490 child_die = sibling_die (child_die))
14491 ++n_entries;
14492
14493 size = (sizeof (struct common_block)
14494 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14495 common_block
14496 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14497 size);
4357ac6c
TT
14498 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14499 common_block->n_entries = 0;
14500
14501 for (child_die = die->child;
14502 child_die && child_die->tag;
14503 child_die = sibling_die (child_die))
14504 {
14505 /* Create the symbol in the DW_TAG_common_block block in the current
14506 symbol scope. */
e7c27a73 14507 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14508 if (sym != NULL)
14509 {
14510 struct attribute *member_loc;
14511
14512 common_block->contents[common_block->n_entries++] = sym;
14513
14514 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14515 cu);
14516 if (member_loc)
14517 {
14518 /* GDB has handled this for a long time, but it is
14519 not specified by DWARF. It seems to have been
14520 emitted by gfortran at least as recently as:
14521 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14522 complaint (&symfile_complaints,
14523 _("Variable in common block has "
14524 "DW_AT_data_member_location "
14525 "- DIE at 0x%x [in module %s]"),
9c541725 14526 to_underlying (child_die->sect_off),
4262abfb 14527 objfile_name (cu->objfile));
0971de02
TT
14528
14529 if (attr_form_is_section_offset (member_loc))
14530 dwarf2_complex_location_expr_complaint ();
14531 else if (attr_form_is_constant (member_loc)
14532 || attr_form_is_block (member_loc))
14533 {
14534 if (attr)
14535 mark_common_block_symbol_computed (sym, die, attr,
14536 member_loc, cu);
14537 }
14538 else
14539 dwarf2_complex_location_expr_complaint ();
14540 }
14541 }
c906108c 14542 }
4357ac6c
TT
14543
14544 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14545 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14546 }
14547}
14548
0114d602 14549/* Create a type for a C++ namespace. */
d9fa45fe 14550
0114d602
DJ
14551static struct type *
14552read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14553{
e7c27a73 14554 struct objfile *objfile = cu->objfile;
0114d602 14555 const char *previous_prefix, *name;
9219021c 14556 int is_anonymous;
0114d602
DJ
14557 struct type *type;
14558
14559 /* For extensions, reuse the type of the original namespace. */
14560 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14561 {
14562 struct die_info *ext_die;
14563 struct dwarf2_cu *ext_cu = cu;
9a619af0 14564
0114d602
DJ
14565 ext_die = dwarf2_extension (die, &ext_cu);
14566 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14567
14568 /* EXT_CU may not be the same as CU.
02142a6c 14569 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14570 return set_die_type (die, type, cu);
14571 }
9219021c 14572
e142c38c 14573 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14574
14575 /* Now build the name of the current namespace. */
14576
0114d602
DJ
14577 previous_prefix = determine_prefix (die, cu);
14578 if (previous_prefix[0] != '\0')
14579 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14580 previous_prefix, name, 0, cu);
0114d602
DJ
14581
14582 /* Create the type. */
19f392bc 14583 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14584 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14585
60531b24 14586 return set_die_type (die, type, cu);
0114d602
DJ
14587}
14588
22cee43f 14589/* Read a namespace scope. */
0114d602
DJ
14590
14591static void
14592read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14593{
14594 struct objfile *objfile = cu->objfile;
0114d602 14595 int is_anonymous;
9219021c 14596
5c4e30ca
DC
14597 /* Add a symbol associated to this if we haven't seen the namespace
14598 before. Also, add a using directive if it's an anonymous
14599 namespace. */
9219021c 14600
f2f0e013 14601 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14602 {
14603 struct type *type;
14604
0114d602 14605 type = read_type_die (die, cu);
e7c27a73 14606 new_symbol (die, type, cu);
5c4e30ca 14607
e8e80198 14608 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14609 if (is_anonymous)
0114d602
DJ
14610 {
14611 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14612
eb1e02fd 14613 std::vector<const char *> excludes;
22cee43f
PMR
14614 add_using_directive (using_directives (cu->language),
14615 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 14616 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 14617 }
5c4e30ca 14618 }
9219021c 14619
639d11d3 14620 if (die->child != NULL)
d9fa45fe 14621 {
639d11d3 14622 struct die_info *child_die = die->child;
6e70227d 14623
d9fa45fe
DC
14624 while (child_die && child_die->tag)
14625 {
e7c27a73 14626 process_die (child_die, cu);
d9fa45fe
DC
14627 child_die = sibling_die (child_die);
14628 }
14629 }
38d518c9
EZ
14630}
14631
f55ee35c
JK
14632/* Read a Fortran module as type. This DIE can be only a declaration used for
14633 imported module. Still we need that type as local Fortran "use ... only"
14634 declaration imports depend on the created type in determine_prefix. */
14635
14636static struct type *
14637read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14638{
14639 struct objfile *objfile = cu->objfile;
15d034d0 14640 const char *module_name;
f55ee35c
JK
14641 struct type *type;
14642
14643 module_name = dwarf2_name (die, cu);
14644 if (!module_name)
3e43a32a
MS
14645 complaint (&symfile_complaints,
14646 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14647 to_underlying (die->sect_off));
19f392bc 14648 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14649
14650 /* determine_prefix uses TYPE_TAG_NAME. */
14651 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14652
14653 return set_die_type (die, type, cu);
14654}
14655
5d7cb8df
JK
14656/* Read a Fortran module. */
14657
14658static void
14659read_module (struct die_info *die, struct dwarf2_cu *cu)
14660{
14661 struct die_info *child_die = die->child;
530e8392
KB
14662 struct type *type;
14663
14664 type = read_type_die (die, cu);
14665 new_symbol (die, type, cu);
5d7cb8df 14666
5d7cb8df
JK
14667 while (child_die && child_die->tag)
14668 {
14669 process_die (child_die, cu);
14670 child_die = sibling_die (child_die);
14671 }
14672}
14673
38d518c9
EZ
14674/* Return the name of the namespace represented by DIE. Set
14675 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14676 namespace. */
14677
14678static const char *
e142c38c 14679namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14680{
14681 struct die_info *current_die;
14682 const char *name = NULL;
14683
14684 /* Loop through the extensions until we find a name. */
14685
14686 for (current_die = die;
14687 current_die != NULL;
f2f0e013 14688 current_die = dwarf2_extension (die, &cu))
38d518c9 14689 {
96553a0c
DE
14690 /* We don't use dwarf2_name here so that we can detect the absence
14691 of a name -> anonymous namespace. */
7d45c7c3 14692 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14693
38d518c9
EZ
14694 if (name != NULL)
14695 break;
14696 }
14697
14698 /* Is it an anonymous namespace? */
14699
14700 *is_anonymous = (name == NULL);
14701 if (*is_anonymous)
2b1dbab0 14702 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14703
14704 return name;
d9fa45fe
DC
14705}
14706
c906108c
SS
14707/* Extract all information from a DW_TAG_pointer_type DIE and add to
14708 the user defined type vector. */
14709
f792889a 14710static struct type *
e7c27a73 14711read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14712{
5e2b427d 14713 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14714 struct comp_unit_head *cu_header = &cu->header;
c906108c 14715 struct type *type;
8b2dbe47
KB
14716 struct attribute *attr_byte_size;
14717 struct attribute *attr_address_class;
14718 int byte_size, addr_class;
7e314c57
JK
14719 struct type *target_type;
14720
14721 target_type = die_type (die, cu);
c906108c 14722
7e314c57
JK
14723 /* The die_type call above may have already set the type for this DIE. */
14724 type = get_die_type (die, cu);
14725 if (type)
14726 return type;
14727
14728 type = lookup_pointer_type (target_type);
8b2dbe47 14729
e142c38c 14730 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14731 if (attr_byte_size)
14732 byte_size = DW_UNSND (attr_byte_size);
c906108c 14733 else
8b2dbe47
KB
14734 byte_size = cu_header->addr_size;
14735
e142c38c 14736 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14737 if (attr_address_class)
14738 addr_class = DW_UNSND (attr_address_class);
14739 else
14740 addr_class = DW_ADDR_none;
14741
14742 /* If the pointer size or address class is different than the
14743 default, create a type variant marked as such and set the
14744 length accordingly. */
14745 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14746 {
5e2b427d 14747 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14748 {
14749 int type_flags;
14750
849957d9 14751 type_flags = gdbarch_address_class_type_flags
5e2b427d 14752 (gdbarch, byte_size, addr_class);
876cecd0
TT
14753 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14754 == 0);
8b2dbe47
KB
14755 type = make_type_with_address_space (type, type_flags);
14756 }
14757 else if (TYPE_LENGTH (type) != byte_size)
14758 {
3e43a32a
MS
14759 complaint (&symfile_complaints,
14760 _("invalid pointer size %d"), byte_size);
8b2dbe47 14761 }
6e70227d 14762 else
9a619af0
MS
14763 {
14764 /* Should we also complain about unhandled address classes? */
14765 }
c906108c 14766 }
8b2dbe47
KB
14767
14768 TYPE_LENGTH (type) = byte_size;
f792889a 14769 return set_die_type (die, type, cu);
c906108c
SS
14770}
14771
14772/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14773 the user defined type vector. */
14774
f792889a 14775static struct type *
e7c27a73 14776read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14777{
14778 struct type *type;
14779 struct type *to_type;
14780 struct type *domain;
14781
e7c27a73
DJ
14782 to_type = die_type (die, cu);
14783 domain = die_containing_type (die, cu);
0d5de010 14784
7e314c57
JK
14785 /* The calls above may have already set the type for this DIE. */
14786 type = get_die_type (die, cu);
14787 if (type)
14788 return type;
14789
0d5de010
DJ
14790 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14791 type = lookup_methodptr_type (to_type);
7078baeb
TT
14792 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14793 {
14794 struct type *new_type = alloc_type (cu->objfile);
14795
14796 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14797 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14798 TYPE_VARARGS (to_type));
14799 type = lookup_methodptr_type (new_type);
14800 }
0d5de010
DJ
14801 else
14802 type = lookup_memberptr_type (to_type, domain);
c906108c 14803
f792889a 14804 return set_die_type (die, type, cu);
c906108c
SS
14805}
14806
4297a3f0 14807/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14808 the user defined type vector. */
14809
f792889a 14810static struct type *
4297a3f0
AV
14811read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14812 enum type_code refcode)
c906108c 14813{
e7c27a73 14814 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14815 struct type *type, *target_type;
c906108c
SS
14816 struct attribute *attr;
14817
4297a3f0
AV
14818 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14819
7e314c57
JK
14820 target_type = die_type (die, cu);
14821
14822 /* The die_type call above may have already set the type for this DIE. */
14823 type = get_die_type (die, cu);
14824 if (type)
14825 return type;
14826
4297a3f0 14827 type = lookup_reference_type (target_type, refcode);
e142c38c 14828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14829 if (attr)
14830 {
14831 TYPE_LENGTH (type) = DW_UNSND (attr);
14832 }
14833 else
14834 {
107d2387 14835 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14836 }
f792889a 14837 return set_die_type (die, type, cu);
c906108c
SS
14838}
14839
cf363f18
MW
14840/* Add the given cv-qualifiers to the element type of the array. GCC
14841 outputs DWARF type qualifiers that apply to an array, not the
14842 element type. But GDB relies on the array element type to carry
14843 the cv-qualifiers. This mimics section 6.7.3 of the C99
14844 specification. */
14845
14846static struct type *
14847add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14848 struct type *base_type, int cnst, int voltl)
14849{
14850 struct type *el_type, *inner_array;
14851
14852 base_type = copy_type (base_type);
14853 inner_array = base_type;
14854
14855 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14856 {
14857 TYPE_TARGET_TYPE (inner_array) =
14858 copy_type (TYPE_TARGET_TYPE (inner_array));
14859 inner_array = TYPE_TARGET_TYPE (inner_array);
14860 }
14861
14862 el_type = TYPE_TARGET_TYPE (inner_array);
14863 cnst |= TYPE_CONST (el_type);
14864 voltl |= TYPE_VOLATILE (el_type);
14865 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14866
14867 return set_die_type (die, base_type, cu);
14868}
14869
f792889a 14870static struct type *
e7c27a73 14871read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14872{
f792889a 14873 struct type *base_type, *cv_type;
c906108c 14874
e7c27a73 14875 base_type = die_type (die, cu);
7e314c57
JK
14876
14877 /* The die_type call above may have already set the type for this DIE. */
14878 cv_type = get_die_type (die, cu);
14879 if (cv_type)
14880 return cv_type;
14881
2f608a3a
KW
14882 /* In case the const qualifier is applied to an array type, the element type
14883 is so qualified, not the array type (section 6.7.3 of C99). */
14884 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14885 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14886
f792889a
DJ
14887 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14888 return set_die_type (die, cv_type, cu);
c906108c
SS
14889}
14890
f792889a 14891static struct type *
e7c27a73 14892read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14893{
f792889a 14894 struct type *base_type, *cv_type;
c906108c 14895
e7c27a73 14896 base_type = die_type (die, cu);
7e314c57
JK
14897
14898 /* The die_type call above may have already set the type for this DIE. */
14899 cv_type = get_die_type (die, cu);
14900 if (cv_type)
14901 return cv_type;
14902
cf363f18
MW
14903 /* In case the volatile qualifier is applied to an array type, the
14904 element type is so qualified, not the array type (section 6.7.3
14905 of C99). */
14906 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14907 return add_array_cv_type (die, cu, base_type, 0, 1);
14908
f792889a
DJ
14909 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14910 return set_die_type (die, cv_type, cu);
c906108c
SS
14911}
14912
06d66ee9
TT
14913/* Handle DW_TAG_restrict_type. */
14914
14915static struct type *
14916read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14917{
14918 struct type *base_type, *cv_type;
14919
14920 base_type = die_type (die, cu);
14921
14922 /* The die_type call above may have already set the type for this DIE. */
14923 cv_type = get_die_type (die, cu);
14924 if (cv_type)
14925 return cv_type;
14926
14927 cv_type = make_restrict_type (base_type);
14928 return set_die_type (die, cv_type, cu);
14929}
14930
a2c2acaf
MW
14931/* Handle DW_TAG_atomic_type. */
14932
14933static struct type *
14934read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14935{
14936 struct type *base_type, *cv_type;
14937
14938 base_type = die_type (die, cu);
14939
14940 /* The die_type call above may have already set the type for this DIE. */
14941 cv_type = get_die_type (die, cu);
14942 if (cv_type)
14943 return cv_type;
14944
14945 cv_type = make_atomic_type (base_type);
14946 return set_die_type (die, cv_type, cu);
14947}
14948
c906108c
SS
14949/* Extract all information from a DW_TAG_string_type DIE and add to
14950 the user defined type vector. It isn't really a user defined type,
14951 but it behaves like one, with other DIE's using an AT_user_def_type
14952 attribute to reference it. */
14953
f792889a 14954static struct type *
e7c27a73 14955read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14956{
e7c27a73 14957 struct objfile *objfile = cu->objfile;
3b7538c0 14958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14959 struct type *type, *range_type, *index_type, *char_type;
14960 struct attribute *attr;
14961 unsigned int length;
14962
e142c38c 14963 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14964 if (attr)
14965 {
14966 length = DW_UNSND (attr);
14967 }
14968 else
14969 {
0963b4bd 14970 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14972 if (attr)
14973 {
14974 length = DW_UNSND (attr);
14975 }
14976 else
14977 {
14978 length = 1;
14979 }
c906108c 14980 }
6ccb9162 14981
46bf5051 14982 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14983 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14984 char_type = language_string_char_type (cu->language_defn, gdbarch);
14985 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14986
f792889a 14987 return set_die_type (die, type, cu);
c906108c
SS
14988}
14989
4d804846
JB
14990/* Assuming that DIE corresponds to a function, returns nonzero
14991 if the function is prototyped. */
14992
14993static int
14994prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14995{
14996 struct attribute *attr;
14997
14998 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14999 if (attr && (DW_UNSND (attr) != 0))
15000 return 1;
15001
15002 /* The DWARF standard implies that the DW_AT_prototyped attribute
15003 is only meaninful for C, but the concept also extends to other
15004 languages that allow unprototyped functions (Eg: Objective C).
15005 For all other languages, assume that functions are always
15006 prototyped. */
15007 if (cu->language != language_c
15008 && cu->language != language_objc
15009 && cu->language != language_opencl)
15010 return 1;
15011
15012 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15013 prototyped and unprototyped functions; default to prototyped,
15014 since that is more common in modern code (and RealView warns
15015 about unprototyped functions). */
15016 if (producer_is_realview (cu->producer))
15017 return 1;
15018
15019 return 0;
15020}
15021
c906108c
SS
15022/* Handle DIES due to C code like:
15023
15024 struct foo
c5aa993b
JM
15025 {
15026 int (*funcp)(int a, long l);
15027 int b;
15028 };
c906108c 15029
0963b4bd 15030 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15031
f792889a 15032static struct type *
e7c27a73 15033read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15034{
bb5ed363 15035 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15036 struct type *type; /* Type that this function returns. */
15037 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15038 struct attribute *attr;
15039
e7c27a73 15040 type = die_type (die, cu);
7e314c57
JK
15041
15042 /* The die_type call above may have already set the type for this DIE. */
15043 ftype = get_die_type (die, cu);
15044 if (ftype)
15045 return ftype;
15046
0c8b41f1 15047 ftype = lookup_function_type (type);
c906108c 15048
4d804846 15049 if (prototyped_function_p (die, cu))
a6c727b2 15050 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15051
c055b101
CV
15052 /* Store the calling convention in the type if it's available in
15053 the subroutine die. Otherwise set the calling convention to
15054 the default value DW_CC_normal. */
15055 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15056 if (attr)
15057 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15058 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15059 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15060 else
15061 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15062
743649fd
MW
15063 /* Record whether the function returns normally to its caller or not
15064 if the DWARF producer set that information. */
15065 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15066 if (attr && (DW_UNSND (attr) != 0))
15067 TYPE_NO_RETURN (ftype) = 1;
15068
76c10ea2
GM
15069 /* We need to add the subroutine type to the die immediately so
15070 we don't infinitely recurse when dealing with parameters
0963b4bd 15071 declared as the same subroutine type. */
76c10ea2 15072 set_die_type (die, ftype, cu);
6e70227d 15073
639d11d3 15074 if (die->child != NULL)
c906108c 15075 {
bb5ed363 15076 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15077 struct die_info *child_die;
8072405b 15078 int nparams, iparams;
c906108c
SS
15079
15080 /* Count the number of parameters.
15081 FIXME: GDB currently ignores vararg functions, but knows about
15082 vararg member functions. */
8072405b 15083 nparams = 0;
639d11d3 15084 child_die = die->child;
c906108c
SS
15085 while (child_die && child_die->tag)
15086 {
15087 if (child_die->tag == DW_TAG_formal_parameter)
15088 nparams++;
15089 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15090 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15091 child_die = sibling_die (child_die);
15092 }
15093
15094 /* Allocate storage for parameters and fill them in. */
15095 TYPE_NFIELDS (ftype) = nparams;
15096 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15097 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15098
8072405b
JK
15099 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15100 even if we error out during the parameters reading below. */
15101 for (iparams = 0; iparams < nparams; iparams++)
15102 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15103
15104 iparams = 0;
639d11d3 15105 child_die = die->child;
c906108c
SS
15106 while (child_die && child_die->tag)
15107 {
15108 if (child_die->tag == DW_TAG_formal_parameter)
15109 {
3ce3b1ba
PA
15110 struct type *arg_type;
15111
15112 /* DWARF version 2 has no clean way to discern C++
15113 static and non-static member functions. G++ helps
15114 GDB by marking the first parameter for non-static
15115 member functions (which is the this pointer) as
15116 artificial. We pass this information to
15117 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15118
15119 DWARF version 3 added DW_AT_object_pointer, which GCC
15120 4.5 does not yet generate. */
e142c38c 15121 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15122 if (attr)
15123 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15124 else
9c37b5ae 15125 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15126 arg_type = die_type (child_die, cu);
15127
15128 /* RealView does not mark THIS as const, which the testsuite
15129 expects. GCC marks THIS as const in method definitions,
15130 but not in the class specifications (GCC PR 43053). */
15131 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15132 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15133 {
15134 int is_this = 0;
15135 struct dwarf2_cu *arg_cu = cu;
15136 const char *name = dwarf2_name (child_die, cu);
15137
15138 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15139 if (attr)
15140 {
15141 /* If the compiler emits this, use it. */
15142 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15143 is_this = 1;
15144 }
15145 else if (name && strcmp (name, "this") == 0)
15146 /* Function definitions will have the argument names. */
15147 is_this = 1;
15148 else if (name == NULL && iparams == 0)
15149 /* Declarations may not have the names, so like
15150 elsewhere in GDB, assume an artificial first
15151 argument is "this". */
15152 is_this = 1;
15153
15154 if (is_this)
15155 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15156 arg_type, 0);
15157 }
15158
15159 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15160 iparams++;
15161 }
15162 child_die = sibling_die (child_die);
15163 }
15164 }
15165
76c10ea2 15166 return ftype;
c906108c
SS
15167}
15168
f792889a 15169static struct type *
e7c27a73 15170read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15171{
e7c27a73 15172 struct objfile *objfile = cu->objfile;
0114d602 15173 const char *name = NULL;
3c8e0968 15174 struct type *this_type, *target_type;
c906108c 15175
94af9270 15176 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15177 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15178 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15179 set_die_type (die, this_type, cu);
3c8e0968
DE
15180 target_type = die_type (die, cu);
15181 if (target_type != this_type)
15182 TYPE_TARGET_TYPE (this_type) = target_type;
15183 else
15184 {
15185 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15186 spec and cause infinite loops in GDB. */
15187 complaint (&symfile_complaints,
15188 _("Self-referential DW_TAG_typedef "
15189 "- DIE at 0x%x [in module %s]"),
9c541725 15190 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15191 TYPE_TARGET_TYPE (this_type) = NULL;
15192 }
f792889a 15193 return this_type;
c906108c
SS
15194}
15195
9b790ce7
UW
15196/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15197 (which may be different from NAME) to the architecture back-end to allow
15198 it to guess the correct format if necessary. */
15199
15200static struct type *
15201dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15202 const char *name_hint)
15203{
15204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15205 const struct floatformat **format;
15206 struct type *type;
15207
15208 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15209 if (format)
15210 type = init_float_type (objfile, bits, name, format);
15211 else
15212 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15213
15214 return type;
15215}
15216
c906108c
SS
15217/* Find a representation of a given base type and install
15218 it in the TYPE field of the die. */
15219
f792889a 15220static struct type *
e7c27a73 15221read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15222{
e7c27a73 15223 struct objfile *objfile = cu->objfile;
c906108c
SS
15224 struct type *type;
15225 struct attribute *attr;
19f392bc 15226 int encoding = 0, bits = 0;
15d034d0 15227 const char *name;
c906108c 15228
e142c38c 15229 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15230 if (attr)
15231 {
15232 encoding = DW_UNSND (attr);
15233 }
e142c38c 15234 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15235 if (attr)
15236 {
19f392bc 15237 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15238 }
39cbfefa 15239 name = dwarf2_name (die, cu);
6ccb9162 15240 if (!name)
c906108c 15241 {
6ccb9162
UW
15242 complaint (&symfile_complaints,
15243 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15244 }
6ccb9162
UW
15245
15246 switch (encoding)
c906108c 15247 {
6ccb9162
UW
15248 case DW_ATE_address:
15249 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15250 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15251 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15252 break;
15253 case DW_ATE_boolean:
19f392bc 15254 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15255 break;
15256 case DW_ATE_complex_float:
9b790ce7 15257 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15258 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15259 break;
15260 case DW_ATE_decimal_float:
19f392bc 15261 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15262 break;
15263 case DW_ATE_float:
9b790ce7 15264 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15265 break;
15266 case DW_ATE_signed:
19f392bc 15267 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15268 break;
15269 case DW_ATE_unsigned:
3b2b8fea
TT
15270 if (cu->language == language_fortran
15271 && name
61012eef 15272 && startswith (name, "character("))
19f392bc
UW
15273 type = init_character_type (objfile, bits, 1, name);
15274 else
15275 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15276 break;
15277 case DW_ATE_signed_char:
6e70227d 15278 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15279 || cu->language == language_pascal
15280 || cu->language == language_fortran)
19f392bc
UW
15281 type = init_character_type (objfile, bits, 0, name);
15282 else
15283 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15284 break;
15285 case DW_ATE_unsigned_char:
868a0084 15286 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15287 || cu->language == language_pascal
c44af4eb
TT
15288 || cu->language == language_fortran
15289 || cu->language == language_rust)
19f392bc
UW
15290 type = init_character_type (objfile, bits, 1, name);
15291 else
15292 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15293 break;
75079b2b 15294 case DW_ATE_UTF:
53e710ac
PA
15295 {
15296 gdbarch *arch = get_objfile_arch (objfile);
15297
15298 if (bits == 16)
15299 type = builtin_type (arch)->builtin_char16;
15300 else if (bits == 32)
15301 type = builtin_type (arch)->builtin_char32;
15302 else
15303 {
15304 complaint (&symfile_complaints,
15305 _("unsupported DW_ATE_UTF bit size: '%d'"),
15306 bits);
15307 type = init_integer_type (objfile, bits, 1, name);
15308 }
15309 return set_die_type (die, type, cu);
15310 }
75079b2b
TT
15311 break;
15312
6ccb9162
UW
15313 default:
15314 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15315 dwarf_type_encoding_name (encoding));
19f392bc
UW
15316 type = init_type (objfile, TYPE_CODE_ERROR,
15317 bits / TARGET_CHAR_BIT, name);
6ccb9162 15318 break;
c906108c 15319 }
6ccb9162 15320
0114d602 15321 if (name && strcmp (name, "char") == 0)
876cecd0 15322 TYPE_NOSIGN (type) = 1;
0114d602 15323
f792889a 15324 return set_die_type (die, type, cu);
c906108c
SS
15325}
15326
80180f79
SA
15327/* Parse dwarf attribute if it's a block, reference or constant and put the
15328 resulting value of the attribute into struct bound_prop.
15329 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15330
15331static int
15332attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15333 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15334{
15335 struct dwarf2_property_baton *baton;
15336 struct obstack *obstack = &cu->objfile->objfile_obstack;
15337
15338 if (attr == NULL || prop == NULL)
15339 return 0;
15340
15341 if (attr_form_is_block (attr))
15342 {
8d749320 15343 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15344 baton->referenced_type = NULL;
15345 baton->locexpr.per_cu = cu->per_cu;
15346 baton->locexpr.size = DW_BLOCK (attr)->size;
15347 baton->locexpr.data = DW_BLOCK (attr)->data;
15348 prop->data.baton = baton;
15349 prop->kind = PROP_LOCEXPR;
15350 gdb_assert (prop->data.baton != NULL);
15351 }
15352 else if (attr_form_is_ref (attr))
15353 {
15354 struct dwarf2_cu *target_cu = cu;
15355 struct die_info *target_die;
15356 struct attribute *target_attr;
15357
15358 target_die = follow_die_ref (die, attr, &target_cu);
15359 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15360 if (target_attr == NULL)
15361 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15362 target_cu);
80180f79
SA
15363 if (target_attr == NULL)
15364 return 0;
15365
df25ebbd 15366 switch (target_attr->name)
80180f79 15367 {
df25ebbd
JB
15368 case DW_AT_location:
15369 if (attr_form_is_section_offset (target_attr))
15370 {
8d749320 15371 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15372 baton->referenced_type = die_type (target_die, target_cu);
15373 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15374 prop->data.baton = baton;
15375 prop->kind = PROP_LOCLIST;
15376 gdb_assert (prop->data.baton != NULL);
15377 }
15378 else if (attr_form_is_block (target_attr))
15379 {
8d749320 15380 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15381 baton->referenced_type = die_type (target_die, target_cu);
15382 baton->locexpr.per_cu = cu->per_cu;
15383 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15384 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15385 prop->data.baton = baton;
15386 prop->kind = PROP_LOCEXPR;
15387 gdb_assert (prop->data.baton != NULL);
15388 }
15389 else
15390 {
15391 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15392 "dynamic property");
15393 return 0;
15394 }
15395 break;
15396 case DW_AT_data_member_location:
15397 {
15398 LONGEST offset;
15399
15400 if (!handle_data_member_location (target_die, target_cu,
15401 &offset))
15402 return 0;
15403
8d749320 15404 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15405 baton->referenced_type = read_type_die (target_die->parent,
15406 target_cu);
df25ebbd
JB
15407 baton->offset_info.offset = offset;
15408 baton->offset_info.type = die_type (target_die, target_cu);
15409 prop->data.baton = baton;
15410 prop->kind = PROP_ADDR_OFFSET;
15411 break;
15412 }
80180f79
SA
15413 }
15414 }
15415 else if (attr_form_is_constant (attr))
15416 {
15417 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15418 prop->kind = PROP_CONST;
15419 }
15420 else
15421 {
15422 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15423 dwarf2_name (die, cu));
15424 return 0;
15425 }
15426
15427 return 1;
15428}
15429
a02abb62
JB
15430/* Read the given DW_AT_subrange DIE. */
15431
f792889a 15432static struct type *
a02abb62
JB
15433read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15434{
4c9ad8c2 15435 struct type *base_type, *orig_base_type;
a02abb62
JB
15436 struct type *range_type;
15437 struct attribute *attr;
729efb13 15438 struct dynamic_prop low, high;
4fae6e18 15439 int low_default_is_valid;
c451ebe5 15440 int high_bound_is_count = 0;
15d034d0 15441 const char *name;
43bbcdc2 15442 LONGEST negative_mask;
e77813c8 15443
4c9ad8c2
TT
15444 orig_base_type = die_type (die, cu);
15445 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15446 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15447 creating the range type, but we use the result of check_typedef
15448 when examining properties of the type. */
15449 base_type = check_typedef (orig_base_type);
a02abb62 15450
7e314c57
JK
15451 /* The die_type call above may have already set the type for this DIE. */
15452 range_type = get_die_type (die, cu);
15453 if (range_type)
15454 return range_type;
15455
729efb13
SA
15456 low.kind = PROP_CONST;
15457 high.kind = PROP_CONST;
15458 high.data.const_val = 0;
15459
4fae6e18
JK
15460 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15461 omitting DW_AT_lower_bound. */
15462 switch (cu->language)
6e70227d 15463 {
4fae6e18
JK
15464 case language_c:
15465 case language_cplus:
729efb13 15466 low.data.const_val = 0;
4fae6e18
JK
15467 low_default_is_valid = 1;
15468 break;
15469 case language_fortran:
729efb13 15470 low.data.const_val = 1;
4fae6e18
JK
15471 low_default_is_valid = 1;
15472 break;
15473 case language_d:
4fae6e18 15474 case language_objc:
c44af4eb 15475 case language_rust:
729efb13 15476 low.data.const_val = 0;
4fae6e18
JK
15477 low_default_is_valid = (cu->header.version >= 4);
15478 break;
15479 case language_ada:
15480 case language_m2:
15481 case language_pascal:
729efb13 15482 low.data.const_val = 1;
4fae6e18
JK
15483 low_default_is_valid = (cu->header.version >= 4);
15484 break;
15485 default:
729efb13 15486 low.data.const_val = 0;
4fae6e18
JK
15487 low_default_is_valid = 0;
15488 break;
a02abb62
JB
15489 }
15490
e142c38c 15491 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15492 if (attr)
11c1ba78 15493 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15494 else if (!low_default_is_valid)
15495 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15496 "- DIE at 0x%x [in module %s]"),
9c541725 15497 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15498
e142c38c 15499 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15500 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15501 {
15502 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15503 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15504 {
c451ebe5
SA
15505 /* If bounds are constant do the final calculation here. */
15506 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15507 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15508 else
15509 high_bound_is_count = 1;
c2ff108b 15510 }
e77813c8
PM
15511 }
15512
15513 /* Dwarf-2 specifications explicitly allows to create subrange types
15514 without specifying a base type.
15515 In that case, the base type must be set to the type of
15516 the lower bound, upper bound or count, in that order, if any of these
15517 three attributes references an object that has a type.
15518 If no base type is found, the Dwarf-2 specifications say that
15519 a signed integer type of size equal to the size of an address should
15520 be used.
15521 For the following C code: `extern char gdb_int [];'
15522 GCC produces an empty range DIE.
15523 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15524 high bound or count are not yet handled by this code. */
e77813c8
PM
15525 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15526 {
15527 struct objfile *objfile = cu->objfile;
15528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15529 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15530 struct type *int_type = objfile_type (objfile)->builtin_int;
15531
15532 /* Test "int", "long int", and "long long int" objfile types,
15533 and select the first one having a size above or equal to the
15534 architecture address size. */
15535 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15536 base_type = int_type;
15537 else
15538 {
15539 int_type = objfile_type (objfile)->builtin_long;
15540 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15541 base_type = int_type;
15542 else
15543 {
15544 int_type = objfile_type (objfile)->builtin_long_long;
15545 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15546 base_type = int_type;
15547 }
15548 }
15549 }
a02abb62 15550
dbb9c2b1
JB
15551 /* Normally, the DWARF producers are expected to use a signed
15552 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15553 But this is unfortunately not always the case, as witnessed
15554 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15555 is used instead. To work around that ambiguity, we treat
15556 the bounds as signed, and thus sign-extend their values, when
15557 the base type is signed. */
6e70227d 15558 negative_mask =
66c6502d 15559 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15560 if (low.kind == PROP_CONST
15561 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15562 low.data.const_val |= negative_mask;
15563 if (high.kind == PROP_CONST
15564 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15565 high.data.const_val |= negative_mask;
43bbcdc2 15566
729efb13 15567 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15568
c451ebe5
SA
15569 if (high_bound_is_count)
15570 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15571
c2ff108b
JK
15572 /* Ada expects an empty array on no boundary attributes. */
15573 if (attr == NULL && cu->language != language_ada)
729efb13 15574 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15575
39cbfefa
DJ
15576 name = dwarf2_name (die, cu);
15577 if (name)
15578 TYPE_NAME (range_type) = name;
6e70227d 15579
e142c38c 15580 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15581 if (attr)
15582 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15583
7e314c57
JK
15584 set_die_type (die, range_type, cu);
15585
15586 /* set_die_type should be already done. */
b4ba55a1
JB
15587 set_descriptive_type (range_type, die, cu);
15588
7e314c57 15589 return range_type;
a02abb62 15590}
6e70227d 15591
f792889a 15592static struct type *
81a17f79
JB
15593read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15594{
15595 struct type *type;
81a17f79 15596
81a17f79
JB
15597 /* For now, we only support the C meaning of an unspecified type: void. */
15598
19f392bc 15599 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15600 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15601
f792889a 15602 return set_die_type (die, type, cu);
81a17f79 15603}
a02abb62 15604
639d11d3
DC
15605/* Read a single die and all its descendents. Set the die's sibling
15606 field to NULL; set other fields in the die correctly, and set all
15607 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15608 location of the info_ptr after reading all of those dies. PARENT
15609 is the parent of the die in question. */
15610
15611static struct die_info *
dee91e82 15612read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15613 const gdb_byte *info_ptr,
15614 const gdb_byte **new_info_ptr,
dee91e82 15615 struct die_info *parent)
639d11d3
DC
15616{
15617 struct die_info *die;
d521ce57 15618 const gdb_byte *cur_ptr;
639d11d3
DC
15619 int has_children;
15620
bf6af496 15621 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15622 if (die == NULL)
15623 {
15624 *new_info_ptr = cur_ptr;
15625 return NULL;
15626 }
93311388 15627 store_in_ref_table (die, reader->cu);
639d11d3
DC
15628
15629 if (has_children)
bf6af496 15630 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15631 else
15632 {
15633 die->child = NULL;
15634 *new_info_ptr = cur_ptr;
15635 }
15636
15637 die->sibling = NULL;
15638 die->parent = parent;
15639 return die;
15640}
15641
15642/* Read a die, all of its descendents, and all of its siblings; set
15643 all of the fields of all of the dies correctly. Arguments are as
15644 in read_die_and_children. */
15645
15646static struct die_info *
bf6af496 15647read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15648 const gdb_byte *info_ptr,
15649 const gdb_byte **new_info_ptr,
bf6af496 15650 struct die_info *parent)
639d11d3
DC
15651{
15652 struct die_info *first_die, *last_sibling;
d521ce57 15653 const gdb_byte *cur_ptr;
639d11d3 15654
c906108c 15655 cur_ptr = info_ptr;
639d11d3
DC
15656 first_die = last_sibling = NULL;
15657
15658 while (1)
c906108c 15659 {
639d11d3 15660 struct die_info *die
dee91e82 15661 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15662
1d325ec1 15663 if (die == NULL)
c906108c 15664 {
639d11d3
DC
15665 *new_info_ptr = cur_ptr;
15666 return first_die;
c906108c 15667 }
1d325ec1
DJ
15668
15669 if (!first_die)
15670 first_die = die;
c906108c 15671 else
1d325ec1
DJ
15672 last_sibling->sibling = die;
15673
15674 last_sibling = die;
c906108c 15675 }
c906108c
SS
15676}
15677
bf6af496
DE
15678/* Read a die, all of its descendents, and all of its siblings; set
15679 all of the fields of all of the dies correctly. Arguments are as
15680 in read_die_and_children.
15681 This the main entry point for reading a DIE and all its children. */
15682
15683static struct die_info *
15684read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15685 const gdb_byte *info_ptr,
15686 const gdb_byte **new_info_ptr,
bf6af496
DE
15687 struct die_info *parent)
15688{
15689 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15690 new_info_ptr, parent);
15691
b4f54984 15692 if (dwarf_die_debug)
bf6af496
DE
15693 {
15694 fprintf_unfiltered (gdb_stdlog,
15695 "Read die from %s@0x%x of %s:\n",
a32a8923 15696 get_section_name (reader->die_section),
bf6af496
DE
15697 (unsigned) (info_ptr - reader->die_section->buffer),
15698 bfd_get_filename (reader->abfd));
b4f54984 15699 dump_die (die, dwarf_die_debug);
bf6af496
DE
15700 }
15701
15702 return die;
15703}
15704
3019eac3
DE
15705/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15706 attributes.
15707 The caller is responsible for filling in the extra attributes
15708 and updating (*DIEP)->num_attrs.
15709 Set DIEP to point to a newly allocated die with its information,
15710 except for its child, sibling, and parent fields.
15711 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15712
d521ce57 15713static const gdb_byte *
3019eac3 15714read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15715 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15716 int *has_children, int num_extra_attrs)
93311388 15717{
b64f50a1 15718 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15719 struct abbrev_info *abbrev;
15720 struct die_info *die;
15721 struct dwarf2_cu *cu = reader->cu;
15722 bfd *abfd = reader->abfd;
15723
9c541725 15724 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15725 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15726 info_ptr += bytes_read;
15727 if (!abbrev_number)
15728 {
15729 *diep = NULL;
15730 *has_children = 0;
15731 return info_ptr;
15732 }
15733
433df2d4 15734 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15735 if (!abbrev)
348e048f
DE
15736 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15737 abbrev_number,
15738 bfd_get_filename (abfd));
15739
3019eac3 15740 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15741 die->sect_off = sect_off;
93311388
DE
15742 die->tag = abbrev->tag;
15743 die->abbrev = abbrev_number;
15744
3019eac3
DE
15745 /* Make the result usable.
15746 The caller needs to update num_attrs after adding the extra
15747 attributes. */
93311388
DE
15748 die->num_attrs = abbrev->num_attrs;
15749
15750 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15751 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15752 info_ptr);
93311388
DE
15753
15754 *diep = die;
15755 *has_children = abbrev->has_children;
15756 return info_ptr;
15757}
15758
3019eac3
DE
15759/* Read a die and all its attributes.
15760 Set DIEP to point to a newly allocated die with its information,
15761 except for its child, sibling, and parent fields.
15762 Set HAS_CHILDREN to tell whether the die has children or not. */
15763
d521ce57 15764static const gdb_byte *
3019eac3 15765read_full_die (const struct die_reader_specs *reader,
d521ce57 15766 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15767 int *has_children)
15768{
d521ce57 15769 const gdb_byte *result;
bf6af496
DE
15770
15771 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15772
b4f54984 15773 if (dwarf_die_debug)
bf6af496
DE
15774 {
15775 fprintf_unfiltered (gdb_stdlog,
15776 "Read die from %s@0x%x of %s:\n",
a32a8923 15777 get_section_name (reader->die_section),
bf6af496
DE
15778 (unsigned) (info_ptr - reader->die_section->buffer),
15779 bfd_get_filename (reader->abfd));
b4f54984 15780 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15781 }
15782
15783 return result;
3019eac3 15784}
433df2d4
DE
15785\f
15786/* Abbreviation tables.
3019eac3 15787
433df2d4 15788 In DWARF version 2, the description of the debugging information is
c906108c
SS
15789 stored in a separate .debug_abbrev section. Before we read any
15790 dies from a section we read in all abbreviations and install them
433df2d4
DE
15791 in a hash table. */
15792
15793/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15794
15795static struct abbrev_info *
15796abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15797{
15798 struct abbrev_info *abbrev;
15799
8d749320 15800 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15801 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15802
433df2d4
DE
15803 return abbrev;
15804}
15805
15806/* Add an abbreviation to the table. */
c906108c
SS
15807
15808static void
433df2d4
DE
15809abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15810 unsigned int abbrev_number,
15811 struct abbrev_info *abbrev)
15812{
15813 unsigned int hash_number;
15814
15815 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15816 abbrev->next = abbrev_table->abbrevs[hash_number];
15817 abbrev_table->abbrevs[hash_number] = abbrev;
15818}
dee91e82 15819
433df2d4
DE
15820/* Look up an abbrev in the table.
15821 Returns NULL if the abbrev is not found. */
15822
15823static struct abbrev_info *
15824abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15825 unsigned int abbrev_number)
c906108c 15826{
433df2d4
DE
15827 unsigned int hash_number;
15828 struct abbrev_info *abbrev;
15829
15830 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15831 abbrev = abbrev_table->abbrevs[hash_number];
15832
15833 while (abbrev)
15834 {
15835 if (abbrev->number == abbrev_number)
15836 return abbrev;
15837 abbrev = abbrev->next;
15838 }
15839 return NULL;
15840}
15841
15842/* Read in an abbrev table. */
15843
15844static struct abbrev_table *
15845abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15846 sect_offset sect_off)
433df2d4
DE
15847{
15848 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15849 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15850 struct abbrev_table *abbrev_table;
d521ce57 15851 const gdb_byte *abbrev_ptr;
c906108c
SS
15852 struct abbrev_info *cur_abbrev;
15853 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15854 unsigned int abbrev_form;
f3dd6933
DJ
15855 struct attr_abbrev *cur_attrs;
15856 unsigned int allocated_attrs;
c906108c 15857
70ba0933 15858 abbrev_table = XNEW (struct abbrev_table);
9c541725 15859 abbrev_table->sect_off = sect_off;
433df2d4 15860 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15861 abbrev_table->abbrevs =
15862 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15863 ABBREV_HASH_SIZE);
433df2d4
DE
15864 memset (abbrev_table->abbrevs, 0,
15865 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15866
433df2d4 15867 dwarf2_read_section (objfile, section);
9c541725 15868 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15869 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15870 abbrev_ptr += bytes_read;
15871
f3dd6933 15872 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15873 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15874
0963b4bd 15875 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15876 while (abbrev_number)
15877 {
433df2d4 15878 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15879
15880 /* read in abbrev header */
15881 cur_abbrev->number = abbrev_number;
aead7601
SM
15882 cur_abbrev->tag
15883 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15884 abbrev_ptr += bytes_read;
15885 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15886 abbrev_ptr += 1;
15887
15888 /* now read in declarations */
22d2f3ab 15889 for (;;)
c906108c 15890 {
43988095
JK
15891 LONGEST implicit_const;
15892
22d2f3ab
JK
15893 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15894 abbrev_ptr += bytes_read;
15895 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15896 abbrev_ptr += bytes_read;
43988095
JK
15897 if (abbrev_form == DW_FORM_implicit_const)
15898 {
15899 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15900 &bytes_read);
15901 abbrev_ptr += bytes_read;
15902 }
15903 else
15904 {
15905 /* Initialize it due to a false compiler warning. */
15906 implicit_const = -1;
15907 }
22d2f3ab
JK
15908
15909 if (abbrev_name == 0)
15910 break;
15911
f3dd6933 15912 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15913 {
f3dd6933
DJ
15914 allocated_attrs += ATTR_ALLOC_CHUNK;
15915 cur_attrs
224c3ddb 15916 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15917 }
ae038cb0 15918
aead7601
SM
15919 cur_attrs[cur_abbrev->num_attrs].name
15920 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15921 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15922 = (enum dwarf_form) abbrev_form;
43988095 15923 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15924 ++cur_abbrev->num_attrs;
c906108c
SS
15925 }
15926
8d749320
SM
15927 cur_abbrev->attrs =
15928 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15929 cur_abbrev->num_attrs);
f3dd6933
DJ
15930 memcpy (cur_abbrev->attrs, cur_attrs,
15931 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15932
433df2d4 15933 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15934
15935 /* Get next abbreviation.
15936 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15937 always properly terminated with an abbrev number of 0.
15938 Exit loop if we encounter an abbreviation which we have
15939 already read (which means we are about to read the abbreviations
15940 for the next compile unit) or if the end of the abbreviation
15941 table is reached. */
433df2d4 15942 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15943 break;
15944 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15945 abbrev_ptr += bytes_read;
433df2d4 15946 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15947 break;
15948 }
f3dd6933
DJ
15949
15950 xfree (cur_attrs);
433df2d4 15951 return abbrev_table;
c906108c
SS
15952}
15953
433df2d4 15954/* Free the resources held by ABBREV_TABLE. */
c906108c 15955
c906108c 15956static void
433df2d4 15957abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15958{
433df2d4
DE
15959 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15960 xfree (abbrev_table);
c906108c
SS
15961}
15962
f4dc4d17
DE
15963/* Same as abbrev_table_free but as a cleanup.
15964 We pass in a pointer to the pointer to the table so that we can
15965 set the pointer to NULL when we're done. It also simplifies
73051182 15966 build_type_psymtabs_1. */
f4dc4d17
DE
15967
15968static void
15969abbrev_table_free_cleanup (void *table_ptr)
15970{
9a3c8263 15971 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15972
15973 if (*abbrev_table_ptr != NULL)
15974 abbrev_table_free (*abbrev_table_ptr);
15975 *abbrev_table_ptr = NULL;
15976}
15977
433df2d4
DE
15978/* Read the abbrev table for CU from ABBREV_SECTION. */
15979
15980static void
15981dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15982 struct dwarf2_section_info *abbrev_section)
c906108c 15983{
433df2d4 15984 cu->abbrev_table =
9c541725 15985 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15986}
c906108c 15987
433df2d4 15988/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15989
433df2d4
DE
15990static void
15991dwarf2_free_abbrev_table (void *ptr_to_cu)
15992{
9a3c8263 15993 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15994
a2ce51a0
DE
15995 if (cu->abbrev_table != NULL)
15996 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15997 /* Set this to NULL so that we SEGV if we try to read it later,
15998 and also because free_comp_unit verifies this is NULL. */
15999 cu->abbrev_table = NULL;
16000}
16001\f
72bf9492
DJ
16002/* Returns nonzero if TAG represents a type that we might generate a partial
16003 symbol for. */
16004
16005static int
16006is_type_tag_for_partial (int tag)
16007{
16008 switch (tag)
16009 {
16010#if 0
16011 /* Some types that would be reasonable to generate partial symbols for,
16012 that we don't at present. */
16013 case DW_TAG_array_type:
16014 case DW_TAG_file_type:
16015 case DW_TAG_ptr_to_member_type:
16016 case DW_TAG_set_type:
16017 case DW_TAG_string_type:
16018 case DW_TAG_subroutine_type:
16019#endif
16020 case DW_TAG_base_type:
16021 case DW_TAG_class_type:
680b30c7 16022 case DW_TAG_interface_type:
72bf9492
DJ
16023 case DW_TAG_enumeration_type:
16024 case DW_TAG_structure_type:
16025 case DW_TAG_subrange_type:
16026 case DW_TAG_typedef:
16027 case DW_TAG_union_type:
16028 return 1;
16029 default:
16030 return 0;
16031 }
16032}
16033
16034/* Load all DIEs that are interesting for partial symbols into memory. */
16035
16036static struct partial_die_info *
dee91e82 16037load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16038 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16039{
dee91e82 16040 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16041 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16042 struct partial_die_info *part_die;
16043 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16044 struct abbrev_info *abbrev;
16045 unsigned int bytes_read;
5afb4e99 16046 unsigned int load_all = 0;
72bf9492
DJ
16047 int nesting_level = 1;
16048
16049 parent_die = NULL;
16050 last_die = NULL;
16051
7adf1e79
DE
16052 gdb_assert (cu->per_cu != NULL);
16053 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16054 load_all = 1;
16055
72bf9492
DJ
16056 cu->partial_dies
16057 = htab_create_alloc_ex (cu->header.length / 12,
16058 partial_die_hash,
16059 partial_die_eq,
16060 NULL,
16061 &cu->comp_unit_obstack,
16062 hashtab_obstack_allocate,
16063 dummy_obstack_deallocate);
16064
8d749320 16065 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16066
16067 while (1)
16068 {
16069 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16070
16071 /* A NULL abbrev means the end of a series of children. */
16072 if (abbrev == NULL)
16073 {
16074 if (--nesting_level == 0)
16075 {
16076 /* PART_DIE was probably the last thing allocated on the
16077 comp_unit_obstack, so we could call obstack_free
16078 here. We don't do that because the waste is small,
16079 and will be cleaned up when we're done with this
16080 compilation unit. This way, we're also more robust
16081 against other users of the comp_unit_obstack. */
16082 return first_die;
16083 }
16084 info_ptr += bytes_read;
16085 last_die = parent_die;
16086 parent_die = parent_die->die_parent;
16087 continue;
16088 }
16089
98bfdba5
PA
16090 /* Check for template arguments. We never save these; if
16091 they're seen, we just mark the parent, and go on our way. */
16092 if (parent_die != NULL
16093 && cu->language == language_cplus
16094 && (abbrev->tag == DW_TAG_template_type_param
16095 || abbrev->tag == DW_TAG_template_value_param))
16096 {
16097 parent_die->has_template_arguments = 1;
16098
16099 if (!load_all)
16100 {
16101 /* We don't need a partial DIE for the template argument. */
dee91e82 16102 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16103 continue;
16104 }
16105 }
16106
0d99eb77 16107 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16108 Skip their other children. */
16109 if (!load_all
16110 && cu->language == language_cplus
16111 && parent_die != NULL
16112 && parent_die->tag == DW_TAG_subprogram)
16113 {
dee91e82 16114 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16115 continue;
16116 }
16117
5afb4e99
DJ
16118 /* Check whether this DIE is interesting enough to save. Normally
16119 we would not be interested in members here, but there may be
16120 later variables referencing them via DW_AT_specification (for
16121 static members). */
16122 if (!load_all
16123 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16124 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16125 && abbrev->tag != DW_TAG_enumerator
16126 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16127 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16128 && abbrev->tag != DW_TAG_variable
5afb4e99 16129 && abbrev->tag != DW_TAG_namespace
f55ee35c 16130 && abbrev->tag != DW_TAG_module
95554aad 16131 && abbrev->tag != DW_TAG_member
74921315
KS
16132 && abbrev->tag != DW_TAG_imported_unit
16133 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16134 {
16135 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16136 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16137 continue;
16138 }
16139
dee91e82
DE
16140 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16141 info_ptr);
72bf9492
DJ
16142
16143 /* This two-pass algorithm for processing partial symbols has a
16144 high cost in cache pressure. Thus, handle some simple cases
16145 here which cover the majority of C partial symbols. DIEs
16146 which neither have specification tags in them, nor could have
16147 specification tags elsewhere pointing at them, can simply be
16148 processed and discarded.
16149
16150 This segment is also optional; scan_partial_symbols and
16151 add_partial_symbol will handle these DIEs if we chain
16152 them in normally. When compilers which do not emit large
16153 quantities of duplicate debug information are more common,
16154 this code can probably be removed. */
16155
16156 /* Any complete simple types at the top level (pretty much all
16157 of them, for a language without namespaces), can be processed
16158 directly. */
16159 if (parent_die == NULL
16160 && part_die->has_specification == 0
16161 && part_die->is_declaration == 0
d8228535 16162 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16163 || part_die->tag == DW_TAG_base_type
16164 || part_die->tag == DW_TAG_subrange_type))
16165 {
16166 if (building_psymtab && part_die->name != NULL)
04a679b8 16167 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16168 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16169 &objfile->static_psymbols,
1762568f 16170 0, cu->language, objfile);
dee91e82 16171 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16172 continue;
16173 }
16174
d8228535
JK
16175 /* The exception for DW_TAG_typedef with has_children above is
16176 a workaround of GCC PR debug/47510. In the case of this complaint
16177 type_name_no_tag_or_error will error on such types later.
16178
16179 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16180 it could not find the child DIEs referenced later, this is checked
16181 above. In correct DWARF DW_TAG_typedef should have no children. */
16182
16183 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16184 complaint (&symfile_complaints,
16185 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16186 "- DIE at 0x%x [in module %s]"),
9c541725 16187 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16188
72bf9492
DJ
16189 /* If we're at the second level, and we're an enumerator, and
16190 our parent has no specification (meaning possibly lives in a
16191 namespace elsewhere), then we can add the partial symbol now
16192 instead of queueing it. */
16193 if (part_die->tag == DW_TAG_enumerator
16194 && parent_die != NULL
16195 && parent_die->die_parent == NULL
16196 && parent_die->tag == DW_TAG_enumeration_type
16197 && parent_die->has_specification == 0)
16198 {
16199 if (part_die->name == NULL)
3e43a32a
MS
16200 complaint (&symfile_complaints,
16201 _("malformed enumerator DIE ignored"));
72bf9492 16202 else if (building_psymtab)
04a679b8 16203 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16204 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16205 cu->language == language_cplus
bb5ed363
DE
16206 ? &objfile->global_psymbols
16207 : &objfile->static_psymbols,
1762568f 16208 0, cu->language, objfile);
72bf9492 16209
dee91e82 16210 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16211 continue;
16212 }
16213
16214 /* We'll save this DIE so link it in. */
16215 part_die->die_parent = parent_die;
16216 part_die->die_sibling = NULL;
16217 part_die->die_child = NULL;
16218
16219 if (last_die && last_die == parent_die)
16220 last_die->die_child = part_die;
16221 else if (last_die)
16222 last_die->die_sibling = part_die;
16223
16224 last_die = part_die;
16225
16226 if (first_die == NULL)
16227 first_die = part_die;
16228
16229 /* Maybe add the DIE to the hash table. Not all DIEs that we
16230 find interesting need to be in the hash table, because we
16231 also have the parent/sibling/child chains; only those that we
16232 might refer to by offset later during partial symbol reading.
16233
16234 For now this means things that might have be the target of a
16235 DW_AT_specification, DW_AT_abstract_origin, or
16236 DW_AT_extension. DW_AT_extension will refer only to
16237 namespaces; DW_AT_abstract_origin refers to functions (and
16238 many things under the function DIE, but we do not recurse
16239 into function DIEs during partial symbol reading) and
16240 possibly variables as well; DW_AT_specification refers to
16241 declarations. Declarations ought to have the DW_AT_declaration
16242 flag. It happens that GCC forgets to put it in sometimes, but
16243 only for functions, not for types.
16244
16245 Adding more things than necessary to the hash table is harmless
16246 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16247 wasted time in find_partial_die, when we reread the compilation
16248 unit with load_all_dies set. */
72bf9492 16249
5afb4e99 16250 if (load_all
72929c62 16251 || abbrev->tag == DW_TAG_constant
5afb4e99 16252 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16253 || abbrev->tag == DW_TAG_variable
16254 || abbrev->tag == DW_TAG_namespace
16255 || part_die->is_declaration)
16256 {
16257 void **slot;
16258
16259 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16260 to_underlying (part_die->sect_off),
16261 INSERT);
72bf9492
DJ
16262 *slot = part_die;
16263 }
16264
8d749320 16265 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16266
16267 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16268 we have no reason to follow the children of structures; for other
98bfdba5
PA
16269 languages we have to, so that we can get at method physnames
16270 to infer fully qualified class names, for DW_AT_specification,
16271 and for C++ template arguments. For C++, we also look one level
16272 inside functions to find template arguments (if the name of the
16273 function does not already contain the template arguments).
bc30ff58
JB
16274
16275 For Ada, we need to scan the children of subprograms and lexical
16276 blocks as well because Ada allows the definition of nested
16277 entities that could be interesting for the debugger, such as
16278 nested subprograms for instance. */
72bf9492 16279 if (last_die->has_children
5afb4e99
DJ
16280 && (load_all
16281 || last_die->tag == DW_TAG_namespace
f55ee35c 16282 || last_die->tag == DW_TAG_module
72bf9492 16283 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16284 || (cu->language == language_cplus
16285 && last_die->tag == DW_TAG_subprogram
16286 && (last_die->name == NULL
16287 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16288 || (cu->language != language_c
16289 && (last_die->tag == DW_TAG_class_type
680b30c7 16290 || last_die->tag == DW_TAG_interface_type
72bf9492 16291 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16292 || last_die->tag == DW_TAG_union_type))
16293 || (cu->language == language_ada
16294 && (last_die->tag == DW_TAG_subprogram
16295 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16296 {
16297 nesting_level++;
16298 parent_die = last_die;
16299 continue;
16300 }
16301
16302 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16303 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16304
16305 /* Back to the top, do it again. */
16306 }
16307}
16308
c906108c
SS
16309/* Read a minimal amount of information into the minimal die structure. */
16310
d521ce57 16311static const gdb_byte *
dee91e82
DE
16312read_partial_die (const struct die_reader_specs *reader,
16313 struct partial_die_info *part_die,
16314 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16315 const gdb_byte *info_ptr)
c906108c 16316{
dee91e82 16317 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16318 struct objfile *objfile = cu->objfile;
d521ce57 16319 const gdb_byte *buffer = reader->buffer;
fa238c03 16320 unsigned int i;
c906108c 16321 struct attribute attr;
c5aa993b 16322 int has_low_pc_attr = 0;
c906108c 16323 int has_high_pc_attr = 0;
91da1414 16324 int high_pc_relative = 0;
c906108c 16325
72bf9492 16326 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16327
9c541725 16328 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16329
16330 info_ptr += abbrev_len;
16331
16332 if (abbrev == NULL)
16333 return info_ptr;
16334
c906108c
SS
16335 part_die->tag = abbrev->tag;
16336 part_die->has_children = abbrev->has_children;
c906108c
SS
16337
16338 for (i = 0; i < abbrev->num_attrs; ++i)
16339 {
dee91e82 16340 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16341
16342 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16343 partial symbol table. */
c906108c
SS
16344 switch (attr.name)
16345 {
16346 case DW_AT_name:
71c25dea
TT
16347 switch (part_die->tag)
16348 {
16349 case DW_TAG_compile_unit:
95554aad 16350 case DW_TAG_partial_unit:
348e048f 16351 case DW_TAG_type_unit:
71c25dea
TT
16352 /* Compilation units have a DW_AT_name that is a filename, not
16353 a source language identifier. */
16354 case DW_TAG_enumeration_type:
16355 case DW_TAG_enumerator:
16356 /* These tags always have simple identifiers already; no need
16357 to canonicalize them. */
16358 part_die->name = DW_STRING (&attr);
16359 break;
16360 default:
16361 part_die->name
16362 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16363 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16364 break;
16365 }
c906108c 16366 break;
31ef98ae 16367 case DW_AT_linkage_name:
c906108c 16368 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16369 /* Note that both forms of linkage name might appear. We
16370 assume they will be the same, and we only store the last
16371 one we see. */
94af9270
KS
16372 if (cu->language == language_ada)
16373 part_die->name = DW_STRING (&attr);
abc72ce4 16374 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16375 break;
16376 case DW_AT_low_pc:
16377 has_low_pc_attr = 1;
31aa7e4e 16378 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16379 break;
16380 case DW_AT_high_pc:
16381 has_high_pc_attr = 1;
31aa7e4e
JB
16382 part_die->highpc = attr_value_as_address (&attr);
16383 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16384 high_pc_relative = 1;
c906108c
SS
16385 break;
16386 case DW_AT_location:
0963b4bd 16387 /* Support the .debug_loc offsets. */
8e19ed76
PS
16388 if (attr_form_is_block (&attr))
16389 {
95554aad 16390 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16391 }
3690dd37 16392 else if (attr_form_is_section_offset (&attr))
8e19ed76 16393 {
4d3c2250 16394 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16395 }
16396 else
16397 {
4d3c2250
KB
16398 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16399 "partial symbol information");
8e19ed76 16400 }
c906108c 16401 break;
c906108c
SS
16402 case DW_AT_external:
16403 part_die->is_external = DW_UNSND (&attr);
16404 break;
16405 case DW_AT_declaration:
16406 part_die->is_declaration = DW_UNSND (&attr);
16407 break;
16408 case DW_AT_type:
16409 part_die->has_type = 1;
16410 break;
16411 case DW_AT_abstract_origin:
16412 case DW_AT_specification:
72bf9492
DJ
16413 case DW_AT_extension:
16414 part_die->has_specification = 1;
c764a876 16415 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16416 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16417 || cu->per_cu->is_dwz);
c906108c
SS
16418 break;
16419 case DW_AT_sibling:
16420 /* Ignore absolute siblings, they might point outside of
16421 the current compile unit. */
16422 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16423 complaint (&symfile_complaints,
16424 _("ignoring absolute DW_AT_sibling"));
c906108c 16425 else
b9502d3f 16426 {
9c541725
PA
16427 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16428 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16429
16430 if (sibling_ptr < info_ptr)
16431 complaint (&symfile_complaints,
16432 _("DW_AT_sibling points backwards"));
22869d73
KS
16433 else if (sibling_ptr > reader->buffer_end)
16434 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16435 else
16436 part_die->sibling = sibling_ptr;
16437 }
c906108c 16438 break;
fa4028e9
JB
16439 case DW_AT_byte_size:
16440 part_die->has_byte_size = 1;
16441 break;
ff908ebf
AW
16442 case DW_AT_const_value:
16443 part_die->has_const_value = 1;
16444 break;
68511cec
CES
16445 case DW_AT_calling_convention:
16446 /* DWARF doesn't provide a way to identify a program's source-level
16447 entry point. DW_AT_calling_convention attributes are only meant
16448 to describe functions' calling conventions.
16449
16450 However, because it's a necessary piece of information in
0c1b455e
TT
16451 Fortran, and before DWARF 4 DW_CC_program was the only
16452 piece of debugging information whose definition refers to
16453 a 'main program' at all, several compilers marked Fortran
16454 main programs with DW_CC_program --- even when those
16455 functions use the standard calling conventions.
16456
16457 Although DWARF now specifies a way to provide this
16458 information, we support this practice for backward
16459 compatibility. */
68511cec 16460 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16461 && cu->language == language_fortran)
16462 part_die->main_subprogram = 1;
68511cec 16463 break;
481860b3
GB
16464 case DW_AT_inline:
16465 if (DW_UNSND (&attr) == DW_INL_inlined
16466 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16467 part_die->may_be_inlined = 1;
16468 break;
95554aad
TT
16469
16470 case DW_AT_import:
16471 if (part_die->tag == DW_TAG_imported_unit)
36586728 16472 {
9c541725 16473 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16474 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16475 || cu->per_cu->is_dwz);
16476 }
95554aad
TT
16477 break;
16478
0c1b455e
TT
16479 case DW_AT_main_subprogram:
16480 part_die->main_subprogram = DW_UNSND (&attr);
16481 break;
16482
c906108c
SS
16483 default:
16484 break;
16485 }
16486 }
16487
91da1414
MW
16488 if (high_pc_relative)
16489 part_die->highpc += part_die->lowpc;
16490
9373cf26
JK
16491 if (has_low_pc_attr && has_high_pc_attr)
16492 {
16493 /* When using the GNU linker, .gnu.linkonce. sections are used to
16494 eliminate duplicate copies of functions and vtables and such.
16495 The linker will arbitrarily choose one and discard the others.
16496 The AT_*_pc values for such functions refer to local labels in
16497 these sections. If the section from that file was discarded, the
16498 labels are not in the output, so the relocs get a value of 0.
16499 If this is a discarded function, mark the pc bounds as invalid,
16500 so that GDB will ignore it. */
16501 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16502 {
bb5ed363 16503 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16504
16505 complaint (&symfile_complaints,
16506 _("DW_AT_low_pc %s is zero "
16507 "for DIE at 0x%x [in module %s]"),
16508 paddress (gdbarch, part_die->lowpc),
9c541725 16509 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16510 }
16511 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16512 else if (part_die->lowpc >= part_die->highpc)
16513 {
bb5ed363 16514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16515
16516 complaint (&symfile_complaints,
16517 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16518 "for DIE at 0x%x [in module %s]"),
16519 paddress (gdbarch, part_die->lowpc),
16520 paddress (gdbarch, part_die->highpc),
9c541725
PA
16521 to_underlying (part_die->sect_off),
16522 objfile_name (objfile));
9373cf26
JK
16523 }
16524 else
16525 part_die->has_pc_info = 1;
16526 }
85cbf3d3 16527
c906108c
SS
16528 return info_ptr;
16529}
16530
72bf9492
DJ
16531/* Find a cached partial DIE at OFFSET in CU. */
16532
16533static struct partial_die_info *
9c541725 16534find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16535{
16536 struct partial_die_info *lookup_die = NULL;
16537 struct partial_die_info part_die;
16538
9c541725 16539 part_die.sect_off = sect_off;
9a3c8263
SM
16540 lookup_die = ((struct partial_die_info *)
16541 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16542 to_underlying (sect_off)));
72bf9492 16543
72bf9492
DJ
16544 return lookup_die;
16545}
16546
348e048f
DE
16547/* Find a partial DIE at OFFSET, which may or may not be in CU,
16548 except in the case of .debug_types DIEs which do not reference
16549 outside their CU (they do however referencing other types via
55f1336d 16550 DW_FORM_ref_sig8). */
72bf9492
DJ
16551
16552static struct partial_die_info *
9c541725 16553find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16554{
bb5ed363 16555 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16556 struct dwarf2_per_cu_data *per_cu = NULL;
16557 struct partial_die_info *pd = NULL;
72bf9492 16558
36586728 16559 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16560 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16561 {
9c541725 16562 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16563 if (pd != NULL)
16564 return pd;
0d99eb77
DE
16565 /* We missed recording what we needed.
16566 Load all dies and try again. */
16567 per_cu = cu->per_cu;
5afb4e99 16568 }
0d99eb77
DE
16569 else
16570 {
16571 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16572 if (cu->per_cu->is_debug_types)
0d99eb77 16573 {
9c541725
PA
16574 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16575 " external reference to offset 0x%x [in module %s].\n"),
16576 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16577 bfd_get_filename (objfile->obfd));
16578 }
9c541725 16579 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16580 objfile);
72bf9492 16581
0d99eb77
DE
16582 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16583 load_partial_comp_unit (per_cu);
ae038cb0 16584
0d99eb77 16585 per_cu->cu->last_used = 0;
9c541725 16586 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16587 }
5afb4e99 16588
dee91e82
DE
16589 /* If we didn't find it, and not all dies have been loaded,
16590 load them all and try again. */
16591
5afb4e99
DJ
16592 if (pd == NULL && per_cu->load_all_dies == 0)
16593 {
5afb4e99 16594 per_cu->load_all_dies = 1;
fd820528
DE
16595
16596 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16597 THIS_CU->cu may already be in use. So we can't just free it and
16598 replace its DIEs with the ones we read in. Instead, we leave those
16599 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16600 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16601 set. */
dee91e82 16602 load_partial_comp_unit (per_cu);
5afb4e99 16603
9c541725 16604 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16605 }
16606
16607 if (pd == NULL)
16608 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16609 _("could not find partial DIE 0x%x "
16610 "in cache [from module %s]\n"),
9c541725 16611 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16612 return pd;
72bf9492
DJ
16613}
16614
abc72ce4
DE
16615/* See if we can figure out if the class lives in a namespace. We do
16616 this by looking for a member function; its demangled name will
16617 contain namespace info, if there is any. */
16618
16619static void
16620guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16621 struct dwarf2_cu *cu)
16622{
16623 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16624 what template types look like, because the demangler
16625 frequently doesn't give the same name as the debug info. We
16626 could fix this by only using the demangled name to get the
16627 prefix (but see comment in read_structure_type). */
16628
16629 struct partial_die_info *real_pdi;
16630 struct partial_die_info *child_pdi;
16631
16632 /* If this DIE (this DIE's specification, if any) has a parent, then
16633 we should not do this. We'll prepend the parent's fully qualified
16634 name when we create the partial symbol. */
16635
16636 real_pdi = struct_pdi;
16637 while (real_pdi->has_specification)
36586728
TT
16638 real_pdi = find_partial_die (real_pdi->spec_offset,
16639 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16640
16641 if (real_pdi->die_parent != NULL)
16642 return;
16643
16644 for (child_pdi = struct_pdi->die_child;
16645 child_pdi != NULL;
16646 child_pdi = child_pdi->die_sibling)
16647 {
16648 if (child_pdi->tag == DW_TAG_subprogram
16649 && child_pdi->linkage_name != NULL)
16650 {
16651 char *actual_class_name
16652 = language_class_name_from_physname (cu->language_defn,
16653 child_pdi->linkage_name);
16654 if (actual_class_name != NULL)
16655 {
16656 struct_pdi->name
224c3ddb
SM
16657 = ((const char *)
16658 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16659 actual_class_name,
16660 strlen (actual_class_name)));
abc72ce4
DE
16661 xfree (actual_class_name);
16662 }
16663 break;
16664 }
16665 }
16666}
16667
72bf9492
DJ
16668/* Adjust PART_DIE before generating a symbol for it. This function
16669 may set the is_external flag or change the DIE's name. */
16670
16671static void
16672fixup_partial_die (struct partial_die_info *part_die,
16673 struct dwarf2_cu *cu)
16674{
abc72ce4
DE
16675 /* Once we've fixed up a die, there's no point in doing so again.
16676 This also avoids a memory leak if we were to call
16677 guess_partial_die_structure_name multiple times. */
16678 if (part_die->fixup_called)
16679 return;
16680
72bf9492
DJ
16681 /* If we found a reference attribute and the DIE has no name, try
16682 to find a name in the referred to DIE. */
16683
16684 if (part_die->name == NULL && part_die->has_specification)
16685 {
16686 struct partial_die_info *spec_die;
72bf9492 16687
36586728
TT
16688 spec_die = find_partial_die (part_die->spec_offset,
16689 part_die->spec_is_dwz, cu);
72bf9492 16690
10b3939b 16691 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16692
16693 if (spec_die->name)
16694 {
16695 part_die->name = spec_die->name;
16696
16697 /* Copy DW_AT_external attribute if it is set. */
16698 if (spec_die->is_external)
16699 part_die->is_external = spec_die->is_external;
16700 }
16701 }
16702
16703 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16704
16705 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16706 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16707
abc72ce4
DE
16708 /* If there is no parent die to provide a namespace, and there are
16709 children, see if we can determine the namespace from their linkage
122d1940 16710 name. */
abc72ce4 16711 if (cu->language == language_cplus
8b70b953 16712 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16713 && part_die->die_parent == NULL
16714 && part_die->has_children
16715 && (part_die->tag == DW_TAG_class_type
16716 || part_die->tag == DW_TAG_structure_type
16717 || part_die->tag == DW_TAG_union_type))
16718 guess_partial_die_structure_name (part_die, cu);
16719
53832f31
TT
16720 /* GCC might emit a nameless struct or union that has a linkage
16721 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16722 if (part_die->name == NULL
96408a79
SA
16723 && (part_die->tag == DW_TAG_class_type
16724 || part_die->tag == DW_TAG_interface_type
16725 || part_die->tag == DW_TAG_structure_type
16726 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16727 && part_die->linkage_name != NULL)
16728 {
16729 char *demangled;
16730
8de20a37 16731 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16732 if (demangled)
16733 {
96408a79
SA
16734 const char *base;
16735
16736 /* Strip any leading namespaces/classes, keep only the base name.
16737 DW_AT_name for named DIEs does not contain the prefixes. */
16738 base = strrchr (demangled, ':');
16739 if (base && base > demangled && base[-1] == ':')
16740 base++;
16741 else
16742 base = demangled;
16743
34a68019 16744 part_die->name
224c3ddb
SM
16745 = ((const char *)
16746 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16747 base, strlen (base)));
53832f31
TT
16748 xfree (demangled);
16749 }
16750 }
16751
abc72ce4 16752 part_die->fixup_called = 1;
72bf9492
DJ
16753}
16754
a8329558 16755/* Read an attribute value described by an attribute form. */
c906108c 16756
d521ce57 16757static const gdb_byte *
dee91e82
DE
16758read_attribute_value (const struct die_reader_specs *reader,
16759 struct attribute *attr, unsigned form,
43988095 16760 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16761{
dee91e82 16762 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16763 struct objfile *objfile = cu->objfile;
16764 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16765 bfd *abfd = reader->abfd;
e7c27a73 16766 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16767 unsigned int bytes_read;
16768 struct dwarf_block *blk;
16769
aead7601 16770 attr->form = (enum dwarf_form) form;
a8329558 16771 switch (form)
c906108c 16772 {
c906108c 16773 case DW_FORM_ref_addr:
ae411497 16774 if (cu->header.version == 2)
4568ecf9 16775 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16776 else
4568ecf9
DE
16777 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16778 &cu->header, &bytes_read);
ae411497
TT
16779 info_ptr += bytes_read;
16780 break;
36586728
TT
16781 case DW_FORM_GNU_ref_alt:
16782 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16783 info_ptr += bytes_read;
16784 break;
ae411497 16785 case DW_FORM_addr:
e7c27a73 16786 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16787 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16788 info_ptr += bytes_read;
c906108c
SS
16789 break;
16790 case DW_FORM_block2:
7b5a2f43 16791 blk = dwarf_alloc_block (cu);
c906108c
SS
16792 blk->size = read_2_bytes (abfd, info_ptr);
16793 info_ptr += 2;
16794 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16795 info_ptr += blk->size;
16796 DW_BLOCK (attr) = blk;
16797 break;
16798 case DW_FORM_block4:
7b5a2f43 16799 blk = dwarf_alloc_block (cu);
c906108c
SS
16800 blk->size = read_4_bytes (abfd, info_ptr);
16801 info_ptr += 4;
16802 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16803 info_ptr += blk->size;
16804 DW_BLOCK (attr) = blk;
16805 break;
16806 case DW_FORM_data2:
16807 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16808 info_ptr += 2;
16809 break;
16810 case DW_FORM_data4:
16811 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16812 info_ptr += 4;
16813 break;
16814 case DW_FORM_data8:
16815 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16816 info_ptr += 8;
16817 break;
0224619f
JK
16818 case DW_FORM_data16:
16819 blk = dwarf_alloc_block (cu);
16820 blk->size = 16;
16821 blk->data = read_n_bytes (abfd, info_ptr, 16);
16822 info_ptr += 16;
16823 DW_BLOCK (attr) = blk;
16824 break;
2dc7f7b3
TT
16825 case DW_FORM_sec_offset:
16826 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16827 info_ptr += bytes_read;
16828 break;
c906108c 16829 case DW_FORM_string:
9b1c24c8 16830 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16831 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16832 info_ptr += bytes_read;
16833 break;
4bdf3d34 16834 case DW_FORM_strp:
36586728
TT
16835 if (!cu->per_cu->is_dwz)
16836 {
16837 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16838 &bytes_read);
16839 DW_STRING_IS_CANONICAL (attr) = 0;
16840 info_ptr += bytes_read;
16841 break;
16842 }
16843 /* FALLTHROUGH */
43988095
JK
16844 case DW_FORM_line_strp:
16845 if (!cu->per_cu->is_dwz)
16846 {
16847 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16848 cu_header, &bytes_read);
16849 DW_STRING_IS_CANONICAL (attr) = 0;
16850 info_ptr += bytes_read;
16851 break;
16852 }
16853 /* FALLTHROUGH */
36586728
TT
16854 case DW_FORM_GNU_strp_alt:
16855 {
16856 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16857 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16858 &bytes_read);
16859
16860 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16861 DW_STRING_IS_CANONICAL (attr) = 0;
16862 info_ptr += bytes_read;
16863 }
4bdf3d34 16864 break;
2dc7f7b3 16865 case DW_FORM_exprloc:
c906108c 16866 case DW_FORM_block:
7b5a2f43 16867 blk = dwarf_alloc_block (cu);
c906108c
SS
16868 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16869 info_ptr += bytes_read;
16870 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16871 info_ptr += blk->size;
16872 DW_BLOCK (attr) = blk;
16873 break;
16874 case DW_FORM_block1:
7b5a2f43 16875 blk = dwarf_alloc_block (cu);
c906108c
SS
16876 blk->size = read_1_byte (abfd, info_ptr);
16877 info_ptr += 1;
16878 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16879 info_ptr += blk->size;
16880 DW_BLOCK (attr) = blk;
16881 break;
16882 case DW_FORM_data1:
16883 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16884 info_ptr += 1;
16885 break;
16886 case DW_FORM_flag:
16887 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16888 info_ptr += 1;
16889 break;
2dc7f7b3
TT
16890 case DW_FORM_flag_present:
16891 DW_UNSND (attr) = 1;
16892 break;
c906108c
SS
16893 case DW_FORM_sdata:
16894 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16895 info_ptr += bytes_read;
16896 break;
16897 case DW_FORM_udata:
16898 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16899 info_ptr += bytes_read;
16900 break;
16901 case DW_FORM_ref1:
9c541725 16902 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16903 + read_1_byte (abfd, info_ptr));
c906108c
SS
16904 info_ptr += 1;
16905 break;
16906 case DW_FORM_ref2:
9c541725 16907 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16908 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16909 info_ptr += 2;
16910 break;
16911 case DW_FORM_ref4:
9c541725 16912 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16913 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16914 info_ptr += 4;
16915 break;
613e1657 16916 case DW_FORM_ref8:
9c541725 16917 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16918 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16919 info_ptr += 8;
16920 break;
55f1336d 16921 case DW_FORM_ref_sig8:
ac9ec31b 16922 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16923 info_ptr += 8;
16924 break;
c906108c 16925 case DW_FORM_ref_udata:
9c541725 16926 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16927 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16928 info_ptr += bytes_read;
16929 break;
c906108c 16930 case DW_FORM_indirect:
a8329558
KW
16931 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16932 info_ptr += bytes_read;
43988095
JK
16933 if (form == DW_FORM_implicit_const)
16934 {
16935 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16936 info_ptr += bytes_read;
16937 }
16938 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16939 info_ptr);
16940 break;
16941 case DW_FORM_implicit_const:
16942 DW_SND (attr) = implicit_const;
a8329558 16943 break;
3019eac3
DE
16944 case DW_FORM_GNU_addr_index:
16945 if (reader->dwo_file == NULL)
16946 {
16947 /* For now flag a hard error.
16948 Later we can turn this into a complaint. */
16949 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16950 dwarf_form_name (form),
16951 bfd_get_filename (abfd));
16952 }
16953 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16954 info_ptr += bytes_read;
16955 break;
16956 case DW_FORM_GNU_str_index:
16957 if (reader->dwo_file == NULL)
16958 {
16959 /* For now flag a hard error.
16960 Later we can turn this into a complaint if warranted. */
16961 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16962 dwarf_form_name (form),
16963 bfd_get_filename (abfd));
16964 }
16965 {
16966 ULONGEST str_index =
16967 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16968
342587c4 16969 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16970 DW_STRING_IS_CANONICAL (attr) = 0;
16971 info_ptr += bytes_read;
16972 }
16973 break;
c906108c 16974 default:
8a3fe4f8 16975 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16976 dwarf_form_name (form),
16977 bfd_get_filename (abfd));
c906108c 16978 }
28e94949 16979
36586728 16980 /* Super hack. */
7771576e 16981 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16982 attr->form = DW_FORM_GNU_ref_alt;
16983
28e94949
JB
16984 /* We have seen instances where the compiler tried to emit a byte
16985 size attribute of -1 which ended up being encoded as an unsigned
16986 0xffffffff. Although 0xffffffff is technically a valid size value,
16987 an object of this size seems pretty unlikely so we can relatively
16988 safely treat these cases as if the size attribute was invalid and
16989 treat them as zero by default. */
16990 if (attr->name == DW_AT_byte_size
16991 && form == DW_FORM_data4
16992 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16993 {
16994 complaint
16995 (&symfile_complaints,
43bbcdc2
PH
16996 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16997 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16998 DW_UNSND (attr) = 0;
16999 }
28e94949 17000
c906108c
SS
17001 return info_ptr;
17002}
17003
a8329558
KW
17004/* Read an attribute described by an abbreviated attribute. */
17005
d521ce57 17006static const gdb_byte *
dee91e82
DE
17007read_attribute (const struct die_reader_specs *reader,
17008 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17009 const gdb_byte *info_ptr)
a8329558
KW
17010{
17011 attr->name = abbrev->name;
43988095
JK
17012 return read_attribute_value (reader, attr, abbrev->form,
17013 abbrev->implicit_const, info_ptr);
a8329558
KW
17014}
17015
0963b4bd 17016/* Read dwarf information from a buffer. */
c906108c
SS
17017
17018static unsigned int
a1855c1d 17019read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17020{
fe1b8b76 17021 return bfd_get_8 (abfd, buf);
c906108c
SS
17022}
17023
17024static int
a1855c1d 17025read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17026{
fe1b8b76 17027 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17028}
17029
17030static unsigned int
a1855c1d 17031read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17032{
fe1b8b76 17033 return bfd_get_16 (abfd, buf);
c906108c
SS
17034}
17035
21ae7a4d 17036static int
a1855c1d 17037read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17038{
17039 return bfd_get_signed_16 (abfd, buf);
17040}
17041
c906108c 17042static unsigned int
a1855c1d 17043read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17044{
fe1b8b76 17045 return bfd_get_32 (abfd, buf);
c906108c
SS
17046}
17047
21ae7a4d 17048static int
a1855c1d 17049read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17050{
17051 return bfd_get_signed_32 (abfd, buf);
17052}
17053
93311388 17054static ULONGEST
a1855c1d 17055read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17056{
fe1b8b76 17057 return bfd_get_64 (abfd, buf);
c906108c
SS
17058}
17059
17060static CORE_ADDR
d521ce57 17061read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17062 unsigned int *bytes_read)
c906108c 17063{
e7c27a73 17064 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17065 CORE_ADDR retval = 0;
17066
107d2387 17067 if (cu_header->signed_addr_p)
c906108c 17068 {
107d2387
AC
17069 switch (cu_header->addr_size)
17070 {
17071 case 2:
fe1b8b76 17072 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17073 break;
17074 case 4:
fe1b8b76 17075 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17076 break;
17077 case 8:
fe1b8b76 17078 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17079 break;
17080 default:
8e65ff28 17081 internal_error (__FILE__, __LINE__,
e2e0b3e5 17082 _("read_address: bad switch, signed [in module %s]"),
659b0389 17083 bfd_get_filename (abfd));
107d2387
AC
17084 }
17085 }
17086 else
17087 {
17088 switch (cu_header->addr_size)
17089 {
17090 case 2:
fe1b8b76 17091 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17092 break;
17093 case 4:
fe1b8b76 17094 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17095 break;
17096 case 8:
fe1b8b76 17097 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17098 break;
17099 default:
8e65ff28 17100 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17101 _("read_address: bad switch, "
17102 "unsigned [in module %s]"),
659b0389 17103 bfd_get_filename (abfd));
107d2387 17104 }
c906108c 17105 }
64367e0a 17106
107d2387
AC
17107 *bytes_read = cu_header->addr_size;
17108 return retval;
c906108c
SS
17109}
17110
f7ef9339 17111/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17112 specification allows the initial length to take up either 4 bytes
17113 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17114 bytes describe the length and all offsets will be 8 bytes in length
17115 instead of 4.
17116
f7ef9339
KB
17117 An older, non-standard 64-bit format is also handled by this
17118 function. The older format in question stores the initial length
17119 as an 8-byte quantity without an escape value. Lengths greater
17120 than 2^32 aren't very common which means that the initial 4 bytes
17121 is almost always zero. Since a length value of zero doesn't make
17122 sense for the 32-bit format, this initial zero can be considered to
17123 be an escape value which indicates the presence of the older 64-bit
17124 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17125 greater than 4GB. If it becomes necessary to handle lengths
17126 somewhat larger than 4GB, we could allow other small values (such
17127 as the non-sensical values of 1, 2, and 3) to also be used as
17128 escape values indicating the presence of the old format.
f7ef9339 17129
917c78fc
MK
17130 The value returned via bytes_read should be used to increment the
17131 relevant pointer after calling read_initial_length().
c764a876 17132
613e1657
KB
17133 [ Note: read_initial_length() and read_offset() are based on the
17134 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17135 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17136 from:
17137
f7ef9339 17138 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17139
613e1657
KB
17140 This document is only a draft and is subject to change. (So beware.)
17141
f7ef9339 17142 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17143 determined empirically by examining 64-bit ELF files produced by
17144 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17145
17146 - Kevin, July 16, 2002
613e1657
KB
17147 ] */
17148
17149static LONGEST
d521ce57 17150read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17151{
fe1b8b76 17152 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17153
dd373385 17154 if (length == 0xffffffff)
613e1657 17155 {
fe1b8b76 17156 length = bfd_get_64 (abfd, buf + 4);
613e1657 17157 *bytes_read = 12;
613e1657 17158 }
dd373385 17159 else if (length == 0)
f7ef9339 17160 {
dd373385 17161 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17162 length = bfd_get_64 (abfd, buf);
f7ef9339 17163 *bytes_read = 8;
f7ef9339 17164 }
613e1657
KB
17165 else
17166 {
17167 *bytes_read = 4;
613e1657
KB
17168 }
17169
c764a876
DE
17170 return length;
17171}
dd373385 17172
c764a876
DE
17173/* Cover function for read_initial_length.
17174 Returns the length of the object at BUF, and stores the size of the
17175 initial length in *BYTES_READ and stores the size that offsets will be in
17176 *OFFSET_SIZE.
17177 If the initial length size is not equivalent to that specified in
17178 CU_HEADER then issue a complaint.
17179 This is useful when reading non-comp-unit headers. */
dd373385 17180
c764a876 17181static LONGEST
d521ce57 17182read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17183 const struct comp_unit_head *cu_header,
17184 unsigned int *bytes_read,
17185 unsigned int *offset_size)
17186{
17187 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17188
17189 gdb_assert (cu_header->initial_length_size == 4
17190 || cu_header->initial_length_size == 8
17191 || cu_header->initial_length_size == 12);
17192
17193 if (cu_header->initial_length_size != *bytes_read)
17194 complaint (&symfile_complaints,
17195 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17196
c764a876 17197 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17198 return length;
613e1657
KB
17199}
17200
17201/* Read an offset from the data stream. The size of the offset is
917c78fc 17202 given by cu_header->offset_size. */
613e1657
KB
17203
17204static LONGEST
d521ce57
TT
17205read_offset (bfd *abfd, const gdb_byte *buf,
17206 const struct comp_unit_head *cu_header,
891d2f0b 17207 unsigned int *bytes_read)
c764a876
DE
17208{
17209 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17210
c764a876
DE
17211 *bytes_read = cu_header->offset_size;
17212 return offset;
17213}
17214
17215/* Read an offset from the data stream. */
17216
17217static LONGEST
d521ce57 17218read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17219{
17220 LONGEST retval = 0;
17221
c764a876 17222 switch (offset_size)
613e1657
KB
17223 {
17224 case 4:
fe1b8b76 17225 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17226 break;
17227 case 8:
fe1b8b76 17228 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17229 break;
17230 default:
8e65ff28 17231 internal_error (__FILE__, __LINE__,
c764a876 17232 _("read_offset_1: bad switch [in module %s]"),
659b0389 17233 bfd_get_filename (abfd));
613e1657
KB
17234 }
17235
917c78fc 17236 return retval;
613e1657
KB
17237}
17238
d521ce57
TT
17239static const gdb_byte *
17240read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17241{
17242 /* If the size of a host char is 8 bits, we can return a pointer
17243 to the buffer, otherwise we have to copy the data to a buffer
17244 allocated on the temporary obstack. */
4bdf3d34 17245 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17246 return buf;
c906108c
SS
17247}
17248
d521ce57
TT
17249static const char *
17250read_direct_string (bfd *abfd, const gdb_byte *buf,
17251 unsigned int *bytes_read_ptr)
c906108c
SS
17252{
17253 /* If the size of a host char is 8 bits, we can return a pointer
17254 to the string, otherwise we have to copy the string to a buffer
17255 allocated on the temporary obstack. */
4bdf3d34 17256 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17257 if (*buf == '\0')
17258 {
17259 *bytes_read_ptr = 1;
17260 return NULL;
17261 }
d521ce57
TT
17262 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17263 return (const char *) buf;
4bdf3d34
JJ
17264}
17265
43988095
JK
17266/* Return pointer to string at section SECT offset STR_OFFSET with error
17267 reporting strings FORM_NAME and SECT_NAME. */
17268
d521ce57 17269static const char *
43988095
JK
17270read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17271 struct dwarf2_section_info *sect,
17272 const char *form_name,
17273 const char *sect_name)
17274{
17275 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17276 if (sect->buffer == NULL)
17277 error (_("%s used without %s section [in module %s]"),
17278 form_name, sect_name, bfd_get_filename (abfd));
17279 if (str_offset >= sect->size)
17280 error (_("%s pointing outside of %s section [in module %s]"),
17281 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17282 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17283 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17284 return NULL;
43988095
JK
17285 return (const char *) (sect->buffer + str_offset);
17286}
17287
17288/* Return pointer to string at .debug_str offset STR_OFFSET. */
17289
17290static const char *
17291read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17292{
17293 return read_indirect_string_at_offset_from (abfd, str_offset,
17294 &dwarf2_per_objfile->str,
17295 "DW_FORM_strp", ".debug_str");
17296}
17297
17298/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17299
17300static const char *
17301read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17302{
17303 return read_indirect_string_at_offset_from (abfd, str_offset,
17304 &dwarf2_per_objfile->line_str,
17305 "DW_FORM_line_strp",
17306 ".debug_line_str");
c906108c
SS
17307}
17308
36586728
TT
17309/* Read a string at offset STR_OFFSET in the .debug_str section from
17310 the .dwz file DWZ. Throw an error if the offset is too large. If
17311 the string consists of a single NUL byte, return NULL; otherwise
17312 return a pointer to the string. */
17313
d521ce57 17314static const char *
36586728
TT
17315read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17316{
17317 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17318
17319 if (dwz->str.buffer == NULL)
17320 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17321 "section [in module %s]"),
17322 bfd_get_filename (dwz->dwz_bfd));
17323 if (str_offset >= dwz->str.size)
17324 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17325 ".debug_str section [in module %s]"),
17326 bfd_get_filename (dwz->dwz_bfd));
17327 gdb_assert (HOST_CHAR_BIT == 8);
17328 if (dwz->str.buffer[str_offset] == '\0')
17329 return NULL;
d521ce57 17330 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17331}
17332
43988095
JK
17333/* Return pointer to string at .debug_str offset as read from BUF.
17334 BUF is assumed to be in a compilation unit described by CU_HEADER.
17335 Return *BYTES_READ_PTR count of bytes read from BUF. */
17336
d521ce57
TT
17337static const char *
17338read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17339 const struct comp_unit_head *cu_header,
17340 unsigned int *bytes_read_ptr)
17341{
17342 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17343
17344 return read_indirect_string_at_offset (abfd, str_offset);
17345}
17346
43988095
JK
17347/* Return pointer to string at .debug_line_str offset as read from BUF.
17348 BUF is assumed to be in a compilation unit described by CU_HEADER.
17349 Return *BYTES_READ_PTR count of bytes read from BUF. */
17350
17351static const char *
17352read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17353 const struct comp_unit_head *cu_header,
17354 unsigned int *bytes_read_ptr)
17355{
17356 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17357
17358 return read_indirect_line_string_at_offset (abfd, str_offset);
17359}
17360
17361ULONGEST
d521ce57 17362read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17363 unsigned int *bytes_read_ptr)
c906108c 17364{
12df843f 17365 ULONGEST result;
ce5d95e1 17366 unsigned int num_read;
870f88f7 17367 int shift;
c906108c
SS
17368 unsigned char byte;
17369
17370 result = 0;
17371 shift = 0;
17372 num_read = 0;
c906108c
SS
17373 while (1)
17374 {
fe1b8b76 17375 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17376 buf++;
17377 num_read++;
12df843f 17378 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17379 if ((byte & 128) == 0)
17380 {
17381 break;
17382 }
17383 shift += 7;
17384 }
17385 *bytes_read_ptr = num_read;
17386 return result;
17387}
17388
12df843f 17389static LONGEST
d521ce57
TT
17390read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17391 unsigned int *bytes_read_ptr)
c906108c 17392{
12df843f 17393 LONGEST result;
870f88f7 17394 int shift, num_read;
c906108c
SS
17395 unsigned char byte;
17396
17397 result = 0;
17398 shift = 0;
c906108c 17399 num_read = 0;
c906108c
SS
17400 while (1)
17401 {
fe1b8b76 17402 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17403 buf++;
17404 num_read++;
12df843f 17405 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17406 shift += 7;
17407 if ((byte & 128) == 0)
17408 {
17409 break;
17410 }
17411 }
77e0b926 17412 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17413 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17414 *bytes_read_ptr = num_read;
17415 return result;
17416}
17417
3019eac3
DE
17418/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17419 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17420 ADDR_SIZE is the size of addresses from the CU header. */
17421
17422static CORE_ADDR
17423read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17424{
17425 struct objfile *objfile = dwarf2_per_objfile->objfile;
17426 bfd *abfd = objfile->obfd;
17427 const gdb_byte *info_ptr;
17428
17429 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17430 if (dwarf2_per_objfile->addr.buffer == NULL)
17431 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17432 objfile_name (objfile));
3019eac3
DE
17433 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17434 error (_("DW_FORM_addr_index pointing outside of "
17435 ".debug_addr section [in module %s]"),
4262abfb 17436 objfile_name (objfile));
3019eac3
DE
17437 info_ptr = (dwarf2_per_objfile->addr.buffer
17438 + addr_base + addr_index * addr_size);
17439 if (addr_size == 4)
17440 return bfd_get_32 (abfd, info_ptr);
17441 else
17442 return bfd_get_64 (abfd, info_ptr);
17443}
17444
17445/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17446
17447static CORE_ADDR
17448read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17449{
17450 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17451}
17452
17453/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17454
17455static CORE_ADDR
d521ce57 17456read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17457 unsigned int *bytes_read)
17458{
17459 bfd *abfd = cu->objfile->obfd;
17460 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17461
17462 return read_addr_index (cu, addr_index);
17463}
17464
17465/* Data structure to pass results from dwarf2_read_addr_index_reader
17466 back to dwarf2_read_addr_index. */
17467
17468struct dwarf2_read_addr_index_data
17469{
17470 ULONGEST addr_base;
17471 int addr_size;
17472};
17473
17474/* die_reader_func for dwarf2_read_addr_index. */
17475
17476static void
17477dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17478 const gdb_byte *info_ptr,
3019eac3
DE
17479 struct die_info *comp_unit_die,
17480 int has_children,
17481 void *data)
17482{
17483 struct dwarf2_cu *cu = reader->cu;
17484 struct dwarf2_read_addr_index_data *aidata =
17485 (struct dwarf2_read_addr_index_data *) data;
17486
17487 aidata->addr_base = cu->addr_base;
17488 aidata->addr_size = cu->header.addr_size;
17489}
17490
17491/* Given an index in .debug_addr, fetch the value.
17492 NOTE: This can be called during dwarf expression evaluation,
17493 long after the debug information has been read, and thus per_cu->cu
17494 may no longer exist. */
17495
17496CORE_ADDR
17497dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17498 unsigned int addr_index)
17499{
17500 struct objfile *objfile = per_cu->objfile;
17501 struct dwarf2_cu *cu = per_cu->cu;
17502 ULONGEST addr_base;
17503 int addr_size;
17504
17505 /* This is intended to be called from outside this file. */
17506 dw2_setup (objfile);
17507
17508 /* We need addr_base and addr_size.
17509 If we don't have PER_CU->cu, we have to get it.
17510 Nasty, but the alternative is storing the needed info in PER_CU,
17511 which at this point doesn't seem justified: it's not clear how frequently
17512 it would get used and it would increase the size of every PER_CU.
17513 Entry points like dwarf2_per_cu_addr_size do a similar thing
17514 so we're not in uncharted territory here.
17515 Alas we need to be a bit more complicated as addr_base is contained
17516 in the DIE.
17517
17518 We don't need to read the entire CU(/TU).
17519 We just need the header and top level die.
a1b64ce1 17520
3019eac3 17521 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17522 For now we skip this optimization. */
3019eac3
DE
17523
17524 if (cu != NULL)
17525 {
17526 addr_base = cu->addr_base;
17527 addr_size = cu->header.addr_size;
17528 }
17529 else
17530 {
17531 struct dwarf2_read_addr_index_data aidata;
17532
a1b64ce1
DE
17533 /* Note: We can't use init_cutu_and_read_dies_simple here,
17534 we need addr_base. */
17535 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17536 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17537 addr_base = aidata.addr_base;
17538 addr_size = aidata.addr_size;
17539 }
17540
17541 return read_addr_index_1 (addr_index, addr_base, addr_size);
17542}
17543
57d63ce2
DE
17544/* Given a DW_FORM_GNU_str_index, fetch the string.
17545 This is only used by the Fission support. */
3019eac3 17546
d521ce57 17547static const char *
342587c4 17548read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17549{
17550 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17551 const char *objf_name = objfile_name (objfile);
3019eac3 17552 bfd *abfd = objfile->obfd;
342587c4 17553 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17554 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17555 struct dwarf2_section_info *str_offsets_section =
17556 &reader->dwo_file->sections.str_offsets;
d521ce57 17557 const gdb_byte *info_ptr;
3019eac3 17558 ULONGEST str_offset;
57d63ce2 17559 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17560
73869dc2
DE
17561 dwarf2_read_section (objfile, str_section);
17562 dwarf2_read_section (objfile, str_offsets_section);
17563 if (str_section->buffer == NULL)
57d63ce2 17564 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17565 " in CU at offset 0x%x [in module %s]"),
17566 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17567 if (str_offsets_section->buffer == NULL)
57d63ce2 17568 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17569 " in CU at offset 0x%x [in module %s]"),
17570 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17571 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17572 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17573 " section in CU at offset 0x%x [in module %s]"),
17574 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17575 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17576 + str_index * cu->header.offset_size);
17577 if (cu->header.offset_size == 4)
17578 str_offset = bfd_get_32 (abfd, info_ptr);
17579 else
17580 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17581 if (str_offset >= str_section->size)
57d63ce2 17582 error (_("Offset from %s pointing outside of"
9c541725
PA
17583 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17584 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17585 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17586}
17587
3019eac3
DE
17588/* Return the length of an LEB128 number in BUF. */
17589
17590static int
17591leb128_size (const gdb_byte *buf)
17592{
17593 const gdb_byte *begin = buf;
17594 gdb_byte byte;
17595
17596 while (1)
17597 {
17598 byte = *buf++;
17599 if ((byte & 128) == 0)
17600 return buf - begin;
17601 }
17602}
17603
c906108c 17604static void
e142c38c 17605set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17606{
17607 switch (lang)
17608 {
17609 case DW_LANG_C89:
76bee0cc 17610 case DW_LANG_C99:
0cfd832f 17611 case DW_LANG_C11:
c906108c 17612 case DW_LANG_C:
d1be3247 17613 case DW_LANG_UPC:
e142c38c 17614 cu->language = language_c;
c906108c 17615 break;
9c37b5ae 17616 case DW_LANG_Java:
c906108c 17617 case DW_LANG_C_plus_plus:
0cfd832f
MW
17618 case DW_LANG_C_plus_plus_11:
17619 case DW_LANG_C_plus_plus_14:
e142c38c 17620 cu->language = language_cplus;
c906108c 17621 break;
6aecb9c2
JB
17622 case DW_LANG_D:
17623 cu->language = language_d;
17624 break;
c906108c
SS
17625 case DW_LANG_Fortran77:
17626 case DW_LANG_Fortran90:
b21b22e0 17627 case DW_LANG_Fortran95:
f7de9aab
MW
17628 case DW_LANG_Fortran03:
17629 case DW_LANG_Fortran08:
e142c38c 17630 cu->language = language_fortran;
c906108c 17631 break;
a766d390
DE
17632 case DW_LANG_Go:
17633 cu->language = language_go;
17634 break;
c906108c 17635 case DW_LANG_Mips_Assembler:
e142c38c 17636 cu->language = language_asm;
c906108c
SS
17637 break;
17638 case DW_LANG_Ada83:
8aaf0b47 17639 case DW_LANG_Ada95:
bc5f45f8
JB
17640 cu->language = language_ada;
17641 break;
72019c9c
GM
17642 case DW_LANG_Modula2:
17643 cu->language = language_m2;
17644 break;
fe8e67fd
PM
17645 case DW_LANG_Pascal83:
17646 cu->language = language_pascal;
17647 break;
22566fbd
DJ
17648 case DW_LANG_ObjC:
17649 cu->language = language_objc;
17650 break;
c44af4eb
TT
17651 case DW_LANG_Rust:
17652 case DW_LANG_Rust_old:
17653 cu->language = language_rust;
17654 break;
c906108c
SS
17655 case DW_LANG_Cobol74:
17656 case DW_LANG_Cobol85:
c906108c 17657 default:
e142c38c 17658 cu->language = language_minimal;
c906108c
SS
17659 break;
17660 }
e142c38c 17661 cu->language_defn = language_def (cu->language);
c906108c
SS
17662}
17663
17664/* Return the named attribute or NULL if not there. */
17665
17666static struct attribute *
e142c38c 17667dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17668{
a48e046c 17669 for (;;)
c906108c 17670 {
a48e046c
TT
17671 unsigned int i;
17672 struct attribute *spec = NULL;
17673
17674 for (i = 0; i < die->num_attrs; ++i)
17675 {
17676 if (die->attrs[i].name == name)
17677 return &die->attrs[i];
17678 if (die->attrs[i].name == DW_AT_specification
17679 || die->attrs[i].name == DW_AT_abstract_origin)
17680 spec = &die->attrs[i];
17681 }
17682
17683 if (!spec)
17684 break;
c906108c 17685
f2f0e013 17686 die = follow_die_ref (die, spec, &cu);
f2f0e013 17687 }
c5aa993b 17688
c906108c
SS
17689 return NULL;
17690}
17691
348e048f
DE
17692/* Return the named attribute or NULL if not there,
17693 but do not follow DW_AT_specification, etc.
17694 This is for use in contexts where we're reading .debug_types dies.
17695 Following DW_AT_specification, DW_AT_abstract_origin will take us
17696 back up the chain, and we want to go down. */
17697
17698static struct attribute *
45e58e77 17699dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17700{
17701 unsigned int i;
17702
17703 for (i = 0; i < die->num_attrs; ++i)
17704 if (die->attrs[i].name == name)
17705 return &die->attrs[i];
17706
17707 return NULL;
17708}
17709
7d45c7c3
KB
17710/* Return the string associated with a string-typed attribute, or NULL if it
17711 is either not found or is of an incorrect type. */
17712
17713static const char *
17714dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17715{
17716 struct attribute *attr;
17717 const char *str = NULL;
17718
17719 attr = dwarf2_attr (die, name, cu);
17720
17721 if (attr != NULL)
17722 {
43988095 17723 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17724 || attr->form == DW_FORM_string
17725 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17726 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17727 str = DW_STRING (attr);
17728 else
17729 complaint (&symfile_complaints,
17730 _("string type expected for attribute %s for "
17731 "DIE at 0x%x in module %s"),
9c541725 17732 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17733 objfile_name (cu->objfile));
17734 }
17735
17736 return str;
17737}
17738
05cf31d1
JB
17739/* Return non-zero iff the attribute NAME is defined for the given DIE,
17740 and holds a non-zero value. This function should only be used for
2dc7f7b3 17741 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17742
17743static int
17744dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17745{
17746 struct attribute *attr = dwarf2_attr (die, name, cu);
17747
17748 return (attr && DW_UNSND (attr));
17749}
17750
3ca72b44 17751static int
e142c38c 17752die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17753{
05cf31d1
JB
17754 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17755 which value is non-zero. However, we have to be careful with
17756 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17757 (via dwarf2_flag_true_p) follows this attribute. So we may
17758 end up accidently finding a declaration attribute that belongs
17759 to a different DIE referenced by the specification attribute,
17760 even though the given DIE does not have a declaration attribute. */
17761 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17762 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17763}
17764
63d06c5c 17765/* Return the die giving the specification for DIE, if there is
f2f0e013 17766 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17767 containing the return value on output. If there is no
17768 specification, but there is an abstract origin, that is
17769 returned. */
63d06c5c
DC
17770
17771static struct die_info *
f2f0e013 17772die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17773{
f2f0e013
DJ
17774 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17775 *spec_cu);
63d06c5c 17776
edb3359d
DJ
17777 if (spec_attr == NULL)
17778 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17779
63d06c5c
DC
17780 if (spec_attr == NULL)
17781 return NULL;
17782 else
f2f0e013 17783 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17784}
c906108c 17785
527f3840
JK
17786/* Stub for free_line_header to match void * callback types. */
17787
17788static void
17789free_line_header_voidp (void *arg)
17790{
9a3c8263 17791 struct line_header *lh = (struct line_header *) arg;
527f3840 17792
fff8551c 17793 delete lh;
527f3840
JK
17794}
17795
fff8551c
PA
17796void
17797line_header::add_include_dir (const char *include_dir)
c906108c 17798{
27e0867f 17799 if (dwarf_line_debug >= 2)
fff8551c
PA
17800 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17801 include_dirs.size () + 1, include_dir);
27e0867f 17802
fff8551c 17803 include_dirs.push_back (include_dir);
debd256d 17804}
6e70227d 17805
fff8551c
PA
17806void
17807line_header::add_file_name (const char *name,
ecfb656c 17808 dir_index d_index,
fff8551c
PA
17809 unsigned int mod_time,
17810 unsigned int length)
debd256d 17811{
27e0867f
DE
17812 if (dwarf_line_debug >= 2)
17813 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17814 (unsigned) file_names.size () + 1, name);
27e0867f 17815
ecfb656c 17816 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17817}
6e70227d 17818
83769d0b 17819/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17820
17821static struct dwarf2_section_info *
17822get_debug_line_section (struct dwarf2_cu *cu)
17823{
17824 struct dwarf2_section_info *section;
17825
17826 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17827 DWO file. */
17828 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17829 section = &cu->dwo_unit->dwo_file->sections.line;
17830 else if (cu->per_cu->is_dwz)
17831 {
17832 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17833
17834 section = &dwz->line;
17835 }
17836 else
17837 section = &dwarf2_per_objfile->line;
17838
17839 return section;
17840}
17841
43988095
JK
17842/* Read directory or file name entry format, starting with byte of
17843 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17844 entries count and the entries themselves in the described entry
17845 format. */
17846
17847static void
17848read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17849 struct line_header *lh,
17850 const struct comp_unit_head *cu_header,
17851 void (*callback) (struct line_header *lh,
17852 const char *name,
ecfb656c 17853 dir_index d_index,
43988095
JK
17854 unsigned int mod_time,
17855 unsigned int length))
17856{
17857 gdb_byte format_count, formati;
17858 ULONGEST data_count, datai;
17859 const gdb_byte *buf = *bufp;
17860 const gdb_byte *format_header_data;
17861 int i;
17862 unsigned int bytes_read;
17863
17864 format_count = read_1_byte (abfd, buf);
17865 buf += 1;
17866 format_header_data = buf;
17867 for (formati = 0; formati < format_count; formati++)
17868 {
17869 read_unsigned_leb128 (abfd, buf, &bytes_read);
17870 buf += bytes_read;
17871 read_unsigned_leb128 (abfd, buf, &bytes_read);
17872 buf += bytes_read;
17873 }
17874
17875 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17876 buf += bytes_read;
17877 for (datai = 0; datai < data_count; datai++)
17878 {
17879 const gdb_byte *format = format_header_data;
17880 struct file_entry fe;
17881
43988095
JK
17882 for (formati = 0; formati < format_count; formati++)
17883 {
ecfb656c 17884 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17885 format += bytes_read;
43988095 17886
ecfb656c 17887 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17888 format += bytes_read;
ecfb656c
PA
17889
17890 gdb::optional<const char *> string;
17891 gdb::optional<unsigned int> uint;
17892
43988095
JK
17893 switch (form)
17894 {
17895 case DW_FORM_string:
ecfb656c 17896 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17897 buf += bytes_read;
17898 break;
17899
17900 case DW_FORM_line_strp:
ecfb656c
PA
17901 string.emplace (read_indirect_line_string (abfd, buf,
17902 cu_header,
17903 &bytes_read));
43988095
JK
17904 buf += bytes_read;
17905 break;
17906
17907 case DW_FORM_data1:
ecfb656c 17908 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17909 buf += 1;
17910 break;
17911
17912 case DW_FORM_data2:
ecfb656c 17913 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17914 buf += 2;
17915 break;
17916
17917 case DW_FORM_data4:
ecfb656c 17918 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17919 buf += 4;
17920 break;
17921
17922 case DW_FORM_data8:
ecfb656c 17923 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17924 buf += 8;
17925 break;
17926
17927 case DW_FORM_udata:
ecfb656c 17928 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17929 buf += bytes_read;
17930 break;
17931
17932 case DW_FORM_block:
17933 /* It is valid only for DW_LNCT_timestamp which is ignored by
17934 current GDB. */
17935 break;
17936 }
ecfb656c
PA
17937
17938 switch (content_type)
17939 {
17940 case DW_LNCT_path:
17941 if (string.has_value ())
17942 fe.name = *string;
17943 break;
17944 case DW_LNCT_directory_index:
17945 if (uint.has_value ())
17946 fe.d_index = (dir_index) *uint;
17947 break;
17948 case DW_LNCT_timestamp:
17949 if (uint.has_value ())
17950 fe.mod_time = *uint;
17951 break;
17952 case DW_LNCT_size:
17953 if (uint.has_value ())
17954 fe.length = *uint;
17955 break;
17956 case DW_LNCT_MD5:
17957 break;
17958 default:
17959 complaint (&symfile_complaints,
17960 _("Unknown format content type %s"),
17961 pulongest (content_type));
17962 }
43988095
JK
17963 }
17964
ecfb656c 17965 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17966 }
17967
17968 *bufp = buf;
17969}
17970
debd256d 17971/* Read the statement program header starting at OFFSET in
3019eac3 17972 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17973 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17974 Returns NULL if there is a problem reading the header, e.g., if it
17975 has a version we don't understand.
debd256d
JB
17976
17977 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17978 the returned object point into the dwarf line section buffer,
17979 and must not be freed. */
ae2de4f8 17980
fff8551c 17981static line_header_up
9c541725 17982dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17983{
d521ce57 17984 const gdb_byte *line_ptr;
c764a876 17985 unsigned int bytes_read, offset_size;
debd256d 17986 int i;
d521ce57 17987 const char *cur_dir, *cur_file;
3019eac3
DE
17988 struct dwarf2_section_info *section;
17989 bfd *abfd;
17990
36586728 17991 section = get_debug_line_section (cu);
3019eac3
DE
17992 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17993 if (section->buffer == NULL)
debd256d 17994 {
3019eac3
DE
17995 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17996 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17997 else
17998 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17999 return 0;
18000 }
18001
fceca515
DE
18002 /* We can't do this until we know the section is non-empty.
18003 Only then do we know we have such a section. */
a32a8923 18004 abfd = get_section_bfd_owner (section);
fceca515 18005
a738430d
MK
18006 /* Make sure that at least there's room for the total_length field.
18007 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18008 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18009 {
4d3c2250 18010 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18011 return 0;
18012 }
18013
fff8551c 18014 line_header_up lh (new line_header ());
debd256d 18015
9c541725 18016 lh->sect_off = sect_off;
527f3840
JK
18017 lh->offset_in_dwz = cu->per_cu->is_dwz;
18018
9c541725 18019 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18020
a738430d 18021 /* Read in the header. */
6e70227d 18022 lh->total_length =
c764a876
DE
18023 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18024 &bytes_read, &offset_size);
debd256d 18025 line_ptr += bytes_read;
3019eac3 18026 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18027 {
4d3c2250 18028 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18029 return 0;
18030 }
18031 lh->statement_program_end = line_ptr + lh->total_length;
18032 lh->version = read_2_bytes (abfd, line_ptr);
18033 line_ptr += 2;
43988095 18034 if (lh->version > 5)
cd366ee8
DE
18035 {
18036 /* This is a version we don't understand. The format could have
18037 changed in ways we don't handle properly so just punt. */
18038 complaint (&symfile_complaints,
18039 _("unsupported version in .debug_line section"));
18040 return NULL;
18041 }
43988095
JK
18042 if (lh->version >= 5)
18043 {
18044 gdb_byte segment_selector_size;
18045
18046 /* Skip address size. */
18047 read_1_byte (abfd, line_ptr);
18048 line_ptr += 1;
18049
18050 segment_selector_size = read_1_byte (abfd, line_ptr);
18051 line_ptr += 1;
18052 if (segment_selector_size != 0)
18053 {
18054 complaint (&symfile_complaints,
18055 _("unsupported segment selector size %u "
18056 "in .debug_line section"),
18057 segment_selector_size);
18058 return NULL;
18059 }
18060 }
c764a876
DE
18061 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18062 line_ptr += offset_size;
debd256d
JB
18063 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18064 line_ptr += 1;
2dc7f7b3
TT
18065 if (lh->version >= 4)
18066 {
18067 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18068 line_ptr += 1;
18069 }
18070 else
18071 lh->maximum_ops_per_instruction = 1;
18072
18073 if (lh->maximum_ops_per_instruction == 0)
18074 {
18075 lh->maximum_ops_per_instruction = 1;
18076 complaint (&symfile_complaints,
3e43a32a
MS
18077 _("invalid maximum_ops_per_instruction "
18078 "in `.debug_line' section"));
2dc7f7b3
TT
18079 }
18080
debd256d
JB
18081 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18082 line_ptr += 1;
18083 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18084 line_ptr += 1;
18085 lh->line_range = read_1_byte (abfd, line_ptr);
18086 line_ptr += 1;
18087 lh->opcode_base = read_1_byte (abfd, line_ptr);
18088 line_ptr += 1;
fff8551c 18089 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18090
18091 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18092 for (i = 1; i < lh->opcode_base; ++i)
18093 {
18094 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18095 line_ptr += 1;
18096 }
18097
43988095 18098 if (lh->version >= 5)
debd256d 18099 {
43988095 18100 /* Read directory table. */
fff8551c
PA
18101 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18102 [] (struct line_header *lh, const char *name,
ecfb656c 18103 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18104 unsigned int length)
18105 {
18106 lh->add_include_dir (name);
18107 });
debd256d 18108
43988095 18109 /* Read file name table. */
fff8551c
PA
18110 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18111 [] (struct line_header *lh, const char *name,
ecfb656c 18112 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18113 unsigned int length)
18114 {
ecfb656c 18115 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18116 });
43988095
JK
18117 }
18118 else
debd256d 18119 {
43988095
JK
18120 /* Read directory table. */
18121 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18122 {
18123 line_ptr += bytes_read;
fff8551c 18124 lh->add_include_dir (cur_dir);
43988095 18125 }
debd256d
JB
18126 line_ptr += bytes_read;
18127
43988095
JK
18128 /* Read file name table. */
18129 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18130 {
ecfb656c
PA
18131 unsigned int mod_time, length;
18132 dir_index d_index;
43988095
JK
18133
18134 line_ptr += bytes_read;
ecfb656c 18135 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18136 line_ptr += bytes_read;
18137 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18138 line_ptr += bytes_read;
18139 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18140 line_ptr += bytes_read;
18141
ecfb656c 18142 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18143 }
18144 line_ptr += bytes_read;
debd256d 18145 }
6e70227d 18146 lh->statement_program_start = line_ptr;
debd256d 18147
3019eac3 18148 if (line_ptr > (section->buffer + section->size))
4d3c2250 18149 complaint (&symfile_complaints,
3e43a32a
MS
18150 _("line number info header doesn't "
18151 "fit in `.debug_line' section"));
debd256d 18152
debd256d
JB
18153 return lh;
18154}
c906108c 18155
c6da4cef
DE
18156/* Subroutine of dwarf_decode_lines to simplify it.
18157 Return the file name of the psymtab for included file FILE_INDEX
18158 in line header LH of PST.
18159 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18160 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18161 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18162
18163 The function creates dangling cleanup registration. */
c6da4cef 18164
d521ce57 18165static const char *
c6da4cef
DE
18166psymtab_include_file_name (const struct line_header *lh, int file_index,
18167 const struct partial_symtab *pst,
18168 const char *comp_dir)
18169{
8c43009f 18170 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18171 const char *include_name = fe.name;
18172 const char *include_name_to_compare = include_name;
72b9f47f
TT
18173 const char *pst_filename;
18174 char *copied_name = NULL;
c6da4cef
DE
18175 int file_is_pst;
18176
8c43009f 18177 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18178
18179 if (!IS_ABSOLUTE_PATH (include_name)
18180 && (dir_name != NULL || comp_dir != NULL))
18181 {
18182 /* Avoid creating a duplicate psymtab for PST.
18183 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18184 Before we do the comparison, however, we need to account
18185 for DIR_NAME and COMP_DIR.
18186 First prepend dir_name (if non-NULL). If we still don't
18187 have an absolute path prepend comp_dir (if non-NULL).
18188 However, the directory we record in the include-file's
18189 psymtab does not contain COMP_DIR (to match the
18190 corresponding symtab(s)).
18191
18192 Example:
18193
18194 bash$ cd /tmp
18195 bash$ gcc -g ./hello.c
18196 include_name = "hello.c"
18197 dir_name = "."
18198 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18199 DW_AT_name = "./hello.c"
18200
18201 */
c6da4cef
DE
18202
18203 if (dir_name != NULL)
18204 {
d521ce57
TT
18205 char *tem = concat (dir_name, SLASH_STRING,
18206 include_name, (char *)NULL);
18207
18208 make_cleanup (xfree, tem);
18209 include_name = tem;
c6da4cef 18210 include_name_to_compare = include_name;
c6da4cef
DE
18211 }
18212 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18213 {
d521ce57
TT
18214 char *tem = concat (comp_dir, SLASH_STRING,
18215 include_name, (char *)NULL);
18216
18217 make_cleanup (xfree, tem);
18218 include_name_to_compare = tem;
c6da4cef
DE
18219 }
18220 }
18221
18222 pst_filename = pst->filename;
18223 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18224 {
72b9f47f
TT
18225 copied_name = concat (pst->dirname, SLASH_STRING,
18226 pst_filename, (char *)NULL);
18227 pst_filename = copied_name;
c6da4cef
DE
18228 }
18229
1e3fad37 18230 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18231
72b9f47f
TT
18232 if (copied_name != NULL)
18233 xfree (copied_name);
c6da4cef
DE
18234
18235 if (file_is_pst)
18236 return NULL;
18237 return include_name;
18238}
18239
d9b3de22
DE
18240/* State machine to track the state of the line number program. */
18241
6f77053d 18242class lnp_state_machine
d9b3de22 18243{
6f77053d
PA
18244public:
18245 /* Initialize a machine state for the start of a line number
18246 program. */
18247 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18248
8c43009f
PA
18249 file_entry *current_file ()
18250 {
18251 /* lh->file_names is 0-based, but the file name numbers in the
18252 statement program are 1-based. */
6f77053d
PA
18253 return m_line_header->file_name_at (m_file);
18254 }
18255
18256 /* Record the line in the state machine. END_SEQUENCE is true if
18257 we're processing the end of a sequence. */
18258 void record_line (bool end_sequence);
18259
18260 /* Check address and if invalid nop-out the rest of the lines in this
18261 sequence. */
18262 void check_line_address (struct dwarf2_cu *cu,
18263 const gdb_byte *line_ptr,
18264 CORE_ADDR lowpc, CORE_ADDR address);
18265
18266 void handle_set_discriminator (unsigned int discriminator)
18267 {
18268 m_discriminator = discriminator;
18269 m_line_has_non_zero_discriminator |= discriminator != 0;
18270 }
18271
18272 /* Handle DW_LNE_set_address. */
18273 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18274 {
18275 m_op_index = 0;
18276 address += baseaddr;
18277 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18278 }
18279
18280 /* Handle DW_LNS_advance_pc. */
18281 void handle_advance_pc (CORE_ADDR adjust);
18282
18283 /* Handle a special opcode. */
18284 void handle_special_opcode (unsigned char op_code);
18285
18286 /* Handle DW_LNS_advance_line. */
18287 void handle_advance_line (int line_delta)
18288 {
18289 advance_line (line_delta);
18290 }
18291
18292 /* Handle DW_LNS_set_file. */
18293 void handle_set_file (file_name_index file);
18294
18295 /* Handle DW_LNS_negate_stmt. */
18296 void handle_negate_stmt ()
18297 {
18298 m_is_stmt = !m_is_stmt;
18299 }
18300
18301 /* Handle DW_LNS_const_add_pc. */
18302 void handle_const_add_pc ();
18303
18304 /* Handle DW_LNS_fixed_advance_pc. */
18305 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18306 {
18307 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18308 m_op_index = 0;
18309 }
18310
18311 /* Handle DW_LNS_copy. */
18312 void handle_copy ()
18313 {
18314 record_line (false);
18315 m_discriminator = 0;
18316 }
18317
18318 /* Handle DW_LNE_end_sequence. */
18319 void handle_end_sequence ()
18320 {
18321 m_record_line_callback = ::record_line;
18322 }
18323
18324private:
18325 /* Advance the line by LINE_DELTA. */
18326 void advance_line (int line_delta)
18327 {
18328 m_line += line_delta;
18329
18330 if (line_delta != 0)
18331 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18332 }
18333
6f77053d
PA
18334 gdbarch *m_gdbarch;
18335
18336 /* True if we're recording lines.
18337 Otherwise we're building partial symtabs and are just interested in
18338 finding include files mentioned by the line number program. */
18339 bool m_record_lines_p;
18340
8c43009f 18341 /* The line number header. */
6f77053d 18342 line_header *m_line_header;
8c43009f 18343
6f77053d
PA
18344 /* These are part of the standard DWARF line number state machine,
18345 and initialized according to the DWARF spec. */
d9b3de22 18346
6f77053d 18347 unsigned char m_op_index = 0;
8c43009f 18348 /* The line table index (1-based) of the current file. */
6f77053d
PA
18349 file_name_index m_file = (file_name_index) 1;
18350 unsigned int m_line = 1;
18351
18352 /* These are initialized in the constructor. */
18353
18354 CORE_ADDR m_address;
18355 bool m_is_stmt;
18356 unsigned int m_discriminator;
d9b3de22
DE
18357
18358 /* Additional bits of state we need to track. */
18359
18360 /* The last file that we called dwarf2_start_subfile for.
18361 This is only used for TLLs. */
6f77053d 18362 unsigned int m_last_file = 0;
d9b3de22 18363 /* The last file a line number was recorded for. */
6f77053d 18364 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18365
18366 /* The function to call to record a line. */
6f77053d 18367 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18368
18369 /* The last line number that was recorded, used to coalesce
18370 consecutive entries for the same line. This can happen, for
18371 example, when discriminators are present. PR 17276. */
6f77053d
PA
18372 unsigned int m_last_line = 0;
18373 bool m_line_has_non_zero_discriminator = false;
8c43009f 18374};
d9b3de22 18375
6f77053d
PA
18376void
18377lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18378{
18379 CORE_ADDR addr_adj = (((m_op_index + adjust)
18380 / m_line_header->maximum_ops_per_instruction)
18381 * m_line_header->minimum_instruction_length);
18382 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18383 m_op_index = ((m_op_index + adjust)
18384 % m_line_header->maximum_ops_per_instruction);
18385}
d9b3de22 18386
6f77053d
PA
18387void
18388lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18389{
6f77053d
PA
18390 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18391 CORE_ADDR addr_adj = (((m_op_index
18392 + (adj_opcode / m_line_header->line_range))
18393 / m_line_header->maximum_ops_per_instruction)
18394 * m_line_header->minimum_instruction_length);
18395 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18396 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18397 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18398
6f77053d
PA
18399 int line_delta = (m_line_header->line_base
18400 + (adj_opcode % m_line_header->line_range));
18401 advance_line (line_delta);
18402 record_line (false);
18403 m_discriminator = 0;
18404}
d9b3de22 18405
6f77053d
PA
18406void
18407lnp_state_machine::handle_set_file (file_name_index file)
18408{
18409 m_file = file;
18410
18411 const file_entry *fe = current_file ();
18412 if (fe == NULL)
18413 dwarf2_debug_line_missing_file_complaint ();
18414 else if (m_record_lines_p)
18415 {
18416 const char *dir = fe->include_dir (m_line_header);
18417
18418 m_last_subfile = current_subfile;
18419 m_line_has_non_zero_discriminator = m_discriminator != 0;
18420 dwarf2_start_subfile (fe->name, dir);
18421 }
18422}
18423
18424void
18425lnp_state_machine::handle_const_add_pc ()
18426{
18427 CORE_ADDR adjust
18428 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18429
18430 CORE_ADDR addr_adj
18431 = (((m_op_index + adjust)
18432 / m_line_header->maximum_ops_per_instruction)
18433 * m_line_header->minimum_instruction_length);
18434
18435 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18436 m_op_index = ((m_op_index + adjust)
18437 % m_line_header->maximum_ops_per_instruction);
18438}
d9b3de22 18439
c91513d8
PP
18440/* Ignore this record_line request. */
18441
18442static void
18443noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18444{
18445 return;
18446}
18447
a05a36a5
DE
18448/* Return non-zero if we should add LINE to the line number table.
18449 LINE is the line to add, LAST_LINE is the last line that was added,
18450 LAST_SUBFILE is the subfile for LAST_LINE.
18451 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18452 had a non-zero discriminator.
18453
18454 We have to be careful in the presence of discriminators.
18455 E.g., for this line:
18456
18457 for (i = 0; i < 100000; i++);
18458
18459 clang can emit four line number entries for that one line,
18460 each with a different discriminator.
18461 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18462
18463 However, we want gdb to coalesce all four entries into one.
18464 Otherwise the user could stepi into the middle of the line and
18465 gdb would get confused about whether the pc really was in the
18466 middle of the line.
18467
18468 Things are further complicated by the fact that two consecutive
18469 line number entries for the same line is a heuristic used by gcc
18470 to denote the end of the prologue. So we can't just discard duplicate
18471 entries, we have to be selective about it. The heuristic we use is
18472 that we only collapse consecutive entries for the same line if at least
18473 one of those entries has a non-zero discriminator. PR 17276.
18474
18475 Note: Addresses in the line number state machine can never go backwards
18476 within one sequence, thus this coalescing is ok. */
18477
18478static int
18479dwarf_record_line_p (unsigned int line, unsigned int last_line,
18480 int line_has_non_zero_discriminator,
18481 struct subfile *last_subfile)
18482{
18483 if (current_subfile != last_subfile)
18484 return 1;
18485 if (line != last_line)
18486 return 1;
18487 /* Same line for the same file that we've seen already.
18488 As a last check, for pr 17276, only record the line if the line
18489 has never had a non-zero discriminator. */
18490 if (!line_has_non_zero_discriminator)
18491 return 1;
18492 return 0;
18493}
18494
252a6764
DE
18495/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18496 in the line table of subfile SUBFILE. */
18497
18498static void
d9b3de22
DE
18499dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18500 unsigned int line, CORE_ADDR address,
18501 record_line_ftype p_record_line)
252a6764
DE
18502{
18503 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18504
27e0867f
DE
18505 if (dwarf_line_debug)
18506 {
18507 fprintf_unfiltered (gdb_stdlog,
18508 "Recording line %u, file %s, address %s\n",
18509 line, lbasename (subfile->name),
18510 paddress (gdbarch, address));
18511 }
18512
d5962de5 18513 (*p_record_line) (subfile, line, addr);
252a6764
DE
18514}
18515
18516/* Subroutine of dwarf_decode_lines_1 to simplify it.
18517 Mark the end of a set of line number records.
d9b3de22 18518 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18519 If SUBFILE is NULL the request is ignored. */
18520
18521static void
18522dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18523 CORE_ADDR address, record_line_ftype p_record_line)
18524{
27e0867f
DE
18525 if (subfile == NULL)
18526 return;
18527
18528 if (dwarf_line_debug)
18529 {
18530 fprintf_unfiltered (gdb_stdlog,
18531 "Finishing current line, file %s, address %s\n",
18532 lbasename (subfile->name),
18533 paddress (gdbarch, address));
18534 }
18535
d9b3de22
DE
18536 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18537}
18538
6f77053d
PA
18539void
18540lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18541{
d9b3de22
DE
18542 if (dwarf_line_debug)
18543 {
18544 fprintf_unfiltered (gdb_stdlog,
18545 "Processing actual line %u: file %u,"
18546 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18547 m_line, to_underlying (m_file),
18548 paddress (m_gdbarch, m_address),
18549 m_is_stmt, m_discriminator);
d9b3de22
DE
18550 }
18551
6f77053d 18552 file_entry *fe = current_file ();
8c43009f
PA
18553
18554 if (fe == NULL)
d9b3de22
DE
18555 dwarf2_debug_line_missing_file_complaint ();
18556 /* For now we ignore lines not starting on an instruction boundary.
18557 But not when processing end_sequence for compatibility with the
18558 previous version of the code. */
6f77053d 18559 else if (m_op_index == 0 || end_sequence)
d9b3de22 18560 {
8c43009f 18561 fe->included_p = 1;
6f77053d 18562 if (m_record_lines_p && m_is_stmt)
d9b3de22 18563 {
6f77053d 18564 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18565 {
6f77053d
PA
18566 dwarf_finish_line (m_gdbarch, m_last_subfile,
18567 m_address, m_record_line_callback);
d9b3de22
DE
18568 }
18569
18570 if (!end_sequence)
18571 {
6f77053d
PA
18572 if (dwarf_record_line_p (m_line, m_last_line,
18573 m_line_has_non_zero_discriminator,
18574 m_last_subfile))
d9b3de22 18575 {
6f77053d
PA
18576 dwarf_record_line_1 (m_gdbarch, current_subfile,
18577 m_line, m_address,
18578 m_record_line_callback);
d9b3de22 18579 }
6f77053d
PA
18580 m_last_subfile = current_subfile;
18581 m_last_line = m_line;
d9b3de22
DE
18582 }
18583 }
18584 }
18585}
18586
6f77053d
PA
18587lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18588 bool record_lines_p)
d9b3de22 18589{
6f77053d
PA
18590 m_gdbarch = arch;
18591 m_record_lines_p = record_lines_p;
18592 m_line_header = lh;
d9b3de22 18593
6f77053d 18594 m_record_line_callback = ::record_line;
d9b3de22 18595
d9b3de22
DE
18596 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18597 was a line entry for it so that the backend has a chance to adjust it
18598 and also record it in case it needs it. This is currently used by MIPS
18599 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18600 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18601 m_is_stmt = lh->default_is_stmt;
18602 m_discriminator = 0;
252a6764
DE
18603}
18604
6f77053d
PA
18605void
18606lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18607 const gdb_byte *line_ptr,
18608 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18609{
18610 /* If address < lowpc then it's not a usable value, it's outside the
18611 pc range of the CU. However, we restrict the test to only address
18612 values of zero to preserve GDB's previous behaviour which is to
18613 handle the specific case of a function being GC'd by the linker. */
18614
18615 if (address == 0 && address < lowpc)
18616 {
18617 /* This line table is for a function which has been
18618 GCd by the linker. Ignore it. PR gdb/12528 */
18619
18620 struct objfile *objfile = cu->objfile;
18621 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18622
18623 complaint (&symfile_complaints,
18624 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18625 line_offset, objfile_name (objfile));
6f77053d
PA
18626 m_record_line_callback = noop_record_line;
18627 /* Note: record_line_callback is left as noop_record_line until
18628 we see DW_LNE_end_sequence. */
924c2928
DE
18629 }
18630}
18631
f3f5162e 18632/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18633 Process the line number information in LH.
18634 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18635 program in order to set included_p for every referenced header. */
debd256d 18636
c906108c 18637static void
43f3e411
DE
18638dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18639 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18640{
d521ce57
TT
18641 const gdb_byte *line_ptr, *extended_end;
18642 const gdb_byte *line_end;
a8c50c1f 18643 unsigned int bytes_read, extended_len;
699ca60a 18644 unsigned char op_code, extended_op;
e142c38c
DJ
18645 CORE_ADDR baseaddr;
18646 struct objfile *objfile = cu->objfile;
f3f5162e 18647 bfd *abfd = objfile->obfd;
fbf65064 18648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18649 /* True if we're recording line info (as opposed to building partial
18650 symtabs and just interested in finding include files mentioned by
18651 the line number program). */
18652 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18653
18654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18655
debd256d
JB
18656 line_ptr = lh->statement_program_start;
18657 line_end = lh->statement_program_end;
c906108c
SS
18658
18659 /* Read the statement sequences until there's nothing left. */
18660 while (line_ptr < line_end)
18661 {
6f77053d
PA
18662 /* The DWARF line number program state machine. Reset the state
18663 machine at the start of each sequence. */
18664 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18665 bool end_sequence = false;
d9b3de22 18666
8c43009f 18667 if (record_lines_p)
c906108c 18668 {
8c43009f
PA
18669 /* Start a subfile for the current file of the state
18670 machine. */
18671 const file_entry *fe = state_machine.current_file ();
18672
18673 if (fe != NULL)
18674 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18675 }
18676
a738430d 18677 /* Decode the table. */
d9b3de22 18678 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18679 {
18680 op_code = read_1_byte (abfd, line_ptr);
18681 line_ptr += 1;
9aa1fe7e 18682
debd256d 18683 if (op_code >= lh->opcode_base)
6e70227d 18684 {
8e07a239 18685 /* Special opcode. */
6f77053d 18686 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18687 }
18688 else switch (op_code)
c906108c
SS
18689 {
18690 case DW_LNS_extended_op:
3e43a32a
MS
18691 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18692 &bytes_read);
473b7be6 18693 line_ptr += bytes_read;
a8c50c1f 18694 extended_end = line_ptr + extended_len;
c906108c
SS
18695 extended_op = read_1_byte (abfd, line_ptr);
18696 line_ptr += 1;
18697 switch (extended_op)
18698 {
18699 case DW_LNE_end_sequence:
6f77053d
PA
18700 state_machine.handle_end_sequence ();
18701 end_sequence = true;
c906108c
SS
18702 break;
18703 case DW_LNE_set_address:
d9b3de22
DE
18704 {
18705 CORE_ADDR address
18706 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18707 line_ptr += bytes_read;
6f77053d
PA
18708
18709 state_machine.check_line_address (cu, line_ptr,
18710 lowpc, address);
18711 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18712 }
c906108c
SS
18713 break;
18714 case DW_LNE_define_file:
debd256d 18715 {
d521ce57 18716 const char *cur_file;
ecfb656c
PA
18717 unsigned int mod_time, length;
18718 dir_index dindex;
6e70227d 18719
3e43a32a
MS
18720 cur_file = read_direct_string (abfd, line_ptr,
18721 &bytes_read);
debd256d 18722 line_ptr += bytes_read;
ecfb656c 18723 dindex = (dir_index)
debd256d
JB
18724 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18725 line_ptr += bytes_read;
18726 mod_time =
18727 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18728 line_ptr += bytes_read;
18729 length =
18730 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18731 line_ptr += bytes_read;
ecfb656c 18732 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18733 }
c906108c 18734 break;
d0c6ba3d 18735 case DW_LNE_set_discriminator:
6f77053d
PA
18736 {
18737 /* The discriminator is not interesting to the
18738 debugger; just ignore it. We still need to
18739 check its value though:
18740 if there are consecutive entries for the same
18741 (non-prologue) line we want to coalesce them.
18742 PR 17276. */
18743 unsigned int discr
18744 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18745 line_ptr += bytes_read;
18746
18747 state_machine.handle_set_discriminator (discr);
18748 }
d0c6ba3d 18749 break;
c906108c 18750 default:
4d3c2250 18751 complaint (&symfile_complaints,
e2e0b3e5 18752 _("mangled .debug_line section"));
debd256d 18753 return;
c906108c 18754 }
a8c50c1f
DJ
18755 /* Make sure that we parsed the extended op correctly. If e.g.
18756 we expected a different address size than the producer used,
18757 we may have read the wrong number of bytes. */
18758 if (line_ptr != extended_end)
18759 {
18760 complaint (&symfile_complaints,
18761 _("mangled .debug_line section"));
18762 return;
18763 }
c906108c
SS
18764 break;
18765 case DW_LNS_copy:
6f77053d 18766 state_machine.handle_copy ();
c906108c
SS
18767 break;
18768 case DW_LNS_advance_pc:
2dc7f7b3
TT
18769 {
18770 CORE_ADDR adjust
18771 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18772 line_ptr += bytes_read;
6f77053d
PA
18773
18774 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18775 }
c906108c
SS
18776 break;
18777 case DW_LNS_advance_line:
a05a36a5
DE
18778 {
18779 int line_delta
18780 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18781 line_ptr += bytes_read;
6f77053d
PA
18782
18783 state_machine.handle_advance_line (line_delta);
a05a36a5 18784 }
c906108c
SS
18785 break;
18786 case DW_LNS_set_file:
d9b3de22 18787 {
6f77053d 18788 file_name_index file
ecfb656c
PA
18789 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18790 &bytes_read);
d9b3de22 18791 line_ptr += bytes_read;
8c43009f 18792
6f77053d 18793 state_machine.handle_set_file (file);
d9b3de22 18794 }
c906108c
SS
18795 break;
18796 case DW_LNS_set_column:
0ad93d4f 18797 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18798 line_ptr += bytes_read;
18799 break;
18800 case DW_LNS_negate_stmt:
6f77053d 18801 state_machine.handle_negate_stmt ();
c906108c
SS
18802 break;
18803 case DW_LNS_set_basic_block:
c906108c 18804 break;
c2c6d25f
JM
18805 /* Add to the address register of the state machine the
18806 address increment value corresponding to special opcode
a738430d
MK
18807 255. I.e., this value is scaled by the minimum
18808 instruction length since special opcode 255 would have
b021a221 18809 scaled the increment. */
c906108c 18810 case DW_LNS_const_add_pc:
6f77053d 18811 state_machine.handle_const_add_pc ();
c906108c
SS
18812 break;
18813 case DW_LNS_fixed_advance_pc:
3e29f34a 18814 {
6f77053d 18815 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18816 line_ptr += 2;
6f77053d
PA
18817
18818 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18819 }
c906108c 18820 break;
9aa1fe7e 18821 default:
a738430d
MK
18822 {
18823 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18824 int i;
a738430d 18825
debd256d 18826 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18827 {
18828 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18829 line_ptr += bytes_read;
18830 }
18831 }
c906108c
SS
18832 }
18833 }
d9b3de22
DE
18834
18835 if (!end_sequence)
18836 dwarf2_debug_line_missing_end_sequence_complaint ();
18837
18838 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18839 in which case we still finish recording the last line). */
6f77053d 18840 state_machine.record_line (true);
c906108c 18841 }
f3f5162e
DE
18842}
18843
18844/* Decode the Line Number Program (LNP) for the given line_header
18845 structure and CU. The actual information extracted and the type
18846 of structures created from the LNP depends on the value of PST.
18847
18848 1. If PST is NULL, then this procedure uses the data from the program
18849 to create all necessary symbol tables, and their linetables.
18850
18851 2. If PST is not NULL, this procedure reads the program to determine
18852 the list of files included by the unit represented by PST, and
18853 builds all the associated partial symbol tables.
18854
18855 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18856 It is used for relative paths in the line table.
18857 NOTE: When processing partial symtabs (pst != NULL),
18858 comp_dir == pst->dirname.
18859
18860 NOTE: It is important that psymtabs have the same file name (via strcmp)
18861 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18862 symtab we don't use it in the name of the psymtabs we create.
18863 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18864 A good testcase for this is mb-inline.exp.
18865
527f3840
JK
18866 LOWPC is the lowest address in CU (or 0 if not known).
18867
18868 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18869 for its PC<->lines mapping information. Otherwise only the filename
18870 table is read in. */
f3f5162e
DE
18871
18872static void
18873dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18874 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18875 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18876{
18877 struct objfile *objfile = cu->objfile;
18878 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18879
527f3840
JK
18880 if (decode_mapping)
18881 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18882
18883 if (decode_for_pst_p)
18884 {
18885 int file_index;
18886
18887 /* Now that we're done scanning the Line Header Program, we can
18888 create the psymtab of each included file. */
fff8551c 18889 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18890 if (lh->file_names[file_index].included_p == 1)
18891 {
d521ce57 18892 const char *include_name =
c6da4cef
DE
18893 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18894 if (include_name != NULL)
aaa75496
JB
18895 dwarf2_create_include_psymtab (include_name, pst, objfile);
18896 }
18897 }
cb1df416
DJ
18898 else
18899 {
18900 /* Make sure a symtab is created for every file, even files
18901 which contain only variables (i.e. no code with associated
18902 line numbers). */
43f3e411 18903 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18904 int i;
cb1df416 18905
fff8551c 18906 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18907 {
8c43009f 18908 file_entry &fe = lh->file_names[i];
9a619af0 18909
8c43009f 18910 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18911
cb1df416 18912 if (current_subfile->symtab == NULL)
43f3e411
DE
18913 {
18914 current_subfile->symtab
18915 = allocate_symtab (cust, current_subfile->name);
18916 }
8c43009f 18917 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18918 }
18919 }
c906108c
SS
18920}
18921
18922/* Start a subfile for DWARF. FILENAME is the name of the file and
18923 DIRNAME the name of the source directory which contains FILENAME
4d663531 18924 or NULL if not known.
c906108c
SS
18925 This routine tries to keep line numbers from identical absolute and
18926 relative file names in a common subfile.
18927
18928 Using the `list' example from the GDB testsuite, which resides in
18929 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18930 of /srcdir/list0.c yields the following debugging information for list0.c:
18931
c5aa993b 18932 DW_AT_name: /srcdir/list0.c
4d663531 18933 DW_AT_comp_dir: /compdir
357e46e7 18934 files.files[0].name: list0.h
c5aa993b 18935 files.files[0].dir: /srcdir
357e46e7 18936 files.files[1].name: list0.c
c5aa993b 18937 files.files[1].dir: /srcdir
c906108c
SS
18938
18939 The line number information for list0.c has to end up in a single
4f1520fb
FR
18940 subfile, so that `break /srcdir/list0.c:1' works as expected.
18941 start_subfile will ensure that this happens provided that we pass the
18942 concatenation of files.files[1].dir and files.files[1].name as the
18943 subfile's name. */
c906108c
SS
18944
18945static void
4d663531 18946dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18947{
d521ce57 18948 char *copy = NULL;
4f1520fb 18949
4d663531 18950 /* In order not to lose the line information directory,
4f1520fb
FR
18951 we concatenate it to the filename when it makes sense.
18952 Note that the Dwarf3 standard says (speaking of filenames in line
18953 information): ``The directory index is ignored for file names
18954 that represent full path names''. Thus ignoring dirname in the
18955 `else' branch below isn't an issue. */
c906108c 18956
d5166ae1 18957 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18958 {
18959 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18960 filename = copy;
18961 }
c906108c 18962
4d663531 18963 start_subfile (filename);
4f1520fb 18964
d521ce57
TT
18965 if (copy != NULL)
18966 xfree (copy);
c906108c
SS
18967}
18968
f4dc4d17
DE
18969/* Start a symtab for DWARF.
18970 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18971
43f3e411 18972static struct compunit_symtab *
f4dc4d17 18973dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18974 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18975{
43f3e411
DE
18976 struct compunit_symtab *cust
18977 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18978
f4dc4d17
DE
18979 record_debugformat ("DWARF 2");
18980 record_producer (cu->producer);
18981
18982 /* We assume that we're processing GCC output. */
18983 processing_gcc_compilation = 2;
18984
4d4ec4e5 18985 cu->processing_has_namespace_info = 0;
43f3e411
DE
18986
18987 return cust;
f4dc4d17
DE
18988}
18989
4c2df51b
DJ
18990static void
18991var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18992 struct dwarf2_cu *cu)
4c2df51b 18993{
e7c27a73
DJ
18994 struct objfile *objfile = cu->objfile;
18995 struct comp_unit_head *cu_header = &cu->header;
18996
4c2df51b
DJ
18997 /* NOTE drow/2003-01-30: There used to be a comment and some special
18998 code here to turn a symbol with DW_AT_external and a
18999 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19000 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19001 with some versions of binutils) where shared libraries could have
19002 relocations against symbols in their debug information - the
19003 minimal symbol would have the right address, but the debug info
19004 would not. It's no longer necessary, because we will explicitly
19005 apply relocations when we read in the debug information now. */
19006
19007 /* A DW_AT_location attribute with no contents indicates that a
19008 variable has been optimized away. */
19009 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19010 {
f1e6e072 19011 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19012 return;
19013 }
19014
19015 /* Handle one degenerate form of location expression specially, to
19016 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19017 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19018 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19019
19020 if (attr_form_is_block (attr)
3019eac3
DE
19021 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19022 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19023 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19024 && (DW_BLOCK (attr)->size
19025 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19026 {
891d2f0b 19027 unsigned int dummy;
4c2df51b 19028
3019eac3
DE
19029 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19030 SYMBOL_VALUE_ADDRESS (sym) =
19031 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19032 else
19033 SYMBOL_VALUE_ADDRESS (sym) =
19034 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19035 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19036 fixup_symbol_section (sym, objfile);
19037 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19038 SYMBOL_SECTION (sym));
4c2df51b
DJ
19039 return;
19040 }
19041
19042 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19043 expression evaluator, and use LOC_COMPUTED only when necessary
19044 (i.e. when the value of a register or memory location is
19045 referenced, or a thread-local block, etc.). Then again, it might
19046 not be worthwhile. I'm assuming that it isn't unless performance
19047 or memory numbers show me otherwise. */
19048
f1e6e072 19049 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19050
f1e6e072 19051 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19052 cu->has_loclist = 1;
4c2df51b
DJ
19053}
19054
c906108c
SS
19055/* Given a pointer to a DWARF information entry, figure out if we need
19056 to make a symbol table entry for it, and if so, create a new entry
19057 and return a pointer to it.
19058 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19059 used the passed type.
19060 If SPACE is not NULL, use it to hold the new symbol. If it is
19061 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19062
19063static struct symbol *
34eaf542
TT
19064new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19065 struct symbol *space)
c906108c 19066{
e7c27a73 19067 struct objfile *objfile = cu->objfile;
3e29f34a 19068 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19069 struct symbol *sym = NULL;
15d034d0 19070 const char *name;
c906108c
SS
19071 struct attribute *attr = NULL;
19072 struct attribute *attr2 = NULL;
e142c38c 19073 CORE_ADDR baseaddr;
e37fd15a
SW
19074 struct pending **list_to_add = NULL;
19075
edb3359d 19076 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19077
19078 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19079
94af9270 19080 name = dwarf2_name (die, cu);
c906108c
SS
19081 if (name)
19082 {
94af9270 19083 const char *linkagename;
34eaf542 19084 int suppress_add = 0;
94af9270 19085
34eaf542
TT
19086 if (space)
19087 sym = space;
19088 else
e623cf5d 19089 sym = allocate_symbol (objfile);
c906108c 19090 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19091
19092 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19093 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19094 linkagename = dwarf2_physname (name, die, cu);
19095 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19096
f55ee35c
JK
19097 /* Fortran does not have mangling standard and the mangling does differ
19098 between gfortran, iFort etc. */
19099 if (cu->language == language_fortran
b250c185 19100 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19101 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19102 dwarf2_full_name (name, die, cu),
29df156d 19103 NULL);
f55ee35c 19104
c906108c 19105 /* Default assumptions.
c5aa993b 19106 Use the passed type or decode it from the die. */
176620f1 19107 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19108 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19109 if (type != NULL)
19110 SYMBOL_TYPE (sym) = type;
19111 else
e7c27a73 19112 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19113 attr = dwarf2_attr (die,
19114 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19115 cu);
c906108c
SS
19116 if (attr)
19117 {
19118 SYMBOL_LINE (sym) = DW_UNSND (attr);
19119 }
cb1df416 19120
edb3359d
DJ
19121 attr = dwarf2_attr (die,
19122 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19123 cu);
cb1df416
DJ
19124 if (attr)
19125 {
ecfb656c 19126 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19127 struct file_entry *fe;
9a619af0 19128
ecfb656c
PA
19129 if (cu->line_header != NULL)
19130 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19131 else
19132 fe = NULL;
19133
19134 if (fe == NULL)
cb1df416
DJ
19135 complaint (&symfile_complaints,
19136 _("file index out of range"));
8c43009f
PA
19137 else
19138 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19139 }
19140
c906108c
SS
19141 switch (die->tag)
19142 {
19143 case DW_TAG_label:
e142c38c 19144 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19145 if (attr)
3e29f34a
MR
19146 {
19147 CORE_ADDR addr;
19148
19149 addr = attr_value_as_address (attr);
19150 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19151 SYMBOL_VALUE_ADDRESS (sym) = addr;
19152 }
0f5238ed
TT
19153 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19154 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19155 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19156 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19157 break;
19158 case DW_TAG_subprogram:
19159 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19160 finish_block. */
f1e6e072 19161 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19162 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19163 if ((attr2 && (DW_UNSND (attr2) != 0))
19164 || cu->language == language_ada)
c906108c 19165 {
2cfa0c8d
JB
19166 /* Subprograms marked external are stored as a global symbol.
19167 Ada subprograms, whether marked external or not, are always
19168 stored as a global symbol, because we want to be able to
19169 access them globally. For instance, we want to be able
19170 to break on a nested subprogram without having to
19171 specify the context. */
e37fd15a 19172 list_to_add = &global_symbols;
c906108c
SS
19173 }
19174 else
19175 {
e37fd15a 19176 list_to_add = cu->list_in_scope;
c906108c
SS
19177 }
19178 break;
edb3359d
DJ
19179 case DW_TAG_inlined_subroutine:
19180 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19181 finish_block. */
f1e6e072 19182 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19183 SYMBOL_INLINED (sym) = 1;
481860b3 19184 list_to_add = cu->list_in_scope;
edb3359d 19185 break;
34eaf542
TT
19186 case DW_TAG_template_value_param:
19187 suppress_add = 1;
19188 /* Fall through. */
72929c62 19189 case DW_TAG_constant:
c906108c 19190 case DW_TAG_variable:
254e6b9e 19191 case DW_TAG_member:
0963b4bd
MS
19192 /* Compilation with minimal debug info may result in
19193 variables with missing type entries. Change the
19194 misleading `void' type to something sensible. */
c906108c 19195 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19196 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19197
e142c38c 19198 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19199 /* In the case of DW_TAG_member, we should only be called for
19200 static const members. */
19201 if (die->tag == DW_TAG_member)
19202 {
3863f96c
DE
19203 /* dwarf2_add_field uses die_is_declaration,
19204 so we do the same. */
254e6b9e
DE
19205 gdb_assert (die_is_declaration (die, cu));
19206 gdb_assert (attr);
19207 }
c906108c
SS
19208 if (attr)
19209 {
e7c27a73 19210 dwarf2_const_value (attr, sym, cu);
e142c38c 19211 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19212 if (!suppress_add)
34eaf542
TT
19213 {
19214 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19215 list_to_add = &global_symbols;
34eaf542 19216 else
e37fd15a 19217 list_to_add = cu->list_in_scope;
34eaf542 19218 }
c906108c
SS
19219 break;
19220 }
e142c38c 19221 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19222 if (attr)
19223 {
e7c27a73 19224 var_decode_location (attr, sym, cu);
e142c38c 19225 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19226
19227 /* Fortran explicitly imports any global symbols to the local
19228 scope by DW_TAG_common_block. */
19229 if (cu->language == language_fortran && die->parent
19230 && die->parent->tag == DW_TAG_common_block)
19231 attr2 = NULL;
19232
caac4577
JG
19233 if (SYMBOL_CLASS (sym) == LOC_STATIC
19234 && SYMBOL_VALUE_ADDRESS (sym) == 0
19235 && !dwarf2_per_objfile->has_section_at_zero)
19236 {
19237 /* When a static variable is eliminated by the linker,
19238 the corresponding debug information is not stripped
19239 out, but the variable address is set to null;
19240 do not add such variables into symbol table. */
19241 }
19242 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19243 {
f55ee35c
JK
19244 /* Workaround gfortran PR debug/40040 - it uses
19245 DW_AT_location for variables in -fPIC libraries which may
19246 get overriden by other libraries/executable and get
19247 a different address. Resolve it by the minimal symbol
19248 which may come from inferior's executable using copy
19249 relocation. Make this workaround only for gfortran as for
19250 other compilers GDB cannot guess the minimal symbol
19251 Fortran mangling kind. */
19252 if (cu->language == language_fortran && die->parent
19253 && die->parent->tag == DW_TAG_module
19254 && cu->producer
28586665 19255 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19256 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19257
1c809c68
TT
19258 /* A variable with DW_AT_external is never static,
19259 but it may be block-scoped. */
19260 list_to_add = (cu->list_in_scope == &file_symbols
19261 ? &global_symbols : cu->list_in_scope);
1c809c68 19262 }
c906108c 19263 else
e37fd15a 19264 list_to_add = cu->list_in_scope;
c906108c
SS
19265 }
19266 else
19267 {
19268 /* We do not know the address of this symbol.
c5aa993b
JM
19269 If it is an external symbol and we have type information
19270 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19271 The address of the variable will then be determined from
19272 the minimal symbol table whenever the variable is
19273 referenced. */
e142c38c 19274 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19275
19276 /* Fortran explicitly imports any global symbols to the local
19277 scope by DW_TAG_common_block. */
19278 if (cu->language == language_fortran && die->parent
19279 && die->parent->tag == DW_TAG_common_block)
19280 {
19281 /* SYMBOL_CLASS doesn't matter here because
19282 read_common_block is going to reset it. */
19283 if (!suppress_add)
19284 list_to_add = cu->list_in_scope;
19285 }
19286 else if (attr2 && (DW_UNSND (attr2) != 0)
19287 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19288 {
0fe7935b
DJ
19289 /* A variable with DW_AT_external is never static, but it
19290 may be block-scoped. */
19291 list_to_add = (cu->list_in_scope == &file_symbols
19292 ? &global_symbols : cu->list_in_scope);
19293
f1e6e072 19294 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19295 }
442ddf59
JK
19296 else if (!die_is_declaration (die, cu))
19297 {
19298 /* Use the default LOC_OPTIMIZED_OUT class. */
19299 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19300 if (!suppress_add)
19301 list_to_add = cu->list_in_scope;
442ddf59 19302 }
c906108c
SS
19303 }
19304 break;
19305 case DW_TAG_formal_parameter:
edb3359d
DJ
19306 /* If we are inside a function, mark this as an argument. If
19307 not, we might be looking at an argument to an inlined function
19308 when we do not have enough information to show inlined frames;
19309 pretend it's a local variable in that case so that the user can
19310 still see it. */
19311 if (context_stack_depth > 0
19312 && context_stack[context_stack_depth - 1].name != NULL)
19313 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19314 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19315 if (attr)
19316 {
e7c27a73 19317 var_decode_location (attr, sym, cu);
c906108c 19318 }
e142c38c 19319 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19320 if (attr)
19321 {
e7c27a73 19322 dwarf2_const_value (attr, sym, cu);
c906108c 19323 }
f346a30d 19324
e37fd15a 19325 list_to_add = cu->list_in_scope;
c906108c
SS
19326 break;
19327 case DW_TAG_unspecified_parameters:
19328 /* From varargs functions; gdb doesn't seem to have any
19329 interest in this information, so just ignore it for now.
19330 (FIXME?) */
19331 break;
34eaf542
TT
19332 case DW_TAG_template_type_param:
19333 suppress_add = 1;
19334 /* Fall through. */
c906108c 19335 case DW_TAG_class_type:
680b30c7 19336 case DW_TAG_interface_type:
c906108c
SS
19337 case DW_TAG_structure_type:
19338 case DW_TAG_union_type:
72019c9c 19339 case DW_TAG_set_type:
c906108c 19340 case DW_TAG_enumeration_type:
f1e6e072 19341 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19342 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19343
63d06c5c 19344 {
9c37b5ae 19345 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19346 really ever be static objects: otherwise, if you try
19347 to, say, break of a class's method and you're in a file
19348 which doesn't mention that class, it won't work unless
19349 the check for all static symbols in lookup_symbol_aux
19350 saves you. See the OtherFileClass tests in
19351 gdb.c++/namespace.exp. */
19352
e37fd15a 19353 if (!suppress_add)
34eaf542 19354 {
34eaf542 19355 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19356 && cu->language == language_cplus
34eaf542 19357 ? &global_symbols : cu->list_in_scope);
63d06c5c 19358
64382290 19359 /* The semantics of C++ state that "struct foo {
9c37b5ae 19360 ... }" also defines a typedef for "foo". */
64382290 19361 if (cu->language == language_cplus
45280282 19362 || cu->language == language_ada
c44af4eb
TT
19363 || cu->language == language_d
19364 || cu->language == language_rust)
64382290
TT
19365 {
19366 /* The symbol's name is already allocated along
19367 with this objfile, so we don't need to
19368 duplicate it for the type. */
19369 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19370 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19371 }
63d06c5c
DC
19372 }
19373 }
c906108c
SS
19374 break;
19375 case DW_TAG_typedef:
f1e6e072 19376 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19377 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19378 list_to_add = cu->list_in_scope;
63d06c5c 19379 break;
c906108c 19380 case DW_TAG_base_type:
a02abb62 19381 case DW_TAG_subrange_type:
f1e6e072 19382 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19383 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19384 list_to_add = cu->list_in_scope;
c906108c
SS
19385 break;
19386 case DW_TAG_enumerator:
e142c38c 19387 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19388 if (attr)
19389 {
e7c27a73 19390 dwarf2_const_value (attr, sym, cu);
c906108c 19391 }
63d06c5c
DC
19392 {
19393 /* NOTE: carlton/2003-11-10: See comment above in the
19394 DW_TAG_class_type, etc. block. */
19395
e142c38c 19396 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19397 && cu->language == language_cplus
e142c38c 19398 ? &global_symbols : cu->list_in_scope);
63d06c5c 19399 }
c906108c 19400 break;
74921315 19401 case DW_TAG_imported_declaration:
5c4e30ca 19402 case DW_TAG_namespace:
f1e6e072 19403 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19404 list_to_add = &global_symbols;
5c4e30ca 19405 break;
530e8392
KB
19406 case DW_TAG_module:
19407 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19408 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19409 list_to_add = &global_symbols;
19410 break;
4357ac6c 19411 case DW_TAG_common_block:
f1e6e072 19412 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19413 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19414 add_symbol_to_list (sym, cu->list_in_scope);
19415 break;
c906108c
SS
19416 default:
19417 /* Not a tag we recognize. Hopefully we aren't processing
19418 trash data, but since we must specifically ignore things
19419 we don't recognize, there is nothing else we should do at
0963b4bd 19420 this point. */
e2e0b3e5 19421 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19422 dwarf_tag_name (die->tag));
c906108c
SS
19423 break;
19424 }
df8a16a1 19425
e37fd15a
SW
19426 if (suppress_add)
19427 {
19428 sym->hash_next = objfile->template_symbols;
19429 objfile->template_symbols = sym;
19430 list_to_add = NULL;
19431 }
19432
19433 if (list_to_add != NULL)
19434 add_symbol_to_list (sym, list_to_add);
19435
df8a16a1
DJ
19436 /* For the benefit of old versions of GCC, check for anonymous
19437 namespaces based on the demangled name. */
4d4ec4e5 19438 if (!cu->processing_has_namespace_info
94af9270 19439 && cu->language == language_cplus)
a10964d1 19440 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19441 }
19442 return (sym);
19443}
19444
34eaf542
TT
19445/* A wrapper for new_symbol_full that always allocates a new symbol. */
19446
19447static struct symbol *
19448new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19449{
19450 return new_symbol_full (die, type, cu, NULL);
19451}
19452
98bfdba5
PA
19453/* Given an attr with a DW_FORM_dataN value in host byte order,
19454 zero-extend it as appropriate for the symbol's type. The DWARF
19455 standard (v4) is not entirely clear about the meaning of using
19456 DW_FORM_dataN for a constant with a signed type, where the type is
19457 wider than the data. The conclusion of a discussion on the DWARF
19458 list was that this is unspecified. We choose to always zero-extend
19459 because that is the interpretation long in use by GCC. */
c906108c 19460
98bfdba5 19461static gdb_byte *
ff39bb5e 19462dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19463 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19464{
e7c27a73 19465 struct objfile *objfile = cu->objfile;
e17a4113
UW
19466 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19467 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19468 LONGEST l = DW_UNSND (attr);
19469
19470 if (bits < sizeof (*value) * 8)
19471 {
19472 l &= ((LONGEST) 1 << bits) - 1;
19473 *value = l;
19474 }
19475 else if (bits == sizeof (*value) * 8)
19476 *value = l;
19477 else
19478 {
224c3ddb 19479 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19480 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19481 return bytes;
19482 }
19483
19484 return NULL;
19485}
19486
19487/* Read a constant value from an attribute. Either set *VALUE, or if
19488 the value does not fit in *VALUE, set *BYTES - either already
19489 allocated on the objfile obstack, or newly allocated on OBSTACK,
19490 or, set *BATON, if we translated the constant to a location
19491 expression. */
19492
19493static void
ff39bb5e 19494dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19495 const char *name, struct obstack *obstack,
19496 struct dwarf2_cu *cu,
d521ce57 19497 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19498 struct dwarf2_locexpr_baton **baton)
19499{
19500 struct objfile *objfile = cu->objfile;
19501 struct comp_unit_head *cu_header = &cu->header;
c906108c 19502 struct dwarf_block *blk;
98bfdba5
PA
19503 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19504 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19505
19506 *value = 0;
19507 *bytes = NULL;
19508 *baton = NULL;
c906108c
SS
19509
19510 switch (attr->form)
19511 {
19512 case DW_FORM_addr:
3019eac3 19513 case DW_FORM_GNU_addr_index:
ac56253d 19514 {
ac56253d
TT
19515 gdb_byte *data;
19516
98bfdba5
PA
19517 if (TYPE_LENGTH (type) != cu_header->addr_size)
19518 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19519 cu_header->addr_size,
98bfdba5 19520 TYPE_LENGTH (type));
ac56253d
TT
19521 /* Symbols of this form are reasonably rare, so we just
19522 piggyback on the existing location code rather than writing
19523 a new implementation of symbol_computed_ops. */
8d749320 19524 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19525 (*baton)->per_cu = cu->per_cu;
19526 gdb_assert ((*baton)->per_cu);
ac56253d 19527
98bfdba5 19528 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19529 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19530 (*baton)->data = data;
ac56253d
TT
19531
19532 data[0] = DW_OP_addr;
19533 store_unsigned_integer (&data[1], cu_header->addr_size,
19534 byte_order, DW_ADDR (attr));
19535 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19536 }
c906108c 19537 break;
4ac36638 19538 case DW_FORM_string:
93b5768b 19539 case DW_FORM_strp:
3019eac3 19540 case DW_FORM_GNU_str_index:
36586728 19541 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19542 /* DW_STRING is already allocated on the objfile obstack, point
19543 directly to it. */
d521ce57 19544 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19545 break;
c906108c
SS
19546 case DW_FORM_block1:
19547 case DW_FORM_block2:
19548 case DW_FORM_block4:
19549 case DW_FORM_block:
2dc7f7b3 19550 case DW_FORM_exprloc:
0224619f 19551 case DW_FORM_data16:
c906108c 19552 blk = DW_BLOCK (attr);
98bfdba5
PA
19553 if (TYPE_LENGTH (type) != blk->size)
19554 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19555 TYPE_LENGTH (type));
19556 *bytes = blk->data;
c906108c 19557 break;
2df3850c
JM
19558
19559 /* The DW_AT_const_value attributes are supposed to carry the
19560 symbol's value "represented as it would be on the target
19561 architecture." By the time we get here, it's already been
19562 converted to host endianness, so we just need to sign- or
19563 zero-extend it as appropriate. */
19564 case DW_FORM_data1:
3aef2284 19565 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19566 break;
c906108c 19567 case DW_FORM_data2:
3aef2284 19568 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19569 break;
c906108c 19570 case DW_FORM_data4:
3aef2284 19571 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19572 break;
c906108c 19573 case DW_FORM_data8:
3aef2284 19574 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19575 break;
19576
c906108c 19577 case DW_FORM_sdata:
663c44ac 19578 case DW_FORM_implicit_const:
98bfdba5 19579 *value = DW_SND (attr);
2df3850c
JM
19580 break;
19581
c906108c 19582 case DW_FORM_udata:
98bfdba5 19583 *value = DW_UNSND (attr);
c906108c 19584 break;
2df3850c 19585
c906108c 19586 default:
4d3c2250 19587 complaint (&symfile_complaints,
e2e0b3e5 19588 _("unsupported const value attribute form: '%s'"),
4d3c2250 19589 dwarf_form_name (attr->form));
98bfdba5 19590 *value = 0;
c906108c
SS
19591 break;
19592 }
19593}
19594
2df3850c 19595
98bfdba5
PA
19596/* Copy constant value from an attribute to a symbol. */
19597
2df3850c 19598static void
ff39bb5e 19599dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19600 struct dwarf2_cu *cu)
2df3850c 19601{
98bfdba5 19602 struct objfile *objfile = cu->objfile;
12df843f 19603 LONGEST value;
d521ce57 19604 const gdb_byte *bytes;
98bfdba5 19605 struct dwarf2_locexpr_baton *baton;
2df3850c 19606
98bfdba5
PA
19607 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19608 SYMBOL_PRINT_NAME (sym),
19609 &objfile->objfile_obstack, cu,
19610 &value, &bytes, &baton);
2df3850c 19611
98bfdba5
PA
19612 if (baton != NULL)
19613 {
98bfdba5 19614 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19615 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19616 }
19617 else if (bytes != NULL)
19618 {
19619 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19620 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19621 }
19622 else
19623 {
19624 SYMBOL_VALUE (sym) = value;
f1e6e072 19625 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19626 }
2df3850c
JM
19627}
19628
c906108c
SS
19629/* Return the type of the die in question using its DW_AT_type attribute. */
19630
19631static struct type *
e7c27a73 19632die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19633{
c906108c 19634 struct attribute *type_attr;
c906108c 19635
e142c38c 19636 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19637 if (!type_attr)
19638 {
19639 /* A missing DW_AT_type represents a void type. */
46bf5051 19640 return objfile_type (cu->objfile)->builtin_void;
c906108c 19641 }
348e048f 19642
673bfd45 19643 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19644}
19645
b4ba55a1
JB
19646/* True iff CU's producer generates GNAT Ada auxiliary information
19647 that allows to find parallel types through that information instead
19648 of having to do expensive parallel lookups by type name. */
19649
19650static int
19651need_gnat_info (struct dwarf2_cu *cu)
19652{
19653 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19654 of GNAT produces this auxiliary information, without any indication
19655 that it is produced. Part of enhancing the FSF version of GNAT
19656 to produce that information will be to put in place an indicator
19657 that we can use in order to determine whether the descriptive type
19658 info is available or not. One suggestion that has been made is
19659 to use a new attribute, attached to the CU die. For now, assume
19660 that the descriptive type info is not available. */
19661 return 0;
19662}
19663
b4ba55a1
JB
19664/* Return the auxiliary type of the die in question using its
19665 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19666 attribute is not present. */
19667
19668static struct type *
19669die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19670{
b4ba55a1 19671 struct attribute *type_attr;
b4ba55a1
JB
19672
19673 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19674 if (!type_attr)
19675 return NULL;
19676
673bfd45 19677 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19678}
19679
19680/* If DIE has a descriptive_type attribute, then set the TYPE's
19681 descriptive type accordingly. */
19682
19683static void
19684set_descriptive_type (struct type *type, struct die_info *die,
19685 struct dwarf2_cu *cu)
19686{
19687 struct type *descriptive_type = die_descriptive_type (die, cu);
19688
19689 if (descriptive_type)
19690 {
19691 ALLOCATE_GNAT_AUX_TYPE (type);
19692 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19693 }
19694}
19695
c906108c
SS
19696/* Return the containing type of the die in question using its
19697 DW_AT_containing_type attribute. */
19698
19699static struct type *
e7c27a73 19700die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19701{
c906108c 19702 struct attribute *type_attr;
c906108c 19703
e142c38c 19704 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19705 if (!type_attr)
19706 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19707 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19708
673bfd45 19709 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19710}
19711
ac9ec31b
DE
19712/* Return an error marker type to use for the ill formed type in DIE/CU. */
19713
19714static struct type *
19715build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19716{
19717 struct objfile *objfile = dwarf2_per_objfile->objfile;
19718 char *message, *saved;
19719
19720 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19721 objfile_name (objfile),
9c541725
PA
19722 to_underlying (cu->header.sect_off),
19723 to_underlying (die->sect_off));
224c3ddb
SM
19724 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19725 message, strlen (message));
ac9ec31b
DE
19726 xfree (message);
19727
19f392bc 19728 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19729}
19730
673bfd45 19731/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19732 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19733 DW_AT_containing_type.
673bfd45
DE
19734 If there is no type substitute an error marker. */
19735
c906108c 19736static struct type *
ff39bb5e 19737lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19738 struct dwarf2_cu *cu)
c906108c 19739{
bb5ed363 19740 struct objfile *objfile = cu->objfile;
f792889a
DJ
19741 struct type *this_type;
19742
ac9ec31b
DE
19743 gdb_assert (attr->name == DW_AT_type
19744 || attr->name == DW_AT_GNAT_descriptive_type
19745 || attr->name == DW_AT_containing_type);
19746
673bfd45
DE
19747 /* First see if we have it cached. */
19748
36586728
TT
19749 if (attr->form == DW_FORM_GNU_ref_alt)
19750 {
19751 struct dwarf2_per_cu_data *per_cu;
9c541725 19752 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19753
9c541725
PA
19754 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19755 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19756 }
7771576e 19757 else if (attr_form_is_ref (attr))
673bfd45 19758 {
9c541725 19759 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19760
9c541725 19761 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19762 }
55f1336d 19763 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19764 {
ac9ec31b 19765 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19766
ac9ec31b 19767 return get_signatured_type (die, signature, cu);
673bfd45
DE
19768 }
19769 else
19770 {
ac9ec31b
DE
19771 complaint (&symfile_complaints,
19772 _("Dwarf Error: Bad type attribute %s in DIE"
19773 " at 0x%x [in module %s]"),
9c541725 19774 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19775 objfile_name (objfile));
ac9ec31b 19776 return build_error_marker_type (cu, die);
673bfd45
DE
19777 }
19778
19779 /* If not cached we need to read it in. */
19780
19781 if (this_type == NULL)
19782 {
ac9ec31b 19783 struct die_info *type_die = NULL;
673bfd45
DE
19784 struct dwarf2_cu *type_cu = cu;
19785
7771576e 19786 if (attr_form_is_ref (attr))
ac9ec31b
DE
19787 type_die = follow_die_ref (die, attr, &type_cu);
19788 if (type_die == NULL)
19789 return build_error_marker_type (cu, die);
19790 /* If we find the type now, it's probably because the type came
3019eac3
DE
19791 from an inter-CU reference and the type's CU got expanded before
19792 ours. */
ac9ec31b 19793 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19794 }
19795
19796 /* If we still don't have a type use an error marker. */
19797
19798 if (this_type == NULL)
ac9ec31b 19799 return build_error_marker_type (cu, die);
673bfd45 19800
f792889a 19801 return this_type;
c906108c
SS
19802}
19803
673bfd45
DE
19804/* Return the type in DIE, CU.
19805 Returns NULL for invalid types.
19806
02142a6c 19807 This first does a lookup in die_type_hash,
673bfd45
DE
19808 and only reads the die in if necessary.
19809
19810 NOTE: This can be called when reading in partial or full symbols. */
19811
f792889a 19812static struct type *
e7c27a73 19813read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19814{
f792889a
DJ
19815 struct type *this_type;
19816
19817 this_type = get_die_type (die, cu);
19818 if (this_type)
19819 return this_type;
19820
673bfd45
DE
19821 return read_type_die_1 (die, cu);
19822}
19823
19824/* Read the type in DIE, CU.
19825 Returns NULL for invalid types. */
19826
19827static struct type *
19828read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19829{
19830 struct type *this_type = NULL;
19831
c906108c
SS
19832 switch (die->tag)
19833 {
19834 case DW_TAG_class_type:
680b30c7 19835 case DW_TAG_interface_type:
c906108c
SS
19836 case DW_TAG_structure_type:
19837 case DW_TAG_union_type:
f792889a 19838 this_type = read_structure_type (die, cu);
c906108c
SS
19839 break;
19840 case DW_TAG_enumeration_type:
f792889a 19841 this_type = read_enumeration_type (die, cu);
c906108c
SS
19842 break;
19843 case DW_TAG_subprogram:
19844 case DW_TAG_subroutine_type:
edb3359d 19845 case DW_TAG_inlined_subroutine:
f792889a 19846 this_type = read_subroutine_type (die, cu);
c906108c
SS
19847 break;
19848 case DW_TAG_array_type:
f792889a 19849 this_type = read_array_type (die, cu);
c906108c 19850 break;
72019c9c 19851 case DW_TAG_set_type:
f792889a 19852 this_type = read_set_type (die, cu);
72019c9c 19853 break;
c906108c 19854 case DW_TAG_pointer_type:
f792889a 19855 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19856 break;
19857 case DW_TAG_ptr_to_member_type:
f792889a 19858 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19859 break;
19860 case DW_TAG_reference_type:
4297a3f0
AV
19861 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19862 break;
19863 case DW_TAG_rvalue_reference_type:
19864 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19865 break;
19866 case DW_TAG_const_type:
f792889a 19867 this_type = read_tag_const_type (die, cu);
c906108c
SS
19868 break;
19869 case DW_TAG_volatile_type:
f792889a 19870 this_type = read_tag_volatile_type (die, cu);
c906108c 19871 break;
06d66ee9
TT
19872 case DW_TAG_restrict_type:
19873 this_type = read_tag_restrict_type (die, cu);
19874 break;
c906108c 19875 case DW_TAG_string_type:
f792889a 19876 this_type = read_tag_string_type (die, cu);
c906108c
SS
19877 break;
19878 case DW_TAG_typedef:
f792889a 19879 this_type = read_typedef (die, cu);
c906108c 19880 break;
a02abb62 19881 case DW_TAG_subrange_type:
f792889a 19882 this_type = read_subrange_type (die, cu);
a02abb62 19883 break;
c906108c 19884 case DW_TAG_base_type:
f792889a 19885 this_type = read_base_type (die, cu);
c906108c 19886 break;
81a17f79 19887 case DW_TAG_unspecified_type:
f792889a 19888 this_type = read_unspecified_type (die, cu);
81a17f79 19889 break;
0114d602
DJ
19890 case DW_TAG_namespace:
19891 this_type = read_namespace_type (die, cu);
19892 break;
f55ee35c
JK
19893 case DW_TAG_module:
19894 this_type = read_module_type (die, cu);
19895 break;
a2c2acaf
MW
19896 case DW_TAG_atomic_type:
19897 this_type = read_tag_atomic_type (die, cu);
19898 break;
c906108c 19899 default:
3e43a32a
MS
19900 complaint (&symfile_complaints,
19901 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19902 dwarf_tag_name (die->tag));
c906108c
SS
19903 break;
19904 }
63d06c5c 19905
f792889a 19906 return this_type;
63d06c5c
DC
19907}
19908
abc72ce4
DE
19909/* See if we can figure out if the class lives in a namespace. We do
19910 this by looking for a member function; its demangled name will
19911 contain namespace info, if there is any.
19912 Return the computed name or NULL.
19913 Space for the result is allocated on the objfile's obstack.
19914 This is the full-die version of guess_partial_die_structure_name.
19915 In this case we know DIE has no useful parent. */
19916
19917static char *
19918guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19919{
19920 struct die_info *spec_die;
19921 struct dwarf2_cu *spec_cu;
19922 struct die_info *child;
19923
19924 spec_cu = cu;
19925 spec_die = die_specification (die, &spec_cu);
19926 if (spec_die != NULL)
19927 {
19928 die = spec_die;
19929 cu = spec_cu;
19930 }
19931
19932 for (child = die->child;
19933 child != NULL;
19934 child = child->sibling)
19935 {
19936 if (child->tag == DW_TAG_subprogram)
19937 {
73b9be8b 19938 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 19939
7d45c7c3 19940 if (linkage_name != NULL)
abc72ce4
DE
19941 {
19942 char *actual_name
19943 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19944 linkage_name);
abc72ce4
DE
19945 char *name = NULL;
19946
19947 if (actual_name != NULL)
19948 {
15d034d0 19949 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19950
19951 if (die_name != NULL
19952 && strcmp (die_name, actual_name) != 0)
19953 {
19954 /* Strip off the class name from the full name.
19955 We want the prefix. */
19956 int die_name_len = strlen (die_name);
19957 int actual_name_len = strlen (actual_name);
19958
19959 /* Test for '::' as a sanity check. */
19960 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19961 && actual_name[actual_name_len
19962 - die_name_len - 1] == ':')
224c3ddb
SM
19963 name = (char *) obstack_copy0 (
19964 &cu->objfile->per_bfd->storage_obstack,
19965 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19966 }
19967 }
19968 xfree (actual_name);
19969 return name;
19970 }
19971 }
19972 }
19973
19974 return NULL;
19975}
19976
96408a79
SA
19977/* GCC might emit a nameless typedef that has a linkage name. Determine the
19978 prefix part in such case. See
19979 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19980
a121b7c1 19981static const char *
96408a79
SA
19982anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19983{
19984 struct attribute *attr;
e6a959d6 19985 const char *base;
96408a79
SA
19986
19987 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19988 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19989 return NULL;
19990
7d45c7c3 19991 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19992 return NULL;
19993
73b9be8b 19994 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
19995 if (attr == NULL || DW_STRING (attr) == NULL)
19996 return NULL;
19997
19998 /* dwarf2_name had to be already called. */
19999 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20000
20001 /* Strip the base name, keep any leading namespaces/classes. */
20002 base = strrchr (DW_STRING (attr), ':');
20003 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20004 return "";
20005
224c3ddb
SM
20006 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20007 DW_STRING (attr),
20008 &base[-1] - DW_STRING (attr));
96408a79
SA
20009}
20010
fdde2d81 20011/* Return the name of the namespace/class that DIE is defined within,
0114d602 20012 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20013
0114d602
DJ
20014 For example, if we're within the method foo() in the following
20015 code:
20016
20017 namespace N {
20018 class C {
20019 void foo () {
20020 }
20021 };
20022 }
20023
20024 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20025
0d5cff50 20026static const char *
e142c38c 20027determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20028{
0114d602
DJ
20029 struct die_info *parent, *spec_die;
20030 struct dwarf2_cu *spec_cu;
20031 struct type *parent_type;
a121b7c1 20032 const char *retval;
63d06c5c 20033
9c37b5ae 20034 if (cu->language != language_cplus
c44af4eb
TT
20035 && cu->language != language_fortran && cu->language != language_d
20036 && cu->language != language_rust)
0114d602
DJ
20037 return "";
20038
96408a79
SA
20039 retval = anonymous_struct_prefix (die, cu);
20040 if (retval)
20041 return retval;
20042
0114d602
DJ
20043 /* We have to be careful in the presence of DW_AT_specification.
20044 For example, with GCC 3.4, given the code
20045
20046 namespace N {
20047 void foo() {
20048 // Definition of N::foo.
20049 }
20050 }
20051
20052 then we'll have a tree of DIEs like this:
20053
20054 1: DW_TAG_compile_unit
20055 2: DW_TAG_namespace // N
20056 3: DW_TAG_subprogram // declaration of N::foo
20057 4: DW_TAG_subprogram // definition of N::foo
20058 DW_AT_specification // refers to die #3
20059
20060 Thus, when processing die #4, we have to pretend that we're in
20061 the context of its DW_AT_specification, namely the contex of die
20062 #3. */
20063 spec_cu = cu;
20064 spec_die = die_specification (die, &spec_cu);
20065 if (spec_die == NULL)
20066 parent = die->parent;
20067 else
63d06c5c 20068 {
0114d602
DJ
20069 parent = spec_die->parent;
20070 cu = spec_cu;
63d06c5c 20071 }
0114d602
DJ
20072
20073 if (parent == NULL)
20074 return "";
98bfdba5
PA
20075 else if (parent->building_fullname)
20076 {
20077 const char *name;
20078 const char *parent_name;
20079
20080 /* It has been seen on RealView 2.2 built binaries,
20081 DW_TAG_template_type_param types actually _defined_ as
20082 children of the parent class:
20083
20084 enum E {};
20085 template class <class Enum> Class{};
20086 Class<enum E> class_e;
20087
20088 1: DW_TAG_class_type (Class)
20089 2: DW_TAG_enumeration_type (E)
20090 3: DW_TAG_enumerator (enum1:0)
20091 3: DW_TAG_enumerator (enum2:1)
20092 ...
20093 2: DW_TAG_template_type_param
20094 DW_AT_type DW_FORM_ref_udata (E)
20095
20096 Besides being broken debug info, it can put GDB into an
20097 infinite loop. Consider:
20098
20099 When we're building the full name for Class<E>, we'll start
20100 at Class, and go look over its template type parameters,
20101 finding E. We'll then try to build the full name of E, and
20102 reach here. We're now trying to build the full name of E,
20103 and look over the parent DIE for containing scope. In the
20104 broken case, if we followed the parent DIE of E, we'd again
20105 find Class, and once again go look at its template type
20106 arguments, etc., etc. Simply don't consider such parent die
20107 as source-level parent of this die (it can't be, the language
20108 doesn't allow it), and break the loop here. */
20109 name = dwarf2_name (die, cu);
20110 parent_name = dwarf2_name (parent, cu);
20111 complaint (&symfile_complaints,
20112 _("template param type '%s' defined within parent '%s'"),
20113 name ? name : "<unknown>",
20114 parent_name ? parent_name : "<unknown>");
20115 return "";
20116 }
63d06c5c 20117 else
0114d602
DJ
20118 switch (parent->tag)
20119 {
63d06c5c 20120 case DW_TAG_namespace:
0114d602 20121 parent_type = read_type_die (parent, cu);
acebe513
UW
20122 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20123 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20124 Work around this problem here. */
20125 if (cu->language == language_cplus
20126 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20127 return "";
0114d602
DJ
20128 /* We give a name to even anonymous namespaces. */
20129 return TYPE_TAG_NAME (parent_type);
63d06c5c 20130 case DW_TAG_class_type:
680b30c7 20131 case DW_TAG_interface_type:
63d06c5c 20132 case DW_TAG_structure_type:
0114d602 20133 case DW_TAG_union_type:
f55ee35c 20134 case DW_TAG_module:
0114d602
DJ
20135 parent_type = read_type_die (parent, cu);
20136 if (TYPE_TAG_NAME (parent_type) != NULL)
20137 return TYPE_TAG_NAME (parent_type);
20138 else
20139 /* An anonymous structure is only allowed non-static data
20140 members; no typedefs, no member functions, et cetera.
20141 So it does not need a prefix. */
20142 return "";
abc72ce4 20143 case DW_TAG_compile_unit:
95554aad 20144 case DW_TAG_partial_unit:
abc72ce4
DE
20145 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20146 if (cu->language == language_cplus
8b70b953 20147 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20148 && die->child != NULL
20149 && (die->tag == DW_TAG_class_type
20150 || die->tag == DW_TAG_structure_type
20151 || die->tag == DW_TAG_union_type))
20152 {
20153 char *name = guess_full_die_structure_name (die, cu);
20154 if (name != NULL)
20155 return name;
20156 }
20157 return "";
3d567982
TT
20158 case DW_TAG_enumeration_type:
20159 parent_type = read_type_die (parent, cu);
20160 if (TYPE_DECLARED_CLASS (parent_type))
20161 {
20162 if (TYPE_TAG_NAME (parent_type) != NULL)
20163 return TYPE_TAG_NAME (parent_type);
20164 return "";
20165 }
20166 /* Fall through. */
63d06c5c 20167 default:
8176b9b8 20168 return determine_prefix (parent, cu);
63d06c5c 20169 }
63d06c5c
DC
20170}
20171
3e43a32a
MS
20172/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20173 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20174 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20175 an obconcat, otherwise allocate storage for the result. The CU argument is
20176 used to determine the language and hence, the appropriate separator. */
987504bb 20177
f55ee35c 20178#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20179
20180static char *
f55ee35c
JK
20181typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20182 int physname, struct dwarf2_cu *cu)
63d06c5c 20183{
f55ee35c 20184 const char *lead = "";
5c315b68 20185 const char *sep;
63d06c5c 20186
3e43a32a
MS
20187 if (suffix == NULL || suffix[0] == '\0'
20188 || prefix == NULL || prefix[0] == '\0')
987504bb 20189 sep = "";
45280282
IB
20190 else if (cu->language == language_d)
20191 {
20192 /* For D, the 'main' function could be defined in any module, but it
20193 should never be prefixed. */
20194 if (strcmp (suffix, "D main") == 0)
20195 {
20196 prefix = "";
20197 sep = "";
20198 }
20199 else
20200 sep = ".";
20201 }
f55ee35c
JK
20202 else if (cu->language == language_fortran && physname)
20203 {
20204 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20205 DW_AT_MIPS_linkage_name is preferred and used instead. */
20206
20207 lead = "__";
20208 sep = "_MOD_";
20209 }
987504bb
JJ
20210 else
20211 sep = "::";
63d06c5c 20212
6dd47d34
DE
20213 if (prefix == NULL)
20214 prefix = "";
20215 if (suffix == NULL)
20216 suffix = "";
20217
987504bb
JJ
20218 if (obs == NULL)
20219 {
3e43a32a 20220 char *retval
224c3ddb
SM
20221 = ((char *)
20222 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20223
f55ee35c
JK
20224 strcpy (retval, lead);
20225 strcat (retval, prefix);
6dd47d34
DE
20226 strcat (retval, sep);
20227 strcat (retval, suffix);
63d06c5c
DC
20228 return retval;
20229 }
987504bb
JJ
20230 else
20231 {
20232 /* We have an obstack. */
f55ee35c 20233 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20234 }
63d06c5c
DC
20235}
20236
c906108c
SS
20237/* Return sibling of die, NULL if no sibling. */
20238
f9aca02d 20239static struct die_info *
fba45db2 20240sibling_die (struct die_info *die)
c906108c 20241{
639d11d3 20242 return die->sibling;
c906108c
SS
20243}
20244
71c25dea
TT
20245/* Get name of a die, return NULL if not found. */
20246
15d034d0
TT
20247static const char *
20248dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20249 struct obstack *obstack)
20250{
20251 if (name && cu->language == language_cplus)
20252 {
2f408ecb 20253 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20254
2f408ecb 20255 if (!canon_name.empty ())
71c25dea 20256 {
2f408ecb
PA
20257 if (canon_name != name)
20258 name = (const char *) obstack_copy0 (obstack,
20259 canon_name.c_str (),
20260 canon_name.length ());
71c25dea
TT
20261 }
20262 }
20263
20264 return name;
c906108c
SS
20265}
20266
96553a0c
DE
20267/* Get name of a die, return NULL if not found.
20268 Anonymous namespaces are converted to their magic string. */
9219021c 20269
15d034d0 20270static const char *
e142c38c 20271dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20272{
20273 struct attribute *attr;
20274
e142c38c 20275 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20276 if ((!attr || !DW_STRING (attr))
96553a0c 20277 && die->tag != DW_TAG_namespace
53832f31
TT
20278 && die->tag != DW_TAG_class_type
20279 && die->tag != DW_TAG_interface_type
20280 && die->tag != DW_TAG_structure_type
20281 && die->tag != DW_TAG_union_type)
71c25dea
TT
20282 return NULL;
20283
20284 switch (die->tag)
20285 {
20286 case DW_TAG_compile_unit:
95554aad 20287 case DW_TAG_partial_unit:
71c25dea
TT
20288 /* Compilation units have a DW_AT_name that is a filename, not
20289 a source language identifier. */
20290 case DW_TAG_enumeration_type:
20291 case DW_TAG_enumerator:
20292 /* These tags always have simple identifiers already; no need
20293 to canonicalize them. */
20294 return DW_STRING (attr);
907af001 20295
96553a0c
DE
20296 case DW_TAG_namespace:
20297 if (attr != NULL && DW_STRING (attr) != NULL)
20298 return DW_STRING (attr);
20299 return CP_ANONYMOUS_NAMESPACE_STR;
20300
907af001
UW
20301 case DW_TAG_class_type:
20302 case DW_TAG_interface_type:
20303 case DW_TAG_structure_type:
20304 case DW_TAG_union_type:
20305 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20306 structures or unions. These were of the form "._%d" in GCC 4.1,
20307 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20308 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20309 if (attr && DW_STRING (attr)
61012eef
GB
20310 && (startswith (DW_STRING (attr), "._")
20311 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20312 return NULL;
53832f31
TT
20313
20314 /* GCC might emit a nameless typedef that has a linkage name. See
20315 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20316 if (!attr || DW_STRING (attr) == NULL)
20317 {
df5c6c50 20318 char *demangled = NULL;
53832f31 20319
73b9be8b 20320 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20321 if (attr == NULL || DW_STRING (attr) == NULL)
20322 return NULL;
20323
df5c6c50
JK
20324 /* Avoid demangling DW_STRING (attr) the second time on a second
20325 call for the same DIE. */
20326 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20327 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20328
20329 if (demangled)
20330 {
e6a959d6 20331 const char *base;
96408a79 20332
53832f31 20333 /* FIXME: we already did this for the partial symbol... */
34a68019 20334 DW_STRING (attr)
224c3ddb
SM
20335 = ((const char *)
20336 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20337 demangled, strlen (demangled)));
53832f31
TT
20338 DW_STRING_IS_CANONICAL (attr) = 1;
20339 xfree (demangled);
96408a79
SA
20340
20341 /* Strip any leading namespaces/classes, keep only the base name.
20342 DW_AT_name for named DIEs does not contain the prefixes. */
20343 base = strrchr (DW_STRING (attr), ':');
20344 if (base && base > DW_STRING (attr) && base[-1] == ':')
20345 return &base[1];
20346 else
20347 return DW_STRING (attr);
53832f31
TT
20348 }
20349 }
907af001
UW
20350 break;
20351
71c25dea 20352 default:
907af001
UW
20353 break;
20354 }
20355
20356 if (!DW_STRING_IS_CANONICAL (attr))
20357 {
20358 DW_STRING (attr)
20359 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20360 &cu->objfile->per_bfd->storage_obstack);
907af001 20361 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20362 }
907af001 20363 return DW_STRING (attr);
9219021c
DC
20364}
20365
20366/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20367 is none. *EXT_CU is the CU containing DIE on input, and the CU
20368 containing the return value on output. */
9219021c
DC
20369
20370static struct die_info *
f2f0e013 20371dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20372{
20373 struct attribute *attr;
9219021c 20374
f2f0e013 20375 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20376 if (attr == NULL)
20377 return NULL;
20378
f2f0e013 20379 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20380}
20381
c906108c
SS
20382/* Convert a DIE tag into its string name. */
20383
f39c6ffd 20384static const char *
aa1ee363 20385dwarf_tag_name (unsigned tag)
c906108c 20386{
f39c6ffd
TT
20387 const char *name = get_DW_TAG_name (tag);
20388
20389 if (name == NULL)
20390 return "DW_TAG_<unknown>";
20391
20392 return name;
c906108c
SS
20393}
20394
20395/* Convert a DWARF attribute code into its string name. */
20396
f39c6ffd 20397static const char *
aa1ee363 20398dwarf_attr_name (unsigned attr)
c906108c 20399{
f39c6ffd
TT
20400 const char *name;
20401
c764a876 20402#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20403 if (attr == DW_AT_MIPS_fde)
20404 return "DW_AT_MIPS_fde";
20405#else
20406 if (attr == DW_AT_HP_block_index)
20407 return "DW_AT_HP_block_index";
c764a876 20408#endif
f39c6ffd
TT
20409
20410 name = get_DW_AT_name (attr);
20411
20412 if (name == NULL)
20413 return "DW_AT_<unknown>";
20414
20415 return name;
c906108c
SS
20416}
20417
20418/* Convert a DWARF value form code into its string name. */
20419
f39c6ffd 20420static const char *
aa1ee363 20421dwarf_form_name (unsigned form)
c906108c 20422{
f39c6ffd
TT
20423 const char *name = get_DW_FORM_name (form);
20424
20425 if (name == NULL)
20426 return "DW_FORM_<unknown>";
20427
20428 return name;
c906108c
SS
20429}
20430
a121b7c1 20431static const char *
fba45db2 20432dwarf_bool_name (unsigned mybool)
c906108c
SS
20433{
20434 if (mybool)
20435 return "TRUE";
20436 else
20437 return "FALSE";
20438}
20439
20440/* Convert a DWARF type code into its string name. */
20441
f39c6ffd 20442static const char *
aa1ee363 20443dwarf_type_encoding_name (unsigned enc)
c906108c 20444{
f39c6ffd 20445 const char *name = get_DW_ATE_name (enc);
c906108c 20446
f39c6ffd
TT
20447 if (name == NULL)
20448 return "DW_ATE_<unknown>";
c906108c 20449
f39c6ffd 20450 return name;
c906108c 20451}
c906108c 20452
f9aca02d 20453static void
d97bc12b 20454dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20455{
20456 unsigned int i;
20457
d97bc12b
DE
20458 print_spaces (indent, f);
20459 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20460 dwarf_tag_name (die->tag), die->abbrev,
20461 to_underlying (die->sect_off));
d97bc12b
DE
20462
20463 if (die->parent != NULL)
20464 {
20465 print_spaces (indent, f);
20466 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20467 to_underlying (die->parent->sect_off));
d97bc12b
DE
20468 }
20469
20470 print_spaces (indent, f);
20471 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20472 dwarf_bool_name (die->child != NULL));
c906108c 20473
d97bc12b
DE
20474 print_spaces (indent, f);
20475 fprintf_unfiltered (f, " attributes:\n");
20476
c906108c
SS
20477 for (i = 0; i < die->num_attrs; ++i)
20478 {
d97bc12b
DE
20479 print_spaces (indent, f);
20480 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20481 dwarf_attr_name (die->attrs[i].name),
20482 dwarf_form_name (die->attrs[i].form));
d97bc12b 20483
c906108c
SS
20484 switch (die->attrs[i].form)
20485 {
c906108c 20486 case DW_FORM_addr:
3019eac3 20487 case DW_FORM_GNU_addr_index:
d97bc12b 20488 fprintf_unfiltered (f, "address: ");
5af949e3 20489 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20490 break;
20491 case DW_FORM_block2:
20492 case DW_FORM_block4:
20493 case DW_FORM_block:
20494 case DW_FORM_block1:
56eb65bd
SP
20495 fprintf_unfiltered (f, "block: size %s",
20496 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20497 break;
2dc7f7b3 20498 case DW_FORM_exprloc:
56eb65bd
SP
20499 fprintf_unfiltered (f, "expression: size %s",
20500 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20501 break;
0224619f
JK
20502 case DW_FORM_data16:
20503 fprintf_unfiltered (f, "constant of 16 bytes");
20504 break;
4568ecf9
DE
20505 case DW_FORM_ref_addr:
20506 fprintf_unfiltered (f, "ref address: ");
20507 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20508 break;
36586728
TT
20509 case DW_FORM_GNU_ref_alt:
20510 fprintf_unfiltered (f, "alt ref address: ");
20511 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20512 break;
10b3939b
DJ
20513 case DW_FORM_ref1:
20514 case DW_FORM_ref2:
20515 case DW_FORM_ref4:
4568ecf9
DE
20516 case DW_FORM_ref8:
20517 case DW_FORM_ref_udata:
d97bc12b 20518 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20519 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20520 break;
c906108c
SS
20521 case DW_FORM_data1:
20522 case DW_FORM_data2:
20523 case DW_FORM_data4:
ce5d95e1 20524 case DW_FORM_data8:
c906108c
SS
20525 case DW_FORM_udata:
20526 case DW_FORM_sdata:
43bbcdc2
PH
20527 fprintf_unfiltered (f, "constant: %s",
20528 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20529 break;
2dc7f7b3
TT
20530 case DW_FORM_sec_offset:
20531 fprintf_unfiltered (f, "section offset: %s",
20532 pulongest (DW_UNSND (&die->attrs[i])));
20533 break;
55f1336d 20534 case DW_FORM_ref_sig8:
ac9ec31b
DE
20535 fprintf_unfiltered (f, "signature: %s",
20536 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20537 break;
c906108c 20538 case DW_FORM_string:
4bdf3d34 20539 case DW_FORM_strp:
43988095 20540 case DW_FORM_line_strp:
3019eac3 20541 case DW_FORM_GNU_str_index:
36586728 20542 case DW_FORM_GNU_strp_alt:
8285870a 20543 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20544 DW_STRING (&die->attrs[i])
8285870a
JK
20545 ? DW_STRING (&die->attrs[i]) : "",
20546 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20547 break;
20548 case DW_FORM_flag:
20549 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20550 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20551 else
d97bc12b 20552 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20553 break;
2dc7f7b3
TT
20554 case DW_FORM_flag_present:
20555 fprintf_unfiltered (f, "flag: TRUE");
20556 break;
a8329558 20557 case DW_FORM_indirect:
0963b4bd
MS
20558 /* The reader will have reduced the indirect form to
20559 the "base form" so this form should not occur. */
3e43a32a
MS
20560 fprintf_unfiltered (f,
20561 "unexpected attribute form: DW_FORM_indirect");
a8329558 20562 break;
663c44ac
JK
20563 case DW_FORM_implicit_const:
20564 fprintf_unfiltered (f, "constant: %s",
20565 plongest (DW_SND (&die->attrs[i])));
20566 break;
c906108c 20567 default:
d97bc12b 20568 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20569 die->attrs[i].form);
d97bc12b 20570 break;
c906108c 20571 }
d97bc12b 20572 fprintf_unfiltered (f, "\n");
c906108c
SS
20573 }
20574}
20575
f9aca02d 20576static void
d97bc12b 20577dump_die_for_error (struct die_info *die)
c906108c 20578{
d97bc12b
DE
20579 dump_die_shallow (gdb_stderr, 0, die);
20580}
20581
20582static void
20583dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20584{
20585 int indent = level * 4;
20586
20587 gdb_assert (die != NULL);
20588
20589 if (level >= max_level)
20590 return;
20591
20592 dump_die_shallow (f, indent, die);
20593
20594 if (die->child != NULL)
c906108c 20595 {
d97bc12b
DE
20596 print_spaces (indent, f);
20597 fprintf_unfiltered (f, " Children:");
20598 if (level + 1 < max_level)
20599 {
20600 fprintf_unfiltered (f, "\n");
20601 dump_die_1 (f, level + 1, max_level, die->child);
20602 }
20603 else
20604 {
3e43a32a
MS
20605 fprintf_unfiltered (f,
20606 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20607 }
20608 }
20609
20610 if (die->sibling != NULL && level > 0)
20611 {
20612 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20613 }
20614}
20615
d97bc12b
DE
20616/* This is called from the pdie macro in gdbinit.in.
20617 It's not static so gcc will keep a copy callable from gdb. */
20618
20619void
20620dump_die (struct die_info *die, int max_level)
20621{
20622 dump_die_1 (gdb_stdlog, 0, max_level, die);
20623}
20624
f9aca02d 20625static void
51545339 20626store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20627{
51545339 20628 void **slot;
c906108c 20629
9c541725
PA
20630 slot = htab_find_slot_with_hash (cu->die_hash, die,
20631 to_underlying (die->sect_off),
b64f50a1 20632 INSERT);
51545339
DJ
20633
20634 *slot = die;
c906108c
SS
20635}
20636
b64f50a1
JK
20637/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20638 required kind. */
20639
20640static sect_offset
ff39bb5e 20641dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20642{
7771576e 20643 if (attr_form_is_ref (attr))
9c541725 20644 return (sect_offset) DW_UNSND (attr);
93311388
DE
20645
20646 complaint (&symfile_complaints,
20647 _("unsupported die ref attribute form: '%s'"),
20648 dwarf_form_name (attr->form));
9c541725 20649 return {};
c906108c
SS
20650}
20651
43bbcdc2
PH
20652/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20653 * the value held by the attribute is not constant. */
a02abb62 20654
43bbcdc2 20655static LONGEST
ff39bb5e 20656dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20657{
663c44ac 20658 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20659 return DW_SND (attr);
20660 else if (attr->form == DW_FORM_udata
20661 || attr->form == DW_FORM_data1
20662 || attr->form == DW_FORM_data2
20663 || attr->form == DW_FORM_data4
20664 || attr->form == DW_FORM_data8)
20665 return DW_UNSND (attr);
20666 else
20667 {
0224619f 20668 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20669 complaint (&symfile_complaints,
20670 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20671 dwarf_form_name (attr->form));
20672 return default_value;
20673 }
20674}
20675
348e048f
DE
20676/* Follow reference or signature attribute ATTR of SRC_DIE.
20677 On entry *REF_CU is the CU of SRC_DIE.
20678 On exit *REF_CU is the CU of the result. */
20679
20680static struct die_info *
ff39bb5e 20681follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20682 struct dwarf2_cu **ref_cu)
20683{
20684 struct die_info *die;
20685
7771576e 20686 if (attr_form_is_ref (attr))
348e048f 20687 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20688 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20689 die = follow_die_sig (src_die, attr, ref_cu);
20690 else
20691 {
20692 dump_die_for_error (src_die);
20693 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20694 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20695 }
20696
20697 return die;
03dd20cc
DJ
20698}
20699
5c631832 20700/* Follow reference OFFSET.
673bfd45
DE
20701 On entry *REF_CU is the CU of the source die referencing OFFSET.
20702 On exit *REF_CU is the CU of the result.
20703 Returns NULL if OFFSET is invalid. */
f504f079 20704
f9aca02d 20705static struct die_info *
9c541725 20706follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20707 struct dwarf2_cu **ref_cu)
c906108c 20708{
10b3939b 20709 struct die_info temp_die;
f2f0e013 20710 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20711
348e048f
DE
20712 gdb_assert (cu->per_cu != NULL);
20713
98bfdba5
PA
20714 target_cu = cu;
20715
3019eac3 20716 if (cu->per_cu->is_debug_types)
348e048f
DE
20717 {
20718 /* .debug_types CUs cannot reference anything outside their CU.
20719 If they need to, they have to reference a signatured type via
55f1336d 20720 DW_FORM_ref_sig8. */
9c541725 20721 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20722 return NULL;
348e048f 20723 }
36586728 20724 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20725 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20726 {
20727 struct dwarf2_per_cu_data *per_cu;
9a619af0 20728
9c541725 20729 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20730 cu->objfile);
03dd20cc
DJ
20731
20732 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20733 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20734 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20735
10b3939b
DJ
20736 target_cu = per_cu->cu;
20737 }
98bfdba5
PA
20738 else if (cu->dies == NULL)
20739 {
20740 /* We're loading full DIEs during partial symbol reading. */
20741 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20742 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20743 }
c906108c 20744
f2f0e013 20745 *ref_cu = target_cu;
9c541725 20746 temp_die.sect_off = sect_off;
9a3c8263 20747 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20748 &temp_die,
20749 to_underlying (sect_off));
5c631832 20750}
10b3939b 20751
5c631832
JK
20752/* Follow reference attribute ATTR of SRC_DIE.
20753 On entry *REF_CU is the CU of SRC_DIE.
20754 On exit *REF_CU is the CU of the result. */
20755
20756static struct die_info *
ff39bb5e 20757follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20758 struct dwarf2_cu **ref_cu)
20759{
9c541725 20760 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20761 struct dwarf2_cu *cu = *ref_cu;
20762 struct die_info *die;
20763
9c541725 20764 die = follow_die_offset (sect_off,
36586728
TT
20765 (attr->form == DW_FORM_GNU_ref_alt
20766 || cu->per_cu->is_dwz),
20767 ref_cu);
5c631832
JK
20768 if (!die)
20769 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20770 "at 0x%x [in module %s]"),
9c541725 20771 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20772 objfile_name (cu->objfile));
348e048f 20773
5c631832
JK
20774 return die;
20775}
20776
9c541725 20777/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20778 Returned value is intended for DW_OP_call*. Returned
20779 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20780
20781struct dwarf2_locexpr_baton
9c541725 20782dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20783 struct dwarf2_per_cu_data *per_cu,
20784 CORE_ADDR (*get_frame_pc) (void *baton),
20785 void *baton)
5c631832 20786{
918dd910 20787 struct dwarf2_cu *cu;
5c631832
JK
20788 struct die_info *die;
20789 struct attribute *attr;
20790 struct dwarf2_locexpr_baton retval;
20791
8cf6f0b1
TT
20792 dw2_setup (per_cu->objfile);
20793
918dd910
JK
20794 if (per_cu->cu == NULL)
20795 load_cu (per_cu);
20796 cu = per_cu->cu;
cc12ce38
DE
20797 if (cu == NULL)
20798 {
20799 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20800 Instead just throw an error, not much else we can do. */
20801 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20802 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20803 }
918dd910 20804
9c541725 20805 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20806 if (!die)
20807 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20808 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20809
20810 attr = dwarf2_attr (die, DW_AT_location, cu);
20811 if (!attr)
20812 {
e103e986
JK
20813 /* DWARF: "If there is no such attribute, then there is no effect.".
20814 DATA is ignored if SIZE is 0. */
5c631832 20815
e103e986 20816 retval.data = NULL;
5c631832
JK
20817 retval.size = 0;
20818 }
8cf6f0b1
TT
20819 else if (attr_form_is_section_offset (attr))
20820 {
20821 struct dwarf2_loclist_baton loclist_baton;
20822 CORE_ADDR pc = (*get_frame_pc) (baton);
20823 size_t size;
20824
20825 fill_in_loclist_baton (cu, &loclist_baton, attr);
20826
20827 retval.data = dwarf2_find_location_expression (&loclist_baton,
20828 &size, pc);
20829 retval.size = size;
20830 }
5c631832
JK
20831 else
20832 {
20833 if (!attr_form_is_block (attr))
20834 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20835 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20836 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20837
20838 retval.data = DW_BLOCK (attr)->data;
20839 retval.size = DW_BLOCK (attr)->size;
20840 }
20841 retval.per_cu = cu->per_cu;
918dd910 20842
918dd910
JK
20843 age_cached_comp_units ();
20844
5c631832 20845 return retval;
348e048f
DE
20846}
20847
8b9737bf
TT
20848/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20849 offset. */
20850
20851struct dwarf2_locexpr_baton
20852dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20853 struct dwarf2_per_cu_data *per_cu,
20854 CORE_ADDR (*get_frame_pc) (void *baton),
20855 void *baton)
20856{
9c541725 20857 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20858
9c541725 20859 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20860}
20861
b6807d98
TT
20862/* Write a constant of a given type as target-ordered bytes into
20863 OBSTACK. */
20864
20865static const gdb_byte *
20866write_constant_as_bytes (struct obstack *obstack,
20867 enum bfd_endian byte_order,
20868 struct type *type,
20869 ULONGEST value,
20870 LONGEST *len)
20871{
20872 gdb_byte *result;
20873
20874 *len = TYPE_LENGTH (type);
224c3ddb 20875 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20876 store_unsigned_integer (result, *len, byte_order, value);
20877
20878 return result;
20879}
20880
20881/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20882 pointer to the constant bytes and set LEN to the length of the
20883 data. If memory is needed, allocate it on OBSTACK. If the DIE
20884 does not have a DW_AT_const_value, return NULL. */
20885
20886const gdb_byte *
9c541725 20887dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20888 struct dwarf2_per_cu_data *per_cu,
20889 struct obstack *obstack,
20890 LONGEST *len)
20891{
20892 struct dwarf2_cu *cu;
20893 struct die_info *die;
20894 struct attribute *attr;
20895 const gdb_byte *result = NULL;
20896 struct type *type;
20897 LONGEST value;
20898 enum bfd_endian byte_order;
20899
20900 dw2_setup (per_cu->objfile);
20901
20902 if (per_cu->cu == NULL)
20903 load_cu (per_cu);
20904 cu = per_cu->cu;
cc12ce38
DE
20905 if (cu == NULL)
20906 {
20907 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20908 Instead just throw an error, not much else we can do. */
20909 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20910 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20911 }
b6807d98 20912
9c541725 20913 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20914 if (!die)
20915 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20916 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20917
20918
20919 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20920 if (attr == NULL)
20921 return NULL;
20922
20923 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20924 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20925
20926 switch (attr->form)
20927 {
20928 case DW_FORM_addr:
20929 case DW_FORM_GNU_addr_index:
20930 {
20931 gdb_byte *tem;
20932
20933 *len = cu->header.addr_size;
224c3ddb 20934 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20935 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20936 result = tem;
20937 }
20938 break;
20939 case DW_FORM_string:
20940 case DW_FORM_strp:
20941 case DW_FORM_GNU_str_index:
20942 case DW_FORM_GNU_strp_alt:
20943 /* DW_STRING is already allocated on the objfile obstack, point
20944 directly to it. */
20945 result = (const gdb_byte *) DW_STRING (attr);
20946 *len = strlen (DW_STRING (attr));
20947 break;
20948 case DW_FORM_block1:
20949 case DW_FORM_block2:
20950 case DW_FORM_block4:
20951 case DW_FORM_block:
20952 case DW_FORM_exprloc:
0224619f 20953 case DW_FORM_data16:
b6807d98
TT
20954 result = DW_BLOCK (attr)->data;
20955 *len = DW_BLOCK (attr)->size;
20956 break;
20957
20958 /* The DW_AT_const_value attributes are supposed to carry the
20959 symbol's value "represented as it would be on the target
20960 architecture." By the time we get here, it's already been
20961 converted to host endianness, so we just need to sign- or
20962 zero-extend it as appropriate. */
20963 case DW_FORM_data1:
20964 type = die_type (die, cu);
20965 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20966 if (result == NULL)
20967 result = write_constant_as_bytes (obstack, byte_order,
20968 type, value, len);
20969 break;
20970 case DW_FORM_data2:
20971 type = die_type (die, cu);
20972 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20973 if (result == NULL)
20974 result = write_constant_as_bytes (obstack, byte_order,
20975 type, value, len);
20976 break;
20977 case DW_FORM_data4:
20978 type = die_type (die, cu);
20979 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20980 if (result == NULL)
20981 result = write_constant_as_bytes (obstack, byte_order,
20982 type, value, len);
20983 break;
20984 case DW_FORM_data8:
20985 type = die_type (die, cu);
20986 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20987 if (result == NULL)
20988 result = write_constant_as_bytes (obstack, byte_order,
20989 type, value, len);
20990 break;
20991
20992 case DW_FORM_sdata:
663c44ac 20993 case DW_FORM_implicit_const:
b6807d98
TT
20994 type = die_type (die, cu);
20995 result = write_constant_as_bytes (obstack, byte_order,
20996 type, DW_SND (attr), len);
20997 break;
20998
20999 case DW_FORM_udata:
21000 type = die_type (die, cu);
21001 result = write_constant_as_bytes (obstack, byte_order,
21002 type, DW_UNSND (attr), len);
21003 break;
21004
21005 default:
21006 complaint (&symfile_complaints,
21007 _("unsupported const value attribute form: '%s'"),
21008 dwarf_form_name (attr->form));
21009 break;
21010 }
21011
21012 return result;
21013}
21014
7942e96e
AA
21015/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21016 valid type for this die is found. */
21017
21018struct type *
9c541725 21019dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21020 struct dwarf2_per_cu_data *per_cu)
21021{
21022 struct dwarf2_cu *cu;
21023 struct die_info *die;
21024
21025 dw2_setup (per_cu->objfile);
21026
21027 if (per_cu->cu == NULL)
21028 load_cu (per_cu);
21029 cu = per_cu->cu;
21030 if (!cu)
21031 return NULL;
21032
9c541725 21033 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21034 if (!die)
21035 return NULL;
21036
21037 return die_type (die, cu);
21038}
21039
8a9b8146
TT
21040/* Return the type of the DIE at DIE_OFFSET in the CU named by
21041 PER_CU. */
21042
21043struct type *
b64f50a1 21044dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21045 struct dwarf2_per_cu_data *per_cu)
21046{
8a9b8146 21047 dw2_setup (per_cu->objfile);
b64f50a1 21048
9c541725 21049 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21050 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21051}
21052
ac9ec31b 21053/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21054 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21055 On exit *REF_CU is the CU of the result.
21056 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21057
21058static struct die_info *
ac9ec31b
DE
21059follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21060 struct dwarf2_cu **ref_cu)
348e048f 21061{
348e048f 21062 struct die_info temp_die;
348e048f
DE
21063 struct dwarf2_cu *sig_cu;
21064 struct die_info *die;
21065
ac9ec31b
DE
21066 /* While it might be nice to assert sig_type->type == NULL here,
21067 we can get here for DW_AT_imported_declaration where we need
21068 the DIE not the type. */
348e048f
DE
21069
21070 /* If necessary, add it to the queue and load its DIEs. */
21071
95554aad 21072 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21073 read_signatured_type (sig_type);
348e048f 21074
348e048f 21075 sig_cu = sig_type->per_cu.cu;
69d751e3 21076 gdb_assert (sig_cu != NULL);
9c541725
PA
21077 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21078 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21079 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21080 to_underlying (temp_die.sect_off));
348e048f
DE
21081 if (die)
21082 {
796a7ff8
DE
21083 /* For .gdb_index version 7 keep track of included TUs.
21084 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21085 if (dwarf2_per_objfile->index_table != NULL
21086 && dwarf2_per_objfile->index_table->version <= 7)
21087 {
21088 VEC_safe_push (dwarf2_per_cu_ptr,
21089 (*ref_cu)->per_cu->imported_symtabs,
21090 sig_cu->per_cu);
21091 }
21092
348e048f
DE
21093 *ref_cu = sig_cu;
21094 return die;
21095 }
21096
ac9ec31b
DE
21097 return NULL;
21098}
21099
21100/* Follow signatured type referenced by ATTR in SRC_DIE.
21101 On entry *REF_CU is the CU of SRC_DIE.
21102 On exit *REF_CU is the CU of the result.
21103 The result is the DIE of the type.
21104 If the referenced type cannot be found an error is thrown. */
21105
21106static struct die_info *
ff39bb5e 21107follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21108 struct dwarf2_cu **ref_cu)
21109{
21110 ULONGEST signature = DW_SIGNATURE (attr);
21111 struct signatured_type *sig_type;
21112 struct die_info *die;
21113
21114 gdb_assert (attr->form == DW_FORM_ref_sig8);
21115
a2ce51a0 21116 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21117 /* sig_type will be NULL if the signatured type is missing from
21118 the debug info. */
21119 if (sig_type == NULL)
21120 {
21121 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21122 " from DIE at 0x%x [in module %s]"),
9c541725 21123 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21124 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21125 }
21126
21127 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21128 if (die == NULL)
21129 {
21130 dump_die_for_error (src_die);
21131 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21132 " from DIE at 0x%x [in module %s]"),
9c541725 21133 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21134 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21135 }
21136
21137 return die;
21138}
21139
21140/* Get the type specified by SIGNATURE referenced in DIE/CU,
21141 reading in and processing the type unit if necessary. */
21142
21143static struct type *
21144get_signatured_type (struct die_info *die, ULONGEST signature,
21145 struct dwarf2_cu *cu)
21146{
21147 struct signatured_type *sig_type;
21148 struct dwarf2_cu *type_cu;
21149 struct die_info *type_die;
21150 struct type *type;
21151
a2ce51a0 21152 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21153 /* sig_type will be NULL if the signatured type is missing from
21154 the debug info. */
21155 if (sig_type == NULL)
21156 {
21157 complaint (&symfile_complaints,
21158 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21159 " from DIE at 0x%x [in module %s]"),
9c541725 21160 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21161 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21162 return build_error_marker_type (cu, die);
21163 }
21164
21165 /* If we already know the type we're done. */
21166 if (sig_type->type != NULL)
21167 return sig_type->type;
21168
21169 type_cu = cu;
21170 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21171 if (type_die != NULL)
21172 {
21173 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21174 is created. This is important, for example, because for c++ classes
21175 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21176 type = read_type_die (type_die, type_cu);
21177 if (type == NULL)
21178 {
21179 complaint (&symfile_complaints,
21180 _("Dwarf Error: Cannot build signatured type %s"
21181 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21182 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21183 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21184 type = build_error_marker_type (cu, die);
21185 }
21186 }
21187 else
21188 {
21189 complaint (&symfile_complaints,
21190 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21191 " from DIE at 0x%x [in module %s]"),
9c541725 21192 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21193 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21194 type = build_error_marker_type (cu, die);
21195 }
21196 sig_type->type = type;
21197
21198 return type;
21199}
21200
21201/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21202 reading in and processing the type unit if necessary. */
21203
21204static struct type *
ff39bb5e 21205get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21206 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21207{
21208 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21209 if (attr_form_is_ref (attr))
ac9ec31b
DE
21210 {
21211 struct dwarf2_cu *type_cu = cu;
21212 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21213
21214 return read_type_die (type_die, type_cu);
21215 }
21216 else if (attr->form == DW_FORM_ref_sig8)
21217 {
21218 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21219 }
21220 else
21221 {
21222 complaint (&symfile_complaints,
21223 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21224 " at 0x%x [in module %s]"),
9c541725 21225 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21226 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21227 return build_error_marker_type (cu, die);
21228 }
348e048f
DE
21229}
21230
e5fe5e75 21231/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21232
21233static void
e5fe5e75 21234load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21235{
52dc124a 21236 struct signatured_type *sig_type;
348e048f 21237
f4dc4d17
DE
21238 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21239 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21240
6721b2ec
DE
21241 /* We have the per_cu, but we need the signatured_type.
21242 Fortunately this is an easy translation. */
21243 gdb_assert (per_cu->is_debug_types);
21244 sig_type = (struct signatured_type *) per_cu;
348e048f 21245
6721b2ec 21246 gdb_assert (per_cu->cu == NULL);
348e048f 21247
52dc124a 21248 read_signatured_type (sig_type);
348e048f 21249
6721b2ec 21250 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21251}
21252
dee91e82
DE
21253/* die_reader_func for read_signatured_type.
21254 This is identical to load_full_comp_unit_reader,
21255 but is kept separate for now. */
348e048f
DE
21256
21257static void
dee91e82 21258read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21259 const gdb_byte *info_ptr,
dee91e82
DE
21260 struct die_info *comp_unit_die,
21261 int has_children,
21262 void *data)
348e048f 21263{
dee91e82 21264 struct dwarf2_cu *cu = reader->cu;
348e048f 21265
dee91e82
DE
21266 gdb_assert (cu->die_hash == NULL);
21267 cu->die_hash =
21268 htab_create_alloc_ex (cu->header.length / 12,
21269 die_hash,
21270 die_eq,
21271 NULL,
21272 &cu->comp_unit_obstack,
21273 hashtab_obstack_allocate,
21274 dummy_obstack_deallocate);
348e048f 21275
dee91e82
DE
21276 if (has_children)
21277 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21278 &info_ptr, comp_unit_die);
21279 cu->dies = comp_unit_die;
21280 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21281
21282 /* We try not to read any attributes in this function, because not
9cdd5dbd 21283 all CUs needed for references have been loaded yet, and symbol
348e048f 21284 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21285 or we won't be able to build types correctly.
21286 Similarly, if we do not read the producer, we can not apply
21287 producer-specific interpretation. */
95554aad 21288 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21289}
348e048f 21290
3019eac3
DE
21291/* Read in a signatured type and build its CU and DIEs.
21292 If the type is a stub for the real type in a DWO file,
21293 read in the real type from the DWO file as well. */
dee91e82
DE
21294
21295static void
21296read_signatured_type (struct signatured_type *sig_type)
21297{
21298 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21299
3019eac3 21300 gdb_assert (per_cu->is_debug_types);
dee91e82 21301 gdb_assert (per_cu->cu == NULL);
348e048f 21302
f4dc4d17
DE
21303 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21304 read_signatured_type_reader, NULL);
7ee85ab1 21305 sig_type->per_cu.tu_read = 1;
c906108c
SS
21306}
21307
c906108c
SS
21308/* Decode simple location descriptions.
21309 Given a pointer to a dwarf block that defines a location, compute
21310 the location and return the value.
21311
4cecd739
DJ
21312 NOTE drow/2003-11-18: This function is called in two situations
21313 now: for the address of static or global variables (partial symbols
21314 only) and for offsets into structures which are expected to be
21315 (more or less) constant. The partial symbol case should go away,
21316 and only the constant case should remain. That will let this
21317 function complain more accurately. A few special modes are allowed
21318 without complaint for global variables (for instance, global
21319 register values and thread-local values).
c906108c
SS
21320
21321 A location description containing no operations indicates that the
4cecd739 21322 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21323 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21324 callers will only want a very basic result and this can become a
21ae7a4d
JK
21325 complaint.
21326
21327 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21328
21329static CORE_ADDR
e7c27a73 21330decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21331{
e7c27a73 21332 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21333 size_t i;
21334 size_t size = blk->size;
d521ce57 21335 const gdb_byte *data = blk->data;
21ae7a4d
JK
21336 CORE_ADDR stack[64];
21337 int stacki;
21338 unsigned int bytes_read, unsnd;
21339 gdb_byte op;
c906108c 21340
21ae7a4d
JK
21341 i = 0;
21342 stacki = 0;
21343 stack[stacki] = 0;
21344 stack[++stacki] = 0;
21345
21346 while (i < size)
21347 {
21348 op = data[i++];
21349 switch (op)
21350 {
21351 case DW_OP_lit0:
21352 case DW_OP_lit1:
21353 case DW_OP_lit2:
21354 case DW_OP_lit3:
21355 case DW_OP_lit4:
21356 case DW_OP_lit5:
21357 case DW_OP_lit6:
21358 case DW_OP_lit7:
21359 case DW_OP_lit8:
21360 case DW_OP_lit9:
21361 case DW_OP_lit10:
21362 case DW_OP_lit11:
21363 case DW_OP_lit12:
21364 case DW_OP_lit13:
21365 case DW_OP_lit14:
21366 case DW_OP_lit15:
21367 case DW_OP_lit16:
21368 case DW_OP_lit17:
21369 case DW_OP_lit18:
21370 case DW_OP_lit19:
21371 case DW_OP_lit20:
21372 case DW_OP_lit21:
21373 case DW_OP_lit22:
21374 case DW_OP_lit23:
21375 case DW_OP_lit24:
21376 case DW_OP_lit25:
21377 case DW_OP_lit26:
21378 case DW_OP_lit27:
21379 case DW_OP_lit28:
21380 case DW_OP_lit29:
21381 case DW_OP_lit30:
21382 case DW_OP_lit31:
21383 stack[++stacki] = op - DW_OP_lit0;
21384 break;
f1bea926 21385
21ae7a4d
JK
21386 case DW_OP_reg0:
21387 case DW_OP_reg1:
21388 case DW_OP_reg2:
21389 case DW_OP_reg3:
21390 case DW_OP_reg4:
21391 case DW_OP_reg5:
21392 case DW_OP_reg6:
21393 case DW_OP_reg7:
21394 case DW_OP_reg8:
21395 case DW_OP_reg9:
21396 case DW_OP_reg10:
21397 case DW_OP_reg11:
21398 case DW_OP_reg12:
21399 case DW_OP_reg13:
21400 case DW_OP_reg14:
21401 case DW_OP_reg15:
21402 case DW_OP_reg16:
21403 case DW_OP_reg17:
21404 case DW_OP_reg18:
21405 case DW_OP_reg19:
21406 case DW_OP_reg20:
21407 case DW_OP_reg21:
21408 case DW_OP_reg22:
21409 case DW_OP_reg23:
21410 case DW_OP_reg24:
21411 case DW_OP_reg25:
21412 case DW_OP_reg26:
21413 case DW_OP_reg27:
21414 case DW_OP_reg28:
21415 case DW_OP_reg29:
21416 case DW_OP_reg30:
21417 case DW_OP_reg31:
21418 stack[++stacki] = op - DW_OP_reg0;
21419 if (i < size)
21420 dwarf2_complex_location_expr_complaint ();
21421 break;
c906108c 21422
21ae7a4d
JK
21423 case DW_OP_regx:
21424 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21425 i += bytes_read;
21426 stack[++stacki] = unsnd;
21427 if (i < size)
21428 dwarf2_complex_location_expr_complaint ();
21429 break;
c906108c 21430
21ae7a4d
JK
21431 case DW_OP_addr:
21432 stack[++stacki] = read_address (objfile->obfd, &data[i],
21433 cu, &bytes_read);
21434 i += bytes_read;
21435 break;
d53d4ac5 21436
21ae7a4d
JK
21437 case DW_OP_const1u:
21438 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21439 i += 1;
21440 break;
21441
21442 case DW_OP_const1s:
21443 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21444 i += 1;
21445 break;
21446
21447 case DW_OP_const2u:
21448 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21449 i += 2;
21450 break;
21451
21452 case DW_OP_const2s:
21453 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21454 i += 2;
21455 break;
d53d4ac5 21456
21ae7a4d
JK
21457 case DW_OP_const4u:
21458 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21459 i += 4;
21460 break;
21461
21462 case DW_OP_const4s:
21463 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21464 i += 4;
21465 break;
21466
585861ea
JK
21467 case DW_OP_const8u:
21468 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21469 i += 8;
21470 break;
21471
21ae7a4d
JK
21472 case DW_OP_constu:
21473 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21474 &bytes_read);
21475 i += bytes_read;
21476 break;
21477
21478 case DW_OP_consts:
21479 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21480 i += bytes_read;
21481 break;
21482
21483 case DW_OP_dup:
21484 stack[stacki + 1] = stack[stacki];
21485 stacki++;
21486 break;
21487
21488 case DW_OP_plus:
21489 stack[stacki - 1] += stack[stacki];
21490 stacki--;
21491 break;
21492
21493 case DW_OP_plus_uconst:
21494 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21495 &bytes_read);
21496 i += bytes_read;
21497 break;
21498
21499 case DW_OP_minus:
21500 stack[stacki - 1] -= stack[stacki];
21501 stacki--;
21502 break;
21503
21504 case DW_OP_deref:
21505 /* If we're not the last op, then we definitely can't encode
21506 this using GDB's address_class enum. This is valid for partial
21507 global symbols, although the variable's address will be bogus
21508 in the psymtab. */
21509 if (i < size)
21510 dwarf2_complex_location_expr_complaint ();
21511 break;
21512
21513 case DW_OP_GNU_push_tls_address:
4aa4e28b 21514 case DW_OP_form_tls_address:
21ae7a4d
JK
21515 /* The top of the stack has the offset from the beginning
21516 of the thread control block at which the variable is located. */
21517 /* Nothing should follow this operator, so the top of stack would
21518 be returned. */
21519 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21520 address will be bogus in the psymtab. Make it always at least
21521 non-zero to not look as a variable garbage collected by linker
21522 which have DW_OP_addr 0. */
21ae7a4d
JK
21523 if (i < size)
21524 dwarf2_complex_location_expr_complaint ();
585861ea 21525 stack[stacki]++;
21ae7a4d
JK
21526 break;
21527
21528 case DW_OP_GNU_uninit:
21529 break;
21530
3019eac3 21531 case DW_OP_GNU_addr_index:
49f6c839 21532 case DW_OP_GNU_const_index:
3019eac3
DE
21533 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21534 &bytes_read);
21535 i += bytes_read;
21536 break;
21537
21ae7a4d
JK
21538 default:
21539 {
f39c6ffd 21540 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21541
21542 if (name)
21543 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21544 name);
21545 else
21546 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21547 op);
21548 }
21549
21550 return (stack[stacki]);
d53d4ac5 21551 }
3c6e0cb3 21552
21ae7a4d
JK
21553 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21554 outside of the allocated space. Also enforce minimum>0. */
21555 if (stacki >= ARRAY_SIZE (stack) - 1)
21556 {
21557 complaint (&symfile_complaints,
21558 _("location description stack overflow"));
21559 return 0;
21560 }
21561
21562 if (stacki <= 0)
21563 {
21564 complaint (&symfile_complaints,
21565 _("location description stack underflow"));
21566 return 0;
21567 }
21568 }
21569 return (stack[stacki]);
c906108c
SS
21570}
21571
21572/* memory allocation interface */
21573
c906108c 21574static struct dwarf_block *
7b5a2f43 21575dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21576{
8d749320 21577 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21578}
21579
c906108c 21580static struct die_info *
b60c80d6 21581dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21582{
21583 struct die_info *die;
b60c80d6
DJ
21584 size_t size = sizeof (struct die_info);
21585
21586 if (num_attrs > 1)
21587 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21588
b60c80d6 21589 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21590 memset (die, 0, sizeof (struct die_info));
21591 return (die);
21592}
2e276125
JB
21593
21594\f
21595/* Macro support. */
21596
233d95b5
JK
21597/* Return file name relative to the compilation directory of file number I in
21598 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21599 responsible for freeing it. */
233d95b5 21600
2e276125 21601static char *
233d95b5 21602file_file_name (int file, struct line_header *lh)
2e276125 21603{
6a83a1e6
EZ
21604 /* Is the file number a valid index into the line header's file name
21605 table? Remember that file numbers start with one, not zero. */
fff8551c 21606 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21607 {
8c43009f 21608 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21609
8c43009f
PA
21610 if (!IS_ABSOLUTE_PATH (fe.name))
21611 {
21612 const char *dir = fe.include_dir (lh);
21613 if (dir != NULL)
21614 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21615 }
21616 return xstrdup (fe.name);
6a83a1e6 21617 }
2e276125
JB
21618 else
21619 {
6a83a1e6
EZ
21620 /* The compiler produced a bogus file number. We can at least
21621 record the macro definitions made in the file, even if we
21622 won't be able to find the file by name. */
21623 char fake_name[80];
9a619af0 21624
8c042590
PM
21625 xsnprintf (fake_name, sizeof (fake_name),
21626 "<bad macro file number %d>", file);
2e276125 21627
6e70227d 21628 complaint (&symfile_complaints,
6a83a1e6
EZ
21629 _("bad file number in macro information (%d)"),
21630 file);
2e276125 21631
6a83a1e6 21632 return xstrdup (fake_name);
2e276125
JB
21633 }
21634}
21635
233d95b5
JK
21636/* Return the full name of file number I in *LH's file name table.
21637 Use COMP_DIR as the name of the current directory of the
21638 compilation. The result is allocated using xmalloc; the caller is
21639 responsible for freeing it. */
21640static char *
21641file_full_name (int file, struct line_header *lh, const char *comp_dir)
21642{
21643 /* Is the file number a valid index into the line header's file name
21644 table? Remember that file numbers start with one, not zero. */
fff8551c 21645 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21646 {
21647 char *relative = file_file_name (file, lh);
21648
21649 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21650 return relative;
b36cec19
PA
21651 return reconcat (relative, comp_dir, SLASH_STRING,
21652 relative, (char *) NULL);
233d95b5
JK
21653 }
21654 else
21655 return file_file_name (file, lh);
21656}
21657
2e276125
JB
21658
21659static struct macro_source_file *
21660macro_start_file (int file, int line,
21661 struct macro_source_file *current_file,
43f3e411 21662 struct line_header *lh)
2e276125 21663{
233d95b5
JK
21664 /* File name relative to the compilation directory of this source file. */
21665 char *file_name = file_file_name (file, lh);
2e276125 21666
2e276125 21667 if (! current_file)
abc9d0dc 21668 {
fc474241
DE
21669 /* Note: We don't create a macro table for this compilation unit
21670 at all until we actually get a filename. */
43f3e411 21671 struct macro_table *macro_table = get_macro_table ();
fc474241 21672
abc9d0dc
TT
21673 /* If we have no current file, then this must be the start_file
21674 directive for the compilation unit's main source file. */
fc474241
DE
21675 current_file = macro_set_main (macro_table, file_name);
21676 macro_define_special (macro_table);
abc9d0dc 21677 }
2e276125 21678 else
233d95b5 21679 current_file = macro_include (current_file, line, file_name);
2e276125 21680
233d95b5 21681 xfree (file_name);
6e70227d 21682
2e276125
JB
21683 return current_file;
21684}
21685
2e276125
JB
21686static const char *
21687consume_improper_spaces (const char *p, const char *body)
21688{
21689 if (*p == ' ')
21690 {
4d3c2250 21691 complaint (&symfile_complaints,
3e43a32a
MS
21692 _("macro definition contains spaces "
21693 "in formal argument list:\n`%s'"),
4d3c2250 21694 body);
2e276125
JB
21695
21696 while (*p == ' ')
21697 p++;
21698 }
21699
21700 return p;
21701}
21702
21703
21704static void
21705parse_macro_definition (struct macro_source_file *file, int line,
21706 const char *body)
21707{
21708 const char *p;
21709
21710 /* The body string takes one of two forms. For object-like macro
21711 definitions, it should be:
21712
21713 <macro name> " " <definition>
21714
21715 For function-like macro definitions, it should be:
21716
21717 <macro name> "() " <definition>
21718 or
21719 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21720
21721 Spaces may appear only where explicitly indicated, and in the
21722 <definition>.
21723
21724 The Dwarf 2 spec says that an object-like macro's name is always
21725 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21726 the space when the macro's definition is the empty string.
2e276125
JB
21727
21728 The Dwarf 2 spec says that there should be no spaces between the
21729 formal arguments in a function-like macro's formal argument list,
21730 but versions of GCC around March 2002 include spaces after the
21731 commas. */
21732
21733
21734 /* Find the extent of the macro name. The macro name is terminated
21735 by either a space or null character (for an object-like macro) or
21736 an opening paren (for a function-like macro). */
21737 for (p = body; *p; p++)
21738 if (*p == ' ' || *p == '(')
21739 break;
21740
21741 if (*p == ' ' || *p == '\0')
21742 {
21743 /* It's an object-like macro. */
21744 int name_len = p - body;
3f8a7804 21745 char *name = savestring (body, name_len);
2e276125
JB
21746 const char *replacement;
21747
21748 if (*p == ' ')
21749 replacement = body + name_len + 1;
21750 else
21751 {
4d3c2250 21752 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21753 replacement = body + name_len;
21754 }
6e70227d 21755
2e276125
JB
21756 macro_define_object (file, line, name, replacement);
21757
21758 xfree (name);
21759 }
21760 else if (*p == '(')
21761 {
21762 /* It's a function-like macro. */
3f8a7804 21763 char *name = savestring (body, p - body);
2e276125
JB
21764 int argc = 0;
21765 int argv_size = 1;
8d749320 21766 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21767
21768 p++;
21769
21770 p = consume_improper_spaces (p, body);
21771
21772 /* Parse the formal argument list. */
21773 while (*p && *p != ')')
21774 {
21775 /* Find the extent of the current argument name. */
21776 const char *arg_start = p;
21777
21778 while (*p && *p != ',' && *p != ')' && *p != ' ')
21779 p++;
21780
21781 if (! *p || p == arg_start)
4d3c2250 21782 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21783 else
21784 {
21785 /* Make sure argv has room for the new argument. */
21786 if (argc >= argv_size)
21787 {
21788 argv_size *= 2;
224c3ddb 21789 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21790 }
21791
3f8a7804 21792 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
21793 }
21794
21795 p = consume_improper_spaces (p, body);
21796
21797 /* Consume the comma, if present. */
21798 if (*p == ',')
21799 {
21800 p++;
21801
21802 p = consume_improper_spaces (p, body);
21803 }
21804 }
21805
21806 if (*p == ')')
21807 {
21808 p++;
21809
21810 if (*p == ' ')
21811 /* Perfectly formed definition, no complaints. */
21812 macro_define_function (file, line, name,
6e70227d 21813 argc, (const char **) argv,
2e276125
JB
21814 p + 1);
21815 else if (*p == '\0')
21816 {
21817 /* Complain, but do define it. */
4d3c2250 21818 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21819 macro_define_function (file, line, name,
6e70227d 21820 argc, (const char **) argv,
2e276125
JB
21821 p);
21822 }
21823 else
21824 /* Just complain. */
4d3c2250 21825 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21826 }
21827 else
21828 /* Just complain. */
4d3c2250 21829 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21830
21831 xfree (name);
21832 {
21833 int i;
21834
21835 for (i = 0; i < argc; i++)
21836 xfree (argv[i]);
21837 }
21838 xfree (argv);
21839 }
21840 else
4d3c2250 21841 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21842}
21843
cf2c3c16
TT
21844/* Skip some bytes from BYTES according to the form given in FORM.
21845 Returns the new pointer. */
2e276125 21846
d521ce57
TT
21847static const gdb_byte *
21848skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21849 enum dwarf_form form,
21850 unsigned int offset_size,
21851 struct dwarf2_section_info *section)
2e276125 21852{
cf2c3c16 21853 unsigned int bytes_read;
2e276125 21854
cf2c3c16 21855 switch (form)
2e276125 21856 {
cf2c3c16
TT
21857 case DW_FORM_data1:
21858 case DW_FORM_flag:
21859 ++bytes;
21860 break;
21861
21862 case DW_FORM_data2:
21863 bytes += 2;
21864 break;
21865
21866 case DW_FORM_data4:
21867 bytes += 4;
21868 break;
21869
21870 case DW_FORM_data8:
21871 bytes += 8;
21872 break;
21873
0224619f
JK
21874 case DW_FORM_data16:
21875 bytes += 16;
21876 break;
21877
cf2c3c16
TT
21878 case DW_FORM_string:
21879 read_direct_string (abfd, bytes, &bytes_read);
21880 bytes += bytes_read;
21881 break;
21882
21883 case DW_FORM_sec_offset:
21884 case DW_FORM_strp:
36586728 21885 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21886 bytes += offset_size;
21887 break;
21888
21889 case DW_FORM_block:
21890 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21891 bytes += bytes_read;
21892 break;
21893
21894 case DW_FORM_block1:
21895 bytes += 1 + read_1_byte (abfd, bytes);
21896 break;
21897 case DW_FORM_block2:
21898 bytes += 2 + read_2_bytes (abfd, bytes);
21899 break;
21900 case DW_FORM_block4:
21901 bytes += 4 + read_4_bytes (abfd, bytes);
21902 break;
21903
21904 case DW_FORM_sdata:
21905 case DW_FORM_udata:
3019eac3
DE
21906 case DW_FORM_GNU_addr_index:
21907 case DW_FORM_GNU_str_index:
d521ce57 21908 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21909 if (bytes == NULL)
21910 {
21911 dwarf2_section_buffer_overflow_complaint (section);
21912 return NULL;
21913 }
cf2c3c16
TT
21914 break;
21915
663c44ac
JK
21916 case DW_FORM_implicit_const:
21917 break;
21918
cf2c3c16
TT
21919 default:
21920 {
21921 complain:
21922 complaint (&symfile_complaints,
21923 _("invalid form 0x%x in `%s'"),
a32a8923 21924 form, get_section_name (section));
cf2c3c16
TT
21925 return NULL;
21926 }
2e276125
JB
21927 }
21928
cf2c3c16
TT
21929 return bytes;
21930}
757a13d0 21931
cf2c3c16
TT
21932/* A helper for dwarf_decode_macros that handles skipping an unknown
21933 opcode. Returns an updated pointer to the macro data buffer; or,
21934 on error, issues a complaint and returns NULL. */
757a13d0 21935
d521ce57 21936static const gdb_byte *
cf2c3c16 21937skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21938 const gdb_byte **opcode_definitions,
21939 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21940 bfd *abfd,
21941 unsigned int offset_size,
21942 struct dwarf2_section_info *section)
21943{
21944 unsigned int bytes_read, i;
21945 unsigned long arg;
d521ce57 21946 const gdb_byte *defn;
2e276125 21947
cf2c3c16 21948 if (opcode_definitions[opcode] == NULL)
2e276125 21949 {
cf2c3c16
TT
21950 complaint (&symfile_complaints,
21951 _("unrecognized DW_MACFINO opcode 0x%x"),
21952 opcode);
21953 return NULL;
21954 }
2e276125 21955
cf2c3c16
TT
21956 defn = opcode_definitions[opcode];
21957 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21958 defn += bytes_read;
2e276125 21959
cf2c3c16
TT
21960 for (i = 0; i < arg; ++i)
21961 {
aead7601
SM
21962 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21963 (enum dwarf_form) defn[i], offset_size,
f664829e 21964 section);
cf2c3c16
TT
21965 if (mac_ptr == NULL)
21966 {
21967 /* skip_form_bytes already issued the complaint. */
21968 return NULL;
21969 }
21970 }
757a13d0 21971
cf2c3c16
TT
21972 return mac_ptr;
21973}
757a13d0 21974
cf2c3c16
TT
21975/* A helper function which parses the header of a macro section.
21976 If the macro section is the extended (for now called "GNU") type,
21977 then this updates *OFFSET_SIZE. Returns a pointer to just after
21978 the header, or issues a complaint and returns NULL on error. */
757a13d0 21979
d521ce57
TT
21980static const gdb_byte *
21981dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21982 bfd *abfd,
d521ce57 21983 const gdb_byte *mac_ptr,
cf2c3c16
TT
21984 unsigned int *offset_size,
21985 int section_is_gnu)
21986{
21987 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21988
cf2c3c16
TT
21989 if (section_is_gnu)
21990 {
21991 unsigned int version, flags;
757a13d0 21992
cf2c3c16 21993 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21994 if (version != 4 && version != 5)
cf2c3c16
TT
21995 {
21996 complaint (&symfile_complaints,
21997 _("unrecognized version `%d' in .debug_macro section"),
21998 version);
21999 return NULL;
22000 }
22001 mac_ptr += 2;
757a13d0 22002
cf2c3c16
TT
22003 flags = read_1_byte (abfd, mac_ptr);
22004 ++mac_ptr;
22005 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22006
cf2c3c16
TT
22007 if ((flags & 2) != 0)
22008 /* We don't need the line table offset. */
22009 mac_ptr += *offset_size;
757a13d0 22010
cf2c3c16
TT
22011 /* Vendor opcode descriptions. */
22012 if ((flags & 4) != 0)
22013 {
22014 unsigned int i, count;
757a13d0 22015
cf2c3c16
TT
22016 count = read_1_byte (abfd, mac_ptr);
22017 ++mac_ptr;
22018 for (i = 0; i < count; ++i)
22019 {
22020 unsigned int opcode, bytes_read;
22021 unsigned long arg;
22022
22023 opcode = read_1_byte (abfd, mac_ptr);
22024 ++mac_ptr;
22025 opcode_definitions[opcode] = mac_ptr;
22026 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22027 mac_ptr += bytes_read;
22028 mac_ptr += arg;
22029 }
757a13d0 22030 }
cf2c3c16 22031 }
757a13d0 22032
cf2c3c16
TT
22033 return mac_ptr;
22034}
757a13d0 22035
cf2c3c16 22036/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22037 including DW_MACRO_import. */
cf2c3c16
TT
22038
22039static void
d521ce57
TT
22040dwarf_decode_macro_bytes (bfd *abfd,
22041 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22042 struct macro_source_file *current_file,
43f3e411 22043 struct line_header *lh,
cf2c3c16 22044 struct dwarf2_section_info *section,
36586728 22045 int section_is_gnu, int section_is_dwz,
cf2c3c16 22046 unsigned int offset_size,
8fc3fc34 22047 htab_t include_hash)
cf2c3c16 22048{
4d663531 22049 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22050 enum dwarf_macro_record_type macinfo_type;
22051 int at_commandline;
d521ce57 22052 const gdb_byte *opcode_definitions[256];
757a13d0 22053
cf2c3c16
TT
22054 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22055 &offset_size, section_is_gnu);
22056 if (mac_ptr == NULL)
22057 {
22058 /* We already issued a complaint. */
22059 return;
22060 }
757a13d0
JK
22061
22062 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22063 GDB is still reading the definitions from command line. First
22064 DW_MACINFO_start_file will need to be ignored as it was already executed
22065 to create CURRENT_FILE for the main source holding also the command line
22066 definitions. On first met DW_MACINFO_start_file this flag is reset to
22067 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22068
22069 at_commandline = 1;
22070
22071 do
22072 {
22073 /* Do we at least have room for a macinfo type byte? */
22074 if (mac_ptr >= mac_end)
22075 {
f664829e 22076 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22077 break;
22078 }
22079
aead7601 22080 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22081 mac_ptr++;
22082
cf2c3c16
TT
22083 /* Note that we rely on the fact that the corresponding GNU and
22084 DWARF constants are the same. */
757a13d0
JK
22085 switch (macinfo_type)
22086 {
22087 /* A zero macinfo type indicates the end of the macro
22088 information. */
22089 case 0:
22090 break;
2e276125 22091
0af92d60
JK
22092 case DW_MACRO_define:
22093 case DW_MACRO_undef:
22094 case DW_MACRO_define_strp:
22095 case DW_MACRO_undef_strp:
22096 case DW_MACRO_define_sup:
22097 case DW_MACRO_undef_sup:
2e276125 22098 {
891d2f0b 22099 unsigned int bytes_read;
2e276125 22100 int line;
d521ce57 22101 const char *body;
cf2c3c16 22102 int is_define;
2e276125 22103
cf2c3c16
TT
22104 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22105 mac_ptr += bytes_read;
22106
0af92d60
JK
22107 if (macinfo_type == DW_MACRO_define
22108 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22109 {
22110 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22111 mac_ptr += bytes_read;
22112 }
22113 else
22114 {
22115 LONGEST str_offset;
22116
22117 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22118 mac_ptr += offset_size;
2e276125 22119
0af92d60
JK
22120 if (macinfo_type == DW_MACRO_define_sup
22121 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22122 || section_is_dwz)
36586728
TT
22123 {
22124 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22125
22126 body = read_indirect_string_from_dwz (dwz, str_offset);
22127 }
22128 else
22129 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22130 }
22131
0af92d60
JK
22132 is_define = (macinfo_type == DW_MACRO_define
22133 || macinfo_type == DW_MACRO_define_strp
22134 || macinfo_type == DW_MACRO_define_sup);
2e276125 22135 if (! current_file)
757a13d0
JK
22136 {
22137 /* DWARF violation as no main source is present. */
22138 complaint (&symfile_complaints,
22139 _("debug info with no main source gives macro %s "
22140 "on line %d: %s"),
cf2c3c16
TT
22141 is_define ? _("definition") : _("undefinition"),
22142 line, body);
757a13d0
JK
22143 break;
22144 }
3e43a32a
MS
22145 if ((line == 0 && !at_commandline)
22146 || (line != 0 && at_commandline))
4d3c2250 22147 complaint (&symfile_complaints,
757a13d0
JK
22148 _("debug info gives %s macro %s with %s line %d: %s"),
22149 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22150 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22151 line == 0 ? _("zero") : _("non-zero"), line, body);
22152
cf2c3c16 22153 if (is_define)
757a13d0 22154 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22155 else
22156 {
0af92d60
JK
22157 gdb_assert (macinfo_type == DW_MACRO_undef
22158 || macinfo_type == DW_MACRO_undef_strp
22159 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22160 macro_undef (current_file, line, body);
22161 }
2e276125
JB
22162 }
22163 break;
22164
0af92d60 22165 case DW_MACRO_start_file:
2e276125 22166 {
891d2f0b 22167 unsigned int bytes_read;
2e276125
JB
22168 int line, file;
22169
22170 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22171 mac_ptr += bytes_read;
22172 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22173 mac_ptr += bytes_read;
22174
3e43a32a
MS
22175 if ((line == 0 && !at_commandline)
22176 || (line != 0 && at_commandline))
757a13d0
JK
22177 complaint (&symfile_complaints,
22178 _("debug info gives source %d included "
22179 "from %s at %s line %d"),
22180 file, at_commandline ? _("command-line") : _("file"),
22181 line == 0 ? _("zero") : _("non-zero"), line);
22182
22183 if (at_commandline)
22184 {
0af92d60 22185 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22186 pass one. */
757a13d0
JK
22187 at_commandline = 0;
22188 }
22189 else
43f3e411 22190 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22191 }
22192 break;
22193
0af92d60 22194 case DW_MACRO_end_file:
2e276125 22195 if (! current_file)
4d3c2250 22196 complaint (&symfile_complaints,
3e43a32a
MS
22197 _("macro debug info has an unmatched "
22198 "`close_file' directive"));
2e276125
JB
22199 else
22200 {
22201 current_file = current_file->included_by;
22202 if (! current_file)
22203 {
cf2c3c16 22204 enum dwarf_macro_record_type next_type;
2e276125
JB
22205
22206 /* GCC circa March 2002 doesn't produce the zero
22207 type byte marking the end of the compilation
22208 unit. Complain if it's not there, but exit no
22209 matter what. */
22210
22211 /* Do we at least have room for a macinfo type byte? */
22212 if (mac_ptr >= mac_end)
22213 {
f664829e 22214 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22215 return;
22216 }
22217
22218 /* We don't increment mac_ptr here, so this is just
22219 a look-ahead. */
aead7601
SM
22220 next_type
22221 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22222 mac_ptr);
2e276125 22223 if (next_type != 0)
4d3c2250 22224 complaint (&symfile_complaints,
3e43a32a
MS
22225 _("no terminating 0-type entry for "
22226 "macros in `.debug_macinfo' section"));
2e276125
JB
22227
22228 return;
22229 }
22230 }
22231 break;
22232
0af92d60
JK
22233 case DW_MACRO_import:
22234 case DW_MACRO_import_sup:
cf2c3c16
TT
22235 {
22236 LONGEST offset;
8fc3fc34 22237 void **slot;
a036ba48
TT
22238 bfd *include_bfd = abfd;
22239 struct dwarf2_section_info *include_section = section;
d521ce57 22240 const gdb_byte *include_mac_end = mac_end;
a036ba48 22241 int is_dwz = section_is_dwz;
d521ce57 22242 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22243
22244 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22245 mac_ptr += offset_size;
22246
0af92d60 22247 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22248 {
22249 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22250
4d663531 22251 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22252
a036ba48 22253 include_section = &dwz->macro;
a32a8923 22254 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22255 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22256 is_dwz = 1;
22257 }
22258
22259 new_mac_ptr = include_section->buffer + offset;
22260 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22261
8fc3fc34
TT
22262 if (*slot != NULL)
22263 {
22264 /* This has actually happened; see
22265 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22266 complaint (&symfile_complaints,
0af92d60 22267 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22268 ".debug_macro section"));
22269 }
22270 else
22271 {
d521ce57 22272 *slot = (void *) new_mac_ptr;
36586728 22273
a036ba48 22274 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22275 include_mac_end, current_file, lh,
36586728 22276 section, section_is_gnu, is_dwz,
4d663531 22277 offset_size, include_hash);
8fc3fc34 22278
d521ce57 22279 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22280 }
cf2c3c16
TT
22281 }
22282 break;
22283
2e276125 22284 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22285 if (!section_is_gnu)
22286 {
22287 unsigned int bytes_read;
2e276125 22288
ac298888
TT
22289 /* This reads the constant, but since we don't recognize
22290 any vendor extensions, we ignore it. */
22291 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22292 mac_ptr += bytes_read;
22293 read_direct_string (abfd, mac_ptr, &bytes_read);
22294 mac_ptr += bytes_read;
2e276125 22295
cf2c3c16
TT
22296 /* We don't recognize any vendor extensions. */
22297 break;
22298 }
22299 /* FALLTHROUGH */
22300
22301 default:
22302 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22303 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22304 section);
22305 if (mac_ptr == NULL)
22306 return;
22307 break;
2e276125 22308 }
757a13d0 22309 } while (macinfo_type != 0);
2e276125 22310}
8e19ed76 22311
cf2c3c16 22312static void
09262596 22313dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22314 int section_is_gnu)
cf2c3c16 22315{
bb5ed363 22316 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22317 struct line_header *lh = cu->line_header;
22318 bfd *abfd;
d521ce57 22319 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22320 struct macro_source_file *current_file = 0;
22321 enum dwarf_macro_record_type macinfo_type;
22322 unsigned int offset_size = cu->header.offset_size;
d521ce57 22323 const gdb_byte *opcode_definitions[256];
8fc3fc34 22324 struct cleanup *cleanup;
8fc3fc34 22325 void **slot;
09262596
DE
22326 struct dwarf2_section_info *section;
22327 const char *section_name;
22328
22329 if (cu->dwo_unit != NULL)
22330 {
22331 if (section_is_gnu)
22332 {
22333 section = &cu->dwo_unit->dwo_file->sections.macro;
22334 section_name = ".debug_macro.dwo";
22335 }
22336 else
22337 {
22338 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22339 section_name = ".debug_macinfo.dwo";
22340 }
22341 }
22342 else
22343 {
22344 if (section_is_gnu)
22345 {
22346 section = &dwarf2_per_objfile->macro;
22347 section_name = ".debug_macro";
22348 }
22349 else
22350 {
22351 section = &dwarf2_per_objfile->macinfo;
22352 section_name = ".debug_macinfo";
22353 }
22354 }
cf2c3c16 22355
bb5ed363 22356 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22357 if (section->buffer == NULL)
22358 {
fceca515 22359 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22360 return;
22361 }
a32a8923 22362 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22363
22364 /* First pass: Find the name of the base filename.
22365 This filename is needed in order to process all macros whose definition
22366 (or undefinition) comes from the command line. These macros are defined
22367 before the first DW_MACINFO_start_file entry, and yet still need to be
22368 associated to the base file.
22369
22370 To determine the base file name, we scan the macro definitions until we
22371 reach the first DW_MACINFO_start_file entry. We then initialize
22372 CURRENT_FILE accordingly so that any macro definition found before the
22373 first DW_MACINFO_start_file can still be associated to the base file. */
22374
22375 mac_ptr = section->buffer + offset;
22376 mac_end = section->buffer + section->size;
22377
22378 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22379 &offset_size, section_is_gnu);
22380 if (mac_ptr == NULL)
22381 {
22382 /* We already issued a complaint. */
22383 return;
22384 }
22385
22386 do
22387 {
22388 /* Do we at least have room for a macinfo type byte? */
22389 if (mac_ptr >= mac_end)
22390 {
22391 /* Complaint is printed during the second pass as GDB will probably
22392 stop the first pass earlier upon finding
22393 DW_MACINFO_start_file. */
22394 break;
22395 }
22396
aead7601 22397 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22398 mac_ptr++;
22399
22400 /* Note that we rely on the fact that the corresponding GNU and
22401 DWARF constants are the same. */
22402 switch (macinfo_type)
22403 {
22404 /* A zero macinfo type indicates the end of the macro
22405 information. */
22406 case 0:
22407 break;
22408
0af92d60
JK
22409 case DW_MACRO_define:
22410 case DW_MACRO_undef:
cf2c3c16
TT
22411 /* Only skip the data by MAC_PTR. */
22412 {
22413 unsigned int bytes_read;
22414
22415 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22416 mac_ptr += bytes_read;
22417 read_direct_string (abfd, mac_ptr, &bytes_read);
22418 mac_ptr += bytes_read;
22419 }
22420 break;
22421
0af92d60 22422 case DW_MACRO_start_file:
cf2c3c16
TT
22423 {
22424 unsigned int bytes_read;
22425 int line, file;
22426
22427 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22428 mac_ptr += bytes_read;
22429 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22430 mac_ptr += bytes_read;
22431
43f3e411 22432 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22433 }
22434 break;
22435
0af92d60 22436 case DW_MACRO_end_file:
cf2c3c16
TT
22437 /* No data to skip by MAC_PTR. */
22438 break;
22439
0af92d60
JK
22440 case DW_MACRO_define_strp:
22441 case DW_MACRO_undef_strp:
22442 case DW_MACRO_define_sup:
22443 case DW_MACRO_undef_sup:
cf2c3c16
TT
22444 {
22445 unsigned int bytes_read;
22446
22447 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22448 mac_ptr += bytes_read;
22449 mac_ptr += offset_size;
22450 }
22451 break;
22452
0af92d60
JK
22453 case DW_MACRO_import:
22454 case DW_MACRO_import_sup:
cf2c3c16 22455 /* Note that, according to the spec, a transparent include
0af92d60 22456 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22457 skip this opcode. */
22458 mac_ptr += offset_size;
22459 break;
22460
22461 case DW_MACINFO_vendor_ext:
22462 /* Only skip the data by MAC_PTR. */
22463 if (!section_is_gnu)
22464 {
22465 unsigned int bytes_read;
22466
22467 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22468 mac_ptr += bytes_read;
22469 read_direct_string (abfd, mac_ptr, &bytes_read);
22470 mac_ptr += bytes_read;
22471 }
22472 /* FALLTHROUGH */
22473
22474 default:
22475 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22476 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22477 section);
22478 if (mac_ptr == NULL)
22479 return;
22480 break;
22481 }
22482 } while (macinfo_type != 0 && current_file == NULL);
22483
22484 /* Second pass: Process all entries.
22485
22486 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22487 command-line macro definitions/undefinitions. This flag is unset when we
22488 reach the first DW_MACINFO_start_file entry. */
22489
fc4007c9
TT
22490 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22491 htab_eq_pointer,
22492 NULL, xcalloc, xfree));
8fc3fc34 22493 mac_ptr = section->buffer + offset;
fc4007c9 22494 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22495 *slot = (void *) mac_ptr;
8fc3fc34 22496 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22497 current_file, lh, section,
fc4007c9
TT
22498 section_is_gnu, 0, offset_size,
22499 include_hash.get ());
cf2c3c16
TT
22500}
22501
8e19ed76 22502/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22503 if so return true else false. */
380bca97 22504
8e19ed76 22505static int
6e5a29e1 22506attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22507{
22508 return (attr == NULL ? 0 :
22509 attr->form == DW_FORM_block1
22510 || attr->form == DW_FORM_block2
22511 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22512 || attr->form == DW_FORM_block
22513 || attr->form == DW_FORM_exprloc);
8e19ed76 22514}
4c2df51b 22515
c6a0999f
JB
22516/* Return non-zero if ATTR's value is a section offset --- classes
22517 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22518 You may use DW_UNSND (attr) to retrieve such offsets.
22519
22520 Section 7.5.4, "Attribute Encodings", explains that no attribute
22521 may have a value that belongs to more than one of these classes; it
22522 would be ambiguous if we did, because we use the same forms for all
22523 of them. */
380bca97 22524
3690dd37 22525static int
6e5a29e1 22526attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22527{
22528 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22529 || attr->form == DW_FORM_data8
22530 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22531}
22532
3690dd37
JB
22533/* Return non-zero if ATTR's value falls in the 'constant' class, or
22534 zero otherwise. When this function returns true, you can apply
22535 dwarf2_get_attr_constant_value to it.
22536
22537 However, note that for some attributes you must check
22538 attr_form_is_section_offset before using this test. DW_FORM_data4
22539 and DW_FORM_data8 are members of both the constant class, and of
22540 the classes that contain offsets into other debug sections
22541 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22542 that, if an attribute's can be either a constant or one of the
22543 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22544 taken as section offsets, not constants.
22545
22546 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22547 cannot handle that. */
380bca97 22548
3690dd37 22549static int
6e5a29e1 22550attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22551{
22552 switch (attr->form)
22553 {
22554 case DW_FORM_sdata:
22555 case DW_FORM_udata:
22556 case DW_FORM_data1:
22557 case DW_FORM_data2:
22558 case DW_FORM_data4:
22559 case DW_FORM_data8:
663c44ac 22560 case DW_FORM_implicit_const:
3690dd37
JB
22561 return 1;
22562 default:
22563 return 0;
22564 }
22565}
22566
7771576e
SA
22567
22568/* DW_ADDR is always stored already as sect_offset; despite for the forms
22569 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22570
22571static int
6e5a29e1 22572attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22573{
22574 switch (attr->form)
22575 {
22576 case DW_FORM_ref_addr:
22577 case DW_FORM_ref1:
22578 case DW_FORM_ref2:
22579 case DW_FORM_ref4:
22580 case DW_FORM_ref8:
22581 case DW_FORM_ref_udata:
22582 case DW_FORM_GNU_ref_alt:
22583 return 1;
22584 default:
22585 return 0;
22586 }
22587}
22588
3019eac3
DE
22589/* Return the .debug_loc section to use for CU.
22590 For DWO files use .debug_loc.dwo. */
22591
22592static struct dwarf2_section_info *
22593cu_debug_loc_section (struct dwarf2_cu *cu)
22594{
22595 if (cu->dwo_unit)
43988095
JK
22596 {
22597 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22598
22599 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22600 }
22601 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22602 : &dwarf2_per_objfile->loc);
3019eac3
DE
22603}
22604
8cf6f0b1
TT
22605/* A helper function that fills in a dwarf2_loclist_baton. */
22606
22607static void
22608fill_in_loclist_baton (struct dwarf2_cu *cu,
22609 struct dwarf2_loclist_baton *baton,
ff39bb5e 22610 const struct attribute *attr)
8cf6f0b1 22611{
3019eac3
DE
22612 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22613
22614 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22615
22616 baton->per_cu = cu->per_cu;
22617 gdb_assert (baton->per_cu);
22618 /* We don't know how long the location list is, but make sure we
22619 don't run off the edge of the section. */
3019eac3
DE
22620 baton->size = section->size - DW_UNSND (attr);
22621 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22622 baton->base_address = cu->base_address;
f664829e 22623 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22624}
22625
4c2df51b 22626static void
ff39bb5e 22627dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22628 struct dwarf2_cu *cu, int is_block)
4c2df51b 22629{
bb5ed363 22630 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22631 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22632
3690dd37 22633 if (attr_form_is_section_offset (attr)
3019eac3 22634 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22635 the section. If so, fall through to the complaint in the
22636 other branch. */
3019eac3 22637 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22638 {
0d53c4c4 22639 struct dwarf2_loclist_baton *baton;
4c2df51b 22640
8d749320 22641 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22642
8cf6f0b1 22643 fill_in_loclist_baton (cu, baton, attr);
be391dca 22644
d00adf39 22645 if (cu->base_known == 0)
0d53c4c4 22646 complaint (&symfile_complaints,
3e43a32a
MS
22647 _("Location list used without "
22648 "specifying the CU base address."));
4c2df51b 22649
f1e6e072
TT
22650 SYMBOL_ACLASS_INDEX (sym) = (is_block
22651 ? dwarf2_loclist_block_index
22652 : dwarf2_loclist_index);
0d53c4c4
DJ
22653 SYMBOL_LOCATION_BATON (sym) = baton;
22654 }
22655 else
22656 {
22657 struct dwarf2_locexpr_baton *baton;
22658
8d749320 22659 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22660 baton->per_cu = cu->per_cu;
22661 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22662
22663 if (attr_form_is_block (attr))
22664 {
22665 /* Note that we're just copying the block's data pointer
22666 here, not the actual data. We're still pointing into the
6502dd73
DJ
22667 info_buffer for SYM's objfile; right now we never release
22668 that buffer, but when we do clean up properly this may
22669 need to change. */
0d53c4c4
DJ
22670 baton->size = DW_BLOCK (attr)->size;
22671 baton->data = DW_BLOCK (attr)->data;
22672 }
22673 else
22674 {
22675 dwarf2_invalid_attrib_class_complaint ("location description",
22676 SYMBOL_NATURAL_NAME (sym));
22677 baton->size = 0;
0d53c4c4 22678 }
6e70227d 22679
f1e6e072
TT
22680 SYMBOL_ACLASS_INDEX (sym) = (is_block
22681 ? dwarf2_locexpr_block_index
22682 : dwarf2_locexpr_index);
0d53c4c4
DJ
22683 SYMBOL_LOCATION_BATON (sym) = baton;
22684 }
4c2df51b 22685}
6502dd73 22686
9aa1f1e3
TT
22687/* Return the OBJFILE associated with the compilation unit CU. If CU
22688 came from a separate debuginfo file, then the master objfile is
22689 returned. */
ae0d2f24
UW
22690
22691struct objfile *
22692dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22693{
9291a0cd 22694 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22695
22696 /* Return the master objfile, so that we can report and look up the
22697 correct file containing this variable. */
22698 if (objfile->separate_debug_objfile_backlink)
22699 objfile = objfile->separate_debug_objfile_backlink;
22700
22701 return objfile;
22702}
22703
96408a79
SA
22704/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22705 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22706 CU_HEADERP first. */
22707
22708static const struct comp_unit_head *
22709per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22710 struct dwarf2_per_cu_data *per_cu)
22711{
d521ce57 22712 const gdb_byte *info_ptr;
96408a79
SA
22713
22714 if (per_cu->cu)
22715 return &per_cu->cu->header;
22716
9c541725 22717 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22718
22719 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22720 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22721 rcuh_kind::COMPILE);
96408a79
SA
22722
22723 return cu_headerp;
22724}
22725
ae0d2f24
UW
22726/* Return the address size given in the compilation unit header for CU. */
22727
98714339 22728int
ae0d2f24
UW
22729dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22730{
96408a79
SA
22731 struct comp_unit_head cu_header_local;
22732 const struct comp_unit_head *cu_headerp;
c471e790 22733
96408a79
SA
22734 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22735
22736 return cu_headerp->addr_size;
ae0d2f24
UW
22737}
22738
9eae7c52
TT
22739/* Return the offset size given in the compilation unit header for CU. */
22740
22741int
22742dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22743{
96408a79
SA
22744 struct comp_unit_head cu_header_local;
22745 const struct comp_unit_head *cu_headerp;
9c6c53f7 22746
96408a79
SA
22747 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22748
22749 return cu_headerp->offset_size;
22750}
22751
22752/* See its dwarf2loc.h declaration. */
22753
22754int
22755dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22756{
22757 struct comp_unit_head cu_header_local;
22758 const struct comp_unit_head *cu_headerp;
22759
22760 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22761
22762 if (cu_headerp->version == 2)
22763 return cu_headerp->addr_size;
22764 else
22765 return cu_headerp->offset_size;
181cebd4
JK
22766}
22767
9aa1f1e3
TT
22768/* Return the text offset of the CU. The returned offset comes from
22769 this CU's objfile. If this objfile came from a separate debuginfo
22770 file, then the offset may be different from the corresponding
22771 offset in the parent objfile. */
22772
22773CORE_ADDR
22774dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22775{
bb3fa9d0 22776 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22777
22778 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22779}
22780
43988095
JK
22781/* Return DWARF version number of PER_CU. */
22782
22783short
22784dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22785{
22786 return per_cu->dwarf_version;
22787}
22788
348e048f
DE
22789/* Locate the .debug_info compilation unit from CU's objfile which contains
22790 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22791
22792static struct dwarf2_per_cu_data *
9c541725 22793dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22794 unsigned int offset_in_dwz,
ae038cb0
DJ
22795 struct objfile *objfile)
22796{
22797 struct dwarf2_per_cu_data *this_cu;
22798 int low, high;
36586728 22799 const sect_offset *cu_off;
ae038cb0 22800
ae038cb0
DJ
22801 low = 0;
22802 high = dwarf2_per_objfile->n_comp_units - 1;
22803 while (high > low)
22804 {
36586728 22805 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22806 int mid = low + (high - low) / 2;
9a619af0 22807
36586728 22808 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22809 cu_off = &mid_cu->sect_off;
36586728 22810 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22811 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22812 high = mid;
22813 else
22814 low = mid + 1;
22815 }
22816 gdb_assert (low == high);
36586728 22817 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22818 cu_off = &this_cu->sect_off;
22819 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22820 {
36586728 22821 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22822 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22823 "offset 0x%x [in module %s]"),
22824 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22825
9c541725
PA
22826 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22827 <= sect_off);
ae038cb0
DJ
22828 return dwarf2_per_objfile->all_comp_units[low-1];
22829 }
22830 else
22831 {
22832 this_cu = dwarf2_per_objfile->all_comp_units[low];
22833 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22834 && sect_off >= this_cu->sect_off + this_cu->length)
22835 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22836 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22837 return this_cu;
22838 }
22839}
22840
23745b47 22841/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22842
9816fde3 22843static void
23745b47 22844init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22845{
9816fde3 22846 memset (cu, 0, sizeof (*cu));
23745b47
DE
22847 per_cu->cu = cu;
22848 cu->per_cu = per_cu;
22849 cu->objfile = per_cu->objfile;
93311388 22850 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22851}
22852
22853/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22854
22855static void
95554aad
TT
22856prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22857 enum language pretend_language)
9816fde3
JK
22858{
22859 struct attribute *attr;
22860
22861 /* Set the language we're debugging. */
22862 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22863 if (attr)
22864 set_cu_language (DW_UNSND (attr), cu);
22865 else
9cded63f 22866 {
95554aad 22867 cu->language = pretend_language;
9cded63f
TT
22868 cu->language_defn = language_def (cu->language);
22869 }
dee91e82 22870
7d45c7c3 22871 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22872}
22873
ae038cb0
DJ
22874/* Release one cached compilation unit, CU. We unlink it from the tree
22875 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22876 the caller is responsible for that.
22877 NOTE: DATA is a void * because this function is also used as a
22878 cleanup routine. */
ae038cb0
DJ
22879
22880static void
68dc6402 22881free_heap_comp_unit (void *data)
ae038cb0 22882{
9a3c8263 22883 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22884
23745b47
DE
22885 gdb_assert (cu->per_cu != NULL);
22886 cu->per_cu->cu = NULL;
ae038cb0
DJ
22887 cu->per_cu = NULL;
22888
22889 obstack_free (&cu->comp_unit_obstack, NULL);
22890
22891 xfree (cu);
22892}
22893
72bf9492 22894/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22895 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22896 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22897
22898static void
22899free_stack_comp_unit (void *data)
22900{
9a3c8263 22901 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22902
23745b47
DE
22903 gdb_assert (cu->per_cu != NULL);
22904 cu->per_cu->cu = NULL;
22905 cu->per_cu = NULL;
22906
72bf9492
DJ
22907 obstack_free (&cu->comp_unit_obstack, NULL);
22908 cu->partial_dies = NULL;
ae038cb0
DJ
22909}
22910
22911/* Free all cached compilation units. */
22912
22913static void
22914free_cached_comp_units (void *data)
22915{
330cdd98 22916 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22917}
22918
22919/* Increase the age counter on each cached compilation unit, and free
22920 any that are too old. */
22921
22922static void
22923age_cached_comp_units (void)
22924{
22925 struct dwarf2_per_cu_data *per_cu, **last_chain;
22926
22927 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22928 per_cu = dwarf2_per_objfile->read_in_chain;
22929 while (per_cu != NULL)
22930 {
22931 per_cu->cu->last_used ++;
b4f54984 22932 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22933 dwarf2_mark (per_cu->cu);
22934 per_cu = per_cu->cu->read_in_chain;
22935 }
22936
22937 per_cu = dwarf2_per_objfile->read_in_chain;
22938 last_chain = &dwarf2_per_objfile->read_in_chain;
22939 while (per_cu != NULL)
22940 {
22941 struct dwarf2_per_cu_data *next_cu;
22942
22943 next_cu = per_cu->cu->read_in_chain;
22944
22945 if (!per_cu->cu->mark)
22946 {
68dc6402 22947 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22948 *last_chain = next_cu;
22949 }
22950 else
22951 last_chain = &per_cu->cu->read_in_chain;
22952
22953 per_cu = next_cu;
22954 }
22955}
22956
22957/* Remove a single compilation unit from the cache. */
22958
22959static void
dee91e82 22960free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22961{
22962 struct dwarf2_per_cu_data *per_cu, **last_chain;
22963
22964 per_cu = dwarf2_per_objfile->read_in_chain;
22965 last_chain = &dwarf2_per_objfile->read_in_chain;
22966 while (per_cu != NULL)
22967 {
22968 struct dwarf2_per_cu_data *next_cu;
22969
22970 next_cu = per_cu->cu->read_in_chain;
22971
dee91e82 22972 if (per_cu == target_per_cu)
ae038cb0 22973 {
68dc6402 22974 free_heap_comp_unit (per_cu->cu);
dee91e82 22975 per_cu->cu = NULL;
ae038cb0
DJ
22976 *last_chain = next_cu;
22977 break;
22978 }
22979 else
22980 last_chain = &per_cu->cu->read_in_chain;
22981
22982 per_cu = next_cu;
22983 }
22984}
22985
fe3e1990
DJ
22986/* Release all extra memory associated with OBJFILE. */
22987
22988void
22989dwarf2_free_objfile (struct objfile *objfile)
22990{
9a3c8263
SM
22991 dwarf2_per_objfile
22992 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22993 dwarf2_objfile_data_key);
fe3e1990
DJ
22994
22995 if (dwarf2_per_objfile == NULL)
22996 return;
22997
330cdd98 22998 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
22999}
23000
dee91e82
DE
23001/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23002 We store these in a hash table separate from the DIEs, and preserve them
23003 when the DIEs are flushed out of cache.
23004
23005 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23006 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23007 or the type may come from a DWO file. Furthermore, while it's more logical
23008 to use per_cu->section+offset, with Fission the section with the data is in
23009 the DWO file but we don't know that section at the point we need it.
23010 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23011 because we can enter the lookup routine, get_die_type_at_offset, from
23012 outside this file, and thus won't necessarily have PER_CU->cu.
23013 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23014
dee91e82 23015struct dwarf2_per_cu_offset_and_type
1c379e20 23016{
dee91e82 23017 const struct dwarf2_per_cu_data *per_cu;
9c541725 23018 sect_offset sect_off;
1c379e20
DJ
23019 struct type *type;
23020};
23021
dee91e82 23022/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23023
23024static hashval_t
dee91e82 23025per_cu_offset_and_type_hash (const void *item)
1c379e20 23026{
9a3c8263
SM
23027 const struct dwarf2_per_cu_offset_and_type *ofs
23028 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23029
9c541725 23030 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23031}
23032
dee91e82 23033/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23034
23035static int
dee91e82 23036per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23037{
9a3c8263
SM
23038 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23039 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23040 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23041 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23042
dee91e82 23043 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23044 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23045}
23046
23047/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23048 table if necessary. For convenience, return TYPE.
23049
23050 The DIEs reading must have careful ordering to:
23051 * Not cause infite loops trying to read in DIEs as a prerequisite for
23052 reading current DIE.
23053 * Not trying to dereference contents of still incompletely read in types
23054 while reading in other DIEs.
23055 * Enable referencing still incompletely read in types just by a pointer to
23056 the type without accessing its fields.
23057
23058 Therefore caller should follow these rules:
23059 * Try to fetch any prerequisite types we may need to build this DIE type
23060 before building the type and calling set_die_type.
e71ec853 23061 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23062 possible before fetching more types to complete the current type.
23063 * Make the type as complete as possible before fetching more types. */
1c379e20 23064
f792889a 23065static struct type *
1c379e20
DJ
23066set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23067{
dee91e82 23068 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23069 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23070 struct attribute *attr;
23071 struct dynamic_prop prop;
1c379e20 23072
b4ba55a1
JB
23073 /* For Ada types, make sure that the gnat-specific data is always
23074 initialized (if not already set). There are a few types where
23075 we should not be doing so, because the type-specific area is
23076 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23077 where the type-specific area is used to store the floatformat).
23078 But this is not a problem, because the gnat-specific information
23079 is actually not needed for these types. */
23080 if (need_gnat_info (cu)
23081 && TYPE_CODE (type) != TYPE_CODE_FUNC
23082 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23083 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23084 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23085 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23086 && !HAVE_GNAT_AUX_INFO (type))
23087 INIT_GNAT_SPECIFIC (type);
23088
3f2f83dd
KB
23089 /* Read DW_AT_allocated and set in type. */
23090 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23091 if (attr_form_is_block (attr))
23092 {
23093 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23094 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23095 }
23096 else if (attr != NULL)
23097 {
23098 complaint (&symfile_complaints,
9c541725
PA
23099 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23100 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23101 to_underlying (die->sect_off));
3f2f83dd
KB
23102 }
23103
23104 /* Read DW_AT_associated and set in type. */
23105 attr = dwarf2_attr (die, DW_AT_associated, cu);
23106 if (attr_form_is_block (attr))
23107 {
23108 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23109 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23110 }
23111 else if (attr != NULL)
23112 {
23113 complaint (&symfile_complaints,
9c541725
PA
23114 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23115 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23116 to_underlying (die->sect_off));
3f2f83dd
KB
23117 }
23118
3cdcd0ce
JB
23119 /* Read DW_AT_data_location and set in type. */
23120 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23121 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23122 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23123
dee91e82 23124 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23125 {
dee91e82
DE
23126 dwarf2_per_objfile->die_type_hash =
23127 htab_create_alloc_ex (127,
23128 per_cu_offset_and_type_hash,
23129 per_cu_offset_and_type_eq,
23130 NULL,
23131 &objfile->objfile_obstack,
23132 hashtab_obstack_allocate,
23133 dummy_obstack_deallocate);
f792889a 23134 }
1c379e20 23135
dee91e82 23136 ofs.per_cu = cu->per_cu;
9c541725 23137 ofs.sect_off = die->sect_off;
1c379e20 23138 ofs.type = type;
dee91e82
DE
23139 slot = (struct dwarf2_per_cu_offset_and_type **)
23140 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23141 if (*slot)
23142 complaint (&symfile_complaints,
23143 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23144 to_underlying (die->sect_off));
8d749320
SM
23145 *slot = XOBNEW (&objfile->objfile_obstack,
23146 struct dwarf2_per_cu_offset_and_type);
1c379e20 23147 **slot = ofs;
f792889a 23148 return type;
1c379e20
DJ
23149}
23150
9c541725 23151/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23152 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23153
23154static struct type *
9c541725 23155get_die_type_at_offset (sect_offset sect_off,
673bfd45 23156 struct dwarf2_per_cu_data *per_cu)
1c379e20 23157{
dee91e82 23158 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23159
dee91e82 23160 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23161 return NULL;
1c379e20 23162
dee91e82 23163 ofs.per_cu = per_cu;
9c541725 23164 ofs.sect_off = sect_off;
9a3c8263
SM
23165 slot = ((struct dwarf2_per_cu_offset_and_type *)
23166 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23167 if (slot)
23168 return slot->type;
23169 else
23170 return NULL;
23171}
23172
02142a6c 23173/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23174 or return NULL if DIE does not have a saved type. */
23175
23176static struct type *
23177get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23178{
9c541725 23179 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23180}
23181
10b3939b
DJ
23182/* Add a dependence relationship from CU to REF_PER_CU. */
23183
23184static void
23185dwarf2_add_dependence (struct dwarf2_cu *cu,
23186 struct dwarf2_per_cu_data *ref_per_cu)
23187{
23188 void **slot;
23189
23190 if (cu->dependencies == NULL)
23191 cu->dependencies
23192 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23193 NULL, &cu->comp_unit_obstack,
23194 hashtab_obstack_allocate,
23195 dummy_obstack_deallocate);
23196
23197 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23198 if (*slot == NULL)
23199 *slot = ref_per_cu;
23200}
1c379e20 23201
f504f079
DE
23202/* Subroutine of dwarf2_mark to pass to htab_traverse.
23203 Set the mark field in every compilation unit in the
ae038cb0
DJ
23204 cache that we must keep because we are keeping CU. */
23205
10b3939b
DJ
23206static int
23207dwarf2_mark_helper (void **slot, void *data)
23208{
23209 struct dwarf2_per_cu_data *per_cu;
23210
23211 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23212
23213 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23214 reading of the chain. As such dependencies remain valid it is not much
23215 useful to track and undo them during QUIT cleanups. */
23216 if (per_cu->cu == NULL)
23217 return 1;
23218
10b3939b
DJ
23219 if (per_cu->cu->mark)
23220 return 1;
23221 per_cu->cu->mark = 1;
23222
23223 if (per_cu->cu->dependencies != NULL)
23224 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23225
23226 return 1;
23227}
23228
f504f079
DE
23229/* Set the mark field in CU and in every other compilation unit in the
23230 cache that we must keep because we are keeping CU. */
23231
ae038cb0
DJ
23232static void
23233dwarf2_mark (struct dwarf2_cu *cu)
23234{
23235 if (cu->mark)
23236 return;
23237 cu->mark = 1;
10b3939b
DJ
23238 if (cu->dependencies != NULL)
23239 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23240}
23241
23242static void
23243dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23244{
23245 while (per_cu)
23246 {
23247 per_cu->cu->mark = 0;
23248 per_cu = per_cu->cu->read_in_chain;
23249 }
72bf9492
DJ
23250}
23251
72bf9492
DJ
23252/* Trivial hash function for partial_die_info: the hash value of a DIE
23253 is its offset in .debug_info for this objfile. */
23254
23255static hashval_t
23256partial_die_hash (const void *item)
23257{
9a3c8263
SM
23258 const struct partial_die_info *part_die
23259 = (const struct partial_die_info *) item;
9a619af0 23260
9c541725 23261 return to_underlying (part_die->sect_off);
72bf9492
DJ
23262}
23263
23264/* Trivial comparison function for partial_die_info structures: two DIEs
23265 are equal if they have the same offset. */
23266
23267static int
23268partial_die_eq (const void *item_lhs, const void *item_rhs)
23269{
9a3c8263
SM
23270 const struct partial_die_info *part_die_lhs
23271 = (const struct partial_die_info *) item_lhs;
23272 const struct partial_die_info *part_die_rhs
23273 = (const struct partial_die_info *) item_rhs;
9a619af0 23274
9c541725 23275 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23276}
23277
b4f54984
DE
23278static struct cmd_list_element *set_dwarf_cmdlist;
23279static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23280
23281static void
b4f54984 23282set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23283{
b4f54984 23284 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23285 gdb_stdout);
ae038cb0
DJ
23286}
23287
23288static void
b4f54984 23289show_dwarf_cmd (char *args, int from_tty)
6e70227d 23290{
b4f54984 23291 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23292}
23293
4bf44c1c 23294/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23295
23296static void
c1bd65d0 23297dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23298{
9a3c8263 23299 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23300 int ix;
8b70b953 23301
626f2d1c
TT
23302 /* Make sure we don't accidentally use dwarf2_per_objfile while
23303 cleaning up. */
23304 dwarf2_per_objfile = NULL;
23305
59b0c7c1
JB
23306 for (ix = 0; ix < data->n_comp_units; ++ix)
23307 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23308
59b0c7c1 23309 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23310 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23311 data->all_type_units[ix]->per_cu.imported_symtabs);
23312 xfree (data->all_type_units);
95554aad 23313
8b70b953 23314 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23315
23316 if (data->dwo_files)
23317 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23318 if (data->dwp_file)
23319 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23320
23321 if (data->dwz_file && data->dwz_file->dwz_bfd)
23322 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23323}
23324
23325\f
ae2de4f8 23326/* The "save gdb-index" command. */
9291a0cd 23327
bc8f2430
JK
23328/* In-memory buffer to prepare data to be written later to a file. */
23329class data_buf
9291a0cd 23330{
bc8f2430 23331public:
bc8f2430
JK
23332 /* Copy DATA to the end of the buffer. */
23333 template<typename T>
23334 void append_data (const T &data)
23335 {
23336 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23337 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23338 grow (sizeof (data)));
bc8f2430 23339 }
b89be57b 23340
c2f134ac
PA
23341 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23342 terminating zero is appended too. */
bc8f2430
JK
23343 void append_cstr0 (const char *cstr)
23344 {
23345 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23346 std::copy (cstr, cstr + size, grow (size));
23347 }
23348
23349 /* Accept a host-format integer in VAL and append it to the buffer
23350 as a target-format integer which is LEN bytes long. */
23351 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23352 {
23353 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23354 }
9291a0cd 23355
bc8f2430
JK
23356 /* Return the size of the buffer. */
23357 size_t size () const
23358 {
23359 return m_vec.size ();
23360 }
23361
23362 /* Write the buffer to FILE. */
23363 void file_write (FILE *file) const
23364 {
a81e6d4d
PA
23365 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23366 error (_("couldn't write data to file"));
bc8f2430
JK
23367 }
23368
23369private:
c2f134ac
PA
23370 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23371 the start of the new block. */
23372 gdb_byte *grow (size_t size)
23373 {
23374 m_vec.resize (m_vec.size () + size);
23375 return &*m_vec.end () - size;
23376 }
23377
d5722aa2 23378 gdb::byte_vector m_vec;
bc8f2430 23379};
9291a0cd
TT
23380
23381/* An entry in the symbol table. */
23382struct symtab_index_entry
23383{
23384 /* The name of the symbol. */
23385 const char *name;
23386 /* The offset of the name in the constant pool. */
23387 offset_type index_offset;
23388 /* A sorted vector of the indices of all the CUs that hold an object
23389 of this name. */
bc8f2430 23390 std::vector<offset_type> cu_indices;
9291a0cd
TT
23391};
23392
23393/* The symbol table. This is a power-of-2-sized hash table. */
23394struct mapped_symtab
23395{
bc8f2430
JK
23396 mapped_symtab ()
23397 {
23398 data.resize (1024);
23399 }
b89be57b 23400
bc8f2430 23401 offset_type n_elements = 0;
4b76cda9 23402 std::vector<symtab_index_entry> data;
bc8f2430 23403};
9291a0cd 23404
bc8f2430 23405/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23406 the slot.
23407
23408 Function is used only during write_hash_table so no index format backward
23409 compatibility is needed. */
b89be57b 23410
4b76cda9 23411static symtab_index_entry &
9291a0cd
TT
23412find_slot (struct mapped_symtab *symtab, const char *name)
23413{
559a7a62 23414 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23415
bc8f2430
JK
23416 index = hash & (symtab->data.size () - 1);
23417 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23418
23419 for (;;)
23420 {
4b76cda9
PA
23421 if (symtab->data[index].name == NULL
23422 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23423 return symtab->data[index];
23424 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23425 }
23426}
23427
23428/* Expand SYMTAB's hash table. */
b89be57b 23429
9291a0cd
TT
23430static void
23431hash_expand (struct mapped_symtab *symtab)
23432{
bc8f2430 23433 auto old_entries = std::move (symtab->data);
9291a0cd 23434
bc8f2430
JK
23435 symtab->data.clear ();
23436 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23437
bc8f2430 23438 for (auto &it : old_entries)
4b76cda9 23439 if (it.name != NULL)
bc8f2430 23440 {
4b76cda9 23441 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23442 ref = std::move (it);
23443 }
9291a0cd
TT
23444}
23445
156942c7
DE
23446/* Add an entry to SYMTAB. NAME is the name of the symbol.
23447 CU_INDEX is the index of the CU in which the symbol appears.
23448 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23449
9291a0cd
TT
23450static void
23451add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23452 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23453 offset_type cu_index)
23454{
156942c7 23455 offset_type cu_index_and_attrs;
9291a0cd
TT
23456
23457 ++symtab->n_elements;
bc8f2430 23458 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23459 hash_expand (symtab);
23460
4b76cda9
PA
23461 symtab_index_entry &slot = find_slot (symtab, name);
23462 if (slot.name == NULL)
9291a0cd 23463 {
4b76cda9 23464 slot.name = name;
156942c7 23465 /* index_offset is set later. */
9291a0cd 23466 }
156942c7
DE
23467
23468 cu_index_and_attrs = 0;
23469 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23470 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23471 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23472
23473 /* We don't want to record an index value twice as we want to avoid the
23474 duplication.
23475 We process all global symbols and then all static symbols
23476 (which would allow us to avoid the duplication by only having to check
23477 the last entry pushed), but a symbol could have multiple kinds in one CU.
23478 To keep things simple we don't worry about the duplication here and
23479 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23480 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23481}
23482
23483/* Sort and remove duplicates of all symbols' cu_indices lists. */
23484
23485static void
23486uniquify_cu_indices (struct mapped_symtab *symtab)
23487{
4b76cda9 23488 for (auto &entry : symtab->data)
156942c7 23489 {
4b76cda9 23490 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23491 {
4b76cda9 23492 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23493 std::sort (cu_indices.begin (), cu_indices.end ());
23494 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23495 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23496 }
23497 }
9291a0cd
TT
23498}
23499
bc8f2430
JK
23500/* A form of 'const char *' suitable for container keys. Only the
23501 pointer is stored. The strings themselves are compared, not the
23502 pointers. */
23503class c_str_view
9291a0cd 23504{
bc8f2430
JK
23505public:
23506 c_str_view (const char *cstr)
23507 : m_cstr (cstr)
23508 {}
9291a0cd 23509
bc8f2430
JK
23510 bool operator== (const c_str_view &other) const
23511 {
23512 return strcmp (m_cstr, other.m_cstr) == 0;
23513 }
9291a0cd 23514
bc8f2430
JK
23515private:
23516 friend class c_str_view_hasher;
23517 const char *const m_cstr;
23518};
9291a0cd 23519
bc8f2430
JK
23520/* A std::unordered_map::hasher for c_str_view that uses the right
23521 hash function for strings in a mapped index. */
23522class c_str_view_hasher
23523{
23524public:
23525 size_t operator () (const c_str_view &x) const
23526 {
23527 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23528 }
23529};
b89be57b 23530
bc8f2430
JK
23531/* A std::unordered_map::hasher for std::vector<>. */
23532template<typename T>
23533class vector_hasher
9291a0cd 23534{
bc8f2430
JK
23535public:
23536 size_t operator () (const std::vector<T> &key) const
23537 {
23538 return iterative_hash (key.data (),
23539 sizeof (key.front ()) * key.size (), 0);
23540 }
23541};
9291a0cd 23542
bc8f2430
JK
23543/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23544 constant pool entries going into the data buffer CPOOL. */
3876f04e 23545
bc8f2430
JK
23546static void
23547write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23548{
23549 {
23550 /* Elements are sorted vectors of the indices of all the CUs that
23551 hold an object of this name. */
23552 std::unordered_map<std::vector<offset_type>, offset_type,
23553 vector_hasher<offset_type>>
23554 symbol_hash_table;
23555
23556 /* We add all the index vectors to the constant pool first, to
23557 ensure alignment is ok. */
4b76cda9 23558 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23559 {
4b76cda9 23560 if (entry.name == NULL)
bc8f2430 23561 continue;
4b76cda9 23562 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23563
23564 /* Finding before inserting is faster than always trying to
23565 insert, because inserting always allocates a node, does the
23566 lookup, and then destroys the new node if another node
23567 already had the same key. C++17 try_emplace will avoid
23568 this. */
23569 const auto found
4b76cda9 23570 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23571 if (found != symbol_hash_table.end ())
23572 {
4b76cda9 23573 entry.index_offset = found->second;
70a1152b
PA
23574 continue;
23575 }
23576
4b76cda9
PA
23577 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23578 entry.index_offset = cpool.size ();
23579 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23580 for (const auto index : entry.cu_indices)
23581 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23582 }
23583 }
9291a0cd
TT
23584
23585 /* Now write out the hash table. */
bc8f2430 23586 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23587 for (const auto &entry : symtab->data)
9291a0cd
TT
23588 {
23589 offset_type str_off, vec_off;
23590
4b76cda9 23591 if (entry.name != NULL)
9291a0cd 23592 {
4b76cda9 23593 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23594 if (insertpair.second)
4b76cda9 23595 cpool.append_cstr0 (entry.name);
bc8f2430 23596 str_off = insertpair.first->second;
4b76cda9 23597 vec_off = entry.index_offset;
9291a0cd
TT
23598 }
23599 else
23600 {
23601 /* While 0 is a valid constant pool index, it is not valid
23602 to have 0 for both offsets. */
23603 str_off = 0;
23604 vec_off = 0;
23605 }
23606
bc8f2430
JK
23607 output.append_data (MAYBE_SWAP (str_off));
23608 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23609 }
9291a0cd
TT
23610}
23611
bc8f2430 23612typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23613
23614/* Helper struct for building the address table. */
23615struct addrmap_index_data
23616{
bc8f2430
JK
23617 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23618 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23619 {}
23620
0a5429f6 23621 struct objfile *objfile;
bc8f2430
JK
23622 data_buf &addr_vec;
23623 psym_index_map &cu_index_htab;
0a5429f6
DE
23624
23625 /* Non-zero if the previous_* fields are valid.
23626 We can't write an entry until we see the next entry (since it is only then
23627 that we know the end of the entry). */
23628 int previous_valid;
23629 /* Index of the CU in the table of all CUs in the index file. */
23630 unsigned int previous_cu_index;
0963b4bd 23631 /* Start address of the CU. */
0a5429f6
DE
23632 CORE_ADDR previous_cu_start;
23633};
23634
bc8f2430 23635/* Write an address entry to ADDR_VEC. */
b89be57b 23636
9291a0cd 23637static void
bc8f2430 23638add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23639 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23640{
9291a0cd
TT
23641 CORE_ADDR baseaddr;
23642
23643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23644
c2f134ac
PA
23645 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23646 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23647 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23648}
23649
23650/* Worker function for traversing an addrmap to build the address table. */
23651
23652static int
23653add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23654{
9a3c8263
SM
23655 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23656 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23657
23658 if (data->previous_valid)
bc8f2430 23659 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23660 data->previous_cu_start, start_addr,
23661 data->previous_cu_index);
23662
23663 data->previous_cu_start = start_addr;
23664 if (pst != NULL)
23665 {
bc8f2430
JK
23666 const auto it = data->cu_index_htab.find (pst);
23667 gdb_assert (it != data->cu_index_htab.cend ());
23668 data->previous_cu_index = it->second;
0a5429f6
DE
23669 data->previous_valid = 1;
23670 }
23671 else
bc8f2430 23672 data->previous_valid = 0;
0a5429f6
DE
23673
23674 return 0;
23675}
23676
bc8f2430 23677/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23678 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23679 in the index file. */
23680
23681static void
bc8f2430
JK
23682write_address_map (struct objfile *objfile, data_buf &addr_vec,
23683 psym_index_map &cu_index_htab)
0a5429f6 23684{
bc8f2430 23685 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23686
23687 /* When writing the address table, we have to cope with the fact that
23688 the addrmap iterator only provides the start of a region; we have to
23689 wait until the next invocation to get the start of the next region. */
23690
23691 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23692 addrmap_index_data.previous_valid = 0;
23693
23694 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23695 &addrmap_index_data);
23696
23697 /* It's highly unlikely the last entry (end address = 0xff...ff)
23698 is valid, but we should still handle it.
23699 The end address is recorded as the start of the next region, but that
23700 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23701 anyway. */
23702 if (addrmap_index_data.previous_valid)
bc8f2430 23703 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23704 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23705 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23706}
23707
156942c7
DE
23708/* Return the symbol kind of PSYM. */
23709
23710static gdb_index_symbol_kind
23711symbol_kind (struct partial_symbol *psym)
23712{
23713 domain_enum domain = PSYMBOL_DOMAIN (psym);
23714 enum address_class aclass = PSYMBOL_CLASS (psym);
23715
23716 switch (domain)
23717 {
23718 case VAR_DOMAIN:
23719 switch (aclass)
23720 {
23721 case LOC_BLOCK:
23722 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23723 case LOC_TYPEDEF:
23724 return GDB_INDEX_SYMBOL_KIND_TYPE;
23725 case LOC_COMPUTED:
23726 case LOC_CONST_BYTES:
23727 case LOC_OPTIMIZED_OUT:
23728 case LOC_STATIC:
23729 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23730 case LOC_CONST:
23731 /* Note: It's currently impossible to recognize psyms as enum values
23732 short of reading the type info. For now punt. */
23733 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23734 default:
23735 /* There are other LOC_FOO values that one might want to classify
23736 as variables, but dwarf2read.c doesn't currently use them. */
23737 return GDB_INDEX_SYMBOL_KIND_OTHER;
23738 }
23739 case STRUCT_DOMAIN:
23740 return GDB_INDEX_SYMBOL_KIND_TYPE;
23741 default:
23742 return GDB_INDEX_SYMBOL_KIND_OTHER;
23743 }
23744}
23745
9291a0cd 23746/* Add a list of partial symbols to SYMTAB. */
b89be57b 23747
9291a0cd
TT
23748static void
23749write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23750 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23751 struct partial_symbol **psymp,
23752 int count,
987d643c
TT
23753 offset_type cu_index,
23754 int is_static)
9291a0cd
TT
23755{
23756 for (; count-- > 0; ++psymp)
23757 {
156942c7 23758 struct partial_symbol *psym = *psymp;
987d643c 23759
156942c7 23760 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23761 error (_("Ada is not currently supported by the index"));
987d643c 23762
987d643c 23763 /* Only add a given psymbol once. */
bc8f2430 23764 if (psyms_seen.insert (psym).second)
987d643c 23765 {
156942c7
DE
23766 gdb_index_symbol_kind kind = symbol_kind (psym);
23767
156942c7
DE
23768 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23769 is_static, kind, cu_index);
987d643c 23770 }
9291a0cd
TT
23771 }
23772}
23773
1fd400ff
TT
23774/* A helper struct used when iterating over debug_types. */
23775struct signatured_type_index_data
23776{
bc8f2430
JK
23777 signatured_type_index_data (data_buf &types_list_,
23778 std::unordered_set<partial_symbol *> &psyms_seen_)
23779 : types_list (types_list_), psyms_seen (psyms_seen_)
23780 {}
23781
1fd400ff
TT
23782 struct objfile *objfile;
23783 struct mapped_symtab *symtab;
bc8f2430
JK
23784 data_buf &types_list;
23785 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23786 int cu_index;
23787};
23788
23789/* A helper function that writes a single signatured_type to an
23790 obstack. */
b89be57b 23791
1fd400ff
TT
23792static int
23793write_one_signatured_type (void **slot, void *d)
23794{
9a3c8263
SM
23795 struct signatured_type_index_data *info
23796 = (struct signatured_type_index_data *) d;
1fd400ff 23797 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23798 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23799
23800 write_psymbols (info->symtab,
987d643c 23801 info->psyms_seen,
3e43a32a
MS
23802 info->objfile->global_psymbols.list
23803 + psymtab->globals_offset,
987d643c
TT
23804 psymtab->n_global_syms, info->cu_index,
23805 0);
1fd400ff 23806 write_psymbols (info->symtab,
987d643c 23807 info->psyms_seen,
3e43a32a
MS
23808 info->objfile->static_psymbols.list
23809 + psymtab->statics_offset,
987d643c
TT
23810 psymtab->n_static_syms, info->cu_index,
23811 1);
1fd400ff 23812
c2f134ac
PA
23813 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23814 to_underlying (entry->per_cu.sect_off));
23815 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23816 to_underlying (entry->type_offset_in_tu));
23817 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23818
23819 ++info->cu_index;
23820
23821 return 1;
23822}
23823
e8f8bcb3
PA
23824/* Recurse into all "included" dependencies and count their symbols as
23825 if they appeared in this psymtab. */
23826
23827static void
23828recursively_count_psymbols (struct partial_symtab *psymtab,
23829 size_t &psyms_seen)
23830{
23831 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23832 if (psymtab->dependencies[i]->user != NULL)
23833 recursively_count_psymbols (psymtab->dependencies[i],
23834 psyms_seen);
23835
23836 psyms_seen += psymtab->n_global_syms;
23837 psyms_seen += psymtab->n_static_syms;
23838}
23839
95554aad
TT
23840/* Recurse into all "included" dependencies and write their symbols as
23841 if they appeared in this psymtab. */
23842
23843static void
23844recursively_write_psymbols (struct objfile *objfile,
23845 struct partial_symtab *psymtab,
23846 struct mapped_symtab *symtab,
bc8f2430 23847 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23848 offset_type cu_index)
23849{
23850 int i;
23851
23852 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23853 if (psymtab->dependencies[i]->user != NULL)
23854 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23855 symtab, psyms_seen, cu_index);
23856
23857 write_psymbols (symtab,
23858 psyms_seen,
23859 objfile->global_psymbols.list + psymtab->globals_offset,
23860 psymtab->n_global_syms, cu_index,
23861 0);
23862 write_psymbols (symtab,
23863 psyms_seen,
23864 objfile->static_psymbols.list + psymtab->statics_offset,
23865 psymtab->n_static_syms, cu_index,
23866 1);
23867}
23868
9291a0cd 23869/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23870
9291a0cd
TT
23871static void
23872write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23873{
9291a0cd
TT
23874 if (dwarf2_per_objfile->using_index)
23875 error (_("Cannot use an index to create the index"));
23876
8b70b953
TT
23877 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23878 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23879
260b681b
DE
23880 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23881 return;
23882
bc8f2430 23883 struct stat st;
4262abfb
JK
23884 if (stat (objfile_name (objfile), &st) < 0)
23885 perror_with_name (objfile_name (objfile));
9291a0cd 23886
bc8f2430
JK
23887 std::string filename (std::string (dir) + SLASH_STRING
23888 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23889
d419f42d 23890 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23891 if (!out_file)
bc8f2430 23892 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23893
16b7a719
PA
23894 /* Order matters here; we want FILE to be closed before FILENAME is
23895 unlinked, because on MS-Windows one cannot delete a file that is
23896 still open. (Don't call anything here that might throw until
23897 file_closer is created.) */
bc8f2430 23898 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23899 gdb_file_up close_out_file (out_file);
9291a0cd 23900
bc8f2430
JK
23901 mapped_symtab symtab;
23902 data_buf cu_list;
987d643c 23903
0a5429f6
DE
23904 /* While we're scanning CU's create a table that maps a psymtab pointer
23905 (which is what addrmap records) to its index (which is what is recorded
23906 in the index file). This will later be needed to write the address
23907 table. */
bc8f2430
JK
23908 psym_index_map cu_index_htab;
23909 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23910
23911 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23912 work here. Also, the debug_types entries do not appear in
23913 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23914
23915 /* The psyms_seen set is potentially going to be largish (~40k
23916 elements when indexing a -g3 build of GDB itself). Estimate the
23917 number of elements in order to avoid too many rehashes, which
23918 require rebuilding buckets and thus many trips to
23919 malloc/free. */
23920 size_t psyms_count = 0;
23921 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23922 {
23923 struct dwarf2_per_cu_data *per_cu
23924 = dwarf2_per_objfile->all_comp_units[i];
23925 struct partial_symtab *psymtab = per_cu->v.psymtab;
23926
23927 if (psymtab != NULL && psymtab->user == NULL)
23928 recursively_count_psymbols (psymtab, psyms_count);
23929 }
23930 /* Generating an index for gdb itself shows a ratio of
23931 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23932 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23933 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23934 {
3e43a32a
MS
23935 struct dwarf2_per_cu_data *per_cu
23936 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23937 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23938
92fac807
JK
23939 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23940 It may be referenced from a local scope but in such case it does not
23941 need to be present in .gdb_index. */
23942 if (psymtab == NULL)
23943 continue;
23944
95554aad 23945 if (psymtab->user == NULL)
bc8f2430
JK
23946 recursively_write_psymbols (objfile, psymtab, &symtab,
23947 psyms_seen, i);
9291a0cd 23948
bc8f2430
JK
23949 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23950 gdb_assert (insertpair.second);
9291a0cd 23951
c2f134ac
PA
23952 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23953 to_underlying (per_cu->sect_off));
23954 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23955 }
23956
0a5429f6 23957 /* Dump the address map. */
bc8f2430
JK
23958 data_buf addr_vec;
23959 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23960
1fd400ff 23961 /* Write out the .debug_type entries, if any. */
bc8f2430 23962 data_buf types_cu_list;
1fd400ff
TT
23963 if (dwarf2_per_objfile->signatured_types)
23964 {
bc8f2430
JK
23965 signatured_type_index_data sig_data (types_cu_list,
23966 psyms_seen);
1fd400ff
TT
23967
23968 sig_data.objfile = objfile;
bc8f2430 23969 sig_data.symtab = &symtab;
1fd400ff
TT
23970 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23971 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23972 write_one_signatured_type, &sig_data);
23973 }
23974
156942c7
DE
23975 /* Now that we've processed all symbols we can shrink their cu_indices
23976 lists. */
bc8f2430 23977 uniquify_cu_indices (&symtab);
156942c7 23978
bc8f2430
JK
23979 data_buf symtab_vec, constant_pool;
23980 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23981
bc8f2430
JK
23982 data_buf contents;
23983 const offset_type size_of_contents = 6 * sizeof (offset_type);
23984 offset_type total_len = size_of_contents;
9291a0cd
TT
23985
23986 /* The version number. */
bc8f2430 23987 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
23988
23989 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
23990 contents.append_data (MAYBE_SWAP (total_len));
23991 total_len += cu_list.size ();
9291a0cd 23992
1fd400ff 23993 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
23994 contents.append_data (MAYBE_SWAP (total_len));
23995 total_len += types_cu_list.size ();
1fd400ff 23996
9291a0cd 23997 /* The offset of the address table from the start of the file. */
bc8f2430
JK
23998 contents.append_data (MAYBE_SWAP (total_len));
23999 total_len += addr_vec.size ();
9291a0cd
TT
24000
24001 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24002 contents.append_data (MAYBE_SWAP (total_len));
24003 total_len += symtab_vec.size ();
9291a0cd
TT
24004
24005 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24006 contents.append_data (MAYBE_SWAP (total_len));
24007 total_len += constant_pool.size ();
9291a0cd 24008
bc8f2430 24009 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24010
bc8f2430
JK
24011 contents.file_write (out_file);
24012 cu_list.file_write (out_file);
24013 types_cu_list.file_write (out_file);
24014 addr_vec.file_write (out_file);
24015 symtab_vec.file_write (out_file);
24016 constant_pool.file_write (out_file);
9291a0cd 24017
bef155c3
TT
24018 /* We want to keep the file. */
24019 unlink_file.keep ();
9291a0cd
TT
24020}
24021
90476074
TT
24022/* Implementation of the `save gdb-index' command.
24023
24024 Note that the file format used by this command is documented in the
24025 GDB manual. Any changes here must be documented there. */
11570e71 24026
9291a0cd
TT
24027static void
24028save_gdb_index_command (char *arg, int from_tty)
24029{
24030 struct objfile *objfile;
24031
24032 if (!arg || !*arg)
96d19272 24033 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24034
24035 ALL_OBJFILES (objfile)
24036 {
24037 struct stat st;
24038
24039 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24040 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24041 continue;
24042
9a3c8263
SM
24043 dwarf2_per_objfile
24044 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24045 dwarf2_objfile_data_key);
9291a0cd
TT
24046 if (dwarf2_per_objfile)
24047 {
9291a0cd 24048
492d29ea 24049 TRY
9291a0cd
TT
24050 {
24051 write_psymtabs_to_index (objfile, arg);
24052 }
492d29ea
PA
24053 CATCH (except, RETURN_MASK_ERROR)
24054 {
24055 exception_fprintf (gdb_stderr, except,
24056 _("Error while writing index for `%s': "),
24057 objfile_name (objfile));
24058 }
24059 END_CATCH
9291a0cd
TT
24060 }
24061 }
dce234bc
PP
24062}
24063
9291a0cd
TT
24064\f
24065
b4f54984 24066int dwarf_always_disassemble;
9eae7c52
TT
24067
24068static void
b4f54984
DE
24069show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24070 struct cmd_list_element *c, const char *value)
9eae7c52 24071{
3e43a32a
MS
24072 fprintf_filtered (file,
24073 _("Whether to always disassemble "
24074 "DWARF expressions is %s.\n"),
9eae7c52
TT
24075 value);
24076}
24077
900e11f9
JK
24078static void
24079show_check_physname (struct ui_file *file, int from_tty,
24080 struct cmd_list_element *c, const char *value)
24081{
24082 fprintf_filtered (file,
24083 _("Whether to check \"physname\" is %s.\n"),
24084 value);
24085}
24086
6502dd73
DJ
24087void
24088_initialize_dwarf2_read (void)
24089{
96d19272
JK
24090 struct cmd_list_element *c;
24091
dce234bc 24092 dwarf2_objfile_data_key
c1bd65d0 24093 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24094
b4f54984
DE
24095 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24096Set DWARF specific variables.\n\
24097Configure DWARF variables such as the cache size"),
24098 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24099 0/*allow-unknown*/, &maintenance_set_cmdlist);
24100
b4f54984
DE
24101 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24102Show DWARF specific variables\n\
24103Show DWARF variables such as the cache size"),
24104 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24105 0/*allow-unknown*/, &maintenance_show_cmdlist);
24106
24107 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24108 &dwarf_max_cache_age, _("\
24109Set the upper bound on the age of cached DWARF compilation units."), _("\
24110Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24111A higher limit means that cached compilation units will be stored\n\
24112in memory longer, and more total memory will be used. Zero disables\n\
24113caching, which can slow down startup."),
2c5b56ce 24114 NULL,
b4f54984
DE
24115 show_dwarf_max_cache_age,
24116 &set_dwarf_cmdlist,
24117 &show_dwarf_cmdlist);
d97bc12b 24118
9eae7c52 24119 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24120 &dwarf_always_disassemble, _("\
9eae7c52
TT
24121Set whether `info address' always disassembles DWARF expressions."), _("\
24122Show whether `info address' always disassembles DWARF expressions."), _("\
24123When enabled, DWARF expressions are always printed in an assembly-like\n\
24124syntax. When disabled, expressions will be printed in a more\n\
24125conversational style, when possible."),
24126 NULL,
b4f54984
DE
24127 show_dwarf_always_disassemble,
24128 &set_dwarf_cmdlist,
24129 &show_dwarf_cmdlist);
24130
24131 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24132Set debugging of the DWARF reader."), _("\
24133Show debugging of the DWARF reader."), _("\
24134When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24135reading and symtab expansion. A value of 1 (one) provides basic\n\
24136information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24137 NULL,
24138 NULL,
24139 &setdebuglist, &showdebuglist);
24140
b4f54984
DE
24141 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24142Set debugging of the DWARF DIE reader."), _("\
24143Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24144When enabled (non-zero), DIEs are dumped after they are read in.\n\
24145The value is the maximum depth to print."),
ccce17b0
YQ
24146 NULL,
24147 NULL,
24148 &setdebuglist, &showdebuglist);
9291a0cd 24149
27e0867f
DE
24150 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24151Set debugging of the dwarf line reader."), _("\
24152Show debugging of the dwarf line reader."), _("\
24153When enabled (non-zero), line number entries are dumped as they are read in.\n\
24154A value of 1 (one) provides basic information.\n\
24155A value greater than 1 provides more verbose information."),
24156 NULL,
24157 NULL,
24158 &setdebuglist, &showdebuglist);
24159
900e11f9
JK
24160 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24161Set cross-checking of \"physname\" code against demangler."), _("\
24162Show cross-checking of \"physname\" code against demangler."), _("\
24163When enabled, GDB's internal \"physname\" code is checked against\n\
24164the demangler."),
24165 NULL, show_check_physname,
24166 &setdebuglist, &showdebuglist);
24167
e615022a
DE
24168 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24169 no_class, &use_deprecated_index_sections, _("\
24170Set whether to use deprecated gdb_index sections."), _("\
24171Show whether to use deprecated gdb_index sections."), _("\
24172When enabled, deprecated .gdb_index sections are used anyway.\n\
24173Normally they are ignored either because of a missing feature or\n\
24174performance issue.\n\
24175Warning: This option must be enabled before gdb reads the file."),
24176 NULL,
24177 NULL,
24178 &setlist, &showlist);
24179
96d19272 24180 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24181 _("\
fc1a9d6e 24182Save a gdb-index file.\n\
11570e71 24183Usage: save gdb-index DIRECTORY"),
96d19272
JK
24184 &save_cmdlist);
24185 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24186
24187 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24188 &dwarf2_locexpr_funcs);
24189 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24190 &dwarf2_loclist_funcs);
24191
24192 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24193 &dwarf2_block_frame_base_locexpr_funcs);
24194 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24195 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24196}
This page took 4.844406 seconds and 4 git commands to generate.