Fix gdbserver regression exposed by gdb.threads/multi-create-ns-info-thr.exp
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
bbf2f4df 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
c906108c 79#include <fcntl.h>
c906108c 80#include <sys/types.h>
325fac50 81#include <algorithm>
bc8f2430
JK
82#include <unordered_set>
83#include <unordered_map>
d8151005 84
34eaf542
TT
85typedef struct symbol *symbolp;
86DEF_VEC_P (symbolp);
87
73be47f5
DE
88/* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
b4f54984
DE
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91static unsigned int dwarf_read_debug = 0;
45cfd468 92
d97bc12b 93/* When non-zero, dump DIEs after they are read in. */
b4f54984 94static unsigned int dwarf_die_debug = 0;
d97bc12b 95
27e0867f
DE
96/* When non-zero, dump line number entries as they are read in. */
97static unsigned int dwarf_line_debug = 0;
98
900e11f9
JK
99/* When non-zero, cross-check physname against demangler. */
100static int check_physname = 0;
101
481860b3 102/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 103static int use_deprecated_index_sections = 0;
481860b3 104
6502dd73
DJ
105static const struct objfile_data *dwarf2_objfile_data_key;
106
f1e6e072
TT
107/* The "aclass" indices for various kinds of computed DWARF symbols. */
108
109static int dwarf2_locexpr_index;
110static int dwarf2_loclist_index;
111static int dwarf2_locexpr_block_index;
112static int dwarf2_loclist_block_index;
113
73869dc2
DE
114/* A descriptor for dwarf sections.
115
116 S.ASECTION, SIZE are typically initialized when the objfile is first
117 scanned. BUFFER, READIN are filled in later when the section is read.
118 If the section contained compressed data then SIZE is updated to record
119 the uncompressed size of the section.
120
121 DWP file format V2 introduces a wrinkle that is easiest to handle by
122 creating the concept of virtual sections contained within a real section.
123 In DWP V2 the sections of the input DWO files are concatenated together
124 into one section, but section offsets are kept relative to the original
125 input section.
126 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
127 the real section this "virtual" section is contained in, and BUFFER,SIZE
128 describe the virtual section. */
129
dce234bc
PP
130struct dwarf2_section_info
131{
73869dc2
DE
132 union
133 {
e5aa3347 134 /* If this is a real section, the bfd section. */
049412e3 135 asection *section;
73869dc2 136 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 137 section. */
73869dc2
DE
138 struct dwarf2_section_info *containing_section;
139 } s;
19ac8c2e 140 /* Pointer to section data, only valid if readin. */
d521ce57 141 const gdb_byte *buffer;
73869dc2 142 /* The size of the section, real or virtual. */
dce234bc 143 bfd_size_type size;
73869dc2
DE
144 /* If this is a virtual section, the offset in the real section.
145 Only valid if is_virtual. */
146 bfd_size_type virtual_offset;
be391dca 147 /* True if we have tried to read this section. */
73869dc2
DE
148 char readin;
149 /* True if this is a virtual section, False otherwise.
049412e3 150 This specifies which of s.section and s.containing_section to use. */
73869dc2 151 char is_virtual;
dce234bc
PP
152};
153
8b70b953
TT
154typedef struct dwarf2_section_info dwarf2_section_info_def;
155DEF_VEC_O (dwarf2_section_info_def);
156
9291a0cd
TT
157/* All offsets in the index are of this type. It must be
158 architecture-independent. */
159typedef uint32_t offset_type;
160
161DEF_VEC_I (offset_type);
162
156942c7
DE
163/* Ensure only legit values are used. */
164#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((unsigned int) (value) <= 1); \
167 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
168 } while (0)
169
170/* Ensure only legit values are used. */
171#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
172 do { \
173 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
174 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
175 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
178/* Ensure we don't use more than the alloted nuber of bits for the CU. */
179#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
180 do { \
181 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
182 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
183 } while (0)
184
9291a0cd
TT
185/* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187struct mapped_index
188{
559a7a62
JK
189 /* Index data format version. */
190 int version;
191
9291a0cd
TT
192 /* The total length of the buffer. */
193 off_t total_size;
b11b1f88 194
9291a0cd
TT
195 /* A pointer to the address table data. */
196 const gdb_byte *address_table;
b11b1f88 197
9291a0cd
TT
198 /* Size of the address table data in bytes. */
199 offset_type address_table_size;
b11b1f88 200
3876f04e
DE
201 /* The symbol table, implemented as a hash table. */
202 const offset_type *symbol_table;
b11b1f88 203
9291a0cd 204 /* Size in slots, each slot is 2 offset_types. */
3876f04e 205 offset_type symbol_table_slots;
b11b1f88 206
9291a0cd
TT
207 /* A pointer to the constant pool. */
208 const char *constant_pool;
209};
210
95554aad
TT
211typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
212DEF_VEC_P (dwarf2_per_cu_ptr);
213
52059ffd
TT
214struct tu_stats
215{
216 int nr_uniq_abbrev_tables;
217 int nr_symtabs;
218 int nr_symtab_sharers;
219 int nr_stmt_less_type_units;
220 int nr_all_type_units_reallocs;
221};
222
9cdd5dbd
DE
223/* Collection of data recorded per objfile.
224 This hangs off of dwarf2_objfile_data_key. */
225
6502dd73
DJ
226struct dwarf2_per_objfile
227{
330cdd98
PA
228 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
229 dwarf2 section names, or is NULL if the standard ELF names are
230 used. */
231 dwarf2_per_objfile (struct objfile *objfile,
232 const dwarf2_debug_sections *names);
ae038cb0 233
330cdd98
PA
234 ~dwarf2_per_objfile ();
235
d6541620 236 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
237
238 /* Free all cached compilation units. */
239 void free_cached_comp_units ();
240private:
241 /* This function is mapped across the sections and remembers the
242 offset and size of each of the debugging sections we are
243 interested in. */
244 void locate_sections (bfd *abfd, asection *sectp,
245 const dwarf2_debug_sections &names);
246
247public:
248 dwarf2_section_info info {};
249 dwarf2_section_info abbrev {};
250 dwarf2_section_info line {};
251 dwarf2_section_info loc {};
252 dwarf2_section_info loclists {};
253 dwarf2_section_info macinfo {};
254 dwarf2_section_info macro {};
255 dwarf2_section_info str {};
256 dwarf2_section_info line_str {};
257 dwarf2_section_info ranges {};
258 dwarf2_section_info rnglists {};
259 dwarf2_section_info addr {};
260 dwarf2_section_info frame {};
261 dwarf2_section_info eh_frame {};
262 dwarf2_section_info gdb_index {};
263
264 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 265
be391dca 266 /* Back link. */
330cdd98 267 struct objfile *objfile = NULL;
be391dca 268
d467dd73 269 /* Table of all the compilation units. This is used to locate
10b3939b 270 the target compilation unit of a particular reference. */
330cdd98 271 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
272
273 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 274 int n_comp_units = 0;
ae038cb0 275
1fd400ff 276 /* The number of .debug_types-related CUs. */
330cdd98 277 int n_type_units = 0;
1fd400ff 278
6aa5f3a6
DE
279 /* The number of elements allocated in all_type_units.
280 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 281 int n_allocated_type_units = 0;
6aa5f3a6 282
a2ce51a0
DE
283 /* The .debug_types-related CUs (TUs).
284 This is stored in malloc space because we may realloc it. */
330cdd98 285 struct signatured_type **all_type_units = NULL;
1fd400ff 286
f4dc4d17
DE
287 /* Table of struct type_unit_group objects.
288 The hash key is the DW_AT_stmt_list value. */
330cdd98 289 htab_t type_unit_groups {};
72dca2f5 290
348e048f
DE
291 /* A table mapping .debug_types signatures to its signatured_type entry.
292 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 293 htab_t signatured_types {};
348e048f 294
f4dc4d17
DE
295 /* Type unit statistics, to see how well the scaling improvements
296 are doing. */
330cdd98 297 struct tu_stats tu_stats {};
f4dc4d17
DE
298
299 /* A chain of compilation units that are currently read in, so that
300 they can be freed later. */
330cdd98 301 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 302
3019eac3
DE
303 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
304 This is NULL if the table hasn't been allocated yet. */
330cdd98 305 htab_t dwo_files {};
3019eac3 306
330cdd98
PA
307 /* True if we've checked for whether there is a DWP file. */
308 bool dwp_checked = false;
80626a55
DE
309
310 /* The DWP file if there is one, or NULL. */
330cdd98 311 struct dwp_file *dwp_file = NULL;
80626a55 312
36586728
TT
313 /* The shared '.dwz' file, if one exists. This is used when the
314 original data was compressed using 'dwz -m'. */
330cdd98 315 struct dwz_file *dwz_file = NULL;
36586728 316
330cdd98 317 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 318 VMA of 0. */
330cdd98 319 bool has_section_at_zero = false;
9291a0cd 320
ae2de4f8
DE
321 /* True if we are using the mapped index,
322 or we are faking it for OBJF_READNOW's sake. */
330cdd98 323 bool using_index = false;
9291a0cd 324
ae2de4f8 325 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 326 mapped_index *index_table = NULL;
98bfdba5 327
7b9f3c50 328 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
329 TUs typically share line table entries with a CU, so we maintain a
330 separate table of all line table entries to support the sharing.
331 Note that while there can be way more TUs than CUs, we've already
332 sorted all the TUs into "type unit groups", grouped by their
333 DW_AT_stmt_list value. Therefore the only sharing done here is with a
334 CU and its associated TU group if there is one. */
330cdd98 335 htab_t quick_file_names_table {};
7b9f3c50 336
98bfdba5
PA
337 /* Set during partial symbol reading, to prevent queueing of full
338 symbols. */
330cdd98 339 bool reading_partial_symbols = false;
673bfd45 340
dee91e82 341 /* Table mapping type DIEs to their struct type *.
673bfd45 342 This is NULL if not allocated yet.
02142a6c 343 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 344 htab_t die_type_hash {};
95554aad
TT
345
346 /* The CUs we recently read. */
330cdd98 347 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
348
349 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 350 htab_t line_header_hash {};
bbf2f4df
PA
351
352 /* Table containing all filenames. This is an optional because the
353 table is lazily constructed on first access. */
354 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
355};
356
357static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 358
251d32d9 359/* Default names of the debugging sections. */
c906108c 360
233a11ab
CS
361/* Note that if the debugging section has been compressed, it might
362 have a name like .zdebug_info. */
363
9cdd5dbd
DE
364static const struct dwarf2_debug_sections dwarf2_elf_names =
365{
251d32d9
TG
366 { ".debug_info", ".zdebug_info" },
367 { ".debug_abbrev", ".zdebug_abbrev" },
368 { ".debug_line", ".zdebug_line" },
369 { ".debug_loc", ".zdebug_loc" },
43988095 370 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 371 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 372 { ".debug_macro", ".zdebug_macro" },
251d32d9 373 { ".debug_str", ".zdebug_str" },
43988095 374 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 375 { ".debug_ranges", ".zdebug_ranges" },
43988095 376 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 377 { ".debug_types", ".zdebug_types" },
3019eac3 378 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
379 { ".debug_frame", ".zdebug_frame" },
380 { ".eh_frame", NULL },
24d3216f
TT
381 { ".gdb_index", ".zgdb_index" },
382 23
251d32d9 383};
c906108c 384
80626a55 385/* List of DWO/DWP sections. */
3019eac3 386
80626a55 387static const struct dwop_section_names
3019eac3
DE
388{
389 struct dwarf2_section_names abbrev_dwo;
390 struct dwarf2_section_names info_dwo;
391 struct dwarf2_section_names line_dwo;
392 struct dwarf2_section_names loc_dwo;
43988095 393 struct dwarf2_section_names loclists_dwo;
09262596
DE
394 struct dwarf2_section_names macinfo_dwo;
395 struct dwarf2_section_names macro_dwo;
3019eac3
DE
396 struct dwarf2_section_names str_dwo;
397 struct dwarf2_section_names str_offsets_dwo;
398 struct dwarf2_section_names types_dwo;
80626a55
DE
399 struct dwarf2_section_names cu_index;
400 struct dwarf2_section_names tu_index;
3019eac3 401}
80626a55 402dwop_section_names =
3019eac3
DE
403{
404 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
405 { ".debug_info.dwo", ".zdebug_info.dwo" },
406 { ".debug_line.dwo", ".zdebug_line.dwo" },
407 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 408 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
409 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
410 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
411 { ".debug_str.dwo", ".zdebug_str.dwo" },
412 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
413 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
414 { ".debug_cu_index", ".zdebug_cu_index" },
415 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
416};
417
c906108c
SS
418/* local data types */
419
107d2387
AC
420/* The data in a compilation unit header, after target2host
421 translation, looks like this. */
c906108c 422struct comp_unit_head
a738430d 423{
c764a876 424 unsigned int length;
a738430d 425 short version;
a738430d
MK
426 unsigned char addr_size;
427 unsigned char signed_addr_p;
9c541725 428 sect_offset abbrev_sect_off;
57349743 429
a738430d
MK
430 /* Size of file offsets; either 4 or 8. */
431 unsigned int offset_size;
57349743 432
a738430d
MK
433 /* Size of the length field; either 4 or 12. */
434 unsigned int initial_length_size;
57349743 435
43988095
JK
436 enum dwarf_unit_type unit_type;
437
a738430d
MK
438 /* Offset to the first byte of this compilation unit header in the
439 .debug_info section, for resolving relative reference dies. */
9c541725 440 sect_offset sect_off;
57349743 441
d00adf39
DE
442 /* Offset to first die in this cu from the start of the cu.
443 This will be the first byte following the compilation unit header. */
9c541725 444 cu_offset first_die_cu_offset;
43988095
JK
445
446 /* 64-bit signature of this type unit - it is valid only for
447 UNIT_TYPE DW_UT_type. */
448 ULONGEST signature;
449
450 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 451 cu_offset type_cu_offset_in_tu;
a738430d 452};
c906108c 453
3da10d80
KS
454/* Type used for delaying computation of method physnames.
455 See comments for compute_delayed_physnames. */
456struct delayed_method_info
457{
458 /* The type to which the method is attached, i.e., its parent class. */
459 struct type *type;
460
461 /* The index of the method in the type's function fieldlists. */
462 int fnfield_index;
463
464 /* The index of the method in the fieldlist. */
465 int index;
466
467 /* The name of the DIE. */
468 const char *name;
469
470 /* The DIE associated with this method. */
471 struct die_info *die;
472};
473
474typedef struct delayed_method_info delayed_method_info;
475DEF_VEC_O (delayed_method_info);
476
e7c27a73
DJ
477/* Internal state when decoding a particular compilation unit. */
478struct dwarf2_cu
479{
480 /* The objfile containing this compilation unit. */
481 struct objfile *objfile;
482
d00adf39 483 /* The header of the compilation unit. */
e7c27a73 484 struct comp_unit_head header;
e142c38c 485
d00adf39
DE
486 /* Base address of this compilation unit. */
487 CORE_ADDR base_address;
488
489 /* Non-zero if base_address has been set. */
490 int base_known;
491
e142c38c
DJ
492 /* The language we are debugging. */
493 enum language language;
494 const struct language_defn *language_defn;
495
b0f35d58
DL
496 const char *producer;
497
e142c38c
DJ
498 /* The generic symbol table building routines have separate lists for
499 file scope symbols and all all other scopes (local scopes). So
500 we need to select the right one to pass to add_symbol_to_list().
501 We do it by keeping a pointer to the correct list in list_in_scope.
502
503 FIXME: The original dwarf code just treated the file scope as the
504 first local scope, and all other local scopes as nested local
505 scopes, and worked fine. Check to see if we really need to
506 distinguish these in buildsym.c. */
507 struct pending **list_in_scope;
508
433df2d4
DE
509 /* The abbrev table for this CU.
510 Normally this points to the abbrev table in the objfile.
511 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
512 struct abbrev_table *abbrev_table;
72bf9492 513
b64f50a1
JK
514 /* Hash table holding all the loaded partial DIEs
515 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
516 htab_t partial_dies;
517
518 /* Storage for things with the same lifetime as this read-in compilation
519 unit, including partial DIEs. */
520 struct obstack comp_unit_obstack;
521
ae038cb0
DJ
522 /* When multiple dwarf2_cu structures are living in memory, this field
523 chains them all together, so that they can be released efficiently.
524 We will probably also want a generation counter so that most-recently-used
525 compilation units are cached... */
526 struct dwarf2_per_cu_data *read_in_chain;
527
69d751e3 528 /* Backlink to our per_cu entry. */
ae038cb0
DJ
529 struct dwarf2_per_cu_data *per_cu;
530
531 /* How many compilation units ago was this CU last referenced? */
532 int last_used;
533
b64f50a1
JK
534 /* A hash table of DIE cu_offset for following references with
535 die_info->offset.sect_off as hash. */
51545339 536 htab_t die_hash;
10b3939b
DJ
537
538 /* Full DIEs if read in. */
539 struct die_info *dies;
540
541 /* A set of pointers to dwarf2_per_cu_data objects for compilation
542 units referenced by this one. Only set during full symbol processing;
543 partial symbol tables do not have dependencies. */
544 htab_t dependencies;
545
cb1df416
DJ
546 /* Header data from the line table, during full symbol processing. */
547 struct line_header *line_header;
4c8aa72d
PA
548 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
549 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
550 this is the DW_TAG_compile_unit die for this CU. We'll hold on
551 to the line header as long as this DIE is being processed. See
552 process_die_scope. */
553 die_info *line_header_die_owner;
cb1df416 554
3da10d80
KS
555 /* A list of methods which need to have physnames computed
556 after all type information has been read. */
557 VEC (delayed_method_info) *method_list;
558
96408a79
SA
559 /* To be copied to symtab->call_site_htab. */
560 htab_t call_site_htab;
561
034e5797
DE
562 /* Non-NULL if this CU came from a DWO file.
563 There is an invariant here that is important to remember:
564 Except for attributes copied from the top level DIE in the "main"
565 (or "stub") file in preparation for reading the DWO file
566 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
567 Either there isn't a DWO file (in which case this is NULL and the point
568 is moot), or there is and either we're not going to read it (in which
569 case this is NULL) or there is and we are reading it (in which case this
570 is non-NULL). */
3019eac3
DE
571 struct dwo_unit *dwo_unit;
572
573 /* The DW_AT_addr_base attribute if present, zero otherwise
574 (zero is a valid value though).
1dbab08b 575 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
576 ULONGEST addr_base;
577
2e3cf129
DE
578 /* The DW_AT_ranges_base attribute if present, zero otherwise
579 (zero is a valid value though).
1dbab08b 580 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 581 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
582 be used without needing to know whether DWO files are in use or not.
583 N.B. This does not apply to DW_AT_ranges appearing in
584 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
585 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
586 DW_AT_ranges_base *would* have to be applied, and we'd have to care
587 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
588 ULONGEST ranges_base;
589
ae038cb0
DJ
590 /* Mark used when releasing cached dies. */
591 unsigned int mark : 1;
592
8be455d7
JK
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 597 unsigned int has_loclist : 1;
ba919b58 598
1b80a9fa
JK
599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
ba919b58
TT
603 unsigned int checked_producer : 1;
604 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 605 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 606 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
607
608 /* When set, the file that we're processing is known to have
609 debugging info for C++ namespaces. GCC 3.3.x did not produce
610 this information, but later versions do. */
611
612 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
613};
614
10b3939b
DJ
615/* Persistent data held for a compilation unit, even when not
616 processing it. We put a pointer to this structure in the
28dee7f5 617 read_symtab_private field of the psymtab. */
10b3939b 618
ae038cb0
DJ
619struct dwarf2_per_cu_data
620{
36586728 621 /* The start offset and length of this compilation unit.
45452591 622 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
623 initial_length_size.
624 If the DIE refers to a DWO file, this is always of the original die,
625 not the DWO file. */
9c541725 626 sect_offset sect_off;
36586728 627 unsigned int length;
ae038cb0 628
43988095
JK
629 /* DWARF standard version this data has been read from (such as 4 or 5). */
630 short dwarf_version;
631
ae038cb0
DJ
632 /* Flag indicating this compilation unit will be read in before
633 any of the current compilation units are processed. */
c764a876 634 unsigned int queued : 1;
ae038cb0 635
0d99eb77
DE
636 /* This flag will be set when reading partial DIEs if we need to load
637 absolutely all DIEs for this compilation unit, instead of just the ones
638 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
639 hash table and don't find it. */
640 unsigned int load_all_dies : 1;
641
0186c6a7
DE
642 /* Non-zero if this CU is from .debug_types.
643 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
644 this is non-zero. */
3019eac3
DE
645 unsigned int is_debug_types : 1;
646
36586728
TT
647 /* Non-zero if this CU is from the .dwz file. */
648 unsigned int is_dwz : 1;
649
a2ce51a0
DE
650 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
651 This flag is only valid if is_debug_types is true.
652 We can't read a CU directly from a DWO file: There are required
653 attributes in the stub. */
654 unsigned int reading_dwo_directly : 1;
655
7ee85ab1
DE
656 /* Non-zero if the TU has been read.
657 This is used to assist the "Stay in DWO Optimization" for Fission:
658 When reading a DWO, it's faster to read TUs from the DWO instead of
659 fetching them from random other DWOs (due to comdat folding).
660 If the TU has already been read, the optimization is unnecessary
661 (and unwise - we don't want to change where gdb thinks the TU lives
662 "midflight").
663 This flag is only valid if is_debug_types is true. */
664 unsigned int tu_read : 1;
665
3019eac3
DE
666 /* The section this CU/TU lives in.
667 If the DIE refers to a DWO file, this is always the original die,
668 not the DWO file. */
8a0459fd 669 struct dwarf2_section_info *section;
348e048f 670
17ea53c3 671 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
672 of the CU cache it gets reset to NULL again. This is left as NULL for
673 dummy CUs (a CU header, but nothing else). */
ae038cb0 674 struct dwarf2_cu *cu;
1c379e20 675
9cdd5dbd
DE
676 /* The corresponding objfile.
677 Normally we can get the objfile from dwarf2_per_objfile.
678 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
679 struct objfile *objfile;
680
fffbe6a8
YQ
681 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
682 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
683 union
684 {
685 /* The partial symbol table associated with this compilation unit,
95554aad 686 or NULL for unread partial units. */
9291a0cd
TT
687 struct partial_symtab *psymtab;
688
689 /* Data needed by the "quick" functions. */
690 struct dwarf2_per_cu_quick_data *quick;
691 } v;
95554aad 692
796a7ff8
DE
693 /* The CUs we import using DW_TAG_imported_unit. This is filled in
694 while reading psymtabs, used to compute the psymtab dependencies,
695 and then cleared. Then it is filled in again while reading full
696 symbols, and only deleted when the objfile is destroyed.
697
698 This is also used to work around a difference between the way gold
699 generates .gdb_index version <=7 and the way gdb does. Arguably this
700 is a gold bug. For symbols coming from TUs, gold records in the index
701 the CU that includes the TU instead of the TU itself. This breaks
702 dw2_lookup_symbol: It assumes that if the index says symbol X lives
703 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
704 will find X. Alas TUs live in their own symtab, so after expanding CU Y
705 we need to look in TU Z to find X. Fortunately, this is akin to
706 DW_TAG_imported_unit, so we just use the same mechanism: For
707 .gdb_index version <=7 this also records the TUs that the CU referred
708 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
709 indices so we only pay a price for gold generated indices.
710 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 711 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
712};
713
348e048f
DE
714/* Entry in the signatured_types hash table. */
715
716struct signatured_type
717{
42e7ad6c 718 /* The "per_cu" object of this type.
ac9ec31b 719 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
720 N.B.: This is the first member so that it's easy to convert pointers
721 between them. */
722 struct dwarf2_per_cu_data per_cu;
723
3019eac3 724 /* The type's signature. */
348e048f
DE
725 ULONGEST signature;
726
3019eac3 727 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
728 If this TU is a DWO stub and the definition lives in a DWO file
729 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
730 cu_offset type_offset_in_tu;
731
732 /* Offset in the section of the type's DIE.
733 If the definition lives in a DWO file, this is the offset in the
734 .debug_types.dwo section.
735 The value is zero until the actual value is known.
736 Zero is otherwise not a valid section offset. */
737 sect_offset type_offset_in_section;
0186c6a7
DE
738
739 /* Type units are grouped by their DW_AT_stmt_list entry so that they
740 can share them. This points to the containing symtab. */
741 struct type_unit_group *type_unit_group;
ac9ec31b
DE
742
743 /* The type.
744 The first time we encounter this type we fully read it in and install it
745 in the symbol tables. Subsequent times we only need the type. */
746 struct type *type;
a2ce51a0
DE
747
748 /* Containing DWO unit.
749 This field is valid iff per_cu.reading_dwo_directly. */
750 struct dwo_unit *dwo_unit;
348e048f
DE
751};
752
0186c6a7
DE
753typedef struct signatured_type *sig_type_ptr;
754DEF_VEC_P (sig_type_ptr);
755
094b34ac
DE
756/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
757 This includes type_unit_group and quick_file_names. */
758
759struct stmt_list_hash
760{
761 /* The DWO unit this table is from or NULL if there is none. */
762 struct dwo_unit *dwo_unit;
763
764 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 765 sect_offset line_sect_off;
094b34ac
DE
766};
767
f4dc4d17
DE
768/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
769 an object of this type. */
770
771struct type_unit_group
772{
0186c6a7 773 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
774 To simplify things we create an artificial CU that "includes" all the
775 type units using this stmt_list so that the rest of the code still has
776 a "per_cu" handle on the symtab.
777 This PER_CU is recognized by having no section. */
8a0459fd 778#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
779 struct dwarf2_per_cu_data per_cu;
780
0186c6a7
DE
781 /* The TUs that share this DW_AT_stmt_list entry.
782 This is added to while parsing type units to build partial symtabs,
783 and is deleted afterwards and not used again. */
784 VEC (sig_type_ptr) *tus;
f4dc4d17 785
43f3e411 786 /* The compunit symtab.
094b34ac 787 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
788 so we create an essentially anonymous symtab as the compunit symtab. */
789 struct compunit_symtab *compunit_symtab;
f4dc4d17 790
094b34ac
DE
791 /* The data used to construct the hash key. */
792 struct stmt_list_hash hash;
f4dc4d17
DE
793
794 /* The number of symtabs from the line header.
795 The value here must match line_header.num_file_names. */
796 unsigned int num_symtabs;
797
798 /* The symbol tables for this TU (obtained from the files listed in
799 DW_AT_stmt_list).
800 WARNING: The order of entries here must match the order of entries
801 in the line header. After the first TU using this type_unit_group, the
802 line header for the subsequent TUs is recreated from this. This is done
803 because we need to use the same symtabs for each TU using the same
804 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
805 there's no guarantee the line header doesn't have duplicate entries. */
806 struct symtab **symtabs;
807};
808
73869dc2 809/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
810
811struct dwo_sections
812{
813 struct dwarf2_section_info abbrev;
3019eac3
DE
814 struct dwarf2_section_info line;
815 struct dwarf2_section_info loc;
43988095 816 struct dwarf2_section_info loclists;
09262596
DE
817 struct dwarf2_section_info macinfo;
818 struct dwarf2_section_info macro;
3019eac3
DE
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info str_offsets;
80626a55
DE
821 /* In the case of a virtual DWO file, these two are unused. */
822 struct dwarf2_section_info info;
3019eac3
DE
823 VEC (dwarf2_section_info_def) *types;
824};
825
c88ee1f0 826/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
827
828struct dwo_unit
829{
830 /* Backlink to the containing struct dwo_file. */
831 struct dwo_file *dwo_file;
832
833 /* The "id" that distinguishes this CU/TU.
834 .debug_info calls this "dwo_id", .debug_types calls this "signature".
835 Since signatures came first, we stick with it for consistency. */
836 ULONGEST signature;
837
838 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 839 struct dwarf2_section_info *section;
3019eac3 840
9c541725
PA
841 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
842 sect_offset sect_off;
3019eac3
DE
843 unsigned int length;
844
845 /* For types, offset in the type's DIE of the type defined by this TU. */
846 cu_offset type_offset_in_tu;
847};
848
73869dc2
DE
849/* include/dwarf2.h defines the DWP section codes.
850 It defines a max value but it doesn't define a min value, which we
851 use for error checking, so provide one. */
852
853enum dwp_v2_section_ids
854{
855 DW_SECT_MIN = 1
856};
857
80626a55 858/* Data for one DWO file.
57d63ce2
DE
859
860 This includes virtual DWO files (a virtual DWO file is a DWO file as it
861 appears in a DWP file). DWP files don't really have DWO files per se -
862 comdat folding of types "loses" the DWO file they came from, and from
863 a high level view DWP files appear to contain a mass of random types.
864 However, to maintain consistency with the non-DWP case we pretend DWP
865 files contain virtual DWO files, and we assign each TU with one virtual
866 DWO file (generally based on the line and abbrev section offsets -
867 a heuristic that seems to work in practice). */
3019eac3
DE
868
869struct dwo_file
870{
0ac5b59e 871 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
872 For virtual DWO files the name is constructed from the section offsets
873 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
874 from related CU+TUs. */
0ac5b59e
DE
875 const char *dwo_name;
876
877 /* The DW_AT_comp_dir attribute. */
878 const char *comp_dir;
3019eac3 879
80626a55
DE
880 /* The bfd, when the file is open. Otherwise this is NULL.
881 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
882 bfd *dbfd;
3019eac3 883
73869dc2
DE
884 /* The sections that make up this DWO file.
885 Remember that for virtual DWO files in DWP V2, these are virtual
886 sections (for lack of a better name). */
3019eac3
DE
887 struct dwo_sections sections;
888
33c5cd75
DB
889 /* The CUs in the file.
890 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
891 an extension to handle LLVM's Link Time Optimization output (where
892 multiple source files may be compiled into a single object/dwo pair). */
893 htab_t cus;
3019eac3
DE
894
895 /* Table of TUs in the file.
896 Each element is a struct dwo_unit. */
897 htab_t tus;
898};
899
80626a55
DE
900/* These sections are what may appear in a DWP file. */
901
902struct dwp_sections
903{
73869dc2 904 /* These are used by both DWP version 1 and 2. */
80626a55
DE
905 struct dwarf2_section_info str;
906 struct dwarf2_section_info cu_index;
907 struct dwarf2_section_info tu_index;
73869dc2
DE
908
909 /* These are only used by DWP version 2 files.
910 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
911 sections are referenced by section number, and are not recorded here.
912 In DWP version 2 there is at most one copy of all these sections, each
913 section being (effectively) comprised of the concatenation of all of the
914 individual sections that exist in the version 1 format.
915 To keep the code simple we treat each of these concatenated pieces as a
916 section itself (a virtual section?). */
917 struct dwarf2_section_info abbrev;
918 struct dwarf2_section_info info;
919 struct dwarf2_section_info line;
920 struct dwarf2_section_info loc;
921 struct dwarf2_section_info macinfo;
922 struct dwarf2_section_info macro;
923 struct dwarf2_section_info str_offsets;
924 struct dwarf2_section_info types;
80626a55
DE
925};
926
73869dc2
DE
927/* These sections are what may appear in a virtual DWO file in DWP version 1.
928 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 929
73869dc2 930struct virtual_v1_dwo_sections
80626a55
DE
931{
932 struct dwarf2_section_info abbrev;
933 struct dwarf2_section_info line;
934 struct dwarf2_section_info loc;
935 struct dwarf2_section_info macinfo;
936 struct dwarf2_section_info macro;
937 struct dwarf2_section_info str_offsets;
938 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 939 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
940 struct dwarf2_section_info info_or_types;
941};
942
73869dc2
DE
943/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
944 In version 2, the sections of the DWO files are concatenated together
945 and stored in one section of that name. Thus each ELF section contains
946 several "virtual" sections. */
947
948struct virtual_v2_dwo_sections
949{
950 bfd_size_type abbrev_offset;
951 bfd_size_type abbrev_size;
952
953 bfd_size_type line_offset;
954 bfd_size_type line_size;
955
956 bfd_size_type loc_offset;
957 bfd_size_type loc_size;
958
959 bfd_size_type macinfo_offset;
960 bfd_size_type macinfo_size;
961
962 bfd_size_type macro_offset;
963 bfd_size_type macro_size;
964
965 bfd_size_type str_offsets_offset;
966 bfd_size_type str_offsets_size;
967
968 /* Each DWP hash table entry records one CU or one TU.
969 That is recorded here, and copied to dwo_unit.section. */
970 bfd_size_type info_or_types_offset;
971 bfd_size_type info_or_types_size;
972};
973
80626a55
DE
974/* Contents of DWP hash tables. */
975
976struct dwp_hash_table
977{
73869dc2 978 uint32_t version, nr_columns;
80626a55 979 uint32_t nr_units, nr_slots;
73869dc2
DE
980 const gdb_byte *hash_table, *unit_table;
981 union
982 {
983 struct
984 {
985 const gdb_byte *indices;
986 } v1;
987 struct
988 {
989 /* This is indexed by column number and gives the id of the section
990 in that column. */
991#define MAX_NR_V2_DWO_SECTIONS \
992 (1 /* .debug_info or .debug_types */ \
993 + 1 /* .debug_abbrev */ \
994 + 1 /* .debug_line */ \
995 + 1 /* .debug_loc */ \
996 + 1 /* .debug_str_offsets */ \
997 + 1 /* .debug_macro or .debug_macinfo */)
998 int section_ids[MAX_NR_V2_DWO_SECTIONS];
999 const gdb_byte *offsets;
1000 const gdb_byte *sizes;
1001 } v2;
1002 } section_pool;
80626a55
DE
1003};
1004
1005/* Data for one DWP file. */
1006
1007struct dwp_file
1008{
1009 /* Name of the file. */
1010 const char *name;
1011
73869dc2
DE
1012 /* File format version. */
1013 int version;
1014
93417882 1015 /* The bfd. */
80626a55
DE
1016 bfd *dbfd;
1017
1018 /* Section info for this file. */
1019 struct dwp_sections sections;
1020
57d63ce2 1021 /* Table of CUs in the file. */
80626a55
DE
1022 const struct dwp_hash_table *cus;
1023
1024 /* Table of TUs in the file. */
1025 const struct dwp_hash_table *tus;
1026
19ac8c2e
DE
1027 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1028 htab_t loaded_cus;
1029 htab_t loaded_tus;
80626a55 1030
73869dc2
DE
1031 /* Table to map ELF section numbers to their sections.
1032 This is only needed for the DWP V1 file format. */
80626a55
DE
1033 unsigned int num_sections;
1034 asection **elf_sections;
1035};
1036
36586728
TT
1037/* This represents a '.dwz' file. */
1038
1039struct dwz_file
1040{
1041 /* A dwz file can only contain a few sections. */
1042 struct dwarf2_section_info abbrev;
1043 struct dwarf2_section_info info;
1044 struct dwarf2_section_info str;
1045 struct dwarf2_section_info line;
1046 struct dwarf2_section_info macro;
2ec9a5e0 1047 struct dwarf2_section_info gdb_index;
36586728
TT
1048
1049 /* The dwz's BFD. */
1050 bfd *dwz_bfd;
1051};
1052
0963b4bd
MS
1053/* Struct used to pass misc. parameters to read_die_and_children, et
1054 al. which are used for both .debug_info and .debug_types dies.
1055 All parameters here are unchanging for the life of the call. This
dee91e82 1056 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1057
1058struct die_reader_specs
1059{
a32a8923 1060 /* The bfd of die_section. */
93311388
DE
1061 bfd* abfd;
1062
1063 /* The CU of the DIE we are parsing. */
1064 struct dwarf2_cu *cu;
1065
80626a55 1066 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1067 struct dwo_file *dwo_file;
1068
dee91e82 1069 /* The section the die comes from.
3019eac3 1070 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1071 struct dwarf2_section_info *die_section;
1072
1073 /* die_section->buffer. */
d521ce57 1074 const gdb_byte *buffer;
f664829e
DE
1075
1076 /* The end of the buffer. */
1077 const gdb_byte *buffer_end;
a2ce51a0
DE
1078
1079 /* The value of the DW_AT_comp_dir attribute. */
1080 const char *comp_dir;
93311388
DE
1081};
1082
fd820528 1083/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1084typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1085 const gdb_byte *info_ptr,
dee91e82
DE
1086 struct die_info *comp_unit_die,
1087 int has_children,
1088 void *data);
1089
ecfb656c
PA
1090/* A 1-based directory index. This is a strong typedef to prevent
1091 accidentally using a directory index as a 0-based index into an
1092 array/vector. */
1093enum class dir_index : unsigned int {};
1094
1095/* Likewise, a 1-based file name index. */
1096enum class file_name_index : unsigned int {};
1097
52059ffd
TT
1098struct file_entry
1099{
fff8551c
PA
1100 file_entry () = default;
1101
ecfb656c 1102 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1103 unsigned int mod_time_, unsigned int length_)
1104 : name (name_),
ecfb656c 1105 d_index (d_index_),
fff8551c
PA
1106 mod_time (mod_time_),
1107 length (length_)
1108 {}
1109
ecfb656c
PA
1110 /* Return the include directory at D_INDEX stored in LH. Returns
1111 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1112 const char *include_dir (const line_header *lh) const;
1113
fff8551c
PA
1114 /* The file name. Note this is an observing pointer. The memory is
1115 owned by debug_line_buffer. */
1116 const char *name {};
1117
8c43009f 1118 /* The directory index (1-based). */
ecfb656c 1119 dir_index d_index {};
fff8551c
PA
1120
1121 unsigned int mod_time {};
1122
1123 unsigned int length {};
1124
1125 /* True if referenced by the Line Number Program. */
1126 bool included_p {};
1127
83769d0b 1128 /* The associated symbol table, if any. */
fff8551c 1129 struct symtab *symtab {};
52059ffd
TT
1130};
1131
debd256d
JB
1132/* The line number information for a compilation unit (found in the
1133 .debug_line section) begins with a "statement program header",
1134 which contains the following information. */
1135struct line_header
1136{
fff8551c
PA
1137 line_header ()
1138 : offset_in_dwz {}
1139 {}
1140
1141 /* Add an entry to the include directory table. */
1142 void add_include_dir (const char *include_dir);
1143
1144 /* Add an entry to the file name table. */
ecfb656c 1145 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1146 unsigned int mod_time, unsigned int length);
1147
ecfb656c 1148 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1149 is out of bounds. */
ecfb656c 1150 const char *include_dir_at (dir_index index) const
8c43009f 1151 {
ecfb656c
PA
1152 /* Convert directory index number (1-based) to vector index
1153 (0-based). */
1154 size_t vec_index = to_underlying (index) - 1;
1155
1156 if (vec_index >= include_dirs.size ())
8c43009f 1157 return NULL;
ecfb656c 1158 return include_dirs[vec_index];
8c43009f
PA
1159 }
1160
ecfb656c 1161 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1162 is out of bounds. */
ecfb656c 1163 file_entry *file_name_at (file_name_index index)
8c43009f 1164 {
ecfb656c
PA
1165 /* Convert file name index number (1-based) to vector index
1166 (0-based). */
1167 size_t vec_index = to_underlying (index) - 1;
1168
1169 if (vec_index >= file_names.size ())
fff8551c 1170 return NULL;
ecfb656c 1171 return &file_names[vec_index];
fff8551c
PA
1172 }
1173
1174 /* Const version of the above. */
1175 const file_entry *file_name_at (unsigned int index) const
1176 {
1177 if (index >= file_names.size ())
8c43009f
PA
1178 return NULL;
1179 return &file_names[index];
1180 }
1181
527f3840 1182 /* Offset of line number information in .debug_line section. */
9c541725 1183 sect_offset sect_off {};
527f3840
JK
1184
1185 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1186 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1187
1188 unsigned int total_length {};
1189 unsigned short version {};
1190 unsigned int header_length {};
1191 unsigned char minimum_instruction_length {};
1192 unsigned char maximum_ops_per_instruction {};
1193 unsigned char default_is_stmt {};
1194 int line_base {};
1195 unsigned char line_range {};
1196 unsigned char opcode_base {};
debd256d
JB
1197
1198 /* standard_opcode_lengths[i] is the number of operands for the
1199 standard opcode whose value is i. This means that
1200 standard_opcode_lengths[0] is unused, and the last meaningful
1201 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1202 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1203
fff8551c
PA
1204 /* The include_directories table. Note these are observing
1205 pointers. The memory is owned by debug_line_buffer. */
1206 std::vector<const char *> include_dirs;
debd256d 1207
fff8551c
PA
1208 /* The file_names table. */
1209 std::vector<file_entry> file_names;
debd256d
JB
1210
1211 /* The start and end of the statement program following this
6502dd73 1212 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1213 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1214};
c906108c 1215
fff8551c
PA
1216typedef std::unique_ptr<line_header> line_header_up;
1217
8c43009f
PA
1218const char *
1219file_entry::include_dir (const line_header *lh) const
1220{
ecfb656c 1221 return lh->include_dir_at (d_index);
8c43009f
PA
1222}
1223
c906108c 1224/* When we construct a partial symbol table entry we only
0963b4bd 1225 need this much information. */
c906108c
SS
1226struct partial_die_info
1227 {
72bf9492 1228 /* Offset of this DIE. */
9c541725 1229 sect_offset sect_off;
72bf9492
DJ
1230
1231 /* DWARF-2 tag for this DIE. */
1232 ENUM_BITFIELD(dwarf_tag) tag : 16;
1233
72bf9492
DJ
1234 /* Assorted flags describing the data found in this DIE. */
1235 unsigned int has_children : 1;
1236 unsigned int is_external : 1;
1237 unsigned int is_declaration : 1;
1238 unsigned int has_type : 1;
1239 unsigned int has_specification : 1;
1240 unsigned int has_pc_info : 1;
481860b3 1241 unsigned int may_be_inlined : 1;
72bf9492 1242
0c1b455e
TT
1243 /* This DIE has been marked DW_AT_main_subprogram. */
1244 unsigned int main_subprogram : 1;
1245
72bf9492
DJ
1246 /* Flag set if the SCOPE field of this structure has been
1247 computed. */
1248 unsigned int scope_set : 1;
1249
fa4028e9
JB
1250 /* Flag set if the DIE has a byte_size attribute. */
1251 unsigned int has_byte_size : 1;
1252
ff908ebf
AW
1253 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1254 unsigned int has_const_value : 1;
1255
98bfdba5
PA
1256 /* Flag set if any of the DIE's children are template arguments. */
1257 unsigned int has_template_arguments : 1;
1258
abc72ce4
DE
1259 /* Flag set if fixup_partial_die has been called on this die. */
1260 unsigned int fixup_called : 1;
1261
36586728
TT
1262 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1263 unsigned int is_dwz : 1;
1264
1265 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1266 unsigned int spec_is_dwz : 1;
1267
72bf9492 1268 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1269 sometimes a default name for unnamed DIEs. */
15d034d0 1270 const char *name;
72bf9492 1271
abc72ce4
DE
1272 /* The linkage name, if present. */
1273 const char *linkage_name;
1274
72bf9492
DJ
1275 /* The scope to prepend to our children. This is generally
1276 allocated on the comp_unit_obstack, so will disappear
1277 when this compilation unit leaves the cache. */
15d034d0 1278 const char *scope;
72bf9492 1279
95554aad
TT
1280 /* Some data associated with the partial DIE. The tag determines
1281 which field is live. */
1282 union
1283 {
1284 /* The location description associated with this DIE, if any. */
1285 struct dwarf_block *locdesc;
1286 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1287 sect_offset sect_off;
95554aad 1288 } d;
72bf9492
DJ
1289
1290 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1291 CORE_ADDR lowpc;
1292 CORE_ADDR highpc;
72bf9492 1293
93311388 1294 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1295 DW_AT_sibling, if any. */
abc72ce4
DE
1296 /* NOTE: This member isn't strictly necessary, read_partial_die could
1297 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1298 const gdb_byte *sibling;
72bf9492
DJ
1299
1300 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1301 DW_AT_specification (or DW_AT_abstract_origin or
1302 DW_AT_extension). */
b64f50a1 1303 sect_offset spec_offset;
72bf9492
DJ
1304
1305 /* Pointers to this DIE's parent, first child, and next sibling,
1306 if any. */
1307 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1308 };
1309
0963b4bd 1310/* This data structure holds the information of an abbrev. */
c906108c
SS
1311struct abbrev_info
1312 {
1313 unsigned int number; /* number identifying abbrev */
1314 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1315 unsigned short has_children; /* boolean */
1316 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1317 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1318 struct abbrev_info *next; /* next in chain */
1319 };
1320
1321struct attr_abbrev
1322 {
9d25dd43
DE
1323 ENUM_BITFIELD(dwarf_attribute) name : 16;
1324 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1325
1326 /* It is valid only if FORM is DW_FORM_implicit_const. */
1327 LONGEST implicit_const;
c906108c
SS
1328 };
1329
433df2d4
DE
1330/* Size of abbrev_table.abbrev_hash_table. */
1331#define ABBREV_HASH_SIZE 121
1332
1333/* Top level data structure to contain an abbreviation table. */
1334
1335struct abbrev_table
1336{
f4dc4d17
DE
1337 /* Where the abbrev table came from.
1338 This is used as a sanity check when the table is used. */
9c541725 1339 sect_offset sect_off;
433df2d4
DE
1340
1341 /* Storage for the abbrev table. */
1342 struct obstack abbrev_obstack;
1343
1344 /* Hash table of abbrevs.
1345 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1346 It could be statically allocated, but the previous code didn't so we
1347 don't either. */
1348 struct abbrev_info **abbrevs;
1349};
1350
0963b4bd 1351/* Attributes have a name and a value. */
b60c80d6
DJ
1352struct attribute
1353 {
9d25dd43 1354 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1355 ENUM_BITFIELD(dwarf_form) form : 15;
1356
1357 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1358 field should be in u.str (existing only for DW_STRING) but it is kept
1359 here for better struct attribute alignment. */
1360 unsigned int string_is_canonical : 1;
1361
b60c80d6
DJ
1362 union
1363 {
15d034d0 1364 const char *str;
b60c80d6 1365 struct dwarf_block *blk;
43bbcdc2
PH
1366 ULONGEST unsnd;
1367 LONGEST snd;
b60c80d6 1368 CORE_ADDR addr;
ac9ec31b 1369 ULONGEST signature;
b60c80d6
DJ
1370 }
1371 u;
1372 };
1373
0963b4bd 1374/* This data structure holds a complete die structure. */
c906108c
SS
1375struct die_info
1376 {
76815b17
DE
1377 /* DWARF-2 tag for this DIE. */
1378 ENUM_BITFIELD(dwarf_tag) tag : 16;
1379
1380 /* Number of attributes */
98bfdba5
PA
1381 unsigned char num_attrs;
1382
1383 /* True if we're presently building the full type name for the
1384 type derived from this DIE. */
1385 unsigned char building_fullname : 1;
76815b17 1386
adde2bff
DE
1387 /* True if this die is in process. PR 16581. */
1388 unsigned char in_process : 1;
1389
76815b17
DE
1390 /* Abbrev number */
1391 unsigned int abbrev;
1392
93311388 1393 /* Offset in .debug_info or .debug_types section. */
9c541725 1394 sect_offset sect_off;
78ba4af6
JB
1395
1396 /* The dies in a compilation unit form an n-ary tree. PARENT
1397 points to this die's parent; CHILD points to the first child of
1398 this node; and all the children of a given node are chained
4950bc1c 1399 together via their SIBLING fields. */
639d11d3
DC
1400 struct die_info *child; /* Its first child, if any. */
1401 struct die_info *sibling; /* Its next sibling, if any. */
1402 struct die_info *parent; /* Its parent, if any. */
c906108c 1403
b60c80d6
DJ
1404 /* An array of attributes, with NUM_ATTRS elements. There may be
1405 zero, but it's not common and zero-sized arrays are not
1406 sufficiently portable C. */
1407 struct attribute attrs[1];
c906108c
SS
1408 };
1409
0963b4bd 1410/* Get at parts of an attribute structure. */
c906108c
SS
1411
1412#define DW_STRING(attr) ((attr)->u.str)
8285870a 1413#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1414#define DW_UNSND(attr) ((attr)->u.unsnd)
1415#define DW_BLOCK(attr) ((attr)->u.blk)
1416#define DW_SND(attr) ((attr)->u.snd)
1417#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1418#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1419
0963b4bd 1420/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1421struct dwarf_block
1422 {
56eb65bd 1423 size_t size;
1d6edc3c
JK
1424
1425 /* Valid only if SIZE is not zero. */
d521ce57 1426 const gdb_byte *data;
c906108c
SS
1427 };
1428
c906108c
SS
1429#ifndef ATTR_ALLOC_CHUNK
1430#define ATTR_ALLOC_CHUNK 4
1431#endif
1432
c906108c
SS
1433/* Allocate fields for structs, unions and enums in this size. */
1434#ifndef DW_FIELD_ALLOC_CHUNK
1435#define DW_FIELD_ALLOC_CHUNK 4
1436#endif
1437
c906108c
SS
1438/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1439 but this would require a corresponding change in unpack_field_as_long
1440 and friends. */
1441static int bits_per_byte = 8;
1442
52059ffd
TT
1443struct nextfield
1444{
1445 struct nextfield *next;
1446 int accessibility;
1447 int virtuality;
1448 struct field field;
1449};
1450
1451struct nextfnfield
1452{
1453 struct nextfnfield *next;
1454 struct fn_field fnfield;
1455};
1456
1457struct fnfieldlist
1458{
1459 const char *name;
1460 int length;
1461 struct nextfnfield *head;
1462};
1463
1464struct typedef_field_list
1465{
1466 struct typedef_field field;
1467 struct typedef_field_list *next;
1468};
1469
c906108c
SS
1470/* The routines that read and process dies for a C struct or C++ class
1471 pass lists of data member fields and lists of member function fields
1472 in an instance of a field_info structure, as defined below. */
1473struct field_info
c5aa993b 1474 {
0963b4bd 1475 /* List of data member and baseclasses fields. */
52059ffd 1476 struct nextfield *fields, *baseclasses;
c906108c 1477
7d0ccb61 1478 /* Number of fields (including baseclasses). */
c5aa993b 1479 int nfields;
c906108c 1480
c5aa993b
JM
1481 /* Number of baseclasses. */
1482 int nbaseclasses;
c906108c 1483
c5aa993b
JM
1484 /* Set if the accesibility of one of the fields is not public. */
1485 int non_public_fields;
c906108c 1486
c5aa993b
JM
1487 /* Member function fieldlist array, contains name of possibly overloaded
1488 member function, number of overloaded member functions and a pointer
1489 to the head of the member function field chain. */
52059ffd 1490 struct fnfieldlist *fnfieldlists;
c906108c 1491
c5aa993b
JM
1492 /* Number of entries in the fnfieldlists array. */
1493 int nfnfields;
98751a41
JK
1494
1495 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1496 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1497 struct typedef_field_list *typedef_field_list;
98751a41 1498 unsigned typedef_field_list_count;
c5aa993b 1499 };
c906108c 1500
10b3939b
DJ
1501/* One item on the queue of compilation units to read in full symbols
1502 for. */
1503struct dwarf2_queue_item
1504{
1505 struct dwarf2_per_cu_data *per_cu;
95554aad 1506 enum language pretend_language;
10b3939b
DJ
1507 struct dwarf2_queue_item *next;
1508};
1509
1510/* The current queue. */
1511static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1512
ae038cb0
DJ
1513/* Loaded secondary compilation units are kept in memory until they
1514 have not been referenced for the processing of this many
1515 compilation units. Set this to zero to disable caching. Cache
1516 sizes of up to at least twenty will improve startup time for
1517 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1518static int dwarf_max_cache_age = 5;
920d2a44 1519static void
b4f54984
DE
1520show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1521 struct cmd_list_element *c, const char *value)
920d2a44 1522{
3e43a32a 1523 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1524 "DWARF compilation units is %s.\n"),
920d2a44
AC
1525 value);
1526}
4390d890 1527\f
c906108c
SS
1528/* local function prototypes */
1529
a32a8923
DE
1530static const char *get_section_name (const struct dwarf2_section_info *);
1531
1532static const char *get_section_file_name (const struct dwarf2_section_info *);
1533
918dd910
JK
1534static void dwarf2_find_base_address (struct die_info *die,
1535 struct dwarf2_cu *cu);
1536
0018ea6f
DE
1537static struct partial_symtab *create_partial_symtab
1538 (struct dwarf2_per_cu_data *per_cu, const char *name);
1539
f1902523
JK
1540static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct die_info *type_unit_die,
1543 int has_children, void *data);
1544
c67a9c90 1545static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1546
72bf9492
DJ
1547static void scan_partial_symbols (struct partial_die_info *,
1548 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1549 int, struct dwarf2_cu *);
c906108c 1550
72bf9492
DJ
1551static void add_partial_symbol (struct partial_die_info *,
1552 struct dwarf2_cu *);
63d06c5c 1553
72bf9492
DJ
1554static void add_partial_namespace (struct partial_die_info *pdi,
1555 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1556 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1557
5d7cb8df 1558static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1559 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1560 struct dwarf2_cu *cu);
1561
72bf9492
DJ
1562static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1563 struct dwarf2_cu *cu);
91c24f0a 1564
bc30ff58
JB
1565static void add_partial_subprogram (struct partial_die_info *pdi,
1566 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1567 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1568
257e7a09
YQ
1569static void dwarf2_read_symtab (struct partial_symtab *,
1570 struct objfile *);
c906108c 1571
a14ed312 1572static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1573
433df2d4
DE
1574static struct abbrev_info *abbrev_table_lookup_abbrev
1575 (const struct abbrev_table *, unsigned int);
1576
1577static struct abbrev_table *abbrev_table_read_table
1578 (struct dwarf2_section_info *, sect_offset);
1579
1580static void abbrev_table_free (struct abbrev_table *);
1581
f4dc4d17
DE
1582static void abbrev_table_free_cleanup (void *);
1583
dee91e82
DE
1584static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1585 struct dwarf2_section_info *);
c906108c 1586
f3dd6933 1587static void dwarf2_free_abbrev_table (void *);
c906108c 1588
d521ce57 1589static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1590
dee91e82 1591static struct partial_die_info *load_partial_dies
d521ce57 1592 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1593
d521ce57
TT
1594static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1595 struct partial_die_info *,
1596 struct abbrev_info *,
1597 unsigned int,
1598 const gdb_byte *);
c906108c 1599
36586728 1600static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1601 struct dwarf2_cu *);
72bf9492
DJ
1602
1603static void fixup_partial_die (struct partial_die_info *,
1604 struct dwarf2_cu *);
1605
d521ce57
TT
1606static const gdb_byte *read_attribute (const struct die_reader_specs *,
1607 struct attribute *, struct attr_abbrev *,
1608 const gdb_byte *);
a8329558 1609
a1855c1d 1610static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1611
a1855c1d 1612static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1613
a1855c1d 1614static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1615
a1855c1d 1616static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1617
a1855c1d 1618static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1619
d521ce57 1620static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1621 unsigned int *);
c906108c 1622
d521ce57 1623static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1624
1625static LONGEST read_checked_initial_length_and_offset
d521ce57 1626 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1627 unsigned int *, unsigned int *);
613e1657 1628
d521ce57
TT
1629static LONGEST read_offset (bfd *, const gdb_byte *,
1630 const struct comp_unit_head *,
c764a876
DE
1631 unsigned int *);
1632
d521ce57 1633static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1634
f4dc4d17
DE
1635static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1636 sect_offset);
1637
d521ce57 1638static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1639
d521ce57 1640static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1641
d521ce57
TT
1642static const char *read_indirect_string (bfd *, const gdb_byte *,
1643 const struct comp_unit_head *,
1644 unsigned int *);
4bdf3d34 1645
43988095
JK
1646static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1647 const struct comp_unit_head *,
1648 unsigned int *);
36586728 1649
43988095 1650static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1651
d521ce57 1652static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1653
d521ce57
TT
1654static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1655 const gdb_byte *,
3019eac3
DE
1656 unsigned int *);
1657
d521ce57 1658static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1659 ULONGEST str_index);
3019eac3 1660
e142c38c 1661static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1662
e142c38c
DJ
1663static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1664 struct dwarf2_cu *);
c906108c 1665
348e048f 1666static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1667 unsigned int);
348e048f 1668
7d45c7c3
KB
1669static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1670 struct dwarf2_cu *cu);
1671
05cf31d1
JB
1672static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1673 struct dwarf2_cu *cu);
1674
e142c38c 1675static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1676
e142c38c 1677static struct die_info *die_specification (struct die_info *die,
f2f0e013 1678 struct dwarf2_cu **);
63d06c5c 1679
9c541725 1680static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1681 struct dwarf2_cu *cu);
debd256d 1682
f3f5162e 1683static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1684 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1685 CORE_ADDR, int decode_mapping);
c906108c 1686
4d663531 1687static void dwarf2_start_subfile (const char *, const char *);
c906108c 1688
43f3e411
DE
1689static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1690 const char *, const char *,
1691 CORE_ADDR);
f4dc4d17 1692
a14ed312 1693static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1694 struct dwarf2_cu *);
c906108c 1695
34eaf542
TT
1696static struct symbol *new_symbol_full (struct die_info *, struct type *,
1697 struct dwarf2_cu *, struct symbol *);
1698
ff39bb5e 1699static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1700 struct dwarf2_cu *);
c906108c 1701
ff39bb5e 1702static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1703 struct type *type,
1704 const char *name,
1705 struct obstack *obstack,
12df843f 1706 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1707 const gdb_byte **bytes,
98bfdba5 1708 struct dwarf2_locexpr_baton **baton);
2df3850c 1709
e7c27a73 1710static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1711
b4ba55a1
JB
1712static int need_gnat_info (struct dwarf2_cu *);
1713
3e43a32a
MS
1714static struct type *die_descriptive_type (struct die_info *,
1715 struct dwarf2_cu *);
b4ba55a1
JB
1716
1717static void set_descriptive_type (struct type *, struct die_info *,
1718 struct dwarf2_cu *);
1719
e7c27a73
DJ
1720static struct type *die_containing_type (struct die_info *,
1721 struct dwarf2_cu *);
c906108c 1722
ff39bb5e 1723static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1724 struct dwarf2_cu *);
c906108c 1725
f792889a 1726static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1727
673bfd45
DE
1728static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1729
0d5cff50 1730static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1731
6e70227d 1732static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1733 const char *suffix, int physname,
1734 struct dwarf2_cu *cu);
63d06c5c 1735
e7c27a73 1736static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1737
348e048f
DE
1738static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1739
e7c27a73 1740static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1741
e7c27a73 1742static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1743
96408a79
SA
1744static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1745
ff013f42
JK
1746static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1747 struct dwarf2_cu *, struct partial_symtab *);
1748
3a2b436a 1749/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1750 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1751enum pc_bounds_kind
1752{
e385593e 1753 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1754 PC_BOUNDS_NOT_PRESENT,
1755
e385593e
JK
1756 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1757 were present but they do not form a valid range of PC addresses. */
1758 PC_BOUNDS_INVALID,
1759
3a2b436a
JK
1760 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1761 PC_BOUNDS_RANGES,
1762
1763 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1764 PC_BOUNDS_HIGH_LOW,
1765};
1766
1767static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1768 CORE_ADDR *, CORE_ADDR *,
1769 struct dwarf2_cu *,
1770 struct partial_symtab *);
c906108c 1771
fae299cd
DC
1772static void get_scope_pc_bounds (struct die_info *,
1773 CORE_ADDR *, CORE_ADDR *,
1774 struct dwarf2_cu *);
1775
801e3a5b
JB
1776static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1777 CORE_ADDR, struct dwarf2_cu *);
1778
a14ed312 1779static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1780 struct dwarf2_cu *);
c906108c 1781
a14ed312 1782static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1783 struct type *, struct dwarf2_cu *);
c906108c 1784
a14ed312 1785static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1786 struct die_info *, struct type *,
e7c27a73 1787 struct dwarf2_cu *);
c906108c 1788
a14ed312 1789static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1790 struct type *,
1791 struct dwarf2_cu *);
c906108c 1792
134d01f1 1793static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1794
e7c27a73 1795static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1796
e7c27a73 1797static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1798
5d7cb8df
JK
1799static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1800
22cee43f
PMR
1801static struct using_direct **using_directives (enum language);
1802
27aa8d6a
SW
1803static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1804
74921315
KS
1805static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1806
f55ee35c
JK
1807static struct type *read_module_type (struct die_info *die,
1808 struct dwarf2_cu *cu);
1809
38d518c9 1810static const char *namespace_name (struct die_info *die,
e142c38c 1811 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1812
134d01f1 1813static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1814
e7c27a73 1815static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1816
6e70227d 1817static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1818 struct dwarf2_cu *);
1819
bf6af496 1820static struct die_info *read_die_and_siblings_1
d521ce57 1821 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1822 struct die_info *);
639d11d3 1823
dee91e82 1824static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1825 const gdb_byte *info_ptr,
1826 const gdb_byte **new_info_ptr,
639d11d3
DC
1827 struct die_info *parent);
1828
d521ce57
TT
1829static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1830 struct die_info **, const gdb_byte *,
1831 int *, int);
3019eac3 1832
d521ce57
TT
1833static const gdb_byte *read_full_die (const struct die_reader_specs *,
1834 struct die_info **, const gdb_byte *,
1835 int *);
93311388 1836
e7c27a73 1837static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1838
15d034d0
TT
1839static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1840 struct obstack *);
71c25dea 1841
15d034d0 1842static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1843
15d034d0 1844static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1845 struct die_info *die,
1846 struct dwarf2_cu *cu);
1847
ca69b9e6
DE
1848static const char *dwarf2_physname (const char *name, struct die_info *die,
1849 struct dwarf2_cu *cu);
1850
e142c38c 1851static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1852 struct dwarf2_cu **);
9219021c 1853
f39c6ffd 1854static const char *dwarf_tag_name (unsigned int);
c906108c 1855
f39c6ffd 1856static const char *dwarf_attr_name (unsigned int);
c906108c 1857
f39c6ffd 1858static const char *dwarf_form_name (unsigned int);
c906108c 1859
a121b7c1 1860static const char *dwarf_bool_name (unsigned int);
c906108c 1861
f39c6ffd 1862static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1863
f9aca02d 1864static struct die_info *sibling_die (struct die_info *);
c906108c 1865
d97bc12b
DE
1866static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1867
1868static void dump_die_for_error (struct die_info *);
1869
1870static void dump_die_1 (struct ui_file *, int level, int max_level,
1871 struct die_info *);
c906108c 1872
d97bc12b 1873/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1874
51545339 1875static void store_in_ref_table (struct die_info *,
10b3939b 1876 struct dwarf2_cu *);
c906108c 1877
ff39bb5e 1878static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1879
ff39bb5e 1880static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1881
348e048f 1882static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1883 const struct attribute *,
348e048f
DE
1884 struct dwarf2_cu **);
1885
10b3939b 1886static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1887 const struct attribute *,
f2f0e013 1888 struct dwarf2_cu **);
c906108c 1889
348e048f 1890static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1891 const struct attribute *,
348e048f
DE
1892 struct dwarf2_cu **);
1893
ac9ec31b
DE
1894static struct type *get_signatured_type (struct die_info *, ULONGEST,
1895 struct dwarf2_cu *);
1896
1897static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1898 const struct attribute *,
ac9ec31b
DE
1899 struct dwarf2_cu *);
1900
e5fe5e75 1901static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1902
52dc124a 1903static void read_signatured_type (struct signatured_type *);
348e048f 1904
63e43d3a
PMR
1905static int attr_to_dynamic_prop (const struct attribute *attr,
1906 struct die_info *die, struct dwarf2_cu *cu,
1907 struct dynamic_prop *prop);
1908
c906108c
SS
1909/* memory allocation interface */
1910
7b5a2f43 1911static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1912
b60c80d6 1913static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1914
43f3e411 1915static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1916
6e5a29e1 1917static int attr_form_is_block (const struct attribute *);
8e19ed76 1918
6e5a29e1 1919static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1920
6e5a29e1 1921static int attr_form_is_constant (const struct attribute *);
3690dd37 1922
6e5a29e1 1923static int attr_form_is_ref (const struct attribute *);
7771576e 1924
8cf6f0b1
TT
1925static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1926 struct dwarf2_loclist_baton *baton,
ff39bb5e 1927 const struct attribute *attr);
8cf6f0b1 1928
ff39bb5e 1929static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1930 struct symbol *sym,
f1e6e072
TT
1931 struct dwarf2_cu *cu,
1932 int is_block);
4c2df51b 1933
d521ce57
TT
1934static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1935 const gdb_byte *info_ptr,
1936 struct abbrev_info *abbrev);
4bb7a0a7 1937
72bf9492
DJ
1938static void free_stack_comp_unit (void *);
1939
72bf9492
DJ
1940static hashval_t partial_die_hash (const void *item);
1941
1942static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1943
ae038cb0 1944static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1945 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1946
9816fde3 1947static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1948 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1949
1950static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1951 struct die_info *comp_unit_die,
1952 enum language pretend_language);
93311388 1953
68dc6402 1954static void free_heap_comp_unit (void *);
ae038cb0
DJ
1955
1956static void free_cached_comp_units (void *);
1957
1958static void age_cached_comp_units (void);
1959
dee91e82 1960static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1961
f792889a
DJ
1962static struct type *set_die_type (struct die_info *, struct type *,
1963 struct dwarf2_cu *);
1c379e20 1964
ae038cb0
DJ
1965static void create_all_comp_units (struct objfile *);
1966
0e50663e 1967static int create_all_type_units (struct objfile *);
1fd400ff 1968
95554aad
TT
1969static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1970 enum language);
10b3939b 1971
95554aad
TT
1972static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1973 enum language);
10b3939b 1974
f4dc4d17
DE
1975static void process_full_type_unit (struct dwarf2_per_cu_data *,
1976 enum language);
1977
10b3939b
DJ
1978static void dwarf2_add_dependence (struct dwarf2_cu *,
1979 struct dwarf2_per_cu_data *);
1980
ae038cb0
DJ
1981static void dwarf2_mark (struct dwarf2_cu *);
1982
1983static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1984
b64f50a1 1985static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1986 struct dwarf2_per_cu_data *);
673bfd45 1987
f792889a 1988static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1989
9291a0cd
TT
1990static void dwarf2_release_queue (void *dummy);
1991
95554aad
TT
1992static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1993 enum language pretend_language);
1994
a0f42c21 1995static void process_queue (void);
9291a0cd 1996
d721ba37
PA
1997/* The return type of find_file_and_directory. Note, the enclosed
1998 string pointers are only valid while this object is valid. */
1999
2000struct file_and_directory
2001{
2002 /* The filename. This is never NULL. */
2003 const char *name;
2004
2005 /* The compilation directory. NULL if not known. If we needed to
2006 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2007 points directly to the DW_AT_comp_dir string attribute owned by
2008 the obstack that owns the DIE. */
2009 const char *comp_dir;
2010
2011 /* If we needed to build a new string for comp_dir, this is what
2012 owns the storage. */
2013 std::string comp_dir_storage;
2014};
2015
2016static file_and_directory find_file_and_directory (struct die_info *die,
2017 struct dwarf2_cu *cu);
9291a0cd
TT
2018
2019static char *file_full_name (int file, struct line_header *lh,
2020 const char *comp_dir);
2021
43988095
JK
2022/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2023enum class rcuh_kind { COMPILE, TYPE };
2024
d521ce57 2025static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2026 (struct comp_unit_head *header,
2027 struct dwarf2_section_info *section,
d521ce57 2028 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2029 rcuh_kind section_kind);
36586728 2030
fd820528 2031static void init_cutu_and_read_dies
f4dc4d17
DE
2032 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2033 int use_existing_cu, int keep,
3019eac3
DE
2034 die_reader_func_ftype *die_reader_func, void *data);
2035
dee91e82
DE
2036static void init_cutu_and_read_dies_simple
2037 (struct dwarf2_per_cu_data *this_cu,
2038 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2039
673bfd45 2040static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2041
3019eac3
DE
2042static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2043
57d63ce2
DE
2044static struct dwo_unit *lookup_dwo_unit_in_dwp
2045 (struct dwp_file *dwp_file, const char *comp_dir,
2046 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2047
2048static struct dwp_file *get_dwp_file (void);
2049
3019eac3 2050static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2051 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2052
2053static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2054 (struct signatured_type *, const char *, const char *);
3019eac3 2055
89e63ee4
DE
2056static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2057
3019eac3
DE
2058static void free_dwo_file_cleanup (void *);
2059
95554aad
TT
2060static void process_cu_includes (void);
2061
1b80a9fa 2062static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2063
2064static void free_line_header_voidp (void *arg);
4390d890
DE
2065\f
2066/* Various complaints about symbol reading that don't abort the process. */
2067
2068static void
2069dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2070{
2071 complaint (&symfile_complaints,
2072 _("statement list doesn't fit in .debug_line section"));
2073}
2074
2075static void
2076dwarf2_debug_line_missing_file_complaint (void)
2077{
2078 complaint (&symfile_complaints,
2079 _(".debug_line section has line data without a file"));
2080}
2081
2082static void
2083dwarf2_debug_line_missing_end_sequence_complaint (void)
2084{
2085 complaint (&symfile_complaints,
2086 _(".debug_line section has line "
2087 "program sequence without an end"));
2088}
2089
2090static void
2091dwarf2_complex_location_expr_complaint (void)
2092{
2093 complaint (&symfile_complaints, _("location expression too complex"));
2094}
2095
2096static void
2097dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2098 int arg3)
2099{
2100 complaint (&symfile_complaints,
2101 _("const value length mismatch for '%s', got %d, expected %d"),
2102 arg1, arg2, arg3);
2103}
2104
2105static void
2106dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2107{
2108 complaint (&symfile_complaints,
2109 _("debug info runs off end of %s section"
2110 " [in module %s]"),
a32a8923
DE
2111 get_section_name (section),
2112 get_section_file_name (section));
4390d890 2113}
1b80a9fa 2114
4390d890
DE
2115static void
2116dwarf2_macro_malformed_definition_complaint (const char *arg1)
2117{
2118 complaint (&symfile_complaints,
2119 _("macro debug info contains a "
2120 "malformed macro definition:\n`%s'"),
2121 arg1);
2122}
2123
2124static void
2125dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2126{
2127 complaint (&symfile_complaints,
2128 _("invalid attribute class or form for '%s' in '%s'"),
2129 arg1, arg2);
2130}
527f3840
JK
2131
2132/* Hash function for line_header_hash. */
2133
2134static hashval_t
2135line_header_hash (const struct line_header *ofs)
2136{
9c541725 2137 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2138}
2139
2140/* Hash function for htab_create_alloc_ex for line_header_hash. */
2141
2142static hashval_t
2143line_header_hash_voidp (const void *item)
2144{
9a3c8263 2145 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2146
2147 return line_header_hash (ofs);
2148}
2149
2150/* Equality function for line_header_hash. */
2151
2152static int
2153line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2154{
9a3c8263
SM
2155 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2156 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2157
9c541725 2158 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2159 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2160}
2161
4390d890 2162\f
9291a0cd
TT
2163#if WORDS_BIGENDIAN
2164
2165/* Convert VALUE between big- and little-endian. */
2166static offset_type
2167byte_swap (offset_type value)
2168{
2169 offset_type result;
2170
2171 result = (value & 0xff) << 24;
2172 result |= (value & 0xff00) << 8;
2173 result |= (value & 0xff0000) >> 8;
2174 result |= (value & 0xff000000) >> 24;
2175 return result;
2176}
2177
2178#define MAYBE_SWAP(V) byte_swap (V)
2179
2180#else
bc8f2430 2181#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2182#endif /* WORDS_BIGENDIAN */
2183
31aa7e4e
JB
2184/* Read the given attribute value as an address, taking the attribute's
2185 form into account. */
2186
2187static CORE_ADDR
2188attr_value_as_address (struct attribute *attr)
2189{
2190 CORE_ADDR addr;
2191
2192 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2193 {
2194 /* Aside from a few clearly defined exceptions, attributes that
2195 contain an address must always be in DW_FORM_addr form.
2196 Unfortunately, some compilers happen to be violating this
2197 requirement by encoding addresses using other forms, such
2198 as DW_FORM_data4 for example. For those broken compilers,
2199 we try to do our best, without any guarantee of success,
2200 to interpret the address correctly. It would also be nice
2201 to generate a complaint, but that would require us to maintain
2202 a list of legitimate cases where a non-address form is allowed,
2203 as well as update callers to pass in at least the CU's DWARF
2204 version. This is more overhead than what we're willing to
2205 expand for a pretty rare case. */
2206 addr = DW_UNSND (attr);
2207 }
2208 else
2209 addr = DW_ADDR (attr);
2210
2211 return addr;
2212}
2213
9291a0cd
TT
2214/* The suffix for an index file. */
2215#define INDEX_SUFFIX ".gdb-index"
2216
330cdd98
PA
2217/* See declaration. */
2218
2219dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2220 const dwarf2_debug_sections *names)
2221 : objfile (objfile_)
2222{
2223 if (names == NULL)
2224 names = &dwarf2_elf_names;
2225
2226 bfd *obfd = objfile->obfd;
2227
2228 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2229 locate_sections (obfd, sec, *names);
2230}
2231
2232dwarf2_per_objfile::~dwarf2_per_objfile ()
2233{
2234 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2235 free_cached_comp_units ();
2236
2237 if (quick_file_names_table)
2238 htab_delete (quick_file_names_table);
2239
2240 if (line_header_hash)
2241 htab_delete (line_header_hash);
2242
2243 /* Everything else should be on the objfile obstack. */
2244}
2245
2246/* See declaration. */
2247
2248void
2249dwarf2_per_objfile::free_cached_comp_units ()
2250{
2251 dwarf2_per_cu_data *per_cu = read_in_chain;
2252 dwarf2_per_cu_data **last_chain = &read_in_chain;
2253 while (per_cu != NULL)
2254 {
2255 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2256
2257 free_heap_comp_unit (per_cu->cu);
2258 *last_chain = next_cu;
2259 per_cu = next_cu;
2260 }
2261}
2262
c906108c 2263/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2264 information and return true if we have enough to do something.
2265 NAMES points to the dwarf2 section names, or is NULL if the standard
2266 ELF names are used. */
c906108c
SS
2267
2268int
251d32d9
TG
2269dwarf2_has_info (struct objfile *objfile,
2270 const struct dwarf2_debug_sections *names)
c906108c 2271{
9a3c8263
SM
2272 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2273 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2274 if (!dwarf2_per_objfile)
2275 {
2276 /* Initialize per-objfile state. */
2277 struct dwarf2_per_objfile *data
8d749320 2278 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2279
330cdd98
PA
2280 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2281 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2282 }
73869dc2 2283 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2284 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2285 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2286 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2287}
2288
2289/* Return the containing section of virtual section SECTION. */
2290
2291static struct dwarf2_section_info *
2292get_containing_section (const struct dwarf2_section_info *section)
2293{
2294 gdb_assert (section->is_virtual);
2295 return section->s.containing_section;
c906108c
SS
2296}
2297
a32a8923
DE
2298/* Return the bfd owner of SECTION. */
2299
2300static struct bfd *
2301get_section_bfd_owner (const struct dwarf2_section_info *section)
2302{
73869dc2
DE
2303 if (section->is_virtual)
2304 {
2305 section = get_containing_section (section);
2306 gdb_assert (!section->is_virtual);
2307 }
049412e3 2308 return section->s.section->owner;
a32a8923
DE
2309}
2310
2311/* Return the bfd section of SECTION.
2312 Returns NULL if the section is not present. */
2313
2314static asection *
2315get_section_bfd_section (const struct dwarf2_section_info *section)
2316{
73869dc2
DE
2317 if (section->is_virtual)
2318 {
2319 section = get_containing_section (section);
2320 gdb_assert (!section->is_virtual);
2321 }
049412e3 2322 return section->s.section;
a32a8923
DE
2323}
2324
2325/* Return the name of SECTION. */
2326
2327static const char *
2328get_section_name (const struct dwarf2_section_info *section)
2329{
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 gdb_assert (sectp != NULL);
2333 return bfd_section_name (get_section_bfd_owner (section), sectp);
2334}
2335
2336/* Return the name of the file SECTION is in. */
2337
2338static const char *
2339get_section_file_name (const struct dwarf2_section_info *section)
2340{
2341 bfd *abfd = get_section_bfd_owner (section);
2342
2343 return bfd_get_filename (abfd);
2344}
2345
2346/* Return the id of SECTION.
2347 Returns 0 if SECTION doesn't exist. */
2348
2349static int
2350get_section_id (const struct dwarf2_section_info *section)
2351{
2352 asection *sectp = get_section_bfd_section (section);
2353
2354 if (sectp == NULL)
2355 return 0;
2356 return sectp->id;
2357}
2358
2359/* Return the flags of SECTION.
73869dc2 2360 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2361
2362static int
2363get_section_flags (const struct dwarf2_section_info *section)
2364{
2365 asection *sectp = get_section_bfd_section (section);
2366
2367 gdb_assert (sectp != NULL);
2368 return bfd_get_section_flags (sectp->owner, sectp);
2369}
2370
251d32d9
TG
2371/* When loading sections, we look either for uncompressed section or for
2372 compressed section names. */
233a11ab
CS
2373
2374static int
251d32d9
TG
2375section_is_p (const char *section_name,
2376 const struct dwarf2_section_names *names)
233a11ab 2377{
251d32d9
TG
2378 if (names->normal != NULL
2379 && strcmp (section_name, names->normal) == 0)
2380 return 1;
2381 if (names->compressed != NULL
2382 && strcmp (section_name, names->compressed) == 0)
2383 return 1;
2384 return 0;
233a11ab
CS
2385}
2386
330cdd98 2387/* See declaration. */
c906108c 2388
330cdd98
PA
2389void
2390dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2391 const dwarf2_debug_sections &names)
c906108c 2392{
dc7650b8 2393 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2394
dc7650b8
JK
2395 if ((aflag & SEC_HAS_CONTENTS) == 0)
2396 {
2397 }
330cdd98 2398 else if (section_is_p (sectp->name, &names.info))
c906108c 2399 {
330cdd98
PA
2400 this->info.s.section = sectp;
2401 this->info.size = bfd_get_section_size (sectp);
c906108c 2402 }
330cdd98 2403 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2404 {
330cdd98
PA
2405 this->abbrev.s.section = sectp;
2406 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2407 }
330cdd98 2408 else if (section_is_p (sectp->name, &names.line))
c906108c 2409 {
330cdd98
PA
2410 this->line.s.section = sectp;
2411 this->line.size = bfd_get_section_size (sectp);
c906108c 2412 }
330cdd98 2413 else if (section_is_p (sectp->name, &names.loc))
c906108c 2414 {
330cdd98
PA
2415 this->loc.s.section = sectp;
2416 this->loc.size = bfd_get_section_size (sectp);
c906108c 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.loclists))
43988095 2419 {
330cdd98
PA
2420 this->loclists.s.section = sectp;
2421 this->loclists.size = bfd_get_section_size (sectp);
43988095 2422 }
330cdd98 2423 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2424 {
330cdd98
PA
2425 this->macinfo.s.section = sectp;
2426 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2427 }
330cdd98 2428 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2429 {
330cdd98
PA
2430 this->macro.s.section = sectp;
2431 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2432 }
330cdd98 2433 else if (section_is_p (sectp->name, &names.str))
c906108c 2434 {
330cdd98
PA
2435 this->str.s.section = sectp;
2436 this->str.size = bfd_get_section_size (sectp);
c906108c 2437 }
330cdd98 2438 else if (section_is_p (sectp->name, &names.line_str))
43988095 2439 {
330cdd98
PA
2440 this->line_str.s.section = sectp;
2441 this->line_str.size = bfd_get_section_size (sectp);
43988095 2442 }
330cdd98 2443 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2444 {
330cdd98
PA
2445 this->addr.s.section = sectp;
2446 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2447 }
330cdd98 2448 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2449 {
330cdd98
PA
2450 this->frame.s.section = sectp;
2451 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2452 }
330cdd98 2453 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2454 {
330cdd98
PA
2455 this->eh_frame.s.section = sectp;
2456 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2457 }
330cdd98 2458 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2459 {
330cdd98
PA
2460 this->ranges.s.section = sectp;
2461 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2462 }
330cdd98 2463 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2464 {
330cdd98
PA
2465 this->rnglists.s.section = sectp;
2466 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2467 }
330cdd98 2468 else if (section_is_p (sectp->name, &names.types))
348e048f 2469 {
8b70b953
TT
2470 struct dwarf2_section_info type_section;
2471
2472 memset (&type_section, 0, sizeof (type_section));
049412e3 2473 type_section.s.section = sectp;
8b70b953
TT
2474 type_section.size = bfd_get_section_size (sectp);
2475
330cdd98 2476 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2477 &type_section);
348e048f 2478 }
330cdd98 2479 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2480 {
330cdd98
PA
2481 this->gdb_index.s.section = sectp;
2482 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2483 }
dce234bc 2484
b4e1fd61 2485 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2486 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2487 this->has_section_at_zero = true;
c906108c
SS
2488}
2489
fceca515
DE
2490/* A helper function that decides whether a section is empty,
2491 or not present. */
9e0ac564
TT
2492
2493static int
19ac8c2e 2494dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2495{
73869dc2
DE
2496 if (section->is_virtual)
2497 return section->size == 0;
049412e3 2498 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2499}
2500
3019eac3
DE
2501/* Read the contents of the section INFO.
2502 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2503 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2504 of the DWO file.
dce234bc 2505 If the section is compressed, uncompress it before returning. */
c906108c 2506
dce234bc
PP
2507static void
2508dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2509{
a32a8923 2510 asection *sectp;
3019eac3 2511 bfd *abfd;
dce234bc 2512 gdb_byte *buf, *retbuf;
c906108c 2513
be391dca
TT
2514 if (info->readin)
2515 return;
dce234bc 2516 info->buffer = NULL;
be391dca 2517 info->readin = 1;
188dd5d6 2518
9e0ac564 2519 if (dwarf2_section_empty_p (info))
dce234bc 2520 return;
c906108c 2521
a32a8923 2522 sectp = get_section_bfd_section (info);
3019eac3 2523
73869dc2
DE
2524 /* If this is a virtual section we need to read in the real one first. */
2525 if (info->is_virtual)
2526 {
2527 struct dwarf2_section_info *containing_section =
2528 get_containing_section (info);
2529
2530 gdb_assert (sectp != NULL);
2531 if ((sectp->flags & SEC_RELOC) != 0)
2532 {
2533 error (_("Dwarf Error: DWP format V2 with relocations is not"
2534 " supported in section %s [in module %s]"),
2535 get_section_name (info), get_section_file_name (info));
2536 }
2537 dwarf2_read_section (objfile, containing_section);
2538 /* Other code should have already caught virtual sections that don't
2539 fit. */
2540 gdb_assert (info->virtual_offset + info->size
2541 <= containing_section->size);
2542 /* If the real section is empty or there was a problem reading the
2543 section we shouldn't get here. */
2544 gdb_assert (containing_section->buffer != NULL);
2545 info->buffer = containing_section->buffer + info->virtual_offset;
2546 return;
2547 }
2548
4bf44c1c
TT
2549 /* If the section has relocations, we must read it ourselves.
2550 Otherwise we attach it to the BFD. */
2551 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2552 {
d521ce57 2553 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2554 return;
dce234bc 2555 }
dce234bc 2556
224c3ddb 2557 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2558 info->buffer = buf;
dce234bc
PP
2559
2560 /* When debugging .o files, we may need to apply relocations; see
2561 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2562 We never compress sections in .o files, so we only need to
2563 try this when the section is not compressed. */
ac8035ab 2564 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2565 if (retbuf != NULL)
2566 {
2567 info->buffer = retbuf;
2568 return;
2569 }
2570
a32a8923
DE
2571 abfd = get_section_bfd_owner (info);
2572 gdb_assert (abfd != NULL);
2573
dce234bc
PP
2574 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2575 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2576 {
2577 error (_("Dwarf Error: Can't read DWARF data"
2578 " in section %s [in module %s]"),
2579 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2580 }
dce234bc
PP
2581}
2582
9e0ac564
TT
2583/* A helper function that returns the size of a section in a safe way.
2584 If you are positive that the section has been read before using the
2585 size, then it is safe to refer to the dwarf2_section_info object's
2586 "size" field directly. In other cases, you must call this
2587 function, because for compressed sections the size field is not set
2588 correctly until the section has been read. */
2589
2590static bfd_size_type
2591dwarf2_section_size (struct objfile *objfile,
2592 struct dwarf2_section_info *info)
2593{
2594 if (!info->readin)
2595 dwarf2_read_section (objfile, info);
2596 return info->size;
2597}
2598
dce234bc 2599/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2600 SECTION_NAME. */
af34e669 2601
dce234bc 2602void
3017a003
TG
2603dwarf2_get_section_info (struct objfile *objfile,
2604 enum dwarf2_section_enum sect,
d521ce57 2605 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2606 bfd_size_type *sizep)
2607{
2608 struct dwarf2_per_objfile *data
9a3c8263
SM
2609 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2610 dwarf2_objfile_data_key);
dce234bc 2611 struct dwarf2_section_info *info;
a3b2a86b
TT
2612
2613 /* We may see an objfile without any DWARF, in which case we just
2614 return nothing. */
2615 if (data == NULL)
2616 {
2617 *sectp = NULL;
2618 *bufp = NULL;
2619 *sizep = 0;
2620 return;
2621 }
3017a003
TG
2622 switch (sect)
2623 {
2624 case DWARF2_DEBUG_FRAME:
2625 info = &data->frame;
2626 break;
2627 case DWARF2_EH_FRAME:
2628 info = &data->eh_frame;
2629 break;
2630 default:
2631 gdb_assert_not_reached ("unexpected section");
2632 }
dce234bc 2633
9e0ac564 2634 dwarf2_read_section (objfile, info);
dce234bc 2635
a32a8923 2636 *sectp = get_section_bfd_section (info);
dce234bc
PP
2637 *bufp = info->buffer;
2638 *sizep = info->size;
2639}
2640
36586728
TT
2641/* A helper function to find the sections for a .dwz file. */
2642
2643static void
2644locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2645{
9a3c8263 2646 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2647
2648 /* Note that we only support the standard ELF names, because .dwz
2649 is ELF-only (at the time of writing). */
2650 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2651 {
049412e3 2652 dwz_file->abbrev.s.section = sectp;
36586728
TT
2653 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2654 }
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2656 {
049412e3 2657 dwz_file->info.s.section = sectp;
36586728
TT
2658 dwz_file->info.size = bfd_get_section_size (sectp);
2659 }
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2661 {
049412e3 2662 dwz_file->str.s.section = sectp;
36586728
TT
2663 dwz_file->str.size = bfd_get_section_size (sectp);
2664 }
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2666 {
049412e3 2667 dwz_file->line.s.section = sectp;
36586728
TT
2668 dwz_file->line.size = bfd_get_section_size (sectp);
2669 }
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2671 {
049412e3 2672 dwz_file->macro.s.section = sectp;
36586728
TT
2673 dwz_file->macro.size = bfd_get_section_size (sectp);
2674 }
2ec9a5e0
TT
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2676 {
049412e3 2677 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2678 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2679 }
36586728
TT
2680}
2681
4db1a1dc
TT
2682/* Open the separate '.dwz' debug file, if needed. Return NULL if
2683 there is no .gnu_debugaltlink section in the file. Error if there
2684 is such a section but the file cannot be found. */
36586728
TT
2685
2686static struct dwz_file *
2687dwarf2_get_dwz_file (void)
2688{
4db1a1dc 2689 char *data;
36586728
TT
2690 struct cleanup *cleanup;
2691 const char *filename;
2692 struct dwz_file *result;
acd13123 2693 bfd_size_type buildid_len_arg;
dc294be5
TT
2694 size_t buildid_len;
2695 bfd_byte *buildid;
36586728
TT
2696
2697 if (dwarf2_per_objfile->dwz_file != NULL)
2698 return dwarf2_per_objfile->dwz_file;
2699
4db1a1dc
TT
2700 bfd_set_error (bfd_error_no_error);
2701 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2702 &buildid_len_arg, &buildid);
4db1a1dc
TT
2703 if (data == NULL)
2704 {
2705 if (bfd_get_error () == bfd_error_no_error)
2706 return NULL;
2707 error (_("could not read '.gnu_debugaltlink' section: %s"),
2708 bfd_errmsg (bfd_get_error ()));
2709 }
36586728 2710 cleanup = make_cleanup (xfree, data);
dc294be5 2711 make_cleanup (xfree, buildid);
36586728 2712
acd13123
TT
2713 buildid_len = (size_t) buildid_len_arg;
2714
f9d83a0b 2715 filename = (const char *) data;
d721ba37
PA
2716
2717 std::string abs_storage;
36586728
TT
2718 if (!IS_ABSOLUTE_PATH (filename))
2719 {
14278e1f
TT
2720 gdb::unique_xmalloc_ptr<char> abs
2721 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2722
14278e1f 2723 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2724 filename = abs_storage.c_str ();
36586728
TT
2725 }
2726
dc294be5
TT
2727 /* First try the file name given in the section. If that doesn't
2728 work, try to use the build-id instead. */
192b62ce 2729 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2730 if (dwz_bfd != NULL)
36586728 2731 {
192b62ce
TT
2732 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2733 dwz_bfd.release ();
36586728
TT
2734 }
2735
dc294be5
TT
2736 if (dwz_bfd == NULL)
2737 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2738
2739 if (dwz_bfd == NULL)
2740 error (_("could not find '.gnu_debugaltlink' file for %s"),
2741 objfile_name (dwarf2_per_objfile->objfile));
2742
36586728
TT
2743 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2744 struct dwz_file);
192b62ce 2745 result->dwz_bfd = dwz_bfd.release ();
36586728 2746
192b62ce 2747 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2748
2749 do_cleanups (cleanup);
2750
192b62ce 2751 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2752 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2753 return result;
2754}
9291a0cd 2755\f
7b9f3c50
DE
2756/* DWARF quick_symbols_functions support. */
2757
2758/* TUs can share .debug_line entries, and there can be a lot more TUs than
2759 unique line tables, so we maintain a separate table of all .debug_line
2760 derived entries to support the sharing.
2761 All the quick functions need is the list of file names. We discard the
2762 line_header when we're done and don't need to record it here. */
2763struct quick_file_names
2764{
094b34ac
DE
2765 /* The data used to construct the hash key. */
2766 struct stmt_list_hash hash;
7b9f3c50
DE
2767
2768 /* The number of entries in file_names, real_names. */
2769 unsigned int num_file_names;
2770
2771 /* The file names from the line table, after being run through
2772 file_full_name. */
2773 const char **file_names;
2774
2775 /* The file names from the line table after being run through
2776 gdb_realpath. These are computed lazily. */
2777 const char **real_names;
2778};
2779
2780/* When using the index (and thus not using psymtabs), each CU has an
2781 object of this type. This is used to hold information needed by
2782 the various "quick" methods. */
2783struct dwarf2_per_cu_quick_data
2784{
2785 /* The file table. This can be NULL if there was no file table
2786 or it's currently not read in.
2787 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2788 struct quick_file_names *file_names;
2789
2790 /* The corresponding symbol table. This is NULL if symbols for this
2791 CU have not yet been read. */
43f3e411 2792 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2793
2794 /* A temporary mark bit used when iterating over all CUs in
2795 expand_symtabs_matching. */
2796 unsigned int mark : 1;
2797
2798 /* True if we've tried to read the file table and found there isn't one.
2799 There will be no point in trying to read it again next time. */
2800 unsigned int no_file_data : 1;
2801};
2802
094b34ac
DE
2803/* Utility hash function for a stmt_list_hash. */
2804
2805static hashval_t
2806hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2807{
2808 hashval_t v = 0;
2809
2810 if (stmt_list_hash->dwo_unit != NULL)
2811 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2812 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2813 return v;
2814}
2815
2816/* Utility equality function for a stmt_list_hash. */
2817
2818static int
2819eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2820 const struct stmt_list_hash *rhs)
2821{
2822 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2823 return 0;
2824 if (lhs->dwo_unit != NULL
2825 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2826 return 0;
2827
9c541725 2828 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2829}
2830
7b9f3c50
DE
2831/* Hash function for a quick_file_names. */
2832
2833static hashval_t
2834hash_file_name_entry (const void *e)
2835{
9a3c8263
SM
2836 const struct quick_file_names *file_data
2837 = (const struct quick_file_names *) e;
7b9f3c50 2838
094b34ac 2839 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2840}
2841
2842/* Equality function for a quick_file_names. */
2843
2844static int
2845eq_file_name_entry (const void *a, const void *b)
2846{
9a3c8263
SM
2847 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2848 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2849
094b34ac 2850 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2851}
2852
2853/* Delete function for a quick_file_names. */
2854
2855static void
2856delete_file_name_entry (void *e)
2857{
9a3c8263 2858 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2859 int i;
2860
2861 for (i = 0; i < file_data->num_file_names; ++i)
2862 {
2863 xfree ((void*) file_data->file_names[i]);
2864 if (file_data->real_names)
2865 xfree ((void*) file_data->real_names[i]);
2866 }
2867
2868 /* The space for the struct itself lives on objfile_obstack,
2869 so we don't free it here. */
2870}
2871
2872/* Create a quick_file_names hash table. */
2873
2874static htab_t
2875create_quick_file_names_table (unsigned int nr_initial_entries)
2876{
2877 return htab_create_alloc (nr_initial_entries,
2878 hash_file_name_entry, eq_file_name_entry,
2879 delete_file_name_entry, xcalloc, xfree);
2880}
9291a0cd 2881
918dd910
JK
2882/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2883 have to be created afterwards. You should call age_cached_comp_units after
2884 processing PER_CU->CU. dw2_setup must have been already called. */
2885
2886static void
2887load_cu (struct dwarf2_per_cu_data *per_cu)
2888{
3019eac3 2889 if (per_cu->is_debug_types)
e5fe5e75 2890 load_full_type_unit (per_cu);
918dd910 2891 else
95554aad 2892 load_full_comp_unit (per_cu, language_minimal);
918dd910 2893
cc12ce38
DE
2894 if (per_cu->cu == NULL)
2895 return; /* Dummy CU. */
2dc860c0
DE
2896
2897 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2898}
2899
a0f42c21 2900/* Read in the symbols for PER_CU. */
2fdf6df6 2901
9291a0cd 2902static void
a0f42c21 2903dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2904{
2905 struct cleanup *back_to;
2906
f4dc4d17
DE
2907 /* Skip type_unit_groups, reading the type units they contain
2908 is handled elsewhere. */
2909 if (IS_TYPE_UNIT_GROUP (per_cu))
2910 return;
2911
9291a0cd
TT
2912 back_to = make_cleanup (dwarf2_release_queue, NULL);
2913
95554aad 2914 if (dwarf2_per_objfile->using_index
43f3e411 2915 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2916 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2917 {
2918 queue_comp_unit (per_cu, language_minimal);
2919 load_cu (per_cu);
89e63ee4
DE
2920
2921 /* If we just loaded a CU from a DWO, and we're working with an index
2922 that may badly handle TUs, load all the TUs in that DWO as well.
2923 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2924 if (!per_cu->is_debug_types
cc12ce38 2925 && per_cu->cu != NULL
89e63ee4
DE
2926 && per_cu->cu->dwo_unit != NULL
2927 && dwarf2_per_objfile->index_table != NULL
2928 && dwarf2_per_objfile->index_table->version <= 7
2929 /* DWP files aren't supported yet. */
2930 && get_dwp_file () == NULL)
2931 queue_and_load_all_dwo_tus (per_cu);
95554aad 2932 }
9291a0cd 2933
a0f42c21 2934 process_queue ();
9291a0cd
TT
2935
2936 /* Age the cache, releasing compilation units that have not
2937 been used recently. */
2938 age_cached_comp_units ();
2939
2940 do_cleanups (back_to);
2941}
2942
2943/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2944 the objfile from which this CU came. Returns the resulting symbol
2945 table. */
2fdf6df6 2946
43f3e411 2947static struct compunit_symtab *
a0f42c21 2948dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2949{
95554aad 2950 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2951 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2952 {
2953 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2954 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2955 dw2_do_instantiate_symtab (per_cu);
95554aad 2956 process_cu_includes ();
9291a0cd
TT
2957 do_cleanups (back_to);
2958 }
f194fefb 2959
43f3e411 2960 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2961}
2962
8832e7e3 2963/* Return the CU/TU given its index.
f4dc4d17
DE
2964
2965 This is intended for loops like:
2966
2967 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2968 + dwarf2_per_objfile->n_type_units); ++i)
2969 {
8832e7e3 2970 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2971
2972 ...;
2973 }
2974*/
2fdf6df6 2975
1fd400ff 2976static struct dwarf2_per_cu_data *
8832e7e3 2977dw2_get_cutu (int index)
1fd400ff
TT
2978{
2979 if (index >= dwarf2_per_objfile->n_comp_units)
2980 {
f4dc4d17 2981 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2982 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2983 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2984 }
2985
2986 return dwarf2_per_objfile->all_comp_units[index];
2987}
2988
8832e7e3
DE
2989/* Return the CU given its index.
2990 This differs from dw2_get_cutu in that it's for when you know INDEX
2991 refers to a CU. */
f4dc4d17
DE
2992
2993static struct dwarf2_per_cu_data *
8832e7e3 2994dw2_get_cu (int index)
f4dc4d17 2995{
8832e7e3 2996 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2997
1fd400ff
TT
2998 return dwarf2_per_objfile->all_comp_units[index];
2999}
3000
2ec9a5e0
TT
3001/* A helper for create_cus_from_index that handles a given list of
3002 CUs. */
2fdf6df6 3003
74a0d9f6 3004static void
2ec9a5e0
TT
3005create_cus_from_index_list (struct objfile *objfile,
3006 const gdb_byte *cu_list, offset_type n_elements,
3007 struct dwarf2_section_info *section,
3008 int is_dwz,
3009 int base_offset)
9291a0cd
TT
3010{
3011 offset_type i;
9291a0cd 3012
2ec9a5e0 3013 for (i = 0; i < n_elements; i += 2)
9291a0cd 3014 {
74a0d9f6 3015 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3016
3017 sect_offset sect_off
3018 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3019 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3020 cu_list += 2 * 8;
3021
9c541725
PA
3022 dwarf2_per_cu_data *the_cu
3023 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3024 struct dwarf2_per_cu_data);
3025 the_cu->sect_off = sect_off;
9291a0cd
TT
3026 the_cu->length = length;
3027 the_cu->objfile = objfile;
8a0459fd 3028 the_cu->section = section;
9291a0cd
TT
3029 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3030 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3031 the_cu->is_dwz = is_dwz;
3032 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3033 }
9291a0cd
TT
3034}
3035
2ec9a5e0 3036/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3037 the CU objects for this objfile. */
2ec9a5e0 3038
74a0d9f6 3039static void
2ec9a5e0
TT
3040create_cus_from_index (struct objfile *objfile,
3041 const gdb_byte *cu_list, offset_type cu_list_elements,
3042 const gdb_byte *dwz_list, offset_type dwz_elements)
3043{
3044 struct dwz_file *dwz;
3045
3046 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3047 dwarf2_per_objfile->all_comp_units =
3048 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3049 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3050
74a0d9f6
JK
3051 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3052 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3053
3054 if (dwz_elements == 0)
74a0d9f6 3055 return;
2ec9a5e0
TT
3056
3057 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3058 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3059 cu_list_elements / 2);
2ec9a5e0
TT
3060}
3061
1fd400ff 3062/* Create the signatured type hash table from the index. */
673bfd45 3063
74a0d9f6 3064static void
673bfd45 3065create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3066 struct dwarf2_section_info *section,
673bfd45
DE
3067 const gdb_byte *bytes,
3068 offset_type elements)
1fd400ff
TT
3069{
3070 offset_type i;
673bfd45 3071 htab_t sig_types_hash;
1fd400ff 3072
6aa5f3a6
DE
3073 dwarf2_per_objfile->n_type_units
3074 = dwarf2_per_objfile->n_allocated_type_units
3075 = elements / 3;
8d749320
SM
3076 dwarf2_per_objfile->all_type_units =
3077 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3078
673bfd45 3079 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3080
3081 for (i = 0; i < elements; i += 3)
3082 {
52dc124a 3083 struct signatured_type *sig_type;
9c541725 3084 ULONGEST signature;
1fd400ff 3085 void **slot;
9c541725 3086 cu_offset type_offset_in_tu;
1fd400ff 3087
74a0d9f6 3088 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3089 sect_offset sect_off
3090 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3091 type_offset_in_tu
3092 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3093 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3094 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3095 bytes += 3 * 8;
3096
52dc124a 3097 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3098 struct signatured_type);
52dc124a 3099 sig_type->signature = signature;
9c541725 3100 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3101 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3102 sig_type->per_cu.section = section;
9c541725 3103 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3104 sig_type->per_cu.objfile = objfile;
3105 sig_type->per_cu.v.quick
1fd400ff
TT
3106 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3107 struct dwarf2_per_cu_quick_data);
3108
52dc124a
DE
3109 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3110 *slot = sig_type;
1fd400ff 3111
b4dd5633 3112 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3113 }
3114
673bfd45 3115 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3116}
3117
9291a0cd
TT
3118/* Read the address map data from the mapped index, and use it to
3119 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3120
9291a0cd
TT
3121static void
3122create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3123{
3e29f34a 3124 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3125 const gdb_byte *iter, *end;
9291a0cd 3126 struct addrmap *mutable_map;
9291a0cd
TT
3127 CORE_ADDR baseaddr;
3128
8268c778
PA
3129 auto_obstack temp_obstack;
3130
9291a0cd
TT
3131 mutable_map = addrmap_create_mutable (&temp_obstack);
3132
3133 iter = index->address_table;
3134 end = iter + index->address_table_size;
3135
3136 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3137
3138 while (iter < end)
3139 {
3140 ULONGEST hi, lo, cu_index;
3141 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3142 iter += 8;
3143 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3144 iter += 8;
3145 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3146 iter += 4;
f652bce2 3147
24a55014 3148 if (lo > hi)
f652bce2 3149 {
24a55014
DE
3150 complaint (&symfile_complaints,
3151 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3152 hex_string (lo), hex_string (hi));
24a55014 3153 continue;
f652bce2 3154 }
24a55014
DE
3155
3156 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3157 {
3158 complaint (&symfile_complaints,
3159 _(".gdb_index address table has invalid CU number %u"),
3160 (unsigned) cu_index);
24a55014 3161 continue;
f652bce2 3162 }
24a55014 3163
3e29f34a
MR
3164 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3165 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3166 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3167 }
3168
3169 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3170 &objfile->objfile_obstack);
9291a0cd
TT
3171}
3172
59d7bcaf
JK
3173/* The hash function for strings in the mapped index. This is the same as
3174 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3175 implementation. This is necessary because the hash function is tied to the
3176 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3177 SYMBOL_HASH_NEXT.
3178
3179 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3180
9291a0cd 3181static hashval_t
559a7a62 3182mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3183{
3184 const unsigned char *str = (const unsigned char *) p;
3185 hashval_t r = 0;
3186 unsigned char c;
3187
3188 while ((c = *str++) != 0)
559a7a62
JK
3189 {
3190 if (index_version >= 5)
3191 c = tolower (c);
3192 r = r * 67 + c - 113;
3193 }
9291a0cd
TT
3194
3195 return r;
3196}
3197
3198/* Find a slot in the mapped index INDEX for the object named NAME.
3199 If NAME is found, set *VEC_OUT to point to the CU vector in the
3200 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3201
9291a0cd
TT
3202static int
3203find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3204 offset_type **vec_out)
3205{
0cf03b49
JK
3206 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3207 offset_type hash;
9291a0cd 3208 offset_type slot, step;
559a7a62 3209 int (*cmp) (const char *, const char *);
9291a0cd 3210
0cf03b49 3211 if (current_language->la_language == language_cplus
45280282
IB
3212 || current_language->la_language == language_fortran
3213 || current_language->la_language == language_d)
0cf03b49
JK
3214 {
3215 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3216 not contain any. */
a8719064 3217
72998fb3 3218 if (strchr (name, '(') != NULL)
0cf03b49 3219 {
72998fb3 3220 char *without_params = cp_remove_params (name);
0cf03b49 3221
72998fb3
DE
3222 if (without_params != NULL)
3223 {
3224 make_cleanup (xfree, without_params);
3225 name = without_params;
3226 }
0cf03b49
JK
3227 }
3228 }
3229
559a7a62 3230 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3231 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3232 simulate our NAME being searched is also lowercased. */
3233 hash = mapped_index_string_hash ((index->version == 4
3234 && case_sensitivity == case_sensitive_off
3235 ? 5 : index->version),
3236 name);
3237
3876f04e
DE
3238 slot = hash & (index->symbol_table_slots - 1);
3239 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3240 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3241
3242 for (;;)
3243 {
3244 /* Convert a slot number to an offset into the table. */
3245 offset_type i = 2 * slot;
3246 const char *str;
3876f04e 3247 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3248 {
3249 do_cleanups (back_to);
3250 return 0;
3251 }
9291a0cd 3252
3876f04e 3253 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3254 if (!cmp (name, str))
9291a0cd
TT
3255 {
3256 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3257 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3258 do_cleanups (back_to);
9291a0cd
TT
3259 return 1;
3260 }
3261
3876f04e 3262 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3263 }
3264}
3265
2ec9a5e0
TT
3266/* A helper function that reads the .gdb_index from SECTION and fills
3267 in MAP. FILENAME is the name of the file containing the section;
3268 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3269 ok to use deprecated sections.
3270
3271 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3272 out parameters that are filled in with information about the CU and
3273 TU lists in the section.
3274
3275 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3276
9291a0cd 3277static int
2ec9a5e0
TT
3278read_index_from_section (struct objfile *objfile,
3279 const char *filename,
3280 int deprecated_ok,
3281 struct dwarf2_section_info *section,
3282 struct mapped_index *map,
3283 const gdb_byte **cu_list,
3284 offset_type *cu_list_elements,
3285 const gdb_byte **types_list,
3286 offset_type *types_list_elements)
9291a0cd 3287{
948f8e3d 3288 const gdb_byte *addr;
2ec9a5e0 3289 offset_type version;
b3b272e1 3290 offset_type *metadata;
1fd400ff 3291 int i;
9291a0cd 3292
2ec9a5e0 3293 if (dwarf2_section_empty_p (section))
9291a0cd 3294 return 0;
82430852
JK
3295
3296 /* Older elfutils strip versions could keep the section in the main
3297 executable while splitting it for the separate debug info file. */
a32a8923 3298 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3299 return 0;
3300
2ec9a5e0 3301 dwarf2_read_section (objfile, section);
9291a0cd 3302
2ec9a5e0 3303 addr = section->buffer;
9291a0cd 3304 /* Version check. */
1fd400ff 3305 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3306 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3307 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3308 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3309 indices. */
831adc1f 3310 if (version < 4)
481860b3
GB
3311 {
3312 static int warning_printed = 0;
3313 if (!warning_printed)
3314 {
3315 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3316 filename);
481860b3
GB
3317 warning_printed = 1;
3318 }
3319 return 0;
3320 }
3321 /* Index version 4 uses a different hash function than index version
3322 5 and later.
3323
3324 Versions earlier than 6 did not emit psymbols for inlined
3325 functions. Using these files will cause GDB not to be able to
3326 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3327 indices unless the user has done
3328 "set use-deprecated-index-sections on". */
2ec9a5e0 3329 if (version < 6 && !deprecated_ok)
481860b3
GB
3330 {
3331 static int warning_printed = 0;
3332 if (!warning_printed)
3333 {
e615022a
DE
3334 warning (_("\
3335Skipping deprecated .gdb_index section in %s.\n\
3336Do \"set use-deprecated-index-sections on\" before the file is read\n\
3337to use the section anyway."),
2ec9a5e0 3338 filename);
481860b3
GB
3339 warning_printed = 1;
3340 }
3341 return 0;
3342 }
796a7ff8 3343 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3344 of the TU (for symbols coming from TUs),
3345 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3346 Plus gold-generated indices can have duplicate entries for global symbols,
3347 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3348 These are just performance bugs, and we can't distinguish gdb-generated
3349 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3350
481860b3 3351 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3352 longer backward compatible. */
796a7ff8 3353 if (version > 8)
594e8718 3354 return 0;
9291a0cd 3355
559a7a62 3356 map->version = version;
2ec9a5e0 3357 map->total_size = section->size;
9291a0cd
TT
3358
3359 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3360
3361 i = 0;
2ec9a5e0
TT
3362 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3363 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3364 / 8);
1fd400ff
TT
3365 ++i;
3366
2ec9a5e0
TT
3367 *types_list = addr + MAYBE_SWAP (metadata[i]);
3368 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3369 - MAYBE_SWAP (metadata[i]))
3370 / 8);
987d643c 3371 ++i;
1fd400ff
TT
3372
3373 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3374 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3375 - MAYBE_SWAP (metadata[i]));
3376 ++i;
3377
3876f04e
DE
3378 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3379 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3380 - MAYBE_SWAP (metadata[i]))
3381 / (2 * sizeof (offset_type)));
1fd400ff 3382 ++i;
9291a0cd 3383
f9d83a0b 3384 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3385
2ec9a5e0
TT
3386 return 1;
3387}
3388
3389
3390/* Read the index file. If everything went ok, initialize the "quick"
3391 elements of all the CUs and return 1. Otherwise, return 0. */
3392
3393static int
3394dwarf2_read_index (struct objfile *objfile)
3395{
3396 struct mapped_index local_map, *map;
3397 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3398 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3399 struct dwz_file *dwz;
2ec9a5e0 3400
4262abfb 3401 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3402 use_deprecated_index_sections,
3403 &dwarf2_per_objfile->gdb_index, &local_map,
3404 &cu_list, &cu_list_elements,
3405 &types_list, &types_list_elements))
3406 return 0;
3407
0fefef59 3408 /* Don't use the index if it's empty. */
2ec9a5e0 3409 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3410 return 0;
3411
2ec9a5e0
TT
3412 /* If there is a .dwz file, read it so we can get its CU list as
3413 well. */
4db1a1dc
TT
3414 dwz = dwarf2_get_dwz_file ();
3415 if (dwz != NULL)
2ec9a5e0 3416 {
2ec9a5e0
TT
3417 struct mapped_index dwz_map;
3418 const gdb_byte *dwz_types_ignore;
3419 offset_type dwz_types_elements_ignore;
3420
3421 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3422 1,
3423 &dwz->gdb_index, &dwz_map,
3424 &dwz_list, &dwz_list_elements,
3425 &dwz_types_ignore,
3426 &dwz_types_elements_ignore))
3427 {
3428 warning (_("could not read '.gdb_index' section from %s; skipping"),
3429 bfd_get_filename (dwz->dwz_bfd));
3430 return 0;
3431 }
3432 }
3433
74a0d9f6
JK
3434 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3435 dwz_list_elements);
1fd400ff 3436
8b70b953
TT
3437 if (types_list_elements)
3438 {
3439 struct dwarf2_section_info *section;
3440
3441 /* We can only handle a single .debug_types when we have an
3442 index. */
3443 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3444 return 0;
3445
3446 section = VEC_index (dwarf2_section_info_def,
3447 dwarf2_per_objfile->types, 0);
3448
74a0d9f6
JK
3449 create_signatured_type_table_from_index (objfile, section, types_list,
3450 types_list_elements);
8b70b953 3451 }
9291a0cd 3452
2ec9a5e0
TT
3453 create_addrmap_from_index (objfile, &local_map);
3454
8d749320 3455 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3456 *map = local_map;
9291a0cd
TT
3457
3458 dwarf2_per_objfile->index_table = map;
3459 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3460 dwarf2_per_objfile->quick_file_names_table =
3461 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3462
3463 return 1;
3464}
3465
3466/* A helper for the "quick" functions which sets the global
3467 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3468
9291a0cd
TT
3469static void
3470dw2_setup (struct objfile *objfile)
3471{
9a3c8263
SM
3472 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3473 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3474 gdb_assert (dwarf2_per_objfile);
3475}
3476
dee91e82 3477/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3478
dee91e82
DE
3479static void
3480dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3481 const gdb_byte *info_ptr,
dee91e82
DE
3482 struct die_info *comp_unit_die,
3483 int has_children,
3484 void *data)
9291a0cd 3485{
dee91e82
DE
3486 struct dwarf2_cu *cu = reader->cu;
3487 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3488 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3489 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3490 struct attribute *attr;
dee91e82 3491 int i;
7b9f3c50
DE
3492 void **slot;
3493 struct quick_file_names *qfn;
9291a0cd 3494
0186c6a7
DE
3495 gdb_assert (! this_cu->is_debug_types);
3496
07261596
TT
3497 /* Our callers never want to match partial units -- instead they
3498 will match the enclosing full CU. */
3499 if (comp_unit_die->tag == DW_TAG_partial_unit)
3500 {
3501 this_cu->v.quick->no_file_data = 1;
3502 return;
3503 }
3504
0186c6a7 3505 lh_cu = this_cu;
7b9f3c50 3506 slot = NULL;
dee91e82 3507
fff8551c 3508 line_header_up lh;
9c541725 3509 sect_offset line_offset {};
fff8551c 3510
dee91e82 3511 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3512 if (attr)
3513 {
7b9f3c50
DE
3514 struct quick_file_names find_entry;
3515
9c541725 3516 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3517
3518 /* We may have already read in this line header (TU line header sharing).
3519 If we have we're done. */
094b34ac 3520 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3521 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3522 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3523 &find_entry, INSERT);
3524 if (*slot != NULL)
3525 {
9a3c8263 3526 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3527 return;
7b9f3c50
DE
3528 }
3529
3019eac3 3530 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3531 }
3532 if (lh == NULL)
3533 {
094b34ac 3534 lh_cu->v.quick->no_file_data = 1;
dee91e82 3535 return;
9291a0cd
TT
3536 }
3537
8d749320 3538 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3539 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3540 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3541 gdb_assert (slot != NULL);
3542 *slot = qfn;
9291a0cd 3543
d721ba37 3544 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3545
fff8551c 3546 qfn->num_file_names = lh->file_names.size ();
8d749320 3547 qfn->file_names =
fff8551c
PA
3548 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3549 for (i = 0; i < lh->file_names.size (); ++i)
3550 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3551 qfn->real_names = NULL;
9291a0cd 3552
094b34ac 3553 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3554}
3555
3556/* A helper for the "quick" functions which attempts to read the line
3557 table for THIS_CU. */
3558
3559static struct quick_file_names *
e4a48d9d 3560dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3561{
0186c6a7
DE
3562 /* This should never be called for TUs. */
3563 gdb_assert (! this_cu->is_debug_types);
3564 /* Nor type unit groups. */
3565 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3566
dee91e82
DE
3567 if (this_cu->v.quick->file_names != NULL)
3568 return this_cu->v.quick->file_names;
3569 /* If we know there is no line data, no point in looking again. */
3570 if (this_cu->v.quick->no_file_data)
3571 return NULL;
3572
0186c6a7 3573 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3574
3575 if (this_cu->v.quick->no_file_data)
3576 return NULL;
3577 return this_cu->v.quick->file_names;
9291a0cd
TT
3578}
3579
3580/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3581 real path for a given file name from the line table. */
2fdf6df6 3582
9291a0cd 3583static const char *
7b9f3c50
DE
3584dw2_get_real_path (struct objfile *objfile,
3585 struct quick_file_names *qfn, int index)
9291a0cd 3586{
7b9f3c50
DE
3587 if (qfn->real_names == NULL)
3588 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3589 qfn->num_file_names, const char *);
9291a0cd 3590
7b9f3c50 3591 if (qfn->real_names[index] == NULL)
14278e1f 3592 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3593
7b9f3c50 3594 return qfn->real_names[index];
9291a0cd
TT
3595}
3596
3597static struct symtab *
3598dw2_find_last_source_symtab (struct objfile *objfile)
3599{
43f3e411 3600 struct compunit_symtab *cust;
9291a0cd 3601 int index;
ae2de4f8 3602
9291a0cd
TT
3603 dw2_setup (objfile);
3604 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3605 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3606 if (cust == NULL)
3607 return NULL;
3608 return compunit_primary_filetab (cust);
9291a0cd
TT
3609}
3610
7b9f3c50
DE
3611/* Traversal function for dw2_forget_cached_source_info. */
3612
3613static int
3614dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3615{
7b9f3c50 3616 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3617
7b9f3c50 3618 if (file_data->real_names)
9291a0cd 3619 {
7b9f3c50 3620 int i;
9291a0cd 3621
7b9f3c50 3622 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3623 {
7b9f3c50
DE
3624 xfree ((void*) file_data->real_names[i]);
3625 file_data->real_names[i] = NULL;
9291a0cd
TT
3626 }
3627 }
7b9f3c50
DE
3628
3629 return 1;
3630}
3631
3632static void
3633dw2_forget_cached_source_info (struct objfile *objfile)
3634{
3635 dw2_setup (objfile);
3636
3637 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3638 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3639}
3640
f8eba3c6
TT
3641/* Helper function for dw2_map_symtabs_matching_filename that expands
3642 the symtabs and calls the iterator. */
3643
3644static int
3645dw2_map_expand_apply (struct objfile *objfile,
3646 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3647 const char *name, const char *real_path,
14bc53a8 3648 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3649{
43f3e411 3650 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3651
3652 /* Don't visit already-expanded CUs. */
43f3e411 3653 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3654 return 0;
3655
3656 /* This may expand more than one symtab, and we want to iterate over
3657 all of them. */
a0f42c21 3658 dw2_instantiate_symtab (per_cu);
f8eba3c6 3659
14bc53a8
PA
3660 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3661 last_made, callback);
f8eba3c6
TT
3662}
3663
3664/* Implementation of the map_symtabs_matching_filename method. */
3665
14bc53a8
PA
3666static bool
3667dw2_map_symtabs_matching_filename
3668 (struct objfile *objfile, const char *name, const char *real_path,
3669 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3670{
3671 int i;
c011a4f4 3672 const char *name_basename = lbasename (name);
9291a0cd
TT
3673
3674 dw2_setup (objfile);
ae2de4f8 3675
848e3e78
DE
3676 /* The rule is CUs specify all the files, including those used by
3677 any TU, so there's no need to scan TUs here. */
f4dc4d17 3678
848e3e78 3679 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3680 {
3681 int j;
8832e7e3 3682 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3683 struct quick_file_names *file_data;
9291a0cd 3684
3d7bb9d9 3685 /* We only need to look at symtabs not already expanded. */
43f3e411 3686 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3687 continue;
3688
e4a48d9d 3689 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3690 if (file_data == NULL)
9291a0cd
TT
3691 continue;
3692
7b9f3c50 3693 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3694 {
7b9f3c50 3695 const char *this_name = file_data->file_names[j];
da235a7c 3696 const char *this_real_name;
9291a0cd 3697
af529f8f 3698 if (compare_filenames_for_search (this_name, name))
9291a0cd 3699 {
f5b95b50 3700 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3701 callback))
3702 return true;
288e77a7 3703 continue;
4aac40c8 3704 }
9291a0cd 3705
c011a4f4
DE
3706 /* Before we invoke realpath, which can get expensive when many
3707 files are involved, do a quick comparison of the basenames. */
3708 if (! basenames_may_differ
3709 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3710 continue;
3711
da235a7c
JK
3712 this_real_name = dw2_get_real_path (objfile, file_data, j);
3713 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3714 {
da235a7c 3715 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3716 callback))
3717 return true;
288e77a7 3718 continue;
da235a7c 3719 }
9291a0cd 3720
da235a7c
JK
3721 if (real_path != NULL)
3722 {
af529f8f
JK
3723 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3724 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3725 if (this_real_name != NULL
af529f8f 3726 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3727 {
f5b95b50 3728 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3729 callback))
3730 return true;
288e77a7 3731 continue;
9291a0cd
TT
3732 }
3733 }
3734 }
3735 }
3736
14bc53a8 3737 return false;
9291a0cd
TT
3738}
3739
da51c347
DE
3740/* Struct used to manage iterating over all CUs looking for a symbol. */
3741
3742struct dw2_symtab_iterator
9291a0cd 3743{
da51c347
DE
3744 /* The internalized form of .gdb_index. */
3745 struct mapped_index *index;
3746 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3747 int want_specific_block;
3748 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3749 Unused if !WANT_SPECIFIC_BLOCK. */
3750 int block_index;
3751 /* The kind of symbol we're looking for. */
3752 domain_enum domain;
3753 /* The list of CUs from the index entry of the symbol,
3754 or NULL if not found. */
3755 offset_type *vec;
3756 /* The next element in VEC to look at. */
3757 int next;
3758 /* The number of elements in VEC, or zero if there is no match. */
3759 int length;
8943b874
DE
3760 /* Have we seen a global version of the symbol?
3761 If so we can ignore all further global instances.
3762 This is to work around gold/15646, inefficient gold-generated
3763 indices. */
3764 int global_seen;
da51c347 3765};
9291a0cd 3766
da51c347
DE
3767/* Initialize the index symtab iterator ITER.
3768 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3769 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3770
9291a0cd 3771static void
da51c347
DE
3772dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3773 struct mapped_index *index,
3774 int want_specific_block,
3775 int block_index,
3776 domain_enum domain,
3777 const char *name)
3778{
3779 iter->index = index;
3780 iter->want_specific_block = want_specific_block;
3781 iter->block_index = block_index;
3782 iter->domain = domain;
3783 iter->next = 0;
8943b874 3784 iter->global_seen = 0;
da51c347
DE
3785
3786 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3787 iter->length = MAYBE_SWAP (*iter->vec);
3788 else
3789 {
3790 iter->vec = NULL;
3791 iter->length = 0;
3792 }
3793}
3794
3795/* Return the next matching CU or NULL if there are no more. */
3796
3797static struct dwarf2_per_cu_data *
3798dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3799{
3800 for ( ; iter->next < iter->length; ++iter->next)
3801 {
3802 offset_type cu_index_and_attrs =
3803 MAYBE_SWAP (iter->vec[iter->next + 1]);
3804 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3805 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3806 int want_static = iter->block_index != GLOBAL_BLOCK;
3807 /* This value is only valid for index versions >= 7. */
3808 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3809 gdb_index_symbol_kind symbol_kind =
3810 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3811 /* Only check the symbol attributes if they're present.
3812 Indices prior to version 7 don't record them,
3813 and indices >= 7 may elide them for certain symbols
3814 (gold does this). */
3815 int attrs_valid =
3816 (iter->index->version >= 7
3817 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3818
3190f0c6
DE
3819 /* Don't crash on bad data. */
3820 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3821 + dwarf2_per_objfile->n_type_units))
3822 {
3823 complaint (&symfile_complaints,
3824 _(".gdb_index entry has bad CU index"
4262abfb
JK
3825 " [in module %s]"),
3826 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3827 continue;
3828 }
3829
8832e7e3 3830 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3831
da51c347 3832 /* Skip if already read in. */
43f3e411 3833 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3834 continue;
3835
8943b874
DE
3836 /* Check static vs global. */
3837 if (attrs_valid)
3838 {
3839 if (iter->want_specific_block
3840 && want_static != is_static)
3841 continue;
3842 /* Work around gold/15646. */
3843 if (!is_static && iter->global_seen)
3844 continue;
3845 if (!is_static)
3846 iter->global_seen = 1;
3847 }
da51c347
DE
3848
3849 /* Only check the symbol's kind if it has one. */
3850 if (attrs_valid)
3851 {
3852 switch (iter->domain)
3853 {
3854 case VAR_DOMAIN:
3855 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3856 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3857 /* Some types are also in VAR_DOMAIN. */
3858 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3859 continue;
3860 break;
3861 case STRUCT_DOMAIN:
3862 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3863 continue;
3864 break;
3865 case LABEL_DOMAIN:
3866 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3867 continue;
3868 break;
3869 default:
3870 break;
3871 }
3872 }
3873
3874 ++iter->next;
3875 return per_cu;
3876 }
3877
3878 return NULL;
3879}
3880
43f3e411 3881static struct compunit_symtab *
da51c347
DE
3882dw2_lookup_symbol (struct objfile *objfile, int block_index,
3883 const char *name, domain_enum domain)
9291a0cd 3884{
43f3e411 3885 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3886 struct mapped_index *index;
3887
9291a0cd
TT
3888 dw2_setup (objfile);
3889
156942c7
DE
3890 index = dwarf2_per_objfile->index_table;
3891
da51c347 3892 /* index is NULL if OBJF_READNOW. */
156942c7 3893 if (index)
9291a0cd 3894 {
da51c347
DE
3895 struct dw2_symtab_iterator iter;
3896 struct dwarf2_per_cu_data *per_cu;
3897
3898 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3899
da51c347 3900 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3901 {
b2e2f908 3902 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3903 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3904 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3905 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3906
b2e2f908
DE
3907 sym = block_find_symbol (block, name, domain,
3908 block_find_non_opaque_type_preferred,
3909 &with_opaque);
3910
da51c347
DE
3911 /* Some caution must be observed with overloaded functions
3912 and methods, since the index will not contain any overload
3913 information (but NAME might contain it). */
da51c347 3914
b2e2f908 3915 if (sym != NULL
a778f165 3916 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
3917 return stab;
3918 if (with_opaque != NULL
a778f165 3919 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 3920 stab_best = stab;
da51c347
DE
3921
3922 /* Keep looking through other CUs. */
9291a0cd
TT
3923 }
3924 }
9291a0cd 3925
da51c347 3926 return stab_best;
9291a0cd
TT
3927}
3928
3929static void
3930dw2_print_stats (struct objfile *objfile)
3931{
e4a48d9d 3932 int i, total, count;
9291a0cd
TT
3933
3934 dw2_setup (objfile);
e4a48d9d 3935 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3936 count = 0;
e4a48d9d 3937 for (i = 0; i < total; ++i)
9291a0cd 3938 {
8832e7e3 3939 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3940
43f3e411 3941 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3942 ++count;
3943 }
e4a48d9d 3944 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3945 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3946}
3947
779bd270
DE
3948/* This dumps minimal information about the index.
3949 It is called via "mt print objfiles".
3950 One use is to verify .gdb_index has been loaded by the
3951 gdb.dwarf2/gdb-index.exp testcase. */
3952
9291a0cd
TT
3953static void
3954dw2_dump (struct objfile *objfile)
3955{
779bd270
DE
3956 dw2_setup (objfile);
3957 gdb_assert (dwarf2_per_objfile->using_index);
3958 printf_filtered (".gdb_index:");
3959 if (dwarf2_per_objfile->index_table != NULL)
3960 {
3961 printf_filtered (" version %d\n",
3962 dwarf2_per_objfile->index_table->version);
3963 }
3964 else
3965 printf_filtered (" faked for \"readnow\"\n");
3966 printf_filtered ("\n");
9291a0cd
TT
3967}
3968
3969static void
3189cb12
DE
3970dw2_relocate (struct objfile *objfile,
3971 const struct section_offsets *new_offsets,
3972 const struct section_offsets *delta)
9291a0cd
TT
3973{
3974 /* There's nothing to relocate here. */
3975}
3976
3977static void
3978dw2_expand_symtabs_for_function (struct objfile *objfile,
3979 const char *func_name)
3980{
da51c347
DE
3981 struct mapped_index *index;
3982
3983 dw2_setup (objfile);
3984
3985 index = dwarf2_per_objfile->index_table;
3986
3987 /* index is NULL if OBJF_READNOW. */
3988 if (index)
3989 {
3990 struct dw2_symtab_iterator iter;
3991 struct dwarf2_per_cu_data *per_cu;
3992
3993 /* Note: It doesn't matter what we pass for block_index here. */
3994 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3995 func_name);
3996
3997 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3998 dw2_instantiate_symtab (per_cu);
3999 }
9291a0cd
TT
4000}
4001
4002static void
4003dw2_expand_all_symtabs (struct objfile *objfile)
4004{
4005 int i;
4006
4007 dw2_setup (objfile);
1fd400ff
TT
4008
4009 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4010 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4011 {
8832e7e3 4012 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4013
a0f42c21 4014 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4015 }
4016}
4017
4018static void
652a8996
JK
4019dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4020 const char *fullname)
9291a0cd
TT
4021{
4022 int i;
4023
4024 dw2_setup (objfile);
d4637a04
DE
4025
4026 /* We don't need to consider type units here.
4027 This is only called for examining code, e.g. expand_line_sal.
4028 There can be an order of magnitude (or more) more type units
4029 than comp units, and we avoid them if we can. */
4030
4031 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4032 {
4033 int j;
8832e7e3 4034 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4035 struct quick_file_names *file_data;
9291a0cd 4036
3d7bb9d9 4037 /* We only need to look at symtabs not already expanded. */
43f3e411 4038 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4039 continue;
4040
e4a48d9d 4041 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4042 if (file_data == NULL)
9291a0cd
TT
4043 continue;
4044
7b9f3c50 4045 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4046 {
652a8996
JK
4047 const char *this_fullname = file_data->file_names[j];
4048
4049 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4050 {
a0f42c21 4051 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4052 break;
4053 }
4054 }
4055 }
4056}
4057
9291a0cd 4058static void
ade7ed9e 4059dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4060 const char * name, domain_enum domain,
ade7ed9e 4061 int global,
40658b94
PH
4062 int (*callback) (struct block *,
4063 struct symbol *, void *),
2edb89d3
JK
4064 void *data, symbol_compare_ftype *match,
4065 symbol_compare_ftype *ordered_compare)
9291a0cd 4066{
40658b94 4067 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4068 current language is Ada for a non-Ada objfile using GNU index. As Ada
4069 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4070}
4071
4072static void
f8eba3c6
TT
4073dw2_expand_symtabs_matching
4074 (struct objfile *objfile,
14bc53a8
PA
4075 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4076 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4077 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4078 enum search_domain kind)
9291a0cd
TT
4079{
4080 int i;
4081 offset_type iter;
4b5246aa 4082 struct mapped_index *index;
9291a0cd
TT
4083
4084 dw2_setup (objfile);
ae2de4f8
DE
4085
4086 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4087 if (!dwarf2_per_objfile->index_table)
4088 return;
4b5246aa 4089 index = dwarf2_per_objfile->index_table;
9291a0cd 4090
7b08b9eb 4091 if (file_matcher != NULL)
24c79950 4092 {
fc4007c9
TT
4093 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4094 htab_eq_pointer,
4095 NULL, xcalloc, xfree));
4096 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4097 htab_eq_pointer,
4098 NULL, xcalloc, xfree));
24c79950 4099
848e3e78
DE
4100 /* The rule is CUs specify all the files, including those used by
4101 any TU, so there's no need to scan TUs here. */
4102
4103 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4104 {
4105 int j;
8832e7e3 4106 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4107 struct quick_file_names *file_data;
4108 void **slot;
7b08b9eb 4109
61d96d7e
DE
4110 QUIT;
4111
24c79950 4112 per_cu->v.quick->mark = 0;
3d7bb9d9 4113
24c79950 4114 /* We only need to look at symtabs not already expanded. */
43f3e411 4115 if (per_cu->v.quick->compunit_symtab)
24c79950 4116 continue;
7b08b9eb 4117
e4a48d9d 4118 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4119 if (file_data == NULL)
4120 continue;
7b08b9eb 4121
fc4007c9 4122 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4123 continue;
fc4007c9 4124 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4125 {
4126 per_cu->v.quick->mark = 1;
4127 continue;
4128 }
4129
4130 for (j = 0; j < file_data->num_file_names; ++j)
4131 {
da235a7c
JK
4132 const char *this_real_name;
4133
14bc53a8 4134 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4135 {
4136 per_cu->v.quick->mark = 1;
4137 break;
4138 }
da235a7c
JK
4139
4140 /* Before we invoke realpath, which can get expensive when many
4141 files are involved, do a quick comparison of the basenames. */
4142 if (!basenames_may_differ
4143 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4144 true))
da235a7c
JK
4145 continue;
4146
4147 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4148 if (file_matcher (this_real_name, false))
da235a7c
JK
4149 {
4150 per_cu->v.quick->mark = 1;
4151 break;
4152 }
24c79950
TT
4153 }
4154
4155 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4156 ? visited_found.get ()
4157 : visited_not_found.get (),
24c79950
TT
4158 file_data, INSERT);
4159 *slot = file_data;
4160 }
24c79950 4161 }
9291a0cd 4162
3876f04e 4163 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4164 {
4165 offset_type idx = 2 * iter;
4166 const char *name;
4167 offset_type *vec, vec_len, vec_idx;
8943b874 4168 int global_seen = 0;
9291a0cd 4169
61d96d7e
DE
4170 QUIT;
4171
3876f04e 4172 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4173 continue;
4174
3876f04e 4175 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4176
14bc53a8 4177 if (!symbol_matcher (name))
9291a0cd
TT
4178 continue;
4179
4180 /* The name was matched, now expand corresponding CUs that were
4181 marked. */
4b5246aa 4182 vec = (offset_type *) (index->constant_pool
3876f04e 4183 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4184 vec_len = MAYBE_SWAP (vec[0]);
4185 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4186 {
e254ef6a 4187 struct dwarf2_per_cu_data *per_cu;
156942c7 4188 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4189 /* This value is only valid for index versions >= 7. */
4190 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4191 gdb_index_symbol_kind symbol_kind =
4192 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4193 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4194 /* Only check the symbol attributes if they're present.
4195 Indices prior to version 7 don't record them,
4196 and indices >= 7 may elide them for certain symbols
4197 (gold does this). */
4198 int attrs_valid =
4199 (index->version >= 7
4200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4201
8943b874
DE
4202 /* Work around gold/15646. */
4203 if (attrs_valid)
4204 {
4205 if (!is_static && global_seen)
4206 continue;
4207 if (!is_static)
4208 global_seen = 1;
4209 }
4210
3190f0c6
DE
4211 /* Only check the symbol's kind if it has one. */
4212 if (attrs_valid)
156942c7
DE
4213 {
4214 switch (kind)
4215 {
4216 case VARIABLES_DOMAIN:
4217 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4218 continue;
4219 break;
4220 case FUNCTIONS_DOMAIN:
4221 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4222 continue;
4223 break;
4224 case TYPES_DOMAIN:
4225 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4226 continue;
4227 break;
4228 default:
4229 break;
4230 }
4231 }
4232
3190f0c6
DE
4233 /* Don't crash on bad data. */
4234 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4235 + dwarf2_per_objfile->n_type_units))
4236 {
4237 complaint (&symfile_complaints,
4238 _(".gdb_index entry has bad CU index"
4262abfb 4239 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4240 continue;
4241 }
4242
8832e7e3 4243 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4244 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4245 {
4246 int symtab_was_null =
4247 (per_cu->v.quick->compunit_symtab == NULL);
4248
4249 dw2_instantiate_symtab (per_cu);
4250
4251 if (expansion_notify != NULL
4252 && symtab_was_null
4253 && per_cu->v.quick->compunit_symtab != NULL)
4254 {
14bc53a8 4255 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4256 }
4257 }
9291a0cd
TT
4258 }
4259 }
4260}
4261
43f3e411 4262/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4263 symtab. */
4264
43f3e411
DE
4265static struct compunit_symtab *
4266recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4267 CORE_ADDR pc)
9703b513
TT
4268{
4269 int i;
4270
43f3e411
DE
4271 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4272 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4273 return cust;
9703b513 4274
43f3e411 4275 if (cust->includes == NULL)
a3ec0bb1
DE
4276 return NULL;
4277
43f3e411 4278 for (i = 0; cust->includes[i]; ++i)
9703b513 4279 {
43f3e411 4280 struct compunit_symtab *s = cust->includes[i];
9703b513 4281
43f3e411 4282 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4283 if (s != NULL)
4284 return s;
4285 }
4286
4287 return NULL;
4288}
4289
43f3e411
DE
4290static struct compunit_symtab *
4291dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4292 struct bound_minimal_symbol msymbol,
4293 CORE_ADDR pc,
4294 struct obj_section *section,
4295 int warn_if_readin)
9291a0cd
TT
4296{
4297 struct dwarf2_per_cu_data *data;
43f3e411 4298 struct compunit_symtab *result;
9291a0cd
TT
4299
4300 dw2_setup (objfile);
4301
4302 if (!objfile->psymtabs_addrmap)
4303 return NULL;
4304
9a3c8263
SM
4305 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4306 pc);
9291a0cd
TT
4307 if (!data)
4308 return NULL;
4309
43f3e411 4310 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4311 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4312 paddress (get_objfile_arch (objfile), pc));
4313
43f3e411
DE
4314 result
4315 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4316 pc);
9703b513
TT
4317 gdb_assert (result != NULL);
4318 return result;
9291a0cd
TT
4319}
4320
9291a0cd 4321static void
44b13c5a 4322dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4323 void *data, int need_fullname)
9291a0cd 4324{
9291a0cd 4325 dw2_setup (objfile);
ae2de4f8 4326
bbf2f4df 4327 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4328 {
bbf2f4df 4329 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4330
bbf2f4df
PA
4331 htab_up visited (htab_create_alloc (10,
4332 htab_hash_pointer, htab_eq_pointer,
4333 NULL, xcalloc, xfree));
24c79950 4334
bbf2f4df
PA
4335 /* The rule is CUs specify all the files, including those used
4336 by any TU, so there's no need to scan TUs here. We can
4337 ignore file names coming from already-expanded CUs. */
24c79950 4338
bbf2f4df
PA
4339 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4340 {
4341 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4342
bbf2f4df
PA
4343 if (per_cu->v.quick->compunit_symtab)
4344 {
4345 void **slot = htab_find_slot (visited.get (),
4346 per_cu->v.quick->file_names,
4347 INSERT);
9291a0cd 4348
bbf2f4df
PA
4349 *slot = per_cu->v.quick->file_names;
4350 }
24c79950 4351 }
24c79950 4352
bbf2f4df 4353 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4354 {
bbf2f4df
PA
4355 int j;
4356 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4357 struct quick_file_names *file_data;
4358 void **slot;
4359
4360 /* We only need to look at symtabs not already expanded. */
4361 if (per_cu->v.quick->compunit_symtab)
4362 continue;
74e2f255 4363
bbf2f4df
PA
4364 file_data = dw2_get_file_names (per_cu);
4365 if (file_data == NULL)
4366 continue;
4367
4368 slot = htab_find_slot (visited.get (), file_data, INSERT);
4369 if (*slot)
4370 {
4371 /* Already visited. */
4372 continue;
4373 }
4374 *slot = file_data;
4375
4376 for (int j = 0; j < file_data->num_file_names; ++j)
4377 {
4378 const char *filename = file_data->file_names[j];
4379 dwarf2_per_objfile->filenames_cache->seen (filename);
4380 }
9291a0cd
TT
4381 }
4382 }
bbf2f4df
PA
4383
4384 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4385 {
14278e1f 4386 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4387
4388 if (need_fullname)
4389 this_real_name = gdb_realpath (filename);
14278e1f 4390 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4391 });
9291a0cd
TT
4392}
4393
4394static int
4395dw2_has_symbols (struct objfile *objfile)
4396{
4397 return 1;
4398}
4399
4400const struct quick_symbol_functions dwarf2_gdb_index_functions =
4401{
4402 dw2_has_symbols,
4403 dw2_find_last_source_symtab,
4404 dw2_forget_cached_source_info,
f8eba3c6 4405 dw2_map_symtabs_matching_filename,
9291a0cd 4406 dw2_lookup_symbol,
9291a0cd
TT
4407 dw2_print_stats,
4408 dw2_dump,
4409 dw2_relocate,
4410 dw2_expand_symtabs_for_function,
4411 dw2_expand_all_symtabs,
652a8996 4412 dw2_expand_symtabs_with_fullname,
40658b94 4413 dw2_map_matching_symbols,
9291a0cd 4414 dw2_expand_symtabs_matching,
43f3e411 4415 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4416 dw2_map_symbol_filenames
4417};
4418
4419/* Initialize for reading DWARF for this objfile. Return 0 if this
4420 file will use psymtabs, or 1 if using the GNU index. */
4421
4422int
4423dwarf2_initialize_objfile (struct objfile *objfile)
4424{
4425 /* If we're about to read full symbols, don't bother with the
4426 indices. In this case we also don't care if some other debug
4427 format is making psymtabs, because they are all about to be
4428 expanded anyway. */
4429 if ((objfile->flags & OBJF_READNOW))
4430 {
4431 int i;
4432
4433 dwarf2_per_objfile->using_index = 1;
4434 create_all_comp_units (objfile);
0e50663e 4435 create_all_type_units (objfile);
7b9f3c50
DE
4436 dwarf2_per_objfile->quick_file_names_table =
4437 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4438
1fd400ff 4439 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4440 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4441 {
8832e7e3 4442 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4443
e254ef6a
DE
4444 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4445 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4446 }
4447
4448 /* Return 1 so that gdb sees the "quick" functions. However,
4449 these functions will be no-ops because we will have expanded
4450 all symtabs. */
4451 return 1;
4452 }
4453
4454 if (dwarf2_read_index (objfile))
4455 return 1;
4456
9291a0cd
TT
4457 return 0;
4458}
4459
4460\f
4461
dce234bc
PP
4462/* Build a partial symbol table. */
4463
4464void
f29dff0a 4465dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4466{
c9bf0622 4467
f29dff0a 4468 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4469 {
4470 init_psymbol_list (objfile, 1024);
4471 }
4472
492d29ea 4473 TRY
c9bf0622
TT
4474 {
4475 /* This isn't really ideal: all the data we allocate on the
4476 objfile's obstack is still uselessly kept around. However,
4477 freeing it seems unsafe. */
906768f9 4478 psymtab_discarder psymtabs (objfile);
c9bf0622 4479 dwarf2_build_psymtabs_hard (objfile);
906768f9 4480 psymtabs.keep ();
c9bf0622 4481 }
492d29ea
PA
4482 CATCH (except, RETURN_MASK_ERROR)
4483 {
4484 exception_print (gdb_stderr, except);
4485 }
4486 END_CATCH
c906108c 4487}
c906108c 4488
1ce1cefd
DE
4489/* Return the total length of the CU described by HEADER. */
4490
4491static unsigned int
4492get_cu_length (const struct comp_unit_head *header)
4493{
4494 return header->initial_length_size + header->length;
4495}
4496
9c541725 4497/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4498
9c541725
PA
4499static inline bool
4500offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4501{
9c541725
PA
4502 sect_offset bottom = cu_header->sect_off;
4503 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4504
9c541725 4505 return sect_off >= bottom && sect_off < top;
45452591
DE
4506}
4507
3b80fe9b
DE
4508/* Find the base address of the compilation unit for range lists and
4509 location lists. It will normally be specified by DW_AT_low_pc.
4510 In DWARF-3 draft 4, the base address could be overridden by
4511 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4512 compilation units with discontinuous ranges. */
4513
4514static void
4515dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4516{
4517 struct attribute *attr;
4518
4519 cu->base_known = 0;
4520 cu->base_address = 0;
4521
4522 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4523 if (attr)
4524 {
31aa7e4e 4525 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4526 cu->base_known = 1;
4527 }
4528 else
4529 {
4530 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4531 if (attr)
4532 {
31aa7e4e 4533 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4534 cu->base_known = 1;
4535 }
4536 }
4537}
4538
93311388 4539/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4540 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4541 NOTE: This leaves members offset, first_die_offset to be filled in
4542 by the caller. */
107d2387 4543
d521ce57 4544static const gdb_byte *
107d2387 4545read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4546 const gdb_byte *info_ptr,
4547 struct dwarf2_section_info *section,
4548 rcuh_kind section_kind)
107d2387
AC
4549{
4550 int signed_addr;
891d2f0b 4551 unsigned int bytes_read;
43988095
JK
4552 const char *filename = get_section_file_name (section);
4553 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4554
4555 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4556 cu_header->initial_length_size = bytes_read;
4557 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4558 info_ptr += bytes_read;
107d2387
AC
4559 cu_header->version = read_2_bytes (abfd, info_ptr);
4560 info_ptr += 2;
43988095
JK
4561 if (cu_header->version < 5)
4562 switch (section_kind)
4563 {
4564 case rcuh_kind::COMPILE:
4565 cu_header->unit_type = DW_UT_compile;
4566 break;
4567 case rcuh_kind::TYPE:
4568 cu_header->unit_type = DW_UT_type;
4569 break;
4570 default:
4571 internal_error (__FILE__, __LINE__,
4572 _("read_comp_unit_head: invalid section_kind"));
4573 }
4574 else
4575 {
4576 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4577 (read_1_byte (abfd, info_ptr));
4578 info_ptr += 1;
4579 switch (cu_header->unit_type)
4580 {
4581 case DW_UT_compile:
4582 if (section_kind != rcuh_kind::COMPILE)
4583 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4584 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4585 filename);
4586 break;
4587 case DW_UT_type:
4588 section_kind = rcuh_kind::TYPE;
4589 break;
4590 default:
4591 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4592 "(is %d, should be %d or %d) [in module %s]"),
4593 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4594 }
4595
4596 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4597 info_ptr += 1;
4598 }
9c541725
PA
4599 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4600 cu_header,
4601 &bytes_read);
613e1657 4602 info_ptr += bytes_read;
43988095
JK
4603 if (cu_header->version < 5)
4604 {
4605 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4606 info_ptr += 1;
4607 }
107d2387
AC
4608 signed_addr = bfd_get_sign_extend_vma (abfd);
4609 if (signed_addr < 0)
8e65ff28 4610 internal_error (__FILE__, __LINE__,
e2e0b3e5 4611 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4612 cu_header->signed_addr_p = signed_addr;
c764a876 4613
43988095
JK
4614 if (section_kind == rcuh_kind::TYPE)
4615 {
4616 LONGEST type_offset;
4617
4618 cu_header->signature = read_8_bytes (abfd, info_ptr);
4619 info_ptr += 8;
4620
4621 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4622 info_ptr += bytes_read;
9c541725
PA
4623 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4624 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4625 error (_("Dwarf Error: Too big type_offset in compilation unit "
4626 "header (is %s) [in module %s]"), plongest (type_offset),
4627 filename);
4628 }
4629
107d2387
AC
4630 return info_ptr;
4631}
4632
36586728
TT
4633/* Helper function that returns the proper abbrev section for
4634 THIS_CU. */
4635
4636static struct dwarf2_section_info *
4637get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4638{
4639 struct dwarf2_section_info *abbrev;
4640
4641 if (this_cu->is_dwz)
4642 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4643 else
4644 abbrev = &dwarf2_per_objfile->abbrev;
4645
4646 return abbrev;
4647}
4648
9ff913ba
DE
4649/* Subroutine of read_and_check_comp_unit_head and
4650 read_and_check_type_unit_head to simplify them.
4651 Perform various error checking on the header. */
4652
4653static void
4654error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4655 struct dwarf2_section_info *section,
4656 struct dwarf2_section_info *abbrev_section)
9ff913ba 4657{
a32a8923 4658 const char *filename = get_section_file_name (section);
9ff913ba 4659
43988095 4660 if (header->version < 2 || header->version > 5)
9ff913ba 4661 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4662 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4663 filename);
4664
9c541725 4665 if (to_underlying (header->abbrev_sect_off)
36586728 4666 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4667 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4668 "(offset 0x%x + 6) [in module %s]"),
4669 to_underlying (header->abbrev_sect_off),
4670 to_underlying (header->sect_off),
9ff913ba
DE
4671 filename);
4672
9c541725 4673 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4674 avoid potential 32-bit overflow. */
9c541725 4675 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4676 > section->size)
9c541725
PA
4677 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4678 "(offset 0x%x + 0) [in module %s]"),
4679 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4680 filename);
4681}
4682
4683/* Read in a CU/TU header and perform some basic error checking.
4684 The contents of the header are stored in HEADER.
4685 The result is a pointer to the start of the first DIE. */
adabb602 4686
d521ce57 4687static const gdb_byte *
9ff913ba
DE
4688read_and_check_comp_unit_head (struct comp_unit_head *header,
4689 struct dwarf2_section_info *section,
4bdcc0c1 4690 struct dwarf2_section_info *abbrev_section,
d521ce57 4691 const gdb_byte *info_ptr,
43988095 4692 rcuh_kind section_kind)
72bf9492 4693{
d521ce57 4694 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4695 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4696
9c541725 4697 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4698
43988095 4699 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4700
9c541725 4701 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4702
4bdcc0c1 4703 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4704
4705 return info_ptr;
348e048f
DE
4706}
4707
f4dc4d17
DE
4708/* Fetch the abbreviation table offset from a comp or type unit header. */
4709
4710static sect_offset
4711read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4712 sect_offset sect_off)
f4dc4d17 4713{
a32a8923 4714 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4715 const gdb_byte *info_ptr;
ac298888 4716 unsigned int initial_length_size, offset_size;
43988095 4717 uint16_t version;
f4dc4d17
DE
4718
4719 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4720 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4721 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4722 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4723 info_ptr += initial_length_size;
4724
4725 version = read_2_bytes (abfd, info_ptr);
4726 info_ptr += 2;
4727 if (version >= 5)
4728 {
4729 /* Skip unit type and address size. */
4730 info_ptr += 2;
4731 }
4732
9c541725 4733 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4734}
4735
aaa75496
JB
4736/* Allocate a new partial symtab for file named NAME and mark this new
4737 partial symtab as being an include of PST. */
4738
4739static void
d521ce57 4740dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4741 struct objfile *objfile)
4742{
4743 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4744
fbd9ab74
JK
4745 if (!IS_ABSOLUTE_PATH (subpst->filename))
4746 {
4747 /* It shares objfile->objfile_obstack. */
4748 subpst->dirname = pst->dirname;
4749 }
4750
aaa75496
JB
4751 subpst->textlow = 0;
4752 subpst->texthigh = 0;
4753
8d749320
SM
4754 subpst->dependencies
4755 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4756 subpst->dependencies[0] = pst;
4757 subpst->number_of_dependencies = 1;
4758
4759 subpst->globals_offset = 0;
4760 subpst->n_global_syms = 0;
4761 subpst->statics_offset = 0;
4762 subpst->n_static_syms = 0;
43f3e411 4763 subpst->compunit_symtab = NULL;
aaa75496
JB
4764 subpst->read_symtab = pst->read_symtab;
4765 subpst->readin = 0;
4766
4767 /* No private part is necessary for include psymtabs. This property
4768 can be used to differentiate between such include psymtabs and
10b3939b 4769 the regular ones. */
58a9656e 4770 subpst->read_symtab_private = NULL;
aaa75496
JB
4771}
4772
4773/* Read the Line Number Program data and extract the list of files
4774 included by the source file represented by PST. Build an include
d85a05f0 4775 partial symtab for each of these included files. */
aaa75496
JB
4776
4777static void
4778dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4779 struct die_info *die,
4780 struct partial_symtab *pst)
aaa75496 4781{
fff8551c 4782 line_header_up lh;
d85a05f0 4783 struct attribute *attr;
aaa75496 4784
d85a05f0
DJ
4785 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4786 if (attr)
9c541725 4787 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4788 if (lh == NULL)
4789 return; /* No linetable, so no includes. */
4790
c6da4cef 4791 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4792 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4793}
4794
348e048f 4795static hashval_t
52dc124a 4796hash_signatured_type (const void *item)
348e048f 4797{
9a3c8263
SM
4798 const struct signatured_type *sig_type
4799 = (const struct signatured_type *) item;
9a619af0 4800
348e048f 4801 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4802 return sig_type->signature;
348e048f
DE
4803}
4804
4805static int
52dc124a 4806eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4807{
9a3c8263
SM
4808 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4809 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4810
348e048f
DE
4811 return lhs->signature == rhs->signature;
4812}
4813
1fd400ff
TT
4814/* Allocate a hash table for signatured types. */
4815
4816static htab_t
673bfd45 4817allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4818{
4819 return htab_create_alloc_ex (41,
52dc124a
DE
4820 hash_signatured_type,
4821 eq_signatured_type,
1fd400ff
TT
4822 NULL,
4823 &objfile->objfile_obstack,
4824 hashtab_obstack_allocate,
4825 dummy_obstack_deallocate);
4826}
4827
d467dd73 4828/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4829
4830static int
d467dd73 4831add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4832{
9a3c8263
SM
4833 struct signatured_type *sigt = (struct signatured_type *) *slot;
4834 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4835
b4dd5633 4836 **datap = sigt;
1fd400ff
TT
4837 ++*datap;
4838
4839 return 1;
4840}
4841
78d4d2c5 4842/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4843 and fill them into TYPES_HTAB. It will process only type units,
4844 therefore DW_UT_type. */
c88ee1f0 4845
78d4d2c5
JK
4846static void
4847create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4848 dwarf2_section_info *section, htab_t &types_htab,
4849 rcuh_kind section_kind)
348e048f 4850{
3019eac3 4851 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4852 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4853 bfd *abfd;
4854 const gdb_byte *info_ptr, *end_ptr;
348e048f 4855
4bdcc0c1
DE
4856 abbrev_section = (dwo_file != NULL
4857 ? &dwo_file->sections.abbrev
4858 : &dwarf2_per_objfile->abbrev);
4859
b4f54984 4860 if (dwarf_read_debug)
43988095
JK
4861 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4862 get_section_name (section),
a32a8923 4863 get_section_file_name (abbrev_section));
09406207 4864
78d4d2c5
JK
4865 dwarf2_read_section (objfile, section);
4866 info_ptr = section->buffer;
348e048f 4867
78d4d2c5
JK
4868 if (info_ptr == NULL)
4869 return;
348e048f 4870
78d4d2c5
JK
4871 /* We can't set abfd until now because the section may be empty or
4872 not present, in which case the bfd is unknown. */
4873 abfd = get_section_bfd_owner (section);
348e048f 4874
78d4d2c5
JK
4875 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4876 because we don't need to read any dies: the signature is in the
4877 header. */
3019eac3 4878
78d4d2c5
JK
4879 end_ptr = info_ptr + section->size;
4880 while (info_ptr < end_ptr)
4881 {
78d4d2c5
JK
4882 struct signatured_type *sig_type;
4883 struct dwo_unit *dwo_tu;
4884 void **slot;
4885 const gdb_byte *ptr = info_ptr;
4886 struct comp_unit_head header;
4887 unsigned int length;
8b70b953 4888
9c541725 4889 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4890
a49dd8dd
JK
4891 /* Initialize it due to a false compiler warning. */
4892 header.signature = -1;
9c541725 4893 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4894
78d4d2c5
JK
4895 /* We need to read the type's signature in order to build the hash
4896 table, but we don't need anything else just yet. */
348e048f 4897
43988095
JK
4898 ptr = read_and_check_comp_unit_head (&header, section,
4899 abbrev_section, ptr, section_kind);
348e048f 4900
78d4d2c5 4901 length = get_cu_length (&header);
6caca83c 4902
78d4d2c5
JK
4903 /* Skip dummy type units. */
4904 if (ptr >= info_ptr + length
43988095
JK
4905 || peek_abbrev_code (abfd, ptr) == 0
4906 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4907 {
4908 info_ptr += length;
4909 continue;
4910 }
dee91e82 4911
78d4d2c5
JK
4912 if (types_htab == NULL)
4913 {
4914 if (dwo_file)
4915 types_htab = allocate_dwo_unit_table (objfile);
4916 else
4917 types_htab = allocate_signatured_type_table (objfile);
4918 }
8b70b953 4919
78d4d2c5
JK
4920 if (dwo_file)
4921 {
4922 sig_type = NULL;
4923 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4924 struct dwo_unit);
4925 dwo_tu->dwo_file = dwo_file;
43988095 4926 dwo_tu->signature = header.signature;
9c541725 4927 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4928 dwo_tu->section = section;
9c541725 4929 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4930 dwo_tu->length = length;
4931 }
4932 else
4933 {
4934 /* N.B.: type_offset is not usable if this type uses a DWO file.
4935 The real type_offset is in the DWO file. */
4936 dwo_tu = NULL;
4937 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4938 struct signatured_type);
43988095 4939 sig_type->signature = header.signature;
9c541725 4940 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4941 sig_type->per_cu.objfile = objfile;
4942 sig_type->per_cu.is_debug_types = 1;
4943 sig_type->per_cu.section = section;
9c541725 4944 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4945 sig_type->per_cu.length = length;
4946 }
4947
4948 slot = htab_find_slot (types_htab,
4949 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4950 INSERT);
4951 gdb_assert (slot != NULL);
4952 if (*slot != NULL)
4953 {
9c541725 4954 sect_offset dup_sect_off;
0349ea22 4955
3019eac3
DE
4956 if (dwo_file)
4957 {
78d4d2c5
JK
4958 const struct dwo_unit *dup_tu
4959 = (const struct dwo_unit *) *slot;
4960
9c541725 4961 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4962 }
4963 else
4964 {
78d4d2c5
JK
4965 const struct signatured_type *dup_tu
4966 = (const struct signatured_type *) *slot;
4967
9c541725 4968 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4969 }
8b70b953 4970
78d4d2c5
JK
4971 complaint (&symfile_complaints,
4972 _("debug type entry at offset 0x%x is duplicate to"
4973 " the entry at offset 0x%x, signature %s"),
9c541725 4974 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4975 hex_string (header.signature));
78d4d2c5
JK
4976 }
4977 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4978
78d4d2c5
JK
4979 if (dwarf_read_debug > 1)
4980 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4981 to_underlying (sect_off),
43988095 4982 hex_string (header.signature));
3019eac3 4983
78d4d2c5
JK
4984 info_ptr += length;
4985 }
4986}
3019eac3 4987
78d4d2c5
JK
4988/* Create the hash table of all entries in the .debug_types
4989 (or .debug_types.dwo) section(s).
4990 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4991 otherwise it is NULL.
b3c8eb43 4992
78d4d2c5 4993 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4994
78d4d2c5 4995 Note: This function processes DWO files only, not DWP files. */
348e048f 4996
78d4d2c5
JK
4997static void
4998create_debug_types_hash_table (struct dwo_file *dwo_file,
4999 VEC (dwarf2_section_info_def) *types,
5000 htab_t &types_htab)
5001{
5002 int ix;
5003 struct dwarf2_section_info *section;
5004
5005 if (VEC_empty (dwarf2_section_info_def, types))
5006 return;
348e048f 5007
78d4d2c5
JK
5008 for (ix = 0;
5009 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5010 ++ix)
43988095
JK
5011 create_debug_type_hash_table (dwo_file, section, types_htab,
5012 rcuh_kind::TYPE);
3019eac3
DE
5013}
5014
5015/* Create the hash table of all entries in the .debug_types section,
5016 and initialize all_type_units.
5017 The result is zero if there is an error (e.g. missing .debug_types section),
5018 otherwise non-zero. */
5019
5020static int
5021create_all_type_units (struct objfile *objfile)
5022{
78d4d2c5 5023 htab_t types_htab = NULL;
b4dd5633 5024 struct signatured_type **iter;
3019eac3 5025
43988095
JK
5026 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5027 rcuh_kind::COMPILE);
78d4d2c5 5028 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5029 if (types_htab == NULL)
5030 {
5031 dwarf2_per_objfile->signatured_types = NULL;
5032 return 0;
5033 }
5034
348e048f
DE
5035 dwarf2_per_objfile->signatured_types = types_htab;
5036
6aa5f3a6
DE
5037 dwarf2_per_objfile->n_type_units
5038 = dwarf2_per_objfile->n_allocated_type_units
5039 = htab_elements (types_htab);
8d749320
SM
5040 dwarf2_per_objfile->all_type_units =
5041 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5042 iter = &dwarf2_per_objfile->all_type_units[0];
5043 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5044 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5045 == dwarf2_per_objfile->n_type_units);
1fd400ff 5046
348e048f
DE
5047 return 1;
5048}
5049
6aa5f3a6
DE
5050/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5051 If SLOT is non-NULL, it is the entry to use in the hash table.
5052 Otherwise we find one. */
5053
5054static struct signatured_type *
5055add_type_unit (ULONGEST sig, void **slot)
5056{
5057 struct objfile *objfile = dwarf2_per_objfile->objfile;
5058 int n_type_units = dwarf2_per_objfile->n_type_units;
5059 struct signatured_type *sig_type;
5060
5061 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5062 ++n_type_units;
5063 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5064 {
5065 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5066 dwarf2_per_objfile->n_allocated_type_units = 1;
5067 dwarf2_per_objfile->n_allocated_type_units *= 2;
5068 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5069 = XRESIZEVEC (struct signatured_type *,
5070 dwarf2_per_objfile->all_type_units,
5071 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5072 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5073 }
5074 dwarf2_per_objfile->n_type_units = n_type_units;
5075
5076 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5077 struct signatured_type);
5078 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5079 sig_type->signature = sig;
5080 sig_type->per_cu.is_debug_types = 1;
5081 if (dwarf2_per_objfile->using_index)
5082 {
5083 sig_type->per_cu.v.quick =
5084 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5085 struct dwarf2_per_cu_quick_data);
5086 }
5087
5088 if (slot == NULL)
5089 {
5090 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5091 sig_type, INSERT);
5092 }
5093 gdb_assert (*slot == NULL);
5094 *slot = sig_type;
5095 /* The rest of sig_type must be filled in by the caller. */
5096 return sig_type;
5097}
5098
a2ce51a0
DE
5099/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5100 Fill in SIG_ENTRY with DWO_ENTRY. */
5101
5102static void
5103fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5104 struct signatured_type *sig_entry,
5105 struct dwo_unit *dwo_entry)
5106{
7ee85ab1 5107 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5108 gdb_assert (! sig_entry->per_cu.queued);
5109 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5110 if (dwarf2_per_objfile->using_index)
5111 {
5112 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5113 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5114 }
5115 else
5116 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5117 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5118 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5119 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5120 gdb_assert (sig_entry->dwo_unit == NULL);
5121
5122 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5123 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5124 sig_entry->per_cu.length = dwo_entry->length;
5125 sig_entry->per_cu.reading_dwo_directly = 1;
5126 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5127 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5128 sig_entry->dwo_unit = dwo_entry;
5129}
5130
5131/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5132 If we haven't read the TU yet, create the signatured_type data structure
5133 for a TU to be read in directly from a DWO file, bypassing the stub.
5134 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5135 using .gdb_index, then when reading a CU we want to stay in the DWO file
5136 containing that CU. Otherwise we could end up reading several other DWO
5137 files (due to comdat folding) to process the transitive closure of all the
5138 mentioned TUs, and that can be slow. The current DWO file will have every
5139 type signature that it needs.
a2ce51a0
DE
5140 We only do this for .gdb_index because in the psymtab case we already have
5141 to read all the DWOs to build the type unit groups. */
5142
5143static struct signatured_type *
5144lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5145{
5146 struct objfile *objfile = dwarf2_per_objfile->objfile;
5147 struct dwo_file *dwo_file;
5148 struct dwo_unit find_dwo_entry, *dwo_entry;
5149 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5150 void **slot;
a2ce51a0
DE
5151
5152 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5153
6aa5f3a6
DE
5154 /* If TU skeletons have been removed then we may not have read in any
5155 TUs yet. */
5156 if (dwarf2_per_objfile->signatured_types == NULL)
5157 {
5158 dwarf2_per_objfile->signatured_types
5159 = allocate_signatured_type_table (objfile);
5160 }
a2ce51a0
DE
5161
5162 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5163 Use the global signatured_types array to do our own comdat-folding
5164 of types. If this is the first time we're reading this TU, and
5165 the TU has an entry in .gdb_index, replace the recorded data from
5166 .gdb_index with this TU. */
a2ce51a0 5167
a2ce51a0 5168 find_sig_entry.signature = sig;
6aa5f3a6
DE
5169 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5170 &find_sig_entry, INSERT);
9a3c8263 5171 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5172
5173 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5174 read. Don't reassign the global entry to point to this DWO if that's
5175 the case. Also note that if the TU is already being read, it may not
5176 have come from a DWO, the program may be a mix of Fission-compiled
5177 code and non-Fission-compiled code. */
5178
5179 /* Have we already tried to read this TU?
5180 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5181 needn't exist in the global table yet). */
5182 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5183 return sig_entry;
5184
6aa5f3a6
DE
5185 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5186 dwo_unit of the TU itself. */
5187 dwo_file = cu->dwo_unit->dwo_file;
5188
a2ce51a0
DE
5189 /* Ok, this is the first time we're reading this TU. */
5190 if (dwo_file->tus == NULL)
5191 return NULL;
5192 find_dwo_entry.signature = sig;
9a3c8263 5193 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5194 if (dwo_entry == NULL)
5195 return NULL;
5196
6aa5f3a6
DE
5197 /* If the global table doesn't have an entry for this TU, add one. */
5198 if (sig_entry == NULL)
5199 sig_entry = add_type_unit (sig, slot);
5200
a2ce51a0 5201 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5202 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5203 return sig_entry;
5204}
5205
a2ce51a0
DE
5206/* Subroutine of lookup_signatured_type.
5207 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5208 then try the DWP file. If the TU stub (skeleton) has been removed then
5209 it won't be in .gdb_index. */
a2ce51a0
DE
5210
5211static struct signatured_type *
5212lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5213{
5214 struct objfile *objfile = dwarf2_per_objfile->objfile;
5215 struct dwp_file *dwp_file = get_dwp_file ();
5216 struct dwo_unit *dwo_entry;
5217 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5218 void **slot;
a2ce51a0
DE
5219
5220 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5221 gdb_assert (dwp_file != NULL);
5222
6aa5f3a6
DE
5223 /* If TU skeletons have been removed then we may not have read in any
5224 TUs yet. */
5225 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5226 {
6aa5f3a6
DE
5227 dwarf2_per_objfile->signatured_types
5228 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5229 }
5230
6aa5f3a6
DE
5231 find_sig_entry.signature = sig;
5232 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5233 &find_sig_entry, INSERT);
9a3c8263 5234 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5235
5236 /* Have we already tried to read this TU?
5237 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5238 needn't exist in the global table yet). */
5239 if (sig_entry != NULL)
5240 return sig_entry;
5241
a2ce51a0
DE
5242 if (dwp_file->tus == NULL)
5243 return NULL;
57d63ce2
DE
5244 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5245 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5246 if (dwo_entry == NULL)
5247 return NULL;
5248
6aa5f3a6 5249 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5250 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5251
a2ce51a0
DE
5252 return sig_entry;
5253}
5254
380bca97 5255/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5256 Returns NULL if signature SIG is not present in the table.
5257 It is up to the caller to complain about this. */
348e048f
DE
5258
5259static struct signatured_type *
a2ce51a0 5260lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5261{
a2ce51a0
DE
5262 if (cu->dwo_unit
5263 && dwarf2_per_objfile->using_index)
5264 {
5265 /* We're in a DWO/DWP file, and we're using .gdb_index.
5266 These cases require special processing. */
5267 if (get_dwp_file () == NULL)
5268 return lookup_dwo_signatured_type (cu, sig);
5269 else
5270 return lookup_dwp_signatured_type (cu, sig);
5271 }
5272 else
5273 {
5274 struct signatured_type find_entry, *entry;
348e048f 5275
a2ce51a0
DE
5276 if (dwarf2_per_objfile->signatured_types == NULL)
5277 return NULL;
5278 find_entry.signature = sig;
9a3c8263
SM
5279 entry = ((struct signatured_type *)
5280 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5281 return entry;
5282 }
348e048f 5283}
42e7ad6c
DE
5284\f
5285/* Low level DIE reading support. */
348e048f 5286
d85a05f0
DJ
5287/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5288
5289static void
5290init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5291 struct dwarf2_cu *cu,
3019eac3
DE
5292 struct dwarf2_section_info *section,
5293 struct dwo_file *dwo_file)
d85a05f0 5294{
fceca515 5295 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5296 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5297 reader->cu = cu;
3019eac3 5298 reader->dwo_file = dwo_file;
dee91e82
DE
5299 reader->die_section = section;
5300 reader->buffer = section->buffer;
f664829e 5301 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5302 reader->comp_dir = NULL;
d85a05f0
DJ
5303}
5304
b0c7bfa9
DE
5305/* Subroutine of init_cutu_and_read_dies to simplify it.
5306 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5307 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5308 already.
5309
5310 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5311 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5312 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5313 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5314 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5315 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5316 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5317 are filled in with the info of the DIE from the DWO file.
5318 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5319 provided an abbrev table to use.
5320 The result is non-zero if a valid (non-dummy) DIE was found. */
5321
5322static int
5323read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5324 struct dwo_unit *dwo_unit,
5325 int abbrev_table_provided,
5326 struct die_info *stub_comp_unit_die,
a2ce51a0 5327 const char *stub_comp_dir,
b0c7bfa9 5328 struct die_reader_specs *result_reader,
d521ce57 5329 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5330 struct die_info **result_comp_unit_die,
5331 int *result_has_children)
5332{
5333 struct objfile *objfile = dwarf2_per_objfile->objfile;
5334 struct dwarf2_cu *cu = this_cu->cu;
5335 struct dwarf2_section_info *section;
5336 bfd *abfd;
d521ce57 5337 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5338 ULONGEST signature; /* Or dwo_id. */
5339 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5340 int i,num_extra_attrs;
5341 struct dwarf2_section_info *dwo_abbrev_section;
5342 struct attribute *attr;
5343 struct die_info *comp_unit_die;
5344
b0aeadb3
DE
5345 /* At most one of these may be provided. */
5346 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5347
b0c7bfa9
DE
5348 /* These attributes aren't processed until later:
5349 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5350 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5351 referenced later. However, these attributes are found in the stub
5352 which we won't have later. In order to not impose this complication
5353 on the rest of the code, we read them here and copy them to the
5354 DWO CU/TU die. */
b0c7bfa9
DE
5355
5356 stmt_list = NULL;
5357 low_pc = NULL;
5358 high_pc = NULL;
5359 ranges = NULL;
5360 comp_dir = NULL;
5361
5362 if (stub_comp_unit_die != NULL)
5363 {
5364 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5365 DWO file. */
5366 if (! this_cu->is_debug_types)
5367 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5368 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5369 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5370 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5371 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5372
5373 /* There should be a DW_AT_addr_base attribute here (if needed).
5374 We need the value before we can process DW_FORM_GNU_addr_index. */
5375 cu->addr_base = 0;
5376 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5377 if (attr)
5378 cu->addr_base = DW_UNSND (attr);
5379
5380 /* There should be a DW_AT_ranges_base attribute here (if needed).
5381 We need the value before we can process DW_AT_ranges. */
5382 cu->ranges_base = 0;
5383 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5384 if (attr)
5385 cu->ranges_base = DW_UNSND (attr);
5386 }
a2ce51a0
DE
5387 else if (stub_comp_dir != NULL)
5388 {
5389 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5390 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5391 comp_dir->name = DW_AT_comp_dir;
5392 comp_dir->form = DW_FORM_string;
5393 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5394 DW_STRING (comp_dir) = stub_comp_dir;
5395 }
b0c7bfa9
DE
5396
5397 /* Set up for reading the DWO CU/TU. */
5398 cu->dwo_unit = dwo_unit;
5399 section = dwo_unit->section;
5400 dwarf2_read_section (objfile, section);
a32a8923 5401 abfd = get_section_bfd_owner (section);
9c541725
PA
5402 begin_info_ptr = info_ptr = (section->buffer
5403 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5404 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5405 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5406
5407 if (this_cu->is_debug_types)
5408 {
b0c7bfa9
DE
5409 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5410
43988095 5411 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5412 dwo_abbrev_section,
43988095 5413 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5414 /* This is not an assert because it can be caused by bad debug info. */
43988095 5415 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5416 {
5417 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5418 " TU at offset 0x%x [in module %s]"),
5419 hex_string (sig_type->signature),
43988095 5420 hex_string (cu->header.signature),
9c541725 5421 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5422 bfd_get_filename (abfd));
5423 }
9c541725 5424 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5425 /* For DWOs coming from DWP files, we don't know the CU length
5426 nor the type's offset in the TU until now. */
5427 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5428 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5429
5430 /* Establish the type offset that can be used to lookup the type.
5431 For DWO files, we don't know it until now. */
9c541725
PA
5432 sig_type->type_offset_in_section
5433 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5434 }
5435 else
5436 {
5437 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5438 dwo_abbrev_section,
43988095 5439 info_ptr, rcuh_kind::COMPILE);
9c541725 5440 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5441 /* For DWOs coming from DWP files, we don't know the CU length
5442 until now. */
5443 dwo_unit->length = get_cu_length (&cu->header);
5444 }
5445
02142a6c
DE
5446 /* Replace the CU's original abbrev table with the DWO's.
5447 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5448 if (abbrev_table_provided)
5449 {
5450 /* Don't free the provided abbrev table, the caller of
5451 init_cutu_and_read_dies owns it. */
5452 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5453 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5454 make_cleanup (dwarf2_free_abbrev_table, cu);
5455 }
5456 else
5457 {
5458 dwarf2_free_abbrev_table (cu);
5459 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5460 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5461 }
5462
5463 /* Read in the die, but leave space to copy over the attributes
5464 from the stub. This has the benefit of simplifying the rest of
5465 the code - all the work to maintain the illusion of a single
5466 DW_TAG_{compile,type}_unit DIE is done here. */
5467 num_extra_attrs = ((stmt_list != NULL)
5468 + (low_pc != NULL)
5469 + (high_pc != NULL)
5470 + (ranges != NULL)
5471 + (comp_dir != NULL));
5472 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5473 result_has_children, num_extra_attrs);
5474
5475 /* Copy over the attributes from the stub to the DIE we just read in. */
5476 comp_unit_die = *result_comp_unit_die;
5477 i = comp_unit_die->num_attrs;
5478 if (stmt_list != NULL)
5479 comp_unit_die->attrs[i++] = *stmt_list;
5480 if (low_pc != NULL)
5481 comp_unit_die->attrs[i++] = *low_pc;
5482 if (high_pc != NULL)
5483 comp_unit_die->attrs[i++] = *high_pc;
5484 if (ranges != NULL)
5485 comp_unit_die->attrs[i++] = *ranges;
5486 if (comp_dir != NULL)
5487 comp_unit_die->attrs[i++] = *comp_dir;
5488 comp_unit_die->num_attrs += num_extra_attrs;
5489
b4f54984 5490 if (dwarf_die_debug)
bf6af496
DE
5491 {
5492 fprintf_unfiltered (gdb_stdlog,
5493 "Read die from %s@0x%x of %s:\n",
a32a8923 5494 get_section_name (section),
bf6af496
DE
5495 (unsigned) (begin_info_ptr - section->buffer),
5496 bfd_get_filename (abfd));
b4f54984 5497 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5498 }
5499
a2ce51a0
DE
5500 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5501 TUs by skipping the stub and going directly to the entry in the DWO file.
5502 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5503 to get it via circuitous means. Blech. */
5504 if (comp_dir != NULL)
5505 result_reader->comp_dir = DW_STRING (comp_dir);
5506
b0c7bfa9
DE
5507 /* Skip dummy compilation units. */
5508 if (info_ptr >= begin_info_ptr + dwo_unit->length
5509 || peek_abbrev_code (abfd, info_ptr) == 0)
5510 return 0;
5511
5512 *result_info_ptr = info_ptr;
5513 return 1;
5514}
5515
5516/* Subroutine of init_cutu_and_read_dies to simplify it.
5517 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5518 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5519
5520static struct dwo_unit *
5521lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5522 struct die_info *comp_unit_die)
5523{
5524 struct dwarf2_cu *cu = this_cu->cu;
5525 struct attribute *attr;
5526 ULONGEST signature;
5527 struct dwo_unit *dwo_unit;
5528 const char *comp_dir, *dwo_name;
5529
a2ce51a0
DE
5530 gdb_assert (cu != NULL);
5531
b0c7bfa9 5532 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5533 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5534 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5535
5536 if (this_cu->is_debug_types)
5537 {
5538 struct signatured_type *sig_type;
5539
5540 /* Since this_cu is the first member of struct signatured_type,
5541 we can go from a pointer to one to a pointer to the other. */
5542 sig_type = (struct signatured_type *) this_cu;
5543 signature = sig_type->signature;
5544 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5545 }
5546 else
5547 {
5548 struct attribute *attr;
5549
5550 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5551 if (! attr)
5552 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5553 " [in module %s]"),
4262abfb 5554 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5555 signature = DW_UNSND (attr);
5556 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5557 signature);
5558 }
5559
b0c7bfa9
DE
5560 return dwo_unit;
5561}
5562
a2ce51a0 5563/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5564 See it for a description of the parameters.
5565 Read a TU directly from a DWO file, bypassing the stub.
5566
5567 Note: This function could be a little bit simpler if we shared cleanups
5568 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5569 to do, so we keep this function self-contained. Or we could move this
5570 into our caller, but it's complex enough already. */
a2ce51a0
DE
5571
5572static void
6aa5f3a6
DE
5573init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5574 int use_existing_cu, int keep,
a2ce51a0
DE
5575 die_reader_func_ftype *die_reader_func,
5576 void *data)
5577{
5578 struct dwarf2_cu *cu;
5579 struct signatured_type *sig_type;
6aa5f3a6 5580 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5581 struct die_reader_specs reader;
5582 const gdb_byte *info_ptr;
5583 struct die_info *comp_unit_die;
5584 int has_children;
5585
5586 /* Verify we can do the following downcast, and that we have the
5587 data we need. */
5588 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5589 sig_type = (struct signatured_type *) this_cu;
5590 gdb_assert (sig_type->dwo_unit != NULL);
5591
5592 cleanups = make_cleanup (null_cleanup, NULL);
5593
6aa5f3a6
DE
5594 if (use_existing_cu && this_cu->cu != NULL)
5595 {
5596 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5597 cu = this_cu->cu;
5598 /* There's no need to do the rereading_dwo_cu handling that
5599 init_cutu_and_read_dies does since we don't read the stub. */
5600 }
5601 else
5602 {
5603 /* If !use_existing_cu, this_cu->cu must be NULL. */
5604 gdb_assert (this_cu->cu == NULL);
8d749320 5605 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5606 init_one_comp_unit (cu, this_cu);
5607 /* If an error occurs while loading, release our storage. */
5608 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5609 }
5610
5611 /* A future optimization, if needed, would be to use an existing
5612 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5613 could share abbrev tables. */
a2ce51a0
DE
5614
5615 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5616 0 /* abbrev_table_provided */,
5617 NULL /* stub_comp_unit_die */,
5618 sig_type->dwo_unit->dwo_file->comp_dir,
5619 &reader, &info_ptr,
5620 &comp_unit_die, &has_children) == 0)
5621 {
5622 /* Dummy die. */
5623 do_cleanups (cleanups);
5624 return;
5625 }
5626
5627 /* All the "real" work is done here. */
5628 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5629
6aa5f3a6 5630 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5631 but the alternative is making the latter more complex.
5632 This function is only for the special case of using DWO files directly:
5633 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5634 if (free_cu_cleanup != NULL)
a2ce51a0 5635 {
6aa5f3a6
DE
5636 if (keep)
5637 {
5638 /* We've successfully allocated this compilation unit. Let our
5639 caller clean it up when finished with it. */
5640 discard_cleanups (free_cu_cleanup);
a2ce51a0 5641
6aa5f3a6
DE
5642 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5643 So we have to manually free the abbrev table. */
5644 dwarf2_free_abbrev_table (cu);
a2ce51a0 5645
6aa5f3a6
DE
5646 /* Link this CU into read_in_chain. */
5647 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5648 dwarf2_per_objfile->read_in_chain = this_cu;
5649 }
5650 else
5651 do_cleanups (free_cu_cleanup);
a2ce51a0 5652 }
a2ce51a0
DE
5653
5654 do_cleanups (cleanups);
5655}
5656
fd820528 5657/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5658 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5659
f4dc4d17
DE
5660 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5661 Otherwise the table specified in the comp unit header is read in and used.
5662 This is an optimization for when we already have the abbrev table.
5663
dee91e82
DE
5664 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5665 Otherwise, a new CU is allocated with xmalloc.
5666
5667 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5668 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5669
5670 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5671 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5672
70221824 5673static void
fd820528 5674init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5675 struct abbrev_table *abbrev_table,
fd820528
DE
5676 int use_existing_cu, int keep,
5677 die_reader_func_ftype *die_reader_func,
5678 void *data)
c906108c 5679{
dee91e82 5680 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5681 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5682 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5683 struct dwarf2_cu *cu;
d521ce57 5684 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5685 struct die_reader_specs reader;
d85a05f0 5686 struct die_info *comp_unit_die;
dee91e82 5687 int has_children;
d85a05f0 5688 struct attribute *attr;
365156ad 5689 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5690 struct signatured_type *sig_type = NULL;
4bdcc0c1 5691 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5692 /* Non-zero if CU currently points to a DWO file and we need to
5693 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5694 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5695 int rereading_dwo_cu = 0;
c906108c 5696
b4f54984 5697 if (dwarf_die_debug)
09406207
DE
5698 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5699 this_cu->is_debug_types ? "type" : "comp",
9c541725 5700 to_underlying (this_cu->sect_off));
09406207 5701
dee91e82
DE
5702 if (use_existing_cu)
5703 gdb_assert (keep);
23745b47 5704
a2ce51a0
DE
5705 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5706 file (instead of going through the stub), short-circuit all of this. */
5707 if (this_cu->reading_dwo_directly)
5708 {
5709 /* Narrow down the scope of possibilities to have to understand. */
5710 gdb_assert (this_cu->is_debug_types);
5711 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5712 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5713 die_reader_func, data);
a2ce51a0
DE
5714 return;
5715 }
5716
dee91e82
DE
5717 cleanups = make_cleanup (null_cleanup, NULL);
5718
5719 /* This is cheap if the section is already read in. */
5720 dwarf2_read_section (objfile, section);
5721
9c541725 5722 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5723
5724 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5725
5726 if (use_existing_cu && this_cu->cu != NULL)
5727 {
5728 cu = this_cu->cu;
42e7ad6c
DE
5729 /* If this CU is from a DWO file we need to start over, we need to
5730 refetch the attributes from the skeleton CU.
5731 This could be optimized by retrieving those attributes from when we
5732 were here the first time: the previous comp_unit_die was stored in
5733 comp_unit_obstack. But there's no data yet that we need this
5734 optimization. */
5735 if (cu->dwo_unit != NULL)
5736 rereading_dwo_cu = 1;
dee91e82
DE
5737 }
5738 else
5739 {
5740 /* If !use_existing_cu, this_cu->cu must be NULL. */
5741 gdb_assert (this_cu->cu == NULL);
8d749320 5742 cu = XNEW (struct dwarf2_cu);
dee91e82 5743 init_one_comp_unit (cu, this_cu);
dee91e82 5744 /* If an error occurs while loading, release our storage. */
365156ad 5745 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5746 }
dee91e82 5747
b0c7bfa9 5748 /* Get the header. */
9c541725 5749 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5750 {
5751 /* We already have the header, there's no need to read it in again. */
9c541725 5752 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5753 }
5754 else
5755 {
3019eac3 5756 if (this_cu->is_debug_types)
dee91e82 5757 {
43988095 5758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5759 abbrev_section, info_ptr,
43988095 5760 rcuh_kind::TYPE);
dee91e82 5761
42e7ad6c
DE
5762 /* Since per_cu is the first member of struct signatured_type,
5763 we can go from a pointer to one to a pointer to the other. */
5764 sig_type = (struct signatured_type *) this_cu;
43988095 5765 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5766 gdb_assert (sig_type->type_offset_in_tu
5767 == cu->header.type_cu_offset_in_tu);
5768 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5769
42e7ad6c
DE
5770 /* LENGTH has not been set yet for type units if we're
5771 using .gdb_index. */
1ce1cefd 5772 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5773
5774 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5775 sig_type->type_offset_in_section =
5776 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5777
5778 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5779 }
5780 else
5781 {
4bdcc0c1
DE
5782 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5783 abbrev_section,
43988095
JK
5784 info_ptr,
5785 rcuh_kind::COMPILE);
dee91e82 5786
9c541725 5787 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5788 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5789 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5790 }
5791 }
10b3939b 5792
6caca83c 5793 /* Skip dummy compilation units. */
dee91e82 5794 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5795 || peek_abbrev_code (abfd, info_ptr) == 0)
5796 {
dee91e82 5797 do_cleanups (cleanups);
21b2bd31 5798 return;
6caca83c
CC
5799 }
5800
433df2d4
DE
5801 /* If we don't have them yet, read the abbrevs for this compilation unit.
5802 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5803 done. Note that it's important that if the CU had an abbrev table
5804 on entry we don't free it when we're done: Somewhere up the call stack
5805 it may be in use. */
f4dc4d17
DE
5806 if (abbrev_table != NULL)
5807 {
5808 gdb_assert (cu->abbrev_table == NULL);
9c541725 5809 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5810 cu->abbrev_table = abbrev_table;
5811 }
5812 else if (cu->abbrev_table == NULL)
dee91e82 5813 {
4bdcc0c1 5814 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5815 make_cleanup (dwarf2_free_abbrev_table, cu);
5816 }
42e7ad6c
DE
5817 else if (rereading_dwo_cu)
5818 {
5819 dwarf2_free_abbrev_table (cu);
5820 dwarf2_read_abbrevs (cu, abbrev_section);
5821 }
af703f96 5822
dee91e82 5823 /* Read the top level CU/TU die. */
3019eac3 5824 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5825 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5826
b0c7bfa9
DE
5827 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5828 from the DWO file.
5829 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5830 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5831 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5832 if (attr)
5833 {
3019eac3 5834 struct dwo_unit *dwo_unit;
b0c7bfa9 5835 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5836
5837 if (has_children)
6a506a2d
DE
5838 {
5839 complaint (&symfile_complaints,
5840 _("compilation unit with DW_AT_GNU_dwo_name"
5841 " has children (offset 0x%x) [in module %s]"),
9c541725 5842 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5843 }
b0c7bfa9 5844 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5845 if (dwo_unit != NULL)
3019eac3 5846 {
6a506a2d
DE
5847 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5848 abbrev_table != NULL,
a2ce51a0 5849 comp_unit_die, NULL,
6a506a2d
DE
5850 &reader, &info_ptr,
5851 &dwo_comp_unit_die, &has_children) == 0)
5852 {
5853 /* Dummy die. */
5854 do_cleanups (cleanups);
5855 return;
5856 }
5857 comp_unit_die = dwo_comp_unit_die;
5858 }
5859 else
5860 {
5861 /* Yikes, we couldn't find the rest of the DIE, we only have
5862 the stub. A complaint has already been logged. There's
5863 not much more we can do except pass on the stub DIE to
5864 die_reader_func. We don't want to throw an error on bad
5865 debug info. */
3019eac3
DE
5866 }
5867 }
5868
b0c7bfa9 5869 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5870 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5871
b0c7bfa9 5872 /* Done, clean up. */
365156ad 5873 if (free_cu_cleanup != NULL)
348e048f 5874 {
365156ad
TT
5875 if (keep)
5876 {
5877 /* We've successfully allocated this compilation unit. Let our
5878 caller clean it up when finished with it. */
5879 discard_cleanups (free_cu_cleanup);
dee91e82 5880
365156ad
TT
5881 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5882 So we have to manually free the abbrev table. */
5883 dwarf2_free_abbrev_table (cu);
dee91e82 5884
365156ad
TT
5885 /* Link this CU into read_in_chain. */
5886 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5887 dwarf2_per_objfile->read_in_chain = this_cu;
5888 }
5889 else
5890 do_cleanups (free_cu_cleanup);
348e048f 5891 }
365156ad
TT
5892
5893 do_cleanups (cleanups);
dee91e82
DE
5894}
5895
33e80786
DE
5896/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5897 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5898 to have already done the lookup to find the DWO file).
dee91e82
DE
5899
5900 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5901 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5902
5903 We fill in THIS_CU->length.
5904
5905 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5906 linker) then DIE_READER_FUNC will not get called.
5907
5908 THIS_CU->cu is always freed when done.
3019eac3
DE
5909 This is done in order to not leave THIS_CU->cu in a state where we have
5910 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5911
5912static void
5913init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5914 struct dwo_file *dwo_file,
dee91e82
DE
5915 die_reader_func_ftype *die_reader_func,
5916 void *data)
5917{
5918 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5919 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5920 bfd *abfd = get_section_bfd_owner (section);
33e80786 5921 struct dwarf2_section_info *abbrev_section;
dee91e82 5922 struct dwarf2_cu cu;
d521ce57 5923 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5924 struct die_reader_specs reader;
5925 struct cleanup *cleanups;
5926 struct die_info *comp_unit_die;
5927 int has_children;
5928
b4f54984 5929 if (dwarf_die_debug)
09406207
DE
5930 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5931 this_cu->is_debug_types ? "type" : "comp",
9c541725 5932 to_underlying (this_cu->sect_off));
09406207 5933
dee91e82
DE
5934 gdb_assert (this_cu->cu == NULL);
5935
33e80786
DE
5936 abbrev_section = (dwo_file != NULL
5937 ? &dwo_file->sections.abbrev
5938 : get_abbrev_section_for_cu (this_cu));
5939
dee91e82
DE
5940 /* This is cheap if the section is already read in. */
5941 dwarf2_read_section (objfile, section);
5942
5943 init_one_comp_unit (&cu, this_cu);
5944
5945 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5946
9c541725 5947 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5948 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5949 abbrev_section, info_ptr,
43988095
JK
5950 (this_cu->is_debug_types
5951 ? rcuh_kind::TYPE
5952 : rcuh_kind::COMPILE));
dee91e82 5953
1ce1cefd 5954 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5955
5956 /* Skip dummy compilation units. */
5957 if (info_ptr >= begin_info_ptr + this_cu->length
5958 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5959 {
dee91e82 5960 do_cleanups (cleanups);
21b2bd31 5961 return;
93311388 5962 }
72bf9492 5963
dee91e82
DE
5964 dwarf2_read_abbrevs (&cu, abbrev_section);
5965 make_cleanup (dwarf2_free_abbrev_table, &cu);
5966
3019eac3 5967 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5968 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5969
5970 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5971
5972 do_cleanups (cleanups);
5973}
5974
3019eac3
DE
5975/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5976 does not lookup the specified DWO file.
5977 This cannot be used to read DWO files.
dee91e82
DE
5978
5979 THIS_CU->cu is always freed when done.
3019eac3
DE
5980 This is done in order to not leave THIS_CU->cu in a state where we have
5981 to care whether it refers to the "main" CU or the DWO CU.
5982 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5983
5984static void
5985init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5986 die_reader_func_ftype *die_reader_func,
5987 void *data)
5988{
33e80786 5989 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5990}
0018ea6f
DE
5991\f
5992/* Type Unit Groups.
dee91e82 5993
0018ea6f
DE
5994 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5995 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5996 so that all types coming from the same compilation (.o file) are grouped
5997 together. A future step could be to put the types in the same symtab as
5998 the CU the types ultimately came from. */
ff013f42 5999
f4dc4d17
DE
6000static hashval_t
6001hash_type_unit_group (const void *item)
6002{
9a3c8263
SM
6003 const struct type_unit_group *tu_group
6004 = (const struct type_unit_group *) item;
f4dc4d17 6005
094b34ac 6006 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6007}
348e048f
DE
6008
6009static int
f4dc4d17 6010eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6011{
9a3c8263
SM
6012 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6013 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6014
094b34ac 6015 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6016}
348e048f 6017
f4dc4d17
DE
6018/* Allocate a hash table for type unit groups. */
6019
6020static htab_t
6021allocate_type_unit_groups_table (void)
6022{
6023 return htab_create_alloc_ex (3,
6024 hash_type_unit_group,
6025 eq_type_unit_group,
6026 NULL,
6027 &dwarf2_per_objfile->objfile->objfile_obstack,
6028 hashtab_obstack_allocate,
6029 dummy_obstack_deallocate);
6030}
dee91e82 6031
f4dc4d17
DE
6032/* Type units that don't have DW_AT_stmt_list are grouped into their own
6033 partial symtabs. We combine several TUs per psymtab to not let the size
6034 of any one psymtab grow too big. */
6035#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6036#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6037
094b34ac 6038/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6039 Create the type_unit_group object used to hold one or more TUs. */
6040
6041static struct type_unit_group *
094b34ac 6042create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6043{
6044 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6045 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6046 struct type_unit_group *tu_group;
f4dc4d17
DE
6047
6048 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6049 struct type_unit_group);
094b34ac 6050 per_cu = &tu_group->per_cu;
f4dc4d17 6051 per_cu->objfile = objfile;
f4dc4d17 6052
094b34ac
DE
6053 if (dwarf2_per_objfile->using_index)
6054 {
6055 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6056 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6057 }
6058 else
6059 {
9c541725 6060 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6061 struct partial_symtab *pst;
6062 char *name;
6063
6064 /* Give the symtab a useful name for debug purposes. */
6065 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6066 name = xstrprintf ("<type_units_%d>",
6067 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6068 else
6069 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6070
6071 pst = create_partial_symtab (per_cu, name);
6072 pst->anonymous = 1;
f4dc4d17 6073
094b34ac
DE
6074 xfree (name);
6075 }
f4dc4d17 6076
094b34ac 6077 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6078 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6079
6080 return tu_group;
6081}
6082
094b34ac
DE
6083/* Look up the type_unit_group for type unit CU, and create it if necessary.
6084 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6085
6086static struct type_unit_group *
ff39bb5e 6087get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6088{
6089 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6090 struct type_unit_group *tu_group;
6091 void **slot;
6092 unsigned int line_offset;
6093 struct type_unit_group type_unit_group_for_lookup;
6094
6095 if (dwarf2_per_objfile->type_unit_groups == NULL)
6096 {
6097 dwarf2_per_objfile->type_unit_groups =
6098 allocate_type_unit_groups_table ();
6099 }
6100
6101 /* Do we need to create a new group, or can we use an existing one? */
6102
6103 if (stmt_list)
6104 {
6105 line_offset = DW_UNSND (stmt_list);
6106 ++tu_stats->nr_symtab_sharers;
6107 }
6108 else
6109 {
6110 /* Ugh, no stmt_list. Rare, but we have to handle it.
6111 We can do various things here like create one group per TU or
6112 spread them over multiple groups to split up the expansion work.
6113 To avoid worst case scenarios (too many groups or too large groups)
6114 we, umm, group them in bunches. */
6115 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6116 | (tu_stats->nr_stmt_less_type_units
6117 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6118 ++tu_stats->nr_stmt_less_type_units;
6119 }
6120
094b34ac 6121 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6122 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6123 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6124 &type_unit_group_for_lookup, INSERT);
6125 if (*slot != NULL)
6126 {
9a3c8263 6127 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6128 gdb_assert (tu_group != NULL);
6129 }
6130 else
6131 {
9c541725 6132 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6133 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6134 *slot = tu_group;
6135 ++tu_stats->nr_symtabs;
6136 }
6137
6138 return tu_group;
6139}
0018ea6f
DE
6140\f
6141/* Partial symbol tables. */
6142
6143/* Create a psymtab named NAME and assign it to PER_CU.
6144
6145 The caller must fill in the following details:
6146 dirname, textlow, texthigh. */
6147
6148static struct partial_symtab *
6149create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6150{
6151 struct objfile *objfile = per_cu->objfile;
6152 struct partial_symtab *pst;
6153
18a94d75 6154 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6155 objfile->global_psymbols.next,
6156 objfile->static_psymbols.next);
6157
6158 pst->psymtabs_addrmap_supported = 1;
6159
6160 /* This is the glue that links PST into GDB's symbol API. */
6161 pst->read_symtab_private = per_cu;
6162 pst->read_symtab = dwarf2_read_symtab;
6163 per_cu->v.psymtab = pst;
6164
6165 return pst;
6166}
6167
b93601f3
TT
6168/* The DATA object passed to process_psymtab_comp_unit_reader has this
6169 type. */
6170
6171struct process_psymtab_comp_unit_data
6172{
6173 /* True if we are reading a DW_TAG_partial_unit. */
6174
6175 int want_partial_unit;
6176
6177 /* The "pretend" language that is used if the CU doesn't declare a
6178 language. */
6179
6180 enum language pretend_language;
6181};
6182
0018ea6f
DE
6183/* die_reader_func for process_psymtab_comp_unit. */
6184
6185static void
6186process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6187 const gdb_byte *info_ptr,
0018ea6f
DE
6188 struct die_info *comp_unit_die,
6189 int has_children,
6190 void *data)
6191{
6192 struct dwarf2_cu *cu = reader->cu;
6193 struct objfile *objfile = cu->objfile;
3e29f34a 6194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6195 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6196 CORE_ADDR baseaddr;
6197 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6198 struct partial_symtab *pst;
3a2b436a 6199 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6200 const char *filename;
9a3c8263
SM
6201 struct process_psymtab_comp_unit_data *info
6202 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6203
b93601f3 6204 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6205 return;
6206
6207 gdb_assert (! per_cu->is_debug_types);
6208
b93601f3 6209 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6210
6211 cu->list_in_scope = &file_symbols;
6212
6213 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6214 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6215 if (filename == NULL)
0018ea6f 6216 filename = "";
0018ea6f
DE
6217
6218 pst = create_partial_symtab (per_cu, filename);
6219
6220 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6221 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6222
6223 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6224
6225 dwarf2_find_base_address (comp_unit_die, cu);
6226
6227 /* Possibly set the default values of LOWPC and HIGHPC from
6228 `DW_AT_ranges'. */
3a2b436a
JK
6229 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6230 &best_highpc, cu, pst);
6231 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6232 /* Store the contiguous range if it is not empty; it can be empty for
6233 CUs with no code. */
6234 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6235 gdbarch_adjust_dwarf2_addr (gdbarch,
6236 best_lowpc + baseaddr),
6237 gdbarch_adjust_dwarf2_addr (gdbarch,
6238 best_highpc + baseaddr) - 1,
6239 pst);
0018ea6f
DE
6240
6241 /* Check if comp unit has_children.
6242 If so, read the rest of the partial symbols from this comp unit.
6243 If not, there's no more debug_info for this comp unit. */
6244 if (has_children)
6245 {
6246 struct partial_die_info *first_die;
6247 CORE_ADDR lowpc, highpc;
6248
6249 lowpc = ((CORE_ADDR) -1);
6250 highpc = ((CORE_ADDR) 0);
6251
6252 first_die = load_partial_dies (reader, info_ptr, 1);
6253
6254 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6255 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6256
6257 /* If we didn't find a lowpc, set it to highpc to avoid
6258 complaints from `maint check'. */
6259 if (lowpc == ((CORE_ADDR) -1))
6260 lowpc = highpc;
6261
6262 /* If the compilation unit didn't have an explicit address range,
6263 then use the information extracted from its child dies. */
e385593e 6264 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6265 {
6266 best_lowpc = lowpc;
6267 best_highpc = highpc;
6268 }
6269 }
3e29f34a
MR
6270 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6271 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6272
8763cede 6273 end_psymtab_common (objfile, pst);
0018ea6f
DE
6274
6275 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6276 {
6277 int i;
6278 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6279 struct dwarf2_per_cu_data *iter;
6280
6281 /* Fill in 'dependencies' here; we fill in 'users' in a
6282 post-pass. */
6283 pst->number_of_dependencies = len;
8d749320
SM
6284 pst->dependencies =
6285 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6286 for (i = 0;
6287 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6288 i, iter);
6289 ++i)
6290 pst->dependencies[i] = iter->v.psymtab;
6291
6292 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6293 }
6294
6295 /* Get the list of files included in the current compilation unit,
6296 and build a psymtab for each of them. */
6297 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6298
b4f54984 6299 if (dwarf_read_debug)
0018ea6f
DE
6300 {
6301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6302
6303 fprintf_unfiltered (gdb_stdlog,
6304 "Psymtab for %s unit @0x%x: %s - %s"
6305 ", %d global, %d static syms\n",
6306 per_cu->is_debug_types ? "type" : "comp",
9c541725 6307 to_underlying (per_cu->sect_off),
0018ea6f
DE
6308 paddress (gdbarch, pst->textlow),
6309 paddress (gdbarch, pst->texthigh),
6310 pst->n_global_syms, pst->n_static_syms);
6311 }
6312}
6313
6314/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6315 Process compilation unit THIS_CU for a psymtab. */
6316
6317static void
6318process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6319 int want_partial_unit,
6320 enum language pretend_language)
0018ea6f
DE
6321{
6322 /* If this compilation unit was already read in, free the
6323 cached copy in order to read it in again. This is
6324 necessary because we skipped some symbols when we first
6325 read in the compilation unit (see load_partial_dies).
6326 This problem could be avoided, but the benefit is unclear. */
6327 if (this_cu->cu != NULL)
6328 free_one_cached_comp_unit (this_cu);
6329
f1902523
JK
6330 if (this_cu->is_debug_types)
6331 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6332 NULL);
6333 else
6334 {
6335 process_psymtab_comp_unit_data info;
6336 info.want_partial_unit = want_partial_unit;
6337 info.pretend_language = pretend_language;
6338 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6339 process_psymtab_comp_unit_reader, &info);
6340 }
0018ea6f
DE
6341
6342 /* Age out any secondary CUs. */
6343 age_cached_comp_units ();
6344}
f4dc4d17
DE
6345
6346/* Reader function for build_type_psymtabs. */
6347
6348static void
6349build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6350 const gdb_byte *info_ptr,
f4dc4d17
DE
6351 struct die_info *type_unit_die,
6352 int has_children,
6353 void *data)
6354{
6355 struct objfile *objfile = dwarf2_per_objfile->objfile;
6356 struct dwarf2_cu *cu = reader->cu;
6357 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6358 struct signatured_type *sig_type;
f4dc4d17
DE
6359 struct type_unit_group *tu_group;
6360 struct attribute *attr;
6361 struct partial_die_info *first_die;
6362 CORE_ADDR lowpc, highpc;
6363 struct partial_symtab *pst;
6364
6365 gdb_assert (data == NULL);
0186c6a7
DE
6366 gdb_assert (per_cu->is_debug_types);
6367 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6368
6369 if (! has_children)
6370 return;
6371
6372 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6373 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6374
0186c6a7 6375 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6376
6377 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6378 cu->list_in_scope = &file_symbols;
6379 pst = create_partial_symtab (per_cu, "");
6380 pst->anonymous = 1;
6381
6382 first_die = load_partial_dies (reader, info_ptr, 1);
6383
6384 lowpc = (CORE_ADDR) -1;
6385 highpc = (CORE_ADDR) 0;
6386 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6387
8763cede 6388 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6389}
6390
73051182
DE
6391/* Struct used to sort TUs by their abbreviation table offset. */
6392
6393struct tu_abbrev_offset
6394{
6395 struct signatured_type *sig_type;
6396 sect_offset abbrev_offset;
6397};
6398
6399/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6400
6401static int
6402sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6403{
9a3c8263
SM
6404 const struct tu_abbrev_offset * const *a
6405 = (const struct tu_abbrev_offset * const*) ap;
6406 const struct tu_abbrev_offset * const *b
6407 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6408 sect_offset aoff = (*a)->abbrev_offset;
6409 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6410
6411 return (aoff > boff) - (aoff < boff);
6412}
6413
6414/* Efficiently read all the type units.
6415 This does the bulk of the work for build_type_psymtabs.
6416
6417 The efficiency is because we sort TUs by the abbrev table they use and
6418 only read each abbrev table once. In one program there are 200K TUs
6419 sharing 8K abbrev tables.
6420
6421 The main purpose of this function is to support building the
6422 dwarf2_per_objfile->type_unit_groups table.
6423 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6424 can collapse the search space by grouping them by stmt_list.
6425 The savings can be significant, in the same program from above the 200K TUs
6426 share 8K stmt_list tables.
6427
6428 FUNC is expected to call get_type_unit_group, which will create the
6429 struct type_unit_group if necessary and add it to
6430 dwarf2_per_objfile->type_unit_groups. */
6431
6432static void
6433build_type_psymtabs_1 (void)
6434{
73051182
DE
6435 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6436 struct cleanup *cleanups;
6437 struct abbrev_table *abbrev_table;
6438 sect_offset abbrev_offset;
6439 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6440 int i;
6441
6442 /* It's up to the caller to not call us multiple times. */
6443 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6444
6445 if (dwarf2_per_objfile->n_type_units == 0)
6446 return;
6447
6448 /* TUs typically share abbrev tables, and there can be way more TUs than
6449 abbrev tables. Sort by abbrev table to reduce the number of times we
6450 read each abbrev table in.
6451 Alternatives are to punt or to maintain a cache of abbrev tables.
6452 This is simpler and efficient enough for now.
6453
6454 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6455 symtab to use). Typically TUs with the same abbrev offset have the same
6456 stmt_list value too so in practice this should work well.
6457
6458 The basic algorithm here is:
6459
6460 sort TUs by abbrev table
6461 for each TU with same abbrev table:
6462 read abbrev table if first user
6463 read TU top level DIE
6464 [IWBN if DWO skeletons had DW_AT_stmt_list]
6465 call FUNC */
6466
b4f54984 6467 if (dwarf_read_debug)
73051182
DE
6468 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6469
6470 /* Sort in a separate table to maintain the order of all_type_units
6471 for .gdb_index: TU indices directly index all_type_units. */
6472 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6473 dwarf2_per_objfile->n_type_units);
6474 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6475 {
6476 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6477
6478 sorted_by_abbrev[i].sig_type = sig_type;
6479 sorted_by_abbrev[i].abbrev_offset =
6480 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6481 sig_type->per_cu.sect_off);
73051182
DE
6482 }
6483 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6484 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6485 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6486
9c541725 6487 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6488 abbrev_table = NULL;
6489 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6490
6491 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6492 {
6493 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6494
6495 /* Switch to the next abbrev table if necessary. */
6496 if (abbrev_table == NULL
9c541725 6497 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6498 {
6499 if (abbrev_table != NULL)
6500 {
6501 abbrev_table_free (abbrev_table);
6502 /* Reset to NULL in case abbrev_table_read_table throws
6503 an error: abbrev_table_free_cleanup will get called. */
6504 abbrev_table = NULL;
6505 }
6506 abbrev_offset = tu->abbrev_offset;
6507 abbrev_table =
6508 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6509 abbrev_offset);
6510 ++tu_stats->nr_uniq_abbrev_tables;
6511 }
6512
6513 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6514 build_type_psymtabs_reader, NULL);
6515 }
6516
73051182 6517 do_cleanups (cleanups);
6aa5f3a6 6518}
73051182 6519
6aa5f3a6
DE
6520/* Print collected type unit statistics. */
6521
6522static void
6523print_tu_stats (void)
6524{
6525 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6526
6527 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6528 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6529 dwarf2_per_objfile->n_type_units);
6530 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6531 tu_stats->nr_uniq_abbrev_tables);
6532 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6533 tu_stats->nr_symtabs);
6534 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6535 tu_stats->nr_symtab_sharers);
6536 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6537 tu_stats->nr_stmt_less_type_units);
6538 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6539 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6540}
6541
f4dc4d17
DE
6542/* Traversal function for build_type_psymtabs. */
6543
6544static int
6545build_type_psymtab_dependencies (void **slot, void *info)
6546{
6547 struct objfile *objfile = dwarf2_per_objfile->objfile;
6548 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6549 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6550 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6551 int len = VEC_length (sig_type_ptr, tu_group->tus);
6552 struct signatured_type *iter;
f4dc4d17
DE
6553 int i;
6554
6555 gdb_assert (len > 0);
0186c6a7 6556 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6557
6558 pst->number_of_dependencies = len;
8d749320
SM
6559 pst->dependencies =
6560 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6561 for (i = 0;
0186c6a7 6562 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6563 ++i)
6564 {
0186c6a7
DE
6565 gdb_assert (iter->per_cu.is_debug_types);
6566 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6567 iter->type_unit_group = tu_group;
f4dc4d17
DE
6568 }
6569
0186c6a7 6570 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6571
6572 return 1;
6573}
6574
6575/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6576 Build partial symbol tables for the .debug_types comp-units. */
6577
6578static void
6579build_type_psymtabs (struct objfile *objfile)
6580{
0e50663e 6581 if (! create_all_type_units (objfile))
348e048f
DE
6582 return;
6583
73051182 6584 build_type_psymtabs_1 ();
6aa5f3a6 6585}
f4dc4d17 6586
6aa5f3a6
DE
6587/* Traversal function for process_skeletonless_type_unit.
6588 Read a TU in a DWO file and build partial symbols for it. */
6589
6590static int
6591process_skeletonless_type_unit (void **slot, void *info)
6592{
6593 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6594 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6595 struct signatured_type find_entry, *entry;
6596
6597 /* If this TU doesn't exist in the global table, add it and read it in. */
6598
6599 if (dwarf2_per_objfile->signatured_types == NULL)
6600 {
6601 dwarf2_per_objfile->signatured_types
6602 = allocate_signatured_type_table (objfile);
6603 }
6604
6605 find_entry.signature = dwo_unit->signature;
6606 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6607 INSERT);
6608 /* If we've already seen this type there's nothing to do. What's happening
6609 is we're doing our own version of comdat-folding here. */
6610 if (*slot != NULL)
6611 return 1;
6612
6613 /* This does the job that create_all_type_units would have done for
6614 this TU. */
6615 entry = add_type_unit (dwo_unit->signature, slot);
6616 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6617 *slot = entry;
6618
6619 /* This does the job that build_type_psymtabs_1 would have done. */
6620 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6621 build_type_psymtabs_reader, NULL);
6622
6623 return 1;
6624}
6625
6626/* Traversal function for process_skeletonless_type_units. */
6627
6628static int
6629process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6630{
6631 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6632
6633 if (dwo_file->tus != NULL)
6634 {
6635 htab_traverse_noresize (dwo_file->tus,
6636 process_skeletonless_type_unit, info);
6637 }
6638
6639 return 1;
6640}
6641
6642/* Scan all TUs of DWO files, verifying we've processed them.
6643 This is needed in case a TU was emitted without its skeleton.
6644 Note: This can't be done until we know what all the DWO files are. */
6645
6646static void
6647process_skeletonless_type_units (struct objfile *objfile)
6648{
6649 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6650 if (get_dwp_file () == NULL
6651 && dwarf2_per_objfile->dwo_files != NULL)
6652 {
6653 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6654 process_dwo_file_for_skeletonless_type_units,
6655 objfile);
6656 }
348e048f
DE
6657}
6658
60606b2c
TT
6659/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6660
6661static void
6662psymtabs_addrmap_cleanup (void *o)
6663{
9a3c8263 6664 struct objfile *objfile = (struct objfile *) o;
ec61707d 6665
60606b2c
TT
6666 objfile->psymtabs_addrmap = NULL;
6667}
6668
95554aad
TT
6669/* Compute the 'user' field for each psymtab in OBJFILE. */
6670
6671static void
6672set_partial_user (struct objfile *objfile)
6673{
6674 int i;
6675
6676 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6677 {
8832e7e3 6678 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6679 struct partial_symtab *pst = per_cu->v.psymtab;
6680 int j;
6681
36586728
TT
6682 if (pst == NULL)
6683 continue;
6684
95554aad
TT
6685 for (j = 0; j < pst->number_of_dependencies; ++j)
6686 {
6687 /* Set the 'user' field only if it is not already set. */
6688 if (pst->dependencies[j]->user == NULL)
6689 pst->dependencies[j]->user = pst;
6690 }
6691 }
6692}
6693
93311388
DE
6694/* Build the partial symbol table by doing a quick pass through the
6695 .debug_info and .debug_abbrev sections. */
72bf9492 6696
93311388 6697static void
c67a9c90 6698dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6699{
60606b2c 6700 struct cleanup *back_to, *addrmap_cleanup;
21b2bd31 6701 int i;
93311388 6702
b4f54984 6703 if (dwarf_read_debug)
45cfd468
DE
6704 {
6705 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6706 objfile_name (objfile));
45cfd468
DE
6707 }
6708
98bfdba5
PA
6709 dwarf2_per_objfile->reading_partial_symbols = 1;
6710
be391dca 6711 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6712
93311388
DE
6713 /* Any cached compilation units will be linked by the per-objfile
6714 read_in_chain. Make sure to free them when we're done. */
6715 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6716
348e048f
DE
6717 build_type_psymtabs (objfile);
6718
93311388 6719 create_all_comp_units (objfile);
c906108c 6720
60606b2c
TT
6721 /* Create a temporary address map on a temporary obstack. We later
6722 copy this to the final obstack. */
8268c778 6723 auto_obstack temp_obstack;
60606b2c
TT
6724 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6725 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6726
21b2bd31 6727 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6728 {
8832e7e3 6729 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6730
b93601f3 6731 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6732 }
ff013f42 6733
6aa5f3a6
DE
6734 /* This has to wait until we read the CUs, we need the list of DWOs. */
6735 process_skeletonless_type_units (objfile);
6736
6737 /* Now that all TUs have been processed we can fill in the dependencies. */
6738 if (dwarf2_per_objfile->type_unit_groups != NULL)
6739 {
6740 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6741 build_type_psymtab_dependencies, NULL);
6742 }
6743
b4f54984 6744 if (dwarf_read_debug)
6aa5f3a6
DE
6745 print_tu_stats ();
6746
95554aad
TT
6747 set_partial_user (objfile);
6748
ff013f42
JK
6749 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6750 &objfile->objfile_obstack);
60606b2c 6751 discard_cleanups (addrmap_cleanup);
ff013f42 6752
ae038cb0 6753 do_cleanups (back_to);
45cfd468 6754
b4f54984 6755 if (dwarf_read_debug)
45cfd468 6756 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6757 objfile_name (objfile));
ae038cb0
DJ
6758}
6759
3019eac3 6760/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6761
6762static void
dee91e82 6763load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6764 const gdb_byte *info_ptr,
dee91e82
DE
6765 struct die_info *comp_unit_die,
6766 int has_children,
6767 void *data)
ae038cb0 6768{
dee91e82 6769 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6770
95554aad 6771 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6772
ae038cb0
DJ
6773 /* Check if comp unit has_children.
6774 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6775 If not, there's no more debug_info for this comp unit. */
d85a05f0 6776 if (has_children)
dee91e82
DE
6777 load_partial_dies (reader, info_ptr, 0);
6778}
98bfdba5 6779
dee91e82
DE
6780/* Load the partial DIEs for a secondary CU into memory.
6781 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6782
dee91e82
DE
6783static void
6784load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6785{
f4dc4d17
DE
6786 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6787 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6788}
6789
ae038cb0 6790static void
36586728
TT
6791read_comp_units_from_section (struct objfile *objfile,
6792 struct dwarf2_section_info *section,
f1902523 6793 struct dwarf2_section_info *abbrev_section,
36586728
TT
6794 unsigned int is_dwz,
6795 int *n_allocated,
6796 int *n_comp_units,
6797 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6798{
d521ce57 6799 const gdb_byte *info_ptr;
a32a8923 6800 bfd *abfd = get_section_bfd_owner (section);
be391dca 6801
b4f54984 6802 if (dwarf_read_debug)
bf6af496 6803 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6804 get_section_name (section),
6805 get_section_file_name (section));
bf6af496 6806
36586728 6807 dwarf2_read_section (objfile, section);
ae038cb0 6808
36586728 6809 info_ptr = section->buffer;
6e70227d 6810
36586728 6811 while (info_ptr < section->buffer + section->size)
ae038cb0 6812 {
ae038cb0 6813 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6814
9c541725 6815 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6816
f1902523
JK
6817 comp_unit_head cu_header;
6818 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6819 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6820
6821 /* Save the compilation unit for later lookup. */
f1902523
JK
6822 if (cu_header.unit_type != DW_UT_type)
6823 {
6824 this_cu = XOBNEW (&objfile->objfile_obstack,
6825 struct dwarf2_per_cu_data);
6826 memset (this_cu, 0, sizeof (*this_cu));
6827 }
6828 else
6829 {
6830 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6831 struct signatured_type);
6832 memset (sig_type, 0, sizeof (*sig_type));
6833 sig_type->signature = cu_header.signature;
6834 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6835 this_cu = &sig_type->per_cu;
6836 }
6837 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6838 this_cu->sect_off = sect_off;
f1902523 6839 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6840 this_cu->is_dwz = is_dwz;
9291a0cd 6841 this_cu->objfile = objfile;
8a0459fd 6842 this_cu->section = section;
ae038cb0 6843
36586728 6844 if (*n_comp_units == *n_allocated)
ae038cb0 6845 {
36586728 6846 *n_allocated *= 2;
224c3ddb
SM
6847 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6848 *all_comp_units, *n_allocated);
ae038cb0 6849 }
36586728
TT
6850 (*all_comp_units)[*n_comp_units] = this_cu;
6851 ++*n_comp_units;
ae038cb0
DJ
6852
6853 info_ptr = info_ptr + this_cu->length;
6854 }
36586728
TT
6855}
6856
6857/* Create a list of all compilation units in OBJFILE.
6858 This is only done for -readnow and building partial symtabs. */
6859
6860static void
6861create_all_comp_units (struct objfile *objfile)
6862{
6863 int n_allocated;
6864 int n_comp_units;
6865 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6866 struct dwz_file *dwz;
36586728
TT
6867
6868 n_comp_units = 0;
6869 n_allocated = 10;
8d749320 6870 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6871
f1902523
JK
6872 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6873 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6874 &n_allocated, &n_comp_units, &all_comp_units);
6875
4db1a1dc
TT
6876 dwz = dwarf2_get_dwz_file ();
6877 if (dwz != NULL)
f1902523 6878 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6879 &n_allocated, &n_comp_units,
6880 &all_comp_units);
ae038cb0 6881
8d749320
SM
6882 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6883 struct dwarf2_per_cu_data *,
6884 n_comp_units);
ae038cb0
DJ
6885 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6886 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6887 xfree (all_comp_units);
6888 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6889}
6890
5734ee8b 6891/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6892 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6893 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6894 DW_AT_ranges). See the comments of add_partial_subprogram on how
6895 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6896
72bf9492
DJ
6897static void
6898scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6899 CORE_ADDR *highpc, int set_addrmap,
6900 struct dwarf2_cu *cu)
c906108c 6901{
72bf9492 6902 struct partial_die_info *pdi;
c906108c 6903
91c24f0a
DC
6904 /* Now, march along the PDI's, descending into ones which have
6905 interesting children but skipping the children of the other ones,
6906 until we reach the end of the compilation unit. */
c906108c 6907
72bf9492 6908 pdi = first_die;
91c24f0a 6909
72bf9492
DJ
6910 while (pdi != NULL)
6911 {
6912 fixup_partial_die (pdi, cu);
c906108c 6913
f55ee35c 6914 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6915 children, so we need to look at them. Ditto for anonymous
6916 enums. */
933c6fe4 6917
72bf9492 6918 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6919 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6920 || pdi->tag == DW_TAG_imported_unit)
c906108c 6921 {
72bf9492 6922 switch (pdi->tag)
c906108c
SS
6923 {
6924 case DW_TAG_subprogram:
cdc07690 6925 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6926 break;
72929c62 6927 case DW_TAG_constant:
c906108c
SS
6928 case DW_TAG_variable:
6929 case DW_TAG_typedef:
91c24f0a 6930 case DW_TAG_union_type:
72bf9492 6931 if (!pdi->is_declaration)
63d06c5c 6932 {
72bf9492 6933 add_partial_symbol (pdi, cu);
63d06c5c
DC
6934 }
6935 break;
c906108c 6936 case DW_TAG_class_type:
680b30c7 6937 case DW_TAG_interface_type:
c906108c 6938 case DW_TAG_structure_type:
72bf9492 6939 if (!pdi->is_declaration)
c906108c 6940 {
72bf9492 6941 add_partial_symbol (pdi, cu);
c906108c 6942 }
e98c9e7c
TT
6943 if (cu->language == language_rust && pdi->has_children)
6944 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6945 set_addrmap, cu);
c906108c 6946 break;
91c24f0a 6947 case DW_TAG_enumeration_type:
72bf9492
DJ
6948 if (!pdi->is_declaration)
6949 add_partial_enumeration (pdi, cu);
c906108c
SS
6950 break;
6951 case DW_TAG_base_type:
a02abb62 6952 case DW_TAG_subrange_type:
c906108c 6953 /* File scope base type definitions are added to the partial
c5aa993b 6954 symbol table. */
72bf9492 6955 add_partial_symbol (pdi, cu);
c906108c 6956 break;
d9fa45fe 6957 case DW_TAG_namespace:
cdc07690 6958 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6959 break;
5d7cb8df 6960 case DW_TAG_module:
cdc07690 6961 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6962 break;
95554aad
TT
6963 case DW_TAG_imported_unit:
6964 {
6965 struct dwarf2_per_cu_data *per_cu;
6966
f4dc4d17
DE
6967 /* For now we don't handle imported units in type units. */
6968 if (cu->per_cu->is_debug_types)
6969 {
6970 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6971 " supported in type units [in module %s]"),
4262abfb 6972 objfile_name (cu->objfile));
f4dc4d17
DE
6973 }
6974
9c541725 6975 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6976 pdi->is_dwz,
95554aad
TT
6977 cu->objfile);
6978
6979 /* Go read the partial unit, if needed. */
6980 if (per_cu->v.psymtab == NULL)
b93601f3 6981 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6982
f4dc4d17 6983 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6984 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6985 }
6986 break;
74921315
KS
6987 case DW_TAG_imported_declaration:
6988 add_partial_symbol (pdi, cu);
6989 break;
c906108c
SS
6990 default:
6991 break;
6992 }
6993 }
6994
72bf9492
DJ
6995 /* If the die has a sibling, skip to the sibling. */
6996
6997 pdi = pdi->die_sibling;
6998 }
6999}
7000
7001/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7002
72bf9492 7003 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7004 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7005 Enumerators are an exception; they use the scope of their parent
7006 enumeration type, i.e. the name of the enumeration type is not
7007 prepended to the enumerator.
91c24f0a 7008
72bf9492
DJ
7009 There are two complexities. One is DW_AT_specification; in this
7010 case "parent" means the parent of the target of the specification,
7011 instead of the direct parent of the DIE. The other is compilers
7012 which do not emit DW_TAG_namespace; in this case we try to guess
7013 the fully qualified name of structure types from their members'
7014 linkage names. This must be done using the DIE's children rather
7015 than the children of any DW_AT_specification target. We only need
7016 to do this for structures at the top level, i.e. if the target of
7017 any DW_AT_specification (if any; otherwise the DIE itself) does not
7018 have a parent. */
7019
7020/* Compute the scope prefix associated with PDI's parent, in
7021 compilation unit CU. The result will be allocated on CU's
7022 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7023 field. NULL is returned if no prefix is necessary. */
15d034d0 7024static const char *
72bf9492
DJ
7025partial_die_parent_scope (struct partial_die_info *pdi,
7026 struct dwarf2_cu *cu)
7027{
15d034d0 7028 const char *grandparent_scope;
72bf9492 7029 struct partial_die_info *parent, *real_pdi;
91c24f0a 7030
72bf9492
DJ
7031 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7032 then this means the parent of the specification DIE. */
7033
7034 real_pdi = pdi;
72bf9492 7035 while (real_pdi->has_specification)
36586728
TT
7036 real_pdi = find_partial_die (real_pdi->spec_offset,
7037 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7038
7039 parent = real_pdi->die_parent;
7040 if (parent == NULL)
7041 return NULL;
7042
7043 if (parent->scope_set)
7044 return parent->scope;
7045
7046 fixup_partial_die (parent, cu);
7047
10b3939b 7048 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7049
acebe513
UW
7050 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7051 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7052 Work around this problem here. */
7053 if (cu->language == language_cplus
6e70227d 7054 && parent->tag == DW_TAG_namespace
acebe513
UW
7055 && strcmp (parent->name, "::") == 0
7056 && grandparent_scope == NULL)
7057 {
7058 parent->scope = NULL;
7059 parent->scope_set = 1;
7060 return NULL;
7061 }
7062
9c6c53f7
SA
7063 if (pdi->tag == DW_TAG_enumerator)
7064 /* Enumerators should not get the name of the enumeration as a prefix. */
7065 parent->scope = grandparent_scope;
7066 else if (parent->tag == DW_TAG_namespace
f55ee35c 7067 || parent->tag == DW_TAG_module
72bf9492
DJ
7068 || parent->tag == DW_TAG_structure_type
7069 || parent->tag == DW_TAG_class_type
680b30c7 7070 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7071 || parent->tag == DW_TAG_union_type
7072 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7073 {
7074 if (grandparent_scope == NULL)
7075 parent->scope = parent->name;
7076 else
3e43a32a
MS
7077 parent->scope = typename_concat (&cu->comp_unit_obstack,
7078 grandparent_scope,
f55ee35c 7079 parent->name, 0, cu);
72bf9492 7080 }
72bf9492
DJ
7081 else
7082 {
7083 /* FIXME drow/2004-04-01: What should we be doing with
7084 function-local names? For partial symbols, we should probably be
7085 ignoring them. */
7086 complaint (&symfile_complaints,
e2e0b3e5 7087 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7088 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7089 parent->scope = grandparent_scope;
c906108c
SS
7090 }
7091
72bf9492
DJ
7092 parent->scope_set = 1;
7093 return parent->scope;
7094}
7095
7096/* Return the fully scoped name associated with PDI, from compilation unit
7097 CU. The result will be allocated with malloc. */
4568ecf9 7098
72bf9492
DJ
7099static char *
7100partial_die_full_name (struct partial_die_info *pdi,
7101 struct dwarf2_cu *cu)
7102{
15d034d0 7103 const char *parent_scope;
72bf9492 7104
98bfdba5
PA
7105 /* If this is a template instantiation, we can not work out the
7106 template arguments from partial DIEs. So, unfortunately, we have
7107 to go through the full DIEs. At least any work we do building
7108 types here will be reused if full symbols are loaded later. */
7109 if (pdi->has_template_arguments)
7110 {
7111 fixup_partial_die (pdi, cu);
7112
7113 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7114 {
7115 struct die_info *die;
7116 struct attribute attr;
7117 struct dwarf2_cu *ref_cu = cu;
7118
b64f50a1 7119 /* DW_FORM_ref_addr is using section offset. */
b4069958 7120 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7121 attr.form = DW_FORM_ref_addr;
9c541725 7122 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7123 die = follow_die_ref (NULL, &attr, &ref_cu);
7124
7125 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7126 }
7127 }
7128
72bf9492
DJ
7129 parent_scope = partial_die_parent_scope (pdi, cu);
7130 if (parent_scope == NULL)
7131 return NULL;
7132 else
f55ee35c 7133 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7134}
7135
7136static void
72bf9492 7137add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7138{
e7c27a73 7139 struct objfile *objfile = cu->objfile;
3e29f34a 7140 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7141 CORE_ADDR addr = 0;
15d034d0 7142 const char *actual_name = NULL;
e142c38c 7143 CORE_ADDR baseaddr;
15d034d0 7144 char *built_actual_name;
e142c38c
DJ
7145
7146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7147
15d034d0
TT
7148 built_actual_name = partial_die_full_name (pdi, cu);
7149 if (built_actual_name != NULL)
7150 actual_name = built_actual_name;
63d06c5c 7151
72bf9492
DJ
7152 if (actual_name == NULL)
7153 actual_name = pdi->name;
7154
c906108c
SS
7155 switch (pdi->tag)
7156 {
7157 case DW_TAG_subprogram:
3e29f34a 7158 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7159 if (pdi->is_external || cu->language == language_ada)
c906108c 7160 {
2cfa0c8d
JB
7161 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7162 of the global scope. But in Ada, we want to be able to access
7163 nested procedures globally. So all Ada subprograms are stored
7164 in the global scope. */
f47fb265 7165 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7166 built_actual_name != NULL,
f47fb265
MS
7167 VAR_DOMAIN, LOC_BLOCK,
7168 &objfile->global_psymbols,
1762568f 7169 addr, cu->language, objfile);
c906108c
SS
7170 }
7171 else
7172 {
f47fb265 7173 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7174 built_actual_name != NULL,
f47fb265
MS
7175 VAR_DOMAIN, LOC_BLOCK,
7176 &objfile->static_psymbols,
1762568f 7177 addr, cu->language, objfile);
c906108c 7178 }
0c1b455e
TT
7179
7180 if (pdi->main_subprogram && actual_name != NULL)
7181 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7182 break;
72929c62
JB
7183 case DW_TAG_constant:
7184 {
7185 struct psymbol_allocation_list *list;
7186
7187 if (pdi->is_external)
7188 list = &objfile->global_psymbols;
7189 else
7190 list = &objfile->static_psymbols;
f47fb265 7191 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7192 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7193 list, 0, cu->language, objfile);
72929c62
JB
7194 }
7195 break;
c906108c 7196 case DW_TAG_variable:
95554aad
TT
7197 if (pdi->d.locdesc)
7198 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7199
95554aad 7200 if (pdi->d.locdesc
caac4577
JG
7201 && addr == 0
7202 && !dwarf2_per_objfile->has_section_at_zero)
7203 {
7204 /* A global or static variable may also have been stripped
7205 out by the linker if unused, in which case its address
7206 will be nullified; do not add such variables into partial
7207 symbol table then. */
7208 }
7209 else if (pdi->is_external)
c906108c
SS
7210 {
7211 /* Global Variable.
7212 Don't enter into the minimal symbol tables as there is
7213 a minimal symbol table entry from the ELF symbols already.
7214 Enter into partial symbol table if it has a location
7215 descriptor or a type.
7216 If the location descriptor is missing, new_symbol will create
7217 a LOC_UNRESOLVED symbol, the address of the variable will then
7218 be determined from the minimal symbol table whenever the variable
7219 is referenced.
7220 The address for the partial symbol table entry is not
7221 used by GDB, but it comes in handy for debugging partial symbol
7222 table building. */
7223
95554aad 7224 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7225 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7226 built_actual_name != NULL,
f47fb265
MS
7227 VAR_DOMAIN, LOC_STATIC,
7228 &objfile->global_psymbols,
1762568f 7229 addr + baseaddr,
f47fb265 7230 cu->language, objfile);
c906108c
SS
7231 }
7232 else
7233 {
ff908ebf
AW
7234 int has_loc = pdi->d.locdesc != NULL;
7235
7236 /* Static Variable. Skip symbols whose value we cannot know (those
7237 without location descriptors or constant values). */
7238 if (!has_loc && !pdi->has_const_value)
decbce07 7239 {
15d034d0 7240 xfree (built_actual_name);
decbce07
MS
7241 return;
7242 }
ff908ebf 7243
f47fb265 7244 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7245 built_actual_name != NULL,
f47fb265
MS
7246 VAR_DOMAIN, LOC_STATIC,
7247 &objfile->static_psymbols,
ff908ebf 7248 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7249 cu->language, objfile);
c906108c
SS
7250 }
7251 break;
7252 case DW_TAG_typedef:
7253 case DW_TAG_base_type:
a02abb62 7254 case DW_TAG_subrange_type:
38d518c9 7255 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7256 built_actual_name != NULL,
176620f1 7257 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7258 &objfile->static_psymbols,
1762568f 7259 0, cu->language, objfile);
c906108c 7260 break;
74921315 7261 case DW_TAG_imported_declaration:
72bf9492
DJ
7262 case DW_TAG_namespace:
7263 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7264 built_actual_name != NULL,
72bf9492
DJ
7265 VAR_DOMAIN, LOC_TYPEDEF,
7266 &objfile->global_psymbols,
1762568f 7267 0, cu->language, objfile);
72bf9492 7268 break;
530e8392
KB
7269 case DW_TAG_module:
7270 add_psymbol_to_list (actual_name, strlen (actual_name),
7271 built_actual_name != NULL,
7272 MODULE_DOMAIN, LOC_TYPEDEF,
7273 &objfile->global_psymbols,
1762568f 7274 0, cu->language, objfile);
530e8392 7275 break;
c906108c 7276 case DW_TAG_class_type:
680b30c7 7277 case DW_TAG_interface_type:
c906108c
SS
7278 case DW_TAG_structure_type:
7279 case DW_TAG_union_type:
7280 case DW_TAG_enumeration_type:
fa4028e9
JB
7281 /* Skip external references. The DWARF standard says in the section
7282 about "Structure, Union, and Class Type Entries": "An incomplete
7283 structure, union or class type is represented by a structure,
7284 union or class entry that does not have a byte size attribute
7285 and that has a DW_AT_declaration attribute." */
7286 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7287 {
15d034d0 7288 xfree (built_actual_name);
decbce07
MS
7289 return;
7290 }
fa4028e9 7291
63d06c5c
DC
7292 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7293 static vs. global. */
38d518c9 7294 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7295 built_actual_name != NULL,
176620f1 7296 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7297 cu->language == language_cplus
63d06c5c
DC
7298 ? &objfile->global_psymbols
7299 : &objfile->static_psymbols,
1762568f 7300 0, cu->language, objfile);
c906108c 7301
c906108c
SS
7302 break;
7303 case DW_TAG_enumerator:
38d518c9 7304 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7305 built_actual_name != NULL,
176620f1 7306 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7307 cu->language == language_cplus
f6fe98ef
DJ
7308 ? &objfile->global_psymbols
7309 : &objfile->static_psymbols,
1762568f 7310 0, cu->language, objfile);
c906108c
SS
7311 break;
7312 default:
7313 break;
7314 }
5c4e30ca 7315
15d034d0 7316 xfree (built_actual_name);
c906108c
SS
7317}
7318
5c4e30ca
DC
7319/* Read a partial die corresponding to a namespace; also, add a symbol
7320 corresponding to that namespace to the symbol table. NAMESPACE is
7321 the name of the enclosing namespace. */
91c24f0a 7322
72bf9492
DJ
7323static void
7324add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7326 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7327{
72bf9492 7328 /* Add a symbol for the namespace. */
e7c27a73 7329
72bf9492 7330 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7331
7332 /* Now scan partial symbols in that namespace. */
7333
91c24f0a 7334 if (pdi->has_children)
cdc07690 7335 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7336}
7337
5d7cb8df
JK
7338/* Read a partial die corresponding to a Fortran module. */
7339
7340static void
7341add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7342 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7343{
530e8392
KB
7344 /* Add a symbol for the namespace. */
7345
7346 add_partial_symbol (pdi, cu);
7347
f55ee35c 7348 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7349
7350 if (pdi->has_children)
cdc07690 7351 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7352}
7353
bc30ff58
JB
7354/* Read a partial die corresponding to a subprogram and create a partial
7355 symbol for that subprogram. When the CU language allows it, this
7356 routine also defines a partial symbol for each nested subprogram
cdc07690 7357 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7358 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7359 and highest PC values found in PDI.
6e70227d 7360
cdc07690
YQ
7361 PDI may also be a lexical block, in which case we simply search
7362 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7363 Again, this is only performed when the CU language allows this
7364 type of definitions. */
7365
7366static void
7367add_partial_subprogram (struct partial_die_info *pdi,
7368 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7369 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7370{
7371 if (pdi->tag == DW_TAG_subprogram)
7372 {
7373 if (pdi->has_pc_info)
7374 {
7375 if (pdi->lowpc < *lowpc)
7376 *lowpc = pdi->lowpc;
7377 if (pdi->highpc > *highpc)
7378 *highpc = pdi->highpc;
cdc07690 7379 if (set_addrmap)
5734ee8b 7380 {
5734ee8b 7381 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7383 CORE_ADDR baseaddr;
7384 CORE_ADDR highpc;
7385 CORE_ADDR lowpc;
5734ee8b
DJ
7386
7387 baseaddr = ANOFFSET (objfile->section_offsets,
7388 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7389 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7390 pdi->lowpc + baseaddr);
7391 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7392 pdi->highpc + baseaddr);
7393 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7394 cu->per_cu->v.psymtab);
5734ee8b 7395 }
481860b3
GB
7396 }
7397
7398 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7399 {
bc30ff58 7400 if (!pdi->is_declaration)
e8d05480
JB
7401 /* Ignore subprogram DIEs that do not have a name, they are
7402 illegal. Do not emit a complaint at this point, we will
7403 do so when we convert this psymtab into a symtab. */
7404 if (pdi->name)
7405 add_partial_symbol (pdi, cu);
bc30ff58
JB
7406 }
7407 }
6e70227d 7408
bc30ff58
JB
7409 if (! pdi->has_children)
7410 return;
7411
7412 if (cu->language == language_ada)
7413 {
7414 pdi = pdi->die_child;
7415 while (pdi != NULL)
7416 {
7417 fixup_partial_die (pdi, cu);
7418 if (pdi->tag == DW_TAG_subprogram
7419 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7421 pdi = pdi->die_sibling;
7422 }
7423 }
7424}
7425
91c24f0a
DC
7426/* Read a partial die corresponding to an enumeration type. */
7427
72bf9492
DJ
7428static void
7429add_partial_enumeration (struct partial_die_info *enum_pdi,
7430 struct dwarf2_cu *cu)
91c24f0a 7431{
72bf9492 7432 struct partial_die_info *pdi;
91c24f0a
DC
7433
7434 if (enum_pdi->name != NULL)
72bf9492
DJ
7435 add_partial_symbol (enum_pdi, cu);
7436
7437 pdi = enum_pdi->die_child;
7438 while (pdi)
91c24f0a 7439 {
72bf9492 7440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7441 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7442 else
72bf9492
DJ
7443 add_partial_symbol (pdi, cu);
7444 pdi = pdi->die_sibling;
91c24f0a 7445 }
91c24f0a
DC
7446}
7447
6caca83c
CC
7448/* Return the initial uleb128 in the die at INFO_PTR. */
7449
7450static unsigned int
d521ce57 7451peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7452{
7453 unsigned int bytes_read;
7454
7455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7456}
7457
4bb7a0a7
DJ
7458/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7459 Return the corresponding abbrev, or NULL if the number is zero (indicating
7460 an empty DIE). In either case *BYTES_READ will be set to the length of
7461 the initial number. */
7462
7463static struct abbrev_info *
d521ce57 7464peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7465 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7466{
7467 bfd *abfd = cu->objfile->obfd;
7468 unsigned int abbrev_number;
7469 struct abbrev_info *abbrev;
7470
7471 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7472
7473 if (abbrev_number == 0)
7474 return NULL;
7475
433df2d4 7476 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7477 if (!abbrev)
7478 {
422b9917
DE
7479 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7480 " at offset 0x%x [in module %s]"),
7481 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7482 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7483 }
7484
7485 return abbrev;
7486}
7487
93311388
DE
7488/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7489 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7490 DIE. Any children of the skipped DIEs will also be skipped. */
7491
d521ce57
TT
7492static const gdb_byte *
7493skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7494{
dee91e82 7495 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7496 struct abbrev_info *abbrev;
7497 unsigned int bytes_read;
7498
7499 while (1)
7500 {
7501 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7502 if (abbrev == NULL)
7503 return info_ptr + bytes_read;
7504 else
dee91e82 7505 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7506 }
7507}
7508
93311388
DE
7509/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7510 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7511 abbrev corresponding to that skipped uleb128 should be passed in
7512 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7513 children. */
7514
d521ce57
TT
7515static const gdb_byte *
7516skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7517 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7518{
7519 unsigned int bytes_read;
7520 struct attribute attr;
dee91e82
DE
7521 bfd *abfd = reader->abfd;
7522 struct dwarf2_cu *cu = reader->cu;
d521ce57 7523 const gdb_byte *buffer = reader->buffer;
f664829e 7524 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7525 unsigned int form, i;
7526
7527 for (i = 0; i < abbrev->num_attrs; i++)
7528 {
7529 /* The only abbrev we care about is DW_AT_sibling. */
7530 if (abbrev->attrs[i].name == DW_AT_sibling)
7531 {
dee91e82 7532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7533 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7534 complaint (&symfile_complaints,
7535 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7536 else
b9502d3f 7537 {
9c541725
PA
7538 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7540
7541 if (sibling_ptr < info_ptr)
7542 complaint (&symfile_complaints,
7543 _("DW_AT_sibling points backwards"));
22869d73
KS
7544 else if (sibling_ptr > reader->buffer_end)
7545 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7546 else
7547 return sibling_ptr;
7548 }
4bb7a0a7
DJ
7549 }
7550
7551 /* If it isn't DW_AT_sibling, skip this attribute. */
7552 form = abbrev->attrs[i].form;
7553 skip_attribute:
7554 switch (form)
7555 {
4bb7a0a7 7556 case DW_FORM_ref_addr:
ae411497
TT
7557 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7558 and later it is offset sized. */
7559 if (cu->header.version == 2)
7560 info_ptr += cu->header.addr_size;
7561 else
7562 info_ptr += cu->header.offset_size;
7563 break;
36586728
TT
7564 case DW_FORM_GNU_ref_alt:
7565 info_ptr += cu->header.offset_size;
7566 break;
ae411497 7567 case DW_FORM_addr:
4bb7a0a7
DJ
7568 info_ptr += cu->header.addr_size;
7569 break;
7570 case DW_FORM_data1:
7571 case DW_FORM_ref1:
7572 case DW_FORM_flag:
7573 info_ptr += 1;
7574 break;
2dc7f7b3 7575 case DW_FORM_flag_present:
43988095 7576 case DW_FORM_implicit_const:
2dc7f7b3 7577 break;
4bb7a0a7
DJ
7578 case DW_FORM_data2:
7579 case DW_FORM_ref2:
7580 info_ptr += 2;
7581 break;
7582 case DW_FORM_data4:
7583 case DW_FORM_ref4:
7584 info_ptr += 4;
7585 break;
7586 case DW_FORM_data8:
7587 case DW_FORM_ref8:
55f1336d 7588 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7589 info_ptr += 8;
7590 break;
0224619f
JK
7591 case DW_FORM_data16:
7592 info_ptr += 16;
7593 break;
4bb7a0a7 7594 case DW_FORM_string:
9b1c24c8 7595 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7596 info_ptr += bytes_read;
7597 break;
2dc7f7b3 7598 case DW_FORM_sec_offset:
4bb7a0a7 7599 case DW_FORM_strp:
36586728 7600 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7601 info_ptr += cu->header.offset_size;
7602 break;
2dc7f7b3 7603 case DW_FORM_exprloc:
4bb7a0a7
DJ
7604 case DW_FORM_block:
7605 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7606 info_ptr += bytes_read;
7607 break;
7608 case DW_FORM_block1:
7609 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7610 break;
7611 case DW_FORM_block2:
7612 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7613 break;
7614 case DW_FORM_block4:
7615 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7616 break;
7617 case DW_FORM_sdata:
7618 case DW_FORM_udata:
7619 case DW_FORM_ref_udata:
3019eac3
DE
7620 case DW_FORM_GNU_addr_index:
7621 case DW_FORM_GNU_str_index:
d521ce57 7622 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7623 break;
7624 case DW_FORM_indirect:
7625 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7626 info_ptr += bytes_read;
7627 /* We need to continue parsing from here, so just go back to
7628 the top. */
7629 goto skip_attribute;
7630
7631 default:
3e43a32a
MS
7632 error (_("Dwarf Error: Cannot handle %s "
7633 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7634 dwarf_form_name (form),
7635 bfd_get_filename (abfd));
7636 }
7637 }
7638
7639 if (abbrev->has_children)
dee91e82 7640 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7641 else
7642 return info_ptr;
7643}
7644
93311388 7645/* Locate ORIG_PDI's sibling.
dee91e82 7646 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7647
d521ce57 7648static const gdb_byte *
dee91e82
DE
7649locate_pdi_sibling (const struct die_reader_specs *reader,
7650 struct partial_die_info *orig_pdi,
d521ce57 7651 const gdb_byte *info_ptr)
91c24f0a
DC
7652{
7653 /* Do we know the sibling already? */
72bf9492 7654
91c24f0a
DC
7655 if (orig_pdi->sibling)
7656 return orig_pdi->sibling;
7657
7658 /* Are there any children to deal with? */
7659
7660 if (!orig_pdi->has_children)
7661 return info_ptr;
7662
4bb7a0a7 7663 /* Skip the children the long way. */
91c24f0a 7664
dee91e82 7665 return skip_children (reader, info_ptr);
91c24f0a
DC
7666}
7667
257e7a09 7668/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7669 not NULL. */
c906108c
SS
7670
7671static void
257e7a09
YQ
7672dwarf2_read_symtab (struct partial_symtab *self,
7673 struct objfile *objfile)
c906108c 7674{
257e7a09 7675 if (self->readin)
c906108c 7676 {
442e4d9c 7677 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7678 self->filename);
442e4d9c
YQ
7679 }
7680 else
7681 {
7682 if (info_verbose)
c906108c 7683 {
442e4d9c 7684 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7685 self->filename);
442e4d9c 7686 gdb_flush (gdb_stdout);
c906108c 7687 }
c906108c 7688
442e4d9c 7689 /* Restore our global data. */
9a3c8263
SM
7690 dwarf2_per_objfile
7691 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7692 dwarf2_objfile_data_key);
10b3939b 7693
442e4d9c
YQ
7694 /* If this psymtab is constructed from a debug-only objfile, the
7695 has_section_at_zero flag will not necessarily be correct. We
7696 can get the correct value for this flag by looking at the data
7697 associated with the (presumably stripped) associated objfile. */
7698 if (objfile->separate_debug_objfile_backlink)
7699 {
7700 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7701 = ((struct dwarf2_per_objfile *)
7702 objfile_data (objfile->separate_debug_objfile_backlink,
7703 dwarf2_objfile_data_key));
9a619af0 7704
442e4d9c
YQ
7705 dwarf2_per_objfile->has_section_at_zero
7706 = dpo_backlink->has_section_at_zero;
7707 }
b2ab525c 7708
442e4d9c 7709 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7710
257e7a09 7711 psymtab_to_symtab_1 (self);
c906108c 7712
442e4d9c
YQ
7713 /* Finish up the debug error message. */
7714 if (info_verbose)
7715 printf_filtered (_("done.\n"));
c906108c 7716 }
95554aad
TT
7717
7718 process_cu_includes ();
c906108c 7719}
9cdd5dbd
DE
7720\f
7721/* Reading in full CUs. */
c906108c 7722
10b3939b
DJ
7723/* Add PER_CU to the queue. */
7724
7725static void
95554aad
TT
7726queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7727 enum language pretend_language)
10b3939b
DJ
7728{
7729 struct dwarf2_queue_item *item;
7730
7731 per_cu->queued = 1;
8d749320 7732 item = XNEW (struct dwarf2_queue_item);
10b3939b 7733 item->per_cu = per_cu;
95554aad 7734 item->pretend_language = pretend_language;
10b3939b
DJ
7735 item->next = NULL;
7736
7737 if (dwarf2_queue == NULL)
7738 dwarf2_queue = item;
7739 else
7740 dwarf2_queue_tail->next = item;
7741
7742 dwarf2_queue_tail = item;
7743}
7744
89e63ee4
DE
7745/* If PER_CU is not yet queued, add it to the queue.
7746 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7747 dependency.
0907af0c 7748 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7749 meaning either PER_CU is already queued or it is already loaded.
7750
7751 N.B. There is an invariant here that if a CU is queued then it is loaded.
7752 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7753
7754static int
89e63ee4 7755maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7756 struct dwarf2_per_cu_data *per_cu,
7757 enum language pretend_language)
7758{
7759 /* We may arrive here during partial symbol reading, if we need full
7760 DIEs to process an unusual case (e.g. template arguments). Do
7761 not queue PER_CU, just tell our caller to load its DIEs. */
7762 if (dwarf2_per_objfile->reading_partial_symbols)
7763 {
7764 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7765 return 1;
7766 return 0;
7767 }
7768
7769 /* Mark the dependence relation so that we don't flush PER_CU
7770 too early. */
89e63ee4
DE
7771 if (dependent_cu != NULL)
7772 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7773
7774 /* If it's already on the queue, we have nothing to do. */
7775 if (per_cu->queued)
7776 return 0;
7777
7778 /* If the compilation unit is already loaded, just mark it as
7779 used. */
7780 if (per_cu->cu != NULL)
7781 {
7782 per_cu->cu->last_used = 0;
7783 return 0;
7784 }
7785
7786 /* Add it to the queue. */
7787 queue_comp_unit (per_cu, pretend_language);
7788
7789 return 1;
7790}
7791
10b3939b
DJ
7792/* Process the queue. */
7793
7794static void
a0f42c21 7795process_queue (void)
10b3939b
DJ
7796{
7797 struct dwarf2_queue_item *item, *next_item;
7798
b4f54984 7799 if (dwarf_read_debug)
45cfd468
DE
7800 {
7801 fprintf_unfiltered (gdb_stdlog,
7802 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7803 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7804 }
7805
03dd20cc
DJ
7806 /* The queue starts out with one item, but following a DIE reference
7807 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7808 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7809 {
cc12ce38
DE
7810 if ((dwarf2_per_objfile->using_index
7811 ? !item->per_cu->v.quick->compunit_symtab
7812 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7813 /* Skip dummy CUs. */
7814 && item->per_cu->cu != NULL)
f4dc4d17
DE
7815 {
7816 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7817 unsigned int debug_print_threshold;
247f5c4f 7818 char buf[100];
f4dc4d17 7819
247f5c4f 7820 if (per_cu->is_debug_types)
f4dc4d17 7821 {
247f5c4f
DE
7822 struct signatured_type *sig_type =
7823 (struct signatured_type *) per_cu;
7824
7825 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7826 hex_string (sig_type->signature),
9c541725 7827 to_underlying (per_cu->sect_off));
73be47f5
DE
7828 /* There can be 100s of TUs.
7829 Only print them in verbose mode. */
7830 debug_print_threshold = 2;
f4dc4d17 7831 }
247f5c4f 7832 else
73be47f5 7833 {
9c541725
PA
7834 sprintf (buf, "CU at offset 0x%x",
7835 to_underlying (per_cu->sect_off));
73be47f5
DE
7836 debug_print_threshold = 1;
7837 }
247f5c4f 7838
b4f54984 7839 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7840 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7841
7842 if (per_cu->is_debug_types)
7843 process_full_type_unit (per_cu, item->pretend_language);
7844 else
7845 process_full_comp_unit (per_cu, item->pretend_language);
7846
b4f54984 7847 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7848 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7849 }
10b3939b
DJ
7850
7851 item->per_cu->queued = 0;
7852 next_item = item->next;
7853 xfree (item);
7854 }
7855
7856 dwarf2_queue_tail = NULL;
45cfd468 7857
b4f54984 7858 if (dwarf_read_debug)
45cfd468
DE
7859 {
7860 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7861 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7862 }
10b3939b
DJ
7863}
7864
7865/* Free all allocated queue entries. This function only releases anything if
7866 an error was thrown; if the queue was processed then it would have been
7867 freed as we went along. */
7868
7869static void
7870dwarf2_release_queue (void *dummy)
7871{
7872 struct dwarf2_queue_item *item, *last;
7873
7874 item = dwarf2_queue;
7875 while (item)
7876 {
7877 /* Anything still marked queued is likely to be in an
7878 inconsistent state, so discard it. */
7879 if (item->per_cu->queued)
7880 {
7881 if (item->per_cu->cu != NULL)
dee91e82 7882 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7883 item->per_cu->queued = 0;
7884 }
7885
7886 last = item;
7887 item = item->next;
7888 xfree (last);
7889 }
7890
7891 dwarf2_queue = dwarf2_queue_tail = NULL;
7892}
7893
7894/* Read in full symbols for PST, and anything it depends on. */
7895
c906108c 7896static void
fba45db2 7897psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7898{
10b3939b 7899 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7900 int i;
7901
95554aad
TT
7902 if (pst->readin)
7903 return;
7904
aaa75496 7905 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7906 if (!pst->dependencies[i]->readin
7907 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7908 {
7909 /* Inform about additional files that need to be read in. */
7910 if (info_verbose)
7911 {
a3f17187 7912 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7913 fputs_filtered (" ", gdb_stdout);
7914 wrap_here ("");
7915 fputs_filtered ("and ", gdb_stdout);
7916 wrap_here ("");
7917 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7918 wrap_here (""); /* Flush output. */
aaa75496
JB
7919 gdb_flush (gdb_stdout);
7920 }
7921 psymtab_to_symtab_1 (pst->dependencies[i]);
7922 }
7923
9a3c8263 7924 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7925
7926 if (per_cu == NULL)
aaa75496
JB
7927 {
7928 /* It's an include file, no symbols to read for it.
7929 Everything is in the parent symtab. */
7930 pst->readin = 1;
7931 return;
7932 }
c906108c 7933
a0f42c21 7934 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7935}
7936
dee91e82
DE
7937/* Trivial hash function for die_info: the hash value of a DIE
7938 is its offset in .debug_info for this objfile. */
10b3939b 7939
dee91e82
DE
7940static hashval_t
7941die_hash (const void *item)
10b3939b 7942{
9a3c8263 7943 const struct die_info *die = (const struct die_info *) item;
6502dd73 7944
9c541725 7945 return to_underlying (die->sect_off);
dee91e82 7946}
63d06c5c 7947
dee91e82
DE
7948/* Trivial comparison function for die_info structures: two DIEs
7949 are equal if they have the same offset. */
98bfdba5 7950
dee91e82
DE
7951static int
7952die_eq (const void *item_lhs, const void *item_rhs)
7953{
9a3c8263
SM
7954 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7955 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7956
9c541725 7957 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7958}
c906108c 7959
dee91e82
DE
7960/* die_reader_func for load_full_comp_unit.
7961 This is identical to read_signatured_type_reader,
7962 but is kept separate for now. */
c906108c 7963
dee91e82
DE
7964static void
7965load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7966 const gdb_byte *info_ptr,
dee91e82
DE
7967 struct die_info *comp_unit_die,
7968 int has_children,
7969 void *data)
7970{
7971 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7972 enum language *language_ptr = (enum language *) data;
6caca83c 7973
dee91e82
DE
7974 gdb_assert (cu->die_hash == NULL);
7975 cu->die_hash =
7976 htab_create_alloc_ex (cu->header.length / 12,
7977 die_hash,
7978 die_eq,
7979 NULL,
7980 &cu->comp_unit_obstack,
7981 hashtab_obstack_allocate,
7982 dummy_obstack_deallocate);
e142c38c 7983
dee91e82
DE
7984 if (has_children)
7985 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7986 &info_ptr, comp_unit_die);
7987 cu->dies = comp_unit_die;
7988 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7989
7990 /* We try not to read any attributes in this function, because not
9cdd5dbd 7991 all CUs needed for references have been loaded yet, and symbol
10b3939b 7992 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7993 or we won't be able to build types correctly.
7994 Similarly, if we do not read the producer, we can not apply
7995 producer-specific interpretation. */
95554aad 7996 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7997}
10b3939b 7998
dee91e82 7999/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8000
dee91e82 8001static void
95554aad
TT
8002load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8003 enum language pretend_language)
dee91e82 8004{
3019eac3 8005 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8006
f4dc4d17
DE
8007 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8008 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8009}
8010
3da10d80
KS
8011/* Add a DIE to the delayed physname list. */
8012
8013static void
8014add_to_method_list (struct type *type, int fnfield_index, int index,
8015 const char *name, struct die_info *die,
8016 struct dwarf2_cu *cu)
8017{
8018 struct delayed_method_info mi;
8019 mi.type = type;
8020 mi.fnfield_index = fnfield_index;
8021 mi.index = index;
8022 mi.name = name;
8023 mi.die = die;
8024 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8025}
8026
8027/* A cleanup for freeing the delayed method list. */
8028
8029static void
8030free_delayed_list (void *ptr)
8031{
8032 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8033 if (cu->method_list != NULL)
8034 {
8035 VEC_free (delayed_method_info, cu->method_list);
8036 cu->method_list = NULL;
8037 }
8038}
8039
3693fdb3
PA
8040/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8041 "const" / "volatile". If so, decrements LEN by the length of the
8042 modifier and return true. Otherwise return false. */
8043
8044template<size_t N>
8045static bool
8046check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8047{
8048 size_t mod_len = sizeof (mod) - 1;
8049 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8050 {
8051 len -= mod_len;
8052 return true;
8053 }
8054 return false;
8055}
8056
3da10d80
KS
8057/* Compute the physnames of any methods on the CU's method list.
8058
8059 The computation of method physnames is delayed in order to avoid the
8060 (bad) condition that one of the method's formal parameters is of an as yet
8061 incomplete type. */
8062
8063static void
8064compute_delayed_physnames (struct dwarf2_cu *cu)
8065{
8066 int i;
8067 struct delayed_method_info *mi;
3693fdb3
PA
8068
8069 /* Only C++ delays computing physnames. */
8070 if (VEC_empty (delayed_method_info, cu->method_list))
8071 return;
8072 gdb_assert (cu->language == language_cplus);
8073
3da10d80
KS
8074 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8075 {
1d06ead6 8076 const char *physname;
3da10d80
KS
8077 struct fn_fieldlist *fn_flp
8078 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8079 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8080 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8081 = physname ? physname : "";
3693fdb3
PA
8082
8083 /* Since there's no tag to indicate whether a method is a
8084 const/volatile overload, extract that information out of the
8085 demangled name. */
8086 if (physname != NULL)
8087 {
8088 size_t len = strlen (physname);
8089
8090 while (1)
8091 {
8092 if (physname[len] == ')') /* shortcut */
8093 break;
8094 else if (check_modifier (physname, len, " const"))
8095 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8096 else if (check_modifier (physname, len, " volatile"))
8097 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8098 else
8099 break;
8100 }
8101 }
3da10d80
KS
8102 }
8103}
8104
a766d390
DE
8105/* Go objects should be embedded in a DW_TAG_module DIE,
8106 and it's not clear if/how imported objects will appear.
8107 To keep Go support simple until that's worked out,
8108 go back through what we've read and create something usable.
8109 We could do this while processing each DIE, and feels kinda cleaner,
8110 but that way is more invasive.
8111 This is to, for example, allow the user to type "p var" or "b main"
8112 without having to specify the package name, and allow lookups
8113 of module.object to work in contexts that use the expression
8114 parser. */
8115
8116static void
8117fixup_go_packaging (struct dwarf2_cu *cu)
8118{
8119 char *package_name = NULL;
8120 struct pending *list;
8121 int i;
8122
8123 for (list = global_symbols; list != NULL; list = list->next)
8124 {
8125 for (i = 0; i < list->nsyms; ++i)
8126 {
8127 struct symbol *sym = list->symbol[i];
8128
8129 if (SYMBOL_LANGUAGE (sym) == language_go
8130 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8131 {
8132 char *this_package_name = go_symbol_package_name (sym);
8133
8134 if (this_package_name == NULL)
8135 continue;
8136 if (package_name == NULL)
8137 package_name = this_package_name;
8138 else
8139 {
8140 if (strcmp (package_name, this_package_name) != 0)
8141 complaint (&symfile_complaints,
8142 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8143 (symbol_symtab (sym) != NULL
8144 ? symtab_to_filename_for_display
8145 (symbol_symtab (sym))
4262abfb 8146 : objfile_name (cu->objfile)),
a766d390
DE
8147 this_package_name, package_name);
8148 xfree (this_package_name);
8149 }
8150 }
8151 }
8152 }
8153
8154 if (package_name != NULL)
8155 {
8156 struct objfile *objfile = cu->objfile;
34a68019 8157 const char *saved_package_name
224c3ddb
SM
8158 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8159 package_name,
8160 strlen (package_name));
19f392bc
UW
8161 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8162 saved_package_name);
a766d390
DE
8163 struct symbol *sym;
8164
8165 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8166
e623cf5d 8167 sym = allocate_symbol (objfile);
f85f34ed 8168 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8169 SYMBOL_SET_NAMES (sym, saved_package_name,
8170 strlen (saved_package_name), 0, objfile);
a766d390
DE
8171 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8172 e.g., "main" finds the "main" module and not C's main(). */
8173 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8174 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8175 SYMBOL_TYPE (sym) = type;
8176
8177 add_symbol_to_list (sym, &global_symbols);
8178
8179 xfree (package_name);
8180 }
8181}
8182
95554aad
TT
8183/* Return the symtab for PER_CU. This works properly regardless of
8184 whether we're using the index or psymtabs. */
8185
43f3e411
DE
8186static struct compunit_symtab *
8187get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8188{
8189 return (dwarf2_per_objfile->using_index
43f3e411
DE
8190 ? per_cu->v.quick->compunit_symtab
8191 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8192}
8193
8194/* A helper function for computing the list of all symbol tables
8195 included by PER_CU. */
8196
8197static void
43f3e411 8198recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8199 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8200 struct dwarf2_per_cu_data *per_cu,
43f3e411 8201 struct compunit_symtab *immediate_parent)
95554aad
TT
8202{
8203 void **slot;
8204 int ix;
43f3e411 8205 struct compunit_symtab *cust;
95554aad
TT
8206 struct dwarf2_per_cu_data *iter;
8207
8208 slot = htab_find_slot (all_children, per_cu, INSERT);
8209 if (*slot != NULL)
8210 {
8211 /* This inclusion and its children have been processed. */
8212 return;
8213 }
8214
8215 *slot = per_cu;
8216 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8217 cust = get_compunit_symtab (per_cu);
8218 if (cust != NULL)
ec94af83
DE
8219 {
8220 /* If this is a type unit only add its symbol table if we haven't
8221 seen it yet (type unit per_cu's can share symtabs). */
8222 if (per_cu->is_debug_types)
8223 {
43f3e411 8224 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8225 if (*slot == NULL)
8226 {
43f3e411
DE
8227 *slot = cust;
8228 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8229 if (cust->user == NULL)
8230 cust->user = immediate_parent;
ec94af83
DE
8231 }
8232 }
8233 else
f9125b6c 8234 {
43f3e411
DE
8235 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8236 if (cust->user == NULL)
8237 cust->user = immediate_parent;
f9125b6c 8238 }
ec94af83 8239 }
95554aad
TT
8240
8241 for (ix = 0;
796a7ff8 8242 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8243 ++ix)
ec94af83
DE
8244 {
8245 recursively_compute_inclusions (result, all_children,
43f3e411 8246 all_type_symtabs, iter, cust);
ec94af83 8247 }
95554aad
TT
8248}
8249
43f3e411 8250/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8251 PER_CU. */
8252
8253static void
43f3e411 8254compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8255{
f4dc4d17
DE
8256 gdb_assert (! per_cu->is_debug_types);
8257
796a7ff8 8258 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8259 {
8260 int ix, len;
ec94af83 8261 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8262 struct compunit_symtab *compunit_symtab_iter;
8263 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8264 htab_t all_children, all_type_symtabs;
43f3e411 8265 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8266
8267 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8268 if (cust == NULL)
95554aad
TT
8269 return;
8270
8271 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8272 NULL, xcalloc, xfree);
ec94af83
DE
8273 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8274 NULL, xcalloc, xfree);
95554aad
TT
8275
8276 for (ix = 0;
796a7ff8 8277 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8278 ix, per_cu_iter);
95554aad 8279 ++ix)
ec94af83
DE
8280 {
8281 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8282 all_type_symtabs, per_cu_iter,
43f3e411 8283 cust);
ec94af83 8284 }
95554aad 8285
ec94af83 8286 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8287 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8288 cust->includes
8d749320
SM
8289 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8290 struct compunit_symtab *, len + 1);
95554aad 8291 for (ix = 0;
43f3e411
DE
8292 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8293 compunit_symtab_iter);
95554aad 8294 ++ix)
43f3e411
DE
8295 cust->includes[ix] = compunit_symtab_iter;
8296 cust->includes[len] = NULL;
95554aad 8297
43f3e411 8298 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8299 htab_delete (all_children);
ec94af83 8300 htab_delete (all_type_symtabs);
95554aad
TT
8301 }
8302}
8303
8304/* Compute the 'includes' field for the symtabs of all the CUs we just
8305 read. */
8306
8307static void
8308process_cu_includes (void)
8309{
8310 int ix;
8311 struct dwarf2_per_cu_data *iter;
8312
8313 for (ix = 0;
8314 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8315 ix, iter);
8316 ++ix)
f4dc4d17
DE
8317 {
8318 if (! iter->is_debug_types)
43f3e411 8319 compute_compunit_symtab_includes (iter);
f4dc4d17 8320 }
95554aad
TT
8321
8322 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8323}
8324
9cdd5dbd 8325/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8326 already been loaded into memory. */
8327
8328static void
95554aad
TT
8329process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8330 enum language pretend_language)
10b3939b 8331{
10b3939b 8332 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8333 struct objfile *objfile = per_cu->objfile;
3e29f34a 8334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8335 CORE_ADDR lowpc, highpc;
43f3e411 8336 struct compunit_symtab *cust;
3da10d80 8337 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8338 CORE_ADDR baseaddr;
4359dff1 8339 struct block *static_block;
3e29f34a 8340 CORE_ADDR addr;
10b3939b
DJ
8341
8342 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8343
10b3939b
DJ
8344 buildsym_init ();
8345 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8346 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8347
8348 cu->list_in_scope = &file_symbols;
c906108c 8349
95554aad
TT
8350 cu->language = pretend_language;
8351 cu->language_defn = language_def (cu->language);
8352
c906108c 8353 /* Do line number decoding in read_file_scope () */
10b3939b 8354 process_die (cu->dies, cu);
c906108c 8355
a766d390
DE
8356 /* For now fudge the Go package. */
8357 if (cu->language == language_go)
8358 fixup_go_packaging (cu);
8359
3da10d80
KS
8360 /* Now that we have processed all the DIEs in the CU, all the types
8361 should be complete, and it should now be safe to compute all of the
8362 physnames. */
8363 compute_delayed_physnames (cu);
8364 do_cleanups (delayed_list_cleanup);
8365
fae299cd
DC
8366 /* Some compilers don't define a DW_AT_high_pc attribute for the
8367 compilation unit. If the DW_AT_high_pc is missing, synthesize
8368 it, by scanning the DIE's below the compilation unit. */
10b3939b 8369 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8370
3e29f34a
MR
8371 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8372 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8373
8374 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8375 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8376 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8377 addrmap to help ensure it has an accurate map of pc values belonging to
8378 this comp unit. */
8379 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8380
43f3e411
DE
8381 cust = end_symtab_from_static_block (static_block,
8382 SECT_OFF_TEXT (objfile), 0);
c906108c 8383
43f3e411 8384 if (cust != NULL)
c906108c 8385 {
df15bd07 8386 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8387
8be455d7
JK
8388 /* Set symtab language to language from DW_AT_language. If the
8389 compilation is from a C file generated by language preprocessors, do
8390 not set the language if it was already deduced by start_subfile. */
43f3e411 8391 if (!(cu->language == language_c
40e3ad0e 8392 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8393 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8394
8395 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8396 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8397 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8398 there were bugs in prologue debug info, fixed later in GCC-4.5
8399 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8400
8401 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8402 needed, it would be wrong due to missing DW_AT_producer there.
8403
8404 Still one can confuse GDB by using non-standard GCC compilation
8405 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8406 */
ab260dad 8407 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8408 cust->locations_valid = 1;
e0d00bc7
JK
8409
8410 if (gcc_4_minor >= 5)
43f3e411 8411 cust->epilogue_unwind_valid = 1;
96408a79 8412
43f3e411 8413 cust->call_site_htab = cu->call_site_htab;
c906108c 8414 }
9291a0cd
TT
8415
8416 if (dwarf2_per_objfile->using_index)
43f3e411 8417 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8418 else
8419 {
8420 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8421 pst->compunit_symtab = cust;
9291a0cd
TT
8422 pst->readin = 1;
8423 }
c906108c 8424
95554aad
TT
8425 /* Push it for inclusion processing later. */
8426 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8427
c906108c 8428 do_cleanups (back_to);
f4dc4d17 8429}
45cfd468 8430
f4dc4d17
DE
8431/* Generate full symbol information for type unit PER_CU, whose DIEs have
8432 already been loaded into memory. */
8433
8434static void
8435process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8436 enum language pretend_language)
8437{
8438 struct dwarf2_cu *cu = per_cu->cu;
8439 struct objfile *objfile = per_cu->objfile;
43f3e411 8440 struct compunit_symtab *cust;
f4dc4d17 8441 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8442 struct signatured_type *sig_type;
8443
8444 gdb_assert (per_cu->is_debug_types);
8445 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8446
8447 buildsym_init ();
8448 back_to = make_cleanup (really_free_pendings, NULL);
8449 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8450
8451 cu->list_in_scope = &file_symbols;
8452
8453 cu->language = pretend_language;
8454 cu->language_defn = language_def (cu->language);
8455
8456 /* The symbol tables are set up in read_type_unit_scope. */
8457 process_die (cu->dies, cu);
8458
8459 /* For now fudge the Go package. */
8460 if (cu->language == language_go)
8461 fixup_go_packaging (cu);
8462
8463 /* Now that we have processed all the DIEs in the CU, all the types
8464 should be complete, and it should now be safe to compute all of the
8465 physnames. */
8466 compute_delayed_physnames (cu);
8467 do_cleanups (delayed_list_cleanup);
8468
8469 /* TUs share symbol tables.
8470 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8471 of it with end_expandable_symtab. Otherwise, complete the addition of
8472 this TU's symbols to the existing symtab. */
43f3e411 8473 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8474 {
43f3e411
DE
8475 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8476 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8477
43f3e411 8478 if (cust != NULL)
f4dc4d17
DE
8479 {
8480 /* Set symtab language to language from DW_AT_language. If the
8481 compilation is from a C file generated by language preprocessors,
8482 do not set the language if it was already deduced by
8483 start_subfile. */
43f3e411
DE
8484 if (!(cu->language == language_c
8485 && COMPUNIT_FILETABS (cust)->language != language_c))
8486 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8487 }
8488 }
8489 else
8490 {
0ab9ce85 8491 augment_type_symtab ();
43f3e411 8492 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8493 }
8494
8495 if (dwarf2_per_objfile->using_index)
43f3e411 8496 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8497 else
8498 {
8499 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8500 pst->compunit_symtab = cust;
f4dc4d17 8501 pst->readin = 1;
45cfd468 8502 }
f4dc4d17
DE
8503
8504 do_cleanups (back_to);
c906108c
SS
8505}
8506
95554aad
TT
8507/* Process an imported unit DIE. */
8508
8509static void
8510process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8511{
8512 struct attribute *attr;
8513
f4dc4d17
DE
8514 /* For now we don't handle imported units in type units. */
8515 if (cu->per_cu->is_debug_types)
8516 {
8517 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8518 " supported in type units [in module %s]"),
4262abfb 8519 objfile_name (cu->objfile));
f4dc4d17
DE
8520 }
8521
95554aad
TT
8522 attr = dwarf2_attr (die, DW_AT_import, cu);
8523 if (attr != NULL)
8524 {
9c541725
PA
8525 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8526 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8527 dwarf2_per_cu_data *per_cu
8528 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8529
69d751e3 8530 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8531 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8532 load_full_comp_unit (per_cu, cu->language);
8533
796a7ff8 8534 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8535 per_cu);
8536 }
8537}
8538
4c8aa72d
PA
8539/* RAII object that represents a process_die scope: i.e.,
8540 starts/finishes processing a DIE. */
8541class process_die_scope
adde2bff 8542{
4c8aa72d
PA
8543public:
8544 process_die_scope (die_info *die, dwarf2_cu *cu)
8545 : m_die (die), m_cu (cu)
8546 {
8547 /* We should only be processing DIEs not already in process. */
8548 gdb_assert (!m_die->in_process);
8549 m_die->in_process = true;
8550 }
8c3cb9fa 8551
4c8aa72d
PA
8552 ~process_die_scope ()
8553 {
8554 m_die->in_process = false;
8555
8556 /* If we're done processing the DIE for the CU that owns the line
8557 header, we don't need the line header anymore. */
8558 if (m_cu->line_header_die_owner == m_die)
8559 {
8560 delete m_cu->line_header;
8561 m_cu->line_header = NULL;
8562 m_cu->line_header_die_owner = NULL;
8563 }
8564 }
8565
8566private:
8567 die_info *m_die;
8568 dwarf2_cu *m_cu;
8569};
adde2bff 8570
c906108c
SS
8571/* Process a die and its children. */
8572
8573static void
e7c27a73 8574process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8575{
4c8aa72d 8576 process_die_scope scope (die, cu);
adde2bff 8577
c906108c
SS
8578 switch (die->tag)
8579 {
8580 case DW_TAG_padding:
8581 break;
8582 case DW_TAG_compile_unit:
95554aad 8583 case DW_TAG_partial_unit:
e7c27a73 8584 read_file_scope (die, cu);
c906108c 8585 break;
348e048f
DE
8586 case DW_TAG_type_unit:
8587 read_type_unit_scope (die, cu);
8588 break;
c906108c 8589 case DW_TAG_subprogram:
c906108c 8590 case DW_TAG_inlined_subroutine:
edb3359d 8591 read_func_scope (die, cu);
c906108c
SS
8592 break;
8593 case DW_TAG_lexical_block:
14898363
L
8594 case DW_TAG_try_block:
8595 case DW_TAG_catch_block:
e7c27a73 8596 read_lexical_block_scope (die, cu);
c906108c 8597 break;
216f72a1 8598 case DW_TAG_call_site:
96408a79
SA
8599 case DW_TAG_GNU_call_site:
8600 read_call_site_scope (die, cu);
8601 break;
c906108c 8602 case DW_TAG_class_type:
680b30c7 8603 case DW_TAG_interface_type:
c906108c
SS
8604 case DW_TAG_structure_type:
8605 case DW_TAG_union_type:
134d01f1 8606 process_structure_scope (die, cu);
c906108c
SS
8607 break;
8608 case DW_TAG_enumeration_type:
134d01f1 8609 process_enumeration_scope (die, cu);
c906108c 8610 break;
134d01f1 8611
f792889a
DJ
8612 /* These dies have a type, but processing them does not create
8613 a symbol or recurse to process the children. Therefore we can
8614 read them on-demand through read_type_die. */
c906108c 8615 case DW_TAG_subroutine_type:
72019c9c 8616 case DW_TAG_set_type:
c906108c 8617 case DW_TAG_array_type:
c906108c 8618 case DW_TAG_pointer_type:
c906108c 8619 case DW_TAG_ptr_to_member_type:
c906108c 8620 case DW_TAG_reference_type:
4297a3f0 8621 case DW_TAG_rvalue_reference_type:
c906108c 8622 case DW_TAG_string_type:
c906108c 8623 break;
134d01f1 8624
c906108c 8625 case DW_TAG_base_type:
a02abb62 8626 case DW_TAG_subrange_type:
cb249c71 8627 case DW_TAG_typedef:
134d01f1
DJ
8628 /* Add a typedef symbol for the type definition, if it has a
8629 DW_AT_name. */
f792889a 8630 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8631 break;
c906108c 8632 case DW_TAG_common_block:
e7c27a73 8633 read_common_block (die, cu);
c906108c
SS
8634 break;
8635 case DW_TAG_common_inclusion:
8636 break;
d9fa45fe 8637 case DW_TAG_namespace:
4d4ec4e5 8638 cu->processing_has_namespace_info = 1;
e7c27a73 8639 read_namespace (die, cu);
d9fa45fe 8640 break;
5d7cb8df 8641 case DW_TAG_module:
4d4ec4e5 8642 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8643 read_module (die, cu);
8644 break;
d9fa45fe 8645 case DW_TAG_imported_declaration:
74921315
KS
8646 cu->processing_has_namespace_info = 1;
8647 if (read_namespace_alias (die, cu))
8648 break;
8649 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8650 case DW_TAG_imported_module:
4d4ec4e5 8651 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8652 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8653 || cu->language != language_fortran))
8654 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8655 dwarf_tag_name (die->tag));
8656 read_import_statement (die, cu);
d9fa45fe 8657 break;
95554aad
TT
8658
8659 case DW_TAG_imported_unit:
8660 process_imported_unit_die (die, cu);
8661 break;
8662
c906108c 8663 default:
e7c27a73 8664 new_symbol (die, NULL, cu);
c906108c
SS
8665 break;
8666 }
8667}
ca69b9e6
DE
8668\f
8669/* DWARF name computation. */
c906108c 8670
94af9270
KS
8671/* A helper function for dwarf2_compute_name which determines whether DIE
8672 needs to have the name of the scope prepended to the name listed in the
8673 die. */
8674
8675static int
8676die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8677{
1c809c68
TT
8678 struct attribute *attr;
8679
94af9270
KS
8680 switch (die->tag)
8681 {
8682 case DW_TAG_namespace:
8683 case DW_TAG_typedef:
8684 case DW_TAG_class_type:
8685 case DW_TAG_interface_type:
8686 case DW_TAG_structure_type:
8687 case DW_TAG_union_type:
8688 case DW_TAG_enumeration_type:
8689 case DW_TAG_enumerator:
8690 case DW_TAG_subprogram:
08a76f8a 8691 case DW_TAG_inlined_subroutine:
94af9270 8692 case DW_TAG_member:
74921315 8693 case DW_TAG_imported_declaration:
94af9270
KS
8694 return 1;
8695
8696 case DW_TAG_variable:
c2b0a229 8697 case DW_TAG_constant:
94af9270
KS
8698 /* We only need to prefix "globally" visible variables. These include
8699 any variable marked with DW_AT_external or any variable that
8700 lives in a namespace. [Variables in anonymous namespaces
8701 require prefixing, but they are not DW_AT_external.] */
8702
8703 if (dwarf2_attr (die, DW_AT_specification, cu))
8704 {
8705 struct dwarf2_cu *spec_cu = cu;
9a619af0 8706
94af9270
KS
8707 return die_needs_namespace (die_specification (die, &spec_cu),
8708 spec_cu);
8709 }
8710
1c809c68 8711 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8712 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8713 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8714 return 0;
8715 /* A variable in a lexical block of some kind does not need a
8716 namespace, even though in C++ such variables may be external
8717 and have a mangled name. */
8718 if (die->parent->tag == DW_TAG_lexical_block
8719 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8720 || die->parent->tag == DW_TAG_catch_block
8721 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8722 return 0;
8723 return 1;
94af9270
KS
8724
8725 default:
8726 return 0;
8727 }
8728}
8729
73b9be8b
KS
8730/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8731 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8732 defined for the given DIE. */
8733
8734static struct attribute *
8735dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8736{
8737 struct attribute *attr;
8738
8739 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8740 if (attr == NULL)
8741 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8742
8743 return attr;
8744}
8745
8746/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8747 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8748 defined for the given DIE. */
8749
8750static const char *
8751dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8752{
8753 const char *linkage_name;
8754
8755 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8756 if (linkage_name == NULL)
8757 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8758
8759 return linkage_name;
8760}
8761
94af9270 8762/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8763 compute the physname for the object, which include a method's:
9c37b5ae 8764 - formal parameters (C++),
a766d390 8765 - receiver type (Go),
a766d390
DE
8766
8767 The term "physname" is a bit confusing.
8768 For C++, for example, it is the demangled name.
8769 For Go, for example, it's the mangled name.
94af9270 8770
af6b7be1
JB
8771 For Ada, return the DIE's linkage name rather than the fully qualified
8772 name. PHYSNAME is ignored..
8773
94af9270
KS
8774 The result is allocated on the objfile_obstack and canonicalized. */
8775
8776static const char *
15d034d0
TT
8777dwarf2_compute_name (const char *name,
8778 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8779 int physname)
8780{
bb5ed363
DE
8781 struct objfile *objfile = cu->objfile;
8782
94af9270
KS
8783 if (name == NULL)
8784 name = dwarf2_name (die, cu);
8785
2ee7123e
DE
8786 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8787 but otherwise compute it by typename_concat inside GDB.
8788 FIXME: Actually this is not really true, or at least not always true.
8789 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8790 Fortran names because there is no mangling standard. So new_symbol_full
8791 will set the demangled name to the result of dwarf2_full_name, and it is
8792 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8793 if (cu->language == language_ada
8794 || (cu->language == language_fortran && physname))
8795 {
8796 /* For Ada unit, we prefer the linkage name over the name, as
8797 the former contains the exported name, which the user expects
8798 to be able to reference. Ideally, we want the user to be able
8799 to reference this entity using either natural or linkage name,
8800 but we haven't started looking at this enhancement yet. */
73b9be8b 8801 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 8802
2ee7123e
DE
8803 if (linkage_name != NULL)
8804 return linkage_name;
f55ee35c
JK
8805 }
8806
94af9270
KS
8807 /* These are the only languages we know how to qualify names in. */
8808 if (name != NULL
9c37b5ae 8809 && (cu->language == language_cplus
c44af4eb
TT
8810 || cu->language == language_fortran || cu->language == language_d
8811 || cu->language == language_rust))
94af9270
KS
8812 {
8813 if (die_needs_namespace (die, cu))
8814 {
8815 long length;
0d5cff50 8816 const char *prefix;
34a68019 8817 const char *canonical_name = NULL;
94af9270 8818
d7e74731
PA
8819 string_file buf;
8820
94af9270 8821 prefix = determine_prefix (die, cu);
94af9270
KS
8822 if (*prefix != '\0')
8823 {
f55ee35c
JK
8824 char *prefixed_name = typename_concat (NULL, prefix, name,
8825 physname, cu);
9a619af0 8826
d7e74731 8827 buf.puts (prefixed_name);
94af9270
KS
8828 xfree (prefixed_name);
8829 }
8830 else
d7e74731 8831 buf.puts (name);
94af9270 8832
98bfdba5
PA
8833 /* Template parameters may be specified in the DIE's DW_AT_name, or
8834 as children with DW_TAG_template_type_param or
8835 DW_TAG_value_type_param. If the latter, add them to the name
8836 here. If the name already has template parameters, then
8837 skip this step; some versions of GCC emit both, and
8838 it is more efficient to use the pre-computed name.
8839
8840 Something to keep in mind about this process: it is very
8841 unlikely, or in some cases downright impossible, to produce
8842 something that will match the mangled name of a function.
8843 If the definition of the function has the same debug info,
8844 we should be able to match up with it anyway. But fallbacks
8845 using the minimal symbol, for instance to find a method
8846 implemented in a stripped copy of libstdc++, will not work.
8847 If we do not have debug info for the definition, we will have to
8848 match them up some other way.
8849
8850 When we do name matching there is a related problem with function
8851 templates; two instantiated function templates are allowed to
8852 differ only by their return types, which we do not add here. */
8853
8854 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8855 {
8856 struct attribute *attr;
8857 struct die_info *child;
8858 int first = 1;
8859
8860 die->building_fullname = 1;
8861
8862 for (child = die->child; child != NULL; child = child->sibling)
8863 {
8864 struct type *type;
12df843f 8865 LONGEST value;
d521ce57 8866 const gdb_byte *bytes;
98bfdba5
PA
8867 struct dwarf2_locexpr_baton *baton;
8868 struct value *v;
8869
8870 if (child->tag != DW_TAG_template_type_param
8871 && child->tag != DW_TAG_template_value_param)
8872 continue;
8873
8874 if (first)
8875 {
d7e74731 8876 buf.puts ("<");
98bfdba5
PA
8877 first = 0;
8878 }
8879 else
d7e74731 8880 buf.puts (", ");
98bfdba5
PA
8881
8882 attr = dwarf2_attr (child, DW_AT_type, cu);
8883 if (attr == NULL)
8884 {
8885 complaint (&symfile_complaints,
8886 _("template parameter missing DW_AT_type"));
d7e74731 8887 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8888 continue;
8889 }
8890 type = die_type (child, cu);
8891
8892 if (child->tag == DW_TAG_template_type_param)
8893 {
d7e74731 8894 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8895 continue;
8896 }
8897
8898 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8899 if (attr == NULL)
8900 {
8901 complaint (&symfile_complaints,
3e43a32a
MS
8902 _("template parameter missing "
8903 "DW_AT_const_value"));
d7e74731 8904 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8905 continue;
8906 }
8907
8908 dwarf2_const_value_attr (attr, type, name,
8909 &cu->comp_unit_obstack, cu,
8910 &value, &bytes, &baton);
8911
8912 if (TYPE_NOSIGN (type))
8913 /* GDB prints characters as NUMBER 'CHAR'. If that's
8914 changed, this can use value_print instead. */
d7e74731 8915 c_printchar (value, type, &buf);
98bfdba5
PA
8916 else
8917 {
8918 struct value_print_options opts;
8919
8920 if (baton != NULL)
8921 v = dwarf2_evaluate_loc_desc (type, NULL,
8922 baton->data,
8923 baton->size,
8924 baton->per_cu);
8925 else if (bytes != NULL)
8926 {
8927 v = allocate_value (type);
8928 memcpy (value_contents_writeable (v), bytes,
8929 TYPE_LENGTH (type));
8930 }
8931 else
8932 v = value_from_longest (type, value);
8933
3e43a32a
MS
8934 /* Specify decimal so that we do not depend on
8935 the radix. */
98bfdba5
PA
8936 get_formatted_print_options (&opts, 'd');
8937 opts.raw = 1;
d7e74731 8938 value_print (v, &buf, &opts);
98bfdba5
PA
8939 release_value (v);
8940 value_free (v);
8941 }
8942 }
8943
8944 die->building_fullname = 0;
8945
8946 if (!first)
8947 {
8948 /* Close the argument list, with a space if necessary
8949 (nested templates). */
d7e74731
PA
8950 if (!buf.empty () && buf.string ().back () == '>')
8951 buf.puts (" >");
98bfdba5 8952 else
d7e74731 8953 buf.puts (">");
98bfdba5
PA
8954 }
8955 }
8956
9c37b5ae 8957 /* For C++ methods, append formal parameter type
94af9270 8958 information, if PHYSNAME. */
6e70227d 8959
94af9270 8960 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8961 && cu->language == language_cplus)
94af9270
KS
8962 {
8963 struct type *type = read_type_die (die, cu);
8964
d7e74731 8965 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8966 &type_print_raw_options);
94af9270 8967
9c37b5ae 8968 if (cu->language == language_cplus)
94af9270 8969 {
60430eff
DJ
8970 /* Assume that an artificial first parameter is
8971 "this", but do not crash if it is not. RealView
8972 marks unnamed (and thus unused) parameters as
8973 artificial; there is no way to differentiate
8974 the two cases. */
94af9270
KS
8975 if (TYPE_NFIELDS (type) > 0
8976 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8977 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8978 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8979 0))))
d7e74731 8980 buf.puts (" const");
94af9270
KS
8981 }
8982 }
8983
d7e74731 8984 const std::string &intermediate_name = buf.string ();
94af9270
KS
8985
8986 if (cu->language == language_cplus)
34a68019 8987 canonical_name
322a8516 8988 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8989 &objfile->per_bfd->storage_obstack);
8990
8991 /* If we only computed INTERMEDIATE_NAME, or if
8992 INTERMEDIATE_NAME is already canonical, then we need to
8993 copy it to the appropriate obstack. */
322a8516 8994 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8995 name = ((const char *)
8996 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8997 intermediate_name.c_str (),
8998 intermediate_name.length ()));
34a68019
TT
8999 else
9000 name = canonical_name;
94af9270
KS
9001 }
9002 }
9003
9004 return name;
9005}
9006
0114d602
DJ
9007/* Return the fully qualified name of DIE, based on its DW_AT_name.
9008 If scope qualifiers are appropriate they will be added. The result
34a68019 9009 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9010 not have a name. NAME may either be from a previous call to
9011 dwarf2_name or NULL.
9012
9c37b5ae 9013 The output string will be canonicalized (if C++). */
0114d602
DJ
9014
9015static const char *
15d034d0 9016dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9017{
94af9270
KS
9018 return dwarf2_compute_name (name, die, cu, 0);
9019}
0114d602 9020
94af9270
KS
9021/* Construct a physname for the given DIE in CU. NAME may either be
9022 from a previous call to dwarf2_name or NULL. The result will be
9023 allocated on the objfile_objstack or NULL if the DIE does not have a
9024 name.
0114d602 9025
9c37b5ae 9026 The output string will be canonicalized (if C++). */
0114d602 9027
94af9270 9028static const char *
15d034d0 9029dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9030{
bb5ed363 9031 struct objfile *objfile = cu->objfile;
900e11f9
JK
9032 const char *retval, *mangled = NULL, *canon = NULL;
9033 struct cleanup *back_to;
9034 int need_copy = 1;
9035
9036 /* In this case dwarf2_compute_name is just a shortcut not building anything
9037 on its own. */
9038 if (!die_needs_namespace (die, cu))
9039 return dwarf2_compute_name (name, die, cu, 1);
9040
9041 back_to = make_cleanup (null_cleanup, NULL);
9042
73b9be8b 9043 mangled = dw2_linkage_name (die, cu);
900e11f9 9044
e98c9e7c
TT
9045 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9046 See https://github.com/rust-lang/rust/issues/32925. */
9047 if (cu->language == language_rust && mangled != NULL
9048 && strchr (mangled, '{') != NULL)
9049 mangled = NULL;
9050
900e11f9
JK
9051 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9052 has computed. */
7d45c7c3 9053 if (mangled != NULL)
900e11f9
JK
9054 {
9055 char *demangled;
9056
900e11f9
JK
9057 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9058 type. It is easier for GDB users to search for such functions as
9059 `name(params)' than `long name(params)'. In such case the minimal
9060 symbol names do not match the full symbol names but for template
9061 functions there is never a need to look up their definition from their
9062 declaration so the only disadvantage remains the minimal symbol
9063 variant `long name(params)' does not have the proper inferior type.
9064 */
9065
a766d390
DE
9066 if (cu->language == language_go)
9067 {
9068 /* This is a lie, but we already lie to the caller new_symbol_full.
9069 new_symbol_full assumes we return the mangled name.
9070 This just undoes that lie until things are cleaned up. */
9071 demangled = NULL;
9072 }
9073 else
9074 {
8de20a37 9075 demangled = gdb_demangle (mangled,
9c37b5ae 9076 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 9077 }
900e11f9
JK
9078 if (demangled)
9079 {
9080 make_cleanup (xfree, demangled);
9081 canon = demangled;
9082 }
9083 else
9084 {
9085 canon = mangled;
9086 need_copy = 0;
9087 }
9088 }
9089
9090 if (canon == NULL || check_physname)
9091 {
9092 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9093
9094 if (canon != NULL && strcmp (physname, canon) != 0)
9095 {
9096 /* It may not mean a bug in GDB. The compiler could also
9097 compute DW_AT_linkage_name incorrectly. But in such case
9098 GDB would need to be bug-to-bug compatible. */
9099
9100 complaint (&symfile_complaints,
9101 _("Computed physname <%s> does not match demangled <%s> "
9102 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9103 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9104 objfile_name (objfile));
900e11f9
JK
9105
9106 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9107 is available here - over computed PHYSNAME. It is safer
9108 against both buggy GDB and buggy compilers. */
9109
9110 retval = canon;
9111 }
9112 else
9113 {
9114 retval = physname;
9115 need_copy = 0;
9116 }
9117 }
9118 else
9119 retval = canon;
9120
9121 if (need_copy)
224c3ddb
SM
9122 retval = ((const char *)
9123 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9124 retval, strlen (retval)));
900e11f9
JK
9125
9126 do_cleanups (back_to);
9127 return retval;
0114d602
DJ
9128}
9129
74921315
KS
9130/* Inspect DIE in CU for a namespace alias. If one exists, record
9131 a new symbol for it.
9132
9133 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9134
9135static int
9136read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9137{
9138 struct attribute *attr;
9139
9140 /* If the die does not have a name, this is not a namespace
9141 alias. */
9142 attr = dwarf2_attr (die, DW_AT_name, cu);
9143 if (attr != NULL)
9144 {
9145 int num;
9146 struct die_info *d = die;
9147 struct dwarf2_cu *imported_cu = cu;
9148
9149 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9150 keep inspecting DIEs until we hit the underlying import. */
9151#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9152 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9153 {
9154 attr = dwarf2_attr (d, DW_AT_import, cu);
9155 if (attr == NULL)
9156 break;
9157
9158 d = follow_die_ref (d, attr, &imported_cu);
9159 if (d->tag != DW_TAG_imported_declaration)
9160 break;
9161 }
9162
9163 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9164 {
9165 complaint (&symfile_complaints,
9166 _("DIE at 0x%x has too many recursively imported "
9c541725 9167 "declarations"), to_underlying (d->sect_off));
74921315
KS
9168 return 0;
9169 }
9170
9171 if (attr != NULL)
9172 {
9173 struct type *type;
9c541725 9174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9175
9c541725 9176 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9178 {
9179 /* This declaration is a global namespace alias. Add
9180 a symbol for it whose type is the aliased namespace. */
9181 new_symbol (die, type, cu);
9182 return 1;
9183 }
9184 }
9185 }
9186
9187 return 0;
9188}
9189
22cee43f
PMR
9190/* Return the using directives repository (global or local?) to use in the
9191 current context for LANGUAGE.
9192
9193 For Ada, imported declarations can materialize renamings, which *may* be
9194 global. However it is impossible (for now?) in DWARF to distinguish
9195 "external" imported declarations and "static" ones. As all imported
9196 declarations seem to be static in all other languages, make them all CU-wide
9197 global only in Ada. */
9198
9199static struct using_direct **
9200using_directives (enum language language)
9201{
9202 if (language == language_ada && context_stack_depth == 0)
9203 return &global_using_directives;
9204 else
9205 return &local_using_directives;
9206}
9207
27aa8d6a
SW
9208/* Read the import statement specified by the given die and record it. */
9209
9210static void
9211read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9212{
bb5ed363 9213 struct objfile *objfile = cu->objfile;
27aa8d6a 9214 struct attribute *import_attr;
32019081 9215 struct die_info *imported_die, *child_die;
de4affc9 9216 struct dwarf2_cu *imported_cu;
27aa8d6a 9217 const char *imported_name;
794684b6 9218 const char *imported_name_prefix;
13387711
SW
9219 const char *canonical_name;
9220 const char *import_alias;
9221 const char *imported_declaration = NULL;
794684b6 9222 const char *import_prefix;
eb1e02fd 9223 std::vector<const char *> excludes;
13387711 9224
27aa8d6a
SW
9225 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9226 if (import_attr == NULL)
9227 {
9228 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9229 dwarf_tag_name (die->tag));
9230 return;
9231 }
9232
de4affc9
CC
9233 imported_cu = cu;
9234 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9235 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9236 if (imported_name == NULL)
9237 {
9238 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9239
9240 The import in the following code:
9241 namespace A
9242 {
9243 typedef int B;
9244 }
9245
9246 int main ()
9247 {
9248 using A::B;
9249 B b;
9250 return b;
9251 }
9252
9253 ...
9254 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9255 <52> DW_AT_decl_file : 1
9256 <53> DW_AT_decl_line : 6
9257 <54> DW_AT_import : <0x75>
9258 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9259 <59> DW_AT_name : B
9260 <5b> DW_AT_decl_file : 1
9261 <5c> DW_AT_decl_line : 2
9262 <5d> DW_AT_type : <0x6e>
9263 ...
9264 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9265 <76> DW_AT_byte_size : 4
9266 <77> DW_AT_encoding : 5 (signed)
9267
9268 imports the wrong die ( 0x75 instead of 0x58 ).
9269 This case will be ignored until the gcc bug is fixed. */
9270 return;
9271 }
9272
82856980
SW
9273 /* Figure out the local name after import. */
9274 import_alias = dwarf2_name (die, cu);
27aa8d6a 9275
794684b6
SW
9276 /* Figure out where the statement is being imported to. */
9277 import_prefix = determine_prefix (die, cu);
9278
9279 /* Figure out what the scope of the imported die is and prepend it
9280 to the name of the imported die. */
de4affc9 9281 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9282
f55ee35c
JK
9283 if (imported_die->tag != DW_TAG_namespace
9284 && imported_die->tag != DW_TAG_module)
794684b6 9285 {
13387711
SW
9286 imported_declaration = imported_name;
9287 canonical_name = imported_name_prefix;
794684b6 9288 }
13387711 9289 else if (strlen (imported_name_prefix) > 0)
12aaed36 9290 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9291 imported_name_prefix,
9292 (cu->language == language_d ? "." : "::"),
9293 imported_name, (char *) NULL);
13387711
SW
9294 else
9295 canonical_name = imported_name;
794684b6 9296
32019081
JK
9297 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9298 for (child_die = die->child; child_die && child_die->tag;
9299 child_die = sibling_die (child_die))
9300 {
9301 /* DWARF-4: A Fortran use statement with a “rename list” may be
9302 represented by an imported module entry with an import attribute
9303 referring to the module and owned entries corresponding to those
9304 entities that are renamed as part of being imported. */
9305
9306 if (child_die->tag != DW_TAG_imported_declaration)
9307 {
9308 complaint (&symfile_complaints,
9309 _("child DW_TAG_imported_declaration expected "
9310 "- DIE at 0x%x [in module %s]"),
9c541725 9311 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9312 continue;
9313 }
9314
9315 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9316 if (import_attr == NULL)
9317 {
9318 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9319 dwarf_tag_name (child_die->tag));
9320 continue;
9321 }
9322
9323 imported_cu = cu;
9324 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9325 &imported_cu);
9326 imported_name = dwarf2_name (imported_die, imported_cu);
9327 if (imported_name == NULL)
9328 {
9329 complaint (&symfile_complaints,
9330 _("child DW_TAG_imported_declaration has unknown "
9331 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9332 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9333 continue;
9334 }
9335
eb1e02fd 9336 excludes.push_back (imported_name);
32019081
JK
9337
9338 process_die (child_die, cu);
9339 }
9340
22cee43f
PMR
9341 add_using_directive (using_directives (cu->language),
9342 import_prefix,
9343 canonical_name,
9344 import_alias,
9345 imported_declaration,
9346 excludes,
9347 0,
9348 &objfile->objfile_obstack);
27aa8d6a
SW
9349}
9350
5230b05a
WT
9351/* ICC<14 does not output the required DW_AT_declaration on incomplete
9352 types, but gives them a size of zero. Starting with version 14,
9353 ICC is compatible with GCC. */
9354
9355static int
9356producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9357{
9358 if (!cu->checked_producer)
9359 check_producer (cu);
9360
9361 return cu->producer_is_icc_lt_14;
9362}
9363
1b80a9fa
JK
9364/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9365 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9366 this, it was first present in GCC release 4.3.0. */
9367
9368static int
9369producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9370{
9371 if (!cu->checked_producer)
9372 check_producer (cu);
9373
9374 return cu->producer_is_gcc_lt_4_3;
9375}
9376
d721ba37
PA
9377static file_and_directory
9378find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9379{
d721ba37
PA
9380 file_and_directory res;
9381
9291a0cd
TT
9382 /* Find the filename. Do not use dwarf2_name here, since the filename
9383 is not a source language identifier. */
d721ba37
PA
9384 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9385 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9386
d721ba37
PA
9387 if (res.comp_dir == NULL
9388 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9389 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9390 {
d721ba37
PA
9391 res.comp_dir_storage = ldirname (res.name);
9392 if (!res.comp_dir_storage.empty ())
9393 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9394 }
d721ba37 9395 if (res.comp_dir != NULL)
9291a0cd
TT
9396 {
9397 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9398 directory, get rid of it. */
d721ba37 9399 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9400
d721ba37
PA
9401 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9402 res.comp_dir = cp + 1;
9291a0cd
TT
9403 }
9404
d721ba37
PA
9405 if (res.name == NULL)
9406 res.name = "<unknown>";
9407
9408 return res;
9291a0cd
TT
9409}
9410
f4dc4d17
DE
9411/* Handle DW_AT_stmt_list for a compilation unit.
9412 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9413 COMP_DIR is the compilation directory. LOWPC is passed to
9414 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9415
9416static void
9417handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9418 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9419{
527f3840 9420 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9421 struct attribute *attr;
527f3840
JK
9422 struct line_header line_header_local;
9423 hashval_t line_header_local_hash;
9424 unsigned u;
9425 void **slot;
9426 int decode_mapping;
2ab95328 9427
f4dc4d17
DE
9428 gdb_assert (! cu->per_cu->is_debug_types);
9429
2ab95328 9430 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9431 if (attr == NULL)
9432 return;
9433
9c541725 9434 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9435
9436 /* The line header hash table is only created if needed (it exists to
9437 prevent redundant reading of the line table for partial_units).
9438 If we're given a partial_unit, we'll need it. If we're given a
9439 compile_unit, then use the line header hash table if it's already
9440 created, but don't create one just yet. */
9441
9442 if (dwarf2_per_objfile->line_header_hash == NULL
9443 && die->tag == DW_TAG_partial_unit)
2ab95328 9444 {
527f3840
JK
9445 dwarf2_per_objfile->line_header_hash
9446 = htab_create_alloc_ex (127, line_header_hash_voidp,
9447 line_header_eq_voidp,
9448 free_line_header_voidp,
9449 &objfile->objfile_obstack,
9450 hashtab_obstack_allocate,
9451 dummy_obstack_deallocate);
9452 }
2ab95328 9453
9c541725 9454 line_header_local.sect_off = line_offset;
527f3840
JK
9455 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9456 line_header_local_hash = line_header_hash (&line_header_local);
9457 if (dwarf2_per_objfile->line_header_hash != NULL)
9458 {
9459 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9460 &line_header_local,
9461 line_header_local_hash, NO_INSERT);
9462
9463 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9464 is not present in *SLOT (since if there is something in *SLOT then
9465 it will be for a partial_unit). */
9466 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9467 {
527f3840 9468 gdb_assert (*slot != NULL);
9a3c8263 9469 cu->line_header = (struct line_header *) *slot;
527f3840 9470 return;
dee91e82 9471 }
2ab95328 9472 }
527f3840
JK
9473
9474 /* dwarf_decode_line_header does not yet provide sufficient information.
9475 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9476 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9477 if (lh == NULL)
527f3840 9478 return;
4c8aa72d
PA
9479
9480 cu->line_header = lh.release ();
9481 cu->line_header_die_owner = die;
527f3840
JK
9482
9483 if (dwarf2_per_objfile->line_header_hash == NULL)
9484 slot = NULL;
9485 else
9486 {
9487 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9488 &line_header_local,
9489 line_header_local_hash, INSERT);
9490 gdb_assert (slot != NULL);
9491 }
9492 if (slot != NULL && *slot == NULL)
9493 {
9494 /* This newly decoded line number information unit will be owned
9495 by line_header_hash hash table. */
9496 *slot = cu->line_header;
4c8aa72d 9497 cu->line_header_die_owner = NULL;
527f3840
JK
9498 }
9499 else
9500 {
9501 /* We cannot free any current entry in (*slot) as that struct line_header
9502 may be already used by multiple CUs. Create only temporary decoded
9503 line_header for this CU - it may happen at most once for each line
9504 number information unit. And if we're not using line_header_hash
9505 then this is what we want as well. */
9506 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9507 }
9508 decode_mapping = (die->tag != DW_TAG_partial_unit);
9509 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9510 decode_mapping);
fff8551c 9511
2ab95328
TT
9512}
9513
95554aad 9514/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9515
c906108c 9516static void
e7c27a73 9517read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9518{
dee91e82 9519 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9521 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9522 CORE_ADDR highpc = ((CORE_ADDR) 0);
9523 struct attribute *attr;
c906108c 9524 struct die_info *child_die;
e142c38c 9525 CORE_ADDR baseaddr;
6e70227d 9526
e142c38c 9527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9528
fae299cd 9529 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9530
9531 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9532 from finish_block. */
2acceee2 9533 if (lowpc == ((CORE_ADDR) -1))
c906108c 9534 lowpc = highpc;
3e29f34a 9535 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9536
d721ba37 9537 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9538
95554aad 9539 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9540
f4b8a18d
KW
9541 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9542 standardised yet. As a workaround for the language detection we fall
9543 back to the DW_AT_producer string. */
9544 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9545 cu->language = language_opencl;
9546
3019eac3
DE
9547 /* Similar hack for Go. */
9548 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9549 set_cu_language (DW_LANG_Go, cu);
9550
d721ba37 9551 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9552
9553 /* Decode line number information if present. We do this before
9554 processing child DIEs, so that the line header table is available
9555 for DW_AT_decl_file. */
d721ba37 9556 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9557
9558 /* Process all dies in compilation unit. */
9559 if (die->child != NULL)
9560 {
9561 child_die = die->child;
9562 while (child_die && child_die->tag)
9563 {
9564 process_die (child_die, cu);
9565 child_die = sibling_die (child_die);
9566 }
9567 }
9568
9569 /* Decode macro information, if present. Dwarf 2 macro information
9570 refers to information in the line number info statement program
9571 header, so we can only read it if we've read the header
9572 successfully. */
0af92d60
JK
9573 attr = dwarf2_attr (die, DW_AT_macros, cu);
9574 if (attr == NULL)
9575 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9576 if (attr && cu->line_header)
9577 {
9578 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9579 complaint (&symfile_complaints,
0af92d60 9580 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9581
43f3e411 9582 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9583 }
9584 else
9585 {
9586 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9587 if (attr && cu->line_header)
9588 {
9589 unsigned int macro_offset = DW_UNSND (attr);
9590
43f3e411 9591 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9592 }
9593 }
3019eac3
DE
9594}
9595
f4dc4d17
DE
9596/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9597 Create the set of symtabs used by this TU, or if this TU is sharing
9598 symtabs with another TU and the symtabs have already been created
9599 then restore those symtabs in the line header.
9600 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9601
9602static void
f4dc4d17 9603setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9604{
f4dc4d17
DE
9605 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9606 struct type_unit_group *tu_group;
9607 int first_time;
3019eac3 9608 struct attribute *attr;
9c541725 9609 unsigned int i;
0186c6a7 9610 struct signatured_type *sig_type;
3019eac3 9611
f4dc4d17 9612 gdb_assert (per_cu->is_debug_types);
0186c6a7 9613 sig_type = (struct signatured_type *) per_cu;
3019eac3 9614
f4dc4d17 9615 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9616
f4dc4d17 9617 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9618 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9619 if (sig_type->type_unit_group == NULL)
9620 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9621 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9622
9623 /* If we've already processed this stmt_list there's no real need to
9624 do it again, we could fake it and just recreate the part we need
9625 (file name,index -> symtab mapping). If data shows this optimization
9626 is useful we can do it then. */
43f3e411 9627 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9628
9629 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9630 debug info. */
fff8551c 9631 line_header_up lh;
f4dc4d17 9632 if (attr != NULL)
3019eac3 9633 {
9c541725 9634 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9635 lh = dwarf_decode_line_header (line_offset, cu);
9636 }
9637 if (lh == NULL)
9638 {
9639 if (first_time)
9640 dwarf2_start_symtab (cu, "", NULL, 0);
9641 else
9642 {
9643 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9644 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9645 }
f4dc4d17 9646 return;
3019eac3
DE
9647 }
9648
4c8aa72d
PA
9649 cu->line_header = lh.release ();
9650 cu->line_header_die_owner = die;
3019eac3 9651
f4dc4d17
DE
9652 if (first_time)
9653 {
43f3e411 9654 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9655
1fd60fc0
DE
9656 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9657 still initializing it, and our caller (a few levels up)
9658 process_full_type_unit still needs to know if this is the first
9659 time. */
9660
4c8aa72d
PA
9661 tu_group->num_symtabs = cu->line_header->file_names.size ();
9662 tu_group->symtabs = XNEWVEC (struct symtab *,
9663 cu->line_header->file_names.size ());
3019eac3 9664
4c8aa72d 9665 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9666 {
4c8aa72d 9667 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9668
4c8aa72d 9669 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9670
f4dc4d17
DE
9671 if (current_subfile->symtab == NULL)
9672 {
4c8aa72d
PA
9673 /* NOTE: start_subfile will recognize when it's been
9674 passed a file it has already seen. So we can't
9675 assume there's a simple mapping from
9676 cu->line_header->file_names to subfiles, plus
9677 cu->line_header->file_names may contain dups. */
43f3e411
DE
9678 current_subfile->symtab
9679 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9680 }
9681
8c43009f
PA
9682 fe.symtab = current_subfile->symtab;
9683 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9684 }
9685 }
9686 else
3019eac3 9687 {
0ab9ce85 9688 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9689
4c8aa72d 9690 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9691 {
4c8aa72d 9692 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 9693
4c8aa72d 9694 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9695 }
3019eac3
DE
9696 }
9697
f4dc4d17
DE
9698 /* The main symtab is allocated last. Type units don't have DW_AT_name
9699 so they don't have a "real" (so to speak) symtab anyway.
9700 There is later code that will assign the main symtab to all symbols
9701 that don't have one. We need to handle the case of a symbol with a
9702 missing symtab (DW_AT_decl_file) anyway. */
9703}
3019eac3 9704
f4dc4d17
DE
9705/* Process DW_TAG_type_unit.
9706 For TUs we want to skip the first top level sibling if it's not the
9707 actual type being defined by this TU. In this case the first top
9708 level sibling is there to provide context only. */
3019eac3 9709
f4dc4d17
DE
9710static void
9711read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9712{
9713 struct die_info *child_die;
3019eac3 9714
f4dc4d17
DE
9715 prepare_one_comp_unit (cu, die, language_minimal);
9716
9717 /* Initialize (or reinitialize) the machinery for building symtabs.
9718 We do this before processing child DIEs, so that the line header table
9719 is available for DW_AT_decl_file. */
9720 setup_type_unit_groups (die, cu);
9721
9722 if (die->child != NULL)
9723 {
9724 child_die = die->child;
9725 while (child_die && child_die->tag)
9726 {
9727 process_die (child_die, cu);
9728 child_die = sibling_die (child_die);
9729 }
9730 }
3019eac3
DE
9731}
9732\f
80626a55
DE
9733/* DWO/DWP files.
9734
9735 http://gcc.gnu.org/wiki/DebugFission
9736 http://gcc.gnu.org/wiki/DebugFissionDWP
9737
9738 To simplify handling of both DWO files ("object" files with the DWARF info)
9739 and DWP files (a file with the DWOs packaged up into one file), we treat
9740 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9741
9742static hashval_t
9743hash_dwo_file (const void *item)
9744{
9a3c8263 9745 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9746 hashval_t hash;
3019eac3 9747
a2ce51a0
DE
9748 hash = htab_hash_string (dwo_file->dwo_name);
9749 if (dwo_file->comp_dir != NULL)
9750 hash += htab_hash_string (dwo_file->comp_dir);
9751 return hash;
3019eac3
DE
9752}
9753
9754static int
9755eq_dwo_file (const void *item_lhs, const void *item_rhs)
9756{
9a3c8263
SM
9757 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9758 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9759
a2ce51a0
DE
9760 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9761 return 0;
9762 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9763 return lhs->comp_dir == rhs->comp_dir;
9764 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9765}
9766
9767/* Allocate a hash table for DWO files. */
9768
9769static htab_t
9770allocate_dwo_file_hash_table (void)
9771{
9772 struct objfile *objfile = dwarf2_per_objfile->objfile;
9773
9774 return htab_create_alloc_ex (41,
9775 hash_dwo_file,
9776 eq_dwo_file,
9777 NULL,
9778 &objfile->objfile_obstack,
9779 hashtab_obstack_allocate,
9780 dummy_obstack_deallocate);
9781}
9782
80626a55
DE
9783/* Lookup DWO file DWO_NAME. */
9784
9785static void **
0ac5b59e 9786lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9787{
9788 struct dwo_file find_entry;
9789 void **slot;
9790
9791 if (dwarf2_per_objfile->dwo_files == NULL)
9792 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9793
9794 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9795 find_entry.dwo_name = dwo_name;
9796 find_entry.comp_dir = comp_dir;
80626a55
DE
9797 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9798
9799 return slot;
9800}
9801
3019eac3
DE
9802static hashval_t
9803hash_dwo_unit (const void *item)
9804{
9a3c8263 9805 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9806
9807 /* This drops the top 32 bits of the id, but is ok for a hash. */
9808 return dwo_unit->signature;
9809}
9810
9811static int
9812eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9813{
9a3c8263
SM
9814 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9815 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9816
9817 /* The signature is assumed to be unique within the DWO file.
9818 So while object file CU dwo_id's always have the value zero,
9819 that's OK, assuming each object file DWO file has only one CU,
9820 and that's the rule for now. */
9821 return lhs->signature == rhs->signature;
9822}
9823
9824/* Allocate a hash table for DWO CUs,TUs.
9825 There is one of these tables for each of CUs,TUs for each DWO file. */
9826
9827static htab_t
9828allocate_dwo_unit_table (struct objfile *objfile)
9829{
9830 /* Start out with a pretty small number.
9831 Generally DWO files contain only one CU and maybe some TUs. */
9832 return htab_create_alloc_ex (3,
9833 hash_dwo_unit,
9834 eq_dwo_unit,
9835 NULL,
9836 &objfile->objfile_obstack,
9837 hashtab_obstack_allocate,
9838 dummy_obstack_deallocate);
9839}
9840
80626a55 9841/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9842
19c3d4c9 9843struct create_dwo_cu_data
3019eac3
DE
9844{
9845 struct dwo_file *dwo_file;
19c3d4c9 9846 struct dwo_unit dwo_unit;
3019eac3
DE
9847};
9848
19c3d4c9 9849/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9850
9851static void
19c3d4c9
DE
9852create_dwo_cu_reader (const struct die_reader_specs *reader,
9853 const gdb_byte *info_ptr,
9854 struct die_info *comp_unit_die,
9855 int has_children,
9856 void *datap)
3019eac3
DE
9857{
9858 struct dwarf2_cu *cu = reader->cu;
9c541725 9859 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9860 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9861 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9862 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9863 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9864 struct attribute *attr;
3019eac3
DE
9865
9866 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9867 if (attr == NULL)
9868 {
19c3d4c9
DE
9869 complaint (&symfile_complaints,
9870 _("Dwarf Error: debug entry at offset 0x%x is missing"
9871 " its dwo_id [in module %s]"),
9c541725 9872 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9873 return;
9874 }
9875
3019eac3
DE
9876 dwo_unit->dwo_file = dwo_file;
9877 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9878 dwo_unit->section = section;
9c541725 9879 dwo_unit->sect_off = sect_off;
3019eac3
DE
9880 dwo_unit->length = cu->per_cu->length;
9881
b4f54984 9882 if (dwarf_read_debug)
4031ecc5 9883 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9884 to_underlying (sect_off),
9885 hex_string (dwo_unit->signature));
3019eac3
DE
9886}
9887
33c5cd75 9888/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9889 Note: This function processes DWO files only, not DWP files. */
3019eac3 9890
33c5cd75
DB
9891static void
9892create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9893 htab_t &cus_htab)
3019eac3
DE
9894{
9895 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9896 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9897 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9898
33c5cd75
DB
9899 dwarf2_read_section (objfile, &section);
9900 info_ptr = section.buffer;
3019eac3
DE
9901
9902 if (info_ptr == NULL)
33c5cd75 9903 return;
3019eac3 9904
b4f54984 9905 if (dwarf_read_debug)
19c3d4c9
DE
9906 {
9907 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9908 get_section_name (&section),
9909 get_section_file_name (&section));
19c3d4c9 9910 }
3019eac3 9911
33c5cd75 9912 end_ptr = info_ptr + section.size;
3019eac3
DE
9913 while (info_ptr < end_ptr)
9914 {
9915 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9916 struct create_dwo_cu_data create_dwo_cu_data;
9917 struct dwo_unit *dwo_unit;
9918 void **slot;
9919 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9920
19c3d4c9
DE
9921 memset (&create_dwo_cu_data.dwo_unit, 0,
9922 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9923 memset (&per_cu, 0, sizeof (per_cu));
9924 per_cu.objfile = objfile;
9925 per_cu.is_debug_types = 0;
33c5cd75
DB
9926 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9927 per_cu.section = &section;
c5ed0576 9928 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9929
9930 init_cutu_and_read_dies_no_follow (
9931 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9932 info_ptr += per_cu.length;
9933
9934 // If the unit could not be parsed, skip it.
9935 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9936 continue;
3019eac3 9937
33c5cd75
DB
9938 if (cus_htab == NULL)
9939 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 9940
33c5cd75
DB
9941 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9942 *dwo_unit = create_dwo_cu_data.dwo_unit;
9943 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9944 gdb_assert (slot != NULL);
9945 if (*slot != NULL)
19c3d4c9 9946 {
33c5cd75
DB
9947 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9948 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 9949
33c5cd75
DB
9950 complaint (&symfile_complaints,
9951 _("debug cu entry at offset 0x%x is duplicate to"
9952 " the entry at offset 0x%x, signature %s"),
9953 to_underlying (sect_off), to_underlying (dup_sect_off),
9954 hex_string (dwo_unit->signature));
19c3d4c9 9955 }
33c5cd75 9956 *slot = (void *)dwo_unit;
3019eac3 9957 }
3019eac3
DE
9958}
9959
80626a55
DE
9960/* DWP file .debug_{cu,tu}_index section format:
9961 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9962
d2415c6c
DE
9963 DWP Version 1:
9964
80626a55
DE
9965 Both index sections have the same format, and serve to map a 64-bit
9966 signature to a set of section numbers. Each section begins with a header,
9967 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9968 indexes, and a pool of 32-bit section numbers. The index sections will be
9969 aligned at 8-byte boundaries in the file.
9970
d2415c6c
DE
9971 The index section header consists of:
9972
9973 V, 32 bit version number
9974 -, 32 bits unused
9975 N, 32 bit number of compilation units or type units in the index
9976 M, 32 bit number of slots in the hash table
80626a55 9977
d2415c6c 9978 Numbers are recorded using the byte order of the application binary.
80626a55 9979
d2415c6c
DE
9980 The hash table begins at offset 16 in the section, and consists of an array
9981 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9982 order of the application binary). Unused slots in the hash table are 0.
9983 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9984
d2415c6c
DE
9985 The parallel table begins immediately after the hash table
9986 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9987 array of 32-bit indexes (using the byte order of the application binary),
9988 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9989 table contains a 32-bit index into the pool of section numbers. For unused
9990 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9991
73869dc2
DE
9992 The pool of section numbers begins immediately following the hash table
9993 (at offset 16 + 12 * M from the beginning of the section). The pool of
9994 section numbers consists of an array of 32-bit words (using the byte order
9995 of the application binary). Each item in the array is indexed starting
9996 from 0. The hash table entry provides the index of the first section
9997 number in the set. Additional section numbers in the set follow, and the
9998 set is terminated by a 0 entry (section number 0 is not used in ELF).
9999
10000 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10001 section must be the first entry in the set, and the .debug_abbrev.dwo must
10002 be the second entry. Other members of the set may follow in any order.
10003
10004 ---
10005
10006 DWP Version 2:
10007
10008 DWP Version 2 combines all the .debug_info, etc. sections into one,
10009 and the entries in the index tables are now offsets into these sections.
10010 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10011 section.
10012
10013 Index Section Contents:
10014 Header
10015 Hash Table of Signatures dwp_hash_table.hash_table
10016 Parallel Table of Indices dwp_hash_table.unit_table
10017 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10018 Table of Section Sizes dwp_hash_table.v2.sizes
10019
10020 The index section header consists of:
10021
10022 V, 32 bit version number
10023 L, 32 bit number of columns in the table of section offsets
10024 N, 32 bit number of compilation units or type units in the index
10025 M, 32 bit number of slots in the hash table
10026
10027 Numbers are recorded using the byte order of the application binary.
10028
10029 The hash table has the same format as version 1.
10030 The parallel table of indices has the same format as version 1,
10031 except that the entries are origin-1 indices into the table of sections
10032 offsets and the table of section sizes.
10033
10034 The table of offsets begins immediately following the parallel table
10035 (at offset 16 + 12 * M from the beginning of the section). The table is
10036 a two-dimensional array of 32-bit words (using the byte order of the
10037 application binary), with L columns and N+1 rows, in row-major order.
10038 Each row in the array is indexed starting from 0. The first row provides
10039 a key to the remaining rows: each column in this row provides an identifier
10040 for a debug section, and the offsets in the same column of subsequent rows
10041 refer to that section. The section identifiers are:
10042
10043 DW_SECT_INFO 1 .debug_info.dwo
10044 DW_SECT_TYPES 2 .debug_types.dwo
10045 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10046 DW_SECT_LINE 4 .debug_line.dwo
10047 DW_SECT_LOC 5 .debug_loc.dwo
10048 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10049 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10050 DW_SECT_MACRO 8 .debug_macro.dwo
10051
10052 The offsets provided by the CU and TU index sections are the base offsets
10053 for the contributions made by each CU or TU to the corresponding section
10054 in the package file. Each CU and TU header contains an abbrev_offset
10055 field, used to find the abbreviations table for that CU or TU within the
10056 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10057 be interpreted as relative to the base offset given in the index section.
10058 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10059 should be interpreted as relative to the base offset for .debug_line.dwo,
10060 and offsets into other debug sections obtained from DWARF attributes should
10061 also be interpreted as relative to the corresponding base offset.
10062
10063 The table of sizes begins immediately following the table of offsets.
10064 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10065 with L columns and N rows, in row-major order. Each row in the array is
10066 indexed starting from 1 (row 0 is shared by the two tables).
10067
10068 ---
10069
10070 Hash table lookup is handled the same in version 1 and 2:
10071
10072 We assume that N and M will not exceed 2^32 - 1.
10073 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10074
d2415c6c
DE
10075 Given a 64-bit compilation unit signature or a type signature S, an entry
10076 in the hash table is located as follows:
80626a55 10077
d2415c6c
DE
10078 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10079 the low-order k bits all set to 1.
80626a55 10080
d2415c6c 10081 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10082
d2415c6c
DE
10083 3) If the hash table entry at index H matches the signature, use that
10084 entry. If the hash table entry at index H is unused (all zeroes),
10085 terminate the search: the signature is not present in the table.
80626a55 10086
d2415c6c 10087 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10088
d2415c6c 10089 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10090 to stop at an unused slot or find the match. */
80626a55
DE
10091
10092/* Create a hash table to map DWO IDs to their CU/TU entry in
10093 .debug_{info,types}.dwo in DWP_FILE.
10094 Returns NULL if there isn't one.
10095 Note: This function processes DWP files only, not DWO files. */
10096
10097static struct dwp_hash_table *
10098create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10099{
10100 struct objfile *objfile = dwarf2_per_objfile->objfile;
10101 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10102 const gdb_byte *index_ptr, *index_end;
80626a55 10103 struct dwarf2_section_info *index;
73869dc2 10104 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10105 struct dwp_hash_table *htab;
10106
10107 if (is_debug_types)
10108 index = &dwp_file->sections.tu_index;
10109 else
10110 index = &dwp_file->sections.cu_index;
10111
10112 if (dwarf2_section_empty_p (index))
10113 return NULL;
10114 dwarf2_read_section (objfile, index);
10115
10116 index_ptr = index->buffer;
10117 index_end = index_ptr + index->size;
10118
10119 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10120 index_ptr += 4;
10121 if (version == 2)
10122 nr_columns = read_4_bytes (dbfd, index_ptr);
10123 else
10124 nr_columns = 0;
10125 index_ptr += 4;
80626a55
DE
10126 nr_units = read_4_bytes (dbfd, index_ptr);
10127 index_ptr += 4;
10128 nr_slots = read_4_bytes (dbfd, index_ptr);
10129 index_ptr += 4;
10130
73869dc2 10131 if (version != 1 && version != 2)
80626a55 10132 {
21aa081e 10133 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10134 " [in module %s]"),
21aa081e 10135 pulongest (version), dwp_file->name);
80626a55
DE
10136 }
10137 if (nr_slots != (nr_slots & -nr_slots))
10138 {
21aa081e 10139 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10140 " is not power of 2 [in module %s]"),
21aa081e 10141 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10142 }
10143
10144 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10145 htab->version = version;
10146 htab->nr_columns = nr_columns;
80626a55
DE
10147 htab->nr_units = nr_units;
10148 htab->nr_slots = nr_slots;
10149 htab->hash_table = index_ptr;
10150 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10151
10152 /* Exit early if the table is empty. */
10153 if (nr_slots == 0 || nr_units == 0
10154 || (version == 2 && nr_columns == 0))
10155 {
10156 /* All must be zero. */
10157 if (nr_slots != 0 || nr_units != 0
10158 || (version == 2 && nr_columns != 0))
10159 {
10160 complaint (&symfile_complaints,
10161 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10162 " all zero [in modules %s]"),
10163 dwp_file->name);
10164 }
10165 return htab;
10166 }
10167
10168 if (version == 1)
10169 {
10170 htab->section_pool.v1.indices =
10171 htab->unit_table + sizeof (uint32_t) * nr_slots;
10172 /* It's harder to decide whether the section is too small in v1.
10173 V1 is deprecated anyway so we punt. */
10174 }
10175 else
10176 {
10177 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10178 int *ids = htab->section_pool.v2.section_ids;
10179 /* Reverse map for error checking. */
10180 int ids_seen[DW_SECT_MAX + 1];
10181 int i;
10182
10183 if (nr_columns < 2)
10184 {
10185 error (_("Dwarf Error: bad DWP hash table, too few columns"
10186 " in section table [in module %s]"),
10187 dwp_file->name);
10188 }
10189 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10190 {
10191 error (_("Dwarf Error: bad DWP hash table, too many columns"
10192 " in section table [in module %s]"),
10193 dwp_file->name);
10194 }
10195 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10196 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10197 for (i = 0; i < nr_columns; ++i)
10198 {
10199 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10200
10201 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10202 {
10203 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10204 " in section table [in module %s]"),
10205 id, dwp_file->name);
10206 }
10207 if (ids_seen[id] != -1)
10208 {
10209 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10210 " id %d in section table [in module %s]"),
10211 id, dwp_file->name);
10212 }
10213 ids_seen[id] = i;
10214 ids[i] = id;
10215 }
10216 /* Must have exactly one info or types section. */
10217 if (((ids_seen[DW_SECT_INFO] != -1)
10218 + (ids_seen[DW_SECT_TYPES] != -1))
10219 != 1)
10220 {
10221 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10222 " DWO info/types section [in module %s]"),
10223 dwp_file->name);
10224 }
10225 /* Must have an abbrev section. */
10226 if (ids_seen[DW_SECT_ABBREV] == -1)
10227 {
10228 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10229 " section [in module %s]"),
10230 dwp_file->name);
10231 }
10232 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10233 htab->section_pool.v2.sizes =
10234 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10235 * nr_units * nr_columns);
10236 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10237 * nr_units * nr_columns))
10238 > index_end)
10239 {
10240 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10241 " [in module %s]"),
10242 dwp_file->name);
10243 }
10244 }
80626a55
DE
10245
10246 return htab;
10247}
10248
10249/* Update SECTIONS with the data from SECTP.
10250
10251 This function is like the other "locate" section routines that are
10252 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10253 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10254
10255 The result is non-zero for success, or zero if an error was found. */
10256
10257static int
73869dc2
DE
10258locate_v1_virtual_dwo_sections (asection *sectp,
10259 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10260{
10261 const struct dwop_section_names *names = &dwop_section_names;
10262
10263 if (section_is_p (sectp->name, &names->abbrev_dwo))
10264 {
10265 /* There can be only one. */
049412e3 10266 if (sections->abbrev.s.section != NULL)
80626a55 10267 return 0;
049412e3 10268 sections->abbrev.s.section = sectp;
80626a55
DE
10269 sections->abbrev.size = bfd_get_section_size (sectp);
10270 }
10271 else if (section_is_p (sectp->name, &names->info_dwo)
10272 || section_is_p (sectp->name, &names->types_dwo))
10273 {
10274 /* There can be only one. */
049412e3 10275 if (sections->info_or_types.s.section != NULL)
80626a55 10276 return 0;
049412e3 10277 sections->info_or_types.s.section = sectp;
80626a55
DE
10278 sections->info_or_types.size = bfd_get_section_size (sectp);
10279 }
10280 else if (section_is_p (sectp->name, &names->line_dwo))
10281 {
10282 /* There can be only one. */
049412e3 10283 if (sections->line.s.section != NULL)
80626a55 10284 return 0;
049412e3 10285 sections->line.s.section = sectp;
80626a55
DE
10286 sections->line.size = bfd_get_section_size (sectp);
10287 }
10288 else if (section_is_p (sectp->name, &names->loc_dwo))
10289 {
10290 /* There can be only one. */
049412e3 10291 if (sections->loc.s.section != NULL)
80626a55 10292 return 0;
049412e3 10293 sections->loc.s.section = sectp;
80626a55
DE
10294 sections->loc.size = bfd_get_section_size (sectp);
10295 }
10296 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10297 {
10298 /* There can be only one. */
049412e3 10299 if (sections->macinfo.s.section != NULL)
80626a55 10300 return 0;
049412e3 10301 sections->macinfo.s.section = sectp;
80626a55
DE
10302 sections->macinfo.size = bfd_get_section_size (sectp);
10303 }
10304 else if (section_is_p (sectp->name, &names->macro_dwo))
10305 {
10306 /* There can be only one. */
049412e3 10307 if (sections->macro.s.section != NULL)
80626a55 10308 return 0;
049412e3 10309 sections->macro.s.section = sectp;
80626a55
DE
10310 sections->macro.size = bfd_get_section_size (sectp);
10311 }
10312 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10313 {
10314 /* There can be only one. */
049412e3 10315 if (sections->str_offsets.s.section != NULL)
80626a55 10316 return 0;
049412e3 10317 sections->str_offsets.s.section = sectp;
80626a55
DE
10318 sections->str_offsets.size = bfd_get_section_size (sectp);
10319 }
10320 else
10321 {
10322 /* No other kind of section is valid. */
10323 return 0;
10324 }
10325
10326 return 1;
10327}
10328
73869dc2
DE
10329/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10330 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10331 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10332 This is for DWP version 1 files. */
80626a55
DE
10333
10334static struct dwo_unit *
73869dc2
DE
10335create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10336 uint32_t unit_index,
10337 const char *comp_dir,
10338 ULONGEST signature, int is_debug_types)
80626a55
DE
10339{
10340 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10341 const struct dwp_hash_table *dwp_htab =
10342 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10343 bfd *dbfd = dwp_file->dbfd;
10344 const char *kind = is_debug_types ? "TU" : "CU";
10345 struct dwo_file *dwo_file;
10346 struct dwo_unit *dwo_unit;
73869dc2 10347 struct virtual_v1_dwo_sections sections;
80626a55
DE
10348 void **dwo_file_slot;
10349 char *virtual_dwo_name;
80626a55
DE
10350 struct cleanup *cleanups;
10351 int i;
10352
73869dc2
DE
10353 gdb_assert (dwp_file->version == 1);
10354
b4f54984 10355 if (dwarf_read_debug)
80626a55 10356 {
73869dc2 10357 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10358 kind,
73869dc2 10359 pulongest (unit_index), hex_string (signature),
80626a55
DE
10360 dwp_file->name);
10361 }
10362
19ac8c2e 10363 /* Fetch the sections of this DWO unit.
80626a55
DE
10364 Put a limit on the number of sections we look for so that bad data
10365 doesn't cause us to loop forever. */
10366
73869dc2 10367#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10368 (1 /* .debug_info or .debug_types */ \
10369 + 1 /* .debug_abbrev */ \
10370 + 1 /* .debug_line */ \
10371 + 1 /* .debug_loc */ \
10372 + 1 /* .debug_str_offsets */ \
19ac8c2e 10373 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10374 + 1 /* trailing zero */)
10375
10376 memset (&sections, 0, sizeof (sections));
10377 cleanups = make_cleanup (null_cleanup, 0);
10378
73869dc2 10379 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10380 {
10381 asection *sectp;
10382 uint32_t section_nr =
10383 read_4_bytes (dbfd,
73869dc2
DE
10384 dwp_htab->section_pool.v1.indices
10385 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10386
10387 if (section_nr == 0)
10388 break;
10389 if (section_nr >= dwp_file->num_sections)
10390 {
10391 error (_("Dwarf Error: bad DWP hash table, section number too large"
10392 " [in module %s]"),
10393 dwp_file->name);
10394 }
10395
10396 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10397 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10398 {
10399 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10400 " [in module %s]"),
10401 dwp_file->name);
10402 }
10403 }
10404
10405 if (i < 2
a32a8923
DE
10406 || dwarf2_section_empty_p (&sections.info_or_types)
10407 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10408 {
10409 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10410 " [in module %s]"),
10411 dwp_file->name);
10412 }
73869dc2 10413 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10414 {
10415 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10416 " [in module %s]"),
10417 dwp_file->name);
10418 }
10419
10420 /* It's easier for the rest of the code if we fake a struct dwo_file and
10421 have dwo_unit "live" in that. At least for now.
10422
10423 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10424 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10425 file, we can combine them back into a virtual DWO file to save space
10426 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10427 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10428
2792b94d
PM
10429 virtual_dwo_name =
10430 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10431 get_section_id (&sections.abbrev),
10432 get_section_id (&sections.line),
10433 get_section_id (&sections.loc),
10434 get_section_id (&sections.str_offsets));
80626a55
DE
10435 make_cleanup (xfree, virtual_dwo_name);
10436 /* Can we use an existing virtual DWO file? */
0ac5b59e 10437 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10438 /* Create one if necessary. */
10439 if (*dwo_file_slot == NULL)
10440 {
b4f54984 10441 if (dwarf_read_debug)
80626a55
DE
10442 {
10443 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10444 virtual_dwo_name);
10445 }
10446 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10447 dwo_file->dwo_name
10448 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10449 virtual_dwo_name,
10450 strlen (virtual_dwo_name));
0ac5b59e 10451 dwo_file->comp_dir = comp_dir;
80626a55
DE
10452 dwo_file->sections.abbrev = sections.abbrev;
10453 dwo_file->sections.line = sections.line;
10454 dwo_file->sections.loc = sections.loc;
10455 dwo_file->sections.macinfo = sections.macinfo;
10456 dwo_file->sections.macro = sections.macro;
10457 dwo_file->sections.str_offsets = sections.str_offsets;
10458 /* The "str" section is global to the entire DWP file. */
10459 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10460 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10461 there's no need to record it in dwo_file.
10462 Also, we can't simply record type sections in dwo_file because
10463 we record a pointer into the vector in dwo_unit. As we collect more
10464 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10465 for it, invalidating all copies of pointers into the previous
10466 contents. */
80626a55
DE
10467 *dwo_file_slot = dwo_file;
10468 }
10469 else
10470 {
b4f54984 10471 if (dwarf_read_debug)
80626a55
DE
10472 {
10473 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10474 virtual_dwo_name);
10475 }
9a3c8263 10476 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10477 }
10478 do_cleanups (cleanups);
10479
10480 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10481 dwo_unit->dwo_file = dwo_file;
10482 dwo_unit->signature = signature;
8d749320
SM
10483 dwo_unit->section =
10484 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10485 *dwo_unit->section = sections.info_or_types;
57d63ce2 10486 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10487
10488 return dwo_unit;
10489}
10490
73869dc2
DE
10491/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10492 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10493 piece within that section used by a TU/CU, return a virtual section
10494 of just that piece. */
10495
10496static struct dwarf2_section_info
10497create_dwp_v2_section (struct dwarf2_section_info *section,
10498 bfd_size_type offset, bfd_size_type size)
10499{
10500 struct dwarf2_section_info result;
10501 asection *sectp;
10502
10503 gdb_assert (section != NULL);
10504 gdb_assert (!section->is_virtual);
10505
10506 memset (&result, 0, sizeof (result));
10507 result.s.containing_section = section;
10508 result.is_virtual = 1;
10509
10510 if (size == 0)
10511 return result;
10512
10513 sectp = get_section_bfd_section (section);
10514
10515 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10516 bounds of the real section. This is a pretty-rare event, so just
10517 flag an error (easier) instead of a warning and trying to cope. */
10518 if (sectp == NULL
10519 || offset + size > bfd_get_section_size (sectp))
10520 {
10521 bfd *abfd = sectp->owner;
10522
10523 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10524 " in section %s [in module %s]"),
10525 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10526 objfile_name (dwarf2_per_objfile->objfile));
10527 }
10528
10529 result.virtual_offset = offset;
10530 result.size = size;
10531 return result;
10532}
10533
10534/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10535 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10536 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10537 This is for DWP version 2 files. */
10538
10539static struct dwo_unit *
10540create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10541 uint32_t unit_index,
10542 const char *comp_dir,
10543 ULONGEST signature, int is_debug_types)
10544{
10545 struct objfile *objfile = dwarf2_per_objfile->objfile;
10546 const struct dwp_hash_table *dwp_htab =
10547 is_debug_types ? dwp_file->tus : dwp_file->cus;
10548 bfd *dbfd = dwp_file->dbfd;
10549 const char *kind = is_debug_types ? "TU" : "CU";
10550 struct dwo_file *dwo_file;
10551 struct dwo_unit *dwo_unit;
10552 struct virtual_v2_dwo_sections sections;
10553 void **dwo_file_slot;
10554 char *virtual_dwo_name;
73869dc2
DE
10555 struct cleanup *cleanups;
10556 int i;
10557
10558 gdb_assert (dwp_file->version == 2);
10559
b4f54984 10560 if (dwarf_read_debug)
73869dc2
DE
10561 {
10562 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10563 kind,
10564 pulongest (unit_index), hex_string (signature),
10565 dwp_file->name);
10566 }
10567
10568 /* Fetch the section offsets of this DWO unit. */
10569
10570 memset (&sections, 0, sizeof (sections));
10571 cleanups = make_cleanup (null_cleanup, 0);
10572
10573 for (i = 0; i < dwp_htab->nr_columns; ++i)
10574 {
10575 uint32_t offset = read_4_bytes (dbfd,
10576 dwp_htab->section_pool.v2.offsets
10577 + (((unit_index - 1) * dwp_htab->nr_columns
10578 + i)
10579 * sizeof (uint32_t)));
10580 uint32_t size = read_4_bytes (dbfd,
10581 dwp_htab->section_pool.v2.sizes
10582 + (((unit_index - 1) * dwp_htab->nr_columns
10583 + i)
10584 * sizeof (uint32_t)));
10585
10586 switch (dwp_htab->section_pool.v2.section_ids[i])
10587 {
10588 case DW_SECT_INFO:
10589 case DW_SECT_TYPES:
10590 sections.info_or_types_offset = offset;
10591 sections.info_or_types_size = size;
10592 break;
10593 case DW_SECT_ABBREV:
10594 sections.abbrev_offset = offset;
10595 sections.abbrev_size = size;
10596 break;
10597 case DW_SECT_LINE:
10598 sections.line_offset = offset;
10599 sections.line_size = size;
10600 break;
10601 case DW_SECT_LOC:
10602 sections.loc_offset = offset;
10603 sections.loc_size = size;
10604 break;
10605 case DW_SECT_STR_OFFSETS:
10606 sections.str_offsets_offset = offset;
10607 sections.str_offsets_size = size;
10608 break;
10609 case DW_SECT_MACINFO:
10610 sections.macinfo_offset = offset;
10611 sections.macinfo_size = size;
10612 break;
10613 case DW_SECT_MACRO:
10614 sections.macro_offset = offset;
10615 sections.macro_size = size;
10616 break;
10617 }
10618 }
10619
10620 /* It's easier for the rest of the code if we fake a struct dwo_file and
10621 have dwo_unit "live" in that. At least for now.
10622
10623 The DWP file can be made up of a random collection of CUs and TUs.
10624 However, for each CU + set of TUs that came from the same original DWO
10625 file, we can combine them back into a virtual DWO file to save space
10626 (fewer struct dwo_file objects to allocate). Remember that for really
10627 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10628
10629 virtual_dwo_name =
10630 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10631 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10632 (long) (sections.line_size ? sections.line_offset : 0),
10633 (long) (sections.loc_size ? sections.loc_offset : 0),
10634 (long) (sections.str_offsets_size
10635 ? sections.str_offsets_offset : 0));
10636 make_cleanup (xfree, virtual_dwo_name);
10637 /* Can we use an existing virtual DWO file? */
10638 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10639 /* Create one if necessary. */
10640 if (*dwo_file_slot == NULL)
10641 {
b4f54984 10642 if (dwarf_read_debug)
73869dc2
DE
10643 {
10644 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10645 virtual_dwo_name);
10646 }
10647 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10648 dwo_file->dwo_name
10649 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10650 virtual_dwo_name,
10651 strlen (virtual_dwo_name));
73869dc2
DE
10652 dwo_file->comp_dir = comp_dir;
10653 dwo_file->sections.abbrev =
10654 create_dwp_v2_section (&dwp_file->sections.abbrev,
10655 sections.abbrev_offset, sections.abbrev_size);
10656 dwo_file->sections.line =
10657 create_dwp_v2_section (&dwp_file->sections.line,
10658 sections.line_offset, sections.line_size);
10659 dwo_file->sections.loc =
10660 create_dwp_v2_section (&dwp_file->sections.loc,
10661 sections.loc_offset, sections.loc_size);
10662 dwo_file->sections.macinfo =
10663 create_dwp_v2_section (&dwp_file->sections.macinfo,
10664 sections.macinfo_offset, sections.macinfo_size);
10665 dwo_file->sections.macro =
10666 create_dwp_v2_section (&dwp_file->sections.macro,
10667 sections.macro_offset, sections.macro_size);
10668 dwo_file->sections.str_offsets =
10669 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10670 sections.str_offsets_offset,
10671 sections.str_offsets_size);
10672 /* The "str" section is global to the entire DWP file. */
10673 dwo_file->sections.str = dwp_file->sections.str;
10674 /* The info or types section is assigned below to dwo_unit,
10675 there's no need to record it in dwo_file.
10676 Also, we can't simply record type sections in dwo_file because
10677 we record a pointer into the vector in dwo_unit. As we collect more
10678 types we'll grow the vector and eventually have to reallocate space
10679 for it, invalidating all copies of pointers into the previous
10680 contents. */
10681 *dwo_file_slot = dwo_file;
10682 }
10683 else
10684 {
b4f54984 10685 if (dwarf_read_debug)
73869dc2
DE
10686 {
10687 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10688 virtual_dwo_name);
10689 }
9a3c8263 10690 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10691 }
10692 do_cleanups (cleanups);
10693
10694 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10695 dwo_unit->dwo_file = dwo_file;
10696 dwo_unit->signature = signature;
8d749320
SM
10697 dwo_unit->section =
10698 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10699 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10700 ? &dwp_file->sections.types
10701 : &dwp_file->sections.info,
10702 sections.info_or_types_offset,
10703 sections.info_or_types_size);
10704 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10705
10706 return dwo_unit;
10707}
10708
57d63ce2
DE
10709/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10710 Returns NULL if the signature isn't found. */
80626a55
DE
10711
10712static struct dwo_unit *
57d63ce2
DE
10713lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10714 ULONGEST signature, int is_debug_types)
80626a55 10715{
57d63ce2
DE
10716 const struct dwp_hash_table *dwp_htab =
10717 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10718 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10719 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10720 uint32_t hash = signature & mask;
10721 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10722 unsigned int i;
10723 void **slot;
870f88f7 10724 struct dwo_unit find_dwo_cu;
80626a55
DE
10725
10726 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10727 find_dwo_cu.signature = signature;
19ac8c2e
DE
10728 slot = htab_find_slot (is_debug_types
10729 ? dwp_file->loaded_tus
10730 : dwp_file->loaded_cus,
10731 &find_dwo_cu, INSERT);
80626a55
DE
10732
10733 if (*slot != NULL)
9a3c8263 10734 return (struct dwo_unit *) *slot;
80626a55
DE
10735
10736 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10737 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10738 {
10739 ULONGEST signature_in_table;
10740
10741 signature_in_table =
57d63ce2 10742 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10743 if (signature_in_table == signature)
10744 {
57d63ce2
DE
10745 uint32_t unit_index =
10746 read_4_bytes (dbfd,
10747 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10748
73869dc2
DE
10749 if (dwp_file->version == 1)
10750 {
10751 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10752 comp_dir, signature,
10753 is_debug_types);
10754 }
10755 else
10756 {
10757 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10758 comp_dir, signature,
10759 is_debug_types);
10760 }
9a3c8263 10761 return (struct dwo_unit *) *slot;
80626a55
DE
10762 }
10763 if (signature_in_table == 0)
10764 return NULL;
10765 hash = (hash + hash2) & mask;
10766 }
10767
10768 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10769 " [in module %s]"),
10770 dwp_file->name);
10771}
10772
ab5088bf 10773/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10774 Open the file specified by FILE_NAME and hand it off to BFD for
10775 preliminary analysis. Return a newly initialized bfd *, which
10776 includes a canonicalized copy of FILE_NAME.
80626a55 10777 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10778 SEARCH_CWD is true if the current directory is to be searched.
10779 It will be searched before debug-file-directory.
13aaf454
DE
10780 If successful, the file is added to the bfd include table of the
10781 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10782 If unable to find/open the file, return NULL.
3019eac3
DE
10783 NOTE: This function is derived from symfile_bfd_open. */
10784
192b62ce 10785static gdb_bfd_ref_ptr
6ac97d4c 10786try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10787{
80626a55 10788 int desc, flags;
3019eac3 10789 char *absolute_name;
9c02c129
DE
10790 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10791 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10792 to debug_file_directory. */
10793 char *search_path;
10794 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10795
6ac97d4c
DE
10796 if (search_cwd)
10797 {
10798 if (*debug_file_directory != '\0')
10799 search_path = concat (".", dirname_separator_string,
b36cec19 10800 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10801 else
10802 search_path = xstrdup (".");
10803 }
9c02c129 10804 else
6ac97d4c 10805 search_path = xstrdup (debug_file_directory);
3019eac3 10806
492c0ab7 10807 flags = OPF_RETURN_REALPATH;
80626a55
DE
10808 if (is_dwp)
10809 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10810 desc = openp (search_path, flags, file_name,
3019eac3 10811 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10812 xfree (search_path);
3019eac3
DE
10813 if (desc < 0)
10814 return NULL;
10815
192b62ce 10816 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10817 xfree (absolute_name);
9c02c129
DE
10818 if (sym_bfd == NULL)
10819 return NULL;
192b62ce 10820 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10821
192b62ce
TT
10822 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10823 return NULL;
3019eac3 10824
13aaf454
DE
10825 /* Success. Record the bfd as having been included by the objfile's bfd.
10826 This is important because things like demangled_names_hash lives in the
10827 objfile's per_bfd space and may have references to things like symbol
10828 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10829 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10830
3019eac3
DE
10831 return sym_bfd;
10832}
10833
ab5088bf 10834/* Try to open DWO file FILE_NAME.
3019eac3
DE
10835 COMP_DIR is the DW_AT_comp_dir attribute.
10836 The result is the bfd handle of the file.
10837 If there is a problem finding or opening the file, return NULL.
10838 Upon success, the canonicalized path of the file is stored in the bfd,
10839 same as symfile_bfd_open. */
10840
192b62ce 10841static gdb_bfd_ref_ptr
ab5088bf 10842open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10843{
80626a55 10844 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10845 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10846
10847 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10848
10849 if (comp_dir != NULL)
10850 {
b36cec19
PA
10851 char *path_to_try = concat (comp_dir, SLASH_STRING,
10852 file_name, (char *) NULL);
3019eac3
DE
10853
10854 /* NOTE: If comp_dir is a relative path, this will also try the
10855 search path, which seems useful. */
192b62ce
TT
10856 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10857 1 /*search_cwd*/));
3019eac3
DE
10858 xfree (path_to_try);
10859 if (abfd != NULL)
10860 return abfd;
10861 }
10862
10863 /* That didn't work, try debug-file-directory, which, despite its name,
10864 is a list of paths. */
10865
10866 if (*debug_file_directory == '\0')
10867 return NULL;
10868
6ac97d4c 10869 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10870}
10871
80626a55
DE
10872/* This function is mapped across the sections and remembers the offset and
10873 size of each of the DWO debugging sections we are interested in. */
10874
10875static void
10876dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10877{
9a3c8263 10878 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10879 const struct dwop_section_names *names = &dwop_section_names;
10880
10881 if (section_is_p (sectp->name, &names->abbrev_dwo))
10882 {
049412e3 10883 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10884 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10885 }
10886 else if (section_is_p (sectp->name, &names->info_dwo))
10887 {
049412e3 10888 dwo_sections->info.s.section = sectp;
80626a55
DE
10889 dwo_sections->info.size = bfd_get_section_size (sectp);
10890 }
10891 else if (section_is_p (sectp->name, &names->line_dwo))
10892 {
049412e3 10893 dwo_sections->line.s.section = sectp;
80626a55
DE
10894 dwo_sections->line.size = bfd_get_section_size (sectp);
10895 }
10896 else if (section_is_p (sectp->name, &names->loc_dwo))
10897 {
049412e3 10898 dwo_sections->loc.s.section = sectp;
80626a55
DE
10899 dwo_sections->loc.size = bfd_get_section_size (sectp);
10900 }
10901 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10902 {
049412e3 10903 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10904 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10905 }
10906 else if (section_is_p (sectp->name, &names->macro_dwo))
10907 {
049412e3 10908 dwo_sections->macro.s.section = sectp;
80626a55
DE
10909 dwo_sections->macro.size = bfd_get_section_size (sectp);
10910 }
10911 else if (section_is_p (sectp->name, &names->str_dwo))
10912 {
049412e3 10913 dwo_sections->str.s.section = sectp;
80626a55
DE
10914 dwo_sections->str.size = bfd_get_section_size (sectp);
10915 }
10916 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10917 {
049412e3 10918 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10919 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10920 }
10921 else if (section_is_p (sectp->name, &names->types_dwo))
10922 {
10923 struct dwarf2_section_info type_section;
10924
10925 memset (&type_section, 0, sizeof (type_section));
049412e3 10926 type_section.s.section = sectp;
80626a55
DE
10927 type_section.size = bfd_get_section_size (sectp);
10928 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10929 &type_section);
10930 }
10931}
10932
ab5088bf 10933/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10934 by PER_CU. This is for the non-DWP case.
80626a55 10935 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10936
10937static struct dwo_file *
0ac5b59e
DE
10938open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10939 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10940{
10941 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10942 struct dwo_file *dwo_file;
3019eac3
DE
10943 struct cleanup *cleanups;
10944
192b62ce 10945 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10946 if (dbfd == NULL)
10947 {
b4f54984 10948 if (dwarf_read_debug)
80626a55
DE
10949 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10950 return NULL;
10951 }
10952 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10953 dwo_file->dwo_name = dwo_name;
10954 dwo_file->comp_dir = comp_dir;
192b62ce 10955 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10956
10957 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10958
192b62ce
TT
10959 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10960 &dwo_file->sections);
3019eac3 10961
33c5cd75 10962 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 10963
78d4d2c5
JK
10964 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10965 dwo_file->tus);
3019eac3
DE
10966
10967 discard_cleanups (cleanups);
10968
b4f54984 10969 if (dwarf_read_debug)
80626a55
DE
10970 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10971
3019eac3
DE
10972 return dwo_file;
10973}
10974
80626a55 10975/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10976 size of each of the DWP debugging sections common to version 1 and 2 that
10977 we are interested in. */
3019eac3 10978
80626a55 10979static void
73869dc2
DE
10980dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10981 void *dwp_file_ptr)
3019eac3 10982{
9a3c8263 10983 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10984 const struct dwop_section_names *names = &dwop_section_names;
10985 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10986
80626a55 10987 /* Record the ELF section number for later lookup: this is what the
73869dc2 10988 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10989 gdb_assert (elf_section_nr < dwp_file->num_sections);
10990 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10991
80626a55
DE
10992 /* Look for specific sections that we need. */
10993 if (section_is_p (sectp->name, &names->str_dwo))
10994 {
049412e3 10995 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10996 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10997 }
10998 else if (section_is_p (sectp->name, &names->cu_index))
10999 {
049412e3 11000 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11001 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11002 }
11003 else if (section_is_p (sectp->name, &names->tu_index))
11004 {
049412e3 11005 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11006 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11007 }
11008}
3019eac3 11009
73869dc2
DE
11010/* This function is mapped across the sections and remembers the offset and
11011 size of each of the DWP version 2 debugging sections that we are interested
11012 in. This is split into a separate function because we don't know if we
11013 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11014
11015static void
11016dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11017{
9a3c8263 11018 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11019 const struct dwop_section_names *names = &dwop_section_names;
11020 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11021
11022 /* Record the ELF section number for later lookup: this is what the
11023 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11024 gdb_assert (elf_section_nr < dwp_file->num_sections);
11025 dwp_file->elf_sections[elf_section_nr] = sectp;
11026
11027 /* Look for specific sections that we need. */
11028 if (section_is_p (sectp->name, &names->abbrev_dwo))
11029 {
049412e3 11030 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11031 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11032 }
11033 else if (section_is_p (sectp->name, &names->info_dwo))
11034 {
049412e3 11035 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11036 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11037 }
11038 else if (section_is_p (sectp->name, &names->line_dwo))
11039 {
049412e3 11040 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11041 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11042 }
11043 else if (section_is_p (sectp->name, &names->loc_dwo))
11044 {
049412e3 11045 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11046 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11047 }
11048 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11049 {
049412e3 11050 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11051 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11052 }
11053 else if (section_is_p (sectp->name, &names->macro_dwo))
11054 {
049412e3 11055 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11056 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11057 }
11058 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11059 {
049412e3 11060 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11061 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11062 }
11063 else if (section_is_p (sectp->name, &names->types_dwo))
11064 {
049412e3 11065 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11066 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11067 }
11068}
11069
80626a55 11070/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11071
80626a55
DE
11072static hashval_t
11073hash_dwp_loaded_cutus (const void *item)
11074{
9a3c8263 11075 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11076
80626a55
DE
11077 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11078 return dwo_unit->signature;
3019eac3
DE
11079}
11080
80626a55 11081/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11082
80626a55
DE
11083static int
11084eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11085{
9a3c8263
SM
11086 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11087 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11088
80626a55
DE
11089 return dua->signature == dub->signature;
11090}
3019eac3 11091
80626a55 11092/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11093
80626a55
DE
11094static htab_t
11095allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11096{
11097 return htab_create_alloc_ex (3,
11098 hash_dwp_loaded_cutus,
11099 eq_dwp_loaded_cutus,
11100 NULL,
11101 &objfile->objfile_obstack,
11102 hashtab_obstack_allocate,
11103 dummy_obstack_deallocate);
11104}
3019eac3 11105
ab5088bf
DE
11106/* Try to open DWP file FILE_NAME.
11107 The result is the bfd handle of the file.
11108 If there is a problem finding or opening the file, return NULL.
11109 Upon success, the canonicalized path of the file is stored in the bfd,
11110 same as symfile_bfd_open. */
11111
192b62ce 11112static gdb_bfd_ref_ptr
ab5088bf
DE
11113open_dwp_file (const char *file_name)
11114{
192b62ce
TT
11115 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11116 1 /*search_cwd*/));
6ac97d4c
DE
11117 if (abfd != NULL)
11118 return abfd;
11119
11120 /* Work around upstream bug 15652.
11121 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11122 [Whether that's a "bug" is debatable, but it is getting in our way.]
11123 We have no real idea where the dwp file is, because gdb's realpath-ing
11124 of the executable's path may have discarded the needed info.
11125 [IWBN if the dwp file name was recorded in the executable, akin to
11126 .gnu_debuglink, but that doesn't exist yet.]
11127 Strip the directory from FILE_NAME and search again. */
11128 if (*debug_file_directory != '\0')
11129 {
11130 /* Don't implicitly search the current directory here.
11131 If the user wants to search "." to handle this case,
11132 it must be added to debug-file-directory. */
11133 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11134 0 /*search_cwd*/);
11135 }
11136
11137 return NULL;
ab5088bf
DE
11138}
11139
80626a55
DE
11140/* Initialize the use of the DWP file for the current objfile.
11141 By convention the name of the DWP file is ${objfile}.dwp.
11142 The result is NULL if it can't be found. */
a766d390 11143
80626a55 11144static struct dwp_file *
ab5088bf 11145open_and_init_dwp_file (void)
80626a55
DE
11146{
11147 struct objfile *objfile = dwarf2_per_objfile->objfile;
11148 struct dwp_file *dwp_file;
80626a55 11149
82bf32bc
JK
11150 /* Try to find first .dwp for the binary file before any symbolic links
11151 resolving. */
6c447423
DE
11152
11153 /* If the objfile is a debug file, find the name of the real binary
11154 file and get the name of dwp file from there. */
d721ba37 11155 std::string dwp_name;
6c447423
DE
11156 if (objfile->separate_debug_objfile_backlink != NULL)
11157 {
11158 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11159 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11160
d721ba37 11161 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11162 }
11163 else
d721ba37
PA
11164 dwp_name = objfile->original_name;
11165
11166 dwp_name += ".dwp";
80626a55 11167
d721ba37 11168 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11169 if (dbfd == NULL
11170 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11171 {
11172 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11173 dwp_name = objfile_name (objfile);
11174 dwp_name += ".dwp";
11175 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11176 }
11177
80626a55
DE
11178 if (dbfd == NULL)
11179 {
b4f54984 11180 if (dwarf_read_debug)
d721ba37 11181 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11182 return NULL;
3019eac3 11183 }
80626a55 11184 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11185 dwp_file->name = bfd_get_filename (dbfd.get ());
11186 dwp_file->dbfd = dbfd.release ();
c906108c 11187
80626a55 11188 /* +1: section 0 is unused */
192b62ce 11189 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11190 dwp_file->elf_sections =
11191 OBSTACK_CALLOC (&objfile->objfile_obstack,
11192 dwp_file->num_sections, asection *);
11193
192b62ce
TT
11194 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11195 dwp_file);
80626a55
DE
11196
11197 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11198
11199 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11200
73869dc2 11201 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11202 if (dwp_file->cus && dwp_file->tus
11203 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11204 {
11205 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11206 pretty bizarre. We use pulongest here because that's the established
4d65956b 11207 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11208 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11209 " TU version %s [in DWP file %s]"),
11210 pulongest (dwp_file->cus->version),
d721ba37 11211 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11212 }
08302ed2
DE
11213
11214 if (dwp_file->cus)
11215 dwp_file->version = dwp_file->cus->version;
11216 else if (dwp_file->tus)
11217 dwp_file->version = dwp_file->tus->version;
11218 else
11219 dwp_file->version = 2;
73869dc2
DE
11220
11221 if (dwp_file->version == 2)
192b62ce
TT
11222 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11223 dwp_file);
73869dc2 11224
19ac8c2e
DE
11225 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11226 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11227
b4f54984 11228 if (dwarf_read_debug)
80626a55
DE
11229 {
11230 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11231 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11232 " %s CUs, %s TUs\n",
11233 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11234 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11235 }
11236
11237 return dwp_file;
3019eac3 11238}
c906108c 11239
ab5088bf
DE
11240/* Wrapper around open_and_init_dwp_file, only open it once. */
11241
11242static struct dwp_file *
11243get_dwp_file (void)
11244{
11245 if (! dwarf2_per_objfile->dwp_checked)
11246 {
11247 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11248 dwarf2_per_objfile->dwp_checked = 1;
11249 }
11250 return dwarf2_per_objfile->dwp_file;
11251}
11252
80626a55
DE
11253/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11254 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11255 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11256 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11257 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11258
11259 This is called, for example, when wanting to read a variable with a
11260 complex location. Therefore we don't want to do file i/o for every call.
11261 Therefore we don't want to look for a DWO file on every call.
11262 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11263 then we check if we've already seen DWO_NAME, and only THEN do we check
11264 for a DWO file.
11265
1c658ad5 11266 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11267 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11268
3019eac3 11269static struct dwo_unit *
80626a55
DE
11270lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11271 const char *dwo_name, const char *comp_dir,
11272 ULONGEST signature, int is_debug_types)
3019eac3
DE
11273{
11274 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11275 const char *kind = is_debug_types ? "TU" : "CU";
11276 void **dwo_file_slot;
3019eac3 11277 struct dwo_file *dwo_file;
80626a55 11278 struct dwp_file *dwp_file;
cb1df416 11279
6a506a2d
DE
11280 /* First see if there's a DWP file.
11281 If we have a DWP file but didn't find the DWO inside it, don't
11282 look for the original DWO file. It makes gdb behave differently
11283 depending on whether one is debugging in the build tree. */
cf2c3c16 11284
ab5088bf 11285 dwp_file = get_dwp_file ();
80626a55 11286 if (dwp_file != NULL)
cf2c3c16 11287 {
80626a55
DE
11288 const struct dwp_hash_table *dwp_htab =
11289 is_debug_types ? dwp_file->tus : dwp_file->cus;
11290
11291 if (dwp_htab != NULL)
11292 {
11293 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11294 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11295 signature, is_debug_types);
80626a55
DE
11296
11297 if (dwo_cutu != NULL)
11298 {
b4f54984 11299 if (dwarf_read_debug)
80626a55
DE
11300 {
11301 fprintf_unfiltered (gdb_stdlog,
11302 "Virtual DWO %s %s found: @%s\n",
11303 kind, hex_string (signature),
11304 host_address_to_string (dwo_cutu));
11305 }
11306 return dwo_cutu;
11307 }
11308 }
11309 }
6a506a2d 11310 else
80626a55 11311 {
6a506a2d 11312 /* No DWP file, look for the DWO file. */
80626a55 11313
6a506a2d
DE
11314 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11315 if (*dwo_file_slot == NULL)
80626a55 11316 {
6a506a2d
DE
11317 /* Read in the file and build a table of the CUs/TUs it contains. */
11318 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11319 }
6a506a2d 11320 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11321 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11322
6a506a2d 11323 if (dwo_file != NULL)
19c3d4c9 11324 {
6a506a2d
DE
11325 struct dwo_unit *dwo_cutu = NULL;
11326
11327 if (is_debug_types && dwo_file->tus)
11328 {
11329 struct dwo_unit find_dwo_cutu;
11330
11331 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11332 find_dwo_cutu.signature = signature;
9a3c8263
SM
11333 dwo_cutu
11334 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11335 }
33c5cd75 11336 else if (!is_debug_types && dwo_file->cus)
80626a55 11337 {
33c5cd75
DB
11338 struct dwo_unit find_dwo_cutu;
11339
11340 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11341 find_dwo_cutu.signature = signature;
11342 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11343 &find_dwo_cutu);
6a506a2d
DE
11344 }
11345
11346 if (dwo_cutu != NULL)
11347 {
b4f54984 11348 if (dwarf_read_debug)
6a506a2d
DE
11349 {
11350 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11351 kind, dwo_name, hex_string (signature),
11352 host_address_to_string (dwo_cutu));
11353 }
11354 return dwo_cutu;
80626a55
DE
11355 }
11356 }
2e276125 11357 }
9cdd5dbd 11358
80626a55
DE
11359 /* We didn't find it. This could mean a dwo_id mismatch, or
11360 someone deleted the DWO/DWP file, or the search path isn't set up
11361 correctly to find the file. */
11362
b4f54984 11363 if (dwarf_read_debug)
80626a55
DE
11364 {
11365 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11366 kind, dwo_name, hex_string (signature));
11367 }
3019eac3 11368
6656a72d
DE
11369 /* This is a warning and not a complaint because it can be caused by
11370 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11371 {
11372 /* Print the name of the DWP file if we looked there, helps the user
11373 better diagnose the problem. */
11374 char *dwp_text = NULL;
11375 struct cleanup *cleanups;
11376
11377 if (dwp_file != NULL)
11378 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11379 cleanups = make_cleanup (xfree, dwp_text);
11380
11381 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11382 " [in module %s]"),
11383 kind, dwo_name, hex_string (signature),
11384 dwp_text != NULL ? dwp_text : "",
11385 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11386 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11387
11388 do_cleanups (cleanups);
11389 }
3019eac3 11390 return NULL;
5fb290d7
DJ
11391}
11392
80626a55
DE
11393/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11394 See lookup_dwo_cutu_unit for details. */
11395
11396static struct dwo_unit *
11397lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11398 const char *dwo_name, const char *comp_dir,
11399 ULONGEST signature)
11400{
11401 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11402}
11403
11404/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11405 See lookup_dwo_cutu_unit for details. */
11406
11407static struct dwo_unit *
11408lookup_dwo_type_unit (struct signatured_type *this_tu,
11409 const char *dwo_name, const char *comp_dir)
11410{
11411 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11412}
11413
89e63ee4
DE
11414/* Traversal function for queue_and_load_all_dwo_tus. */
11415
11416static int
11417queue_and_load_dwo_tu (void **slot, void *info)
11418{
11419 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11420 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11421 ULONGEST signature = dwo_unit->signature;
11422 struct signatured_type *sig_type =
11423 lookup_dwo_signatured_type (per_cu->cu, signature);
11424
11425 if (sig_type != NULL)
11426 {
11427 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11428
11429 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11430 a real dependency of PER_CU on SIG_TYPE. That is detected later
11431 while processing PER_CU. */
11432 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11433 load_full_type_unit (sig_cu);
11434 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11435 }
11436
11437 return 1;
11438}
11439
11440/* Queue all TUs contained in the DWO of PER_CU to be read in.
11441 The DWO may have the only definition of the type, though it may not be
11442 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11443 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11444
11445static void
11446queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11447{
11448 struct dwo_unit *dwo_unit;
11449 struct dwo_file *dwo_file;
11450
11451 gdb_assert (!per_cu->is_debug_types);
11452 gdb_assert (get_dwp_file () == NULL);
11453 gdb_assert (per_cu->cu != NULL);
11454
11455 dwo_unit = per_cu->cu->dwo_unit;
11456 gdb_assert (dwo_unit != NULL);
11457
11458 dwo_file = dwo_unit->dwo_file;
11459 if (dwo_file->tus != NULL)
11460 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11461}
11462
3019eac3
DE
11463/* Free all resources associated with DWO_FILE.
11464 Close the DWO file and munmap the sections.
11465 All memory should be on the objfile obstack. */
348e048f
DE
11466
11467static void
3019eac3 11468free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11469{
348e048f 11470
5c6fa7ab 11471 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11472 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11473
3019eac3
DE
11474 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11475}
348e048f 11476
3019eac3 11477/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11478
3019eac3
DE
11479static void
11480free_dwo_file_cleanup (void *arg)
11481{
11482 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11483 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11484
3019eac3
DE
11485 free_dwo_file (dwo_file, objfile);
11486}
348e048f 11487
3019eac3 11488/* Traversal function for free_dwo_files. */
2ab95328 11489
3019eac3
DE
11490static int
11491free_dwo_file_from_slot (void **slot, void *info)
11492{
11493 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11494 struct objfile *objfile = (struct objfile *) info;
348e048f 11495
3019eac3 11496 free_dwo_file (dwo_file, objfile);
348e048f 11497
3019eac3
DE
11498 return 1;
11499}
348e048f 11500
3019eac3 11501/* Free all resources associated with DWO_FILES. */
348e048f 11502
3019eac3
DE
11503static void
11504free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11505{
11506 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11507}
3019eac3
DE
11508\f
11509/* Read in various DIEs. */
348e048f 11510
d389af10
JK
11511/* qsort helper for inherit_abstract_dies. */
11512
11513static int
11514unsigned_int_compar (const void *ap, const void *bp)
11515{
11516 unsigned int a = *(unsigned int *) ap;
11517 unsigned int b = *(unsigned int *) bp;
11518
11519 return (a > b) - (b > a);
11520}
11521
11522/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11523 Inherit only the children of the DW_AT_abstract_origin DIE not being
11524 already referenced by DW_AT_abstract_origin from the children of the
11525 current DIE. */
d389af10
JK
11526
11527static void
11528inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11529{
11530 struct die_info *child_die;
11531 unsigned die_children_count;
11532 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11533 sect_offset *offsets;
11534 sect_offset *offsets_end, *offsetp;
d389af10
JK
11535 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11536 struct die_info *origin_die;
11537 /* Iterator of the ORIGIN_DIE children. */
11538 struct die_info *origin_child_die;
11539 struct cleanup *cleanups;
11540 struct attribute *attr;
cd02d79d
PA
11541 struct dwarf2_cu *origin_cu;
11542 struct pending **origin_previous_list_in_scope;
d389af10
JK
11543
11544 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11545 if (!attr)
11546 return;
11547
cd02d79d
PA
11548 /* Note that following die references may follow to a die in a
11549 different cu. */
11550
11551 origin_cu = cu;
11552 origin_die = follow_die_ref (die, attr, &origin_cu);
11553
11554 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11555 symbols in. */
11556 origin_previous_list_in_scope = origin_cu->list_in_scope;
11557 origin_cu->list_in_scope = cu->list_in_scope;
11558
edb3359d
DJ
11559 if (die->tag != origin_die->tag
11560 && !(die->tag == DW_TAG_inlined_subroutine
11561 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11562 complaint (&symfile_complaints,
11563 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11564 to_underlying (die->sect_off),
11565 to_underlying (origin_die->sect_off));
d389af10
JK
11566
11567 child_die = die->child;
11568 die_children_count = 0;
11569 while (child_die && child_die->tag)
11570 {
11571 child_die = sibling_die (child_die);
11572 die_children_count++;
11573 }
8d749320 11574 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11575 cleanups = make_cleanup (xfree, offsets);
11576
11577 offsets_end = offsets;
3ea89b92
PMR
11578 for (child_die = die->child;
11579 child_die && child_die->tag;
11580 child_die = sibling_die (child_die))
11581 {
11582 struct die_info *child_origin_die;
11583 struct dwarf2_cu *child_origin_cu;
11584
11585 /* We are trying to process concrete instance entries:
216f72a1 11586 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11587 it's not relevant to our analysis here. i.e. detecting DIEs that are
11588 present in the abstract instance but not referenced in the concrete
11589 one. */
216f72a1
JK
11590 if (child_die->tag == DW_TAG_call_site
11591 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11592 continue;
11593
c38f313d
DJ
11594 /* For each CHILD_DIE, find the corresponding child of
11595 ORIGIN_DIE. If there is more than one layer of
11596 DW_AT_abstract_origin, follow them all; there shouldn't be,
11597 but GCC versions at least through 4.4 generate this (GCC PR
11598 40573). */
3ea89b92
PMR
11599 child_origin_die = child_die;
11600 child_origin_cu = cu;
c38f313d
DJ
11601 while (1)
11602 {
cd02d79d
PA
11603 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11604 child_origin_cu);
c38f313d
DJ
11605 if (attr == NULL)
11606 break;
cd02d79d
PA
11607 child_origin_die = follow_die_ref (child_origin_die, attr,
11608 &child_origin_cu);
c38f313d
DJ
11609 }
11610
d389af10
JK
11611 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11612 counterpart may exist. */
c38f313d 11613 if (child_origin_die != child_die)
d389af10 11614 {
edb3359d
DJ
11615 if (child_die->tag != child_origin_die->tag
11616 && !(child_die->tag == DW_TAG_inlined_subroutine
11617 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11618 complaint (&symfile_complaints,
11619 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11620 "different tags"),
11621 to_underlying (child_die->sect_off),
11622 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11623 if (child_origin_die->parent != origin_die)
11624 complaint (&symfile_complaints,
11625 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11626 "different parents"),
11627 to_underlying (child_die->sect_off),
11628 to_underlying (child_origin_die->sect_off));
c38f313d 11629 else
9c541725 11630 *offsets_end++ = child_origin_die->sect_off;
d389af10 11631 }
d389af10
JK
11632 }
11633 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11634 unsigned_int_compar);
11635 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11636 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11637 complaint (&symfile_complaints,
11638 _("Multiple children of DIE 0x%x refer "
11639 "to DIE 0x%x as their abstract origin"),
9c541725 11640 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11641
11642 offsetp = offsets;
11643 origin_child_die = origin_die->child;
11644 while (origin_child_die && origin_child_die->tag)
11645 {
11646 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11647 while (offsetp < offsets_end
9c541725 11648 && *offsetp < origin_child_die->sect_off)
d389af10 11649 offsetp++;
b64f50a1 11650 if (offsetp >= offsets_end
9c541725 11651 || *offsetp > origin_child_die->sect_off)
d389af10 11652 {
adde2bff
DE
11653 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11654 Check whether we're already processing ORIGIN_CHILD_DIE.
11655 This can happen with mutually referenced abstract_origins.
11656 PR 16581. */
11657 if (!origin_child_die->in_process)
11658 process_die (origin_child_die, origin_cu);
d389af10
JK
11659 }
11660 origin_child_die = sibling_die (origin_child_die);
11661 }
cd02d79d 11662 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11663
11664 do_cleanups (cleanups);
11665}
11666
c906108c 11667static void
e7c27a73 11668read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11669{
e7c27a73 11670 struct objfile *objfile = cu->objfile;
3e29f34a 11671 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11672 struct context_stack *newobj;
c906108c
SS
11673 CORE_ADDR lowpc;
11674 CORE_ADDR highpc;
11675 struct die_info *child_die;
edb3359d 11676 struct attribute *attr, *call_line, *call_file;
15d034d0 11677 const char *name;
e142c38c 11678 CORE_ADDR baseaddr;
801e3a5b 11679 struct block *block;
edb3359d 11680 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11681 VEC (symbolp) *template_args = NULL;
11682 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11683
11684 if (inlined_func)
11685 {
11686 /* If we do not have call site information, we can't show the
11687 caller of this inlined function. That's too confusing, so
11688 only use the scope for local variables. */
11689 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11690 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11691 if (call_line == NULL || call_file == NULL)
11692 {
11693 read_lexical_block_scope (die, cu);
11694 return;
11695 }
11696 }
c906108c 11697
e142c38c
DJ
11698 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11699
94af9270 11700 name = dwarf2_name (die, cu);
c906108c 11701
e8d05480
JB
11702 /* Ignore functions with missing or empty names. These are actually
11703 illegal according to the DWARF standard. */
11704 if (name == NULL)
11705 {
11706 complaint (&symfile_complaints,
b64f50a1 11707 _("missing name for subprogram DIE at %d"),
9c541725 11708 to_underlying (die->sect_off));
e8d05480
JB
11709 return;
11710 }
11711
11712 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11713 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11714 <= PC_BOUNDS_INVALID)
e8d05480 11715 {
ae4d0c03
PM
11716 attr = dwarf2_attr (die, DW_AT_external, cu);
11717 if (!attr || !DW_UNSND (attr))
11718 complaint (&symfile_complaints,
3e43a32a
MS
11719 _("cannot get low and high bounds "
11720 "for subprogram DIE at %d"),
9c541725 11721 to_underlying (die->sect_off));
e8d05480
JB
11722 return;
11723 }
c906108c 11724
3e29f34a
MR
11725 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11726 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11727
34eaf542
TT
11728 /* If we have any template arguments, then we must allocate a
11729 different sort of symbol. */
11730 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11731 {
11732 if (child_die->tag == DW_TAG_template_type_param
11733 || child_die->tag == DW_TAG_template_value_param)
11734 {
e623cf5d 11735 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11736 templ_func->base.is_cplus_template_function = 1;
11737 break;
11738 }
11739 }
11740
fe978cb0
PA
11741 newobj = push_context (0, lowpc);
11742 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11743 (struct symbol *) templ_func);
4c2df51b 11744
4cecd739
DJ
11745 /* If there is a location expression for DW_AT_frame_base, record
11746 it. */
e142c38c 11747 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11748 if (attr)
fe978cb0 11749 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11750
63e43d3a
PMR
11751 /* If there is a location for the static link, record it. */
11752 newobj->static_link = NULL;
11753 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11754 if (attr)
11755 {
224c3ddb
SM
11756 newobj->static_link
11757 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11758 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11759 }
11760
e142c38c 11761 cu->list_in_scope = &local_symbols;
c906108c 11762
639d11d3 11763 if (die->child != NULL)
c906108c 11764 {
639d11d3 11765 child_die = die->child;
c906108c
SS
11766 while (child_die && child_die->tag)
11767 {
34eaf542
TT
11768 if (child_die->tag == DW_TAG_template_type_param
11769 || child_die->tag == DW_TAG_template_value_param)
11770 {
11771 struct symbol *arg = new_symbol (child_die, NULL, cu);
11772
f1078f66
DJ
11773 if (arg != NULL)
11774 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11775 }
11776 else
11777 process_die (child_die, cu);
c906108c
SS
11778 child_die = sibling_die (child_die);
11779 }
11780 }
11781
d389af10
JK
11782 inherit_abstract_dies (die, cu);
11783
4a811a97
UW
11784 /* If we have a DW_AT_specification, we might need to import using
11785 directives from the context of the specification DIE. See the
11786 comment in determine_prefix. */
11787 if (cu->language == language_cplus
11788 && dwarf2_attr (die, DW_AT_specification, cu))
11789 {
11790 struct dwarf2_cu *spec_cu = cu;
11791 struct die_info *spec_die = die_specification (die, &spec_cu);
11792
11793 while (spec_die)
11794 {
11795 child_die = spec_die->child;
11796 while (child_die && child_die->tag)
11797 {
11798 if (child_die->tag == DW_TAG_imported_module)
11799 process_die (child_die, spec_cu);
11800 child_die = sibling_die (child_die);
11801 }
11802
11803 /* In some cases, GCC generates specification DIEs that
11804 themselves contain DW_AT_specification attributes. */
11805 spec_die = die_specification (spec_die, &spec_cu);
11806 }
11807 }
11808
fe978cb0 11809 newobj = pop_context ();
c906108c 11810 /* Make a block for the local symbols within. */
fe978cb0 11811 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11812 newobj->static_link, lowpc, highpc);
801e3a5b 11813
df8a16a1 11814 /* For C++, set the block's scope. */
45280282
IB
11815 if ((cu->language == language_cplus
11816 || cu->language == language_fortran
c44af4eb
TT
11817 || cu->language == language_d
11818 || cu->language == language_rust)
4d4ec4e5 11819 && cu->processing_has_namespace_info)
195a3f6c
TT
11820 block_set_scope (block, determine_prefix (die, cu),
11821 &objfile->objfile_obstack);
df8a16a1 11822
801e3a5b
JB
11823 /* If we have address ranges, record them. */
11824 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11825
fe978cb0 11826 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11827
34eaf542
TT
11828 /* Attach template arguments to function. */
11829 if (! VEC_empty (symbolp, template_args))
11830 {
11831 gdb_assert (templ_func != NULL);
11832
11833 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11834 templ_func->template_arguments
8d749320
SM
11835 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11836 templ_func->n_template_arguments);
34eaf542
TT
11837 memcpy (templ_func->template_arguments,
11838 VEC_address (symbolp, template_args),
11839 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11840 VEC_free (symbolp, template_args);
11841 }
11842
208d8187
JB
11843 /* In C++, we can have functions nested inside functions (e.g., when
11844 a function declares a class that has methods). This means that
11845 when we finish processing a function scope, we may need to go
11846 back to building a containing block's symbol lists. */
fe978cb0 11847 local_symbols = newobj->locals;
22cee43f 11848 local_using_directives = newobj->local_using_directives;
208d8187 11849
921e78cf
JB
11850 /* If we've finished processing a top-level function, subsequent
11851 symbols go in the file symbol list. */
11852 if (outermost_context_p ())
e142c38c 11853 cu->list_in_scope = &file_symbols;
c906108c
SS
11854}
11855
11856/* Process all the DIES contained within a lexical block scope. Start
11857 a new scope, process the dies, and then close the scope. */
11858
11859static void
e7c27a73 11860read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11861{
e7c27a73 11862 struct objfile *objfile = cu->objfile;
3e29f34a 11863 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11864 struct context_stack *newobj;
c906108c
SS
11865 CORE_ADDR lowpc, highpc;
11866 struct die_info *child_die;
e142c38c
DJ
11867 CORE_ADDR baseaddr;
11868
11869 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11870
11871 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11872 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11873 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11874 be nasty. Might be easier to properly extend generic blocks to
af34e669 11875 describe ranges. */
e385593e
JK
11876 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11877 {
11878 case PC_BOUNDS_NOT_PRESENT:
11879 /* DW_TAG_lexical_block has no attributes, process its children as if
11880 there was no wrapping by that DW_TAG_lexical_block.
11881 GCC does no longer produces such DWARF since GCC r224161. */
11882 for (child_die = die->child;
11883 child_die != NULL && child_die->tag;
11884 child_die = sibling_die (child_die))
11885 process_die (child_die, cu);
11886 return;
11887 case PC_BOUNDS_INVALID:
11888 return;
11889 }
3e29f34a
MR
11890 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11891 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11892
11893 push_context (0, lowpc);
639d11d3 11894 if (die->child != NULL)
c906108c 11895 {
639d11d3 11896 child_die = die->child;
c906108c
SS
11897 while (child_die && child_die->tag)
11898 {
e7c27a73 11899 process_die (child_die, cu);
c906108c
SS
11900 child_die = sibling_die (child_die);
11901 }
11902 }
3ea89b92 11903 inherit_abstract_dies (die, cu);
fe978cb0 11904 newobj = pop_context ();
c906108c 11905
22cee43f 11906 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11907 {
801e3a5b 11908 struct block *block
63e43d3a 11909 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11910 newobj->start_addr, highpc);
801e3a5b
JB
11911
11912 /* Note that recording ranges after traversing children, as we
11913 do here, means that recording a parent's ranges entails
11914 walking across all its children's ranges as they appear in
11915 the address map, which is quadratic behavior.
11916
11917 It would be nicer to record the parent's ranges before
11918 traversing its children, simply overriding whatever you find
11919 there. But since we don't even decide whether to create a
11920 block until after we've traversed its children, that's hard
11921 to do. */
11922 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11923 }
fe978cb0 11924 local_symbols = newobj->locals;
22cee43f 11925 local_using_directives = newobj->local_using_directives;
c906108c
SS
11926}
11927
216f72a1 11928/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11929
11930static void
11931read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11932{
11933 struct objfile *objfile = cu->objfile;
11934 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11935 CORE_ADDR pc, baseaddr;
11936 struct attribute *attr;
11937 struct call_site *call_site, call_site_local;
11938 void **slot;
11939 int nparams;
11940 struct die_info *child_die;
11941
11942 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11943
216f72a1
JK
11944 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11945 if (attr == NULL)
11946 {
11947 /* This was a pre-DWARF-5 GNU extension alias
11948 for DW_AT_call_return_pc. */
11949 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11950 }
96408a79
SA
11951 if (!attr)
11952 {
11953 complaint (&symfile_complaints,
216f72a1 11954 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11955 "DIE 0x%x [in module %s]"),
9c541725 11956 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11957 return;
11958 }
31aa7e4e 11959 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11960 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11961
11962 if (cu->call_site_htab == NULL)
11963 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11964 NULL, &objfile->objfile_obstack,
11965 hashtab_obstack_allocate, NULL);
11966 call_site_local.pc = pc;
11967 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11968 if (*slot != NULL)
11969 {
11970 complaint (&symfile_complaints,
216f72a1 11971 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11972 "DIE 0x%x [in module %s]"),
9c541725 11973 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11974 objfile_name (objfile));
96408a79
SA
11975 return;
11976 }
11977
11978 /* Count parameters at the caller. */
11979
11980 nparams = 0;
11981 for (child_die = die->child; child_die && child_die->tag;
11982 child_die = sibling_die (child_die))
11983 {
216f72a1
JK
11984 if (child_die->tag != DW_TAG_call_site_parameter
11985 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11986 {
11987 complaint (&symfile_complaints,
216f72a1
JK
11988 _("Tag %d is not DW_TAG_call_site_parameter in "
11989 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11990 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11991 objfile_name (objfile));
96408a79
SA
11992 continue;
11993 }
11994
11995 nparams++;
11996 }
11997
224c3ddb
SM
11998 call_site
11999 = ((struct call_site *)
12000 obstack_alloc (&objfile->objfile_obstack,
12001 sizeof (*call_site)
12002 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12003 *slot = call_site;
12004 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12005 call_site->pc = pc;
12006
216f72a1
JK
12007 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12008 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12009 {
12010 struct die_info *func_die;
12011
12012 /* Skip also over DW_TAG_inlined_subroutine. */
12013 for (func_die = die->parent;
12014 func_die && func_die->tag != DW_TAG_subprogram
12015 && func_die->tag != DW_TAG_subroutine_type;
12016 func_die = func_die->parent);
12017
216f72a1
JK
12018 /* DW_AT_call_all_calls is a superset
12019 of DW_AT_call_all_tail_calls. */
96408a79 12020 if (func_die
216f72a1 12021 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12022 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12023 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12024 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12025 {
12026 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12027 not complete. But keep CALL_SITE for look ups via call_site_htab,
12028 both the initial caller containing the real return address PC and
12029 the final callee containing the current PC of a chain of tail
12030 calls do not need to have the tail call list complete. But any
12031 function candidate for a virtual tail call frame searched via
12032 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12033 determined unambiguously. */
12034 }
12035 else
12036 {
12037 struct type *func_type = NULL;
12038
12039 if (func_die)
12040 func_type = get_die_type (func_die, cu);
12041 if (func_type != NULL)
12042 {
12043 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12044
12045 /* Enlist this call site to the function. */
12046 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12047 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12048 }
12049 else
12050 complaint (&symfile_complaints,
216f72a1 12051 _("Cannot find function owning DW_TAG_call_site "
96408a79 12052 "DIE 0x%x [in module %s]"),
9c541725 12053 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12054 }
12055 }
12056
216f72a1
JK
12057 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12058 if (attr == NULL)
12059 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12060 if (attr == NULL)
12061 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12062 if (attr == NULL)
216f72a1
JK
12063 {
12064 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12065 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12066 }
96408a79
SA
12067 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12068 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12069 /* Keep NULL DWARF_BLOCK. */;
12070 else if (attr_form_is_block (attr))
12071 {
12072 struct dwarf2_locexpr_baton *dlbaton;
12073
8d749320 12074 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12075 dlbaton->data = DW_BLOCK (attr)->data;
12076 dlbaton->size = DW_BLOCK (attr)->size;
12077 dlbaton->per_cu = cu->per_cu;
12078
12079 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12080 }
7771576e 12081 else if (attr_form_is_ref (attr))
96408a79 12082 {
96408a79
SA
12083 struct dwarf2_cu *target_cu = cu;
12084 struct die_info *target_die;
12085
ac9ec31b 12086 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12087 gdb_assert (target_cu->objfile == objfile);
12088 if (die_is_declaration (target_die, target_cu))
12089 {
7d45c7c3 12090 const char *target_physname;
9112db09
JK
12091
12092 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12093 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12094 if (target_physname == NULL)
9112db09 12095 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12096 if (target_physname == NULL)
12097 complaint (&symfile_complaints,
216f72a1 12098 _("DW_AT_call_target target DIE has invalid "
96408a79 12099 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12100 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12101 else
7d455152 12102 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12103 }
12104 else
12105 {
12106 CORE_ADDR lowpc;
12107
12108 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12109 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12110 <= PC_BOUNDS_INVALID)
96408a79 12111 complaint (&symfile_complaints,
216f72a1 12112 _("DW_AT_call_target target DIE has invalid "
96408a79 12113 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12114 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12115 else
3e29f34a
MR
12116 {
12117 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12118 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12119 }
96408a79
SA
12120 }
12121 }
12122 else
12123 complaint (&symfile_complaints,
216f72a1 12124 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12125 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12126 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12127
12128 call_site->per_cu = cu->per_cu;
12129
12130 for (child_die = die->child;
12131 child_die && child_die->tag;
12132 child_die = sibling_die (child_die))
12133 {
96408a79 12134 struct call_site_parameter *parameter;
1788b2d3 12135 struct attribute *loc, *origin;
96408a79 12136
216f72a1
JK
12137 if (child_die->tag != DW_TAG_call_site_parameter
12138 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12139 {
12140 /* Already printed the complaint above. */
12141 continue;
12142 }
12143
12144 gdb_assert (call_site->parameter_count < nparams);
12145 parameter = &call_site->parameter[call_site->parameter_count];
12146
1788b2d3
JK
12147 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12148 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12149 register is contained in DW_AT_call_value. */
96408a79 12150
24c5c679 12151 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12152 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12153 if (origin == NULL)
12154 {
12155 /* This was a pre-DWARF-5 GNU extension alias
12156 for DW_AT_call_parameter. */
12157 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12158 }
7771576e 12159 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12160 {
1788b2d3 12161 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12162
12163 sect_offset sect_off
12164 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12165 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12166 {
12167 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12168 binding can be done only inside one CU. Such referenced DIE
12169 therefore cannot be even moved to DW_TAG_partial_unit. */
12170 complaint (&symfile_complaints,
216f72a1
JK
12171 _("DW_AT_call_parameter offset is not in CU for "
12172 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12173 to_underlying (child_die->sect_off),
12174 objfile_name (objfile));
d76b7dbc
JK
12175 continue;
12176 }
9c541725
PA
12177 parameter->u.param_cu_off
12178 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12179 }
12180 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12181 {
12182 complaint (&symfile_complaints,
12183 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12184 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12185 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12186 continue;
12187 }
24c5c679 12188 else
96408a79 12189 {
24c5c679
JK
12190 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12191 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12192 if (parameter->u.dwarf_reg != -1)
12193 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12194 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12195 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12196 &parameter->u.fb_offset))
12197 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12198 else
12199 {
12200 complaint (&symfile_complaints,
12201 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12202 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12203 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12204 "[in module %s]"),
9c541725
PA
12205 to_underlying (child_die->sect_off),
12206 objfile_name (objfile));
24c5c679
JK
12207 continue;
12208 }
96408a79
SA
12209 }
12210
216f72a1
JK
12211 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12212 if (attr == NULL)
12213 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12214 if (!attr_form_is_block (attr))
12215 {
12216 complaint (&symfile_complaints,
216f72a1
JK
12217 _("No DW_FORM_block* DW_AT_call_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 continue;
12222 }
12223 parameter->value = DW_BLOCK (attr)->data;
12224 parameter->value_size = DW_BLOCK (attr)->size;
12225
12226 /* Parameters are not pre-cleared by memset above. */
12227 parameter->data_value = NULL;
12228 parameter->data_value_size = 0;
12229 call_site->parameter_count++;
12230
216f72a1
JK
12231 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12232 if (attr == NULL)
12233 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12234 if (attr)
12235 {
12236 if (!attr_form_is_block (attr))
12237 complaint (&symfile_complaints,
216f72a1
JK
12238 _("No DW_FORM_block* DW_AT_call_data_value for "
12239 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12240 to_underlying (child_die->sect_off),
12241 objfile_name (objfile));
96408a79
SA
12242 else
12243 {
12244 parameter->data_value = DW_BLOCK (attr)->data;
12245 parameter->data_value_size = DW_BLOCK (attr)->size;
12246 }
12247 }
12248 }
12249}
12250
43988095
JK
12251/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12252 reading .debug_rnglists.
12253 Callback's type should be:
12254 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12255 Return true if the attributes are present and valid, otherwise,
12256 return false. */
12257
12258template <typename Callback>
12259static bool
12260dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12261 Callback &&callback)
12262{
12263 struct objfile *objfile = cu->objfile;
12264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12265 struct comp_unit_head *cu_header = &cu->header;
12266 bfd *obfd = objfile->obfd;
12267 unsigned int addr_size = cu_header->addr_size;
12268 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12269 /* Base address selection entry. */
12270 CORE_ADDR base;
12271 int found_base;
12272 unsigned int dummy;
12273 const gdb_byte *buffer;
12274 CORE_ADDR low = 0;
12275 CORE_ADDR high = 0;
12276 CORE_ADDR baseaddr;
12277 bool overflow = false;
12278
12279 found_base = cu->base_known;
12280 base = cu->base_address;
12281
12282 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12283 if (offset >= dwarf2_per_objfile->rnglists.size)
12284 {
12285 complaint (&symfile_complaints,
12286 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12287 offset);
12288 return false;
12289 }
12290 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12291
12292 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12293
12294 while (1)
12295 {
7814882a
JK
12296 /* Initialize it due to a false compiler warning. */
12297 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12298 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12299 + dwarf2_per_objfile->rnglists.size);
12300 unsigned int bytes_read;
12301
12302 if (buffer == buf_end)
12303 {
12304 overflow = true;
12305 break;
12306 }
12307 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12308 switch (rlet)
12309 {
12310 case DW_RLE_end_of_list:
12311 break;
12312 case DW_RLE_base_address:
12313 if (buffer + cu->header.addr_size > buf_end)
12314 {
12315 overflow = true;
12316 break;
12317 }
12318 base = read_address (obfd, buffer, cu, &bytes_read);
12319 found_base = 1;
12320 buffer += bytes_read;
12321 break;
12322 case DW_RLE_start_length:
12323 if (buffer + cu->header.addr_size > buf_end)
12324 {
12325 overflow = true;
12326 break;
12327 }
12328 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12329 buffer += bytes_read;
12330 range_end = (range_beginning
12331 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12332 buffer += bytes_read;
12333 if (buffer > buf_end)
12334 {
12335 overflow = true;
12336 break;
12337 }
12338 break;
12339 case DW_RLE_offset_pair:
12340 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12341 buffer += bytes_read;
12342 if (buffer > buf_end)
12343 {
12344 overflow = true;
12345 break;
12346 }
12347 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12348 buffer += bytes_read;
12349 if (buffer > buf_end)
12350 {
12351 overflow = true;
12352 break;
12353 }
12354 break;
12355 case DW_RLE_start_end:
12356 if (buffer + 2 * cu->header.addr_size > buf_end)
12357 {
12358 overflow = true;
12359 break;
12360 }
12361 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12362 buffer += bytes_read;
12363 range_end = read_address (obfd, buffer, cu, &bytes_read);
12364 buffer += bytes_read;
12365 break;
12366 default:
12367 complaint (&symfile_complaints,
12368 _("Invalid .debug_rnglists data (no base address)"));
12369 return false;
12370 }
12371 if (rlet == DW_RLE_end_of_list || overflow)
12372 break;
12373 if (rlet == DW_RLE_base_address)
12374 continue;
12375
12376 if (!found_base)
12377 {
12378 /* We have no valid base address for the ranges
12379 data. */
12380 complaint (&symfile_complaints,
12381 _("Invalid .debug_rnglists data (no base address)"));
12382 return false;
12383 }
12384
12385 if (range_beginning > range_end)
12386 {
12387 /* Inverted range entries are invalid. */
12388 complaint (&symfile_complaints,
12389 _("Invalid .debug_rnglists data (inverted range)"));
12390 return false;
12391 }
12392
12393 /* Empty range entries have no effect. */
12394 if (range_beginning == range_end)
12395 continue;
12396
12397 range_beginning += base;
12398 range_end += base;
12399
12400 /* A not-uncommon case of bad debug info.
12401 Don't pollute the addrmap with bad data. */
12402 if (range_beginning + baseaddr == 0
12403 && !dwarf2_per_objfile->has_section_at_zero)
12404 {
12405 complaint (&symfile_complaints,
12406 _(".debug_rnglists entry has start address of zero"
12407 " [in module %s]"), objfile_name (objfile));
12408 continue;
12409 }
12410
12411 callback (range_beginning, range_end);
12412 }
12413
12414 if (overflow)
12415 {
12416 complaint (&symfile_complaints,
12417 _("Offset %d is not terminated "
12418 "for DW_AT_ranges attribute"),
12419 offset);
12420 return false;
12421 }
12422
12423 return true;
12424}
12425
12426/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12427 Callback's type should be:
12428 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12429 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12430
43988095 12431template <typename Callback>
43039443 12432static int
5f46c5a5 12433dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12434 Callback &&callback)
43039443
JK
12435{
12436 struct objfile *objfile = cu->objfile;
3e29f34a 12437 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12438 struct comp_unit_head *cu_header = &cu->header;
12439 bfd *obfd = objfile->obfd;
12440 unsigned int addr_size = cu_header->addr_size;
12441 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12442 /* Base address selection entry. */
12443 CORE_ADDR base;
12444 int found_base;
12445 unsigned int dummy;
d521ce57 12446 const gdb_byte *buffer;
ff013f42 12447 CORE_ADDR baseaddr;
43039443 12448
43988095
JK
12449 if (cu_header->version >= 5)
12450 return dwarf2_rnglists_process (offset, cu, callback);
12451
d00adf39
DE
12452 found_base = cu->base_known;
12453 base = cu->base_address;
43039443 12454
be391dca 12455 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12456 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12457 {
12458 complaint (&symfile_complaints,
12459 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12460 offset);
12461 return 0;
12462 }
dce234bc 12463 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12464
e7030f15 12465 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12466
43039443
JK
12467 while (1)
12468 {
12469 CORE_ADDR range_beginning, range_end;
12470
12471 range_beginning = read_address (obfd, buffer, cu, &dummy);
12472 buffer += addr_size;
12473 range_end = read_address (obfd, buffer, cu, &dummy);
12474 buffer += addr_size;
12475 offset += 2 * addr_size;
12476
12477 /* An end of list marker is a pair of zero addresses. */
12478 if (range_beginning == 0 && range_end == 0)
12479 /* Found the end of list entry. */
12480 break;
12481
12482 /* Each base address selection entry is a pair of 2 values.
12483 The first is the largest possible address, the second is
12484 the base address. Check for a base address here. */
12485 if ((range_beginning & mask) == mask)
12486 {
28d2bfb9
AB
12487 /* If we found the largest possible address, then we already
12488 have the base address in range_end. */
12489 base = range_end;
43039443
JK
12490 found_base = 1;
12491 continue;
12492 }
12493
12494 if (!found_base)
12495 {
12496 /* We have no valid base address for the ranges
12497 data. */
12498 complaint (&symfile_complaints,
12499 _("Invalid .debug_ranges data (no base address)"));
12500 return 0;
12501 }
12502
9277c30c
UW
12503 if (range_beginning > range_end)
12504 {
12505 /* Inverted range entries are invalid. */
12506 complaint (&symfile_complaints,
12507 _("Invalid .debug_ranges data (inverted range)"));
12508 return 0;
12509 }
12510
12511 /* Empty range entries have no effect. */
12512 if (range_beginning == range_end)
12513 continue;
12514
43039443
JK
12515 range_beginning += base;
12516 range_end += base;
12517
01093045
DE
12518 /* A not-uncommon case of bad debug info.
12519 Don't pollute the addrmap with bad data. */
12520 if (range_beginning + baseaddr == 0
12521 && !dwarf2_per_objfile->has_section_at_zero)
12522 {
12523 complaint (&symfile_complaints,
12524 _(".debug_ranges entry has start address of zero"
4262abfb 12525 " [in module %s]"), objfile_name (objfile));
01093045
DE
12526 continue;
12527 }
12528
5f46c5a5
JK
12529 callback (range_beginning, range_end);
12530 }
12531
12532 return 1;
12533}
12534
12535/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12536 Return 1 if the attributes are present and valid, otherwise, return 0.
12537 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12538
12539static int
12540dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12541 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12542 struct partial_symtab *ranges_pst)
12543{
12544 struct objfile *objfile = cu->objfile;
12545 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12546 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12547 SECT_OFF_TEXT (objfile));
12548 int low_set = 0;
12549 CORE_ADDR low = 0;
12550 CORE_ADDR high = 0;
12551 int retval;
12552
12553 retval = dwarf2_ranges_process (offset, cu,
12554 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12555 {
9277c30c 12556 if (ranges_pst != NULL)
3e29f34a
MR
12557 {
12558 CORE_ADDR lowpc;
12559 CORE_ADDR highpc;
12560
12561 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12562 range_beginning + baseaddr);
12563 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12564 range_end + baseaddr);
12565 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12566 ranges_pst);
12567 }
ff013f42 12568
43039443
JK
12569 /* FIXME: This is recording everything as a low-high
12570 segment of consecutive addresses. We should have a
12571 data structure for discontiguous block ranges
12572 instead. */
12573 if (! low_set)
12574 {
12575 low = range_beginning;
12576 high = range_end;
12577 low_set = 1;
12578 }
12579 else
12580 {
12581 if (range_beginning < low)
12582 low = range_beginning;
12583 if (range_end > high)
12584 high = range_end;
12585 }
5f46c5a5
JK
12586 });
12587 if (!retval)
12588 return 0;
43039443
JK
12589
12590 if (! low_set)
12591 /* If the first entry is an end-of-list marker, the range
12592 describes an empty scope, i.e. no instructions. */
12593 return 0;
12594
12595 if (low_return)
12596 *low_return = low;
12597 if (high_return)
12598 *high_return = high;
12599 return 1;
12600}
12601
3a2b436a
JK
12602/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12603 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12604 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12605
3a2b436a 12606static enum pc_bounds_kind
af34e669 12607dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12608 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12609 struct partial_symtab *pst)
c906108c
SS
12610{
12611 struct attribute *attr;
91da1414 12612 struct attribute *attr_high;
af34e669
DJ
12613 CORE_ADDR low = 0;
12614 CORE_ADDR high = 0;
e385593e 12615 enum pc_bounds_kind ret;
c906108c 12616
91da1414
MW
12617 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12618 if (attr_high)
af34e669 12619 {
e142c38c 12620 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12621 if (attr)
91da1414 12622 {
31aa7e4e
JB
12623 low = attr_value_as_address (attr);
12624 high = attr_value_as_address (attr_high);
12625 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12626 high += low;
91da1414 12627 }
af34e669
DJ
12628 else
12629 /* Found high w/o low attribute. */
e385593e 12630 return PC_BOUNDS_INVALID;
af34e669
DJ
12631
12632 /* Found consecutive range of addresses. */
3a2b436a 12633 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12634 }
c906108c 12635 else
af34e669 12636 {
e142c38c 12637 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12638 if (attr != NULL)
12639 {
ab435259
DE
12640 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12641 We take advantage of the fact that DW_AT_ranges does not appear
12642 in DW_TAG_compile_unit of DWO files. */
12643 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12644 unsigned int ranges_offset = (DW_UNSND (attr)
12645 + (need_ranges_base
12646 ? cu->ranges_base
12647 : 0));
2e3cf129 12648
af34e669 12649 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12650 .debug_ranges section. */
2e3cf129 12651 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12652 return PC_BOUNDS_INVALID;
43039443 12653 /* Found discontinuous range of addresses. */
3a2b436a 12654 ret = PC_BOUNDS_RANGES;
af34e669 12655 }
e385593e
JK
12656 else
12657 return PC_BOUNDS_NOT_PRESENT;
af34e669 12658 }
c906108c 12659
9373cf26
JK
12660 /* read_partial_die has also the strict LOW < HIGH requirement. */
12661 if (high <= low)
e385593e 12662 return PC_BOUNDS_INVALID;
c906108c
SS
12663
12664 /* When using the GNU linker, .gnu.linkonce. sections are used to
12665 eliminate duplicate copies of functions and vtables and such.
12666 The linker will arbitrarily choose one and discard the others.
12667 The AT_*_pc values for such functions refer to local labels in
12668 these sections. If the section from that file was discarded, the
12669 labels are not in the output, so the relocs get a value of 0.
12670 If this is a discarded function, mark the pc bounds as invalid,
12671 so that GDB will ignore it. */
72dca2f5 12672 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12673 return PC_BOUNDS_INVALID;
c906108c
SS
12674
12675 *lowpc = low;
96408a79
SA
12676 if (highpc)
12677 *highpc = high;
af34e669 12678 return ret;
c906108c
SS
12679}
12680
b084d499
JB
12681/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12682 its low and high PC addresses. Do nothing if these addresses could not
12683 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12684 and HIGHPC to the high address if greater than HIGHPC. */
12685
12686static void
12687dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12688 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12689 struct dwarf2_cu *cu)
12690{
12691 CORE_ADDR low, high;
12692 struct die_info *child = die->child;
12693
e385593e 12694 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12695 {
325fac50
PA
12696 *lowpc = std::min (*lowpc, low);
12697 *highpc = std::max (*highpc, high);
b084d499
JB
12698 }
12699
12700 /* If the language does not allow nested subprograms (either inside
12701 subprograms or lexical blocks), we're done. */
12702 if (cu->language != language_ada)
12703 return;
6e70227d 12704
b084d499
JB
12705 /* Check all the children of the given DIE. If it contains nested
12706 subprograms, then check their pc bounds. Likewise, we need to
12707 check lexical blocks as well, as they may also contain subprogram
12708 definitions. */
12709 while (child && child->tag)
12710 {
12711 if (child->tag == DW_TAG_subprogram
12712 || child->tag == DW_TAG_lexical_block)
12713 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12714 child = sibling_die (child);
12715 }
12716}
12717
fae299cd
DC
12718/* Get the low and high pc's represented by the scope DIE, and store
12719 them in *LOWPC and *HIGHPC. If the correct values can't be
12720 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12721
12722static void
12723get_scope_pc_bounds (struct die_info *die,
12724 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12725 struct dwarf2_cu *cu)
12726{
12727 CORE_ADDR best_low = (CORE_ADDR) -1;
12728 CORE_ADDR best_high = (CORE_ADDR) 0;
12729 CORE_ADDR current_low, current_high;
12730
3a2b436a 12731 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12732 >= PC_BOUNDS_RANGES)
fae299cd
DC
12733 {
12734 best_low = current_low;
12735 best_high = current_high;
12736 }
12737 else
12738 {
12739 struct die_info *child = die->child;
12740
12741 while (child && child->tag)
12742 {
12743 switch (child->tag) {
12744 case DW_TAG_subprogram:
b084d499 12745 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12746 break;
12747 case DW_TAG_namespace:
f55ee35c 12748 case DW_TAG_module:
fae299cd
DC
12749 /* FIXME: carlton/2004-01-16: Should we do this for
12750 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12751 that current GCC's always emit the DIEs corresponding
12752 to definitions of methods of classes as children of a
12753 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12754 the DIEs giving the declarations, which could be
12755 anywhere). But I don't see any reason why the
12756 standards says that they have to be there. */
12757 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12758
12759 if (current_low != ((CORE_ADDR) -1))
12760 {
325fac50
PA
12761 best_low = std::min (best_low, current_low);
12762 best_high = std::max (best_high, current_high);
fae299cd
DC
12763 }
12764 break;
12765 default:
0963b4bd 12766 /* Ignore. */
fae299cd
DC
12767 break;
12768 }
12769
12770 child = sibling_die (child);
12771 }
12772 }
12773
12774 *lowpc = best_low;
12775 *highpc = best_high;
12776}
12777
801e3a5b
JB
12778/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12779 in DIE. */
380bca97 12780
801e3a5b
JB
12781static void
12782dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12783 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12784{
bb5ed363 12785 struct objfile *objfile = cu->objfile;
3e29f34a 12786 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12787 struct attribute *attr;
91da1414 12788 struct attribute *attr_high;
801e3a5b 12789
91da1414
MW
12790 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12791 if (attr_high)
801e3a5b 12792 {
801e3a5b
JB
12793 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12794 if (attr)
12795 {
31aa7e4e
JB
12796 CORE_ADDR low = attr_value_as_address (attr);
12797 CORE_ADDR high = attr_value_as_address (attr_high);
12798
12799 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12800 high += low;
9a619af0 12801
3e29f34a
MR
12802 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12803 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12804 record_block_range (block, low, high - 1);
801e3a5b
JB
12805 }
12806 }
12807
12808 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12809 if (attr)
12810 {
bb5ed363 12811 bfd *obfd = objfile->obfd;
ab435259
DE
12812 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12813 We take advantage of the fact that DW_AT_ranges does not appear
12814 in DW_TAG_compile_unit of DWO files. */
12815 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12816
12817 /* The value of the DW_AT_ranges attribute is the offset of the
12818 address range list in the .debug_ranges section. */
ab435259
DE
12819 unsigned long offset = (DW_UNSND (attr)
12820 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12821 const gdb_byte *buffer;
801e3a5b
JB
12822
12823 /* For some target architectures, but not others, the
12824 read_address function sign-extends the addresses it returns.
12825 To recognize base address selection entries, we need a
12826 mask. */
12827 unsigned int addr_size = cu->header.addr_size;
12828 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12829
12830 /* The base address, to which the next pair is relative. Note
12831 that this 'base' is a DWARF concept: most entries in a range
12832 list are relative, to reduce the number of relocs against the
12833 debugging information. This is separate from this function's
12834 'baseaddr' argument, which GDB uses to relocate debugging
12835 information from a shared library based on the address at
12836 which the library was loaded. */
d00adf39
DE
12837 CORE_ADDR base = cu->base_address;
12838 int base_known = cu->base_known;
801e3a5b 12839
5f46c5a5
JK
12840 dwarf2_ranges_process (offset, cu,
12841 [&] (CORE_ADDR start, CORE_ADDR end)
12842 {
58fdfd2c
JK
12843 start += baseaddr;
12844 end += baseaddr;
5f46c5a5
JK
12845 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12846 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12847 record_block_range (block, start, end - 1);
12848 });
801e3a5b
JB
12849 }
12850}
12851
685b1105
JK
12852/* Check whether the producer field indicates either of GCC < 4.6, or the
12853 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12854
685b1105
JK
12855static void
12856check_producer (struct dwarf2_cu *cu)
60d5a603 12857{
38360086 12858 int major, minor;
60d5a603
JK
12859
12860 if (cu->producer == NULL)
12861 {
12862 /* For unknown compilers expect their behavior is DWARF version
12863 compliant.
12864
12865 GCC started to support .debug_types sections by -gdwarf-4 since
12866 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12867 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12868 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12869 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12870 }
b1ffba5a 12871 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12872 {
38360086
MW
12873 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12874 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12875 }
5230b05a
WT
12876 else if (producer_is_icc (cu->producer, &major, &minor))
12877 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
12878 else
12879 {
12880 /* For other non-GCC compilers, expect their behavior is DWARF version
12881 compliant. */
60d5a603
JK
12882 }
12883
ba919b58 12884 cu->checked_producer = 1;
685b1105 12885}
ba919b58 12886
685b1105
JK
12887/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12888 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12889 during 4.6.0 experimental. */
12890
12891static int
12892producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12893{
12894 if (!cu->checked_producer)
12895 check_producer (cu);
12896
12897 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12898}
12899
12900/* Return the default accessibility type if it is not overriden by
12901 DW_AT_accessibility. */
12902
12903static enum dwarf_access_attribute
12904dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12905{
12906 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12907 {
12908 /* The default DWARF 2 accessibility for members is public, the default
12909 accessibility for inheritance is private. */
12910
12911 if (die->tag != DW_TAG_inheritance)
12912 return DW_ACCESS_public;
12913 else
12914 return DW_ACCESS_private;
12915 }
12916 else
12917 {
12918 /* DWARF 3+ defines the default accessibility a different way. The same
12919 rules apply now for DW_TAG_inheritance as for the members and it only
12920 depends on the container kind. */
12921
12922 if (die->parent->tag == DW_TAG_class_type)
12923 return DW_ACCESS_private;
12924 else
12925 return DW_ACCESS_public;
12926 }
12927}
12928
74ac6d43
TT
12929/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12930 offset. If the attribute was not found return 0, otherwise return
12931 1. If it was found but could not properly be handled, set *OFFSET
12932 to 0. */
12933
12934static int
12935handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12936 LONGEST *offset)
12937{
12938 struct attribute *attr;
12939
12940 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12941 if (attr != NULL)
12942 {
12943 *offset = 0;
12944
12945 /* Note that we do not check for a section offset first here.
12946 This is because DW_AT_data_member_location is new in DWARF 4,
12947 so if we see it, we can assume that a constant form is really
12948 a constant and not a section offset. */
12949 if (attr_form_is_constant (attr))
12950 *offset = dwarf2_get_attr_constant_value (attr, 0);
12951 else if (attr_form_is_section_offset (attr))
12952 dwarf2_complex_location_expr_complaint ();
12953 else if (attr_form_is_block (attr))
12954 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12955 else
12956 dwarf2_complex_location_expr_complaint ();
12957
12958 return 1;
12959 }
12960
12961 return 0;
12962}
12963
c906108c
SS
12964/* Add an aggregate field to the field list. */
12965
12966static void
107d2387 12967dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12968 struct dwarf2_cu *cu)
6e70227d 12969{
e7c27a73 12970 struct objfile *objfile = cu->objfile;
5e2b427d 12971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12972 struct nextfield *new_field;
12973 struct attribute *attr;
12974 struct field *fp;
15d034d0 12975 const char *fieldname = "";
c906108c
SS
12976
12977 /* Allocate a new field list entry and link it in. */
8d749320 12978 new_field = XNEW (struct nextfield);
b8c9b27d 12979 make_cleanup (xfree, new_field);
c906108c 12980 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12981
12982 if (die->tag == DW_TAG_inheritance)
12983 {
12984 new_field->next = fip->baseclasses;
12985 fip->baseclasses = new_field;
12986 }
12987 else
12988 {
12989 new_field->next = fip->fields;
12990 fip->fields = new_field;
12991 }
c906108c
SS
12992 fip->nfields++;
12993
e142c38c 12994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12995 if (attr)
12996 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12997 else
12998 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12999 if (new_field->accessibility != DW_ACCESS_public)
13000 fip->non_public_fields = 1;
60d5a603 13001
e142c38c 13002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13003 if (attr)
13004 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13005 else
13006 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13007
13008 fp = &new_field->field;
a9a9bd0f 13009
e142c38c 13010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13011 {
74ac6d43
TT
13012 LONGEST offset;
13013
a9a9bd0f 13014 /* Data member other than a C++ static data member. */
6e70227d 13015
c906108c 13016 /* Get type of field. */
e7c27a73 13017 fp->type = die_type (die, cu);
c906108c 13018
d6a843b5 13019 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13020
c906108c 13021 /* Get bit size of field (zero if none). */
e142c38c 13022 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13023 if (attr)
13024 {
13025 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13026 }
13027 else
13028 {
13029 FIELD_BITSIZE (*fp) = 0;
13030 }
13031
13032 /* Get bit offset of field. */
74ac6d43
TT
13033 if (handle_data_member_location (die, cu, &offset))
13034 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13035 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13036 if (attr)
13037 {
5e2b427d 13038 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13039 {
13040 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13041 additional bit offset from the MSB of the containing
13042 anonymous object to the MSB of the field. We don't
13043 have to do anything special since we don't need to
13044 know the size of the anonymous object. */
f41f5e61 13045 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13046 }
13047 else
13048 {
13049 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13050 MSB of the anonymous object, subtract off the number of
13051 bits from the MSB of the field to the MSB of the
13052 object, and then subtract off the number of bits of
13053 the field itself. The result is the bit offset of
13054 the LSB of the field. */
c906108c
SS
13055 int anonymous_size;
13056 int bit_offset = DW_UNSND (attr);
13057
e142c38c 13058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13059 if (attr)
13060 {
13061 /* The size of the anonymous object containing
13062 the bit field is explicit, so use the
13063 indicated size (in bytes). */
13064 anonymous_size = DW_UNSND (attr);
13065 }
13066 else
13067 {
13068 /* The size of the anonymous object containing
13069 the bit field must be inferred from the type
13070 attribute of the data member containing the
13071 bit field. */
13072 anonymous_size = TYPE_LENGTH (fp->type);
13073 }
f41f5e61
PA
13074 SET_FIELD_BITPOS (*fp,
13075 (FIELD_BITPOS (*fp)
13076 + anonymous_size * bits_per_byte
13077 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13078 }
13079 }
da5b30da
AA
13080 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13081 if (attr != NULL)
13082 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13083 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13084
13085 /* Get name of field. */
39cbfefa
DJ
13086 fieldname = dwarf2_name (die, cu);
13087 if (fieldname == NULL)
13088 fieldname = "";
d8151005
DJ
13089
13090 /* The name is already allocated along with this objfile, so we don't
13091 need to duplicate it for the type. */
13092 fp->name = fieldname;
c906108c
SS
13093
13094 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13095 pointer or virtual base class pointer) to private. */
e142c38c 13096 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13097 {
d48cc9dd 13098 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13099 new_field->accessibility = DW_ACCESS_private;
13100 fip->non_public_fields = 1;
13101 }
13102 }
a9a9bd0f 13103 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13104 {
a9a9bd0f
DC
13105 /* C++ static member. */
13106
13107 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13108 is a declaration, but all versions of G++ as of this writing
13109 (so through at least 3.2.1) incorrectly generate
13110 DW_TAG_variable tags. */
6e70227d 13111
ff355380 13112 const char *physname;
c906108c 13113
a9a9bd0f 13114 /* Get name of field. */
39cbfefa
DJ
13115 fieldname = dwarf2_name (die, cu);
13116 if (fieldname == NULL)
c906108c
SS
13117 return;
13118
254e6b9e 13119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13120 if (attr
13121 /* Only create a symbol if this is an external value.
13122 new_symbol checks this and puts the value in the global symbol
13123 table, which we want. If it is not external, new_symbol
13124 will try to put the value in cu->list_in_scope which is wrong. */
13125 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13126 {
13127 /* A static const member, not much different than an enum as far as
13128 we're concerned, except that we can support more types. */
13129 new_symbol (die, NULL, cu);
13130 }
13131
2df3850c 13132 /* Get physical name. */
ff355380 13133 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13134
d8151005
DJ
13135 /* The name is already allocated along with this objfile, so we don't
13136 need to duplicate it for the type. */
13137 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13138 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13139 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13140 }
13141 else if (die->tag == DW_TAG_inheritance)
13142 {
74ac6d43 13143 LONGEST offset;
d4b96c9a 13144
74ac6d43
TT
13145 /* C++ base class field. */
13146 if (handle_data_member_location (die, cu, &offset))
13147 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13148 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13149 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13150 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13151 fip->nbaseclasses++;
13152 }
13153}
13154
98751a41
JK
13155/* Add a typedef defined in the scope of the FIP's class. */
13156
13157static void
13158dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13159 struct dwarf2_cu *cu)
6e70227d 13160{
98751a41 13161 struct typedef_field_list *new_field;
98751a41 13162 struct typedef_field *fp;
98751a41
JK
13163
13164 /* Allocate a new field list entry and link it in. */
8d749320 13165 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13166 make_cleanup (xfree, new_field);
13167
13168 gdb_assert (die->tag == DW_TAG_typedef);
13169
13170 fp = &new_field->field;
13171
13172 /* Get name of field. */
13173 fp->name = dwarf2_name (die, cu);
13174 if (fp->name == NULL)
13175 return;
13176
13177 fp->type = read_type_die (die, cu);
13178
13179 new_field->next = fip->typedef_field_list;
13180 fip->typedef_field_list = new_field;
13181 fip->typedef_field_list_count++;
13182}
13183
c906108c
SS
13184/* Create the vector of fields, and attach it to the type. */
13185
13186static void
fba45db2 13187dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13188 struct dwarf2_cu *cu)
c906108c
SS
13189{
13190 int nfields = fip->nfields;
13191
13192 /* Record the field count, allocate space for the array of fields,
13193 and create blank accessibility bitfields if necessary. */
13194 TYPE_NFIELDS (type) = nfields;
13195 TYPE_FIELDS (type) = (struct field *)
13196 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13197 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13198
b4ba55a1 13199 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13200 {
13201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13202
13203 TYPE_FIELD_PRIVATE_BITS (type) =
13204 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13205 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13206
13207 TYPE_FIELD_PROTECTED_BITS (type) =
13208 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13209 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13210
774b6a14
TT
13211 TYPE_FIELD_IGNORE_BITS (type) =
13212 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13213 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13214 }
13215
13216 /* If the type has baseclasses, allocate and clear a bit vector for
13217 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13218 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13219 {
13220 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13221 unsigned char *pointer;
c906108c
SS
13222
13223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13224 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13225 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13226 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13227 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13228 }
13229
3e43a32a
MS
13230 /* Copy the saved-up fields into the field vector. Start from the head of
13231 the list, adding to the tail of the field array, so that they end up in
13232 the same order in the array in which they were added to the list. */
c906108c
SS
13233 while (nfields-- > 0)
13234 {
7d0ccb61
DJ
13235 struct nextfield *fieldp;
13236
13237 if (fip->fields)
13238 {
13239 fieldp = fip->fields;
13240 fip->fields = fieldp->next;
13241 }
13242 else
13243 {
13244 fieldp = fip->baseclasses;
13245 fip->baseclasses = fieldp->next;
13246 }
13247
13248 TYPE_FIELD (type, nfields) = fieldp->field;
13249 switch (fieldp->accessibility)
c906108c 13250 {
c5aa993b 13251 case DW_ACCESS_private:
b4ba55a1
JB
13252 if (cu->language != language_ada)
13253 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13254 break;
c906108c 13255
c5aa993b 13256 case DW_ACCESS_protected:
b4ba55a1
JB
13257 if (cu->language != language_ada)
13258 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13259 break;
c906108c 13260
c5aa993b
JM
13261 case DW_ACCESS_public:
13262 break;
c906108c 13263
c5aa993b
JM
13264 default:
13265 /* Unknown accessibility. Complain and treat it as public. */
13266 {
e2e0b3e5 13267 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13268 fieldp->accessibility);
c5aa993b
JM
13269 }
13270 break;
c906108c
SS
13271 }
13272 if (nfields < fip->nbaseclasses)
13273 {
7d0ccb61 13274 switch (fieldp->virtuality)
c906108c 13275 {
c5aa993b
JM
13276 case DW_VIRTUALITY_virtual:
13277 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13278 if (cu->language == language_ada)
a73c6dcd 13279 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13280 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13281 break;
c906108c
SS
13282 }
13283 }
c906108c
SS
13284 }
13285}
13286
7d27a96d
TT
13287/* Return true if this member function is a constructor, false
13288 otherwise. */
13289
13290static int
13291dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13292{
13293 const char *fieldname;
fe978cb0 13294 const char *type_name;
7d27a96d
TT
13295 int len;
13296
13297 if (die->parent == NULL)
13298 return 0;
13299
13300 if (die->parent->tag != DW_TAG_structure_type
13301 && die->parent->tag != DW_TAG_union_type
13302 && die->parent->tag != DW_TAG_class_type)
13303 return 0;
13304
13305 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13306 type_name = dwarf2_name (die->parent, cu);
13307 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13308 return 0;
13309
13310 len = strlen (fieldname);
fe978cb0
PA
13311 return (strncmp (fieldname, type_name, len) == 0
13312 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13313}
13314
c906108c
SS
13315/* Add a member function to the proper fieldlist. */
13316
13317static void
107d2387 13318dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13319 struct type *type, struct dwarf2_cu *cu)
c906108c 13320{
e7c27a73 13321 struct objfile *objfile = cu->objfile;
c906108c
SS
13322 struct attribute *attr;
13323 struct fnfieldlist *flp;
13324 int i;
13325 struct fn_field *fnp;
15d034d0 13326 const char *fieldname;
c906108c 13327 struct nextfnfield *new_fnfield;
f792889a 13328 struct type *this_type;
60d5a603 13329 enum dwarf_access_attribute accessibility;
c906108c 13330
b4ba55a1 13331 if (cu->language == language_ada)
a73c6dcd 13332 error (_("unexpected member function in Ada type"));
b4ba55a1 13333
2df3850c 13334 /* Get name of member function. */
39cbfefa
DJ
13335 fieldname = dwarf2_name (die, cu);
13336 if (fieldname == NULL)
2df3850c 13337 return;
c906108c 13338
c906108c
SS
13339 /* Look up member function name in fieldlist. */
13340 for (i = 0; i < fip->nfnfields; i++)
13341 {
27bfe10e 13342 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13343 break;
13344 }
13345
13346 /* Create new list element if necessary. */
13347 if (i < fip->nfnfields)
13348 flp = &fip->fnfieldlists[i];
13349 else
13350 {
13351 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13352 {
13353 fip->fnfieldlists = (struct fnfieldlist *)
13354 xrealloc (fip->fnfieldlists,
13355 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13356 * sizeof (struct fnfieldlist));
c906108c 13357 if (fip->nfnfields == 0)
c13c43fd 13358 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13359 }
13360 flp = &fip->fnfieldlists[fip->nfnfields];
13361 flp->name = fieldname;
13362 flp->length = 0;
13363 flp->head = NULL;
3da10d80 13364 i = fip->nfnfields++;
c906108c
SS
13365 }
13366
13367 /* Create a new member function field and chain it to the field list
0963b4bd 13368 entry. */
8d749320 13369 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13370 make_cleanup (xfree, new_fnfield);
c906108c
SS
13371 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13372 new_fnfield->next = flp->head;
13373 flp->head = new_fnfield;
13374 flp->length++;
13375
13376 /* Fill in the member function field info. */
13377 fnp = &new_fnfield->fnfield;
3da10d80
KS
13378
13379 /* Delay processing of the physname until later. */
9c37b5ae 13380 if (cu->language == language_cplus)
3da10d80
KS
13381 {
13382 add_to_method_list (type, i, flp->length - 1, fieldname,
13383 die, cu);
13384 }
13385 else
13386 {
1d06ead6 13387 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13388 fnp->physname = physname ? physname : "";
13389 }
13390
c906108c 13391 fnp->type = alloc_type (objfile);
f792889a
DJ
13392 this_type = read_type_die (die, cu);
13393 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13394 {
f792889a 13395 int nparams = TYPE_NFIELDS (this_type);
c906108c 13396
f792889a 13397 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13398 of the method itself (TYPE_CODE_METHOD). */
13399 smash_to_method_type (fnp->type, type,
f792889a
DJ
13400 TYPE_TARGET_TYPE (this_type),
13401 TYPE_FIELDS (this_type),
13402 TYPE_NFIELDS (this_type),
13403 TYPE_VARARGS (this_type));
c906108c
SS
13404
13405 /* Handle static member functions.
c5aa993b 13406 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13407 member functions. G++ helps GDB by marking the first
13408 parameter for non-static member functions (which is the this
13409 pointer) as artificial. We obtain this information from
13410 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13411 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13412 fnp->voffset = VOFFSET_STATIC;
13413 }
13414 else
e2e0b3e5 13415 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13416 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13417
13418 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13419 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13420 fnp->fcontext = die_containing_type (die, cu);
c906108c 13421
3e43a32a
MS
13422 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13423 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13424
13425 /* Get accessibility. */
e142c38c 13426 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13427 if (attr)
aead7601 13428 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13429 else
13430 accessibility = dwarf2_default_access_attribute (die, cu);
13431 switch (accessibility)
c906108c 13432 {
60d5a603
JK
13433 case DW_ACCESS_private:
13434 fnp->is_private = 1;
13435 break;
13436 case DW_ACCESS_protected:
13437 fnp->is_protected = 1;
13438 break;
c906108c
SS
13439 }
13440
b02dede2 13441 /* Check for artificial methods. */
e142c38c 13442 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13443 if (attr && DW_UNSND (attr) != 0)
13444 fnp->is_artificial = 1;
13445
7d27a96d
TT
13446 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13447
0d564a31 13448 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13449 function. For older versions of GCC, this is an offset in the
13450 appropriate virtual table, as specified by DW_AT_containing_type.
13451 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13452 to the object address. */
13453
e142c38c 13454 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13455 if (attr)
8e19ed76 13456 {
aec5aa8b 13457 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13458 {
aec5aa8b
TT
13459 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13460 {
13461 /* Old-style GCC. */
13462 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13463 }
13464 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13465 || (DW_BLOCK (attr)->size > 1
13466 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13467 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13468 {
aec5aa8b
TT
13469 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13470 if ((fnp->voffset % cu->header.addr_size) != 0)
13471 dwarf2_complex_location_expr_complaint ();
13472 else
13473 fnp->voffset /= cu->header.addr_size;
13474 fnp->voffset += 2;
13475 }
13476 else
13477 dwarf2_complex_location_expr_complaint ();
13478
13479 if (!fnp->fcontext)
7e993ebf
KS
13480 {
13481 /* If there is no `this' field and no DW_AT_containing_type,
13482 we cannot actually find a base class context for the
13483 vtable! */
13484 if (TYPE_NFIELDS (this_type) == 0
13485 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13486 {
13487 complaint (&symfile_complaints,
13488 _("cannot determine context for virtual member "
13489 "function \"%s\" (offset %d)"),
9c541725 13490 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13491 }
13492 else
13493 {
13494 fnp->fcontext
13495 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13496 }
13497 }
aec5aa8b 13498 }
3690dd37 13499 else if (attr_form_is_section_offset (attr))
8e19ed76 13500 {
4d3c2250 13501 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13502 }
13503 else
13504 {
4d3c2250
KB
13505 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13506 fieldname);
8e19ed76 13507 }
0d564a31 13508 }
d48cc9dd
DJ
13509 else
13510 {
13511 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13512 if (attr && DW_UNSND (attr))
13513 {
13514 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13515 complaint (&symfile_complaints,
3e43a32a
MS
13516 _("Member function \"%s\" (offset %d) is virtual "
13517 "but the vtable offset is not specified"),
9c541725 13518 fieldname, to_underlying (die->sect_off));
9655fd1a 13519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13520 TYPE_CPLUS_DYNAMIC (type) = 1;
13521 }
13522 }
c906108c
SS
13523}
13524
13525/* Create the vector of member function fields, and attach it to the type. */
13526
13527static void
fba45db2 13528dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13529 struct dwarf2_cu *cu)
c906108c
SS
13530{
13531 struct fnfieldlist *flp;
c906108c
SS
13532 int i;
13533
b4ba55a1 13534 if (cu->language == language_ada)
a73c6dcd 13535 error (_("unexpected member functions in Ada type"));
b4ba55a1 13536
c906108c
SS
13537 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13538 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13539 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13540
13541 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13542 {
13543 struct nextfnfield *nfp = flp->head;
13544 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13545 int k;
13546
13547 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13548 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13549 fn_flp->fn_fields = (struct fn_field *)
13550 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13551 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13552 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13553 }
13554
13555 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13556}
13557
1168df01
JB
13558/* Returns non-zero if NAME is the name of a vtable member in CU's
13559 language, zero otherwise. */
13560static int
13561is_vtable_name (const char *name, struct dwarf2_cu *cu)
13562{
13563 static const char vptr[] = "_vptr";
987504bb 13564 static const char vtable[] = "vtable";
1168df01 13565
9c37b5ae
TT
13566 /* Look for the C++ form of the vtable. */
13567 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13568 return 1;
13569
13570 return 0;
13571}
13572
c0dd20ea 13573/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13574 functions, with the ABI-specified layout. If TYPE describes
13575 such a structure, smash it into a member function type.
61049d3b
DJ
13576
13577 GCC shouldn't do this; it should just output pointer to member DIEs.
13578 This is GCC PR debug/28767. */
c0dd20ea 13579
0b92b5bb
TT
13580static void
13581quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13582{
09e2d7c7 13583 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13584
13585 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13586 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13587 return;
c0dd20ea
DJ
13588
13589 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13590 if (TYPE_FIELD_NAME (type, 0) == NULL
13591 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13592 || TYPE_FIELD_NAME (type, 1) == NULL
13593 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13594 return;
c0dd20ea
DJ
13595
13596 /* Find the type of the method. */
0b92b5bb 13597 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13598 if (pfn_type == NULL
13599 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13600 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13601 return;
c0dd20ea
DJ
13602
13603 /* Look for the "this" argument. */
13604 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13605 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13606 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13607 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13608 return;
c0dd20ea 13609
09e2d7c7 13610 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13611 new_type = alloc_type (objfile);
09e2d7c7 13612 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13613 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13614 TYPE_VARARGS (pfn_type));
0b92b5bb 13615 smash_to_methodptr_type (type, new_type);
c0dd20ea 13616}
1168df01 13617
685b1105 13618
c906108c 13619/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13620 (definition) to create a type for the structure or union. Fill in
13621 the type's name and general properties; the members will not be
83655187
DE
13622 processed until process_structure_scope. A symbol table entry for
13623 the type will also not be done until process_structure_scope (assuming
13624 the type has a name).
c906108c 13625
c767944b
DJ
13626 NOTE: we need to call these functions regardless of whether or not the
13627 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13628 structure or union. This gets the type entered into our set of
83655187 13629 user defined types. */
c906108c 13630
f792889a 13631static struct type *
134d01f1 13632read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13633{
e7c27a73 13634 struct objfile *objfile = cu->objfile;
c906108c
SS
13635 struct type *type;
13636 struct attribute *attr;
15d034d0 13637 const char *name;
c906108c 13638
348e048f
DE
13639 /* If the definition of this type lives in .debug_types, read that type.
13640 Don't follow DW_AT_specification though, that will take us back up
13641 the chain and we want to go down. */
45e58e77 13642 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13643 if (attr)
13644 {
ac9ec31b 13645 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13646
ac9ec31b 13647 /* The type's CU may not be the same as CU.
02142a6c 13648 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13649 return set_die_type (die, type, cu);
13650 }
13651
c0dd20ea 13652 type = alloc_type (objfile);
c906108c 13653 INIT_CPLUS_SPECIFIC (type);
93311388 13654
39cbfefa
DJ
13655 name = dwarf2_name (die, cu);
13656 if (name != NULL)
c906108c 13657 {
987504bb 13658 if (cu->language == language_cplus
c44af4eb
TT
13659 || cu->language == language_d
13660 || cu->language == language_rust)
63d06c5c 13661 {
15d034d0 13662 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13663
13664 /* dwarf2_full_name might have already finished building the DIE's
13665 type. If so, there is no need to continue. */
13666 if (get_die_type (die, cu) != NULL)
13667 return get_die_type (die, cu);
13668
13669 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13670 if (die->tag == DW_TAG_structure_type
13671 || die->tag == DW_TAG_class_type)
13672 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13673 }
13674 else
13675 {
d8151005
DJ
13676 /* The name is already allocated along with this objfile, so
13677 we don't need to duplicate it for the type. */
7d455152 13678 TYPE_TAG_NAME (type) = name;
94af9270
KS
13679 if (die->tag == DW_TAG_class_type)
13680 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13681 }
c906108c
SS
13682 }
13683
13684 if (die->tag == DW_TAG_structure_type)
13685 {
13686 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13687 }
13688 else if (die->tag == DW_TAG_union_type)
13689 {
13690 TYPE_CODE (type) = TYPE_CODE_UNION;
13691 }
13692 else
13693 {
4753d33b 13694 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13695 }
13696
0cc2414c
TT
13697 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13698 TYPE_DECLARED_CLASS (type) = 1;
13699
e142c38c 13700 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13701 if (attr)
13702 {
155bfbd3
JB
13703 if (attr_form_is_constant (attr))
13704 TYPE_LENGTH (type) = DW_UNSND (attr);
13705 else
13706 {
13707 /* For the moment, dynamic type sizes are not supported
13708 by GDB's struct type. The actual size is determined
13709 on-demand when resolving the type of a given object,
13710 so set the type's length to zero for now. Otherwise,
13711 we record an expression as the length, and that expression
13712 could lead to a very large value, which could eventually
13713 lead to us trying to allocate that much memory when creating
13714 a value of that type. */
13715 TYPE_LENGTH (type) = 0;
13716 }
c906108c
SS
13717 }
13718 else
13719 {
13720 TYPE_LENGTH (type) = 0;
13721 }
13722
5230b05a 13723 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 13724 {
5230b05a
WT
13725 /* ICC<14 does not output the required DW_AT_declaration on
13726 incomplete types, but gives them a size of zero. */
422b1cb0 13727 TYPE_STUB (type) = 1;
685b1105
JK
13728 }
13729 else
13730 TYPE_STUB_SUPPORTED (type) = 1;
13731
dc718098 13732 if (die_is_declaration (die, cu))
876cecd0 13733 TYPE_STUB (type) = 1;
a6c727b2
DJ
13734 else if (attr == NULL && die->child == NULL
13735 && producer_is_realview (cu->producer))
13736 /* RealView does not output the required DW_AT_declaration
13737 on incomplete types. */
13738 TYPE_STUB (type) = 1;
dc718098 13739
c906108c
SS
13740 /* We need to add the type field to the die immediately so we don't
13741 infinitely recurse when dealing with pointers to the structure
0963b4bd 13742 type within the structure itself. */
1c379e20 13743 set_die_type (die, type, cu);
c906108c 13744
7e314c57
JK
13745 /* set_die_type should be already done. */
13746 set_descriptive_type (type, die, cu);
13747
c767944b
DJ
13748 return type;
13749}
13750
13751/* Finish creating a structure or union type, including filling in
13752 its members and creating a symbol for it. */
13753
13754static void
13755process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13756{
13757 struct objfile *objfile = cu->objfile;
ca040673 13758 struct die_info *child_die;
c767944b
DJ
13759 struct type *type;
13760
13761 type = get_die_type (die, cu);
13762 if (type == NULL)
13763 type = read_structure_type (die, cu);
13764
e142c38c 13765 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13766 {
13767 struct field_info fi;
34eaf542 13768 VEC (symbolp) *template_args = NULL;
c767944b 13769 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13770
13771 memset (&fi, 0, sizeof (struct field_info));
13772
639d11d3 13773 child_die = die->child;
c906108c
SS
13774
13775 while (child_die && child_die->tag)
13776 {
a9a9bd0f
DC
13777 if (child_die->tag == DW_TAG_member
13778 || child_die->tag == DW_TAG_variable)
c906108c 13779 {
a9a9bd0f
DC
13780 /* NOTE: carlton/2002-11-05: A C++ static data member
13781 should be a DW_TAG_member that is a declaration, but
13782 all versions of G++ as of this writing (so through at
13783 least 3.2.1) incorrectly generate DW_TAG_variable
13784 tags for them instead. */
e7c27a73 13785 dwarf2_add_field (&fi, child_die, cu);
c906108c 13786 }
8713b1b1 13787 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13788 {
e98c9e7c
TT
13789 /* Rust doesn't have member functions in the C++ sense.
13790 However, it does emit ordinary functions as children
13791 of a struct DIE. */
13792 if (cu->language == language_rust)
13793 read_func_scope (child_die, cu);
13794 else
13795 {
13796 /* C++ member function. */
13797 dwarf2_add_member_fn (&fi, child_die, type, cu);
13798 }
c906108c
SS
13799 }
13800 else if (child_die->tag == DW_TAG_inheritance)
13801 {
13802 /* C++ base class field. */
e7c27a73 13803 dwarf2_add_field (&fi, child_die, cu);
c906108c 13804 }
98751a41
JK
13805 else if (child_die->tag == DW_TAG_typedef)
13806 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13807 else if (child_die->tag == DW_TAG_template_type_param
13808 || child_die->tag == DW_TAG_template_value_param)
13809 {
13810 struct symbol *arg = new_symbol (child_die, NULL, cu);
13811
f1078f66
DJ
13812 if (arg != NULL)
13813 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13814 }
13815
c906108c
SS
13816 child_die = sibling_die (child_die);
13817 }
13818
34eaf542
TT
13819 /* Attach template arguments to type. */
13820 if (! VEC_empty (symbolp, template_args))
13821 {
13822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13823 TYPE_N_TEMPLATE_ARGUMENTS (type)
13824 = VEC_length (symbolp, template_args);
13825 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13826 = XOBNEWVEC (&objfile->objfile_obstack,
13827 struct symbol *,
13828 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13829 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13830 VEC_address (symbolp, template_args),
13831 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13832 * sizeof (struct symbol *)));
13833 VEC_free (symbolp, template_args);
13834 }
13835
c906108c
SS
13836 /* Attach fields and member functions to the type. */
13837 if (fi.nfields)
e7c27a73 13838 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13839 if (fi.nfnfields)
13840 {
e7c27a73 13841 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13842
c5aa993b 13843 /* Get the type which refers to the base class (possibly this
c906108c 13844 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13845 class from the DW_AT_containing_type attribute. This use of
13846 DW_AT_containing_type is a GNU extension. */
c906108c 13847
e142c38c 13848 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13849 {
e7c27a73 13850 struct type *t = die_containing_type (die, cu);
c906108c 13851
ae6ae975 13852 set_type_vptr_basetype (type, t);
c906108c
SS
13853 if (type == t)
13854 {
c906108c
SS
13855 int i;
13856
13857 /* Our own class provides vtbl ptr. */
13858 for (i = TYPE_NFIELDS (t) - 1;
13859 i >= TYPE_N_BASECLASSES (t);
13860 --i)
13861 {
0d5cff50 13862 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13863
1168df01 13864 if (is_vtable_name (fieldname, cu))
c906108c 13865 {
ae6ae975 13866 set_type_vptr_fieldno (type, i);
c906108c
SS
13867 break;
13868 }
13869 }
13870
13871 /* Complain if virtual function table field not found. */
13872 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13873 complaint (&symfile_complaints,
3e43a32a
MS
13874 _("virtual function table pointer "
13875 "not found when defining class '%s'"),
4d3c2250
KB
13876 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13877 "");
c906108c
SS
13878 }
13879 else
13880 {
ae6ae975 13881 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13882 }
13883 }
f6235d4c 13884 else if (cu->producer
61012eef 13885 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13886 {
13887 /* The IBM XLC compiler does not provide direct indication
13888 of the containing type, but the vtable pointer is
13889 always named __vfp. */
13890
13891 int i;
13892
13893 for (i = TYPE_NFIELDS (type) - 1;
13894 i >= TYPE_N_BASECLASSES (type);
13895 --i)
13896 {
13897 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13898 {
ae6ae975
DE
13899 set_type_vptr_fieldno (type, i);
13900 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13901 break;
13902 }
13903 }
13904 }
c906108c 13905 }
98751a41
JK
13906
13907 /* Copy fi.typedef_field_list linked list elements content into the
13908 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13909 if (fi.typedef_field_list)
13910 {
13911 int i = fi.typedef_field_list_count;
13912
a0d7a4ff 13913 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13914 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13915 = ((struct typedef_field *)
13916 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13917 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13918
13919 /* Reverse the list order to keep the debug info elements order. */
13920 while (--i >= 0)
13921 {
13922 struct typedef_field *dest, *src;
6e70227d 13923
98751a41
JK
13924 dest = &TYPE_TYPEDEF_FIELD (type, i);
13925 src = &fi.typedef_field_list->field;
13926 fi.typedef_field_list = fi.typedef_field_list->next;
13927 *dest = *src;
13928 }
13929 }
c767944b
DJ
13930
13931 do_cleanups (back_to);
c906108c 13932 }
63d06c5c 13933
bb5ed363 13934 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13935
90aeadfc
DC
13936 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13937 snapshots) has been known to create a die giving a declaration
13938 for a class that has, as a child, a die giving a definition for a
13939 nested class. So we have to process our children even if the
13940 current die is a declaration. Normally, of course, a declaration
13941 won't have any children at all. */
134d01f1 13942
ca040673
DE
13943 child_die = die->child;
13944
90aeadfc
DC
13945 while (child_die != NULL && child_die->tag)
13946 {
13947 if (child_die->tag == DW_TAG_member
13948 || child_die->tag == DW_TAG_variable
34eaf542
TT
13949 || child_die->tag == DW_TAG_inheritance
13950 || child_die->tag == DW_TAG_template_value_param
13951 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13952 {
90aeadfc 13953 /* Do nothing. */
134d01f1 13954 }
90aeadfc
DC
13955 else
13956 process_die (child_die, cu);
134d01f1 13957
90aeadfc 13958 child_die = sibling_die (child_die);
134d01f1
DJ
13959 }
13960
fa4028e9
JB
13961 /* Do not consider external references. According to the DWARF standard,
13962 these DIEs are identified by the fact that they have no byte_size
13963 attribute, and a declaration attribute. */
13964 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13965 || !die_is_declaration (die, cu))
c767944b 13966 new_symbol (die, type, cu);
134d01f1
DJ
13967}
13968
55426c9d
JB
13969/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13970 update TYPE using some information only available in DIE's children. */
13971
13972static void
13973update_enumeration_type_from_children (struct die_info *die,
13974 struct type *type,
13975 struct dwarf2_cu *cu)
13976{
60f7655a 13977 struct die_info *child_die;
55426c9d
JB
13978 int unsigned_enum = 1;
13979 int flag_enum = 1;
13980 ULONGEST mask = 0;
55426c9d 13981
8268c778 13982 auto_obstack obstack;
55426c9d 13983
60f7655a
DE
13984 for (child_die = die->child;
13985 child_die != NULL && child_die->tag;
13986 child_die = sibling_die (child_die))
55426c9d
JB
13987 {
13988 struct attribute *attr;
13989 LONGEST value;
13990 const gdb_byte *bytes;
13991 struct dwarf2_locexpr_baton *baton;
13992 const char *name;
60f7655a 13993
55426c9d
JB
13994 if (child_die->tag != DW_TAG_enumerator)
13995 continue;
13996
13997 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13998 if (attr == NULL)
13999 continue;
14000
14001 name = dwarf2_name (child_die, cu);
14002 if (name == NULL)
14003 name = "<anonymous enumerator>";
14004
14005 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14006 &value, &bytes, &baton);
14007 if (value < 0)
14008 {
14009 unsigned_enum = 0;
14010 flag_enum = 0;
14011 }
14012 else if ((mask & value) != 0)
14013 flag_enum = 0;
14014 else
14015 mask |= value;
14016
14017 /* If we already know that the enum type is neither unsigned, nor
14018 a flag type, no need to look at the rest of the enumerates. */
14019 if (!unsigned_enum && !flag_enum)
14020 break;
55426c9d
JB
14021 }
14022
14023 if (unsigned_enum)
14024 TYPE_UNSIGNED (type) = 1;
14025 if (flag_enum)
14026 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14027}
14028
134d01f1
DJ
14029/* Given a DW_AT_enumeration_type die, set its type. We do not
14030 complete the type's fields yet, or create any symbols. */
c906108c 14031
f792889a 14032static struct type *
134d01f1 14033read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14034{
e7c27a73 14035 struct objfile *objfile = cu->objfile;
c906108c 14036 struct type *type;
c906108c 14037 struct attribute *attr;
0114d602 14038 const char *name;
134d01f1 14039
348e048f
DE
14040 /* If the definition of this type lives in .debug_types, read that type.
14041 Don't follow DW_AT_specification though, that will take us back up
14042 the chain and we want to go down. */
45e58e77 14043 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14044 if (attr)
14045 {
ac9ec31b 14046 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14047
ac9ec31b 14048 /* The type's CU may not be the same as CU.
02142a6c 14049 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14050 return set_die_type (die, type, cu);
14051 }
14052
c906108c
SS
14053 type = alloc_type (objfile);
14054
14055 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14056 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14057 if (name != NULL)
7d455152 14058 TYPE_TAG_NAME (type) = name;
c906108c 14059
0626fc76
TT
14060 attr = dwarf2_attr (die, DW_AT_type, cu);
14061 if (attr != NULL)
14062 {
14063 struct type *underlying_type = die_type (die, cu);
14064
14065 TYPE_TARGET_TYPE (type) = underlying_type;
14066 }
14067
e142c38c 14068 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14069 if (attr)
14070 {
14071 TYPE_LENGTH (type) = DW_UNSND (attr);
14072 }
14073 else
14074 {
14075 TYPE_LENGTH (type) = 0;
14076 }
14077
137033e9
JB
14078 /* The enumeration DIE can be incomplete. In Ada, any type can be
14079 declared as private in the package spec, and then defined only
14080 inside the package body. Such types are known as Taft Amendment
14081 Types. When another package uses such a type, an incomplete DIE
14082 may be generated by the compiler. */
02eb380e 14083 if (die_is_declaration (die, cu))
876cecd0 14084 TYPE_STUB (type) = 1;
02eb380e 14085
0626fc76
TT
14086 /* Finish the creation of this type by using the enum's children.
14087 We must call this even when the underlying type has been provided
14088 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14089 update_enumeration_type_from_children (die, type, cu);
14090
0626fc76
TT
14091 /* If this type has an underlying type that is not a stub, then we
14092 may use its attributes. We always use the "unsigned" attribute
14093 in this situation, because ordinarily we guess whether the type
14094 is unsigned -- but the guess can be wrong and the underlying type
14095 can tell us the reality. However, we defer to a local size
14096 attribute if one exists, because this lets the compiler override
14097 the underlying type if needed. */
14098 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14099 {
14100 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14101 if (TYPE_LENGTH (type) == 0)
14102 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14103 }
14104
3d567982
TT
14105 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14106
f792889a 14107 return set_die_type (die, type, cu);
134d01f1
DJ
14108}
14109
14110/* Given a pointer to a die which begins an enumeration, process all
14111 the dies that define the members of the enumeration, and create the
14112 symbol for the enumeration type.
14113
14114 NOTE: We reverse the order of the element list. */
14115
14116static void
14117process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14118{
f792889a 14119 struct type *this_type;
134d01f1 14120
f792889a
DJ
14121 this_type = get_die_type (die, cu);
14122 if (this_type == NULL)
14123 this_type = read_enumeration_type (die, cu);
9dc481d3 14124
639d11d3 14125 if (die->child != NULL)
c906108c 14126 {
9dc481d3
DE
14127 struct die_info *child_die;
14128 struct symbol *sym;
14129 struct field *fields = NULL;
14130 int num_fields = 0;
15d034d0 14131 const char *name;
9dc481d3 14132
639d11d3 14133 child_die = die->child;
c906108c
SS
14134 while (child_die && child_die->tag)
14135 {
14136 if (child_die->tag != DW_TAG_enumerator)
14137 {
e7c27a73 14138 process_die (child_die, cu);
c906108c
SS
14139 }
14140 else
14141 {
39cbfefa
DJ
14142 name = dwarf2_name (child_die, cu);
14143 if (name)
c906108c 14144 {
f792889a 14145 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14146
14147 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14148 {
14149 fields = (struct field *)
14150 xrealloc (fields,
14151 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14152 * sizeof (struct field));
c906108c
SS
14153 }
14154
3567439c 14155 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14156 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14157 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14158 FIELD_BITSIZE (fields[num_fields]) = 0;
14159
14160 num_fields++;
14161 }
14162 }
14163
14164 child_die = sibling_die (child_die);
14165 }
14166
14167 if (num_fields)
14168 {
f792889a
DJ
14169 TYPE_NFIELDS (this_type) = num_fields;
14170 TYPE_FIELDS (this_type) = (struct field *)
14171 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14172 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14173 sizeof (struct field) * num_fields);
b8c9b27d 14174 xfree (fields);
c906108c 14175 }
c906108c 14176 }
134d01f1 14177
6c83ed52
TT
14178 /* If we are reading an enum from a .debug_types unit, and the enum
14179 is a declaration, and the enum is not the signatured type in the
14180 unit, then we do not want to add a symbol for it. Adding a
14181 symbol would in some cases obscure the true definition of the
14182 enum, giving users an incomplete type when the definition is
14183 actually available. Note that we do not want to do this for all
14184 enums which are just declarations, because C++0x allows forward
14185 enum declarations. */
3019eac3 14186 if (cu->per_cu->is_debug_types
6c83ed52
TT
14187 && die_is_declaration (die, cu))
14188 {
52dc124a 14189 struct signatured_type *sig_type;
6c83ed52 14190
c0f78cd4 14191 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14192 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14193 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14194 return;
14195 }
14196
f792889a 14197 new_symbol (die, this_type, cu);
c906108c
SS
14198}
14199
14200/* Extract all information from a DW_TAG_array_type DIE and put it in
14201 the DIE's type field. For now, this only handles one dimensional
14202 arrays. */
14203
f792889a 14204static struct type *
e7c27a73 14205read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14206{
e7c27a73 14207 struct objfile *objfile = cu->objfile;
c906108c 14208 struct die_info *child_die;
7e314c57 14209 struct type *type;
c906108c
SS
14210 struct type *element_type, *range_type, *index_type;
14211 struct type **range_types = NULL;
14212 struct attribute *attr;
14213 int ndim = 0;
14214 struct cleanup *back_to;
15d034d0 14215 const char *name;
dc53a7ad 14216 unsigned int bit_stride = 0;
c906108c 14217
e7c27a73 14218 element_type = die_type (die, cu);
c906108c 14219
7e314c57
JK
14220 /* The die_type call above may have already set the type for this DIE. */
14221 type = get_die_type (die, cu);
14222 if (type)
14223 return type;
14224
dc53a7ad
JB
14225 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14226 if (attr != NULL)
14227 bit_stride = DW_UNSND (attr) * 8;
14228
14229 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14230 if (attr != NULL)
14231 bit_stride = DW_UNSND (attr);
14232
c906108c
SS
14233 /* Irix 6.2 native cc creates array types without children for
14234 arrays with unspecified length. */
639d11d3 14235 if (die->child == NULL)
c906108c 14236 {
46bf5051 14237 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14238 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14239 type = create_array_type_with_stride (NULL, element_type, range_type,
14240 bit_stride);
f792889a 14241 return set_die_type (die, type, cu);
c906108c
SS
14242 }
14243
14244 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14245 child_die = die->child;
c906108c
SS
14246 while (child_die && child_die->tag)
14247 {
14248 if (child_die->tag == DW_TAG_subrange_type)
14249 {
f792889a 14250 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14251
f792889a 14252 if (child_type != NULL)
a02abb62 14253 {
0963b4bd
MS
14254 /* The range type was succesfully read. Save it for the
14255 array type creation. */
a02abb62
JB
14256 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14257 {
14258 range_types = (struct type **)
14259 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14260 * sizeof (struct type *));
14261 if (ndim == 0)
14262 make_cleanup (free_current_contents, &range_types);
14263 }
f792889a 14264 range_types[ndim++] = child_type;
a02abb62 14265 }
c906108c
SS
14266 }
14267 child_die = sibling_die (child_die);
14268 }
14269
14270 /* Dwarf2 dimensions are output from left to right, create the
14271 necessary array types in backwards order. */
7ca2d3a3 14272
c906108c 14273 type = element_type;
7ca2d3a3
DL
14274
14275 if (read_array_order (die, cu) == DW_ORD_col_major)
14276 {
14277 int i = 0;
9a619af0 14278
7ca2d3a3 14279 while (i < ndim)
dc53a7ad
JB
14280 type = create_array_type_with_stride (NULL, type, range_types[i++],
14281 bit_stride);
7ca2d3a3
DL
14282 }
14283 else
14284 {
14285 while (ndim-- > 0)
dc53a7ad
JB
14286 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14287 bit_stride);
7ca2d3a3 14288 }
c906108c 14289
f5f8a009
EZ
14290 /* Understand Dwarf2 support for vector types (like they occur on
14291 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14292 array type. This is not part of the Dwarf2/3 standard yet, but a
14293 custom vendor extension. The main difference between a regular
14294 array and the vector variant is that vectors are passed by value
14295 to functions. */
e142c38c 14296 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14297 if (attr)
ea37ba09 14298 make_vector_type (type);
f5f8a009 14299
dbc98a8b
KW
14300 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14301 implementation may choose to implement triple vectors using this
14302 attribute. */
14303 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14304 if (attr)
14305 {
14306 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14307 TYPE_LENGTH (type) = DW_UNSND (attr);
14308 else
3e43a32a
MS
14309 complaint (&symfile_complaints,
14310 _("DW_AT_byte_size for array type smaller "
14311 "than the total size of elements"));
dbc98a8b
KW
14312 }
14313
39cbfefa
DJ
14314 name = dwarf2_name (die, cu);
14315 if (name)
14316 TYPE_NAME (type) = name;
6e70227d 14317
0963b4bd 14318 /* Install the type in the die. */
7e314c57
JK
14319 set_die_type (die, type, cu);
14320
14321 /* set_die_type should be already done. */
b4ba55a1
JB
14322 set_descriptive_type (type, die, cu);
14323
c906108c
SS
14324 do_cleanups (back_to);
14325
7e314c57 14326 return type;
c906108c
SS
14327}
14328
7ca2d3a3 14329static enum dwarf_array_dim_ordering
6e70227d 14330read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14331{
14332 struct attribute *attr;
14333
14334 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14335
aead7601
SM
14336 if (attr)
14337 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14338
0963b4bd
MS
14339 /* GNU F77 is a special case, as at 08/2004 array type info is the
14340 opposite order to the dwarf2 specification, but data is still
14341 laid out as per normal fortran.
7ca2d3a3 14342
0963b4bd
MS
14343 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14344 version checking. */
7ca2d3a3 14345
905e0470
PM
14346 if (cu->language == language_fortran
14347 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14348 {
14349 return DW_ORD_row_major;
14350 }
14351
6e70227d 14352 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14353 {
14354 case array_column_major:
14355 return DW_ORD_col_major;
14356 case array_row_major:
14357 default:
14358 return DW_ORD_row_major;
14359 };
14360}
14361
72019c9c 14362/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14363 the DIE's type field. */
72019c9c 14364
f792889a 14365static struct type *
72019c9c
GM
14366read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14367{
7e314c57
JK
14368 struct type *domain_type, *set_type;
14369 struct attribute *attr;
f792889a 14370
7e314c57
JK
14371 domain_type = die_type (die, cu);
14372
14373 /* The die_type call above may have already set the type for this DIE. */
14374 set_type = get_die_type (die, cu);
14375 if (set_type)
14376 return set_type;
14377
14378 set_type = create_set_type (NULL, domain_type);
14379
14380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14381 if (attr)
14382 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14383
f792889a 14384 return set_die_type (die, set_type, cu);
72019c9c 14385}
7ca2d3a3 14386
0971de02
TT
14387/* A helper for read_common_block that creates a locexpr baton.
14388 SYM is the symbol which we are marking as computed.
14389 COMMON_DIE is the DIE for the common block.
14390 COMMON_LOC is the location expression attribute for the common
14391 block itself.
14392 MEMBER_LOC is the location expression attribute for the particular
14393 member of the common block that we are processing.
14394 CU is the CU from which the above come. */
14395
14396static void
14397mark_common_block_symbol_computed (struct symbol *sym,
14398 struct die_info *common_die,
14399 struct attribute *common_loc,
14400 struct attribute *member_loc,
14401 struct dwarf2_cu *cu)
14402{
14403 struct objfile *objfile = dwarf2_per_objfile->objfile;
14404 struct dwarf2_locexpr_baton *baton;
14405 gdb_byte *ptr;
14406 unsigned int cu_off;
14407 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14408 LONGEST offset = 0;
14409
14410 gdb_assert (common_loc && member_loc);
14411 gdb_assert (attr_form_is_block (common_loc));
14412 gdb_assert (attr_form_is_block (member_loc)
14413 || attr_form_is_constant (member_loc));
14414
8d749320 14415 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14416 baton->per_cu = cu->per_cu;
14417 gdb_assert (baton->per_cu);
14418
14419 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14420
14421 if (attr_form_is_constant (member_loc))
14422 {
14423 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14424 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14425 }
14426 else
14427 baton->size += DW_BLOCK (member_loc)->size;
14428
224c3ddb 14429 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14430 baton->data = ptr;
14431
14432 *ptr++ = DW_OP_call4;
9c541725 14433 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14434 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14435 ptr += 4;
14436
14437 if (attr_form_is_constant (member_loc))
14438 {
14439 *ptr++ = DW_OP_addr;
14440 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14441 ptr += cu->header.addr_size;
14442 }
14443 else
14444 {
14445 /* We have to copy the data here, because DW_OP_call4 will only
14446 use a DW_AT_location attribute. */
14447 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14448 ptr += DW_BLOCK (member_loc)->size;
14449 }
14450
14451 *ptr++ = DW_OP_plus;
14452 gdb_assert (ptr - baton->data == baton->size);
14453
0971de02 14454 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14455 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14456}
14457
4357ac6c
TT
14458/* Create appropriate locally-scoped variables for all the
14459 DW_TAG_common_block entries. Also create a struct common_block
14460 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14461 is used to sepate the common blocks name namespace from regular
14462 variable names. */
c906108c
SS
14463
14464static void
e7c27a73 14465read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14466{
0971de02
TT
14467 struct attribute *attr;
14468
14469 attr = dwarf2_attr (die, DW_AT_location, cu);
14470 if (attr)
14471 {
14472 /* Support the .debug_loc offsets. */
14473 if (attr_form_is_block (attr))
14474 {
14475 /* Ok. */
14476 }
14477 else if (attr_form_is_section_offset (attr))
14478 {
14479 dwarf2_complex_location_expr_complaint ();
14480 attr = NULL;
14481 }
14482 else
14483 {
14484 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14485 "common block member");
14486 attr = NULL;
14487 }
14488 }
14489
639d11d3 14490 if (die->child != NULL)
c906108c 14491 {
4357ac6c
TT
14492 struct objfile *objfile = cu->objfile;
14493 struct die_info *child_die;
14494 size_t n_entries = 0, size;
14495 struct common_block *common_block;
14496 struct symbol *sym;
74ac6d43 14497
4357ac6c
TT
14498 for (child_die = die->child;
14499 child_die && child_die->tag;
14500 child_die = sibling_die (child_die))
14501 ++n_entries;
14502
14503 size = (sizeof (struct common_block)
14504 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14505 common_block
14506 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14507 size);
4357ac6c
TT
14508 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14509 common_block->n_entries = 0;
14510
14511 for (child_die = die->child;
14512 child_die && child_die->tag;
14513 child_die = sibling_die (child_die))
14514 {
14515 /* Create the symbol in the DW_TAG_common_block block in the current
14516 symbol scope. */
e7c27a73 14517 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14518 if (sym != NULL)
14519 {
14520 struct attribute *member_loc;
14521
14522 common_block->contents[common_block->n_entries++] = sym;
14523
14524 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14525 cu);
14526 if (member_loc)
14527 {
14528 /* GDB has handled this for a long time, but it is
14529 not specified by DWARF. It seems to have been
14530 emitted by gfortran at least as recently as:
14531 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14532 complaint (&symfile_complaints,
14533 _("Variable in common block has "
14534 "DW_AT_data_member_location "
14535 "- DIE at 0x%x [in module %s]"),
9c541725 14536 to_underlying (child_die->sect_off),
4262abfb 14537 objfile_name (cu->objfile));
0971de02
TT
14538
14539 if (attr_form_is_section_offset (member_loc))
14540 dwarf2_complex_location_expr_complaint ();
14541 else if (attr_form_is_constant (member_loc)
14542 || attr_form_is_block (member_loc))
14543 {
14544 if (attr)
14545 mark_common_block_symbol_computed (sym, die, attr,
14546 member_loc, cu);
14547 }
14548 else
14549 dwarf2_complex_location_expr_complaint ();
14550 }
14551 }
c906108c 14552 }
4357ac6c
TT
14553
14554 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14555 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14556 }
14557}
14558
0114d602 14559/* Create a type for a C++ namespace. */
d9fa45fe 14560
0114d602
DJ
14561static struct type *
14562read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14563{
e7c27a73 14564 struct objfile *objfile = cu->objfile;
0114d602 14565 const char *previous_prefix, *name;
9219021c 14566 int is_anonymous;
0114d602
DJ
14567 struct type *type;
14568
14569 /* For extensions, reuse the type of the original namespace. */
14570 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14571 {
14572 struct die_info *ext_die;
14573 struct dwarf2_cu *ext_cu = cu;
9a619af0 14574
0114d602
DJ
14575 ext_die = dwarf2_extension (die, &ext_cu);
14576 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14577
14578 /* EXT_CU may not be the same as CU.
02142a6c 14579 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14580 return set_die_type (die, type, cu);
14581 }
9219021c 14582
e142c38c 14583 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14584
14585 /* Now build the name of the current namespace. */
14586
0114d602
DJ
14587 previous_prefix = determine_prefix (die, cu);
14588 if (previous_prefix[0] != '\0')
14589 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14590 previous_prefix, name, 0, cu);
0114d602
DJ
14591
14592 /* Create the type. */
19f392bc 14593 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14594 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14595
60531b24 14596 return set_die_type (die, type, cu);
0114d602
DJ
14597}
14598
22cee43f 14599/* Read a namespace scope. */
0114d602
DJ
14600
14601static void
14602read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14603{
14604 struct objfile *objfile = cu->objfile;
0114d602 14605 int is_anonymous;
9219021c 14606
5c4e30ca
DC
14607 /* Add a symbol associated to this if we haven't seen the namespace
14608 before. Also, add a using directive if it's an anonymous
14609 namespace. */
9219021c 14610
f2f0e013 14611 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14612 {
14613 struct type *type;
14614
0114d602 14615 type = read_type_die (die, cu);
e7c27a73 14616 new_symbol (die, type, cu);
5c4e30ca 14617
e8e80198 14618 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14619 if (is_anonymous)
0114d602
DJ
14620 {
14621 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14622
eb1e02fd 14623 std::vector<const char *> excludes;
22cee43f
PMR
14624 add_using_directive (using_directives (cu->language),
14625 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 14626 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 14627 }
5c4e30ca 14628 }
9219021c 14629
639d11d3 14630 if (die->child != NULL)
d9fa45fe 14631 {
639d11d3 14632 struct die_info *child_die = die->child;
6e70227d 14633
d9fa45fe
DC
14634 while (child_die && child_die->tag)
14635 {
e7c27a73 14636 process_die (child_die, cu);
d9fa45fe
DC
14637 child_die = sibling_die (child_die);
14638 }
14639 }
38d518c9
EZ
14640}
14641
f55ee35c
JK
14642/* Read a Fortran module as type. This DIE can be only a declaration used for
14643 imported module. Still we need that type as local Fortran "use ... only"
14644 declaration imports depend on the created type in determine_prefix. */
14645
14646static struct type *
14647read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14648{
14649 struct objfile *objfile = cu->objfile;
15d034d0 14650 const char *module_name;
f55ee35c
JK
14651 struct type *type;
14652
14653 module_name = dwarf2_name (die, cu);
14654 if (!module_name)
3e43a32a
MS
14655 complaint (&symfile_complaints,
14656 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14657 to_underlying (die->sect_off));
19f392bc 14658 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14659
14660 /* determine_prefix uses TYPE_TAG_NAME. */
14661 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14662
14663 return set_die_type (die, type, cu);
14664}
14665
5d7cb8df
JK
14666/* Read a Fortran module. */
14667
14668static void
14669read_module (struct die_info *die, struct dwarf2_cu *cu)
14670{
14671 struct die_info *child_die = die->child;
530e8392
KB
14672 struct type *type;
14673
14674 type = read_type_die (die, cu);
14675 new_symbol (die, type, cu);
5d7cb8df 14676
5d7cb8df
JK
14677 while (child_die && child_die->tag)
14678 {
14679 process_die (child_die, cu);
14680 child_die = sibling_die (child_die);
14681 }
14682}
14683
38d518c9
EZ
14684/* Return the name of the namespace represented by DIE. Set
14685 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14686 namespace. */
14687
14688static const char *
e142c38c 14689namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14690{
14691 struct die_info *current_die;
14692 const char *name = NULL;
14693
14694 /* Loop through the extensions until we find a name. */
14695
14696 for (current_die = die;
14697 current_die != NULL;
f2f0e013 14698 current_die = dwarf2_extension (die, &cu))
38d518c9 14699 {
96553a0c
DE
14700 /* We don't use dwarf2_name here so that we can detect the absence
14701 of a name -> anonymous namespace. */
7d45c7c3 14702 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14703
38d518c9
EZ
14704 if (name != NULL)
14705 break;
14706 }
14707
14708 /* Is it an anonymous namespace? */
14709
14710 *is_anonymous = (name == NULL);
14711 if (*is_anonymous)
2b1dbab0 14712 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14713
14714 return name;
d9fa45fe
DC
14715}
14716
c906108c
SS
14717/* Extract all information from a DW_TAG_pointer_type DIE and add to
14718 the user defined type vector. */
14719
f792889a 14720static struct type *
e7c27a73 14721read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14722{
5e2b427d 14723 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14724 struct comp_unit_head *cu_header = &cu->header;
c906108c 14725 struct type *type;
8b2dbe47
KB
14726 struct attribute *attr_byte_size;
14727 struct attribute *attr_address_class;
14728 int byte_size, addr_class;
7e314c57
JK
14729 struct type *target_type;
14730
14731 target_type = die_type (die, cu);
c906108c 14732
7e314c57
JK
14733 /* The die_type call above may have already set the type for this DIE. */
14734 type = get_die_type (die, cu);
14735 if (type)
14736 return type;
14737
14738 type = lookup_pointer_type (target_type);
8b2dbe47 14739
e142c38c 14740 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14741 if (attr_byte_size)
14742 byte_size = DW_UNSND (attr_byte_size);
c906108c 14743 else
8b2dbe47
KB
14744 byte_size = cu_header->addr_size;
14745
e142c38c 14746 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14747 if (attr_address_class)
14748 addr_class = DW_UNSND (attr_address_class);
14749 else
14750 addr_class = DW_ADDR_none;
14751
14752 /* If the pointer size or address class is different than the
14753 default, create a type variant marked as such and set the
14754 length accordingly. */
14755 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14756 {
5e2b427d 14757 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14758 {
14759 int type_flags;
14760
849957d9 14761 type_flags = gdbarch_address_class_type_flags
5e2b427d 14762 (gdbarch, byte_size, addr_class);
876cecd0
TT
14763 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14764 == 0);
8b2dbe47
KB
14765 type = make_type_with_address_space (type, type_flags);
14766 }
14767 else if (TYPE_LENGTH (type) != byte_size)
14768 {
3e43a32a
MS
14769 complaint (&symfile_complaints,
14770 _("invalid pointer size %d"), byte_size);
8b2dbe47 14771 }
6e70227d 14772 else
9a619af0
MS
14773 {
14774 /* Should we also complain about unhandled address classes? */
14775 }
c906108c 14776 }
8b2dbe47
KB
14777
14778 TYPE_LENGTH (type) = byte_size;
f792889a 14779 return set_die_type (die, type, cu);
c906108c
SS
14780}
14781
14782/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14783 the user defined type vector. */
14784
f792889a 14785static struct type *
e7c27a73 14786read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14787{
14788 struct type *type;
14789 struct type *to_type;
14790 struct type *domain;
14791
e7c27a73
DJ
14792 to_type = die_type (die, cu);
14793 domain = die_containing_type (die, cu);
0d5de010 14794
7e314c57
JK
14795 /* The calls above may have already set the type for this DIE. */
14796 type = get_die_type (die, cu);
14797 if (type)
14798 return type;
14799
0d5de010
DJ
14800 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14801 type = lookup_methodptr_type (to_type);
7078baeb
TT
14802 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14803 {
14804 struct type *new_type = alloc_type (cu->objfile);
14805
14806 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14807 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14808 TYPE_VARARGS (to_type));
14809 type = lookup_methodptr_type (new_type);
14810 }
0d5de010
DJ
14811 else
14812 type = lookup_memberptr_type (to_type, domain);
c906108c 14813
f792889a 14814 return set_die_type (die, type, cu);
c906108c
SS
14815}
14816
4297a3f0 14817/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14818 the user defined type vector. */
14819
f792889a 14820static struct type *
4297a3f0
AV
14821read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14822 enum type_code refcode)
c906108c 14823{
e7c27a73 14824 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14825 struct type *type, *target_type;
c906108c
SS
14826 struct attribute *attr;
14827
4297a3f0
AV
14828 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14829
7e314c57
JK
14830 target_type = die_type (die, cu);
14831
14832 /* The die_type call above may have already set the type for this DIE. */
14833 type = get_die_type (die, cu);
14834 if (type)
14835 return type;
14836
4297a3f0 14837 type = lookup_reference_type (target_type, refcode);
e142c38c 14838 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14839 if (attr)
14840 {
14841 TYPE_LENGTH (type) = DW_UNSND (attr);
14842 }
14843 else
14844 {
107d2387 14845 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14846 }
f792889a 14847 return set_die_type (die, type, cu);
c906108c
SS
14848}
14849
cf363f18
MW
14850/* Add the given cv-qualifiers to the element type of the array. GCC
14851 outputs DWARF type qualifiers that apply to an array, not the
14852 element type. But GDB relies on the array element type to carry
14853 the cv-qualifiers. This mimics section 6.7.3 of the C99
14854 specification. */
14855
14856static struct type *
14857add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14858 struct type *base_type, int cnst, int voltl)
14859{
14860 struct type *el_type, *inner_array;
14861
14862 base_type = copy_type (base_type);
14863 inner_array = base_type;
14864
14865 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14866 {
14867 TYPE_TARGET_TYPE (inner_array) =
14868 copy_type (TYPE_TARGET_TYPE (inner_array));
14869 inner_array = TYPE_TARGET_TYPE (inner_array);
14870 }
14871
14872 el_type = TYPE_TARGET_TYPE (inner_array);
14873 cnst |= TYPE_CONST (el_type);
14874 voltl |= TYPE_VOLATILE (el_type);
14875 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14876
14877 return set_die_type (die, base_type, cu);
14878}
14879
f792889a 14880static struct type *
e7c27a73 14881read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14882{
f792889a 14883 struct type *base_type, *cv_type;
c906108c 14884
e7c27a73 14885 base_type = die_type (die, cu);
7e314c57
JK
14886
14887 /* The die_type call above may have already set the type for this DIE. */
14888 cv_type = get_die_type (die, cu);
14889 if (cv_type)
14890 return cv_type;
14891
2f608a3a
KW
14892 /* In case the const qualifier is applied to an array type, the element type
14893 is so qualified, not the array type (section 6.7.3 of C99). */
14894 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14895 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14896
f792889a
DJ
14897 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14898 return set_die_type (die, cv_type, cu);
c906108c
SS
14899}
14900
f792889a 14901static struct type *
e7c27a73 14902read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14903{
f792889a 14904 struct type *base_type, *cv_type;
c906108c 14905
e7c27a73 14906 base_type = die_type (die, cu);
7e314c57
JK
14907
14908 /* The die_type call above may have already set the type for this DIE. */
14909 cv_type = get_die_type (die, cu);
14910 if (cv_type)
14911 return cv_type;
14912
cf363f18
MW
14913 /* In case the volatile qualifier is applied to an array type, the
14914 element type is so qualified, not the array type (section 6.7.3
14915 of C99). */
14916 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14917 return add_array_cv_type (die, cu, base_type, 0, 1);
14918
f792889a
DJ
14919 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14920 return set_die_type (die, cv_type, cu);
c906108c
SS
14921}
14922
06d66ee9
TT
14923/* Handle DW_TAG_restrict_type. */
14924
14925static struct type *
14926read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14927{
14928 struct type *base_type, *cv_type;
14929
14930 base_type = die_type (die, cu);
14931
14932 /* The die_type call above may have already set the type for this DIE. */
14933 cv_type = get_die_type (die, cu);
14934 if (cv_type)
14935 return cv_type;
14936
14937 cv_type = make_restrict_type (base_type);
14938 return set_die_type (die, cv_type, cu);
14939}
14940
a2c2acaf
MW
14941/* Handle DW_TAG_atomic_type. */
14942
14943static struct type *
14944read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14945{
14946 struct type *base_type, *cv_type;
14947
14948 base_type = die_type (die, cu);
14949
14950 /* The die_type call above may have already set the type for this DIE. */
14951 cv_type = get_die_type (die, cu);
14952 if (cv_type)
14953 return cv_type;
14954
14955 cv_type = make_atomic_type (base_type);
14956 return set_die_type (die, cv_type, cu);
14957}
14958
c906108c
SS
14959/* Extract all information from a DW_TAG_string_type DIE and add to
14960 the user defined type vector. It isn't really a user defined type,
14961 but it behaves like one, with other DIE's using an AT_user_def_type
14962 attribute to reference it. */
14963
f792889a 14964static struct type *
e7c27a73 14965read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14966{
e7c27a73 14967 struct objfile *objfile = cu->objfile;
3b7538c0 14968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14969 struct type *type, *range_type, *index_type, *char_type;
14970 struct attribute *attr;
14971 unsigned int length;
14972
e142c38c 14973 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14974 if (attr)
14975 {
14976 length = DW_UNSND (attr);
14977 }
14978 else
14979 {
0963b4bd 14980 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14981 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14982 if (attr)
14983 {
14984 length = DW_UNSND (attr);
14985 }
14986 else
14987 {
14988 length = 1;
14989 }
c906108c 14990 }
6ccb9162 14991
46bf5051 14992 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14993 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14994 char_type = language_string_char_type (cu->language_defn, gdbarch);
14995 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14996
f792889a 14997 return set_die_type (die, type, cu);
c906108c
SS
14998}
14999
4d804846
JB
15000/* Assuming that DIE corresponds to a function, returns nonzero
15001 if the function is prototyped. */
15002
15003static int
15004prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15005{
15006 struct attribute *attr;
15007
15008 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15009 if (attr && (DW_UNSND (attr) != 0))
15010 return 1;
15011
15012 /* The DWARF standard implies that the DW_AT_prototyped attribute
15013 is only meaninful for C, but the concept also extends to other
15014 languages that allow unprototyped functions (Eg: Objective C).
15015 For all other languages, assume that functions are always
15016 prototyped. */
15017 if (cu->language != language_c
15018 && cu->language != language_objc
15019 && cu->language != language_opencl)
15020 return 1;
15021
15022 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15023 prototyped and unprototyped functions; default to prototyped,
15024 since that is more common in modern code (and RealView warns
15025 about unprototyped functions). */
15026 if (producer_is_realview (cu->producer))
15027 return 1;
15028
15029 return 0;
15030}
15031
c906108c
SS
15032/* Handle DIES due to C code like:
15033
15034 struct foo
c5aa993b
JM
15035 {
15036 int (*funcp)(int a, long l);
15037 int b;
15038 };
c906108c 15039
0963b4bd 15040 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15041
f792889a 15042static struct type *
e7c27a73 15043read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15044{
bb5ed363 15045 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15046 struct type *type; /* Type that this function returns. */
15047 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15048 struct attribute *attr;
15049
e7c27a73 15050 type = die_type (die, cu);
7e314c57
JK
15051
15052 /* The die_type call above may have already set the type for this DIE. */
15053 ftype = get_die_type (die, cu);
15054 if (ftype)
15055 return ftype;
15056
0c8b41f1 15057 ftype = lookup_function_type (type);
c906108c 15058
4d804846 15059 if (prototyped_function_p (die, cu))
a6c727b2 15060 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15061
c055b101
CV
15062 /* Store the calling convention in the type if it's available in
15063 the subroutine die. Otherwise set the calling convention to
15064 the default value DW_CC_normal. */
15065 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15066 if (attr)
15067 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15068 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15069 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15070 else
15071 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15072
743649fd
MW
15073 /* Record whether the function returns normally to its caller or not
15074 if the DWARF producer set that information. */
15075 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15076 if (attr && (DW_UNSND (attr) != 0))
15077 TYPE_NO_RETURN (ftype) = 1;
15078
76c10ea2
GM
15079 /* We need to add the subroutine type to the die immediately so
15080 we don't infinitely recurse when dealing with parameters
0963b4bd 15081 declared as the same subroutine type. */
76c10ea2 15082 set_die_type (die, ftype, cu);
6e70227d 15083
639d11d3 15084 if (die->child != NULL)
c906108c 15085 {
bb5ed363 15086 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15087 struct die_info *child_die;
8072405b 15088 int nparams, iparams;
c906108c
SS
15089
15090 /* Count the number of parameters.
15091 FIXME: GDB currently ignores vararg functions, but knows about
15092 vararg member functions. */
8072405b 15093 nparams = 0;
639d11d3 15094 child_die = die->child;
c906108c
SS
15095 while (child_die && child_die->tag)
15096 {
15097 if (child_die->tag == DW_TAG_formal_parameter)
15098 nparams++;
15099 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15100 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15101 child_die = sibling_die (child_die);
15102 }
15103
15104 /* Allocate storage for parameters and fill them in. */
15105 TYPE_NFIELDS (ftype) = nparams;
15106 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15107 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15108
8072405b
JK
15109 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15110 even if we error out during the parameters reading below. */
15111 for (iparams = 0; iparams < nparams; iparams++)
15112 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15113
15114 iparams = 0;
639d11d3 15115 child_die = die->child;
c906108c
SS
15116 while (child_die && child_die->tag)
15117 {
15118 if (child_die->tag == DW_TAG_formal_parameter)
15119 {
3ce3b1ba
PA
15120 struct type *arg_type;
15121
15122 /* DWARF version 2 has no clean way to discern C++
15123 static and non-static member functions. G++ helps
15124 GDB by marking the first parameter for non-static
15125 member functions (which is the this pointer) as
15126 artificial. We pass this information to
15127 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15128
15129 DWARF version 3 added DW_AT_object_pointer, which GCC
15130 4.5 does not yet generate. */
e142c38c 15131 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15132 if (attr)
15133 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15134 else
9c37b5ae 15135 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15136 arg_type = die_type (child_die, cu);
15137
15138 /* RealView does not mark THIS as const, which the testsuite
15139 expects. GCC marks THIS as const in method definitions,
15140 but not in the class specifications (GCC PR 43053). */
15141 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15142 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15143 {
15144 int is_this = 0;
15145 struct dwarf2_cu *arg_cu = cu;
15146 const char *name = dwarf2_name (child_die, cu);
15147
15148 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15149 if (attr)
15150 {
15151 /* If the compiler emits this, use it. */
15152 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15153 is_this = 1;
15154 }
15155 else if (name && strcmp (name, "this") == 0)
15156 /* Function definitions will have the argument names. */
15157 is_this = 1;
15158 else if (name == NULL && iparams == 0)
15159 /* Declarations may not have the names, so like
15160 elsewhere in GDB, assume an artificial first
15161 argument is "this". */
15162 is_this = 1;
15163
15164 if (is_this)
15165 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15166 arg_type, 0);
15167 }
15168
15169 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15170 iparams++;
15171 }
15172 child_die = sibling_die (child_die);
15173 }
15174 }
15175
76c10ea2 15176 return ftype;
c906108c
SS
15177}
15178
f792889a 15179static struct type *
e7c27a73 15180read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15181{
e7c27a73 15182 struct objfile *objfile = cu->objfile;
0114d602 15183 const char *name = NULL;
3c8e0968 15184 struct type *this_type, *target_type;
c906108c 15185
94af9270 15186 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15187 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15188 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15189 set_die_type (die, this_type, cu);
3c8e0968
DE
15190 target_type = die_type (die, cu);
15191 if (target_type != this_type)
15192 TYPE_TARGET_TYPE (this_type) = target_type;
15193 else
15194 {
15195 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15196 spec and cause infinite loops in GDB. */
15197 complaint (&symfile_complaints,
15198 _("Self-referential DW_TAG_typedef "
15199 "- DIE at 0x%x [in module %s]"),
9c541725 15200 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15201 TYPE_TARGET_TYPE (this_type) = NULL;
15202 }
f792889a 15203 return this_type;
c906108c
SS
15204}
15205
9b790ce7
UW
15206/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15207 (which may be different from NAME) to the architecture back-end to allow
15208 it to guess the correct format if necessary. */
15209
15210static struct type *
15211dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15212 const char *name_hint)
15213{
15214 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15215 const struct floatformat **format;
15216 struct type *type;
15217
15218 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15219 if (format)
15220 type = init_float_type (objfile, bits, name, format);
15221 else
77b7c781 15222 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
15223
15224 return type;
15225}
15226
c906108c
SS
15227/* Find a representation of a given base type and install
15228 it in the TYPE field of the die. */
15229
f792889a 15230static struct type *
e7c27a73 15231read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15232{
e7c27a73 15233 struct objfile *objfile = cu->objfile;
c906108c
SS
15234 struct type *type;
15235 struct attribute *attr;
19f392bc 15236 int encoding = 0, bits = 0;
15d034d0 15237 const char *name;
c906108c 15238
e142c38c 15239 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15240 if (attr)
15241 {
15242 encoding = DW_UNSND (attr);
15243 }
e142c38c 15244 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15245 if (attr)
15246 {
19f392bc 15247 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15248 }
39cbfefa 15249 name = dwarf2_name (die, cu);
6ccb9162 15250 if (!name)
c906108c 15251 {
6ccb9162
UW
15252 complaint (&symfile_complaints,
15253 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15254 }
6ccb9162
UW
15255
15256 switch (encoding)
c906108c 15257 {
6ccb9162
UW
15258 case DW_ATE_address:
15259 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 15260 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 15261 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15262 break;
15263 case DW_ATE_boolean:
19f392bc 15264 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15265 break;
15266 case DW_ATE_complex_float:
9b790ce7 15267 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15268 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15269 break;
15270 case DW_ATE_decimal_float:
19f392bc 15271 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15272 break;
15273 case DW_ATE_float:
9b790ce7 15274 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15275 break;
15276 case DW_ATE_signed:
19f392bc 15277 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15278 break;
15279 case DW_ATE_unsigned:
3b2b8fea
TT
15280 if (cu->language == language_fortran
15281 && name
61012eef 15282 && startswith (name, "character("))
19f392bc
UW
15283 type = init_character_type (objfile, bits, 1, name);
15284 else
15285 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15286 break;
15287 case DW_ATE_signed_char:
6e70227d 15288 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15289 || cu->language == language_pascal
15290 || cu->language == language_fortran)
19f392bc
UW
15291 type = init_character_type (objfile, bits, 0, name);
15292 else
15293 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15294 break;
15295 case DW_ATE_unsigned_char:
868a0084 15296 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15297 || cu->language == language_pascal
c44af4eb
TT
15298 || cu->language == language_fortran
15299 || cu->language == language_rust)
19f392bc
UW
15300 type = init_character_type (objfile, bits, 1, name);
15301 else
15302 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15303 break;
75079b2b 15304 case DW_ATE_UTF:
53e710ac
PA
15305 {
15306 gdbarch *arch = get_objfile_arch (objfile);
15307
15308 if (bits == 16)
15309 type = builtin_type (arch)->builtin_char16;
15310 else if (bits == 32)
15311 type = builtin_type (arch)->builtin_char32;
15312 else
15313 {
15314 complaint (&symfile_complaints,
15315 _("unsupported DW_ATE_UTF bit size: '%d'"),
15316 bits);
15317 type = init_integer_type (objfile, bits, 1, name);
15318 }
15319 return set_die_type (die, type, cu);
15320 }
75079b2b
TT
15321 break;
15322
6ccb9162
UW
15323 default:
15324 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15325 dwarf_type_encoding_name (encoding));
77b7c781 15326 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 15327 break;
c906108c 15328 }
6ccb9162 15329
0114d602 15330 if (name && strcmp (name, "char") == 0)
876cecd0 15331 TYPE_NOSIGN (type) = 1;
0114d602 15332
f792889a 15333 return set_die_type (die, type, cu);
c906108c
SS
15334}
15335
80180f79
SA
15336/* Parse dwarf attribute if it's a block, reference or constant and put the
15337 resulting value of the attribute into struct bound_prop.
15338 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15339
15340static int
15341attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15342 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15343{
15344 struct dwarf2_property_baton *baton;
15345 struct obstack *obstack = &cu->objfile->objfile_obstack;
15346
15347 if (attr == NULL || prop == NULL)
15348 return 0;
15349
15350 if (attr_form_is_block (attr))
15351 {
8d749320 15352 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15353 baton->referenced_type = NULL;
15354 baton->locexpr.per_cu = cu->per_cu;
15355 baton->locexpr.size = DW_BLOCK (attr)->size;
15356 baton->locexpr.data = DW_BLOCK (attr)->data;
15357 prop->data.baton = baton;
15358 prop->kind = PROP_LOCEXPR;
15359 gdb_assert (prop->data.baton != NULL);
15360 }
15361 else if (attr_form_is_ref (attr))
15362 {
15363 struct dwarf2_cu *target_cu = cu;
15364 struct die_info *target_die;
15365 struct attribute *target_attr;
15366
15367 target_die = follow_die_ref (die, attr, &target_cu);
15368 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15369 if (target_attr == NULL)
15370 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15371 target_cu);
80180f79
SA
15372 if (target_attr == NULL)
15373 return 0;
15374
df25ebbd 15375 switch (target_attr->name)
80180f79 15376 {
df25ebbd
JB
15377 case DW_AT_location:
15378 if (attr_form_is_section_offset (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 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15383 prop->data.baton = baton;
15384 prop->kind = PROP_LOCLIST;
15385 gdb_assert (prop->data.baton != NULL);
15386 }
15387 else if (attr_form_is_block (target_attr))
15388 {
8d749320 15389 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15390 baton->referenced_type = die_type (target_die, target_cu);
15391 baton->locexpr.per_cu = cu->per_cu;
15392 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15393 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15394 prop->data.baton = baton;
15395 prop->kind = PROP_LOCEXPR;
15396 gdb_assert (prop->data.baton != NULL);
15397 }
15398 else
15399 {
15400 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15401 "dynamic property");
15402 return 0;
15403 }
15404 break;
15405 case DW_AT_data_member_location:
15406 {
15407 LONGEST offset;
15408
15409 if (!handle_data_member_location (target_die, target_cu,
15410 &offset))
15411 return 0;
15412
8d749320 15413 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15414 baton->referenced_type = read_type_die (target_die->parent,
15415 target_cu);
df25ebbd
JB
15416 baton->offset_info.offset = offset;
15417 baton->offset_info.type = die_type (target_die, target_cu);
15418 prop->data.baton = baton;
15419 prop->kind = PROP_ADDR_OFFSET;
15420 break;
15421 }
80180f79
SA
15422 }
15423 }
15424 else if (attr_form_is_constant (attr))
15425 {
15426 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15427 prop->kind = PROP_CONST;
15428 }
15429 else
15430 {
15431 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15432 dwarf2_name (die, cu));
15433 return 0;
15434 }
15435
15436 return 1;
15437}
15438
a02abb62
JB
15439/* Read the given DW_AT_subrange DIE. */
15440
f792889a 15441static struct type *
a02abb62
JB
15442read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15443{
4c9ad8c2 15444 struct type *base_type, *orig_base_type;
a02abb62
JB
15445 struct type *range_type;
15446 struct attribute *attr;
729efb13 15447 struct dynamic_prop low, high;
4fae6e18 15448 int low_default_is_valid;
c451ebe5 15449 int high_bound_is_count = 0;
15d034d0 15450 const char *name;
43bbcdc2 15451 LONGEST negative_mask;
e77813c8 15452
4c9ad8c2
TT
15453 orig_base_type = die_type (die, cu);
15454 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15455 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15456 creating the range type, but we use the result of check_typedef
15457 when examining properties of the type. */
15458 base_type = check_typedef (orig_base_type);
a02abb62 15459
7e314c57
JK
15460 /* The die_type call above may have already set the type for this DIE. */
15461 range_type = get_die_type (die, cu);
15462 if (range_type)
15463 return range_type;
15464
729efb13
SA
15465 low.kind = PROP_CONST;
15466 high.kind = PROP_CONST;
15467 high.data.const_val = 0;
15468
4fae6e18
JK
15469 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15470 omitting DW_AT_lower_bound. */
15471 switch (cu->language)
6e70227d 15472 {
4fae6e18
JK
15473 case language_c:
15474 case language_cplus:
729efb13 15475 low.data.const_val = 0;
4fae6e18
JK
15476 low_default_is_valid = 1;
15477 break;
15478 case language_fortran:
729efb13 15479 low.data.const_val = 1;
4fae6e18
JK
15480 low_default_is_valid = 1;
15481 break;
15482 case language_d:
4fae6e18 15483 case language_objc:
c44af4eb 15484 case language_rust:
729efb13 15485 low.data.const_val = 0;
4fae6e18
JK
15486 low_default_is_valid = (cu->header.version >= 4);
15487 break;
15488 case language_ada:
15489 case language_m2:
15490 case language_pascal:
729efb13 15491 low.data.const_val = 1;
4fae6e18
JK
15492 low_default_is_valid = (cu->header.version >= 4);
15493 break;
15494 default:
729efb13 15495 low.data.const_val = 0;
4fae6e18
JK
15496 low_default_is_valid = 0;
15497 break;
a02abb62
JB
15498 }
15499
e142c38c 15500 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15501 if (attr)
11c1ba78 15502 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15503 else if (!low_default_is_valid)
15504 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15505 "- DIE at 0x%x [in module %s]"),
9c541725 15506 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15507
e142c38c 15508 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15509 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15510 {
15511 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15512 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15513 {
c451ebe5
SA
15514 /* If bounds are constant do the final calculation here. */
15515 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15516 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15517 else
15518 high_bound_is_count = 1;
c2ff108b 15519 }
e77813c8
PM
15520 }
15521
15522 /* Dwarf-2 specifications explicitly allows to create subrange types
15523 without specifying a base type.
15524 In that case, the base type must be set to the type of
15525 the lower bound, upper bound or count, in that order, if any of these
15526 three attributes references an object that has a type.
15527 If no base type is found, the Dwarf-2 specifications say that
15528 a signed integer type of size equal to the size of an address should
15529 be used.
15530 For the following C code: `extern char gdb_int [];'
15531 GCC produces an empty range DIE.
15532 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15533 high bound or count are not yet handled by this code. */
e77813c8
PM
15534 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15535 {
15536 struct objfile *objfile = cu->objfile;
15537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15538 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15539 struct type *int_type = objfile_type (objfile)->builtin_int;
15540
15541 /* Test "int", "long int", and "long long int" objfile types,
15542 and select the first one having a size above or equal to the
15543 architecture address size. */
15544 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15545 base_type = int_type;
15546 else
15547 {
15548 int_type = objfile_type (objfile)->builtin_long;
15549 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15550 base_type = int_type;
15551 else
15552 {
15553 int_type = objfile_type (objfile)->builtin_long_long;
15554 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15555 base_type = int_type;
15556 }
15557 }
15558 }
a02abb62 15559
dbb9c2b1
JB
15560 /* Normally, the DWARF producers are expected to use a signed
15561 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15562 But this is unfortunately not always the case, as witnessed
15563 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15564 is used instead. To work around that ambiguity, we treat
15565 the bounds as signed, and thus sign-extend their values, when
15566 the base type is signed. */
6e70227d 15567 negative_mask =
66c6502d 15568 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15569 if (low.kind == PROP_CONST
15570 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15571 low.data.const_val |= negative_mask;
15572 if (high.kind == PROP_CONST
15573 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15574 high.data.const_val |= negative_mask;
43bbcdc2 15575
729efb13 15576 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15577
c451ebe5
SA
15578 if (high_bound_is_count)
15579 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15580
c2ff108b
JK
15581 /* Ada expects an empty array on no boundary attributes. */
15582 if (attr == NULL && cu->language != language_ada)
729efb13 15583 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15584
39cbfefa
DJ
15585 name = dwarf2_name (die, cu);
15586 if (name)
15587 TYPE_NAME (range_type) = name;
6e70227d 15588
e142c38c 15589 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15590 if (attr)
15591 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15592
7e314c57
JK
15593 set_die_type (die, range_type, cu);
15594
15595 /* set_die_type should be already done. */
b4ba55a1
JB
15596 set_descriptive_type (range_type, die, cu);
15597
7e314c57 15598 return range_type;
a02abb62 15599}
6e70227d 15600
f792889a 15601static struct type *
81a17f79
JB
15602read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15603{
15604 struct type *type;
81a17f79 15605
81a17f79
JB
15606 /* For now, we only support the C meaning of an unspecified type: void. */
15607
19f392bc 15608 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15609 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15610
f792889a 15611 return set_die_type (die, type, cu);
81a17f79 15612}
a02abb62 15613
639d11d3
DC
15614/* Read a single die and all its descendents. Set the die's sibling
15615 field to NULL; set other fields in the die correctly, and set all
15616 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15617 location of the info_ptr after reading all of those dies. PARENT
15618 is the parent of the die in question. */
15619
15620static struct die_info *
dee91e82 15621read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15622 const gdb_byte *info_ptr,
15623 const gdb_byte **new_info_ptr,
dee91e82 15624 struct die_info *parent)
639d11d3
DC
15625{
15626 struct die_info *die;
d521ce57 15627 const gdb_byte *cur_ptr;
639d11d3
DC
15628 int has_children;
15629
bf6af496 15630 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15631 if (die == NULL)
15632 {
15633 *new_info_ptr = cur_ptr;
15634 return NULL;
15635 }
93311388 15636 store_in_ref_table (die, reader->cu);
639d11d3
DC
15637
15638 if (has_children)
bf6af496 15639 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15640 else
15641 {
15642 die->child = NULL;
15643 *new_info_ptr = cur_ptr;
15644 }
15645
15646 die->sibling = NULL;
15647 die->parent = parent;
15648 return die;
15649}
15650
15651/* Read a die, all of its descendents, and all of its siblings; set
15652 all of the fields of all of the dies correctly. Arguments are as
15653 in read_die_and_children. */
15654
15655static struct die_info *
bf6af496 15656read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15657 const gdb_byte *info_ptr,
15658 const gdb_byte **new_info_ptr,
bf6af496 15659 struct die_info *parent)
639d11d3
DC
15660{
15661 struct die_info *first_die, *last_sibling;
d521ce57 15662 const gdb_byte *cur_ptr;
639d11d3 15663
c906108c 15664 cur_ptr = info_ptr;
639d11d3
DC
15665 first_die = last_sibling = NULL;
15666
15667 while (1)
c906108c 15668 {
639d11d3 15669 struct die_info *die
dee91e82 15670 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15671
1d325ec1 15672 if (die == NULL)
c906108c 15673 {
639d11d3
DC
15674 *new_info_ptr = cur_ptr;
15675 return first_die;
c906108c 15676 }
1d325ec1
DJ
15677
15678 if (!first_die)
15679 first_die = die;
c906108c 15680 else
1d325ec1
DJ
15681 last_sibling->sibling = die;
15682
15683 last_sibling = die;
c906108c 15684 }
c906108c
SS
15685}
15686
bf6af496
DE
15687/* Read a die, all of its descendents, and all of its siblings; set
15688 all of the fields of all of the dies correctly. Arguments are as
15689 in read_die_and_children.
15690 This the main entry point for reading a DIE and all its children. */
15691
15692static struct die_info *
15693read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15694 const gdb_byte *info_ptr,
15695 const gdb_byte **new_info_ptr,
bf6af496
DE
15696 struct die_info *parent)
15697{
15698 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15699 new_info_ptr, parent);
15700
b4f54984 15701 if (dwarf_die_debug)
bf6af496
DE
15702 {
15703 fprintf_unfiltered (gdb_stdlog,
15704 "Read die from %s@0x%x of %s:\n",
a32a8923 15705 get_section_name (reader->die_section),
bf6af496
DE
15706 (unsigned) (info_ptr - reader->die_section->buffer),
15707 bfd_get_filename (reader->abfd));
b4f54984 15708 dump_die (die, dwarf_die_debug);
bf6af496
DE
15709 }
15710
15711 return die;
15712}
15713
3019eac3
DE
15714/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15715 attributes.
15716 The caller is responsible for filling in the extra attributes
15717 and updating (*DIEP)->num_attrs.
15718 Set DIEP to point to a newly allocated die with its information,
15719 except for its child, sibling, and parent fields.
15720 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15721
d521ce57 15722static const gdb_byte *
3019eac3 15723read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15724 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15725 int *has_children, int num_extra_attrs)
93311388 15726{
b64f50a1 15727 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15728 struct abbrev_info *abbrev;
15729 struct die_info *die;
15730 struct dwarf2_cu *cu = reader->cu;
15731 bfd *abfd = reader->abfd;
15732
9c541725 15733 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15734 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15735 info_ptr += bytes_read;
15736 if (!abbrev_number)
15737 {
15738 *diep = NULL;
15739 *has_children = 0;
15740 return info_ptr;
15741 }
15742
433df2d4 15743 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15744 if (!abbrev)
348e048f
DE
15745 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15746 abbrev_number,
15747 bfd_get_filename (abfd));
15748
3019eac3 15749 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15750 die->sect_off = sect_off;
93311388
DE
15751 die->tag = abbrev->tag;
15752 die->abbrev = abbrev_number;
15753
3019eac3
DE
15754 /* Make the result usable.
15755 The caller needs to update num_attrs after adding the extra
15756 attributes. */
93311388
DE
15757 die->num_attrs = abbrev->num_attrs;
15758
15759 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15760 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15761 info_ptr);
93311388
DE
15762
15763 *diep = die;
15764 *has_children = abbrev->has_children;
15765 return info_ptr;
15766}
15767
3019eac3
DE
15768/* Read a die and all its attributes.
15769 Set DIEP to point to a newly allocated die with its information,
15770 except for its child, sibling, and parent fields.
15771 Set HAS_CHILDREN to tell whether the die has children or not. */
15772
d521ce57 15773static const gdb_byte *
3019eac3 15774read_full_die (const struct die_reader_specs *reader,
d521ce57 15775 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15776 int *has_children)
15777{
d521ce57 15778 const gdb_byte *result;
bf6af496
DE
15779
15780 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15781
b4f54984 15782 if (dwarf_die_debug)
bf6af496
DE
15783 {
15784 fprintf_unfiltered (gdb_stdlog,
15785 "Read die from %s@0x%x of %s:\n",
a32a8923 15786 get_section_name (reader->die_section),
bf6af496
DE
15787 (unsigned) (info_ptr - reader->die_section->buffer),
15788 bfd_get_filename (reader->abfd));
b4f54984 15789 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15790 }
15791
15792 return result;
3019eac3 15793}
433df2d4
DE
15794\f
15795/* Abbreviation tables.
3019eac3 15796
433df2d4 15797 In DWARF version 2, the description of the debugging information is
c906108c
SS
15798 stored in a separate .debug_abbrev section. Before we read any
15799 dies from a section we read in all abbreviations and install them
433df2d4
DE
15800 in a hash table. */
15801
15802/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15803
15804static struct abbrev_info *
15805abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15806{
15807 struct abbrev_info *abbrev;
15808
8d749320 15809 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15810 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15811
433df2d4
DE
15812 return abbrev;
15813}
15814
15815/* Add an abbreviation to the table. */
c906108c
SS
15816
15817static void
433df2d4
DE
15818abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15819 unsigned int abbrev_number,
15820 struct abbrev_info *abbrev)
15821{
15822 unsigned int hash_number;
15823
15824 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15825 abbrev->next = abbrev_table->abbrevs[hash_number];
15826 abbrev_table->abbrevs[hash_number] = abbrev;
15827}
dee91e82 15828
433df2d4
DE
15829/* Look up an abbrev in the table.
15830 Returns NULL if the abbrev is not found. */
15831
15832static struct abbrev_info *
15833abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15834 unsigned int abbrev_number)
c906108c 15835{
433df2d4
DE
15836 unsigned int hash_number;
15837 struct abbrev_info *abbrev;
15838
15839 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15840 abbrev = abbrev_table->abbrevs[hash_number];
15841
15842 while (abbrev)
15843 {
15844 if (abbrev->number == abbrev_number)
15845 return abbrev;
15846 abbrev = abbrev->next;
15847 }
15848 return NULL;
15849}
15850
15851/* Read in an abbrev table. */
15852
15853static struct abbrev_table *
15854abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15855 sect_offset sect_off)
433df2d4
DE
15856{
15857 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15858 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15859 struct abbrev_table *abbrev_table;
d521ce57 15860 const gdb_byte *abbrev_ptr;
c906108c
SS
15861 struct abbrev_info *cur_abbrev;
15862 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15863 unsigned int abbrev_form;
f3dd6933
DJ
15864 struct attr_abbrev *cur_attrs;
15865 unsigned int allocated_attrs;
c906108c 15866
70ba0933 15867 abbrev_table = XNEW (struct abbrev_table);
9c541725 15868 abbrev_table->sect_off = sect_off;
433df2d4 15869 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15870 abbrev_table->abbrevs =
15871 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15872 ABBREV_HASH_SIZE);
433df2d4
DE
15873 memset (abbrev_table->abbrevs, 0,
15874 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15875
433df2d4 15876 dwarf2_read_section (objfile, section);
9c541725 15877 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15878 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15879 abbrev_ptr += bytes_read;
15880
f3dd6933 15881 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15882 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15883
0963b4bd 15884 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15885 while (abbrev_number)
15886 {
433df2d4 15887 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15888
15889 /* read in abbrev header */
15890 cur_abbrev->number = abbrev_number;
aead7601
SM
15891 cur_abbrev->tag
15892 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15893 abbrev_ptr += bytes_read;
15894 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15895 abbrev_ptr += 1;
15896
15897 /* now read in declarations */
22d2f3ab 15898 for (;;)
c906108c 15899 {
43988095
JK
15900 LONGEST implicit_const;
15901
22d2f3ab
JK
15902 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15903 abbrev_ptr += bytes_read;
15904 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15905 abbrev_ptr += bytes_read;
43988095
JK
15906 if (abbrev_form == DW_FORM_implicit_const)
15907 {
15908 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15909 &bytes_read);
15910 abbrev_ptr += bytes_read;
15911 }
15912 else
15913 {
15914 /* Initialize it due to a false compiler warning. */
15915 implicit_const = -1;
15916 }
22d2f3ab
JK
15917
15918 if (abbrev_name == 0)
15919 break;
15920
f3dd6933 15921 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15922 {
f3dd6933
DJ
15923 allocated_attrs += ATTR_ALLOC_CHUNK;
15924 cur_attrs
224c3ddb 15925 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15926 }
ae038cb0 15927
aead7601
SM
15928 cur_attrs[cur_abbrev->num_attrs].name
15929 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15930 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15931 = (enum dwarf_form) abbrev_form;
43988095 15932 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15933 ++cur_abbrev->num_attrs;
c906108c
SS
15934 }
15935
8d749320
SM
15936 cur_abbrev->attrs =
15937 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15938 cur_abbrev->num_attrs);
f3dd6933
DJ
15939 memcpy (cur_abbrev->attrs, cur_attrs,
15940 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15941
433df2d4 15942 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15943
15944 /* Get next abbreviation.
15945 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15946 always properly terminated with an abbrev number of 0.
15947 Exit loop if we encounter an abbreviation which we have
15948 already read (which means we are about to read the abbreviations
15949 for the next compile unit) or if the end of the abbreviation
15950 table is reached. */
433df2d4 15951 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15952 break;
15953 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15954 abbrev_ptr += bytes_read;
433df2d4 15955 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15956 break;
15957 }
f3dd6933
DJ
15958
15959 xfree (cur_attrs);
433df2d4 15960 return abbrev_table;
c906108c
SS
15961}
15962
433df2d4 15963/* Free the resources held by ABBREV_TABLE. */
c906108c 15964
c906108c 15965static void
433df2d4 15966abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15967{
433df2d4
DE
15968 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15969 xfree (abbrev_table);
c906108c
SS
15970}
15971
f4dc4d17
DE
15972/* Same as abbrev_table_free but as a cleanup.
15973 We pass in a pointer to the pointer to the table so that we can
15974 set the pointer to NULL when we're done. It also simplifies
73051182 15975 build_type_psymtabs_1. */
f4dc4d17
DE
15976
15977static void
15978abbrev_table_free_cleanup (void *table_ptr)
15979{
9a3c8263 15980 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15981
15982 if (*abbrev_table_ptr != NULL)
15983 abbrev_table_free (*abbrev_table_ptr);
15984 *abbrev_table_ptr = NULL;
15985}
15986
433df2d4
DE
15987/* Read the abbrev table for CU from ABBREV_SECTION. */
15988
15989static void
15990dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15991 struct dwarf2_section_info *abbrev_section)
c906108c 15992{
433df2d4 15993 cu->abbrev_table =
9c541725 15994 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15995}
c906108c 15996
433df2d4 15997/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15998
433df2d4
DE
15999static void
16000dwarf2_free_abbrev_table (void *ptr_to_cu)
16001{
9a3c8263 16002 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16003
a2ce51a0
DE
16004 if (cu->abbrev_table != NULL)
16005 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16006 /* Set this to NULL so that we SEGV if we try to read it later,
16007 and also because free_comp_unit verifies this is NULL. */
16008 cu->abbrev_table = NULL;
16009}
16010\f
72bf9492
DJ
16011/* Returns nonzero if TAG represents a type that we might generate a partial
16012 symbol for. */
16013
16014static int
16015is_type_tag_for_partial (int tag)
16016{
16017 switch (tag)
16018 {
16019#if 0
16020 /* Some types that would be reasonable to generate partial symbols for,
16021 that we don't at present. */
16022 case DW_TAG_array_type:
16023 case DW_TAG_file_type:
16024 case DW_TAG_ptr_to_member_type:
16025 case DW_TAG_set_type:
16026 case DW_TAG_string_type:
16027 case DW_TAG_subroutine_type:
16028#endif
16029 case DW_TAG_base_type:
16030 case DW_TAG_class_type:
680b30c7 16031 case DW_TAG_interface_type:
72bf9492
DJ
16032 case DW_TAG_enumeration_type:
16033 case DW_TAG_structure_type:
16034 case DW_TAG_subrange_type:
16035 case DW_TAG_typedef:
16036 case DW_TAG_union_type:
16037 return 1;
16038 default:
16039 return 0;
16040 }
16041}
16042
16043/* Load all DIEs that are interesting for partial symbols into memory. */
16044
16045static struct partial_die_info *
dee91e82 16046load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16047 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16048{
dee91e82 16049 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16050 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16051 struct partial_die_info *part_die;
16052 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16053 struct abbrev_info *abbrev;
16054 unsigned int bytes_read;
5afb4e99 16055 unsigned int load_all = 0;
72bf9492
DJ
16056 int nesting_level = 1;
16057
16058 parent_die = NULL;
16059 last_die = NULL;
16060
7adf1e79
DE
16061 gdb_assert (cu->per_cu != NULL);
16062 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16063 load_all = 1;
16064
72bf9492
DJ
16065 cu->partial_dies
16066 = htab_create_alloc_ex (cu->header.length / 12,
16067 partial_die_hash,
16068 partial_die_eq,
16069 NULL,
16070 &cu->comp_unit_obstack,
16071 hashtab_obstack_allocate,
16072 dummy_obstack_deallocate);
16073
8d749320 16074 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16075
16076 while (1)
16077 {
16078 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16079
16080 /* A NULL abbrev means the end of a series of children. */
16081 if (abbrev == NULL)
16082 {
16083 if (--nesting_level == 0)
16084 {
16085 /* PART_DIE was probably the last thing allocated on the
16086 comp_unit_obstack, so we could call obstack_free
16087 here. We don't do that because the waste is small,
16088 and will be cleaned up when we're done with this
16089 compilation unit. This way, we're also more robust
16090 against other users of the comp_unit_obstack. */
16091 return first_die;
16092 }
16093 info_ptr += bytes_read;
16094 last_die = parent_die;
16095 parent_die = parent_die->die_parent;
16096 continue;
16097 }
16098
98bfdba5
PA
16099 /* Check for template arguments. We never save these; if
16100 they're seen, we just mark the parent, and go on our way. */
16101 if (parent_die != NULL
16102 && cu->language == language_cplus
16103 && (abbrev->tag == DW_TAG_template_type_param
16104 || abbrev->tag == DW_TAG_template_value_param))
16105 {
16106 parent_die->has_template_arguments = 1;
16107
16108 if (!load_all)
16109 {
16110 /* We don't need a partial DIE for the template argument. */
dee91e82 16111 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16112 continue;
16113 }
16114 }
16115
0d99eb77 16116 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16117 Skip their other children. */
16118 if (!load_all
16119 && cu->language == language_cplus
16120 && parent_die != NULL
16121 && parent_die->tag == DW_TAG_subprogram)
16122 {
dee91e82 16123 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16124 continue;
16125 }
16126
5afb4e99
DJ
16127 /* Check whether this DIE is interesting enough to save. Normally
16128 we would not be interested in members here, but there may be
16129 later variables referencing them via DW_AT_specification (for
16130 static members). */
16131 if (!load_all
16132 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16133 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16134 && abbrev->tag != DW_TAG_enumerator
16135 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16136 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16137 && abbrev->tag != DW_TAG_variable
5afb4e99 16138 && abbrev->tag != DW_TAG_namespace
f55ee35c 16139 && abbrev->tag != DW_TAG_module
95554aad 16140 && abbrev->tag != DW_TAG_member
74921315
KS
16141 && abbrev->tag != DW_TAG_imported_unit
16142 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16143 {
16144 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16145 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16146 continue;
16147 }
16148
dee91e82
DE
16149 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16150 info_ptr);
72bf9492
DJ
16151
16152 /* This two-pass algorithm for processing partial symbols has a
16153 high cost in cache pressure. Thus, handle some simple cases
16154 here which cover the majority of C partial symbols. DIEs
16155 which neither have specification tags in them, nor could have
16156 specification tags elsewhere pointing at them, can simply be
16157 processed and discarded.
16158
16159 This segment is also optional; scan_partial_symbols and
16160 add_partial_symbol will handle these DIEs if we chain
16161 them in normally. When compilers which do not emit large
16162 quantities of duplicate debug information are more common,
16163 this code can probably be removed. */
16164
16165 /* Any complete simple types at the top level (pretty much all
16166 of them, for a language without namespaces), can be processed
16167 directly. */
16168 if (parent_die == NULL
16169 && part_die->has_specification == 0
16170 && part_die->is_declaration == 0
d8228535 16171 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16172 || part_die->tag == DW_TAG_base_type
16173 || part_die->tag == DW_TAG_subrange_type))
16174 {
16175 if (building_psymtab && part_die->name != NULL)
04a679b8 16176 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16177 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16178 &objfile->static_psymbols,
1762568f 16179 0, cu->language, objfile);
dee91e82 16180 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16181 continue;
16182 }
16183
d8228535
JK
16184 /* The exception for DW_TAG_typedef with has_children above is
16185 a workaround of GCC PR debug/47510. In the case of this complaint
16186 type_name_no_tag_or_error will error on such types later.
16187
16188 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16189 it could not find the child DIEs referenced later, this is checked
16190 above. In correct DWARF DW_TAG_typedef should have no children. */
16191
16192 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16193 complaint (&symfile_complaints,
16194 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16195 "- DIE at 0x%x [in module %s]"),
9c541725 16196 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16197
72bf9492
DJ
16198 /* If we're at the second level, and we're an enumerator, and
16199 our parent has no specification (meaning possibly lives in a
16200 namespace elsewhere), then we can add the partial symbol now
16201 instead of queueing it. */
16202 if (part_die->tag == DW_TAG_enumerator
16203 && parent_die != NULL
16204 && parent_die->die_parent == NULL
16205 && parent_die->tag == DW_TAG_enumeration_type
16206 && parent_die->has_specification == 0)
16207 {
16208 if (part_die->name == NULL)
3e43a32a
MS
16209 complaint (&symfile_complaints,
16210 _("malformed enumerator DIE ignored"));
72bf9492 16211 else if (building_psymtab)
04a679b8 16212 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16213 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16214 cu->language == language_cplus
bb5ed363
DE
16215 ? &objfile->global_psymbols
16216 : &objfile->static_psymbols,
1762568f 16217 0, cu->language, objfile);
72bf9492 16218
dee91e82 16219 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16220 continue;
16221 }
16222
16223 /* We'll save this DIE so link it in. */
16224 part_die->die_parent = parent_die;
16225 part_die->die_sibling = NULL;
16226 part_die->die_child = NULL;
16227
16228 if (last_die && last_die == parent_die)
16229 last_die->die_child = part_die;
16230 else if (last_die)
16231 last_die->die_sibling = part_die;
16232
16233 last_die = part_die;
16234
16235 if (first_die == NULL)
16236 first_die = part_die;
16237
16238 /* Maybe add the DIE to the hash table. Not all DIEs that we
16239 find interesting need to be in the hash table, because we
16240 also have the parent/sibling/child chains; only those that we
16241 might refer to by offset later during partial symbol reading.
16242
16243 For now this means things that might have be the target of a
16244 DW_AT_specification, DW_AT_abstract_origin, or
16245 DW_AT_extension. DW_AT_extension will refer only to
16246 namespaces; DW_AT_abstract_origin refers to functions (and
16247 many things under the function DIE, but we do not recurse
16248 into function DIEs during partial symbol reading) and
16249 possibly variables as well; DW_AT_specification refers to
16250 declarations. Declarations ought to have the DW_AT_declaration
16251 flag. It happens that GCC forgets to put it in sometimes, but
16252 only for functions, not for types.
16253
16254 Adding more things than necessary to the hash table is harmless
16255 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16256 wasted time in find_partial_die, when we reread the compilation
16257 unit with load_all_dies set. */
72bf9492 16258
5afb4e99 16259 if (load_all
72929c62 16260 || abbrev->tag == DW_TAG_constant
5afb4e99 16261 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16262 || abbrev->tag == DW_TAG_variable
16263 || abbrev->tag == DW_TAG_namespace
16264 || part_die->is_declaration)
16265 {
16266 void **slot;
16267
16268 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16269 to_underlying (part_die->sect_off),
16270 INSERT);
72bf9492
DJ
16271 *slot = part_die;
16272 }
16273
8d749320 16274 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16275
16276 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16277 we have no reason to follow the children of structures; for other
98bfdba5
PA
16278 languages we have to, so that we can get at method physnames
16279 to infer fully qualified class names, for DW_AT_specification,
16280 and for C++ template arguments. For C++, we also look one level
16281 inside functions to find template arguments (if the name of the
16282 function does not already contain the template arguments).
bc30ff58
JB
16283
16284 For Ada, we need to scan the children of subprograms and lexical
16285 blocks as well because Ada allows the definition of nested
16286 entities that could be interesting for the debugger, such as
16287 nested subprograms for instance. */
72bf9492 16288 if (last_die->has_children
5afb4e99
DJ
16289 && (load_all
16290 || last_die->tag == DW_TAG_namespace
f55ee35c 16291 || last_die->tag == DW_TAG_module
72bf9492 16292 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16293 || (cu->language == language_cplus
16294 && last_die->tag == DW_TAG_subprogram
16295 && (last_die->name == NULL
16296 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16297 || (cu->language != language_c
16298 && (last_die->tag == DW_TAG_class_type
680b30c7 16299 || last_die->tag == DW_TAG_interface_type
72bf9492 16300 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16301 || last_die->tag == DW_TAG_union_type))
16302 || (cu->language == language_ada
16303 && (last_die->tag == DW_TAG_subprogram
16304 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16305 {
16306 nesting_level++;
16307 parent_die = last_die;
16308 continue;
16309 }
16310
16311 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16312 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16313
16314 /* Back to the top, do it again. */
16315 }
16316}
16317
c906108c
SS
16318/* Read a minimal amount of information into the minimal die structure. */
16319
d521ce57 16320static const gdb_byte *
dee91e82
DE
16321read_partial_die (const struct die_reader_specs *reader,
16322 struct partial_die_info *part_die,
16323 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16324 const gdb_byte *info_ptr)
c906108c 16325{
dee91e82 16326 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16327 struct objfile *objfile = cu->objfile;
d521ce57 16328 const gdb_byte *buffer = reader->buffer;
fa238c03 16329 unsigned int i;
c906108c 16330 struct attribute attr;
c5aa993b 16331 int has_low_pc_attr = 0;
c906108c 16332 int has_high_pc_attr = 0;
91da1414 16333 int high_pc_relative = 0;
c906108c 16334
72bf9492 16335 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16336
9c541725 16337 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16338
16339 info_ptr += abbrev_len;
16340
16341 if (abbrev == NULL)
16342 return info_ptr;
16343
c906108c
SS
16344 part_die->tag = abbrev->tag;
16345 part_die->has_children = abbrev->has_children;
c906108c
SS
16346
16347 for (i = 0; i < abbrev->num_attrs; ++i)
16348 {
dee91e82 16349 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16350
16351 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16352 partial symbol table. */
c906108c
SS
16353 switch (attr.name)
16354 {
16355 case DW_AT_name:
71c25dea
TT
16356 switch (part_die->tag)
16357 {
16358 case DW_TAG_compile_unit:
95554aad 16359 case DW_TAG_partial_unit:
348e048f 16360 case DW_TAG_type_unit:
71c25dea
TT
16361 /* Compilation units have a DW_AT_name that is a filename, not
16362 a source language identifier. */
16363 case DW_TAG_enumeration_type:
16364 case DW_TAG_enumerator:
16365 /* These tags always have simple identifiers already; no need
16366 to canonicalize them. */
16367 part_die->name = DW_STRING (&attr);
16368 break;
16369 default:
16370 part_die->name
16371 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16372 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16373 break;
16374 }
c906108c 16375 break;
31ef98ae 16376 case DW_AT_linkage_name:
c906108c 16377 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16378 /* Note that both forms of linkage name might appear. We
16379 assume they will be the same, and we only store the last
16380 one we see. */
94af9270
KS
16381 if (cu->language == language_ada)
16382 part_die->name = DW_STRING (&attr);
abc72ce4 16383 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16384 break;
16385 case DW_AT_low_pc:
16386 has_low_pc_attr = 1;
31aa7e4e 16387 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16388 break;
16389 case DW_AT_high_pc:
16390 has_high_pc_attr = 1;
31aa7e4e
JB
16391 part_die->highpc = attr_value_as_address (&attr);
16392 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16393 high_pc_relative = 1;
c906108c
SS
16394 break;
16395 case DW_AT_location:
0963b4bd 16396 /* Support the .debug_loc offsets. */
8e19ed76
PS
16397 if (attr_form_is_block (&attr))
16398 {
95554aad 16399 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16400 }
3690dd37 16401 else if (attr_form_is_section_offset (&attr))
8e19ed76 16402 {
4d3c2250 16403 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16404 }
16405 else
16406 {
4d3c2250
KB
16407 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16408 "partial symbol information");
8e19ed76 16409 }
c906108c 16410 break;
c906108c
SS
16411 case DW_AT_external:
16412 part_die->is_external = DW_UNSND (&attr);
16413 break;
16414 case DW_AT_declaration:
16415 part_die->is_declaration = DW_UNSND (&attr);
16416 break;
16417 case DW_AT_type:
16418 part_die->has_type = 1;
16419 break;
16420 case DW_AT_abstract_origin:
16421 case DW_AT_specification:
72bf9492
DJ
16422 case DW_AT_extension:
16423 part_die->has_specification = 1;
c764a876 16424 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16425 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16426 || cu->per_cu->is_dwz);
c906108c
SS
16427 break;
16428 case DW_AT_sibling:
16429 /* Ignore absolute siblings, they might point outside of
16430 the current compile unit. */
16431 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16432 complaint (&symfile_complaints,
16433 _("ignoring absolute DW_AT_sibling"));
c906108c 16434 else
b9502d3f 16435 {
9c541725
PA
16436 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16437 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16438
16439 if (sibling_ptr < info_ptr)
16440 complaint (&symfile_complaints,
16441 _("DW_AT_sibling points backwards"));
22869d73
KS
16442 else if (sibling_ptr > reader->buffer_end)
16443 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16444 else
16445 part_die->sibling = sibling_ptr;
16446 }
c906108c 16447 break;
fa4028e9
JB
16448 case DW_AT_byte_size:
16449 part_die->has_byte_size = 1;
16450 break;
ff908ebf
AW
16451 case DW_AT_const_value:
16452 part_die->has_const_value = 1;
16453 break;
68511cec
CES
16454 case DW_AT_calling_convention:
16455 /* DWARF doesn't provide a way to identify a program's source-level
16456 entry point. DW_AT_calling_convention attributes are only meant
16457 to describe functions' calling conventions.
16458
16459 However, because it's a necessary piece of information in
0c1b455e
TT
16460 Fortran, and before DWARF 4 DW_CC_program was the only
16461 piece of debugging information whose definition refers to
16462 a 'main program' at all, several compilers marked Fortran
16463 main programs with DW_CC_program --- even when those
16464 functions use the standard calling conventions.
16465
16466 Although DWARF now specifies a way to provide this
16467 information, we support this practice for backward
16468 compatibility. */
68511cec 16469 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16470 && cu->language == language_fortran)
16471 part_die->main_subprogram = 1;
68511cec 16472 break;
481860b3
GB
16473 case DW_AT_inline:
16474 if (DW_UNSND (&attr) == DW_INL_inlined
16475 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16476 part_die->may_be_inlined = 1;
16477 break;
95554aad
TT
16478
16479 case DW_AT_import:
16480 if (part_die->tag == DW_TAG_imported_unit)
36586728 16481 {
9c541725 16482 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16483 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16484 || cu->per_cu->is_dwz);
16485 }
95554aad
TT
16486 break;
16487
0c1b455e
TT
16488 case DW_AT_main_subprogram:
16489 part_die->main_subprogram = DW_UNSND (&attr);
16490 break;
16491
c906108c
SS
16492 default:
16493 break;
16494 }
16495 }
16496
91da1414
MW
16497 if (high_pc_relative)
16498 part_die->highpc += part_die->lowpc;
16499
9373cf26
JK
16500 if (has_low_pc_attr && has_high_pc_attr)
16501 {
16502 /* When using the GNU linker, .gnu.linkonce. sections are used to
16503 eliminate duplicate copies of functions and vtables and such.
16504 The linker will arbitrarily choose one and discard the others.
16505 The AT_*_pc values for such functions refer to local labels in
16506 these sections. If the section from that file was discarded, the
16507 labels are not in the output, so the relocs get a value of 0.
16508 If this is a discarded function, mark the pc bounds as invalid,
16509 so that GDB will ignore it. */
16510 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16511 {
bb5ed363 16512 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16513
16514 complaint (&symfile_complaints,
16515 _("DW_AT_low_pc %s is zero "
16516 "for DIE at 0x%x [in module %s]"),
16517 paddress (gdbarch, part_die->lowpc),
9c541725 16518 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16519 }
16520 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16521 else if (part_die->lowpc >= part_die->highpc)
16522 {
bb5ed363 16523 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16524
16525 complaint (&symfile_complaints,
16526 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16527 "for DIE at 0x%x [in module %s]"),
16528 paddress (gdbarch, part_die->lowpc),
16529 paddress (gdbarch, part_die->highpc),
9c541725
PA
16530 to_underlying (part_die->sect_off),
16531 objfile_name (objfile));
9373cf26
JK
16532 }
16533 else
16534 part_die->has_pc_info = 1;
16535 }
85cbf3d3 16536
c906108c
SS
16537 return info_ptr;
16538}
16539
72bf9492
DJ
16540/* Find a cached partial DIE at OFFSET in CU. */
16541
16542static struct partial_die_info *
9c541725 16543find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16544{
16545 struct partial_die_info *lookup_die = NULL;
16546 struct partial_die_info part_die;
16547
9c541725 16548 part_die.sect_off = sect_off;
9a3c8263
SM
16549 lookup_die = ((struct partial_die_info *)
16550 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16551 to_underlying (sect_off)));
72bf9492 16552
72bf9492
DJ
16553 return lookup_die;
16554}
16555
348e048f
DE
16556/* Find a partial DIE at OFFSET, which may or may not be in CU,
16557 except in the case of .debug_types DIEs which do not reference
16558 outside their CU (they do however referencing other types via
55f1336d 16559 DW_FORM_ref_sig8). */
72bf9492
DJ
16560
16561static struct partial_die_info *
9c541725 16562find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16563{
bb5ed363 16564 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16565 struct dwarf2_per_cu_data *per_cu = NULL;
16566 struct partial_die_info *pd = NULL;
72bf9492 16567
36586728 16568 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16569 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16570 {
9c541725 16571 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16572 if (pd != NULL)
16573 return pd;
0d99eb77
DE
16574 /* We missed recording what we needed.
16575 Load all dies and try again. */
16576 per_cu = cu->per_cu;
5afb4e99 16577 }
0d99eb77
DE
16578 else
16579 {
16580 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16581 if (cu->per_cu->is_debug_types)
0d99eb77 16582 {
9c541725
PA
16583 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16584 " external reference to offset 0x%x [in module %s].\n"),
16585 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16586 bfd_get_filename (objfile->obfd));
16587 }
9c541725 16588 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16589 objfile);
72bf9492 16590
0d99eb77
DE
16591 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16592 load_partial_comp_unit (per_cu);
ae038cb0 16593
0d99eb77 16594 per_cu->cu->last_used = 0;
9c541725 16595 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16596 }
5afb4e99 16597
dee91e82
DE
16598 /* If we didn't find it, and not all dies have been loaded,
16599 load them all and try again. */
16600
5afb4e99
DJ
16601 if (pd == NULL && per_cu->load_all_dies == 0)
16602 {
5afb4e99 16603 per_cu->load_all_dies = 1;
fd820528
DE
16604
16605 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16606 THIS_CU->cu may already be in use. So we can't just free it and
16607 replace its DIEs with the ones we read in. Instead, we leave those
16608 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16609 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16610 set. */
dee91e82 16611 load_partial_comp_unit (per_cu);
5afb4e99 16612
9c541725 16613 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16614 }
16615
16616 if (pd == NULL)
16617 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16618 _("could not find partial DIE 0x%x "
16619 "in cache [from module %s]\n"),
9c541725 16620 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16621 return pd;
72bf9492
DJ
16622}
16623
abc72ce4
DE
16624/* See if we can figure out if the class lives in a namespace. We do
16625 this by looking for a member function; its demangled name will
16626 contain namespace info, if there is any. */
16627
16628static void
16629guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16630 struct dwarf2_cu *cu)
16631{
16632 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16633 what template types look like, because the demangler
16634 frequently doesn't give the same name as the debug info. We
16635 could fix this by only using the demangled name to get the
16636 prefix (but see comment in read_structure_type). */
16637
16638 struct partial_die_info *real_pdi;
16639 struct partial_die_info *child_pdi;
16640
16641 /* If this DIE (this DIE's specification, if any) has a parent, then
16642 we should not do this. We'll prepend the parent's fully qualified
16643 name when we create the partial symbol. */
16644
16645 real_pdi = struct_pdi;
16646 while (real_pdi->has_specification)
36586728
TT
16647 real_pdi = find_partial_die (real_pdi->spec_offset,
16648 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16649
16650 if (real_pdi->die_parent != NULL)
16651 return;
16652
16653 for (child_pdi = struct_pdi->die_child;
16654 child_pdi != NULL;
16655 child_pdi = child_pdi->die_sibling)
16656 {
16657 if (child_pdi->tag == DW_TAG_subprogram
16658 && child_pdi->linkage_name != NULL)
16659 {
16660 char *actual_class_name
16661 = language_class_name_from_physname (cu->language_defn,
16662 child_pdi->linkage_name);
16663 if (actual_class_name != NULL)
16664 {
16665 struct_pdi->name
224c3ddb
SM
16666 = ((const char *)
16667 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16668 actual_class_name,
16669 strlen (actual_class_name)));
abc72ce4
DE
16670 xfree (actual_class_name);
16671 }
16672 break;
16673 }
16674 }
16675}
16676
72bf9492
DJ
16677/* Adjust PART_DIE before generating a symbol for it. This function
16678 may set the is_external flag or change the DIE's name. */
16679
16680static void
16681fixup_partial_die (struct partial_die_info *part_die,
16682 struct dwarf2_cu *cu)
16683{
abc72ce4
DE
16684 /* Once we've fixed up a die, there's no point in doing so again.
16685 This also avoids a memory leak if we were to call
16686 guess_partial_die_structure_name multiple times. */
16687 if (part_die->fixup_called)
16688 return;
16689
72bf9492
DJ
16690 /* If we found a reference attribute and the DIE has no name, try
16691 to find a name in the referred to DIE. */
16692
16693 if (part_die->name == NULL && part_die->has_specification)
16694 {
16695 struct partial_die_info *spec_die;
72bf9492 16696
36586728
TT
16697 spec_die = find_partial_die (part_die->spec_offset,
16698 part_die->spec_is_dwz, cu);
72bf9492 16699
10b3939b 16700 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16701
16702 if (spec_die->name)
16703 {
16704 part_die->name = spec_die->name;
16705
16706 /* Copy DW_AT_external attribute if it is set. */
16707 if (spec_die->is_external)
16708 part_die->is_external = spec_die->is_external;
16709 }
16710 }
16711
16712 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16713
16714 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16715 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16716
abc72ce4
DE
16717 /* If there is no parent die to provide a namespace, and there are
16718 children, see if we can determine the namespace from their linkage
122d1940 16719 name. */
abc72ce4 16720 if (cu->language == language_cplus
8b70b953 16721 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16722 && part_die->die_parent == NULL
16723 && part_die->has_children
16724 && (part_die->tag == DW_TAG_class_type
16725 || part_die->tag == DW_TAG_structure_type
16726 || part_die->tag == DW_TAG_union_type))
16727 guess_partial_die_structure_name (part_die, cu);
16728
53832f31
TT
16729 /* GCC might emit a nameless struct or union that has a linkage
16730 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16731 if (part_die->name == NULL
96408a79
SA
16732 && (part_die->tag == DW_TAG_class_type
16733 || part_die->tag == DW_TAG_interface_type
16734 || part_die->tag == DW_TAG_structure_type
16735 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16736 && part_die->linkage_name != NULL)
16737 {
16738 char *demangled;
16739
8de20a37 16740 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16741 if (demangled)
16742 {
96408a79
SA
16743 const char *base;
16744
16745 /* Strip any leading namespaces/classes, keep only the base name.
16746 DW_AT_name for named DIEs does not contain the prefixes. */
16747 base = strrchr (demangled, ':');
16748 if (base && base > demangled && base[-1] == ':')
16749 base++;
16750 else
16751 base = demangled;
16752
34a68019 16753 part_die->name
224c3ddb
SM
16754 = ((const char *)
16755 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16756 base, strlen (base)));
53832f31
TT
16757 xfree (demangled);
16758 }
16759 }
16760
abc72ce4 16761 part_die->fixup_called = 1;
72bf9492
DJ
16762}
16763
a8329558 16764/* Read an attribute value described by an attribute form. */
c906108c 16765
d521ce57 16766static const gdb_byte *
dee91e82
DE
16767read_attribute_value (const struct die_reader_specs *reader,
16768 struct attribute *attr, unsigned form,
43988095 16769 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16770{
dee91e82 16771 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16772 struct objfile *objfile = cu->objfile;
16773 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16774 bfd *abfd = reader->abfd;
e7c27a73 16775 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16776 unsigned int bytes_read;
16777 struct dwarf_block *blk;
16778
aead7601 16779 attr->form = (enum dwarf_form) form;
a8329558 16780 switch (form)
c906108c 16781 {
c906108c 16782 case DW_FORM_ref_addr:
ae411497 16783 if (cu->header.version == 2)
4568ecf9 16784 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16785 else
4568ecf9
DE
16786 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16787 &cu->header, &bytes_read);
ae411497
TT
16788 info_ptr += bytes_read;
16789 break;
36586728
TT
16790 case DW_FORM_GNU_ref_alt:
16791 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16792 info_ptr += bytes_read;
16793 break;
ae411497 16794 case DW_FORM_addr:
e7c27a73 16795 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16796 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16797 info_ptr += bytes_read;
c906108c
SS
16798 break;
16799 case DW_FORM_block2:
7b5a2f43 16800 blk = dwarf_alloc_block (cu);
c906108c
SS
16801 blk->size = read_2_bytes (abfd, info_ptr);
16802 info_ptr += 2;
16803 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16804 info_ptr += blk->size;
16805 DW_BLOCK (attr) = blk;
16806 break;
16807 case DW_FORM_block4:
7b5a2f43 16808 blk = dwarf_alloc_block (cu);
c906108c
SS
16809 blk->size = read_4_bytes (abfd, info_ptr);
16810 info_ptr += 4;
16811 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16812 info_ptr += blk->size;
16813 DW_BLOCK (attr) = blk;
16814 break;
16815 case DW_FORM_data2:
16816 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16817 info_ptr += 2;
16818 break;
16819 case DW_FORM_data4:
16820 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16821 info_ptr += 4;
16822 break;
16823 case DW_FORM_data8:
16824 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16825 info_ptr += 8;
16826 break;
0224619f
JK
16827 case DW_FORM_data16:
16828 blk = dwarf_alloc_block (cu);
16829 blk->size = 16;
16830 blk->data = read_n_bytes (abfd, info_ptr, 16);
16831 info_ptr += 16;
16832 DW_BLOCK (attr) = blk;
16833 break;
2dc7f7b3
TT
16834 case DW_FORM_sec_offset:
16835 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16836 info_ptr += bytes_read;
16837 break;
c906108c 16838 case DW_FORM_string:
9b1c24c8 16839 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16840 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16841 info_ptr += bytes_read;
16842 break;
4bdf3d34 16843 case DW_FORM_strp:
36586728
TT
16844 if (!cu->per_cu->is_dwz)
16845 {
16846 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16847 &bytes_read);
16848 DW_STRING_IS_CANONICAL (attr) = 0;
16849 info_ptr += bytes_read;
16850 break;
16851 }
16852 /* FALLTHROUGH */
43988095
JK
16853 case DW_FORM_line_strp:
16854 if (!cu->per_cu->is_dwz)
16855 {
16856 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16857 cu_header, &bytes_read);
16858 DW_STRING_IS_CANONICAL (attr) = 0;
16859 info_ptr += bytes_read;
16860 break;
16861 }
16862 /* FALLTHROUGH */
36586728
TT
16863 case DW_FORM_GNU_strp_alt:
16864 {
16865 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16866 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16867 &bytes_read);
16868
16869 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16870 DW_STRING_IS_CANONICAL (attr) = 0;
16871 info_ptr += bytes_read;
16872 }
4bdf3d34 16873 break;
2dc7f7b3 16874 case DW_FORM_exprloc:
c906108c 16875 case DW_FORM_block:
7b5a2f43 16876 blk = dwarf_alloc_block (cu);
c906108c
SS
16877 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16878 info_ptr += bytes_read;
16879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16880 info_ptr += blk->size;
16881 DW_BLOCK (attr) = blk;
16882 break;
16883 case DW_FORM_block1:
7b5a2f43 16884 blk = dwarf_alloc_block (cu);
c906108c
SS
16885 blk->size = read_1_byte (abfd, info_ptr);
16886 info_ptr += 1;
16887 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16888 info_ptr += blk->size;
16889 DW_BLOCK (attr) = blk;
16890 break;
16891 case DW_FORM_data1:
16892 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16893 info_ptr += 1;
16894 break;
16895 case DW_FORM_flag:
16896 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16897 info_ptr += 1;
16898 break;
2dc7f7b3
TT
16899 case DW_FORM_flag_present:
16900 DW_UNSND (attr) = 1;
16901 break;
c906108c
SS
16902 case DW_FORM_sdata:
16903 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16904 info_ptr += bytes_read;
16905 break;
16906 case DW_FORM_udata:
16907 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16908 info_ptr += bytes_read;
16909 break;
16910 case DW_FORM_ref1:
9c541725 16911 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16912 + read_1_byte (abfd, info_ptr));
c906108c
SS
16913 info_ptr += 1;
16914 break;
16915 case DW_FORM_ref2:
9c541725 16916 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16917 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16918 info_ptr += 2;
16919 break;
16920 case DW_FORM_ref4:
9c541725 16921 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16922 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16923 info_ptr += 4;
16924 break;
613e1657 16925 case DW_FORM_ref8:
9c541725 16926 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16927 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16928 info_ptr += 8;
16929 break;
55f1336d 16930 case DW_FORM_ref_sig8:
ac9ec31b 16931 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16932 info_ptr += 8;
16933 break;
c906108c 16934 case DW_FORM_ref_udata:
9c541725 16935 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16936 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16937 info_ptr += bytes_read;
16938 break;
c906108c 16939 case DW_FORM_indirect:
a8329558
KW
16940 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16941 info_ptr += bytes_read;
43988095
JK
16942 if (form == DW_FORM_implicit_const)
16943 {
16944 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16945 info_ptr += bytes_read;
16946 }
16947 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16948 info_ptr);
16949 break;
16950 case DW_FORM_implicit_const:
16951 DW_SND (attr) = implicit_const;
a8329558 16952 break;
3019eac3
DE
16953 case DW_FORM_GNU_addr_index:
16954 if (reader->dwo_file == NULL)
16955 {
16956 /* For now flag a hard error.
16957 Later we can turn this into a complaint. */
16958 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16959 dwarf_form_name (form),
16960 bfd_get_filename (abfd));
16961 }
16962 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16963 info_ptr += bytes_read;
16964 break;
16965 case DW_FORM_GNU_str_index:
16966 if (reader->dwo_file == NULL)
16967 {
16968 /* For now flag a hard error.
16969 Later we can turn this into a complaint if warranted. */
16970 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16971 dwarf_form_name (form),
16972 bfd_get_filename (abfd));
16973 }
16974 {
16975 ULONGEST str_index =
16976 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16977
342587c4 16978 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16979 DW_STRING_IS_CANONICAL (attr) = 0;
16980 info_ptr += bytes_read;
16981 }
16982 break;
c906108c 16983 default:
8a3fe4f8 16984 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16985 dwarf_form_name (form),
16986 bfd_get_filename (abfd));
c906108c 16987 }
28e94949 16988
36586728 16989 /* Super hack. */
7771576e 16990 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16991 attr->form = DW_FORM_GNU_ref_alt;
16992
28e94949
JB
16993 /* We have seen instances where the compiler tried to emit a byte
16994 size attribute of -1 which ended up being encoded as an unsigned
16995 0xffffffff. Although 0xffffffff is technically a valid size value,
16996 an object of this size seems pretty unlikely so we can relatively
16997 safely treat these cases as if the size attribute was invalid and
16998 treat them as zero by default. */
16999 if (attr->name == DW_AT_byte_size
17000 && form == DW_FORM_data4
17001 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17002 {
17003 complaint
17004 (&symfile_complaints,
43bbcdc2
PH
17005 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17006 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17007 DW_UNSND (attr) = 0;
17008 }
28e94949 17009
c906108c
SS
17010 return info_ptr;
17011}
17012
a8329558
KW
17013/* Read an attribute described by an abbreviated attribute. */
17014
d521ce57 17015static const gdb_byte *
dee91e82
DE
17016read_attribute (const struct die_reader_specs *reader,
17017 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17018 const gdb_byte *info_ptr)
a8329558
KW
17019{
17020 attr->name = abbrev->name;
43988095
JK
17021 return read_attribute_value (reader, attr, abbrev->form,
17022 abbrev->implicit_const, info_ptr);
a8329558
KW
17023}
17024
0963b4bd 17025/* Read dwarf information from a buffer. */
c906108c
SS
17026
17027static unsigned int
a1855c1d 17028read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17029{
fe1b8b76 17030 return bfd_get_8 (abfd, buf);
c906108c
SS
17031}
17032
17033static int
a1855c1d 17034read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17035{
fe1b8b76 17036 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17037}
17038
17039static unsigned int
a1855c1d 17040read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17041{
fe1b8b76 17042 return bfd_get_16 (abfd, buf);
c906108c
SS
17043}
17044
21ae7a4d 17045static int
a1855c1d 17046read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17047{
17048 return bfd_get_signed_16 (abfd, buf);
17049}
17050
c906108c 17051static unsigned int
a1855c1d 17052read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17053{
fe1b8b76 17054 return bfd_get_32 (abfd, buf);
c906108c
SS
17055}
17056
21ae7a4d 17057static int
a1855c1d 17058read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17059{
17060 return bfd_get_signed_32 (abfd, buf);
17061}
17062
93311388 17063static ULONGEST
a1855c1d 17064read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17065{
fe1b8b76 17066 return bfd_get_64 (abfd, buf);
c906108c
SS
17067}
17068
17069static CORE_ADDR
d521ce57 17070read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17071 unsigned int *bytes_read)
c906108c 17072{
e7c27a73 17073 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17074 CORE_ADDR retval = 0;
17075
107d2387 17076 if (cu_header->signed_addr_p)
c906108c 17077 {
107d2387
AC
17078 switch (cu_header->addr_size)
17079 {
17080 case 2:
fe1b8b76 17081 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17082 break;
17083 case 4:
fe1b8b76 17084 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17085 break;
17086 case 8:
fe1b8b76 17087 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17088 break;
17089 default:
8e65ff28 17090 internal_error (__FILE__, __LINE__,
e2e0b3e5 17091 _("read_address: bad switch, signed [in module %s]"),
659b0389 17092 bfd_get_filename (abfd));
107d2387
AC
17093 }
17094 }
17095 else
17096 {
17097 switch (cu_header->addr_size)
17098 {
17099 case 2:
fe1b8b76 17100 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17101 break;
17102 case 4:
fe1b8b76 17103 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17104 break;
17105 case 8:
fe1b8b76 17106 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17107 break;
17108 default:
8e65ff28 17109 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17110 _("read_address: bad switch, "
17111 "unsigned [in module %s]"),
659b0389 17112 bfd_get_filename (abfd));
107d2387 17113 }
c906108c 17114 }
64367e0a 17115
107d2387
AC
17116 *bytes_read = cu_header->addr_size;
17117 return retval;
c906108c
SS
17118}
17119
f7ef9339 17120/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17121 specification allows the initial length to take up either 4 bytes
17122 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17123 bytes describe the length and all offsets will be 8 bytes in length
17124 instead of 4.
17125
f7ef9339
KB
17126 An older, non-standard 64-bit format is also handled by this
17127 function. The older format in question stores the initial length
17128 as an 8-byte quantity without an escape value. Lengths greater
17129 than 2^32 aren't very common which means that the initial 4 bytes
17130 is almost always zero. Since a length value of zero doesn't make
17131 sense for the 32-bit format, this initial zero can be considered to
17132 be an escape value which indicates the presence of the older 64-bit
17133 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17134 greater than 4GB. If it becomes necessary to handle lengths
17135 somewhat larger than 4GB, we could allow other small values (such
17136 as the non-sensical values of 1, 2, and 3) to also be used as
17137 escape values indicating the presence of the old format.
f7ef9339 17138
917c78fc
MK
17139 The value returned via bytes_read should be used to increment the
17140 relevant pointer after calling read_initial_length().
c764a876 17141
613e1657
KB
17142 [ Note: read_initial_length() and read_offset() are based on the
17143 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17144 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17145 from:
17146
f7ef9339 17147 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17148
613e1657
KB
17149 This document is only a draft and is subject to change. (So beware.)
17150
f7ef9339 17151 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17152 determined empirically by examining 64-bit ELF files produced by
17153 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17154
17155 - Kevin, July 16, 2002
613e1657
KB
17156 ] */
17157
17158static LONGEST
d521ce57 17159read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17160{
fe1b8b76 17161 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17162
dd373385 17163 if (length == 0xffffffff)
613e1657 17164 {
fe1b8b76 17165 length = bfd_get_64 (abfd, buf + 4);
613e1657 17166 *bytes_read = 12;
613e1657 17167 }
dd373385 17168 else if (length == 0)
f7ef9339 17169 {
dd373385 17170 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17171 length = bfd_get_64 (abfd, buf);
f7ef9339 17172 *bytes_read = 8;
f7ef9339 17173 }
613e1657
KB
17174 else
17175 {
17176 *bytes_read = 4;
613e1657
KB
17177 }
17178
c764a876
DE
17179 return length;
17180}
dd373385 17181
c764a876
DE
17182/* Cover function for read_initial_length.
17183 Returns the length of the object at BUF, and stores the size of the
17184 initial length in *BYTES_READ and stores the size that offsets will be in
17185 *OFFSET_SIZE.
17186 If the initial length size is not equivalent to that specified in
17187 CU_HEADER then issue a complaint.
17188 This is useful when reading non-comp-unit headers. */
dd373385 17189
c764a876 17190static LONGEST
d521ce57 17191read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17192 const struct comp_unit_head *cu_header,
17193 unsigned int *bytes_read,
17194 unsigned int *offset_size)
17195{
17196 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17197
17198 gdb_assert (cu_header->initial_length_size == 4
17199 || cu_header->initial_length_size == 8
17200 || cu_header->initial_length_size == 12);
17201
17202 if (cu_header->initial_length_size != *bytes_read)
17203 complaint (&symfile_complaints,
17204 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17205
c764a876 17206 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17207 return length;
613e1657
KB
17208}
17209
17210/* Read an offset from the data stream. The size of the offset is
917c78fc 17211 given by cu_header->offset_size. */
613e1657
KB
17212
17213static LONGEST
d521ce57
TT
17214read_offset (bfd *abfd, const gdb_byte *buf,
17215 const struct comp_unit_head *cu_header,
891d2f0b 17216 unsigned int *bytes_read)
c764a876
DE
17217{
17218 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17219
c764a876
DE
17220 *bytes_read = cu_header->offset_size;
17221 return offset;
17222}
17223
17224/* Read an offset from the data stream. */
17225
17226static LONGEST
d521ce57 17227read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17228{
17229 LONGEST retval = 0;
17230
c764a876 17231 switch (offset_size)
613e1657
KB
17232 {
17233 case 4:
fe1b8b76 17234 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17235 break;
17236 case 8:
fe1b8b76 17237 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17238 break;
17239 default:
8e65ff28 17240 internal_error (__FILE__, __LINE__,
c764a876 17241 _("read_offset_1: bad switch [in module %s]"),
659b0389 17242 bfd_get_filename (abfd));
613e1657
KB
17243 }
17244
917c78fc 17245 return retval;
613e1657
KB
17246}
17247
d521ce57
TT
17248static const gdb_byte *
17249read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17250{
17251 /* If the size of a host char is 8 bits, we can return a pointer
17252 to the buffer, otherwise we have to copy the data to a buffer
17253 allocated on the temporary obstack. */
4bdf3d34 17254 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17255 return buf;
c906108c
SS
17256}
17257
d521ce57
TT
17258static const char *
17259read_direct_string (bfd *abfd, const gdb_byte *buf,
17260 unsigned int *bytes_read_ptr)
c906108c
SS
17261{
17262 /* If the size of a host char is 8 bits, we can return a pointer
17263 to the string, otherwise we have to copy the string to a buffer
17264 allocated on the temporary obstack. */
4bdf3d34 17265 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17266 if (*buf == '\0')
17267 {
17268 *bytes_read_ptr = 1;
17269 return NULL;
17270 }
d521ce57
TT
17271 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17272 return (const char *) buf;
4bdf3d34
JJ
17273}
17274
43988095
JK
17275/* Return pointer to string at section SECT offset STR_OFFSET with error
17276 reporting strings FORM_NAME and SECT_NAME. */
17277
d521ce57 17278static const char *
43988095
JK
17279read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17280 struct dwarf2_section_info *sect,
17281 const char *form_name,
17282 const char *sect_name)
17283{
17284 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17285 if (sect->buffer == NULL)
17286 error (_("%s used without %s section [in module %s]"),
17287 form_name, sect_name, bfd_get_filename (abfd));
17288 if (str_offset >= sect->size)
17289 error (_("%s pointing outside of %s section [in module %s]"),
17290 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17291 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17292 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17293 return NULL;
43988095
JK
17294 return (const char *) (sect->buffer + str_offset);
17295}
17296
17297/* Return pointer to string at .debug_str offset STR_OFFSET. */
17298
17299static const char *
17300read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17301{
17302 return read_indirect_string_at_offset_from (abfd, str_offset,
17303 &dwarf2_per_objfile->str,
17304 "DW_FORM_strp", ".debug_str");
17305}
17306
17307/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17308
17309static const char *
17310read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17311{
17312 return read_indirect_string_at_offset_from (abfd, str_offset,
17313 &dwarf2_per_objfile->line_str,
17314 "DW_FORM_line_strp",
17315 ".debug_line_str");
c906108c
SS
17316}
17317
36586728
TT
17318/* Read a string at offset STR_OFFSET in the .debug_str section from
17319 the .dwz file DWZ. Throw an error if the offset is too large. If
17320 the string consists of a single NUL byte, return NULL; otherwise
17321 return a pointer to the string. */
17322
d521ce57 17323static const char *
36586728
TT
17324read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17325{
17326 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17327
17328 if (dwz->str.buffer == NULL)
17329 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17330 "section [in module %s]"),
17331 bfd_get_filename (dwz->dwz_bfd));
17332 if (str_offset >= dwz->str.size)
17333 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17334 ".debug_str section [in module %s]"),
17335 bfd_get_filename (dwz->dwz_bfd));
17336 gdb_assert (HOST_CHAR_BIT == 8);
17337 if (dwz->str.buffer[str_offset] == '\0')
17338 return NULL;
d521ce57 17339 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17340}
17341
43988095
JK
17342/* Return pointer to string at .debug_str offset as read from BUF.
17343 BUF is assumed to be in a compilation unit described by CU_HEADER.
17344 Return *BYTES_READ_PTR count of bytes read from BUF. */
17345
d521ce57
TT
17346static const char *
17347read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17348 const struct comp_unit_head *cu_header,
17349 unsigned int *bytes_read_ptr)
17350{
17351 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17352
17353 return read_indirect_string_at_offset (abfd, str_offset);
17354}
17355
43988095
JK
17356/* Return pointer to string at .debug_line_str offset as read from BUF.
17357 BUF is assumed to be in a compilation unit described by CU_HEADER.
17358 Return *BYTES_READ_PTR count of bytes read from BUF. */
17359
17360static const char *
17361read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17362 const struct comp_unit_head *cu_header,
17363 unsigned int *bytes_read_ptr)
17364{
17365 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17366
17367 return read_indirect_line_string_at_offset (abfd, str_offset);
17368}
17369
17370ULONGEST
d521ce57 17371read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17372 unsigned int *bytes_read_ptr)
c906108c 17373{
12df843f 17374 ULONGEST result;
ce5d95e1 17375 unsigned int num_read;
870f88f7 17376 int shift;
c906108c
SS
17377 unsigned char byte;
17378
17379 result = 0;
17380 shift = 0;
17381 num_read = 0;
c906108c
SS
17382 while (1)
17383 {
fe1b8b76 17384 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17385 buf++;
17386 num_read++;
12df843f 17387 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17388 if ((byte & 128) == 0)
17389 {
17390 break;
17391 }
17392 shift += 7;
17393 }
17394 *bytes_read_ptr = num_read;
17395 return result;
17396}
17397
12df843f 17398static LONGEST
d521ce57
TT
17399read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17400 unsigned int *bytes_read_ptr)
c906108c 17401{
12df843f 17402 LONGEST result;
870f88f7 17403 int shift, num_read;
c906108c
SS
17404 unsigned char byte;
17405
17406 result = 0;
17407 shift = 0;
c906108c 17408 num_read = 0;
c906108c
SS
17409 while (1)
17410 {
fe1b8b76 17411 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17412 buf++;
17413 num_read++;
12df843f 17414 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17415 shift += 7;
17416 if ((byte & 128) == 0)
17417 {
17418 break;
17419 }
17420 }
77e0b926 17421 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17422 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17423 *bytes_read_ptr = num_read;
17424 return result;
17425}
17426
3019eac3
DE
17427/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17428 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17429 ADDR_SIZE is the size of addresses from the CU header. */
17430
17431static CORE_ADDR
17432read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17433{
17434 struct objfile *objfile = dwarf2_per_objfile->objfile;
17435 bfd *abfd = objfile->obfd;
17436 const gdb_byte *info_ptr;
17437
17438 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17439 if (dwarf2_per_objfile->addr.buffer == NULL)
17440 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17441 objfile_name (objfile));
3019eac3
DE
17442 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17443 error (_("DW_FORM_addr_index pointing outside of "
17444 ".debug_addr section [in module %s]"),
4262abfb 17445 objfile_name (objfile));
3019eac3
DE
17446 info_ptr = (dwarf2_per_objfile->addr.buffer
17447 + addr_base + addr_index * addr_size);
17448 if (addr_size == 4)
17449 return bfd_get_32 (abfd, info_ptr);
17450 else
17451 return bfd_get_64 (abfd, info_ptr);
17452}
17453
17454/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17455
17456static CORE_ADDR
17457read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17458{
17459 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17460}
17461
17462/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17463
17464static CORE_ADDR
d521ce57 17465read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17466 unsigned int *bytes_read)
17467{
17468 bfd *abfd = cu->objfile->obfd;
17469 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17470
17471 return read_addr_index (cu, addr_index);
17472}
17473
17474/* Data structure to pass results from dwarf2_read_addr_index_reader
17475 back to dwarf2_read_addr_index. */
17476
17477struct dwarf2_read_addr_index_data
17478{
17479 ULONGEST addr_base;
17480 int addr_size;
17481};
17482
17483/* die_reader_func for dwarf2_read_addr_index. */
17484
17485static void
17486dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17487 const gdb_byte *info_ptr,
3019eac3
DE
17488 struct die_info *comp_unit_die,
17489 int has_children,
17490 void *data)
17491{
17492 struct dwarf2_cu *cu = reader->cu;
17493 struct dwarf2_read_addr_index_data *aidata =
17494 (struct dwarf2_read_addr_index_data *) data;
17495
17496 aidata->addr_base = cu->addr_base;
17497 aidata->addr_size = cu->header.addr_size;
17498}
17499
17500/* Given an index in .debug_addr, fetch the value.
17501 NOTE: This can be called during dwarf expression evaluation,
17502 long after the debug information has been read, and thus per_cu->cu
17503 may no longer exist. */
17504
17505CORE_ADDR
17506dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17507 unsigned int addr_index)
17508{
17509 struct objfile *objfile = per_cu->objfile;
17510 struct dwarf2_cu *cu = per_cu->cu;
17511 ULONGEST addr_base;
17512 int addr_size;
17513
17514 /* This is intended to be called from outside this file. */
17515 dw2_setup (objfile);
17516
17517 /* We need addr_base and addr_size.
17518 If we don't have PER_CU->cu, we have to get it.
17519 Nasty, but the alternative is storing the needed info in PER_CU,
17520 which at this point doesn't seem justified: it's not clear how frequently
17521 it would get used and it would increase the size of every PER_CU.
17522 Entry points like dwarf2_per_cu_addr_size do a similar thing
17523 so we're not in uncharted territory here.
17524 Alas we need to be a bit more complicated as addr_base is contained
17525 in the DIE.
17526
17527 We don't need to read the entire CU(/TU).
17528 We just need the header and top level die.
a1b64ce1 17529
3019eac3 17530 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17531 For now we skip this optimization. */
3019eac3
DE
17532
17533 if (cu != NULL)
17534 {
17535 addr_base = cu->addr_base;
17536 addr_size = cu->header.addr_size;
17537 }
17538 else
17539 {
17540 struct dwarf2_read_addr_index_data aidata;
17541
a1b64ce1
DE
17542 /* Note: We can't use init_cutu_and_read_dies_simple here,
17543 we need addr_base. */
17544 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17545 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17546 addr_base = aidata.addr_base;
17547 addr_size = aidata.addr_size;
17548 }
17549
17550 return read_addr_index_1 (addr_index, addr_base, addr_size);
17551}
17552
57d63ce2
DE
17553/* Given a DW_FORM_GNU_str_index, fetch the string.
17554 This is only used by the Fission support. */
3019eac3 17555
d521ce57 17556static const char *
342587c4 17557read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17558{
17559 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17560 const char *objf_name = objfile_name (objfile);
3019eac3 17561 bfd *abfd = objfile->obfd;
342587c4 17562 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17563 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17564 struct dwarf2_section_info *str_offsets_section =
17565 &reader->dwo_file->sections.str_offsets;
d521ce57 17566 const gdb_byte *info_ptr;
3019eac3 17567 ULONGEST str_offset;
57d63ce2 17568 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17569
73869dc2
DE
17570 dwarf2_read_section (objfile, str_section);
17571 dwarf2_read_section (objfile, str_offsets_section);
17572 if (str_section->buffer == NULL)
57d63ce2 17573 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17574 " in CU at offset 0x%x [in module %s]"),
17575 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17576 if (str_offsets_section->buffer == NULL)
57d63ce2 17577 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17578 " in CU at offset 0x%x [in module %s]"),
17579 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17580 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17581 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17582 " section in CU at offset 0x%x [in module %s]"),
17583 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17584 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17585 + str_index * cu->header.offset_size);
17586 if (cu->header.offset_size == 4)
17587 str_offset = bfd_get_32 (abfd, info_ptr);
17588 else
17589 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17590 if (str_offset >= str_section->size)
57d63ce2 17591 error (_("Offset from %s pointing outside of"
9c541725
PA
17592 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17593 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17594 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17595}
17596
3019eac3
DE
17597/* Return the length of an LEB128 number in BUF. */
17598
17599static int
17600leb128_size (const gdb_byte *buf)
17601{
17602 const gdb_byte *begin = buf;
17603 gdb_byte byte;
17604
17605 while (1)
17606 {
17607 byte = *buf++;
17608 if ((byte & 128) == 0)
17609 return buf - begin;
17610 }
17611}
17612
c906108c 17613static void
e142c38c 17614set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17615{
17616 switch (lang)
17617 {
17618 case DW_LANG_C89:
76bee0cc 17619 case DW_LANG_C99:
0cfd832f 17620 case DW_LANG_C11:
c906108c 17621 case DW_LANG_C:
d1be3247 17622 case DW_LANG_UPC:
e142c38c 17623 cu->language = language_c;
c906108c 17624 break;
9c37b5ae 17625 case DW_LANG_Java:
c906108c 17626 case DW_LANG_C_plus_plus:
0cfd832f
MW
17627 case DW_LANG_C_plus_plus_11:
17628 case DW_LANG_C_plus_plus_14:
e142c38c 17629 cu->language = language_cplus;
c906108c 17630 break;
6aecb9c2
JB
17631 case DW_LANG_D:
17632 cu->language = language_d;
17633 break;
c906108c
SS
17634 case DW_LANG_Fortran77:
17635 case DW_LANG_Fortran90:
b21b22e0 17636 case DW_LANG_Fortran95:
f7de9aab
MW
17637 case DW_LANG_Fortran03:
17638 case DW_LANG_Fortran08:
e142c38c 17639 cu->language = language_fortran;
c906108c 17640 break;
a766d390
DE
17641 case DW_LANG_Go:
17642 cu->language = language_go;
17643 break;
c906108c 17644 case DW_LANG_Mips_Assembler:
e142c38c 17645 cu->language = language_asm;
c906108c
SS
17646 break;
17647 case DW_LANG_Ada83:
8aaf0b47 17648 case DW_LANG_Ada95:
bc5f45f8
JB
17649 cu->language = language_ada;
17650 break;
72019c9c
GM
17651 case DW_LANG_Modula2:
17652 cu->language = language_m2;
17653 break;
fe8e67fd
PM
17654 case DW_LANG_Pascal83:
17655 cu->language = language_pascal;
17656 break;
22566fbd
DJ
17657 case DW_LANG_ObjC:
17658 cu->language = language_objc;
17659 break;
c44af4eb
TT
17660 case DW_LANG_Rust:
17661 case DW_LANG_Rust_old:
17662 cu->language = language_rust;
17663 break;
c906108c
SS
17664 case DW_LANG_Cobol74:
17665 case DW_LANG_Cobol85:
c906108c 17666 default:
e142c38c 17667 cu->language = language_minimal;
c906108c
SS
17668 break;
17669 }
e142c38c 17670 cu->language_defn = language_def (cu->language);
c906108c
SS
17671}
17672
17673/* Return the named attribute or NULL if not there. */
17674
17675static struct attribute *
e142c38c 17676dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17677{
a48e046c 17678 for (;;)
c906108c 17679 {
a48e046c
TT
17680 unsigned int i;
17681 struct attribute *spec = NULL;
17682
17683 for (i = 0; i < die->num_attrs; ++i)
17684 {
17685 if (die->attrs[i].name == name)
17686 return &die->attrs[i];
17687 if (die->attrs[i].name == DW_AT_specification
17688 || die->attrs[i].name == DW_AT_abstract_origin)
17689 spec = &die->attrs[i];
17690 }
17691
17692 if (!spec)
17693 break;
c906108c 17694
f2f0e013 17695 die = follow_die_ref (die, spec, &cu);
f2f0e013 17696 }
c5aa993b 17697
c906108c
SS
17698 return NULL;
17699}
17700
348e048f
DE
17701/* Return the named attribute or NULL if not there,
17702 but do not follow DW_AT_specification, etc.
17703 This is for use in contexts where we're reading .debug_types dies.
17704 Following DW_AT_specification, DW_AT_abstract_origin will take us
17705 back up the chain, and we want to go down. */
17706
17707static struct attribute *
45e58e77 17708dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17709{
17710 unsigned int i;
17711
17712 for (i = 0; i < die->num_attrs; ++i)
17713 if (die->attrs[i].name == name)
17714 return &die->attrs[i];
17715
17716 return NULL;
17717}
17718
7d45c7c3
KB
17719/* Return the string associated with a string-typed attribute, or NULL if it
17720 is either not found or is of an incorrect type. */
17721
17722static const char *
17723dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17724{
17725 struct attribute *attr;
17726 const char *str = NULL;
17727
17728 attr = dwarf2_attr (die, name, cu);
17729
17730 if (attr != NULL)
17731 {
43988095 17732 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17733 || attr->form == DW_FORM_string
17734 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17735 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17736 str = DW_STRING (attr);
17737 else
17738 complaint (&symfile_complaints,
17739 _("string type expected for attribute %s for "
17740 "DIE at 0x%x in module %s"),
9c541725 17741 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17742 objfile_name (cu->objfile));
17743 }
17744
17745 return str;
17746}
17747
05cf31d1
JB
17748/* Return non-zero iff the attribute NAME is defined for the given DIE,
17749 and holds a non-zero value. This function should only be used for
2dc7f7b3 17750 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17751
17752static int
17753dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17754{
17755 struct attribute *attr = dwarf2_attr (die, name, cu);
17756
17757 return (attr && DW_UNSND (attr));
17758}
17759
3ca72b44 17760static int
e142c38c 17761die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17762{
05cf31d1
JB
17763 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17764 which value is non-zero. However, we have to be careful with
17765 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17766 (via dwarf2_flag_true_p) follows this attribute. So we may
17767 end up accidently finding a declaration attribute that belongs
17768 to a different DIE referenced by the specification attribute,
17769 even though the given DIE does not have a declaration attribute. */
17770 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17771 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17772}
17773
63d06c5c 17774/* Return the die giving the specification for DIE, if there is
f2f0e013 17775 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17776 containing the return value on output. If there is no
17777 specification, but there is an abstract origin, that is
17778 returned. */
63d06c5c
DC
17779
17780static struct die_info *
f2f0e013 17781die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17782{
f2f0e013
DJ
17783 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17784 *spec_cu);
63d06c5c 17785
edb3359d
DJ
17786 if (spec_attr == NULL)
17787 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17788
63d06c5c
DC
17789 if (spec_attr == NULL)
17790 return NULL;
17791 else
f2f0e013 17792 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17793}
c906108c 17794
527f3840
JK
17795/* Stub for free_line_header to match void * callback types. */
17796
17797static void
17798free_line_header_voidp (void *arg)
17799{
9a3c8263 17800 struct line_header *lh = (struct line_header *) arg;
527f3840 17801
fff8551c 17802 delete lh;
527f3840
JK
17803}
17804
fff8551c
PA
17805void
17806line_header::add_include_dir (const char *include_dir)
c906108c 17807{
27e0867f 17808 if (dwarf_line_debug >= 2)
fff8551c
PA
17809 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17810 include_dirs.size () + 1, include_dir);
27e0867f 17811
fff8551c 17812 include_dirs.push_back (include_dir);
debd256d 17813}
6e70227d 17814
fff8551c
PA
17815void
17816line_header::add_file_name (const char *name,
ecfb656c 17817 dir_index d_index,
fff8551c
PA
17818 unsigned int mod_time,
17819 unsigned int length)
debd256d 17820{
27e0867f
DE
17821 if (dwarf_line_debug >= 2)
17822 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17823 (unsigned) file_names.size () + 1, name);
27e0867f 17824
ecfb656c 17825 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17826}
6e70227d 17827
83769d0b 17828/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17829
17830static struct dwarf2_section_info *
17831get_debug_line_section (struct dwarf2_cu *cu)
17832{
17833 struct dwarf2_section_info *section;
17834
17835 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17836 DWO file. */
17837 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17838 section = &cu->dwo_unit->dwo_file->sections.line;
17839 else if (cu->per_cu->is_dwz)
17840 {
17841 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17842
17843 section = &dwz->line;
17844 }
17845 else
17846 section = &dwarf2_per_objfile->line;
17847
17848 return section;
17849}
17850
43988095
JK
17851/* Read directory or file name entry format, starting with byte of
17852 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17853 entries count and the entries themselves in the described entry
17854 format. */
17855
17856static void
17857read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17858 struct line_header *lh,
17859 const struct comp_unit_head *cu_header,
17860 void (*callback) (struct line_header *lh,
17861 const char *name,
ecfb656c 17862 dir_index d_index,
43988095
JK
17863 unsigned int mod_time,
17864 unsigned int length))
17865{
17866 gdb_byte format_count, formati;
17867 ULONGEST data_count, datai;
17868 const gdb_byte *buf = *bufp;
17869 const gdb_byte *format_header_data;
17870 int i;
17871 unsigned int bytes_read;
17872
17873 format_count = read_1_byte (abfd, buf);
17874 buf += 1;
17875 format_header_data = buf;
17876 for (formati = 0; formati < format_count; formati++)
17877 {
17878 read_unsigned_leb128 (abfd, buf, &bytes_read);
17879 buf += bytes_read;
17880 read_unsigned_leb128 (abfd, buf, &bytes_read);
17881 buf += bytes_read;
17882 }
17883
17884 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17885 buf += bytes_read;
17886 for (datai = 0; datai < data_count; datai++)
17887 {
17888 const gdb_byte *format = format_header_data;
17889 struct file_entry fe;
17890
43988095
JK
17891 for (formati = 0; formati < format_count; formati++)
17892 {
ecfb656c 17893 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17894 format += bytes_read;
43988095 17895
ecfb656c 17896 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17897 format += bytes_read;
ecfb656c
PA
17898
17899 gdb::optional<const char *> string;
17900 gdb::optional<unsigned int> uint;
17901
43988095
JK
17902 switch (form)
17903 {
17904 case DW_FORM_string:
ecfb656c 17905 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17906 buf += bytes_read;
17907 break;
17908
17909 case DW_FORM_line_strp:
ecfb656c
PA
17910 string.emplace (read_indirect_line_string (abfd, buf,
17911 cu_header,
17912 &bytes_read));
43988095
JK
17913 buf += bytes_read;
17914 break;
17915
17916 case DW_FORM_data1:
ecfb656c 17917 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17918 buf += 1;
17919 break;
17920
17921 case DW_FORM_data2:
ecfb656c 17922 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17923 buf += 2;
17924 break;
17925
17926 case DW_FORM_data4:
ecfb656c 17927 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17928 buf += 4;
17929 break;
17930
17931 case DW_FORM_data8:
ecfb656c 17932 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17933 buf += 8;
17934 break;
17935
17936 case DW_FORM_udata:
ecfb656c 17937 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17938 buf += bytes_read;
17939 break;
17940
17941 case DW_FORM_block:
17942 /* It is valid only for DW_LNCT_timestamp which is ignored by
17943 current GDB. */
17944 break;
17945 }
ecfb656c
PA
17946
17947 switch (content_type)
17948 {
17949 case DW_LNCT_path:
17950 if (string.has_value ())
17951 fe.name = *string;
17952 break;
17953 case DW_LNCT_directory_index:
17954 if (uint.has_value ())
17955 fe.d_index = (dir_index) *uint;
17956 break;
17957 case DW_LNCT_timestamp:
17958 if (uint.has_value ())
17959 fe.mod_time = *uint;
17960 break;
17961 case DW_LNCT_size:
17962 if (uint.has_value ())
17963 fe.length = *uint;
17964 break;
17965 case DW_LNCT_MD5:
17966 break;
17967 default:
17968 complaint (&symfile_complaints,
17969 _("Unknown format content type %s"),
17970 pulongest (content_type));
17971 }
43988095
JK
17972 }
17973
ecfb656c 17974 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17975 }
17976
17977 *bufp = buf;
17978}
17979
debd256d 17980/* Read the statement program header starting at OFFSET in
3019eac3 17981 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17982 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17983 Returns NULL if there is a problem reading the header, e.g., if it
17984 has a version we don't understand.
debd256d
JB
17985
17986 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17987 the returned object point into the dwarf line section buffer,
17988 and must not be freed. */
ae2de4f8 17989
fff8551c 17990static line_header_up
9c541725 17991dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17992{
d521ce57 17993 const gdb_byte *line_ptr;
c764a876 17994 unsigned int bytes_read, offset_size;
debd256d 17995 int i;
d521ce57 17996 const char *cur_dir, *cur_file;
3019eac3
DE
17997 struct dwarf2_section_info *section;
17998 bfd *abfd;
17999
36586728 18000 section = get_debug_line_section (cu);
3019eac3
DE
18001 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18002 if (section->buffer == NULL)
debd256d 18003 {
3019eac3
DE
18004 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18005 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18006 else
18007 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18008 return 0;
18009 }
18010
fceca515
DE
18011 /* We can't do this until we know the section is non-empty.
18012 Only then do we know we have such a section. */
a32a8923 18013 abfd = get_section_bfd_owner (section);
fceca515 18014
a738430d
MK
18015 /* Make sure that at least there's room for the total_length field.
18016 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18017 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18018 {
4d3c2250 18019 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18020 return 0;
18021 }
18022
fff8551c 18023 line_header_up lh (new line_header ());
debd256d 18024
9c541725 18025 lh->sect_off = sect_off;
527f3840
JK
18026 lh->offset_in_dwz = cu->per_cu->is_dwz;
18027
9c541725 18028 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18029
a738430d 18030 /* Read in the header. */
6e70227d 18031 lh->total_length =
c764a876
DE
18032 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18033 &bytes_read, &offset_size);
debd256d 18034 line_ptr += bytes_read;
3019eac3 18035 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18036 {
4d3c2250 18037 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18038 return 0;
18039 }
18040 lh->statement_program_end = line_ptr + lh->total_length;
18041 lh->version = read_2_bytes (abfd, line_ptr);
18042 line_ptr += 2;
43988095 18043 if (lh->version > 5)
cd366ee8
DE
18044 {
18045 /* This is a version we don't understand. The format could have
18046 changed in ways we don't handle properly so just punt. */
18047 complaint (&symfile_complaints,
18048 _("unsupported version in .debug_line section"));
18049 return NULL;
18050 }
43988095
JK
18051 if (lh->version >= 5)
18052 {
18053 gdb_byte segment_selector_size;
18054
18055 /* Skip address size. */
18056 read_1_byte (abfd, line_ptr);
18057 line_ptr += 1;
18058
18059 segment_selector_size = read_1_byte (abfd, line_ptr);
18060 line_ptr += 1;
18061 if (segment_selector_size != 0)
18062 {
18063 complaint (&symfile_complaints,
18064 _("unsupported segment selector size %u "
18065 "in .debug_line section"),
18066 segment_selector_size);
18067 return NULL;
18068 }
18069 }
c764a876
DE
18070 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18071 line_ptr += offset_size;
debd256d
JB
18072 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18073 line_ptr += 1;
2dc7f7b3
TT
18074 if (lh->version >= 4)
18075 {
18076 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18077 line_ptr += 1;
18078 }
18079 else
18080 lh->maximum_ops_per_instruction = 1;
18081
18082 if (lh->maximum_ops_per_instruction == 0)
18083 {
18084 lh->maximum_ops_per_instruction = 1;
18085 complaint (&symfile_complaints,
3e43a32a
MS
18086 _("invalid maximum_ops_per_instruction "
18087 "in `.debug_line' section"));
2dc7f7b3
TT
18088 }
18089
debd256d
JB
18090 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18091 line_ptr += 1;
18092 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18093 line_ptr += 1;
18094 lh->line_range = read_1_byte (abfd, line_ptr);
18095 line_ptr += 1;
18096 lh->opcode_base = read_1_byte (abfd, line_ptr);
18097 line_ptr += 1;
fff8551c 18098 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18099
18100 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18101 for (i = 1; i < lh->opcode_base; ++i)
18102 {
18103 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18104 line_ptr += 1;
18105 }
18106
43988095 18107 if (lh->version >= 5)
debd256d 18108 {
43988095 18109 /* Read directory 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 {
18115 lh->add_include_dir (name);
18116 });
debd256d 18117
43988095 18118 /* Read file name table. */
fff8551c
PA
18119 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18120 [] (struct line_header *lh, const char *name,
ecfb656c 18121 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18122 unsigned int length)
18123 {
ecfb656c 18124 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18125 });
43988095
JK
18126 }
18127 else
debd256d 18128 {
43988095
JK
18129 /* Read directory table. */
18130 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18131 {
18132 line_ptr += bytes_read;
fff8551c 18133 lh->add_include_dir (cur_dir);
43988095 18134 }
debd256d
JB
18135 line_ptr += bytes_read;
18136
43988095
JK
18137 /* Read file name table. */
18138 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18139 {
ecfb656c
PA
18140 unsigned int mod_time, length;
18141 dir_index d_index;
43988095
JK
18142
18143 line_ptr += bytes_read;
ecfb656c 18144 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18145 line_ptr += bytes_read;
18146 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18147 line_ptr += bytes_read;
18148 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18149 line_ptr += bytes_read;
18150
ecfb656c 18151 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18152 }
18153 line_ptr += bytes_read;
debd256d 18154 }
6e70227d 18155 lh->statement_program_start = line_ptr;
debd256d 18156
3019eac3 18157 if (line_ptr > (section->buffer + section->size))
4d3c2250 18158 complaint (&symfile_complaints,
3e43a32a
MS
18159 _("line number info header doesn't "
18160 "fit in `.debug_line' section"));
debd256d 18161
debd256d
JB
18162 return lh;
18163}
c906108c 18164
c6da4cef
DE
18165/* Subroutine of dwarf_decode_lines to simplify it.
18166 Return the file name of the psymtab for included file FILE_INDEX
18167 in line header LH of PST.
18168 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18169 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18170 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18171
18172 The function creates dangling cleanup registration. */
c6da4cef 18173
d521ce57 18174static const char *
c6da4cef
DE
18175psymtab_include_file_name (const struct line_header *lh, int file_index,
18176 const struct partial_symtab *pst,
18177 const char *comp_dir)
18178{
8c43009f 18179 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18180 const char *include_name = fe.name;
18181 const char *include_name_to_compare = include_name;
72b9f47f
TT
18182 const char *pst_filename;
18183 char *copied_name = NULL;
c6da4cef
DE
18184 int file_is_pst;
18185
8c43009f 18186 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18187
18188 if (!IS_ABSOLUTE_PATH (include_name)
18189 && (dir_name != NULL || comp_dir != NULL))
18190 {
18191 /* Avoid creating a duplicate psymtab for PST.
18192 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18193 Before we do the comparison, however, we need to account
18194 for DIR_NAME and COMP_DIR.
18195 First prepend dir_name (if non-NULL). If we still don't
18196 have an absolute path prepend comp_dir (if non-NULL).
18197 However, the directory we record in the include-file's
18198 psymtab does not contain COMP_DIR (to match the
18199 corresponding symtab(s)).
18200
18201 Example:
18202
18203 bash$ cd /tmp
18204 bash$ gcc -g ./hello.c
18205 include_name = "hello.c"
18206 dir_name = "."
18207 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18208 DW_AT_name = "./hello.c"
18209
18210 */
c6da4cef
DE
18211
18212 if (dir_name != NULL)
18213 {
d521ce57
TT
18214 char *tem = concat (dir_name, SLASH_STRING,
18215 include_name, (char *)NULL);
18216
18217 make_cleanup (xfree, tem);
18218 include_name = tem;
c6da4cef 18219 include_name_to_compare = include_name;
c6da4cef
DE
18220 }
18221 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18222 {
d521ce57
TT
18223 char *tem = concat (comp_dir, SLASH_STRING,
18224 include_name, (char *)NULL);
18225
18226 make_cleanup (xfree, tem);
18227 include_name_to_compare = tem;
c6da4cef
DE
18228 }
18229 }
18230
18231 pst_filename = pst->filename;
18232 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18233 {
72b9f47f
TT
18234 copied_name = concat (pst->dirname, SLASH_STRING,
18235 pst_filename, (char *)NULL);
18236 pst_filename = copied_name;
c6da4cef
DE
18237 }
18238
1e3fad37 18239 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18240
72b9f47f
TT
18241 if (copied_name != NULL)
18242 xfree (copied_name);
c6da4cef
DE
18243
18244 if (file_is_pst)
18245 return NULL;
18246 return include_name;
18247}
18248
d9b3de22
DE
18249/* State machine to track the state of the line number program. */
18250
6f77053d 18251class lnp_state_machine
d9b3de22 18252{
6f77053d
PA
18253public:
18254 /* Initialize a machine state for the start of a line number
18255 program. */
18256 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18257
8c43009f
PA
18258 file_entry *current_file ()
18259 {
18260 /* lh->file_names is 0-based, but the file name numbers in the
18261 statement program are 1-based. */
6f77053d
PA
18262 return m_line_header->file_name_at (m_file);
18263 }
18264
18265 /* Record the line in the state machine. END_SEQUENCE is true if
18266 we're processing the end of a sequence. */
18267 void record_line (bool end_sequence);
18268
18269 /* Check address and if invalid nop-out the rest of the lines in this
18270 sequence. */
18271 void check_line_address (struct dwarf2_cu *cu,
18272 const gdb_byte *line_ptr,
18273 CORE_ADDR lowpc, CORE_ADDR address);
18274
18275 void handle_set_discriminator (unsigned int discriminator)
18276 {
18277 m_discriminator = discriminator;
18278 m_line_has_non_zero_discriminator |= discriminator != 0;
18279 }
18280
18281 /* Handle DW_LNE_set_address. */
18282 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18283 {
18284 m_op_index = 0;
18285 address += baseaddr;
18286 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18287 }
18288
18289 /* Handle DW_LNS_advance_pc. */
18290 void handle_advance_pc (CORE_ADDR adjust);
18291
18292 /* Handle a special opcode. */
18293 void handle_special_opcode (unsigned char op_code);
18294
18295 /* Handle DW_LNS_advance_line. */
18296 void handle_advance_line (int line_delta)
18297 {
18298 advance_line (line_delta);
18299 }
18300
18301 /* Handle DW_LNS_set_file. */
18302 void handle_set_file (file_name_index file);
18303
18304 /* Handle DW_LNS_negate_stmt. */
18305 void handle_negate_stmt ()
18306 {
18307 m_is_stmt = !m_is_stmt;
18308 }
18309
18310 /* Handle DW_LNS_const_add_pc. */
18311 void handle_const_add_pc ();
18312
18313 /* Handle DW_LNS_fixed_advance_pc. */
18314 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18315 {
18316 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18317 m_op_index = 0;
18318 }
18319
18320 /* Handle DW_LNS_copy. */
18321 void handle_copy ()
18322 {
18323 record_line (false);
18324 m_discriminator = 0;
18325 }
18326
18327 /* Handle DW_LNE_end_sequence. */
18328 void handle_end_sequence ()
18329 {
18330 m_record_line_callback = ::record_line;
18331 }
18332
18333private:
18334 /* Advance the line by LINE_DELTA. */
18335 void advance_line (int line_delta)
18336 {
18337 m_line += line_delta;
18338
18339 if (line_delta != 0)
18340 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18341 }
18342
6f77053d
PA
18343 gdbarch *m_gdbarch;
18344
18345 /* True if we're recording lines.
18346 Otherwise we're building partial symtabs and are just interested in
18347 finding include files mentioned by the line number program. */
18348 bool m_record_lines_p;
18349
8c43009f 18350 /* The line number header. */
6f77053d 18351 line_header *m_line_header;
8c43009f 18352
6f77053d
PA
18353 /* These are part of the standard DWARF line number state machine,
18354 and initialized according to the DWARF spec. */
d9b3de22 18355
6f77053d 18356 unsigned char m_op_index = 0;
8c43009f 18357 /* The line table index (1-based) of the current file. */
6f77053d
PA
18358 file_name_index m_file = (file_name_index) 1;
18359 unsigned int m_line = 1;
18360
18361 /* These are initialized in the constructor. */
18362
18363 CORE_ADDR m_address;
18364 bool m_is_stmt;
18365 unsigned int m_discriminator;
d9b3de22
DE
18366
18367 /* Additional bits of state we need to track. */
18368
18369 /* The last file that we called dwarf2_start_subfile for.
18370 This is only used for TLLs. */
6f77053d 18371 unsigned int m_last_file = 0;
d9b3de22 18372 /* The last file a line number was recorded for. */
6f77053d 18373 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18374
18375 /* The function to call to record a line. */
6f77053d 18376 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18377
18378 /* The last line number that was recorded, used to coalesce
18379 consecutive entries for the same line. This can happen, for
18380 example, when discriminators are present. PR 17276. */
6f77053d
PA
18381 unsigned int m_last_line = 0;
18382 bool m_line_has_non_zero_discriminator = false;
8c43009f 18383};
d9b3de22 18384
6f77053d
PA
18385void
18386lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18387{
18388 CORE_ADDR addr_adj = (((m_op_index + adjust)
18389 / m_line_header->maximum_ops_per_instruction)
18390 * m_line_header->minimum_instruction_length);
18391 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18392 m_op_index = ((m_op_index + adjust)
18393 % m_line_header->maximum_ops_per_instruction);
18394}
d9b3de22 18395
6f77053d
PA
18396void
18397lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18398{
6f77053d
PA
18399 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18400 CORE_ADDR addr_adj = (((m_op_index
18401 + (adj_opcode / m_line_header->line_range))
18402 / m_line_header->maximum_ops_per_instruction)
18403 * m_line_header->minimum_instruction_length);
18404 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18405 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18406 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18407
6f77053d
PA
18408 int line_delta = (m_line_header->line_base
18409 + (adj_opcode % m_line_header->line_range));
18410 advance_line (line_delta);
18411 record_line (false);
18412 m_discriminator = 0;
18413}
d9b3de22 18414
6f77053d
PA
18415void
18416lnp_state_machine::handle_set_file (file_name_index file)
18417{
18418 m_file = file;
18419
18420 const file_entry *fe = current_file ();
18421 if (fe == NULL)
18422 dwarf2_debug_line_missing_file_complaint ();
18423 else if (m_record_lines_p)
18424 {
18425 const char *dir = fe->include_dir (m_line_header);
18426
18427 m_last_subfile = current_subfile;
18428 m_line_has_non_zero_discriminator = m_discriminator != 0;
18429 dwarf2_start_subfile (fe->name, dir);
18430 }
18431}
18432
18433void
18434lnp_state_machine::handle_const_add_pc ()
18435{
18436 CORE_ADDR adjust
18437 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18438
18439 CORE_ADDR addr_adj
18440 = (((m_op_index + adjust)
18441 / m_line_header->maximum_ops_per_instruction)
18442 * m_line_header->minimum_instruction_length);
18443
18444 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18445 m_op_index = ((m_op_index + adjust)
18446 % m_line_header->maximum_ops_per_instruction);
18447}
d9b3de22 18448
c91513d8
PP
18449/* Ignore this record_line request. */
18450
18451static void
18452noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18453{
18454 return;
18455}
18456
a05a36a5
DE
18457/* Return non-zero if we should add LINE to the line number table.
18458 LINE is the line to add, LAST_LINE is the last line that was added,
18459 LAST_SUBFILE is the subfile for LAST_LINE.
18460 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18461 had a non-zero discriminator.
18462
18463 We have to be careful in the presence of discriminators.
18464 E.g., for this line:
18465
18466 for (i = 0; i < 100000; i++);
18467
18468 clang can emit four line number entries for that one line,
18469 each with a different discriminator.
18470 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18471
18472 However, we want gdb to coalesce all four entries into one.
18473 Otherwise the user could stepi into the middle of the line and
18474 gdb would get confused about whether the pc really was in the
18475 middle of the line.
18476
18477 Things are further complicated by the fact that two consecutive
18478 line number entries for the same line is a heuristic used by gcc
18479 to denote the end of the prologue. So we can't just discard duplicate
18480 entries, we have to be selective about it. The heuristic we use is
18481 that we only collapse consecutive entries for the same line if at least
18482 one of those entries has a non-zero discriminator. PR 17276.
18483
18484 Note: Addresses in the line number state machine can never go backwards
18485 within one sequence, thus this coalescing is ok. */
18486
18487static int
18488dwarf_record_line_p (unsigned int line, unsigned int last_line,
18489 int line_has_non_zero_discriminator,
18490 struct subfile *last_subfile)
18491{
18492 if (current_subfile != last_subfile)
18493 return 1;
18494 if (line != last_line)
18495 return 1;
18496 /* Same line for the same file that we've seen already.
18497 As a last check, for pr 17276, only record the line if the line
18498 has never had a non-zero discriminator. */
18499 if (!line_has_non_zero_discriminator)
18500 return 1;
18501 return 0;
18502}
18503
252a6764
DE
18504/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18505 in the line table of subfile SUBFILE. */
18506
18507static void
d9b3de22
DE
18508dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18509 unsigned int line, CORE_ADDR address,
18510 record_line_ftype p_record_line)
252a6764
DE
18511{
18512 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18513
27e0867f
DE
18514 if (dwarf_line_debug)
18515 {
18516 fprintf_unfiltered (gdb_stdlog,
18517 "Recording line %u, file %s, address %s\n",
18518 line, lbasename (subfile->name),
18519 paddress (gdbarch, address));
18520 }
18521
d5962de5 18522 (*p_record_line) (subfile, line, addr);
252a6764
DE
18523}
18524
18525/* Subroutine of dwarf_decode_lines_1 to simplify it.
18526 Mark the end of a set of line number records.
d9b3de22 18527 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18528 If SUBFILE is NULL the request is ignored. */
18529
18530static void
18531dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18532 CORE_ADDR address, record_line_ftype p_record_line)
18533{
27e0867f
DE
18534 if (subfile == NULL)
18535 return;
18536
18537 if (dwarf_line_debug)
18538 {
18539 fprintf_unfiltered (gdb_stdlog,
18540 "Finishing current line, file %s, address %s\n",
18541 lbasename (subfile->name),
18542 paddress (gdbarch, address));
18543 }
18544
d9b3de22
DE
18545 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18546}
18547
6f77053d
PA
18548void
18549lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18550{
d9b3de22
DE
18551 if (dwarf_line_debug)
18552 {
18553 fprintf_unfiltered (gdb_stdlog,
18554 "Processing actual line %u: file %u,"
18555 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18556 m_line, to_underlying (m_file),
18557 paddress (m_gdbarch, m_address),
18558 m_is_stmt, m_discriminator);
d9b3de22
DE
18559 }
18560
6f77053d 18561 file_entry *fe = current_file ();
8c43009f
PA
18562
18563 if (fe == NULL)
d9b3de22
DE
18564 dwarf2_debug_line_missing_file_complaint ();
18565 /* For now we ignore lines not starting on an instruction boundary.
18566 But not when processing end_sequence for compatibility with the
18567 previous version of the code. */
6f77053d 18568 else if (m_op_index == 0 || end_sequence)
d9b3de22 18569 {
8c43009f 18570 fe->included_p = 1;
6f77053d 18571 if (m_record_lines_p && m_is_stmt)
d9b3de22 18572 {
6f77053d 18573 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18574 {
6f77053d
PA
18575 dwarf_finish_line (m_gdbarch, m_last_subfile,
18576 m_address, m_record_line_callback);
d9b3de22
DE
18577 }
18578
18579 if (!end_sequence)
18580 {
6f77053d
PA
18581 if (dwarf_record_line_p (m_line, m_last_line,
18582 m_line_has_non_zero_discriminator,
18583 m_last_subfile))
d9b3de22 18584 {
6f77053d
PA
18585 dwarf_record_line_1 (m_gdbarch, current_subfile,
18586 m_line, m_address,
18587 m_record_line_callback);
d9b3de22 18588 }
6f77053d
PA
18589 m_last_subfile = current_subfile;
18590 m_last_line = m_line;
d9b3de22
DE
18591 }
18592 }
18593 }
18594}
18595
6f77053d
PA
18596lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18597 bool record_lines_p)
d9b3de22 18598{
6f77053d
PA
18599 m_gdbarch = arch;
18600 m_record_lines_p = record_lines_p;
18601 m_line_header = lh;
d9b3de22 18602
6f77053d 18603 m_record_line_callback = ::record_line;
d9b3de22 18604
d9b3de22
DE
18605 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18606 was a line entry for it so that the backend has a chance to adjust it
18607 and also record it in case it needs it. This is currently used by MIPS
18608 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18609 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18610 m_is_stmt = lh->default_is_stmt;
18611 m_discriminator = 0;
252a6764
DE
18612}
18613
6f77053d
PA
18614void
18615lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18616 const gdb_byte *line_ptr,
18617 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18618{
18619 /* If address < lowpc then it's not a usable value, it's outside the
18620 pc range of the CU. However, we restrict the test to only address
18621 values of zero to preserve GDB's previous behaviour which is to
18622 handle the specific case of a function being GC'd by the linker. */
18623
18624 if (address == 0 && address < lowpc)
18625 {
18626 /* This line table is for a function which has been
18627 GCd by the linker. Ignore it. PR gdb/12528 */
18628
18629 struct objfile *objfile = cu->objfile;
18630 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18631
18632 complaint (&symfile_complaints,
18633 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18634 line_offset, objfile_name (objfile));
6f77053d
PA
18635 m_record_line_callback = noop_record_line;
18636 /* Note: record_line_callback is left as noop_record_line until
18637 we see DW_LNE_end_sequence. */
924c2928
DE
18638 }
18639}
18640
f3f5162e 18641/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18642 Process the line number information in LH.
18643 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18644 program in order to set included_p for every referenced header. */
debd256d 18645
c906108c 18646static void
43f3e411
DE
18647dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18648 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18649{
d521ce57
TT
18650 const gdb_byte *line_ptr, *extended_end;
18651 const gdb_byte *line_end;
a8c50c1f 18652 unsigned int bytes_read, extended_len;
699ca60a 18653 unsigned char op_code, extended_op;
e142c38c
DJ
18654 CORE_ADDR baseaddr;
18655 struct objfile *objfile = cu->objfile;
f3f5162e 18656 bfd *abfd = objfile->obfd;
fbf65064 18657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18658 /* True if we're recording line info (as opposed to building partial
18659 symtabs and just interested in finding include files mentioned by
18660 the line number program). */
18661 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18662
18663 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18664
debd256d
JB
18665 line_ptr = lh->statement_program_start;
18666 line_end = lh->statement_program_end;
c906108c
SS
18667
18668 /* Read the statement sequences until there's nothing left. */
18669 while (line_ptr < line_end)
18670 {
6f77053d
PA
18671 /* The DWARF line number program state machine. Reset the state
18672 machine at the start of each sequence. */
18673 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18674 bool end_sequence = false;
d9b3de22 18675
8c43009f 18676 if (record_lines_p)
c906108c 18677 {
8c43009f
PA
18678 /* Start a subfile for the current file of the state
18679 machine. */
18680 const file_entry *fe = state_machine.current_file ();
18681
18682 if (fe != NULL)
18683 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18684 }
18685
a738430d 18686 /* Decode the table. */
d9b3de22 18687 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18688 {
18689 op_code = read_1_byte (abfd, line_ptr);
18690 line_ptr += 1;
9aa1fe7e 18691
debd256d 18692 if (op_code >= lh->opcode_base)
6e70227d 18693 {
8e07a239 18694 /* Special opcode. */
6f77053d 18695 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18696 }
18697 else switch (op_code)
c906108c
SS
18698 {
18699 case DW_LNS_extended_op:
3e43a32a
MS
18700 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18701 &bytes_read);
473b7be6 18702 line_ptr += bytes_read;
a8c50c1f 18703 extended_end = line_ptr + extended_len;
c906108c
SS
18704 extended_op = read_1_byte (abfd, line_ptr);
18705 line_ptr += 1;
18706 switch (extended_op)
18707 {
18708 case DW_LNE_end_sequence:
6f77053d
PA
18709 state_machine.handle_end_sequence ();
18710 end_sequence = true;
c906108c
SS
18711 break;
18712 case DW_LNE_set_address:
d9b3de22
DE
18713 {
18714 CORE_ADDR address
18715 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18716 line_ptr += bytes_read;
6f77053d
PA
18717
18718 state_machine.check_line_address (cu, line_ptr,
18719 lowpc, address);
18720 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18721 }
c906108c
SS
18722 break;
18723 case DW_LNE_define_file:
debd256d 18724 {
d521ce57 18725 const char *cur_file;
ecfb656c
PA
18726 unsigned int mod_time, length;
18727 dir_index dindex;
6e70227d 18728
3e43a32a
MS
18729 cur_file = read_direct_string (abfd, line_ptr,
18730 &bytes_read);
debd256d 18731 line_ptr += bytes_read;
ecfb656c 18732 dindex = (dir_index)
debd256d
JB
18733 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18734 line_ptr += bytes_read;
18735 mod_time =
18736 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18737 line_ptr += bytes_read;
18738 length =
18739 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18740 line_ptr += bytes_read;
ecfb656c 18741 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18742 }
c906108c 18743 break;
d0c6ba3d 18744 case DW_LNE_set_discriminator:
6f77053d
PA
18745 {
18746 /* The discriminator is not interesting to the
18747 debugger; just ignore it. We still need to
18748 check its value though:
18749 if there are consecutive entries for the same
18750 (non-prologue) line we want to coalesce them.
18751 PR 17276. */
18752 unsigned int discr
18753 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18754 line_ptr += bytes_read;
18755
18756 state_machine.handle_set_discriminator (discr);
18757 }
d0c6ba3d 18758 break;
c906108c 18759 default:
4d3c2250 18760 complaint (&symfile_complaints,
e2e0b3e5 18761 _("mangled .debug_line section"));
debd256d 18762 return;
c906108c 18763 }
a8c50c1f
DJ
18764 /* Make sure that we parsed the extended op correctly. If e.g.
18765 we expected a different address size than the producer used,
18766 we may have read the wrong number of bytes. */
18767 if (line_ptr != extended_end)
18768 {
18769 complaint (&symfile_complaints,
18770 _("mangled .debug_line section"));
18771 return;
18772 }
c906108c
SS
18773 break;
18774 case DW_LNS_copy:
6f77053d 18775 state_machine.handle_copy ();
c906108c
SS
18776 break;
18777 case DW_LNS_advance_pc:
2dc7f7b3
TT
18778 {
18779 CORE_ADDR adjust
18780 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18781 line_ptr += bytes_read;
6f77053d
PA
18782
18783 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18784 }
c906108c
SS
18785 break;
18786 case DW_LNS_advance_line:
a05a36a5
DE
18787 {
18788 int line_delta
18789 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18790 line_ptr += bytes_read;
6f77053d
PA
18791
18792 state_machine.handle_advance_line (line_delta);
a05a36a5 18793 }
c906108c
SS
18794 break;
18795 case DW_LNS_set_file:
d9b3de22 18796 {
6f77053d 18797 file_name_index file
ecfb656c
PA
18798 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18799 &bytes_read);
d9b3de22 18800 line_ptr += bytes_read;
8c43009f 18801
6f77053d 18802 state_machine.handle_set_file (file);
d9b3de22 18803 }
c906108c
SS
18804 break;
18805 case DW_LNS_set_column:
0ad93d4f 18806 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18807 line_ptr += bytes_read;
18808 break;
18809 case DW_LNS_negate_stmt:
6f77053d 18810 state_machine.handle_negate_stmt ();
c906108c
SS
18811 break;
18812 case DW_LNS_set_basic_block:
c906108c 18813 break;
c2c6d25f
JM
18814 /* Add to the address register of the state machine the
18815 address increment value corresponding to special opcode
a738430d
MK
18816 255. I.e., this value is scaled by the minimum
18817 instruction length since special opcode 255 would have
b021a221 18818 scaled the increment. */
c906108c 18819 case DW_LNS_const_add_pc:
6f77053d 18820 state_machine.handle_const_add_pc ();
c906108c
SS
18821 break;
18822 case DW_LNS_fixed_advance_pc:
3e29f34a 18823 {
6f77053d 18824 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18825 line_ptr += 2;
6f77053d
PA
18826
18827 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18828 }
c906108c 18829 break;
9aa1fe7e 18830 default:
a738430d
MK
18831 {
18832 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18833 int i;
a738430d 18834
debd256d 18835 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18836 {
18837 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18838 line_ptr += bytes_read;
18839 }
18840 }
c906108c
SS
18841 }
18842 }
d9b3de22
DE
18843
18844 if (!end_sequence)
18845 dwarf2_debug_line_missing_end_sequence_complaint ();
18846
18847 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18848 in which case we still finish recording the last line). */
6f77053d 18849 state_machine.record_line (true);
c906108c 18850 }
f3f5162e
DE
18851}
18852
18853/* Decode the Line Number Program (LNP) for the given line_header
18854 structure and CU. The actual information extracted and the type
18855 of structures created from the LNP depends on the value of PST.
18856
18857 1. If PST is NULL, then this procedure uses the data from the program
18858 to create all necessary symbol tables, and their linetables.
18859
18860 2. If PST is not NULL, this procedure reads the program to determine
18861 the list of files included by the unit represented by PST, and
18862 builds all the associated partial symbol tables.
18863
18864 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18865 It is used for relative paths in the line table.
18866 NOTE: When processing partial symtabs (pst != NULL),
18867 comp_dir == pst->dirname.
18868
18869 NOTE: It is important that psymtabs have the same file name (via strcmp)
18870 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18871 symtab we don't use it in the name of the psymtabs we create.
18872 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18873 A good testcase for this is mb-inline.exp.
18874
527f3840
JK
18875 LOWPC is the lowest address in CU (or 0 if not known).
18876
18877 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18878 for its PC<->lines mapping information. Otherwise only the filename
18879 table is read in. */
f3f5162e
DE
18880
18881static void
18882dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18883 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18884 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18885{
18886 struct objfile *objfile = cu->objfile;
18887 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18888
527f3840
JK
18889 if (decode_mapping)
18890 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18891
18892 if (decode_for_pst_p)
18893 {
18894 int file_index;
18895
18896 /* Now that we're done scanning the Line Header Program, we can
18897 create the psymtab of each included file. */
fff8551c 18898 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18899 if (lh->file_names[file_index].included_p == 1)
18900 {
d521ce57 18901 const char *include_name =
c6da4cef
DE
18902 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18903 if (include_name != NULL)
aaa75496
JB
18904 dwarf2_create_include_psymtab (include_name, pst, objfile);
18905 }
18906 }
cb1df416
DJ
18907 else
18908 {
18909 /* Make sure a symtab is created for every file, even files
18910 which contain only variables (i.e. no code with associated
18911 line numbers). */
43f3e411 18912 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18913 int i;
cb1df416 18914
fff8551c 18915 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18916 {
8c43009f 18917 file_entry &fe = lh->file_names[i];
9a619af0 18918
8c43009f 18919 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18920
cb1df416 18921 if (current_subfile->symtab == NULL)
43f3e411
DE
18922 {
18923 current_subfile->symtab
18924 = allocate_symtab (cust, current_subfile->name);
18925 }
8c43009f 18926 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18927 }
18928 }
c906108c
SS
18929}
18930
18931/* Start a subfile for DWARF. FILENAME is the name of the file and
18932 DIRNAME the name of the source directory which contains FILENAME
4d663531 18933 or NULL if not known.
c906108c
SS
18934 This routine tries to keep line numbers from identical absolute and
18935 relative file names in a common subfile.
18936
18937 Using the `list' example from the GDB testsuite, which resides in
18938 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18939 of /srcdir/list0.c yields the following debugging information for list0.c:
18940
c5aa993b 18941 DW_AT_name: /srcdir/list0.c
4d663531 18942 DW_AT_comp_dir: /compdir
357e46e7 18943 files.files[0].name: list0.h
c5aa993b 18944 files.files[0].dir: /srcdir
357e46e7 18945 files.files[1].name: list0.c
c5aa993b 18946 files.files[1].dir: /srcdir
c906108c
SS
18947
18948 The line number information for list0.c has to end up in a single
4f1520fb
FR
18949 subfile, so that `break /srcdir/list0.c:1' works as expected.
18950 start_subfile will ensure that this happens provided that we pass the
18951 concatenation of files.files[1].dir and files.files[1].name as the
18952 subfile's name. */
c906108c
SS
18953
18954static void
4d663531 18955dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18956{
d521ce57 18957 char *copy = NULL;
4f1520fb 18958
4d663531 18959 /* In order not to lose the line information directory,
4f1520fb
FR
18960 we concatenate it to the filename when it makes sense.
18961 Note that the Dwarf3 standard says (speaking of filenames in line
18962 information): ``The directory index is ignored for file names
18963 that represent full path names''. Thus ignoring dirname in the
18964 `else' branch below isn't an issue. */
c906108c 18965
d5166ae1 18966 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18967 {
18968 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18969 filename = copy;
18970 }
c906108c 18971
4d663531 18972 start_subfile (filename);
4f1520fb 18973
d521ce57
TT
18974 if (copy != NULL)
18975 xfree (copy);
c906108c
SS
18976}
18977
f4dc4d17
DE
18978/* Start a symtab for DWARF.
18979 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18980
43f3e411 18981static struct compunit_symtab *
f4dc4d17 18982dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18983 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18984{
43f3e411
DE
18985 struct compunit_symtab *cust
18986 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18987
f4dc4d17
DE
18988 record_debugformat ("DWARF 2");
18989 record_producer (cu->producer);
18990
18991 /* We assume that we're processing GCC output. */
18992 processing_gcc_compilation = 2;
18993
4d4ec4e5 18994 cu->processing_has_namespace_info = 0;
43f3e411
DE
18995
18996 return cust;
f4dc4d17
DE
18997}
18998
4c2df51b
DJ
18999static void
19000var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19001 struct dwarf2_cu *cu)
4c2df51b 19002{
e7c27a73
DJ
19003 struct objfile *objfile = cu->objfile;
19004 struct comp_unit_head *cu_header = &cu->header;
19005
4c2df51b
DJ
19006 /* NOTE drow/2003-01-30: There used to be a comment and some special
19007 code here to turn a symbol with DW_AT_external and a
19008 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19009 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19010 with some versions of binutils) where shared libraries could have
19011 relocations against symbols in their debug information - the
19012 minimal symbol would have the right address, but the debug info
19013 would not. It's no longer necessary, because we will explicitly
19014 apply relocations when we read in the debug information now. */
19015
19016 /* A DW_AT_location attribute with no contents indicates that a
19017 variable has been optimized away. */
19018 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19019 {
f1e6e072 19020 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19021 return;
19022 }
19023
19024 /* Handle one degenerate form of location expression specially, to
19025 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19026 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19027 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19028
19029 if (attr_form_is_block (attr)
3019eac3
DE
19030 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19031 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19032 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19033 && (DW_BLOCK (attr)->size
19034 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19035 {
891d2f0b 19036 unsigned int dummy;
4c2df51b 19037
3019eac3
DE
19038 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19039 SYMBOL_VALUE_ADDRESS (sym) =
19040 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19041 else
19042 SYMBOL_VALUE_ADDRESS (sym) =
19043 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19044 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19045 fixup_symbol_section (sym, objfile);
19046 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19047 SYMBOL_SECTION (sym));
4c2df51b
DJ
19048 return;
19049 }
19050
19051 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19052 expression evaluator, and use LOC_COMPUTED only when necessary
19053 (i.e. when the value of a register or memory location is
19054 referenced, or a thread-local block, etc.). Then again, it might
19055 not be worthwhile. I'm assuming that it isn't unless performance
19056 or memory numbers show me otherwise. */
19057
f1e6e072 19058 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19059
f1e6e072 19060 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19061 cu->has_loclist = 1;
4c2df51b
DJ
19062}
19063
c906108c
SS
19064/* Given a pointer to a DWARF information entry, figure out if we need
19065 to make a symbol table entry for it, and if so, create a new entry
19066 and return a pointer to it.
19067 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19068 used the passed type.
19069 If SPACE is not NULL, use it to hold the new symbol. If it is
19070 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19071
19072static struct symbol *
34eaf542
TT
19073new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19074 struct symbol *space)
c906108c 19075{
e7c27a73 19076 struct objfile *objfile = cu->objfile;
3e29f34a 19077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19078 struct symbol *sym = NULL;
15d034d0 19079 const char *name;
c906108c
SS
19080 struct attribute *attr = NULL;
19081 struct attribute *attr2 = NULL;
e142c38c 19082 CORE_ADDR baseaddr;
e37fd15a
SW
19083 struct pending **list_to_add = NULL;
19084
edb3359d 19085 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19086
19087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19088
94af9270 19089 name = dwarf2_name (die, cu);
c906108c
SS
19090 if (name)
19091 {
94af9270 19092 const char *linkagename;
34eaf542 19093 int suppress_add = 0;
94af9270 19094
34eaf542
TT
19095 if (space)
19096 sym = space;
19097 else
e623cf5d 19098 sym = allocate_symbol (objfile);
c906108c 19099 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19100
19101 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19102 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19103 linkagename = dwarf2_physname (name, die, cu);
19104 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19105
f55ee35c
JK
19106 /* Fortran does not have mangling standard and the mangling does differ
19107 between gfortran, iFort etc. */
19108 if (cu->language == language_fortran
b250c185 19109 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19110 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19111 dwarf2_full_name (name, die, cu),
29df156d 19112 NULL);
f55ee35c 19113
c906108c 19114 /* Default assumptions.
c5aa993b 19115 Use the passed type or decode it from the die. */
176620f1 19116 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19117 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19118 if (type != NULL)
19119 SYMBOL_TYPE (sym) = type;
19120 else
e7c27a73 19121 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19122 attr = dwarf2_attr (die,
19123 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19124 cu);
c906108c
SS
19125 if (attr)
19126 {
19127 SYMBOL_LINE (sym) = DW_UNSND (attr);
19128 }
cb1df416 19129
edb3359d
DJ
19130 attr = dwarf2_attr (die,
19131 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19132 cu);
cb1df416
DJ
19133 if (attr)
19134 {
ecfb656c 19135 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19136 struct file_entry *fe;
9a619af0 19137
ecfb656c
PA
19138 if (cu->line_header != NULL)
19139 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19140 else
19141 fe = NULL;
19142
19143 if (fe == NULL)
cb1df416
DJ
19144 complaint (&symfile_complaints,
19145 _("file index out of range"));
8c43009f
PA
19146 else
19147 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19148 }
19149
c906108c
SS
19150 switch (die->tag)
19151 {
19152 case DW_TAG_label:
e142c38c 19153 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19154 if (attr)
3e29f34a
MR
19155 {
19156 CORE_ADDR addr;
19157
19158 addr = attr_value_as_address (attr);
19159 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19160 SYMBOL_VALUE_ADDRESS (sym) = addr;
19161 }
0f5238ed
TT
19162 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19163 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19164 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19165 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19166 break;
19167 case DW_TAG_subprogram:
19168 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19169 finish_block. */
f1e6e072 19170 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19171 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19172 if ((attr2 && (DW_UNSND (attr2) != 0))
19173 || cu->language == language_ada)
c906108c 19174 {
2cfa0c8d
JB
19175 /* Subprograms marked external are stored as a global symbol.
19176 Ada subprograms, whether marked external or not, are always
19177 stored as a global symbol, because we want to be able to
19178 access them globally. For instance, we want to be able
19179 to break on a nested subprogram without having to
19180 specify the context. */
e37fd15a 19181 list_to_add = &global_symbols;
c906108c
SS
19182 }
19183 else
19184 {
e37fd15a 19185 list_to_add = cu->list_in_scope;
c906108c
SS
19186 }
19187 break;
edb3359d
DJ
19188 case DW_TAG_inlined_subroutine:
19189 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19190 finish_block. */
f1e6e072 19191 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19192 SYMBOL_INLINED (sym) = 1;
481860b3 19193 list_to_add = cu->list_in_scope;
edb3359d 19194 break;
34eaf542
TT
19195 case DW_TAG_template_value_param:
19196 suppress_add = 1;
19197 /* Fall through. */
72929c62 19198 case DW_TAG_constant:
c906108c 19199 case DW_TAG_variable:
254e6b9e 19200 case DW_TAG_member:
0963b4bd
MS
19201 /* Compilation with minimal debug info may result in
19202 variables with missing type entries. Change the
19203 misleading `void' type to something sensible. */
c906108c 19204 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19205 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19206
e142c38c 19207 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19208 /* In the case of DW_TAG_member, we should only be called for
19209 static const members. */
19210 if (die->tag == DW_TAG_member)
19211 {
3863f96c
DE
19212 /* dwarf2_add_field uses die_is_declaration,
19213 so we do the same. */
254e6b9e
DE
19214 gdb_assert (die_is_declaration (die, cu));
19215 gdb_assert (attr);
19216 }
c906108c
SS
19217 if (attr)
19218 {
e7c27a73 19219 dwarf2_const_value (attr, sym, cu);
e142c38c 19220 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19221 if (!suppress_add)
34eaf542
TT
19222 {
19223 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19224 list_to_add = &global_symbols;
34eaf542 19225 else
e37fd15a 19226 list_to_add = cu->list_in_scope;
34eaf542 19227 }
c906108c
SS
19228 break;
19229 }
e142c38c 19230 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19231 if (attr)
19232 {
e7c27a73 19233 var_decode_location (attr, sym, cu);
e142c38c 19234 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19235
19236 /* Fortran explicitly imports any global symbols to the local
19237 scope by DW_TAG_common_block. */
19238 if (cu->language == language_fortran && die->parent
19239 && die->parent->tag == DW_TAG_common_block)
19240 attr2 = NULL;
19241
caac4577
JG
19242 if (SYMBOL_CLASS (sym) == LOC_STATIC
19243 && SYMBOL_VALUE_ADDRESS (sym) == 0
19244 && !dwarf2_per_objfile->has_section_at_zero)
19245 {
19246 /* When a static variable is eliminated by the linker,
19247 the corresponding debug information is not stripped
19248 out, but the variable address is set to null;
19249 do not add such variables into symbol table. */
19250 }
19251 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19252 {
f55ee35c
JK
19253 /* Workaround gfortran PR debug/40040 - it uses
19254 DW_AT_location for variables in -fPIC libraries which may
19255 get overriden by other libraries/executable and get
19256 a different address. Resolve it by the minimal symbol
19257 which may come from inferior's executable using copy
19258 relocation. Make this workaround only for gfortran as for
19259 other compilers GDB cannot guess the minimal symbol
19260 Fortran mangling kind. */
19261 if (cu->language == language_fortran && die->parent
19262 && die->parent->tag == DW_TAG_module
19263 && cu->producer
28586665 19264 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19265 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19266
1c809c68
TT
19267 /* A variable with DW_AT_external is never static,
19268 but it may be block-scoped. */
19269 list_to_add = (cu->list_in_scope == &file_symbols
19270 ? &global_symbols : cu->list_in_scope);
1c809c68 19271 }
c906108c 19272 else
e37fd15a 19273 list_to_add = cu->list_in_scope;
c906108c
SS
19274 }
19275 else
19276 {
19277 /* We do not know the address of this symbol.
c5aa993b
JM
19278 If it is an external symbol and we have type information
19279 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19280 The address of the variable will then be determined from
19281 the minimal symbol table whenever the variable is
19282 referenced. */
e142c38c 19283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19284
19285 /* Fortran explicitly imports any global symbols to the local
19286 scope by DW_TAG_common_block. */
19287 if (cu->language == language_fortran && die->parent
19288 && die->parent->tag == DW_TAG_common_block)
19289 {
19290 /* SYMBOL_CLASS doesn't matter here because
19291 read_common_block is going to reset it. */
19292 if (!suppress_add)
19293 list_to_add = cu->list_in_scope;
19294 }
19295 else if (attr2 && (DW_UNSND (attr2) != 0)
19296 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19297 {
0fe7935b
DJ
19298 /* A variable with DW_AT_external is never static, but it
19299 may be block-scoped. */
19300 list_to_add = (cu->list_in_scope == &file_symbols
19301 ? &global_symbols : cu->list_in_scope);
19302
f1e6e072 19303 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19304 }
442ddf59
JK
19305 else if (!die_is_declaration (die, cu))
19306 {
19307 /* Use the default LOC_OPTIMIZED_OUT class. */
19308 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19309 if (!suppress_add)
19310 list_to_add = cu->list_in_scope;
442ddf59 19311 }
c906108c
SS
19312 }
19313 break;
19314 case DW_TAG_formal_parameter:
edb3359d
DJ
19315 /* If we are inside a function, mark this as an argument. If
19316 not, we might be looking at an argument to an inlined function
19317 when we do not have enough information to show inlined frames;
19318 pretend it's a local variable in that case so that the user can
19319 still see it. */
19320 if (context_stack_depth > 0
19321 && context_stack[context_stack_depth - 1].name != NULL)
19322 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19323 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19324 if (attr)
19325 {
e7c27a73 19326 var_decode_location (attr, sym, cu);
c906108c 19327 }
e142c38c 19328 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19329 if (attr)
19330 {
e7c27a73 19331 dwarf2_const_value (attr, sym, cu);
c906108c 19332 }
f346a30d 19333
e37fd15a 19334 list_to_add = cu->list_in_scope;
c906108c
SS
19335 break;
19336 case DW_TAG_unspecified_parameters:
19337 /* From varargs functions; gdb doesn't seem to have any
19338 interest in this information, so just ignore it for now.
19339 (FIXME?) */
19340 break;
34eaf542
TT
19341 case DW_TAG_template_type_param:
19342 suppress_add = 1;
19343 /* Fall through. */
c906108c 19344 case DW_TAG_class_type:
680b30c7 19345 case DW_TAG_interface_type:
c906108c
SS
19346 case DW_TAG_structure_type:
19347 case DW_TAG_union_type:
72019c9c 19348 case DW_TAG_set_type:
c906108c 19349 case DW_TAG_enumeration_type:
f1e6e072 19350 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19351 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19352
63d06c5c 19353 {
9c37b5ae 19354 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19355 really ever be static objects: otherwise, if you try
19356 to, say, break of a class's method and you're in a file
19357 which doesn't mention that class, it won't work unless
19358 the check for all static symbols in lookup_symbol_aux
19359 saves you. See the OtherFileClass tests in
19360 gdb.c++/namespace.exp. */
19361
e37fd15a 19362 if (!suppress_add)
34eaf542 19363 {
34eaf542 19364 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19365 && cu->language == language_cplus
34eaf542 19366 ? &global_symbols : cu->list_in_scope);
63d06c5c 19367
64382290 19368 /* The semantics of C++ state that "struct foo {
9c37b5ae 19369 ... }" also defines a typedef for "foo". */
64382290 19370 if (cu->language == language_cplus
45280282 19371 || cu->language == language_ada
c44af4eb
TT
19372 || cu->language == language_d
19373 || cu->language == language_rust)
64382290
TT
19374 {
19375 /* The symbol's name is already allocated along
19376 with this objfile, so we don't need to
19377 duplicate it for the type. */
19378 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19379 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19380 }
63d06c5c
DC
19381 }
19382 }
c906108c
SS
19383 break;
19384 case DW_TAG_typedef:
f1e6e072 19385 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19386 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19387 list_to_add = cu->list_in_scope;
63d06c5c 19388 break;
c906108c 19389 case DW_TAG_base_type:
a02abb62 19390 case DW_TAG_subrange_type:
f1e6e072 19391 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19392 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19393 list_to_add = cu->list_in_scope;
c906108c
SS
19394 break;
19395 case DW_TAG_enumerator:
e142c38c 19396 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19397 if (attr)
19398 {
e7c27a73 19399 dwarf2_const_value (attr, sym, cu);
c906108c 19400 }
63d06c5c
DC
19401 {
19402 /* NOTE: carlton/2003-11-10: See comment above in the
19403 DW_TAG_class_type, etc. block. */
19404
e142c38c 19405 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19406 && cu->language == language_cplus
e142c38c 19407 ? &global_symbols : cu->list_in_scope);
63d06c5c 19408 }
c906108c 19409 break;
74921315 19410 case DW_TAG_imported_declaration:
5c4e30ca 19411 case DW_TAG_namespace:
f1e6e072 19412 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19413 list_to_add = &global_symbols;
5c4e30ca 19414 break;
530e8392
KB
19415 case DW_TAG_module:
19416 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19417 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19418 list_to_add = &global_symbols;
19419 break;
4357ac6c 19420 case DW_TAG_common_block:
f1e6e072 19421 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19422 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19423 add_symbol_to_list (sym, cu->list_in_scope);
19424 break;
c906108c
SS
19425 default:
19426 /* Not a tag we recognize. Hopefully we aren't processing
19427 trash data, but since we must specifically ignore things
19428 we don't recognize, there is nothing else we should do at
0963b4bd 19429 this point. */
e2e0b3e5 19430 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19431 dwarf_tag_name (die->tag));
c906108c
SS
19432 break;
19433 }
df8a16a1 19434
e37fd15a
SW
19435 if (suppress_add)
19436 {
19437 sym->hash_next = objfile->template_symbols;
19438 objfile->template_symbols = sym;
19439 list_to_add = NULL;
19440 }
19441
19442 if (list_to_add != NULL)
19443 add_symbol_to_list (sym, list_to_add);
19444
df8a16a1
DJ
19445 /* For the benefit of old versions of GCC, check for anonymous
19446 namespaces based on the demangled name. */
4d4ec4e5 19447 if (!cu->processing_has_namespace_info
94af9270 19448 && cu->language == language_cplus)
a10964d1 19449 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19450 }
19451 return (sym);
19452}
19453
34eaf542
TT
19454/* A wrapper for new_symbol_full that always allocates a new symbol. */
19455
19456static struct symbol *
19457new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19458{
19459 return new_symbol_full (die, type, cu, NULL);
19460}
19461
98bfdba5
PA
19462/* Given an attr with a DW_FORM_dataN value in host byte order,
19463 zero-extend it as appropriate for the symbol's type. The DWARF
19464 standard (v4) is not entirely clear about the meaning of using
19465 DW_FORM_dataN for a constant with a signed type, where the type is
19466 wider than the data. The conclusion of a discussion on the DWARF
19467 list was that this is unspecified. We choose to always zero-extend
19468 because that is the interpretation long in use by GCC. */
c906108c 19469
98bfdba5 19470static gdb_byte *
ff39bb5e 19471dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19472 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19473{
e7c27a73 19474 struct objfile *objfile = cu->objfile;
e17a4113
UW
19475 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19476 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19477 LONGEST l = DW_UNSND (attr);
19478
19479 if (bits < sizeof (*value) * 8)
19480 {
19481 l &= ((LONGEST) 1 << bits) - 1;
19482 *value = l;
19483 }
19484 else if (bits == sizeof (*value) * 8)
19485 *value = l;
19486 else
19487 {
224c3ddb 19488 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19489 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19490 return bytes;
19491 }
19492
19493 return NULL;
19494}
19495
19496/* Read a constant value from an attribute. Either set *VALUE, or if
19497 the value does not fit in *VALUE, set *BYTES - either already
19498 allocated on the objfile obstack, or newly allocated on OBSTACK,
19499 or, set *BATON, if we translated the constant to a location
19500 expression. */
19501
19502static void
ff39bb5e 19503dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19504 const char *name, struct obstack *obstack,
19505 struct dwarf2_cu *cu,
d521ce57 19506 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19507 struct dwarf2_locexpr_baton **baton)
19508{
19509 struct objfile *objfile = cu->objfile;
19510 struct comp_unit_head *cu_header = &cu->header;
c906108c 19511 struct dwarf_block *blk;
98bfdba5
PA
19512 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19513 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19514
19515 *value = 0;
19516 *bytes = NULL;
19517 *baton = NULL;
c906108c
SS
19518
19519 switch (attr->form)
19520 {
19521 case DW_FORM_addr:
3019eac3 19522 case DW_FORM_GNU_addr_index:
ac56253d 19523 {
ac56253d
TT
19524 gdb_byte *data;
19525
98bfdba5
PA
19526 if (TYPE_LENGTH (type) != cu_header->addr_size)
19527 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19528 cu_header->addr_size,
98bfdba5 19529 TYPE_LENGTH (type));
ac56253d
TT
19530 /* Symbols of this form are reasonably rare, so we just
19531 piggyback on the existing location code rather than writing
19532 a new implementation of symbol_computed_ops. */
8d749320 19533 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19534 (*baton)->per_cu = cu->per_cu;
19535 gdb_assert ((*baton)->per_cu);
ac56253d 19536
98bfdba5 19537 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19538 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19539 (*baton)->data = data;
ac56253d
TT
19540
19541 data[0] = DW_OP_addr;
19542 store_unsigned_integer (&data[1], cu_header->addr_size,
19543 byte_order, DW_ADDR (attr));
19544 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19545 }
c906108c 19546 break;
4ac36638 19547 case DW_FORM_string:
93b5768b 19548 case DW_FORM_strp:
3019eac3 19549 case DW_FORM_GNU_str_index:
36586728 19550 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19551 /* DW_STRING is already allocated on the objfile obstack, point
19552 directly to it. */
d521ce57 19553 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19554 break;
c906108c
SS
19555 case DW_FORM_block1:
19556 case DW_FORM_block2:
19557 case DW_FORM_block4:
19558 case DW_FORM_block:
2dc7f7b3 19559 case DW_FORM_exprloc:
0224619f 19560 case DW_FORM_data16:
c906108c 19561 blk = DW_BLOCK (attr);
98bfdba5
PA
19562 if (TYPE_LENGTH (type) != blk->size)
19563 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19564 TYPE_LENGTH (type));
19565 *bytes = blk->data;
c906108c 19566 break;
2df3850c
JM
19567
19568 /* The DW_AT_const_value attributes are supposed to carry the
19569 symbol's value "represented as it would be on the target
19570 architecture." By the time we get here, it's already been
19571 converted to host endianness, so we just need to sign- or
19572 zero-extend it as appropriate. */
19573 case DW_FORM_data1:
3aef2284 19574 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19575 break;
c906108c 19576 case DW_FORM_data2:
3aef2284 19577 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19578 break;
c906108c 19579 case DW_FORM_data4:
3aef2284 19580 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19581 break;
c906108c 19582 case DW_FORM_data8:
3aef2284 19583 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19584 break;
19585
c906108c 19586 case DW_FORM_sdata:
663c44ac 19587 case DW_FORM_implicit_const:
98bfdba5 19588 *value = DW_SND (attr);
2df3850c
JM
19589 break;
19590
c906108c 19591 case DW_FORM_udata:
98bfdba5 19592 *value = DW_UNSND (attr);
c906108c 19593 break;
2df3850c 19594
c906108c 19595 default:
4d3c2250 19596 complaint (&symfile_complaints,
e2e0b3e5 19597 _("unsupported const value attribute form: '%s'"),
4d3c2250 19598 dwarf_form_name (attr->form));
98bfdba5 19599 *value = 0;
c906108c
SS
19600 break;
19601 }
19602}
19603
2df3850c 19604
98bfdba5
PA
19605/* Copy constant value from an attribute to a symbol. */
19606
2df3850c 19607static void
ff39bb5e 19608dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19609 struct dwarf2_cu *cu)
2df3850c 19610{
98bfdba5 19611 struct objfile *objfile = cu->objfile;
12df843f 19612 LONGEST value;
d521ce57 19613 const gdb_byte *bytes;
98bfdba5 19614 struct dwarf2_locexpr_baton *baton;
2df3850c 19615
98bfdba5
PA
19616 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19617 SYMBOL_PRINT_NAME (sym),
19618 &objfile->objfile_obstack, cu,
19619 &value, &bytes, &baton);
2df3850c 19620
98bfdba5
PA
19621 if (baton != NULL)
19622 {
98bfdba5 19623 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19624 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19625 }
19626 else if (bytes != NULL)
19627 {
19628 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19629 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19630 }
19631 else
19632 {
19633 SYMBOL_VALUE (sym) = value;
f1e6e072 19634 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19635 }
2df3850c
JM
19636}
19637
c906108c
SS
19638/* Return the type of the die in question using its DW_AT_type attribute. */
19639
19640static struct type *
e7c27a73 19641die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19642{
c906108c 19643 struct attribute *type_attr;
c906108c 19644
e142c38c 19645 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19646 if (!type_attr)
19647 {
19648 /* A missing DW_AT_type represents a void type. */
46bf5051 19649 return objfile_type (cu->objfile)->builtin_void;
c906108c 19650 }
348e048f 19651
673bfd45 19652 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19653}
19654
b4ba55a1
JB
19655/* True iff CU's producer generates GNAT Ada auxiliary information
19656 that allows to find parallel types through that information instead
19657 of having to do expensive parallel lookups by type name. */
19658
19659static int
19660need_gnat_info (struct dwarf2_cu *cu)
19661{
19662 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19663 of GNAT produces this auxiliary information, without any indication
19664 that it is produced. Part of enhancing the FSF version of GNAT
19665 to produce that information will be to put in place an indicator
19666 that we can use in order to determine whether the descriptive type
19667 info is available or not. One suggestion that has been made is
19668 to use a new attribute, attached to the CU die. For now, assume
19669 that the descriptive type info is not available. */
19670 return 0;
19671}
19672
b4ba55a1
JB
19673/* Return the auxiliary type of the die in question using its
19674 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19675 attribute is not present. */
19676
19677static struct type *
19678die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19679{
b4ba55a1 19680 struct attribute *type_attr;
b4ba55a1
JB
19681
19682 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19683 if (!type_attr)
19684 return NULL;
19685
673bfd45 19686 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19687}
19688
19689/* If DIE has a descriptive_type attribute, then set the TYPE's
19690 descriptive type accordingly. */
19691
19692static void
19693set_descriptive_type (struct type *type, struct die_info *die,
19694 struct dwarf2_cu *cu)
19695{
19696 struct type *descriptive_type = die_descriptive_type (die, cu);
19697
19698 if (descriptive_type)
19699 {
19700 ALLOCATE_GNAT_AUX_TYPE (type);
19701 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19702 }
19703}
19704
c906108c
SS
19705/* Return the containing type of the die in question using its
19706 DW_AT_containing_type attribute. */
19707
19708static struct type *
e7c27a73 19709die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19710{
c906108c 19711 struct attribute *type_attr;
c906108c 19712
e142c38c 19713 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19714 if (!type_attr)
19715 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19716 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19717
673bfd45 19718 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19719}
19720
ac9ec31b
DE
19721/* Return an error marker type to use for the ill formed type in DIE/CU. */
19722
19723static struct type *
19724build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19725{
19726 struct objfile *objfile = dwarf2_per_objfile->objfile;
19727 char *message, *saved;
19728
19729 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19730 objfile_name (objfile),
9c541725
PA
19731 to_underlying (cu->header.sect_off),
19732 to_underlying (die->sect_off));
224c3ddb
SM
19733 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19734 message, strlen (message));
ac9ec31b
DE
19735 xfree (message);
19736
19f392bc 19737 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19738}
19739
673bfd45 19740/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19741 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19742 DW_AT_containing_type.
673bfd45
DE
19743 If there is no type substitute an error marker. */
19744
c906108c 19745static struct type *
ff39bb5e 19746lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19747 struct dwarf2_cu *cu)
c906108c 19748{
bb5ed363 19749 struct objfile *objfile = cu->objfile;
f792889a
DJ
19750 struct type *this_type;
19751
ac9ec31b
DE
19752 gdb_assert (attr->name == DW_AT_type
19753 || attr->name == DW_AT_GNAT_descriptive_type
19754 || attr->name == DW_AT_containing_type);
19755
673bfd45
DE
19756 /* First see if we have it cached. */
19757
36586728
TT
19758 if (attr->form == DW_FORM_GNU_ref_alt)
19759 {
19760 struct dwarf2_per_cu_data *per_cu;
9c541725 19761 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19762
9c541725
PA
19763 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19764 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19765 }
7771576e 19766 else if (attr_form_is_ref (attr))
673bfd45 19767 {
9c541725 19768 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19769
9c541725 19770 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19771 }
55f1336d 19772 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19773 {
ac9ec31b 19774 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19775
ac9ec31b 19776 return get_signatured_type (die, signature, cu);
673bfd45
DE
19777 }
19778 else
19779 {
ac9ec31b
DE
19780 complaint (&symfile_complaints,
19781 _("Dwarf Error: Bad type attribute %s in DIE"
19782 " at 0x%x [in module %s]"),
9c541725 19783 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19784 objfile_name (objfile));
ac9ec31b 19785 return build_error_marker_type (cu, die);
673bfd45
DE
19786 }
19787
19788 /* If not cached we need to read it in. */
19789
19790 if (this_type == NULL)
19791 {
ac9ec31b 19792 struct die_info *type_die = NULL;
673bfd45
DE
19793 struct dwarf2_cu *type_cu = cu;
19794
7771576e 19795 if (attr_form_is_ref (attr))
ac9ec31b
DE
19796 type_die = follow_die_ref (die, attr, &type_cu);
19797 if (type_die == NULL)
19798 return build_error_marker_type (cu, die);
19799 /* If we find the type now, it's probably because the type came
3019eac3
DE
19800 from an inter-CU reference and the type's CU got expanded before
19801 ours. */
ac9ec31b 19802 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19803 }
19804
19805 /* If we still don't have a type use an error marker. */
19806
19807 if (this_type == NULL)
ac9ec31b 19808 return build_error_marker_type (cu, die);
673bfd45 19809
f792889a 19810 return this_type;
c906108c
SS
19811}
19812
673bfd45
DE
19813/* Return the type in DIE, CU.
19814 Returns NULL for invalid types.
19815
02142a6c 19816 This first does a lookup in die_type_hash,
673bfd45
DE
19817 and only reads the die in if necessary.
19818
19819 NOTE: This can be called when reading in partial or full symbols. */
19820
f792889a 19821static struct type *
e7c27a73 19822read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19823{
f792889a
DJ
19824 struct type *this_type;
19825
19826 this_type = get_die_type (die, cu);
19827 if (this_type)
19828 return this_type;
19829
673bfd45
DE
19830 return read_type_die_1 (die, cu);
19831}
19832
19833/* Read the type in DIE, CU.
19834 Returns NULL for invalid types. */
19835
19836static struct type *
19837read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19838{
19839 struct type *this_type = NULL;
19840
c906108c
SS
19841 switch (die->tag)
19842 {
19843 case DW_TAG_class_type:
680b30c7 19844 case DW_TAG_interface_type:
c906108c
SS
19845 case DW_TAG_structure_type:
19846 case DW_TAG_union_type:
f792889a 19847 this_type = read_structure_type (die, cu);
c906108c
SS
19848 break;
19849 case DW_TAG_enumeration_type:
f792889a 19850 this_type = read_enumeration_type (die, cu);
c906108c
SS
19851 break;
19852 case DW_TAG_subprogram:
19853 case DW_TAG_subroutine_type:
edb3359d 19854 case DW_TAG_inlined_subroutine:
f792889a 19855 this_type = read_subroutine_type (die, cu);
c906108c
SS
19856 break;
19857 case DW_TAG_array_type:
f792889a 19858 this_type = read_array_type (die, cu);
c906108c 19859 break;
72019c9c 19860 case DW_TAG_set_type:
f792889a 19861 this_type = read_set_type (die, cu);
72019c9c 19862 break;
c906108c 19863 case DW_TAG_pointer_type:
f792889a 19864 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19865 break;
19866 case DW_TAG_ptr_to_member_type:
f792889a 19867 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19868 break;
19869 case DW_TAG_reference_type:
4297a3f0
AV
19870 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19871 break;
19872 case DW_TAG_rvalue_reference_type:
19873 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19874 break;
19875 case DW_TAG_const_type:
f792889a 19876 this_type = read_tag_const_type (die, cu);
c906108c
SS
19877 break;
19878 case DW_TAG_volatile_type:
f792889a 19879 this_type = read_tag_volatile_type (die, cu);
c906108c 19880 break;
06d66ee9
TT
19881 case DW_TAG_restrict_type:
19882 this_type = read_tag_restrict_type (die, cu);
19883 break;
c906108c 19884 case DW_TAG_string_type:
f792889a 19885 this_type = read_tag_string_type (die, cu);
c906108c
SS
19886 break;
19887 case DW_TAG_typedef:
f792889a 19888 this_type = read_typedef (die, cu);
c906108c 19889 break;
a02abb62 19890 case DW_TAG_subrange_type:
f792889a 19891 this_type = read_subrange_type (die, cu);
a02abb62 19892 break;
c906108c 19893 case DW_TAG_base_type:
f792889a 19894 this_type = read_base_type (die, cu);
c906108c 19895 break;
81a17f79 19896 case DW_TAG_unspecified_type:
f792889a 19897 this_type = read_unspecified_type (die, cu);
81a17f79 19898 break;
0114d602
DJ
19899 case DW_TAG_namespace:
19900 this_type = read_namespace_type (die, cu);
19901 break;
f55ee35c
JK
19902 case DW_TAG_module:
19903 this_type = read_module_type (die, cu);
19904 break;
a2c2acaf
MW
19905 case DW_TAG_atomic_type:
19906 this_type = read_tag_atomic_type (die, cu);
19907 break;
c906108c 19908 default:
3e43a32a
MS
19909 complaint (&symfile_complaints,
19910 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19911 dwarf_tag_name (die->tag));
c906108c
SS
19912 break;
19913 }
63d06c5c 19914
f792889a 19915 return this_type;
63d06c5c
DC
19916}
19917
abc72ce4
DE
19918/* See if we can figure out if the class lives in a namespace. We do
19919 this by looking for a member function; its demangled name will
19920 contain namespace info, if there is any.
19921 Return the computed name or NULL.
19922 Space for the result is allocated on the objfile's obstack.
19923 This is the full-die version of guess_partial_die_structure_name.
19924 In this case we know DIE has no useful parent. */
19925
19926static char *
19927guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19928{
19929 struct die_info *spec_die;
19930 struct dwarf2_cu *spec_cu;
19931 struct die_info *child;
19932
19933 spec_cu = cu;
19934 spec_die = die_specification (die, &spec_cu);
19935 if (spec_die != NULL)
19936 {
19937 die = spec_die;
19938 cu = spec_cu;
19939 }
19940
19941 for (child = die->child;
19942 child != NULL;
19943 child = child->sibling)
19944 {
19945 if (child->tag == DW_TAG_subprogram)
19946 {
73b9be8b 19947 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 19948
7d45c7c3 19949 if (linkage_name != NULL)
abc72ce4
DE
19950 {
19951 char *actual_name
19952 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19953 linkage_name);
abc72ce4
DE
19954 char *name = NULL;
19955
19956 if (actual_name != NULL)
19957 {
15d034d0 19958 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19959
19960 if (die_name != NULL
19961 && strcmp (die_name, actual_name) != 0)
19962 {
19963 /* Strip off the class name from the full name.
19964 We want the prefix. */
19965 int die_name_len = strlen (die_name);
19966 int actual_name_len = strlen (actual_name);
19967
19968 /* Test for '::' as a sanity check. */
19969 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19970 && actual_name[actual_name_len
19971 - die_name_len - 1] == ':')
224c3ddb
SM
19972 name = (char *) obstack_copy0 (
19973 &cu->objfile->per_bfd->storage_obstack,
19974 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19975 }
19976 }
19977 xfree (actual_name);
19978 return name;
19979 }
19980 }
19981 }
19982
19983 return NULL;
19984}
19985
96408a79
SA
19986/* GCC might emit a nameless typedef that has a linkage name. Determine the
19987 prefix part in such case. See
19988 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19989
a121b7c1 19990static const char *
96408a79
SA
19991anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19992{
19993 struct attribute *attr;
e6a959d6 19994 const char *base;
96408a79
SA
19995
19996 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19997 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19998 return NULL;
19999
7d45c7c3 20000 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20001 return NULL;
20002
73b9be8b 20003 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20004 if (attr == NULL || DW_STRING (attr) == NULL)
20005 return NULL;
20006
20007 /* dwarf2_name had to be already called. */
20008 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20009
20010 /* Strip the base name, keep any leading namespaces/classes. */
20011 base = strrchr (DW_STRING (attr), ':');
20012 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20013 return "";
20014
224c3ddb
SM
20015 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20016 DW_STRING (attr),
20017 &base[-1] - DW_STRING (attr));
96408a79
SA
20018}
20019
fdde2d81 20020/* Return the name of the namespace/class that DIE is defined within,
0114d602 20021 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20022
0114d602
DJ
20023 For example, if we're within the method foo() in the following
20024 code:
20025
20026 namespace N {
20027 class C {
20028 void foo () {
20029 }
20030 };
20031 }
20032
20033 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20034
0d5cff50 20035static const char *
e142c38c 20036determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20037{
0114d602
DJ
20038 struct die_info *parent, *spec_die;
20039 struct dwarf2_cu *spec_cu;
20040 struct type *parent_type;
a121b7c1 20041 const char *retval;
63d06c5c 20042
9c37b5ae 20043 if (cu->language != language_cplus
c44af4eb
TT
20044 && cu->language != language_fortran && cu->language != language_d
20045 && cu->language != language_rust)
0114d602
DJ
20046 return "";
20047
96408a79
SA
20048 retval = anonymous_struct_prefix (die, cu);
20049 if (retval)
20050 return retval;
20051
0114d602
DJ
20052 /* We have to be careful in the presence of DW_AT_specification.
20053 For example, with GCC 3.4, given the code
20054
20055 namespace N {
20056 void foo() {
20057 // Definition of N::foo.
20058 }
20059 }
20060
20061 then we'll have a tree of DIEs like this:
20062
20063 1: DW_TAG_compile_unit
20064 2: DW_TAG_namespace // N
20065 3: DW_TAG_subprogram // declaration of N::foo
20066 4: DW_TAG_subprogram // definition of N::foo
20067 DW_AT_specification // refers to die #3
20068
20069 Thus, when processing die #4, we have to pretend that we're in
20070 the context of its DW_AT_specification, namely the contex of die
20071 #3. */
20072 spec_cu = cu;
20073 spec_die = die_specification (die, &spec_cu);
20074 if (spec_die == NULL)
20075 parent = die->parent;
20076 else
63d06c5c 20077 {
0114d602
DJ
20078 parent = spec_die->parent;
20079 cu = spec_cu;
63d06c5c 20080 }
0114d602
DJ
20081
20082 if (parent == NULL)
20083 return "";
98bfdba5
PA
20084 else if (parent->building_fullname)
20085 {
20086 const char *name;
20087 const char *parent_name;
20088
20089 /* It has been seen on RealView 2.2 built binaries,
20090 DW_TAG_template_type_param types actually _defined_ as
20091 children of the parent class:
20092
20093 enum E {};
20094 template class <class Enum> Class{};
20095 Class<enum E> class_e;
20096
20097 1: DW_TAG_class_type (Class)
20098 2: DW_TAG_enumeration_type (E)
20099 3: DW_TAG_enumerator (enum1:0)
20100 3: DW_TAG_enumerator (enum2:1)
20101 ...
20102 2: DW_TAG_template_type_param
20103 DW_AT_type DW_FORM_ref_udata (E)
20104
20105 Besides being broken debug info, it can put GDB into an
20106 infinite loop. Consider:
20107
20108 When we're building the full name for Class<E>, we'll start
20109 at Class, and go look over its template type parameters,
20110 finding E. We'll then try to build the full name of E, and
20111 reach here. We're now trying to build the full name of E,
20112 and look over the parent DIE for containing scope. In the
20113 broken case, if we followed the parent DIE of E, we'd again
20114 find Class, and once again go look at its template type
20115 arguments, etc., etc. Simply don't consider such parent die
20116 as source-level parent of this die (it can't be, the language
20117 doesn't allow it), and break the loop here. */
20118 name = dwarf2_name (die, cu);
20119 parent_name = dwarf2_name (parent, cu);
20120 complaint (&symfile_complaints,
20121 _("template param type '%s' defined within parent '%s'"),
20122 name ? name : "<unknown>",
20123 parent_name ? parent_name : "<unknown>");
20124 return "";
20125 }
63d06c5c 20126 else
0114d602
DJ
20127 switch (parent->tag)
20128 {
63d06c5c 20129 case DW_TAG_namespace:
0114d602 20130 parent_type = read_type_die (parent, cu);
acebe513
UW
20131 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20132 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20133 Work around this problem here. */
20134 if (cu->language == language_cplus
20135 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20136 return "";
0114d602
DJ
20137 /* We give a name to even anonymous namespaces. */
20138 return TYPE_TAG_NAME (parent_type);
63d06c5c 20139 case DW_TAG_class_type:
680b30c7 20140 case DW_TAG_interface_type:
63d06c5c 20141 case DW_TAG_structure_type:
0114d602 20142 case DW_TAG_union_type:
f55ee35c 20143 case DW_TAG_module:
0114d602
DJ
20144 parent_type = read_type_die (parent, cu);
20145 if (TYPE_TAG_NAME (parent_type) != NULL)
20146 return TYPE_TAG_NAME (parent_type);
20147 else
20148 /* An anonymous structure is only allowed non-static data
20149 members; no typedefs, no member functions, et cetera.
20150 So it does not need a prefix. */
20151 return "";
abc72ce4 20152 case DW_TAG_compile_unit:
95554aad 20153 case DW_TAG_partial_unit:
abc72ce4
DE
20154 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20155 if (cu->language == language_cplus
8b70b953 20156 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20157 && die->child != NULL
20158 && (die->tag == DW_TAG_class_type
20159 || die->tag == DW_TAG_structure_type
20160 || die->tag == DW_TAG_union_type))
20161 {
20162 char *name = guess_full_die_structure_name (die, cu);
20163 if (name != NULL)
20164 return name;
20165 }
20166 return "";
3d567982
TT
20167 case DW_TAG_enumeration_type:
20168 parent_type = read_type_die (parent, cu);
20169 if (TYPE_DECLARED_CLASS (parent_type))
20170 {
20171 if (TYPE_TAG_NAME (parent_type) != NULL)
20172 return TYPE_TAG_NAME (parent_type);
20173 return "";
20174 }
20175 /* Fall through. */
63d06c5c 20176 default:
8176b9b8 20177 return determine_prefix (parent, cu);
63d06c5c 20178 }
63d06c5c
DC
20179}
20180
3e43a32a
MS
20181/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20182 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20183 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20184 an obconcat, otherwise allocate storage for the result. The CU argument is
20185 used to determine the language and hence, the appropriate separator. */
987504bb 20186
f55ee35c 20187#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20188
20189static char *
f55ee35c
JK
20190typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20191 int physname, struct dwarf2_cu *cu)
63d06c5c 20192{
f55ee35c 20193 const char *lead = "";
5c315b68 20194 const char *sep;
63d06c5c 20195
3e43a32a
MS
20196 if (suffix == NULL || suffix[0] == '\0'
20197 || prefix == NULL || prefix[0] == '\0')
987504bb 20198 sep = "";
45280282
IB
20199 else if (cu->language == language_d)
20200 {
20201 /* For D, the 'main' function could be defined in any module, but it
20202 should never be prefixed. */
20203 if (strcmp (suffix, "D main") == 0)
20204 {
20205 prefix = "";
20206 sep = "";
20207 }
20208 else
20209 sep = ".";
20210 }
f55ee35c
JK
20211 else if (cu->language == language_fortran && physname)
20212 {
20213 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20214 DW_AT_MIPS_linkage_name is preferred and used instead. */
20215
20216 lead = "__";
20217 sep = "_MOD_";
20218 }
987504bb
JJ
20219 else
20220 sep = "::";
63d06c5c 20221
6dd47d34
DE
20222 if (prefix == NULL)
20223 prefix = "";
20224 if (suffix == NULL)
20225 suffix = "";
20226
987504bb
JJ
20227 if (obs == NULL)
20228 {
3e43a32a 20229 char *retval
224c3ddb
SM
20230 = ((char *)
20231 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20232
f55ee35c
JK
20233 strcpy (retval, lead);
20234 strcat (retval, prefix);
6dd47d34
DE
20235 strcat (retval, sep);
20236 strcat (retval, suffix);
63d06c5c
DC
20237 return retval;
20238 }
987504bb
JJ
20239 else
20240 {
20241 /* We have an obstack. */
f55ee35c 20242 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20243 }
63d06c5c
DC
20244}
20245
c906108c
SS
20246/* Return sibling of die, NULL if no sibling. */
20247
f9aca02d 20248static struct die_info *
fba45db2 20249sibling_die (struct die_info *die)
c906108c 20250{
639d11d3 20251 return die->sibling;
c906108c
SS
20252}
20253
71c25dea
TT
20254/* Get name of a die, return NULL if not found. */
20255
15d034d0
TT
20256static const char *
20257dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20258 struct obstack *obstack)
20259{
20260 if (name && cu->language == language_cplus)
20261 {
2f408ecb 20262 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20263
2f408ecb 20264 if (!canon_name.empty ())
71c25dea 20265 {
2f408ecb
PA
20266 if (canon_name != name)
20267 name = (const char *) obstack_copy0 (obstack,
20268 canon_name.c_str (),
20269 canon_name.length ());
71c25dea
TT
20270 }
20271 }
20272
20273 return name;
c906108c
SS
20274}
20275
96553a0c
DE
20276/* Get name of a die, return NULL if not found.
20277 Anonymous namespaces are converted to their magic string. */
9219021c 20278
15d034d0 20279static const char *
e142c38c 20280dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20281{
20282 struct attribute *attr;
20283
e142c38c 20284 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20285 if ((!attr || !DW_STRING (attr))
96553a0c 20286 && die->tag != DW_TAG_namespace
53832f31
TT
20287 && die->tag != DW_TAG_class_type
20288 && die->tag != DW_TAG_interface_type
20289 && die->tag != DW_TAG_structure_type
20290 && die->tag != DW_TAG_union_type)
71c25dea
TT
20291 return NULL;
20292
20293 switch (die->tag)
20294 {
20295 case DW_TAG_compile_unit:
95554aad 20296 case DW_TAG_partial_unit:
71c25dea
TT
20297 /* Compilation units have a DW_AT_name that is a filename, not
20298 a source language identifier. */
20299 case DW_TAG_enumeration_type:
20300 case DW_TAG_enumerator:
20301 /* These tags always have simple identifiers already; no need
20302 to canonicalize them. */
20303 return DW_STRING (attr);
907af001 20304
96553a0c
DE
20305 case DW_TAG_namespace:
20306 if (attr != NULL && DW_STRING (attr) != NULL)
20307 return DW_STRING (attr);
20308 return CP_ANONYMOUS_NAMESPACE_STR;
20309
907af001
UW
20310 case DW_TAG_class_type:
20311 case DW_TAG_interface_type:
20312 case DW_TAG_structure_type:
20313 case DW_TAG_union_type:
20314 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20315 structures or unions. These were of the form "._%d" in GCC 4.1,
20316 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20317 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20318 if (attr && DW_STRING (attr)
61012eef
GB
20319 && (startswith (DW_STRING (attr), "._")
20320 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20321 return NULL;
53832f31
TT
20322
20323 /* GCC might emit a nameless typedef that has a linkage name. See
20324 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20325 if (!attr || DW_STRING (attr) == NULL)
20326 {
df5c6c50 20327 char *demangled = NULL;
53832f31 20328
73b9be8b 20329 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20330 if (attr == NULL || DW_STRING (attr) == NULL)
20331 return NULL;
20332
df5c6c50
JK
20333 /* Avoid demangling DW_STRING (attr) the second time on a second
20334 call for the same DIE. */
20335 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20336 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20337
20338 if (demangled)
20339 {
e6a959d6 20340 const char *base;
96408a79 20341
53832f31 20342 /* FIXME: we already did this for the partial symbol... */
34a68019 20343 DW_STRING (attr)
224c3ddb
SM
20344 = ((const char *)
20345 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20346 demangled, strlen (demangled)));
53832f31
TT
20347 DW_STRING_IS_CANONICAL (attr) = 1;
20348 xfree (demangled);
96408a79
SA
20349
20350 /* Strip any leading namespaces/classes, keep only the base name.
20351 DW_AT_name for named DIEs does not contain the prefixes. */
20352 base = strrchr (DW_STRING (attr), ':');
20353 if (base && base > DW_STRING (attr) && base[-1] == ':')
20354 return &base[1];
20355 else
20356 return DW_STRING (attr);
53832f31
TT
20357 }
20358 }
907af001
UW
20359 break;
20360
71c25dea 20361 default:
907af001
UW
20362 break;
20363 }
20364
20365 if (!DW_STRING_IS_CANONICAL (attr))
20366 {
20367 DW_STRING (attr)
20368 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20369 &cu->objfile->per_bfd->storage_obstack);
907af001 20370 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20371 }
907af001 20372 return DW_STRING (attr);
9219021c
DC
20373}
20374
20375/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20376 is none. *EXT_CU is the CU containing DIE on input, and the CU
20377 containing the return value on output. */
9219021c
DC
20378
20379static struct die_info *
f2f0e013 20380dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20381{
20382 struct attribute *attr;
9219021c 20383
f2f0e013 20384 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20385 if (attr == NULL)
20386 return NULL;
20387
f2f0e013 20388 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20389}
20390
c906108c
SS
20391/* Convert a DIE tag into its string name. */
20392
f39c6ffd 20393static const char *
aa1ee363 20394dwarf_tag_name (unsigned tag)
c906108c 20395{
f39c6ffd
TT
20396 const char *name = get_DW_TAG_name (tag);
20397
20398 if (name == NULL)
20399 return "DW_TAG_<unknown>";
20400
20401 return name;
c906108c
SS
20402}
20403
20404/* Convert a DWARF attribute code into its string name. */
20405
f39c6ffd 20406static const char *
aa1ee363 20407dwarf_attr_name (unsigned attr)
c906108c 20408{
f39c6ffd
TT
20409 const char *name;
20410
c764a876 20411#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20412 if (attr == DW_AT_MIPS_fde)
20413 return "DW_AT_MIPS_fde";
20414#else
20415 if (attr == DW_AT_HP_block_index)
20416 return "DW_AT_HP_block_index";
c764a876 20417#endif
f39c6ffd
TT
20418
20419 name = get_DW_AT_name (attr);
20420
20421 if (name == NULL)
20422 return "DW_AT_<unknown>";
20423
20424 return name;
c906108c
SS
20425}
20426
20427/* Convert a DWARF value form code into its string name. */
20428
f39c6ffd 20429static const char *
aa1ee363 20430dwarf_form_name (unsigned form)
c906108c 20431{
f39c6ffd
TT
20432 const char *name = get_DW_FORM_name (form);
20433
20434 if (name == NULL)
20435 return "DW_FORM_<unknown>";
20436
20437 return name;
c906108c
SS
20438}
20439
a121b7c1 20440static const char *
fba45db2 20441dwarf_bool_name (unsigned mybool)
c906108c
SS
20442{
20443 if (mybool)
20444 return "TRUE";
20445 else
20446 return "FALSE";
20447}
20448
20449/* Convert a DWARF type code into its string name. */
20450
f39c6ffd 20451static const char *
aa1ee363 20452dwarf_type_encoding_name (unsigned enc)
c906108c 20453{
f39c6ffd 20454 const char *name = get_DW_ATE_name (enc);
c906108c 20455
f39c6ffd
TT
20456 if (name == NULL)
20457 return "DW_ATE_<unknown>";
c906108c 20458
f39c6ffd 20459 return name;
c906108c 20460}
c906108c 20461
f9aca02d 20462static void
d97bc12b 20463dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20464{
20465 unsigned int i;
20466
d97bc12b
DE
20467 print_spaces (indent, f);
20468 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20469 dwarf_tag_name (die->tag), die->abbrev,
20470 to_underlying (die->sect_off));
d97bc12b
DE
20471
20472 if (die->parent != NULL)
20473 {
20474 print_spaces (indent, f);
20475 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20476 to_underlying (die->parent->sect_off));
d97bc12b
DE
20477 }
20478
20479 print_spaces (indent, f);
20480 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20481 dwarf_bool_name (die->child != NULL));
c906108c 20482
d97bc12b
DE
20483 print_spaces (indent, f);
20484 fprintf_unfiltered (f, " attributes:\n");
20485
c906108c
SS
20486 for (i = 0; i < die->num_attrs; ++i)
20487 {
d97bc12b
DE
20488 print_spaces (indent, f);
20489 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20490 dwarf_attr_name (die->attrs[i].name),
20491 dwarf_form_name (die->attrs[i].form));
d97bc12b 20492
c906108c
SS
20493 switch (die->attrs[i].form)
20494 {
c906108c 20495 case DW_FORM_addr:
3019eac3 20496 case DW_FORM_GNU_addr_index:
d97bc12b 20497 fprintf_unfiltered (f, "address: ");
5af949e3 20498 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20499 break;
20500 case DW_FORM_block2:
20501 case DW_FORM_block4:
20502 case DW_FORM_block:
20503 case DW_FORM_block1:
56eb65bd
SP
20504 fprintf_unfiltered (f, "block: size %s",
20505 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20506 break;
2dc7f7b3 20507 case DW_FORM_exprloc:
56eb65bd
SP
20508 fprintf_unfiltered (f, "expression: size %s",
20509 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20510 break;
0224619f
JK
20511 case DW_FORM_data16:
20512 fprintf_unfiltered (f, "constant of 16 bytes");
20513 break;
4568ecf9
DE
20514 case DW_FORM_ref_addr:
20515 fprintf_unfiltered (f, "ref address: ");
20516 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20517 break;
36586728
TT
20518 case DW_FORM_GNU_ref_alt:
20519 fprintf_unfiltered (f, "alt ref address: ");
20520 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20521 break;
10b3939b
DJ
20522 case DW_FORM_ref1:
20523 case DW_FORM_ref2:
20524 case DW_FORM_ref4:
4568ecf9
DE
20525 case DW_FORM_ref8:
20526 case DW_FORM_ref_udata:
d97bc12b 20527 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20528 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20529 break;
c906108c
SS
20530 case DW_FORM_data1:
20531 case DW_FORM_data2:
20532 case DW_FORM_data4:
ce5d95e1 20533 case DW_FORM_data8:
c906108c
SS
20534 case DW_FORM_udata:
20535 case DW_FORM_sdata:
43bbcdc2
PH
20536 fprintf_unfiltered (f, "constant: %s",
20537 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20538 break;
2dc7f7b3
TT
20539 case DW_FORM_sec_offset:
20540 fprintf_unfiltered (f, "section offset: %s",
20541 pulongest (DW_UNSND (&die->attrs[i])));
20542 break;
55f1336d 20543 case DW_FORM_ref_sig8:
ac9ec31b
DE
20544 fprintf_unfiltered (f, "signature: %s",
20545 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20546 break;
c906108c 20547 case DW_FORM_string:
4bdf3d34 20548 case DW_FORM_strp:
43988095 20549 case DW_FORM_line_strp:
3019eac3 20550 case DW_FORM_GNU_str_index:
36586728 20551 case DW_FORM_GNU_strp_alt:
8285870a 20552 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20553 DW_STRING (&die->attrs[i])
8285870a
JK
20554 ? DW_STRING (&die->attrs[i]) : "",
20555 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20556 break;
20557 case DW_FORM_flag:
20558 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20559 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20560 else
d97bc12b 20561 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20562 break;
2dc7f7b3
TT
20563 case DW_FORM_flag_present:
20564 fprintf_unfiltered (f, "flag: TRUE");
20565 break;
a8329558 20566 case DW_FORM_indirect:
0963b4bd
MS
20567 /* The reader will have reduced the indirect form to
20568 the "base form" so this form should not occur. */
3e43a32a
MS
20569 fprintf_unfiltered (f,
20570 "unexpected attribute form: DW_FORM_indirect");
a8329558 20571 break;
663c44ac
JK
20572 case DW_FORM_implicit_const:
20573 fprintf_unfiltered (f, "constant: %s",
20574 plongest (DW_SND (&die->attrs[i])));
20575 break;
c906108c 20576 default:
d97bc12b 20577 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20578 die->attrs[i].form);
d97bc12b 20579 break;
c906108c 20580 }
d97bc12b 20581 fprintf_unfiltered (f, "\n");
c906108c
SS
20582 }
20583}
20584
f9aca02d 20585static void
d97bc12b 20586dump_die_for_error (struct die_info *die)
c906108c 20587{
d97bc12b
DE
20588 dump_die_shallow (gdb_stderr, 0, die);
20589}
20590
20591static void
20592dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20593{
20594 int indent = level * 4;
20595
20596 gdb_assert (die != NULL);
20597
20598 if (level >= max_level)
20599 return;
20600
20601 dump_die_shallow (f, indent, die);
20602
20603 if (die->child != NULL)
c906108c 20604 {
d97bc12b
DE
20605 print_spaces (indent, f);
20606 fprintf_unfiltered (f, " Children:");
20607 if (level + 1 < max_level)
20608 {
20609 fprintf_unfiltered (f, "\n");
20610 dump_die_1 (f, level + 1, max_level, die->child);
20611 }
20612 else
20613 {
3e43a32a
MS
20614 fprintf_unfiltered (f,
20615 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20616 }
20617 }
20618
20619 if (die->sibling != NULL && level > 0)
20620 {
20621 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20622 }
20623}
20624
d97bc12b
DE
20625/* This is called from the pdie macro in gdbinit.in.
20626 It's not static so gcc will keep a copy callable from gdb. */
20627
20628void
20629dump_die (struct die_info *die, int max_level)
20630{
20631 dump_die_1 (gdb_stdlog, 0, max_level, die);
20632}
20633
f9aca02d 20634static void
51545339 20635store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20636{
51545339 20637 void **slot;
c906108c 20638
9c541725
PA
20639 slot = htab_find_slot_with_hash (cu->die_hash, die,
20640 to_underlying (die->sect_off),
b64f50a1 20641 INSERT);
51545339
DJ
20642
20643 *slot = die;
c906108c
SS
20644}
20645
b64f50a1
JK
20646/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20647 required kind. */
20648
20649static sect_offset
ff39bb5e 20650dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20651{
7771576e 20652 if (attr_form_is_ref (attr))
9c541725 20653 return (sect_offset) DW_UNSND (attr);
93311388
DE
20654
20655 complaint (&symfile_complaints,
20656 _("unsupported die ref attribute form: '%s'"),
20657 dwarf_form_name (attr->form));
9c541725 20658 return {};
c906108c
SS
20659}
20660
43bbcdc2
PH
20661/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20662 * the value held by the attribute is not constant. */
a02abb62 20663
43bbcdc2 20664static LONGEST
ff39bb5e 20665dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20666{
663c44ac 20667 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20668 return DW_SND (attr);
20669 else if (attr->form == DW_FORM_udata
20670 || attr->form == DW_FORM_data1
20671 || attr->form == DW_FORM_data2
20672 || attr->form == DW_FORM_data4
20673 || attr->form == DW_FORM_data8)
20674 return DW_UNSND (attr);
20675 else
20676 {
0224619f 20677 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20678 complaint (&symfile_complaints,
20679 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20680 dwarf_form_name (attr->form));
20681 return default_value;
20682 }
20683}
20684
348e048f
DE
20685/* Follow reference or signature attribute ATTR of SRC_DIE.
20686 On entry *REF_CU is the CU of SRC_DIE.
20687 On exit *REF_CU is the CU of the result. */
20688
20689static struct die_info *
ff39bb5e 20690follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20691 struct dwarf2_cu **ref_cu)
20692{
20693 struct die_info *die;
20694
7771576e 20695 if (attr_form_is_ref (attr))
348e048f 20696 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20697 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20698 die = follow_die_sig (src_die, attr, ref_cu);
20699 else
20700 {
20701 dump_die_for_error (src_die);
20702 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20703 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20704 }
20705
20706 return die;
03dd20cc
DJ
20707}
20708
5c631832 20709/* Follow reference OFFSET.
673bfd45
DE
20710 On entry *REF_CU is the CU of the source die referencing OFFSET.
20711 On exit *REF_CU is the CU of the result.
20712 Returns NULL if OFFSET is invalid. */
f504f079 20713
f9aca02d 20714static struct die_info *
9c541725 20715follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20716 struct dwarf2_cu **ref_cu)
c906108c 20717{
10b3939b 20718 struct die_info temp_die;
f2f0e013 20719 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20720
348e048f
DE
20721 gdb_assert (cu->per_cu != NULL);
20722
98bfdba5
PA
20723 target_cu = cu;
20724
3019eac3 20725 if (cu->per_cu->is_debug_types)
348e048f
DE
20726 {
20727 /* .debug_types CUs cannot reference anything outside their CU.
20728 If they need to, they have to reference a signatured type via
55f1336d 20729 DW_FORM_ref_sig8. */
9c541725 20730 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20731 return NULL;
348e048f 20732 }
36586728 20733 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20734 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20735 {
20736 struct dwarf2_per_cu_data *per_cu;
9a619af0 20737
9c541725 20738 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20739 cu->objfile);
03dd20cc
DJ
20740
20741 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20742 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20743 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20744
10b3939b
DJ
20745 target_cu = per_cu->cu;
20746 }
98bfdba5
PA
20747 else if (cu->dies == NULL)
20748 {
20749 /* We're loading full DIEs during partial symbol reading. */
20750 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20751 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20752 }
c906108c 20753
f2f0e013 20754 *ref_cu = target_cu;
9c541725 20755 temp_die.sect_off = sect_off;
9a3c8263 20756 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20757 &temp_die,
20758 to_underlying (sect_off));
5c631832 20759}
10b3939b 20760
5c631832
JK
20761/* Follow reference attribute ATTR of SRC_DIE.
20762 On entry *REF_CU is the CU of SRC_DIE.
20763 On exit *REF_CU is the CU of the result. */
20764
20765static struct die_info *
ff39bb5e 20766follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20767 struct dwarf2_cu **ref_cu)
20768{
9c541725 20769 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20770 struct dwarf2_cu *cu = *ref_cu;
20771 struct die_info *die;
20772
9c541725 20773 die = follow_die_offset (sect_off,
36586728
TT
20774 (attr->form == DW_FORM_GNU_ref_alt
20775 || cu->per_cu->is_dwz),
20776 ref_cu);
5c631832
JK
20777 if (!die)
20778 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20779 "at 0x%x [in module %s]"),
9c541725 20780 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20781 objfile_name (cu->objfile));
348e048f 20782
5c631832
JK
20783 return die;
20784}
20785
9c541725 20786/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20787 Returned value is intended for DW_OP_call*. Returned
20788 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20789
20790struct dwarf2_locexpr_baton
9c541725 20791dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20792 struct dwarf2_per_cu_data *per_cu,
20793 CORE_ADDR (*get_frame_pc) (void *baton),
20794 void *baton)
5c631832 20795{
918dd910 20796 struct dwarf2_cu *cu;
5c631832
JK
20797 struct die_info *die;
20798 struct attribute *attr;
20799 struct dwarf2_locexpr_baton retval;
20800
8cf6f0b1
TT
20801 dw2_setup (per_cu->objfile);
20802
918dd910
JK
20803 if (per_cu->cu == NULL)
20804 load_cu (per_cu);
20805 cu = per_cu->cu;
cc12ce38
DE
20806 if (cu == NULL)
20807 {
20808 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20809 Instead just throw an error, not much else we can do. */
20810 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20811 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20812 }
918dd910 20813
9c541725 20814 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20815 if (!die)
20816 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20817 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20818
20819 attr = dwarf2_attr (die, DW_AT_location, cu);
20820 if (!attr)
20821 {
e103e986
JK
20822 /* DWARF: "If there is no such attribute, then there is no effect.".
20823 DATA is ignored if SIZE is 0. */
5c631832 20824
e103e986 20825 retval.data = NULL;
5c631832
JK
20826 retval.size = 0;
20827 }
8cf6f0b1
TT
20828 else if (attr_form_is_section_offset (attr))
20829 {
20830 struct dwarf2_loclist_baton loclist_baton;
20831 CORE_ADDR pc = (*get_frame_pc) (baton);
20832 size_t size;
20833
20834 fill_in_loclist_baton (cu, &loclist_baton, attr);
20835
20836 retval.data = dwarf2_find_location_expression (&loclist_baton,
20837 &size, pc);
20838 retval.size = size;
20839 }
5c631832
JK
20840 else
20841 {
20842 if (!attr_form_is_block (attr))
20843 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20844 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20845 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20846
20847 retval.data = DW_BLOCK (attr)->data;
20848 retval.size = DW_BLOCK (attr)->size;
20849 }
20850 retval.per_cu = cu->per_cu;
918dd910 20851
918dd910
JK
20852 age_cached_comp_units ();
20853
5c631832 20854 return retval;
348e048f
DE
20855}
20856
8b9737bf
TT
20857/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20858 offset. */
20859
20860struct dwarf2_locexpr_baton
20861dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20862 struct dwarf2_per_cu_data *per_cu,
20863 CORE_ADDR (*get_frame_pc) (void *baton),
20864 void *baton)
20865{
9c541725 20866 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20867
9c541725 20868 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20869}
20870
b6807d98
TT
20871/* Write a constant of a given type as target-ordered bytes into
20872 OBSTACK. */
20873
20874static const gdb_byte *
20875write_constant_as_bytes (struct obstack *obstack,
20876 enum bfd_endian byte_order,
20877 struct type *type,
20878 ULONGEST value,
20879 LONGEST *len)
20880{
20881 gdb_byte *result;
20882
20883 *len = TYPE_LENGTH (type);
224c3ddb 20884 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20885 store_unsigned_integer (result, *len, byte_order, value);
20886
20887 return result;
20888}
20889
20890/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20891 pointer to the constant bytes and set LEN to the length of the
20892 data. If memory is needed, allocate it on OBSTACK. If the DIE
20893 does not have a DW_AT_const_value, return NULL. */
20894
20895const gdb_byte *
9c541725 20896dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20897 struct dwarf2_per_cu_data *per_cu,
20898 struct obstack *obstack,
20899 LONGEST *len)
20900{
20901 struct dwarf2_cu *cu;
20902 struct die_info *die;
20903 struct attribute *attr;
20904 const gdb_byte *result = NULL;
20905 struct type *type;
20906 LONGEST value;
20907 enum bfd_endian byte_order;
20908
20909 dw2_setup (per_cu->objfile);
20910
20911 if (per_cu->cu == NULL)
20912 load_cu (per_cu);
20913 cu = per_cu->cu;
cc12ce38
DE
20914 if (cu == NULL)
20915 {
20916 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20917 Instead just throw an error, not much else we can do. */
20918 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20919 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20920 }
b6807d98 20921
9c541725 20922 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20923 if (!die)
20924 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20925 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20926
20927
20928 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20929 if (attr == NULL)
20930 return NULL;
20931
20932 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20933 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20934
20935 switch (attr->form)
20936 {
20937 case DW_FORM_addr:
20938 case DW_FORM_GNU_addr_index:
20939 {
20940 gdb_byte *tem;
20941
20942 *len = cu->header.addr_size;
224c3ddb 20943 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20944 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20945 result = tem;
20946 }
20947 break;
20948 case DW_FORM_string:
20949 case DW_FORM_strp:
20950 case DW_FORM_GNU_str_index:
20951 case DW_FORM_GNU_strp_alt:
20952 /* DW_STRING is already allocated on the objfile obstack, point
20953 directly to it. */
20954 result = (const gdb_byte *) DW_STRING (attr);
20955 *len = strlen (DW_STRING (attr));
20956 break;
20957 case DW_FORM_block1:
20958 case DW_FORM_block2:
20959 case DW_FORM_block4:
20960 case DW_FORM_block:
20961 case DW_FORM_exprloc:
0224619f 20962 case DW_FORM_data16:
b6807d98
TT
20963 result = DW_BLOCK (attr)->data;
20964 *len = DW_BLOCK (attr)->size;
20965 break;
20966
20967 /* The DW_AT_const_value attributes are supposed to carry the
20968 symbol's value "represented as it would be on the target
20969 architecture." By the time we get here, it's already been
20970 converted to host endianness, so we just need to sign- or
20971 zero-extend it as appropriate. */
20972 case DW_FORM_data1:
20973 type = die_type (die, cu);
20974 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20975 if (result == NULL)
20976 result = write_constant_as_bytes (obstack, byte_order,
20977 type, value, len);
20978 break;
20979 case DW_FORM_data2:
20980 type = die_type (die, cu);
20981 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20982 if (result == NULL)
20983 result = write_constant_as_bytes (obstack, byte_order,
20984 type, value, len);
20985 break;
20986 case DW_FORM_data4:
20987 type = die_type (die, cu);
20988 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20989 if (result == NULL)
20990 result = write_constant_as_bytes (obstack, byte_order,
20991 type, value, len);
20992 break;
20993 case DW_FORM_data8:
20994 type = die_type (die, cu);
20995 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20996 if (result == NULL)
20997 result = write_constant_as_bytes (obstack, byte_order,
20998 type, value, len);
20999 break;
21000
21001 case DW_FORM_sdata:
663c44ac 21002 case DW_FORM_implicit_const:
b6807d98
TT
21003 type = die_type (die, cu);
21004 result = write_constant_as_bytes (obstack, byte_order,
21005 type, DW_SND (attr), len);
21006 break;
21007
21008 case DW_FORM_udata:
21009 type = die_type (die, cu);
21010 result = write_constant_as_bytes (obstack, byte_order,
21011 type, DW_UNSND (attr), len);
21012 break;
21013
21014 default:
21015 complaint (&symfile_complaints,
21016 _("unsupported const value attribute form: '%s'"),
21017 dwarf_form_name (attr->form));
21018 break;
21019 }
21020
21021 return result;
21022}
21023
7942e96e
AA
21024/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21025 valid type for this die is found. */
21026
21027struct type *
9c541725 21028dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21029 struct dwarf2_per_cu_data *per_cu)
21030{
21031 struct dwarf2_cu *cu;
21032 struct die_info *die;
21033
21034 dw2_setup (per_cu->objfile);
21035
21036 if (per_cu->cu == NULL)
21037 load_cu (per_cu);
21038 cu = per_cu->cu;
21039 if (!cu)
21040 return NULL;
21041
9c541725 21042 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21043 if (!die)
21044 return NULL;
21045
21046 return die_type (die, cu);
21047}
21048
8a9b8146
TT
21049/* Return the type of the DIE at DIE_OFFSET in the CU named by
21050 PER_CU. */
21051
21052struct type *
b64f50a1 21053dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21054 struct dwarf2_per_cu_data *per_cu)
21055{
8a9b8146 21056 dw2_setup (per_cu->objfile);
b64f50a1 21057
9c541725 21058 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21059 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21060}
21061
ac9ec31b 21062/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21063 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21064 On exit *REF_CU is the CU of the result.
21065 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21066
21067static struct die_info *
ac9ec31b
DE
21068follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21069 struct dwarf2_cu **ref_cu)
348e048f 21070{
348e048f 21071 struct die_info temp_die;
348e048f
DE
21072 struct dwarf2_cu *sig_cu;
21073 struct die_info *die;
21074
ac9ec31b
DE
21075 /* While it might be nice to assert sig_type->type == NULL here,
21076 we can get here for DW_AT_imported_declaration where we need
21077 the DIE not the type. */
348e048f
DE
21078
21079 /* If necessary, add it to the queue and load its DIEs. */
21080
95554aad 21081 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21082 read_signatured_type (sig_type);
348e048f 21083
348e048f 21084 sig_cu = sig_type->per_cu.cu;
69d751e3 21085 gdb_assert (sig_cu != NULL);
9c541725
PA
21086 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21087 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21088 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21089 to_underlying (temp_die.sect_off));
348e048f
DE
21090 if (die)
21091 {
796a7ff8
DE
21092 /* For .gdb_index version 7 keep track of included TUs.
21093 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21094 if (dwarf2_per_objfile->index_table != NULL
21095 && dwarf2_per_objfile->index_table->version <= 7)
21096 {
21097 VEC_safe_push (dwarf2_per_cu_ptr,
21098 (*ref_cu)->per_cu->imported_symtabs,
21099 sig_cu->per_cu);
21100 }
21101
348e048f
DE
21102 *ref_cu = sig_cu;
21103 return die;
21104 }
21105
ac9ec31b
DE
21106 return NULL;
21107}
21108
21109/* Follow signatured type referenced by ATTR in SRC_DIE.
21110 On entry *REF_CU is the CU of SRC_DIE.
21111 On exit *REF_CU is the CU of the result.
21112 The result is the DIE of the type.
21113 If the referenced type cannot be found an error is thrown. */
21114
21115static struct die_info *
ff39bb5e 21116follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21117 struct dwarf2_cu **ref_cu)
21118{
21119 ULONGEST signature = DW_SIGNATURE (attr);
21120 struct signatured_type *sig_type;
21121 struct die_info *die;
21122
21123 gdb_assert (attr->form == DW_FORM_ref_sig8);
21124
a2ce51a0 21125 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21126 /* sig_type will be NULL if the signatured type is missing from
21127 the debug info. */
21128 if (sig_type == NULL)
21129 {
21130 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21131 " from DIE at 0x%x [in module %s]"),
9c541725 21132 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21133 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21134 }
21135
21136 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21137 if (die == NULL)
21138 {
21139 dump_die_for_error (src_die);
21140 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21141 " from DIE at 0x%x [in module %s]"),
9c541725 21142 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21143 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21144 }
21145
21146 return die;
21147}
21148
21149/* Get the type specified by SIGNATURE referenced in DIE/CU,
21150 reading in and processing the type unit if necessary. */
21151
21152static struct type *
21153get_signatured_type (struct die_info *die, ULONGEST signature,
21154 struct dwarf2_cu *cu)
21155{
21156 struct signatured_type *sig_type;
21157 struct dwarf2_cu *type_cu;
21158 struct die_info *type_die;
21159 struct type *type;
21160
a2ce51a0 21161 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21162 /* sig_type will be NULL if the signatured type is missing from
21163 the debug info. */
21164 if (sig_type == NULL)
21165 {
21166 complaint (&symfile_complaints,
21167 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21168 " from DIE at 0x%x [in module %s]"),
9c541725 21169 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21170 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21171 return build_error_marker_type (cu, die);
21172 }
21173
21174 /* If we already know the type we're done. */
21175 if (sig_type->type != NULL)
21176 return sig_type->type;
21177
21178 type_cu = cu;
21179 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21180 if (type_die != NULL)
21181 {
21182 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21183 is created. This is important, for example, because for c++ classes
21184 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21185 type = read_type_die (type_die, type_cu);
21186 if (type == NULL)
21187 {
21188 complaint (&symfile_complaints,
21189 _("Dwarf Error: Cannot build signatured type %s"
21190 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21191 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21192 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21193 type = build_error_marker_type (cu, die);
21194 }
21195 }
21196 else
21197 {
21198 complaint (&symfile_complaints,
21199 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21200 " from DIE at 0x%x [in module %s]"),
9c541725 21201 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21202 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21203 type = build_error_marker_type (cu, die);
21204 }
21205 sig_type->type = type;
21206
21207 return type;
21208}
21209
21210/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21211 reading in and processing the type unit if necessary. */
21212
21213static struct type *
ff39bb5e 21214get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21215 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21216{
21217 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21218 if (attr_form_is_ref (attr))
ac9ec31b
DE
21219 {
21220 struct dwarf2_cu *type_cu = cu;
21221 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21222
21223 return read_type_die (type_die, type_cu);
21224 }
21225 else if (attr->form == DW_FORM_ref_sig8)
21226 {
21227 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21228 }
21229 else
21230 {
21231 complaint (&symfile_complaints,
21232 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21233 " at 0x%x [in module %s]"),
9c541725 21234 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21235 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21236 return build_error_marker_type (cu, die);
21237 }
348e048f
DE
21238}
21239
e5fe5e75 21240/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21241
21242static void
e5fe5e75 21243load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21244{
52dc124a 21245 struct signatured_type *sig_type;
348e048f 21246
f4dc4d17
DE
21247 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21248 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21249
6721b2ec
DE
21250 /* We have the per_cu, but we need the signatured_type.
21251 Fortunately this is an easy translation. */
21252 gdb_assert (per_cu->is_debug_types);
21253 sig_type = (struct signatured_type *) per_cu;
348e048f 21254
6721b2ec 21255 gdb_assert (per_cu->cu == NULL);
348e048f 21256
52dc124a 21257 read_signatured_type (sig_type);
348e048f 21258
6721b2ec 21259 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21260}
21261
dee91e82
DE
21262/* die_reader_func for read_signatured_type.
21263 This is identical to load_full_comp_unit_reader,
21264 but is kept separate for now. */
348e048f
DE
21265
21266static void
dee91e82 21267read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21268 const gdb_byte *info_ptr,
dee91e82
DE
21269 struct die_info *comp_unit_die,
21270 int has_children,
21271 void *data)
348e048f 21272{
dee91e82 21273 struct dwarf2_cu *cu = reader->cu;
348e048f 21274
dee91e82
DE
21275 gdb_assert (cu->die_hash == NULL);
21276 cu->die_hash =
21277 htab_create_alloc_ex (cu->header.length / 12,
21278 die_hash,
21279 die_eq,
21280 NULL,
21281 &cu->comp_unit_obstack,
21282 hashtab_obstack_allocate,
21283 dummy_obstack_deallocate);
348e048f 21284
dee91e82
DE
21285 if (has_children)
21286 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21287 &info_ptr, comp_unit_die);
21288 cu->dies = comp_unit_die;
21289 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21290
21291 /* We try not to read any attributes in this function, because not
9cdd5dbd 21292 all CUs needed for references have been loaded yet, and symbol
348e048f 21293 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21294 or we won't be able to build types correctly.
21295 Similarly, if we do not read the producer, we can not apply
21296 producer-specific interpretation. */
95554aad 21297 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21298}
348e048f 21299
3019eac3
DE
21300/* Read in a signatured type and build its CU and DIEs.
21301 If the type is a stub for the real type in a DWO file,
21302 read in the real type from the DWO file as well. */
dee91e82
DE
21303
21304static void
21305read_signatured_type (struct signatured_type *sig_type)
21306{
21307 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21308
3019eac3 21309 gdb_assert (per_cu->is_debug_types);
dee91e82 21310 gdb_assert (per_cu->cu == NULL);
348e048f 21311
f4dc4d17
DE
21312 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21313 read_signatured_type_reader, NULL);
7ee85ab1 21314 sig_type->per_cu.tu_read = 1;
c906108c
SS
21315}
21316
c906108c
SS
21317/* Decode simple location descriptions.
21318 Given a pointer to a dwarf block that defines a location, compute
21319 the location and return the value.
21320
4cecd739
DJ
21321 NOTE drow/2003-11-18: This function is called in two situations
21322 now: for the address of static or global variables (partial symbols
21323 only) and for offsets into structures which are expected to be
21324 (more or less) constant. The partial symbol case should go away,
21325 and only the constant case should remain. That will let this
21326 function complain more accurately. A few special modes are allowed
21327 without complaint for global variables (for instance, global
21328 register values and thread-local values).
c906108c
SS
21329
21330 A location description containing no operations indicates that the
4cecd739 21331 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21332 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21333 callers will only want a very basic result and this can become a
21ae7a4d
JK
21334 complaint.
21335
21336 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21337
21338static CORE_ADDR
e7c27a73 21339decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21340{
e7c27a73 21341 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21342 size_t i;
21343 size_t size = blk->size;
d521ce57 21344 const gdb_byte *data = blk->data;
21ae7a4d
JK
21345 CORE_ADDR stack[64];
21346 int stacki;
21347 unsigned int bytes_read, unsnd;
21348 gdb_byte op;
c906108c 21349
21ae7a4d
JK
21350 i = 0;
21351 stacki = 0;
21352 stack[stacki] = 0;
21353 stack[++stacki] = 0;
21354
21355 while (i < size)
21356 {
21357 op = data[i++];
21358 switch (op)
21359 {
21360 case DW_OP_lit0:
21361 case DW_OP_lit1:
21362 case DW_OP_lit2:
21363 case DW_OP_lit3:
21364 case DW_OP_lit4:
21365 case DW_OP_lit5:
21366 case DW_OP_lit6:
21367 case DW_OP_lit7:
21368 case DW_OP_lit8:
21369 case DW_OP_lit9:
21370 case DW_OP_lit10:
21371 case DW_OP_lit11:
21372 case DW_OP_lit12:
21373 case DW_OP_lit13:
21374 case DW_OP_lit14:
21375 case DW_OP_lit15:
21376 case DW_OP_lit16:
21377 case DW_OP_lit17:
21378 case DW_OP_lit18:
21379 case DW_OP_lit19:
21380 case DW_OP_lit20:
21381 case DW_OP_lit21:
21382 case DW_OP_lit22:
21383 case DW_OP_lit23:
21384 case DW_OP_lit24:
21385 case DW_OP_lit25:
21386 case DW_OP_lit26:
21387 case DW_OP_lit27:
21388 case DW_OP_lit28:
21389 case DW_OP_lit29:
21390 case DW_OP_lit30:
21391 case DW_OP_lit31:
21392 stack[++stacki] = op - DW_OP_lit0;
21393 break;
f1bea926 21394
21ae7a4d
JK
21395 case DW_OP_reg0:
21396 case DW_OP_reg1:
21397 case DW_OP_reg2:
21398 case DW_OP_reg3:
21399 case DW_OP_reg4:
21400 case DW_OP_reg5:
21401 case DW_OP_reg6:
21402 case DW_OP_reg7:
21403 case DW_OP_reg8:
21404 case DW_OP_reg9:
21405 case DW_OP_reg10:
21406 case DW_OP_reg11:
21407 case DW_OP_reg12:
21408 case DW_OP_reg13:
21409 case DW_OP_reg14:
21410 case DW_OP_reg15:
21411 case DW_OP_reg16:
21412 case DW_OP_reg17:
21413 case DW_OP_reg18:
21414 case DW_OP_reg19:
21415 case DW_OP_reg20:
21416 case DW_OP_reg21:
21417 case DW_OP_reg22:
21418 case DW_OP_reg23:
21419 case DW_OP_reg24:
21420 case DW_OP_reg25:
21421 case DW_OP_reg26:
21422 case DW_OP_reg27:
21423 case DW_OP_reg28:
21424 case DW_OP_reg29:
21425 case DW_OP_reg30:
21426 case DW_OP_reg31:
21427 stack[++stacki] = op - DW_OP_reg0;
21428 if (i < size)
21429 dwarf2_complex_location_expr_complaint ();
21430 break;
c906108c 21431
21ae7a4d
JK
21432 case DW_OP_regx:
21433 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21434 i += bytes_read;
21435 stack[++stacki] = unsnd;
21436 if (i < size)
21437 dwarf2_complex_location_expr_complaint ();
21438 break;
c906108c 21439
21ae7a4d
JK
21440 case DW_OP_addr:
21441 stack[++stacki] = read_address (objfile->obfd, &data[i],
21442 cu, &bytes_read);
21443 i += bytes_read;
21444 break;
d53d4ac5 21445
21ae7a4d
JK
21446 case DW_OP_const1u:
21447 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21448 i += 1;
21449 break;
21450
21451 case DW_OP_const1s:
21452 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21453 i += 1;
21454 break;
21455
21456 case DW_OP_const2u:
21457 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21458 i += 2;
21459 break;
21460
21461 case DW_OP_const2s:
21462 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21463 i += 2;
21464 break;
d53d4ac5 21465
21ae7a4d
JK
21466 case DW_OP_const4u:
21467 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21468 i += 4;
21469 break;
21470
21471 case DW_OP_const4s:
21472 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21473 i += 4;
21474 break;
21475
585861ea
JK
21476 case DW_OP_const8u:
21477 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21478 i += 8;
21479 break;
21480
21ae7a4d
JK
21481 case DW_OP_constu:
21482 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21483 &bytes_read);
21484 i += bytes_read;
21485 break;
21486
21487 case DW_OP_consts:
21488 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21489 i += bytes_read;
21490 break;
21491
21492 case DW_OP_dup:
21493 stack[stacki + 1] = stack[stacki];
21494 stacki++;
21495 break;
21496
21497 case DW_OP_plus:
21498 stack[stacki - 1] += stack[stacki];
21499 stacki--;
21500 break;
21501
21502 case DW_OP_plus_uconst:
21503 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21504 &bytes_read);
21505 i += bytes_read;
21506 break;
21507
21508 case DW_OP_minus:
21509 stack[stacki - 1] -= stack[stacki];
21510 stacki--;
21511 break;
21512
21513 case DW_OP_deref:
21514 /* If we're not the last op, then we definitely can't encode
21515 this using GDB's address_class enum. This is valid for partial
21516 global symbols, although the variable's address will be bogus
21517 in the psymtab. */
21518 if (i < size)
21519 dwarf2_complex_location_expr_complaint ();
21520 break;
21521
21522 case DW_OP_GNU_push_tls_address:
4aa4e28b 21523 case DW_OP_form_tls_address:
21ae7a4d
JK
21524 /* The top of the stack has the offset from the beginning
21525 of the thread control block at which the variable is located. */
21526 /* Nothing should follow this operator, so the top of stack would
21527 be returned. */
21528 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21529 address will be bogus in the psymtab. Make it always at least
21530 non-zero to not look as a variable garbage collected by linker
21531 which have DW_OP_addr 0. */
21ae7a4d
JK
21532 if (i < size)
21533 dwarf2_complex_location_expr_complaint ();
585861ea 21534 stack[stacki]++;
21ae7a4d
JK
21535 break;
21536
21537 case DW_OP_GNU_uninit:
21538 break;
21539
3019eac3 21540 case DW_OP_GNU_addr_index:
49f6c839 21541 case DW_OP_GNU_const_index:
3019eac3
DE
21542 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21543 &bytes_read);
21544 i += bytes_read;
21545 break;
21546
21ae7a4d
JK
21547 default:
21548 {
f39c6ffd 21549 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21550
21551 if (name)
21552 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21553 name);
21554 else
21555 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21556 op);
21557 }
21558
21559 return (stack[stacki]);
d53d4ac5 21560 }
3c6e0cb3 21561
21ae7a4d
JK
21562 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21563 outside of the allocated space. Also enforce minimum>0. */
21564 if (stacki >= ARRAY_SIZE (stack) - 1)
21565 {
21566 complaint (&symfile_complaints,
21567 _("location description stack overflow"));
21568 return 0;
21569 }
21570
21571 if (stacki <= 0)
21572 {
21573 complaint (&symfile_complaints,
21574 _("location description stack underflow"));
21575 return 0;
21576 }
21577 }
21578 return (stack[stacki]);
c906108c
SS
21579}
21580
21581/* memory allocation interface */
21582
c906108c 21583static struct dwarf_block *
7b5a2f43 21584dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21585{
8d749320 21586 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21587}
21588
c906108c 21589static struct die_info *
b60c80d6 21590dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21591{
21592 struct die_info *die;
b60c80d6
DJ
21593 size_t size = sizeof (struct die_info);
21594
21595 if (num_attrs > 1)
21596 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21597
b60c80d6 21598 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21599 memset (die, 0, sizeof (struct die_info));
21600 return (die);
21601}
2e276125
JB
21602
21603\f
21604/* Macro support. */
21605
233d95b5
JK
21606/* Return file name relative to the compilation directory of file number I in
21607 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21608 responsible for freeing it. */
233d95b5 21609
2e276125 21610static char *
233d95b5 21611file_file_name (int file, struct line_header *lh)
2e276125 21612{
6a83a1e6
EZ
21613 /* Is the file number a valid index into the line header's file name
21614 table? Remember that file numbers start with one, not zero. */
fff8551c 21615 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21616 {
8c43009f 21617 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21618
8c43009f
PA
21619 if (!IS_ABSOLUTE_PATH (fe.name))
21620 {
21621 const char *dir = fe.include_dir (lh);
21622 if (dir != NULL)
21623 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21624 }
21625 return xstrdup (fe.name);
6a83a1e6 21626 }
2e276125
JB
21627 else
21628 {
6a83a1e6
EZ
21629 /* The compiler produced a bogus file number. We can at least
21630 record the macro definitions made in the file, even if we
21631 won't be able to find the file by name. */
21632 char fake_name[80];
9a619af0 21633
8c042590
PM
21634 xsnprintf (fake_name, sizeof (fake_name),
21635 "<bad macro file number %d>", file);
2e276125 21636
6e70227d 21637 complaint (&symfile_complaints,
6a83a1e6
EZ
21638 _("bad file number in macro information (%d)"),
21639 file);
2e276125 21640
6a83a1e6 21641 return xstrdup (fake_name);
2e276125
JB
21642 }
21643}
21644
233d95b5
JK
21645/* Return the full name of file number I in *LH's file name table.
21646 Use COMP_DIR as the name of the current directory of the
21647 compilation. The result is allocated using xmalloc; the caller is
21648 responsible for freeing it. */
21649static char *
21650file_full_name (int file, struct line_header *lh, const char *comp_dir)
21651{
21652 /* Is the file number a valid index into the line header's file name
21653 table? Remember that file numbers start with one, not zero. */
fff8551c 21654 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21655 {
21656 char *relative = file_file_name (file, lh);
21657
21658 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21659 return relative;
b36cec19
PA
21660 return reconcat (relative, comp_dir, SLASH_STRING,
21661 relative, (char *) NULL);
233d95b5
JK
21662 }
21663 else
21664 return file_file_name (file, lh);
21665}
21666
2e276125
JB
21667
21668static struct macro_source_file *
21669macro_start_file (int file, int line,
21670 struct macro_source_file *current_file,
43f3e411 21671 struct line_header *lh)
2e276125 21672{
233d95b5
JK
21673 /* File name relative to the compilation directory of this source file. */
21674 char *file_name = file_file_name (file, lh);
2e276125 21675
2e276125 21676 if (! current_file)
abc9d0dc 21677 {
fc474241
DE
21678 /* Note: We don't create a macro table for this compilation unit
21679 at all until we actually get a filename. */
43f3e411 21680 struct macro_table *macro_table = get_macro_table ();
fc474241 21681
abc9d0dc
TT
21682 /* If we have no current file, then this must be the start_file
21683 directive for the compilation unit's main source file. */
fc474241
DE
21684 current_file = macro_set_main (macro_table, file_name);
21685 macro_define_special (macro_table);
abc9d0dc 21686 }
2e276125 21687 else
233d95b5 21688 current_file = macro_include (current_file, line, file_name);
2e276125 21689
233d95b5 21690 xfree (file_name);
6e70227d 21691
2e276125
JB
21692 return current_file;
21693}
21694
2e276125
JB
21695static const char *
21696consume_improper_spaces (const char *p, const char *body)
21697{
21698 if (*p == ' ')
21699 {
4d3c2250 21700 complaint (&symfile_complaints,
3e43a32a
MS
21701 _("macro definition contains spaces "
21702 "in formal argument list:\n`%s'"),
4d3c2250 21703 body);
2e276125
JB
21704
21705 while (*p == ' ')
21706 p++;
21707 }
21708
21709 return p;
21710}
21711
21712
21713static void
21714parse_macro_definition (struct macro_source_file *file, int line,
21715 const char *body)
21716{
21717 const char *p;
21718
21719 /* The body string takes one of two forms. For object-like macro
21720 definitions, it should be:
21721
21722 <macro name> " " <definition>
21723
21724 For function-like macro definitions, it should be:
21725
21726 <macro name> "() " <definition>
21727 or
21728 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21729
21730 Spaces may appear only where explicitly indicated, and in the
21731 <definition>.
21732
21733 The Dwarf 2 spec says that an object-like macro's name is always
21734 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21735 the space when the macro's definition is the empty string.
2e276125
JB
21736
21737 The Dwarf 2 spec says that there should be no spaces between the
21738 formal arguments in a function-like macro's formal argument list,
21739 but versions of GCC around March 2002 include spaces after the
21740 commas. */
21741
21742
21743 /* Find the extent of the macro name. The macro name is terminated
21744 by either a space or null character (for an object-like macro) or
21745 an opening paren (for a function-like macro). */
21746 for (p = body; *p; p++)
21747 if (*p == ' ' || *p == '(')
21748 break;
21749
21750 if (*p == ' ' || *p == '\0')
21751 {
21752 /* It's an object-like macro. */
21753 int name_len = p - body;
3f8a7804 21754 char *name = savestring (body, name_len);
2e276125
JB
21755 const char *replacement;
21756
21757 if (*p == ' ')
21758 replacement = body + name_len + 1;
21759 else
21760 {
4d3c2250 21761 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21762 replacement = body + name_len;
21763 }
6e70227d 21764
2e276125
JB
21765 macro_define_object (file, line, name, replacement);
21766
21767 xfree (name);
21768 }
21769 else if (*p == '(')
21770 {
21771 /* It's a function-like macro. */
3f8a7804 21772 char *name = savestring (body, p - body);
2e276125
JB
21773 int argc = 0;
21774 int argv_size = 1;
8d749320 21775 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21776
21777 p++;
21778
21779 p = consume_improper_spaces (p, body);
21780
21781 /* Parse the formal argument list. */
21782 while (*p && *p != ')')
21783 {
21784 /* Find the extent of the current argument name. */
21785 const char *arg_start = p;
21786
21787 while (*p && *p != ',' && *p != ')' && *p != ' ')
21788 p++;
21789
21790 if (! *p || p == arg_start)
4d3c2250 21791 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21792 else
21793 {
21794 /* Make sure argv has room for the new argument. */
21795 if (argc >= argv_size)
21796 {
21797 argv_size *= 2;
224c3ddb 21798 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21799 }
21800
3f8a7804 21801 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
21802 }
21803
21804 p = consume_improper_spaces (p, body);
21805
21806 /* Consume the comma, if present. */
21807 if (*p == ',')
21808 {
21809 p++;
21810
21811 p = consume_improper_spaces (p, body);
21812 }
21813 }
21814
21815 if (*p == ')')
21816 {
21817 p++;
21818
21819 if (*p == ' ')
21820 /* Perfectly formed definition, no complaints. */
21821 macro_define_function (file, line, name,
6e70227d 21822 argc, (const char **) argv,
2e276125
JB
21823 p + 1);
21824 else if (*p == '\0')
21825 {
21826 /* Complain, but do define it. */
4d3c2250 21827 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21828 macro_define_function (file, line, name,
6e70227d 21829 argc, (const char **) argv,
2e276125
JB
21830 p);
21831 }
21832 else
21833 /* Just complain. */
4d3c2250 21834 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21835 }
21836 else
21837 /* Just complain. */
4d3c2250 21838 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21839
21840 xfree (name);
21841 {
21842 int i;
21843
21844 for (i = 0; i < argc; i++)
21845 xfree (argv[i]);
21846 }
21847 xfree (argv);
21848 }
21849 else
4d3c2250 21850 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21851}
21852
cf2c3c16
TT
21853/* Skip some bytes from BYTES according to the form given in FORM.
21854 Returns the new pointer. */
2e276125 21855
d521ce57
TT
21856static const gdb_byte *
21857skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21858 enum dwarf_form form,
21859 unsigned int offset_size,
21860 struct dwarf2_section_info *section)
2e276125 21861{
cf2c3c16 21862 unsigned int bytes_read;
2e276125 21863
cf2c3c16 21864 switch (form)
2e276125 21865 {
cf2c3c16
TT
21866 case DW_FORM_data1:
21867 case DW_FORM_flag:
21868 ++bytes;
21869 break;
21870
21871 case DW_FORM_data2:
21872 bytes += 2;
21873 break;
21874
21875 case DW_FORM_data4:
21876 bytes += 4;
21877 break;
21878
21879 case DW_FORM_data8:
21880 bytes += 8;
21881 break;
21882
0224619f
JK
21883 case DW_FORM_data16:
21884 bytes += 16;
21885 break;
21886
cf2c3c16
TT
21887 case DW_FORM_string:
21888 read_direct_string (abfd, bytes, &bytes_read);
21889 bytes += bytes_read;
21890 break;
21891
21892 case DW_FORM_sec_offset:
21893 case DW_FORM_strp:
36586728 21894 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21895 bytes += offset_size;
21896 break;
21897
21898 case DW_FORM_block:
21899 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21900 bytes += bytes_read;
21901 break;
21902
21903 case DW_FORM_block1:
21904 bytes += 1 + read_1_byte (abfd, bytes);
21905 break;
21906 case DW_FORM_block2:
21907 bytes += 2 + read_2_bytes (abfd, bytes);
21908 break;
21909 case DW_FORM_block4:
21910 bytes += 4 + read_4_bytes (abfd, bytes);
21911 break;
21912
21913 case DW_FORM_sdata:
21914 case DW_FORM_udata:
3019eac3
DE
21915 case DW_FORM_GNU_addr_index:
21916 case DW_FORM_GNU_str_index:
d521ce57 21917 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21918 if (bytes == NULL)
21919 {
21920 dwarf2_section_buffer_overflow_complaint (section);
21921 return NULL;
21922 }
cf2c3c16
TT
21923 break;
21924
663c44ac
JK
21925 case DW_FORM_implicit_const:
21926 break;
21927
cf2c3c16
TT
21928 default:
21929 {
21930 complain:
21931 complaint (&symfile_complaints,
21932 _("invalid form 0x%x in `%s'"),
a32a8923 21933 form, get_section_name (section));
cf2c3c16
TT
21934 return NULL;
21935 }
2e276125
JB
21936 }
21937
cf2c3c16
TT
21938 return bytes;
21939}
757a13d0 21940
cf2c3c16
TT
21941/* A helper for dwarf_decode_macros that handles skipping an unknown
21942 opcode. Returns an updated pointer to the macro data buffer; or,
21943 on error, issues a complaint and returns NULL. */
757a13d0 21944
d521ce57 21945static const gdb_byte *
cf2c3c16 21946skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21947 const gdb_byte **opcode_definitions,
21948 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21949 bfd *abfd,
21950 unsigned int offset_size,
21951 struct dwarf2_section_info *section)
21952{
21953 unsigned int bytes_read, i;
21954 unsigned long arg;
d521ce57 21955 const gdb_byte *defn;
2e276125 21956
cf2c3c16 21957 if (opcode_definitions[opcode] == NULL)
2e276125 21958 {
cf2c3c16
TT
21959 complaint (&symfile_complaints,
21960 _("unrecognized DW_MACFINO opcode 0x%x"),
21961 opcode);
21962 return NULL;
21963 }
2e276125 21964
cf2c3c16
TT
21965 defn = opcode_definitions[opcode];
21966 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21967 defn += bytes_read;
2e276125 21968
cf2c3c16
TT
21969 for (i = 0; i < arg; ++i)
21970 {
aead7601
SM
21971 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21972 (enum dwarf_form) defn[i], offset_size,
f664829e 21973 section);
cf2c3c16
TT
21974 if (mac_ptr == NULL)
21975 {
21976 /* skip_form_bytes already issued the complaint. */
21977 return NULL;
21978 }
21979 }
757a13d0 21980
cf2c3c16
TT
21981 return mac_ptr;
21982}
757a13d0 21983
cf2c3c16
TT
21984/* A helper function which parses the header of a macro section.
21985 If the macro section is the extended (for now called "GNU") type,
21986 then this updates *OFFSET_SIZE. Returns a pointer to just after
21987 the header, or issues a complaint and returns NULL on error. */
757a13d0 21988
d521ce57
TT
21989static const gdb_byte *
21990dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21991 bfd *abfd,
d521ce57 21992 const gdb_byte *mac_ptr,
cf2c3c16
TT
21993 unsigned int *offset_size,
21994 int section_is_gnu)
21995{
21996 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21997
cf2c3c16
TT
21998 if (section_is_gnu)
21999 {
22000 unsigned int version, flags;
757a13d0 22001
cf2c3c16 22002 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22003 if (version != 4 && version != 5)
cf2c3c16
TT
22004 {
22005 complaint (&symfile_complaints,
22006 _("unrecognized version `%d' in .debug_macro section"),
22007 version);
22008 return NULL;
22009 }
22010 mac_ptr += 2;
757a13d0 22011
cf2c3c16
TT
22012 flags = read_1_byte (abfd, mac_ptr);
22013 ++mac_ptr;
22014 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22015
cf2c3c16
TT
22016 if ((flags & 2) != 0)
22017 /* We don't need the line table offset. */
22018 mac_ptr += *offset_size;
757a13d0 22019
cf2c3c16
TT
22020 /* Vendor opcode descriptions. */
22021 if ((flags & 4) != 0)
22022 {
22023 unsigned int i, count;
757a13d0 22024
cf2c3c16
TT
22025 count = read_1_byte (abfd, mac_ptr);
22026 ++mac_ptr;
22027 for (i = 0; i < count; ++i)
22028 {
22029 unsigned int opcode, bytes_read;
22030 unsigned long arg;
22031
22032 opcode = read_1_byte (abfd, mac_ptr);
22033 ++mac_ptr;
22034 opcode_definitions[opcode] = mac_ptr;
22035 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22036 mac_ptr += bytes_read;
22037 mac_ptr += arg;
22038 }
757a13d0 22039 }
cf2c3c16 22040 }
757a13d0 22041
cf2c3c16
TT
22042 return mac_ptr;
22043}
757a13d0 22044
cf2c3c16 22045/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22046 including DW_MACRO_import. */
cf2c3c16
TT
22047
22048static void
d521ce57
TT
22049dwarf_decode_macro_bytes (bfd *abfd,
22050 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22051 struct macro_source_file *current_file,
43f3e411 22052 struct line_header *lh,
cf2c3c16 22053 struct dwarf2_section_info *section,
36586728 22054 int section_is_gnu, int section_is_dwz,
cf2c3c16 22055 unsigned int offset_size,
8fc3fc34 22056 htab_t include_hash)
cf2c3c16 22057{
4d663531 22058 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22059 enum dwarf_macro_record_type macinfo_type;
22060 int at_commandline;
d521ce57 22061 const gdb_byte *opcode_definitions[256];
757a13d0 22062
cf2c3c16
TT
22063 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22064 &offset_size, section_is_gnu);
22065 if (mac_ptr == NULL)
22066 {
22067 /* We already issued a complaint. */
22068 return;
22069 }
757a13d0
JK
22070
22071 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22072 GDB is still reading the definitions from command line. First
22073 DW_MACINFO_start_file will need to be ignored as it was already executed
22074 to create CURRENT_FILE for the main source holding also the command line
22075 definitions. On first met DW_MACINFO_start_file this flag is reset to
22076 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22077
22078 at_commandline = 1;
22079
22080 do
22081 {
22082 /* Do we at least have room for a macinfo type byte? */
22083 if (mac_ptr >= mac_end)
22084 {
f664829e 22085 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22086 break;
22087 }
22088
aead7601 22089 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22090 mac_ptr++;
22091
cf2c3c16
TT
22092 /* Note that we rely on the fact that the corresponding GNU and
22093 DWARF constants are the same. */
757a13d0
JK
22094 switch (macinfo_type)
22095 {
22096 /* A zero macinfo type indicates the end of the macro
22097 information. */
22098 case 0:
22099 break;
2e276125 22100
0af92d60
JK
22101 case DW_MACRO_define:
22102 case DW_MACRO_undef:
22103 case DW_MACRO_define_strp:
22104 case DW_MACRO_undef_strp:
22105 case DW_MACRO_define_sup:
22106 case DW_MACRO_undef_sup:
2e276125 22107 {
891d2f0b 22108 unsigned int bytes_read;
2e276125 22109 int line;
d521ce57 22110 const char *body;
cf2c3c16 22111 int is_define;
2e276125 22112
cf2c3c16
TT
22113 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22114 mac_ptr += bytes_read;
22115
0af92d60
JK
22116 if (macinfo_type == DW_MACRO_define
22117 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22118 {
22119 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22120 mac_ptr += bytes_read;
22121 }
22122 else
22123 {
22124 LONGEST str_offset;
22125
22126 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22127 mac_ptr += offset_size;
2e276125 22128
0af92d60
JK
22129 if (macinfo_type == DW_MACRO_define_sup
22130 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22131 || section_is_dwz)
36586728
TT
22132 {
22133 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22134
22135 body = read_indirect_string_from_dwz (dwz, str_offset);
22136 }
22137 else
22138 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22139 }
22140
0af92d60
JK
22141 is_define = (macinfo_type == DW_MACRO_define
22142 || macinfo_type == DW_MACRO_define_strp
22143 || macinfo_type == DW_MACRO_define_sup);
2e276125 22144 if (! current_file)
757a13d0
JK
22145 {
22146 /* DWARF violation as no main source is present. */
22147 complaint (&symfile_complaints,
22148 _("debug info with no main source gives macro %s "
22149 "on line %d: %s"),
cf2c3c16
TT
22150 is_define ? _("definition") : _("undefinition"),
22151 line, body);
757a13d0
JK
22152 break;
22153 }
3e43a32a
MS
22154 if ((line == 0 && !at_commandline)
22155 || (line != 0 && at_commandline))
4d3c2250 22156 complaint (&symfile_complaints,
757a13d0
JK
22157 _("debug info gives %s macro %s with %s line %d: %s"),
22158 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22159 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22160 line == 0 ? _("zero") : _("non-zero"), line, body);
22161
cf2c3c16 22162 if (is_define)
757a13d0 22163 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22164 else
22165 {
0af92d60
JK
22166 gdb_assert (macinfo_type == DW_MACRO_undef
22167 || macinfo_type == DW_MACRO_undef_strp
22168 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22169 macro_undef (current_file, line, body);
22170 }
2e276125
JB
22171 }
22172 break;
22173
0af92d60 22174 case DW_MACRO_start_file:
2e276125 22175 {
891d2f0b 22176 unsigned int bytes_read;
2e276125
JB
22177 int line, file;
22178
22179 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22180 mac_ptr += bytes_read;
22181 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22182 mac_ptr += bytes_read;
22183
3e43a32a
MS
22184 if ((line == 0 && !at_commandline)
22185 || (line != 0 && at_commandline))
757a13d0
JK
22186 complaint (&symfile_complaints,
22187 _("debug info gives source %d included "
22188 "from %s at %s line %d"),
22189 file, at_commandline ? _("command-line") : _("file"),
22190 line == 0 ? _("zero") : _("non-zero"), line);
22191
22192 if (at_commandline)
22193 {
0af92d60 22194 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22195 pass one. */
757a13d0
JK
22196 at_commandline = 0;
22197 }
22198 else
43f3e411 22199 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22200 }
22201 break;
22202
0af92d60 22203 case DW_MACRO_end_file:
2e276125 22204 if (! current_file)
4d3c2250 22205 complaint (&symfile_complaints,
3e43a32a
MS
22206 _("macro debug info has an unmatched "
22207 "`close_file' directive"));
2e276125
JB
22208 else
22209 {
22210 current_file = current_file->included_by;
22211 if (! current_file)
22212 {
cf2c3c16 22213 enum dwarf_macro_record_type next_type;
2e276125
JB
22214
22215 /* GCC circa March 2002 doesn't produce the zero
22216 type byte marking the end of the compilation
22217 unit. Complain if it's not there, but exit no
22218 matter what. */
22219
22220 /* Do we at least have room for a macinfo type byte? */
22221 if (mac_ptr >= mac_end)
22222 {
f664829e 22223 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22224 return;
22225 }
22226
22227 /* We don't increment mac_ptr here, so this is just
22228 a look-ahead. */
aead7601
SM
22229 next_type
22230 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22231 mac_ptr);
2e276125 22232 if (next_type != 0)
4d3c2250 22233 complaint (&symfile_complaints,
3e43a32a
MS
22234 _("no terminating 0-type entry for "
22235 "macros in `.debug_macinfo' section"));
2e276125
JB
22236
22237 return;
22238 }
22239 }
22240 break;
22241
0af92d60
JK
22242 case DW_MACRO_import:
22243 case DW_MACRO_import_sup:
cf2c3c16
TT
22244 {
22245 LONGEST offset;
8fc3fc34 22246 void **slot;
a036ba48
TT
22247 bfd *include_bfd = abfd;
22248 struct dwarf2_section_info *include_section = section;
d521ce57 22249 const gdb_byte *include_mac_end = mac_end;
a036ba48 22250 int is_dwz = section_is_dwz;
d521ce57 22251 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22252
22253 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22254 mac_ptr += offset_size;
22255
0af92d60 22256 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22257 {
22258 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22259
4d663531 22260 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22261
a036ba48 22262 include_section = &dwz->macro;
a32a8923 22263 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22264 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22265 is_dwz = 1;
22266 }
22267
22268 new_mac_ptr = include_section->buffer + offset;
22269 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22270
8fc3fc34
TT
22271 if (*slot != NULL)
22272 {
22273 /* This has actually happened; see
22274 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22275 complaint (&symfile_complaints,
0af92d60 22276 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22277 ".debug_macro section"));
22278 }
22279 else
22280 {
d521ce57 22281 *slot = (void *) new_mac_ptr;
36586728 22282
a036ba48 22283 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22284 include_mac_end, current_file, lh,
36586728 22285 section, section_is_gnu, is_dwz,
4d663531 22286 offset_size, include_hash);
8fc3fc34 22287
d521ce57 22288 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22289 }
cf2c3c16
TT
22290 }
22291 break;
22292
2e276125 22293 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22294 if (!section_is_gnu)
22295 {
22296 unsigned int bytes_read;
2e276125 22297
ac298888
TT
22298 /* This reads the constant, but since we don't recognize
22299 any vendor extensions, we ignore it. */
22300 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22301 mac_ptr += bytes_read;
22302 read_direct_string (abfd, mac_ptr, &bytes_read);
22303 mac_ptr += bytes_read;
2e276125 22304
cf2c3c16
TT
22305 /* We don't recognize any vendor extensions. */
22306 break;
22307 }
22308 /* FALLTHROUGH */
22309
22310 default:
22311 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22312 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22313 section);
22314 if (mac_ptr == NULL)
22315 return;
22316 break;
2e276125 22317 }
757a13d0 22318 } while (macinfo_type != 0);
2e276125 22319}
8e19ed76 22320
cf2c3c16 22321static void
09262596 22322dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22323 int section_is_gnu)
cf2c3c16 22324{
bb5ed363 22325 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22326 struct line_header *lh = cu->line_header;
22327 bfd *abfd;
d521ce57 22328 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22329 struct macro_source_file *current_file = 0;
22330 enum dwarf_macro_record_type macinfo_type;
22331 unsigned int offset_size = cu->header.offset_size;
d521ce57 22332 const gdb_byte *opcode_definitions[256];
8fc3fc34 22333 struct cleanup *cleanup;
8fc3fc34 22334 void **slot;
09262596
DE
22335 struct dwarf2_section_info *section;
22336 const char *section_name;
22337
22338 if (cu->dwo_unit != NULL)
22339 {
22340 if (section_is_gnu)
22341 {
22342 section = &cu->dwo_unit->dwo_file->sections.macro;
22343 section_name = ".debug_macro.dwo";
22344 }
22345 else
22346 {
22347 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22348 section_name = ".debug_macinfo.dwo";
22349 }
22350 }
22351 else
22352 {
22353 if (section_is_gnu)
22354 {
22355 section = &dwarf2_per_objfile->macro;
22356 section_name = ".debug_macro";
22357 }
22358 else
22359 {
22360 section = &dwarf2_per_objfile->macinfo;
22361 section_name = ".debug_macinfo";
22362 }
22363 }
cf2c3c16 22364
bb5ed363 22365 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22366 if (section->buffer == NULL)
22367 {
fceca515 22368 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22369 return;
22370 }
a32a8923 22371 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22372
22373 /* First pass: Find the name of the base filename.
22374 This filename is needed in order to process all macros whose definition
22375 (or undefinition) comes from the command line. These macros are defined
22376 before the first DW_MACINFO_start_file entry, and yet still need to be
22377 associated to the base file.
22378
22379 To determine the base file name, we scan the macro definitions until we
22380 reach the first DW_MACINFO_start_file entry. We then initialize
22381 CURRENT_FILE accordingly so that any macro definition found before the
22382 first DW_MACINFO_start_file can still be associated to the base file. */
22383
22384 mac_ptr = section->buffer + offset;
22385 mac_end = section->buffer + section->size;
22386
22387 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22388 &offset_size, section_is_gnu);
22389 if (mac_ptr == NULL)
22390 {
22391 /* We already issued a complaint. */
22392 return;
22393 }
22394
22395 do
22396 {
22397 /* Do we at least have room for a macinfo type byte? */
22398 if (mac_ptr >= mac_end)
22399 {
22400 /* Complaint is printed during the second pass as GDB will probably
22401 stop the first pass earlier upon finding
22402 DW_MACINFO_start_file. */
22403 break;
22404 }
22405
aead7601 22406 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22407 mac_ptr++;
22408
22409 /* Note that we rely on the fact that the corresponding GNU and
22410 DWARF constants are the same. */
22411 switch (macinfo_type)
22412 {
22413 /* A zero macinfo type indicates the end of the macro
22414 information. */
22415 case 0:
22416 break;
22417
0af92d60
JK
22418 case DW_MACRO_define:
22419 case DW_MACRO_undef:
cf2c3c16
TT
22420 /* Only skip the data by MAC_PTR. */
22421 {
22422 unsigned int bytes_read;
22423
22424 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22425 mac_ptr += bytes_read;
22426 read_direct_string (abfd, mac_ptr, &bytes_read);
22427 mac_ptr += bytes_read;
22428 }
22429 break;
22430
0af92d60 22431 case DW_MACRO_start_file:
cf2c3c16
TT
22432 {
22433 unsigned int bytes_read;
22434 int line, file;
22435
22436 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22437 mac_ptr += bytes_read;
22438 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22439 mac_ptr += bytes_read;
22440
43f3e411 22441 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22442 }
22443 break;
22444
0af92d60 22445 case DW_MACRO_end_file:
cf2c3c16
TT
22446 /* No data to skip by MAC_PTR. */
22447 break;
22448
0af92d60
JK
22449 case DW_MACRO_define_strp:
22450 case DW_MACRO_undef_strp:
22451 case DW_MACRO_define_sup:
22452 case DW_MACRO_undef_sup:
cf2c3c16
TT
22453 {
22454 unsigned int bytes_read;
22455
22456 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22457 mac_ptr += bytes_read;
22458 mac_ptr += offset_size;
22459 }
22460 break;
22461
0af92d60
JK
22462 case DW_MACRO_import:
22463 case DW_MACRO_import_sup:
cf2c3c16 22464 /* Note that, according to the spec, a transparent include
0af92d60 22465 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22466 skip this opcode. */
22467 mac_ptr += offset_size;
22468 break;
22469
22470 case DW_MACINFO_vendor_ext:
22471 /* Only skip the data by MAC_PTR. */
22472 if (!section_is_gnu)
22473 {
22474 unsigned int bytes_read;
22475
22476 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22477 mac_ptr += bytes_read;
22478 read_direct_string (abfd, mac_ptr, &bytes_read);
22479 mac_ptr += bytes_read;
22480 }
22481 /* FALLTHROUGH */
22482
22483 default:
22484 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22485 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22486 section);
22487 if (mac_ptr == NULL)
22488 return;
22489 break;
22490 }
22491 } while (macinfo_type != 0 && current_file == NULL);
22492
22493 /* Second pass: Process all entries.
22494
22495 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22496 command-line macro definitions/undefinitions. This flag is unset when we
22497 reach the first DW_MACINFO_start_file entry. */
22498
fc4007c9
TT
22499 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22500 htab_eq_pointer,
22501 NULL, xcalloc, xfree));
8fc3fc34 22502 mac_ptr = section->buffer + offset;
fc4007c9 22503 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22504 *slot = (void *) mac_ptr;
8fc3fc34 22505 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22506 current_file, lh, section,
fc4007c9
TT
22507 section_is_gnu, 0, offset_size,
22508 include_hash.get ());
cf2c3c16
TT
22509}
22510
8e19ed76 22511/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22512 if so return true else false. */
380bca97 22513
8e19ed76 22514static int
6e5a29e1 22515attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22516{
22517 return (attr == NULL ? 0 :
22518 attr->form == DW_FORM_block1
22519 || attr->form == DW_FORM_block2
22520 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22521 || attr->form == DW_FORM_block
22522 || attr->form == DW_FORM_exprloc);
8e19ed76 22523}
4c2df51b 22524
c6a0999f
JB
22525/* Return non-zero if ATTR's value is a section offset --- classes
22526 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22527 You may use DW_UNSND (attr) to retrieve such offsets.
22528
22529 Section 7.5.4, "Attribute Encodings", explains that no attribute
22530 may have a value that belongs to more than one of these classes; it
22531 would be ambiguous if we did, because we use the same forms for all
22532 of them. */
380bca97 22533
3690dd37 22534static int
6e5a29e1 22535attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22536{
22537 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22538 || attr->form == DW_FORM_data8
22539 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22540}
22541
3690dd37
JB
22542/* Return non-zero if ATTR's value falls in the 'constant' class, or
22543 zero otherwise. When this function returns true, you can apply
22544 dwarf2_get_attr_constant_value to it.
22545
22546 However, note that for some attributes you must check
22547 attr_form_is_section_offset before using this test. DW_FORM_data4
22548 and DW_FORM_data8 are members of both the constant class, and of
22549 the classes that contain offsets into other debug sections
22550 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22551 that, if an attribute's can be either a constant or one of the
22552 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22553 taken as section offsets, not constants.
22554
22555 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22556 cannot handle that. */
380bca97 22557
3690dd37 22558static int
6e5a29e1 22559attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22560{
22561 switch (attr->form)
22562 {
22563 case DW_FORM_sdata:
22564 case DW_FORM_udata:
22565 case DW_FORM_data1:
22566 case DW_FORM_data2:
22567 case DW_FORM_data4:
22568 case DW_FORM_data8:
663c44ac 22569 case DW_FORM_implicit_const:
3690dd37
JB
22570 return 1;
22571 default:
22572 return 0;
22573 }
22574}
22575
7771576e
SA
22576
22577/* DW_ADDR is always stored already as sect_offset; despite for the forms
22578 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22579
22580static int
6e5a29e1 22581attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22582{
22583 switch (attr->form)
22584 {
22585 case DW_FORM_ref_addr:
22586 case DW_FORM_ref1:
22587 case DW_FORM_ref2:
22588 case DW_FORM_ref4:
22589 case DW_FORM_ref8:
22590 case DW_FORM_ref_udata:
22591 case DW_FORM_GNU_ref_alt:
22592 return 1;
22593 default:
22594 return 0;
22595 }
22596}
22597
3019eac3
DE
22598/* Return the .debug_loc section to use for CU.
22599 For DWO files use .debug_loc.dwo. */
22600
22601static struct dwarf2_section_info *
22602cu_debug_loc_section (struct dwarf2_cu *cu)
22603{
22604 if (cu->dwo_unit)
43988095
JK
22605 {
22606 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22607
22608 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22609 }
22610 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22611 : &dwarf2_per_objfile->loc);
3019eac3
DE
22612}
22613
8cf6f0b1
TT
22614/* A helper function that fills in a dwarf2_loclist_baton. */
22615
22616static void
22617fill_in_loclist_baton (struct dwarf2_cu *cu,
22618 struct dwarf2_loclist_baton *baton,
ff39bb5e 22619 const struct attribute *attr)
8cf6f0b1 22620{
3019eac3
DE
22621 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22622
22623 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22624
22625 baton->per_cu = cu->per_cu;
22626 gdb_assert (baton->per_cu);
22627 /* We don't know how long the location list is, but make sure we
22628 don't run off the edge of the section. */
3019eac3
DE
22629 baton->size = section->size - DW_UNSND (attr);
22630 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22631 baton->base_address = cu->base_address;
f664829e 22632 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22633}
22634
4c2df51b 22635static void
ff39bb5e 22636dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22637 struct dwarf2_cu *cu, int is_block)
4c2df51b 22638{
bb5ed363 22639 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22640 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22641
3690dd37 22642 if (attr_form_is_section_offset (attr)
3019eac3 22643 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22644 the section. If so, fall through to the complaint in the
22645 other branch. */
3019eac3 22646 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22647 {
0d53c4c4 22648 struct dwarf2_loclist_baton *baton;
4c2df51b 22649
8d749320 22650 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22651
8cf6f0b1 22652 fill_in_loclist_baton (cu, baton, attr);
be391dca 22653
d00adf39 22654 if (cu->base_known == 0)
0d53c4c4 22655 complaint (&symfile_complaints,
3e43a32a
MS
22656 _("Location list used without "
22657 "specifying the CU base address."));
4c2df51b 22658
f1e6e072
TT
22659 SYMBOL_ACLASS_INDEX (sym) = (is_block
22660 ? dwarf2_loclist_block_index
22661 : dwarf2_loclist_index);
0d53c4c4
DJ
22662 SYMBOL_LOCATION_BATON (sym) = baton;
22663 }
22664 else
22665 {
22666 struct dwarf2_locexpr_baton *baton;
22667
8d749320 22668 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22669 baton->per_cu = cu->per_cu;
22670 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22671
22672 if (attr_form_is_block (attr))
22673 {
22674 /* Note that we're just copying the block's data pointer
22675 here, not the actual data. We're still pointing into the
6502dd73
DJ
22676 info_buffer for SYM's objfile; right now we never release
22677 that buffer, but when we do clean up properly this may
22678 need to change. */
0d53c4c4
DJ
22679 baton->size = DW_BLOCK (attr)->size;
22680 baton->data = DW_BLOCK (attr)->data;
22681 }
22682 else
22683 {
22684 dwarf2_invalid_attrib_class_complaint ("location description",
22685 SYMBOL_NATURAL_NAME (sym));
22686 baton->size = 0;
0d53c4c4 22687 }
6e70227d 22688
f1e6e072
TT
22689 SYMBOL_ACLASS_INDEX (sym) = (is_block
22690 ? dwarf2_locexpr_block_index
22691 : dwarf2_locexpr_index);
0d53c4c4
DJ
22692 SYMBOL_LOCATION_BATON (sym) = baton;
22693 }
4c2df51b 22694}
6502dd73 22695
9aa1f1e3
TT
22696/* Return the OBJFILE associated with the compilation unit CU. If CU
22697 came from a separate debuginfo file, then the master objfile is
22698 returned. */
ae0d2f24
UW
22699
22700struct objfile *
22701dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22702{
9291a0cd 22703 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22704
22705 /* Return the master objfile, so that we can report and look up the
22706 correct file containing this variable. */
22707 if (objfile->separate_debug_objfile_backlink)
22708 objfile = objfile->separate_debug_objfile_backlink;
22709
22710 return objfile;
22711}
22712
96408a79
SA
22713/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22714 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22715 CU_HEADERP first. */
22716
22717static const struct comp_unit_head *
22718per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22719 struct dwarf2_per_cu_data *per_cu)
22720{
d521ce57 22721 const gdb_byte *info_ptr;
96408a79
SA
22722
22723 if (per_cu->cu)
22724 return &per_cu->cu->header;
22725
9c541725 22726 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22727
22728 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22729 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22730 rcuh_kind::COMPILE);
96408a79
SA
22731
22732 return cu_headerp;
22733}
22734
ae0d2f24
UW
22735/* Return the address size given in the compilation unit header for CU. */
22736
98714339 22737int
ae0d2f24
UW
22738dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22739{
96408a79
SA
22740 struct comp_unit_head cu_header_local;
22741 const struct comp_unit_head *cu_headerp;
c471e790 22742
96408a79
SA
22743 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22744
22745 return cu_headerp->addr_size;
ae0d2f24
UW
22746}
22747
9eae7c52
TT
22748/* Return the offset size given in the compilation unit header for CU. */
22749
22750int
22751dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22752{
96408a79
SA
22753 struct comp_unit_head cu_header_local;
22754 const struct comp_unit_head *cu_headerp;
9c6c53f7 22755
96408a79
SA
22756 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22757
22758 return cu_headerp->offset_size;
22759}
22760
22761/* See its dwarf2loc.h declaration. */
22762
22763int
22764dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22765{
22766 struct comp_unit_head cu_header_local;
22767 const struct comp_unit_head *cu_headerp;
22768
22769 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22770
22771 if (cu_headerp->version == 2)
22772 return cu_headerp->addr_size;
22773 else
22774 return cu_headerp->offset_size;
181cebd4
JK
22775}
22776
9aa1f1e3
TT
22777/* Return the text offset of the CU. The returned offset comes from
22778 this CU's objfile. If this objfile came from a separate debuginfo
22779 file, then the offset may be different from the corresponding
22780 offset in the parent objfile. */
22781
22782CORE_ADDR
22783dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22784{
bb3fa9d0 22785 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22786
22787 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22788}
22789
43988095
JK
22790/* Return DWARF version number of PER_CU. */
22791
22792short
22793dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22794{
22795 return per_cu->dwarf_version;
22796}
22797
348e048f
DE
22798/* Locate the .debug_info compilation unit from CU's objfile which contains
22799 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22800
22801static struct dwarf2_per_cu_data *
9c541725 22802dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22803 unsigned int offset_in_dwz,
ae038cb0
DJ
22804 struct objfile *objfile)
22805{
22806 struct dwarf2_per_cu_data *this_cu;
22807 int low, high;
36586728 22808 const sect_offset *cu_off;
ae038cb0 22809
ae038cb0
DJ
22810 low = 0;
22811 high = dwarf2_per_objfile->n_comp_units - 1;
22812 while (high > low)
22813 {
36586728 22814 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22815 int mid = low + (high - low) / 2;
9a619af0 22816
36586728 22817 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22818 cu_off = &mid_cu->sect_off;
36586728 22819 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22820 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22821 high = mid;
22822 else
22823 low = mid + 1;
22824 }
22825 gdb_assert (low == high);
36586728 22826 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22827 cu_off = &this_cu->sect_off;
22828 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22829 {
36586728 22830 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22831 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22832 "offset 0x%x [in module %s]"),
22833 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22834
9c541725
PA
22835 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22836 <= sect_off);
ae038cb0
DJ
22837 return dwarf2_per_objfile->all_comp_units[low-1];
22838 }
22839 else
22840 {
22841 this_cu = dwarf2_per_objfile->all_comp_units[low];
22842 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22843 && sect_off >= this_cu->sect_off + this_cu->length)
22844 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22845 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22846 return this_cu;
22847 }
22848}
22849
23745b47 22850/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22851
9816fde3 22852static void
23745b47 22853init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22854{
9816fde3 22855 memset (cu, 0, sizeof (*cu));
23745b47
DE
22856 per_cu->cu = cu;
22857 cu->per_cu = per_cu;
22858 cu->objfile = per_cu->objfile;
93311388 22859 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22860}
22861
22862/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22863
22864static void
95554aad
TT
22865prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22866 enum language pretend_language)
9816fde3
JK
22867{
22868 struct attribute *attr;
22869
22870 /* Set the language we're debugging. */
22871 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22872 if (attr)
22873 set_cu_language (DW_UNSND (attr), cu);
22874 else
9cded63f 22875 {
95554aad 22876 cu->language = pretend_language;
9cded63f
TT
22877 cu->language_defn = language_def (cu->language);
22878 }
dee91e82 22879
7d45c7c3 22880 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22881}
22882
ae038cb0
DJ
22883/* Release one cached compilation unit, CU. We unlink it from the tree
22884 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22885 the caller is responsible for that.
22886 NOTE: DATA is a void * because this function is also used as a
22887 cleanup routine. */
ae038cb0
DJ
22888
22889static void
68dc6402 22890free_heap_comp_unit (void *data)
ae038cb0 22891{
9a3c8263 22892 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22893
23745b47
DE
22894 gdb_assert (cu->per_cu != NULL);
22895 cu->per_cu->cu = NULL;
ae038cb0
DJ
22896 cu->per_cu = NULL;
22897
22898 obstack_free (&cu->comp_unit_obstack, NULL);
22899
22900 xfree (cu);
22901}
22902
72bf9492 22903/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22904 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22905 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22906
22907static void
22908free_stack_comp_unit (void *data)
22909{
9a3c8263 22910 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22911
23745b47
DE
22912 gdb_assert (cu->per_cu != NULL);
22913 cu->per_cu->cu = NULL;
22914 cu->per_cu = NULL;
22915
72bf9492
DJ
22916 obstack_free (&cu->comp_unit_obstack, NULL);
22917 cu->partial_dies = NULL;
ae038cb0
DJ
22918}
22919
22920/* Free all cached compilation units. */
22921
22922static void
22923free_cached_comp_units (void *data)
22924{
330cdd98 22925 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22926}
22927
22928/* Increase the age counter on each cached compilation unit, and free
22929 any that are too old. */
22930
22931static void
22932age_cached_comp_units (void)
22933{
22934 struct dwarf2_per_cu_data *per_cu, **last_chain;
22935
22936 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22937 per_cu = dwarf2_per_objfile->read_in_chain;
22938 while (per_cu != NULL)
22939 {
22940 per_cu->cu->last_used ++;
b4f54984 22941 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22942 dwarf2_mark (per_cu->cu);
22943 per_cu = per_cu->cu->read_in_chain;
22944 }
22945
22946 per_cu = dwarf2_per_objfile->read_in_chain;
22947 last_chain = &dwarf2_per_objfile->read_in_chain;
22948 while (per_cu != NULL)
22949 {
22950 struct dwarf2_per_cu_data *next_cu;
22951
22952 next_cu = per_cu->cu->read_in_chain;
22953
22954 if (!per_cu->cu->mark)
22955 {
68dc6402 22956 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22957 *last_chain = next_cu;
22958 }
22959 else
22960 last_chain = &per_cu->cu->read_in_chain;
22961
22962 per_cu = next_cu;
22963 }
22964}
22965
22966/* Remove a single compilation unit from the cache. */
22967
22968static void
dee91e82 22969free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22970{
22971 struct dwarf2_per_cu_data *per_cu, **last_chain;
22972
22973 per_cu = dwarf2_per_objfile->read_in_chain;
22974 last_chain = &dwarf2_per_objfile->read_in_chain;
22975 while (per_cu != NULL)
22976 {
22977 struct dwarf2_per_cu_data *next_cu;
22978
22979 next_cu = per_cu->cu->read_in_chain;
22980
dee91e82 22981 if (per_cu == target_per_cu)
ae038cb0 22982 {
68dc6402 22983 free_heap_comp_unit (per_cu->cu);
dee91e82 22984 per_cu->cu = NULL;
ae038cb0
DJ
22985 *last_chain = next_cu;
22986 break;
22987 }
22988 else
22989 last_chain = &per_cu->cu->read_in_chain;
22990
22991 per_cu = next_cu;
22992 }
22993}
22994
fe3e1990
DJ
22995/* Release all extra memory associated with OBJFILE. */
22996
22997void
22998dwarf2_free_objfile (struct objfile *objfile)
22999{
9a3c8263
SM
23000 dwarf2_per_objfile
23001 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23002 dwarf2_objfile_data_key);
fe3e1990
DJ
23003
23004 if (dwarf2_per_objfile == NULL)
23005 return;
23006
330cdd98 23007 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23008}
23009
dee91e82
DE
23010/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23011 We store these in a hash table separate from the DIEs, and preserve them
23012 when the DIEs are flushed out of cache.
23013
23014 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23015 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23016 or the type may come from a DWO file. Furthermore, while it's more logical
23017 to use per_cu->section+offset, with Fission the section with the data is in
23018 the DWO file but we don't know that section at the point we need it.
23019 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23020 because we can enter the lookup routine, get_die_type_at_offset, from
23021 outside this file, and thus won't necessarily have PER_CU->cu.
23022 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23023
dee91e82 23024struct dwarf2_per_cu_offset_and_type
1c379e20 23025{
dee91e82 23026 const struct dwarf2_per_cu_data *per_cu;
9c541725 23027 sect_offset sect_off;
1c379e20
DJ
23028 struct type *type;
23029};
23030
dee91e82 23031/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23032
23033static hashval_t
dee91e82 23034per_cu_offset_and_type_hash (const void *item)
1c379e20 23035{
9a3c8263
SM
23036 const struct dwarf2_per_cu_offset_and_type *ofs
23037 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23038
9c541725 23039 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23040}
23041
dee91e82 23042/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23043
23044static int
dee91e82 23045per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23046{
9a3c8263
SM
23047 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23048 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23049 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23050 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23051
dee91e82 23052 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23053 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23054}
23055
23056/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23057 table if necessary. For convenience, return TYPE.
23058
23059 The DIEs reading must have careful ordering to:
23060 * Not cause infite loops trying to read in DIEs as a prerequisite for
23061 reading current DIE.
23062 * Not trying to dereference contents of still incompletely read in types
23063 while reading in other DIEs.
23064 * Enable referencing still incompletely read in types just by a pointer to
23065 the type without accessing its fields.
23066
23067 Therefore caller should follow these rules:
23068 * Try to fetch any prerequisite types we may need to build this DIE type
23069 before building the type and calling set_die_type.
e71ec853 23070 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23071 possible before fetching more types to complete the current type.
23072 * Make the type as complete as possible before fetching more types. */
1c379e20 23073
f792889a 23074static struct type *
1c379e20
DJ
23075set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23076{
dee91e82 23077 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23078 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23079 struct attribute *attr;
23080 struct dynamic_prop prop;
1c379e20 23081
b4ba55a1
JB
23082 /* For Ada types, make sure that the gnat-specific data is always
23083 initialized (if not already set). There are a few types where
23084 we should not be doing so, because the type-specific area is
23085 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23086 where the type-specific area is used to store the floatformat).
23087 But this is not a problem, because the gnat-specific information
23088 is actually not needed for these types. */
23089 if (need_gnat_info (cu)
23090 && TYPE_CODE (type) != TYPE_CODE_FUNC
23091 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23092 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23093 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23094 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23095 && !HAVE_GNAT_AUX_INFO (type))
23096 INIT_GNAT_SPECIFIC (type);
23097
3f2f83dd
KB
23098 /* Read DW_AT_allocated and set in type. */
23099 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23100 if (attr_form_is_block (attr))
23101 {
23102 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23103 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23104 }
23105 else if (attr != NULL)
23106 {
23107 complaint (&symfile_complaints,
9c541725
PA
23108 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23109 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23110 to_underlying (die->sect_off));
3f2f83dd
KB
23111 }
23112
23113 /* Read DW_AT_associated and set in type. */
23114 attr = dwarf2_attr (die, DW_AT_associated, cu);
23115 if (attr_form_is_block (attr))
23116 {
23117 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23118 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23119 }
23120 else if (attr != NULL)
23121 {
23122 complaint (&symfile_complaints,
9c541725
PA
23123 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23124 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23125 to_underlying (die->sect_off));
3f2f83dd
KB
23126 }
23127
3cdcd0ce
JB
23128 /* Read DW_AT_data_location and set in type. */
23129 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23130 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23131 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23132
dee91e82 23133 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23134 {
dee91e82
DE
23135 dwarf2_per_objfile->die_type_hash =
23136 htab_create_alloc_ex (127,
23137 per_cu_offset_and_type_hash,
23138 per_cu_offset_and_type_eq,
23139 NULL,
23140 &objfile->objfile_obstack,
23141 hashtab_obstack_allocate,
23142 dummy_obstack_deallocate);
f792889a 23143 }
1c379e20 23144
dee91e82 23145 ofs.per_cu = cu->per_cu;
9c541725 23146 ofs.sect_off = die->sect_off;
1c379e20 23147 ofs.type = type;
dee91e82
DE
23148 slot = (struct dwarf2_per_cu_offset_and_type **)
23149 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23150 if (*slot)
23151 complaint (&symfile_complaints,
23152 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23153 to_underlying (die->sect_off));
8d749320
SM
23154 *slot = XOBNEW (&objfile->objfile_obstack,
23155 struct dwarf2_per_cu_offset_and_type);
1c379e20 23156 **slot = ofs;
f792889a 23157 return type;
1c379e20
DJ
23158}
23159
9c541725 23160/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23161 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23162
23163static struct type *
9c541725 23164get_die_type_at_offset (sect_offset sect_off,
673bfd45 23165 struct dwarf2_per_cu_data *per_cu)
1c379e20 23166{
dee91e82 23167 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23168
dee91e82 23169 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23170 return NULL;
1c379e20 23171
dee91e82 23172 ofs.per_cu = per_cu;
9c541725 23173 ofs.sect_off = sect_off;
9a3c8263
SM
23174 slot = ((struct dwarf2_per_cu_offset_and_type *)
23175 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23176 if (slot)
23177 return slot->type;
23178 else
23179 return NULL;
23180}
23181
02142a6c 23182/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23183 or return NULL if DIE does not have a saved type. */
23184
23185static struct type *
23186get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23187{
9c541725 23188 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23189}
23190
10b3939b
DJ
23191/* Add a dependence relationship from CU to REF_PER_CU. */
23192
23193static void
23194dwarf2_add_dependence (struct dwarf2_cu *cu,
23195 struct dwarf2_per_cu_data *ref_per_cu)
23196{
23197 void **slot;
23198
23199 if (cu->dependencies == NULL)
23200 cu->dependencies
23201 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23202 NULL, &cu->comp_unit_obstack,
23203 hashtab_obstack_allocate,
23204 dummy_obstack_deallocate);
23205
23206 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23207 if (*slot == NULL)
23208 *slot = ref_per_cu;
23209}
1c379e20 23210
f504f079
DE
23211/* Subroutine of dwarf2_mark to pass to htab_traverse.
23212 Set the mark field in every compilation unit in the
ae038cb0
DJ
23213 cache that we must keep because we are keeping CU. */
23214
10b3939b
DJ
23215static int
23216dwarf2_mark_helper (void **slot, void *data)
23217{
23218 struct dwarf2_per_cu_data *per_cu;
23219
23220 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23221
23222 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23223 reading of the chain. As such dependencies remain valid it is not much
23224 useful to track and undo them during QUIT cleanups. */
23225 if (per_cu->cu == NULL)
23226 return 1;
23227
10b3939b
DJ
23228 if (per_cu->cu->mark)
23229 return 1;
23230 per_cu->cu->mark = 1;
23231
23232 if (per_cu->cu->dependencies != NULL)
23233 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23234
23235 return 1;
23236}
23237
f504f079
DE
23238/* Set the mark field in CU and in every other compilation unit in the
23239 cache that we must keep because we are keeping CU. */
23240
ae038cb0
DJ
23241static void
23242dwarf2_mark (struct dwarf2_cu *cu)
23243{
23244 if (cu->mark)
23245 return;
23246 cu->mark = 1;
10b3939b
DJ
23247 if (cu->dependencies != NULL)
23248 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23249}
23250
23251static void
23252dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23253{
23254 while (per_cu)
23255 {
23256 per_cu->cu->mark = 0;
23257 per_cu = per_cu->cu->read_in_chain;
23258 }
72bf9492
DJ
23259}
23260
72bf9492
DJ
23261/* Trivial hash function for partial_die_info: the hash value of a DIE
23262 is its offset in .debug_info for this objfile. */
23263
23264static hashval_t
23265partial_die_hash (const void *item)
23266{
9a3c8263
SM
23267 const struct partial_die_info *part_die
23268 = (const struct partial_die_info *) item;
9a619af0 23269
9c541725 23270 return to_underlying (part_die->sect_off);
72bf9492
DJ
23271}
23272
23273/* Trivial comparison function for partial_die_info structures: two DIEs
23274 are equal if they have the same offset. */
23275
23276static int
23277partial_die_eq (const void *item_lhs, const void *item_rhs)
23278{
9a3c8263
SM
23279 const struct partial_die_info *part_die_lhs
23280 = (const struct partial_die_info *) item_lhs;
23281 const struct partial_die_info *part_die_rhs
23282 = (const struct partial_die_info *) item_rhs;
9a619af0 23283
9c541725 23284 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23285}
23286
b4f54984
DE
23287static struct cmd_list_element *set_dwarf_cmdlist;
23288static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23289
23290static void
b4f54984 23291set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23292{
b4f54984 23293 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23294 gdb_stdout);
ae038cb0
DJ
23295}
23296
23297static void
b4f54984 23298show_dwarf_cmd (char *args, int from_tty)
6e70227d 23299{
b4f54984 23300 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23301}
23302
4bf44c1c 23303/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23304
23305static void
c1bd65d0 23306dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23307{
9a3c8263 23308 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23309 int ix;
8b70b953 23310
626f2d1c
TT
23311 /* Make sure we don't accidentally use dwarf2_per_objfile while
23312 cleaning up. */
23313 dwarf2_per_objfile = NULL;
23314
59b0c7c1
JB
23315 for (ix = 0; ix < data->n_comp_units; ++ix)
23316 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23317
59b0c7c1 23318 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23319 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23320 data->all_type_units[ix]->per_cu.imported_symtabs);
23321 xfree (data->all_type_units);
95554aad 23322
8b70b953 23323 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23324
23325 if (data->dwo_files)
23326 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23327 if (data->dwp_file)
23328 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23329
23330 if (data->dwz_file && data->dwz_file->dwz_bfd)
23331 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23332}
23333
23334\f
ae2de4f8 23335/* The "save gdb-index" command. */
9291a0cd 23336
bc8f2430
JK
23337/* In-memory buffer to prepare data to be written later to a file. */
23338class data_buf
9291a0cd 23339{
bc8f2430 23340public:
bc8f2430
JK
23341 /* Copy DATA to the end of the buffer. */
23342 template<typename T>
23343 void append_data (const T &data)
23344 {
23345 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23346 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23347 grow (sizeof (data)));
bc8f2430 23348 }
b89be57b 23349
c2f134ac
PA
23350 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23351 terminating zero is appended too. */
bc8f2430
JK
23352 void append_cstr0 (const char *cstr)
23353 {
23354 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23355 std::copy (cstr, cstr + size, grow (size));
23356 }
23357
23358 /* Accept a host-format integer in VAL and append it to the buffer
23359 as a target-format integer which is LEN bytes long. */
23360 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23361 {
23362 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23363 }
9291a0cd 23364
bc8f2430
JK
23365 /* Return the size of the buffer. */
23366 size_t size () const
23367 {
23368 return m_vec.size ();
23369 }
23370
23371 /* Write the buffer to FILE. */
23372 void file_write (FILE *file) const
23373 {
a81e6d4d
PA
23374 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23375 error (_("couldn't write data to file"));
bc8f2430
JK
23376 }
23377
23378private:
c2f134ac
PA
23379 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23380 the start of the new block. */
23381 gdb_byte *grow (size_t size)
23382 {
23383 m_vec.resize (m_vec.size () + size);
23384 return &*m_vec.end () - size;
23385 }
23386
d5722aa2 23387 gdb::byte_vector m_vec;
bc8f2430 23388};
9291a0cd
TT
23389
23390/* An entry in the symbol table. */
23391struct symtab_index_entry
23392{
23393 /* The name of the symbol. */
23394 const char *name;
23395 /* The offset of the name in the constant pool. */
23396 offset_type index_offset;
23397 /* A sorted vector of the indices of all the CUs that hold an object
23398 of this name. */
bc8f2430 23399 std::vector<offset_type> cu_indices;
9291a0cd
TT
23400};
23401
23402/* The symbol table. This is a power-of-2-sized hash table. */
23403struct mapped_symtab
23404{
bc8f2430
JK
23405 mapped_symtab ()
23406 {
23407 data.resize (1024);
23408 }
b89be57b 23409
bc8f2430 23410 offset_type n_elements = 0;
4b76cda9 23411 std::vector<symtab_index_entry> data;
bc8f2430 23412};
9291a0cd 23413
bc8f2430 23414/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23415 the slot.
23416
23417 Function is used only during write_hash_table so no index format backward
23418 compatibility is needed. */
b89be57b 23419
4b76cda9 23420static symtab_index_entry &
9291a0cd
TT
23421find_slot (struct mapped_symtab *symtab, const char *name)
23422{
559a7a62 23423 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23424
bc8f2430
JK
23425 index = hash & (symtab->data.size () - 1);
23426 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23427
23428 for (;;)
23429 {
4b76cda9
PA
23430 if (symtab->data[index].name == NULL
23431 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23432 return symtab->data[index];
23433 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23434 }
23435}
23436
23437/* Expand SYMTAB's hash table. */
b89be57b 23438
9291a0cd
TT
23439static void
23440hash_expand (struct mapped_symtab *symtab)
23441{
bc8f2430 23442 auto old_entries = std::move (symtab->data);
9291a0cd 23443
bc8f2430
JK
23444 symtab->data.clear ();
23445 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23446
bc8f2430 23447 for (auto &it : old_entries)
4b76cda9 23448 if (it.name != NULL)
bc8f2430 23449 {
4b76cda9 23450 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23451 ref = std::move (it);
23452 }
9291a0cd
TT
23453}
23454
156942c7
DE
23455/* Add an entry to SYMTAB. NAME is the name of the symbol.
23456 CU_INDEX is the index of the CU in which the symbol appears.
23457 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23458
9291a0cd
TT
23459static void
23460add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23461 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23462 offset_type cu_index)
23463{
156942c7 23464 offset_type cu_index_and_attrs;
9291a0cd
TT
23465
23466 ++symtab->n_elements;
bc8f2430 23467 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23468 hash_expand (symtab);
23469
4b76cda9
PA
23470 symtab_index_entry &slot = find_slot (symtab, name);
23471 if (slot.name == NULL)
9291a0cd 23472 {
4b76cda9 23473 slot.name = name;
156942c7 23474 /* index_offset is set later. */
9291a0cd 23475 }
156942c7
DE
23476
23477 cu_index_and_attrs = 0;
23478 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23479 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23480 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23481
23482 /* We don't want to record an index value twice as we want to avoid the
23483 duplication.
23484 We process all global symbols and then all static symbols
23485 (which would allow us to avoid the duplication by only having to check
23486 the last entry pushed), but a symbol could have multiple kinds in one CU.
23487 To keep things simple we don't worry about the duplication here and
23488 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23489 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23490}
23491
23492/* Sort and remove duplicates of all symbols' cu_indices lists. */
23493
23494static void
23495uniquify_cu_indices (struct mapped_symtab *symtab)
23496{
4b76cda9 23497 for (auto &entry : symtab->data)
156942c7 23498 {
4b76cda9 23499 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23500 {
4b76cda9 23501 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23502 std::sort (cu_indices.begin (), cu_indices.end ());
23503 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23504 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23505 }
23506 }
9291a0cd
TT
23507}
23508
bc8f2430
JK
23509/* A form of 'const char *' suitable for container keys. Only the
23510 pointer is stored. The strings themselves are compared, not the
23511 pointers. */
23512class c_str_view
9291a0cd 23513{
bc8f2430
JK
23514public:
23515 c_str_view (const char *cstr)
23516 : m_cstr (cstr)
23517 {}
9291a0cd 23518
bc8f2430
JK
23519 bool operator== (const c_str_view &other) const
23520 {
23521 return strcmp (m_cstr, other.m_cstr) == 0;
23522 }
9291a0cd 23523
bc8f2430
JK
23524private:
23525 friend class c_str_view_hasher;
23526 const char *const m_cstr;
23527};
9291a0cd 23528
bc8f2430
JK
23529/* A std::unordered_map::hasher for c_str_view that uses the right
23530 hash function for strings in a mapped index. */
23531class c_str_view_hasher
23532{
23533public:
23534 size_t operator () (const c_str_view &x) const
23535 {
23536 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23537 }
23538};
b89be57b 23539
bc8f2430
JK
23540/* A std::unordered_map::hasher for std::vector<>. */
23541template<typename T>
23542class vector_hasher
9291a0cd 23543{
bc8f2430
JK
23544public:
23545 size_t operator () (const std::vector<T> &key) const
23546 {
23547 return iterative_hash (key.data (),
23548 sizeof (key.front ()) * key.size (), 0);
23549 }
23550};
9291a0cd 23551
bc8f2430
JK
23552/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23553 constant pool entries going into the data buffer CPOOL. */
3876f04e 23554
bc8f2430
JK
23555static void
23556write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23557{
23558 {
23559 /* Elements are sorted vectors of the indices of all the CUs that
23560 hold an object of this name. */
23561 std::unordered_map<std::vector<offset_type>, offset_type,
23562 vector_hasher<offset_type>>
23563 symbol_hash_table;
23564
23565 /* We add all the index vectors to the constant pool first, to
23566 ensure alignment is ok. */
4b76cda9 23567 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23568 {
4b76cda9 23569 if (entry.name == NULL)
bc8f2430 23570 continue;
4b76cda9 23571 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23572
23573 /* Finding before inserting is faster than always trying to
23574 insert, because inserting always allocates a node, does the
23575 lookup, and then destroys the new node if another node
23576 already had the same key. C++17 try_emplace will avoid
23577 this. */
23578 const auto found
4b76cda9 23579 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23580 if (found != symbol_hash_table.end ())
23581 {
4b76cda9 23582 entry.index_offset = found->second;
70a1152b
PA
23583 continue;
23584 }
23585
4b76cda9
PA
23586 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23587 entry.index_offset = cpool.size ();
23588 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23589 for (const auto index : entry.cu_indices)
23590 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23591 }
23592 }
9291a0cd
TT
23593
23594 /* Now write out the hash table. */
bc8f2430 23595 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23596 for (const auto &entry : symtab->data)
9291a0cd
TT
23597 {
23598 offset_type str_off, vec_off;
23599
4b76cda9 23600 if (entry.name != NULL)
9291a0cd 23601 {
4b76cda9 23602 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23603 if (insertpair.second)
4b76cda9 23604 cpool.append_cstr0 (entry.name);
bc8f2430 23605 str_off = insertpair.first->second;
4b76cda9 23606 vec_off = entry.index_offset;
9291a0cd
TT
23607 }
23608 else
23609 {
23610 /* While 0 is a valid constant pool index, it is not valid
23611 to have 0 for both offsets. */
23612 str_off = 0;
23613 vec_off = 0;
23614 }
23615
bc8f2430
JK
23616 output.append_data (MAYBE_SWAP (str_off));
23617 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23618 }
9291a0cd
TT
23619}
23620
bc8f2430 23621typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23622
23623/* Helper struct for building the address table. */
23624struct addrmap_index_data
23625{
bc8f2430
JK
23626 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23627 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23628 {}
23629
0a5429f6 23630 struct objfile *objfile;
bc8f2430
JK
23631 data_buf &addr_vec;
23632 psym_index_map &cu_index_htab;
0a5429f6
DE
23633
23634 /* Non-zero if the previous_* fields are valid.
23635 We can't write an entry until we see the next entry (since it is only then
23636 that we know the end of the entry). */
23637 int previous_valid;
23638 /* Index of the CU in the table of all CUs in the index file. */
23639 unsigned int previous_cu_index;
0963b4bd 23640 /* Start address of the CU. */
0a5429f6
DE
23641 CORE_ADDR previous_cu_start;
23642};
23643
bc8f2430 23644/* Write an address entry to ADDR_VEC. */
b89be57b 23645
9291a0cd 23646static void
bc8f2430 23647add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23648 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23649{
9291a0cd
TT
23650 CORE_ADDR baseaddr;
23651
23652 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23653
c2f134ac
PA
23654 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23655 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23656 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23657}
23658
23659/* Worker function for traversing an addrmap to build the address table. */
23660
23661static int
23662add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23663{
9a3c8263
SM
23664 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23665 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23666
23667 if (data->previous_valid)
bc8f2430 23668 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23669 data->previous_cu_start, start_addr,
23670 data->previous_cu_index);
23671
23672 data->previous_cu_start = start_addr;
23673 if (pst != NULL)
23674 {
bc8f2430
JK
23675 const auto it = data->cu_index_htab.find (pst);
23676 gdb_assert (it != data->cu_index_htab.cend ());
23677 data->previous_cu_index = it->second;
0a5429f6
DE
23678 data->previous_valid = 1;
23679 }
23680 else
bc8f2430 23681 data->previous_valid = 0;
0a5429f6
DE
23682
23683 return 0;
23684}
23685
bc8f2430 23686/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23687 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23688 in the index file. */
23689
23690static void
bc8f2430
JK
23691write_address_map (struct objfile *objfile, data_buf &addr_vec,
23692 psym_index_map &cu_index_htab)
0a5429f6 23693{
bc8f2430 23694 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23695
23696 /* When writing the address table, we have to cope with the fact that
23697 the addrmap iterator only provides the start of a region; we have to
23698 wait until the next invocation to get the start of the next region. */
23699
23700 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23701 addrmap_index_data.previous_valid = 0;
23702
23703 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23704 &addrmap_index_data);
23705
23706 /* It's highly unlikely the last entry (end address = 0xff...ff)
23707 is valid, but we should still handle it.
23708 The end address is recorded as the start of the next region, but that
23709 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23710 anyway. */
23711 if (addrmap_index_data.previous_valid)
bc8f2430 23712 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23713 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23714 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23715}
23716
156942c7
DE
23717/* Return the symbol kind of PSYM. */
23718
23719static gdb_index_symbol_kind
23720symbol_kind (struct partial_symbol *psym)
23721{
23722 domain_enum domain = PSYMBOL_DOMAIN (psym);
23723 enum address_class aclass = PSYMBOL_CLASS (psym);
23724
23725 switch (domain)
23726 {
23727 case VAR_DOMAIN:
23728 switch (aclass)
23729 {
23730 case LOC_BLOCK:
23731 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23732 case LOC_TYPEDEF:
23733 return GDB_INDEX_SYMBOL_KIND_TYPE;
23734 case LOC_COMPUTED:
23735 case LOC_CONST_BYTES:
23736 case LOC_OPTIMIZED_OUT:
23737 case LOC_STATIC:
23738 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23739 case LOC_CONST:
23740 /* Note: It's currently impossible to recognize psyms as enum values
23741 short of reading the type info. For now punt. */
23742 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23743 default:
23744 /* There are other LOC_FOO values that one might want to classify
23745 as variables, but dwarf2read.c doesn't currently use them. */
23746 return GDB_INDEX_SYMBOL_KIND_OTHER;
23747 }
23748 case STRUCT_DOMAIN:
23749 return GDB_INDEX_SYMBOL_KIND_TYPE;
23750 default:
23751 return GDB_INDEX_SYMBOL_KIND_OTHER;
23752 }
23753}
23754
9291a0cd 23755/* Add a list of partial symbols to SYMTAB. */
b89be57b 23756
9291a0cd
TT
23757static void
23758write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23759 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23760 struct partial_symbol **psymp,
23761 int count,
987d643c
TT
23762 offset_type cu_index,
23763 int is_static)
9291a0cd
TT
23764{
23765 for (; count-- > 0; ++psymp)
23766 {
156942c7 23767 struct partial_symbol *psym = *psymp;
987d643c 23768
156942c7 23769 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23770 error (_("Ada is not currently supported by the index"));
987d643c 23771
987d643c 23772 /* Only add a given psymbol once. */
bc8f2430 23773 if (psyms_seen.insert (psym).second)
987d643c 23774 {
156942c7
DE
23775 gdb_index_symbol_kind kind = symbol_kind (psym);
23776
156942c7
DE
23777 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23778 is_static, kind, cu_index);
987d643c 23779 }
9291a0cd
TT
23780 }
23781}
23782
1fd400ff
TT
23783/* A helper struct used when iterating over debug_types. */
23784struct signatured_type_index_data
23785{
bc8f2430
JK
23786 signatured_type_index_data (data_buf &types_list_,
23787 std::unordered_set<partial_symbol *> &psyms_seen_)
23788 : types_list (types_list_), psyms_seen (psyms_seen_)
23789 {}
23790
1fd400ff
TT
23791 struct objfile *objfile;
23792 struct mapped_symtab *symtab;
bc8f2430
JK
23793 data_buf &types_list;
23794 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23795 int cu_index;
23796};
23797
23798/* A helper function that writes a single signatured_type to an
23799 obstack. */
b89be57b 23800
1fd400ff
TT
23801static int
23802write_one_signatured_type (void **slot, void *d)
23803{
9a3c8263
SM
23804 struct signatured_type_index_data *info
23805 = (struct signatured_type_index_data *) d;
1fd400ff 23806 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23807 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23808
23809 write_psymbols (info->symtab,
987d643c 23810 info->psyms_seen,
3e43a32a
MS
23811 info->objfile->global_psymbols.list
23812 + psymtab->globals_offset,
987d643c
TT
23813 psymtab->n_global_syms, info->cu_index,
23814 0);
1fd400ff 23815 write_psymbols (info->symtab,
987d643c 23816 info->psyms_seen,
3e43a32a
MS
23817 info->objfile->static_psymbols.list
23818 + psymtab->statics_offset,
987d643c
TT
23819 psymtab->n_static_syms, info->cu_index,
23820 1);
1fd400ff 23821
c2f134ac
PA
23822 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23823 to_underlying (entry->per_cu.sect_off));
23824 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23825 to_underlying (entry->type_offset_in_tu));
23826 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23827
23828 ++info->cu_index;
23829
23830 return 1;
23831}
23832
e8f8bcb3
PA
23833/* Recurse into all "included" dependencies and count their symbols as
23834 if they appeared in this psymtab. */
23835
23836static void
23837recursively_count_psymbols (struct partial_symtab *psymtab,
23838 size_t &psyms_seen)
23839{
23840 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23841 if (psymtab->dependencies[i]->user != NULL)
23842 recursively_count_psymbols (psymtab->dependencies[i],
23843 psyms_seen);
23844
23845 psyms_seen += psymtab->n_global_syms;
23846 psyms_seen += psymtab->n_static_syms;
23847}
23848
95554aad
TT
23849/* Recurse into all "included" dependencies and write their symbols as
23850 if they appeared in this psymtab. */
23851
23852static void
23853recursively_write_psymbols (struct objfile *objfile,
23854 struct partial_symtab *psymtab,
23855 struct mapped_symtab *symtab,
bc8f2430 23856 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23857 offset_type cu_index)
23858{
23859 int i;
23860
23861 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23862 if (psymtab->dependencies[i]->user != NULL)
23863 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23864 symtab, psyms_seen, cu_index);
23865
23866 write_psymbols (symtab,
23867 psyms_seen,
23868 objfile->global_psymbols.list + psymtab->globals_offset,
23869 psymtab->n_global_syms, cu_index,
23870 0);
23871 write_psymbols (symtab,
23872 psyms_seen,
23873 objfile->static_psymbols.list + psymtab->statics_offset,
23874 psymtab->n_static_syms, cu_index,
23875 1);
23876}
23877
9291a0cd 23878/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23879
9291a0cd
TT
23880static void
23881write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23882{
9291a0cd
TT
23883 if (dwarf2_per_objfile->using_index)
23884 error (_("Cannot use an index to create the index"));
23885
8b70b953
TT
23886 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23887 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23888
260b681b
DE
23889 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23890 return;
23891
bc8f2430 23892 struct stat st;
4262abfb
JK
23893 if (stat (objfile_name (objfile), &st) < 0)
23894 perror_with_name (objfile_name (objfile));
9291a0cd 23895
bc8f2430
JK
23896 std::string filename (std::string (dir) + SLASH_STRING
23897 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23898
d419f42d 23899 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23900 if (!out_file)
bc8f2430 23901 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23902
16b7a719
PA
23903 /* Order matters here; we want FILE to be closed before FILENAME is
23904 unlinked, because on MS-Windows one cannot delete a file that is
23905 still open. (Don't call anything here that might throw until
23906 file_closer is created.) */
bc8f2430 23907 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23908 gdb_file_up close_out_file (out_file);
9291a0cd 23909
bc8f2430
JK
23910 mapped_symtab symtab;
23911 data_buf cu_list;
987d643c 23912
0a5429f6
DE
23913 /* While we're scanning CU's create a table that maps a psymtab pointer
23914 (which is what addrmap records) to its index (which is what is recorded
23915 in the index file). This will later be needed to write the address
23916 table. */
bc8f2430
JK
23917 psym_index_map cu_index_htab;
23918 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23919
23920 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23921 work here. Also, the debug_types entries do not appear in
23922 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23923
23924 /* The psyms_seen set is potentially going to be largish (~40k
23925 elements when indexing a -g3 build of GDB itself). Estimate the
23926 number of elements in order to avoid too many rehashes, which
23927 require rebuilding buckets and thus many trips to
23928 malloc/free. */
23929 size_t psyms_count = 0;
23930 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23931 {
23932 struct dwarf2_per_cu_data *per_cu
23933 = dwarf2_per_objfile->all_comp_units[i];
23934 struct partial_symtab *psymtab = per_cu->v.psymtab;
23935
23936 if (psymtab != NULL && psymtab->user == NULL)
23937 recursively_count_psymbols (psymtab, psyms_count);
23938 }
23939 /* Generating an index for gdb itself shows a ratio of
23940 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23941 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23942 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23943 {
3e43a32a
MS
23944 struct dwarf2_per_cu_data *per_cu
23945 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23946 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23947
92fac807
JK
23948 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23949 It may be referenced from a local scope but in such case it does not
23950 need to be present in .gdb_index. */
23951 if (psymtab == NULL)
23952 continue;
23953
95554aad 23954 if (psymtab->user == NULL)
bc8f2430
JK
23955 recursively_write_psymbols (objfile, psymtab, &symtab,
23956 psyms_seen, i);
9291a0cd 23957
bc8f2430
JK
23958 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23959 gdb_assert (insertpair.second);
9291a0cd 23960
c2f134ac
PA
23961 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23962 to_underlying (per_cu->sect_off));
23963 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23964 }
23965
0a5429f6 23966 /* Dump the address map. */
bc8f2430
JK
23967 data_buf addr_vec;
23968 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23969
1fd400ff 23970 /* Write out the .debug_type entries, if any. */
bc8f2430 23971 data_buf types_cu_list;
1fd400ff
TT
23972 if (dwarf2_per_objfile->signatured_types)
23973 {
bc8f2430
JK
23974 signatured_type_index_data sig_data (types_cu_list,
23975 psyms_seen);
1fd400ff
TT
23976
23977 sig_data.objfile = objfile;
bc8f2430 23978 sig_data.symtab = &symtab;
1fd400ff
TT
23979 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23980 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23981 write_one_signatured_type, &sig_data);
23982 }
23983
156942c7
DE
23984 /* Now that we've processed all symbols we can shrink their cu_indices
23985 lists. */
bc8f2430 23986 uniquify_cu_indices (&symtab);
156942c7 23987
bc8f2430
JK
23988 data_buf symtab_vec, constant_pool;
23989 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23990
bc8f2430
JK
23991 data_buf contents;
23992 const offset_type size_of_contents = 6 * sizeof (offset_type);
23993 offset_type total_len = size_of_contents;
9291a0cd
TT
23994
23995 /* The version number. */
bc8f2430 23996 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
23997
23998 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
23999 contents.append_data (MAYBE_SWAP (total_len));
24000 total_len += cu_list.size ();
9291a0cd 24001
1fd400ff 24002 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24003 contents.append_data (MAYBE_SWAP (total_len));
24004 total_len += types_cu_list.size ();
1fd400ff 24005
9291a0cd 24006 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24007 contents.append_data (MAYBE_SWAP (total_len));
24008 total_len += addr_vec.size ();
9291a0cd
TT
24009
24010 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24011 contents.append_data (MAYBE_SWAP (total_len));
24012 total_len += symtab_vec.size ();
9291a0cd
TT
24013
24014 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24015 contents.append_data (MAYBE_SWAP (total_len));
24016 total_len += constant_pool.size ();
9291a0cd 24017
bc8f2430 24018 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24019
bc8f2430
JK
24020 contents.file_write (out_file);
24021 cu_list.file_write (out_file);
24022 types_cu_list.file_write (out_file);
24023 addr_vec.file_write (out_file);
24024 symtab_vec.file_write (out_file);
24025 constant_pool.file_write (out_file);
9291a0cd 24026
bef155c3
TT
24027 /* We want to keep the file. */
24028 unlink_file.keep ();
9291a0cd
TT
24029}
24030
90476074
TT
24031/* Implementation of the `save gdb-index' command.
24032
24033 Note that the file format used by this command is documented in the
24034 GDB manual. Any changes here must be documented there. */
11570e71 24035
9291a0cd 24036static void
8384c356 24037save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24038{
24039 struct objfile *objfile;
24040
24041 if (!arg || !*arg)
96d19272 24042 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24043
24044 ALL_OBJFILES (objfile)
24045 {
24046 struct stat st;
24047
24048 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24049 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24050 continue;
24051
9a3c8263
SM
24052 dwarf2_per_objfile
24053 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24054 dwarf2_objfile_data_key);
9291a0cd
TT
24055 if (dwarf2_per_objfile)
24056 {
9291a0cd 24057
492d29ea 24058 TRY
9291a0cd
TT
24059 {
24060 write_psymtabs_to_index (objfile, arg);
24061 }
492d29ea
PA
24062 CATCH (except, RETURN_MASK_ERROR)
24063 {
24064 exception_fprintf (gdb_stderr, except,
24065 _("Error while writing index for `%s': "),
24066 objfile_name (objfile));
24067 }
24068 END_CATCH
9291a0cd
TT
24069 }
24070 }
dce234bc
PP
24071}
24072
9291a0cd
TT
24073\f
24074
b4f54984 24075int dwarf_always_disassemble;
9eae7c52
TT
24076
24077static void
b4f54984
DE
24078show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24079 struct cmd_list_element *c, const char *value)
9eae7c52 24080{
3e43a32a
MS
24081 fprintf_filtered (file,
24082 _("Whether to always disassemble "
24083 "DWARF expressions is %s.\n"),
9eae7c52
TT
24084 value);
24085}
24086
900e11f9
JK
24087static void
24088show_check_physname (struct ui_file *file, int from_tty,
24089 struct cmd_list_element *c, const char *value)
24090{
24091 fprintf_filtered (file,
24092 _("Whether to check \"physname\" is %s.\n"),
24093 value);
24094}
24095
6502dd73
DJ
24096void
24097_initialize_dwarf2_read (void)
24098{
96d19272
JK
24099 struct cmd_list_element *c;
24100
dce234bc 24101 dwarf2_objfile_data_key
c1bd65d0 24102 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24103
b4f54984
DE
24104 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24105Set DWARF specific variables.\n\
24106Configure DWARF variables such as the cache size"),
24107 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24108 0/*allow-unknown*/, &maintenance_set_cmdlist);
24109
b4f54984
DE
24110 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24111Show DWARF specific variables\n\
24112Show DWARF variables such as the cache size"),
24113 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24114 0/*allow-unknown*/, &maintenance_show_cmdlist);
24115
24116 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24117 &dwarf_max_cache_age, _("\
24118Set the upper bound on the age of cached DWARF compilation units."), _("\
24119Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24120A higher limit means that cached compilation units will be stored\n\
24121in memory longer, and more total memory will be used. Zero disables\n\
24122caching, which can slow down startup."),
2c5b56ce 24123 NULL,
b4f54984
DE
24124 show_dwarf_max_cache_age,
24125 &set_dwarf_cmdlist,
24126 &show_dwarf_cmdlist);
d97bc12b 24127
9eae7c52 24128 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24129 &dwarf_always_disassemble, _("\
9eae7c52
TT
24130Set whether `info address' always disassembles DWARF expressions."), _("\
24131Show whether `info address' always disassembles DWARF expressions."), _("\
24132When enabled, DWARF expressions are always printed in an assembly-like\n\
24133syntax. When disabled, expressions will be printed in a more\n\
24134conversational style, when possible."),
24135 NULL,
b4f54984
DE
24136 show_dwarf_always_disassemble,
24137 &set_dwarf_cmdlist,
24138 &show_dwarf_cmdlist);
24139
24140 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24141Set debugging of the DWARF reader."), _("\
24142Show debugging of the DWARF reader."), _("\
24143When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24144reading and symtab expansion. A value of 1 (one) provides basic\n\
24145information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24146 NULL,
24147 NULL,
24148 &setdebuglist, &showdebuglist);
24149
b4f54984
DE
24150 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24151Set debugging of the DWARF DIE reader."), _("\
24152Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24153When enabled (non-zero), DIEs are dumped after they are read in.\n\
24154The value is the maximum depth to print."),
ccce17b0
YQ
24155 NULL,
24156 NULL,
24157 &setdebuglist, &showdebuglist);
9291a0cd 24158
27e0867f
DE
24159 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24160Set debugging of the dwarf line reader."), _("\
24161Show debugging of the dwarf line reader."), _("\
24162When enabled (non-zero), line number entries are dumped as they are read in.\n\
24163A value of 1 (one) provides basic information.\n\
24164A value greater than 1 provides more verbose information."),
24165 NULL,
24166 NULL,
24167 &setdebuglist, &showdebuglist);
24168
900e11f9
JK
24169 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24170Set cross-checking of \"physname\" code against demangler."), _("\
24171Show cross-checking of \"physname\" code against demangler."), _("\
24172When enabled, GDB's internal \"physname\" code is checked against\n\
24173the demangler."),
24174 NULL, show_check_physname,
24175 &setdebuglist, &showdebuglist);
24176
e615022a
DE
24177 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24178 no_class, &use_deprecated_index_sections, _("\
24179Set whether to use deprecated gdb_index sections."), _("\
24180Show whether to use deprecated gdb_index sections."), _("\
24181When enabled, deprecated .gdb_index sections are used anyway.\n\
24182Normally they are ignored either because of a missing feature or\n\
24183performance issue.\n\
24184Warning: This option must be enabled before gdb reads the file."),
24185 NULL,
24186 NULL,
24187 &setlist, &showlist);
24188
96d19272 24189 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24190 _("\
fc1a9d6e 24191Save a gdb-index file.\n\
11570e71 24192Usage: save gdb-index DIRECTORY"),
96d19272
JK
24193 &save_cmdlist);
24194 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24195
24196 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24197 &dwarf2_locexpr_funcs);
24198 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24199 &dwarf2_loclist_funcs);
24200
24201 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24202 &dwarf2_block_frame_base_locexpr_funcs);
24203 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24204 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24205}
This page took 4.860063 seconds and 4 git commands to generate.