Remove cleanups from break-catch-syscall.c
[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{
36586728
TT
2689 const char *filename;
2690 struct dwz_file *result;
acd13123 2691 bfd_size_type buildid_len_arg;
dc294be5
TT
2692 size_t buildid_len;
2693 bfd_byte *buildid;
36586728
TT
2694
2695 if (dwarf2_per_objfile->dwz_file != NULL)
2696 return dwarf2_per_objfile->dwz_file;
2697
4db1a1dc 2698 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2699 gdb::unique_xmalloc_ptr<char> data
2700 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2701 &buildid_len_arg, &buildid));
4db1a1dc
TT
2702 if (data == NULL)
2703 {
2704 if (bfd_get_error () == bfd_error_no_error)
2705 return NULL;
2706 error (_("could not read '.gnu_debugaltlink' section: %s"),
2707 bfd_errmsg (bfd_get_error ()));
2708 }
791afaa2
TT
2709
2710 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2711
acd13123
TT
2712 buildid_len = (size_t) buildid_len_arg;
2713
791afaa2 2714 filename = data.get ();
d721ba37
PA
2715
2716 std::string abs_storage;
36586728
TT
2717 if (!IS_ABSOLUTE_PATH (filename))
2718 {
14278e1f
TT
2719 gdb::unique_xmalloc_ptr<char> abs
2720 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2721
14278e1f 2722 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2723 filename = abs_storage.c_str ();
36586728
TT
2724 }
2725
dc294be5
TT
2726 /* First try the file name given in the section. If that doesn't
2727 work, try to use the build-id instead. */
192b62ce 2728 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2729 if (dwz_bfd != NULL)
36586728 2730 {
192b62ce
TT
2731 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2732 dwz_bfd.release ();
36586728
TT
2733 }
2734
dc294be5
TT
2735 if (dwz_bfd == NULL)
2736 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2737
2738 if (dwz_bfd == NULL)
2739 error (_("could not find '.gnu_debugaltlink' file for %s"),
2740 objfile_name (dwarf2_per_objfile->objfile));
2741
36586728
TT
2742 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2743 struct dwz_file);
192b62ce 2744 result->dwz_bfd = dwz_bfd.release ();
36586728 2745
192b62ce 2746 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2747
192b62ce 2748 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2749 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2750 return result;
2751}
9291a0cd 2752\f
7b9f3c50
DE
2753/* DWARF quick_symbols_functions support. */
2754
2755/* TUs can share .debug_line entries, and there can be a lot more TUs than
2756 unique line tables, so we maintain a separate table of all .debug_line
2757 derived entries to support the sharing.
2758 All the quick functions need is the list of file names. We discard the
2759 line_header when we're done and don't need to record it here. */
2760struct quick_file_names
2761{
094b34ac
DE
2762 /* The data used to construct the hash key. */
2763 struct stmt_list_hash hash;
7b9f3c50
DE
2764
2765 /* The number of entries in file_names, real_names. */
2766 unsigned int num_file_names;
2767
2768 /* The file names from the line table, after being run through
2769 file_full_name. */
2770 const char **file_names;
2771
2772 /* The file names from the line table after being run through
2773 gdb_realpath. These are computed lazily. */
2774 const char **real_names;
2775};
2776
2777/* When using the index (and thus not using psymtabs), each CU has an
2778 object of this type. This is used to hold information needed by
2779 the various "quick" methods. */
2780struct dwarf2_per_cu_quick_data
2781{
2782 /* The file table. This can be NULL if there was no file table
2783 or it's currently not read in.
2784 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2785 struct quick_file_names *file_names;
2786
2787 /* The corresponding symbol table. This is NULL if symbols for this
2788 CU have not yet been read. */
43f3e411 2789 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2790
2791 /* A temporary mark bit used when iterating over all CUs in
2792 expand_symtabs_matching. */
2793 unsigned int mark : 1;
2794
2795 /* True if we've tried to read the file table and found there isn't one.
2796 There will be no point in trying to read it again next time. */
2797 unsigned int no_file_data : 1;
2798};
2799
094b34ac
DE
2800/* Utility hash function for a stmt_list_hash. */
2801
2802static hashval_t
2803hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2804{
2805 hashval_t v = 0;
2806
2807 if (stmt_list_hash->dwo_unit != NULL)
2808 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2809 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2810 return v;
2811}
2812
2813/* Utility equality function for a stmt_list_hash. */
2814
2815static int
2816eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2817 const struct stmt_list_hash *rhs)
2818{
2819 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2820 return 0;
2821 if (lhs->dwo_unit != NULL
2822 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2823 return 0;
2824
9c541725 2825 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2826}
2827
7b9f3c50
DE
2828/* Hash function for a quick_file_names. */
2829
2830static hashval_t
2831hash_file_name_entry (const void *e)
2832{
9a3c8263
SM
2833 const struct quick_file_names *file_data
2834 = (const struct quick_file_names *) e;
7b9f3c50 2835
094b34ac 2836 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2837}
2838
2839/* Equality function for a quick_file_names. */
2840
2841static int
2842eq_file_name_entry (const void *a, const void *b)
2843{
9a3c8263
SM
2844 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2845 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2846
094b34ac 2847 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2848}
2849
2850/* Delete function for a quick_file_names. */
2851
2852static void
2853delete_file_name_entry (void *e)
2854{
9a3c8263 2855 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2856 int i;
2857
2858 for (i = 0; i < file_data->num_file_names; ++i)
2859 {
2860 xfree ((void*) file_data->file_names[i]);
2861 if (file_data->real_names)
2862 xfree ((void*) file_data->real_names[i]);
2863 }
2864
2865 /* The space for the struct itself lives on objfile_obstack,
2866 so we don't free it here. */
2867}
2868
2869/* Create a quick_file_names hash table. */
2870
2871static htab_t
2872create_quick_file_names_table (unsigned int nr_initial_entries)
2873{
2874 return htab_create_alloc (nr_initial_entries,
2875 hash_file_name_entry, eq_file_name_entry,
2876 delete_file_name_entry, xcalloc, xfree);
2877}
9291a0cd 2878
918dd910
JK
2879/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2880 have to be created afterwards. You should call age_cached_comp_units after
2881 processing PER_CU->CU. dw2_setup must have been already called. */
2882
2883static void
2884load_cu (struct dwarf2_per_cu_data *per_cu)
2885{
3019eac3 2886 if (per_cu->is_debug_types)
e5fe5e75 2887 load_full_type_unit (per_cu);
918dd910 2888 else
95554aad 2889 load_full_comp_unit (per_cu, language_minimal);
918dd910 2890
cc12ce38
DE
2891 if (per_cu->cu == NULL)
2892 return; /* Dummy CU. */
2dc860c0
DE
2893
2894 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2895}
2896
a0f42c21 2897/* Read in the symbols for PER_CU. */
2fdf6df6 2898
9291a0cd 2899static void
a0f42c21 2900dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2901{
2902 struct cleanup *back_to;
2903
f4dc4d17
DE
2904 /* Skip type_unit_groups, reading the type units they contain
2905 is handled elsewhere. */
2906 if (IS_TYPE_UNIT_GROUP (per_cu))
2907 return;
2908
9291a0cd
TT
2909 back_to = make_cleanup (dwarf2_release_queue, NULL);
2910
95554aad 2911 if (dwarf2_per_objfile->using_index
43f3e411 2912 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2913 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2914 {
2915 queue_comp_unit (per_cu, language_minimal);
2916 load_cu (per_cu);
89e63ee4
DE
2917
2918 /* If we just loaded a CU from a DWO, and we're working with an index
2919 that may badly handle TUs, load all the TUs in that DWO as well.
2920 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2921 if (!per_cu->is_debug_types
cc12ce38 2922 && per_cu->cu != NULL
89e63ee4
DE
2923 && per_cu->cu->dwo_unit != NULL
2924 && dwarf2_per_objfile->index_table != NULL
2925 && dwarf2_per_objfile->index_table->version <= 7
2926 /* DWP files aren't supported yet. */
2927 && get_dwp_file () == NULL)
2928 queue_and_load_all_dwo_tus (per_cu);
95554aad 2929 }
9291a0cd 2930
a0f42c21 2931 process_queue ();
9291a0cd
TT
2932
2933 /* Age the cache, releasing compilation units that have not
2934 been used recently. */
2935 age_cached_comp_units ();
2936
2937 do_cleanups (back_to);
2938}
2939
2940/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2941 the objfile from which this CU came. Returns the resulting symbol
2942 table. */
2fdf6df6 2943
43f3e411 2944static struct compunit_symtab *
a0f42c21 2945dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2946{
95554aad 2947 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2948 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2949 {
2950 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2951 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2952 dw2_do_instantiate_symtab (per_cu);
95554aad 2953 process_cu_includes ();
9291a0cd
TT
2954 do_cleanups (back_to);
2955 }
f194fefb 2956
43f3e411 2957 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2958}
2959
8832e7e3 2960/* Return the CU/TU given its index.
f4dc4d17
DE
2961
2962 This is intended for loops like:
2963
2964 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2965 + dwarf2_per_objfile->n_type_units); ++i)
2966 {
8832e7e3 2967 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2968
2969 ...;
2970 }
2971*/
2fdf6df6 2972
1fd400ff 2973static struct dwarf2_per_cu_data *
8832e7e3 2974dw2_get_cutu (int index)
1fd400ff
TT
2975{
2976 if (index >= dwarf2_per_objfile->n_comp_units)
2977 {
f4dc4d17 2978 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2979 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2980 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2981 }
2982
2983 return dwarf2_per_objfile->all_comp_units[index];
2984}
2985
8832e7e3
DE
2986/* Return the CU given its index.
2987 This differs from dw2_get_cutu in that it's for when you know INDEX
2988 refers to a CU. */
f4dc4d17
DE
2989
2990static struct dwarf2_per_cu_data *
8832e7e3 2991dw2_get_cu (int index)
f4dc4d17 2992{
8832e7e3 2993 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2994
1fd400ff
TT
2995 return dwarf2_per_objfile->all_comp_units[index];
2996}
2997
2ec9a5e0
TT
2998/* A helper for create_cus_from_index that handles a given list of
2999 CUs. */
2fdf6df6 3000
74a0d9f6 3001static void
2ec9a5e0
TT
3002create_cus_from_index_list (struct objfile *objfile,
3003 const gdb_byte *cu_list, offset_type n_elements,
3004 struct dwarf2_section_info *section,
3005 int is_dwz,
3006 int base_offset)
9291a0cd
TT
3007{
3008 offset_type i;
9291a0cd 3009
2ec9a5e0 3010 for (i = 0; i < n_elements; i += 2)
9291a0cd 3011 {
74a0d9f6 3012 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3013
3014 sect_offset sect_off
3015 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3016 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3017 cu_list += 2 * 8;
3018
9c541725
PA
3019 dwarf2_per_cu_data *the_cu
3020 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3021 struct dwarf2_per_cu_data);
3022 the_cu->sect_off = sect_off;
9291a0cd
TT
3023 the_cu->length = length;
3024 the_cu->objfile = objfile;
8a0459fd 3025 the_cu->section = section;
9291a0cd
TT
3026 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3027 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3028 the_cu->is_dwz = is_dwz;
3029 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3030 }
9291a0cd
TT
3031}
3032
2ec9a5e0 3033/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3034 the CU objects for this objfile. */
2ec9a5e0 3035
74a0d9f6 3036static void
2ec9a5e0
TT
3037create_cus_from_index (struct objfile *objfile,
3038 const gdb_byte *cu_list, offset_type cu_list_elements,
3039 const gdb_byte *dwz_list, offset_type dwz_elements)
3040{
3041 struct dwz_file *dwz;
3042
3043 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3044 dwarf2_per_objfile->all_comp_units =
3045 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3046 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3047
74a0d9f6
JK
3048 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3049 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3050
3051 if (dwz_elements == 0)
74a0d9f6 3052 return;
2ec9a5e0
TT
3053
3054 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3055 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3056 cu_list_elements / 2);
2ec9a5e0
TT
3057}
3058
1fd400ff 3059/* Create the signatured type hash table from the index. */
673bfd45 3060
74a0d9f6 3061static void
673bfd45 3062create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3063 struct dwarf2_section_info *section,
673bfd45
DE
3064 const gdb_byte *bytes,
3065 offset_type elements)
1fd400ff
TT
3066{
3067 offset_type i;
673bfd45 3068 htab_t sig_types_hash;
1fd400ff 3069
6aa5f3a6
DE
3070 dwarf2_per_objfile->n_type_units
3071 = dwarf2_per_objfile->n_allocated_type_units
3072 = elements / 3;
8d749320
SM
3073 dwarf2_per_objfile->all_type_units =
3074 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3075
673bfd45 3076 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3077
3078 for (i = 0; i < elements; i += 3)
3079 {
52dc124a 3080 struct signatured_type *sig_type;
9c541725 3081 ULONGEST signature;
1fd400ff 3082 void **slot;
9c541725 3083 cu_offset type_offset_in_tu;
1fd400ff 3084
74a0d9f6 3085 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3086 sect_offset sect_off
3087 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3088 type_offset_in_tu
3089 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3090 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3091 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3092 bytes += 3 * 8;
3093
52dc124a 3094 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3095 struct signatured_type);
52dc124a 3096 sig_type->signature = signature;
9c541725 3097 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3098 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3099 sig_type->per_cu.section = section;
9c541725 3100 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3101 sig_type->per_cu.objfile = objfile;
3102 sig_type->per_cu.v.quick
1fd400ff
TT
3103 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3104 struct dwarf2_per_cu_quick_data);
3105
52dc124a
DE
3106 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3107 *slot = sig_type;
1fd400ff 3108
b4dd5633 3109 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3110 }
3111
673bfd45 3112 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3113}
3114
9291a0cd
TT
3115/* Read the address map data from the mapped index, and use it to
3116 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3117
9291a0cd
TT
3118static void
3119create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3120{
3e29f34a 3121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3122 const gdb_byte *iter, *end;
9291a0cd 3123 struct addrmap *mutable_map;
9291a0cd
TT
3124 CORE_ADDR baseaddr;
3125
8268c778
PA
3126 auto_obstack temp_obstack;
3127
9291a0cd
TT
3128 mutable_map = addrmap_create_mutable (&temp_obstack);
3129
3130 iter = index->address_table;
3131 end = iter + index->address_table_size;
3132
3133 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3134
3135 while (iter < end)
3136 {
3137 ULONGEST hi, lo, cu_index;
3138 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3139 iter += 8;
3140 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3141 iter += 8;
3142 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3143 iter += 4;
f652bce2 3144
24a55014 3145 if (lo > hi)
f652bce2 3146 {
24a55014
DE
3147 complaint (&symfile_complaints,
3148 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3149 hex_string (lo), hex_string (hi));
24a55014 3150 continue;
f652bce2 3151 }
24a55014
DE
3152
3153 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3154 {
3155 complaint (&symfile_complaints,
3156 _(".gdb_index address table has invalid CU number %u"),
3157 (unsigned) cu_index);
24a55014 3158 continue;
f652bce2 3159 }
24a55014 3160
3e29f34a
MR
3161 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3162 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3163 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3164 }
3165
3166 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3167 &objfile->objfile_obstack);
9291a0cd
TT
3168}
3169
59d7bcaf
JK
3170/* The hash function for strings in the mapped index. This is the same as
3171 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3172 implementation. This is necessary because the hash function is tied to the
3173 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3174 SYMBOL_HASH_NEXT.
3175
3176 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3177
9291a0cd 3178static hashval_t
559a7a62 3179mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3180{
3181 const unsigned char *str = (const unsigned char *) p;
3182 hashval_t r = 0;
3183 unsigned char c;
3184
3185 while ((c = *str++) != 0)
559a7a62
JK
3186 {
3187 if (index_version >= 5)
3188 c = tolower (c);
3189 r = r * 67 + c - 113;
3190 }
9291a0cd
TT
3191
3192 return r;
3193}
3194
3195/* Find a slot in the mapped index INDEX for the object named NAME.
3196 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3197 constant pool and return true. If NAME cannot be found, return
3198 false. */
2fdf6df6 3199
109483d9 3200static bool
9291a0cd
TT
3201find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3202 offset_type **vec_out)
3203{
0cf03b49 3204 offset_type hash;
9291a0cd 3205 offset_type slot, step;
559a7a62 3206 int (*cmp) (const char *, const char *);
9291a0cd 3207
791afaa2 3208 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3209 if (current_language->la_language == language_cplus
45280282
IB
3210 || current_language->la_language == language_fortran
3211 || current_language->la_language == language_d)
0cf03b49
JK
3212 {
3213 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3214 not contain any. */
a8719064 3215
72998fb3 3216 if (strchr (name, '(') != NULL)
0cf03b49 3217 {
109483d9 3218 without_params = cp_remove_params (name);
0cf03b49 3219
72998fb3 3220 if (without_params != NULL)
791afaa2 3221 name = without_params.get ();
0cf03b49
JK
3222 }
3223 }
3224
559a7a62 3225 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3226 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3227 simulate our NAME being searched is also lowercased. */
3228 hash = mapped_index_string_hash ((index->version == 4
3229 && case_sensitivity == case_sensitive_off
3230 ? 5 : index->version),
3231 name);
3232
3876f04e
DE
3233 slot = hash & (index->symbol_table_slots - 1);
3234 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3235 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3236
3237 for (;;)
3238 {
3239 /* Convert a slot number to an offset into the table. */
3240 offset_type i = 2 * slot;
3241 const char *str;
3876f04e 3242 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3243 return false;
9291a0cd 3244
3876f04e 3245 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3246 if (!cmp (name, str))
9291a0cd
TT
3247 {
3248 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3249 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3250 return true;
9291a0cd
TT
3251 }
3252
3876f04e 3253 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3254 }
3255}
3256
2ec9a5e0
TT
3257/* A helper function that reads the .gdb_index from SECTION and fills
3258 in MAP. FILENAME is the name of the file containing the section;
3259 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3260 ok to use deprecated sections.
3261
3262 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3263 out parameters that are filled in with information about the CU and
3264 TU lists in the section.
3265
3266 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3267
9291a0cd 3268static int
2ec9a5e0
TT
3269read_index_from_section (struct objfile *objfile,
3270 const char *filename,
3271 int deprecated_ok,
3272 struct dwarf2_section_info *section,
3273 struct mapped_index *map,
3274 const gdb_byte **cu_list,
3275 offset_type *cu_list_elements,
3276 const gdb_byte **types_list,
3277 offset_type *types_list_elements)
9291a0cd 3278{
948f8e3d 3279 const gdb_byte *addr;
2ec9a5e0 3280 offset_type version;
b3b272e1 3281 offset_type *metadata;
1fd400ff 3282 int i;
9291a0cd 3283
2ec9a5e0 3284 if (dwarf2_section_empty_p (section))
9291a0cd 3285 return 0;
82430852
JK
3286
3287 /* Older elfutils strip versions could keep the section in the main
3288 executable while splitting it for the separate debug info file. */
a32a8923 3289 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3290 return 0;
3291
2ec9a5e0 3292 dwarf2_read_section (objfile, section);
9291a0cd 3293
2ec9a5e0 3294 addr = section->buffer;
9291a0cd 3295 /* Version check. */
1fd400ff 3296 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3297 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3298 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3299 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3300 indices. */
831adc1f 3301 if (version < 4)
481860b3
GB
3302 {
3303 static int warning_printed = 0;
3304 if (!warning_printed)
3305 {
3306 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3307 filename);
481860b3
GB
3308 warning_printed = 1;
3309 }
3310 return 0;
3311 }
3312 /* Index version 4 uses a different hash function than index version
3313 5 and later.
3314
3315 Versions earlier than 6 did not emit psymbols for inlined
3316 functions. Using these files will cause GDB not to be able to
3317 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3318 indices unless the user has done
3319 "set use-deprecated-index-sections on". */
2ec9a5e0 3320 if (version < 6 && !deprecated_ok)
481860b3
GB
3321 {
3322 static int warning_printed = 0;
3323 if (!warning_printed)
3324 {
e615022a
DE
3325 warning (_("\
3326Skipping deprecated .gdb_index section in %s.\n\
3327Do \"set use-deprecated-index-sections on\" before the file is read\n\
3328to use the section anyway."),
2ec9a5e0 3329 filename);
481860b3
GB
3330 warning_printed = 1;
3331 }
3332 return 0;
3333 }
796a7ff8 3334 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3335 of the TU (for symbols coming from TUs),
3336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3337 Plus gold-generated indices can have duplicate entries for global symbols,
3338 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3339 These are just performance bugs, and we can't distinguish gdb-generated
3340 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3341
481860b3 3342 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3343 longer backward compatible. */
796a7ff8 3344 if (version > 8)
594e8718 3345 return 0;
9291a0cd 3346
559a7a62 3347 map->version = version;
2ec9a5e0 3348 map->total_size = section->size;
9291a0cd
TT
3349
3350 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3351
3352 i = 0;
2ec9a5e0
TT
3353 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3354 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3355 / 8);
1fd400ff
TT
3356 ++i;
3357
2ec9a5e0
TT
3358 *types_list = addr + MAYBE_SWAP (metadata[i]);
3359 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3360 - MAYBE_SWAP (metadata[i]))
3361 / 8);
987d643c 3362 ++i;
1fd400ff
TT
3363
3364 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3365 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3366 - MAYBE_SWAP (metadata[i]));
3367 ++i;
3368
3876f04e
DE
3369 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3370 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3371 - MAYBE_SWAP (metadata[i]))
3372 / (2 * sizeof (offset_type)));
1fd400ff 3373 ++i;
9291a0cd 3374
f9d83a0b 3375 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3376
2ec9a5e0
TT
3377 return 1;
3378}
3379
3380
3381/* Read the index file. If everything went ok, initialize the "quick"
3382 elements of all the CUs and return 1. Otherwise, return 0. */
3383
3384static int
3385dwarf2_read_index (struct objfile *objfile)
3386{
3387 struct mapped_index local_map, *map;
3388 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3389 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3390 struct dwz_file *dwz;
2ec9a5e0 3391
4262abfb 3392 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3393 use_deprecated_index_sections,
3394 &dwarf2_per_objfile->gdb_index, &local_map,
3395 &cu_list, &cu_list_elements,
3396 &types_list, &types_list_elements))
3397 return 0;
3398
0fefef59 3399 /* Don't use the index if it's empty. */
2ec9a5e0 3400 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3401 return 0;
3402
2ec9a5e0
TT
3403 /* If there is a .dwz file, read it so we can get its CU list as
3404 well. */
4db1a1dc
TT
3405 dwz = dwarf2_get_dwz_file ();
3406 if (dwz != NULL)
2ec9a5e0 3407 {
2ec9a5e0
TT
3408 struct mapped_index dwz_map;
3409 const gdb_byte *dwz_types_ignore;
3410 offset_type dwz_types_elements_ignore;
3411
3412 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3413 1,
3414 &dwz->gdb_index, &dwz_map,
3415 &dwz_list, &dwz_list_elements,
3416 &dwz_types_ignore,
3417 &dwz_types_elements_ignore))
3418 {
3419 warning (_("could not read '.gdb_index' section from %s; skipping"),
3420 bfd_get_filename (dwz->dwz_bfd));
3421 return 0;
3422 }
3423 }
3424
74a0d9f6
JK
3425 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3426 dwz_list_elements);
1fd400ff 3427
8b70b953
TT
3428 if (types_list_elements)
3429 {
3430 struct dwarf2_section_info *section;
3431
3432 /* We can only handle a single .debug_types when we have an
3433 index. */
3434 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3435 return 0;
3436
3437 section = VEC_index (dwarf2_section_info_def,
3438 dwarf2_per_objfile->types, 0);
3439
74a0d9f6
JK
3440 create_signatured_type_table_from_index (objfile, section, types_list,
3441 types_list_elements);
8b70b953 3442 }
9291a0cd 3443
2ec9a5e0
TT
3444 create_addrmap_from_index (objfile, &local_map);
3445
8d749320 3446 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3447 *map = local_map;
9291a0cd
TT
3448
3449 dwarf2_per_objfile->index_table = map;
3450 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3451 dwarf2_per_objfile->quick_file_names_table =
3452 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3453
3454 return 1;
3455}
3456
3457/* A helper for the "quick" functions which sets the global
3458 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3459
9291a0cd
TT
3460static void
3461dw2_setup (struct objfile *objfile)
3462{
9a3c8263
SM
3463 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3464 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3465 gdb_assert (dwarf2_per_objfile);
3466}
3467
dee91e82 3468/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3469
dee91e82
DE
3470static void
3471dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3472 const gdb_byte *info_ptr,
dee91e82
DE
3473 struct die_info *comp_unit_die,
3474 int has_children,
3475 void *data)
9291a0cd 3476{
dee91e82
DE
3477 struct dwarf2_cu *cu = reader->cu;
3478 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3479 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3480 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3481 struct attribute *attr;
dee91e82 3482 int i;
7b9f3c50
DE
3483 void **slot;
3484 struct quick_file_names *qfn;
9291a0cd 3485
0186c6a7
DE
3486 gdb_assert (! this_cu->is_debug_types);
3487
07261596
TT
3488 /* Our callers never want to match partial units -- instead they
3489 will match the enclosing full CU. */
3490 if (comp_unit_die->tag == DW_TAG_partial_unit)
3491 {
3492 this_cu->v.quick->no_file_data = 1;
3493 return;
3494 }
3495
0186c6a7 3496 lh_cu = this_cu;
7b9f3c50 3497 slot = NULL;
dee91e82 3498
fff8551c 3499 line_header_up lh;
9c541725 3500 sect_offset line_offset {};
fff8551c 3501
dee91e82 3502 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3503 if (attr)
3504 {
7b9f3c50
DE
3505 struct quick_file_names find_entry;
3506
9c541725 3507 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3508
3509 /* We may have already read in this line header (TU line header sharing).
3510 If we have we're done. */
094b34ac 3511 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3512 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3513 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3514 &find_entry, INSERT);
3515 if (*slot != NULL)
3516 {
9a3c8263 3517 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3518 return;
7b9f3c50
DE
3519 }
3520
3019eac3 3521 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3522 }
3523 if (lh == NULL)
3524 {
094b34ac 3525 lh_cu->v.quick->no_file_data = 1;
dee91e82 3526 return;
9291a0cd
TT
3527 }
3528
8d749320 3529 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3530 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3531 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3532 gdb_assert (slot != NULL);
3533 *slot = qfn;
9291a0cd 3534
d721ba37 3535 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3536
fff8551c 3537 qfn->num_file_names = lh->file_names.size ();
8d749320 3538 qfn->file_names =
fff8551c
PA
3539 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3540 for (i = 0; i < lh->file_names.size (); ++i)
3541 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3542 qfn->real_names = NULL;
9291a0cd 3543
094b34ac 3544 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3545}
3546
3547/* A helper for the "quick" functions which attempts to read the line
3548 table for THIS_CU. */
3549
3550static struct quick_file_names *
e4a48d9d 3551dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3552{
0186c6a7
DE
3553 /* This should never be called for TUs. */
3554 gdb_assert (! this_cu->is_debug_types);
3555 /* Nor type unit groups. */
3556 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3557
dee91e82
DE
3558 if (this_cu->v.quick->file_names != NULL)
3559 return this_cu->v.quick->file_names;
3560 /* If we know there is no line data, no point in looking again. */
3561 if (this_cu->v.quick->no_file_data)
3562 return NULL;
3563
0186c6a7 3564 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3565
3566 if (this_cu->v.quick->no_file_data)
3567 return NULL;
3568 return this_cu->v.quick->file_names;
9291a0cd
TT
3569}
3570
3571/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3572 real path for a given file name from the line table. */
2fdf6df6 3573
9291a0cd 3574static const char *
7b9f3c50
DE
3575dw2_get_real_path (struct objfile *objfile,
3576 struct quick_file_names *qfn, int index)
9291a0cd 3577{
7b9f3c50
DE
3578 if (qfn->real_names == NULL)
3579 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3580 qfn->num_file_names, const char *);
9291a0cd 3581
7b9f3c50 3582 if (qfn->real_names[index] == NULL)
14278e1f 3583 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3584
7b9f3c50 3585 return qfn->real_names[index];
9291a0cd
TT
3586}
3587
3588static struct symtab *
3589dw2_find_last_source_symtab (struct objfile *objfile)
3590{
43f3e411 3591 struct compunit_symtab *cust;
9291a0cd 3592 int index;
ae2de4f8 3593
9291a0cd
TT
3594 dw2_setup (objfile);
3595 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3596 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3597 if (cust == NULL)
3598 return NULL;
3599 return compunit_primary_filetab (cust);
9291a0cd
TT
3600}
3601
7b9f3c50
DE
3602/* Traversal function for dw2_forget_cached_source_info. */
3603
3604static int
3605dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3606{
7b9f3c50 3607 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3608
7b9f3c50 3609 if (file_data->real_names)
9291a0cd 3610 {
7b9f3c50 3611 int i;
9291a0cd 3612
7b9f3c50 3613 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3614 {
7b9f3c50
DE
3615 xfree ((void*) file_data->real_names[i]);
3616 file_data->real_names[i] = NULL;
9291a0cd
TT
3617 }
3618 }
7b9f3c50
DE
3619
3620 return 1;
3621}
3622
3623static void
3624dw2_forget_cached_source_info (struct objfile *objfile)
3625{
3626 dw2_setup (objfile);
3627
3628 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3629 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3630}
3631
f8eba3c6
TT
3632/* Helper function for dw2_map_symtabs_matching_filename that expands
3633 the symtabs and calls the iterator. */
3634
3635static int
3636dw2_map_expand_apply (struct objfile *objfile,
3637 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3638 const char *name, const char *real_path,
14bc53a8 3639 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3640{
43f3e411 3641 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3642
3643 /* Don't visit already-expanded CUs. */
43f3e411 3644 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3645 return 0;
3646
3647 /* This may expand more than one symtab, and we want to iterate over
3648 all of them. */
a0f42c21 3649 dw2_instantiate_symtab (per_cu);
f8eba3c6 3650
14bc53a8
PA
3651 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3652 last_made, callback);
f8eba3c6
TT
3653}
3654
3655/* Implementation of the map_symtabs_matching_filename method. */
3656
14bc53a8
PA
3657static bool
3658dw2_map_symtabs_matching_filename
3659 (struct objfile *objfile, const char *name, const char *real_path,
3660 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3661{
3662 int i;
c011a4f4 3663 const char *name_basename = lbasename (name);
9291a0cd
TT
3664
3665 dw2_setup (objfile);
ae2de4f8 3666
848e3e78
DE
3667 /* The rule is CUs specify all the files, including those used by
3668 any TU, so there's no need to scan TUs here. */
f4dc4d17 3669
848e3e78 3670 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3671 {
3672 int j;
8832e7e3 3673 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3674 struct quick_file_names *file_data;
9291a0cd 3675
3d7bb9d9 3676 /* We only need to look at symtabs not already expanded. */
43f3e411 3677 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3678 continue;
3679
e4a48d9d 3680 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3681 if (file_data == NULL)
9291a0cd
TT
3682 continue;
3683
7b9f3c50 3684 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3685 {
7b9f3c50 3686 const char *this_name = file_data->file_names[j];
da235a7c 3687 const char *this_real_name;
9291a0cd 3688
af529f8f 3689 if (compare_filenames_for_search (this_name, name))
9291a0cd 3690 {
f5b95b50 3691 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3692 callback))
3693 return true;
288e77a7 3694 continue;
4aac40c8 3695 }
9291a0cd 3696
c011a4f4
DE
3697 /* Before we invoke realpath, which can get expensive when many
3698 files are involved, do a quick comparison of the basenames. */
3699 if (! basenames_may_differ
3700 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3701 continue;
3702
da235a7c
JK
3703 this_real_name = dw2_get_real_path (objfile, file_data, j);
3704 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3705 {
da235a7c 3706 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3707 callback))
3708 return true;
288e77a7 3709 continue;
da235a7c 3710 }
9291a0cd 3711
da235a7c
JK
3712 if (real_path != NULL)
3713 {
af529f8f
JK
3714 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3715 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3716 if (this_real_name != NULL
af529f8f 3717 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3718 {
f5b95b50 3719 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3720 callback))
3721 return true;
288e77a7 3722 continue;
9291a0cd
TT
3723 }
3724 }
3725 }
3726 }
3727
14bc53a8 3728 return false;
9291a0cd
TT
3729}
3730
da51c347
DE
3731/* Struct used to manage iterating over all CUs looking for a symbol. */
3732
3733struct dw2_symtab_iterator
9291a0cd 3734{
da51c347
DE
3735 /* The internalized form of .gdb_index. */
3736 struct mapped_index *index;
3737 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3738 int want_specific_block;
3739 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3740 Unused if !WANT_SPECIFIC_BLOCK. */
3741 int block_index;
3742 /* The kind of symbol we're looking for. */
3743 domain_enum domain;
3744 /* The list of CUs from the index entry of the symbol,
3745 or NULL if not found. */
3746 offset_type *vec;
3747 /* The next element in VEC to look at. */
3748 int next;
3749 /* The number of elements in VEC, or zero if there is no match. */
3750 int length;
8943b874
DE
3751 /* Have we seen a global version of the symbol?
3752 If so we can ignore all further global instances.
3753 This is to work around gold/15646, inefficient gold-generated
3754 indices. */
3755 int global_seen;
da51c347 3756};
9291a0cd 3757
da51c347
DE
3758/* Initialize the index symtab iterator ITER.
3759 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3760 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3761
9291a0cd 3762static void
da51c347
DE
3763dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3764 struct mapped_index *index,
3765 int want_specific_block,
3766 int block_index,
3767 domain_enum domain,
3768 const char *name)
3769{
3770 iter->index = index;
3771 iter->want_specific_block = want_specific_block;
3772 iter->block_index = block_index;
3773 iter->domain = domain;
3774 iter->next = 0;
8943b874 3775 iter->global_seen = 0;
da51c347
DE
3776
3777 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3778 iter->length = MAYBE_SWAP (*iter->vec);
3779 else
3780 {
3781 iter->vec = NULL;
3782 iter->length = 0;
3783 }
3784}
3785
3786/* Return the next matching CU or NULL if there are no more. */
3787
3788static struct dwarf2_per_cu_data *
3789dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3790{
3791 for ( ; iter->next < iter->length; ++iter->next)
3792 {
3793 offset_type cu_index_and_attrs =
3794 MAYBE_SWAP (iter->vec[iter->next + 1]);
3795 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3796 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3797 int want_static = iter->block_index != GLOBAL_BLOCK;
3798 /* This value is only valid for index versions >= 7. */
3799 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3800 gdb_index_symbol_kind symbol_kind =
3801 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3802 /* Only check the symbol attributes if they're present.
3803 Indices prior to version 7 don't record them,
3804 and indices >= 7 may elide them for certain symbols
3805 (gold does this). */
3806 int attrs_valid =
3807 (iter->index->version >= 7
3808 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3809
3190f0c6
DE
3810 /* Don't crash on bad data. */
3811 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3812 + dwarf2_per_objfile->n_type_units))
3813 {
3814 complaint (&symfile_complaints,
3815 _(".gdb_index entry has bad CU index"
4262abfb
JK
3816 " [in module %s]"),
3817 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3818 continue;
3819 }
3820
8832e7e3 3821 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3822
da51c347 3823 /* Skip if already read in. */
43f3e411 3824 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3825 continue;
3826
8943b874
DE
3827 /* Check static vs global. */
3828 if (attrs_valid)
3829 {
3830 if (iter->want_specific_block
3831 && want_static != is_static)
3832 continue;
3833 /* Work around gold/15646. */
3834 if (!is_static && iter->global_seen)
3835 continue;
3836 if (!is_static)
3837 iter->global_seen = 1;
3838 }
da51c347
DE
3839
3840 /* Only check the symbol's kind if it has one. */
3841 if (attrs_valid)
3842 {
3843 switch (iter->domain)
3844 {
3845 case VAR_DOMAIN:
3846 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3847 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3848 /* Some types are also in VAR_DOMAIN. */
3849 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3850 continue;
3851 break;
3852 case STRUCT_DOMAIN:
3853 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3854 continue;
3855 break;
3856 case LABEL_DOMAIN:
3857 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3858 continue;
3859 break;
3860 default:
3861 break;
3862 }
3863 }
3864
3865 ++iter->next;
3866 return per_cu;
3867 }
3868
3869 return NULL;
3870}
3871
43f3e411 3872static struct compunit_symtab *
da51c347
DE
3873dw2_lookup_symbol (struct objfile *objfile, int block_index,
3874 const char *name, domain_enum domain)
9291a0cd 3875{
43f3e411 3876 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3877 struct mapped_index *index;
3878
9291a0cd
TT
3879 dw2_setup (objfile);
3880
156942c7
DE
3881 index = dwarf2_per_objfile->index_table;
3882
da51c347 3883 /* index is NULL if OBJF_READNOW. */
156942c7 3884 if (index)
9291a0cd 3885 {
da51c347
DE
3886 struct dw2_symtab_iterator iter;
3887 struct dwarf2_per_cu_data *per_cu;
3888
3889 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3890
da51c347 3891 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3892 {
b2e2f908 3893 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3894 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3895 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3896 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3897
b2e2f908
DE
3898 sym = block_find_symbol (block, name, domain,
3899 block_find_non_opaque_type_preferred,
3900 &with_opaque);
3901
da51c347
DE
3902 /* Some caution must be observed with overloaded functions
3903 and methods, since the index will not contain any overload
3904 information (but NAME might contain it). */
da51c347 3905
b2e2f908 3906 if (sym != NULL
a778f165 3907 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
3908 return stab;
3909 if (with_opaque != NULL
a778f165 3910 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 3911 stab_best = stab;
da51c347
DE
3912
3913 /* Keep looking through other CUs. */
9291a0cd
TT
3914 }
3915 }
9291a0cd 3916
da51c347 3917 return stab_best;
9291a0cd
TT
3918}
3919
3920static void
3921dw2_print_stats (struct objfile *objfile)
3922{
e4a48d9d 3923 int i, total, count;
9291a0cd
TT
3924
3925 dw2_setup (objfile);
e4a48d9d 3926 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3927 count = 0;
e4a48d9d 3928 for (i = 0; i < total; ++i)
9291a0cd 3929 {
8832e7e3 3930 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3931
43f3e411 3932 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3933 ++count;
3934 }
e4a48d9d 3935 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3936 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3937}
3938
779bd270
DE
3939/* This dumps minimal information about the index.
3940 It is called via "mt print objfiles".
3941 One use is to verify .gdb_index has been loaded by the
3942 gdb.dwarf2/gdb-index.exp testcase. */
3943
9291a0cd
TT
3944static void
3945dw2_dump (struct objfile *objfile)
3946{
779bd270
DE
3947 dw2_setup (objfile);
3948 gdb_assert (dwarf2_per_objfile->using_index);
3949 printf_filtered (".gdb_index:");
3950 if (dwarf2_per_objfile->index_table != NULL)
3951 {
3952 printf_filtered (" version %d\n",
3953 dwarf2_per_objfile->index_table->version);
3954 }
3955 else
3956 printf_filtered (" faked for \"readnow\"\n");
3957 printf_filtered ("\n");
9291a0cd
TT
3958}
3959
3960static void
3189cb12
DE
3961dw2_relocate (struct objfile *objfile,
3962 const struct section_offsets *new_offsets,
3963 const struct section_offsets *delta)
9291a0cd
TT
3964{
3965 /* There's nothing to relocate here. */
3966}
3967
3968static void
3969dw2_expand_symtabs_for_function (struct objfile *objfile,
3970 const char *func_name)
3971{
da51c347
DE
3972 struct mapped_index *index;
3973
3974 dw2_setup (objfile);
3975
3976 index = dwarf2_per_objfile->index_table;
3977
3978 /* index is NULL if OBJF_READNOW. */
3979 if (index)
3980 {
3981 struct dw2_symtab_iterator iter;
3982 struct dwarf2_per_cu_data *per_cu;
3983
3984 /* Note: It doesn't matter what we pass for block_index here. */
3985 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3986 func_name);
3987
3988 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3989 dw2_instantiate_symtab (per_cu);
3990 }
9291a0cd
TT
3991}
3992
3993static void
3994dw2_expand_all_symtabs (struct objfile *objfile)
3995{
3996 int i;
3997
3998 dw2_setup (objfile);
1fd400ff
TT
3999
4000 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4001 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4002 {
8832e7e3 4003 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4004
a0f42c21 4005 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4006 }
4007}
4008
4009static void
652a8996
JK
4010dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4011 const char *fullname)
9291a0cd
TT
4012{
4013 int i;
4014
4015 dw2_setup (objfile);
d4637a04
DE
4016
4017 /* We don't need to consider type units here.
4018 This is only called for examining code, e.g. expand_line_sal.
4019 There can be an order of magnitude (or more) more type units
4020 than comp units, and we avoid them if we can. */
4021
4022 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4023 {
4024 int j;
8832e7e3 4025 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4026 struct quick_file_names *file_data;
9291a0cd 4027
3d7bb9d9 4028 /* We only need to look at symtabs not already expanded. */
43f3e411 4029 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4030 continue;
4031
e4a48d9d 4032 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4033 if (file_data == NULL)
9291a0cd
TT
4034 continue;
4035
7b9f3c50 4036 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4037 {
652a8996
JK
4038 const char *this_fullname = file_data->file_names[j];
4039
4040 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4041 {
a0f42c21 4042 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4043 break;
4044 }
4045 }
4046 }
4047}
4048
9291a0cd 4049static void
ade7ed9e 4050dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4051 const char * name, domain_enum domain,
ade7ed9e 4052 int global,
40658b94
PH
4053 int (*callback) (struct block *,
4054 struct symbol *, void *),
2edb89d3
JK
4055 void *data, symbol_compare_ftype *match,
4056 symbol_compare_ftype *ordered_compare)
9291a0cd 4057{
40658b94 4058 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4059 current language is Ada for a non-Ada objfile using GNU index. As Ada
4060 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4061}
4062
4063static void
f8eba3c6
TT
4064dw2_expand_symtabs_matching
4065 (struct objfile *objfile,
14bc53a8
PA
4066 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4067 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4068 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4069 enum search_domain kind)
9291a0cd
TT
4070{
4071 int i;
4072 offset_type iter;
4b5246aa 4073 struct mapped_index *index;
9291a0cd
TT
4074
4075 dw2_setup (objfile);
ae2de4f8
DE
4076
4077 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4078 if (!dwarf2_per_objfile->index_table)
4079 return;
4b5246aa 4080 index = dwarf2_per_objfile->index_table;
9291a0cd 4081
7b08b9eb 4082 if (file_matcher != NULL)
24c79950 4083 {
fc4007c9
TT
4084 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4085 htab_eq_pointer,
4086 NULL, xcalloc, xfree));
4087 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4088 htab_eq_pointer,
4089 NULL, xcalloc, xfree));
24c79950 4090
848e3e78
DE
4091 /* The rule is CUs specify all the files, including those used by
4092 any TU, so there's no need to scan TUs here. */
4093
4094 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4095 {
4096 int j;
8832e7e3 4097 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4098 struct quick_file_names *file_data;
4099 void **slot;
7b08b9eb 4100
61d96d7e
DE
4101 QUIT;
4102
24c79950 4103 per_cu->v.quick->mark = 0;
3d7bb9d9 4104
24c79950 4105 /* We only need to look at symtabs not already expanded. */
43f3e411 4106 if (per_cu->v.quick->compunit_symtab)
24c79950 4107 continue;
7b08b9eb 4108
e4a48d9d 4109 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4110 if (file_data == NULL)
4111 continue;
7b08b9eb 4112
fc4007c9 4113 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4114 continue;
fc4007c9 4115 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4116 {
4117 per_cu->v.quick->mark = 1;
4118 continue;
4119 }
4120
4121 for (j = 0; j < file_data->num_file_names; ++j)
4122 {
da235a7c
JK
4123 const char *this_real_name;
4124
14bc53a8 4125 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4126 {
4127 per_cu->v.quick->mark = 1;
4128 break;
4129 }
da235a7c
JK
4130
4131 /* Before we invoke realpath, which can get expensive when many
4132 files are involved, do a quick comparison of the basenames. */
4133 if (!basenames_may_differ
4134 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4135 true))
da235a7c
JK
4136 continue;
4137
4138 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4139 if (file_matcher (this_real_name, false))
da235a7c
JK
4140 {
4141 per_cu->v.quick->mark = 1;
4142 break;
4143 }
24c79950
TT
4144 }
4145
4146 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4147 ? visited_found.get ()
4148 : visited_not_found.get (),
24c79950
TT
4149 file_data, INSERT);
4150 *slot = file_data;
4151 }
24c79950 4152 }
9291a0cd 4153
3876f04e 4154 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4155 {
4156 offset_type idx = 2 * iter;
4157 const char *name;
4158 offset_type *vec, vec_len, vec_idx;
8943b874 4159 int global_seen = 0;
9291a0cd 4160
61d96d7e
DE
4161 QUIT;
4162
3876f04e 4163 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4164 continue;
4165
3876f04e 4166 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4167
14bc53a8 4168 if (!symbol_matcher (name))
9291a0cd
TT
4169 continue;
4170
4171 /* The name was matched, now expand corresponding CUs that were
4172 marked. */
4b5246aa 4173 vec = (offset_type *) (index->constant_pool
3876f04e 4174 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4175 vec_len = MAYBE_SWAP (vec[0]);
4176 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4177 {
e254ef6a 4178 struct dwarf2_per_cu_data *per_cu;
156942c7 4179 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4180 /* This value is only valid for index versions >= 7. */
4181 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4182 gdb_index_symbol_kind symbol_kind =
4183 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4184 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4185 /* Only check the symbol attributes if they're present.
4186 Indices prior to version 7 don't record them,
4187 and indices >= 7 may elide them for certain symbols
4188 (gold does this). */
4189 int attrs_valid =
4190 (index->version >= 7
4191 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4192
8943b874
DE
4193 /* Work around gold/15646. */
4194 if (attrs_valid)
4195 {
4196 if (!is_static && global_seen)
4197 continue;
4198 if (!is_static)
4199 global_seen = 1;
4200 }
4201
3190f0c6
DE
4202 /* Only check the symbol's kind if it has one. */
4203 if (attrs_valid)
156942c7
DE
4204 {
4205 switch (kind)
4206 {
4207 case VARIABLES_DOMAIN:
4208 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4209 continue;
4210 break;
4211 case FUNCTIONS_DOMAIN:
4212 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4213 continue;
4214 break;
4215 case TYPES_DOMAIN:
4216 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4217 continue;
4218 break;
4219 default:
4220 break;
4221 }
4222 }
4223
3190f0c6
DE
4224 /* Don't crash on bad data. */
4225 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4226 + dwarf2_per_objfile->n_type_units))
4227 {
4228 complaint (&symfile_complaints,
4229 _(".gdb_index entry has bad CU index"
4262abfb 4230 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4231 continue;
4232 }
4233
8832e7e3 4234 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4235 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4236 {
4237 int symtab_was_null =
4238 (per_cu->v.quick->compunit_symtab == NULL);
4239
4240 dw2_instantiate_symtab (per_cu);
4241
4242 if (expansion_notify != NULL
4243 && symtab_was_null
4244 && per_cu->v.quick->compunit_symtab != NULL)
4245 {
14bc53a8 4246 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4247 }
4248 }
9291a0cd
TT
4249 }
4250 }
4251}
4252
43f3e411 4253/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4254 symtab. */
4255
43f3e411
DE
4256static struct compunit_symtab *
4257recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4258 CORE_ADDR pc)
9703b513
TT
4259{
4260 int i;
4261
43f3e411
DE
4262 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4263 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4264 return cust;
9703b513 4265
43f3e411 4266 if (cust->includes == NULL)
a3ec0bb1
DE
4267 return NULL;
4268
43f3e411 4269 for (i = 0; cust->includes[i]; ++i)
9703b513 4270 {
43f3e411 4271 struct compunit_symtab *s = cust->includes[i];
9703b513 4272
43f3e411 4273 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4274 if (s != NULL)
4275 return s;
4276 }
4277
4278 return NULL;
4279}
4280
43f3e411
DE
4281static struct compunit_symtab *
4282dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4283 struct bound_minimal_symbol msymbol,
4284 CORE_ADDR pc,
4285 struct obj_section *section,
4286 int warn_if_readin)
9291a0cd
TT
4287{
4288 struct dwarf2_per_cu_data *data;
43f3e411 4289 struct compunit_symtab *result;
9291a0cd
TT
4290
4291 dw2_setup (objfile);
4292
4293 if (!objfile->psymtabs_addrmap)
4294 return NULL;
4295
9a3c8263
SM
4296 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4297 pc);
9291a0cd
TT
4298 if (!data)
4299 return NULL;
4300
43f3e411 4301 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4302 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4303 paddress (get_objfile_arch (objfile), pc));
4304
43f3e411
DE
4305 result
4306 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4307 pc);
9703b513
TT
4308 gdb_assert (result != NULL);
4309 return result;
9291a0cd
TT
4310}
4311
9291a0cd 4312static void
44b13c5a 4313dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4314 void *data, int need_fullname)
9291a0cd 4315{
9291a0cd 4316 dw2_setup (objfile);
ae2de4f8 4317
bbf2f4df 4318 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4319 {
bbf2f4df 4320 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4321
bbf2f4df
PA
4322 htab_up visited (htab_create_alloc (10,
4323 htab_hash_pointer, htab_eq_pointer,
4324 NULL, xcalloc, xfree));
24c79950 4325
bbf2f4df
PA
4326 /* The rule is CUs specify all the files, including those used
4327 by any TU, so there's no need to scan TUs here. We can
4328 ignore file names coming from already-expanded CUs. */
24c79950 4329
bbf2f4df
PA
4330 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4331 {
4332 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4333
bbf2f4df
PA
4334 if (per_cu->v.quick->compunit_symtab)
4335 {
4336 void **slot = htab_find_slot (visited.get (),
4337 per_cu->v.quick->file_names,
4338 INSERT);
9291a0cd 4339
bbf2f4df
PA
4340 *slot = per_cu->v.quick->file_names;
4341 }
24c79950 4342 }
24c79950 4343
bbf2f4df 4344 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4345 {
bbf2f4df
PA
4346 int j;
4347 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4348 struct quick_file_names *file_data;
4349 void **slot;
4350
4351 /* We only need to look at symtabs not already expanded. */
4352 if (per_cu->v.quick->compunit_symtab)
4353 continue;
74e2f255 4354
bbf2f4df
PA
4355 file_data = dw2_get_file_names (per_cu);
4356 if (file_data == NULL)
4357 continue;
4358
4359 slot = htab_find_slot (visited.get (), file_data, INSERT);
4360 if (*slot)
4361 {
4362 /* Already visited. */
4363 continue;
4364 }
4365 *slot = file_data;
4366
4367 for (int j = 0; j < file_data->num_file_names; ++j)
4368 {
4369 const char *filename = file_data->file_names[j];
4370 dwarf2_per_objfile->filenames_cache->seen (filename);
4371 }
9291a0cd
TT
4372 }
4373 }
bbf2f4df
PA
4374
4375 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4376 {
14278e1f 4377 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4378
4379 if (need_fullname)
4380 this_real_name = gdb_realpath (filename);
14278e1f 4381 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4382 });
9291a0cd
TT
4383}
4384
4385static int
4386dw2_has_symbols (struct objfile *objfile)
4387{
4388 return 1;
4389}
4390
4391const struct quick_symbol_functions dwarf2_gdb_index_functions =
4392{
4393 dw2_has_symbols,
4394 dw2_find_last_source_symtab,
4395 dw2_forget_cached_source_info,
f8eba3c6 4396 dw2_map_symtabs_matching_filename,
9291a0cd 4397 dw2_lookup_symbol,
9291a0cd
TT
4398 dw2_print_stats,
4399 dw2_dump,
4400 dw2_relocate,
4401 dw2_expand_symtabs_for_function,
4402 dw2_expand_all_symtabs,
652a8996 4403 dw2_expand_symtabs_with_fullname,
40658b94 4404 dw2_map_matching_symbols,
9291a0cd 4405 dw2_expand_symtabs_matching,
43f3e411 4406 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4407 dw2_map_symbol_filenames
4408};
4409
4410/* Initialize for reading DWARF for this objfile. Return 0 if this
4411 file will use psymtabs, or 1 if using the GNU index. */
4412
4413int
4414dwarf2_initialize_objfile (struct objfile *objfile)
4415{
4416 /* If we're about to read full symbols, don't bother with the
4417 indices. In this case we also don't care if some other debug
4418 format is making psymtabs, because they are all about to be
4419 expanded anyway. */
4420 if ((objfile->flags & OBJF_READNOW))
4421 {
4422 int i;
4423
4424 dwarf2_per_objfile->using_index = 1;
4425 create_all_comp_units (objfile);
0e50663e 4426 create_all_type_units (objfile);
7b9f3c50
DE
4427 dwarf2_per_objfile->quick_file_names_table =
4428 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4429
1fd400ff 4430 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4431 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4432 {
8832e7e3 4433 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4434
e254ef6a
DE
4435 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4436 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4437 }
4438
4439 /* Return 1 so that gdb sees the "quick" functions. However,
4440 these functions will be no-ops because we will have expanded
4441 all symtabs. */
4442 return 1;
4443 }
4444
4445 if (dwarf2_read_index (objfile))
4446 return 1;
4447
9291a0cd
TT
4448 return 0;
4449}
4450
4451\f
4452
dce234bc
PP
4453/* Build a partial symbol table. */
4454
4455void
f29dff0a 4456dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4457{
c9bf0622 4458
af5bf4ad
SM
4459 if (objfile->global_psymbols.capacity () == 0
4460 && objfile->static_psymbols.capacity () == 0)
4461 init_psymbol_list (objfile, 1024);
c906108c 4462
492d29ea 4463 TRY
c9bf0622
TT
4464 {
4465 /* This isn't really ideal: all the data we allocate on the
4466 objfile's obstack is still uselessly kept around. However,
4467 freeing it seems unsafe. */
906768f9 4468 psymtab_discarder psymtabs (objfile);
c9bf0622 4469 dwarf2_build_psymtabs_hard (objfile);
906768f9 4470 psymtabs.keep ();
c9bf0622 4471 }
492d29ea
PA
4472 CATCH (except, RETURN_MASK_ERROR)
4473 {
4474 exception_print (gdb_stderr, except);
4475 }
4476 END_CATCH
c906108c 4477}
c906108c 4478
1ce1cefd
DE
4479/* Return the total length of the CU described by HEADER. */
4480
4481static unsigned int
4482get_cu_length (const struct comp_unit_head *header)
4483{
4484 return header->initial_length_size + header->length;
4485}
4486
9c541725 4487/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4488
9c541725
PA
4489static inline bool
4490offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4491{
9c541725
PA
4492 sect_offset bottom = cu_header->sect_off;
4493 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4494
9c541725 4495 return sect_off >= bottom && sect_off < top;
45452591
DE
4496}
4497
3b80fe9b
DE
4498/* Find the base address of the compilation unit for range lists and
4499 location lists. It will normally be specified by DW_AT_low_pc.
4500 In DWARF-3 draft 4, the base address could be overridden by
4501 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4502 compilation units with discontinuous ranges. */
4503
4504static void
4505dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4506{
4507 struct attribute *attr;
4508
4509 cu->base_known = 0;
4510 cu->base_address = 0;
4511
4512 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4513 if (attr)
4514 {
31aa7e4e 4515 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4516 cu->base_known = 1;
4517 }
4518 else
4519 {
4520 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4521 if (attr)
4522 {
31aa7e4e 4523 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4524 cu->base_known = 1;
4525 }
4526 }
4527}
4528
93311388 4529/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4530 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4531 NOTE: This leaves members offset, first_die_offset to be filled in
4532 by the caller. */
107d2387 4533
d521ce57 4534static const gdb_byte *
107d2387 4535read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4536 const gdb_byte *info_ptr,
4537 struct dwarf2_section_info *section,
4538 rcuh_kind section_kind)
107d2387
AC
4539{
4540 int signed_addr;
891d2f0b 4541 unsigned int bytes_read;
43988095
JK
4542 const char *filename = get_section_file_name (section);
4543 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4544
4545 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4546 cu_header->initial_length_size = bytes_read;
4547 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4548 info_ptr += bytes_read;
107d2387
AC
4549 cu_header->version = read_2_bytes (abfd, info_ptr);
4550 info_ptr += 2;
43988095
JK
4551 if (cu_header->version < 5)
4552 switch (section_kind)
4553 {
4554 case rcuh_kind::COMPILE:
4555 cu_header->unit_type = DW_UT_compile;
4556 break;
4557 case rcuh_kind::TYPE:
4558 cu_header->unit_type = DW_UT_type;
4559 break;
4560 default:
4561 internal_error (__FILE__, __LINE__,
4562 _("read_comp_unit_head: invalid section_kind"));
4563 }
4564 else
4565 {
4566 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4567 (read_1_byte (abfd, info_ptr));
4568 info_ptr += 1;
4569 switch (cu_header->unit_type)
4570 {
4571 case DW_UT_compile:
4572 if (section_kind != rcuh_kind::COMPILE)
4573 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4574 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4575 filename);
4576 break;
4577 case DW_UT_type:
4578 section_kind = rcuh_kind::TYPE;
4579 break;
4580 default:
4581 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4582 "(is %d, should be %d or %d) [in module %s]"),
4583 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4584 }
4585
4586 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4587 info_ptr += 1;
4588 }
9c541725
PA
4589 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4590 cu_header,
4591 &bytes_read);
613e1657 4592 info_ptr += bytes_read;
43988095
JK
4593 if (cu_header->version < 5)
4594 {
4595 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4596 info_ptr += 1;
4597 }
107d2387
AC
4598 signed_addr = bfd_get_sign_extend_vma (abfd);
4599 if (signed_addr < 0)
8e65ff28 4600 internal_error (__FILE__, __LINE__,
e2e0b3e5 4601 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4602 cu_header->signed_addr_p = signed_addr;
c764a876 4603
43988095
JK
4604 if (section_kind == rcuh_kind::TYPE)
4605 {
4606 LONGEST type_offset;
4607
4608 cu_header->signature = read_8_bytes (abfd, info_ptr);
4609 info_ptr += 8;
4610
4611 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4612 info_ptr += bytes_read;
9c541725
PA
4613 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4614 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4615 error (_("Dwarf Error: Too big type_offset in compilation unit "
4616 "header (is %s) [in module %s]"), plongest (type_offset),
4617 filename);
4618 }
4619
107d2387
AC
4620 return info_ptr;
4621}
4622
36586728
TT
4623/* Helper function that returns the proper abbrev section for
4624 THIS_CU. */
4625
4626static struct dwarf2_section_info *
4627get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4628{
4629 struct dwarf2_section_info *abbrev;
4630
4631 if (this_cu->is_dwz)
4632 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4633 else
4634 abbrev = &dwarf2_per_objfile->abbrev;
4635
4636 return abbrev;
4637}
4638
9ff913ba
DE
4639/* Subroutine of read_and_check_comp_unit_head and
4640 read_and_check_type_unit_head to simplify them.
4641 Perform various error checking on the header. */
4642
4643static void
4644error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4645 struct dwarf2_section_info *section,
4646 struct dwarf2_section_info *abbrev_section)
9ff913ba 4647{
a32a8923 4648 const char *filename = get_section_file_name (section);
9ff913ba 4649
43988095 4650 if (header->version < 2 || header->version > 5)
9ff913ba 4651 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4652 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4653 filename);
4654
9c541725 4655 if (to_underlying (header->abbrev_sect_off)
36586728 4656 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4657 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4658 "(offset 0x%x + 6) [in module %s]"),
4659 to_underlying (header->abbrev_sect_off),
4660 to_underlying (header->sect_off),
9ff913ba
DE
4661 filename);
4662
9c541725 4663 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4664 avoid potential 32-bit overflow. */
9c541725 4665 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4666 > section->size)
9c541725
PA
4667 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4668 "(offset 0x%x + 0) [in module %s]"),
4669 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4670 filename);
4671}
4672
4673/* Read in a CU/TU header and perform some basic error checking.
4674 The contents of the header are stored in HEADER.
4675 The result is a pointer to the start of the first DIE. */
adabb602 4676
d521ce57 4677static const gdb_byte *
9ff913ba
DE
4678read_and_check_comp_unit_head (struct comp_unit_head *header,
4679 struct dwarf2_section_info *section,
4bdcc0c1 4680 struct dwarf2_section_info *abbrev_section,
d521ce57 4681 const gdb_byte *info_ptr,
43988095 4682 rcuh_kind section_kind)
72bf9492 4683{
d521ce57 4684 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4685 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4686
9c541725 4687 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4688
43988095 4689 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4690
9c541725 4691 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4692
4bdcc0c1 4693 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4694
4695 return info_ptr;
348e048f
DE
4696}
4697
f4dc4d17
DE
4698/* Fetch the abbreviation table offset from a comp or type unit header. */
4699
4700static sect_offset
4701read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4702 sect_offset sect_off)
f4dc4d17 4703{
a32a8923 4704 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4705 const gdb_byte *info_ptr;
ac298888 4706 unsigned int initial_length_size, offset_size;
43988095 4707 uint16_t version;
f4dc4d17
DE
4708
4709 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4710 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4711 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4712 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4713 info_ptr += initial_length_size;
4714
4715 version = read_2_bytes (abfd, info_ptr);
4716 info_ptr += 2;
4717 if (version >= 5)
4718 {
4719 /* Skip unit type and address size. */
4720 info_ptr += 2;
4721 }
4722
9c541725 4723 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4724}
4725
aaa75496
JB
4726/* Allocate a new partial symtab for file named NAME and mark this new
4727 partial symtab as being an include of PST. */
4728
4729static void
d521ce57 4730dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4731 struct objfile *objfile)
4732{
4733 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4734
fbd9ab74
JK
4735 if (!IS_ABSOLUTE_PATH (subpst->filename))
4736 {
4737 /* It shares objfile->objfile_obstack. */
4738 subpst->dirname = pst->dirname;
4739 }
4740
aaa75496
JB
4741 subpst->textlow = 0;
4742 subpst->texthigh = 0;
4743
8d749320
SM
4744 subpst->dependencies
4745 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4746 subpst->dependencies[0] = pst;
4747 subpst->number_of_dependencies = 1;
4748
4749 subpst->globals_offset = 0;
4750 subpst->n_global_syms = 0;
4751 subpst->statics_offset = 0;
4752 subpst->n_static_syms = 0;
43f3e411 4753 subpst->compunit_symtab = NULL;
aaa75496
JB
4754 subpst->read_symtab = pst->read_symtab;
4755 subpst->readin = 0;
4756
4757 /* No private part is necessary for include psymtabs. This property
4758 can be used to differentiate between such include psymtabs and
10b3939b 4759 the regular ones. */
58a9656e 4760 subpst->read_symtab_private = NULL;
aaa75496
JB
4761}
4762
4763/* Read the Line Number Program data and extract the list of files
4764 included by the source file represented by PST. Build an include
d85a05f0 4765 partial symtab for each of these included files. */
aaa75496
JB
4766
4767static void
4768dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4769 struct die_info *die,
4770 struct partial_symtab *pst)
aaa75496 4771{
fff8551c 4772 line_header_up lh;
d85a05f0 4773 struct attribute *attr;
aaa75496 4774
d85a05f0
DJ
4775 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4776 if (attr)
9c541725 4777 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4778 if (lh == NULL)
4779 return; /* No linetable, so no includes. */
4780
c6da4cef 4781 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4782 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4783}
4784
348e048f 4785static hashval_t
52dc124a 4786hash_signatured_type (const void *item)
348e048f 4787{
9a3c8263
SM
4788 const struct signatured_type *sig_type
4789 = (const struct signatured_type *) item;
9a619af0 4790
348e048f 4791 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4792 return sig_type->signature;
348e048f
DE
4793}
4794
4795static int
52dc124a 4796eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4797{
9a3c8263
SM
4798 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4799 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4800
348e048f
DE
4801 return lhs->signature == rhs->signature;
4802}
4803
1fd400ff
TT
4804/* Allocate a hash table for signatured types. */
4805
4806static htab_t
673bfd45 4807allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4808{
4809 return htab_create_alloc_ex (41,
52dc124a
DE
4810 hash_signatured_type,
4811 eq_signatured_type,
1fd400ff
TT
4812 NULL,
4813 &objfile->objfile_obstack,
4814 hashtab_obstack_allocate,
4815 dummy_obstack_deallocate);
4816}
4817
d467dd73 4818/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4819
4820static int
d467dd73 4821add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4822{
9a3c8263
SM
4823 struct signatured_type *sigt = (struct signatured_type *) *slot;
4824 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4825
b4dd5633 4826 **datap = sigt;
1fd400ff
TT
4827 ++*datap;
4828
4829 return 1;
4830}
4831
78d4d2c5 4832/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4833 and fill them into TYPES_HTAB. It will process only type units,
4834 therefore DW_UT_type. */
c88ee1f0 4835
78d4d2c5
JK
4836static void
4837create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4838 dwarf2_section_info *section, htab_t &types_htab,
4839 rcuh_kind section_kind)
348e048f 4840{
3019eac3 4841 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4842 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4843 bfd *abfd;
4844 const gdb_byte *info_ptr, *end_ptr;
348e048f 4845
4bdcc0c1
DE
4846 abbrev_section = (dwo_file != NULL
4847 ? &dwo_file->sections.abbrev
4848 : &dwarf2_per_objfile->abbrev);
4849
b4f54984 4850 if (dwarf_read_debug)
43988095
JK
4851 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4852 get_section_name (section),
a32a8923 4853 get_section_file_name (abbrev_section));
09406207 4854
78d4d2c5
JK
4855 dwarf2_read_section (objfile, section);
4856 info_ptr = section->buffer;
348e048f 4857
78d4d2c5
JK
4858 if (info_ptr == NULL)
4859 return;
348e048f 4860
78d4d2c5
JK
4861 /* We can't set abfd until now because the section may be empty or
4862 not present, in which case the bfd is unknown. */
4863 abfd = get_section_bfd_owner (section);
348e048f 4864
78d4d2c5
JK
4865 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4866 because we don't need to read any dies: the signature is in the
4867 header. */
3019eac3 4868
78d4d2c5
JK
4869 end_ptr = info_ptr + section->size;
4870 while (info_ptr < end_ptr)
4871 {
78d4d2c5
JK
4872 struct signatured_type *sig_type;
4873 struct dwo_unit *dwo_tu;
4874 void **slot;
4875 const gdb_byte *ptr = info_ptr;
4876 struct comp_unit_head header;
4877 unsigned int length;
8b70b953 4878
9c541725 4879 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4880
a49dd8dd
JK
4881 /* Initialize it due to a false compiler warning. */
4882 header.signature = -1;
9c541725 4883 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4884
78d4d2c5
JK
4885 /* We need to read the type's signature in order to build the hash
4886 table, but we don't need anything else just yet. */
348e048f 4887
43988095
JK
4888 ptr = read_and_check_comp_unit_head (&header, section,
4889 abbrev_section, ptr, section_kind);
348e048f 4890
78d4d2c5 4891 length = get_cu_length (&header);
6caca83c 4892
78d4d2c5
JK
4893 /* Skip dummy type units. */
4894 if (ptr >= info_ptr + length
43988095
JK
4895 || peek_abbrev_code (abfd, ptr) == 0
4896 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4897 {
4898 info_ptr += length;
4899 continue;
4900 }
dee91e82 4901
78d4d2c5
JK
4902 if (types_htab == NULL)
4903 {
4904 if (dwo_file)
4905 types_htab = allocate_dwo_unit_table (objfile);
4906 else
4907 types_htab = allocate_signatured_type_table (objfile);
4908 }
8b70b953 4909
78d4d2c5
JK
4910 if (dwo_file)
4911 {
4912 sig_type = NULL;
4913 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4914 struct dwo_unit);
4915 dwo_tu->dwo_file = dwo_file;
43988095 4916 dwo_tu->signature = header.signature;
9c541725 4917 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4918 dwo_tu->section = section;
9c541725 4919 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4920 dwo_tu->length = length;
4921 }
4922 else
4923 {
4924 /* N.B.: type_offset is not usable if this type uses a DWO file.
4925 The real type_offset is in the DWO file. */
4926 dwo_tu = NULL;
4927 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4928 struct signatured_type);
43988095 4929 sig_type->signature = header.signature;
9c541725 4930 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4931 sig_type->per_cu.objfile = objfile;
4932 sig_type->per_cu.is_debug_types = 1;
4933 sig_type->per_cu.section = section;
9c541725 4934 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4935 sig_type->per_cu.length = length;
4936 }
4937
4938 slot = htab_find_slot (types_htab,
4939 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4940 INSERT);
4941 gdb_assert (slot != NULL);
4942 if (*slot != NULL)
4943 {
9c541725 4944 sect_offset dup_sect_off;
0349ea22 4945
3019eac3
DE
4946 if (dwo_file)
4947 {
78d4d2c5
JK
4948 const struct dwo_unit *dup_tu
4949 = (const struct dwo_unit *) *slot;
4950
9c541725 4951 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4952 }
4953 else
4954 {
78d4d2c5
JK
4955 const struct signatured_type *dup_tu
4956 = (const struct signatured_type *) *slot;
4957
9c541725 4958 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4959 }
8b70b953 4960
78d4d2c5
JK
4961 complaint (&symfile_complaints,
4962 _("debug type entry at offset 0x%x is duplicate to"
4963 " the entry at offset 0x%x, signature %s"),
9c541725 4964 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4965 hex_string (header.signature));
78d4d2c5
JK
4966 }
4967 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4968
78d4d2c5
JK
4969 if (dwarf_read_debug > 1)
4970 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4971 to_underlying (sect_off),
43988095 4972 hex_string (header.signature));
3019eac3 4973
78d4d2c5
JK
4974 info_ptr += length;
4975 }
4976}
3019eac3 4977
78d4d2c5
JK
4978/* Create the hash table of all entries in the .debug_types
4979 (or .debug_types.dwo) section(s).
4980 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4981 otherwise it is NULL.
b3c8eb43 4982
78d4d2c5 4983 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4984
78d4d2c5 4985 Note: This function processes DWO files only, not DWP files. */
348e048f 4986
78d4d2c5
JK
4987static void
4988create_debug_types_hash_table (struct dwo_file *dwo_file,
4989 VEC (dwarf2_section_info_def) *types,
4990 htab_t &types_htab)
4991{
4992 int ix;
4993 struct dwarf2_section_info *section;
4994
4995 if (VEC_empty (dwarf2_section_info_def, types))
4996 return;
348e048f 4997
78d4d2c5
JK
4998 for (ix = 0;
4999 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5000 ++ix)
43988095
JK
5001 create_debug_type_hash_table (dwo_file, section, types_htab,
5002 rcuh_kind::TYPE);
3019eac3
DE
5003}
5004
5005/* Create the hash table of all entries in the .debug_types section,
5006 and initialize all_type_units.
5007 The result is zero if there is an error (e.g. missing .debug_types section),
5008 otherwise non-zero. */
5009
5010static int
5011create_all_type_units (struct objfile *objfile)
5012{
78d4d2c5 5013 htab_t types_htab = NULL;
b4dd5633 5014 struct signatured_type **iter;
3019eac3 5015
43988095
JK
5016 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5017 rcuh_kind::COMPILE);
78d4d2c5 5018 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5019 if (types_htab == NULL)
5020 {
5021 dwarf2_per_objfile->signatured_types = NULL;
5022 return 0;
5023 }
5024
348e048f
DE
5025 dwarf2_per_objfile->signatured_types = types_htab;
5026
6aa5f3a6
DE
5027 dwarf2_per_objfile->n_type_units
5028 = dwarf2_per_objfile->n_allocated_type_units
5029 = htab_elements (types_htab);
8d749320
SM
5030 dwarf2_per_objfile->all_type_units =
5031 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5032 iter = &dwarf2_per_objfile->all_type_units[0];
5033 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5034 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5035 == dwarf2_per_objfile->n_type_units);
1fd400ff 5036
348e048f
DE
5037 return 1;
5038}
5039
6aa5f3a6
DE
5040/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5041 If SLOT is non-NULL, it is the entry to use in the hash table.
5042 Otherwise we find one. */
5043
5044static struct signatured_type *
5045add_type_unit (ULONGEST sig, void **slot)
5046{
5047 struct objfile *objfile = dwarf2_per_objfile->objfile;
5048 int n_type_units = dwarf2_per_objfile->n_type_units;
5049 struct signatured_type *sig_type;
5050
5051 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5052 ++n_type_units;
5053 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5054 {
5055 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5056 dwarf2_per_objfile->n_allocated_type_units = 1;
5057 dwarf2_per_objfile->n_allocated_type_units *= 2;
5058 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5059 = XRESIZEVEC (struct signatured_type *,
5060 dwarf2_per_objfile->all_type_units,
5061 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5062 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5063 }
5064 dwarf2_per_objfile->n_type_units = n_type_units;
5065
5066 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5067 struct signatured_type);
5068 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5069 sig_type->signature = sig;
5070 sig_type->per_cu.is_debug_types = 1;
5071 if (dwarf2_per_objfile->using_index)
5072 {
5073 sig_type->per_cu.v.quick =
5074 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5075 struct dwarf2_per_cu_quick_data);
5076 }
5077
5078 if (slot == NULL)
5079 {
5080 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5081 sig_type, INSERT);
5082 }
5083 gdb_assert (*slot == NULL);
5084 *slot = sig_type;
5085 /* The rest of sig_type must be filled in by the caller. */
5086 return sig_type;
5087}
5088
a2ce51a0
DE
5089/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5090 Fill in SIG_ENTRY with DWO_ENTRY. */
5091
5092static void
5093fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5094 struct signatured_type *sig_entry,
5095 struct dwo_unit *dwo_entry)
5096{
7ee85ab1 5097 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5098 gdb_assert (! sig_entry->per_cu.queued);
5099 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5100 if (dwarf2_per_objfile->using_index)
5101 {
5102 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5103 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5104 }
5105 else
5106 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5107 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5108 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5109 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5110 gdb_assert (sig_entry->dwo_unit == NULL);
5111
5112 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5113 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5114 sig_entry->per_cu.length = dwo_entry->length;
5115 sig_entry->per_cu.reading_dwo_directly = 1;
5116 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5117 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5118 sig_entry->dwo_unit = dwo_entry;
5119}
5120
5121/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5122 If we haven't read the TU yet, create the signatured_type data structure
5123 for a TU to be read in directly from a DWO file, bypassing the stub.
5124 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5125 using .gdb_index, then when reading a CU we want to stay in the DWO file
5126 containing that CU. Otherwise we could end up reading several other DWO
5127 files (due to comdat folding) to process the transitive closure of all the
5128 mentioned TUs, and that can be slow. The current DWO file will have every
5129 type signature that it needs.
a2ce51a0
DE
5130 We only do this for .gdb_index because in the psymtab case we already have
5131 to read all the DWOs to build the type unit groups. */
5132
5133static struct signatured_type *
5134lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5135{
5136 struct objfile *objfile = dwarf2_per_objfile->objfile;
5137 struct dwo_file *dwo_file;
5138 struct dwo_unit find_dwo_entry, *dwo_entry;
5139 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5140 void **slot;
a2ce51a0
DE
5141
5142 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5143
6aa5f3a6
DE
5144 /* If TU skeletons have been removed then we may not have read in any
5145 TUs yet. */
5146 if (dwarf2_per_objfile->signatured_types == NULL)
5147 {
5148 dwarf2_per_objfile->signatured_types
5149 = allocate_signatured_type_table (objfile);
5150 }
a2ce51a0
DE
5151
5152 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5153 Use the global signatured_types array to do our own comdat-folding
5154 of types. If this is the first time we're reading this TU, and
5155 the TU has an entry in .gdb_index, replace the recorded data from
5156 .gdb_index with this TU. */
a2ce51a0 5157
a2ce51a0 5158 find_sig_entry.signature = sig;
6aa5f3a6
DE
5159 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5160 &find_sig_entry, INSERT);
9a3c8263 5161 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5162
5163 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5164 read. Don't reassign the global entry to point to this DWO if that's
5165 the case. Also note that if the TU is already being read, it may not
5166 have come from a DWO, the program may be a mix of Fission-compiled
5167 code and non-Fission-compiled code. */
5168
5169 /* Have we already tried to read this TU?
5170 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5171 needn't exist in the global table yet). */
5172 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5173 return sig_entry;
5174
6aa5f3a6
DE
5175 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5176 dwo_unit of the TU itself. */
5177 dwo_file = cu->dwo_unit->dwo_file;
5178
a2ce51a0
DE
5179 /* Ok, this is the first time we're reading this TU. */
5180 if (dwo_file->tus == NULL)
5181 return NULL;
5182 find_dwo_entry.signature = sig;
9a3c8263 5183 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5184 if (dwo_entry == NULL)
5185 return NULL;
5186
6aa5f3a6
DE
5187 /* If the global table doesn't have an entry for this TU, add one. */
5188 if (sig_entry == NULL)
5189 sig_entry = add_type_unit (sig, slot);
5190
a2ce51a0 5191 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5192 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5193 return sig_entry;
5194}
5195
a2ce51a0
DE
5196/* Subroutine of lookup_signatured_type.
5197 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5198 then try the DWP file. If the TU stub (skeleton) has been removed then
5199 it won't be in .gdb_index. */
a2ce51a0
DE
5200
5201static struct signatured_type *
5202lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5203{
5204 struct objfile *objfile = dwarf2_per_objfile->objfile;
5205 struct dwp_file *dwp_file = get_dwp_file ();
5206 struct dwo_unit *dwo_entry;
5207 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5208 void **slot;
a2ce51a0
DE
5209
5210 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5211 gdb_assert (dwp_file != NULL);
5212
6aa5f3a6
DE
5213 /* If TU skeletons have been removed then we may not have read in any
5214 TUs yet. */
5215 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5216 {
6aa5f3a6
DE
5217 dwarf2_per_objfile->signatured_types
5218 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5219 }
5220
6aa5f3a6
DE
5221 find_sig_entry.signature = sig;
5222 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5223 &find_sig_entry, INSERT);
9a3c8263 5224 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5225
5226 /* Have we already tried to read this TU?
5227 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5228 needn't exist in the global table yet). */
5229 if (sig_entry != NULL)
5230 return sig_entry;
5231
a2ce51a0
DE
5232 if (dwp_file->tus == NULL)
5233 return NULL;
57d63ce2
DE
5234 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5235 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5236 if (dwo_entry == NULL)
5237 return NULL;
5238
6aa5f3a6 5239 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5240 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5241
a2ce51a0
DE
5242 return sig_entry;
5243}
5244
380bca97 5245/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5246 Returns NULL if signature SIG is not present in the table.
5247 It is up to the caller to complain about this. */
348e048f
DE
5248
5249static struct signatured_type *
a2ce51a0 5250lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5251{
a2ce51a0
DE
5252 if (cu->dwo_unit
5253 && dwarf2_per_objfile->using_index)
5254 {
5255 /* We're in a DWO/DWP file, and we're using .gdb_index.
5256 These cases require special processing. */
5257 if (get_dwp_file () == NULL)
5258 return lookup_dwo_signatured_type (cu, sig);
5259 else
5260 return lookup_dwp_signatured_type (cu, sig);
5261 }
5262 else
5263 {
5264 struct signatured_type find_entry, *entry;
348e048f 5265
a2ce51a0
DE
5266 if (dwarf2_per_objfile->signatured_types == NULL)
5267 return NULL;
5268 find_entry.signature = sig;
9a3c8263
SM
5269 entry = ((struct signatured_type *)
5270 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5271 return entry;
5272 }
348e048f 5273}
42e7ad6c
DE
5274\f
5275/* Low level DIE reading support. */
348e048f 5276
d85a05f0
DJ
5277/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5278
5279static void
5280init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5281 struct dwarf2_cu *cu,
3019eac3
DE
5282 struct dwarf2_section_info *section,
5283 struct dwo_file *dwo_file)
d85a05f0 5284{
fceca515 5285 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5286 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5287 reader->cu = cu;
3019eac3 5288 reader->dwo_file = dwo_file;
dee91e82
DE
5289 reader->die_section = section;
5290 reader->buffer = section->buffer;
f664829e 5291 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5292 reader->comp_dir = NULL;
d85a05f0
DJ
5293}
5294
b0c7bfa9
DE
5295/* Subroutine of init_cutu_and_read_dies to simplify it.
5296 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5297 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5298 already.
5299
5300 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5301 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5302 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5303 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5304 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5305 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5306 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5307 are filled in with the info of the DIE from the DWO file.
5308 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5309 provided an abbrev table to use.
5310 The result is non-zero if a valid (non-dummy) DIE was found. */
5311
5312static int
5313read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5314 struct dwo_unit *dwo_unit,
5315 int abbrev_table_provided,
5316 struct die_info *stub_comp_unit_die,
a2ce51a0 5317 const char *stub_comp_dir,
b0c7bfa9 5318 struct die_reader_specs *result_reader,
d521ce57 5319 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5320 struct die_info **result_comp_unit_die,
5321 int *result_has_children)
5322{
5323 struct objfile *objfile = dwarf2_per_objfile->objfile;
5324 struct dwarf2_cu *cu = this_cu->cu;
5325 struct dwarf2_section_info *section;
5326 bfd *abfd;
d521ce57 5327 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5328 ULONGEST signature; /* Or dwo_id. */
5329 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5330 int i,num_extra_attrs;
5331 struct dwarf2_section_info *dwo_abbrev_section;
5332 struct attribute *attr;
5333 struct die_info *comp_unit_die;
5334
b0aeadb3
DE
5335 /* At most one of these may be provided. */
5336 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5337
b0c7bfa9
DE
5338 /* These attributes aren't processed until later:
5339 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5340 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5341 referenced later. However, these attributes are found in the stub
5342 which we won't have later. In order to not impose this complication
5343 on the rest of the code, we read them here and copy them to the
5344 DWO CU/TU die. */
b0c7bfa9
DE
5345
5346 stmt_list = NULL;
5347 low_pc = NULL;
5348 high_pc = NULL;
5349 ranges = NULL;
5350 comp_dir = NULL;
5351
5352 if (stub_comp_unit_die != NULL)
5353 {
5354 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5355 DWO file. */
5356 if (! this_cu->is_debug_types)
5357 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5358 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5359 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5360 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5361 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5362
5363 /* There should be a DW_AT_addr_base attribute here (if needed).
5364 We need the value before we can process DW_FORM_GNU_addr_index. */
5365 cu->addr_base = 0;
5366 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5367 if (attr)
5368 cu->addr_base = DW_UNSND (attr);
5369
5370 /* There should be a DW_AT_ranges_base attribute here (if needed).
5371 We need the value before we can process DW_AT_ranges. */
5372 cu->ranges_base = 0;
5373 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5374 if (attr)
5375 cu->ranges_base = DW_UNSND (attr);
5376 }
a2ce51a0
DE
5377 else if (stub_comp_dir != NULL)
5378 {
5379 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5380 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5381 comp_dir->name = DW_AT_comp_dir;
5382 comp_dir->form = DW_FORM_string;
5383 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5384 DW_STRING (comp_dir) = stub_comp_dir;
5385 }
b0c7bfa9
DE
5386
5387 /* Set up for reading the DWO CU/TU. */
5388 cu->dwo_unit = dwo_unit;
5389 section = dwo_unit->section;
5390 dwarf2_read_section (objfile, section);
a32a8923 5391 abfd = get_section_bfd_owner (section);
9c541725
PA
5392 begin_info_ptr = info_ptr = (section->buffer
5393 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5394 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5395 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5396
5397 if (this_cu->is_debug_types)
5398 {
b0c7bfa9
DE
5399 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5400
43988095 5401 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5402 dwo_abbrev_section,
43988095 5403 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5404 /* This is not an assert because it can be caused by bad debug info. */
43988095 5405 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5406 {
5407 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5408 " TU at offset 0x%x [in module %s]"),
5409 hex_string (sig_type->signature),
43988095 5410 hex_string (cu->header.signature),
9c541725 5411 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5412 bfd_get_filename (abfd));
5413 }
9c541725 5414 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5415 /* For DWOs coming from DWP files, we don't know the CU length
5416 nor the type's offset in the TU until now. */
5417 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5418 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5419
5420 /* Establish the type offset that can be used to lookup the type.
5421 For DWO files, we don't know it until now. */
9c541725
PA
5422 sig_type->type_offset_in_section
5423 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5424 }
5425 else
5426 {
5427 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5428 dwo_abbrev_section,
43988095 5429 info_ptr, rcuh_kind::COMPILE);
9c541725 5430 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5431 /* For DWOs coming from DWP files, we don't know the CU length
5432 until now. */
5433 dwo_unit->length = get_cu_length (&cu->header);
5434 }
5435
02142a6c
DE
5436 /* Replace the CU's original abbrev table with the DWO's.
5437 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5438 if (abbrev_table_provided)
5439 {
5440 /* Don't free the provided abbrev table, the caller of
5441 init_cutu_and_read_dies owns it. */
5442 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5443 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5444 make_cleanup (dwarf2_free_abbrev_table, cu);
5445 }
5446 else
5447 {
5448 dwarf2_free_abbrev_table (cu);
5449 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5450 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5451 }
5452
5453 /* Read in the die, but leave space to copy over the attributes
5454 from the stub. This has the benefit of simplifying the rest of
5455 the code - all the work to maintain the illusion of a single
5456 DW_TAG_{compile,type}_unit DIE is done here. */
5457 num_extra_attrs = ((stmt_list != NULL)
5458 + (low_pc != NULL)
5459 + (high_pc != NULL)
5460 + (ranges != NULL)
5461 + (comp_dir != NULL));
5462 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5463 result_has_children, num_extra_attrs);
5464
5465 /* Copy over the attributes from the stub to the DIE we just read in. */
5466 comp_unit_die = *result_comp_unit_die;
5467 i = comp_unit_die->num_attrs;
5468 if (stmt_list != NULL)
5469 comp_unit_die->attrs[i++] = *stmt_list;
5470 if (low_pc != NULL)
5471 comp_unit_die->attrs[i++] = *low_pc;
5472 if (high_pc != NULL)
5473 comp_unit_die->attrs[i++] = *high_pc;
5474 if (ranges != NULL)
5475 comp_unit_die->attrs[i++] = *ranges;
5476 if (comp_dir != NULL)
5477 comp_unit_die->attrs[i++] = *comp_dir;
5478 comp_unit_die->num_attrs += num_extra_attrs;
5479
b4f54984 5480 if (dwarf_die_debug)
bf6af496
DE
5481 {
5482 fprintf_unfiltered (gdb_stdlog,
5483 "Read die from %s@0x%x of %s:\n",
a32a8923 5484 get_section_name (section),
bf6af496
DE
5485 (unsigned) (begin_info_ptr - section->buffer),
5486 bfd_get_filename (abfd));
b4f54984 5487 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5488 }
5489
a2ce51a0
DE
5490 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5491 TUs by skipping the stub and going directly to the entry in the DWO file.
5492 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5493 to get it via circuitous means. Blech. */
5494 if (comp_dir != NULL)
5495 result_reader->comp_dir = DW_STRING (comp_dir);
5496
b0c7bfa9
DE
5497 /* Skip dummy compilation units. */
5498 if (info_ptr >= begin_info_ptr + dwo_unit->length
5499 || peek_abbrev_code (abfd, info_ptr) == 0)
5500 return 0;
5501
5502 *result_info_ptr = info_ptr;
5503 return 1;
5504}
5505
5506/* Subroutine of init_cutu_and_read_dies to simplify it.
5507 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5508 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5509
5510static struct dwo_unit *
5511lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5512 struct die_info *comp_unit_die)
5513{
5514 struct dwarf2_cu *cu = this_cu->cu;
5515 struct attribute *attr;
5516 ULONGEST signature;
5517 struct dwo_unit *dwo_unit;
5518 const char *comp_dir, *dwo_name;
5519
a2ce51a0
DE
5520 gdb_assert (cu != NULL);
5521
b0c7bfa9 5522 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5523 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5524 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5525
5526 if (this_cu->is_debug_types)
5527 {
5528 struct signatured_type *sig_type;
5529
5530 /* Since this_cu is the first member of struct signatured_type,
5531 we can go from a pointer to one to a pointer to the other. */
5532 sig_type = (struct signatured_type *) this_cu;
5533 signature = sig_type->signature;
5534 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5535 }
5536 else
5537 {
5538 struct attribute *attr;
5539
5540 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5541 if (! attr)
5542 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5543 " [in module %s]"),
4262abfb 5544 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5545 signature = DW_UNSND (attr);
5546 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5547 signature);
5548 }
5549
b0c7bfa9
DE
5550 return dwo_unit;
5551}
5552
a2ce51a0 5553/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5554 See it for a description of the parameters.
5555 Read a TU directly from a DWO file, bypassing the stub.
5556
5557 Note: This function could be a little bit simpler if we shared cleanups
5558 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5559 to do, so we keep this function self-contained. Or we could move this
5560 into our caller, but it's complex enough already. */
a2ce51a0
DE
5561
5562static void
6aa5f3a6
DE
5563init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5564 int use_existing_cu, int keep,
a2ce51a0
DE
5565 die_reader_func_ftype *die_reader_func,
5566 void *data)
5567{
5568 struct dwarf2_cu *cu;
5569 struct signatured_type *sig_type;
6aa5f3a6 5570 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5571 struct die_reader_specs reader;
5572 const gdb_byte *info_ptr;
5573 struct die_info *comp_unit_die;
5574 int has_children;
5575
5576 /* Verify we can do the following downcast, and that we have the
5577 data we need. */
5578 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5579 sig_type = (struct signatured_type *) this_cu;
5580 gdb_assert (sig_type->dwo_unit != NULL);
5581
5582 cleanups = make_cleanup (null_cleanup, NULL);
5583
6aa5f3a6
DE
5584 if (use_existing_cu && this_cu->cu != NULL)
5585 {
5586 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5587 cu = this_cu->cu;
5588 /* There's no need to do the rereading_dwo_cu handling that
5589 init_cutu_and_read_dies does since we don't read the stub. */
5590 }
5591 else
5592 {
5593 /* If !use_existing_cu, this_cu->cu must be NULL. */
5594 gdb_assert (this_cu->cu == NULL);
8d749320 5595 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5596 init_one_comp_unit (cu, this_cu);
5597 /* If an error occurs while loading, release our storage. */
5598 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5599 }
5600
5601 /* A future optimization, if needed, would be to use an existing
5602 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5603 could share abbrev tables. */
a2ce51a0
DE
5604
5605 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5606 0 /* abbrev_table_provided */,
5607 NULL /* stub_comp_unit_die */,
5608 sig_type->dwo_unit->dwo_file->comp_dir,
5609 &reader, &info_ptr,
5610 &comp_unit_die, &has_children) == 0)
5611 {
5612 /* Dummy die. */
5613 do_cleanups (cleanups);
5614 return;
5615 }
5616
5617 /* All the "real" work is done here. */
5618 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5619
6aa5f3a6 5620 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5621 but the alternative is making the latter more complex.
5622 This function is only for the special case of using DWO files directly:
5623 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5624 if (free_cu_cleanup != NULL)
a2ce51a0 5625 {
6aa5f3a6
DE
5626 if (keep)
5627 {
5628 /* We've successfully allocated this compilation unit. Let our
5629 caller clean it up when finished with it. */
5630 discard_cleanups (free_cu_cleanup);
a2ce51a0 5631
6aa5f3a6
DE
5632 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5633 So we have to manually free the abbrev table. */
5634 dwarf2_free_abbrev_table (cu);
a2ce51a0 5635
6aa5f3a6
DE
5636 /* Link this CU into read_in_chain. */
5637 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5638 dwarf2_per_objfile->read_in_chain = this_cu;
5639 }
5640 else
5641 do_cleanups (free_cu_cleanup);
a2ce51a0 5642 }
a2ce51a0
DE
5643
5644 do_cleanups (cleanups);
5645}
5646
fd820528 5647/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5648 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5649
f4dc4d17
DE
5650 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5651 Otherwise the table specified in the comp unit header is read in and used.
5652 This is an optimization for when we already have the abbrev table.
5653
dee91e82
DE
5654 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5655 Otherwise, a new CU is allocated with xmalloc.
5656
5657 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5658 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5659
5660 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5661 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5662
70221824 5663static void
fd820528 5664init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5665 struct abbrev_table *abbrev_table,
fd820528
DE
5666 int use_existing_cu, int keep,
5667 die_reader_func_ftype *die_reader_func,
5668 void *data)
c906108c 5669{
dee91e82 5670 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5671 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5672 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5673 struct dwarf2_cu *cu;
d521ce57 5674 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5675 struct die_reader_specs reader;
d85a05f0 5676 struct die_info *comp_unit_die;
dee91e82 5677 int has_children;
d85a05f0 5678 struct attribute *attr;
365156ad 5679 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5680 struct signatured_type *sig_type = NULL;
4bdcc0c1 5681 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5682 /* Non-zero if CU currently points to a DWO file and we need to
5683 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5684 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5685 int rereading_dwo_cu = 0;
c906108c 5686
b4f54984 5687 if (dwarf_die_debug)
09406207
DE
5688 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5689 this_cu->is_debug_types ? "type" : "comp",
9c541725 5690 to_underlying (this_cu->sect_off));
09406207 5691
dee91e82
DE
5692 if (use_existing_cu)
5693 gdb_assert (keep);
23745b47 5694
a2ce51a0
DE
5695 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5696 file (instead of going through the stub), short-circuit all of this. */
5697 if (this_cu->reading_dwo_directly)
5698 {
5699 /* Narrow down the scope of possibilities to have to understand. */
5700 gdb_assert (this_cu->is_debug_types);
5701 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5702 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5703 die_reader_func, data);
a2ce51a0
DE
5704 return;
5705 }
5706
dee91e82
DE
5707 cleanups = make_cleanup (null_cleanup, NULL);
5708
5709 /* This is cheap if the section is already read in. */
5710 dwarf2_read_section (objfile, section);
5711
9c541725 5712 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5713
5714 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5715
5716 if (use_existing_cu && this_cu->cu != NULL)
5717 {
5718 cu = this_cu->cu;
42e7ad6c
DE
5719 /* If this CU is from a DWO file we need to start over, we need to
5720 refetch the attributes from the skeleton CU.
5721 This could be optimized by retrieving those attributes from when we
5722 were here the first time: the previous comp_unit_die was stored in
5723 comp_unit_obstack. But there's no data yet that we need this
5724 optimization. */
5725 if (cu->dwo_unit != NULL)
5726 rereading_dwo_cu = 1;
dee91e82
DE
5727 }
5728 else
5729 {
5730 /* If !use_existing_cu, this_cu->cu must be NULL. */
5731 gdb_assert (this_cu->cu == NULL);
8d749320 5732 cu = XNEW (struct dwarf2_cu);
dee91e82 5733 init_one_comp_unit (cu, this_cu);
dee91e82 5734 /* If an error occurs while loading, release our storage. */
365156ad 5735 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5736 }
dee91e82 5737
b0c7bfa9 5738 /* Get the header. */
9c541725 5739 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5740 {
5741 /* We already have the header, there's no need to read it in again. */
9c541725 5742 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5743 }
5744 else
5745 {
3019eac3 5746 if (this_cu->is_debug_types)
dee91e82 5747 {
43988095 5748 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5749 abbrev_section, info_ptr,
43988095 5750 rcuh_kind::TYPE);
dee91e82 5751
42e7ad6c
DE
5752 /* Since per_cu is the first member of struct signatured_type,
5753 we can go from a pointer to one to a pointer to the other. */
5754 sig_type = (struct signatured_type *) this_cu;
43988095 5755 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5756 gdb_assert (sig_type->type_offset_in_tu
5757 == cu->header.type_cu_offset_in_tu);
5758 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5759
42e7ad6c
DE
5760 /* LENGTH has not been set yet for type units if we're
5761 using .gdb_index. */
1ce1cefd 5762 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5763
5764 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5765 sig_type->type_offset_in_section =
5766 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5767
5768 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5769 }
5770 else
5771 {
4bdcc0c1
DE
5772 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5773 abbrev_section,
43988095
JK
5774 info_ptr,
5775 rcuh_kind::COMPILE);
dee91e82 5776
9c541725 5777 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5778 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5779 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5780 }
5781 }
10b3939b 5782
6caca83c 5783 /* Skip dummy compilation units. */
dee91e82 5784 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5785 || peek_abbrev_code (abfd, info_ptr) == 0)
5786 {
dee91e82 5787 do_cleanups (cleanups);
21b2bd31 5788 return;
6caca83c
CC
5789 }
5790
433df2d4
DE
5791 /* If we don't have them yet, read the abbrevs for this compilation unit.
5792 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5793 done. Note that it's important that if the CU had an abbrev table
5794 on entry we don't free it when we're done: Somewhere up the call stack
5795 it may be in use. */
f4dc4d17
DE
5796 if (abbrev_table != NULL)
5797 {
5798 gdb_assert (cu->abbrev_table == NULL);
9c541725 5799 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5800 cu->abbrev_table = abbrev_table;
5801 }
5802 else if (cu->abbrev_table == NULL)
dee91e82 5803 {
4bdcc0c1 5804 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5805 make_cleanup (dwarf2_free_abbrev_table, cu);
5806 }
42e7ad6c
DE
5807 else if (rereading_dwo_cu)
5808 {
5809 dwarf2_free_abbrev_table (cu);
5810 dwarf2_read_abbrevs (cu, abbrev_section);
5811 }
af703f96 5812
dee91e82 5813 /* Read the top level CU/TU die. */
3019eac3 5814 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5815 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5816
b0c7bfa9
DE
5817 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5818 from the DWO file.
5819 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5820 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5821 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5822 if (attr)
5823 {
3019eac3 5824 struct dwo_unit *dwo_unit;
b0c7bfa9 5825 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5826
5827 if (has_children)
6a506a2d
DE
5828 {
5829 complaint (&symfile_complaints,
5830 _("compilation unit with DW_AT_GNU_dwo_name"
5831 " has children (offset 0x%x) [in module %s]"),
9c541725 5832 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5833 }
b0c7bfa9 5834 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5835 if (dwo_unit != NULL)
3019eac3 5836 {
6a506a2d
DE
5837 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5838 abbrev_table != NULL,
a2ce51a0 5839 comp_unit_die, NULL,
6a506a2d
DE
5840 &reader, &info_ptr,
5841 &dwo_comp_unit_die, &has_children) == 0)
5842 {
5843 /* Dummy die. */
5844 do_cleanups (cleanups);
5845 return;
5846 }
5847 comp_unit_die = dwo_comp_unit_die;
5848 }
5849 else
5850 {
5851 /* Yikes, we couldn't find the rest of the DIE, we only have
5852 the stub. A complaint has already been logged. There's
5853 not much more we can do except pass on the stub DIE to
5854 die_reader_func. We don't want to throw an error on bad
5855 debug info. */
3019eac3
DE
5856 }
5857 }
5858
b0c7bfa9 5859 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5860 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5861
b0c7bfa9 5862 /* Done, clean up. */
365156ad 5863 if (free_cu_cleanup != NULL)
348e048f 5864 {
365156ad
TT
5865 if (keep)
5866 {
5867 /* We've successfully allocated this compilation unit. Let our
5868 caller clean it up when finished with it. */
5869 discard_cleanups (free_cu_cleanup);
dee91e82 5870
365156ad
TT
5871 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5872 So we have to manually free the abbrev table. */
5873 dwarf2_free_abbrev_table (cu);
dee91e82 5874
365156ad
TT
5875 /* Link this CU into read_in_chain. */
5876 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5877 dwarf2_per_objfile->read_in_chain = this_cu;
5878 }
5879 else
5880 do_cleanups (free_cu_cleanup);
348e048f 5881 }
365156ad
TT
5882
5883 do_cleanups (cleanups);
dee91e82
DE
5884}
5885
33e80786
DE
5886/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5887 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5888 to have already done the lookup to find the DWO file).
dee91e82
DE
5889
5890 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5891 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5892
5893 We fill in THIS_CU->length.
5894
5895 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5896 linker) then DIE_READER_FUNC will not get called.
5897
5898 THIS_CU->cu is always freed when done.
3019eac3
DE
5899 This is done in order to not leave THIS_CU->cu in a state where we have
5900 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5901
5902static void
5903init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5904 struct dwo_file *dwo_file,
dee91e82
DE
5905 die_reader_func_ftype *die_reader_func,
5906 void *data)
5907{
5908 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5909 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5910 bfd *abfd = get_section_bfd_owner (section);
33e80786 5911 struct dwarf2_section_info *abbrev_section;
dee91e82 5912 struct dwarf2_cu cu;
d521ce57 5913 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5914 struct die_reader_specs reader;
5915 struct cleanup *cleanups;
5916 struct die_info *comp_unit_die;
5917 int has_children;
5918
b4f54984 5919 if (dwarf_die_debug)
09406207
DE
5920 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5921 this_cu->is_debug_types ? "type" : "comp",
9c541725 5922 to_underlying (this_cu->sect_off));
09406207 5923
dee91e82
DE
5924 gdb_assert (this_cu->cu == NULL);
5925
33e80786
DE
5926 abbrev_section = (dwo_file != NULL
5927 ? &dwo_file->sections.abbrev
5928 : get_abbrev_section_for_cu (this_cu));
5929
dee91e82
DE
5930 /* This is cheap if the section is already read in. */
5931 dwarf2_read_section (objfile, section);
5932
5933 init_one_comp_unit (&cu, this_cu);
5934
5935 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5936
9c541725 5937 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5938 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5939 abbrev_section, info_ptr,
43988095
JK
5940 (this_cu->is_debug_types
5941 ? rcuh_kind::TYPE
5942 : rcuh_kind::COMPILE));
dee91e82 5943
1ce1cefd 5944 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5945
5946 /* Skip dummy compilation units. */
5947 if (info_ptr >= begin_info_ptr + this_cu->length
5948 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5949 {
dee91e82 5950 do_cleanups (cleanups);
21b2bd31 5951 return;
93311388 5952 }
72bf9492 5953
dee91e82
DE
5954 dwarf2_read_abbrevs (&cu, abbrev_section);
5955 make_cleanup (dwarf2_free_abbrev_table, &cu);
5956
3019eac3 5957 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5958 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5959
5960 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5961
5962 do_cleanups (cleanups);
5963}
5964
3019eac3
DE
5965/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5966 does not lookup the specified DWO file.
5967 This cannot be used to read DWO files.
dee91e82
DE
5968
5969 THIS_CU->cu is always freed when done.
3019eac3
DE
5970 This is done in order to not leave THIS_CU->cu in a state where we have
5971 to care whether it refers to the "main" CU or the DWO CU.
5972 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5973
5974static void
5975init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5976 die_reader_func_ftype *die_reader_func,
5977 void *data)
5978{
33e80786 5979 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5980}
0018ea6f
DE
5981\f
5982/* Type Unit Groups.
dee91e82 5983
0018ea6f
DE
5984 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5985 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5986 so that all types coming from the same compilation (.o file) are grouped
5987 together. A future step could be to put the types in the same symtab as
5988 the CU the types ultimately came from. */
ff013f42 5989
f4dc4d17
DE
5990static hashval_t
5991hash_type_unit_group (const void *item)
5992{
9a3c8263
SM
5993 const struct type_unit_group *tu_group
5994 = (const struct type_unit_group *) item;
f4dc4d17 5995
094b34ac 5996 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5997}
348e048f
DE
5998
5999static int
f4dc4d17 6000eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6001{
9a3c8263
SM
6002 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6003 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6004
094b34ac 6005 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6006}
348e048f 6007
f4dc4d17
DE
6008/* Allocate a hash table for type unit groups. */
6009
6010static htab_t
6011allocate_type_unit_groups_table (void)
6012{
6013 return htab_create_alloc_ex (3,
6014 hash_type_unit_group,
6015 eq_type_unit_group,
6016 NULL,
6017 &dwarf2_per_objfile->objfile->objfile_obstack,
6018 hashtab_obstack_allocate,
6019 dummy_obstack_deallocate);
6020}
dee91e82 6021
f4dc4d17
DE
6022/* Type units that don't have DW_AT_stmt_list are grouped into their own
6023 partial symtabs. We combine several TUs per psymtab to not let the size
6024 of any one psymtab grow too big. */
6025#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6026#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6027
094b34ac 6028/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6029 Create the type_unit_group object used to hold one or more TUs. */
6030
6031static struct type_unit_group *
094b34ac 6032create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6033{
6034 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6035 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6036 struct type_unit_group *tu_group;
f4dc4d17
DE
6037
6038 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6039 struct type_unit_group);
094b34ac 6040 per_cu = &tu_group->per_cu;
f4dc4d17 6041 per_cu->objfile = objfile;
f4dc4d17 6042
094b34ac
DE
6043 if (dwarf2_per_objfile->using_index)
6044 {
6045 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6046 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6047 }
6048 else
6049 {
9c541725 6050 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6051 struct partial_symtab *pst;
6052 char *name;
6053
6054 /* Give the symtab a useful name for debug purposes. */
6055 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6056 name = xstrprintf ("<type_units_%d>",
6057 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6058 else
6059 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6060
6061 pst = create_partial_symtab (per_cu, name);
6062 pst->anonymous = 1;
f4dc4d17 6063
094b34ac
DE
6064 xfree (name);
6065 }
f4dc4d17 6066
094b34ac 6067 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6068 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6069
6070 return tu_group;
6071}
6072
094b34ac
DE
6073/* Look up the type_unit_group for type unit CU, and create it if necessary.
6074 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6075
6076static struct type_unit_group *
ff39bb5e 6077get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6078{
6079 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6080 struct type_unit_group *tu_group;
6081 void **slot;
6082 unsigned int line_offset;
6083 struct type_unit_group type_unit_group_for_lookup;
6084
6085 if (dwarf2_per_objfile->type_unit_groups == NULL)
6086 {
6087 dwarf2_per_objfile->type_unit_groups =
6088 allocate_type_unit_groups_table ();
6089 }
6090
6091 /* Do we need to create a new group, or can we use an existing one? */
6092
6093 if (stmt_list)
6094 {
6095 line_offset = DW_UNSND (stmt_list);
6096 ++tu_stats->nr_symtab_sharers;
6097 }
6098 else
6099 {
6100 /* Ugh, no stmt_list. Rare, but we have to handle it.
6101 We can do various things here like create one group per TU or
6102 spread them over multiple groups to split up the expansion work.
6103 To avoid worst case scenarios (too many groups or too large groups)
6104 we, umm, group them in bunches. */
6105 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6106 | (tu_stats->nr_stmt_less_type_units
6107 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6108 ++tu_stats->nr_stmt_less_type_units;
6109 }
6110
094b34ac 6111 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6112 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6113 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6114 &type_unit_group_for_lookup, INSERT);
6115 if (*slot != NULL)
6116 {
9a3c8263 6117 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6118 gdb_assert (tu_group != NULL);
6119 }
6120 else
6121 {
9c541725 6122 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6123 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6124 *slot = tu_group;
6125 ++tu_stats->nr_symtabs;
6126 }
6127
6128 return tu_group;
6129}
0018ea6f
DE
6130\f
6131/* Partial symbol tables. */
6132
6133/* Create a psymtab named NAME and assign it to PER_CU.
6134
6135 The caller must fill in the following details:
6136 dirname, textlow, texthigh. */
6137
6138static struct partial_symtab *
6139create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6140{
6141 struct objfile *objfile = per_cu->objfile;
6142 struct partial_symtab *pst;
6143
18a94d75 6144 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
6145 objfile->global_psymbols,
6146 objfile->static_psymbols);
0018ea6f
DE
6147
6148 pst->psymtabs_addrmap_supported = 1;
6149
6150 /* This is the glue that links PST into GDB's symbol API. */
6151 pst->read_symtab_private = per_cu;
6152 pst->read_symtab = dwarf2_read_symtab;
6153 per_cu->v.psymtab = pst;
6154
6155 return pst;
6156}
6157
b93601f3
TT
6158/* The DATA object passed to process_psymtab_comp_unit_reader has this
6159 type. */
6160
6161struct process_psymtab_comp_unit_data
6162{
6163 /* True if we are reading a DW_TAG_partial_unit. */
6164
6165 int want_partial_unit;
6166
6167 /* The "pretend" language that is used if the CU doesn't declare a
6168 language. */
6169
6170 enum language pretend_language;
6171};
6172
0018ea6f
DE
6173/* die_reader_func for process_psymtab_comp_unit. */
6174
6175static void
6176process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6177 const gdb_byte *info_ptr,
0018ea6f
DE
6178 struct die_info *comp_unit_die,
6179 int has_children,
6180 void *data)
6181{
6182 struct dwarf2_cu *cu = reader->cu;
6183 struct objfile *objfile = cu->objfile;
3e29f34a 6184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6185 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6186 CORE_ADDR baseaddr;
6187 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6188 struct partial_symtab *pst;
3a2b436a 6189 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6190 const char *filename;
9a3c8263
SM
6191 struct process_psymtab_comp_unit_data *info
6192 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6193
b93601f3 6194 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6195 return;
6196
6197 gdb_assert (! per_cu->is_debug_types);
6198
b93601f3 6199 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6200
6201 cu->list_in_scope = &file_symbols;
6202
6203 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6204 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6205 if (filename == NULL)
0018ea6f 6206 filename = "";
0018ea6f
DE
6207
6208 pst = create_partial_symtab (per_cu, filename);
6209
6210 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6211 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6212
6213 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6214
6215 dwarf2_find_base_address (comp_unit_die, cu);
6216
6217 /* Possibly set the default values of LOWPC and HIGHPC from
6218 `DW_AT_ranges'. */
3a2b436a
JK
6219 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6220 &best_highpc, cu, pst);
6221 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6222 /* Store the contiguous range if it is not empty; it can be empty for
6223 CUs with no code. */
6224 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6225 gdbarch_adjust_dwarf2_addr (gdbarch,
6226 best_lowpc + baseaddr),
6227 gdbarch_adjust_dwarf2_addr (gdbarch,
6228 best_highpc + baseaddr) - 1,
6229 pst);
0018ea6f
DE
6230
6231 /* Check if comp unit has_children.
6232 If so, read the rest of the partial symbols from this comp unit.
6233 If not, there's no more debug_info for this comp unit. */
6234 if (has_children)
6235 {
6236 struct partial_die_info *first_die;
6237 CORE_ADDR lowpc, highpc;
6238
6239 lowpc = ((CORE_ADDR) -1);
6240 highpc = ((CORE_ADDR) 0);
6241
6242 first_die = load_partial_dies (reader, info_ptr, 1);
6243
6244 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6245 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6246
6247 /* If we didn't find a lowpc, set it to highpc to avoid
6248 complaints from `maint check'. */
6249 if (lowpc == ((CORE_ADDR) -1))
6250 lowpc = highpc;
6251
6252 /* If the compilation unit didn't have an explicit address range,
6253 then use the information extracted from its child dies. */
e385593e 6254 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6255 {
6256 best_lowpc = lowpc;
6257 best_highpc = highpc;
6258 }
6259 }
3e29f34a
MR
6260 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6261 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6262
8763cede 6263 end_psymtab_common (objfile, pst);
0018ea6f
DE
6264
6265 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6266 {
6267 int i;
6268 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6269 struct dwarf2_per_cu_data *iter;
6270
6271 /* Fill in 'dependencies' here; we fill in 'users' in a
6272 post-pass. */
6273 pst->number_of_dependencies = len;
8d749320
SM
6274 pst->dependencies =
6275 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6276 for (i = 0;
6277 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6278 i, iter);
6279 ++i)
6280 pst->dependencies[i] = iter->v.psymtab;
6281
6282 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6283 }
6284
6285 /* Get the list of files included in the current compilation unit,
6286 and build a psymtab for each of them. */
6287 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6288
b4f54984 6289 if (dwarf_read_debug)
0018ea6f
DE
6290 {
6291 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6292
6293 fprintf_unfiltered (gdb_stdlog,
6294 "Psymtab for %s unit @0x%x: %s - %s"
6295 ", %d global, %d static syms\n",
6296 per_cu->is_debug_types ? "type" : "comp",
9c541725 6297 to_underlying (per_cu->sect_off),
0018ea6f
DE
6298 paddress (gdbarch, pst->textlow),
6299 paddress (gdbarch, pst->texthigh),
6300 pst->n_global_syms, pst->n_static_syms);
6301 }
6302}
6303
6304/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6305 Process compilation unit THIS_CU for a psymtab. */
6306
6307static void
6308process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6309 int want_partial_unit,
6310 enum language pretend_language)
0018ea6f
DE
6311{
6312 /* If this compilation unit was already read in, free the
6313 cached copy in order to read it in again. This is
6314 necessary because we skipped some symbols when we first
6315 read in the compilation unit (see load_partial_dies).
6316 This problem could be avoided, but the benefit is unclear. */
6317 if (this_cu->cu != NULL)
6318 free_one_cached_comp_unit (this_cu);
6319
f1902523
JK
6320 if (this_cu->is_debug_types)
6321 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6322 NULL);
6323 else
6324 {
6325 process_psymtab_comp_unit_data info;
6326 info.want_partial_unit = want_partial_unit;
6327 info.pretend_language = pretend_language;
6328 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6329 process_psymtab_comp_unit_reader, &info);
6330 }
0018ea6f
DE
6331
6332 /* Age out any secondary CUs. */
6333 age_cached_comp_units ();
6334}
f4dc4d17
DE
6335
6336/* Reader function for build_type_psymtabs. */
6337
6338static void
6339build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6340 const gdb_byte *info_ptr,
f4dc4d17
DE
6341 struct die_info *type_unit_die,
6342 int has_children,
6343 void *data)
6344{
6345 struct objfile *objfile = dwarf2_per_objfile->objfile;
6346 struct dwarf2_cu *cu = reader->cu;
6347 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6348 struct signatured_type *sig_type;
f4dc4d17
DE
6349 struct type_unit_group *tu_group;
6350 struct attribute *attr;
6351 struct partial_die_info *first_die;
6352 CORE_ADDR lowpc, highpc;
6353 struct partial_symtab *pst;
6354
6355 gdb_assert (data == NULL);
0186c6a7
DE
6356 gdb_assert (per_cu->is_debug_types);
6357 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6358
6359 if (! has_children)
6360 return;
6361
6362 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6363 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6364
0186c6a7 6365 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6366
6367 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6368 cu->list_in_scope = &file_symbols;
6369 pst = create_partial_symtab (per_cu, "");
6370 pst->anonymous = 1;
6371
6372 first_die = load_partial_dies (reader, info_ptr, 1);
6373
6374 lowpc = (CORE_ADDR) -1;
6375 highpc = (CORE_ADDR) 0;
6376 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6377
8763cede 6378 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6379}
6380
73051182
DE
6381/* Struct used to sort TUs by their abbreviation table offset. */
6382
6383struct tu_abbrev_offset
6384{
6385 struct signatured_type *sig_type;
6386 sect_offset abbrev_offset;
6387};
6388
6389/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6390
6391static int
6392sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6393{
9a3c8263
SM
6394 const struct tu_abbrev_offset * const *a
6395 = (const struct tu_abbrev_offset * const*) ap;
6396 const struct tu_abbrev_offset * const *b
6397 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6398 sect_offset aoff = (*a)->abbrev_offset;
6399 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6400
6401 return (aoff > boff) - (aoff < boff);
6402}
6403
6404/* Efficiently read all the type units.
6405 This does the bulk of the work for build_type_psymtabs.
6406
6407 The efficiency is because we sort TUs by the abbrev table they use and
6408 only read each abbrev table once. In one program there are 200K TUs
6409 sharing 8K abbrev tables.
6410
6411 The main purpose of this function is to support building the
6412 dwarf2_per_objfile->type_unit_groups table.
6413 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6414 can collapse the search space by grouping them by stmt_list.
6415 The savings can be significant, in the same program from above the 200K TUs
6416 share 8K stmt_list tables.
6417
6418 FUNC is expected to call get_type_unit_group, which will create the
6419 struct type_unit_group if necessary and add it to
6420 dwarf2_per_objfile->type_unit_groups. */
6421
6422static void
6423build_type_psymtabs_1 (void)
6424{
73051182
DE
6425 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6426 struct cleanup *cleanups;
6427 struct abbrev_table *abbrev_table;
6428 sect_offset abbrev_offset;
6429 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6430 int i;
6431
6432 /* It's up to the caller to not call us multiple times. */
6433 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6434
6435 if (dwarf2_per_objfile->n_type_units == 0)
6436 return;
6437
6438 /* TUs typically share abbrev tables, and there can be way more TUs than
6439 abbrev tables. Sort by abbrev table to reduce the number of times we
6440 read each abbrev table in.
6441 Alternatives are to punt or to maintain a cache of abbrev tables.
6442 This is simpler and efficient enough for now.
6443
6444 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6445 symtab to use). Typically TUs with the same abbrev offset have the same
6446 stmt_list value too so in practice this should work well.
6447
6448 The basic algorithm here is:
6449
6450 sort TUs by abbrev table
6451 for each TU with same abbrev table:
6452 read abbrev table if first user
6453 read TU top level DIE
6454 [IWBN if DWO skeletons had DW_AT_stmt_list]
6455 call FUNC */
6456
b4f54984 6457 if (dwarf_read_debug)
73051182
DE
6458 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6459
6460 /* Sort in a separate table to maintain the order of all_type_units
6461 for .gdb_index: TU indices directly index all_type_units. */
6462 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6463 dwarf2_per_objfile->n_type_units);
6464 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6465 {
6466 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6467
6468 sorted_by_abbrev[i].sig_type = sig_type;
6469 sorted_by_abbrev[i].abbrev_offset =
6470 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6471 sig_type->per_cu.sect_off);
73051182
DE
6472 }
6473 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6474 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6475 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6476
9c541725 6477 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6478 abbrev_table = NULL;
6479 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6480
6481 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6482 {
6483 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6484
6485 /* Switch to the next abbrev table if necessary. */
6486 if (abbrev_table == NULL
9c541725 6487 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6488 {
6489 if (abbrev_table != NULL)
6490 {
6491 abbrev_table_free (abbrev_table);
6492 /* Reset to NULL in case abbrev_table_read_table throws
6493 an error: abbrev_table_free_cleanup will get called. */
6494 abbrev_table = NULL;
6495 }
6496 abbrev_offset = tu->abbrev_offset;
6497 abbrev_table =
6498 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6499 abbrev_offset);
6500 ++tu_stats->nr_uniq_abbrev_tables;
6501 }
6502
6503 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6504 build_type_psymtabs_reader, NULL);
6505 }
6506
73051182 6507 do_cleanups (cleanups);
6aa5f3a6 6508}
73051182 6509
6aa5f3a6
DE
6510/* Print collected type unit statistics. */
6511
6512static void
6513print_tu_stats (void)
6514{
6515 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6516
6517 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6518 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6519 dwarf2_per_objfile->n_type_units);
6520 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6521 tu_stats->nr_uniq_abbrev_tables);
6522 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6523 tu_stats->nr_symtabs);
6524 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6525 tu_stats->nr_symtab_sharers);
6526 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6527 tu_stats->nr_stmt_less_type_units);
6528 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6529 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6530}
6531
f4dc4d17
DE
6532/* Traversal function for build_type_psymtabs. */
6533
6534static int
6535build_type_psymtab_dependencies (void **slot, void *info)
6536{
6537 struct objfile *objfile = dwarf2_per_objfile->objfile;
6538 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6539 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6540 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6541 int len = VEC_length (sig_type_ptr, tu_group->tus);
6542 struct signatured_type *iter;
f4dc4d17
DE
6543 int i;
6544
6545 gdb_assert (len > 0);
0186c6a7 6546 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6547
6548 pst->number_of_dependencies = len;
8d749320
SM
6549 pst->dependencies =
6550 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6551 for (i = 0;
0186c6a7 6552 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6553 ++i)
6554 {
0186c6a7
DE
6555 gdb_assert (iter->per_cu.is_debug_types);
6556 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6557 iter->type_unit_group = tu_group;
f4dc4d17
DE
6558 }
6559
0186c6a7 6560 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6561
6562 return 1;
6563}
6564
6565/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6566 Build partial symbol tables for the .debug_types comp-units. */
6567
6568static void
6569build_type_psymtabs (struct objfile *objfile)
6570{
0e50663e 6571 if (! create_all_type_units (objfile))
348e048f
DE
6572 return;
6573
73051182 6574 build_type_psymtabs_1 ();
6aa5f3a6 6575}
f4dc4d17 6576
6aa5f3a6
DE
6577/* Traversal function for process_skeletonless_type_unit.
6578 Read a TU in a DWO file and build partial symbols for it. */
6579
6580static int
6581process_skeletonless_type_unit (void **slot, void *info)
6582{
6583 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6584 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6585 struct signatured_type find_entry, *entry;
6586
6587 /* If this TU doesn't exist in the global table, add it and read it in. */
6588
6589 if (dwarf2_per_objfile->signatured_types == NULL)
6590 {
6591 dwarf2_per_objfile->signatured_types
6592 = allocate_signatured_type_table (objfile);
6593 }
6594
6595 find_entry.signature = dwo_unit->signature;
6596 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6597 INSERT);
6598 /* If we've already seen this type there's nothing to do. What's happening
6599 is we're doing our own version of comdat-folding here. */
6600 if (*slot != NULL)
6601 return 1;
6602
6603 /* This does the job that create_all_type_units would have done for
6604 this TU. */
6605 entry = add_type_unit (dwo_unit->signature, slot);
6606 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6607 *slot = entry;
6608
6609 /* This does the job that build_type_psymtabs_1 would have done. */
6610 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6611 build_type_psymtabs_reader, NULL);
6612
6613 return 1;
6614}
6615
6616/* Traversal function for process_skeletonless_type_units. */
6617
6618static int
6619process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6620{
6621 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6622
6623 if (dwo_file->tus != NULL)
6624 {
6625 htab_traverse_noresize (dwo_file->tus,
6626 process_skeletonless_type_unit, info);
6627 }
6628
6629 return 1;
6630}
6631
6632/* Scan all TUs of DWO files, verifying we've processed them.
6633 This is needed in case a TU was emitted without its skeleton.
6634 Note: This can't be done until we know what all the DWO files are. */
6635
6636static void
6637process_skeletonless_type_units (struct objfile *objfile)
6638{
6639 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6640 if (get_dwp_file () == NULL
6641 && dwarf2_per_objfile->dwo_files != NULL)
6642 {
6643 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6644 process_dwo_file_for_skeletonless_type_units,
6645 objfile);
6646 }
348e048f
DE
6647}
6648
95554aad
TT
6649/* Compute the 'user' field for each psymtab in OBJFILE. */
6650
6651static void
6652set_partial_user (struct objfile *objfile)
6653{
6654 int i;
6655
6656 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6657 {
8832e7e3 6658 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6659 struct partial_symtab *pst = per_cu->v.psymtab;
6660 int j;
6661
36586728
TT
6662 if (pst == NULL)
6663 continue;
6664
95554aad
TT
6665 for (j = 0; j < pst->number_of_dependencies; ++j)
6666 {
6667 /* Set the 'user' field only if it is not already set. */
6668 if (pst->dependencies[j]->user == NULL)
6669 pst->dependencies[j]->user = pst;
6670 }
6671 }
6672}
6673
93311388
DE
6674/* Build the partial symbol table by doing a quick pass through the
6675 .debug_info and .debug_abbrev sections. */
72bf9492 6676
93311388 6677static void
c67a9c90 6678dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6679{
791afaa2 6680 struct cleanup *back_to;
21b2bd31 6681 int i;
93311388 6682
b4f54984 6683 if (dwarf_read_debug)
45cfd468
DE
6684 {
6685 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6686 objfile_name (objfile));
45cfd468
DE
6687 }
6688
98bfdba5
PA
6689 dwarf2_per_objfile->reading_partial_symbols = 1;
6690
be391dca 6691 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6692
93311388
DE
6693 /* Any cached compilation units will be linked by the per-objfile
6694 read_in_chain. Make sure to free them when we're done. */
6695 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6696
348e048f
DE
6697 build_type_psymtabs (objfile);
6698
93311388 6699 create_all_comp_units (objfile);
c906108c 6700
60606b2c
TT
6701 /* Create a temporary address map on a temporary obstack. We later
6702 copy this to the final obstack. */
8268c778 6703 auto_obstack temp_obstack;
791afaa2
TT
6704
6705 scoped_restore save_psymtabs_addrmap
6706 = make_scoped_restore (&objfile->psymtabs_addrmap,
6707 addrmap_create_mutable (&temp_obstack));
72bf9492 6708
21b2bd31 6709 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6710 {
8832e7e3 6711 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6712
b93601f3 6713 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6714 }
ff013f42 6715
6aa5f3a6
DE
6716 /* This has to wait until we read the CUs, we need the list of DWOs. */
6717 process_skeletonless_type_units (objfile);
6718
6719 /* Now that all TUs have been processed we can fill in the dependencies. */
6720 if (dwarf2_per_objfile->type_unit_groups != NULL)
6721 {
6722 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6723 build_type_psymtab_dependencies, NULL);
6724 }
6725
b4f54984 6726 if (dwarf_read_debug)
6aa5f3a6
DE
6727 print_tu_stats ();
6728
95554aad
TT
6729 set_partial_user (objfile);
6730
ff013f42
JK
6731 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6732 &objfile->objfile_obstack);
791afaa2
TT
6733 /* At this point we want to keep the address map. */
6734 save_psymtabs_addrmap.release ();
ff013f42 6735
ae038cb0 6736 do_cleanups (back_to);
45cfd468 6737
b4f54984 6738 if (dwarf_read_debug)
45cfd468 6739 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6740 objfile_name (objfile));
ae038cb0
DJ
6741}
6742
3019eac3 6743/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6744
6745static void
dee91e82 6746load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6747 const gdb_byte *info_ptr,
dee91e82
DE
6748 struct die_info *comp_unit_die,
6749 int has_children,
6750 void *data)
ae038cb0 6751{
dee91e82 6752 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6753
95554aad 6754 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6755
ae038cb0
DJ
6756 /* Check if comp unit has_children.
6757 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6758 If not, there's no more debug_info for this comp unit. */
d85a05f0 6759 if (has_children)
dee91e82
DE
6760 load_partial_dies (reader, info_ptr, 0);
6761}
98bfdba5 6762
dee91e82
DE
6763/* Load the partial DIEs for a secondary CU into memory.
6764 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6765
dee91e82
DE
6766static void
6767load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6768{
f4dc4d17
DE
6769 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6770 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6771}
6772
ae038cb0 6773static void
36586728
TT
6774read_comp_units_from_section (struct objfile *objfile,
6775 struct dwarf2_section_info *section,
f1902523 6776 struct dwarf2_section_info *abbrev_section,
36586728
TT
6777 unsigned int is_dwz,
6778 int *n_allocated,
6779 int *n_comp_units,
6780 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6781{
d521ce57 6782 const gdb_byte *info_ptr;
a32a8923 6783 bfd *abfd = get_section_bfd_owner (section);
be391dca 6784
b4f54984 6785 if (dwarf_read_debug)
bf6af496 6786 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6787 get_section_name (section),
6788 get_section_file_name (section));
bf6af496 6789
36586728 6790 dwarf2_read_section (objfile, section);
ae038cb0 6791
36586728 6792 info_ptr = section->buffer;
6e70227d 6793
36586728 6794 while (info_ptr < section->buffer + section->size)
ae038cb0 6795 {
ae038cb0 6796 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6797
9c541725 6798 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6799
f1902523
JK
6800 comp_unit_head cu_header;
6801 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6802 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6803
6804 /* Save the compilation unit for later lookup. */
f1902523
JK
6805 if (cu_header.unit_type != DW_UT_type)
6806 {
6807 this_cu = XOBNEW (&objfile->objfile_obstack,
6808 struct dwarf2_per_cu_data);
6809 memset (this_cu, 0, sizeof (*this_cu));
6810 }
6811 else
6812 {
6813 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6814 struct signatured_type);
6815 memset (sig_type, 0, sizeof (*sig_type));
6816 sig_type->signature = cu_header.signature;
6817 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6818 this_cu = &sig_type->per_cu;
6819 }
6820 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6821 this_cu->sect_off = sect_off;
f1902523 6822 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6823 this_cu->is_dwz = is_dwz;
9291a0cd 6824 this_cu->objfile = objfile;
8a0459fd 6825 this_cu->section = section;
ae038cb0 6826
36586728 6827 if (*n_comp_units == *n_allocated)
ae038cb0 6828 {
36586728 6829 *n_allocated *= 2;
224c3ddb
SM
6830 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6831 *all_comp_units, *n_allocated);
ae038cb0 6832 }
36586728
TT
6833 (*all_comp_units)[*n_comp_units] = this_cu;
6834 ++*n_comp_units;
ae038cb0
DJ
6835
6836 info_ptr = info_ptr + this_cu->length;
6837 }
36586728
TT
6838}
6839
6840/* Create a list of all compilation units in OBJFILE.
6841 This is only done for -readnow and building partial symtabs. */
6842
6843static void
6844create_all_comp_units (struct objfile *objfile)
6845{
6846 int n_allocated;
6847 int n_comp_units;
6848 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6849 struct dwz_file *dwz;
36586728
TT
6850
6851 n_comp_units = 0;
6852 n_allocated = 10;
8d749320 6853 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6854
f1902523
JK
6855 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6856 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6857 &n_allocated, &n_comp_units, &all_comp_units);
6858
4db1a1dc
TT
6859 dwz = dwarf2_get_dwz_file ();
6860 if (dwz != NULL)
f1902523 6861 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6862 &n_allocated, &n_comp_units,
6863 &all_comp_units);
ae038cb0 6864
8d749320
SM
6865 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6866 struct dwarf2_per_cu_data *,
6867 n_comp_units);
ae038cb0
DJ
6868 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6869 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6870 xfree (all_comp_units);
6871 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6872}
6873
5734ee8b 6874/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6875 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6876 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6877 DW_AT_ranges). See the comments of add_partial_subprogram on how
6878 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6879
72bf9492
DJ
6880static void
6881scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6882 CORE_ADDR *highpc, int set_addrmap,
6883 struct dwarf2_cu *cu)
c906108c 6884{
72bf9492 6885 struct partial_die_info *pdi;
c906108c 6886
91c24f0a
DC
6887 /* Now, march along the PDI's, descending into ones which have
6888 interesting children but skipping the children of the other ones,
6889 until we reach the end of the compilation unit. */
c906108c 6890
72bf9492 6891 pdi = first_die;
91c24f0a 6892
72bf9492
DJ
6893 while (pdi != NULL)
6894 {
6895 fixup_partial_die (pdi, cu);
c906108c 6896
f55ee35c 6897 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6898 children, so we need to look at them. Ditto for anonymous
6899 enums. */
933c6fe4 6900
72bf9492 6901 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6902 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6903 || pdi->tag == DW_TAG_imported_unit)
c906108c 6904 {
72bf9492 6905 switch (pdi->tag)
c906108c
SS
6906 {
6907 case DW_TAG_subprogram:
cdc07690 6908 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6909 break;
72929c62 6910 case DW_TAG_constant:
c906108c
SS
6911 case DW_TAG_variable:
6912 case DW_TAG_typedef:
91c24f0a 6913 case DW_TAG_union_type:
72bf9492 6914 if (!pdi->is_declaration)
63d06c5c 6915 {
72bf9492 6916 add_partial_symbol (pdi, cu);
63d06c5c
DC
6917 }
6918 break;
c906108c 6919 case DW_TAG_class_type:
680b30c7 6920 case DW_TAG_interface_type:
c906108c 6921 case DW_TAG_structure_type:
72bf9492 6922 if (!pdi->is_declaration)
c906108c 6923 {
72bf9492 6924 add_partial_symbol (pdi, cu);
c906108c 6925 }
e98c9e7c
TT
6926 if (cu->language == language_rust && pdi->has_children)
6927 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6928 set_addrmap, cu);
c906108c 6929 break;
91c24f0a 6930 case DW_TAG_enumeration_type:
72bf9492
DJ
6931 if (!pdi->is_declaration)
6932 add_partial_enumeration (pdi, cu);
c906108c
SS
6933 break;
6934 case DW_TAG_base_type:
a02abb62 6935 case DW_TAG_subrange_type:
c906108c 6936 /* File scope base type definitions are added to the partial
c5aa993b 6937 symbol table. */
72bf9492 6938 add_partial_symbol (pdi, cu);
c906108c 6939 break;
d9fa45fe 6940 case DW_TAG_namespace:
cdc07690 6941 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6942 break;
5d7cb8df 6943 case DW_TAG_module:
cdc07690 6944 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6945 break;
95554aad
TT
6946 case DW_TAG_imported_unit:
6947 {
6948 struct dwarf2_per_cu_data *per_cu;
6949
f4dc4d17
DE
6950 /* For now we don't handle imported units in type units. */
6951 if (cu->per_cu->is_debug_types)
6952 {
6953 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6954 " supported in type units [in module %s]"),
4262abfb 6955 objfile_name (cu->objfile));
f4dc4d17
DE
6956 }
6957
9c541725 6958 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6959 pdi->is_dwz,
95554aad
TT
6960 cu->objfile);
6961
6962 /* Go read the partial unit, if needed. */
6963 if (per_cu->v.psymtab == NULL)
b93601f3 6964 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6965
f4dc4d17 6966 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6967 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6968 }
6969 break;
74921315
KS
6970 case DW_TAG_imported_declaration:
6971 add_partial_symbol (pdi, cu);
6972 break;
c906108c
SS
6973 default:
6974 break;
6975 }
6976 }
6977
72bf9492
DJ
6978 /* If the die has a sibling, skip to the sibling. */
6979
6980 pdi = pdi->die_sibling;
6981 }
6982}
6983
6984/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6985
72bf9492 6986 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6987 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6988 Enumerators are an exception; they use the scope of their parent
6989 enumeration type, i.e. the name of the enumeration type is not
6990 prepended to the enumerator.
91c24f0a 6991
72bf9492
DJ
6992 There are two complexities. One is DW_AT_specification; in this
6993 case "parent" means the parent of the target of the specification,
6994 instead of the direct parent of the DIE. The other is compilers
6995 which do not emit DW_TAG_namespace; in this case we try to guess
6996 the fully qualified name of structure types from their members'
6997 linkage names. This must be done using the DIE's children rather
6998 than the children of any DW_AT_specification target. We only need
6999 to do this for structures at the top level, i.e. if the target of
7000 any DW_AT_specification (if any; otherwise the DIE itself) does not
7001 have a parent. */
7002
7003/* Compute the scope prefix associated with PDI's parent, in
7004 compilation unit CU. The result will be allocated on CU's
7005 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7006 field. NULL is returned if no prefix is necessary. */
15d034d0 7007static const char *
72bf9492
DJ
7008partial_die_parent_scope (struct partial_die_info *pdi,
7009 struct dwarf2_cu *cu)
7010{
15d034d0 7011 const char *grandparent_scope;
72bf9492 7012 struct partial_die_info *parent, *real_pdi;
91c24f0a 7013
72bf9492
DJ
7014 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7015 then this means the parent of the specification DIE. */
7016
7017 real_pdi = pdi;
72bf9492 7018 while (real_pdi->has_specification)
36586728
TT
7019 real_pdi = find_partial_die (real_pdi->spec_offset,
7020 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7021
7022 parent = real_pdi->die_parent;
7023 if (parent == NULL)
7024 return NULL;
7025
7026 if (parent->scope_set)
7027 return parent->scope;
7028
7029 fixup_partial_die (parent, cu);
7030
10b3939b 7031 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7032
acebe513
UW
7033 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7034 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7035 Work around this problem here. */
7036 if (cu->language == language_cplus
6e70227d 7037 && parent->tag == DW_TAG_namespace
acebe513
UW
7038 && strcmp (parent->name, "::") == 0
7039 && grandparent_scope == NULL)
7040 {
7041 parent->scope = NULL;
7042 parent->scope_set = 1;
7043 return NULL;
7044 }
7045
9c6c53f7
SA
7046 if (pdi->tag == DW_TAG_enumerator)
7047 /* Enumerators should not get the name of the enumeration as a prefix. */
7048 parent->scope = grandparent_scope;
7049 else if (parent->tag == DW_TAG_namespace
f55ee35c 7050 || parent->tag == DW_TAG_module
72bf9492
DJ
7051 || parent->tag == DW_TAG_structure_type
7052 || parent->tag == DW_TAG_class_type
680b30c7 7053 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7054 || parent->tag == DW_TAG_union_type
7055 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7056 {
7057 if (grandparent_scope == NULL)
7058 parent->scope = parent->name;
7059 else
3e43a32a
MS
7060 parent->scope = typename_concat (&cu->comp_unit_obstack,
7061 grandparent_scope,
f55ee35c 7062 parent->name, 0, cu);
72bf9492 7063 }
72bf9492
DJ
7064 else
7065 {
7066 /* FIXME drow/2004-04-01: What should we be doing with
7067 function-local names? For partial symbols, we should probably be
7068 ignoring them. */
7069 complaint (&symfile_complaints,
e2e0b3e5 7070 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7071 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7072 parent->scope = grandparent_scope;
c906108c
SS
7073 }
7074
72bf9492
DJ
7075 parent->scope_set = 1;
7076 return parent->scope;
7077}
7078
7079/* Return the fully scoped name associated with PDI, from compilation unit
7080 CU. The result will be allocated with malloc. */
4568ecf9 7081
72bf9492
DJ
7082static char *
7083partial_die_full_name (struct partial_die_info *pdi,
7084 struct dwarf2_cu *cu)
7085{
15d034d0 7086 const char *parent_scope;
72bf9492 7087
98bfdba5
PA
7088 /* If this is a template instantiation, we can not work out the
7089 template arguments from partial DIEs. So, unfortunately, we have
7090 to go through the full DIEs. At least any work we do building
7091 types here will be reused if full symbols are loaded later. */
7092 if (pdi->has_template_arguments)
7093 {
7094 fixup_partial_die (pdi, cu);
7095
7096 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7097 {
7098 struct die_info *die;
7099 struct attribute attr;
7100 struct dwarf2_cu *ref_cu = cu;
7101
b64f50a1 7102 /* DW_FORM_ref_addr is using section offset. */
b4069958 7103 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7104 attr.form = DW_FORM_ref_addr;
9c541725 7105 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7106 die = follow_die_ref (NULL, &attr, &ref_cu);
7107
7108 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7109 }
7110 }
7111
72bf9492
DJ
7112 parent_scope = partial_die_parent_scope (pdi, cu);
7113 if (parent_scope == NULL)
7114 return NULL;
7115 else
f55ee35c 7116 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7117}
7118
7119static void
72bf9492 7120add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7121{
e7c27a73 7122 struct objfile *objfile = cu->objfile;
3e29f34a 7123 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7124 CORE_ADDR addr = 0;
15d034d0 7125 const char *actual_name = NULL;
e142c38c 7126 CORE_ADDR baseaddr;
15d034d0 7127 char *built_actual_name;
e142c38c
DJ
7128
7129 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7130
15d034d0
TT
7131 built_actual_name = partial_die_full_name (pdi, cu);
7132 if (built_actual_name != NULL)
7133 actual_name = built_actual_name;
63d06c5c 7134
72bf9492
DJ
7135 if (actual_name == NULL)
7136 actual_name = pdi->name;
7137
c906108c
SS
7138 switch (pdi->tag)
7139 {
7140 case DW_TAG_subprogram:
3e29f34a 7141 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7142 if (pdi->is_external || cu->language == language_ada)
c906108c 7143 {
2cfa0c8d
JB
7144 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7145 of the global scope. But in Ada, we want to be able to access
7146 nested procedures globally. So all Ada subprograms are stored
7147 in the global scope. */
f47fb265 7148 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7149 built_actual_name != NULL,
f47fb265
MS
7150 VAR_DOMAIN, LOC_BLOCK,
7151 &objfile->global_psymbols,
1762568f 7152 addr, cu->language, objfile);
c906108c
SS
7153 }
7154 else
7155 {
f47fb265 7156 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7157 built_actual_name != NULL,
f47fb265
MS
7158 VAR_DOMAIN, LOC_BLOCK,
7159 &objfile->static_psymbols,
1762568f 7160 addr, cu->language, objfile);
c906108c 7161 }
0c1b455e
TT
7162
7163 if (pdi->main_subprogram && actual_name != NULL)
7164 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7165 break;
72929c62
JB
7166 case DW_TAG_constant:
7167 {
af5bf4ad 7168 std::vector<partial_symbol *> *list;
72929c62
JB
7169
7170 if (pdi->is_external)
7171 list = &objfile->global_psymbols;
7172 else
7173 list = &objfile->static_psymbols;
f47fb265 7174 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7175 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7176 list, 0, cu->language, objfile);
72929c62
JB
7177 }
7178 break;
c906108c 7179 case DW_TAG_variable:
95554aad
TT
7180 if (pdi->d.locdesc)
7181 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7182
95554aad 7183 if (pdi->d.locdesc
caac4577
JG
7184 && addr == 0
7185 && !dwarf2_per_objfile->has_section_at_zero)
7186 {
7187 /* A global or static variable may also have been stripped
7188 out by the linker if unused, in which case its address
7189 will be nullified; do not add such variables into partial
7190 symbol table then. */
7191 }
7192 else if (pdi->is_external)
c906108c
SS
7193 {
7194 /* Global Variable.
7195 Don't enter into the minimal symbol tables as there is
7196 a minimal symbol table entry from the ELF symbols already.
7197 Enter into partial symbol table if it has a location
7198 descriptor or a type.
7199 If the location descriptor is missing, new_symbol will create
7200 a LOC_UNRESOLVED symbol, the address of the variable will then
7201 be determined from the minimal symbol table whenever the variable
7202 is referenced.
7203 The address for the partial symbol table entry is not
7204 used by GDB, but it comes in handy for debugging partial symbol
7205 table building. */
7206
95554aad 7207 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7208 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7209 built_actual_name != NULL,
f47fb265
MS
7210 VAR_DOMAIN, LOC_STATIC,
7211 &objfile->global_psymbols,
1762568f 7212 addr + baseaddr,
f47fb265 7213 cu->language, objfile);
c906108c
SS
7214 }
7215 else
7216 {
ff908ebf
AW
7217 int has_loc = pdi->d.locdesc != NULL;
7218
7219 /* Static Variable. Skip symbols whose value we cannot know (those
7220 without location descriptors or constant values). */
7221 if (!has_loc && !pdi->has_const_value)
decbce07 7222 {
15d034d0 7223 xfree (built_actual_name);
decbce07
MS
7224 return;
7225 }
ff908ebf 7226
f47fb265 7227 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7228 built_actual_name != NULL,
f47fb265
MS
7229 VAR_DOMAIN, LOC_STATIC,
7230 &objfile->static_psymbols,
ff908ebf 7231 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7232 cu->language, objfile);
c906108c
SS
7233 }
7234 break;
7235 case DW_TAG_typedef:
7236 case DW_TAG_base_type:
a02abb62 7237 case DW_TAG_subrange_type:
38d518c9 7238 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7239 built_actual_name != NULL,
176620f1 7240 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7241 &objfile->static_psymbols,
1762568f 7242 0, cu->language, objfile);
c906108c 7243 break;
74921315 7244 case DW_TAG_imported_declaration:
72bf9492
DJ
7245 case DW_TAG_namespace:
7246 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7247 built_actual_name != NULL,
72bf9492
DJ
7248 VAR_DOMAIN, LOC_TYPEDEF,
7249 &objfile->global_psymbols,
1762568f 7250 0, cu->language, objfile);
72bf9492 7251 break;
530e8392
KB
7252 case DW_TAG_module:
7253 add_psymbol_to_list (actual_name, strlen (actual_name),
7254 built_actual_name != NULL,
7255 MODULE_DOMAIN, LOC_TYPEDEF,
7256 &objfile->global_psymbols,
1762568f 7257 0, cu->language, objfile);
530e8392 7258 break;
c906108c 7259 case DW_TAG_class_type:
680b30c7 7260 case DW_TAG_interface_type:
c906108c
SS
7261 case DW_TAG_structure_type:
7262 case DW_TAG_union_type:
7263 case DW_TAG_enumeration_type:
fa4028e9
JB
7264 /* Skip external references. The DWARF standard says in the section
7265 about "Structure, Union, and Class Type Entries": "An incomplete
7266 structure, union or class type is represented by a structure,
7267 union or class entry that does not have a byte size attribute
7268 and that has a DW_AT_declaration attribute." */
7269 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7270 {
15d034d0 7271 xfree (built_actual_name);
decbce07
MS
7272 return;
7273 }
fa4028e9 7274
63d06c5c
DC
7275 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7276 static vs. global. */
38d518c9 7277 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7278 built_actual_name != NULL,
176620f1 7279 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7280 cu->language == language_cplus
63d06c5c
DC
7281 ? &objfile->global_psymbols
7282 : &objfile->static_psymbols,
1762568f 7283 0, cu->language, objfile);
c906108c 7284
c906108c
SS
7285 break;
7286 case DW_TAG_enumerator:
38d518c9 7287 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7288 built_actual_name != NULL,
176620f1 7289 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7290 cu->language == language_cplus
f6fe98ef
DJ
7291 ? &objfile->global_psymbols
7292 : &objfile->static_psymbols,
1762568f 7293 0, cu->language, objfile);
c906108c
SS
7294 break;
7295 default:
7296 break;
7297 }
5c4e30ca 7298
15d034d0 7299 xfree (built_actual_name);
c906108c
SS
7300}
7301
5c4e30ca
DC
7302/* Read a partial die corresponding to a namespace; also, add a symbol
7303 corresponding to that namespace to the symbol table. NAMESPACE is
7304 the name of the enclosing namespace. */
91c24f0a 7305
72bf9492
DJ
7306static void
7307add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7308 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7309 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7310{
72bf9492 7311 /* Add a symbol for the namespace. */
e7c27a73 7312
72bf9492 7313 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7314
7315 /* Now scan partial symbols in that namespace. */
7316
91c24f0a 7317 if (pdi->has_children)
cdc07690 7318 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7319}
7320
5d7cb8df
JK
7321/* Read a partial die corresponding to a Fortran module. */
7322
7323static void
7324add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7325 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7326{
530e8392
KB
7327 /* Add a symbol for the namespace. */
7328
7329 add_partial_symbol (pdi, cu);
7330
f55ee35c 7331 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7332
7333 if (pdi->has_children)
cdc07690 7334 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7335}
7336
bc30ff58
JB
7337/* Read a partial die corresponding to a subprogram and create a partial
7338 symbol for that subprogram. When the CU language allows it, this
7339 routine also defines a partial symbol for each nested subprogram
cdc07690 7340 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7341 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7342 and highest PC values found in PDI.
6e70227d 7343
cdc07690
YQ
7344 PDI may also be a lexical block, in which case we simply search
7345 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7346 Again, this is only performed when the CU language allows this
7347 type of definitions. */
7348
7349static void
7350add_partial_subprogram (struct partial_die_info *pdi,
7351 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7352 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7353{
7354 if (pdi->tag == DW_TAG_subprogram)
7355 {
7356 if (pdi->has_pc_info)
7357 {
7358 if (pdi->lowpc < *lowpc)
7359 *lowpc = pdi->lowpc;
7360 if (pdi->highpc > *highpc)
7361 *highpc = pdi->highpc;
cdc07690 7362 if (set_addrmap)
5734ee8b 7363 {
5734ee8b 7364 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7366 CORE_ADDR baseaddr;
7367 CORE_ADDR highpc;
7368 CORE_ADDR lowpc;
5734ee8b
DJ
7369
7370 baseaddr = ANOFFSET (objfile->section_offsets,
7371 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7372 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7373 pdi->lowpc + baseaddr);
7374 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7375 pdi->highpc + baseaddr);
7376 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7377 cu->per_cu->v.psymtab);
5734ee8b 7378 }
481860b3
GB
7379 }
7380
7381 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7382 {
bc30ff58 7383 if (!pdi->is_declaration)
e8d05480
JB
7384 /* Ignore subprogram DIEs that do not have a name, they are
7385 illegal. Do not emit a complaint at this point, we will
7386 do so when we convert this psymtab into a symtab. */
7387 if (pdi->name)
7388 add_partial_symbol (pdi, cu);
bc30ff58
JB
7389 }
7390 }
6e70227d 7391
bc30ff58
JB
7392 if (! pdi->has_children)
7393 return;
7394
7395 if (cu->language == language_ada)
7396 {
7397 pdi = pdi->die_child;
7398 while (pdi != NULL)
7399 {
7400 fixup_partial_die (pdi, cu);
7401 if (pdi->tag == DW_TAG_subprogram
7402 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7403 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7404 pdi = pdi->die_sibling;
7405 }
7406 }
7407}
7408
91c24f0a
DC
7409/* Read a partial die corresponding to an enumeration type. */
7410
72bf9492
DJ
7411static void
7412add_partial_enumeration (struct partial_die_info *enum_pdi,
7413 struct dwarf2_cu *cu)
91c24f0a 7414{
72bf9492 7415 struct partial_die_info *pdi;
91c24f0a
DC
7416
7417 if (enum_pdi->name != NULL)
72bf9492
DJ
7418 add_partial_symbol (enum_pdi, cu);
7419
7420 pdi = enum_pdi->die_child;
7421 while (pdi)
91c24f0a 7422 {
72bf9492 7423 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7424 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7425 else
72bf9492
DJ
7426 add_partial_symbol (pdi, cu);
7427 pdi = pdi->die_sibling;
91c24f0a 7428 }
91c24f0a
DC
7429}
7430
6caca83c
CC
7431/* Return the initial uleb128 in the die at INFO_PTR. */
7432
7433static unsigned int
d521ce57 7434peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7435{
7436 unsigned int bytes_read;
7437
7438 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7439}
7440
4bb7a0a7
DJ
7441/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7442 Return the corresponding abbrev, or NULL if the number is zero (indicating
7443 an empty DIE). In either case *BYTES_READ will be set to the length of
7444 the initial number. */
7445
7446static struct abbrev_info *
d521ce57 7447peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7448 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7449{
7450 bfd *abfd = cu->objfile->obfd;
7451 unsigned int abbrev_number;
7452 struct abbrev_info *abbrev;
7453
7454 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7455
7456 if (abbrev_number == 0)
7457 return NULL;
7458
433df2d4 7459 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7460 if (!abbrev)
7461 {
422b9917
DE
7462 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7463 " at offset 0x%x [in module %s]"),
7464 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7465 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7466 }
7467
7468 return abbrev;
7469}
7470
93311388
DE
7471/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7472 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7473 DIE. Any children of the skipped DIEs will also be skipped. */
7474
d521ce57
TT
7475static const gdb_byte *
7476skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7477{
dee91e82 7478 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7479 struct abbrev_info *abbrev;
7480 unsigned int bytes_read;
7481
7482 while (1)
7483 {
7484 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7485 if (abbrev == NULL)
7486 return info_ptr + bytes_read;
7487 else
dee91e82 7488 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7489 }
7490}
7491
93311388
DE
7492/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7493 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7494 abbrev corresponding to that skipped uleb128 should be passed in
7495 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7496 children. */
7497
d521ce57
TT
7498static const gdb_byte *
7499skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7500 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7501{
7502 unsigned int bytes_read;
7503 struct attribute attr;
dee91e82
DE
7504 bfd *abfd = reader->abfd;
7505 struct dwarf2_cu *cu = reader->cu;
d521ce57 7506 const gdb_byte *buffer = reader->buffer;
f664829e 7507 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7508 unsigned int form, i;
7509
7510 for (i = 0; i < abbrev->num_attrs; i++)
7511 {
7512 /* The only abbrev we care about is DW_AT_sibling. */
7513 if (abbrev->attrs[i].name == DW_AT_sibling)
7514 {
dee91e82 7515 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7516 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7517 complaint (&symfile_complaints,
7518 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7519 else
b9502d3f 7520 {
9c541725
PA
7521 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7522 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7523
7524 if (sibling_ptr < info_ptr)
7525 complaint (&symfile_complaints,
7526 _("DW_AT_sibling points backwards"));
22869d73
KS
7527 else if (sibling_ptr > reader->buffer_end)
7528 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7529 else
7530 return sibling_ptr;
7531 }
4bb7a0a7
DJ
7532 }
7533
7534 /* If it isn't DW_AT_sibling, skip this attribute. */
7535 form = abbrev->attrs[i].form;
7536 skip_attribute:
7537 switch (form)
7538 {
4bb7a0a7 7539 case DW_FORM_ref_addr:
ae411497
TT
7540 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7541 and later it is offset sized. */
7542 if (cu->header.version == 2)
7543 info_ptr += cu->header.addr_size;
7544 else
7545 info_ptr += cu->header.offset_size;
7546 break;
36586728
TT
7547 case DW_FORM_GNU_ref_alt:
7548 info_ptr += cu->header.offset_size;
7549 break;
ae411497 7550 case DW_FORM_addr:
4bb7a0a7
DJ
7551 info_ptr += cu->header.addr_size;
7552 break;
7553 case DW_FORM_data1:
7554 case DW_FORM_ref1:
7555 case DW_FORM_flag:
7556 info_ptr += 1;
7557 break;
2dc7f7b3 7558 case DW_FORM_flag_present:
43988095 7559 case DW_FORM_implicit_const:
2dc7f7b3 7560 break;
4bb7a0a7
DJ
7561 case DW_FORM_data2:
7562 case DW_FORM_ref2:
7563 info_ptr += 2;
7564 break;
7565 case DW_FORM_data4:
7566 case DW_FORM_ref4:
7567 info_ptr += 4;
7568 break;
7569 case DW_FORM_data8:
7570 case DW_FORM_ref8:
55f1336d 7571 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7572 info_ptr += 8;
7573 break;
0224619f
JK
7574 case DW_FORM_data16:
7575 info_ptr += 16;
7576 break;
4bb7a0a7 7577 case DW_FORM_string:
9b1c24c8 7578 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7579 info_ptr += bytes_read;
7580 break;
2dc7f7b3 7581 case DW_FORM_sec_offset:
4bb7a0a7 7582 case DW_FORM_strp:
36586728 7583 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7584 info_ptr += cu->header.offset_size;
7585 break;
2dc7f7b3 7586 case DW_FORM_exprloc:
4bb7a0a7
DJ
7587 case DW_FORM_block:
7588 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7589 info_ptr += bytes_read;
7590 break;
7591 case DW_FORM_block1:
7592 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7593 break;
7594 case DW_FORM_block2:
7595 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7596 break;
7597 case DW_FORM_block4:
7598 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7599 break;
7600 case DW_FORM_sdata:
7601 case DW_FORM_udata:
7602 case DW_FORM_ref_udata:
3019eac3
DE
7603 case DW_FORM_GNU_addr_index:
7604 case DW_FORM_GNU_str_index:
d521ce57 7605 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7606 break;
7607 case DW_FORM_indirect:
7608 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7609 info_ptr += bytes_read;
7610 /* We need to continue parsing from here, so just go back to
7611 the top. */
7612 goto skip_attribute;
7613
7614 default:
3e43a32a
MS
7615 error (_("Dwarf Error: Cannot handle %s "
7616 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7617 dwarf_form_name (form),
7618 bfd_get_filename (abfd));
7619 }
7620 }
7621
7622 if (abbrev->has_children)
dee91e82 7623 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7624 else
7625 return info_ptr;
7626}
7627
93311388 7628/* Locate ORIG_PDI's sibling.
dee91e82 7629 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7630
d521ce57 7631static const gdb_byte *
dee91e82
DE
7632locate_pdi_sibling (const struct die_reader_specs *reader,
7633 struct partial_die_info *orig_pdi,
d521ce57 7634 const gdb_byte *info_ptr)
91c24f0a
DC
7635{
7636 /* Do we know the sibling already? */
72bf9492 7637
91c24f0a
DC
7638 if (orig_pdi->sibling)
7639 return orig_pdi->sibling;
7640
7641 /* Are there any children to deal with? */
7642
7643 if (!orig_pdi->has_children)
7644 return info_ptr;
7645
4bb7a0a7 7646 /* Skip the children the long way. */
91c24f0a 7647
dee91e82 7648 return skip_children (reader, info_ptr);
91c24f0a
DC
7649}
7650
257e7a09 7651/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7652 not NULL. */
c906108c
SS
7653
7654static void
257e7a09
YQ
7655dwarf2_read_symtab (struct partial_symtab *self,
7656 struct objfile *objfile)
c906108c 7657{
257e7a09 7658 if (self->readin)
c906108c 7659 {
442e4d9c 7660 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7661 self->filename);
442e4d9c
YQ
7662 }
7663 else
7664 {
7665 if (info_verbose)
c906108c 7666 {
442e4d9c 7667 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7668 self->filename);
442e4d9c 7669 gdb_flush (gdb_stdout);
c906108c 7670 }
c906108c 7671
442e4d9c 7672 /* Restore our global data. */
9a3c8263
SM
7673 dwarf2_per_objfile
7674 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7675 dwarf2_objfile_data_key);
10b3939b 7676
442e4d9c
YQ
7677 /* If this psymtab is constructed from a debug-only objfile, the
7678 has_section_at_zero flag will not necessarily be correct. We
7679 can get the correct value for this flag by looking at the data
7680 associated with the (presumably stripped) associated objfile. */
7681 if (objfile->separate_debug_objfile_backlink)
7682 {
7683 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7684 = ((struct dwarf2_per_objfile *)
7685 objfile_data (objfile->separate_debug_objfile_backlink,
7686 dwarf2_objfile_data_key));
9a619af0 7687
442e4d9c
YQ
7688 dwarf2_per_objfile->has_section_at_zero
7689 = dpo_backlink->has_section_at_zero;
7690 }
b2ab525c 7691
442e4d9c 7692 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7693
257e7a09 7694 psymtab_to_symtab_1 (self);
c906108c 7695
442e4d9c
YQ
7696 /* Finish up the debug error message. */
7697 if (info_verbose)
7698 printf_filtered (_("done.\n"));
c906108c 7699 }
95554aad
TT
7700
7701 process_cu_includes ();
c906108c 7702}
9cdd5dbd
DE
7703\f
7704/* Reading in full CUs. */
c906108c 7705
10b3939b
DJ
7706/* Add PER_CU to the queue. */
7707
7708static void
95554aad
TT
7709queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7710 enum language pretend_language)
10b3939b
DJ
7711{
7712 struct dwarf2_queue_item *item;
7713
7714 per_cu->queued = 1;
8d749320 7715 item = XNEW (struct dwarf2_queue_item);
10b3939b 7716 item->per_cu = per_cu;
95554aad 7717 item->pretend_language = pretend_language;
10b3939b
DJ
7718 item->next = NULL;
7719
7720 if (dwarf2_queue == NULL)
7721 dwarf2_queue = item;
7722 else
7723 dwarf2_queue_tail->next = item;
7724
7725 dwarf2_queue_tail = item;
7726}
7727
89e63ee4
DE
7728/* If PER_CU is not yet queued, add it to the queue.
7729 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7730 dependency.
0907af0c 7731 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7732 meaning either PER_CU is already queued or it is already loaded.
7733
7734 N.B. There is an invariant here that if a CU is queued then it is loaded.
7735 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7736
7737static int
89e63ee4 7738maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7739 struct dwarf2_per_cu_data *per_cu,
7740 enum language pretend_language)
7741{
7742 /* We may arrive here during partial symbol reading, if we need full
7743 DIEs to process an unusual case (e.g. template arguments). Do
7744 not queue PER_CU, just tell our caller to load its DIEs. */
7745 if (dwarf2_per_objfile->reading_partial_symbols)
7746 {
7747 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7748 return 1;
7749 return 0;
7750 }
7751
7752 /* Mark the dependence relation so that we don't flush PER_CU
7753 too early. */
89e63ee4
DE
7754 if (dependent_cu != NULL)
7755 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7756
7757 /* If it's already on the queue, we have nothing to do. */
7758 if (per_cu->queued)
7759 return 0;
7760
7761 /* If the compilation unit is already loaded, just mark it as
7762 used. */
7763 if (per_cu->cu != NULL)
7764 {
7765 per_cu->cu->last_used = 0;
7766 return 0;
7767 }
7768
7769 /* Add it to the queue. */
7770 queue_comp_unit (per_cu, pretend_language);
7771
7772 return 1;
7773}
7774
10b3939b
DJ
7775/* Process the queue. */
7776
7777static void
a0f42c21 7778process_queue (void)
10b3939b
DJ
7779{
7780 struct dwarf2_queue_item *item, *next_item;
7781
b4f54984 7782 if (dwarf_read_debug)
45cfd468
DE
7783 {
7784 fprintf_unfiltered (gdb_stdlog,
7785 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7786 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7787 }
7788
03dd20cc
DJ
7789 /* The queue starts out with one item, but following a DIE reference
7790 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7791 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7792 {
cc12ce38
DE
7793 if ((dwarf2_per_objfile->using_index
7794 ? !item->per_cu->v.quick->compunit_symtab
7795 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7796 /* Skip dummy CUs. */
7797 && item->per_cu->cu != NULL)
f4dc4d17
DE
7798 {
7799 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7800 unsigned int debug_print_threshold;
247f5c4f 7801 char buf[100];
f4dc4d17 7802
247f5c4f 7803 if (per_cu->is_debug_types)
f4dc4d17 7804 {
247f5c4f
DE
7805 struct signatured_type *sig_type =
7806 (struct signatured_type *) per_cu;
7807
7808 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7809 hex_string (sig_type->signature),
9c541725 7810 to_underlying (per_cu->sect_off));
73be47f5
DE
7811 /* There can be 100s of TUs.
7812 Only print them in verbose mode. */
7813 debug_print_threshold = 2;
f4dc4d17 7814 }
247f5c4f 7815 else
73be47f5 7816 {
9c541725
PA
7817 sprintf (buf, "CU at offset 0x%x",
7818 to_underlying (per_cu->sect_off));
73be47f5
DE
7819 debug_print_threshold = 1;
7820 }
247f5c4f 7821
b4f54984 7822 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7823 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7824
7825 if (per_cu->is_debug_types)
7826 process_full_type_unit (per_cu, item->pretend_language);
7827 else
7828 process_full_comp_unit (per_cu, item->pretend_language);
7829
b4f54984 7830 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7831 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7832 }
10b3939b
DJ
7833
7834 item->per_cu->queued = 0;
7835 next_item = item->next;
7836 xfree (item);
7837 }
7838
7839 dwarf2_queue_tail = NULL;
45cfd468 7840
b4f54984 7841 if (dwarf_read_debug)
45cfd468
DE
7842 {
7843 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7844 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7845 }
10b3939b
DJ
7846}
7847
7848/* Free all allocated queue entries. This function only releases anything if
7849 an error was thrown; if the queue was processed then it would have been
7850 freed as we went along. */
7851
7852static void
7853dwarf2_release_queue (void *dummy)
7854{
7855 struct dwarf2_queue_item *item, *last;
7856
7857 item = dwarf2_queue;
7858 while (item)
7859 {
7860 /* Anything still marked queued is likely to be in an
7861 inconsistent state, so discard it. */
7862 if (item->per_cu->queued)
7863 {
7864 if (item->per_cu->cu != NULL)
dee91e82 7865 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7866 item->per_cu->queued = 0;
7867 }
7868
7869 last = item;
7870 item = item->next;
7871 xfree (last);
7872 }
7873
7874 dwarf2_queue = dwarf2_queue_tail = NULL;
7875}
7876
7877/* Read in full symbols for PST, and anything it depends on. */
7878
c906108c 7879static void
fba45db2 7880psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7881{
10b3939b 7882 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7883 int i;
7884
95554aad
TT
7885 if (pst->readin)
7886 return;
7887
aaa75496 7888 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7889 if (!pst->dependencies[i]->readin
7890 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7891 {
7892 /* Inform about additional files that need to be read in. */
7893 if (info_verbose)
7894 {
a3f17187 7895 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7896 fputs_filtered (" ", gdb_stdout);
7897 wrap_here ("");
7898 fputs_filtered ("and ", gdb_stdout);
7899 wrap_here ("");
7900 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7901 wrap_here (""); /* Flush output. */
aaa75496
JB
7902 gdb_flush (gdb_stdout);
7903 }
7904 psymtab_to_symtab_1 (pst->dependencies[i]);
7905 }
7906
9a3c8263 7907 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7908
7909 if (per_cu == NULL)
aaa75496
JB
7910 {
7911 /* It's an include file, no symbols to read for it.
7912 Everything is in the parent symtab. */
7913 pst->readin = 1;
7914 return;
7915 }
c906108c 7916
a0f42c21 7917 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7918}
7919
dee91e82
DE
7920/* Trivial hash function for die_info: the hash value of a DIE
7921 is its offset in .debug_info for this objfile. */
10b3939b 7922
dee91e82
DE
7923static hashval_t
7924die_hash (const void *item)
10b3939b 7925{
9a3c8263 7926 const struct die_info *die = (const struct die_info *) item;
6502dd73 7927
9c541725 7928 return to_underlying (die->sect_off);
dee91e82 7929}
63d06c5c 7930
dee91e82
DE
7931/* Trivial comparison function for die_info structures: two DIEs
7932 are equal if they have the same offset. */
98bfdba5 7933
dee91e82
DE
7934static int
7935die_eq (const void *item_lhs, const void *item_rhs)
7936{
9a3c8263
SM
7937 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7938 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7939
9c541725 7940 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7941}
c906108c 7942
dee91e82
DE
7943/* die_reader_func for load_full_comp_unit.
7944 This is identical to read_signatured_type_reader,
7945 but is kept separate for now. */
c906108c 7946
dee91e82
DE
7947static void
7948load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7949 const gdb_byte *info_ptr,
dee91e82
DE
7950 struct die_info *comp_unit_die,
7951 int has_children,
7952 void *data)
7953{
7954 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7955 enum language *language_ptr = (enum language *) data;
6caca83c 7956
dee91e82
DE
7957 gdb_assert (cu->die_hash == NULL);
7958 cu->die_hash =
7959 htab_create_alloc_ex (cu->header.length / 12,
7960 die_hash,
7961 die_eq,
7962 NULL,
7963 &cu->comp_unit_obstack,
7964 hashtab_obstack_allocate,
7965 dummy_obstack_deallocate);
e142c38c 7966
dee91e82
DE
7967 if (has_children)
7968 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7969 &info_ptr, comp_unit_die);
7970 cu->dies = comp_unit_die;
7971 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7972
7973 /* We try not to read any attributes in this function, because not
9cdd5dbd 7974 all CUs needed for references have been loaded yet, and symbol
10b3939b 7975 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7976 or we won't be able to build types correctly.
7977 Similarly, if we do not read the producer, we can not apply
7978 producer-specific interpretation. */
95554aad 7979 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7980}
10b3939b 7981
dee91e82 7982/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7983
dee91e82 7984static void
95554aad
TT
7985load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7986 enum language pretend_language)
dee91e82 7987{
3019eac3 7988 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7989
f4dc4d17
DE
7990 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7991 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7992}
7993
3da10d80
KS
7994/* Add a DIE to the delayed physname list. */
7995
7996static void
7997add_to_method_list (struct type *type, int fnfield_index, int index,
7998 const char *name, struct die_info *die,
7999 struct dwarf2_cu *cu)
8000{
8001 struct delayed_method_info mi;
8002 mi.type = type;
8003 mi.fnfield_index = fnfield_index;
8004 mi.index = index;
8005 mi.name = name;
8006 mi.die = die;
8007 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8008}
8009
8010/* A cleanup for freeing the delayed method list. */
8011
8012static void
8013free_delayed_list (void *ptr)
8014{
8015 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8016 if (cu->method_list != NULL)
8017 {
8018 VEC_free (delayed_method_info, cu->method_list);
8019 cu->method_list = NULL;
8020 }
8021}
8022
3693fdb3
PA
8023/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8024 "const" / "volatile". If so, decrements LEN by the length of the
8025 modifier and return true. Otherwise return false. */
8026
8027template<size_t N>
8028static bool
8029check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8030{
8031 size_t mod_len = sizeof (mod) - 1;
8032 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8033 {
8034 len -= mod_len;
8035 return true;
8036 }
8037 return false;
8038}
8039
3da10d80
KS
8040/* Compute the physnames of any methods on the CU's method list.
8041
8042 The computation of method physnames is delayed in order to avoid the
8043 (bad) condition that one of the method's formal parameters is of an as yet
8044 incomplete type. */
8045
8046static void
8047compute_delayed_physnames (struct dwarf2_cu *cu)
8048{
8049 int i;
8050 struct delayed_method_info *mi;
3693fdb3
PA
8051
8052 /* Only C++ delays computing physnames. */
8053 if (VEC_empty (delayed_method_info, cu->method_list))
8054 return;
8055 gdb_assert (cu->language == language_cplus);
8056
3da10d80
KS
8057 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8058 {
1d06ead6 8059 const char *physname;
3da10d80
KS
8060 struct fn_fieldlist *fn_flp
8061 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8062 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8063 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8064 = physname ? physname : "";
3693fdb3
PA
8065
8066 /* Since there's no tag to indicate whether a method is a
8067 const/volatile overload, extract that information out of the
8068 demangled name. */
8069 if (physname != NULL)
8070 {
8071 size_t len = strlen (physname);
8072
8073 while (1)
8074 {
8075 if (physname[len] == ')') /* shortcut */
8076 break;
8077 else if (check_modifier (physname, len, " const"))
8078 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8079 else if (check_modifier (physname, len, " volatile"))
8080 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8081 else
8082 break;
8083 }
8084 }
3da10d80
KS
8085 }
8086}
8087
a766d390
DE
8088/* Go objects should be embedded in a DW_TAG_module DIE,
8089 and it's not clear if/how imported objects will appear.
8090 To keep Go support simple until that's worked out,
8091 go back through what we've read and create something usable.
8092 We could do this while processing each DIE, and feels kinda cleaner,
8093 but that way is more invasive.
8094 This is to, for example, allow the user to type "p var" or "b main"
8095 without having to specify the package name, and allow lookups
8096 of module.object to work in contexts that use the expression
8097 parser. */
8098
8099static void
8100fixup_go_packaging (struct dwarf2_cu *cu)
8101{
8102 char *package_name = NULL;
8103 struct pending *list;
8104 int i;
8105
8106 for (list = global_symbols; list != NULL; list = list->next)
8107 {
8108 for (i = 0; i < list->nsyms; ++i)
8109 {
8110 struct symbol *sym = list->symbol[i];
8111
8112 if (SYMBOL_LANGUAGE (sym) == language_go
8113 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8114 {
8115 char *this_package_name = go_symbol_package_name (sym);
8116
8117 if (this_package_name == NULL)
8118 continue;
8119 if (package_name == NULL)
8120 package_name = this_package_name;
8121 else
8122 {
8123 if (strcmp (package_name, this_package_name) != 0)
8124 complaint (&symfile_complaints,
8125 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8126 (symbol_symtab (sym) != NULL
8127 ? symtab_to_filename_for_display
8128 (symbol_symtab (sym))
4262abfb 8129 : objfile_name (cu->objfile)),
a766d390
DE
8130 this_package_name, package_name);
8131 xfree (this_package_name);
8132 }
8133 }
8134 }
8135 }
8136
8137 if (package_name != NULL)
8138 {
8139 struct objfile *objfile = cu->objfile;
34a68019 8140 const char *saved_package_name
224c3ddb
SM
8141 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8142 package_name,
8143 strlen (package_name));
19f392bc
UW
8144 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8145 saved_package_name);
a766d390
DE
8146 struct symbol *sym;
8147
8148 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8149
e623cf5d 8150 sym = allocate_symbol (objfile);
f85f34ed 8151 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8152 SYMBOL_SET_NAMES (sym, saved_package_name,
8153 strlen (saved_package_name), 0, objfile);
a766d390
DE
8154 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8155 e.g., "main" finds the "main" module and not C's main(). */
8156 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8157 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8158 SYMBOL_TYPE (sym) = type;
8159
8160 add_symbol_to_list (sym, &global_symbols);
8161
8162 xfree (package_name);
8163 }
8164}
8165
95554aad
TT
8166/* Return the symtab for PER_CU. This works properly regardless of
8167 whether we're using the index or psymtabs. */
8168
43f3e411
DE
8169static struct compunit_symtab *
8170get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8171{
8172 return (dwarf2_per_objfile->using_index
43f3e411
DE
8173 ? per_cu->v.quick->compunit_symtab
8174 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8175}
8176
8177/* A helper function for computing the list of all symbol tables
8178 included by PER_CU. */
8179
8180static void
43f3e411 8181recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8182 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8183 struct dwarf2_per_cu_data *per_cu,
43f3e411 8184 struct compunit_symtab *immediate_parent)
95554aad
TT
8185{
8186 void **slot;
8187 int ix;
43f3e411 8188 struct compunit_symtab *cust;
95554aad
TT
8189 struct dwarf2_per_cu_data *iter;
8190
8191 slot = htab_find_slot (all_children, per_cu, INSERT);
8192 if (*slot != NULL)
8193 {
8194 /* This inclusion and its children have been processed. */
8195 return;
8196 }
8197
8198 *slot = per_cu;
8199 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8200 cust = get_compunit_symtab (per_cu);
8201 if (cust != NULL)
ec94af83
DE
8202 {
8203 /* If this is a type unit only add its symbol table if we haven't
8204 seen it yet (type unit per_cu's can share symtabs). */
8205 if (per_cu->is_debug_types)
8206 {
43f3e411 8207 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8208 if (*slot == NULL)
8209 {
43f3e411
DE
8210 *slot = cust;
8211 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8212 if (cust->user == NULL)
8213 cust->user = immediate_parent;
ec94af83
DE
8214 }
8215 }
8216 else
f9125b6c 8217 {
43f3e411
DE
8218 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8219 if (cust->user == NULL)
8220 cust->user = immediate_parent;
f9125b6c 8221 }
ec94af83 8222 }
95554aad
TT
8223
8224 for (ix = 0;
796a7ff8 8225 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8226 ++ix)
ec94af83
DE
8227 {
8228 recursively_compute_inclusions (result, all_children,
43f3e411 8229 all_type_symtabs, iter, cust);
ec94af83 8230 }
95554aad
TT
8231}
8232
43f3e411 8233/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8234 PER_CU. */
8235
8236static void
43f3e411 8237compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8238{
f4dc4d17
DE
8239 gdb_assert (! per_cu->is_debug_types);
8240
796a7ff8 8241 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8242 {
8243 int ix, len;
ec94af83 8244 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8245 struct compunit_symtab *compunit_symtab_iter;
8246 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8247 htab_t all_children, all_type_symtabs;
43f3e411 8248 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8249
8250 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8251 if (cust == NULL)
95554aad
TT
8252 return;
8253
8254 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8255 NULL, xcalloc, xfree);
ec94af83
DE
8256 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8257 NULL, xcalloc, xfree);
95554aad
TT
8258
8259 for (ix = 0;
796a7ff8 8260 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8261 ix, per_cu_iter);
95554aad 8262 ++ix)
ec94af83
DE
8263 {
8264 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8265 all_type_symtabs, per_cu_iter,
43f3e411 8266 cust);
ec94af83 8267 }
95554aad 8268
ec94af83 8269 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8270 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8271 cust->includes
8d749320
SM
8272 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8273 struct compunit_symtab *, len + 1);
95554aad 8274 for (ix = 0;
43f3e411
DE
8275 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8276 compunit_symtab_iter);
95554aad 8277 ++ix)
43f3e411
DE
8278 cust->includes[ix] = compunit_symtab_iter;
8279 cust->includes[len] = NULL;
95554aad 8280
43f3e411 8281 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8282 htab_delete (all_children);
ec94af83 8283 htab_delete (all_type_symtabs);
95554aad
TT
8284 }
8285}
8286
8287/* Compute the 'includes' field for the symtabs of all the CUs we just
8288 read. */
8289
8290static void
8291process_cu_includes (void)
8292{
8293 int ix;
8294 struct dwarf2_per_cu_data *iter;
8295
8296 for (ix = 0;
8297 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8298 ix, iter);
8299 ++ix)
f4dc4d17
DE
8300 {
8301 if (! iter->is_debug_types)
43f3e411 8302 compute_compunit_symtab_includes (iter);
f4dc4d17 8303 }
95554aad
TT
8304
8305 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8306}
8307
9cdd5dbd 8308/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8309 already been loaded into memory. */
8310
8311static void
95554aad
TT
8312process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8313 enum language pretend_language)
10b3939b 8314{
10b3939b 8315 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8316 struct objfile *objfile = per_cu->objfile;
3e29f34a 8317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8318 CORE_ADDR lowpc, highpc;
43f3e411 8319 struct compunit_symtab *cust;
3da10d80 8320 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8321 CORE_ADDR baseaddr;
4359dff1 8322 struct block *static_block;
3e29f34a 8323 CORE_ADDR addr;
10b3939b
DJ
8324
8325 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8326
10b3939b
DJ
8327 buildsym_init ();
8328 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8329 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8330
8331 cu->list_in_scope = &file_symbols;
c906108c 8332
95554aad
TT
8333 cu->language = pretend_language;
8334 cu->language_defn = language_def (cu->language);
8335
c906108c 8336 /* Do line number decoding in read_file_scope () */
10b3939b 8337 process_die (cu->dies, cu);
c906108c 8338
a766d390
DE
8339 /* For now fudge the Go package. */
8340 if (cu->language == language_go)
8341 fixup_go_packaging (cu);
8342
3da10d80
KS
8343 /* Now that we have processed all the DIEs in the CU, all the types
8344 should be complete, and it should now be safe to compute all of the
8345 physnames. */
8346 compute_delayed_physnames (cu);
8347 do_cleanups (delayed_list_cleanup);
8348
fae299cd
DC
8349 /* Some compilers don't define a DW_AT_high_pc attribute for the
8350 compilation unit. If the DW_AT_high_pc is missing, synthesize
8351 it, by scanning the DIE's below the compilation unit. */
10b3939b 8352 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8353
3e29f34a
MR
8354 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8355 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8356
8357 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8358 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8359 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8360 addrmap to help ensure it has an accurate map of pc values belonging to
8361 this comp unit. */
8362 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8363
43f3e411
DE
8364 cust = end_symtab_from_static_block (static_block,
8365 SECT_OFF_TEXT (objfile), 0);
c906108c 8366
43f3e411 8367 if (cust != NULL)
c906108c 8368 {
df15bd07 8369 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8370
8be455d7
JK
8371 /* Set symtab language to language from DW_AT_language. If the
8372 compilation is from a C file generated by language preprocessors, do
8373 not set the language if it was already deduced by start_subfile. */
43f3e411 8374 if (!(cu->language == language_c
40e3ad0e 8375 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8376 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8377
8378 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8379 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8380 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8381 there were bugs in prologue debug info, fixed later in GCC-4.5
8382 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8383
8384 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8385 needed, it would be wrong due to missing DW_AT_producer there.
8386
8387 Still one can confuse GDB by using non-standard GCC compilation
8388 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8389 */
ab260dad 8390 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8391 cust->locations_valid = 1;
e0d00bc7
JK
8392
8393 if (gcc_4_minor >= 5)
43f3e411 8394 cust->epilogue_unwind_valid = 1;
96408a79 8395
43f3e411 8396 cust->call_site_htab = cu->call_site_htab;
c906108c 8397 }
9291a0cd
TT
8398
8399 if (dwarf2_per_objfile->using_index)
43f3e411 8400 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8401 else
8402 {
8403 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8404 pst->compunit_symtab = cust;
9291a0cd
TT
8405 pst->readin = 1;
8406 }
c906108c 8407
95554aad
TT
8408 /* Push it for inclusion processing later. */
8409 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8410
c906108c 8411 do_cleanups (back_to);
f4dc4d17 8412}
45cfd468 8413
f4dc4d17
DE
8414/* Generate full symbol information for type unit PER_CU, whose DIEs have
8415 already been loaded into memory. */
8416
8417static void
8418process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8419 enum language pretend_language)
8420{
8421 struct dwarf2_cu *cu = per_cu->cu;
8422 struct objfile *objfile = per_cu->objfile;
43f3e411 8423 struct compunit_symtab *cust;
f4dc4d17 8424 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8425 struct signatured_type *sig_type;
8426
8427 gdb_assert (per_cu->is_debug_types);
8428 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8429
8430 buildsym_init ();
8431 back_to = make_cleanup (really_free_pendings, NULL);
8432 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8433
8434 cu->list_in_scope = &file_symbols;
8435
8436 cu->language = pretend_language;
8437 cu->language_defn = language_def (cu->language);
8438
8439 /* The symbol tables are set up in read_type_unit_scope. */
8440 process_die (cu->dies, cu);
8441
8442 /* For now fudge the Go package. */
8443 if (cu->language == language_go)
8444 fixup_go_packaging (cu);
8445
8446 /* Now that we have processed all the DIEs in the CU, all the types
8447 should be complete, and it should now be safe to compute all of the
8448 physnames. */
8449 compute_delayed_physnames (cu);
8450 do_cleanups (delayed_list_cleanup);
8451
8452 /* TUs share symbol tables.
8453 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8454 of it with end_expandable_symtab. Otherwise, complete the addition of
8455 this TU's symbols to the existing symtab. */
43f3e411 8456 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8457 {
43f3e411
DE
8458 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8459 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8460
43f3e411 8461 if (cust != NULL)
f4dc4d17
DE
8462 {
8463 /* Set symtab language to language from DW_AT_language. If the
8464 compilation is from a C file generated by language preprocessors,
8465 do not set the language if it was already deduced by
8466 start_subfile. */
43f3e411
DE
8467 if (!(cu->language == language_c
8468 && COMPUNIT_FILETABS (cust)->language != language_c))
8469 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8470 }
8471 }
8472 else
8473 {
0ab9ce85 8474 augment_type_symtab ();
43f3e411 8475 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8476 }
8477
8478 if (dwarf2_per_objfile->using_index)
43f3e411 8479 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8480 else
8481 {
8482 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8483 pst->compunit_symtab = cust;
f4dc4d17 8484 pst->readin = 1;
45cfd468 8485 }
f4dc4d17
DE
8486
8487 do_cleanups (back_to);
c906108c
SS
8488}
8489
95554aad
TT
8490/* Process an imported unit DIE. */
8491
8492static void
8493process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8494{
8495 struct attribute *attr;
8496
f4dc4d17
DE
8497 /* For now we don't handle imported units in type units. */
8498 if (cu->per_cu->is_debug_types)
8499 {
8500 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8501 " supported in type units [in module %s]"),
4262abfb 8502 objfile_name (cu->objfile));
f4dc4d17
DE
8503 }
8504
95554aad
TT
8505 attr = dwarf2_attr (die, DW_AT_import, cu);
8506 if (attr != NULL)
8507 {
9c541725
PA
8508 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8509 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8510 dwarf2_per_cu_data *per_cu
8511 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8512
69d751e3 8513 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8514 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8515 load_full_comp_unit (per_cu, cu->language);
8516
796a7ff8 8517 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8518 per_cu);
8519 }
8520}
8521
4c8aa72d
PA
8522/* RAII object that represents a process_die scope: i.e.,
8523 starts/finishes processing a DIE. */
8524class process_die_scope
adde2bff 8525{
4c8aa72d
PA
8526public:
8527 process_die_scope (die_info *die, dwarf2_cu *cu)
8528 : m_die (die), m_cu (cu)
8529 {
8530 /* We should only be processing DIEs not already in process. */
8531 gdb_assert (!m_die->in_process);
8532 m_die->in_process = true;
8533 }
8c3cb9fa 8534
4c8aa72d
PA
8535 ~process_die_scope ()
8536 {
8537 m_die->in_process = false;
8538
8539 /* If we're done processing the DIE for the CU that owns the line
8540 header, we don't need the line header anymore. */
8541 if (m_cu->line_header_die_owner == m_die)
8542 {
8543 delete m_cu->line_header;
8544 m_cu->line_header = NULL;
8545 m_cu->line_header_die_owner = NULL;
8546 }
8547 }
8548
8549private:
8550 die_info *m_die;
8551 dwarf2_cu *m_cu;
8552};
adde2bff 8553
c906108c
SS
8554/* Process a die and its children. */
8555
8556static void
e7c27a73 8557process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8558{
4c8aa72d 8559 process_die_scope scope (die, cu);
adde2bff 8560
c906108c
SS
8561 switch (die->tag)
8562 {
8563 case DW_TAG_padding:
8564 break;
8565 case DW_TAG_compile_unit:
95554aad 8566 case DW_TAG_partial_unit:
e7c27a73 8567 read_file_scope (die, cu);
c906108c 8568 break;
348e048f
DE
8569 case DW_TAG_type_unit:
8570 read_type_unit_scope (die, cu);
8571 break;
c906108c 8572 case DW_TAG_subprogram:
c906108c 8573 case DW_TAG_inlined_subroutine:
edb3359d 8574 read_func_scope (die, cu);
c906108c
SS
8575 break;
8576 case DW_TAG_lexical_block:
14898363
L
8577 case DW_TAG_try_block:
8578 case DW_TAG_catch_block:
e7c27a73 8579 read_lexical_block_scope (die, cu);
c906108c 8580 break;
216f72a1 8581 case DW_TAG_call_site:
96408a79
SA
8582 case DW_TAG_GNU_call_site:
8583 read_call_site_scope (die, cu);
8584 break;
c906108c 8585 case DW_TAG_class_type:
680b30c7 8586 case DW_TAG_interface_type:
c906108c
SS
8587 case DW_TAG_structure_type:
8588 case DW_TAG_union_type:
134d01f1 8589 process_structure_scope (die, cu);
c906108c
SS
8590 break;
8591 case DW_TAG_enumeration_type:
134d01f1 8592 process_enumeration_scope (die, cu);
c906108c 8593 break;
134d01f1 8594
f792889a
DJ
8595 /* These dies have a type, but processing them does not create
8596 a symbol or recurse to process the children. Therefore we can
8597 read them on-demand through read_type_die. */
c906108c 8598 case DW_TAG_subroutine_type:
72019c9c 8599 case DW_TAG_set_type:
c906108c 8600 case DW_TAG_array_type:
c906108c 8601 case DW_TAG_pointer_type:
c906108c 8602 case DW_TAG_ptr_to_member_type:
c906108c 8603 case DW_TAG_reference_type:
4297a3f0 8604 case DW_TAG_rvalue_reference_type:
c906108c 8605 case DW_TAG_string_type:
c906108c 8606 break;
134d01f1 8607
c906108c 8608 case DW_TAG_base_type:
a02abb62 8609 case DW_TAG_subrange_type:
cb249c71 8610 case DW_TAG_typedef:
134d01f1
DJ
8611 /* Add a typedef symbol for the type definition, if it has a
8612 DW_AT_name. */
f792889a 8613 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8614 break;
c906108c 8615 case DW_TAG_common_block:
e7c27a73 8616 read_common_block (die, cu);
c906108c
SS
8617 break;
8618 case DW_TAG_common_inclusion:
8619 break;
d9fa45fe 8620 case DW_TAG_namespace:
4d4ec4e5 8621 cu->processing_has_namespace_info = 1;
e7c27a73 8622 read_namespace (die, cu);
d9fa45fe 8623 break;
5d7cb8df 8624 case DW_TAG_module:
4d4ec4e5 8625 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8626 read_module (die, cu);
8627 break;
d9fa45fe 8628 case DW_TAG_imported_declaration:
74921315
KS
8629 cu->processing_has_namespace_info = 1;
8630 if (read_namespace_alias (die, cu))
8631 break;
8632 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8633 case DW_TAG_imported_module:
4d4ec4e5 8634 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8635 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8636 || cu->language != language_fortran))
8637 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8638 dwarf_tag_name (die->tag));
8639 read_import_statement (die, cu);
d9fa45fe 8640 break;
95554aad
TT
8641
8642 case DW_TAG_imported_unit:
8643 process_imported_unit_die (die, cu);
8644 break;
8645
c906108c 8646 default:
e7c27a73 8647 new_symbol (die, NULL, cu);
c906108c
SS
8648 break;
8649 }
8650}
ca69b9e6
DE
8651\f
8652/* DWARF name computation. */
c906108c 8653
94af9270
KS
8654/* A helper function for dwarf2_compute_name which determines whether DIE
8655 needs to have the name of the scope prepended to the name listed in the
8656 die. */
8657
8658static int
8659die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8660{
1c809c68
TT
8661 struct attribute *attr;
8662
94af9270
KS
8663 switch (die->tag)
8664 {
8665 case DW_TAG_namespace:
8666 case DW_TAG_typedef:
8667 case DW_TAG_class_type:
8668 case DW_TAG_interface_type:
8669 case DW_TAG_structure_type:
8670 case DW_TAG_union_type:
8671 case DW_TAG_enumeration_type:
8672 case DW_TAG_enumerator:
8673 case DW_TAG_subprogram:
08a76f8a 8674 case DW_TAG_inlined_subroutine:
94af9270 8675 case DW_TAG_member:
74921315 8676 case DW_TAG_imported_declaration:
94af9270
KS
8677 return 1;
8678
8679 case DW_TAG_variable:
c2b0a229 8680 case DW_TAG_constant:
94af9270
KS
8681 /* We only need to prefix "globally" visible variables. These include
8682 any variable marked with DW_AT_external or any variable that
8683 lives in a namespace. [Variables in anonymous namespaces
8684 require prefixing, but they are not DW_AT_external.] */
8685
8686 if (dwarf2_attr (die, DW_AT_specification, cu))
8687 {
8688 struct dwarf2_cu *spec_cu = cu;
9a619af0 8689
94af9270
KS
8690 return die_needs_namespace (die_specification (die, &spec_cu),
8691 spec_cu);
8692 }
8693
1c809c68 8694 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8695 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8696 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8697 return 0;
8698 /* A variable in a lexical block of some kind does not need a
8699 namespace, even though in C++ such variables may be external
8700 and have a mangled name. */
8701 if (die->parent->tag == DW_TAG_lexical_block
8702 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8703 || die->parent->tag == DW_TAG_catch_block
8704 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8705 return 0;
8706 return 1;
94af9270
KS
8707
8708 default:
8709 return 0;
8710 }
8711}
8712
73b9be8b
KS
8713/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8714 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8715 defined for the given DIE. */
8716
8717static struct attribute *
8718dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8719{
8720 struct attribute *attr;
8721
8722 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8723 if (attr == NULL)
8724 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8725
8726 return attr;
8727}
8728
8729/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8730 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8731 defined for the given DIE. */
8732
8733static const char *
8734dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8735{
8736 const char *linkage_name;
8737
8738 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8739 if (linkage_name == NULL)
8740 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8741
8742 return linkage_name;
8743}
8744
94af9270 8745/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8746 compute the physname for the object, which include a method's:
9c37b5ae 8747 - formal parameters (C++),
a766d390 8748 - receiver type (Go),
a766d390
DE
8749
8750 The term "physname" is a bit confusing.
8751 For C++, for example, it is the demangled name.
8752 For Go, for example, it's the mangled name.
94af9270 8753
af6b7be1
JB
8754 For Ada, return the DIE's linkage name rather than the fully qualified
8755 name. PHYSNAME is ignored..
8756
94af9270
KS
8757 The result is allocated on the objfile_obstack and canonicalized. */
8758
8759static const char *
15d034d0
TT
8760dwarf2_compute_name (const char *name,
8761 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8762 int physname)
8763{
bb5ed363
DE
8764 struct objfile *objfile = cu->objfile;
8765
94af9270
KS
8766 if (name == NULL)
8767 name = dwarf2_name (die, cu);
8768
2ee7123e
DE
8769 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8770 but otherwise compute it by typename_concat inside GDB.
8771 FIXME: Actually this is not really true, or at least not always true.
8772 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8773 Fortran names because there is no mangling standard. So new_symbol_full
8774 will set the demangled name to the result of dwarf2_full_name, and it is
8775 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8776 if (cu->language == language_ada
8777 || (cu->language == language_fortran && physname))
8778 {
8779 /* For Ada unit, we prefer the linkage name over the name, as
8780 the former contains the exported name, which the user expects
8781 to be able to reference. Ideally, we want the user to be able
8782 to reference this entity using either natural or linkage name,
8783 but we haven't started looking at this enhancement yet. */
73b9be8b 8784 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 8785
2ee7123e
DE
8786 if (linkage_name != NULL)
8787 return linkage_name;
f55ee35c
JK
8788 }
8789
94af9270
KS
8790 /* These are the only languages we know how to qualify names in. */
8791 if (name != NULL
9c37b5ae 8792 && (cu->language == language_cplus
c44af4eb
TT
8793 || cu->language == language_fortran || cu->language == language_d
8794 || cu->language == language_rust))
94af9270
KS
8795 {
8796 if (die_needs_namespace (die, cu))
8797 {
8798 long length;
0d5cff50 8799 const char *prefix;
34a68019 8800 const char *canonical_name = NULL;
94af9270 8801
d7e74731
PA
8802 string_file buf;
8803
94af9270 8804 prefix = determine_prefix (die, cu);
94af9270
KS
8805 if (*prefix != '\0')
8806 {
f55ee35c
JK
8807 char *prefixed_name = typename_concat (NULL, prefix, name,
8808 physname, cu);
9a619af0 8809
d7e74731 8810 buf.puts (prefixed_name);
94af9270
KS
8811 xfree (prefixed_name);
8812 }
8813 else
d7e74731 8814 buf.puts (name);
94af9270 8815
98bfdba5
PA
8816 /* Template parameters may be specified in the DIE's DW_AT_name, or
8817 as children with DW_TAG_template_type_param or
8818 DW_TAG_value_type_param. If the latter, add them to the name
8819 here. If the name already has template parameters, then
8820 skip this step; some versions of GCC emit both, and
8821 it is more efficient to use the pre-computed name.
8822
8823 Something to keep in mind about this process: it is very
8824 unlikely, or in some cases downright impossible, to produce
8825 something that will match the mangled name of a function.
8826 If the definition of the function has the same debug info,
8827 we should be able to match up with it anyway. But fallbacks
8828 using the minimal symbol, for instance to find a method
8829 implemented in a stripped copy of libstdc++, will not work.
8830 If we do not have debug info for the definition, we will have to
8831 match them up some other way.
8832
8833 When we do name matching there is a related problem with function
8834 templates; two instantiated function templates are allowed to
8835 differ only by their return types, which we do not add here. */
8836
8837 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8838 {
8839 struct attribute *attr;
8840 struct die_info *child;
8841 int first = 1;
8842
8843 die->building_fullname = 1;
8844
8845 for (child = die->child; child != NULL; child = child->sibling)
8846 {
8847 struct type *type;
12df843f 8848 LONGEST value;
d521ce57 8849 const gdb_byte *bytes;
98bfdba5
PA
8850 struct dwarf2_locexpr_baton *baton;
8851 struct value *v;
8852
8853 if (child->tag != DW_TAG_template_type_param
8854 && child->tag != DW_TAG_template_value_param)
8855 continue;
8856
8857 if (first)
8858 {
d7e74731 8859 buf.puts ("<");
98bfdba5
PA
8860 first = 0;
8861 }
8862 else
d7e74731 8863 buf.puts (", ");
98bfdba5
PA
8864
8865 attr = dwarf2_attr (child, DW_AT_type, cu);
8866 if (attr == NULL)
8867 {
8868 complaint (&symfile_complaints,
8869 _("template parameter missing DW_AT_type"));
d7e74731 8870 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8871 continue;
8872 }
8873 type = die_type (child, cu);
8874
8875 if (child->tag == DW_TAG_template_type_param)
8876 {
d7e74731 8877 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8878 continue;
8879 }
8880
8881 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8882 if (attr == NULL)
8883 {
8884 complaint (&symfile_complaints,
3e43a32a
MS
8885 _("template parameter missing "
8886 "DW_AT_const_value"));
d7e74731 8887 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8888 continue;
8889 }
8890
8891 dwarf2_const_value_attr (attr, type, name,
8892 &cu->comp_unit_obstack, cu,
8893 &value, &bytes, &baton);
8894
8895 if (TYPE_NOSIGN (type))
8896 /* GDB prints characters as NUMBER 'CHAR'. If that's
8897 changed, this can use value_print instead. */
d7e74731 8898 c_printchar (value, type, &buf);
98bfdba5
PA
8899 else
8900 {
8901 struct value_print_options opts;
8902
8903 if (baton != NULL)
8904 v = dwarf2_evaluate_loc_desc (type, NULL,
8905 baton->data,
8906 baton->size,
8907 baton->per_cu);
8908 else if (bytes != NULL)
8909 {
8910 v = allocate_value (type);
8911 memcpy (value_contents_writeable (v), bytes,
8912 TYPE_LENGTH (type));
8913 }
8914 else
8915 v = value_from_longest (type, value);
8916
3e43a32a
MS
8917 /* Specify decimal so that we do not depend on
8918 the radix. */
98bfdba5
PA
8919 get_formatted_print_options (&opts, 'd');
8920 opts.raw = 1;
d7e74731 8921 value_print (v, &buf, &opts);
98bfdba5
PA
8922 release_value (v);
8923 value_free (v);
8924 }
8925 }
8926
8927 die->building_fullname = 0;
8928
8929 if (!first)
8930 {
8931 /* Close the argument list, with a space if necessary
8932 (nested templates). */
d7e74731
PA
8933 if (!buf.empty () && buf.string ().back () == '>')
8934 buf.puts (" >");
98bfdba5 8935 else
d7e74731 8936 buf.puts (">");
98bfdba5
PA
8937 }
8938 }
8939
9c37b5ae 8940 /* For C++ methods, append formal parameter type
94af9270 8941 information, if PHYSNAME. */
6e70227d 8942
94af9270 8943 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8944 && cu->language == language_cplus)
94af9270
KS
8945 {
8946 struct type *type = read_type_die (die, cu);
8947
d7e74731 8948 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8949 &type_print_raw_options);
94af9270 8950
9c37b5ae 8951 if (cu->language == language_cplus)
94af9270 8952 {
60430eff
DJ
8953 /* Assume that an artificial first parameter is
8954 "this", but do not crash if it is not. RealView
8955 marks unnamed (and thus unused) parameters as
8956 artificial; there is no way to differentiate
8957 the two cases. */
94af9270
KS
8958 if (TYPE_NFIELDS (type) > 0
8959 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8960 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8961 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8962 0))))
d7e74731 8963 buf.puts (" const");
94af9270
KS
8964 }
8965 }
8966
d7e74731 8967 const std::string &intermediate_name = buf.string ();
94af9270
KS
8968
8969 if (cu->language == language_cplus)
34a68019 8970 canonical_name
322a8516 8971 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8972 &objfile->per_bfd->storage_obstack);
8973
8974 /* If we only computed INTERMEDIATE_NAME, or if
8975 INTERMEDIATE_NAME is already canonical, then we need to
8976 copy it to the appropriate obstack. */
322a8516 8977 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8978 name = ((const char *)
8979 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8980 intermediate_name.c_str (),
8981 intermediate_name.length ()));
34a68019
TT
8982 else
8983 name = canonical_name;
94af9270
KS
8984 }
8985 }
8986
8987 return name;
8988}
8989
0114d602
DJ
8990/* Return the fully qualified name of DIE, based on its DW_AT_name.
8991 If scope qualifiers are appropriate they will be added. The result
34a68019 8992 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8993 not have a name. NAME may either be from a previous call to
8994 dwarf2_name or NULL.
8995
9c37b5ae 8996 The output string will be canonicalized (if C++). */
0114d602
DJ
8997
8998static const char *
15d034d0 8999dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9000{
94af9270
KS
9001 return dwarf2_compute_name (name, die, cu, 0);
9002}
0114d602 9003
94af9270
KS
9004/* Construct a physname for the given DIE in CU. NAME may either be
9005 from a previous call to dwarf2_name or NULL. The result will be
9006 allocated on the objfile_objstack or NULL if the DIE does not have a
9007 name.
0114d602 9008
9c37b5ae 9009 The output string will be canonicalized (if C++). */
0114d602 9010
94af9270 9011static const char *
15d034d0 9012dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9013{
bb5ed363 9014 struct objfile *objfile = cu->objfile;
900e11f9 9015 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9016 int need_copy = 1;
9017
9018 /* In this case dwarf2_compute_name is just a shortcut not building anything
9019 on its own. */
9020 if (!die_needs_namespace (die, cu))
9021 return dwarf2_compute_name (name, die, cu, 1);
9022
73b9be8b 9023 mangled = dw2_linkage_name (die, cu);
900e11f9 9024
e98c9e7c
TT
9025 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9026 See https://github.com/rust-lang/rust/issues/32925. */
9027 if (cu->language == language_rust && mangled != NULL
9028 && strchr (mangled, '{') != NULL)
9029 mangled = NULL;
9030
900e11f9
JK
9031 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9032 has computed. */
791afaa2 9033 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9034 if (mangled != NULL)
900e11f9 9035 {
900e11f9
JK
9036 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9037 type. It is easier for GDB users to search for such functions as
9038 `name(params)' than `long name(params)'. In such case the minimal
9039 symbol names do not match the full symbol names but for template
9040 functions there is never a need to look up their definition from their
9041 declaration so the only disadvantage remains the minimal symbol
9042 variant `long name(params)' does not have the proper inferior type.
9043 */
9044
a766d390
DE
9045 if (cu->language == language_go)
9046 {
9047 /* This is a lie, but we already lie to the caller new_symbol_full.
9048 new_symbol_full assumes we return the mangled name.
9049 This just undoes that lie until things are cleaned up. */
a766d390
DE
9050 }
9051 else
9052 {
791afaa2
TT
9053 demangled.reset (gdb_demangle (mangled,
9054 (DMGL_PARAMS | DMGL_ANSI
9055 | DMGL_RET_DROP)));
a766d390 9056 }
900e11f9 9057 if (demangled)
791afaa2 9058 canon = demangled.get ();
900e11f9
JK
9059 else
9060 {
9061 canon = mangled;
9062 need_copy = 0;
9063 }
9064 }
9065
9066 if (canon == NULL || check_physname)
9067 {
9068 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9069
9070 if (canon != NULL && strcmp (physname, canon) != 0)
9071 {
9072 /* It may not mean a bug in GDB. The compiler could also
9073 compute DW_AT_linkage_name incorrectly. But in such case
9074 GDB would need to be bug-to-bug compatible. */
9075
9076 complaint (&symfile_complaints,
9077 _("Computed physname <%s> does not match demangled <%s> "
9078 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9079 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9080 objfile_name (objfile));
900e11f9
JK
9081
9082 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9083 is available here - over computed PHYSNAME. It is safer
9084 against both buggy GDB and buggy compilers. */
9085
9086 retval = canon;
9087 }
9088 else
9089 {
9090 retval = physname;
9091 need_copy = 0;
9092 }
9093 }
9094 else
9095 retval = canon;
9096
9097 if (need_copy)
224c3ddb
SM
9098 retval = ((const char *)
9099 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9100 retval, strlen (retval)));
900e11f9 9101
900e11f9 9102 return retval;
0114d602
DJ
9103}
9104
74921315
KS
9105/* Inspect DIE in CU for a namespace alias. If one exists, record
9106 a new symbol for it.
9107
9108 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9109
9110static int
9111read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9112{
9113 struct attribute *attr;
9114
9115 /* If the die does not have a name, this is not a namespace
9116 alias. */
9117 attr = dwarf2_attr (die, DW_AT_name, cu);
9118 if (attr != NULL)
9119 {
9120 int num;
9121 struct die_info *d = die;
9122 struct dwarf2_cu *imported_cu = cu;
9123
9124 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9125 keep inspecting DIEs until we hit the underlying import. */
9126#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9127 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9128 {
9129 attr = dwarf2_attr (d, DW_AT_import, cu);
9130 if (attr == NULL)
9131 break;
9132
9133 d = follow_die_ref (d, attr, &imported_cu);
9134 if (d->tag != DW_TAG_imported_declaration)
9135 break;
9136 }
9137
9138 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9139 {
9140 complaint (&symfile_complaints,
9141 _("DIE at 0x%x has too many recursively imported "
9c541725 9142 "declarations"), to_underlying (d->sect_off));
74921315
KS
9143 return 0;
9144 }
9145
9146 if (attr != NULL)
9147 {
9148 struct type *type;
9c541725 9149 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9150
9c541725 9151 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9152 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9153 {
9154 /* This declaration is a global namespace alias. Add
9155 a symbol for it whose type is the aliased namespace. */
9156 new_symbol (die, type, cu);
9157 return 1;
9158 }
9159 }
9160 }
9161
9162 return 0;
9163}
9164
22cee43f
PMR
9165/* Return the using directives repository (global or local?) to use in the
9166 current context for LANGUAGE.
9167
9168 For Ada, imported declarations can materialize renamings, which *may* be
9169 global. However it is impossible (for now?) in DWARF to distinguish
9170 "external" imported declarations and "static" ones. As all imported
9171 declarations seem to be static in all other languages, make them all CU-wide
9172 global only in Ada. */
9173
9174static struct using_direct **
9175using_directives (enum language language)
9176{
9177 if (language == language_ada && context_stack_depth == 0)
9178 return &global_using_directives;
9179 else
9180 return &local_using_directives;
9181}
9182
27aa8d6a
SW
9183/* Read the import statement specified by the given die and record it. */
9184
9185static void
9186read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9187{
bb5ed363 9188 struct objfile *objfile = cu->objfile;
27aa8d6a 9189 struct attribute *import_attr;
32019081 9190 struct die_info *imported_die, *child_die;
de4affc9 9191 struct dwarf2_cu *imported_cu;
27aa8d6a 9192 const char *imported_name;
794684b6 9193 const char *imported_name_prefix;
13387711
SW
9194 const char *canonical_name;
9195 const char *import_alias;
9196 const char *imported_declaration = NULL;
794684b6 9197 const char *import_prefix;
eb1e02fd 9198 std::vector<const char *> excludes;
13387711 9199
27aa8d6a
SW
9200 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9201 if (import_attr == NULL)
9202 {
9203 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9204 dwarf_tag_name (die->tag));
9205 return;
9206 }
9207
de4affc9
CC
9208 imported_cu = cu;
9209 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9210 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9211 if (imported_name == NULL)
9212 {
9213 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9214
9215 The import in the following code:
9216 namespace A
9217 {
9218 typedef int B;
9219 }
9220
9221 int main ()
9222 {
9223 using A::B;
9224 B b;
9225 return b;
9226 }
9227
9228 ...
9229 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9230 <52> DW_AT_decl_file : 1
9231 <53> DW_AT_decl_line : 6
9232 <54> DW_AT_import : <0x75>
9233 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9234 <59> DW_AT_name : B
9235 <5b> DW_AT_decl_file : 1
9236 <5c> DW_AT_decl_line : 2
9237 <5d> DW_AT_type : <0x6e>
9238 ...
9239 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9240 <76> DW_AT_byte_size : 4
9241 <77> DW_AT_encoding : 5 (signed)
9242
9243 imports the wrong die ( 0x75 instead of 0x58 ).
9244 This case will be ignored until the gcc bug is fixed. */
9245 return;
9246 }
9247
82856980
SW
9248 /* Figure out the local name after import. */
9249 import_alias = dwarf2_name (die, cu);
27aa8d6a 9250
794684b6
SW
9251 /* Figure out where the statement is being imported to. */
9252 import_prefix = determine_prefix (die, cu);
9253
9254 /* Figure out what the scope of the imported die is and prepend it
9255 to the name of the imported die. */
de4affc9 9256 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9257
f55ee35c
JK
9258 if (imported_die->tag != DW_TAG_namespace
9259 && imported_die->tag != DW_TAG_module)
794684b6 9260 {
13387711
SW
9261 imported_declaration = imported_name;
9262 canonical_name = imported_name_prefix;
794684b6 9263 }
13387711 9264 else if (strlen (imported_name_prefix) > 0)
12aaed36 9265 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9266 imported_name_prefix,
9267 (cu->language == language_d ? "." : "::"),
9268 imported_name, (char *) NULL);
13387711
SW
9269 else
9270 canonical_name = imported_name;
794684b6 9271
32019081
JK
9272 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9273 for (child_die = die->child; child_die && child_die->tag;
9274 child_die = sibling_die (child_die))
9275 {
9276 /* DWARF-4: A Fortran use statement with a “rename list” may be
9277 represented by an imported module entry with an import attribute
9278 referring to the module and owned entries corresponding to those
9279 entities that are renamed as part of being imported. */
9280
9281 if (child_die->tag != DW_TAG_imported_declaration)
9282 {
9283 complaint (&symfile_complaints,
9284 _("child DW_TAG_imported_declaration expected "
9285 "- DIE at 0x%x [in module %s]"),
9c541725 9286 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9287 continue;
9288 }
9289
9290 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9291 if (import_attr == NULL)
9292 {
9293 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9294 dwarf_tag_name (child_die->tag));
9295 continue;
9296 }
9297
9298 imported_cu = cu;
9299 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9300 &imported_cu);
9301 imported_name = dwarf2_name (imported_die, imported_cu);
9302 if (imported_name == NULL)
9303 {
9304 complaint (&symfile_complaints,
9305 _("child DW_TAG_imported_declaration has unknown "
9306 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9307 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9308 continue;
9309 }
9310
eb1e02fd 9311 excludes.push_back (imported_name);
32019081
JK
9312
9313 process_die (child_die, cu);
9314 }
9315
22cee43f
PMR
9316 add_using_directive (using_directives (cu->language),
9317 import_prefix,
9318 canonical_name,
9319 import_alias,
9320 imported_declaration,
9321 excludes,
9322 0,
9323 &objfile->objfile_obstack);
27aa8d6a
SW
9324}
9325
5230b05a
WT
9326/* ICC<14 does not output the required DW_AT_declaration on incomplete
9327 types, but gives them a size of zero. Starting with version 14,
9328 ICC is compatible with GCC. */
9329
9330static int
9331producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9332{
9333 if (!cu->checked_producer)
9334 check_producer (cu);
9335
9336 return cu->producer_is_icc_lt_14;
9337}
9338
1b80a9fa
JK
9339/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9340 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9341 this, it was first present in GCC release 4.3.0. */
9342
9343static int
9344producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9345{
9346 if (!cu->checked_producer)
9347 check_producer (cu);
9348
9349 return cu->producer_is_gcc_lt_4_3;
9350}
9351
d721ba37
PA
9352static file_and_directory
9353find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9354{
d721ba37
PA
9355 file_and_directory res;
9356
9291a0cd
TT
9357 /* Find the filename. Do not use dwarf2_name here, since the filename
9358 is not a source language identifier. */
d721ba37
PA
9359 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9360 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9361
d721ba37
PA
9362 if (res.comp_dir == NULL
9363 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9364 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9365 {
d721ba37
PA
9366 res.comp_dir_storage = ldirname (res.name);
9367 if (!res.comp_dir_storage.empty ())
9368 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9369 }
d721ba37 9370 if (res.comp_dir != NULL)
9291a0cd
TT
9371 {
9372 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9373 directory, get rid of it. */
d721ba37 9374 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9375
d721ba37
PA
9376 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9377 res.comp_dir = cp + 1;
9291a0cd
TT
9378 }
9379
d721ba37
PA
9380 if (res.name == NULL)
9381 res.name = "<unknown>";
9382
9383 return res;
9291a0cd
TT
9384}
9385
f4dc4d17
DE
9386/* Handle DW_AT_stmt_list for a compilation unit.
9387 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9388 COMP_DIR is the compilation directory. LOWPC is passed to
9389 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9390
9391static void
9392handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9393 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9394{
527f3840 9395 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9396 struct attribute *attr;
527f3840
JK
9397 struct line_header line_header_local;
9398 hashval_t line_header_local_hash;
9399 unsigned u;
9400 void **slot;
9401 int decode_mapping;
2ab95328 9402
f4dc4d17
DE
9403 gdb_assert (! cu->per_cu->is_debug_types);
9404
2ab95328 9405 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9406 if (attr == NULL)
9407 return;
9408
9c541725 9409 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9410
9411 /* The line header hash table is only created if needed (it exists to
9412 prevent redundant reading of the line table for partial_units).
9413 If we're given a partial_unit, we'll need it. If we're given a
9414 compile_unit, then use the line header hash table if it's already
9415 created, but don't create one just yet. */
9416
9417 if (dwarf2_per_objfile->line_header_hash == NULL
9418 && die->tag == DW_TAG_partial_unit)
2ab95328 9419 {
527f3840
JK
9420 dwarf2_per_objfile->line_header_hash
9421 = htab_create_alloc_ex (127, line_header_hash_voidp,
9422 line_header_eq_voidp,
9423 free_line_header_voidp,
9424 &objfile->objfile_obstack,
9425 hashtab_obstack_allocate,
9426 dummy_obstack_deallocate);
9427 }
2ab95328 9428
9c541725 9429 line_header_local.sect_off = line_offset;
527f3840
JK
9430 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9431 line_header_local_hash = line_header_hash (&line_header_local);
9432 if (dwarf2_per_objfile->line_header_hash != NULL)
9433 {
9434 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9435 &line_header_local,
9436 line_header_local_hash, NO_INSERT);
9437
9438 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9439 is not present in *SLOT (since if there is something in *SLOT then
9440 it will be for a partial_unit). */
9441 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9442 {
527f3840 9443 gdb_assert (*slot != NULL);
9a3c8263 9444 cu->line_header = (struct line_header *) *slot;
527f3840 9445 return;
dee91e82 9446 }
2ab95328 9447 }
527f3840
JK
9448
9449 /* dwarf_decode_line_header does not yet provide sufficient information.
9450 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9451 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9452 if (lh == NULL)
527f3840 9453 return;
4c8aa72d
PA
9454
9455 cu->line_header = lh.release ();
9456 cu->line_header_die_owner = die;
527f3840
JK
9457
9458 if (dwarf2_per_objfile->line_header_hash == NULL)
9459 slot = NULL;
9460 else
9461 {
9462 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9463 &line_header_local,
9464 line_header_local_hash, INSERT);
9465 gdb_assert (slot != NULL);
9466 }
9467 if (slot != NULL && *slot == NULL)
9468 {
9469 /* This newly decoded line number information unit will be owned
9470 by line_header_hash hash table. */
9471 *slot = cu->line_header;
4c8aa72d 9472 cu->line_header_die_owner = NULL;
527f3840
JK
9473 }
9474 else
9475 {
9476 /* We cannot free any current entry in (*slot) as that struct line_header
9477 may be already used by multiple CUs. Create only temporary decoded
9478 line_header for this CU - it may happen at most once for each line
9479 number information unit. And if we're not using line_header_hash
9480 then this is what we want as well. */
9481 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9482 }
9483 decode_mapping = (die->tag != DW_TAG_partial_unit);
9484 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9485 decode_mapping);
fff8551c 9486
2ab95328
TT
9487}
9488
95554aad 9489/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9490
c906108c 9491static void
e7c27a73 9492read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9493{
dee91e82 9494 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9495 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9496 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9497 CORE_ADDR highpc = ((CORE_ADDR) 0);
9498 struct attribute *attr;
c906108c 9499 struct die_info *child_die;
e142c38c 9500 CORE_ADDR baseaddr;
6e70227d 9501
e142c38c 9502 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9503
fae299cd 9504 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9505
9506 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9507 from finish_block. */
2acceee2 9508 if (lowpc == ((CORE_ADDR) -1))
c906108c 9509 lowpc = highpc;
3e29f34a 9510 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9511
d721ba37 9512 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9513
95554aad 9514 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9515
f4b8a18d
KW
9516 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9517 standardised yet. As a workaround for the language detection we fall
9518 back to the DW_AT_producer string. */
9519 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9520 cu->language = language_opencl;
9521
3019eac3
DE
9522 /* Similar hack for Go. */
9523 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9524 set_cu_language (DW_LANG_Go, cu);
9525
d721ba37 9526 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9527
9528 /* Decode line number information if present. We do this before
9529 processing child DIEs, so that the line header table is available
9530 for DW_AT_decl_file. */
d721ba37 9531 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9532
9533 /* Process all dies in compilation unit. */
9534 if (die->child != NULL)
9535 {
9536 child_die = die->child;
9537 while (child_die && child_die->tag)
9538 {
9539 process_die (child_die, cu);
9540 child_die = sibling_die (child_die);
9541 }
9542 }
9543
9544 /* Decode macro information, if present. Dwarf 2 macro information
9545 refers to information in the line number info statement program
9546 header, so we can only read it if we've read the header
9547 successfully. */
0af92d60
JK
9548 attr = dwarf2_attr (die, DW_AT_macros, cu);
9549 if (attr == NULL)
9550 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9551 if (attr && cu->line_header)
9552 {
9553 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9554 complaint (&symfile_complaints,
0af92d60 9555 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9556
43f3e411 9557 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9558 }
9559 else
9560 {
9561 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9562 if (attr && cu->line_header)
9563 {
9564 unsigned int macro_offset = DW_UNSND (attr);
9565
43f3e411 9566 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9567 }
9568 }
3019eac3
DE
9569}
9570
f4dc4d17
DE
9571/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9572 Create the set of symtabs used by this TU, or if this TU is sharing
9573 symtabs with another TU and the symtabs have already been created
9574 then restore those symtabs in the line header.
9575 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9576
9577static void
f4dc4d17 9578setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9579{
f4dc4d17
DE
9580 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9581 struct type_unit_group *tu_group;
9582 int first_time;
3019eac3 9583 struct attribute *attr;
9c541725 9584 unsigned int i;
0186c6a7 9585 struct signatured_type *sig_type;
3019eac3 9586
f4dc4d17 9587 gdb_assert (per_cu->is_debug_types);
0186c6a7 9588 sig_type = (struct signatured_type *) per_cu;
3019eac3 9589
f4dc4d17 9590 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9591
f4dc4d17 9592 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9593 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9594 if (sig_type->type_unit_group == NULL)
9595 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9596 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9597
9598 /* If we've already processed this stmt_list there's no real need to
9599 do it again, we could fake it and just recreate the part we need
9600 (file name,index -> symtab mapping). If data shows this optimization
9601 is useful we can do it then. */
43f3e411 9602 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9603
9604 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9605 debug info. */
fff8551c 9606 line_header_up lh;
f4dc4d17 9607 if (attr != NULL)
3019eac3 9608 {
9c541725 9609 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9610 lh = dwarf_decode_line_header (line_offset, cu);
9611 }
9612 if (lh == NULL)
9613 {
9614 if (first_time)
9615 dwarf2_start_symtab (cu, "", NULL, 0);
9616 else
9617 {
9618 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9619 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9620 }
f4dc4d17 9621 return;
3019eac3
DE
9622 }
9623
4c8aa72d
PA
9624 cu->line_header = lh.release ();
9625 cu->line_header_die_owner = die;
3019eac3 9626
f4dc4d17
DE
9627 if (first_time)
9628 {
43f3e411 9629 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9630
1fd60fc0
DE
9631 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9632 still initializing it, and our caller (a few levels up)
9633 process_full_type_unit still needs to know if this is the first
9634 time. */
9635
4c8aa72d
PA
9636 tu_group->num_symtabs = cu->line_header->file_names.size ();
9637 tu_group->symtabs = XNEWVEC (struct symtab *,
9638 cu->line_header->file_names.size ());
3019eac3 9639
4c8aa72d 9640 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9641 {
4c8aa72d 9642 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9643
4c8aa72d 9644 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9645
f4dc4d17
DE
9646 if (current_subfile->symtab == NULL)
9647 {
4c8aa72d
PA
9648 /* NOTE: start_subfile will recognize when it's been
9649 passed a file it has already seen. So we can't
9650 assume there's a simple mapping from
9651 cu->line_header->file_names to subfiles, plus
9652 cu->line_header->file_names may contain dups. */
43f3e411
DE
9653 current_subfile->symtab
9654 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9655 }
9656
8c43009f
PA
9657 fe.symtab = current_subfile->symtab;
9658 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9659 }
9660 }
9661 else
3019eac3 9662 {
0ab9ce85 9663 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 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];
f4dc4d17 9668
4c8aa72d 9669 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9670 }
3019eac3
DE
9671 }
9672
f4dc4d17
DE
9673 /* The main symtab is allocated last. Type units don't have DW_AT_name
9674 so they don't have a "real" (so to speak) symtab anyway.
9675 There is later code that will assign the main symtab to all symbols
9676 that don't have one. We need to handle the case of a symbol with a
9677 missing symtab (DW_AT_decl_file) anyway. */
9678}
3019eac3 9679
f4dc4d17
DE
9680/* Process DW_TAG_type_unit.
9681 For TUs we want to skip the first top level sibling if it's not the
9682 actual type being defined by this TU. In this case the first top
9683 level sibling is there to provide context only. */
3019eac3 9684
f4dc4d17
DE
9685static void
9686read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9687{
9688 struct die_info *child_die;
3019eac3 9689
f4dc4d17
DE
9690 prepare_one_comp_unit (cu, die, language_minimal);
9691
9692 /* Initialize (or reinitialize) the machinery for building symtabs.
9693 We do this before processing child DIEs, so that the line header table
9694 is available for DW_AT_decl_file. */
9695 setup_type_unit_groups (die, cu);
9696
9697 if (die->child != NULL)
9698 {
9699 child_die = die->child;
9700 while (child_die && child_die->tag)
9701 {
9702 process_die (child_die, cu);
9703 child_die = sibling_die (child_die);
9704 }
9705 }
3019eac3
DE
9706}
9707\f
80626a55
DE
9708/* DWO/DWP files.
9709
9710 http://gcc.gnu.org/wiki/DebugFission
9711 http://gcc.gnu.org/wiki/DebugFissionDWP
9712
9713 To simplify handling of both DWO files ("object" files with the DWARF info)
9714 and DWP files (a file with the DWOs packaged up into one file), we treat
9715 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9716
9717static hashval_t
9718hash_dwo_file (const void *item)
9719{
9a3c8263 9720 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9721 hashval_t hash;
3019eac3 9722
a2ce51a0
DE
9723 hash = htab_hash_string (dwo_file->dwo_name);
9724 if (dwo_file->comp_dir != NULL)
9725 hash += htab_hash_string (dwo_file->comp_dir);
9726 return hash;
3019eac3
DE
9727}
9728
9729static int
9730eq_dwo_file (const void *item_lhs, const void *item_rhs)
9731{
9a3c8263
SM
9732 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9733 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9734
a2ce51a0
DE
9735 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9736 return 0;
9737 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9738 return lhs->comp_dir == rhs->comp_dir;
9739 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9740}
9741
9742/* Allocate a hash table for DWO files. */
9743
9744static htab_t
9745allocate_dwo_file_hash_table (void)
9746{
9747 struct objfile *objfile = dwarf2_per_objfile->objfile;
9748
9749 return htab_create_alloc_ex (41,
9750 hash_dwo_file,
9751 eq_dwo_file,
9752 NULL,
9753 &objfile->objfile_obstack,
9754 hashtab_obstack_allocate,
9755 dummy_obstack_deallocate);
9756}
9757
80626a55
DE
9758/* Lookup DWO file DWO_NAME. */
9759
9760static void **
0ac5b59e 9761lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9762{
9763 struct dwo_file find_entry;
9764 void **slot;
9765
9766 if (dwarf2_per_objfile->dwo_files == NULL)
9767 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9768
9769 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9770 find_entry.dwo_name = dwo_name;
9771 find_entry.comp_dir = comp_dir;
80626a55
DE
9772 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9773
9774 return slot;
9775}
9776
3019eac3
DE
9777static hashval_t
9778hash_dwo_unit (const void *item)
9779{
9a3c8263 9780 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9781
9782 /* This drops the top 32 bits of the id, but is ok for a hash. */
9783 return dwo_unit->signature;
9784}
9785
9786static int
9787eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9788{
9a3c8263
SM
9789 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9790 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9791
9792 /* The signature is assumed to be unique within the DWO file.
9793 So while object file CU dwo_id's always have the value zero,
9794 that's OK, assuming each object file DWO file has only one CU,
9795 and that's the rule for now. */
9796 return lhs->signature == rhs->signature;
9797}
9798
9799/* Allocate a hash table for DWO CUs,TUs.
9800 There is one of these tables for each of CUs,TUs for each DWO file. */
9801
9802static htab_t
9803allocate_dwo_unit_table (struct objfile *objfile)
9804{
9805 /* Start out with a pretty small number.
9806 Generally DWO files contain only one CU and maybe some TUs. */
9807 return htab_create_alloc_ex (3,
9808 hash_dwo_unit,
9809 eq_dwo_unit,
9810 NULL,
9811 &objfile->objfile_obstack,
9812 hashtab_obstack_allocate,
9813 dummy_obstack_deallocate);
9814}
9815
80626a55 9816/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9817
19c3d4c9 9818struct create_dwo_cu_data
3019eac3
DE
9819{
9820 struct dwo_file *dwo_file;
19c3d4c9 9821 struct dwo_unit dwo_unit;
3019eac3
DE
9822};
9823
19c3d4c9 9824/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9825
9826static void
19c3d4c9
DE
9827create_dwo_cu_reader (const struct die_reader_specs *reader,
9828 const gdb_byte *info_ptr,
9829 struct die_info *comp_unit_die,
9830 int has_children,
9831 void *datap)
3019eac3
DE
9832{
9833 struct dwarf2_cu *cu = reader->cu;
9c541725 9834 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9835 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9836 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9837 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9838 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9839 struct attribute *attr;
3019eac3
DE
9840
9841 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9842 if (attr == NULL)
9843 {
19c3d4c9
DE
9844 complaint (&symfile_complaints,
9845 _("Dwarf Error: debug entry at offset 0x%x is missing"
9846 " its dwo_id [in module %s]"),
9c541725 9847 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9848 return;
9849 }
9850
3019eac3
DE
9851 dwo_unit->dwo_file = dwo_file;
9852 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9853 dwo_unit->section = section;
9c541725 9854 dwo_unit->sect_off = sect_off;
3019eac3
DE
9855 dwo_unit->length = cu->per_cu->length;
9856
b4f54984 9857 if (dwarf_read_debug)
4031ecc5 9858 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9859 to_underlying (sect_off),
9860 hex_string (dwo_unit->signature));
3019eac3
DE
9861}
9862
33c5cd75 9863/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9864 Note: This function processes DWO files only, not DWP files. */
3019eac3 9865
33c5cd75
DB
9866static void
9867create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9868 htab_t &cus_htab)
3019eac3
DE
9869{
9870 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9871 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9872 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9873
33c5cd75
DB
9874 dwarf2_read_section (objfile, &section);
9875 info_ptr = section.buffer;
3019eac3
DE
9876
9877 if (info_ptr == NULL)
33c5cd75 9878 return;
3019eac3 9879
b4f54984 9880 if (dwarf_read_debug)
19c3d4c9
DE
9881 {
9882 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9883 get_section_name (&section),
9884 get_section_file_name (&section));
19c3d4c9 9885 }
3019eac3 9886
33c5cd75 9887 end_ptr = info_ptr + section.size;
3019eac3
DE
9888 while (info_ptr < end_ptr)
9889 {
9890 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9891 struct create_dwo_cu_data create_dwo_cu_data;
9892 struct dwo_unit *dwo_unit;
9893 void **slot;
9894 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9895
19c3d4c9
DE
9896 memset (&create_dwo_cu_data.dwo_unit, 0,
9897 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9898 memset (&per_cu, 0, sizeof (per_cu));
9899 per_cu.objfile = objfile;
9900 per_cu.is_debug_types = 0;
33c5cd75
DB
9901 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9902 per_cu.section = &section;
c5ed0576 9903 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9904
9905 init_cutu_and_read_dies_no_follow (
9906 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9907 info_ptr += per_cu.length;
9908
9909 // If the unit could not be parsed, skip it.
9910 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9911 continue;
3019eac3 9912
33c5cd75
DB
9913 if (cus_htab == NULL)
9914 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 9915
33c5cd75
DB
9916 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9917 *dwo_unit = create_dwo_cu_data.dwo_unit;
9918 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9919 gdb_assert (slot != NULL);
9920 if (*slot != NULL)
19c3d4c9 9921 {
33c5cd75
DB
9922 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9923 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 9924
33c5cd75
DB
9925 complaint (&symfile_complaints,
9926 _("debug cu entry at offset 0x%x is duplicate to"
9927 " the entry at offset 0x%x, signature %s"),
9928 to_underlying (sect_off), to_underlying (dup_sect_off),
9929 hex_string (dwo_unit->signature));
19c3d4c9 9930 }
33c5cd75 9931 *slot = (void *)dwo_unit;
3019eac3 9932 }
3019eac3
DE
9933}
9934
80626a55
DE
9935/* DWP file .debug_{cu,tu}_index section format:
9936 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9937
d2415c6c
DE
9938 DWP Version 1:
9939
80626a55
DE
9940 Both index sections have the same format, and serve to map a 64-bit
9941 signature to a set of section numbers. Each section begins with a header,
9942 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9943 indexes, and a pool of 32-bit section numbers. The index sections will be
9944 aligned at 8-byte boundaries in the file.
9945
d2415c6c
DE
9946 The index section header consists of:
9947
9948 V, 32 bit version number
9949 -, 32 bits unused
9950 N, 32 bit number of compilation units or type units in the index
9951 M, 32 bit number of slots in the hash table
80626a55 9952
d2415c6c 9953 Numbers are recorded using the byte order of the application binary.
80626a55 9954
d2415c6c
DE
9955 The hash table begins at offset 16 in the section, and consists of an array
9956 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9957 order of the application binary). Unused slots in the hash table are 0.
9958 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9959
d2415c6c
DE
9960 The parallel table begins immediately after the hash table
9961 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9962 array of 32-bit indexes (using the byte order of the application binary),
9963 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9964 table contains a 32-bit index into the pool of section numbers. For unused
9965 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9966
73869dc2
DE
9967 The pool of section numbers begins immediately following the hash table
9968 (at offset 16 + 12 * M from the beginning of the section). The pool of
9969 section numbers consists of an array of 32-bit words (using the byte order
9970 of the application binary). Each item in the array is indexed starting
9971 from 0. The hash table entry provides the index of the first section
9972 number in the set. Additional section numbers in the set follow, and the
9973 set is terminated by a 0 entry (section number 0 is not used in ELF).
9974
9975 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9976 section must be the first entry in the set, and the .debug_abbrev.dwo must
9977 be the second entry. Other members of the set may follow in any order.
9978
9979 ---
9980
9981 DWP Version 2:
9982
9983 DWP Version 2 combines all the .debug_info, etc. sections into one,
9984 and the entries in the index tables are now offsets into these sections.
9985 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9986 section.
9987
9988 Index Section Contents:
9989 Header
9990 Hash Table of Signatures dwp_hash_table.hash_table
9991 Parallel Table of Indices dwp_hash_table.unit_table
9992 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9993 Table of Section Sizes dwp_hash_table.v2.sizes
9994
9995 The index section header consists of:
9996
9997 V, 32 bit version number
9998 L, 32 bit number of columns in the table of section offsets
9999 N, 32 bit number of compilation units or type units in the index
10000 M, 32 bit number of slots in the hash table
10001
10002 Numbers are recorded using the byte order of the application binary.
10003
10004 The hash table has the same format as version 1.
10005 The parallel table of indices has the same format as version 1,
10006 except that the entries are origin-1 indices into the table of sections
10007 offsets and the table of section sizes.
10008
10009 The table of offsets begins immediately following the parallel table
10010 (at offset 16 + 12 * M from the beginning of the section). The table is
10011 a two-dimensional array of 32-bit words (using the byte order of the
10012 application binary), with L columns and N+1 rows, in row-major order.
10013 Each row in the array is indexed starting from 0. The first row provides
10014 a key to the remaining rows: each column in this row provides an identifier
10015 for a debug section, and the offsets in the same column of subsequent rows
10016 refer to that section. The section identifiers are:
10017
10018 DW_SECT_INFO 1 .debug_info.dwo
10019 DW_SECT_TYPES 2 .debug_types.dwo
10020 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10021 DW_SECT_LINE 4 .debug_line.dwo
10022 DW_SECT_LOC 5 .debug_loc.dwo
10023 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10024 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10025 DW_SECT_MACRO 8 .debug_macro.dwo
10026
10027 The offsets provided by the CU and TU index sections are the base offsets
10028 for the contributions made by each CU or TU to the corresponding section
10029 in the package file. Each CU and TU header contains an abbrev_offset
10030 field, used to find the abbreviations table for that CU or TU within the
10031 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10032 be interpreted as relative to the base offset given in the index section.
10033 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10034 should be interpreted as relative to the base offset for .debug_line.dwo,
10035 and offsets into other debug sections obtained from DWARF attributes should
10036 also be interpreted as relative to the corresponding base offset.
10037
10038 The table of sizes begins immediately following the table of offsets.
10039 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10040 with L columns and N rows, in row-major order. Each row in the array is
10041 indexed starting from 1 (row 0 is shared by the two tables).
10042
10043 ---
10044
10045 Hash table lookup is handled the same in version 1 and 2:
10046
10047 We assume that N and M will not exceed 2^32 - 1.
10048 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10049
d2415c6c
DE
10050 Given a 64-bit compilation unit signature or a type signature S, an entry
10051 in the hash table is located as follows:
80626a55 10052
d2415c6c
DE
10053 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10054 the low-order k bits all set to 1.
80626a55 10055
d2415c6c 10056 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10057
d2415c6c
DE
10058 3) If the hash table entry at index H matches the signature, use that
10059 entry. If the hash table entry at index H is unused (all zeroes),
10060 terminate the search: the signature is not present in the table.
80626a55 10061
d2415c6c 10062 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10063
d2415c6c 10064 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10065 to stop at an unused slot or find the match. */
80626a55
DE
10066
10067/* Create a hash table to map DWO IDs to their CU/TU entry in
10068 .debug_{info,types}.dwo in DWP_FILE.
10069 Returns NULL if there isn't one.
10070 Note: This function processes DWP files only, not DWO files. */
10071
10072static struct dwp_hash_table *
10073create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10074{
10075 struct objfile *objfile = dwarf2_per_objfile->objfile;
10076 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10077 const gdb_byte *index_ptr, *index_end;
80626a55 10078 struct dwarf2_section_info *index;
73869dc2 10079 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10080 struct dwp_hash_table *htab;
10081
10082 if (is_debug_types)
10083 index = &dwp_file->sections.tu_index;
10084 else
10085 index = &dwp_file->sections.cu_index;
10086
10087 if (dwarf2_section_empty_p (index))
10088 return NULL;
10089 dwarf2_read_section (objfile, index);
10090
10091 index_ptr = index->buffer;
10092 index_end = index_ptr + index->size;
10093
10094 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10095 index_ptr += 4;
10096 if (version == 2)
10097 nr_columns = read_4_bytes (dbfd, index_ptr);
10098 else
10099 nr_columns = 0;
10100 index_ptr += 4;
80626a55
DE
10101 nr_units = read_4_bytes (dbfd, index_ptr);
10102 index_ptr += 4;
10103 nr_slots = read_4_bytes (dbfd, index_ptr);
10104 index_ptr += 4;
10105
73869dc2 10106 if (version != 1 && version != 2)
80626a55 10107 {
21aa081e 10108 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10109 " [in module %s]"),
21aa081e 10110 pulongest (version), dwp_file->name);
80626a55
DE
10111 }
10112 if (nr_slots != (nr_slots & -nr_slots))
10113 {
21aa081e 10114 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10115 " is not power of 2 [in module %s]"),
21aa081e 10116 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10117 }
10118
10119 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10120 htab->version = version;
10121 htab->nr_columns = nr_columns;
80626a55
DE
10122 htab->nr_units = nr_units;
10123 htab->nr_slots = nr_slots;
10124 htab->hash_table = index_ptr;
10125 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10126
10127 /* Exit early if the table is empty. */
10128 if (nr_slots == 0 || nr_units == 0
10129 || (version == 2 && nr_columns == 0))
10130 {
10131 /* All must be zero. */
10132 if (nr_slots != 0 || nr_units != 0
10133 || (version == 2 && nr_columns != 0))
10134 {
10135 complaint (&symfile_complaints,
10136 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10137 " all zero [in modules %s]"),
10138 dwp_file->name);
10139 }
10140 return htab;
10141 }
10142
10143 if (version == 1)
10144 {
10145 htab->section_pool.v1.indices =
10146 htab->unit_table + sizeof (uint32_t) * nr_slots;
10147 /* It's harder to decide whether the section is too small in v1.
10148 V1 is deprecated anyway so we punt. */
10149 }
10150 else
10151 {
10152 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10153 int *ids = htab->section_pool.v2.section_ids;
10154 /* Reverse map for error checking. */
10155 int ids_seen[DW_SECT_MAX + 1];
10156 int i;
10157
10158 if (nr_columns < 2)
10159 {
10160 error (_("Dwarf Error: bad DWP hash table, too few columns"
10161 " in section table [in module %s]"),
10162 dwp_file->name);
10163 }
10164 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10165 {
10166 error (_("Dwarf Error: bad DWP hash table, too many columns"
10167 " in section table [in module %s]"),
10168 dwp_file->name);
10169 }
10170 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10171 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10172 for (i = 0; i < nr_columns; ++i)
10173 {
10174 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10175
10176 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10177 {
10178 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10179 " in section table [in module %s]"),
10180 id, dwp_file->name);
10181 }
10182 if (ids_seen[id] != -1)
10183 {
10184 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10185 " id %d in section table [in module %s]"),
10186 id, dwp_file->name);
10187 }
10188 ids_seen[id] = i;
10189 ids[i] = id;
10190 }
10191 /* Must have exactly one info or types section. */
10192 if (((ids_seen[DW_SECT_INFO] != -1)
10193 + (ids_seen[DW_SECT_TYPES] != -1))
10194 != 1)
10195 {
10196 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10197 " DWO info/types section [in module %s]"),
10198 dwp_file->name);
10199 }
10200 /* Must have an abbrev section. */
10201 if (ids_seen[DW_SECT_ABBREV] == -1)
10202 {
10203 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10204 " section [in module %s]"),
10205 dwp_file->name);
10206 }
10207 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10208 htab->section_pool.v2.sizes =
10209 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10210 * nr_units * nr_columns);
10211 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10212 * nr_units * nr_columns))
10213 > index_end)
10214 {
10215 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10216 " [in module %s]"),
10217 dwp_file->name);
10218 }
10219 }
80626a55
DE
10220
10221 return htab;
10222}
10223
10224/* Update SECTIONS with the data from SECTP.
10225
10226 This function is like the other "locate" section routines that are
10227 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10228 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10229
10230 The result is non-zero for success, or zero if an error was found. */
10231
10232static int
73869dc2
DE
10233locate_v1_virtual_dwo_sections (asection *sectp,
10234 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10235{
10236 const struct dwop_section_names *names = &dwop_section_names;
10237
10238 if (section_is_p (sectp->name, &names->abbrev_dwo))
10239 {
10240 /* There can be only one. */
049412e3 10241 if (sections->abbrev.s.section != NULL)
80626a55 10242 return 0;
049412e3 10243 sections->abbrev.s.section = sectp;
80626a55
DE
10244 sections->abbrev.size = bfd_get_section_size (sectp);
10245 }
10246 else if (section_is_p (sectp->name, &names->info_dwo)
10247 || section_is_p (sectp->name, &names->types_dwo))
10248 {
10249 /* There can be only one. */
049412e3 10250 if (sections->info_or_types.s.section != NULL)
80626a55 10251 return 0;
049412e3 10252 sections->info_or_types.s.section = sectp;
80626a55
DE
10253 sections->info_or_types.size = bfd_get_section_size (sectp);
10254 }
10255 else if (section_is_p (sectp->name, &names->line_dwo))
10256 {
10257 /* There can be only one. */
049412e3 10258 if (sections->line.s.section != NULL)
80626a55 10259 return 0;
049412e3 10260 sections->line.s.section = sectp;
80626a55
DE
10261 sections->line.size = bfd_get_section_size (sectp);
10262 }
10263 else if (section_is_p (sectp->name, &names->loc_dwo))
10264 {
10265 /* There can be only one. */
049412e3 10266 if (sections->loc.s.section != NULL)
80626a55 10267 return 0;
049412e3 10268 sections->loc.s.section = sectp;
80626a55
DE
10269 sections->loc.size = bfd_get_section_size (sectp);
10270 }
10271 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10272 {
10273 /* There can be only one. */
049412e3 10274 if (sections->macinfo.s.section != NULL)
80626a55 10275 return 0;
049412e3 10276 sections->macinfo.s.section = sectp;
80626a55
DE
10277 sections->macinfo.size = bfd_get_section_size (sectp);
10278 }
10279 else if (section_is_p (sectp->name, &names->macro_dwo))
10280 {
10281 /* There can be only one. */
049412e3 10282 if (sections->macro.s.section != NULL)
80626a55 10283 return 0;
049412e3 10284 sections->macro.s.section = sectp;
80626a55
DE
10285 sections->macro.size = bfd_get_section_size (sectp);
10286 }
10287 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10288 {
10289 /* There can be only one. */
049412e3 10290 if (sections->str_offsets.s.section != NULL)
80626a55 10291 return 0;
049412e3 10292 sections->str_offsets.s.section = sectp;
80626a55
DE
10293 sections->str_offsets.size = bfd_get_section_size (sectp);
10294 }
10295 else
10296 {
10297 /* No other kind of section is valid. */
10298 return 0;
10299 }
10300
10301 return 1;
10302}
10303
73869dc2
DE
10304/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10305 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10306 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10307 This is for DWP version 1 files. */
80626a55
DE
10308
10309static struct dwo_unit *
73869dc2
DE
10310create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10311 uint32_t unit_index,
10312 const char *comp_dir,
10313 ULONGEST signature, int is_debug_types)
80626a55
DE
10314{
10315 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10316 const struct dwp_hash_table *dwp_htab =
10317 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10318 bfd *dbfd = dwp_file->dbfd;
10319 const char *kind = is_debug_types ? "TU" : "CU";
10320 struct dwo_file *dwo_file;
10321 struct dwo_unit *dwo_unit;
73869dc2 10322 struct virtual_v1_dwo_sections sections;
80626a55 10323 void **dwo_file_slot;
80626a55
DE
10324 int i;
10325
73869dc2
DE
10326 gdb_assert (dwp_file->version == 1);
10327
b4f54984 10328 if (dwarf_read_debug)
80626a55 10329 {
73869dc2 10330 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10331 kind,
73869dc2 10332 pulongest (unit_index), hex_string (signature),
80626a55
DE
10333 dwp_file->name);
10334 }
10335
19ac8c2e 10336 /* Fetch the sections of this DWO unit.
80626a55
DE
10337 Put a limit on the number of sections we look for so that bad data
10338 doesn't cause us to loop forever. */
10339
73869dc2 10340#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10341 (1 /* .debug_info or .debug_types */ \
10342 + 1 /* .debug_abbrev */ \
10343 + 1 /* .debug_line */ \
10344 + 1 /* .debug_loc */ \
10345 + 1 /* .debug_str_offsets */ \
19ac8c2e 10346 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10347 + 1 /* trailing zero */)
10348
10349 memset (&sections, 0, sizeof (sections));
80626a55 10350
73869dc2 10351 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10352 {
10353 asection *sectp;
10354 uint32_t section_nr =
10355 read_4_bytes (dbfd,
73869dc2
DE
10356 dwp_htab->section_pool.v1.indices
10357 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10358
10359 if (section_nr == 0)
10360 break;
10361 if (section_nr >= dwp_file->num_sections)
10362 {
10363 error (_("Dwarf Error: bad DWP hash table, section number too large"
10364 " [in module %s]"),
10365 dwp_file->name);
10366 }
10367
10368 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10369 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10370 {
10371 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10372 " [in module %s]"),
10373 dwp_file->name);
10374 }
10375 }
10376
10377 if (i < 2
a32a8923
DE
10378 || dwarf2_section_empty_p (&sections.info_or_types)
10379 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10380 {
10381 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10382 " [in module %s]"),
10383 dwp_file->name);
10384 }
73869dc2 10385 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10386 {
10387 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10388 " [in module %s]"),
10389 dwp_file->name);
10390 }
10391
10392 /* It's easier for the rest of the code if we fake a struct dwo_file and
10393 have dwo_unit "live" in that. At least for now.
10394
10395 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10396 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10397 file, we can combine them back into a virtual DWO file to save space
10398 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10399 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10400
791afaa2
TT
10401 std::string virtual_dwo_name =
10402 string_printf ("virtual-dwo/%d-%d-%d-%d",
10403 get_section_id (&sections.abbrev),
10404 get_section_id (&sections.line),
10405 get_section_id (&sections.loc),
10406 get_section_id (&sections.str_offsets));
80626a55 10407 /* Can we use an existing virtual DWO file? */
791afaa2 10408 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
10409 /* Create one if necessary. */
10410 if (*dwo_file_slot == NULL)
10411 {
b4f54984 10412 if (dwarf_read_debug)
80626a55
DE
10413 {
10414 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 10415 virtual_dwo_name.c_str ());
80626a55
DE
10416 }
10417 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10418 dwo_file->dwo_name
10419 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
10420 virtual_dwo_name.c_str (),
10421 virtual_dwo_name.size ());
0ac5b59e 10422 dwo_file->comp_dir = comp_dir;
80626a55
DE
10423 dwo_file->sections.abbrev = sections.abbrev;
10424 dwo_file->sections.line = sections.line;
10425 dwo_file->sections.loc = sections.loc;
10426 dwo_file->sections.macinfo = sections.macinfo;
10427 dwo_file->sections.macro = sections.macro;
10428 dwo_file->sections.str_offsets = sections.str_offsets;
10429 /* The "str" section is global to the entire DWP file. */
10430 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10431 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10432 there's no need to record it in dwo_file.
10433 Also, we can't simply record type sections in dwo_file because
10434 we record a pointer into the vector in dwo_unit. As we collect more
10435 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10436 for it, invalidating all copies of pointers into the previous
10437 contents. */
80626a55
DE
10438 *dwo_file_slot = dwo_file;
10439 }
10440 else
10441 {
b4f54984 10442 if (dwarf_read_debug)
80626a55
DE
10443 {
10444 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 10445 virtual_dwo_name.c_str ());
80626a55 10446 }
9a3c8263 10447 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 10448 }
80626a55
DE
10449
10450 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10451 dwo_unit->dwo_file = dwo_file;
10452 dwo_unit->signature = signature;
8d749320
SM
10453 dwo_unit->section =
10454 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10455 *dwo_unit->section = sections.info_or_types;
57d63ce2 10456 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10457
10458 return dwo_unit;
10459}
10460
73869dc2
DE
10461/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10462 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10463 piece within that section used by a TU/CU, return a virtual section
10464 of just that piece. */
10465
10466static struct dwarf2_section_info
10467create_dwp_v2_section (struct dwarf2_section_info *section,
10468 bfd_size_type offset, bfd_size_type size)
10469{
10470 struct dwarf2_section_info result;
10471 asection *sectp;
10472
10473 gdb_assert (section != NULL);
10474 gdb_assert (!section->is_virtual);
10475
10476 memset (&result, 0, sizeof (result));
10477 result.s.containing_section = section;
10478 result.is_virtual = 1;
10479
10480 if (size == 0)
10481 return result;
10482
10483 sectp = get_section_bfd_section (section);
10484
10485 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10486 bounds of the real section. This is a pretty-rare event, so just
10487 flag an error (easier) instead of a warning and trying to cope. */
10488 if (sectp == NULL
10489 || offset + size > bfd_get_section_size (sectp))
10490 {
10491 bfd *abfd = sectp->owner;
10492
10493 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10494 " in section %s [in module %s]"),
10495 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10496 objfile_name (dwarf2_per_objfile->objfile));
10497 }
10498
10499 result.virtual_offset = offset;
10500 result.size = size;
10501 return result;
10502}
10503
10504/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10505 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10506 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10507 This is for DWP version 2 files. */
10508
10509static struct dwo_unit *
10510create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10511 uint32_t unit_index,
10512 const char *comp_dir,
10513 ULONGEST signature, int is_debug_types)
10514{
10515 struct objfile *objfile = dwarf2_per_objfile->objfile;
10516 const struct dwp_hash_table *dwp_htab =
10517 is_debug_types ? dwp_file->tus : dwp_file->cus;
10518 bfd *dbfd = dwp_file->dbfd;
10519 const char *kind = is_debug_types ? "TU" : "CU";
10520 struct dwo_file *dwo_file;
10521 struct dwo_unit *dwo_unit;
10522 struct virtual_v2_dwo_sections sections;
10523 void **dwo_file_slot;
73869dc2
DE
10524 int i;
10525
10526 gdb_assert (dwp_file->version == 2);
10527
b4f54984 10528 if (dwarf_read_debug)
73869dc2
DE
10529 {
10530 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10531 kind,
10532 pulongest (unit_index), hex_string (signature),
10533 dwp_file->name);
10534 }
10535
10536 /* Fetch the section offsets of this DWO unit. */
10537
10538 memset (&sections, 0, sizeof (sections));
73869dc2
DE
10539
10540 for (i = 0; i < dwp_htab->nr_columns; ++i)
10541 {
10542 uint32_t offset = read_4_bytes (dbfd,
10543 dwp_htab->section_pool.v2.offsets
10544 + (((unit_index - 1) * dwp_htab->nr_columns
10545 + i)
10546 * sizeof (uint32_t)));
10547 uint32_t size = read_4_bytes (dbfd,
10548 dwp_htab->section_pool.v2.sizes
10549 + (((unit_index - 1) * dwp_htab->nr_columns
10550 + i)
10551 * sizeof (uint32_t)));
10552
10553 switch (dwp_htab->section_pool.v2.section_ids[i])
10554 {
10555 case DW_SECT_INFO:
10556 case DW_SECT_TYPES:
10557 sections.info_or_types_offset = offset;
10558 sections.info_or_types_size = size;
10559 break;
10560 case DW_SECT_ABBREV:
10561 sections.abbrev_offset = offset;
10562 sections.abbrev_size = size;
10563 break;
10564 case DW_SECT_LINE:
10565 sections.line_offset = offset;
10566 sections.line_size = size;
10567 break;
10568 case DW_SECT_LOC:
10569 sections.loc_offset = offset;
10570 sections.loc_size = size;
10571 break;
10572 case DW_SECT_STR_OFFSETS:
10573 sections.str_offsets_offset = offset;
10574 sections.str_offsets_size = size;
10575 break;
10576 case DW_SECT_MACINFO:
10577 sections.macinfo_offset = offset;
10578 sections.macinfo_size = size;
10579 break;
10580 case DW_SECT_MACRO:
10581 sections.macro_offset = offset;
10582 sections.macro_size = size;
10583 break;
10584 }
10585 }
10586
10587 /* It's easier for the rest of the code if we fake a struct dwo_file and
10588 have dwo_unit "live" in that. At least for now.
10589
10590 The DWP file can be made up of a random collection of CUs and TUs.
10591 However, for each CU + set of TUs that came from the same original DWO
10592 file, we can combine them back into a virtual DWO file to save space
10593 (fewer struct dwo_file objects to allocate). Remember that for really
10594 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10595
791afaa2
TT
10596 std::string virtual_dwo_name =
10597 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10598 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10599 (long) (sections.line_size ? sections.line_offset : 0),
10600 (long) (sections.loc_size ? sections.loc_offset : 0),
10601 (long) (sections.str_offsets_size
10602 ? sections.str_offsets_offset : 0));
73869dc2 10603 /* Can we use an existing virtual DWO file? */
791afaa2 10604 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
10605 /* Create one if necessary. */
10606 if (*dwo_file_slot == NULL)
10607 {
b4f54984 10608 if (dwarf_read_debug)
73869dc2
DE
10609 {
10610 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 10611 virtual_dwo_name.c_str ());
73869dc2
DE
10612 }
10613 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10614 dwo_file->dwo_name
10615 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
10616 virtual_dwo_name.c_str (),
10617 virtual_dwo_name.size ());
73869dc2
DE
10618 dwo_file->comp_dir = comp_dir;
10619 dwo_file->sections.abbrev =
10620 create_dwp_v2_section (&dwp_file->sections.abbrev,
10621 sections.abbrev_offset, sections.abbrev_size);
10622 dwo_file->sections.line =
10623 create_dwp_v2_section (&dwp_file->sections.line,
10624 sections.line_offset, sections.line_size);
10625 dwo_file->sections.loc =
10626 create_dwp_v2_section (&dwp_file->sections.loc,
10627 sections.loc_offset, sections.loc_size);
10628 dwo_file->sections.macinfo =
10629 create_dwp_v2_section (&dwp_file->sections.macinfo,
10630 sections.macinfo_offset, sections.macinfo_size);
10631 dwo_file->sections.macro =
10632 create_dwp_v2_section (&dwp_file->sections.macro,
10633 sections.macro_offset, sections.macro_size);
10634 dwo_file->sections.str_offsets =
10635 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10636 sections.str_offsets_offset,
10637 sections.str_offsets_size);
10638 /* The "str" section is global to the entire DWP file. */
10639 dwo_file->sections.str = dwp_file->sections.str;
10640 /* The info or types section is assigned below to dwo_unit,
10641 there's no need to record it in dwo_file.
10642 Also, we can't simply record type sections in dwo_file because
10643 we record a pointer into the vector in dwo_unit. As we collect more
10644 types we'll grow the vector and eventually have to reallocate space
10645 for it, invalidating all copies of pointers into the previous
10646 contents. */
10647 *dwo_file_slot = dwo_file;
10648 }
10649 else
10650 {
b4f54984 10651 if (dwarf_read_debug)
73869dc2
DE
10652 {
10653 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 10654 virtual_dwo_name.c_str ());
73869dc2 10655 }
9a3c8263 10656 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 10657 }
73869dc2
DE
10658
10659 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10660 dwo_unit->dwo_file = dwo_file;
10661 dwo_unit->signature = signature;
8d749320
SM
10662 dwo_unit->section =
10663 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10664 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10665 ? &dwp_file->sections.types
10666 : &dwp_file->sections.info,
10667 sections.info_or_types_offset,
10668 sections.info_or_types_size);
10669 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10670
10671 return dwo_unit;
10672}
10673
57d63ce2
DE
10674/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10675 Returns NULL if the signature isn't found. */
80626a55
DE
10676
10677static struct dwo_unit *
57d63ce2
DE
10678lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10679 ULONGEST signature, int is_debug_types)
80626a55 10680{
57d63ce2
DE
10681 const struct dwp_hash_table *dwp_htab =
10682 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10683 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10684 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10685 uint32_t hash = signature & mask;
10686 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10687 unsigned int i;
10688 void **slot;
870f88f7 10689 struct dwo_unit find_dwo_cu;
80626a55
DE
10690
10691 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10692 find_dwo_cu.signature = signature;
19ac8c2e
DE
10693 slot = htab_find_slot (is_debug_types
10694 ? dwp_file->loaded_tus
10695 : dwp_file->loaded_cus,
10696 &find_dwo_cu, INSERT);
80626a55
DE
10697
10698 if (*slot != NULL)
9a3c8263 10699 return (struct dwo_unit *) *slot;
80626a55
DE
10700
10701 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10702 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10703 {
10704 ULONGEST signature_in_table;
10705
10706 signature_in_table =
57d63ce2 10707 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10708 if (signature_in_table == signature)
10709 {
57d63ce2
DE
10710 uint32_t unit_index =
10711 read_4_bytes (dbfd,
10712 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10713
73869dc2
DE
10714 if (dwp_file->version == 1)
10715 {
10716 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10717 comp_dir, signature,
10718 is_debug_types);
10719 }
10720 else
10721 {
10722 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10723 comp_dir, signature,
10724 is_debug_types);
10725 }
9a3c8263 10726 return (struct dwo_unit *) *slot;
80626a55
DE
10727 }
10728 if (signature_in_table == 0)
10729 return NULL;
10730 hash = (hash + hash2) & mask;
10731 }
10732
10733 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10734 " [in module %s]"),
10735 dwp_file->name);
10736}
10737
ab5088bf 10738/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10739 Open the file specified by FILE_NAME and hand it off to BFD for
10740 preliminary analysis. Return a newly initialized bfd *, which
10741 includes a canonicalized copy of FILE_NAME.
80626a55 10742 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10743 SEARCH_CWD is true if the current directory is to be searched.
10744 It will be searched before debug-file-directory.
13aaf454
DE
10745 If successful, the file is added to the bfd include table of the
10746 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10747 If unable to find/open the file, return NULL.
3019eac3
DE
10748 NOTE: This function is derived from symfile_bfd_open. */
10749
192b62ce 10750static gdb_bfd_ref_ptr
6ac97d4c 10751try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10752{
80626a55 10753 int desc, flags;
3019eac3 10754 char *absolute_name;
9c02c129
DE
10755 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10756 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10757 to debug_file_directory. */
10758 char *search_path;
10759 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10760
6ac97d4c
DE
10761 if (search_cwd)
10762 {
10763 if (*debug_file_directory != '\0')
10764 search_path = concat (".", dirname_separator_string,
b36cec19 10765 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10766 else
10767 search_path = xstrdup (".");
10768 }
9c02c129 10769 else
6ac97d4c 10770 search_path = xstrdup (debug_file_directory);
3019eac3 10771
492c0ab7 10772 flags = OPF_RETURN_REALPATH;
80626a55
DE
10773 if (is_dwp)
10774 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10775 desc = openp (search_path, flags, file_name,
3019eac3 10776 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10777 xfree (search_path);
3019eac3
DE
10778 if (desc < 0)
10779 return NULL;
10780
192b62ce 10781 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10782 xfree (absolute_name);
9c02c129
DE
10783 if (sym_bfd == NULL)
10784 return NULL;
192b62ce 10785 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10786
192b62ce
TT
10787 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10788 return NULL;
3019eac3 10789
13aaf454
DE
10790 /* Success. Record the bfd as having been included by the objfile's bfd.
10791 This is important because things like demangled_names_hash lives in the
10792 objfile's per_bfd space and may have references to things like symbol
10793 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10794 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10795
3019eac3
DE
10796 return sym_bfd;
10797}
10798
ab5088bf 10799/* Try to open DWO file FILE_NAME.
3019eac3
DE
10800 COMP_DIR is the DW_AT_comp_dir attribute.
10801 The result is the bfd handle of the file.
10802 If there is a problem finding or opening the file, return NULL.
10803 Upon success, the canonicalized path of the file is stored in the bfd,
10804 same as symfile_bfd_open. */
10805
192b62ce 10806static gdb_bfd_ref_ptr
ab5088bf 10807open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10808{
80626a55 10809 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10810 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10811
10812 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10813
10814 if (comp_dir != NULL)
10815 {
b36cec19
PA
10816 char *path_to_try = concat (comp_dir, SLASH_STRING,
10817 file_name, (char *) NULL);
3019eac3
DE
10818
10819 /* NOTE: If comp_dir is a relative path, this will also try the
10820 search path, which seems useful. */
192b62ce
TT
10821 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10822 1 /*search_cwd*/));
3019eac3
DE
10823 xfree (path_to_try);
10824 if (abfd != NULL)
10825 return abfd;
10826 }
10827
10828 /* That didn't work, try debug-file-directory, which, despite its name,
10829 is a list of paths. */
10830
10831 if (*debug_file_directory == '\0')
10832 return NULL;
10833
6ac97d4c 10834 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10835}
10836
80626a55
DE
10837/* This function is mapped across the sections and remembers the offset and
10838 size of each of the DWO debugging sections we are interested in. */
10839
10840static void
10841dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10842{
9a3c8263 10843 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10844 const struct dwop_section_names *names = &dwop_section_names;
10845
10846 if (section_is_p (sectp->name, &names->abbrev_dwo))
10847 {
049412e3 10848 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10849 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10850 }
10851 else if (section_is_p (sectp->name, &names->info_dwo))
10852 {
049412e3 10853 dwo_sections->info.s.section = sectp;
80626a55
DE
10854 dwo_sections->info.size = bfd_get_section_size (sectp);
10855 }
10856 else if (section_is_p (sectp->name, &names->line_dwo))
10857 {
049412e3 10858 dwo_sections->line.s.section = sectp;
80626a55
DE
10859 dwo_sections->line.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->loc_dwo))
10862 {
049412e3 10863 dwo_sections->loc.s.section = sectp;
80626a55
DE
10864 dwo_sections->loc.size = bfd_get_section_size (sectp);
10865 }
10866 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10867 {
049412e3 10868 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10869 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10870 }
10871 else if (section_is_p (sectp->name, &names->macro_dwo))
10872 {
049412e3 10873 dwo_sections->macro.s.section = sectp;
80626a55
DE
10874 dwo_sections->macro.size = bfd_get_section_size (sectp);
10875 }
10876 else if (section_is_p (sectp->name, &names->str_dwo))
10877 {
049412e3 10878 dwo_sections->str.s.section = sectp;
80626a55
DE
10879 dwo_sections->str.size = bfd_get_section_size (sectp);
10880 }
10881 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10882 {
049412e3 10883 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10884 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10885 }
10886 else if (section_is_p (sectp->name, &names->types_dwo))
10887 {
10888 struct dwarf2_section_info type_section;
10889
10890 memset (&type_section, 0, sizeof (type_section));
049412e3 10891 type_section.s.section = sectp;
80626a55
DE
10892 type_section.size = bfd_get_section_size (sectp);
10893 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10894 &type_section);
10895 }
10896}
10897
ab5088bf 10898/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10899 by PER_CU. This is for the non-DWP case.
80626a55 10900 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10901
10902static struct dwo_file *
0ac5b59e
DE
10903open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10904 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10905{
10906 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10907 struct dwo_file *dwo_file;
3019eac3
DE
10908 struct cleanup *cleanups;
10909
192b62ce 10910 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10911 if (dbfd == NULL)
10912 {
b4f54984 10913 if (dwarf_read_debug)
80626a55
DE
10914 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10915 return NULL;
10916 }
10917 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10918 dwo_file->dwo_name = dwo_name;
10919 dwo_file->comp_dir = comp_dir;
192b62ce 10920 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10921
10922 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10923
192b62ce
TT
10924 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10925 &dwo_file->sections);
3019eac3 10926
33c5cd75 10927 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 10928
78d4d2c5
JK
10929 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10930 dwo_file->tus);
3019eac3
DE
10931
10932 discard_cleanups (cleanups);
10933
b4f54984 10934 if (dwarf_read_debug)
80626a55
DE
10935 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10936
3019eac3
DE
10937 return dwo_file;
10938}
10939
80626a55 10940/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10941 size of each of the DWP debugging sections common to version 1 and 2 that
10942 we are interested in. */
3019eac3 10943
80626a55 10944static void
73869dc2
DE
10945dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10946 void *dwp_file_ptr)
3019eac3 10947{
9a3c8263 10948 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10949 const struct dwop_section_names *names = &dwop_section_names;
10950 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10951
80626a55 10952 /* Record the ELF section number for later lookup: this is what the
73869dc2 10953 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10954 gdb_assert (elf_section_nr < dwp_file->num_sections);
10955 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10956
80626a55
DE
10957 /* Look for specific sections that we need. */
10958 if (section_is_p (sectp->name, &names->str_dwo))
10959 {
049412e3 10960 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10961 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10962 }
10963 else if (section_is_p (sectp->name, &names->cu_index))
10964 {
049412e3 10965 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10966 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10967 }
10968 else if (section_is_p (sectp->name, &names->tu_index))
10969 {
049412e3 10970 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10971 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10972 }
10973}
3019eac3 10974
73869dc2
DE
10975/* This function is mapped across the sections and remembers the offset and
10976 size of each of the DWP version 2 debugging sections that we are interested
10977 in. This is split into a separate function because we don't know if we
10978 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10979
10980static void
10981dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10982{
9a3c8263 10983 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10984 const struct dwop_section_names *names = &dwop_section_names;
10985 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10986
10987 /* Record the ELF section number for later lookup: this is what the
10988 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10989 gdb_assert (elf_section_nr < dwp_file->num_sections);
10990 dwp_file->elf_sections[elf_section_nr] = sectp;
10991
10992 /* Look for specific sections that we need. */
10993 if (section_is_p (sectp->name, &names->abbrev_dwo))
10994 {
049412e3 10995 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10996 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10997 }
10998 else if (section_is_p (sectp->name, &names->info_dwo))
10999 {
049412e3 11000 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11001 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11002 }
11003 else if (section_is_p (sectp->name, &names->line_dwo))
11004 {
049412e3 11005 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11006 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11007 }
11008 else if (section_is_p (sectp->name, &names->loc_dwo))
11009 {
049412e3 11010 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11011 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11012 }
11013 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11014 {
049412e3 11015 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11016 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11017 }
11018 else if (section_is_p (sectp->name, &names->macro_dwo))
11019 {
049412e3 11020 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11021 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11022 }
11023 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11024 {
049412e3 11025 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11026 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11027 }
11028 else if (section_is_p (sectp->name, &names->types_dwo))
11029 {
049412e3 11030 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11031 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11032 }
11033}
11034
80626a55 11035/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11036
80626a55
DE
11037static hashval_t
11038hash_dwp_loaded_cutus (const void *item)
11039{
9a3c8263 11040 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11041
80626a55
DE
11042 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11043 return dwo_unit->signature;
3019eac3
DE
11044}
11045
80626a55 11046/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11047
80626a55
DE
11048static int
11049eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11050{
9a3c8263
SM
11051 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11052 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11053
80626a55
DE
11054 return dua->signature == dub->signature;
11055}
3019eac3 11056
80626a55 11057/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11058
80626a55
DE
11059static htab_t
11060allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11061{
11062 return htab_create_alloc_ex (3,
11063 hash_dwp_loaded_cutus,
11064 eq_dwp_loaded_cutus,
11065 NULL,
11066 &objfile->objfile_obstack,
11067 hashtab_obstack_allocate,
11068 dummy_obstack_deallocate);
11069}
3019eac3 11070
ab5088bf
DE
11071/* Try to open DWP file FILE_NAME.
11072 The result is the bfd handle of the file.
11073 If there is a problem finding or opening the file, return NULL.
11074 Upon success, the canonicalized path of the file is stored in the bfd,
11075 same as symfile_bfd_open. */
11076
192b62ce 11077static gdb_bfd_ref_ptr
ab5088bf
DE
11078open_dwp_file (const char *file_name)
11079{
192b62ce
TT
11080 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11081 1 /*search_cwd*/));
6ac97d4c
DE
11082 if (abfd != NULL)
11083 return abfd;
11084
11085 /* Work around upstream bug 15652.
11086 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11087 [Whether that's a "bug" is debatable, but it is getting in our way.]
11088 We have no real idea where the dwp file is, because gdb's realpath-ing
11089 of the executable's path may have discarded the needed info.
11090 [IWBN if the dwp file name was recorded in the executable, akin to
11091 .gnu_debuglink, but that doesn't exist yet.]
11092 Strip the directory from FILE_NAME and search again. */
11093 if (*debug_file_directory != '\0')
11094 {
11095 /* Don't implicitly search the current directory here.
11096 If the user wants to search "." to handle this case,
11097 it must be added to debug-file-directory. */
11098 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11099 0 /*search_cwd*/);
11100 }
11101
11102 return NULL;
ab5088bf
DE
11103}
11104
80626a55
DE
11105/* Initialize the use of the DWP file for the current objfile.
11106 By convention the name of the DWP file is ${objfile}.dwp.
11107 The result is NULL if it can't be found. */
a766d390 11108
80626a55 11109static struct dwp_file *
ab5088bf 11110open_and_init_dwp_file (void)
80626a55
DE
11111{
11112 struct objfile *objfile = dwarf2_per_objfile->objfile;
11113 struct dwp_file *dwp_file;
80626a55 11114
82bf32bc
JK
11115 /* Try to find first .dwp for the binary file before any symbolic links
11116 resolving. */
6c447423
DE
11117
11118 /* If the objfile is a debug file, find the name of the real binary
11119 file and get the name of dwp file from there. */
d721ba37 11120 std::string dwp_name;
6c447423
DE
11121 if (objfile->separate_debug_objfile_backlink != NULL)
11122 {
11123 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11124 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11125
d721ba37 11126 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11127 }
11128 else
d721ba37
PA
11129 dwp_name = objfile->original_name;
11130
11131 dwp_name += ".dwp";
80626a55 11132
d721ba37 11133 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11134 if (dbfd == NULL
11135 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11136 {
11137 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11138 dwp_name = objfile_name (objfile);
11139 dwp_name += ".dwp";
11140 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11141 }
11142
80626a55
DE
11143 if (dbfd == NULL)
11144 {
b4f54984 11145 if (dwarf_read_debug)
d721ba37 11146 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11147 return NULL;
3019eac3 11148 }
80626a55 11149 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11150 dwp_file->name = bfd_get_filename (dbfd.get ());
11151 dwp_file->dbfd = dbfd.release ();
c906108c 11152
80626a55 11153 /* +1: section 0 is unused */
192b62ce 11154 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11155 dwp_file->elf_sections =
11156 OBSTACK_CALLOC (&objfile->objfile_obstack,
11157 dwp_file->num_sections, asection *);
11158
192b62ce
TT
11159 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11160 dwp_file);
80626a55
DE
11161
11162 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11163
11164 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11165
73869dc2 11166 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11167 if (dwp_file->cus && dwp_file->tus
11168 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11169 {
11170 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11171 pretty bizarre. We use pulongest here because that's the established
4d65956b 11172 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11173 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11174 " TU version %s [in DWP file %s]"),
11175 pulongest (dwp_file->cus->version),
d721ba37 11176 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11177 }
08302ed2
DE
11178
11179 if (dwp_file->cus)
11180 dwp_file->version = dwp_file->cus->version;
11181 else if (dwp_file->tus)
11182 dwp_file->version = dwp_file->tus->version;
11183 else
11184 dwp_file->version = 2;
73869dc2
DE
11185
11186 if (dwp_file->version == 2)
192b62ce
TT
11187 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11188 dwp_file);
73869dc2 11189
19ac8c2e
DE
11190 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11191 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11192
b4f54984 11193 if (dwarf_read_debug)
80626a55
DE
11194 {
11195 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11196 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11197 " %s CUs, %s TUs\n",
11198 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11199 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11200 }
11201
11202 return dwp_file;
3019eac3 11203}
c906108c 11204
ab5088bf
DE
11205/* Wrapper around open_and_init_dwp_file, only open it once. */
11206
11207static struct dwp_file *
11208get_dwp_file (void)
11209{
11210 if (! dwarf2_per_objfile->dwp_checked)
11211 {
11212 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11213 dwarf2_per_objfile->dwp_checked = 1;
11214 }
11215 return dwarf2_per_objfile->dwp_file;
11216}
11217
80626a55
DE
11218/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11219 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11220 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11221 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11222 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11223
11224 This is called, for example, when wanting to read a variable with a
11225 complex location. Therefore we don't want to do file i/o for every call.
11226 Therefore we don't want to look for a DWO file on every call.
11227 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11228 then we check if we've already seen DWO_NAME, and only THEN do we check
11229 for a DWO file.
11230
1c658ad5 11231 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11232 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11233
3019eac3 11234static struct dwo_unit *
80626a55
DE
11235lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11236 const char *dwo_name, const char *comp_dir,
11237 ULONGEST signature, int is_debug_types)
3019eac3
DE
11238{
11239 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11240 const char *kind = is_debug_types ? "TU" : "CU";
11241 void **dwo_file_slot;
3019eac3 11242 struct dwo_file *dwo_file;
80626a55 11243 struct dwp_file *dwp_file;
cb1df416 11244
6a506a2d
DE
11245 /* First see if there's a DWP file.
11246 If we have a DWP file but didn't find the DWO inside it, don't
11247 look for the original DWO file. It makes gdb behave differently
11248 depending on whether one is debugging in the build tree. */
cf2c3c16 11249
ab5088bf 11250 dwp_file = get_dwp_file ();
80626a55 11251 if (dwp_file != NULL)
cf2c3c16 11252 {
80626a55
DE
11253 const struct dwp_hash_table *dwp_htab =
11254 is_debug_types ? dwp_file->tus : dwp_file->cus;
11255
11256 if (dwp_htab != NULL)
11257 {
11258 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11259 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11260 signature, is_debug_types);
80626a55
DE
11261
11262 if (dwo_cutu != NULL)
11263 {
b4f54984 11264 if (dwarf_read_debug)
80626a55
DE
11265 {
11266 fprintf_unfiltered (gdb_stdlog,
11267 "Virtual DWO %s %s found: @%s\n",
11268 kind, hex_string (signature),
11269 host_address_to_string (dwo_cutu));
11270 }
11271 return dwo_cutu;
11272 }
11273 }
11274 }
6a506a2d 11275 else
80626a55 11276 {
6a506a2d 11277 /* No DWP file, look for the DWO file. */
80626a55 11278
6a506a2d
DE
11279 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11280 if (*dwo_file_slot == NULL)
80626a55 11281 {
6a506a2d
DE
11282 /* Read in the file and build a table of the CUs/TUs it contains. */
11283 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11284 }
6a506a2d 11285 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11286 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11287
6a506a2d 11288 if (dwo_file != NULL)
19c3d4c9 11289 {
6a506a2d
DE
11290 struct dwo_unit *dwo_cutu = NULL;
11291
11292 if (is_debug_types && dwo_file->tus)
11293 {
11294 struct dwo_unit find_dwo_cutu;
11295
11296 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11297 find_dwo_cutu.signature = signature;
9a3c8263
SM
11298 dwo_cutu
11299 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11300 }
33c5cd75 11301 else if (!is_debug_types && dwo_file->cus)
80626a55 11302 {
33c5cd75
DB
11303 struct dwo_unit find_dwo_cutu;
11304
11305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11306 find_dwo_cutu.signature = signature;
11307 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11308 &find_dwo_cutu);
6a506a2d
DE
11309 }
11310
11311 if (dwo_cutu != NULL)
11312 {
b4f54984 11313 if (dwarf_read_debug)
6a506a2d
DE
11314 {
11315 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11316 kind, dwo_name, hex_string (signature),
11317 host_address_to_string (dwo_cutu));
11318 }
11319 return dwo_cutu;
80626a55
DE
11320 }
11321 }
2e276125 11322 }
9cdd5dbd 11323
80626a55
DE
11324 /* We didn't find it. This could mean a dwo_id mismatch, or
11325 someone deleted the DWO/DWP file, or the search path isn't set up
11326 correctly to find the file. */
11327
b4f54984 11328 if (dwarf_read_debug)
80626a55
DE
11329 {
11330 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11331 kind, dwo_name, hex_string (signature));
11332 }
3019eac3 11333
6656a72d
DE
11334 /* This is a warning and not a complaint because it can be caused by
11335 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11336 {
11337 /* Print the name of the DWP file if we looked there, helps the user
11338 better diagnose the problem. */
791afaa2 11339 std::string dwp_text;
43942612
DE
11340
11341 if (dwp_file != NULL)
791afaa2
TT
11342 dwp_text = string_printf (" [in DWP file %s]",
11343 lbasename (dwp_file->name));
43942612
DE
11344
11345 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11346 " [in module %s]"),
11347 kind, dwo_name, hex_string (signature),
791afaa2 11348 dwp_text.c_str (),
43942612 11349 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11350 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 11351 }
3019eac3 11352 return NULL;
5fb290d7
DJ
11353}
11354
80626a55
DE
11355/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11356 See lookup_dwo_cutu_unit for details. */
11357
11358static struct dwo_unit *
11359lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11360 const char *dwo_name, const char *comp_dir,
11361 ULONGEST signature)
11362{
11363 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11364}
11365
11366/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11367 See lookup_dwo_cutu_unit for details. */
11368
11369static struct dwo_unit *
11370lookup_dwo_type_unit (struct signatured_type *this_tu,
11371 const char *dwo_name, const char *comp_dir)
11372{
11373 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11374}
11375
89e63ee4
DE
11376/* Traversal function for queue_and_load_all_dwo_tus. */
11377
11378static int
11379queue_and_load_dwo_tu (void **slot, void *info)
11380{
11381 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11382 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11383 ULONGEST signature = dwo_unit->signature;
11384 struct signatured_type *sig_type =
11385 lookup_dwo_signatured_type (per_cu->cu, signature);
11386
11387 if (sig_type != NULL)
11388 {
11389 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11390
11391 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11392 a real dependency of PER_CU on SIG_TYPE. That is detected later
11393 while processing PER_CU. */
11394 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11395 load_full_type_unit (sig_cu);
11396 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11397 }
11398
11399 return 1;
11400}
11401
11402/* Queue all TUs contained in the DWO of PER_CU to be read in.
11403 The DWO may have the only definition of the type, though it may not be
11404 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11405 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11406
11407static void
11408queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11409{
11410 struct dwo_unit *dwo_unit;
11411 struct dwo_file *dwo_file;
11412
11413 gdb_assert (!per_cu->is_debug_types);
11414 gdb_assert (get_dwp_file () == NULL);
11415 gdb_assert (per_cu->cu != NULL);
11416
11417 dwo_unit = per_cu->cu->dwo_unit;
11418 gdb_assert (dwo_unit != NULL);
11419
11420 dwo_file = dwo_unit->dwo_file;
11421 if (dwo_file->tus != NULL)
11422 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11423}
11424
3019eac3
DE
11425/* Free all resources associated with DWO_FILE.
11426 Close the DWO file and munmap the sections.
11427 All memory should be on the objfile obstack. */
348e048f
DE
11428
11429static void
3019eac3 11430free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11431{
348e048f 11432
5c6fa7ab 11433 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11434 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11435
3019eac3
DE
11436 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11437}
348e048f 11438
3019eac3 11439/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11440
3019eac3
DE
11441static void
11442free_dwo_file_cleanup (void *arg)
11443{
11444 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11445 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11446
3019eac3
DE
11447 free_dwo_file (dwo_file, objfile);
11448}
348e048f 11449
3019eac3 11450/* Traversal function for free_dwo_files. */
2ab95328 11451
3019eac3
DE
11452static int
11453free_dwo_file_from_slot (void **slot, void *info)
11454{
11455 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11456 struct objfile *objfile = (struct objfile *) info;
348e048f 11457
3019eac3 11458 free_dwo_file (dwo_file, objfile);
348e048f 11459
3019eac3
DE
11460 return 1;
11461}
348e048f 11462
3019eac3 11463/* Free all resources associated with DWO_FILES. */
348e048f 11464
3019eac3
DE
11465static void
11466free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11467{
11468 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11469}
3019eac3
DE
11470\f
11471/* Read in various DIEs. */
348e048f 11472
d389af10 11473/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11474 Inherit only the children of the DW_AT_abstract_origin DIE not being
11475 already referenced by DW_AT_abstract_origin from the children of the
11476 current DIE. */
d389af10
JK
11477
11478static void
11479inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11480{
11481 struct die_info *child_die;
791afaa2 11482 sect_offset *offsetp;
d389af10
JK
11483 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11484 struct die_info *origin_die;
11485 /* Iterator of the ORIGIN_DIE children. */
11486 struct die_info *origin_child_die;
d389af10 11487 struct attribute *attr;
cd02d79d
PA
11488 struct dwarf2_cu *origin_cu;
11489 struct pending **origin_previous_list_in_scope;
d389af10
JK
11490
11491 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11492 if (!attr)
11493 return;
11494
cd02d79d
PA
11495 /* Note that following die references may follow to a die in a
11496 different cu. */
11497
11498 origin_cu = cu;
11499 origin_die = follow_die_ref (die, attr, &origin_cu);
11500
11501 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11502 symbols in. */
11503 origin_previous_list_in_scope = origin_cu->list_in_scope;
11504 origin_cu->list_in_scope = cu->list_in_scope;
11505
edb3359d
DJ
11506 if (die->tag != origin_die->tag
11507 && !(die->tag == DW_TAG_inlined_subroutine
11508 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11509 complaint (&symfile_complaints,
11510 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11511 to_underlying (die->sect_off),
11512 to_underlying (origin_die->sect_off));
d389af10 11513
791afaa2 11514 std::vector<sect_offset> offsets;
d389af10 11515
3ea89b92
PMR
11516 for (child_die = die->child;
11517 child_die && child_die->tag;
11518 child_die = sibling_die (child_die))
11519 {
11520 struct die_info *child_origin_die;
11521 struct dwarf2_cu *child_origin_cu;
11522
11523 /* We are trying to process concrete instance entries:
216f72a1 11524 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11525 it's not relevant to our analysis here. i.e. detecting DIEs that are
11526 present in the abstract instance but not referenced in the concrete
11527 one. */
216f72a1
JK
11528 if (child_die->tag == DW_TAG_call_site
11529 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11530 continue;
11531
c38f313d
DJ
11532 /* For each CHILD_DIE, find the corresponding child of
11533 ORIGIN_DIE. If there is more than one layer of
11534 DW_AT_abstract_origin, follow them all; there shouldn't be,
11535 but GCC versions at least through 4.4 generate this (GCC PR
11536 40573). */
3ea89b92
PMR
11537 child_origin_die = child_die;
11538 child_origin_cu = cu;
c38f313d
DJ
11539 while (1)
11540 {
cd02d79d
PA
11541 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11542 child_origin_cu);
c38f313d
DJ
11543 if (attr == NULL)
11544 break;
cd02d79d
PA
11545 child_origin_die = follow_die_ref (child_origin_die, attr,
11546 &child_origin_cu);
c38f313d
DJ
11547 }
11548
d389af10
JK
11549 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11550 counterpart may exist. */
c38f313d 11551 if (child_origin_die != child_die)
d389af10 11552 {
edb3359d
DJ
11553 if (child_die->tag != child_origin_die->tag
11554 && !(child_die->tag == DW_TAG_inlined_subroutine
11555 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11556 complaint (&symfile_complaints,
11557 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11558 "different tags"),
11559 to_underlying (child_die->sect_off),
11560 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11561 if (child_origin_die->parent != origin_die)
11562 complaint (&symfile_complaints,
11563 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11564 "different parents"),
11565 to_underlying (child_die->sect_off),
11566 to_underlying (child_origin_die->sect_off));
c38f313d 11567 else
791afaa2 11568 offsets.push_back (child_origin_die->sect_off);
d389af10 11569 }
d389af10 11570 }
791afaa2
TT
11571 std::sort (offsets.begin (), offsets.end ());
11572 sect_offset *offsets_end = offsets.data () + offsets.size ();
11573 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 11574 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11575 complaint (&symfile_complaints,
11576 _("Multiple children of DIE 0x%x refer "
11577 "to DIE 0x%x as their abstract origin"),
9c541725 11578 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 11579
791afaa2 11580 offsetp = offsets.data ();
d389af10
JK
11581 origin_child_die = origin_die->child;
11582 while (origin_child_die && origin_child_die->tag)
11583 {
11584 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11585 while (offsetp < offsets_end
9c541725 11586 && *offsetp < origin_child_die->sect_off)
d389af10 11587 offsetp++;
b64f50a1 11588 if (offsetp >= offsets_end
9c541725 11589 || *offsetp > origin_child_die->sect_off)
d389af10 11590 {
adde2bff
DE
11591 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11592 Check whether we're already processing ORIGIN_CHILD_DIE.
11593 This can happen with mutually referenced abstract_origins.
11594 PR 16581. */
11595 if (!origin_child_die->in_process)
11596 process_die (origin_child_die, origin_cu);
d389af10
JK
11597 }
11598 origin_child_die = sibling_die (origin_child_die);
11599 }
cd02d79d 11600 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11601}
11602
c906108c 11603static void
e7c27a73 11604read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11605{
e7c27a73 11606 struct objfile *objfile = cu->objfile;
3e29f34a 11607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11608 struct context_stack *newobj;
c906108c
SS
11609 CORE_ADDR lowpc;
11610 CORE_ADDR highpc;
11611 struct die_info *child_die;
edb3359d 11612 struct attribute *attr, *call_line, *call_file;
15d034d0 11613 const char *name;
e142c38c 11614 CORE_ADDR baseaddr;
801e3a5b 11615 struct block *block;
edb3359d 11616 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11617 VEC (symbolp) *template_args = NULL;
11618 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11619
11620 if (inlined_func)
11621 {
11622 /* If we do not have call site information, we can't show the
11623 caller of this inlined function. That's too confusing, so
11624 only use the scope for local variables. */
11625 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11626 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11627 if (call_line == NULL || call_file == NULL)
11628 {
11629 read_lexical_block_scope (die, cu);
11630 return;
11631 }
11632 }
c906108c 11633
e142c38c
DJ
11634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11635
94af9270 11636 name = dwarf2_name (die, cu);
c906108c 11637
e8d05480
JB
11638 /* Ignore functions with missing or empty names. These are actually
11639 illegal according to the DWARF standard. */
11640 if (name == NULL)
11641 {
11642 complaint (&symfile_complaints,
b64f50a1 11643 _("missing name for subprogram DIE at %d"),
9c541725 11644 to_underlying (die->sect_off));
e8d05480
JB
11645 return;
11646 }
11647
11648 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11649 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11650 <= PC_BOUNDS_INVALID)
e8d05480 11651 {
ae4d0c03
PM
11652 attr = dwarf2_attr (die, DW_AT_external, cu);
11653 if (!attr || !DW_UNSND (attr))
11654 complaint (&symfile_complaints,
3e43a32a
MS
11655 _("cannot get low and high bounds "
11656 "for subprogram DIE at %d"),
9c541725 11657 to_underlying (die->sect_off));
e8d05480
JB
11658 return;
11659 }
c906108c 11660
3e29f34a
MR
11661 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11662 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11663
34eaf542
TT
11664 /* If we have any template arguments, then we must allocate a
11665 different sort of symbol. */
11666 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11667 {
11668 if (child_die->tag == DW_TAG_template_type_param
11669 || child_die->tag == DW_TAG_template_value_param)
11670 {
e623cf5d 11671 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11672 templ_func->base.is_cplus_template_function = 1;
11673 break;
11674 }
11675 }
11676
fe978cb0
PA
11677 newobj = push_context (0, lowpc);
11678 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11679 (struct symbol *) templ_func);
4c2df51b 11680
4cecd739
DJ
11681 /* If there is a location expression for DW_AT_frame_base, record
11682 it. */
e142c38c 11683 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11684 if (attr)
fe978cb0 11685 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11686
63e43d3a
PMR
11687 /* If there is a location for the static link, record it. */
11688 newobj->static_link = NULL;
11689 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11690 if (attr)
11691 {
224c3ddb
SM
11692 newobj->static_link
11693 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11694 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11695 }
11696
e142c38c 11697 cu->list_in_scope = &local_symbols;
c906108c 11698
639d11d3 11699 if (die->child != NULL)
c906108c 11700 {
639d11d3 11701 child_die = die->child;
c906108c
SS
11702 while (child_die && child_die->tag)
11703 {
34eaf542
TT
11704 if (child_die->tag == DW_TAG_template_type_param
11705 || child_die->tag == DW_TAG_template_value_param)
11706 {
11707 struct symbol *arg = new_symbol (child_die, NULL, cu);
11708
f1078f66
DJ
11709 if (arg != NULL)
11710 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11711 }
11712 else
11713 process_die (child_die, cu);
c906108c
SS
11714 child_die = sibling_die (child_die);
11715 }
11716 }
11717
d389af10
JK
11718 inherit_abstract_dies (die, cu);
11719
4a811a97
UW
11720 /* If we have a DW_AT_specification, we might need to import using
11721 directives from the context of the specification DIE. See the
11722 comment in determine_prefix. */
11723 if (cu->language == language_cplus
11724 && dwarf2_attr (die, DW_AT_specification, cu))
11725 {
11726 struct dwarf2_cu *spec_cu = cu;
11727 struct die_info *spec_die = die_specification (die, &spec_cu);
11728
11729 while (spec_die)
11730 {
11731 child_die = spec_die->child;
11732 while (child_die && child_die->tag)
11733 {
11734 if (child_die->tag == DW_TAG_imported_module)
11735 process_die (child_die, spec_cu);
11736 child_die = sibling_die (child_die);
11737 }
11738
11739 /* In some cases, GCC generates specification DIEs that
11740 themselves contain DW_AT_specification attributes. */
11741 spec_die = die_specification (spec_die, &spec_cu);
11742 }
11743 }
11744
fe978cb0 11745 newobj = pop_context ();
c906108c 11746 /* Make a block for the local symbols within. */
fe978cb0 11747 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11748 newobj->static_link, lowpc, highpc);
801e3a5b 11749
df8a16a1 11750 /* For C++, set the block's scope. */
45280282
IB
11751 if ((cu->language == language_cplus
11752 || cu->language == language_fortran
c44af4eb
TT
11753 || cu->language == language_d
11754 || cu->language == language_rust)
4d4ec4e5 11755 && cu->processing_has_namespace_info)
195a3f6c
TT
11756 block_set_scope (block, determine_prefix (die, cu),
11757 &objfile->objfile_obstack);
df8a16a1 11758
801e3a5b
JB
11759 /* If we have address ranges, record them. */
11760 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11761
fe978cb0 11762 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11763
34eaf542
TT
11764 /* Attach template arguments to function. */
11765 if (! VEC_empty (symbolp, template_args))
11766 {
11767 gdb_assert (templ_func != NULL);
11768
11769 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11770 templ_func->template_arguments
8d749320
SM
11771 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11772 templ_func->n_template_arguments);
34eaf542
TT
11773 memcpy (templ_func->template_arguments,
11774 VEC_address (symbolp, template_args),
11775 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11776 VEC_free (symbolp, template_args);
11777 }
11778
208d8187
JB
11779 /* In C++, we can have functions nested inside functions (e.g., when
11780 a function declares a class that has methods). This means that
11781 when we finish processing a function scope, we may need to go
11782 back to building a containing block's symbol lists. */
fe978cb0 11783 local_symbols = newobj->locals;
22cee43f 11784 local_using_directives = newobj->local_using_directives;
208d8187 11785
921e78cf
JB
11786 /* If we've finished processing a top-level function, subsequent
11787 symbols go in the file symbol list. */
11788 if (outermost_context_p ())
e142c38c 11789 cu->list_in_scope = &file_symbols;
c906108c
SS
11790}
11791
11792/* Process all the DIES contained within a lexical block scope. Start
11793 a new scope, process the dies, and then close the scope. */
11794
11795static void
e7c27a73 11796read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11797{
e7c27a73 11798 struct objfile *objfile = cu->objfile;
3e29f34a 11799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11800 struct context_stack *newobj;
c906108c
SS
11801 CORE_ADDR lowpc, highpc;
11802 struct die_info *child_die;
e142c38c
DJ
11803 CORE_ADDR baseaddr;
11804
11805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11806
11807 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11808 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11809 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11810 be nasty. Might be easier to properly extend generic blocks to
af34e669 11811 describe ranges. */
e385593e
JK
11812 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11813 {
11814 case PC_BOUNDS_NOT_PRESENT:
11815 /* DW_TAG_lexical_block has no attributes, process its children as if
11816 there was no wrapping by that DW_TAG_lexical_block.
11817 GCC does no longer produces such DWARF since GCC r224161. */
11818 for (child_die = die->child;
11819 child_die != NULL && child_die->tag;
11820 child_die = sibling_die (child_die))
11821 process_die (child_die, cu);
11822 return;
11823 case PC_BOUNDS_INVALID:
11824 return;
11825 }
3e29f34a
MR
11826 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11827 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11828
11829 push_context (0, lowpc);
639d11d3 11830 if (die->child != NULL)
c906108c 11831 {
639d11d3 11832 child_die = die->child;
c906108c
SS
11833 while (child_die && child_die->tag)
11834 {
e7c27a73 11835 process_die (child_die, cu);
c906108c
SS
11836 child_die = sibling_die (child_die);
11837 }
11838 }
3ea89b92 11839 inherit_abstract_dies (die, cu);
fe978cb0 11840 newobj = pop_context ();
c906108c 11841
22cee43f 11842 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11843 {
801e3a5b 11844 struct block *block
63e43d3a 11845 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11846 newobj->start_addr, highpc);
801e3a5b
JB
11847
11848 /* Note that recording ranges after traversing children, as we
11849 do here, means that recording a parent's ranges entails
11850 walking across all its children's ranges as they appear in
11851 the address map, which is quadratic behavior.
11852
11853 It would be nicer to record the parent's ranges before
11854 traversing its children, simply overriding whatever you find
11855 there. But since we don't even decide whether to create a
11856 block until after we've traversed its children, that's hard
11857 to do. */
11858 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11859 }
fe978cb0 11860 local_symbols = newobj->locals;
22cee43f 11861 local_using_directives = newobj->local_using_directives;
c906108c
SS
11862}
11863
216f72a1 11864/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11865
11866static void
11867read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11868{
11869 struct objfile *objfile = cu->objfile;
11870 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11871 CORE_ADDR pc, baseaddr;
11872 struct attribute *attr;
11873 struct call_site *call_site, call_site_local;
11874 void **slot;
11875 int nparams;
11876 struct die_info *child_die;
11877
11878 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11879
216f72a1
JK
11880 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11881 if (attr == NULL)
11882 {
11883 /* This was a pre-DWARF-5 GNU extension alias
11884 for DW_AT_call_return_pc. */
11885 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11886 }
96408a79
SA
11887 if (!attr)
11888 {
11889 complaint (&symfile_complaints,
216f72a1 11890 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11891 "DIE 0x%x [in module %s]"),
9c541725 11892 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11893 return;
11894 }
31aa7e4e 11895 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11896 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11897
11898 if (cu->call_site_htab == NULL)
11899 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11900 NULL, &objfile->objfile_obstack,
11901 hashtab_obstack_allocate, NULL);
11902 call_site_local.pc = pc;
11903 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11904 if (*slot != NULL)
11905 {
11906 complaint (&symfile_complaints,
216f72a1 11907 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11908 "DIE 0x%x [in module %s]"),
9c541725 11909 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11910 objfile_name (objfile));
96408a79
SA
11911 return;
11912 }
11913
11914 /* Count parameters at the caller. */
11915
11916 nparams = 0;
11917 for (child_die = die->child; child_die && child_die->tag;
11918 child_die = sibling_die (child_die))
11919 {
216f72a1
JK
11920 if (child_die->tag != DW_TAG_call_site_parameter
11921 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11922 {
11923 complaint (&symfile_complaints,
216f72a1
JK
11924 _("Tag %d is not DW_TAG_call_site_parameter in "
11925 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11926 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11927 objfile_name (objfile));
96408a79
SA
11928 continue;
11929 }
11930
11931 nparams++;
11932 }
11933
224c3ddb
SM
11934 call_site
11935 = ((struct call_site *)
11936 obstack_alloc (&objfile->objfile_obstack,
11937 sizeof (*call_site)
11938 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11939 *slot = call_site;
11940 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11941 call_site->pc = pc;
11942
216f72a1
JK
11943 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11944 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11945 {
11946 struct die_info *func_die;
11947
11948 /* Skip also over DW_TAG_inlined_subroutine. */
11949 for (func_die = die->parent;
11950 func_die && func_die->tag != DW_TAG_subprogram
11951 && func_die->tag != DW_TAG_subroutine_type;
11952 func_die = func_die->parent);
11953
216f72a1
JK
11954 /* DW_AT_call_all_calls is a superset
11955 of DW_AT_call_all_tail_calls. */
96408a79 11956 if (func_die
216f72a1 11957 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11958 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11959 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11960 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11961 {
11962 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11963 not complete. But keep CALL_SITE for look ups via call_site_htab,
11964 both the initial caller containing the real return address PC and
11965 the final callee containing the current PC of a chain of tail
11966 calls do not need to have the tail call list complete. But any
11967 function candidate for a virtual tail call frame searched via
11968 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11969 determined unambiguously. */
11970 }
11971 else
11972 {
11973 struct type *func_type = NULL;
11974
11975 if (func_die)
11976 func_type = get_die_type (func_die, cu);
11977 if (func_type != NULL)
11978 {
11979 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11980
11981 /* Enlist this call site to the function. */
11982 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11983 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11984 }
11985 else
11986 complaint (&symfile_complaints,
216f72a1 11987 _("Cannot find function owning DW_TAG_call_site "
96408a79 11988 "DIE 0x%x [in module %s]"),
9c541725 11989 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11990 }
11991 }
11992
216f72a1
JK
11993 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11994 if (attr == NULL)
11995 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11996 if (attr == NULL)
11997 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11998 if (attr == NULL)
216f72a1
JK
11999 {
12000 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12001 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12002 }
96408a79
SA
12003 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12004 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12005 /* Keep NULL DWARF_BLOCK. */;
12006 else if (attr_form_is_block (attr))
12007 {
12008 struct dwarf2_locexpr_baton *dlbaton;
12009
8d749320 12010 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12011 dlbaton->data = DW_BLOCK (attr)->data;
12012 dlbaton->size = DW_BLOCK (attr)->size;
12013 dlbaton->per_cu = cu->per_cu;
12014
12015 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12016 }
7771576e 12017 else if (attr_form_is_ref (attr))
96408a79 12018 {
96408a79
SA
12019 struct dwarf2_cu *target_cu = cu;
12020 struct die_info *target_die;
12021
ac9ec31b 12022 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12023 gdb_assert (target_cu->objfile == objfile);
12024 if (die_is_declaration (target_die, target_cu))
12025 {
7d45c7c3 12026 const char *target_physname;
9112db09
JK
12027
12028 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12029 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12030 if (target_physname == NULL)
9112db09 12031 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12032 if (target_physname == NULL)
12033 complaint (&symfile_complaints,
216f72a1 12034 _("DW_AT_call_target target DIE has invalid "
96408a79 12035 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12036 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12037 else
7d455152 12038 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12039 }
12040 else
12041 {
12042 CORE_ADDR lowpc;
12043
12044 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12045 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12046 <= PC_BOUNDS_INVALID)
96408a79 12047 complaint (&symfile_complaints,
216f72a1 12048 _("DW_AT_call_target target DIE has invalid "
96408a79 12049 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12050 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12051 else
3e29f34a
MR
12052 {
12053 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12054 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12055 }
96408a79
SA
12056 }
12057 }
12058 else
12059 complaint (&symfile_complaints,
216f72a1 12060 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12061 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12062 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12063
12064 call_site->per_cu = cu->per_cu;
12065
12066 for (child_die = die->child;
12067 child_die && child_die->tag;
12068 child_die = sibling_die (child_die))
12069 {
96408a79 12070 struct call_site_parameter *parameter;
1788b2d3 12071 struct attribute *loc, *origin;
96408a79 12072
216f72a1
JK
12073 if (child_die->tag != DW_TAG_call_site_parameter
12074 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12075 {
12076 /* Already printed the complaint above. */
12077 continue;
12078 }
12079
12080 gdb_assert (call_site->parameter_count < nparams);
12081 parameter = &call_site->parameter[call_site->parameter_count];
12082
1788b2d3
JK
12083 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12084 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12085 register is contained in DW_AT_call_value. */
96408a79 12086
24c5c679 12087 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12088 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12089 if (origin == NULL)
12090 {
12091 /* This was a pre-DWARF-5 GNU extension alias
12092 for DW_AT_call_parameter. */
12093 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12094 }
7771576e 12095 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12096 {
1788b2d3 12097 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12098
12099 sect_offset sect_off
12100 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12101 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12102 {
12103 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12104 binding can be done only inside one CU. Such referenced DIE
12105 therefore cannot be even moved to DW_TAG_partial_unit. */
12106 complaint (&symfile_complaints,
216f72a1
JK
12107 _("DW_AT_call_parameter offset is not in CU for "
12108 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12109 to_underlying (child_die->sect_off),
12110 objfile_name (objfile));
d76b7dbc
JK
12111 continue;
12112 }
9c541725
PA
12113 parameter->u.param_cu_off
12114 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12115 }
12116 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12117 {
12118 complaint (&symfile_complaints,
12119 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12120 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12121 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12122 continue;
12123 }
24c5c679 12124 else
96408a79 12125 {
24c5c679
JK
12126 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12127 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12128 if (parameter->u.dwarf_reg != -1)
12129 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12130 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12131 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12132 &parameter->u.fb_offset))
12133 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12134 else
12135 {
12136 complaint (&symfile_complaints,
12137 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12138 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12139 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12140 "[in module %s]"),
9c541725
PA
12141 to_underlying (child_die->sect_off),
12142 objfile_name (objfile));
24c5c679
JK
12143 continue;
12144 }
96408a79
SA
12145 }
12146
216f72a1
JK
12147 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12148 if (attr == NULL)
12149 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12150 if (!attr_form_is_block (attr))
12151 {
12152 complaint (&symfile_complaints,
216f72a1
JK
12153 _("No DW_FORM_block* DW_AT_call_value for "
12154 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12155 to_underlying (child_die->sect_off),
12156 objfile_name (objfile));
96408a79
SA
12157 continue;
12158 }
12159 parameter->value = DW_BLOCK (attr)->data;
12160 parameter->value_size = DW_BLOCK (attr)->size;
12161
12162 /* Parameters are not pre-cleared by memset above. */
12163 parameter->data_value = NULL;
12164 parameter->data_value_size = 0;
12165 call_site->parameter_count++;
12166
216f72a1
JK
12167 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12168 if (attr == NULL)
12169 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12170 if (attr)
12171 {
12172 if (!attr_form_is_block (attr))
12173 complaint (&symfile_complaints,
216f72a1
JK
12174 _("No DW_FORM_block* DW_AT_call_data_value for "
12175 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12176 to_underlying (child_die->sect_off),
12177 objfile_name (objfile));
96408a79
SA
12178 else
12179 {
12180 parameter->data_value = DW_BLOCK (attr)->data;
12181 parameter->data_value_size = DW_BLOCK (attr)->size;
12182 }
12183 }
12184 }
12185}
12186
43988095
JK
12187/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12188 reading .debug_rnglists.
12189 Callback's type should be:
12190 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12191 Return true if the attributes are present and valid, otherwise,
12192 return false. */
12193
12194template <typename Callback>
12195static bool
12196dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12197 Callback &&callback)
12198{
12199 struct objfile *objfile = cu->objfile;
12200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12201 struct comp_unit_head *cu_header = &cu->header;
12202 bfd *obfd = objfile->obfd;
12203 unsigned int addr_size = cu_header->addr_size;
12204 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12205 /* Base address selection entry. */
12206 CORE_ADDR base;
12207 int found_base;
12208 unsigned int dummy;
12209 const gdb_byte *buffer;
12210 CORE_ADDR low = 0;
12211 CORE_ADDR high = 0;
12212 CORE_ADDR baseaddr;
12213 bool overflow = false;
12214
12215 found_base = cu->base_known;
12216 base = cu->base_address;
12217
12218 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12219 if (offset >= dwarf2_per_objfile->rnglists.size)
12220 {
12221 complaint (&symfile_complaints,
12222 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12223 offset);
12224 return false;
12225 }
12226 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12227
12228 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12229
12230 while (1)
12231 {
7814882a
JK
12232 /* Initialize it due to a false compiler warning. */
12233 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12234 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12235 + dwarf2_per_objfile->rnglists.size);
12236 unsigned int bytes_read;
12237
12238 if (buffer == buf_end)
12239 {
12240 overflow = true;
12241 break;
12242 }
12243 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12244 switch (rlet)
12245 {
12246 case DW_RLE_end_of_list:
12247 break;
12248 case DW_RLE_base_address:
12249 if (buffer + cu->header.addr_size > buf_end)
12250 {
12251 overflow = true;
12252 break;
12253 }
12254 base = read_address (obfd, buffer, cu, &bytes_read);
12255 found_base = 1;
12256 buffer += bytes_read;
12257 break;
12258 case DW_RLE_start_length:
12259 if (buffer + cu->header.addr_size > buf_end)
12260 {
12261 overflow = true;
12262 break;
12263 }
12264 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12265 buffer += bytes_read;
12266 range_end = (range_beginning
12267 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12268 buffer += bytes_read;
12269 if (buffer > buf_end)
12270 {
12271 overflow = true;
12272 break;
12273 }
12274 break;
12275 case DW_RLE_offset_pair:
12276 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12277 buffer += bytes_read;
12278 if (buffer > buf_end)
12279 {
12280 overflow = true;
12281 break;
12282 }
12283 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12284 buffer += bytes_read;
12285 if (buffer > buf_end)
12286 {
12287 overflow = true;
12288 break;
12289 }
12290 break;
12291 case DW_RLE_start_end:
12292 if (buffer + 2 * cu->header.addr_size > buf_end)
12293 {
12294 overflow = true;
12295 break;
12296 }
12297 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12298 buffer += bytes_read;
12299 range_end = read_address (obfd, buffer, cu, &bytes_read);
12300 buffer += bytes_read;
12301 break;
12302 default:
12303 complaint (&symfile_complaints,
12304 _("Invalid .debug_rnglists data (no base address)"));
12305 return false;
12306 }
12307 if (rlet == DW_RLE_end_of_list || overflow)
12308 break;
12309 if (rlet == DW_RLE_base_address)
12310 continue;
12311
12312 if (!found_base)
12313 {
12314 /* We have no valid base address for the ranges
12315 data. */
12316 complaint (&symfile_complaints,
12317 _("Invalid .debug_rnglists data (no base address)"));
12318 return false;
12319 }
12320
12321 if (range_beginning > range_end)
12322 {
12323 /* Inverted range entries are invalid. */
12324 complaint (&symfile_complaints,
12325 _("Invalid .debug_rnglists data (inverted range)"));
12326 return false;
12327 }
12328
12329 /* Empty range entries have no effect. */
12330 if (range_beginning == range_end)
12331 continue;
12332
12333 range_beginning += base;
12334 range_end += base;
12335
12336 /* A not-uncommon case of bad debug info.
12337 Don't pollute the addrmap with bad data. */
12338 if (range_beginning + baseaddr == 0
12339 && !dwarf2_per_objfile->has_section_at_zero)
12340 {
12341 complaint (&symfile_complaints,
12342 _(".debug_rnglists entry has start address of zero"
12343 " [in module %s]"), objfile_name (objfile));
12344 continue;
12345 }
12346
12347 callback (range_beginning, range_end);
12348 }
12349
12350 if (overflow)
12351 {
12352 complaint (&symfile_complaints,
12353 _("Offset %d is not terminated "
12354 "for DW_AT_ranges attribute"),
12355 offset);
12356 return false;
12357 }
12358
12359 return true;
12360}
12361
12362/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12363 Callback's type should be:
12364 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12365 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12366
43988095 12367template <typename Callback>
43039443 12368static int
5f46c5a5 12369dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12370 Callback &&callback)
43039443
JK
12371{
12372 struct objfile *objfile = cu->objfile;
3e29f34a 12373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12374 struct comp_unit_head *cu_header = &cu->header;
12375 bfd *obfd = objfile->obfd;
12376 unsigned int addr_size = cu_header->addr_size;
12377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12378 /* Base address selection entry. */
12379 CORE_ADDR base;
12380 int found_base;
12381 unsigned int dummy;
d521ce57 12382 const gdb_byte *buffer;
ff013f42 12383 CORE_ADDR baseaddr;
43039443 12384
43988095
JK
12385 if (cu_header->version >= 5)
12386 return dwarf2_rnglists_process (offset, cu, callback);
12387
d00adf39
DE
12388 found_base = cu->base_known;
12389 base = cu->base_address;
43039443 12390
be391dca 12391 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12392 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12393 {
12394 complaint (&symfile_complaints,
12395 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12396 offset);
12397 return 0;
12398 }
dce234bc 12399 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12400
e7030f15 12401 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12402
43039443
JK
12403 while (1)
12404 {
12405 CORE_ADDR range_beginning, range_end;
12406
12407 range_beginning = read_address (obfd, buffer, cu, &dummy);
12408 buffer += addr_size;
12409 range_end = read_address (obfd, buffer, cu, &dummy);
12410 buffer += addr_size;
12411 offset += 2 * addr_size;
12412
12413 /* An end of list marker is a pair of zero addresses. */
12414 if (range_beginning == 0 && range_end == 0)
12415 /* Found the end of list entry. */
12416 break;
12417
12418 /* Each base address selection entry is a pair of 2 values.
12419 The first is the largest possible address, the second is
12420 the base address. Check for a base address here. */
12421 if ((range_beginning & mask) == mask)
12422 {
28d2bfb9
AB
12423 /* If we found the largest possible address, then we already
12424 have the base address in range_end. */
12425 base = range_end;
43039443
JK
12426 found_base = 1;
12427 continue;
12428 }
12429
12430 if (!found_base)
12431 {
12432 /* We have no valid base address for the ranges
12433 data. */
12434 complaint (&symfile_complaints,
12435 _("Invalid .debug_ranges data (no base address)"));
12436 return 0;
12437 }
12438
9277c30c
UW
12439 if (range_beginning > range_end)
12440 {
12441 /* Inverted range entries are invalid. */
12442 complaint (&symfile_complaints,
12443 _("Invalid .debug_ranges data (inverted range)"));
12444 return 0;
12445 }
12446
12447 /* Empty range entries have no effect. */
12448 if (range_beginning == range_end)
12449 continue;
12450
43039443
JK
12451 range_beginning += base;
12452 range_end += base;
12453
01093045
DE
12454 /* A not-uncommon case of bad debug info.
12455 Don't pollute the addrmap with bad data. */
12456 if (range_beginning + baseaddr == 0
12457 && !dwarf2_per_objfile->has_section_at_zero)
12458 {
12459 complaint (&symfile_complaints,
12460 _(".debug_ranges entry has start address of zero"
4262abfb 12461 " [in module %s]"), objfile_name (objfile));
01093045
DE
12462 continue;
12463 }
12464
5f46c5a5
JK
12465 callback (range_beginning, range_end);
12466 }
12467
12468 return 1;
12469}
12470
12471/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12472 Return 1 if the attributes are present and valid, otherwise, return 0.
12473 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12474
12475static int
12476dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12477 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12478 struct partial_symtab *ranges_pst)
12479{
12480 struct objfile *objfile = cu->objfile;
12481 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12482 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12483 SECT_OFF_TEXT (objfile));
12484 int low_set = 0;
12485 CORE_ADDR low = 0;
12486 CORE_ADDR high = 0;
12487 int retval;
12488
12489 retval = dwarf2_ranges_process (offset, cu,
12490 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12491 {
9277c30c 12492 if (ranges_pst != NULL)
3e29f34a
MR
12493 {
12494 CORE_ADDR lowpc;
12495 CORE_ADDR highpc;
12496
12497 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12498 range_beginning + baseaddr);
12499 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12500 range_end + baseaddr);
12501 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12502 ranges_pst);
12503 }
ff013f42 12504
43039443
JK
12505 /* FIXME: This is recording everything as a low-high
12506 segment of consecutive addresses. We should have a
12507 data structure for discontiguous block ranges
12508 instead. */
12509 if (! low_set)
12510 {
12511 low = range_beginning;
12512 high = range_end;
12513 low_set = 1;
12514 }
12515 else
12516 {
12517 if (range_beginning < low)
12518 low = range_beginning;
12519 if (range_end > high)
12520 high = range_end;
12521 }
5f46c5a5
JK
12522 });
12523 if (!retval)
12524 return 0;
43039443
JK
12525
12526 if (! low_set)
12527 /* If the first entry is an end-of-list marker, the range
12528 describes an empty scope, i.e. no instructions. */
12529 return 0;
12530
12531 if (low_return)
12532 *low_return = low;
12533 if (high_return)
12534 *high_return = high;
12535 return 1;
12536}
12537
3a2b436a
JK
12538/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12539 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12540 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12541
3a2b436a 12542static enum pc_bounds_kind
af34e669 12543dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12544 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12545 struct partial_symtab *pst)
c906108c
SS
12546{
12547 struct attribute *attr;
91da1414 12548 struct attribute *attr_high;
af34e669
DJ
12549 CORE_ADDR low = 0;
12550 CORE_ADDR high = 0;
e385593e 12551 enum pc_bounds_kind ret;
c906108c 12552
91da1414
MW
12553 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12554 if (attr_high)
af34e669 12555 {
e142c38c 12556 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12557 if (attr)
91da1414 12558 {
31aa7e4e
JB
12559 low = attr_value_as_address (attr);
12560 high = attr_value_as_address (attr_high);
12561 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12562 high += low;
91da1414 12563 }
af34e669
DJ
12564 else
12565 /* Found high w/o low attribute. */
e385593e 12566 return PC_BOUNDS_INVALID;
af34e669
DJ
12567
12568 /* Found consecutive range of addresses. */
3a2b436a 12569 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12570 }
c906108c 12571 else
af34e669 12572 {
e142c38c 12573 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12574 if (attr != NULL)
12575 {
ab435259
DE
12576 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12577 We take advantage of the fact that DW_AT_ranges does not appear
12578 in DW_TAG_compile_unit of DWO files. */
12579 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12580 unsigned int ranges_offset = (DW_UNSND (attr)
12581 + (need_ranges_base
12582 ? cu->ranges_base
12583 : 0));
2e3cf129 12584
af34e669 12585 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12586 .debug_ranges section. */
2e3cf129 12587 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12588 return PC_BOUNDS_INVALID;
43039443 12589 /* Found discontinuous range of addresses. */
3a2b436a 12590 ret = PC_BOUNDS_RANGES;
af34e669 12591 }
e385593e
JK
12592 else
12593 return PC_BOUNDS_NOT_PRESENT;
af34e669 12594 }
c906108c 12595
9373cf26
JK
12596 /* read_partial_die has also the strict LOW < HIGH requirement. */
12597 if (high <= low)
e385593e 12598 return PC_BOUNDS_INVALID;
c906108c
SS
12599
12600 /* When using the GNU linker, .gnu.linkonce. sections are used to
12601 eliminate duplicate copies of functions and vtables and such.
12602 The linker will arbitrarily choose one and discard the others.
12603 The AT_*_pc values for such functions refer to local labels in
12604 these sections. If the section from that file was discarded, the
12605 labels are not in the output, so the relocs get a value of 0.
12606 If this is a discarded function, mark the pc bounds as invalid,
12607 so that GDB will ignore it. */
72dca2f5 12608 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12609 return PC_BOUNDS_INVALID;
c906108c
SS
12610
12611 *lowpc = low;
96408a79
SA
12612 if (highpc)
12613 *highpc = high;
af34e669 12614 return ret;
c906108c
SS
12615}
12616
b084d499
JB
12617/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12618 its low and high PC addresses. Do nothing if these addresses could not
12619 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12620 and HIGHPC to the high address if greater than HIGHPC. */
12621
12622static void
12623dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12624 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12625 struct dwarf2_cu *cu)
12626{
12627 CORE_ADDR low, high;
12628 struct die_info *child = die->child;
12629
e385593e 12630 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12631 {
325fac50
PA
12632 *lowpc = std::min (*lowpc, low);
12633 *highpc = std::max (*highpc, high);
b084d499
JB
12634 }
12635
12636 /* If the language does not allow nested subprograms (either inside
12637 subprograms or lexical blocks), we're done. */
12638 if (cu->language != language_ada)
12639 return;
6e70227d 12640
b084d499
JB
12641 /* Check all the children of the given DIE. If it contains nested
12642 subprograms, then check their pc bounds. Likewise, we need to
12643 check lexical blocks as well, as they may also contain subprogram
12644 definitions. */
12645 while (child && child->tag)
12646 {
12647 if (child->tag == DW_TAG_subprogram
12648 || child->tag == DW_TAG_lexical_block)
12649 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12650 child = sibling_die (child);
12651 }
12652}
12653
fae299cd
DC
12654/* Get the low and high pc's represented by the scope DIE, and store
12655 them in *LOWPC and *HIGHPC. If the correct values can't be
12656 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12657
12658static void
12659get_scope_pc_bounds (struct die_info *die,
12660 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12661 struct dwarf2_cu *cu)
12662{
12663 CORE_ADDR best_low = (CORE_ADDR) -1;
12664 CORE_ADDR best_high = (CORE_ADDR) 0;
12665 CORE_ADDR current_low, current_high;
12666
3a2b436a 12667 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12668 >= PC_BOUNDS_RANGES)
fae299cd
DC
12669 {
12670 best_low = current_low;
12671 best_high = current_high;
12672 }
12673 else
12674 {
12675 struct die_info *child = die->child;
12676
12677 while (child && child->tag)
12678 {
12679 switch (child->tag) {
12680 case DW_TAG_subprogram:
b084d499 12681 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12682 break;
12683 case DW_TAG_namespace:
f55ee35c 12684 case DW_TAG_module:
fae299cd
DC
12685 /* FIXME: carlton/2004-01-16: Should we do this for
12686 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12687 that current GCC's always emit the DIEs corresponding
12688 to definitions of methods of classes as children of a
12689 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12690 the DIEs giving the declarations, which could be
12691 anywhere). But I don't see any reason why the
12692 standards says that they have to be there. */
12693 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12694
12695 if (current_low != ((CORE_ADDR) -1))
12696 {
325fac50
PA
12697 best_low = std::min (best_low, current_low);
12698 best_high = std::max (best_high, current_high);
fae299cd
DC
12699 }
12700 break;
12701 default:
0963b4bd 12702 /* Ignore. */
fae299cd
DC
12703 break;
12704 }
12705
12706 child = sibling_die (child);
12707 }
12708 }
12709
12710 *lowpc = best_low;
12711 *highpc = best_high;
12712}
12713
801e3a5b
JB
12714/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12715 in DIE. */
380bca97 12716
801e3a5b
JB
12717static void
12718dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12719 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12720{
bb5ed363 12721 struct objfile *objfile = cu->objfile;
3e29f34a 12722 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12723 struct attribute *attr;
91da1414 12724 struct attribute *attr_high;
801e3a5b 12725
91da1414
MW
12726 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12727 if (attr_high)
801e3a5b 12728 {
801e3a5b
JB
12729 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12730 if (attr)
12731 {
31aa7e4e
JB
12732 CORE_ADDR low = attr_value_as_address (attr);
12733 CORE_ADDR high = attr_value_as_address (attr_high);
12734
12735 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12736 high += low;
9a619af0 12737
3e29f34a
MR
12738 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12739 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12740 record_block_range (block, low, high - 1);
801e3a5b
JB
12741 }
12742 }
12743
12744 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12745 if (attr)
12746 {
bb5ed363 12747 bfd *obfd = objfile->obfd;
ab435259
DE
12748 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12749 We take advantage of the fact that DW_AT_ranges does not appear
12750 in DW_TAG_compile_unit of DWO files. */
12751 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12752
12753 /* The value of the DW_AT_ranges attribute is the offset of the
12754 address range list in the .debug_ranges section. */
ab435259
DE
12755 unsigned long offset = (DW_UNSND (attr)
12756 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12757 const gdb_byte *buffer;
801e3a5b
JB
12758
12759 /* For some target architectures, but not others, the
12760 read_address function sign-extends the addresses it returns.
12761 To recognize base address selection entries, we need a
12762 mask. */
12763 unsigned int addr_size = cu->header.addr_size;
12764 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12765
12766 /* The base address, to which the next pair is relative. Note
12767 that this 'base' is a DWARF concept: most entries in a range
12768 list are relative, to reduce the number of relocs against the
12769 debugging information. This is separate from this function's
12770 'baseaddr' argument, which GDB uses to relocate debugging
12771 information from a shared library based on the address at
12772 which the library was loaded. */
d00adf39
DE
12773 CORE_ADDR base = cu->base_address;
12774 int base_known = cu->base_known;
801e3a5b 12775
5f46c5a5
JK
12776 dwarf2_ranges_process (offset, cu,
12777 [&] (CORE_ADDR start, CORE_ADDR end)
12778 {
58fdfd2c
JK
12779 start += baseaddr;
12780 end += baseaddr;
5f46c5a5
JK
12781 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12782 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12783 record_block_range (block, start, end - 1);
12784 });
801e3a5b
JB
12785 }
12786}
12787
685b1105
JK
12788/* Check whether the producer field indicates either of GCC < 4.6, or the
12789 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12790
685b1105
JK
12791static void
12792check_producer (struct dwarf2_cu *cu)
60d5a603 12793{
38360086 12794 int major, minor;
60d5a603
JK
12795
12796 if (cu->producer == NULL)
12797 {
12798 /* For unknown compilers expect their behavior is DWARF version
12799 compliant.
12800
12801 GCC started to support .debug_types sections by -gdwarf-4 since
12802 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12803 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12804 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12805 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12806 }
b1ffba5a 12807 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12808 {
38360086
MW
12809 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12810 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12811 }
5230b05a
WT
12812 else if (producer_is_icc (cu->producer, &major, &minor))
12813 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
12814 else
12815 {
12816 /* For other non-GCC compilers, expect their behavior is DWARF version
12817 compliant. */
60d5a603
JK
12818 }
12819
ba919b58 12820 cu->checked_producer = 1;
685b1105 12821}
ba919b58 12822
685b1105
JK
12823/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12824 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12825 during 4.6.0 experimental. */
12826
12827static int
12828producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12829{
12830 if (!cu->checked_producer)
12831 check_producer (cu);
12832
12833 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12834}
12835
12836/* Return the default accessibility type if it is not overriden by
12837 DW_AT_accessibility. */
12838
12839static enum dwarf_access_attribute
12840dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12841{
12842 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12843 {
12844 /* The default DWARF 2 accessibility for members is public, the default
12845 accessibility for inheritance is private. */
12846
12847 if (die->tag != DW_TAG_inheritance)
12848 return DW_ACCESS_public;
12849 else
12850 return DW_ACCESS_private;
12851 }
12852 else
12853 {
12854 /* DWARF 3+ defines the default accessibility a different way. The same
12855 rules apply now for DW_TAG_inheritance as for the members and it only
12856 depends on the container kind. */
12857
12858 if (die->parent->tag == DW_TAG_class_type)
12859 return DW_ACCESS_private;
12860 else
12861 return DW_ACCESS_public;
12862 }
12863}
12864
74ac6d43
TT
12865/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12866 offset. If the attribute was not found return 0, otherwise return
12867 1. If it was found but could not properly be handled, set *OFFSET
12868 to 0. */
12869
12870static int
12871handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12872 LONGEST *offset)
12873{
12874 struct attribute *attr;
12875
12876 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12877 if (attr != NULL)
12878 {
12879 *offset = 0;
12880
12881 /* Note that we do not check for a section offset first here.
12882 This is because DW_AT_data_member_location is new in DWARF 4,
12883 so if we see it, we can assume that a constant form is really
12884 a constant and not a section offset. */
12885 if (attr_form_is_constant (attr))
12886 *offset = dwarf2_get_attr_constant_value (attr, 0);
12887 else if (attr_form_is_section_offset (attr))
12888 dwarf2_complex_location_expr_complaint ();
12889 else if (attr_form_is_block (attr))
12890 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12891 else
12892 dwarf2_complex_location_expr_complaint ();
12893
12894 return 1;
12895 }
12896
12897 return 0;
12898}
12899
c906108c
SS
12900/* Add an aggregate field to the field list. */
12901
12902static void
107d2387 12903dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12904 struct dwarf2_cu *cu)
6e70227d 12905{
e7c27a73 12906 struct objfile *objfile = cu->objfile;
5e2b427d 12907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12908 struct nextfield *new_field;
12909 struct attribute *attr;
12910 struct field *fp;
15d034d0 12911 const char *fieldname = "";
c906108c
SS
12912
12913 /* Allocate a new field list entry and link it in. */
8d749320 12914 new_field = XNEW (struct nextfield);
b8c9b27d 12915 make_cleanup (xfree, new_field);
c906108c 12916 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12917
12918 if (die->tag == DW_TAG_inheritance)
12919 {
12920 new_field->next = fip->baseclasses;
12921 fip->baseclasses = new_field;
12922 }
12923 else
12924 {
12925 new_field->next = fip->fields;
12926 fip->fields = new_field;
12927 }
c906108c
SS
12928 fip->nfields++;
12929
e142c38c 12930 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12931 if (attr)
12932 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12933 else
12934 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12935 if (new_field->accessibility != DW_ACCESS_public)
12936 fip->non_public_fields = 1;
60d5a603 12937
e142c38c 12938 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12939 if (attr)
12940 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12941 else
12942 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12943
12944 fp = &new_field->field;
a9a9bd0f 12945
e142c38c 12946 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12947 {
74ac6d43
TT
12948 LONGEST offset;
12949
a9a9bd0f 12950 /* Data member other than a C++ static data member. */
6e70227d 12951
c906108c 12952 /* Get type of field. */
e7c27a73 12953 fp->type = die_type (die, cu);
c906108c 12954
d6a843b5 12955 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12956
c906108c 12957 /* Get bit size of field (zero if none). */
e142c38c 12958 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12959 if (attr)
12960 {
12961 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12962 }
12963 else
12964 {
12965 FIELD_BITSIZE (*fp) = 0;
12966 }
12967
12968 /* Get bit offset of field. */
74ac6d43
TT
12969 if (handle_data_member_location (die, cu, &offset))
12970 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12971 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12972 if (attr)
12973 {
5e2b427d 12974 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12975 {
12976 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12977 additional bit offset from the MSB of the containing
12978 anonymous object to the MSB of the field. We don't
12979 have to do anything special since we don't need to
12980 know the size of the anonymous object. */
f41f5e61 12981 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12982 }
12983 else
12984 {
12985 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12986 MSB of the anonymous object, subtract off the number of
12987 bits from the MSB of the field to the MSB of the
12988 object, and then subtract off the number of bits of
12989 the field itself. The result is the bit offset of
12990 the LSB of the field. */
c906108c
SS
12991 int anonymous_size;
12992 int bit_offset = DW_UNSND (attr);
12993
e142c38c 12994 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12995 if (attr)
12996 {
12997 /* The size of the anonymous object containing
12998 the bit field is explicit, so use the
12999 indicated size (in bytes). */
13000 anonymous_size = DW_UNSND (attr);
13001 }
13002 else
13003 {
13004 /* The size of the anonymous object containing
13005 the bit field must be inferred from the type
13006 attribute of the data member containing the
13007 bit field. */
13008 anonymous_size = TYPE_LENGTH (fp->type);
13009 }
f41f5e61
PA
13010 SET_FIELD_BITPOS (*fp,
13011 (FIELD_BITPOS (*fp)
13012 + anonymous_size * bits_per_byte
13013 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13014 }
13015 }
da5b30da
AA
13016 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13017 if (attr != NULL)
13018 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13019 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13020
13021 /* Get name of field. */
39cbfefa
DJ
13022 fieldname = dwarf2_name (die, cu);
13023 if (fieldname == NULL)
13024 fieldname = "";
d8151005
DJ
13025
13026 /* The name is already allocated along with this objfile, so we don't
13027 need to duplicate it for the type. */
13028 fp->name = fieldname;
c906108c
SS
13029
13030 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13031 pointer or virtual base class pointer) to private. */
e142c38c 13032 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13033 {
d48cc9dd 13034 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13035 new_field->accessibility = DW_ACCESS_private;
13036 fip->non_public_fields = 1;
13037 }
13038 }
a9a9bd0f 13039 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13040 {
a9a9bd0f
DC
13041 /* C++ static member. */
13042
13043 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13044 is a declaration, but all versions of G++ as of this writing
13045 (so through at least 3.2.1) incorrectly generate
13046 DW_TAG_variable tags. */
6e70227d 13047
ff355380 13048 const char *physname;
c906108c 13049
a9a9bd0f 13050 /* Get name of field. */
39cbfefa
DJ
13051 fieldname = dwarf2_name (die, cu);
13052 if (fieldname == NULL)
c906108c
SS
13053 return;
13054
254e6b9e 13055 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13056 if (attr
13057 /* Only create a symbol if this is an external value.
13058 new_symbol checks this and puts the value in the global symbol
13059 table, which we want. If it is not external, new_symbol
13060 will try to put the value in cu->list_in_scope which is wrong. */
13061 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13062 {
13063 /* A static const member, not much different than an enum as far as
13064 we're concerned, except that we can support more types. */
13065 new_symbol (die, NULL, cu);
13066 }
13067
2df3850c 13068 /* Get physical name. */
ff355380 13069 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13070
d8151005
DJ
13071 /* The name is already allocated along with this objfile, so we don't
13072 need to duplicate it for the type. */
13073 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13074 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13075 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13076 }
13077 else if (die->tag == DW_TAG_inheritance)
13078 {
74ac6d43 13079 LONGEST offset;
d4b96c9a 13080
74ac6d43
TT
13081 /* C++ base class field. */
13082 if (handle_data_member_location (die, cu, &offset))
13083 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13084 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13085 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13086 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13087 fip->nbaseclasses++;
13088 }
13089}
13090
98751a41
JK
13091/* Add a typedef defined in the scope of the FIP's class. */
13092
13093static void
13094dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13095 struct dwarf2_cu *cu)
6e70227d 13096{
98751a41 13097 struct typedef_field_list *new_field;
98751a41 13098 struct typedef_field *fp;
98751a41
JK
13099
13100 /* Allocate a new field list entry and link it in. */
8d749320 13101 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13102 make_cleanup (xfree, new_field);
13103
13104 gdb_assert (die->tag == DW_TAG_typedef);
13105
13106 fp = &new_field->field;
13107
13108 /* Get name of field. */
13109 fp->name = dwarf2_name (die, cu);
13110 if (fp->name == NULL)
13111 return;
13112
13113 fp->type = read_type_die (die, cu);
13114
c191a687
KS
13115 /* Save accessibility. */
13116 enum dwarf_access_attribute accessibility;
13117 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13118 if (attr != NULL)
13119 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13120 else
13121 accessibility = dwarf2_default_access_attribute (die, cu);
13122 switch (accessibility)
13123 {
13124 case DW_ACCESS_public:
13125 /* The assumed value if neither private nor protected. */
13126 break;
13127 case DW_ACCESS_private:
13128 fp->is_private = 1;
13129 break;
13130 case DW_ACCESS_protected:
13131 fp->is_protected = 1;
13132 break;
13133 default:
37534686
KS
13134 complaint (&symfile_complaints,
13135 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
13136 }
13137
98751a41
JK
13138 new_field->next = fip->typedef_field_list;
13139 fip->typedef_field_list = new_field;
13140 fip->typedef_field_list_count++;
13141}
13142
c906108c
SS
13143/* Create the vector of fields, and attach it to the type. */
13144
13145static void
fba45db2 13146dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13147 struct dwarf2_cu *cu)
c906108c
SS
13148{
13149 int nfields = fip->nfields;
13150
13151 /* Record the field count, allocate space for the array of fields,
13152 and create blank accessibility bitfields if necessary. */
13153 TYPE_NFIELDS (type) = nfields;
13154 TYPE_FIELDS (type) = (struct field *)
13155 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13156 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13157
b4ba55a1 13158 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13159 {
13160 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13161
13162 TYPE_FIELD_PRIVATE_BITS (type) =
13163 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13164 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13165
13166 TYPE_FIELD_PROTECTED_BITS (type) =
13167 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13168 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13169
774b6a14
TT
13170 TYPE_FIELD_IGNORE_BITS (type) =
13171 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13172 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13173 }
13174
13175 /* If the type has baseclasses, allocate and clear a bit vector for
13176 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13177 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13178 {
13179 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13180 unsigned char *pointer;
c906108c
SS
13181
13182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13183 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13184 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13185 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13186 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13187 }
13188
3e43a32a
MS
13189 /* Copy the saved-up fields into the field vector. Start from the head of
13190 the list, adding to the tail of the field array, so that they end up in
13191 the same order in the array in which they were added to the list. */
c906108c
SS
13192 while (nfields-- > 0)
13193 {
7d0ccb61
DJ
13194 struct nextfield *fieldp;
13195
13196 if (fip->fields)
13197 {
13198 fieldp = fip->fields;
13199 fip->fields = fieldp->next;
13200 }
13201 else
13202 {
13203 fieldp = fip->baseclasses;
13204 fip->baseclasses = fieldp->next;
13205 }
13206
13207 TYPE_FIELD (type, nfields) = fieldp->field;
13208 switch (fieldp->accessibility)
c906108c 13209 {
c5aa993b 13210 case DW_ACCESS_private:
b4ba55a1
JB
13211 if (cu->language != language_ada)
13212 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13213 break;
c906108c 13214
c5aa993b 13215 case DW_ACCESS_protected:
b4ba55a1
JB
13216 if (cu->language != language_ada)
13217 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13218 break;
c906108c 13219
c5aa993b
JM
13220 case DW_ACCESS_public:
13221 break;
c906108c 13222
c5aa993b
JM
13223 default:
13224 /* Unknown accessibility. Complain and treat it as public. */
13225 {
e2e0b3e5 13226 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13227 fieldp->accessibility);
c5aa993b
JM
13228 }
13229 break;
c906108c
SS
13230 }
13231 if (nfields < fip->nbaseclasses)
13232 {
7d0ccb61 13233 switch (fieldp->virtuality)
c906108c 13234 {
c5aa993b
JM
13235 case DW_VIRTUALITY_virtual:
13236 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13237 if (cu->language == language_ada)
a73c6dcd 13238 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13239 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13240 break;
c906108c
SS
13241 }
13242 }
c906108c
SS
13243 }
13244}
13245
7d27a96d
TT
13246/* Return true if this member function is a constructor, false
13247 otherwise. */
13248
13249static int
13250dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13251{
13252 const char *fieldname;
fe978cb0 13253 const char *type_name;
7d27a96d
TT
13254 int len;
13255
13256 if (die->parent == NULL)
13257 return 0;
13258
13259 if (die->parent->tag != DW_TAG_structure_type
13260 && die->parent->tag != DW_TAG_union_type
13261 && die->parent->tag != DW_TAG_class_type)
13262 return 0;
13263
13264 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13265 type_name = dwarf2_name (die->parent, cu);
13266 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13267 return 0;
13268
13269 len = strlen (fieldname);
fe978cb0
PA
13270 return (strncmp (fieldname, type_name, len) == 0
13271 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13272}
13273
c906108c
SS
13274/* Add a member function to the proper fieldlist. */
13275
13276static void
107d2387 13277dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13278 struct type *type, struct dwarf2_cu *cu)
c906108c 13279{
e7c27a73 13280 struct objfile *objfile = cu->objfile;
c906108c
SS
13281 struct attribute *attr;
13282 struct fnfieldlist *flp;
13283 int i;
13284 struct fn_field *fnp;
15d034d0 13285 const char *fieldname;
c906108c 13286 struct nextfnfield *new_fnfield;
f792889a 13287 struct type *this_type;
60d5a603 13288 enum dwarf_access_attribute accessibility;
c906108c 13289
b4ba55a1 13290 if (cu->language == language_ada)
a73c6dcd 13291 error (_("unexpected member function in Ada type"));
b4ba55a1 13292
2df3850c 13293 /* Get name of member function. */
39cbfefa
DJ
13294 fieldname = dwarf2_name (die, cu);
13295 if (fieldname == NULL)
2df3850c 13296 return;
c906108c 13297
c906108c
SS
13298 /* Look up member function name in fieldlist. */
13299 for (i = 0; i < fip->nfnfields; i++)
13300 {
27bfe10e 13301 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13302 break;
13303 }
13304
13305 /* Create new list element if necessary. */
13306 if (i < fip->nfnfields)
13307 flp = &fip->fnfieldlists[i];
13308 else
13309 {
13310 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13311 {
13312 fip->fnfieldlists = (struct fnfieldlist *)
13313 xrealloc (fip->fnfieldlists,
13314 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13315 * sizeof (struct fnfieldlist));
c906108c 13316 if (fip->nfnfields == 0)
c13c43fd 13317 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13318 }
13319 flp = &fip->fnfieldlists[fip->nfnfields];
13320 flp->name = fieldname;
13321 flp->length = 0;
13322 flp->head = NULL;
3da10d80 13323 i = fip->nfnfields++;
c906108c
SS
13324 }
13325
13326 /* Create a new member function field and chain it to the field list
0963b4bd 13327 entry. */
8d749320 13328 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13329 make_cleanup (xfree, new_fnfield);
c906108c
SS
13330 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13331 new_fnfield->next = flp->head;
13332 flp->head = new_fnfield;
13333 flp->length++;
13334
13335 /* Fill in the member function field info. */
13336 fnp = &new_fnfield->fnfield;
3da10d80
KS
13337
13338 /* Delay processing of the physname until later. */
9c37b5ae 13339 if (cu->language == language_cplus)
3da10d80
KS
13340 {
13341 add_to_method_list (type, i, flp->length - 1, fieldname,
13342 die, cu);
13343 }
13344 else
13345 {
1d06ead6 13346 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13347 fnp->physname = physname ? physname : "";
13348 }
13349
c906108c 13350 fnp->type = alloc_type (objfile);
f792889a
DJ
13351 this_type = read_type_die (die, cu);
13352 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13353 {
f792889a 13354 int nparams = TYPE_NFIELDS (this_type);
c906108c 13355
f792889a 13356 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13357 of the method itself (TYPE_CODE_METHOD). */
13358 smash_to_method_type (fnp->type, type,
f792889a
DJ
13359 TYPE_TARGET_TYPE (this_type),
13360 TYPE_FIELDS (this_type),
13361 TYPE_NFIELDS (this_type),
13362 TYPE_VARARGS (this_type));
c906108c
SS
13363
13364 /* Handle static member functions.
c5aa993b 13365 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13366 member functions. G++ helps GDB by marking the first
13367 parameter for non-static member functions (which is the this
13368 pointer) as artificial. We obtain this information from
13369 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13370 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13371 fnp->voffset = VOFFSET_STATIC;
13372 }
13373 else
e2e0b3e5 13374 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13375 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13376
13377 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13378 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13379 fnp->fcontext = die_containing_type (die, cu);
c906108c 13380
3e43a32a
MS
13381 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13382 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13383
13384 /* Get accessibility. */
e142c38c 13385 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13386 if (attr)
aead7601 13387 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13388 else
13389 accessibility = dwarf2_default_access_attribute (die, cu);
13390 switch (accessibility)
c906108c 13391 {
60d5a603
JK
13392 case DW_ACCESS_private:
13393 fnp->is_private = 1;
13394 break;
13395 case DW_ACCESS_protected:
13396 fnp->is_protected = 1;
13397 break;
c906108c
SS
13398 }
13399
b02dede2 13400 /* Check for artificial methods. */
e142c38c 13401 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13402 if (attr && DW_UNSND (attr) != 0)
13403 fnp->is_artificial = 1;
13404
7d27a96d
TT
13405 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13406
0d564a31 13407 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13408 function. For older versions of GCC, this is an offset in the
13409 appropriate virtual table, as specified by DW_AT_containing_type.
13410 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13411 to the object address. */
13412
e142c38c 13413 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13414 if (attr)
8e19ed76 13415 {
aec5aa8b 13416 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13417 {
aec5aa8b
TT
13418 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13419 {
13420 /* Old-style GCC. */
13421 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13422 }
13423 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13424 || (DW_BLOCK (attr)->size > 1
13425 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13426 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13427 {
aec5aa8b
TT
13428 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13429 if ((fnp->voffset % cu->header.addr_size) != 0)
13430 dwarf2_complex_location_expr_complaint ();
13431 else
13432 fnp->voffset /= cu->header.addr_size;
13433 fnp->voffset += 2;
13434 }
13435 else
13436 dwarf2_complex_location_expr_complaint ();
13437
13438 if (!fnp->fcontext)
7e993ebf
KS
13439 {
13440 /* If there is no `this' field and no DW_AT_containing_type,
13441 we cannot actually find a base class context for the
13442 vtable! */
13443 if (TYPE_NFIELDS (this_type) == 0
13444 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13445 {
13446 complaint (&symfile_complaints,
13447 _("cannot determine context for virtual member "
13448 "function \"%s\" (offset %d)"),
9c541725 13449 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13450 }
13451 else
13452 {
13453 fnp->fcontext
13454 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13455 }
13456 }
aec5aa8b 13457 }
3690dd37 13458 else if (attr_form_is_section_offset (attr))
8e19ed76 13459 {
4d3c2250 13460 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13461 }
13462 else
13463 {
4d3c2250
KB
13464 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13465 fieldname);
8e19ed76 13466 }
0d564a31 13467 }
d48cc9dd
DJ
13468 else
13469 {
13470 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13471 if (attr && DW_UNSND (attr))
13472 {
13473 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13474 complaint (&symfile_complaints,
3e43a32a
MS
13475 _("Member function \"%s\" (offset %d) is virtual "
13476 "but the vtable offset is not specified"),
9c541725 13477 fieldname, to_underlying (die->sect_off));
9655fd1a 13478 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13479 TYPE_CPLUS_DYNAMIC (type) = 1;
13480 }
13481 }
c906108c
SS
13482}
13483
13484/* Create the vector of member function fields, and attach it to the type. */
13485
13486static void
fba45db2 13487dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13488 struct dwarf2_cu *cu)
c906108c
SS
13489{
13490 struct fnfieldlist *flp;
c906108c
SS
13491 int i;
13492
b4ba55a1 13493 if (cu->language == language_ada)
a73c6dcd 13494 error (_("unexpected member functions in Ada type"));
b4ba55a1 13495
c906108c
SS
13496 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13497 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13498 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13499
13500 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13501 {
13502 struct nextfnfield *nfp = flp->head;
13503 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13504 int k;
13505
13506 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13507 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13508 fn_flp->fn_fields = (struct fn_field *)
13509 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13510 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13511 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13512 }
13513
13514 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13515}
13516
1168df01
JB
13517/* Returns non-zero if NAME is the name of a vtable member in CU's
13518 language, zero otherwise. */
13519static int
13520is_vtable_name (const char *name, struct dwarf2_cu *cu)
13521{
13522 static const char vptr[] = "_vptr";
987504bb 13523 static const char vtable[] = "vtable";
1168df01 13524
9c37b5ae
TT
13525 /* Look for the C++ form of the vtable. */
13526 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13527 return 1;
13528
13529 return 0;
13530}
13531
c0dd20ea 13532/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13533 functions, with the ABI-specified layout. If TYPE describes
13534 such a structure, smash it into a member function type.
61049d3b
DJ
13535
13536 GCC shouldn't do this; it should just output pointer to member DIEs.
13537 This is GCC PR debug/28767. */
c0dd20ea 13538
0b92b5bb
TT
13539static void
13540quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13541{
09e2d7c7 13542 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13543
13544 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13545 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13546 return;
c0dd20ea
DJ
13547
13548 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13549 if (TYPE_FIELD_NAME (type, 0) == NULL
13550 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13551 || TYPE_FIELD_NAME (type, 1) == NULL
13552 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13553 return;
c0dd20ea
DJ
13554
13555 /* Find the type of the method. */
0b92b5bb 13556 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13557 if (pfn_type == NULL
13558 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13559 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13560 return;
c0dd20ea
DJ
13561
13562 /* Look for the "this" argument. */
13563 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13564 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13565 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13566 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13567 return;
c0dd20ea 13568
09e2d7c7 13569 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13570 new_type = alloc_type (objfile);
09e2d7c7 13571 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13572 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13573 TYPE_VARARGS (pfn_type));
0b92b5bb 13574 smash_to_methodptr_type (type, new_type);
c0dd20ea 13575}
1168df01 13576
685b1105 13577
c906108c 13578/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13579 (definition) to create a type for the structure or union. Fill in
13580 the type's name and general properties; the members will not be
83655187
DE
13581 processed until process_structure_scope. A symbol table entry for
13582 the type will also not be done until process_structure_scope (assuming
13583 the type has a name).
c906108c 13584
c767944b
DJ
13585 NOTE: we need to call these functions regardless of whether or not the
13586 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13587 structure or union. This gets the type entered into our set of
83655187 13588 user defined types. */
c906108c 13589
f792889a 13590static struct type *
134d01f1 13591read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13592{
e7c27a73 13593 struct objfile *objfile = cu->objfile;
c906108c
SS
13594 struct type *type;
13595 struct attribute *attr;
15d034d0 13596 const char *name;
c906108c 13597
348e048f
DE
13598 /* If the definition of this type lives in .debug_types, read that type.
13599 Don't follow DW_AT_specification though, that will take us back up
13600 the chain and we want to go down. */
45e58e77 13601 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13602 if (attr)
13603 {
ac9ec31b 13604 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13605
ac9ec31b 13606 /* The type's CU may not be the same as CU.
02142a6c 13607 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13608 return set_die_type (die, type, cu);
13609 }
13610
c0dd20ea 13611 type = alloc_type (objfile);
c906108c 13612 INIT_CPLUS_SPECIFIC (type);
93311388 13613
39cbfefa
DJ
13614 name = dwarf2_name (die, cu);
13615 if (name != NULL)
c906108c 13616 {
987504bb 13617 if (cu->language == language_cplus
c44af4eb
TT
13618 || cu->language == language_d
13619 || cu->language == language_rust)
63d06c5c 13620 {
15d034d0 13621 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13622
13623 /* dwarf2_full_name might have already finished building the DIE's
13624 type. If so, there is no need to continue. */
13625 if (get_die_type (die, cu) != NULL)
13626 return get_die_type (die, cu);
13627
13628 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13629 if (die->tag == DW_TAG_structure_type
13630 || die->tag == DW_TAG_class_type)
13631 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13632 }
13633 else
13634 {
d8151005
DJ
13635 /* The name is already allocated along with this objfile, so
13636 we don't need to duplicate it for the type. */
7d455152 13637 TYPE_TAG_NAME (type) = name;
94af9270
KS
13638 if (die->tag == DW_TAG_class_type)
13639 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13640 }
c906108c
SS
13641 }
13642
13643 if (die->tag == DW_TAG_structure_type)
13644 {
13645 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13646 }
13647 else if (die->tag == DW_TAG_union_type)
13648 {
13649 TYPE_CODE (type) = TYPE_CODE_UNION;
13650 }
13651 else
13652 {
4753d33b 13653 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13654 }
13655
0cc2414c
TT
13656 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13657 TYPE_DECLARED_CLASS (type) = 1;
13658
e142c38c 13659 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13660 if (attr)
13661 {
155bfbd3
JB
13662 if (attr_form_is_constant (attr))
13663 TYPE_LENGTH (type) = DW_UNSND (attr);
13664 else
13665 {
13666 /* For the moment, dynamic type sizes are not supported
13667 by GDB's struct type. The actual size is determined
13668 on-demand when resolving the type of a given object,
13669 so set the type's length to zero for now. Otherwise,
13670 we record an expression as the length, and that expression
13671 could lead to a very large value, which could eventually
13672 lead to us trying to allocate that much memory when creating
13673 a value of that type. */
13674 TYPE_LENGTH (type) = 0;
13675 }
c906108c
SS
13676 }
13677 else
13678 {
13679 TYPE_LENGTH (type) = 0;
13680 }
13681
5230b05a 13682 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 13683 {
5230b05a
WT
13684 /* ICC<14 does not output the required DW_AT_declaration on
13685 incomplete types, but gives them a size of zero. */
422b1cb0 13686 TYPE_STUB (type) = 1;
685b1105
JK
13687 }
13688 else
13689 TYPE_STUB_SUPPORTED (type) = 1;
13690
dc718098 13691 if (die_is_declaration (die, cu))
876cecd0 13692 TYPE_STUB (type) = 1;
a6c727b2
DJ
13693 else if (attr == NULL && die->child == NULL
13694 && producer_is_realview (cu->producer))
13695 /* RealView does not output the required DW_AT_declaration
13696 on incomplete types. */
13697 TYPE_STUB (type) = 1;
dc718098 13698
c906108c
SS
13699 /* We need to add the type field to the die immediately so we don't
13700 infinitely recurse when dealing with pointers to the structure
0963b4bd 13701 type within the structure itself. */
1c379e20 13702 set_die_type (die, type, cu);
c906108c 13703
7e314c57
JK
13704 /* set_die_type should be already done. */
13705 set_descriptive_type (type, die, cu);
13706
c767944b
DJ
13707 return type;
13708}
13709
13710/* Finish creating a structure or union type, including filling in
13711 its members and creating a symbol for it. */
13712
13713static void
13714process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13715{
13716 struct objfile *objfile = cu->objfile;
ca040673 13717 struct die_info *child_die;
c767944b
DJ
13718 struct type *type;
13719
13720 type = get_die_type (die, cu);
13721 if (type == NULL)
13722 type = read_structure_type (die, cu);
13723
e142c38c 13724 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13725 {
13726 struct field_info fi;
34eaf542 13727 VEC (symbolp) *template_args = NULL;
c767944b 13728 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13729
13730 memset (&fi, 0, sizeof (struct field_info));
13731
639d11d3 13732 child_die = die->child;
c906108c
SS
13733
13734 while (child_die && child_die->tag)
13735 {
a9a9bd0f
DC
13736 if (child_die->tag == DW_TAG_member
13737 || child_die->tag == DW_TAG_variable)
c906108c 13738 {
a9a9bd0f
DC
13739 /* NOTE: carlton/2002-11-05: A C++ static data member
13740 should be a DW_TAG_member that is a declaration, but
13741 all versions of G++ as of this writing (so through at
13742 least 3.2.1) incorrectly generate DW_TAG_variable
13743 tags for them instead. */
e7c27a73 13744 dwarf2_add_field (&fi, child_die, cu);
c906108c 13745 }
8713b1b1 13746 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13747 {
e98c9e7c
TT
13748 /* Rust doesn't have member functions in the C++ sense.
13749 However, it does emit ordinary functions as children
13750 of a struct DIE. */
13751 if (cu->language == language_rust)
13752 read_func_scope (child_die, cu);
13753 else
13754 {
13755 /* C++ member function. */
13756 dwarf2_add_member_fn (&fi, child_die, type, cu);
13757 }
c906108c
SS
13758 }
13759 else if (child_die->tag == DW_TAG_inheritance)
13760 {
13761 /* C++ base class field. */
e7c27a73 13762 dwarf2_add_field (&fi, child_die, cu);
c906108c 13763 }
98751a41
JK
13764 else if (child_die->tag == DW_TAG_typedef)
13765 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13766 else if (child_die->tag == DW_TAG_template_type_param
13767 || child_die->tag == DW_TAG_template_value_param)
13768 {
13769 struct symbol *arg = new_symbol (child_die, NULL, cu);
13770
f1078f66
DJ
13771 if (arg != NULL)
13772 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13773 }
13774
c906108c
SS
13775 child_die = sibling_die (child_die);
13776 }
13777
34eaf542
TT
13778 /* Attach template arguments to type. */
13779 if (! VEC_empty (symbolp, template_args))
13780 {
13781 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13782 TYPE_N_TEMPLATE_ARGUMENTS (type)
13783 = VEC_length (symbolp, template_args);
13784 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13785 = XOBNEWVEC (&objfile->objfile_obstack,
13786 struct symbol *,
13787 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13789 VEC_address (symbolp, template_args),
13790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13791 * sizeof (struct symbol *)));
13792 VEC_free (symbolp, template_args);
13793 }
13794
c906108c
SS
13795 /* Attach fields and member functions to the type. */
13796 if (fi.nfields)
e7c27a73 13797 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13798 if (fi.nfnfields)
13799 {
e7c27a73 13800 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13801
c5aa993b 13802 /* Get the type which refers to the base class (possibly this
c906108c 13803 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13804 class from the DW_AT_containing_type attribute. This use of
13805 DW_AT_containing_type is a GNU extension. */
c906108c 13806
e142c38c 13807 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13808 {
e7c27a73 13809 struct type *t = die_containing_type (die, cu);
c906108c 13810
ae6ae975 13811 set_type_vptr_basetype (type, t);
c906108c
SS
13812 if (type == t)
13813 {
c906108c
SS
13814 int i;
13815
13816 /* Our own class provides vtbl ptr. */
13817 for (i = TYPE_NFIELDS (t) - 1;
13818 i >= TYPE_N_BASECLASSES (t);
13819 --i)
13820 {
0d5cff50 13821 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13822
1168df01 13823 if (is_vtable_name (fieldname, cu))
c906108c 13824 {
ae6ae975 13825 set_type_vptr_fieldno (type, i);
c906108c
SS
13826 break;
13827 }
13828 }
13829
13830 /* Complain if virtual function table field not found. */
13831 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13832 complaint (&symfile_complaints,
3e43a32a
MS
13833 _("virtual function table pointer "
13834 "not found when defining class '%s'"),
4d3c2250
KB
13835 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13836 "");
c906108c
SS
13837 }
13838 else
13839 {
ae6ae975 13840 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13841 }
13842 }
f6235d4c 13843 else if (cu->producer
61012eef 13844 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13845 {
13846 /* The IBM XLC compiler does not provide direct indication
13847 of the containing type, but the vtable pointer is
13848 always named __vfp. */
13849
13850 int i;
13851
13852 for (i = TYPE_NFIELDS (type) - 1;
13853 i >= TYPE_N_BASECLASSES (type);
13854 --i)
13855 {
13856 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13857 {
ae6ae975
DE
13858 set_type_vptr_fieldno (type, i);
13859 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13860 break;
13861 }
13862 }
13863 }
c906108c 13864 }
98751a41
JK
13865
13866 /* Copy fi.typedef_field_list linked list elements content into the
13867 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13868 if (fi.typedef_field_list)
13869 {
13870 int i = fi.typedef_field_list_count;
13871
a0d7a4ff 13872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13873 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13874 = ((struct typedef_field *)
13875 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13876 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13877
13878 /* Reverse the list order to keep the debug info elements order. */
13879 while (--i >= 0)
13880 {
13881 struct typedef_field *dest, *src;
6e70227d 13882
98751a41
JK
13883 dest = &TYPE_TYPEDEF_FIELD (type, i);
13884 src = &fi.typedef_field_list->field;
13885 fi.typedef_field_list = fi.typedef_field_list->next;
13886 *dest = *src;
13887 }
13888 }
c767944b
DJ
13889
13890 do_cleanups (back_to);
c906108c 13891 }
63d06c5c 13892
bb5ed363 13893 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13894
90aeadfc
DC
13895 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13896 snapshots) has been known to create a die giving a declaration
13897 for a class that has, as a child, a die giving a definition for a
13898 nested class. So we have to process our children even if the
13899 current die is a declaration. Normally, of course, a declaration
13900 won't have any children at all. */
134d01f1 13901
ca040673
DE
13902 child_die = die->child;
13903
90aeadfc
DC
13904 while (child_die != NULL && child_die->tag)
13905 {
13906 if (child_die->tag == DW_TAG_member
13907 || child_die->tag == DW_TAG_variable
34eaf542
TT
13908 || child_die->tag == DW_TAG_inheritance
13909 || child_die->tag == DW_TAG_template_value_param
13910 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13911 {
90aeadfc 13912 /* Do nothing. */
134d01f1 13913 }
90aeadfc
DC
13914 else
13915 process_die (child_die, cu);
134d01f1 13916
90aeadfc 13917 child_die = sibling_die (child_die);
134d01f1
DJ
13918 }
13919
fa4028e9
JB
13920 /* Do not consider external references. According to the DWARF standard,
13921 these DIEs are identified by the fact that they have no byte_size
13922 attribute, and a declaration attribute. */
13923 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13924 || !die_is_declaration (die, cu))
c767944b 13925 new_symbol (die, type, cu);
134d01f1
DJ
13926}
13927
55426c9d
JB
13928/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13929 update TYPE using some information only available in DIE's children. */
13930
13931static void
13932update_enumeration_type_from_children (struct die_info *die,
13933 struct type *type,
13934 struct dwarf2_cu *cu)
13935{
60f7655a 13936 struct die_info *child_die;
55426c9d
JB
13937 int unsigned_enum = 1;
13938 int flag_enum = 1;
13939 ULONGEST mask = 0;
55426c9d 13940
8268c778 13941 auto_obstack obstack;
55426c9d 13942
60f7655a
DE
13943 for (child_die = die->child;
13944 child_die != NULL && child_die->tag;
13945 child_die = sibling_die (child_die))
55426c9d
JB
13946 {
13947 struct attribute *attr;
13948 LONGEST value;
13949 const gdb_byte *bytes;
13950 struct dwarf2_locexpr_baton *baton;
13951 const char *name;
60f7655a 13952
55426c9d
JB
13953 if (child_die->tag != DW_TAG_enumerator)
13954 continue;
13955
13956 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13957 if (attr == NULL)
13958 continue;
13959
13960 name = dwarf2_name (child_die, cu);
13961 if (name == NULL)
13962 name = "<anonymous enumerator>";
13963
13964 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13965 &value, &bytes, &baton);
13966 if (value < 0)
13967 {
13968 unsigned_enum = 0;
13969 flag_enum = 0;
13970 }
13971 else if ((mask & value) != 0)
13972 flag_enum = 0;
13973 else
13974 mask |= value;
13975
13976 /* If we already know that the enum type is neither unsigned, nor
13977 a flag type, no need to look at the rest of the enumerates. */
13978 if (!unsigned_enum && !flag_enum)
13979 break;
55426c9d
JB
13980 }
13981
13982 if (unsigned_enum)
13983 TYPE_UNSIGNED (type) = 1;
13984 if (flag_enum)
13985 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
13986}
13987
134d01f1
DJ
13988/* Given a DW_AT_enumeration_type die, set its type. We do not
13989 complete the type's fields yet, or create any symbols. */
c906108c 13990
f792889a 13991static struct type *
134d01f1 13992read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13993{
e7c27a73 13994 struct objfile *objfile = cu->objfile;
c906108c 13995 struct type *type;
c906108c 13996 struct attribute *attr;
0114d602 13997 const char *name;
134d01f1 13998
348e048f
DE
13999 /* If the definition of this type lives in .debug_types, read that type.
14000 Don't follow DW_AT_specification though, that will take us back up
14001 the chain and we want to go down. */
45e58e77 14002 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14003 if (attr)
14004 {
ac9ec31b 14005 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14006
ac9ec31b 14007 /* The type's CU may not be the same as CU.
02142a6c 14008 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14009 return set_die_type (die, type, cu);
14010 }
14011
c906108c
SS
14012 type = alloc_type (objfile);
14013
14014 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14015 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14016 if (name != NULL)
7d455152 14017 TYPE_TAG_NAME (type) = name;
c906108c 14018
0626fc76
TT
14019 attr = dwarf2_attr (die, DW_AT_type, cu);
14020 if (attr != NULL)
14021 {
14022 struct type *underlying_type = die_type (die, cu);
14023
14024 TYPE_TARGET_TYPE (type) = underlying_type;
14025 }
14026
e142c38c 14027 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14028 if (attr)
14029 {
14030 TYPE_LENGTH (type) = DW_UNSND (attr);
14031 }
14032 else
14033 {
14034 TYPE_LENGTH (type) = 0;
14035 }
14036
137033e9
JB
14037 /* The enumeration DIE can be incomplete. In Ada, any type can be
14038 declared as private in the package spec, and then defined only
14039 inside the package body. Such types are known as Taft Amendment
14040 Types. When another package uses such a type, an incomplete DIE
14041 may be generated by the compiler. */
02eb380e 14042 if (die_is_declaration (die, cu))
876cecd0 14043 TYPE_STUB (type) = 1;
02eb380e 14044
0626fc76
TT
14045 /* Finish the creation of this type by using the enum's children.
14046 We must call this even when the underlying type has been provided
14047 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14048 update_enumeration_type_from_children (die, type, cu);
14049
0626fc76
TT
14050 /* If this type has an underlying type that is not a stub, then we
14051 may use its attributes. We always use the "unsigned" attribute
14052 in this situation, because ordinarily we guess whether the type
14053 is unsigned -- but the guess can be wrong and the underlying type
14054 can tell us the reality. However, we defer to a local size
14055 attribute if one exists, because this lets the compiler override
14056 the underlying type if needed. */
14057 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14058 {
14059 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14060 if (TYPE_LENGTH (type) == 0)
14061 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14062 }
14063
3d567982
TT
14064 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14065
f792889a 14066 return set_die_type (die, type, cu);
134d01f1
DJ
14067}
14068
14069/* Given a pointer to a die which begins an enumeration, process all
14070 the dies that define the members of the enumeration, and create the
14071 symbol for the enumeration type.
14072
14073 NOTE: We reverse the order of the element list. */
14074
14075static void
14076process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14077{
f792889a 14078 struct type *this_type;
134d01f1 14079
f792889a
DJ
14080 this_type = get_die_type (die, cu);
14081 if (this_type == NULL)
14082 this_type = read_enumeration_type (die, cu);
9dc481d3 14083
639d11d3 14084 if (die->child != NULL)
c906108c 14085 {
9dc481d3
DE
14086 struct die_info *child_die;
14087 struct symbol *sym;
14088 struct field *fields = NULL;
14089 int num_fields = 0;
15d034d0 14090 const char *name;
9dc481d3 14091
639d11d3 14092 child_die = die->child;
c906108c
SS
14093 while (child_die && child_die->tag)
14094 {
14095 if (child_die->tag != DW_TAG_enumerator)
14096 {
e7c27a73 14097 process_die (child_die, cu);
c906108c
SS
14098 }
14099 else
14100 {
39cbfefa
DJ
14101 name = dwarf2_name (child_die, cu);
14102 if (name)
c906108c 14103 {
f792889a 14104 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14105
14106 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14107 {
14108 fields = (struct field *)
14109 xrealloc (fields,
14110 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14111 * sizeof (struct field));
c906108c
SS
14112 }
14113
3567439c 14114 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14115 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14116 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14117 FIELD_BITSIZE (fields[num_fields]) = 0;
14118
14119 num_fields++;
14120 }
14121 }
14122
14123 child_die = sibling_die (child_die);
14124 }
14125
14126 if (num_fields)
14127 {
f792889a
DJ
14128 TYPE_NFIELDS (this_type) = num_fields;
14129 TYPE_FIELDS (this_type) = (struct field *)
14130 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14131 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14132 sizeof (struct field) * num_fields);
b8c9b27d 14133 xfree (fields);
c906108c 14134 }
c906108c 14135 }
134d01f1 14136
6c83ed52
TT
14137 /* If we are reading an enum from a .debug_types unit, and the enum
14138 is a declaration, and the enum is not the signatured type in the
14139 unit, then we do not want to add a symbol for it. Adding a
14140 symbol would in some cases obscure the true definition of the
14141 enum, giving users an incomplete type when the definition is
14142 actually available. Note that we do not want to do this for all
14143 enums which are just declarations, because C++0x allows forward
14144 enum declarations. */
3019eac3 14145 if (cu->per_cu->is_debug_types
6c83ed52
TT
14146 && die_is_declaration (die, cu))
14147 {
52dc124a 14148 struct signatured_type *sig_type;
6c83ed52 14149
c0f78cd4 14150 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14151 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14152 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14153 return;
14154 }
14155
f792889a 14156 new_symbol (die, this_type, cu);
c906108c
SS
14157}
14158
14159/* Extract all information from a DW_TAG_array_type DIE and put it in
14160 the DIE's type field. For now, this only handles one dimensional
14161 arrays. */
14162
f792889a 14163static struct type *
e7c27a73 14164read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14165{
e7c27a73 14166 struct objfile *objfile = cu->objfile;
c906108c 14167 struct die_info *child_die;
7e314c57 14168 struct type *type;
c906108c 14169 struct type *element_type, *range_type, *index_type;
c906108c 14170 struct attribute *attr;
15d034d0 14171 const char *name;
dc53a7ad 14172 unsigned int bit_stride = 0;
c906108c 14173
e7c27a73 14174 element_type = die_type (die, cu);
c906108c 14175
7e314c57
JK
14176 /* The die_type call above may have already set the type for this DIE. */
14177 type = get_die_type (die, cu);
14178 if (type)
14179 return type;
14180
dc53a7ad
JB
14181 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14182 if (attr != NULL)
14183 bit_stride = DW_UNSND (attr) * 8;
14184
14185 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14186 if (attr != NULL)
14187 bit_stride = DW_UNSND (attr);
14188
c906108c
SS
14189 /* Irix 6.2 native cc creates array types without children for
14190 arrays with unspecified length. */
639d11d3 14191 if (die->child == NULL)
c906108c 14192 {
46bf5051 14193 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14194 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14195 type = create_array_type_with_stride (NULL, element_type, range_type,
14196 bit_stride);
f792889a 14197 return set_die_type (die, type, cu);
c906108c
SS
14198 }
14199
791afaa2 14200 std::vector<struct type *> range_types;
639d11d3 14201 child_die = die->child;
c906108c
SS
14202 while (child_die && child_die->tag)
14203 {
14204 if (child_die->tag == DW_TAG_subrange_type)
14205 {
f792889a 14206 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14207
f792889a 14208 if (child_type != NULL)
a02abb62 14209 {
0963b4bd
MS
14210 /* The range type was succesfully read. Save it for the
14211 array type creation. */
791afaa2 14212 range_types.push_back (child_type);
a02abb62 14213 }
c906108c
SS
14214 }
14215 child_die = sibling_die (child_die);
14216 }
14217
14218 /* Dwarf2 dimensions are output from left to right, create the
14219 necessary array types in backwards order. */
7ca2d3a3 14220
c906108c 14221 type = element_type;
7ca2d3a3
DL
14222
14223 if (read_array_order (die, cu) == DW_ORD_col_major)
14224 {
14225 int i = 0;
9a619af0 14226
791afaa2 14227 while (i < range_types.size ())
dc53a7ad
JB
14228 type = create_array_type_with_stride (NULL, type, range_types[i++],
14229 bit_stride);
7ca2d3a3
DL
14230 }
14231 else
14232 {
791afaa2 14233 size_t ndim = range_types.size ();
7ca2d3a3 14234 while (ndim-- > 0)
dc53a7ad
JB
14235 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14236 bit_stride);
7ca2d3a3 14237 }
c906108c 14238
f5f8a009
EZ
14239 /* Understand Dwarf2 support for vector types (like they occur on
14240 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14241 array type. This is not part of the Dwarf2/3 standard yet, but a
14242 custom vendor extension. The main difference between a regular
14243 array and the vector variant is that vectors are passed by value
14244 to functions. */
e142c38c 14245 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14246 if (attr)
ea37ba09 14247 make_vector_type (type);
f5f8a009 14248
dbc98a8b
KW
14249 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14250 implementation may choose to implement triple vectors using this
14251 attribute. */
14252 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14253 if (attr)
14254 {
14255 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14256 TYPE_LENGTH (type) = DW_UNSND (attr);
14257 else
3e43a32a
MS
14258 complaint (&symfile_complaints,
14259 _("DW_AT_byte_size for array type smaller "
14260 "than the total size of elements"));
dbc98a8b
KW
14261 }
14262
39cbfefa
DJ
14263 name = dwarf2_name (die, cu);
14264 if (name)
14265 TYPE_NAME (type) = name;
6e70227d 14266
0963b4bd 14267 /* Install the type in the die. */
7e314c57
JK
14268 set_die_type (die, type, cu);
14269
14270 /* set_die_type should be already done. */
b4ba55a1
JB
14271 set_descriptive_type (type, die, cu);
14272
7e314c57 14273 return type;
c906108c
SS
14274}
14275
7ca2d3a3 14276static enum dwarf_array_dim_ordering
6e70227d 14277read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14278{
14279 struct attribute *attr;
14280
14281 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14282
aead7601
SM
14283 if (attr)
14284 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14285
0963b4bd
MS
14286 /* GNU F77 is a special case, as at 08/2004 array type info is the
14287 opposite order to the dwarf2 specification, but data is still
14288 laid out as per normal fortran.
7ca2d3a3 14289
0963b4bd
MS
14290 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14291 version checking. */
7ca2d3a3 14292
905e0470
PM
14293 if (cu->language == language_fortran
14294 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14295 {
14296 return DW_ORD_row_major;
14297 }
14298
6e70227d 14299 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14300 {
14301 case array_column_major:
14302 return DW_ORD_col_major;
14303 case array_row_major:
14304 default:
14305 return DW_ORD_row_major;
14306 };
14307}
14308
72019c9c 14309/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14310 the DIE's type field. */
72019c9c 14311
f792889a 14312static struct type *
72019c9c
GM
14313read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14314{
7e314c57
JK
14315 struct type *domain_type, *set_type;
14316 struct attribute *attr;
f792889a 14317
7e314c57
JK
14318 domain_type = die_type (die, cu);
14319
14320 /* The die_type call above may have already set the type for this DIE. */
14321 set_type = get_die_type (die, cu);
14322 if (set_type)
14323 return set_type;
14324
14325 set_type = create_set_type (NULL, domain_type);
14326
14327 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14328 if (attr)
14329 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14330
f792889a 14331 return set_die_type (die, set_type, cu);
72019c9c 14332}
7ca2d3a3 14333
0971de02
TT
14334/* A helper for read_common_block that creates a locexpr baton.
14335 SYM is the symbol which we are marking as computed.
14336 COMMON_DIE is the DIE for the common block.
14337 COMMON_LOC is the location expression attribute for the common
14338 block itself.
14339 MEMBER_LOC is the location expression attribute for the particular
14340 member of the common block that we are processing.
14341 CU is the CU from which the above come. */
14342
14343static void
14344mark_common_block_symbol_computed (struct symbol *sym,
14345 struct die_info *common_die,
14346 struct attribute *common_loc,
14347 struct attribute *member_loc,
14348 struct dwarf2_cu *cu)
14349{
14350 struct objfile *objfile = dwarf2_per_objfile->objfile;
14351 struct dwarf2_locexpr_baton *baton;
14352 gdb_byte *ptr;
14353 unsigned int cu_off;
14354 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14355 LONGEST offset = 0;
14356
14357 gdb_assert (common_loc && member_loc);
14358 gdb_assert (attr_form_is_block (common_loc));
14359 gdb_assert (attr_form_is_block (member_loc)
14360 || attr_form_is_constant (member_loc));
14361
8d749320 14362 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14363 baton->per_cu = cu->per_cu;
14364 gdb_assert (baton->per_cu);
14365
14366 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14367
14368 if (attr_form_is_constant (member_loc))
14369 {
14370 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14371 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14372 }
14373 else
14374 baton->size += DW_BLOCK (member_loc)->size;
14375
224c3ddb 14376 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14377 baton->data = ptr;
14378
14379 *ptr++ = DW_OP_call4;
9c541725 14380 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14381 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14382 ptr += 4;
14383
14384 if (attr_form_is_constant (member_loc))
14385 {
14386 *ptr++ = DW_OP_addr;
14387 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14388 ptr += cu->header.addr_size;
14389 }
14390 else
14391 {
14392 /* We have to copy the data here, because DW_OP_call4 will only
14393 use a DW_AT_location attribute. */
14394 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14395 ptr += DW_BLOCK (member_loc)->size;
14396 }
14397
14398 *ptr++ = DW_OP_plus;
14399 gdb_assert (ptr - baton->data == baton->size);
14400
0971de02 14401 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14402 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14403}
14404
4357ac6c
TT
14405/* Create appropriate locally-scoped variables for all the
14406 DW_TAG_common_block entries. Also create a struct common_block
14407 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14408 is used to sepate the common blocks name namespace from regular
14409 variable names. */
c906108c
SS
14410
14411static void
e7c27a73 14412read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14413{
0971de02
TT
14414 struct attribute *attr;
14415
14416 attr = dwarf2_attr (die, DW_AT_location, cu);
14417 if (attr)
14418 {
14419 /* Support the .debug_loc offsets. */
14420 if (attr_form_is_block (attr))
14421 {
14422 /* Ok. */
14423 }
14424 else if (attr_form_is_section_offset (attr))
14425 {
14426 dwarf2_complex_location_expr_complaint ();
14427 attr = NULL;
14428 }
14429 else
14430 {
14431 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14432 "common block member");
14433 attr = NULL;
14434 }
14435 }
14436
639d11d3 14437 if (die->child != NULL)
c906108c 14438 {
4357ac6c
TT
14439 struct objfile *objfile = cu->objfile;
14440 struct die_info *child_die;
14441 size_t n_entries = 0, size;
14442 struct common_block *common_block;
14443 struct symbol *sym;
74ac6d43 14444
4357ac6c
TT
14445 for (child_die = die->child;
14446 child_die && child_die->tag;
14447 child_die = sibling_die (child_die))
14448 ++n_entries;
14449
14450 size = (sizeof (struct common_block)
14451 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14452 common_block
14453 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14454 size);
4357ac6c
TT
14455 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14456 common_block->n_entries = 0;
14457
14458 for (child_die = die->child;
14459 child_die && child_die->tag;
14460 child_die = sibling_die (child_die))
14461 {
14462 /* Create the symbol in the DW_TAG_common_block block in the current
14463 symbol scope. */
e7c27a73 14464 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14465 if (sym != NULL)
14466 {
14467 struct attribute *member_loc;
14468
14469 common_block->contents[common_block->n_entries++] = sym;
14470
14471 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14472 cu);
14473 if (member_loc)
14474 {
14475 /* GDB has handled this for a long time, but it is
14476 not specified by DWARF. It seems to have been
14477 emitted by gfortran at least as recently as:
14478 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14479 complaint (&symfile_complaints,
14480 _("Variable in common block has "
14481 "DW_AT_data_member_location "
14482 "- DIE at 0x%x [in module %s]"),
9c541725 14483 to_underlying (child_die->sect_off),
4262abfb 14484 objfile_name (cu->objfile));
0971de02
TT
14485
14486 if (attr_form_is_section_offset (member_loc))
14487 dwarf2_complex_location_expr_complaint ();
14488 else if (attr_form_is_constant (member_loc)
14489 || attr_form_is_block (member_loc))
14490 {
14491 if (attr)
14492 mark_common_block_symbol_computed (sym, die, attr,
14493 member_loc, cu);
14494 }
14495 else
14496 dwarf2_complex_location_expr_complaint ();
14497 }
14498 }
c906108c 14499 }
4357ac6c
TT
14500
14501 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14502 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14503 }
14504}
14505
0114d602 14506/* Create a type for a C++ namespace. */
d9fa45fe 14507
0114d602
DJ
14508static struct type *
14509read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14510{
e7c27a73 14511 struct objfile *objfile = cu->objfile;
0114d602 14512 const char *previous_prefix, *name;
9219021c 14513 int is_anonymous;
0114d602
DJ
14514 struct type *type;
14515
14516 /* For extensions, reuse the type of the original namespace. */
14517 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14518 {
14519 struct die_info *ext_die;
14520 struct dwarf2_cu *ext_cu = cu;
9a619af0 14521
0114d602
DJ
14522 ext_die = dwarf2_extension (die, &ext_cu);
14523 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14524
14525 /* EXT_CU may not be the same as CU.
02142a6c 14526 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14527 return set_die_type (die, type, cu);
14528 }
9219021c 14529
e142c38c 14530 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14531
14532 /* Now build the name of the current namespace. */
14533
0114d602
DJ
14534 previous_prefix = determine_prefix (die, cu);
14535 if (previous_prefix[0] != '\0')
14536 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14537 previous_prefix, name, 0, cu);
0114d602
DJ
14538
14539 /* Create the type. */
19f392bc 14540 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14541 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14542
60531b24 14543 return set_die_type (die, type, cu);
0114d602
DJ
14544}
14545
22cee43f 14546/* Read a namespace scope. */
0114d602
DJ
14547
14548static void
14549read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14550{
14551 struct objfile *objfile = cu->objfile;
0114d602 14552 int is_anonymous;
9219021c 14553
5c4e30ca
DC
14554 /* Add a symbol associated to this if we haven't seen the namespace
14555 before. Also, add a using directive if it's an anonymous
14556 namespace. */
9219021c 14557
f2f0e013 14558 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14559 {
14560 struct type *type;
14561
0114d602 14562 type = read_type_die (die, cu);
e7c27a73 14563 new_symbol (die, type, cu);
5c4e30ca 14564
e8e80198 14565 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14566 if (is_anonymous)
0114d602
DJ
14567 {
14568 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14569
eb1e02fd 14570 std::vector<const char *> excludes;
22cee43f
PMR
14571 add_using_directive (using_directives (cu->language),
14572 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 14573 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 14574 }
5c4e30ca 14575 }
9219021c 14576
639d11d3 14577 if (die->child != NULL)
d9fa45fe 14578 {
639d11d3 14579 struct die_info *child_die = die->child;
6e70227d 14580
d9fa45fe
DC
14581 while (child_die && child_die->tag)
14582 {
e7c27a73 14583 process_die (child_die, cu);
d9fa45fe
DC
14584 child_die = sibling_die (child_die);
14585 }
14586 }
38d518c9
EZ
14587}
14588
f55ee35c
JK
14589/* Read a Fortran module as type. This DIE can be only a declaration used for
14590 imported module. Still we need that type as local Fortran "use ... only"
14591 declaration imports depend on the created type in determine_prefix. */
14592
14593static struct type *
14594read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14595{
14596 struct objfile *objfile = cu->objfile;
15d034d0 14597 const char *module_name;
f55ee35c
JK
14598 struct type *type;
14599
14600 module_name = dwarf2_name (die, cu);
14601 if (!module_name)
3e43a32a
MS
14602 complaint (&symfile_complaints,
14603 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14604 to_underlying (die->sect_off));
19f392bc 14605 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14606
14607 /* determine_prefix uses TYPE_TAG_NAME. */
14608 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14609
14610 return set_die_type (die, type, cu);
14611}
14612
5d7cb8df
JK
14613/* Read a Fortran module. */
14614
14615static void
14616read_module (struct die_info *die, struct dwarf2_cu *cu)
14617{
14618 struct die_info *child_die = die->child;
530e8392
KB
14619 struct type *type;
14620
14621 type = read_type_die (die, cu);
14622 new_symbol (die, type, cu);
5d7cb8df 14623
5d7cb8df
JK
14624 while (child_die && child_die->tag)
14625 {
14626 process_die (child_die, cu);
14627 child_die = sibling_die (child_die);
14628 }
14629}
14630
38d518c9
EZ
14631/* Return the name of the namespace represented by DIE. Set
14632 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14633 namespace. */
14634
14635static const char *
e142c38c 14636namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14637{
14638 struct die_info *current_die;
14639 const char *name = NULL;
14640
14641 /* Loop through the extensions until we find a name. */
14642
14643 for (current_die = die;
14644 current_die != NULL;
f2f0e013 14645 current_die = dwarf2_extension (die, &cu))
38d518c9 14646 {
96553a0c
DE
14647 /* We don't use dwarf2_name here so that we can detect the absence
14648 of a name -> anonymous namespace. */
7d45c7c3 14649 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14650
38d518c9
EZ
14651 if (name != NULL)
14652 break;
14653 }
14654
14655 /* Is it an anonymous namespace? */
14656
14657 *is_anonymous = (name == NULL);
14658 if (*is_anonymous)
2b1dbab0 14659 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14660
14661 return name;
d9fa45fe
DC
14662}
14663
c906108c
SS
14664/* Extract all information from a DW_TAG_pointer_type DIE and add to
14665 the user defined type vector. */
14666
f792889a 14667static struct type *
e7c27a73 14668read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14669{
5e2b427d 14670 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14671 struct comp_unit_head *cu_header = &cu->header;
c906108c 14672 struct type *type;
8b2dbe47
KB
14673 struct attribute *attr_byte_size;
14674 struct attribute *attr_address_class;
14675 int byte_size, addr_class;
7e314c57
JK
14676 struct type *target_type;
14677
14678 target_type = die_type (die, cu);
c906108c 14679
7e314c57
JK
14680 /* The die_type call above may have already set the type for this DIE. */
14681 type = get_die_type (die, cu);
14682 if (type)
14683 return type;
14684
14685 type = lookup_pointer_type (target_type);
8b2dbe47 14686
e142c38c 14687 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14688 if (attr_byte_size)
14689 byte_size = DW_UNSND (attr_byte_size);
c906108c 14690 else
8b2dbe47
KB
14691 byte_size = cu_header->addr_size;
14692
e142c38c 14693 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14694 if (attr_address_class)
14695 addr_class = DW_UNSND (attr_address_class);
14696 else
14697 addr_class = DW_ADDR_none;
14698
14699 /* If the pointer size or address class is different than the
14700 default, create a type variant marked as such and set the
14701 length accordingly. */
14702 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14703 {
5e2b427d 14704 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14705 {
14706 int type_flags;
14707
849957d9 14708 type_flags = gdbarch_address_class_type_flags
5e2b427d 14709 (gdbarch, byte_size, addr_class);
876cecd0
TT
14710 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14711 == 0);
8b2dbe47
KB
14712 type = make_type_with_address_space (type, type_flags);
14713 }
14714 else if (TYPE_LENGTH (type) != byte_size)
14715 {
3e43a32a
MS
14716 complaint (&symfile_complaints,
14717 _("invalid pointer size %d"), byte_size);
8b2dbe47 14718 }
6e70227d 14719 else
9a619af0
MS
14720 {
14721 /* Should we also complain about unhandled address classes? */
14722 }
c906108c 14723 }
8b2dbe47
KB
14724
14725 TYPE_LENGTH (type) = byte_size;
f792889a 14726 return set_die_type (die, type, cu);
c906108c
SS
14727}
14728
14729/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14730 the user defined type vector. */
14731
f792889a 14732static struct type *
e7c27a73 14733read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14734{
14735 struct type *type;
14736 struct type *to_type;
14737 struct type *domain;
14738
e7c27a73
DJ
14739 to_type = die_type (die, cu);
14740 domain = die_containing_type (die, cu);
0d5de010 14741
7e314c57
JK
14742 /* The calls above may have already set the type for this DIE. */
14743 type = get_die_type (die, cu);
14744 if (type)
14745 return type;
14746
0d5de010
DJ
14747 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14748 type = lookup_methodptr_type (to_type);
7078baeb
TT
14749 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14750 {
14751 struct type *new_type = alloc_type (cu->objfile);
14752
14753 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14754 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14755 TYPE_VARARGS (to_type));
14756 type = lookup_methodptr_type (new_type);
14757 }
0d5de010
DJ
14758 else
14759 type = lookup_memberptr_type (to_type, domain);
c906108c 14760
f792889a 14761 return set_die_type (die, type, cu);
c906108c
SS
14762}
14763
4297a3f0 14764/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14765 the user defined type vector. */
14766
f792889a 14767static struct type *
4297a3f0
AV
14768read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14769 enum type_code refcode)
c906108c 14770{
e7c27a73 14771 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14772 struct type *type, *target_type;
c906108c
SS
14773 struct attribute *attr;
14774
4297a3f0
AV
14775 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14776
7e314c57
JK
14777 target_type = die_type (die, cu);
14778
14779 /* The die_type call above may have already set the type for this DIE. */
14780 type = get_die_type (die, cu);
14781 if (type)
14782 return type;
14783
4297a3f0 14784 type = lookup_reference_type (target_type, refcode);
e142c38c 14785 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14786 if (attr)
14787 {
14788 TYPE_LENGTH (type) = DW_UNSND (attr);
14789 }
14790 else
14791 {
107d2387 14792 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14793 }
f792889a 14794 return set_die_type (die, type, cu);
c906108c
SS
14795}
14796
cf363f18
MW
14797/* Add the given cv-qualifiers to the element type of the array. GCC
14798 outputs DWARF type qualifiers that apply to an array, not the
14799 element type. But GDB relies on the array element type to carry
14800 the cv-qualifiers. This mimics section 6.7.3 of the C99
14801 specification. */
14802
14803static struct type *
14804add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14805 struct type *base_type, int cnst, int voltl)
14806{
14807 struct type *el_type, *inner_array;
14808
14809 base_type = copy_type (base_type);
14810 inner_array = base_type;
14811
14812 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14813 {
14814 TYPE_TARGET_TYPE (inner_array) =
14815 copy_type (TYPE_TARGET_TYPE (inner_array));
14816 inner_array = TYPE_TARGET_TYPE (inner_array);
14817 }
14818
14819 el_type = TYPE_TARGET_TYPE (inner_array);
14820 cnst |= TYPE_CONST (el_type);
14821 voltl |= TYPE_VOLATILE (el_type);
14822 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14823
14824 return set_die_type (die, base_type, cu);
14825}
14826
f792889a 14827static struct type *
e7c27a73 14828read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14829{
f792889a 14830 struct type *base_type, *cv_type;
c906108c 14831
e7c27a73 14832 base_type = die_type (die, cu);
7e314c57
JK
14833
14834 /* The die_type call above may have already set the type for this DIE. */
14835 cv_type = get_die_type (die, cu);
14836 if (cv_type)
14837 return cv_type;
14838
2f608a3a
KW
14839 /* In case the const qualifier is applied to an array type, the element type
14840 is so qualified, not the array type (section 6.7.3 of C99). */
14841 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14842 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14843
f792889a
DJ
14844 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14845 return set_die_type (die, cv_type, cu);
c906108c
SS
14846}
14847
f792889a 14848static struct type *
e7c27a73 14849read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14850{
f792889a 14851 struct type *base_type, *cv_type;
c906108c 14852
e7c27a73 14853 base_type = die_type (die, cu);
7e314c57
JK
14854
14855 /* The die_type call above may have already set the type for this DIE. */
14856 cv_type = get_die_type (die, cu);
14857 if (cv_type)
14858 return cv_type;
14859
cf363f18
MW
14860 /* In case the volatile qualifier is applied to an array type, the
14861 element type is so qualified, not the array type (section 6.7.3
14862 of C99). */
14863 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14864 return add_array_cv_type (die, cu, base_type, 0, 1);
14865
f792889a
DJ
14866 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14867 return set_die_type (die, cv_type, cu);
c906108c
SS
14868}
14869
06d66ee9
TT
14870/* Handle DW_TAG_restrict_type. */
14871
14872static struct type *
14873read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14874{
14875 struct type *base_type, *cv_type;
14876
14877 base_type = die_type (die, cu);
14878
14879 /* The die_type call above may have already set the type for this DIE. */
14880 cv_type = get_die_type (die, cu);
14881 if (cv_type)
14882 return cv_type;
14883
14884 cv_type = make_restrict_type (base_type);
14885 return set_die_type (die, cv_type, cu);
14886}
14887
a2c2acaf
MW
14888/* Handle DW_TAG_atomic_type. */
14889
14890static struct type *
14891read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14892{
14893 struct type *base_type, *cv_type;
14894
14895 base_type = die_type (die, cu);
14896
14897 /* The die_type call above may have already set the type for this DIE. */
14898 cv_type = get_die_type (die, cu);
14899 if (cv_type)
14900 return cv_type;
14901
14902 cv_type = make_atomic_type (base_type);
14903 return set_die_type (die, cv_type, cu);
14904}
14905
c906108c
SS
14906/* Extract all information from a DW_TAG_string_type DIE and add to
14907 the user defined type vector. It isn't really a user defined type,
14908 but it behaves like one, with other DIE's using an AT_user_def_type
14909 attribute to reference it. */
14910
f792889a 14911static struct type *
e7c27a73 14912read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14913{
e7c27a73 14914 struct objfile *objfile = cu->objfile;
3b7538c0 14915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14916 struct type *type, *range_type, *index_type, *char_type;
14917 struct attribute *attr;
14918 unsigned int length;
14919
e142c38c 14920 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14921 if (attr)
14922 {
14923 length = DW_UNSND (attr);
14924 }
14925 else
14926 {
0963b4bd 14927 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14929 if (attr)
14930 {
14931 length = DW_UNSND (attr);
14932 }
14933 else
14934 {
14935 length = 1;
14936 }
c906108c 14937 }
6ccb9162 14938
46bf5051 14939 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14940 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14941 char_type = language_string_char_type (cu->language_defn, gdbarch);
14942 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14943
f792889a 14944 return set_die_type (die, type, cu);
c906108c
SS
14945}
14946
4d804846
JB
14947/* Assuming that DIE corresponds to a function, returns nonzero
14948 if the function is prototyped. */
14949
14950static int
14951prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14952{
14953 struct attribute *attr;
14954
14955 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14956 if (attr && (DW_UNSND (attr) != 0))
14957 return 1;
14958
14959 /* The DWARF standard implies that the DW_AT_prototyped attribute
14960 is only meaninful for C, but the concept also extends to other
14961 languages that allow unprototyped functions (Eg: Objective C).
14962 For all other languages, assume that functions are always
14963 prototyped. */
14964 if (cu->language != language_c
14965 && cu->language != language_objc
14966 && cu->language != language_opencl)
14967 return 1;
14968
14969 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14970 prototyped and unprototyped functions; default to prototyped,
14971 since that is more common in modern code (and RealView warns
14972 about unprototyped functions). */
14973 if (producer_is_realview (cu->producer))
14974 return 1;
14975
14976 return 0;
14977}
14978
c906108c
SS
14979/* Handle DIES due to C code like:
14980
14981 struct foo
c5aa993b
JM
14982 {
14983 int (*funcp)(int a, long l);
14984 int b;
14985 };
c906108c 14986
0963b4bd 14987 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14988
f792889a 14989static struct type *
e7c27a73 14990read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14991{
bb5ed363 14992 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14993 struct type *type; /* Type that this function returns. */
14994 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14995 struct attribute *attr;
14996
e7c27a73 14997 type = die_type (die, cu);
7e314c57
JK
14998
14999 /* The die_type call above may have already set the type for this DIE. */
15000 ftype = get_die_type (die, cu);
15001 if (ftype)
15002 return ftype;
15003
0c8b41f1 15004 ftype = lookup_function_type (type);
c906108c 15005
4d804846 15006 if (prototyped_function_p (die, cu))
a6c727b2 15007 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15008
c055b101
CV
15009 /* Store the calling convention in the type if it's available in
15010 the subroutine die. Otherwise set the calling convention to
15011 the default value DW_CC_normal. */
15012 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15013 if (attr)
15014 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15015 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15016 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15017 else
15018 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15019
743649fd
MW
15020 /* Record whether the function returns normally to its caller or not
15021 if the DWARF producer set that information. */
15022 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15023 if (attr && (DW_UNSND (attr) != 0))
15024 TYPE_NO_RETURN (ftype) = 1;
15025
76c10ea2
GM
15026 /* We need to add the subroutine type to the die immediately so
15027 we don't infinitely recurse when dealing with parameters
0963b4bd 15028 declared as the same subroutine type. */
76c10ea2 15029 set_die_type (die, ftype, cu);
6e70227d 15030
639d11d3 15031 if (die->child != NULL)
c906108c 15032 {
bb5ed363 15033 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15034 struct die_info *child_die;
8072405b 15035 int nparams, iparams;
c906108c
SS
15036
15037 /* Count the number of parameters.
15038 FIXME: GDB currently ignores vararg functions, but knows about
15039 vararg member functions. */
8072405b 15040 nparams = 0;
639d11d3 15041 child_die = die->child;
c906108c
SS
15042 while (child_die && child_die->tag)
15043 {
15044 if (child_die->tag == DW_TAG_formal_parameter)
15045 nparams++;
15046 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15047 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15048 child_die = sibling_die (child_die);
15049 }
15050
15051 /* Allocate storage for parameters and fill them in. */
15052 TYPE_NFIELDS (ftype) = nparams;
15053 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15054 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15055
8072405b
JK
15056 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15057 even if we error out during the parameters reading below. */
15058 for (iparams = 0; iparams < nparams; iparams++)
15059 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15060
15061 iparams = 0;
639d11d3 15062 child_die = die->child;
c906108c
SS
15063 while (child_die && child_die->tag)
15064 {
15065 if (child_die->tag == DW_TAG_formal_parameter)
15066 {
3ce3b1ba
PA
15067 struct type *arg_type;
15068
15069 /* DWARF version 2 has no clean way to discern C++
15070 static and non-static member functions. G++ helps
15071 GDB by marking the first parameter for non-static
15072 member functions (which is the this pointer) as
15073 artificial. We pass this information to
15074 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15075
15076 DWARF version 3 added DW_AT_object_pointer, which GCC
15077 4.5 does not yet generate. */
e142c38c 15078 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15079 if (attr)
15080 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15081 else
9c37b5ae 15082 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15083 arg_type = die_type (child_die, cu);
15084
15085 /* RealView does not mark THIS as const, which the testsuite
15086 expects. GCC marks THIS as const in method definitions,
15087 but not in the class specifications (GCC PR 43053). */
15088 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15089 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15090 {
15091 int is_this = 0;
15092 struct dwarf2_cu *arg_cu = cu;
15093 const char *name = dwarf2_name (child_die, cu);
15094
15095 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15096 if (attr)
15097 {
15098 /* If the compiler emits this, use it. */
15099 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15100 is_this = 1;
15101 }
15102 else if (name && strcmp (name, "this") == 0)
15103 /* Function definitions will have the argument names. */
15104 is_this = 1;
15105 else if (name == NULL && iparams == 0)
15106 /* Declarations may not have the names, so like
15107 elsewhere in GDB, assume an artificial first
15108 argument is "this". */
15109 is_this = 1;
15110
15111 if (is_this)
15112 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15113 arg_type, 0);
15114 }
15115
15116 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15117 iparams++;
15118 }
15119 child_die = sibling_die (child_die);
15120 }
15121 }
15122
76c10ea2 15123 return ftype;
c906108c
SS
15124}
15125
f792889a 15126static struct type *
e7c27a73 15127read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15128{
e7c27a73 15129 struct objfile *objfile = cu->objfile;
0114d602 15130 const char *name = NULL;
3c8e0968 15131 struct type *this_type, *target_type;
c906108c 15132
94af9270 15133 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15134 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15135 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15136 set_die_type (die, this_type, cu);
3c8e0968
DE
15137 target_type = die_type (die, cu);
15138 if (target_type != this_type)
15139 TYPE_TARGET_TYPE (this_type) = target_type;
15140 else
15141 {
15142 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15143 spec and cause infinite loops in GDB. */
15144 complaint (&symfile_complaints,
15145 _("Self-referential DW_TAG_typedef "
15146 "- DIE at 0x%x [in module %s]"),
9c541725 15147 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15148 TYPE_TARGET_TYPE (this_type) = NULL;
15149 }
f792889a 15150 return this_type;
c906108c
SS
15151}
15152
9b790ce7
UW
15153/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15154 (which may be different from NAME) to the architecture back-end to allow
15155 it to guess the correct format if necessary. */
15156
15157static struct type *
15158dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15159 const char *name_hint)
15160{
15161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15162 const struct floatformat **format;
15163 struct type *type;
15164
15165 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15166 if (format)
15167 type = init_float_type (objfile, bits, name, format);
15168 else
77b7c781 15169 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
15170
15171 return type;
15172}
15173
c906108c
SS
15174/* Find a representation of a given base type and install
15175 it in the TYPE field of the die. */
15176
f792889a 15177static struct type *
e7c27a73 15178read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15179{
e7c27a73 15180 struct objfile *objfile = cu->objfile;
c906108c
SS
15181 struct type *type;
15182 struct attribute *attr;
19f392bc 15183 int encoding = 0, bits = 0;
15d034d0 15184 const char *name;
c906108c 15185
e142c38c 15186 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15187 if (attr)
15188 {
15189 encoding = DW_UNSND (attr);
15190 }
e142c38c 15191 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15192 if (attr)
15193 {
19f392bc 15194 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15195 }
39cbfefa 15196 name = dwarf2_name (die, cu);
6ccb9162 15197 if (!name)
c906108c 15198 {
6ccb9162
UW
15199 complaint (&symfile_complaints,
15200 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15201 }
6ccb9162
UW
15202
15203 switch (encoding)
c906108c 15204 {
6ccb9162
UW
15205 case DW_ATE_address:
15206 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 15207 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 15208 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15209 break;
15210 case DW_ATE_boolean:
19f392bc 15211 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15212 break;
15213 case DW_ATE_complex_float:
9b790ce7 15214 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15215 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15216 break;
15217 case DW_ATE_decimal_float:
19f392bc 15218 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15219 break;
15220 case DW_ATE_float:
9b790ce7 15221 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15222 break;
15223 case DW_ATE_signed:
19f392bc 15224 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15225 break;
15226 case DW_ATE_unsigned:
3b2b8fea
TT
15227 if (cu->language == language_fortran
15228 && name
61012eef 15229 && startswith (name, "character("))
19f392bc
UW
15230 type = init_character_type (objfile, bits, 1, name);
15231 else
15232 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15233 break;
15234 case DW_ATE_signed_char:
6e70227d 15235 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15236 || cu->language == language_pascal
15237 || cu->language == language_fortran)
19f392bc
UW
15238 type = init_character_type (objfile, bits, 0, name);
15239 else
15240 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15241 break;
15242 case DW_ATE_unsigned_char:
868a0084 15243 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15244 || cu->language == language_pascal
c44af4eb
TT
15245 || cu->language == language_fortran
15246 || cu->language == language_rust)
19f392bc
UW
15247 type = init_character_type (objfile, bits, 1, name);
15248 else
15249 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15250 break;
75079b2b 15251 case DW_ATE_UTF:
53e710ac
PA
15252 {
15253 gdbarch *arch = get_objfile_arch (objfile);
15254
15255 if (bits == 16)
15256 type = builtin_type (arch)->builtin_char16;
15257 else if (bits == 32)
15258 type = builtin_type (arch)->builtin_char32;
15259 else
15260 {
15261 complaint (&symfile_complaints,
15262 _("unsupported DW_ATE_UTF bit size: '%d'"),
15263 bits);
15264 type = init_integer_type (objfile, bits, 1, name);
15265 }
15266 return set_die_type (die, type, cu);
15267 }
75079b2b
TT
15268 break;
15269
6ccb9162
UW
15270 default:
15271 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15272 dwarf_type_encoding_name (encoding));
77b7c781 15273 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 15274 break;
c906108c 15275 }
6ccb9162 15276
0114d602 15277 if (name && strcmp (name, "char") == 0)
876cecd0 15278 TYPE_NOSIGN (type) = 1;
0114d602 15279
f792889a 15280 return set_die_type (die, type, cu);
c906108c
SS
15281}
15282
80180f79
SA
15283/* Parse dwarf attribute if it's a block, reference or constant and put the
15284 resulting value of the attribute into struct bound_prop.
15285 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15286
15287static int
15288attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15289 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15290{
15291 struct dwarf2_property_baton *baton;
15292 struct obstack *obstack = &cu->objfile->objfile_obstack;
15293
15294 if (attr == NULL || prop == NULL)
15295 return 0;
15296
15297 if (attr_form_is_block (attr))
15298 {
8d749320 15299 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15300 baton->referenced_type = NULL;
15301 baton->locexpr.per_cu = cu->per_cu;
15302 baton->locexpr.size = DW_BLOCK (attr)->size;
15303 baton->locexpr.data = DW_BLOCK (attr)->data;
15304 prop->data.baton = baton;
15305 prop->kind = PROP_LOCEXPR;
15306 gdb_assert (prop->data.baton != NULL);
15307 }
15308 else if (attr_form_is_ref (attr))
15309 {
15310 struct dwarf2_cu *target_cu = cu;
15311 struct die_info *target_die;
15312 struct attribute *target_attr;
15313
15314 target_die = follow_die_ref (die, attr, &target_cu);
15315 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15316 if (target_attr == NULL)
15317 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15318 target_cu);
80180f79
SA
15319 if (target_attr == NULL)
15320 return 0;
15321
df25ebbd 15322 switch (target_attr->name)
80180f79 15323 {
df25ebbd
JB
15324 case DW_AT_location:
15325 if (attr_form_is_section_offset (target_attr))
15326 {
8d749320 15327 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15328 baton->referenced_type = die_type (target_die, target_cu);
15329 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15330 prop->data.baton = baton;
15331 prop->kind = PROP_LOCLIST;
15332 gdb_assert (prop->data.baton != NULL);
15333 }
15334 else if (attr_form_is_block (target_attr))
15335 {
8d749320 15336 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15337 baton->referenced_type = die_type (target_die, target_cu);
15338 baton->locexpr.per_cu = cu->per_cu;
15339 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15340 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15341 prop->data.baton = baton;
15342 prop->kind = PROP_LOCEXPR;
15343 gdb_assert (prop->data.baton != NULL);
15344 }
15345 else
15346 {
15347 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15348 "dynamic property");
15349 return 0;
15350 }
15351 break;
15352 case DW_AT_data_member_location:
15353 {
15354 LONGEST offset;
15355
15356 if (!handle_data_member_location (target_die, target_cu,
15357 &offset))
15358 return 0;
15359
8d749320 15360 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15361 baton->referenced_type = read_type_die (target_die->parent,
15362 target_cu);
df25ebbd
JB
15363 baton->offset_info.offset = offset;
15364 baton->offset_info.type = die_type (target_die, target_cu);
15365 prop->data.baton = baton;
15366 prop->kind = PROP_ADDR_OFFSET;
15367 break;
15368 }
80180f79
SA
15369 }
15370 }
15371 else if (attr_form_is_constant (attr))
15372 {
15373 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15374 prop->kind = PROP_CONST;
15375 }
15376 else
15377 {
15378 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15379 dwarf2_name (die, cu));
15380 return 0;
15381 }
15382
15383 return 1;
15384}
15385
a02abb62
JB
15386/* Read the given DW_AT_subrange DIE. */
15387
f792889a 15388static struct type *
a02abb62
JB
15389read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15390{
4c9ad8c2 15391 struct type *base_type, *orig_base_type;
a02abb62
JB
15392 struct type *range_type;
15393 struct attribute *attr;
729efb13 15394 struct dynamic_prop low, high;
4fae6e18 15395 int low_default_is_valid;
c451ebe5 15396 int high_bound_is_count = 0;
15d034d0 15397 const char *name;
43bbcdc2 15398 LONGEST negative_mask;
e77813c8 15399
4c9ad8c2
TT
15400 orig_base_type = die_type (die, cu);
15401 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15402 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15403 creating the range type, but we use the result of check_typedef
15404 when examining properties of the type. */
15405 base_type = check_typedef (orig_base_type);
a02abb62 15406
7e314c57
JK
15407 /* The die_type call above may have already set the type for this DIE. */
15408 range_type = get_die_type (die, cu);
15409 if (range_type)
15410 return range_type;
15411
729efb13
SA
15412 low.kind = PROP_CONST;
15413 high.kind = PROP_CONST;
15414 high.data.const_val = 0;
15415
4fae6e18
JK
15416 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15417 omitting DW_AT_lower_bound. */
15418 switch (cu->language)
6e70227d 15419 {
4fae6e18
JK
15420 case language_c:
15421 case language_cplus:
729efb13 15422 low.data.const_val = 0;
4fae6e18
JK
15423 low_default_is_valid = 1;
15424 break;
15425 case language_fortran:
729efb13 15426 low.data.const_val = 1;
4fae6e18
JK
15427 low_default_is_valid = 1;
15428 break;
15429 case language_d:
4fae6e18 15430 case language_objc:
c44af4eb 15431 case language_rust:
729efb13 15432 low.data.const_val = 0;
4fae6e18
JK
15433 low_default_is_valid = (cu->header.version >= 4);
15434 break;
15435 case language_ada:
15436 case language_m2:
15437 case language_pascal:
729efb13 15438 low.data.const_val = 1;
4fae6e18
JK
15439 low_default_is_valid = (cu->header.version >= 4);
15440 break;
15441 default:
729efb13 15442 low.data.const_val = 0;
4fae6e18
JK
15443 low_default_is_valid = 0;
15444 break;
a02abb62
JB
15445 }
15446
e142c38c 15447 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15448 if (attr)
11c1ba78 15449 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15450 else if (!low_default_is_valid)
15451 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15452 "- DIE at 0x%x [in module %s]"),
9c541725 15453 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15454
e142c38c 15455 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15456 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15457 {
15458 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15459 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15460 {
c451ebe5
SA
15461 /* If bounds are constant do the final calculation here. */
15462 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15463 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15464 else
15465 high_bound_is_count = 1;
c2ff108b 15466 }
e77813c8
PM
15467 }
15468
15469 /* Dwarf-2 specifications explicitly allows to create subrange types
15470 without specifying a base type.
15471 In that case, the base type must be set to the type of
15472 the lower bound, upper bound or count, in that order, if any of these
15473 three attributes references an object that has a type.
15474 If no base type is found, the Dwarf-2 specifications say that
15475 a signed integer type of size equal to the size of an address should
15476 be used.
15477 For the following C code: `extern char gdb_int [];'
15478 GCC produces an empty range DIE.
15479 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15480 high bound or count are not yet handled by this code. */
e77813c8
PM
15481 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15482 {
15483 struct objfile *objfile = cu->objfile;
15484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15485 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15486 struct type *int_type = objfile_type (objfile)->builtin_int;
15487
15488 /* Test "int", "long int", and "long long int" objfile types,
15489 and select the first one having a size above or equal to the
15490 architecture address size. */
15491 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15492 base_type = int_type;
15493 else
15494 {
15495 int_type = objfile_type (objfile)->builtin_long;
15496 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15497 base_type = int_type;
15498 else
15499 {
15500 int_type = objfile_type (objfile)->builtin_long_long;
15501 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15502 base_type = int_type;
15503 }
15504 }
15505 }
a02abb62 15506
dbb9c2b1
JB
15507 /* Normally, the DWARF producers are expected to use a signed
15508 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15509 But this is unfortunately not always the case, as witnessed
15510 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15511 is used instead. To work around that ambiguity, we treat
15512 the bounds as signed, and thus sign-extend their values, when
15513 the base type is signed. */
6e70227d 15514 negative_mask =
66c6502d 15515 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15516 if (low.kind == PROP_CONST
15517 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15518 low.data.const_val |= negative_mask;
15519 if (high.kind == PROP_CONST
15520 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15521 high.data.const_val |= negative_mask;
43bbcdc2 15522
729efb13 15523 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15524
c451ebe5
SA
15525 if (high_bound_is_count)
15526 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15527
c2ff108b
JK
15528 /* Ada expects an empty array on no boundary attributes. */
15529 if (attr == NULL && cu->language != language_ada)
729efb13 15530 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15531
39cbfefa
DJ
15532 name = dwarf2_name (die, cu);
15533 if (name)
15534 TYPE_NAME (range_type) = name;
6e70227d 15535
e142c38c 15536 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15537 if (attr)
15538 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15539
7e314c57
JK
15540 set_die_type (die, range_type, cu);
15541
15542 /* set_die_type should be already done. */
b4ba55a1
JB
15543 set_descriptive_type (range_type, die, cu);
15544
7e314c57 15545 return range_type;
a02abb62 15546}
6e70227d 15547
f792889a 15548static struct type *
81a17f79
JB
15549read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15550{
15551 struct type *type;
81a17f79 15552
81a17f79
JB
15553 /* For now, we only support the C meaning of an unspecified type: void. */
15554
19f392bc 15555 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15556 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15557
f792889a 15558 return set_die_type (die, type, cu);
81a17f79 15559}
a02abb62 15560
639d11d3
DC
15561/* Read a single die and all its descendents. Set the die's sibling
15562 field to NULL; set other fields in the die correctly, and set all
15563 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15564 location of the info_ptr after reading all of those dies. PARENT
15565 is the parent of the die in question. */
15566
15567static struct die_info *
dee91e82 15568read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15569 const gdb_byte *info_ptr,
15570 const gdb_byte **new_info_ptr,
dee91e82 15571 struct die_info *parent)
639d11d3
DC
15572{
15573 struct die_info *die;
d521ce57 15574 const gdb_byte *cur_ptr;
639d11d3
DC
15575 int has_children;
15576
bf6af496 15577 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15578 if (die == NULL)
15579 {
15580 *new_info_ptr = cur_ptr;
15581 return NULL;
15582 }
93311388 15583 store_in_ref_table (die, reader->cu);
639d11d3
DC
15584
15585 if (has_children)
bf6af496 15586 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15587 else
15588 {
15589 die->child = NULL;
15590 *new_info_ptr = cur_ptr;
15591 }
15592
15593 die->sibling = NULL;
15594 die->parent = parent;
15595 return die;
15596}
15597
15598/* Read a die, all of its descendents, and all of its siblings; set
15599 all of the fields of all of the dies correctly. Arguments are as
15600 in read_die_and_children. */
15601
15602static struct die_info *
bf6af496 15603read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15604 const gdb_byte *info_ptr,
15605 const gdb_byte **new_info_ptr,
bf6af496 15606 struct die_info *parent)
639d11d3
DC
15607{
15608 struct die_info *first_die, *last_sibling;
d521ce57 15609 const gdb_byte *cur_ptr;
639d11d3 15610
c906108c 15611 cur_ptr = info_ptr;
639d11d3
DC
15612 first_die = last_sibling = NULL;
15613
15614 while (1)
c906108c 15615 {
639d11d3 15616 struct die_info *die
dee91e82 15617 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15618
1d325ec1 15619 if (die == NULL)
c906108c 15620 {
639d11d3
DC
15621 *new_info_ptr = cur_ptr;
15622 return first_die;
c906108c 15623 }
1d325ec1
DJ
15624
15625 if (!first_die)
15626 first_die = die;
c906108c 15627 else
1d325ec1
DJ
15628 last_sibling->sibling = die;
15629
15630 last_sibling = die;
c906108c 15631 }
c906108c
SS
15632}
15633
bf6af496
DE
15634/* Read a die, all of its descendents, and all of its siblings; set
15635 all of the fields of all of the dies correctly. Arguments are as
15636 in read_die_and_children.
15637 This the main entry point for reading a DIE and all its children. */
15638
15639static struct die_info *
15640read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15641 const gdb_byte *info_ptr,
15642 const gdb_byte **new_info_ptr,
bf6af496
DE
15643 struct die_info *parent)
15644{
15645 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15646 new_info_ptr, parent);
15647
b4f54984 15648 if (dwarf_die_debug)
bf6af496
DE
15649 {
15650 fprintf_unfiltered (gdb_stdlog,
15651 "Read die from %s@0x%x of %s:\n",
a32a8923 15652 get_section_name (reader->die_section),
bf6af496
DE
15653 (unsigned) (info_ptr - reader->die_section->buffer),
15654 bfd_get_filename (reader->abfd));
b4f54984 15655 dump_die (die, dwarf_die_debug);
bf6af496
DE
15656 }
15657
15658 return die;
15659}
15660
3019eac3
DE
15661/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15662 attributes.
15663 The caller is responsible for filling in the extra attributes
15664 and updating (*DIEP)->num_attrs.
15665 Set DIEP to point to a newly allocated die with its information,
15666 except for its child, sibling, and parent fields.
15667 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15668
d521ce57 15669static const gdb_byte *
3019eac3 15670read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15671 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15672 int *has_children, int num_extra_attrs)
93311388 15673{
b64f50a1 15674 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15675 struct abbrev_info *abbrev;
15676 struct die_info *die;
15677 struct dwarf2_cu *cu = reader->cu;
15678 bfd *abfd = reader->abfd;
15679
9c541725 15680 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15681 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15682 info_ptr += bytes_read;
15683 if (!abbrev_number)
15684 {
15685 *diep = NULL;
15686 *has_children = 0;
15687 return info_ptr;
15688 }
15689
433df2d4 15690 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15691 if (!abbrev)
348e048f
DE
15692 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15693 abbrev_number,
15694 bfd_get_filename (abfd));
15695
3019eac3 15696 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15697 die->sect_off = sect_off;
93311388
DE
15698 die->tag = abbrev->tag;
15699 die->abbrev = abbrev_number;
15700
3019eac3
DE
15701 /* Make the result usable.
15702 The caller needs to update num_attrs after adding the extra
15703 attributes. */
93311388
DE
15704 die->num_attrs = abbrev->num_attrs;
15705
15706 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15707 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15708 info_ptr);
93311388
DE
15709
15710 *diep = die;
15711 *has_children = abbrev->has_children;
15712 return info_ptr;
15713}
15714
3019eac3
DE
15715/* Read a die and all its attributes.
15716 Set DIEP to point to a newly allocated die with its information,
15717 except for its child, sibling, and parent fields.
15718 Set HAS_CHILDREN to tell whether the die has children or not. */
15719
d521ce57 15720static const gdb_byte *
3019eac3 15721read_full_die (const struct die_reader_specs *reader,
d521ce57 15722 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15723 int *has_children)
15724{
d521ce57 15725 const gdb_byte *result;
bf6af496
DE
15726
15727 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15728
b4f54984 15729 if (dwarf_die_debug)
bf6af496
DE
15730 {
15731 fprintf_unfiltered (gdb_stdlog,
15732 "Read die from %s@0x%x of %s:\n",
a32a8923 15733 get_section_name (reader->die_section),
bf6af496
DE
15734 (unsigned) (info_ptr - reader->die_section->buffer),
15735 bfd_get_filename (reader->abfd));
b4f54984 15736 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15737 }
15738
15739 return result;
3019eac3 15740}
433df2d4
DE
15741\f
15742/* Abbreviation tables.
3019eac3 15743
433df2d4 15744 In DWARF version 2, the description of the debugging information is
c906108c
SS
15745 stored in a separate .debug_abbrev section. Before we read any
15746 dies from a section we read in all abbreviations and install them
433df2d4
DE
15747 in a hash table. */
15748
15749/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15750
15751static struct abbrev_info *
15752abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15753{
15754 struct abbrev_info *abbrev;
15755
8d749320 15756 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15757 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15758
433df2d4
DE
15759 return abbrev;
15760}
15761
15762/* Add an abbreviation to the table. */
c906108c
SS
15763
15764static void
433df2d4
DE
15765abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15766 unsigned int abbrev_number,
15767 struct abbrev_info *abbrev)
15768{
15769 unsigned int hash_number;
15770
15771 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15772 abbrev->next = abbrev_table->abbrevs[hash_number];
15773 abbrev_table->abbrevs[hash_number] = abbrev;
15774}
dee91e82 15775
433df2d4
DE
15776/* Look up an abbrev in the table.
15777 Returns NULL if the abbrev is not found. */
15778
15779static struct abbrev_info *
15780abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15781 unsigned int abbrev_number)
c906108c 15782{
433df2d4
DE
15783 unsigned int hash_number;
15784 struct abbrev_info *abbrev;
15785
15786 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15787 abbrev = abbrev_table->abbrevs[hash_number];
15788
15789 while (abbrev)
15790 {
15791 if (abbrev->number == abbrev_number)
15792 return abbrev;
15793 abbrev = abbrev->next;
15794 }
15795 return NULL;
15796}
15797
15798/* Read in an abbrev table. */
15799
15800static struct abbrev_table *
15801abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15802 sect_offset sect_off)
433df2d4
DE
15803{
15804 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15805 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15806 struct abbrev_table *abbrev_table;
d521ce57 15807 const gdb_byte *abbrev_ptr;
c906108c
SS
15808 struct abbrev_info *cur_abbrev;
15809 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15810 unsigned int abbrev_form;
f3dd6933
DJ
15811 struct attr_abbrev *cur_attrs;
15812 unsigned int allocated_attrs;
c906108c 15813
70ba0933 15814 abbrev_table = XNEW (struct abbrev_table);
9c541725 15815 abbrev_table->sect_off = sect_off;
433df2d4 15816 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15817 abbrev_table->abbrevs =
15818 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15819 ABBREV_HASH_SIZE);
433df2d4
DE
15820 memset (abbrev_table->abbrevs, 0,
15821 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15822
433df2d4 15823 dwarf2_read_section (objfile, section);
9c541725 15824 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15825 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15826 abbrev_ptr += bytes_read;
15827
f3dd6933 15828 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15829 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15830
0963b4bd 15831 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15832 while (abbrev_number)
15833 {
433df2d4 15834 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15835
15836 /* read in abbrev header */
15837 cur_abbrev->number = abbrev_number;
aead7601
SM
15838 cur_abbrev->tag
15839 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15840 abbrev_ptr += bytes_read;
15841 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15842 abbrev_ptr += 1;
15843
15844 /* now read in declarations */
22d2f3ab 15845 for (;;)
c906108c 15846 {
43988095
JK
15847 LONGEST implicit_const;
15848
22d2f3ab
JK
15849 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15850 abbrev_ptr += bytes_read;
15851 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15852 abbrev_ptr += bytes_read;
43988095
JK
15853 if (abbrev_form == DW_FORM_implicit_const)
15854 {
15855 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15856 &bytes_read);
15857 abbrev_ptr += bytes_read;
15858 }
15859 else
15860 {
15861 /* Initialize it due to a false compiler warning. */
15862 implicit_const = -1;
15863 }
22d2f3ab
JK
15864
15865 if (abbrev_name == 0)
15866 break;
15867
f3dd6933 15868 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15869 {
f3dd6933
DJ
15870 allocated_attrs += ATTR_ALLOC_CHUNK;
15871 cur_attrs
224c3ddb 15872 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15873 }
ae038cb0 15874
aead7601
SM
15875 cur_attrs[cur_abbrev->num_attrs].name
15876 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15877 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15878 = (enum dwarf_form) abbrev_form;
43988095 15879 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15880 ++cur_abbrev->num_attrs;
c906108c
SS
15881 }
15882
8d749320
SM
15883 cur_abbrev->attrs =
15884 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15885 cur_abbrev->num_attrs);
f3dd6933
DJ
15886 memcpy (cur_abbrev->attrs, cur_attrs,
15887 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15888
433df2d4 15889 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15890
15891 /* Get next abbreviation.
15892 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15893 always properly terminated with an abbrev number of 0.
15894 Exit loop if we encounter an abbreviation which we have
15895 already read (which means we are about to read the abbreviations
15896 for the next compile unit) or if the end of the abbreviation
15897 table is reached. */
433df2d4 15898 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15899 break;
15900 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15901 abbrev_ptr += bytes_read;
433df2d4 15902 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15903 break;
15904 }
f3dd6933
DJ
15905
15906 xfree (cur_attrs);
433df2d4 15907 return abbrev_table;
c906108c
SS
15908}
15909
433df2d4 15910/* Free the resources held by ABBREV_TABLE. */
c906108c 15911
c906108c 15912static void
433df2d4 15913abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15914{
433df2d4
DE
15915 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15916 xfree (abbrev_table);
c906108c
SS
15917}
15918
f4dc4d17
DE
15919/* Same as abbrev_table_free but as a cleanup.
15920 We pass in a pointer to the pointer to the table so that we can
15921 set the pointer to NULL when we're done. It also simplifies
73051182 15922 build_type_psymtabs_1. */
f4dc4d17
DE
15923
15924static void
15925abbrev_table_free_cleanup (void *table_ptr)
15926{
9a3c8263 15927 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15928
15929 if (*abbrev_table_ptr != NULL)
15930 abbrev_table_free (*abbrev_table_ptr);
15931 *abbrev_table_ptr = NULL;
15932}
15933
433df2d4
DE
15934/* Read the abbrev table for CU from ABBREV_SECTION. */
15935
15936static void
15937dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15938 struct dwarf2_section_info *abbrev_section)
c906108c 15939{
433df2d4 15940 cu->abbrev_table =
9c541725 15941 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15942}
c906108c 15943
433df2d4 15944/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15945
433df2d4
DE
15946static void
15947dwarf2_free_abbrev_table (void *ptr_to_cu)
15948{
9a3c8263 15949 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15950
a2ce51a0
DE
15951 if (cu->abbrev_table != NULL)
15952 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15953 /* Set this to NULL so that we SEGV if we try to read it later,
15954 and also because free_comp_unit verifies this is NULL. */
15955 cu->abbrev_table = NULL;
15956}
15957\f
72bf9492
DJ
15958/* Returns nonzero if TAG represents a type that we might generate a partial
15959 symbol for. */
15960
15961static int
15962is_type_tag_for_partial (int tag)
15963{
15964 switch (tag)
15965 {
15966#if 0
15967 /* Some types that would be reasonable to generate partial symbols for,
15968 that we don't at present. */
15969 case DW_TAG_array_type:
15970 case DW_TAG_file_type:
15971 case DW_TAG_ptr_to_member_type:
15972 case DW_TAG_set_type:
15973 case DW_TAG_string_type:
15974 case DW_TAG_subroutine_type:
15975#endif
15976 case DW_TAG_base_type:
15977 case DW_TAG_class_type:
680b30c7 15978 case DW_TAG_interface_type:
72bf9492
DJ
15979 case DW_TAG_enumeration_type:
15980 case DW_TAG_structure_type:
15981 case DW_TAG_subrange_type:
15982 case DW_TAG_typedef:
15983 case DW_TAG_union_type:
15984 return 1;
15985 default:
15986 return 0;
15987 }
15988}
15989
15990/* Load all DIEs that are interesting for partial symbols into memory. */
15991
15992static struct partial_die_info *
dee91e82 15993load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15994 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15995{
dee91e82 15996 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15997 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15998 struct partial_die_info *part_die;
15999 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16000 struct abbrev_info *abbrev;
16001 unsigned int bytes_read;
5afb4e99 16002 unsigned int load_all = 0;
72bf9492
DJ
16003 int nesting_level = 1;
16004
16005 parent_die = NULL;
16006 last_die = NULL;
16007
7adf1e79
DE
16008 gdb_assert (cu->per_cu != NULL);
16009 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16010 load_all = 1;
16011
72bf9492
DJ
16012 cu->partial_dies
16013 = htab_create_alloc_ex (cu->header.length / 12,
16014 partial_die_hash,
16015 partial_die_eq,
16016 NULL,
16017 &cu->comp_unit_obstack,
16018 hashtab_obstack_allocate,
16019 dummy_obstack_deallocate);
16020
8d749320 16021 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16022
16023 while (1)
16024 {
16025 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16026
16027 /* A NULL abbrev means the end of a series of children. */
16028 if (abbrev == NULL)
16029 {
16030 if (--nesting_level == 0)
16031 {
16032 /* PART_DIE was probably the last thing allocated on the
16033 comp_unit_obstack, so we could call obstack_free
16034 here. We don't do that because the waste is small,
16035 and will be cleaned up when we're done with this
16036 compilation unit. This way, we're also more robust
16037 against other users of the comp_unit_obstack. */
16038 return first_die;
16039 }
16040 info_ptr += bytes_read;
16041 last_die = parent_die;
16042 parent_die = parent_die->die_parent;
16043 continue;
16044 }
16045
98bfdba5
PA
16046 /* Check for template arguments. We never save these; if
16047 they're seen, we just mark the parent, and go on our way. */
16048 if (parent_die != NULL
16049 && cu->language == language_cplus
16050 && (abbrev->tag == DW_TAG_template_type_param
16051 || abbrev->tag == DW_TAG_template_value_param))
16052 {
16053 parent_die->has_template_arguments = 1;
16054
16055 if (!load_all)
16056 {
16057 /* We don't need a partial DIE for the template argument. */
dee91e82 16058 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16059 continue;
16060 }
16061 }
16062
0d99eb77 16063 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16064 Skip their other children. */
16065 if (!load_all
16066 && cu->language == language_cplus
16067 && parent_die != NULL
16068 && parent_die->tag == DW_TAG_subprogram)
16069 {
dee91e82 16070 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16071 continue;
16072 }
16073
5afb4e99
DJ
16074 /* Check whether this DIE is interesting enough to save. Normally
16075 we would not be interested in members here, but there may be
16076 later variables referencing them via DW_AT_specification (for
16077 static members). */
16078 if (!load_all
16079 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16080 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16081 && abbrev->tag != DW_TAG_enumerator
16082 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16083 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16084 && abbrev->tag != DW_TAG_variable
5afb4e99 16085 && abbrev->tag != DW_TAG_namespace
f55ee35c 16086 && abbrev->tag != DW_TAG_module
95554aad 16087 && abbrev->tag != DW_TAG_member
74921315
KS
16088 && abbrev->tag != DW_TAG_imported_unit
16089 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16090 {
16091 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16092 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16093 continue;
16094 }
16095
dee91e82
DE
16096 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16097 info_ptr);
72bf9492
DJ
16098
16099 /* This two-pass algorithm for processing partial symbols has a
16100 high cost in cache pressure. Thus, handle some simple cases
16101 here which cover the majority of C partial symbols. DIEs
16102 which neither have specification tags in them, nor could have
16103 specification tags elsewhere pointing at them, can simply be
16104 processed and discarded.
16105
16106 This segment is also optional; scan_partial_symbols and
16107 add_partial_symbol will handle these DIEs if we chain
16108 them in normally. When compilers which do not emit large
16109 quantities of duplicate debug information are more common,
16110 this code can probably be removed. */
16111
16112 /* Any complete simple types at the top level (pretty much all
16113 of them, for a language without namespaces), can be processed
16114 directly. */
16115 if (parent_die == NULL
16116 && part_die->has_specification == 0
16117 && part_die->is_declaration == 0
d8228535 16118 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16119 || part_die->tag == DW_TAG_base_type
16120 || part_die->tag == DW_TAG_subrange_type))
16121 {
16122 if (building_psymtab && part_die->name != NULL)
04a679b8 16123 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16124 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16125 &objfile->static_psymbols,
1762568f 16126 0, cu->language, objfile);
dee91e82 16127 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16128 continue;
16129 }
16130
d8228535
JK
16131 /* The exception for DW_TAG_typedef with has_children above is
16132 a workaround of GCC PR debug/47510. In the case of this complaint
16133 type_name_no_tag_or_error will error on such types later.
16134
16135 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16136 it could not find the child DIEs referenced later, this is checked
16137 above. In correct DWARF DW_TAG_typedef should have no children. */
16138
16139 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16140 complaint (&symfile_complaints,
16141 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16142 "- DIE at 0x%x [in module %s]"),
9c541725 16143 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16144
72bf9492
DJ
16145 /* If we're at the second level, and we're an enumerator, and
16146 our parent has no specification (meaning possibly lives in a
16147 namespace elsewhere), then we can add the partial symbol now
16148 instead of queueing it. */
16149 if (part_die->tag == DW_TAG_enumerator
16150 && parent_die != NULL
16151 && parent_die->die_parent == NULL
16152 && parent_die->tag == DW_TAG_enumeration_type
16153 && parent_die->has_specification == 0)
16154 {
16155 if (part_die->name == NULL)
3e43a32a
MS
16156 complaint (&symfile_complaints,
16157 _("malformed enumerator DIE ignored"));
72bf9492 16158 else if (building_psymtab)
04a679b8 16159 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16160 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16161 cu->language == language_cplus
bb5ed363
DE
16162 ? &objfile->global_psymbols
16163 : &objfile->static_psymbols,
1762568f 16164 0, cu->language, objfile);
72bf9492 16165
dee91e82 16166 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16167 continue;
16168 }
16169
16170 /* We'll save this DIE so link it in. */
16171 part_die->die_parent = parent_die;
16172 part_die->die_sibling = NULL;
16173 part_die->die_child = NULL;
16174
16175 if (last_die && last_die == parent_die)
16176 last_die->die_child = part_die;
16177 else if (last_die)
16178 last_die->die_sibling = part_die;
16179
16180 last_die = part_die;
16181
16182 if (first_die == NULL)
16183 first_die = part_die;
16184
16185 /* Maybe add the DIE to the hash table. Not all DIEs that we
16186 find interesting need to be in the hash table, because we
16187 also have the parent/sibling/child chains; only those that we
16188 might refer to by offset later during partial symbol reading.
16189
16190 For now this means things that might have be the target of a
16191 DW_AT_specification, DW_AT_abstract_origin, or
16192 DW_AT_extension. DW_AT_extension will refer only to
16193 namespaces; DW_AT_abstract_origin refers to functions (and
16194 many things under the function DIE, but we do not recurse
16195 into function DIEs during partial symbol reading) and
16196 possibly variables as well; DW_AT_specification refers to
16197 declarations. Declarations ought to have the DW_AT_declaration
16198 flag. It happens that GCC forgets to put it in sometimes, but
16199 only for functions, not for types.
16200
16201 Adding more things than necessary to the hash table is harmless
16202 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16203 wasted time in find_partial_die, when we reread the compilation
16204 unit with load_all_dies set. */
72bf9492 16205
5afb4e99 16206 if (load_all
72929c62 16207 || abbrev->tag == DW_TAG_constant
5afb4e99 16208 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16209 || abbrev->tag == DW_TAG_variable
16210 || abbrev->tag == DW_TAG_namespace
16211 || part_die->is_declaration)
16212 {
16213 void **slot;
16214
16215 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16216 to_underlying (part_die->sect_off),
16217 INSERT);
72bf9492
DJ
16218 *slot = part_die;
16219 }
16220
8d749320 16221 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16222
16223 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16224 we have no reason to follow the children of structures; for other
98bfdba5
PA
16225 languages we have to, so that we can get at method physnames
16226 to infer fully qualified class names, for DW_AT_specification,
16227 and for C++ template arguments. For C++, we also look one level
16228 inside functions to find template arguments (if the name of the
16229 function does not already contain the template arguments).
bc30ff58
JB
16230
16231 For Ada, we need to scan the children of subprograms and lexical
16232 blocks as well because Ada allows the definition of nested
16233 entities that could be interesting for the debugger, such as
16234 nested subprograms for instance. */
72bf9492 16235 if (last_die->has_children
5afb4e99
DJ
16236 && (load_all
16237 || last_die->tag == DW_TAG_namespace
f55ee35c 16238 || last_die->tag == DW_TAG_module
72bf9492 16239 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16240 || (cu->language == language_cplus
16241 && last_die->tag == DW_TAG_subprogram
16242 && (last_die->name == NULL
16243 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16244 || (cu->language != language_c
16245 && (last_die->tag == DW_TAG_class_type
680b30c7 16246 || last_die->tag == DW_TAG_interface_type
72bf9492 16247 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16248 || last_die->tag == DW_TAG_union_type))
16249 || (cu->language == language_ada
16250 && (last_die->tag == DW_TAG_subprogram
16251 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16252 {
16253 nesting_level++;
16254 parent_die = last_die;
16255 continue;
16256 }
16257
16258 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16259 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16260
16261 /* Back to the top, do it again. */
16262 }
16263}
16264
c906108c
SS
16265/* Read a minimal amount of information into the minimal die structure. */
16266
d521ce57 16267static const gdb_byte *
dee91e82
DE
16268read_partial_die (const struct die_reader_specs *reader,
16269 struct partial_die_info *part_die,
16270 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16271 const gdb_byte *info_ptr)
c906108c 16272{
dee91e82 16273 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16274 struct objfile *objfile = cu->objfile;
d521ce57 16275 const gdb_byte *buffer = reader->buffer;
fa238c03 16276 unsigned int i;
c906108c 16277 struct attribute attr;
c5aa993b 16278 int has_low_pc_attr = 0;
c906108c 16279 int has_high_pc_attr = 0;
91da1414 16280 int high_pc_relative = 0;
c906108c 16281
72bf9492 16282 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16283
9c541725 16284 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16285
16286 info_ptr += abbrev_len;
16287
16288 if (abbrev == NULL)
16289 return info_ptr;
16290
c906108c
SS
16291 part_die->tag = abbrev->tag;
16292 part_die->has_children = abbrev->has_children;
c906108c
SS
16293
16294 for (i = 0; i < abbrev->num_attrs; ++i)
16295 {
dee91e82 16296 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16297
16298 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16299 partial symbol table. */
c906108c
SS
16300 switch (attr.name)
16301 {
16302 case DW_AT_name:
71c25dea
TT
16303 switch (part_die->tag)
16304 {
16305 case DW_TAG_compile_unit:
95554aad 16306 case DW_TAG_partial_unit:
348e048f 16307 case DW_TAG_type_unit:
71c25dea
TT
16308 /* Compilation units have a DW_AT_name that is a filename, not
16309 a source language identifier. */
16310 case DW_TAG_enumeration_type:
16311 case DW_TAG_enumerator:
16312 /* These tags always have simple identifiers already; no need
16313 to canonicalize them. */
16314 part_die->name = DW_STRING (&attr);
16315 break;
16316 default:
16317 part_die->name
16318 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16319 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16320 break;
16321 }
c906108c 16322 break;
31ef98ae 16323 case DW_AT_linkage_name:
c906108c 16324 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16325 /* Note that both forms of linkage name might appear. We
16326 assume they will be the same, and we only store the last
16327 one we see. */
94af9270
KS
16328 if (cu->language == language_ada)
16329 part_die->name = DW_STRING (&attr);
abc72ce4 16330 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16331 break;
16332 case DW_AT_low_pc:
16333 has_low_pc_attr = 1;
31aa7e4e 16334 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16335 break;
16336 case DW_AT_high_pc:
16337 has_high_pc_attr = 1;
31aa7e4e
JB
16338 part_die->highpc = attr_value_as_address (&attr);
16339 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16340 high_pc_relative = 1;
c906108c
SS
16341 break;
16342 case DW_AT_location:
0963b4bd 16343 /* Support the .debug_loc offsets. */
8e19ed76
PS
16344 if (attr_form_is_block (&attr))
16345 {
95554aad 16346 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16347 }
3690dd37 16348 else if (attr_form_is_section_offset (&attr))
8e19ed76 16349 {
4d3c2250 16350 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16351 }
16352 else
16353 {
4d3c2250
KB
16354 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16355 "partial symbol information");
8e19ed76 16356 }
c906108c 16357 break;
c906108c
SS
16358 case DW_AT_external:
16359 part_die->is_external = DW_UNSND (&attr);
16360 break;
16361 case DW_AT_declaration:
16362 part_die->is_declaration = DW_UNSND (&attr);
16363 break;
16364 case DW_AT_type:
16365 part_die->has_type = 1;
16366 break;
16367 case DW_AT_abstract_origin:
16368 case DW_AT_specification:
72bf9492
DJ
16369 case DW_AT_extension:
16370 part_die->has_specification = 1;
c764a876 16371 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16372 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16373 || cu->per_cu->is_dwz);
c906108c
SS
16374 break;
16375 case DW_AT_sibling:
16376 /* Ignore absolute siblings, they might point outside of
16377 the current compile unit. */
16378 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16379 complaint (&symfile_complaints,
16380 _("ignoring absolute DW_AT_sibling"));
c906108c 16381 else
b9502d3f 16382 {
9c541725
PA
16383 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16384 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16385
16386 if (sibling_ptr < info_ptr)
16387 complaint (&symfile_complaints,
16388 _("DW_AT_sibling points backwards"));
22869d73
KS
16389 else if (sibling_ptr > reader->buffer_end)
16390 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16391 else
16392 part_die->sibling = sibling_ptr;
16393 }
c906108c 16394 break;
fa4028e9
JB
16395 case DW_AT_byte_size:
16396 part_die->has_byte_size = 1;
16397 break;
ff908ebf
AW
16398 case DW_AT_const_value:
16399 part_die->has_const_value = 1;
16400 break;
68511cec
CES
16401 case DW_AT_calling_convention:
16402 /* DWARF doesn't provide a way to identify a program's source-level
16403 entry point. DW_AT_calling_convention attributes are only meant
16404 to describe functions' calling conventions.
16405
16406 However, because it's a necessary piece of information in
0c1b455e
TT
16407 Fortran, and before DWARF 4 DW_CC_program was the only
16408 piece of debugging information whose definition refers to
16409 a 'main program' at all, several compilers marked Fortran
16410 main programs with DW_CC_program --- even when those
16411 functions use the standard calling conventions.
16412
16413 Although DWARF now specifies a way to provide this
16414 information, we support this practice for backward
16415 compatibility. */
68511cec 16416 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16417 && cu->language == language_fortran)
16418 part_die->main_subprogram = 1;
68511cec 16419 break;
481860b3
GB
16420 case DW_AT_inline:
16421 if (DW_UNSND (&attr) == DW_INL_inlined
16422 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16423 part_die->may_be_inlined = 1;
16424 break;
95554aad
TT
16425
16426 case DW_AT_import:
16427 if (part_die->tag == DW_TAG_imported_unit)
36586728 16428 {
9c541725 16429 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16430 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16431 || cu->per_cu->is_dwz);
16432 }
95554aad
TT
16433 break;
16434
0c1b455e
TT
16435 case DW_AT_main_subprogram:
16436 part_die->main_subprogram = DW_UNSND (&attr);
16437 break;
16438
c906108c
SS
16439 default:
16440 break;
16441 }
16442 }
16443
91da1414
MW
16444 if (high_pc_relative)
16445 part_die->highpc += part_die->lowpc;
16446
9373cf26
JK
16447 if (has_low_pc_attr && has_high_pc_attr)
16448 {
16449 /* When using the GNU linker, .gnu.linkonce. sections are used to
16450 eliminate duplicate copies of functions and vtables and such.
16451 The linker will arbitrarily choose one and discard the others.
16452 The AT_*_pc values for such functions refer to local labels in
16453 these sections. If the section from that file was discarded, the
16454 labels are not in the output, so the relocs get a value of 0.
16455 If this is a discarded function, mark the pc bounds as invalid,
16456 so that GDB will ignore it. */
16457 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16458 {
bb5ed363 16459 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16460
16461 complaint (&symfile_complaints,
16462 _("DW_AT_low_pc %s is zero "
16463 "for DIE at 0x%x [in module %s]"),
16464 paddress (gdbarch, part_die->lowpc),
9c541725 16465 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16466 }
16467 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16468 else if (part_die->lowpc >= part_die->highpc)
16469 {
bb5ed363 16470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16471
16472 complaint (&symfile_complaints,
16473 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16474 "for DIE at 0x%x [in module %s]"),
16475 paddress (gdbarch, part_die->lowpc),
16476 paddress (gdbarch, part_die->highpc),
9c541725
PA
16477 to_underlying (part_die->sect_off),
16478 objfile_name (objfile));
9373cf26
JK
16479 }
16480 else
16481 part_die->has_pc_info = 1;
16482 }
85cbf3d3 16483
c906108c
SS
16484 return info_ptr;
16485}
16486
72bf9492
DJ
16487/* Find a cached partial DIE at OFFSET in CU. */
16488
16489static struct partial_die_info *
9c541725 16490find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16491{
16492 struct partial_die_info *lookup_die = NULL;
16493 struct partial_die_info part_die;
16494
9c541725 16495 part_die.sect_off = sect_off;
9a3c8263
SM
16496 lookup_die = ((struct partial_die_info *)
16497 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16498 to_underlying (sect_off)));
72bf9492 16499
72bf9492
DJ
16500 return lookup_die;
16501}
16502
348e048f
DE
16503/* Find a partial DIE at OFFSET, which may or may not be in CU,
16504 except in the case of .debug_types DIEs which do not reference
16505 outside their CU (they do however referencing other types via
55f1336d 16506 DW_FORM_ref_sig8). */
72bf9492
DJ
16507
16508static struct partial_die_info *
9c541725 16509find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16510{
bb5ed363 16511 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16512 struct dwarf2_per_cu_data *per_cu = NULL;
16513 struct partial_die_info *pd = NULL;
72bf9492 16514
36586728 16515 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16516 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16517 {
9c541725 16518 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16519 if (pd != NULL)
16520 return pd;
0d99eb77
DE
16521 /* We missed recording what we needed.
16522 Load all dies and try again. */
16523 per_cu = cu->per_cu;
5afb4e99 16524 }
0d99eb77
DE
16525 else
16526 {
16527 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16528 if (cu->per_cu->is_debug_types)
0d99eb77 16529 {
9c541725
PA
16530 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16531 " external reference to offset 0x%x [in module %s].\n"),
16532 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16533 bfd_get_filename (objfile->obfd));
16534 }
9c541725 16535 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16536 objfile);
72bf9492 16537
0d99eb77
DE
16538 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16539 load_partial_comp_unit (per_cu);
ae038cb0 16540
0d99eb77 16541 per_cu->cu->last_used = 0;
9c541725 16542 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16543 }
5afb4e99 16544
dee91e82
DE
16545 /* If we didn't find it, and not all dies have been loaded,
16546 load them all and try again. */
16547
5afb4e99
DJ
16548 if (pd == NULL && per_cu->load_all_dies == 0)
16549 {
5afb4e99 16550 per_cu->load_all_dies = 1;
fd820528
DE
16551
16552 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16553 THIS_CU->cu may already be in use. So we can't just free it and
16554 replace its DIEs with the ones we read in. Instead, we leave those
16555 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16556 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16557 set. */
dee91e82 16558 load_partial_comp_unit (per_cu);
5afb4e99 16559
9c541725 16560 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16561 }
16562
16563 if (pd == NULL)
16564 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16565 _("could not find partial DIE 0x%x "
16566 "in cache [from module %s]\n"),
9c541725 16567 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16568 return pd;
72bf9492
DJ
16569}
16570
abc72ce4
DE
16571/* See if we can figure out if the class lives in a namespace. We do
16572 this by looking for a member function; its demangled name will
16573 contain namespace info, if there is any. */
16574
16575static void
16576guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16577 struct dwarf2_cu *cu)
16578{
16579 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16580 what template types look like, because the demangler
16581 frequently doesn't give the same name as the debug info. We
16582 could fix this by only using the demangled name to get the
16583 prefix (but see comment in read_structure_type). */
16584
16585 struct partial_die_info *real_pdi;
16586 struct partial_die_info *child_pdi;
16587
16588 /* If this DIE (this DIE's specification, if any) has a parent, then
16589 we should not do this. We'll prepend the parent's fully qualified
16590 name when we create the partial symbol. */
16591
16592 real_pdi = struct_pdi;
16593 while (real_pdi->has_specification)
36586728
TT
16594 real_pdi = find_partial_die (real_pdi->spec_offset,
16595 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16596
16597 if (real_pdi->die_parent != NULL)
16598 return;
16599
16600 for (child_pdi = struct_pdi->die_child;
16601 child_pdi != NULL;
16602 child_pdi = child_pdi->die_sibling)
16603 {
16604 if (child_pdi->tag == DW_TAG_subprogram
16605 && child_pdi->linkage_name != NULL)
16606 {
16607 char *actual_class_name
16608 = language_class_name_from_physname (cu->language_defn,
16609 child_pdi->linkage_name);
16610 if (actual_class_name != NULL)
16611 {
16612 struct_pdi->name
224c3ddb
SM
16613 = ((const char *)
16614 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16615 actual_class_name,
16616 strlen (actual_class_name)));
abc72ce4
DE
16617 xfree (actual_class_name);
16618 }
16619 break;
16620 }
16621 }
16622}
16623
72bf9492
DJ
16624/* Adjust PART_DIE before generating a symbol for it. This function
16625 may set the is_external flag or change the DIE's name. */
16626
16627static void
16628fixup_partial_die (struct partial_die_info *part_die,
16629 struct dwarf2_cu *cu)
16630{
abc72ce4
DE
16631 /* Once we've fixed up a die, there's no point in doing so again.
16632 This also avoids a memory leak if we were to call
16633 guess_partial_die_structure_name multiple times. */
16634 if (part_die->fixup_called)
16635 return;
16636
72bf9492
DJ
16637 /* If we found a reference attribute and the DIE has no name, try
16638 to find a name in the referred to DIE. */
16639
16640 if (part_die->name == NULL && part_die->has_specification)
16641 {
16642 struct partial_die_info *spec_die;
72bf9492 16643
36586728
TT
16644 spec_die = find_partial_die (part_die->spec_offset,
16645 part_die->spec_is_dwz, cu);
72bf9492 16646
10b3939b 16647 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16648
16649 if (spec_die->name)
16650 {
16651 part_die->name = spec_die->name;
16652
16653 /* Copy DW_AT_external attribute if it is set. */
16654 if (spec_die->is_external)
16655 part_die->is_external = spec_die->is_external;
16656 }
16657 }
16658
16659 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16660
16661 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16662 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16663
abc72ce4
DE
16664 /* If there is no parent die to provide a namespace, and there are
16665 children, see if we can determine the namespace from their linkage
122d1940 16666 name. */
abc72ce4 16667 if (cu->language == language_cplus
8b70b953 16668 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16669 && part_die->die_parent == NULL
16670 && part_die->has_children
16671 && (part_die->tag == DW_TAG_class_type
16672 || part_die->tag == DW_TAG_structure_type
16673 || part_die->tag == DW_TAG_union_type))
16674 guess_partial_die_structure_name (part_die, cu);
16675
53832f31
TT
16676 /* GCC might emit a nameless struct or union that has a linkage
16677 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16678 if (part_die->name == NULL
96408a79
SA
16679 && (part_die->tag == DW_TAG_class_type
16680 || part_die->tag == DW_TAG_interface_type
16681 || part_die->tag == DW_TAG_structure_type
16682 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16683 && part_die->linkage_name != NULL)
16684 {
16685 char *demangled;
16686
8de20a37 16687 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16688 if (demangled)
16689 {
96408a79
SA
16690 const char *base;
16691
16692 /* Strip any leading namespaces/classes, keep only the base name.
16693 DW_AT_name for named DIEs does not contain the prefixes. */
16694 base = strrchr (demangled, ':');
16695 if (base && base > demangled && base[-1] == ':')
16696 base++;
16697 else
16698 base = demangled;
16699
34a68019 16700 part_die->name
224c3ddb
SM
16701 = ((const char *)
16702 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16703 base, strlen (base)));
53832f31
TT
16704 xfree (demangled);
16705 }
16706 }
16707
abc72ce4 16708 part_die->fixup_called = 1;
72bf9492
DJ
16709}
16710
a8329558 16711/* Read an attribute value described by an attribute form. */
c906108c 16712
d521ce57 16713static const gdb_byte *
dee91e82
DE
16714read_attribute_value (const struct die_reader_specs *reader,
16715 struct attribute *attr, unsigned form,
43988095 16716 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16717{
dee91e82 16718 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16719 struct objfile *objfile = cu->objfile;
16720 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16721 bfd *abfd = reader->abfd;
e7c27a73 16722 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16723 unsigned int bytes_read;
16724 struct dwarf_block *blk;
16725
aead7601 16726 attr->form = (enum dwarf_form) form;
a8329558 16727 switch (form)
c906108c 16728 {
c906108c 16729 case DW_FORM_ref_addr:
ae411497 16730 if (cu->header.version == 2)
4568ecf9 16731 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16732 else
4568ecf9
DE
16733 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16734 &cu->header, &bytes_read);
ae411497
TT
16735 info_ptr += bytes_read;
16736 break;
36586728
TT
16737 case DW_FORM_GNU_ref_alt:
16738 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16739 info_ptr += bytes_read;
16740 break;
ae411497 16741 case DW_FORM_addr:
e7c27a73 16742 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16743 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16744 info_ptr += bytes_read;
c906108c
SS
16745 break;
16746 case DW_FORM_block2:
7b5a2f43 16747 blk = dwarf_alloc_block (cu);
c906108c
SS
16748 blk->size = read_2_bytes (abfd, info_ptr);
16749 info_ptr += 2;
16750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16751 info_ptr += blk->size;
16752 DW_BLOCK (attr) = blk;
16753 break;
16754 case DW_FORM_block4:
7b5a2f43 16755 blk = dwarf_alloc_block (cu);
c906108c
SS
16756 blk->size = read_4_bytes (abfd, info_ptr);
16757 info_ptr += 4;
16758 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16759 info_ptr += blk->size;
16760 DW_BLOCK (attr) = blk;
16761 break;
16762 case DW_FORM_data2:
16763 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16764 info_ptr += 2;
16765 break;
16766 case DW_FORM_data4:
16767 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16768 info_ptr += 4;
16769 break;
16770 case DW_FORM_data8:
16771 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16772 info_ptr += 8;
16773 break;
0224619f
JK
16774 case DW_FORM_data16:
16775 blk = dwarf_alloc_block (cu);
16776 blk->size = 16;
16777 blk->data = read_n_bytes (abfd, info_ptr, 16);
16778 info_ptr += 16;
16779 DW_BLOCK (attr) = blk;
16780 break;
2dc7f7b3
TT
16781 case DW_FORM_sec_offset:
16782 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16783 info_ptr += bytes_read;
16784 break;
c906108c 16785 case DW_FORM_string:
9b1c24c8 16786 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16787 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16788 info_ptr += bytes_read;
16789 break;
4bdf3d34 16790 case DW_FORM_strp:
36586728
TT
16791 if (!cu->per_cu->is_dwz)
16792 {
16793 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16794 &bytes_read);
16795 DW_STRING_IS_CANONICAL (attr) = 0;
16796 info_ptr += bytes_read;
16797 break;
16798 }
16799 /* FALLTHROUGH */
43988095
JK
16800 case DW_FORM_line_strp:
16801 if (!cu->per_cu->is_dwz)
16802 {
16803 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16804 cu_header, &bytes_read);
16805 DW_STRING_IS_CANONICAL (attr) = 0;
16806 info_ptr += bytes_read;
16807 break;
16808 }
16809 /* FALLTHROUGH */
36586728
TT
16810 case DW_FORM_GNU_strp_alt:
16811 {
16812 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16813 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16814 &bytes_read);
16815
16816 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16817 DW_STRING_IS_CANONICAL (attr) = 0;
16818 info_ptr += bytes_read;
16819 }
4bdf3d34 16820 break;
2dc7f7b3 16821 case DW_FORM_exprloc:
c906108c 16822 case DW_FORM_block:
7b5a2f43 16823 blk = dwarf_alloc_block (cu);
c906108c
SS
16824 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16825 info_ptr += bytes_read;
16826 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16827 info_ptr += blk->size;
16828 DW_BLOCK (attr) = blk;
16829 break;
16830 case DW_FORM_block1:
7b5a2f43 16831 blk = dwarf_alloc_block (cu);
c906108c
SS
16832 blk->size = read_1_byte (abfd, info_ptr);
16833 info_ptr += 1;
16834 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16835 info_ptr += blk->size;
16836 DW_BLOCK (attr) = blk;
16837 break;
16838 case DW_FORM_data1:
16839 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16840 info_ptr += 1;
16841 break;
16842 case DW_FORM_flag:
16843 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16844 info_ptr += 1;
16845 break;
2dc7f7b3
TT
16846 case DW_FORM_flag_present:
16847 DW_UNSND (attr) = 1;
16848 break;
c906108c
SS
16849 case DW_FORM_sdata:
16850 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16851 info_ptr += bytes_read;
16852 break;
16853 case DW_FORM_udata:
16854 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16855 info_ptr += bytes_read;
16856 break;
16857 case DW_FORM_ref1:
9c541725 16858 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16859 + read_1_byte (abfd, info_ptr));
c906108c
SS
16860 info_ptr += 1;
16861 break;
16862 case DW_FORM_ref2:
9c541725 16863 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16864 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16865 info_ptr += 2;
16866 break;
16867 case DW_FORM_ref4:
9c541725 16868 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16869 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16870 info_ptr += 4;
16871 break;
613e1657 16872 case DW_FORM_ref8:
9c541725 16873 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16874 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16875 info_ptr += 8;
16876 break;
55f1336d 16877 case DW_FORM_ref_sig8:
ac9ec31b 16878 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16879 info_ptr += 8;
16880 break;
c906108c 16881 case DW_FORM_ref_udata:
9c541725 16882 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16883 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16884 info_ptr += bytes_read;
16885 break;
c906108c 16886 case DW_FORM_indirect:
a8329558
KW
16887 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16888 info_ptr += bytes_read;
43988095
JK
16889 if (form == DW_FORM_implicit_const)
16890 {
16891 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16892 info_ptr += bytes_read;
16893 }
16894 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16895 info_ptr);
16896 break;
16897 case DW_FORM_implicit_const:
16898 DW_SND (attr) = implicit_const;
a8329558 16899 break;
3019eac3
DE
16900 case DW_FORM_GNU_addr_index:
16901 if (reader->dwo_file == NULL)
16902 {
16903 /* For now flag a hard error.
16904 Later we can turn this into a complaint. */
16905 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16906 dwarf_form_name (form),
16907 bfd_get_filename (abfd));
16908 }
16909 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16910 info_ptr += bytes_read;
16911 break;
16912 case DW_FORM_GNU_str_index:
16913 if (reader->dwo_file == NULL)
16914 {
16915 /* For now flag a hard error.
16916 Later we can turn this into a complaint if warranted. */
16917 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16918 dwarf_form_name (form),
16919 bfd_get_filename (abfd));
16920 }
16921 {
16922 ULONGEST str_index =
16923 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16924
342587c4 16925 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16926 DW_STRING_IS_CANONICAL (attr) = 0;
16927 info_ptr += bytes_read;
16928 }
16929 break;
c906108c 16930 default:
8a3fe4f8 16931 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16932 dwarf_form_name (form),
16933 bfd_get_filename (abfd));
c906108c 16934 }
28e94949 16935
36586728 16936 /* Super hack. */
7771576e 16937 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16938 attr->form = DW_FORM_GNU_ref_alt;
16939
28e94949
JB
16940 /* We have seen instances where the compiler tried to emit a byte
16941 size attribute of -1 which ended up being encoded as an unsigned
16942 0xffffffff. Although 0xffffffff is technically a valid size value,
16943 an object of this size seems pretty unlikely so we can relatively
16944 safely treat these cases as if the size attribute was invalid and
16945 treat them as zero by default. */
16946 if (attr->name == DW_AT_byte_size
16947 && form == DW_FORM_data4
16948 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16949 {
16950 complaint
16951 (&symfile_complaints,
43bbcdc2
PH
16952 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16953 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16954 DW_UNSND (attr) = 0;
16955 }
28e94949 16956
c906108c
SS
16957 return info_ptr;
16958}
16959
a8329558
KW
16960/* Read an attribute described by an abbreviated attribute. */
16961
d521ce57 16962static const gdb_byte *
dee91e82
DE
16963read_attribute (const struct die_reader_specs *reader,
16964 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16965 const gdb_byte *info_ptr)
a8329558
KW
16966{
16967 attr->name = abbrev->name;
43988095
JK
16968 return read_attribute_value (reader, attr, abbrev->form,
16969 abbrev->implicit_const, info_ptr);
a8329558
KW
16970}
16971
0963b4bd 16972/* Read dwarf information from a buffer. */
c906108c
SS
16973
16974static unsigned int
a1855c1d 16975read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16976{
fe1b8b76 16977 return bfd_get_8 (abfd, buf);
c906108c
SS
16978}
16979
16980static int
a1855c1d 16981read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16982{
fe1b8b76 16983 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16984}
16985
16986static unsigned int
a1855c1d 16987read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16988{
fe1b8b76 16989 return bfd_get_16 (abfd, buf);
c906108c
SS
16990}
16991
21ae7a4d 16992static int
a1855c1d 16993read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16994{
16995 return bfd_get_signed_16 (abfd, buf);
16996}
16997
c906108c 16998static unsigned int
a1855c1d 16999read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17000{
fe1b8b76 17001 return bfd_get_32 (abfd, buf);
c906108c
SS
17002}
17003
21ae7a4d 17004static int
a1855c1d 17005read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17006{
17007 return bfd_get_signed_32 (abfd, buf);
17008}
17009
93311388 17010static ULONGEST
a1855c1d 17011read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17012{
fe1b8b76 17013 return bfd_get_64 (abfd, buf);
c906108c
SS
17014}
17015
17016static CORE_ADDR
d521ce57 17017read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17018 unsigned int *bytes_read)
c906108c 17019{
e7c27a73 17020 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17021 CORE_ADDR retval = 0;
17022
107d2387 17023 if (cu_header->signed_addr_p)
c906108c 17024 {
107d2387
AC
17025 switch (cu_header->addr_size)
17026 {
17027 case 2:
fe1b8b76 17028 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17029 break;
17030 case 4:
fe1b8b76 17031 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17032 break;
17033 case 8:
fe1b8b76 17034 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17035 break;
17036 default:
8e65ff28 17037 internal_error (__FILE__, __LINE__,
e2e0b3e5 17038 _("read_address: bad switch, signed [in module %s]"),
659b0389 17039 bfd_get_filename (abfd));
107d2387
AC
17040 }
17041 }
17042 else
17043 {
17044 switch (cu_header->addr_size)
17045 {
17046 case 2:
fe1b8b76 17047 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17048 break;
17049 case 4:
fe1b8b76 17050 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17051 break;
17052 case 8:
fe1b8b76 17053 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17054 break;
17055 default:
8e65ff28 17056 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17057 _("read_address: bad switch, "
17058 "unsigned [in module %s]"),
659b0389 17059 bfd_get_filename (abfd));
107d2387 17060 }
c906108c 17061 }
64367e0a 17062
107d2387
AC
17063 *bytes_read = cu_header->addr_size;
17064 return retval;
c906108c
SS
17065}
17066
f7ef9339 17067/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17068 specification allows the initial length to take up either 4 bytes
17069 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17070 bytes describe the length and all offsets will be 8 bytes in length
17071 instead of 4.
17072
f7ef9339
KB
17073 An older, non-standard 64-bit format is also handled by this
17074 function. The older format in question stores the initial length
17075 as an 8-byte quantity without an escape value. Lengths greater
17076 than 2^32 aren't very common which means that the initial 4 bytes
17077 is almost always zero. Since a length value of zero doesn't make
17078 sense for the 32-bit format, this initial zero can be considered to
17079 be an escape value which indicates the presence of the older 64-bit
17080 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17081 greater than 4GB. If it becomes necessary to handle lengths
17082 somewhat larger than 4GB, we could allow other small values (such
17083 as the non-sensical values of 1, 2, and 3) to also be used as
17084 escape values indicating the presence of the old format.
f7ef9339 17085
917c78fc
MK
17086 The value returned via bytes_read should be used to increment the
17087 relevant pointer after calling read_initial_length().
c764a876 17088
613e1657
KB
17089 [ Note: read_initial_length() and read_offset() are based on the
17090 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17091 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17092 from:
17093
f7ef9339 17094 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17095
613e1657
KB
17096 This document is only a draft and is subject to change. (So beware.)
17097
f7ef9339 17098 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17099 determined empirically by examining 64-bit ELF files produced by
17100 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17101
17102 - Kevin, July 16, 2002
613e1657
KB
17103 ] */
17104
17105static LONGEST
d521ce57 17106read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17107{
fe1b8b76 17108 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17109
dd373385 17110 if (length == 0xffffffff)
613e1657 17111 {
fe1b8b76 17112 length = bfd_get_64 (abfd, buf + 4);
613e1657 17113 *bytes_read = 12;
613e1657 17114 }
dd373385 17115 else if (length == 0)
f7ef9339 17116 {
dd373385 17117 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17118 length = bfd_get_64 (abfd, buf);
f7ef9339 17119 *bytes_read = 8;
f7ef9339 17120 }
613e1657
KB
17121 else
17122 {
17123 *bytes_read = 4;
613e1657
KB
17124 }
17125
c764a876
DE
17126 return length;
17127}
dd373385 17128
c764a876
DE
17129/* Cover function for read_initial_length.
17130 Returns the length of the object at BUF, and stores the size of the
17131 initial length in *BYTES_READ and stores the size that offsets will be in
17132 *OFFSET_SIZE.
17133 If the initial length size is not equivalent to that specified in
17134 CU_HEADER then issue a complaint.
17135 This is useful when reading non-comp-unit headers. */
dd373385 17136
c764a876 17137static LONGEST
d521ce57 17138read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17139 const struct comp_unit_head *cu_header,
17140 unsigned int *bytes_read,
17141 unsigned int *offset_size)
17142{
17143 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17144
17145 gdb_assert (cu_header->initial_length_size == 4
17146 || cu_header->initial_length_size == 8
17147 || cu_header->initial_length_size == 12);
17148
17149 if (cu_header->initial_length_size != *bytes_read)
17150 complaint (&symfile_complaints,
17151 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17152
c764a876 17153 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17154 return length;
613e1657
KB
17155}
17156
17157/* Read an offset from the data stream. The size of the offset is
917c78fc 17158 given by cu_header->offset_size. */
613e1657
KB
17159
17160static LONGEST
d521ce57
TT
17161read_offset (bfd *abfd, const gdb_byte *buf,
17162 const struct comp_unit_head *cu_header,
891d2f0b 17163 unsigned int *bytes_read)
c764a876
DE
17164{
17165 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17166
c764a876
DE
17167 *bytes_read = cu_header->offset_size;
17168 return offset;
17169}
17170
17171/* Read an offset from the data stream. */
17172
17173static LONGEST
d521ce57 17174read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17175{
17176 LONGEST retval = 0;
17177
c764a876 17178 switch (offset_size)
613e1657
KB
17179 {
17180 case 4:
fe1b8b76 17181 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17182 break;
17183 case 8:
fe1b8b76 17184 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17185 break;
17186 default:
8e65ff28 17187 internal_error (__FILE__, __LINE__,
c764a876 17188 _("read_offset_1: bad switch [in module %s]"),
659b0389 17189 bfd_get_filename (abfd));
613e1657
KB
17190 }
17191
917c78fc 17192 return retval;
613e1657
KB
17193}
17194
d521ce57
TT
17195static const gdb_byte *
17196read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17197{
17198 /* If the size of a host char is 8 bits, we can return a pointer
17199 to the buffer, otherwise we have to copy the data to a buffer
17200 allocated on the temporary obstack. */
4bdf3d34 17201 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17202 return buf;
c906108c
SS
17203}
17204
d521ce57
TT
17205static const char *
17206read_direct_string (bfd *abfd, const gdb_byte *buf,
17207 unsigned int *bytes_read_ptr)
c906108c
SS
17208{
17209 /* If the size of a host char is 8 bits, we can return a pointer
17210 to the string, otherwise we have to copy the string to a buffer
17211 allocated on the temporary obstack. */
4bdf3d34 17212 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17213 if (*buf == '\0')
17214 {
17215 *bytes_read_ptr = 1;
17216 return NULL;
17217 }
d521ce57
TT
17218 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17219 return (const char *) buf;
4bdf3d34
JJ
17220}
17221
43988095
JK
17222/* Return pointer to string at section SECT offset STR_OFFSET with error
17223 reporting strings FORM_NAME and SECT_NAME. */
17224
d521ce57 17225static const char *
43988095
JK
17226read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17227 struct dwarf2_section_info *sect,
17228 const char *form_name,
17229 const char *sect_name)
17230{
17231 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17232 if (sect->buffer == NULL)
17233 error (_("%s used without %s section [in module %s]"),
17234 form_name, sect_name, bfd_get_filename (abfd));
17235 if (str_offset >= sect->size)
17236 error (_("%s pointing outside of %s section [in module %s]"),
17237 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17238 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17239 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17240 return NULL;
43988095
JK
17241 return (const char *) (sect->buffer + str_offset);
17242}
17243
17244/* Return pointer to string at .debug_str offset STR_OFFSET. */
17245
17246static const char *
17247read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17248{
17249 return read_indirect_string_at_offset_from (abfd, str_offset,
17250 &dwarf2_per_objfile->str,
17251 "DW_FORM_strp", ".debug_str");
17252}
17253
17254/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17255
17256static const char *
17257read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17258{
17259 return read_indirect_string_at_offset_from (abfd, str_offset,
17260 &dwarf2_per_objfile->line_str,
17261 "DW_FORM_line_strp",
17262 ".debug_line_str");
c906108c
SS
17263}
17264
36586728
TT
17265/* Read a string at offset STR_OFFSET in the .debug_str section from
17266 the .dwz file DWZ. Throw an error if the offset is too large. If
17267 the string consists of a single NUL byte, return NULL; otherwise
17268 return a pointer to the string. */
17269
d521ce57 17270static const char *
36586728
TT
17271read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17272{
17273 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17274
17275 if (dwz->str.buffer == NULL)
17276 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17277 "section [in module %s]"),
17278 bfd_get_filename (dwz->dwz_bfd));
17279 if (str_offset >= dwz->str.size)
17280 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17281 ".debug_str section [in module %s]"),
17282 bfd_get_filename (dwz->dwz_bfd));
17283 gdb_assert (HOST_CHAR_BIT == 8);
17284 if (dwz->str.buffer[str_offset] == '\0')
17285 return NULL;
d521ce57 17286 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17287}
17288
43988095
JK
17289/* Return pointer to string at .debug_str offset as read from BUF.
17290 BUF is assumed to be in a compilation unit described by CU_HEADER.
17291 Return *BYTES_READ_PTR count of bytes read from BUF. */
17292
d521ce57
TT
17293static const char *
17294read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17295 const struct comp_unit_head *cu_header,
17296 unsigned int *bytes_read_ptr)
17297{
17298 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17299
17300 return read_indirect_string_at_offset (abfd, str_offset);
17301}
17302
43988095
JK
17303/* Return pointer to string at .debug_line_str offset as read from BUF.
17304 BUF is assumed to be in a compilation unit described by CU_HEADER.
17305 Return *BYTES_READ_PTR count of bytes read from BUF. */
17306
17307static const char *
17308read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17309 const struct comp_unit_head *cu_header,
17310 unsigned int *bytes_read_ptr)
17311{
17312 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17313
17314 return read_indirect_line_string_at_offset (abfd, str_offset);
17315}
17316
17317ULONGEST
d521ce57 17318read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17319 unsigned int *bytes_read_ptr)
c906108c 17320{
12df843f 17321 ULONGEST result;
ce5d95e1 17322 unsigned int num_read;
870f88f7 17323 int shift;
c906108c
SS
17324 unsigned char byte;
17325
17326 result = 0;
17327 shift = 0;
17328 num_read = 0;
c906108c
SS
17329 while (1)
17330 {
fe1b8b76 17331 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17332 buf++;
17333 num_read++;
12df843f 17334 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17335 if ((byte & 128) == 0)
17336 {
17337 break;
17338 }
17339 shift += 7;
17340 }
17341 *bytes_read_ptr = num_read;
17342 return result;
17343}
17344
12df843f 17345static LONGEST
d521ce57
TT
17346read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17347 unsigned int *bytes_read_ptr)
c906108c 17348{
12df843f 17349 LONGEST result;
870f88f7 17350 int shift, num_read;
c906108c
SS
17351 unsigned char byte;
17352
17353 result = 0;
17354 shift = 0;
c906108c 17355 num_read = 0;
c906108c
SS
17356 while (1)
17357 {
fe1b8b76 17358 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17359 buf++;
17360 num_read++;
12df843f 17361 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17362 shift += 7;
17363 if ((byte & 128) == 0)
17364 {
17365 break;
17366 }
17367 }
77e0b926 17368 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17369 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17370 *bytes_read_ptr = num_read;
17371 return result;
17372}
17373
3019eac3
DE
17374/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17375 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17376 ADDR_SIZE is the size of addresses from the CU header. */
17377
17378static CORE_ADDR
17379read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17380{
17381 struct objfile *objfile = dwarf2_per_objfile->objfile;
17382 bfd *abfd = objfile->obfd;
17383 const gdb_byte *info_ptr;
17384
17385 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17386 if (dwarf2_per_objfile->addr.buffer == NULL)
17387 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17388 objfile_name (objfile));
3019eac3
DE
17389 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17390 error (_("DW_FORM_addr_index pointing outside of "
17391 ".debug_addr section [in module %s]"),
4262abfb 17392 objfile_name (objfile));
3019eac3
DE
17393 info_ptr = (dwarf2_per_objfile->addr.buffer
17394 + addr_base + addr_index * addr_size);
17395 if (addr_size == 4)
17396 return bfd_get_32 (abfd, info_ptr);
17397 else
17398 return bfd_get_64 (abfd, info_ptr);
17399}
17400
17401/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17402
17403static CORE_ADDR
17404read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17405{
17406 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17407}
17408
17409/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17410
17411static CORE_ADDR
d521ce57 17412read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17413 unsigned int *bytes_read)
17414{
17415 bfd *abfd = cu->objfile->obfd;
17416 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17417
17418 return read_addr_index (cu, addr_index);
17419}
17420
17421/* Data structure to pass results from dwarf2_read_addr_index_reader
17422 back to dwarf2_read_addr_index. */
17423
17424struct dwarf2_read_addr_index_data
17425{
17426 ULONGEST addr_base;
17427 int addr_size;
17428};
17429
17430/* die_reader_func for dwarf2_read_addr_index. */
17431
17432static void
17433dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17434 const gdb_byte *info_ptr,
3019eac3
DE
17435 struct die_info *comp_unit_die,
17436 int has_children,
17437 void *data)
17438{
17439 struct dwarf2_cu *cu = reader->cu;
17440 struct dwarf2_read_addr_index_data *aidata =
17441 (struct dwarf2_read_addr_index_data *) data;
17442
17443 aidata->addr_base = cu->addr_base;
17444 aidata->addr_size = cu->header.addr_size;
17445}
17446
17447/* Given an index in .debug_addr, fetch the value.
17448 NOTE: This can be called during dwarf expression evaluation,
17449 long after the debug information has been read, and thus per_cu->cu
17450 may no longer exist. */
17451
17452CORE_ADDR
17453dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17454 unsigned int addr_index)
17455{
17456 struct objfile *objfile = per_cu->objfile;
17457 struct dwarf2_cu *cu = per_cu->cu;
17458 ULONGEST addr_base;
17459 int addr_size;
17460
17461 /* This is intended to be called from outside this file. */
17462 dw2_setup (objfile);
17463
17464 /* We need addr_base and addr_size.
17465 If we don't have PER_CU->cu, we have to get it.
17466 Nasty, but the alternative is storing the needed info in PER_CU,
17467 which at this point doesn't seem justified: it's not clear how frequently
17468 it would get used and it would increase the size of every PER_CU.
17469 Entry points like dwarf2_per_cu_addr_size do a similar thing
17470 so we're not in uncharted territory here.
17471 Alas we need to be a bit more complicated as addr_base is contained
17472 in the DIE.
17473
17474 We don't need to read the entire CU(/TU).
17475 We just need the header and top level die.
a1b64ce1 17476
3019eac3 17477 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17478 For now we skip this optimization. */
3019eac3
DE
17479
17480 if (cu != NULL)
17481 {
17482 addr_base = cu->addr_base;
17483 addr_size = cu->header.addr_size;
17484 }
17485 else
17486 {
17487 struct dwarf2_read_addr_index_data aidata;
17488
a1b64ce1
DE
17489 /* Note: We can't use init_cutu_and_read_dies_simple here,
17490 we need addr_base. */
17491 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17492 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17493 addr_base = aidata.addr_base;
17494 addr_size = aidata.addr_size;
17495 }
17496
17497 return read_addr_index_1 (addr_index, addr_base, addr_size);
17498}
17499
57d63ce2
DE
17500/* Given a DW_FORM_GNU_str_index, fetch the string.
17501 This is only used by the Fission support. */
3019eac3 17502
d521ce57 17503static const char *
342587c4 17504read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17505{
17506 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17507 const char *objf_name = objfile_name (objfile);
3019eac3 17508 bfd *abfd = objfile->obfd;
342587c4 17509 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17510 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17511 struct dwarf2_section_info *str_offsets_section =
17512 &reader->dwo_file->sections.str_offsets;
d521ce57 17513 const gdb_byte *info_ptr;
3019eac3 17514 ULONGEST str_offset;
57d63ce2 17515 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17516
73869dc2
DE
17517 dwarf2_read_section (objfile, str_section);
17518 dwarf2_read_section (objfile, str_offsets_section);
17519 if (str_section->buffer == NULL)
57d63ce2 17520 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17521 " in CU at offset 0x%x [in module %s]"),
17522 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17523 if (str_offsets_section->buffer == NULL)
57d63ce2 17524 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17525 " in CU at offset 0x%x [in module %s]"),
17526 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17527 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17528 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17529 " section in CU at offset 0x%x [in module %s]"),
17530 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17531 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17532 + str_index * cu->header.offset_size);
17533 if (cu->header.offset_size == 4)
17534 str_offset = bfd_get_32 (abfd, info_ptr);
17535 else
17536 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17537 if (str_offset >= str_section->size)
57d63ce2 17538 error (_("Offset from %s pointing outside of"
9c541725
PA
17539 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17540 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17541 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17542}
17543
3019eac3
DE
17544/* Return the length of an LEB128 number in BUF. */
17545
17546static int
17547leb128_size (const gdb_byte *buf)
17548{
17549 const gdb_byte *begin = buf;
17550 gdb_byte byte;
17551
17552 while (1)
17553 {
17554 byte = *buf++;
17555 if ((byte & 128) == 0)
17556 return buf - begin;
17557 }
17558}
17559
c906108c 17560static void
e142c38c 17561set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17562{
17563 switch (lang)
17564 {
17565 case DW_LANG_C89:
76bee0cc 17566 case DW_LANG_C99:
0cfd832f 17567 case DW_LANG_C11:
c906108c 17568 case DW_LANG_C:
d1be3247 17569 case DW_LANG_UPC:
e142c38c 17570 cu->language = language_c;
c906108c 17571 break;
9c37b5ae 17572 case DW_LANG_Java:
c906108c 17573 case DW_LANG_C_plus_plus:
0cfd832f
MW
17574 case DW_LANG_C_plus_plus_11:
17575 case DW_LANG_C_plus_plus_14:
e142c38c 17576 cu->language = language_cplus;
c906108c 17577 break;
6aecb9c2
JB
17578 case DW_LANG_D:
17579 cu->language = language_d;
17580 break;
c906108c
SS
17581 case DW_LANG_Fortran77:
17582 case DW_LANG_Fortran90:
b21b22e0 17583 case DW_LANG_Fortran95:
f7de9aab
MW
17584 case DW_LANG_Fortran03:
17585 case DW_LANG_Fortran08:
e142c38c 17586 cu->language = language_fortran;
c906108c 17587 break;
a766d390
DE
17588 case DW_LANG_Go:
17589 cu->language = language_go;
17590 break;
c906108c 17591 case DW_LANG_Mips_Assembler:
e142c38c 17592 cu->language = language_asm;
c906108c
SS
17593 break;
17594 case DW_LANG_Ada83:
8aaf0b47 17595 case DW_LANG_Ada95:
bc5f45f8
JB
17596 cu->language = language_ada;
17597 break;
72019c9c
GM
17598 case DW_LANG_Modula2:
17599 cu->language = language_m2;
17600 break;
fe8e67fd
PM
17601 case DW_LANG_Pascal83:
17602 cu->language = language_pascal;
17603 break;
22566fbd
DJ
17604 case DW_LANG_ObjC:
17605 cu->language = language_objc;
17606 break;
c44af4eb
TT
17607 case DW_LANG_Rust:
17608 case DW_LANG_Rust_old:
17609 cu->language = language_rust;
17610 break;
c906108c
SS
17611 case DW_LANG_Cobol74:
17612 case DW_LANG_Cobol85:
c906108c 17613 default:
e142c38c 17614 cu->language = language_minimal;
c906108c
SS
17615 break;
17616 }
e142c38c 17617 cu->language_defn = language_def (cu->language);
c906108c
SS
17618}
17619
17620/* Return the named attribute or NULL if not there. */
17621
17622static struct attribute *
e142c38c 17623dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17624{
a48e046c 17625 for (;;)
c906108c 17626 {
a48e046c
TT
17627 unsigned int i;
17628 struct attribute *spec = NULL;
17629
17630 for (i = 0; i < die->num_attrs; ++i)
17631 {
17632 if (die->attrs[i].name == name)
17633 return &die->attrs[i];
17634 if (die->attrs[i].name == DW_AT_specification
17635 || die->attrs[i].name == DW_AT_abstract_origin)
17636 spec = &die->attrs[i];
17637 }
17638
17639 if (!spec)
17640 break;
c906108c 17641
f2f0e013 17642 die = follow_die_ref (die, spec, &cu);
f2f0e013 17643 }
c5aa993b 17644
c906108c
SS
17645 return NULL;
17646}
17647
348e048f
DE
17648/* Return the named attribute or NULL if not there,
17649 but do not follow DW_AT_specification, etc.
17650 This is for use in contexts where we're reading .debug_types dies.
17651 Following DW_AT_specification, DW_AT_abstract_origin will take us
17652 back up the chain, and we want to go down. */
17653
17654static struct attribute *
45e58e77 17655dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17656{
17657 unsigned int i;
17658
17659 for (i = 0; i < die->num_attrs; ++i)
17660 if (die->attrs[i].name == name)
17661 return &die->attrs[i];
17662
17663 return NULL;
17664}
17665
7d45c7c3
KB
17666/* Return the string associated with a string-typed attribute, or NULL if it
17667 is either not found or is of an incorrect type. */
17668
17669static const char *
17670dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17671{
17672 struct attribute *attr;
17673 const char *str = NULL;
17674
17675 attr = dwarf2_attr (die, name, cu);
17676
17677 if (attr != NULL)
17678 {
43988095 17679 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17680 || attr->form == DW_FORM_string
17681 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17682 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17683 str = DW_STRING (attr);
17684 else
17685 complaint (&symfile_complaints,
17686 _("string type expected for attribute %s for "
17687 "DIE at 0x%x in module %s"),
9c541725 17688 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17689 objfile_name (cu->objfile));
17690 }
17691
17692 return str;
17693}
17694
05cf31d1
JB
17695/* Return non-zero iff the attribute NAME is defined for the given DIE,
17696 and holds a non-zero value. This function should only be used for
2dc7f7b3 17697 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17698
17699static int
17700dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17701{
17702 struct attribute *attr = dwarf2_attr (die, name, cu);
17703
17704 return (attr && DW_UNSND (attr));
17705}
17706
3ca72b44 17707static int
e142c38c 17708die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17709{
05cf31d1
JB
17710 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17711 which value is non-zero. However, we have to be careful with
17712 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17713 (via dwarf2_flag_true_p) follows this attribute. So we may
17714 end up accidently finding a declaration attribute that belongs
17715 to a different DIE referenced by the specification attribute,
17716 even though the given DIE does not have a declaration attribute. */
17717 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17718 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17719}
17720
63d06c5c 17721/* Return the die giving the specification for DIE, if there is
f2f0e013 17722 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17723 containing the return value on output. If there is no
17724 specification, but there is an abstract origin, that is
17725 returned. */
63d06c5c
DC
17726
17727static struct die_info *
f2f0e013 17728die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17729{
f2f0e013
DJ
17730 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17731 *spec_cu);
63d06c5c 17732
edb3359d
DJ
17733 if (spec_attr == NULL)
17734 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17735
63d06c5c
DC
17736 if (spec_attr == NULL)
17737 return NULL;
17738 else
f2f0e013 17739 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17740}
c906108c 17741
527f3840
JK
17742/* Stub for free_line_header to match void * callback types. */
17743
17744static void
17745free_line_header_voidp (void *arg)
17746{
9a3c8263 17747 struct line_header *lh = (struct line_header *) arg;
527f3840 17748
fff8551c 17749 delete lh;
527f3840
JK
17750}
17751
fff8551c
PA
17752void
17753line_header::add_include_dir (const char *include_dir)
c906108c 17754{
27e0867f 17755 if (dwarf_line_debug >= 2)
fff8551c
PA
17756 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17757 include_dirs.size () + 1, include_dir);
27e0867f 17758
fff8551c 17759 include_dirs.push_back (include_dir);
debd256d 17760}
6e70227d 17761
fff8551c
PA
17762void
17763line_header::add_file_name (const char *name,
ecfb656c 17764 dir_index d_index,
fff8551c
PA
17765 unsigned int mod_time,
17766 unsigned int length)
debd256d 17767{
27e0867f
DE
17768 if (dwarf_line_debug >= 2)
17769 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17770 (unsigned) file_names.size () + 1, name);
27e0867f 17771
ecfb656c 17772 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17773}
6e70227d 17774
83769d0b 17775/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17776
17777static struct dwarf2_section_info *
17778get_debug_line_section (struct dwarf2_cu *cu)
17779{
17780 struct dwarf2_section_info *section;
17781
17782 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17783 DWO file. */
17784 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17785 section = &cu->dwo_unit->dwo_file->sections.line;
17786 else if (cu->per_cu->is_dwz)
17787 {
17788 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17789
17790 section = &dwz->line;
17791 }
17792 else
17793 section = &dwarf2_per_objfile->line;
17794
17795 return section;
17796}
17797
43988095
JK
17798/* Read directory or file name entry format, starting with byte of
17799 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17800 entries count and the entries themselves in the described entry
17801 format. */
17802
17803static void
17804read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17805 struct line_header *lh,
17806 const struct comp_unit_head *cu_header,
17807 void (*callback) (struct line_header *lh,
17808 const char *name,
ecfb656c 17809 dir_index d_index,
43988095
JK
17810 unsigned int mod_time,
17811 unsigned int length))
17812{
17813 gdb_byte format_count, formati;
17814 ULONGEST data_count, datai;
17815 const gdb_byte *buf = *bufp;
17816 const gdb_byte *format_header_data;
17817 int i;
17818 unsigned int bytes_read;
17819
17820 format_count = read_1_byte (abfd, buf);
17821 buf += 1;
17822 format_header_data = buf;
17823 for (formati = 0; formati < format_count; formati++)
17824 {
17825 read_unsigned_leb128 (abfd, buf, &bytes_read);
17826 buf += bytes_read;
17827 read_unsigned_leb128 (abfd, buf, &bytes_read);
17828 buf += bytes_read;
17829 }
17830
17831 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17832 buf += bytes_read;
17833 for (datai = 0; datai < data_count; datai++)
17834 {
17835 const gdb_byte *format = format_header_data;
17836 struct file_entry fe;
17837
43988095
JK
17838 for (formati = 0; formati < format_count; formati++)
17839 {
ecfb656c 17840 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17841 format += bytes_read;
43988095 17842
ecfb656c 17843 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17844 format += bytes_read;
ecfb656c
PA
17845
17846 gdb::optional<const char *> string;
17847 gdb::optional<unsigned int> uint;
17848
43988095
JK
17849 switch (form)
17850 {
17851 case DW_FORM_string:
ecfb656c 17852 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17853 buf += bytes_read;
17854 break;
17855
17856 case DW_FORM_line_strp:
ecfb656c
PA
17857 string.emplace (read_indirect_line_string (abfd, buf,
17858 cu_header,
17859 &bytes_read));
43988095
JK
17860 buf += bytes_read;
17861 break;
17862
17863 case DW_FORM_data1:
ecfb656c 17864 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17865 buf += 1;
17866 break;
17867
17868 case DW_FORM_data2:
ecfb656c 17869 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17870 buf += 2;
17871 break;
17872
17873 case DW_FORM_data4:
ecfb656c 17874 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17875 buf += 4;
17876 break;
17877
17878 case DW_FORM_data8:
ecfb656c 17879 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17880 buf += 8;
17881 break;
17882
17883 case DW_FORM_udata:
ecfb656c 17884 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17885 buf += bytes_read;
17886 break;
17887
17888 case DW_FORM_block:
17889 /* It is valid only for DW_LNCT_timestamp which is ignored by
17890 current GDB. */
17891 break;
17892 }
ecfb656c
PA
17893
17894 switch (content_type)
17895 {
17896 case DW_LNCT_path:
17897 if (string.has_value ())
17898 fe.name = *string;
17899 break;
17900 case DW_LNCT_directory_index:
17901 if (uint.has_value ())
17902 fe.d_index = (dir_index) *uint;
17903 break;
17904 case DW_LNCT_timestamp:
17905 if (uint.has_value ())
17906 fe.mod_time = *uint;
17907 break;
17908 case DW_LNCT_size:
17909 if (uint.has_value ())
17910 fe.length = *uint;
17911 break;
17912 case DW_LNCT_MD5:
17913 break;
17914 default:
17915 complaint (&symfile_complaints,
17916 _("Unknown format content type %s"),
17917 pulongest (content_type));
17918 }
43988095
JK
17919 }
17920
ecfb656c 17921 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17922 }
17923
17924 *bufp = buf;
17925}
17926
debd256d 17927/* Read the statement program header starting at OFFSET in
3019eac3 17928 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17929 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17930 Returns NULL if there is a problem reading the header, e.g., if it
17931 has a version we don't understand.
debd256d
JB
17932
17933 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17934 the returned object point into the dwarf line section buffer,
17935 and must not be freed. */
ae2de4f8 17936
fff8551c 17937static line_header_up
9c541725 17938dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17939{
d521ce57 17940 const gdb_byte *line_ptr;
c764a876 17941 unsigned int bytes_read, offset_size;
debd256d 17942 int i;
d521ce57 17943 const char *cur_dir, *cur_file;
3019eac3
DE
17944 struct dwarf2_section_info *section;
17945 bfd *abfd;
17946
36586728 17947 section = get_debug_line_section (cu);
3019eac3
DE
17948 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17949 if (section->buffer == NULL)
debd256d 17950 {
3019eac3
DE
17951 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17952 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17953 else
17954 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17955 return 0;
17956 }
17957
fceca515
DE
17958 /* We can't do this until we know the section is non-empty.
17959 Only then do we know we have such a section. */
a32a8923 17960 abfd = get_section_bfd_owner (section);
fceca515 17961
a738430d
MK
17962 /* Make sure that at least there's room for the total_length field.
17963 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17964 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17965 {
4d3c2250 17966 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17967 return 0;
17968 }
17969
fff8551c 17970 line_header_up lh (new line_header ());
debd256d 17971
9c541725 17972 lh->sect_off = sect_off;
527f3840
JK
17973 lh->offset_in_dwz = cu->per_cu->is_dwz;
17974
9c541725 17975 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 17976
a738430d 17977 /* Read in the header. */
6e70227d 17978 lh->total_length =
c764a876
DE
17979 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17980 &bytes_read, &offset_size);
debd256d 17981 line_ptr += bytes_read;
3019eac3 17982 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17983 {
4d3c2250 17984 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17985 return 0;
17986 }
17987 lh->statement_program_end = line_ptr + lh->total_length;
17988 lh->version = read_2_bytes (abfd, line_ptr);
17989 line_ptr += 2;
43988095 17990 if (lh->version > 5)
cd366ee8
DE
17991 {
17992 /* This is a version we don't understand. The format could have
17993 changed in ways we don't handle properly so just punt. */
17994 complaint (&symfile_complaints,
17995 _("unsupported version in .debug_line section"));
17996 return NULL;
17997 }
43988095
JK
17998 if (lh->version >= 5)
17999 {
18000 gdb_byte segment_selector_size;
18001
18002 /* Skip address size. */
18003 read_1_byte (abfd, line_ptr);
18004 line_ptr += 1;
18005
18006 segment_selector_size = read_1_byte (abfd, line_ptr);
18007 line_ptr += 1;
18008 if (segment_selector_size != 0)
18009 {
18010 complaint (&symfile_complaints,
18011 _("unsupported segment selector size %u "
18012 "in .debug_line section"),
18013 segment_selector_size);
18014 return NULL;
18015 }
18016 }
c764a876
DE
18017 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18018 line_ptr += offset_size;
debd256d
JB
18019 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18020 line_ptr += 1;
2dc7f7b3
TT
18021 if (lh->version >= 4)
18022 {
18023 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18024 line_ptr += 1;
18025 }
18026 else
18027 lh->maximum_ops_per_instruction = 1;
18028
18029 if (lh->maximum_ops_per_instruction == 0)
18030 {
18031 lh->maximum_ops_per_instruction = 1;
18032 complaint (&symfile_complaints,
3e43a32a
MS
18033 _("invalid maximum_ops_per_instruction "
18034 "in `.debug_line' section"));
2dc7f7b3
TT
18035 }
18036
debd256d
JB
18037 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18038 line_ptr += 1;
18039 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18040 line_ptr += 1;
18041 lh->line_range = read_1_byte (abfd, line_ptr);
18042 line_ptr += 1;
18043 lh->opcode_base = read_1_byte (abfd, line_ptr);
18044 line_ptr += 1;
fff8551c 18045 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18046
18047 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18048 for (i = 1; i < lh->opcode_base; ++i)
18049 {
18050 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18051 line_ptr += 1;
18052 }
18053
43988095 18054 if (lh->version >= 5)
debd256d 18055 {
43988095 18056 /* Read directory table. */
fff8551c
PA
18057 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18058 [] (struct line_header *lh, const char *name,
ecfb656c 18059 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18060 unsigned int length)
18061 {
18062 lh->add_include_dir (name);
18063 });
debd256d 18064
43988095 18065 /* Read file name table. */
fff8551c
PA
18066 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18067 [] (struct line_header *lh, const char *name,
ecfb656c 18068 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18069 unsigned int length)
18070 {
ecfb656c 18071 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18072 });
43988095
JK
18073 }
18074 else
debd256d 18075 {
43988095
JK
18076 /* Read directory table. */
18077 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18078 {
18079 line_ptr += bytes_read;
fff8551c 18080 lh->add_include_dir (cur_dir);
43988095 18081 }
debd256d
JB
18082 line_ptr += bytes_read;
18083
43988095
JK
18084 /* Read file name table. */
18085 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18086 {
ecfb656c
PA
18087 unsigned int mod_time, length;
18088 dir_index d_index;
43988095
JK
18089
18090 line_ptr += bytes_read;
ecfb656c 18091 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18092 line_ptr += bytes_read;
18093 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18094 line_ptr += bytes_read;
18095 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18096 line_ptr += bytes_read;
18097
ecfb656c 18098 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18099 }
18100 line_ptr += bytes_read;
debd256d 18101 }
6e70227d 18102 lh->statement_program_start = line_ptr;
debd256d 18103
3019eac3 18104 if (line_ptr > (section->buffer + section->size))
4d3c2250 18105 complaint (&symfile_complaints,
3e43a32a
MS
18106 _("line number info header doesn't "
18107 "fit in `.debug_line' section"));
debd256d 18108
debd256d
JB
18109 return lh;
18110}
c906108c 18111
c6da4cef
DE
18112/* Subroutine of dwarf_decode_lines to simplify it.
18113 Return the file name of the psymtab for included file FILE_INDEX
18114 in line header LH of PST.
18115 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18116 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18117 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18118
18119 The function creates dangling cleanup registration. */
c6da4cef 18120
d521ce57 18121static const char *
c6da4cef
DE
18122psymtab_include_file_name (const struct line_header *lh, int file_index,
18123 const struct partial_symtab *pst,
18124 const char *comp_dir)
18125{
8c43009f 18126 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18127 const char *include_name = fe.name;
18128 const char *include_name_to_compare = include_name;
72b9f47f
TT
18129 const char *pst_filename;
18130 char *copied_name = NULL;
c6da4cef
DE
18131 int file_is_pst;
18132
8c43009f 18133 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18134
18135 if (!IS_ABSOLUTE_PATH (include_name)
18136 && (dir_name != NULL || comp_dir != NULL))
18137 {
18138 /* Avoid creating a duplicate psymtab for PST.
18139 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18140 Before we do the comparison, however, we need to account
18141 for DIR_NAME and COMP_DIR.
18142 First prepend dir_name (if non-NULL). If we still don't
18143 have an absolute path prepend comp_dir (if non-NULL).
18144 However, the directory we record in the include-file's
18145 psymtab does not contain COMP_DIR (to match the
18146 corresponding symtab(s)).
18147
18148 Example:
18149
18150 bash$ cd /tmp
18151 bash$ gcc -g ./hello.c
18152 include_name = "hello.c"
18153 dir_name = "."
18154 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18155 DW_AT_name = "./hello.c"
18156
18157 */
c6da4cef
DE
18158
18159 if (dir_name != NULL)
18160 {
d521ce57
TT
18161 char *tem = concat (dir_name, SLASH_STRING,
18162 include_name, (char *)NULL);
18163
18164 make_cleanup (xfree, tem);
18165 include_name = tem;
c6da4cef 18166 include_name_to_compare = include_name;
c6da4cef
DE
18167 }
18168 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18169 {
d521ce57
TT
18170 char *tem = concat (comp_dir, SLASH_STRING,
18171 include_name, (char *)NULL);
18172
18173 make_cleanup (xfree, tem);
18174 include_name_to_compare = tem;
c6da4cef
DE
18175 }
18176 }
18177
18178 pst_filename = pst->filename;
18179 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18180 {
72b9f47f
TT
18181 copied_name = concat (pst->dirname, SLASH_STRING,
18182 pst_filename, (char *)NULL);
18183 pst_filename = copied_name;
c6da4cef
DE
18184 }
18185
1e3fad37 18186 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18187
72b9f47f
TT
18188 if (copied_name != NULL)
18189 xfree (copied_name);
c6da4cef
DE
18190
18191 if (file_is_pst)
18192 return NULL;
18193 return include_name;
18194}
18195
d9b3de22
DE
18196/* State machine to track the state of the line number program. */
18197
6f77053d 18198class lnp_state_machine
d9b3de22 18199{
6f77053d
PA
18200public:
18201 /* Initialize a machine state for the start of a line number
18202 program. */
18203 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18204
8c43009f
PA
18205 file_entry *current_file ()
18206 {
18207 /* lh->file_names is 0-based, but the file name numbers in the
18208 statement program are 1-based. */
6f77053d
PA
18209 return m_line_header->file_name_at (m_file);
18210 }
18211
18212 /* Record the line in the state machine. END_SEQUENCE is true if
18213 we're processing the end of a sequence. */
18214 void record_line (bool end_sequence);
18215
18216 /* Check address and if invalid nop-out the rest of the lines in this
18217 sequence. */
18218 void check_line_address (struct dwarf2_cu *cu,
18219 const gdb_byte *line_ptr,
18220 CORE_ADDR lowpc, CORE_ADDR address);
18221
18222 void handle_set_discriminator (unsigned int discriminator)
18223 {
18224 m_discriminator = discriminator;
18225 m_line_has_non_zero_discriminator |= discriminator != 0;
18226 }
18227
18228 /* Handle DW_LNE_set_address. */
18229 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18230 {
18231 m_op_index = 0;
18232 address += baseaddr;
18233 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18234 }
18235
18236 /* Handle DW_LNS_advance_pc. */
18237 void handle_advance_pc (CORE_ADDR adjust);
18238
18239 /* Handle a special opcode. */
18240 void handle_special_opcode (unsigned char op_code);
18241
18242 /* Handle DW_LNS_advance_line. */
18243 void handle_advance_line (int line_delta)
18244 {
18245 advance_line (line_delta);
18246 }
18247
18248 /* Handle DW_LNS_set_file. */
18249 void handle_set_file (file_name_index file);
18250
18251 /* Handle DW_LNS_negate_stmt. */
18252 void handle_negate_stmt ()
18253 {
18254 m_is_stmt = !m_is_stmt;
18255 }
18256
18257 /* Handle DW_LNS_const_add_pc. */
18258 void handle_const_add_pc ();
18259
18260 /* Handle DW_LNS_fixed_advance_pc. */
18261 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18262 {
18263 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18264 m_op_index = 0;
18265 }
18266
18267 /* Handle DW_LNS_copy. */
18268 void handle_copy ()
18269 {
18270 record_line (false);
18271 m_discriminator = 0;
18272 }
18273
18274 /* Handle DW_LNE_end_sequence. */
18275 void handle_end_sequence ()
18276 {
18277 m_record_line_callback = ::record_line;
18278 }
18279
18280private:
18281 /* Advance the line by LINE_DELTA. */
18282 void advance_line (int line_delta)
18283 {
18284 m_line += line_delta;
18285
18286 if (line_delta != 0)
18287 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18288 }
18289
6f77053d
PA
18290 gdbarch *m_gdbarch;
18291
18292 /* True if we're recording lines.
18293 Otherwise we're building partial symtabs and are just interested in
18294 finding include files mentioned by the line number program. */
18295 bool m_record_lines_p;
18296
8c43009f 18297 /* The line number header. */
6f77053d 18298 line_header *m_line_header;
8c43009f 18299
6f77053d
PA
18300 /* These are part of the standard DWARF line number state machine,
18301 and initialized according to the DWARF spec. */
d9b3de22 18302
6f77053d 18303 unsigned char m_op_index = 0;
8c43009f 18304 /* The line table index (1-based) of the current file. */
6f77053d
PA
18305 file_name_index m_file = (file_name_index) 1;
18306 unsigned int m_line = 1;
18307
18308 /* These are initialized in the constructor. */
18309
18310 CORE_ADDR m_address;
18311 bool m_is_stmt;
18312 unsigned int m_discriminator;
d9b3de22
DE
18313
18314 /* Additional bits of state we need to track. */
18315
18316 /* The last file that we called dwarf2_start_subfile for.
18317 This is only used for TLLs. */
6f77053d 18318 unsigned int m_last_file = 0;
d9b3de22 18319 /* The last file a line number was recorded for. */
6f77053d 18320 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18321
18322 /* The function to call to record a line. */
6f77053d 18323 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18324
18325 /* The last line number that was recorded, used to coalesce
18326 consecutive entries for the same line. This can happen, for
18327 example, when discriminators are present. PR 17276. */
6f77053d
PA
18328 unsigned int m_last_line = 0;
18329 bool m_line_has_non_zero_discriminator = false;
8c43009f 18330};
d9b3de22 18331
6f77053d
PA
18332void
18333lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18334{
18335 CORE_ADDR addr_adj = (((m_op_index + adjust)
18336 / m_line_header->maximum_ops_per_instruction)
18337 * m_line_header->minimum_instruction_length);
18338 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18339 m_op_index = ((m_op_index + adjust)
18340 % m_line_header->maximum_ops_per_instruction);
18341}
d9b3de22 18342
6f77053d
PA
18343void
18344lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18345{
6f77053d
PA
18346 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18347 CORE_ADDR addr_adj = (((m_op_index
18348 + (adj_opcode / m_line_header->line_range))
18349 / m_line_header->maximum_ops_per_instruction)
18350 * m_line_header->minimum_instruction_length);
18351 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18352 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18353 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18354
6f77053d
PA
18355 int line_delta = (m_line_header->line_base
18356 + (adj_opcode % m_line_header->line_range));
18357 advance_line (line_delta);
18358 record_line (false);
18359 m_discriminator = 0;
18360}
d9b3de22 18361
6f77053d
PA
18362void
18363lnp_state_machine::handle_set_file (file_name_index file)
18364{
18365 m_file = file;
18366
18367 const file_entry *fe = current_file ();
18368 if (fe == NULL)
18369 dwarf2_debug_line_missing_file_complaint ();
18370 else if (m_record_lines_p)
18371 {
18372 const char *dir = fe->include_dir (m_line_header);
18373
18374 m_last_subfile = current_subfile;
18375 m_line_has_non_zero_discriminator = m_discriminator != 0;
18376 dwarf2_start_subfile (fe->name, dir);
18377 }
18378}
18379
18380void
18381lnp_state_machine::handle_const_add_pc ()
18382{
18383 CORE_ADDR adjust
18384 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18385
18386 CORE_ADDR addr_adj
18387 = (((m_op_index + adjust)
18388 / m_line_header->maximum_ops_per_instruction)
18389 * m_line_header->minimum_instruction_length);
18390
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
c91513d8
PP
18396/* Ignore this record_line request. */
18397
18398static void
18399noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18400{
18401 return;
18402}
18403
a05a36a5
DE
18404/* Return non-zero if we should add LINE to the line number table.
18405 LINE is the line to add, LAST_LINE is the last line that was added,
18406 LAST_SUBFILE is the subfile for LAST_LINE.
18407 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18408 had a non-zero discriminator.
18409
18410 We have to be careful in the presence of discriminators.
18411 E.g., for this line:
18412
18413 for (i = 0; i < 100000; i++);
18414
18415 clang can emit four line number entries for that one line,
18416 each with a different discriminator.
18417 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18418
18419 However, we want gdb to coalesce all four entries into one.
18420 Otherwise the user could stepi into the middle of the line and
18421 gdb would get confused about whether the pc really was in the
18422 middle of the line.
18423
18424 Things are further complicated by the fact that two consecutive
18425 line number entries for the same line is a heuristic used by gcc
18426 to denote the end of the prologue. So we can't just discard duplicate
18427 entries, we have to be selective about it. The heuristic we use is
18428 that we only collapse consecutive entries for the same line if at least
18429 one of those entries has a non-zero discriminator. PR 17276.
18430
18431 Note: Addresses in the line number state machine can never go backwards
18432 within one sequence, thus this coalescing is ok. */
18433
18434static int
18435dwarf_record_line_p (unsigned int line, unsigned int last_line,
18436 int line_has_non_zero_discriminator,
18437 struct subfile *last_subfile)
18438{
18439 if (current_subfile != last_subfile)
18440 return 1;
18441 if (line != last_line)
18442 return 1;
18443 /* Same line for the same file that we've seen already.
18444 As a last check, for pr 17276, only record the line if the line
18445 has never had a non-zero discriminator. */
18446 if (!line_has_non_zero_discriminator)
18447 return 1;
18448 return 0;
18449}
18450
252a6764
DE
18451/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18452 in the line table of subfile SUBFILE. */
18453
18454static void
d9b3de22
DE
18455dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18456 unsigned int line, CORE_ADDR address,
18457 record_line_ftype p_record_line)
252a6764
DE
18458{
18459 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18460
27e0867f
DE
18461 if (dwarf_line_debug)
18462 {
18463 fprintf_unfiltered (gdb_stdlog,
18464 "Recording line %u, file %s, address %s\n",
18465 line, lbasename (subfile->name),
18466 paddress (gdbarch, address));
18467 }
18468
d5962de5 18469 (*p_record_line) (subfile, line, addr);
252a6764
DE
18470}
18471
18472/* Subroutine of dwarf_decode_lines_1 to simplify it.
18473 Mark the end of a set of line number records.
d9b3de22 18474 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18475 If SUBFILE is NULL the request is ignored. */
18476
18477static void
18478dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18479 CORE_ADDR address, record_line_ftype p_record_line)
18480{
27e0867f
DE
18481 if (subfile == NULL)
18482 return;
18483
18484 if (dwarf_line_debug)
18485 {
18486 fprintf_unfiltered (gdb_stdlog,
18487 "Finishing current line, file %s, address %s\n",
18488 lbasename (subfile->name),
18489 paddress (gdbarch, address));
18490 }
18491
d9b3de22
DE
18492 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18493}
18494
6f77053d
PA
18495void
18496lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18497{
d9b3de22
DE
18498 if (dwarf_line_debug)
18499 {
18500 fprintf_unfiltered (gdb_stdlog,
18501 "Processing actual line %u: file %u,"
18502 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18503 m_line, to_underlying (m_file),
18504 paddress (m_gdbarch, m_address),
18505 m_is_stmt, m_discriminator);
d9b3de22
DE
18506 }
18507
6f77053d 18508 file_entry *fe = current_file ();
8c43009f
PA
18509
18510 if (fe == NULL)
d9b3de22
DE
18511 dwarf2_debug_line_missing_file_complaint ();
18512 /* For now we ignore lines not starting on an instruction boundary.
18513 But not when processing end_sequence for compatibility with the
18514 previous version of the code. */
6f77053d 18515 else if (m_op_index == 0 || end_sequence)
d9b3de22 18516 {
8c43009f 18517 fe->included_p = 1;
6f77053d 18518 if (m_record_lines_p && m_is_stmt)
d9b3de22 18519 {
6f77053d 18520 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18521 {
6f77053d
PA
18522 dwarf_finish_line (m_gdbarch, m_last_subfile,
18523 m_address, m_record_line_callback);
d9b3de22
DE
18524 }
18525
18526 if (!end_sequence)
18527 {
6f77053d
PA
18528 if (dwarf_record_line_p (m_line, m_last_line,
18529 m_line_has_non_zero_discriminator,
18530 m_last_subfile))
d9b3de22 18531 {
6f77053d
PA
18532 dwarf_record_line_1 (m_gdbarch, current_subfile,
18533 m_line, m_address,
18534 m_record_line_callback);
d9b3de22 18535 }
6f77053d
PA
18536 m_last_subfile = current_subfile;
18537 m_last_line = m_line;
d9b3de22
DE
18538 }
18539 }
18540 }
18541}
18542
6f77053d
PA
18543lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18544 bool record_lines_p)
d9b3de22 18545{
6f77053d
PA
18546 m_gdbarch = arch;
18547 m_record_lines_p = record_lines_p;
18548 m_line_header = lh;
d9b3de22 18549
6f77053d 18550 m_record_line_callback = ::record_line;
d9b3de22 18551
d9b3de22
DE
18552 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18553 was a line entry for it so that the backend has a chance to adjust it
18554 and also record it in case it needs it. This is currently used by MIPS
18555 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18556 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18557 m_is_stmt = lh->default_is_stmt;
18558 m_discriminator = 0;
252a6764
DE
18559}
18560
6f77053d
PA
18561void
18562lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18563 const gdb_byte *line_ptr,
18564 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18565{
18566 /* If address < lowpc then it's not a usable value, it's outside the
18567 pc range of the CU. However, we restrict the test to only address
18568 values of zero to preserve GDB's previous behaviour which is to
18569 handle the specific case of a function being GC'd by the linker. */
18570
18571 if (address == 0 && address < lowpc)
18572 {
18573 /* This line table is for a function which has been
18574 GCd by the linker. Ignore it. PR gdb/12528 */
18575
18576 struct objfile *objfile = cu->objfile;
18577 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18578
18579 complaint (&symfile_complaints,
18580 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18581 line_offset, objfile_name (objfile));
6f77053d
PA
18582 m_record_line_callback = noop_record_line;
18583 /* Note: record_line_callback is left as noop_record_line until
18584 we see DW_LNE_end_sequence. */
924c2928
DE
18585 }
18586}
18587
f3f5162e 18588/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18589 Process the line number information in LH.
18590 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18591 program in order to set included_p for every referenced header. */
debd256d 18592
c906108c 18593static void
43f3e411
DE
18594dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18595 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18596{
d521ce57
TT
18597 const gdb_byte *line_ptr, *extended_end;
18598 const gdb_byte *line_end;
a8c50c1f 18599 unsigned int bytes_read, extended_len;
699ca60a 18600 unsigned char op_code, extended_op;
e142c38c
DJ
18601 CORE_ADDR baseaddr;
18602 struct objfile *objfile = cu->objfile;
f3f5162e 18603 bfd *abfd = objfile->obfd;
fbf65064 18604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18605 /* True if we're recording line info (as opposed to building partial
18606 symtabs and just interested in finding include files mentioned by
18607 the line number program). */
18608 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18609
18610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18611
debd256d
JB
18612 line_ptr = lh->statement_program_start;
18613 line_end = lh->statement_program_end;
c906108c
SS
18614
18615 /* Read the statement sequences until there's nothing left. */
18616 while (line_ptr < line_end)
18617 {
6f77053d
PA
18618 /* The DWARF line number program state machine. Reset the state
18619 machine at the start of each sequence. */
18620 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18621 bool end_sequence = false;
d9b3de22 18622
8c43009f 18623 if (record_lines_p)
c906108c 18624 {
8c43009f
PA
18625 /* Start a subfile for the current file of the state
18626 machine. */
18627 const file_entry *fe = state_machine.current_file ();
18628
18629 if (fe != NULL)
18630 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18631 }
18632
a738430d 18633 /* Decode the table. */
d9b3de22 18634 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18635 {
18636 op_code = read_1_byte (abfd, line_ptr);
18637 line_ptr += 1;
9aa1fe7e 18638
debd256d 18639 if (op_code >= lh->opcode_base)
6e70227d 18640 {
8e07a239 18641 /* Special opcode. */
6f77053d 18642 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18643 }
18644 else switch (op_code)
c906108c
SS
18645 {
18646 case DW_LNS_extended_op:
3e43a32a
MS
18647 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18648 &bytes_read);
473b7be6 18649 line_ptr += bytes_read;
a8c50c1f 18650 extended_end = line_ptr + extended_len;
c906108c
SS
18651 extended_op = read_1_byte (abfd, line_ptr);
18652 line_ptr += 1;
18653 switch (extended_op)
18654 {
18655 case DW_LNE_end_sequence:
6f77053d
PA
18656 state_machine.handle_end_sequence ();
18657 end_sequence = true;
c906108c
SS
18658 break;
18659 case DW_LNE_set_address:
d9b3de22
DE
18660 {
18661 CORE_ADDR address
18662 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18663 line_ptr += bytes_read;
6f77053d
PA
18664
18665 state_machine.check_line_address (cu, line_ptr,
18666 lowpc, address);
18667 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18668 }
c906108c
SS
18669 break;
18670 case DW_LNE_define_file:
debd256d 18671 {
d521ce57 18672 const char *cur_file;
ecfb656c
PA
18673 unsigned int mod_time, length;
18674 dir_index dindex;
6e70227d 18675
3e43a32a
MS
18676 cur_file = read_direct_string (abfd, line_ptr,
18677 &bytes_read);
debd256d 18678 line_ptr += bytes_read;
ecfb656c 18679 dindex = (dir_index)
debd256d
JB
18680 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18681 line_ptr += bytes_read;
18682 mod_time =
18683 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18684 line_ptr += bytes_read;
18685 length =
18686 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18687 line_ptr += bytes_read;
ecfb656c 18688 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18689 }
c906108c 18690 break;
d0c6ba3d 18691 case DW_LNE_set_discriminator:
6f77053d
PA
18692 {
18693 /* The discriminator is not interesting to the
18694 debugger; just ignore it. We still need to
18695 check its value though:
18696 if there are consecutive entries for the same
18697 (non-prologue) line we want to coalesce them.
18698 PR 17276. */
18699 unsigned int discr
18700 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18701 line_ptr += bytes_read;
18702
18703 state_machine.handle_set_discriminator (discr);
18704 }
d0c6ba3d 18705 break;
c906108c 18706 default:
4d3c2250 18707 complaint (&symfile_complaints,
e2e0b3e5 18708 _("mangled .debug_line section"));
debd256d 18709 return;
c906108c 18710 }
a8c50c1f
DJ
18711 /* Make sure that we parsed the extended op correctly. If e.g.
18712 we expected a different address size than the producer used,
18713 we may have read the wrong number of bytes. */
18714 if (line_ptr != extended_end)
18715 {
18716 complaint (&symfile_complaints,
18717 _("mangled .debug_line section"));
18718 return;
18719 }
c906108c
SS
18720 break;
18721 case DW_LNS_copy:
6f77053d 18722 state_machine.handle_copy ();
c906108c
SS
18723 break;
18724 case DW_LNS_advance_pc:
2dc7f7b3
TT
18725 {
18726 CORE_ADDR adjust
18727 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18728 line_ptr += bytes_read;
6f77053d
PA
18729
18730 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18731 }
c906108c
SS
18732 break;
18733 case DW_LNS_advance_line:
a05a36a5
DE
18734 {
18735 int line_delta
18736 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18737 line_ptr += bytes_read;
6f77053d
PA
18738
18739 state_machine.handle_advance_line (line_delta);
a05a36a5 18740 }
c906108c
SS
18741 break;
18742 case DW_LNS_set_file:
d9b3de22 18743 {
6f77053d 18744 file_name_index file
ecfb656c
PA
18745 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18746 &bytes_read);
d9b3de22 18747 line_ptr += bytes_read;
8c43009f 18748
6f77053d 18749 state_machine.handle_set_file (file);
d9b3de22 18750 }
c906108c
SS
18751 break;
18752 case DW_LNS_set_column:
0ad93d4f 18753 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18754 line_ptr += bytes_read;
18755 break;
18756 case DW_LNS_negate_stmt:
6f77053d 18757 state_machine.handle_negate_stmt ();
c906108c
SS
18758 break;
18759 case DW_LNS_set_basic_block:
c906108c 18760 break;
c2c6d25f
JM
18761 /* Add to the address register of the state machine the
18762 address increment value corresponding to special opcode
a738430d
MK
18763 255. I.e., this value is scaled by the minimum
18764 instruction length since special opcode 255 would have
b021a221 18765 scaled the increment. */
c906108c 18766 case DW_LNS_const_add_pc:
6f77053d 18767 state_machine.handle_const_add_pc ();
c906108c
SS
18768 break;
18769 case DW_LNS_fixed_advance_pc:
3e29f34a 18770 {
6f77053d 18771 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18772 line_ptr += 2;
6f77053d
PA
18773
18774 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18775 }
c906108c 18776 break;
9aa1fe7e 18777 default:
a738430d
MK
18778 {
18779 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18780 int i;
a738430d 18781
debd256d 18782 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18783 {
18784 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18785 line_ptr += bytes_read;
18786 }
18787 }
c906108c
SS
18788 }
18789 }
d9b3de22
DE
18790
18791 if (!end_sequence)
18792 dwarf2_debug_line_missing_end_sequence_complaint ();
18793
18794 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18795 in which case we still finish recording the last line). */
6f77053d 18796 state_machine.record_line (true);
c906108c 18797 }
f3f5162e
DE
18798}
18799
18800/* Decode the Line Number Program (LNP) for the given line_header
18801 structure and CU. The actual information extracted and the type
18802 of structures created from the LNP depends on the value of PST.
18803
18804 1. If PST is NULL, then this procedure uses the data from the program
18805 to create all necessary symbol tables, and their linetables.
18806
18807 2. If PST is not NULL, this procedure reads the program to determine
18808 the list of files included by the unit represented by PST, and
18809 builds all the associated partial symbol tables.
18810
18811 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18812 It is used for relative paths in the line table.
18813 NOTE: When processing partial symtabs (pst != NULL),
18814 comp_dir == pst->dirname.
18815
18816 NOTE: It is important that psymtabs have the same file name (via strcmp)
18817 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18818 symtab we don't use it in the name of the psymtabs we create.
18819 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18820 A good testcase for this is mb-inline.exp.
18821
527f3840
JK
18822 LOWPC is the lowest address in CU (or 0 if not known).
18823
18824 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18825 for its PC<->lines mapping information. Otherwise only the filename
18826 table is read in. */
f3f5162e
DE
18827
18828static void
18829dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18830 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18831 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18832{
18833 struct objfile *objfile = cu->objfile;
18834 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18835
527f3840
JK
18836 if (decode_mapping)
18837 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18838
18839 if (decode_for_pst_p)
18840 {
18841 int file_index;
18842
18843 /* Now that we're done scanning the Line Header Program, we can
18844 create the psymtab of each included file. */
fff8551c 18845 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18846 if (lh->file_names[file_index].included_p == 1)
18847 {
d521ce57 18848 const char *include_name =
c6da4cef
DE
18849 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18850 if (include_name != NULL)
aaa75496
JB
18851 dwarf2_create_include_psymtab (include_name, pst, objfile);
18852 }
18853 }
cb1df416
DJ
18854 else
18855 {
18856 /* Make sure a symtab is created for every file, even files
18857 which contain only variables (i.e. no code with associated
18858 line numbers). */
43f3e411 18859 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18860 int i;
cb1df416 18861
fff8551c 18862 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18863 {
8c43009f 18864 file_entry &fe = lh->file_names[i];
9a619af0 18865
8c43009f 18866 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18867
cb1df416 18868 if (current_subfile->symtab == NULL)
43f3e411
DE
18869 {
18870 current_subfile->symtab
18871 = allocate_symtab (cust, current_subfile->name);
18872 }
8c43009f 18873 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18874 }
18875 }
c906108c
SS
18876}
18877
18878/* Start a subfile for DWARF. FILENAME is the name of the file and
18879 DIRNAME the name of the source directory which contains FILENAME
4d663531 18880 or NULL if not known.
c906108c
SS
18881 This routine tries to keep line numbers from identical absolute and
18882 relative file names in a common subfile.
18883
18884 Using the `list' example from the GDB testsuite, which resides in
18885 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18886 of /srcdir/list0.c yields the following debugging information for list0.c:
18887
c5aa993b 18888 DW_AT_name: /srcdir/list0.c
4d663531 18889 DW_AT_comp_dir: /compdir
357e46e7 18890 files.files[0].name: list0.h
c5aa993b 18891 files.files[0].dir: /srcdir
357e46e7 18892 files.files[1].name: list0.c
c5aa993b 18893 files.files[1].dir: /srcdir
c906108c
SS
18894
18895 The line number information for list0.c has to end up in a single
4f1520fb
FR
18896 subfile, so that `break /srcdir/list0.c:1' works as expected.
18897 start_subfile will ensure that this happens provided that we pass the
18898 concatenation of files.files[1].dir and files.files[1].name as the
18899 subfile's name. */
c906108c
SS
18900
18901static void
4d663531 18902dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18903{
d521ce57 18904 char *copy = NULL;
4f1520fb 18905
4d663531 18906 /* In order not to lose the line information directory,
4f1520fb
FR
18907 we concatenate it to the filename when it makes sense.
18908 Note that the Dwarf3 standard says (speaking of filenames in line
18909 information): ``The directory index is ignored for file names
18910 that represent full path names''. Thus ignoring dirname in the
18911 `else' branch below isn't an issue. */
c906108c 18912
d5166ae1 18913 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18914 {
18915 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18916 filename = copy;
18917 }
c906108c 18918
4d663531 18919 start_subfile (filename);
4f1520fb 18920
d521ce57
TT
18921 if (copy != NULL)
18922 xfree (copy);
c906108c
SS
18923}
18924
f4dc4d17
DE
18925/* Start a symtab for DWARF.
18926 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18927
43f3e411 18928static struct compunit_symtab *
f4dc4d17 18929dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18930 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18931{
43f3e411
DE
18932 struct compunit_symtab *cust
18933 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18934
f4dc4d17
DE
18935 record_debugformat ("DWARF 2");
18936 record_producer (cu->producer);
18937
18938 /* We assume that we're processing GCC output. */
18939 processing_gcc_compilation = 2;
18940
4d4ec4e5 18941 cu->processing_has_namespace_info = 0;
43f3e411
DE
18942
18943 return cust;
f4dc4d17
DE
18944}
18945
4c2df51b
DJ
18946static void
18947var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18948 struct dwarf2_cu *cu)
4c2df51b 18949{
e7c27a73
DJ
18950 struct objfile *objfile = cu->objfile;
18951 struct comp_unit_head *cu_header = &cu->header;
18952
4c2df51b
DJ
18953 /* NOTE drow/2003-01-30: There used to be a comment and some special
18954 code here to turn a symbol with DW_AT_external and a
18955 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18956 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18957 with some versions of binutils) where shared libraries could have
18958 relocations against symbols in their debug information - the
18959 minimal symbol would have the right address, but the debug info
18960 would not. It's no longer necessary, because we will explicitly
18961 apply relocations when we read in the debug information now. */
18962
18963 /* A DW_AT_location attribute with no contents indicates that a
18964 variable has been optimized away. */
18965 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18966 {
f1e6e072 18967 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18968 return;
18969 }
18970
18971 /* Handle one degenerate form of location expression specially, to
18972 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18973 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18974 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18975
18976 if (attr_form_is_block (attr)
3019eac3
DE
18977 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18978 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18979 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18980 && (DW_BLOCK (attr)->size
18981 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18982 {
891d2f0b 18983 unsigned int dummy;
4c2df51b 18984
3019eac3
DE
18985 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18986 SYMBOL_VALUE_ADDRESS (sym) =
18987 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18988 else
18989 SYMBOL_VALUE_ADDRESS (sym) =
18990 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18991 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18992 fixup_symbol_section (sym, objfile);
18993 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18994 SYMBOL_SECTION (sym));
4c2df51b
DJ
18995 return;
18996 }
18997
18998 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18999 expression evaluator, and use LOC_COMPUTED only when necessary
19000 (i.e. when the value of a register or memory location is
19001 referenced, or a thread-local block, etc.). Then again, it might
19002 not be worthwhile. I'm assuming that it isn't unless performance
19003 or memory numbers show me otherwise. */
19004
f1e6e072 19005 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19006
f1e6e072 19007 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19008 cu->has_loclist = 1;
4c2df51b
DJ
19009}
19010
c906108c
SS
19011/* Given a pointer to a DWARF information entry, figure out if we need
19012 to make a symbol table entry for it, and if so, create a new entry
19013 and return a pointer to it.
19014 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19015 used the passed type.
19016 If SPACE is not NULL, use it to hold the new symbol. If it is
19017 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19018
19019static struct symbol *
34eaf542
TT
19020new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19021 struct symbol *space)
c906108c 19022{
e7c27a73 19023 struct objfile *objfile = cu->objfile;
3e29f34a 19024 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19025 struct symbol *sym = NULL;
15d034d0 19026 const char *name;
c906108c
SS
19027 struct attribute *attr = NULL;
19028 struct attribute *attr2 = NULL;
e142c38c 19029 CORE_ADDR baseaddr;
e37fd15a
SW
19030 struct pending **list_to_add = NULL;
19031
edb3359d 19032 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19033
19034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19035
94af9270 19036 name = dwarf2_name (die, cu);
c906108c
SS
19037 if (name)
19038 {
94af9270 19039 const char *linkagename;
34eaf542 19040 int suppress_add = 0;
94af9270 19041
34eaf542
TT
19042 if (space)
19043 sym = space;
19044 else
e623cf5d 19045 sym = allocate_symbol (objfile);
c906108c 19046 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19047
19048 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19049 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19050 linkagename = dwarf2_physname (name, die, cu);
19051 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19052
f55ee35c
JK
19053 /* Fortran does not have mangling standard and the mangling does differ
19054 between gfortran, iFort etc. */
19055 if (cu->language == language_fortran
b250c185 19056 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19057 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19058 dwarf2_full_name (name, die, cu),
29df156d 19059 NULL);
f55ee35c 19060
c906108c 19061 /* Default assumptions.
c5aa993b 19062 Use the passed type or decode it from the die. */
176620f1 19063 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19064 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19065 if (type != NULL)
19066 SYMBOL_TYPE (sym) = type;
19067 else
e7c27a73 19068 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19069 attr = dwarf2_attr (die,
19070 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19071 cu);
c906108c
SS
19072 if (attr)
19073 {
19074 SYMBOL_LINE (sym) = DW_UNSND (attr);
19075 }
cb1df416 19076
edb3359d
DJ
19077 attr = dwarf2_attr (die,
19078 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19079 cu);
cb1df416
DJ
19080 if (attr)
19081 {
ecfb656c 19082 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19083 struct file_entry *fe;
9a619af0 19084
ecfb656c
PA
19085 if (cu->line_header != NULL)
19086 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19087 else
19088 fe = NULL;
19089
19090 if (fe == NULL)
cb1df416
DJ
19091 complaint (&symfile_complaints,
19092 _("file index out of range"));
8c43009f
PA
19093 else
19094 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19095 }
19096
c906108c
SS
19097 switch (die->tag)
19098 {
19099 case DW_TAG_label:
e142c38c 19100 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19101 if (attr)
3e29f34a
MR
19102 {
19103 CORE_ADDR addr;
19104
19105 addr = attr_value_as_address (attr);
19106 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19107 SYMBOL_VALUE_ADDRESS (sym) = addr;
19108 }
0f5238ed
TT
19109 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19110 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19111 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19112 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19113 break;
19114 case DW_TAG_subprogram:
19115 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19116 finish_block. */
f1e6e072 19117 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19118 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19119 if ((attr2 && (DW_UNSND (attr2) != 0))
19120 || cu->language == language_ada)
c906108c 19121 {
2cfa0c8d
JB
19122 /* Subprograms marked external are stored as a global symbol.
19123 Ada subprograms, whether marked external or not, are always
19124 stored as a global symbol, because we want to be able to
19125 access them globally. For instance, we want to be able
19126 to break on a nested subprogram without having to
19127 specify the context. */
e37fd15a 19128 list_to_add = &global_symbols;
c906108c
SS
19129 }
19130 else
19131 {
e37fd15a 19132 list_to_add = cu->list_in_scope;
c906108c
SS
19133 }
19134 break;
edb3359d
DJ
19135 case DW_TAG_inlined_subroutine:
19136 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19137 finish_block. */
f1e6e072 19138 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19139 SYMBOL_INLINED (sym) = 1;
481860b3 19140 list_to_add = cu->list_in_scope;
edb3359d 19141 break;
34eaf542
TT
19142 case DW_TAG_template_value_param:
19143 suppress_add = 1;
19144 /* Fall through. */
72929c62 19145 case DW_TAG_constant:
c906108c 19146 case DW_TAG_variable:
254e6b9e 19147 case DW_TAG_member:
0963b4bd
MS
19148 /* Compilation with minimal debug info may result in
19149 variables with missing type entries. Change the
19150 misleading `void' type to something sensible. */
c906108c 19151 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19152 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19153
e142c38c 19154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19155 /* In the case of DW_TAG_member, we should only be called for
19156 static const members. */
19157 if (die->tag == DW_TAG_member)
19158 {
3863f96c
DE
19159 /* dwarf2_add_field uses die_is_declaration,
19160 so we do the same. */
254e6b9e
DE
19161 gdb_assert (die_is_declaration (die, cu));
19162 gdb_assert (attr);
19163 }
c906108c
SS
19164 if (attr)
19165 {
e7c27a73 19166 dwarf2_const_value (attr, sym, cu);
e142c38c 19167 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19168 if (!suppress_add)
34eaf542
TT
19169 {
19170 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19171 list_to_add = &global_symbols;
34eaf542 19172 else
e37fd15a 19173 list_to_add = cu->list_in_scope;
34eaf542 19174 }
c906108c
SS
19175 break;
19176 }
e142c38c 19177 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19178 if (attr)
19179 {
e7c27a73 19180 var_decode_location (attr, sym, cu);
e142c38c 19181 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19182
19183 /* Fortran explicitly imports any global symbols to the local
19184 scope by DW_TAG_common_block. */
19185 if (cu->language == language_fortran && die->parent
19186 && die->parent->tag == DW_TAG_common_block)
19187 attr2 = NULL;
19188
caac4577
JG
19189 if (SYMBOL_CLASS (sym) == LOC_STATIC
19190 && SYMBOL_VALUE_ADDRESS (sym) == 0
19191 && !dwarf2_per_objfile->has_section_at_zero)
19192 {
19193 /* When a static variable is eliminated by the linker,
19194 the corresponding debug information is not stripped
19195 out, but the variable address is set to null;
19196 do not add such variables into symbol table. */
19197 }
19198 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19199 {
f55ee35c
JK
19200 /* Workaround gfortran PR debug/40040 - it uses
19201 DW_AT_location for variables in -fPIC libraries which may
19202 get overriden by other libraries/executable and get
19203 a different address. Resolve it by the minimal symbol
19204 which may come from inferior's executable using copy
19205 relocation. Make this workaround only for gfortran as for
19206 other compilers GDB cannot guess the minimal symbol
19207 Fortran mangling kind. */
19208 if (cu->language == language_fortran && die->parent
19209 && die->parent->tag == DW_TAG_module
19210 && cu->producer
28586665 19211 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19212 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19213
1c809c68
TT
19214 /* A variable with DW_AT_external is never static,
19215 but it may be block-scoped. */
19216 list_to_add = (cu->list_in_scope == &file_symbols
19217 ? &global_symbols : cu->list_in_scope);
1c809c68 19218 }
c906108c 19219 else
e37fd15a 19220 list_to_add = cu->list_in_scope;
c906108c
SS
19221 }
19222 else
19223 {
19224 /* We do not know the address of this symbol.
c5aa993b
JM
19225 If it is an external symbol and we have type information
19226 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19227 The address of the variable will then be determined from
19228 the minimal symbol table whenever the variable is
19229 referenced. */
e142c38c 19230 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19231
19232 /* Fortran explicitly imports any global symbols to the local
19233 scope by DW_TAG_common_block. */
19234 if (cu->language == language_fortran && die->parent
19235 && die->parent->tag == DW_TAG_common_block)
19236 {
19237 /* SYMBOL_CLASS doesn't matter here because
19238 read_common_block is going to reset it. */
19239 if (!suppress_add)
19240 list_to_add = cu->list_in_scope;
19241 }
19242 else if (attr2 && (DW_UNSND (attr2) != 0)
19243 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19244 {
0fe7935b
DJ
19245 /* A variable with DW_AT_external is never static, but it
19246 may be block-scoped. */
19247 list_to_add = (cu->list_in_scope == &file_symbols
19248 ? &global_symbols : cu->list_in_scope);
19249
f1e6e072 19250 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19251 }
442ddf59
JK
19252 else if (!die_is_declaration (die, cu))
19253 {
19254 /* Use the default LOC_OPTIMIZED_OUT class. */
19255 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19256 if (!suppress_add)
19257 list_to_add = cu->list_in_scope;
442ddf59 19258 }
c906108c
SS
19259 }
19260 break;
19261 case DW_TAG_formal_parameter:
edb3359d
DJ
19262 /* If we are inside a function, mark this as an argument. If
19263 not, we might be looking at an argument to an inlined function
19264 when we do not have enough information to show inlined frames;
19265 pretend it's a local variable in that case so that the user can
19266 still see it. */
19267 if (context_stack_depth > 0
19268 && context_stack[context_stack_depth - 1].name != NULL)
19269 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19270 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19271 if (attr)
19272 {
e7c27a73 19273 var_decode_location (attr, sym, cu);
c906108c 19274 }
e142c38c 19275 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19276 if (attr)
19277 {
e7c27a73 19278 dwarf2_const_value (attr, sym, cu);
c906108c 19279 }
f346a30d 19280
e37fd15a 19281 list_to_add = cu->list_in_scope;
c906108c
SS
19282 break;
19283 case DW_TAG_unspecified_parameters:
19284 /* From varargs functions; gdb doesn't seem to have any
19285 interest in this information, so just ignore it for now.
19286 (FIXME?) */
19287 break;
34eaf542
TT
19288 case DW_TAG_template_type_param:
19289 suppress_add = 1;
19290 /* Fall through. */
c906108c 19291 case DW_TAG_class_type:
680b30c7 19292 case DW_TAG_interface_type:
c906108c
SS
19293 case DW_TAG_structure_type:
19294 case DW_TAG_union_type:
72019c9c 19295 case DW_TAG_set_type:
c906108c 19296 case DW_TAG_enumeration_type:
f1e6e072 19297 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19298 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19299
63d06c5c 19300 {
9c37b5ae 19301 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19302 really ever be static objects: otherwise, if you try
19303 to, say, break of a class's method and you're in a file
19304 which doesn't mention that class, it won't work unless
19305 the check for all static symbols in lookup_symbol_aux
19306 saves you. See the OtherFileClass tests in
19307 gdb.c++/namespace.exp. */
19308
e37fd15a 19309 if (!suppress_add)
34eaf542 19310 {
34eaf542 19311 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19312 && cu->language == language_cplus
34eaf542 19313 ? &global_symbols : cu->list_in_scope);
63d06c5c 19314
64382290 19315 /* The semantics of C++ state that "struct foo {
9c37b5ae 19316 ... }" also defines a typedef for "foo". */
64382290 19317 if (cu->language == language_cplus
45280282 19318 || cu->language == language_ada
c44af4eb
TT
19319 || cu->language == language_d
19320 || cu->language == language_rust)
64382290
TT
19321 {
19322 /* The symbol's name is already allocated along
19323 with this objfile, so we don't need to
19324 duplicate it for the type. */
19325 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19326 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19327 }
63d06c5c
DC
19328 }
19329 }
c906108c
SS
19330 break;
19331 case DW_TAG_typedef:
f1e6e072 19332 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19333 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19334 list_to_add = cu->list_in_scope;
63d06c5c 19335 break;
c906108c 19336 case DW_TAG_base_type:
a02abb62 19337 case DW_TAG_subrange_type:
f1e6e072 19338 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19339 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19340 list_to_add = cu->list_in_scope;
c906108c
SS
19341 break;
19342 case DW_TAG_enumerator:
e142c38c 19343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19344 if (attr)
19345 {
e7c27a73 19346 dwarf2_const_value (attr, sym, cu);
c906108c 19347 }
63d06c5c
DC
19348 {
19349 /* NOTE: carlton/2003-11-10: See comment above in the
19350 DW_TAG_class_type, etc. block. */
19351
e142c38c 19352 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19353 && cu->language == language_cplus
e142c38c 19354 ? &global_symbols : cu->list_in_scope);
63d06c5c 19355 }
c906108c 19356 break;
74921315 19357 case DW_TAG_imported_declaration:
5c4e30ca 19358 case DW_TAG_namespace:
f1e6e072 19359 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19360 list_to_add = &global_symbols;
5c4e30ca 19361 break;
530e8392
KB
19362 case DW_TAG_module:
19363 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19364 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19365 list_to_add = &global_symbols;
19366 break;
4357ac6c 19367 case DW_TAG_common_block:
f1e6e072 19368 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19369 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19370 add_symbol_to_list (sym, cu->list_in_scope);
19371 break;
c906108c
SS
19372 default:
19373 /* Not a tag we recognize. Hopefully we aren't processing
19374 trash data, but since we must specifically ignore things
19375 we don't recognize, there is nothing else we should do at
0963b4bd 19376 this point. */
e2e0b3e5 19377 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19378 dwarf_tag_name (die->tag));
c906108c
SS
19379 break;
19380 }
df8a16a1 19381
e37fd15a
SW
19382 if (suppress_add)
19383 {
19384 sym->hash_next = objfile->template_symbols;
19385 objfile->template_symbols = sym;
19386 list_to_add = NULL;
19387 }
19388
19389 if (list_to_add != NULL)
19390 add_symbol_to_list (sym, list_to_add);
19391
df8a16a1
DJ
19392 /* For the benefit of old versions of GCC, check for anonymous
19393 namespaces based on the demangled name. */
4d4ec4e5 19394 if (!cu->processing_has_namespace_info
94af9270 19395 && cu->language == language_cplus)
a10964d1 19396 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19397 }
19398 return (sym);
19399}
19400
34eaf542
TT
19401/* A wrapper for new_symbol_full that always allocates a new symbol. */
19402
19403static struct symbol *
19404new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19405{
19406 return new_symbol_full (die, type, cu, NULL);
19407}
19408
98bfdba5
PA
19409/* Given an attr with a DW_FORM_dataN value in host byte order,
19410 zero-extend it as appropriate for the symbol's type. The DWARF
19411 standard (v4) is not entirely clear about the meaning of using
19412 DW_FORM_dataN for a constant with a signed type, where the type is
19413 wider than the data. The conclusion of a discussion on the DWARF
19414 list was that this is unspecified. We choose to always zero-extend
19415 because that is the interpretation long in use by GCC. */
c906108c 19416
98bfdba5 19417static gdb_byte *
ff39bb5e 19418dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19419 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19420{
e7c27a73 19421 struct objfile *objfile = cu->objfile;
e17a4113
UW
19422 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19423 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19424 LONGEST l = DW_UNSND (attr);
19425
19426 if (bits < sizeof (*value) * 8)
19427 {
19428 l &= ((LONGEST) 1 << bits) - 1;
19429 *value = l;
19430 }
19431 else if (bits == sizeof (*value) * 8)
19432 *value = l;
19433 else
19434 {
224c3ddb 19435 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19436 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19437 return bytes;
19438 }
19439
19440 return NULL;
19441}
19442
19443/* Read a constant value from an attribute. Either set *VALUE, or if
19444 the value does not fit in *VALUE, set *BYTES - either already
19445 allocated on the objfile obstack, or newly allocated on OBSTACK,
19446 or, set *BATON, if we translated the constant to a location
19447 expression. */
19448
19449static void
ff39bb5e 19450dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19451 const char *name, struct obstack *obstack,
19452 struct dwarf2_cu *cu,
d521ce57 19453 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19454 struct dwarf2_locexpr_baton **baton)
19455{
19456 struct objfile *objfile = cu->objfile;
19457 struct comp_unit_head *cu_header = &cu->header;
c906108c 19458 struct dwarf_block *blk;
98bfdba5
PA
19459 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19460 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19461
19462 *value = 0;
19463 *bytes = NULL;
19464 *baton = NULL;
c906108c
SS
19465
19466 switch (attr->form)
19467 {
19468 case DW_FORM_addr:
3019eac3 19469 case DW_FORM_GNU_addr_index:
ac56253d 19470 {
ac56253d
TT
19471 gdb_byte *data;
19472
98bfdba5
PA
19473 if (TYPE_LENGTH (type) != cu_header->addr_size)
19474 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19475 cu_header->addr_size,
98bfdba5 19476 TYPE_LENGTH (type));
ac56253d
TT
19477 /* Symbols of this form are reasonably rare, so we just
19478 piggyback on the existing location code rather than writing
19479 a new implementation of symbol_computed_ops. */
8d749320 19480 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19481 (*baton)->per_cu = cu->per_cu;
19482 gdb_assert ((*baton)->per_cu);
ac56253d 19483
98bfdba5 19484 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19485 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19486 (*baton)->data = data;
ac56253d
TT
19487
19488 data[0] = DW_OP_addr;
19489 store_unsigned_integer (&data[1], cu_header->addr_size,
19490 byte_order, DW_ADDR (attr));
19491 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19492 }
c906108c 19493 break;
4ac36638 19494 case DW_FORM_string:
93b5768b 19495 case DW_FORM_strp:
3019eac3 19496 case DW_FORM_GNU_str_index:
36586728 19497 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19498 /* DW_STRING is already allocated on the objfile obstack, point
19499 directly to it. */
d521ce57 19500 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19501 break;
c906108c
SS
19502 case DW_FORM_block1:
19503 case DW_FORM_block2:
19504 case DW_FORM_block4:
19505 case DW_FORM_block:
2dc7f7b3 19506 case DW_FORM_exprloc:
0224619f 19507 case DW_FORM_data16:
c906108c 19508 blk = DW_BLOCK (attr);
98bfdba5
PA
19509 if (TYPE_LENGTH (type) != blk->size)
19510 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19511 TYPE_LENGTH (type));
19512 *bytes = blk->data;
c906108c 19513 break;
2df3850c
JM
19514
19515 /* The DW_AT_const_value attributes are supposed to carry the
19516 symbol's value "represented as it would be on the target
19517 architecture." By the time we get here, it's already been
19518 converted to host endianness, so we just need to sign- or
19519 zero-extend it as appropriate. */
19520 case DW_FORM_data1:
3aef2284 19521 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19522 break;
c906108c 19523 case DW_FORM_data2:
3aef2284 19524 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19525 break;
c906108c 19526 case DW_FORM_data4:
3aef2284 19527 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19528 break;
c906108c 19529 case DW_FORM_data8:
3aef2284 19530 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19531 break;
19532
c906108c 19533 case DW_FORM_sdata:
663c44ac 19534 case DW_FORM_implicit_const:
98bfdba5 19535 *value = DW_SND (attr);
2df3850c
JM
19536 break;
19537
c906108c 19538 case DW_FORM_udata:
98bfdba5 19539 *value = DW_UNSND (attr);
c906108c 19540 break;
2df3850c 19541
c906108c 19542 default:
4d3c2250 19543 complaint (&symfile_complaints,
e2e0b3e5 19544 _("unsupported const value attribute form: '%s'"),
4d3c2250 19545 dwarf_form_name (attr->form));
98bfdba5 19546 *value = 0;
c906108c
SS
19547 break;
19548 }
19549}
19550
2df3850c 19551
98bfdba5
PA
19552/* Copy constant value from an attribute to a symbol. */
19553
2df3850c 19554static void
ff39bb5e 19555dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19556 struct dwarf2_cu *cu)
2df3850c 19557{
98bfdba5 19558 struct objfile *objfile = cu->objfile;
12df843f 19559 LONGEST value;
d521ce57 19560 const gdb_byte *bytes;
98bfdba5 19561 struct dwarf2_locexpr_baton *baton;
2df3850c 19562
98bfdba5
PA
19563 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19564 SYMBOL_PRINT_NAME (sym),
19565 &objfile->objfile_obstack, cu,
19566 &value, &bytes, &baton);
2df3850c 19567
98bfdba5
PA
19568 if (baton != NULL)
19569 {
98bfdba5 19570 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19571 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19572 }
19573 else if (bytes != NULL)
19574 {
19575 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19576 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19577 }
19578 else
19579 {
19580 SYMBOL_VALUE (sym) = value;
f1e6e072 19581 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19582 }
2df3850c
JM
19583}
19584
c906108c
SS
19585/* Return the type of the die in question using its DW_AT_type attribute. */
19586
19587static struct type *
e7c27a73 19588die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19589{
c906108c 19590 struct attribute *type_attr;
c906108c 19591
e142c38c 19592 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19593 if (!type_attr)
19594 {
19595 /* A missing DW_AT_type represents a void type. */
46bf5051 19596 return objfile_type (cu->objfile)->builtin_void;
c906108c 19597 }
348e048f 19598
673bfd45 19599 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19600}
19601
b4ba55a1
JB
19602/* True iff CU's producer generates GNAT Ada auxiliary information
19603 that allows to find parallel types through that information instead
19604 of having to do expensive parallel lookups by type name. */
19605
19606static int
19607need_gnat_info (struct dwarf2_cu *cu)
19608{
19609 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19610 of GNAT produces this auxiliary information, without any indication
19611 that it is produced. Part of enhancing the FSF version of GNAT
19612 to produce that information will be to put in place an indicator
19613 that we can use in order to determine whether the descriptive type
19614 info is available or not. One suggestion that has been made is
19615 to use a new attribute, attached to the CU die. For now, assume
19616 that the descriptive type info is not available. */
19617 return 0;
19618}
19619
b4ba55a1
JB
19620/* Return the auxiliary type of the die in question using its
19621 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19622 attribute is not present. */
19623
19624static struct type *
19625die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19626{
b4ba55a1 19627 struct attribute *type_attr;
b4ba55a1
JB
19628
19629 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19630 if (!type_attr)
19631 return NULL;
19632
673bfd45 19633 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19634}
19635
19636/* If DIE has a descriptive_type attribute, then set the TYPE's
19637 descriptive type accordingly. */
19638
19639static void
19640set_descriptive_type (struct type *type, struct die_info *die,
19641 struct dwarf2_cu *cu)
19642{
19643 struct type *descriptive_type = die_descriptive_type (die, cu);
19644
19645 if (descriptive_type)
19646 {
19647 ALLOCATE_GNAT_AUX_TYPE (type);
19648 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19649 }
19650}
19651
c906108c
SS
19652/* Return the containing type of the die in question using its
19653 DW_AT_containing_type attribute. */
19654
19655static struct type *
e7c27a73 19656die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19657{
c906108c 19658 struct attribute *type_attr;
c906108c 19659
e142c38c 19660 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19661 if (!type_attr)
19662 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19663 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19664
673bfd45 19665 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19666}
19667
ac9ec31b
DE
19668/* Return an error marker type to use for the ill formed type in DIE/CU. */
19669
19670static struct type *
19671build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19672{
19673 struct objfile *objfile = dwarf2_per_objfile->objfile;
19674 char *message, *saved;
19675
19676 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19677 objfile_name (objfile),
9c541725
PA
19678 to_underlying (cu->header.sect_off),
19679 to_underlying (die->sect_off));
224c3ddb
SM
19680 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19681 message, strlen (message));
ac9ec31b
DE
19682 xfree (message);
19683
19f392bc 19684 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19685}
19686
673bfd45 19687/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19688 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19689 DW_AT_containing_type.
673bfd45
DE
19690 If there is no type substitute an error marker. */
19691
c906108c 19692static struct type *
ff39bb5e 19693lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19694 struct dwarf2_cu *cu)
c906108c 19695{
bb5ed363 19696 struct objfile *objfile = cu->objfile;
f792889a
DJ
19697 struct type *this_type;
19698
ac9ec31b
DE
19699 gdb_assert (attr->name == DW_AT_type
19700 || attr->name == DW_AT_GNAT_descriptive_type
19701 || attr->name == DW_AT_containing_type);
19702
673bfd45
DE
19703 /* First see if we have it cached. */
19704
36586728
TT
19705 if (attr->form == DW_FORM_GNU_ref_alt)
19706 {
19707 struct dwarf2_per_cu_data *per_cu;
9c541725 19708 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19709
9c541725
PA
19710 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19711 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19712 }
7771576e 19713 else if (attr_form_is_ref (attr))
673bfd45 19714 {
9c541725 19715 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19716
9c541725 19717 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19718 }
55f1336d 19719 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19720 {
ac9ec31b 19721 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19722
ac9ec31b 19723 return get_signatured_type (die, signature, cu);
673bfd45
DE
19724 }
19725 else
19726 {
ac9ec31b
DE
19727 complaint (&symfile_complaints,
19728 _("Dwarf Error: Bad type attribute %s in DIE"
19729 " at 0x%x [in module %s]"),
9c541725 19730 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19731 objfile_name (objfile));
ac9ec31b 19732 return build_error_marker_type (cu, die);
673bfd45
DE
19733 }
19734
19735 /* If not cached we need to read it in. */
19736
19737 if (this_type == NULL)
19738 {
ac9ec31b 19739 struct die_info *type_die = NULL;
673bfd45
DE
19740 struct dwarf2_cu *type_cu = cu;
19741
7771576e 19742 if (attr_form_is_ref (attr))
ac9ec31b
DE
19743 type_die = follow_die_ref (die, attr, &type_cu);
19744 if (type_die == NULL)
19745 return build_error_marker_type (cu, die);
19746 /* If we find the type now, it's probably because the type came
3019eac3
DE
19747 from an inter-CU reference and the type's CU got expanded before
19748 ours. */
ac9ec31b 19749 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19750 }
19751
19752 /* If we still don't have a type use an error marker. */
19753
19754 if (this_type == NULL)
ac9ec31b 19755 return build_error_marker_type (cu, die);
673bfd45 19756
f792889a 19757 return this_type;
c906108c
SS
19758}
19759
673bfd45
DE
19760/* Return the type in DIE, CU.
19761 Returns NULL for invalid types.
19762
02142a6c 19763 This first does a lookup in die_type_hash,
673bfd45
DE
19764 and only reads the die in if necessary.
19765
19766 NOTE: This can be called when reading in partial or full symbols. */
19767
f792889a 19768static struct type *
e7c27a73 19769read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19770{
f792889a
DJ
19771 struct type *this_type;
19772
19773 this_type = get_die_type (die, cu);
19774 if (this_type)
19775 return this_type;
19776
673bfd45
DE
19777 return read_type_die_1 (die, cu);
19778}
19779
19780/* Read the type in DIE, CU.
19781 Returns NULL for invalid types. */
19782
19783static struct type *
19784read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19785{
19786 struct type *this_type = NULL;
19787
c906108c
SS
19788 switch (die->tag)
19789 {
19790 case DW_TAG_class_type:
680b30c7 19791 case DW_TAG_interface_type:
c906108c
SS
19792 case DW_TAG_structure_type:
19793 case DW_TAG_union_type:
f792889a 19794 this_type = read_structure_type (die, cu);
c906108c
SS
19795 break;
19796 case DW_TAG_enumeration_type:
f792889a 19797 this_type = read_enumeration_type (die, cu);
c906108c
SS
19798 break;
19799 case DW_TAG_subprogram:
19800 case DW_TAG_subroutine_type:
edb3359d 19801 case DW_TAG_inlined_subroutine:
f792889a 19802 this_type = read_subroutine_type (die, cu);
c906108c
SS
19803 break;
19804 case DW_TAG_array_type:
f792889a 19805 this_type = read_array_type (die, cu);
c906108c 19806 break;
72019c9c 19807 case DW_TAG_set_type:
f792889a 19808 this_type = read_set_type (die, cu);
72019c9c 19809 break;
c906108c 19810 case DW_TAG_pointer_type:
f792889a 19811 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19812 break;
19813 case DW_TAG_ptr_to_member_type:
f792889a 19814 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19815 break;
19816 case DW_TAG_reference_type:
4297a3f0
AV
19817 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19818 break;
19819 case DW_TAG_rvalue_reference_type:
19820 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19821 break;
19822 case DW_TAG_const_type:
f792889a 19823 this_type = read_tag_const_type (die, cu);
c906108c
SS
19824 break;
19825 case DW_TAG_volatile_type:
f792889a 19826 this_type = read_tag_volatile_type (die, cu);
c906108c 19827 break;
06d66ee9
TT
19828 case DW_TAG_restrict_type:
19829 this_type = read_tag_restrict_type (die, cu);
19830 break;
c906108c 19831 case DW_TAG_string_type:
f792889a 19832 this_type = read_tag_string_type (die, cu);
c906108c
SS
19833 break;
19834 case DW_TAG_typedef:
f792889a 19835 this_type = read_typedef (die, cu);
c906108c 19836 break;
a02abb62 19837 case DW_TAG_subrange_type:
f792889a 19838 this_type = read_subrange_type (die, cu);
a02abb62 19839 break;
c906108c 19840 case DW_TAG_base_type:
f792889a 19841 this_type = read_base_type (die, cu);
c906108c 19842 break;
81a17f79 19843 case DW_TAG_unspecified_type:
f792889a 19844 this_type = read_unspecified_type (die, cu);
81a17f79 19845 break;
0114d602
DJ
19846 case DW_TAG_namespace:
19847 this_type = read_namespace_type (die, cu);
19848 break;
f55ee35c
JK
19849 case DW_TAG_module:
19850 this_type = read_module_type (die, cu);
19851 break;
a2c2acaf
MW
19852 case DW_TAG_atomic_type:
19853 this_type = read_tag_atomic_type (die, cu);
19854 break;
c906108c 19855 default:
3e43a32a
MS
19856 complaint (&symfile_complaints,
19857 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19858 dwarf_tag_name (die->tag));
c906108c
SS
19859 break;
19860 }
63d06c5c 19861
f792889a 19862 return this_type;
63d06c5c
DC
19863}
19864
abc72ce4
DE
19865/* See if we can figure out if the class lives in a namespace. We do
19866 this by looking for a member function; its demangled name will
19867 contain namespace info, if there is any.
19868 Return the computed name or NULL.
19869 Space for the result is allocated on the objfile's obstack.
19870 This is the full-die version of guess_partial_die_structure_name.
19871 In this case we know DIE has no useful parent. */
19872
19873static char *
19874guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19875{
19876 struct die_info *spec_die;
19877 struct dwarf2_cu *spec_cu;
19878 struct die_info *child;
19879
19880 spec_cu = cu;
19881 spec_die = die_specification (die, &spec_cu);
19882 if (spec_die != NULL)
19883 {
19884 die = spec_die;
19885 cu = spec_cu;
19886 }
19887
19888 for (child = die->child;
19889 child != NULL;
19890 child = child->sibling)
19891 {
19892 if (child->tag == DW_TAG_subprogram)
19893 {
73b9be8b 19894 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 19895
7d45c7c3 19896 if (linkage_name != NULL)
abc72ce4
DE
19897 {
19898 char *actual_name
19899 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19900 linkage_name);
abc72ce4
DE
19901 char *name = NULL;
19902
19903 if (actual_name != NULL)
19904 {
15d034d0 19905 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19906
19907 if (die_name != NULL
19908 && strcmp (die_name, actual_name) != 0)
19909 {
19910 /* Strip off the class name from the full name.
19911 We want the prefix. */
19912 int die_name_len = strlen (die_name);
19913 int actual_name_len = strlen (actual_name);
19914
19915 /* Test for '::' as a sanity check. */
19916 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19917 && actual_name[actual_name_len
19918 - die_name_len - 1] == ':')
224c3ddb
SM
19919 name = (char *) obstack_copy0 (
19920 &cu->objfile->per_bfd->storage_obstack,
19921 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19922 }
19923 }
19924 xfree (actual_name);
19925 return name;
19926 }
19927 }
19928 }
19929
19930 return NULL;
19931}
19932
96408a79
SA
19933/* GCC might emit a nameless typedef that has a linkage name. Determine the
19934 prefix part in such case. See
19935 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19936
a121b7c1 19937static const char *
96408a79
SA
19938anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19939{
19940 struct attribute *attr;
e6a959d6 19941 const char *base;
96408a79
SA
19942
19943 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19944 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19945 return NULL;
19946
7d45c7c3 19947 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19948 return NULL;
19949
73b9be8b 19950 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
19951 if (attr == NULL || DW_STRING (attr) == NULL)
19952 return NULL;
19953
19954 /* dwarf2_name had to be already called. */
19955 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19956
19957 /* Strip the base name, keep any leading namespaces/classes. */
19958 base = strrchr (DW_STRING (attr), ':');
19959 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19960 return "";
19961
224c3ddb
SM
19962 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19963 DW_STRING (attr),
19964 &base[-1] - DW_STRING (attr));
96408a79
SA
19965}
19966
fdde2d81 19967/* Return the name of the namespace/class that DIE is defined within,
0114d602 19968 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19969
0114d602
DJ
19970 For example, if we're within the method foo() in the following
19971 code:
19972
19973 namespace N {
19974 class C {
19975 void foo () {
19976 }
19977 };
19978 }
19979
19980 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19981
0d5cff50 19982static const char *
e142c38c 19983determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19984{
0114d602
DJ
19985 struct die_info *parent, *spec_die;
19986 struct dwarf2_cu *spec_cu;
19987 struct type *parent_type;
a121b7c1 19988 const char *retval;
63d06c5c 19989
9c37b5ae 19990 if (cu->language != language_cplus
c44af4eb
TT
19991 && cu->language != language_fortran && cu->language != language_d
19992 && cu->language != language_rust)
0114d602
DJ
19993 return "";
19994
96408a79
SA
19995 retval = anonymous_struct_prefix (die, cu);
19996 if (retval)
19997 return retval;
19998
0114d602
DJ
19999 /* We have to be careful in the presence of DW_AT_specification.
20000 For example, with GCC 3.4, given the code
20001
20002 namespace N {
20003 void foo() {
20004 // Definition of N::foo.
20005 }
20006 }
20007
20008 then we'll have a tree of DIEs like this:
20009
20010 1: DW_TAG_compile_unit
20011 2: DW_TAG_namespace // N
20012 3: DW_TAG_subprogram // declaration of N::foo
20013 4: DW_TAG_subprogram // definition of N::foo
20014 DW_AT_specification // refers to die #3
20015
20016 Thus, when processing die #4, we have to pretend that we're in
20017 the context of its DW_AT_specification, namely the contex of die
20018 #3. */
20019 spec_cu = cu;
20020 spec_die = die_specification (die, &spec_cu);
20021 if (spec_die == NULL)
20022 parent = die->parent;
20023 else
63d06c5c 20024 {
0114d602
DJ
20025 parent = spec_die->parent;
20026 cu = spec_cu;
63d06c5c 20027 }
0114d602
DJ
20028
20029 if (parent == NULL)
20030 return "";
98bfdba5
PA
20031 else if (parent->building_fullname)
20032 {
20033 const char *name;
20034 const char *parent_name;
20035
20036 /* It has been seen on RealView 2.2 built binaries,
20037 DW_TAG_template_type_param types actually _defined_ as
20038 children of the parent class:
20039
20040 enum E {};
20041 template class <class Enum> Class{};
20042 Class<enum E> class_e;
20043
20044 1: DW_TAG_class_type (Class)
20045 2: DW_TAG_enumeration_type (E)
20046 3: DW_TAG_enumerator (enum1:0)
20047 3: DW_TAG_enumerator (enum2:1)
20048 ...
20049 2: DW_TAG_template_type_param
20050 DW_AT_type DW_FORM_ref_udata (E)
20051
20052 Besides being broken debug info, it can put GDB into an
20053 infinite loop. Consider:
20054
20055 When we're building the full name for Class<E>, we'll start
20056 at Class, and go look over its template type parameters,
20057 finding E. We'll then try to build the full name of E, and
20058 reach here. We're now trying to build the full name of E,
20059 and look over the parent DIE for containing scope. In the
20060 broken case, if we followed the parent DIE of E, we'd again
20061 find Class, and once again go look at its template type
20062 arguments, etc., etc. Simply don't consider such parent die
20063 as source-level parent of this die (it can't be, the language
20064 doesn't allow it), and break the loop here. */
20065 name = dwarf2_name (die, cu);
20066 parent_name = dwarf2_name (parent, cu);
20067 complaint (&symfile_complaints,
20068 _("template param type '%s' defined within parent '%s'"),
20069 name ? name : "<unknown>",
20070 parent_name ? parent_name : "<unknown>");
20071 return "";
20072 }
63d06c5c 20073 else
0114d602
DJ
20074 switch (parent->tag)
20075 {
63d06c5c 20076 case DW_TAG_namespace:
0114d602 20077 parent_type = read_type_die (parent, cu);
acebe513
UW
20078 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20079 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20080 Work around this problem here. */
20081 if (cu->language == language_cplus
20082 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20083 return "";
0114d602
DJ
20084 /* We give a name to even anonymous namespaces. */
20085 return TYPE_TAG_NAME (parent_type);
63d06c5c 20086 case DW_TAG_class_type:
680b30c7 20087 case DW_TAG_interface_type:
63d06c5c 20088 case DW_TAG_structure_type:
0114d602 20089 case DW_TAG_union_type:
f55ee35c 20090 case DW_TAG_module:
0114d602
DJ
20091 parent_type = read_type_die (parent, cu);
20092 if (TYPE_TAG_NAME (parent_type) != NULL)
20093 return TYPE_TAG_NAME (parent_type);
20094 else
20095 /* An anonymous structure is only allowed non-static data
20096 members; no typedefs, no member functions, et cetera.
20097 So it does not need a prefix. */
20098 return "";
abc72ce4 20099 case DW_TAG_compile_unit:
95554aad 20100 case DW_TAG_partial_unit:
abc72ce4
DE
20101 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20102 if (cu->language == language_cplus
8b70b953 20103 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20104 && die->child != NULL
20105 && (die->tag == DW_TAG_class_type
20106 || die->tag == DW_TAG_structure_type
20107 || die->tag == DW_TAG_union_type))
20108 {
20109 char *name = guess_full_die_structure_name (die, cu);
20110 if (name != NULL)
20111 return name;
20112 }
20113 return "";
3d567982
TT
20114 case DW_TAG_enumeration_type:
20115 parent_type = read_type_die (parent, cu);
20116 if (TYPE_DECLARED_CLASS (parent_type))
20117 {
20118 if (TYPE_TAG_NAME (parent_type) != NULL)
20119 return TYPE_TAG_NAME (parent_type);
20120 return "";
20121 }
20122 /* Fall through. */
63d06c5c 20123 default:
8176b9b8 20124 return determine_prefix (parent, cu);
63d06c5c 20125 }
63d06c5c
DC
20126}
20127
3e43a32a
MS
20128/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20129 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20130 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20131 an obconcat, otherwise allocate storage for the result. The CU argument is
20132 used to determine the language and hence, the appropriate separator. */
987504bb 20133
f55ee35c 20134#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20135
20136static char *
f55ee35c
JK
20137typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20138 int physname, struct dwarf2_cu *cu)
63d06c5c 20139{
f55ee35c 20140 const char *lead = "";
5c315b68 20141 const char *sep;
63d06c5c 20142
3e43a32a
MS
20143 if (suffix == NULL || suffix[0] == '\0'
20144 || prefix == NULL || prefix[0] == '\0')
987504bb 20145 sep = "";
45280282
IB
20146 else if (cu->language == language_d)
20147 {
20148 /* For D, the 'main' function could be defined in any module, but it
20149 should never be prefixed. */
20150 if (strcmp (suffix, "D main") == 0)
20151 {
20152 prefix = "";
20153 sep = "";
20154 }
20155 else
20156 sep = ".";
20157 }
f55ee35c
JK
20158 else if (cu->language == language_fortran && physname)
20159 {
20160 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20161 DW_AT_MIPS_linkage_name is preferred and used instead. */
20162
20163 lead = "__";
20164 sep = "_MOD_";
20165 }
987504bb
JJ
20166 else
20167 sep = "::";
63d06c5c 20168
6dd47d34
DE
20169 if (prefix == NULL)
20170 prefix = "";
20171 if (suffix == NULL)
20172 suffix = "";
20173
987504bb
JJ
20174 if (obs == NULL)
20175 {
3e43a32a 20176 char *retval
224c3ddb
SM
20177 = ((char *)
20178 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20179
f55ee35c
JK
20180 strcpy (retval, lead);
20181 strcat (retval, prefix);
6dd47d34
DE
20182 strcat (retval, sep);
20183 strcat (retval, suffix);
63d06c5c
DC
20184 return retval;
20185 }
987504bb
JJ
20186 else
20187 {
20188 /* We have an obstack. */
f55ee35c 20189 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20190 }
63d06c5c
DC
20191}
20192
c906108c
SS
20193/* Return sibling of die, NULL if no sibling. */
20194
f9aca02d 20195static struct die_info *
fba45db2 20196sibling_die (struct die_info *die)
c906108c 20197{
639d11d3 20198 return die->sibling;
c906108c
SS
20199}
20200
71c25dea
TT
20201/* Get name of a die, return NULL if not found. */
20202
15d034d0
TT
20203static const char *
20204dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20205 struct obstack *obstack)
20206{
20207 if (name && cu->language == language_cplus)
20208 {
2f408ecb 20209 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20210
2f408ecb 20211 if (!canon_name.empty ())
71c25dea 20212 {
2f408ecb
PA
20213 if (canon_name != name)
20214 name = (const char *) obstack_copy0 (obstack,
20215 canon_name.c_str (),
20216 canon_name.length ());
71c25dea
TT
20217 }
20218 }
20219
20220 return name;
c906108c
SS
20221}
20222
96553a0c
DE
20223/* Get name of a die, return NULL if not found.
20224 Anonymous namespaces are converted to their magic string. */
9219021c 20225
15d034d0 20226static const char *
e142c38c 20227dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20228{
20229 struct attribute *attr;
20230
e142c38c 20231 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20232 if ((!attr || !DW_STRING (attr))
96553a0c 20233 && die->tag != DW_TAG_namespace
53832f31
TT
20234 && die->tag != DW_TAG_class_type
20235 && die->tag != DW_TAG_interface_type
20236 && die->tag != DW_TAG_structure_type
20237 && die->tag != DW_TAG_union_type)
71c25dea
TT
20238 return NULL;
20239
20240 switch (die->tag)
20241 {
20242 case DW_TAG_compile_unit:
95554aad 20243 case DW_TAG_partial_unit:
71c25dea
TT
20244 /* Compilation units have a DW_AT_name that is a filename, not
20245 a source language identifier. */
20246 case DW_TAG_enumeration_type:
20247 case DW_TAG_enumerator:
20248 /* These tags always have simple identifiers already; no need
20249 to canonicalize them. */
20250 return DW_STRING (attr);
907af001 20251
96553a0c
DE
20252 case DW_TAG_namespace:
20253 if (attr != NULL && DW_STRING (attr) != NULL)
20254 return DW_STRING (attr);
20255 return CP_ANONYMOUS_NAMESPACE_STR;
20256
907af001
UW
20257 case DW_TAG_class_type:
20258 case DW_TAG_interface_type:
20259 case DW_TAG_structure_type:
20260 case DW_TAG_union_type:
20261 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20262 structures or unions. These were of the form "._%d" in GCC 4.1,
20263 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20264 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20265 if (attr && DW_STRING (attr)
61012eef
GB
20266 && (startswith (DW_STRING (attr), "._")
20267 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20268 return NULL;
53832f31
TT
20269
20270 /* GCC might emit a nameless typedef that has a linkage name. See
20271 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20272 if (!attr || DW_STRING (attr) == NULL)
20273 {
df5c6c50 20274 char *demangled = NULL;
53832f31 20275
73b9be8b 20276 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20277 if (attr == NULL || DW_STRING (attr) == NULL)
20278 return NULL;
20279
df5c6c50
JK
20280 /* Avoid demangling DW_STRING (attr) the second time on a second
20281 call for the same DIE. */
20282 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20283 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20284
20285 if (demangled)
20286 {
e6a959d6 20287 const char *base;
96408a79 20288
53832f31 20289 /* FIXME: we already did this for the partial symbol... */
34a68019 20290 DW_STRING (attr)
224c3ddb
SM
20291 = ((const char *)
20292 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20293 demangled, strlen (demangled)));
53832f31
TT
20294 DW_STRING_IS_CANONICAL (attr) = 1;
20295 xfree (demangled);
96408a79
SA
20296
20297 /* Strip any leading namespaces/classes, keep only the base name.
20298 DW_AT_name for named DIEs does not contain the prefixes. */
20299 base = strrchr (DW_STRING (attr), ':');
20300 if (base && base > DW_STRING (attr) && base[-1] == ':')
20301 return &base[1];
20302 else
20303 return DW_STRING (attr);
53832f31
TT
20304 }
20305 }
907af001
UW
20306 break;
20307
71c25dea 20308 default:
907af001
UW
20309 break;
20310 }
20311
20312 if (!DW_STRING_IS_CANONICAL (attr))
20313 {
20314 DW_STRING (attr)
20315 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20316 &cu->objfile->per_bfd->storage_obstack);
907af001 20317 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20318 }
907af001 20319 return DW_STRING (attr);
9219021c
DC
20320}
20321
20322/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20323 is none. *EXT_CU is the CU containing DIE on input, and the CU
20324 containing the return value on output. */
9219021c
DC
20325
20326static struct die_info *
f2f0e013 20327dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20328{
20329 struct attribute *attr;
9219021c 20330
f2f0e013 20331 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20332 if (attr == NULL)
20333 return NULL;
20334
f2f0e013 20335 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20336}
20337
c906108c
SS
20338/* Convert a DIE tag into its string name. */
20339
f39c6ffd 20340static const char *
aa1ee363 20341dwarf_tag_name (unsigned tag)
c906108c 20342{
f39c6ffd
TT
20343 const char *name = get_DW_TAG_name (tag);
20344
20345 if (name == NULL)
20346 return "DW_TAG_<unknown>";
20347
20348 return name;
c906108c
SS
20349}
20350
20351/* Convert a DWARF attribute code into its string name. */
20352
f39c6ffd 20353static const char *
aa1ee363 20354dwarf_attr_name (unsigned attr)
c906108c 20355{
f39c6ffd
TT
20356 const char *name;
20357
c764a876 20358#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20359 if (attr == DW_AT_MIPS_fde)
20360 return "DW_AT_MIPS_fde";
20361#else
20362 if (attr == DW_AT_HP_block_index)
20363 return "DW_AT_HP_block_index";
c764a876 20364#endif
f39c6ffd
TT
20365
20366 name = get_DW_AT_name (attr);
20367
20368 if (name == NULL)
20369 return "DW_AT_<unknown>";
20370
20371 return name;
c906108c
SS
20372}
20373
20374/* Convert a DWARF value form code into its string name. */
20375
f39c6ffd 20376static const char *
aa1ee363 20377dwarf_form_name (unsigned form)
c906108c 20378{
f39c6ffd
TT
20379 const char *name = get_DW_FORM_name (form);
20380
20381 if (name == NULL)
20382 return "DW_FORM_<unknown>";
20383
20384 return name;
c906108c
SS
20385}
20386
a121b7c1 20387static const char *
fba45db2 20388dwarf_bool_name (unsigned mybool)
c906108c
SS
20389{
20390 if (mybool)
20391 return "TRUE";
20392 else
20393 return "FALSE";
20394}
20395
20396/* Convert a DWARF type code into its string name. */
20397
f39c6ffd 20398static const char *
aa1ee363 20399dwarf_type_encoding_name (unsigned enc)
c906108c 20400{
f39c6ffd 20401 const char *name = get_DW_ATE_name (enc);
c906108c 20402
f39c6ffd
TT
20403 if (name == NULL)
20404 return "DW_ATE_<unknown>";
c906108c 20405
f39c6ffd 20406 return name;
c906108c 20407}
c906108c 20408
f9aca02d 20409static void
d97bc12b 20410dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20411{
20412 unsigned int i;
20413
d97bc12b
DE
20414 print_spaces (indent, f);
20415 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20416 dwarf_tag_name (die->tag), die->abbrev,
20417 to_underlying (die->sect_off));
d97bc12b
DE
20418
20419 if (die->parent != NULL)
20420 {
20421 print_spaces (indent, f);
20422 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20423 to_underlying (die->parent->sect_off));
d97bc12b
DE
20424 }
20425
20426 print_spaces (indent, f);
20427 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20428 dwarf_bool_name (die->child != NULL));
c906108c 20429
d97bc12b
DE
20430 print_spaces (indent, f);
20431 fprintf_unfiltered (f, " attributes:\n");
20432
c906108c
SS
20433 for (i = 0; i < die->num_attrs; ++i)
20434 {
d97bc12b
DE
20435 print_spaces (indent, f);
20436 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20437 dwarf_attr_name (die->attrs[i].name),
20438 dwarf_form_name (die->attrs[i].form));
d97bc12b 20439
c906108c
SS
20440 switch (die->attrs[i].form)
20441 {
c906108c 20442 case DW_FORM_addr:
3019eac3 20443 case DW_FORM_GNU_addr_index:
d97bc12b 20444 fprintf_unfiltered (f, "address: ");
5af949e3 20445 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20446 break;
20447 case DW_FORM_block2:
20448 case DW_FORM_block4:
20449 case DW_FORM_block:
20450 case DW_FORM_block1:
56eb65bd
SP
20451 fprintf_unfiltered (f, "block: size %s",
20452 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20453 break;
2dc7f7b3 20454 case DW_FORM_exprloc:
56eb65bd
SP
20455 fprintf_unfiltered (f, "expression: size %s",
20456 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20457 break;
0224619f
JK
20458 case DW_FORM_data16:
20459 fprintf_unfiltered (f, "constant of 16 bytes");
20460 break;
4568ecf9
DE
20461 case DW_FORM_ref_addr:
20462 fprintf_unfiltered (f, "ref address: ");
20463 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20464 break;
36586728
TT
20465 case DW_FORM_GNU_ref_alt:
20466 fprintf_unfiltered (f, "alt ref address: ");
20467 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20468 break;
10b3939b
DJ
20469 case DW_FORM_ref1:
20470 case DW_FORM_ref2:
20471 case DW_FORM_ref4:
4568ecf9
DE
20472 case DW_FORM_ref8:
20473 case DW_FORM_ref_udata:
d97bc12b 20474 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20475 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20476 break;
c906108c
SS
20477 case DW_FORM_data1:
20478 case DW_FORM_data2:
20479 case DW_FORM_data4:
ce5d95e1 20480 case DW_FORM_data8:
c906108c
SS
20481 case DW_FORM_udata:
20482 case DW_FORM_sdata:
43bbcdc2
PH
20483 fprintf_unfiltered (f, "constant: %s",
20484 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20485 break;
2dc7f7b3
TT
20486 case DW_FORM_sec_offset:
20487 fprintf_unfiltered (f, "section offset: %s",
20488 pulongest (DW_UNSND (&die->attrs[i])));
20489 break;
55f1336d 20490 case DW_FORM_ref_sig8:
ac9ec31b
DE
20491 fprintf_unfiltered (f, "signature: %s",
20492 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20493 break;
c906108c 20494 case DW_FORM_string:
4bdf3d34 20495 case DW_FORM_strp:
43988095 20496 case DW_FORM_line_strp:
3019eac3 20497 case DW_FORM_GNU_str_index:
36586728 20498 case DW_FORM_GNU_strp_alt:
8285870a 20499 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20500 DW_STRING (&die->attrs[i])
8285870a
JK
20501 ? DW_STRING (&die->attrs[i]) : "",
20502 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20503 break;
20504 case DW_FORM_flag:
20505 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20506 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20507 else
d97bc12b 20508 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20509 break;
2dc7f7b3
TT
20510 case DW_FORM_flag_present:
20511 fprintf_unfiltered (f, "flag: TRUE");
20512 break;
a8329558 20513 case DW_FORM_indirect:
0963b4bd
MS
20514 /* The reader will have reduced the indirect form to
20515 the "base form" so this form should not occur. */
3e43a32a
MS
20516 fprintf_unfiltered (f,
20517 "unexpected attribute form: DW_FORM_indirect");
a8329558 20518 break;
663c44ac
JK
20519 case DW_FORM_implicit_const:
20520 fprintf_unfiltered (f, "constant: %s",
20521 plongest (DW_SND (&die->attrs[i])));
20522 break;
c906108c 20523 default:
d97bc12b 20524 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20525 die->attrs[i].form);
d97bc12b 20526 break;
c906108c 20527 }
d97bc12b 20528 fprintf_unfiltered (f, "\n");
c906108c
SS
20529 }
20530}
20531
f9aca02d 20532static void
d97bc12b 20533dump_die_for_error (struct die_info *die)
c906108c 20534{
d97bc12b
DE
20535 dump_die_shallow (gdb_stderr, 0, die);
20536}
20537
20538static void
20539dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20540{
20541 int indent = level * 4;
20542
20543 gdb_assert (die != NULL);
20544
20545 if (level >= max_level)
20546 return;
20547
20548 dump_die_shallow (f, indent, die);
20549
20550 if (die->child != NULL)
c906108c 20551 {
d97bc12b
DE
20552 print_spaces (indent, f);
20553 fprintf_unfiltered (f, " Children:");
20554 if (level + 1 < max_level)
20555 {
20556 fprintf_unfiltered (f, "\n");
20557 dump_die_1 (f, level + 1, max_level, die->child);
20558 }
20559 else
20560 {
3e43a32a
MS
20561 fprintf_unfiltered (f,
20562 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20563 }
20564 }
20565
20566 if (die->sibling != NULL && level > 0)
20567 {
20568 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20569 }
20570}
20571
d97bc12b
DE
20572/* This is called from the pdie macro in gdbinit.in.
20573 It's not static so gcc will keep a copy callable from gdb. */
20574
20575void
20576dump_die (struct die_info *die, int max_level)
20577{
20578 dump_die_1 (gdb_stdlog, 0, max_level, die);
20579}
20580
f9aca02d 20581static void
51545339 20582store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20583{
51545339 20584 void **slot;
c906108c 20585
9c541725
PA
20586 slot = htab_find_slot_with_hash (cu->die_hash, die,
20587 to_underlying (die->sect_off),
b64f50a1 20588 INSERT);
51545339
DJ
20589
20590 *slot = die;
c906108c
SS
20591}
20592
b64f50a1
JK
20593/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20594 required kind. */
20595
20596static sect_offset
ff39bb5e 20597dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20598{
7771576e 20599 if (attr_form_is_ref (attr))
9c541725 20600 return (sect_offset) DW_UNSND (attr);
93311388
DE
20601
20602 complaint (&symfile_complaints,
20603 _("unsupported die ref attribute form: '%s'"),
20604 dwarf_form_name (attr->form));
9c541725 20605 return {};
c906108c
SS
20606}
20607
43bbcdc2
PH
20608/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20609 * the value held by the attribute is not constant. */
a02abb62 20610
43bbcdc2 20611static LONGEST
ff39bb5e 20612dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20613{
663c44ac 20614 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20615 return DW_SND (attr);
20616 else if (attr->form == DW_FORM_udata
20617 || attr->form == DW_FORM_data1
20618 || attr->form == DW_FORM_data2
20619 || attr->form == DW_FORM_data4
20620 || attr->form == DW_FORM_data8)
20621 return DW_UNSND (attr);
20622 else
20623 {
0224619f 20624 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20625 complaint (&symfile_complaints,
20626 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20627 dwarf_form_name (attr->form));
20628 return default_value;
20629 }
20630}
20631
348e048f
DE
20632/* Follow reference or signature attribute ATTR of SRC_DIE.
20633 On entry *REF_CU is the CU of SRC_DIE.
20634 On exit *REF_CU is the CU of the result. */
20635
20636static struct die_info *
ff39bb5e 20637follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20638 struct dwarf2_cu **ref_cu)
20639{
20640 struct die_info *die;
20641
7771576e 20642 if (attr_form_is_ref (attr))
348e048f 20643 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20644 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20645 die = follow_die_sig (src_die, attr, ref_cu);
20646 else
20647 {
20648 dump_die_for_error (src_die);
20649 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20650 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20651 }
20652
20653 return die;
03dd20cc
DJ
20654}
20655
5c631832 20656/* Follow reference OFFSET.
673bfd45
DE
20657 On entry *REF_CU is the CU of the source die referencing OFFSET.
20658 On exit *REF_CU is the CU of the result.
20659 Returns NULL if OFFSET is invalid. */
f504f079 20660
f9aca02d 20661static struct die_info *
9c541725 20662follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20663 struct dwarf2_cu **ref_cu)
c906108c 20664{
10b3939b 20665 struct die_info temp_die;
f2f0e013 20666 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20667
348e048f
DE
20668 gdb_assert (cu->per_cu != NULL);
20669
98bfdba5
PA
20670 target_cu = cu;
20671
3019eac3 20672 if (cu->per_cu->is_debug_types)
348e048f
DE
20673 {
20674 /* .debug_types CUs cannot reference anything outside their CU.
20675 If they need to, they have to reference a signatured type via
55f1336d 20676 DW_FORM_ref_sig8. */
9c541725 20677 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20678 return NULL;
348e048f 20679 }
36586728 20680 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20681 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20682 {
20683 struct dwarf2_per_cu_data *per_cu;
9a619af0 20684
9c541725 20685 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20686 cu->objfile);
03dd20cc
DJ
20687
20688 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20689 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20690 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20691
10b3939b
DJ
20692 target_cu = per_cu->cu;
20693 }
98bfdba5
PA
20694 else if (cu->dies == NULL)
20695 {
20696 /* We're loading full DIEs during partial symbol reading. */
20697 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20698 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20699 }
c906108c 20700
f2f0e013 20701 *ref_cu = target_cu;
9c541725 20702 temp_die.sect_off = sect_off;
9a3c8263 20703 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20704 &temp_die,
20705 to_underlying (sect_off));
5c631832 20706}
10b3939b 20707
5c631832
JK
20708/* Follow reference attribute ATTR of SRC_DIE.
20709 On entry *REF_CU is the CU of SRC_DIE.
20710 On exit *REF_CU is the CU of the result. */
20711
20712static struct die_info *
ff39bb5e 20713follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20714 struct dwarf2_cu **ref_cu)
20715{
9c541725 20716 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20717 struct dwarf2_cu *cu = *ref_cu;
20718 struct die_info *die;
20719
9c541725 20720 die = follow_die_offset (sect_off,
36586728
TT
20721 (attr->form == DW_FORM_GNU_ref_alt
20722 || cu->per_cu->is_dwz),
20723 ref_cu);
5c631832
JK
20724 if (!die)
20725 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20726 "at 0x%x [in module %s]"),
9c541725 20727 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20728 objfile_name (cu->objfile));
348e048f 20729
5c631832
JK
20730 return die;
20731}
20732
9c541725 20733/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20734 Returned value is intended for DW_OP_call*. Returned
20735 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20736
20737struct dwarf2_locexpr_baton
9c541725 20738dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20739 struct dwarf2_per_cu_data *per_cu,
20740 CORE_ADDR (*get_frame_pc) (void *baton),
20741 void *baton)
5c631832 20742{
918dd910 20743 struct dwarf2_cu *cu;
5c631832
JK
20744 struct die_info *die;
20745 struct attribute *attr;
20746 struct dwarf2_locexpr_baton retval;
20747
8cf6f0b1
TT
20748 dw2_setup (per_cu->objfile);
20749
918dd910
JK
20750 if (per_cu->cu == NULL)
20751 load_cu (per_cu);
20752 cu = per_cu->cu;
cc12ce38
DE
20753 if (cu == NULL)
20754 {
20755 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20756 Instead just throw an error, not much else we can do. */
20757 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20758 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20759 }
918dd910 20760
9c541725 20761 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20762 if (!die)
20763 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20764 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20765
20766 attr = dwarf2_attr (die, DW_AT_location, cu);
20767 if (!attr)
20768 {
e103e986
JK
20769 /* DWARF: "If there is no such attribute, then there is no effect.".
20770 DATA is ignored if SIZE is 0. */
5c631832 20771
e103e986 20772 retval.data = NULL;
5c631832
JK
20773 retval.size = 0;
20774 }
8cf6f0b1
TT
20775 else if (attr_form_is_section_offset (attr))
20776 {
20777 struct dwarf2_loclist_baton loclist_baton;
20778 CORE_ADDR pc = (*get_frame_pc) (baton);
20779 size_t size;
20780
20781 fill_in_loclist_baton (cu, &loclist_baton, attr);
20782
20783 retval.data = dwarf2_find_location_expression (&loclist_baton,
20784 &size, pc);
20785 retval.size = size;
20786 }
5c631832
JK
20787 else
20788 {
20789 if (!attr_form_is_block (attr))
20790 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20791 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20792 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20793
20794 retval.data = DW_BLOCK (attr)->data;
20795 retval.size = DW_BLOCK (attr)->size;
20796 }
20797 retval.per_cu = cu->per_cu;
918dd910 20798
918dd910
JK
20799 age_cached_comp_units ();
20800
5c631832 20801 return retval;
348e048f
DE
20802}
20803
8b9737bf
TT
20804/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20805 offset. */
20806
20807struct dwarf2_locexpr_baton
20808dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20809 struct dwarf2_per_cu_data *per_cu,
20810 CORE_ADDR (*get_frame_pc) (void *baton),
20811 void *baton)
20812{
9c541725 20813 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20814
9c541725 20815 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20816}
20817
b6807d98
TT
20818/* Write a constant of a given type as target-ordered bytes into
20819 OBSTACK. */
20820
20821static const gdb_byte *
20822write_constant_as_bytes (struct obstack *obstack,
20823 enum bfd_endian byte_order,
20824 struct type *type,
20825 ULONGEST value,
20826 LONGEST *len)
20827{
20828 gdb_byte *result;
20829
20830 *len = TYPE_LENGTH (type);
224c3ddb 20831 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20832 store_unsigned_integer (result, *len, byte_order, value);
20833
20834 return result;
20835}
20836
20837/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20838 pointer to the constant bytes and set LEN to the length of the
20839 data. If memory is needed, allocate it on OBSTACK. If the DIE
20840 does not have a DW_AT_const_value, return NULL. */
20841
20842const gdb_byte *
9c541725 20843dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20844 struct dwarf2_per_cu_data *per_cu,
20845 struct obstack *obstack,
20846 LONGEST *len)
20847{
20848 struct dwarf2_cu *cu;
20849 struct die_info *die;
20850 struct attribute *attr;
20851 const gdb_byte *result = NULL;
20852 struct type *type;
20853 LONGEST value;
20854 enum bfd_endian byte_order;
20855
20856 dw2_setup (per_cu->objfile);
20857
20858 if (per_cu->cu == NULL)
20859 load_cu (per_cu);
20860 cu = per_cu->cu;
cc12ce38
DE
20861 if (cu == NULL)
20862 {
20863 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20864 Instead just throw an error, not much else we can do. */
20865 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20866 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20867 }
b6807d98 20868
9c541725 20869 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20870 if (!die)
20871 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20872 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20873
20874
20875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20876 if (attr == NULL)
20877 return NULL;
20878
20879 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20880 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20881
20882 switch (attr->form)
20883 {
20884 case DW_FORM_addr:
20885 case DW_FORM_GNU_addr_index:
20886 {
20887 gdb_byte *tem;
20888
20889 *len = cu->header.addr_size;
224c3ddb 20890 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20891 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20892 result = tem;
20893 }
20894 break;
20895 case DW_FORM_string:
20896 case DW_FORM_strp:
20897 case DW_FORM_GNU_str_index:
20898 case DW_FORM_GNU_strp_alt:
20899 /* DW_STRING is already allocated on the objfile obstack, point
20900 directly to it. */
20901 result = (const gdb_byte *) DW_STRING (attr);
20902 *len = strlen (DW_STRING (attr));
20903 break;
20904 case DW_FORM_block1:
20905 case DW_FORM_block2:
20906 case DW_FORM_block4:
20907 case DW_FORM_block:
20908 case DW_FORM_exprloc:
0224619f 20909 case DW_FORM_data16:
b6807d98
TT
20910 result = DW_BLOCK (attr)->data;
20911 *len = DW_BLOCK (attr)->size;
20912 break;
20913
20914 /* The DW_AT_const_value attributes are supposed to carry the
20915 symbol's value "represented as it would be on the target
20916 architecture." By the time we get here, it's already been
20917 converted to host endianness, so we just need to sign- or
20918 zero-extend it as appropriate. */
20919 case DW_FORM_data1:
20920 type = die_type (die, cu);
20921 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20922 if (result == NULL)
20923 result = write_constant_as_bytes (obstack, byte_order,
20924 type, value, len);
20925 break;
20926 case DW_FORM_data2:
20927 type = die_type (die, cu);
20928 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20929 if (result == NULL)
20930 result = write_constant_as_bytes (obstack, byte_order,
20931 type, value, len);
20932 break;
20933 case DW_FORM_data4:
20934 type = die_type (die, cu);
20935 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20936 if (result == NULL)
20937 result = write_constant_as_bytes (obstack, byte_order,
20938 type, value, len);
20939 break;
20940 case DW_FORM_data8:
20941 type = die_type (die, cu);
20942 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20943 if (result == NULL)
20944 result = write_constant_as_bytes (obstack, byte_order,
20945 type, value, len);
20946 break;
20947
20948 case DW_FORM_sdata:
663c44ac 20949 case DW_FORM_implicit_const:
b6807d98
TT
20950 type = die_type (die, cu);
20951 result = write_constant_as_bytes (obstack, byte_order,
20952 type, DW_SND (attr), len);
20953 break;
20954
20955 case DW_FORM_udata:
20956 type = die_type (die, cu);
20957 result = write_constant_as_bytes (obstack, byte_order,
20958 type, DW_UNSND (attr), len);
20959 break;
20960
20961 default:
20962 complaint (&symfile_complaints,
20963 _("unsupported const value attribute form: '%s'"),
20964 dwarf_form_name (attr->form));
20965 break;
20966 }
20967
20968 return result;
20969}
20970
7942e96e
AA
20971/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20972 valid type for this die is found. */
20973
20974struct type *
9c541725 20975dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
20976 struct dwarf2_per_cu_data *per_cu)
20977{
20978 struct dwarf2_cu *cu;
20979 struct die_info *die;
20980
20981 dw2_setup (per_cu->objfile);
20982
20983 if (per_cu->cu == NULL)
20984 load_cu (per_cu);
20985 cu = per_cu->cu;
20986 if (!cu)
20987 return NULL;
20988
9c541725 20989 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
20990 if (!die)
20991 return NULL;
20992
20993 return die_type (die, cu);
20994}
20995
8a9b8146
TT
20996/* Return the type of the DIE at DIE_OFFSET in the CU named by
20997 PER_CU. */
20998
20999struct type *
b64f50a1 21000dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21001 struct dwarf2_per_cu_data *per_cu)
21002{
8a9b8146 21003 dw2_setup (per_cu->objfile);
b64f50a1 21004
9c541725 21005 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21006 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21007}
21008
ac9ec31b 21009/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21010 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21011 On exit *REF_CU is the CU of the result.
21012 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21013
21014static struct die_info *
ac9ec31b
DE
21015follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21016 struct dwarf2_cu **ref_cu)
348e048f 21017{
348e048f 21018 struct die_info temp_die;
348e048f
DE
21019 struct dwarf2_cu *sig_cu;
21020 struct die_info *die;
21021
ac9ec31b
DE
21022 /* While it might be nice to assert sig_type->type == NULL here,
21023 we can get here for DW_AT_imported_declaration where we need
21024 the DIE not the type. */
348e048f
DE
21025
21026 /* If necessary, add it to the queue and load its DIEs. */
21027
95554aad 21028 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21029 read_signatured_type (sig_type);
348e048f 21030
348e048f 21031 sig_cu = sig_type->per_cu.cu;
69d751e3 21032 gdb_assert (sig_cu != NULL);
9c541725
PA
21033 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21034 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21035 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21036 to_underlying (temp_die.sect_off));
348e048f
DE
21037 if (die)
21038 {
796a7ff8
DE
21039 /* For .gdb_index version 7 keep track of included TUs.
21040 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21041 if (dwarf2_per_objfile->index_table != NULL
21042 && dwarf2_per_objfile->index_table->version <= 7)
21043 {
21044 VEC_safe_push (dwarf2_per_cu_ptr,
21045 (*ref_cu)->per_cu->imported_symtabs,
21046 sig_cu->per_cu);
21047 }
21048
348e048f
DE
21049 *ref_cu = sig_cu;
21050 return die;
21051 }
21052
ac9ec31b
DE
21053 return NULL;
21054}
21055
21056/* Follow signatured type referenced by ATTR in SRC_DIE.
21057 On entry *REF_CU is the CU of SRC_DIE.
21058 On exit *REF_CU is the CU of the result.
21059 The result is the DIE of the type.
21060 If the referenced type cannot be found an error is thrown. */
21061
21062static struct die_info *
ff39bb5e 21063follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21064 struct dwarf2_cu **ref_cu)
21065{
21066 ULONGEST signature = DW_SIGNATURE (attr);
21067 struct signatured_type *sig_type;
21068 struct die_info *die;
21069
21070 gdb_assert (attr->form == DW_FORM_ref_sig8);
21071
a2ce51a0 21072 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21073 /* sig_type will be NULL if the signatured type is missing from
21074 the debug info. */
21075 if (sig_type == NULL)
21076 {
21077 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21078 " from DIE at 0x%x [in module %s]"),
9c541725 21079 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21080 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21081 }
21082
21083 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21084 if (die == NULL)
21085 {
21086 dump_die_for_error (src_die);
21087 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21088 " from DIE at 0x%x [in module %s]"),
9c541725 21089 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21090 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21091 }
21092
21093 return die;
21094}
21095
21096/* Get the type specified by SIGNATURE referenced in DIE/CU,
21097 reading in and processing the type unit if necessary. */
21098
21099static struct type *
21100get_signatured_type (struct die_info *die, ULONGEST signature,
21101 struct dwarf2_cu *cu)
21102{
21103 struct signatured_type *sig_type;
21104 struct dwarf2_cu *type_cu;
21105 struct die_info *type_die;
21106 struct type *type;
21107
a2ce51a0 21108 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21109 /* sig_type will be NULL if the signatured type is missing from
21110 the debug info. */
21111 if (sig_type == NULL)
21112 {
21113 complaint (&symfile_complaints,
21114 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21115 " from DIE at 0x%x [in module %s]"),
9c541725 21116 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21117 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21118 return build_error_marker_type (cu, die);
21119 }
21120
21121 /* If we already know the type we're done. */
21122 if (sig_type->type != NULL)
21123 return sig_type->type;
21124
21125 type_cu = cu;
21126 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21127 if (type_die != NULL)
21128 {
21129 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21130 is created. This is important, for example, because for c++ classes
21131 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21132 type = read_type_die (type_die, type_cu);
21133 if (type == NULL)
21134 {
21135 complaint (&symfile_complaints,
21136 _("Dwarf Error: Cannot build signatured type %s"
21137 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21138 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21139 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21140 type = build_error_marker_type (cu, die);
21141 }
21142 }
21143 else
21144 {
21145 complaint (&symfile_complaints,
21146 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21147 " from DIE at 0x%x [in module %s]"),
9c541725 21148 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21149 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21150 type = build_error_marker_type (cu, die);
21151 }
21152 sig_type->type = type;
21153
21154 return type;
21155}
21156
21157/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21158 reading in and processing the type unit if necessary. */
21159
21160static struct type *
ff39bb5e 21161get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21162 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21163{
21164 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21165 if (attr_form_is_ref (attr))
ac9ec31b
DE
21166 {
21167 struct dwarf2_cu *type_cu = cu;
21168 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21169
21170 return read_type_die (type_die, type_cu);
21171 }
21172 else if (attr->form == DW_FORM_ref_sig8)
21173 {
21174 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21175 }
21176 else
21177 {
21178 complaint (&symfile_complaints,
21179 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21180 " at 0x%x [in module %s]"),
9c541725 21181 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21182 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21183 return build_error_marker_type (cu, die);
21184 }
348e048f
DE
21185}
21186
e5fe5e75 21187/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21188
21189static void
e5fe5e75 21190load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21191{
52dc124a 21192 struct signatured_type *sig_type;
348e048f 21193
f4dc4d17
DE
21194 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21195 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21196
6721b2ec
DE
21197 /* We have the per_cu, but we need the signatured_type.
21198 Fortunately this is an easy translation. */
21199 gdb_assert (per_cu->is_debug_types);
21200 sig_type = (struct signatured_type *) per_cu;
348e048f 21201
6721b2ec 21202 gdb_assert (per_cu->cu == NULL);
348e048f 21203
52dc124a 21204 read_signatured_type (sig_type);
348e048f 21205
6721b2ec 21206 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21207}
21208
dee91e82
DE
21209/* die_reader_func for read_signatured_type.
21210 This is identical to load_full_comp_unit_reader,
21211 but is kept separate for now. */
348e048f
DE
21212
21213static void
dee91e82 21214read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21215 const gdb_byte *info_ptr,
dee91e82
DE
21216 struct die_info *comp_unit_die,
21217 int has_children,
21218 void *data)
348e048f 21219{
dee91e82 21220 struct dwarf2_cu *cu = reader->cu;
348e048f 21221
dee91e82
DE
21222 gdb_assert (cu->die_hash == NULL);
21223 cu->die_hash =
21224 htab_create_alloc_ex (cu->header.length / 12,
21225 die_hash,
21226 die_eq,
21227 NULL,
21228 &cu->comp_unit_obstack,
21229 hashtab_obstack_allocate,
21230 dummy_obstack_deallocate);
348e048f 21231
dee91e82
DE
21232 if (has_children)
21233 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21234 &info_ptr, comp_unit_die);
21235 cu->dies = comp_unit_die;
21236 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21237
21238 /* We try not to read any attributes in this function, because not
9cdd5dbd 21239 all CUs needed for references have been loaded yet, and symbol
348e048f 21240 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21241 or we won't be able to build types correctly.
21242 Similarly, if we do not read the producer, we can not apply
21243 producer-specific interpretation. */
95554aad 21244 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21245}
348e048f 21246
3019eac3
DE
21247/* Read in a signatured type and build its CU and DIEs.
21248 If the type is a stub for the real type in a DWO file,
21249 read in the real type from the DWO file as well. */
dee91e82
DE
21250
21251static void
21252read_signatured_type (struct signatured_type *sig_type)
21253{
21254 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21255
3019eac3 21256 gdb_assert (per_cu->is_debug_types);
dee91e82 21257 gdb_assert (per_cu->cu == NULL);
348e048f 21258
f4dc4d17
DE
21259 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21260 read_signatured_type_reader, NULL);
7ee85ab1 21261 sig_type->per_cu.tu_read = 1;
c906108c
SS
21262}
21263
c906108c
SS
21264/* Decode simple location descriptions.
21265 Given a pointer to a dwarf block that defines a location, compute
21266 the location and return the value.
21267
4cecd739
DJ
21268 NOTE drow/2003-11-18: This function is called in two situations
21269 now: for the address of static or global variables (partial symbols
21270 only) and for offsets into structures which are expected to be
21271 (more or less) constant. The partial symbol case should go away,
21272 and only the constant case should remain. That will let this
21273 function complain more accurately. A few special modes are allowed
21274 without complaint for global variables (for instance, global
21275 register values and thread-local values).
c906108c
SS
21276
21277 A location description containing no operations indicates that the
4cecd739 21278 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21279 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21280 callers will only want a very basic result and this can become a
21ae7a4d
JK
21281 complaint.
21282
21283 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21284
21285static CORE_ADDR
e7c27a73 21286decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21287{
e7c27a73 21288 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21289 size_t i;
21290 size_t size = blk->size;
d521ce57 21291 const gdb_byte *data = blk->data;
21ae7a4d
JK
21292 CORE_ADDR stack[64];
21293 int stacki;
21294 unsigned int bytes_read, unsnd;
21295 gdb_byte op;
c906108c 21296
21ae7a4d
JK
21297 i = 0;
21298 stacki = 0;
21299 stack[stacki] = 0;
21300 stack[++stacki] = 0;
21301
21302 while (i < size)
21303 {
21304 op = data[i++];
21305 switch (op)
21306 {
21307 case DW_OP_lit0:
21308 case DW_OP_lit1:
21309 case DW_OP_lit2:
21310 case DW_OP_lit3:
21311 case DW_OP_lit4:
21312 case DW_OP_lit5:
21313 case DW_OP_lit6:
21314 case DW_OP_lit7:
21315 case DW_OP_lit8:
21316 case DW_OP_lit9:
21317 case DW_OP_lit10:
21318 case DW_OP_lit11:
21319 case DW_OP_lit12:
21320 case DW_OP_lit13:
21321 case DW_OP_lit14:
21322 case DW_OP_lit15:
21323 case DW_OP_lit16:
21324 case DW_OP_lit17:
21325 case DW_OP_lit18:
21326 case DW_OP_lit19:
21327 case DW_OP_lit20:
21328 case DW_OP_lit21:
21329 case DW_OP_lit22:
21330 case DW_OP_lit23:
21331 case DW_OP_lit24:
21332 case DW_OP_lit25:
21333 case DW_OP_lit26:
21334 case DW_OP_lit27:
21335 case DW_OP_lit28:
21336 case DW_OP_lit29:
21337 case DW_OP_lit30:
21338 case DW_OP_lit31:
21339 stack[++stacki] = op - DW_OP_lit0;
21340 break;
f1bea926 21341
21ae7a4d
JK
21342 case DW_OP_reg0:
21343 case DW_OP_reg1:
21344 case DW_OP_reg2:
21345 case DW_OP_reg3:
21346 case DW_OP_reg4:
21347 case DW_OP_reg5:
21348 case DW_OP_reg6:
21349 case DW_OP_reg7:
21350 case DW_OP_reg8:
21351 case DW_OP_reg9:
21352 case DW_OP_reg10:
21353 case DW_OP_reg11:
21354 case DW_OP_reg12:
21355 case DW_OP_reg13:
21356 case DW_OP_reg14:
21357 case DW_OP_reg15:
21358 case DW_OP_reg16:
21359 case DW_OP_reg17:
21360 case DW_OP_reg18:
21361 case DW_OP_reg19:
21362 case DW_OP_reg20:
21363 case DW_OP_reg21:
21364 case DW_OP_reg22:
21365 case DW_OP_reg23:
21366 case DW_OP_reg24:
21367 case DW_OP_reg25:
21368 case DW_OP_reg26:
21369 case DW_OP_reg27:
21370 case DW_OP_reg28:
21371 case DW_OP_reg29:
21372 case DW_OP_reg30:
21373 case DW_OP_reg31:
21374 stack[++stacki] = op - DW_OP_reg0;
21375 if (i < size)
21376 dwarf2_complex_location_expr_complaint ();
21377 break;
c906108c 21378
21ae7a4d
JK
21379 case DW_OP_regx:
21380 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21381 i += bytes_read;
21382 stack[++stacki] = unsnd;
21383 if (i < size)
21384 dwarf2_complex_location_expr_complaint ();
21385 break;
c906108c 21386
21ae7a4d
JK
21387 case DW_OP_addr:
21388 stack[++stacki] = read_address (objfile->obfd, &data[i],
21389 cu, &bytes_read);
21390 i += bytes_read;
21391 break;
d53d4ac5 21392
21ae7a4d
JK
21393 case DW_OP_const1u:
21394 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21395 i += 1;
21396 break;
21397
21398 case DW_OP_const1s:
21399 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21400 i += 1;
21401 break;
21402
21403 case DW_OP_const2u:
21404 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21405 i += 2;
21406 break;
21407
21408 case DW_OP_const2s:
21409 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21410 i += 2;
21411 break;
d53d4ac5 21412
21ae7a4d
JK
21413 case DW_OP_const4u:
21414 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21415 i += 4;
21416 break;
21417
21418 case DW_OP_const4s:
21419 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21420 i += 4;
21421 break;
21422
585861ea
JK
21423 case DW_OP_const8u:
21424 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21425 i += 8;
21426 break;
21427
21ae7a4d
JK
21428 case DW_OP_constu:
21429 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21430 &bytes_read);
21431 i += bytes_read;
21432 break;
21433
21434 case DW_OP_consts:
21435 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21436 i += bytes_read;
21437 break;
21438
21439 case DW_OP_dup:
21440 stack[stacki + 1] = stack[stacki];
21441 stacki++;
21442 break;
21443
21444 case DW_OP_plus:
21445 stack[stacki - 1] += stack[stacki];
21446 stacki--;
21447 break;
21448
21449 case DW_OP_plus_uconst:
21450 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21451 &bytes_read);
21452 i += bytes_read;
21453 break;
21454
21455 case DW_OP_minus:
21456 stack[stacki - 1] -= stack[stacki];
21457 stacki--;
21458 break;
21459
21460 case DW_OP_deref:
21461 /* If we're not the last op, then we definitely can't encode
21462 this using GDB's address_class enum. This is valid for partial
21463 global symbols, although the variable's address will be bogus
21464 in the psymtab. */
21465 if (i < size)
21466 dwarf2_complex_location_expr_complaint ();
21467 break;
21468
21469 case DW_OP_GNU_push_tls_address:
4aa4e28b 21470 case DW_OP_form_tls_address:
21ae7a4d
JK
21471 /* The top of the stack has the offset from the beginning
21472 of the thread control block at which the variable is located. */
21473 /* Nothing should follow this operator, so the top of stack would
21474 be returned. */
21475 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21476 address will be bogus in the psymtab. Make it always at least
21477 non-zero to not look as a variable garbage collected by linker
21478 which have DW_OP_addr 0. */
21ae7a4d
JK
21479 if (i < size)
21480 dwarf2_complex_location_expr_complaint ();
585861ea 21481 stack[stacki]++;
21ae7a4d
JK
21482 break;
21483
21484 case DW_OP_GNU_uninit:
21485 break;
21486
3019eac3 21487 case DW_OP_GNU_addr_index:
49f6c839 21488 case DW_OP_GNU_const_index:
3019eac3
DE
21489 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21490 &bytes_read);
21491 i += bytes_read;
21492 break;
21493
21ae7a4d
JK
21494 default:
21495 {
f39c6ffd 21496 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21497
21498 if (name)
21499 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21500 name);
21501 else
21502 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21503 op);
21504 }
21505
21506 return (stack[stacki]);
d53d4ac5 21507 }
3c6e0cb3 21508
21ae7a4d
JK
21509 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21510 outside of the allocated space. Also enforce minimum>0. */
21511 if (stacki >= ARRAY_SIZE (stack) - 1)
21512 {
21513 complaint (&symfile_complaints,
21514 _("location description stack overflow"));
21515 return 0;
21516 }
21517
21518 if (stacki <= 0)
21519 {
21520 complaint (&symfile_complaints,
21521 _("location description stack underflow"));
21522 return 0;
21523 }
21524 }
21525 return (stack[stacki]);
c906108c
SS
21526}
21527
21528/* memory allocation interface */
21529
c906108c 21530static struct dwarf_block *
7b5a2f43 21531dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21532{
8d749320 21533 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21534}
21535
c906108c 21536static struct die_info *
b60c80d6 21537dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21538{
21539 struct die_info *die;
b60c80d6
DJ
21540 size_t size = sizeof (struct die_info);
21541
21542 if (num_attrs > 1)
21543 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21544
b60c80d6 21545 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21546 memset (die, 0, sizeof (struct die_info));
21547 return (die);
21548}
2e276125
JB
21549
21550\f
21551/* Macro support. */
21552
233d95b5
JK
21553/* Return file name relative to the compilation directory of file number I in
21554 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21555 responsible for freeing it. */
233d95b5 21556
2e276125 21557static char *
233d95b5 21558file_file_name (int file, struct line_header *lh)
2e276125 21559{
6a83a1e6
EZ
21560 /* Is the file number a valid index into the line header's file name
21561 table? Remember that file numbers start with one, not zero. */
fff8551c 21562 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21563 {
8c43009f 21564 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21565
8c43009f
PA
21566 if (!IS_ABSOLUTE_PATH (fe.name))
21567 {
21568 const char *dir = fe.include_dir (lh);
21569 if (dir != NULL)
21570 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21571 }
21572 return xstrdup (fe.name);
6a83a1e6 21573 }
2e276125
JB
21574 else
21575 {
6a83a1e6
EZ
21576 /* The compiler produced a bogus file number. We can at least
21577 record the macro definitions made in the file, even if we
21578 won't be able to find the file by name. */
21579 char fake_name[80];
9a619af0 21580
8c042590
PM
21581 xsnprintf (fake_name, sizeof (fake_name),
21582 "<bad macro file number %d>", file);
2e276125 21583
6e70227d 21584 complaint (&symfile_complaints,
6a83a1e6
EZ
21585 _("bad file number in macro information (%d)"),
21586 file);
2e276125 21587
6a83a1e6 21588 return xstrdup (fake_name);
2e276125
JB
21589 }
21590}
21591
233d95b5
JK
21592/* Return the full name of file number I in *LH's file name table.
21593 Use COMP_DIR as the name of the current directory of the
21594 compilation. The result is allocated using xmalloc; the caller is
21595 responsible for freeing it. */
21596static char *
21597file_full_name (int file, struct line_header *lh, const char *comp_dir)
21598{
21599 /* Is the file number a valid index into the line header's file name
21600 table? Remember that file numbers start with one, not zero. */
fff8551c 21601 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21602 {
21603 char *relative = file_file_name (file, lh);
21604
21605 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21606 return relative;
b36cec19
PA
21607 return reconcat (relative, comp_dir, SLASH_STRING,
21608 relative, (char *) NULL);
233d95b5
JK
21609 }
21610 else
21611 return file_file_name (file, lh);
21612}
21613
2e276125
JB
21614
21615static struct macro_source_file *
21616macro_start_file (int file, int line,
21617 struct macro_source_file *current_file,
43f3e411 21618 struct line_header *lh)
2e276125 21619{
233d95b5
JK
21620 /* File name relative to the compilation directory of this source file. */
21621 char *file_name = file_file_name (file, lh);
2e276125 21622
2e276125 21623 if (! current_file)
abc9d0dc 21624 {
fc474241
DE
21625 /* Note: We don't create a macro table for this compilation unit
21626 at all until we actually get a filename. */
43f3e411 21627 struct macro_table *macro_table = get_macro_table ();
fc474241 21628
abc9d0dc
TT
21629 /* If we have no current file, then this must be the start_file
21630 directive for the compilation unit's main source file. */
fc474241
DE
21631 current_file = macro_set_main (macro_table, file_name);
21632 macro_define_special (macro_table);
abc9d0dc 21633 }
2e276125 21634 else
233d95b5 21635 current_file = macro_include (current_file, line, file_name);
2e276125 21636
233d95b5 21637 xfree (file_name);
6e70227d 21638
2e276125
JB
21639 return current_file;
21640}
21641
2e276125
JB
21642static const char *
21643consume_improper_spaces (const char *p, const char *body)
21644{
21645 if (*p == ' ')
21646 {
4d3c2250 21647 complaint (&symfile_complaints,
3e43a32a
MS
21648 _("macro definition contains spaces "
21649 "in formal argument list:\n`%s'"),
4d3c2250 21650 body);
2e276125
JB
21651
21652 while (*p == ' ')
21653 p++;
21654 }
21655
21656 return p;
21657}
21658
21659
21660static void
21661parse_macro_definition (struct macro_source_file *file, int line,
21662 const char *body)
21663{
21664 const char *p;
21665
21666 /* The body string takes one of two forms. For object-like macro
21667 definitions, it should be:
21668
21669 <macro name> " " <definition>
21670
21671 For function-like macro definitions, it should be:
21672
21673 <macro name> "() " <definition>
21674 or
21675 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21676
21677 Spaces may appear only where explicitly indicated, and in the
21678 <definition>.
21679
21680 The Dwarf 2 spec says that an object-like macro's name is always
21681 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21682 the space when the macro's definition is the empty string.
2e276125
JB
21683
21684 The Dwarf 2 spec says that there should be no spaces between the
21685 formal arguments in a function-like macro's formal argument list,
21686 but versions of GCC around March 2002 include spaces after the
21687 commas. */
21688
21689
21690 /* Find the extent of the macro name. The macro name is terminated
21691 by either a space or null character (for an object-like macro) or
21692 an opening paren (for a function-like macro). */
21693 for (p = body; *p; p++)
21694 if (*p == ' ' || *p == '(')
21695 break;
21696
21697 if (*p == ' ' || *p == '\0')
21698 {
21699 /* It's an object-like macro. */
21700 int name_len = p - body;
3f8a7804 21701 char *name = savestring (body, name_len);
2e276125
JB
21702 const char *replacement;
21703
21704 if (*p == ' ')
21705 replacement = body + name_len + 1;
21706 else
21707 {
4d3c2250 21708 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21709 replacement = body + name_len;
21710 }
6e70227d 21711
2e276125
JB
21712 macro_define_object (file, line, name, replacement);
21713
21714 xfree (name);
21715 }
21716 else if (*p == '(')
21717 {
21718 /* It's a function-like macro. */
3f8a7804 21719 char *name = savestring (body, p - body);
2e276125
JB
21720 int argc = 0;
21721 int argv_size = 1;
8d749320 21722 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21723
21724 p++;
21725
21726 p = consume_improper_spaces (p, body);
21727
21728 /* Parse the formal argument list. */
21729 while (*p && *p != ')')
21730 {
21731 /* Find the extent of the current argument name. */
21732 const char *arg_start = p;
21733
21734 while (*p && *p != ',' && *p != ')' && *p != ' ')
21735 p++;
21736
21737 if (! *p || p == arg_start)
4d3c2250 21738 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21739 else
21740 {
21741 /* Make sure argv has room for the new argument. */
21742 if (argc >= argv_size)
21743 {
21744 argv_size *= 2;
224c3ddb 21745 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21746 }
21747
3f8a7804 21748 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
21749 }
21750
21751 p = consume_improper_spaces (p, body);
21752
21753 /* Consume the comma, if present. */
21754 if (*p == ',')
21755 {
21756 p++;
21757
21758 p = consume_improper_spaces (p, body);
21759 }
21760 }
21761
21762 if (*p == ')')
21763 {
21764 p++;
21765
21766 if (*p == ' ')
21767 /* Perfectly formed definition, no complaints. */
21768 macro_define_function (file, line, name,
6e70227d 21769 argc, (const char **) argv,
2e276125
JB
21770 p + 1);
21771 else if (*p == '\0')
21772 {
21773 /* Complain, but do define it. */
4d3c2250 21774 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21775 macro_define_function (file, line, name,
6e70227d 21776 argc, (const char **) argv,
2e276125
JB
21777 p);
21778 }
21779 else
21780 /* Just complain. */
4d3c2250 21781 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21782 }
21783 else
21784 /* Just complain. */
4d3c2250 21785 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21786
21787 xfree (name);
21788 {
21789 int i;
21790
21791 for (i = 0; i < argc; i++)
21792 xfree (argv[i]);
21793 }
21794 xfree (argv);
21795 }
21796 else
4d3c2250 21797 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21798}
21799
cf2c3c16
TT
21800/* Skip some bytes from BYTES according to the form given in FORM.
21801 Returns the new pointer. */
2e276125 21802
d521ce57
TT
21803static const gdb_byte *
21804skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21805 enum dwarf_form form,
21806 unsigned int offset_size,
21807 struct dwarf2_section_info *section)
2e276125 21808{
cf2c3c16 21809 unsigned int bytes_read;
2e276125 21810
cf2c3c16 21811 switch (form)
2e276125 21812 {
cf2c3c16
TT
21813 case DW_FORM_data1:
21814 case DW_FORM_flag:
21815 ++bytes;
21816 break;
21817
21818 case DW_FORM_data2:
21819 bytes += 2;
21820 break;
21821
21822 case DW_FORM_data4:
21823 bytes += 4;
21824 break;
21825
21826 case DW_FORM_data8:
21827 bytes += 8;
21828 break;
21829
0224619f
JK
21830 case DW_FORM_data16:
21831 bytes += 16;
21832 break;
21833
cf2c3c16
TT
21834 case DW_FORM_string:
21835 read_direct_string (abfd, bytes, &bytes_read);
21836 bytes += bytes_read;
21837 break;
21838
21839 case DW_FORM_sec_offset:
21840 case DW_FORM_strp:
36586728 21841 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21842 bytes += offset_size;
21843 break;
21844
21845 case DW_FORM_block:
21846 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21847 bytes += bytes_read;
21848 break;
21849
21850 case DW_FORM_block1:
21851 bytes += 1 + read_1_byte (abfd, bytes);
21852 break;
21853 case DW_FORM_block2:
21854 bytes += 2 + read_2_bytes (abfd, bytes);
21855 break;
21856 case DW_FORM_block4:
21857 bytes += 4 + read_4_bytes (abfd, bytes);
21858 break;
21859
21860 case DW_FORM_sdata:
21861 case DW_FORM_udata:
3019eac3
DE
21862 case DW_FORM_GNU_addr_index:
21863 case DW_FORM_GNU_str_index:
d521ce57 21864 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21865 if (bytes == NULL)
21866 {
21867 dwarf2_section_buffer_overflow_complaint (section);
21868 return NULL;
21869 }
cf2c3c16
TT
21870 break;
21871
663c44ac
JK
21872 case DW_FORM_implicit_const:
21873 break;
21874
cf2c3c16
TT
21875 default:
21876 {
21877 complain:
21878 complaint (&symfile_complaints,
21879 _("invalid form 0x%x in `%s'"),
a32a8923 21880 form, get_section_name (section));
cf2c3c16
TT
21881 return NULL;
21882 }
2e276125
JB
21883 }
21884
cf2c3c16
TT
21885 return bytes;
21886}
757a13d0 21887
cf2c3c16
TT
21888/* A helper for dwarf_decode_macros that handles skipping an unknown
21889 opcode. Returns an updated pointer to the macro data buffer; or,
21890 on error, issues a complaint and returns NULL. */
757a13d0 21891
d521ce57 21892static const gdb_byte *
cf2c3c16 21893skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21894 const gdb_byte **opcode_definitions,
21895 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21896 bfd *abfd,
21897 unsigned int offset_size,
21898 struct dwarf2_section_info *section)
21899{
21900 unsigned int bytes_read, i;
21901 unsigned long arg;
d521ce57 21902 const gdb_byte *defn;
2e276125 21903
cf2c3c16 21904 if (opcode_definitions[opcode] == NULL)
2e276125 21905 {
cf2c3c16
TT
21906 complaint (&symfile_complaints,
21907 _("unrecognized DW_MACFINO opcode 0x%x"),
21908 opcode);
21909 return NULL;
21910 }
2e276125 21911
cf2c3c16
TT
21912 defn = opcode_definitions[opcode];
21913 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21914 defn += bytes_read;
2e276125 21915
cf2c3c16
TT
21916 for (i = 0; i < arg; ++i)
21917 {
aead7601
SM
21918 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21919 (enum dwarf_form) defn[i], offset_size,
f664829e 21920 section);
cf2c3c16
TT
21921 if (mac_ptr == NULL)
21922 {
21923 /* skip_form_bytes already issued the complaint. */
21924 return NULL;
21925 }
21926 }
757a13d0 21927
cf2c3c16
TT
21928 return mac_ptr;
21929}
757a13d0 21930
cf2c3c16
TT
21931/* A helper function which parses the header of a macro section.
21932 If the macro section is the extended (for now called "GNU") type,
21933 then this updates *OFFSET_SIZE. Returns a pointer to just after
21934 the header, or issues a complaint and returns NULL on error. */
757a13d0 21935
d521ce57
TT
21936static const gdb_byte *
21937dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21938 bfd *abfd,
d521ce57 21939 const gdb_byte *mac_ptr,
cf2c3c16
TT
21940 unsigned int *offset_size,
21941 int section_is_gnu)
21942{
21943 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21944
cf2c3c16
TT
21945 if (section_is_gnu)
21946 {
21947 unsigned int version, flags;
757a13d0 21948
cf2c3c16 21949 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21950 if (version != 4 && version != 5)
cf2c3c16
TT
21951 {
21952 complaint (&symfile_complaints,
21953 _("unrecognized version `%d' in .debug_macro section"),
21954 version);
21955 return NULL;
21956 }
21957 mac_ptr += 2;
757a13d0 21958
cf2c3c16
TT
21959 flags = read_1_byte (abfd, mac_ptr);
21960 ++mac_ptr;
21961 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21962
cf2c3c16
TT
21963 if ((flags & 2) != 0)
21964 /* We don't need the line table offset. */
21965 mac_ptr += *offset_size;
757a13d0 21966
cf2c3c16
TT
21967 /* Vendor opcode descriptions. */
21968 if ((flags & 4) != 0)
21969 {
21970 unsigned int i, count;
757a13d0 21971
cf2c3c16
TT
21972 count = read_1_byte (abfd, mac_ptr);
21973 ++mac_ptr;
21974 for (i = 0; i < count; ++i)
21975 {
21976 unsigned int opcode, bytes_read;
21977 unsigned long arg;
21978
21979 opcode = read_1_byte (abfd, mac_ptr);
21980 ++mac_ptr;
21981 opcode_definitions[opcode] = mac_ptr;
21982 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21983 mac_ptr += bytes_read;
21984 mac_ptr += arg;
21985 }
757a13d0 21986 }
cf2c3c16 21987 }
757a13d0 21988
cf2c3c16
TT
21989 return mac_ptr;
21990}
757a13d0 21991
cf2c3c16 21992/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21993 including DW_MACRO_import. */
cf2c3c16
TT
21994
21995static void
d521ce57
TT
21996dwarf_decode_macro_bytes (bfd *abfd,
21997 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21998 struct macro_source_file *current_file,
43f3e411 21999 struct line_header *lh,
cf2c3c16 22000 struct dwarf2_section_info *section,
36586728 22001 int section_is_gnu, int section_is_dwz,
cf2c3c16 22002 unsigned int offset_size,
8fc3fc34 22003 htab_t include_hash)
cf2c3c16 22004{
4d663531 22005 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22006 enum dwarf_macro_record_type macinfo_type;
22007 int at_commandline;
d521ce57 22008 const gdb_byte *opcode_definitions[256];
757a13d0 22009
cf2c3c16
TT
22010 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22011 &offset_size, section_is_gnu);
22012 if (mac_ptr == NULL)
22013 {
22014 /* We already issued a complaint. */
22015 return;
22016 }
757a13d0
JK
22017
22018 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22019 GDB is still reading the definitions from command line. First
22020 DW_MACINFO_start_file will need to be ignored as it was already executed
22021 to create CURRENT_FILE for the main source holding also the command line
22022 definitions. On first met DW_MACINFO_start_file this flag is reset to
22023 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22024
22025 at_commandline = 1;
22026
22027 do
22028 {
22029 /* Do we at least have room for a macinfo type byte? */
22030 if (mac_ptr >= mac_end)
22031 {
f664829e 22032 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22033 break;
22034 }
22035
aead7601 22036 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22037 mac_ptr++;
22038
cf2c3c16
TT
22039 /* Note that we rely on the fact that the corresponding GNU and
22040 DWARF constants are the same. */
757a13d0
JK
22041 switch (macinfo_type)
22042 {
22043 /* A zero macinfo type indicates the end of the macro
22044 information. */
22045 case 0:
22046 break;
2e276125 22047
0af92d60
JK
22048 case DW_MACRO_define:
22049 case DW_MACRO_undef:
22050 case DW_MACRO_define_strp:
22051 case DW_MACRO_undef_strp:
22052 case DW_MACRO_define_sup:
22053 case DW_MACRO_undef_sup:
2e276125 22054 {
891d2f0b 22055 unsigned int bytes_read;
2e276125 22056 int line;
d521ce57 22057 const char *body;
cf2c3c16 22058 int is_define;
2e276125 22059
cf2c3c16
TT
22060 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22061 mac_ptr += bytes_read;
22062
0af92d60
JK
22063 if (macinfo_type == DW_MACRO_define
22064 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22065 {
22066 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22067 mac_ptr += bytes_read;
22068 }
22069 else
22070 {
22071 LONGEST str_offset;
22072
22073 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22074 mac_ptr += offset_size;
2e276125 22075
0af92d60
JK
22076 if (macinfo_type == DW_MACRO_define_sup
22077 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22078 || section_is_dwz)
36586728
TT
22079 {
22080 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22081
22082 body = read_indirect_string_from_dwz (dwz, str_offset);
22083 }
22084 else
22085 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22086 }
22087
0af92d60
JK
22088 is_define = (macinfo_type == DW_MACRO_define
22089 || macinfo_type == DW_MACRO_define_strp
22090 || macinfo_type == DW_MACRO_define_sup);
2e276125 22091 if (! current_file)
757a13d0
JK
22092 {
22093 /* DWARF violation as no main source is present. */
22094 complaint (&symfile_complaints,
22095 _("debug info with no main source gives macro %s "
22096 "on line %d: %s"),
cf2c3c16
TT
22097 is_define ? _("definition") : _("undefinition"),
22098 line, body);
757a13d0
JK
22099 break;
22100 }
3e43a32a
MS
22101 if ((line == 0 && !at_commandline)
22102 || (line != 0 && at_commandline))
4d3c2250 22103 complaint (&symfile_complaints,
757a13d0
JK
22104 _("debug info gives %s macro %s with %s line %d: %s"),
22105 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22106 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22107 line == 0 ? _("zero") : _("non-zero"), line, body);
22108
cf2c3c16 22109 if (is_define)
757a13d0 22110 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22111 else
22112 {
0af92d60
JK
22113 gdb_assert (macinfo_type == DW_MACRO_undef
22114 || macinfo_type == DW_MACRO_undef_strp
22115 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22116 macro_undef (current_file, line, body);
22117 }
2e276125
JB
22118 }
22119 break;
22120
0af92d60 22121 case DW_MACRO_start_file:
2e276125 22122 {
891d2f0b 22123 unsigned int bytes_read;
2e276125
JB
22124 int line, file;
22125
22126 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22127 mac_ptr += bytes_read;
22128 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22129 mac_ptr += bytes_read;
22130
3e43a32a
MS
22131 if ((line == 0 && !at_commandline)
22132 || (line != 0 && at_commandline))
757a13d0
JK
22133 complaint (&symfile_complaints,
22134 _("debug info gives source %d included "
22135 "from %s at %s line %d"),
22136 file, at_commandline ? _("command-line") : _("file"),
22137 line == 0 ? _("zero") : _("non-zero"), line);
22138
22139 if (at_commandline)
22140 {
0af92d60 22141 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22142 pass one. */
757a13d0
JK
22143 at_commandline = 0;
22144 }
22145 else
43f3e411 22146 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22147 }
22148 break;
22149
0af92d60 22150 case DW_MACRO_end_file:
2e276125 22151 if (! current_file)
4d3c2250 22152 complaint (&symfile_complaints,
3e43a32a
MS
22153 _("macro debug info has an unmatched "
22154 "`close_file' directive"));
2e276125
JB
22155 else
22156 {
22157 current_file = current_file->included_by;
22158 if (! current_file)
22159 {
cf2c3c16 22160 enum dwarf_macro_record_type next_type;
2e276125
JB
22161
22162 /* GCC circa March 2002 doesn't produce the zero
22163 type byte marking the end of the compilation
22164 unit. Complain if it's not there, but exit no
22165 matter what. */
22166
22167 /* Do we at least have room for a macinfo type byte? */
22168 if (mac_ptr >= mac_end)
22169 {
f664829e 22170 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22171 return;
22172 }
22173
22174 /* We don't increment mac_ptr here, so this is just
22175 a look-ahead. */
aead7601
SM
22176 next_type
22177 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22178 mac_ptr);
2e276125 22179 if (next_type != 0)
4d3c2250 22180 complaint (&symfile_complaints,
3e43a32a
MS
22181 _("no terminating 0-type entry for "
22182 "macros in `.debug_macinfo' section"));
2e276125
JB
22183
22184 return;
22185 }
22186 }
22187 break;
22188
0af92d60
JK
22189 case DW_MACRO_import:
22190 case DW_MACRO_import_sup:
cf2c3c16
TT
22191 {
22192 LONGEST offset;
8fc3fc34 22193 void **slot;
a036ba48
TT
22194 bfd *include_bfd = abfd;
22195 struct dwarf2_section_info *include_section = section;
d521ce57 22196 const gdb_byte *include_mac_end = mac_end;
a036ba48 22197 int is_dwz = section_is_dwz;
d521ce57 22198 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22199
22200 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22201 mac_ptr += offset_size;
22202
0af92d60 22203 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22204 {
22205 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22206
4d663531 22207 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22208
a036ba48 22209 include_section = &dwz->macro;
a32a8923 22210 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22211 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22212 is_dwz = 1;
22213 }
22214
22215 new_mac_ptr = include_section->buffer + offset;
22216 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22217
8fc3fc34
TT
22218 if (*slot != NULL)
22219 {
22220 /* This has actually happened; see
22221 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22222 complaint (&symfile_complaints,
0af92d60 22223 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22224 ".debug_macro section"));
22225 }
22226 else
22227 {
d521ce57 22228 *slot = (void *) new_mac_ptr;
36586728 22229
a036ba48 22230 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22231 include_mac_end, current_file, lh,
36586728 22232 section, section_is_gnu, is_dwz,
4d663531 22233 offset_size, include_hash);
8fc3fc34 22234
d521ce57 22235 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22236 }
cf2c3c16
TT
22237 }
22238 break;
22239
2e276125 22240 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22241 if (!section_is_gnu)
22242 {
22243 unsigned int bytes_read;
2e276125 22244
ac298888
TT
22245 /* This reads the constant, but since we don't recognize
22246 any vendor extensions, we ignore it. */
22247 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22248 mac_ptr += bytes_read;
22249 read_direct_string (abfd, mac_ptr, &bytes_read);
22250 mac_ptr += bytes_read;
2e276125 22251
cf2c3c16
TT
22252 /* We don't recognize any vendor extensions. */
22253 break;
22254 }
22255 /* FALLTHROUGH */
22256
22257 default:
22258 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22259 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22260 section);
22261 if (mac_ptr == NULL)
22262 return;
22263 break;
2e276125 22264 }
757a13d0 22265 } while (macinfo_type != 0);
2e276125 22266}
8e19ed76 22267
cf2c3c16 22268static void
09262596 22269dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22270 int section_is_gnu)
cf2c3c16 22271{
bb5ed363 22272 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22273 struct line_header *lh = cu->line_header;
22274 bfd *abfd;
d521ce57 22275 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22276 struct macro_source_file *current_file = 0;
22277 enum dwarf_macro_record_type macinfo_type;
22278 unsigned int offset_size = cu->header.offset_size;
d521ce57 22279 const gdb_byte *opcode_definitions[256];
8fc3fc34 22280 void **slot;
09262596
DE
22281 struct dwarf2_section_info *section;
22282 const char *section_name;
22283
22284 if (cu->dwo_unit != NULL)
22285 {
22286 if (section_is_gnu)
22287 {
22288 section = &cu->dwo_unit->dwo_file->sections.macro;
22289 section_name = ".debug_macro.dwo";
22290 }
22291 else
22292 {
22293 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22294 section_name = ".debug_macinfo.dwo";
22295 }
22296 }
22297 else
22298 {
22299 if (section_is_gnu)
22300 {
22301 section = &dwarf2_per_objfile->macro;
22302 section_name = ".debug_macro";
22303 }
22304 else
22305 {
22306 section = &dwarf2_per_objfile->macinfo;
22307 section_name = ".debug_macinfo";
22308 }
22309 }
cf2c3c16 22310
bb5ed363 22311 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22312 if (section->buffer == NULL)
22313 {
fceca515 22314 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22315 return;
22316 }
a32a8923 22317 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22318
22319 /* First pass: Find the name of the base filename.
22320 This filename is needed in order to process all macros whose definition
22321 (or undefinition) comes from the command line. These macros are defined
22322 before the first DW_MACINFO_start_file entry, and yet still need to be
22323 associated to the base file.
22324
22325 To determine the base file name, we scan the macro definitions until we
22326 reach the first DW_MACINFO_start_file entry. We then initialize
22327 CURRENT_FILE accordingly so that any macro definition found before the
22328 first DW_MACINFO_start_file can still be associated to the base file. */
22329
22330 mac_ptr = section->buffer + offset;
22331 mac_end = section->buffer + section->size;
22332
22333 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22334 &offset_size, section_is_gnu);
22335 if (mac_ptr == NULL)
22336 {
22337 /* We already issued a complaint. */
22338 return;
22339 }
22340
22341 do
22342 {
22343 /* Do we at least have room for a macinfo type byte? */
22344 if (mac_ptr >= mac_end)
22345 {
22346 /* Complaint is printed during the second pass as GDB will probably
22347 stop the first pass earlier upon finding
22348 DW_MACINFO_start_file. */
22349 break;
22350 }
22351
aead7601 22352 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22353 mac_ptr++;
22354
22355 /* Note that we rely on the fact that the corresponding GNU and
22356 DWARF constants are the same. */
22357 switch (macinfo_type)
22358 {
22359 /* A zero macinfo type indicates the end of the macro
22360 information. */
22361 case 0:
22362 break;
22363
0af92d60
JK
22364 case DW_MACRO_define:
22365 case DW_MACRO_undef:
cf2c3c16
TT
22366 /* Only skip the data by MAC_PTR. */
22367 {
22368 unsigned int bytes_read;
22369
22370 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22371 mac_ptr += bytes_read;
22372 read_direct_string (abfd, mac_ptr, &bytes_read);
22373 mac_ptr += bytes_read;
22374 }
22375 break;
22376
0af92d60 22377 case DW_MACRO_start_file:
cf2c3c16
TT
22378 {
22379 unsigned int bytes_read;
22380 int line, file;
22381
22382 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22383 mac_ptr += bytes_read;
22384 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22385 mac_ptr += bytes_read;
22386
43f3e411 22387 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22388 }
22389 break;
22390
0af92d60 22391 case DW_MACRO_end_file:
cf2c3c16
TT
22392 /* No data to skip by MAC_PTR. */
22393 break;
22394
0af92d60
JK
22395 case DW_MACRO_define_strp:
22396 case DW_MACRO_undef_strp:
22397 case DW_MACRO_define_sup:
22398 case DW_MACRO_undef_sup:
cf2c3c16
TT
22399 {
22400 unsigned int bytes_read;
22401
22402 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22403 mac_ptr += bytes_read;
22404 mac_ptr += offset_size;
22405 }
22406 break;
22407
0af92d60
JK
22408 case DW_MACRO_import:
22409 case DW_MACRO_import_sup:
cf2c3c16 22410 /* Note that, according to the spec, a transparent include
0af92d60 22411 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22412 skip this opcode. */
22413 mac_ptr += offset_size;
22414 break;
22415
22416 case DW_MACINFO_vendor_ext:
22417 /* Only skip the data by MAC_PTR. */
22418 if (!section_is_gnu)
22419 {
22420 unsigned int bytes_read;
22421
22422 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22423 mac_ptr += bytes_read;
22424 read_direct_string (abfd, mac_ptr, &bytes_read);
22425 mac_ptr += bytes_read;
22426 }
22427 /* FALLTHROUGH */
22428
22429 default:
22430 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22431 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22432 section);
22433 if (mac_ptr == NULL)
22434 return;
22435 break;
22436 }
22437 } while (macinfo_type != 0 && current_file == NULL);
22438
22439 /* Second pass: Process all entries.
22440
22441 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22442 command-line macro definitions/undefinitions. This flag is unset when we
22443 reach the first DW_MACINFO_start_file entry. */
22444
fc4007c9
TT
22445 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22446 htab_eq_pointer,
22447 NULL, xcalloc, xfree));
8fc3fc34 22448 mac_ptr = section->buffer + offset;
fc4007c9 22449 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22450 *slot = (void *) mac_ptr;
8fc3fc34 22451 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22452 current_file, lh, section,
fc4007c9
TT
22453 section_is_gnu, 0, offset_size,
22454 include_hash.get ());
cf2c3c16
TT
22455}
22456
8e19ed76 22457/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22458 if so return true else false. */
380bca97 22459
8e19ed76 22460static int
6e5a29e1 22461attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22462{
22463 return (attr == NULL ? 0 :
22464 attr->form == DW_FORM_block1
22465 || attr->form == DW_FORM_block2
22466 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22467 || attr->form == DW_FORM_block
22468 || attr->form == DW_FORM_exprloc);
8e19ed76 22469}
4c2df51b 22470
c6a0999f
JB
22471/* Return non-zero if ATTR's value is a section offset --- classes
22472 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22473 You may use DW_UNSND (attr) to retrieve such offsets.
22474
22475 Section 7.5.4, "Attribute Encodings", explains that no attribute
22476 may have a value that belongs to more than one of these classes; it
22477 would be ambiguous if we did, because we use the same forms for all
22478 of them. */
380bca97 22479
3690dd37 22480static int
6e5a29e1 22481attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22482{
22483 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22484 || attr->form == DW_FORM_data8
22485 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22486}
22487
3690dd37
JB
22488/* Return non-zero if ATTR's value falls in the 'constant' class, or
22489 zero otherwise. When this function returns true, you can apply
22490 dwarf2_get_attr_constant_value to it.
22491
22492 However, note that for some attributes you must check
22493 attr_form_is_section_offset before using this test. DW_FORM_data4
22494 and DW_FORM_data8 are members of both the constant class, and of
22495 the classes that contain offsets into other debug sections
22496 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22497 that, if an attribute's can be either a constant or one of the
22498 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22499 taken as section offsets, not constants.
22500
22501 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22502 cannot handle that. */
380bca97 22503
3690dd37 22504static int
6e5a29e1 22505attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22506{
22507 switch (attr->form)
22508 {
22509 case DW_FORM_sdata:
22510 case DW_FORM_udata:
22511 case DW_FORM_data1:
22512 case DW_FORM_data2:
22513 case DW_FORM_data4:
22514 case DW_FORM_data8:
663c44ac 22515 case DW_FORM_implicit_const:
3690dd37
JB
22516 return 1;
22517 default:
22518 return 0;
22519 }
22520}
22521
7771576e
SA
22522
22523/* DW_ADDR is always stored already as sect_offset; despite for the forms
22524 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22525
22526static int
6e5a29e1 22527attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22528{
22529 switch (attr->form)
22530 {
22531 case DW_FORM_ref_addr:
22532 case DW_FORM_ref1:
22533 case DW_FORM_ref2:
22534 case DW_FORM_ref4:
22535 case DW_FORM_ref8:
22536 case DW_FORM_ref_udata:
22537 case DW_FORM_GNU_ref_alt:
22538 return 1;
22539 default:
22540 return 0;
22541 }
22542}
22543
3019eac3
DE
22544/* Return the .debug_loc section to use for CU.
22545 For DWO files use .debug_loc.dwo. */
22546
22547static struct dwarf2_section_info *
22548cu_debug_loc_section (struct dwarf2_cu *cu)
22549{
22550 if (cu->dwo_unit)
43988095
JK
22551 {
22552 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22553
22554 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22555 }
22556 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22557 : &dwarf2_per_objfile->loc);
3019eac3
DE
22558}
22559
8cf6f0b1
TT
22560/* A helper function that fills in a dwarf2_loclist_baton. */
22561
22562static void
22563fill_in_loclist_baton (struct dwarf2_cu *cu,
22564 struct dwarf2_loclist_baton *baton,
ff39bb5e 22565 const struct attribute *attr)
8cf6f0b1 22566{
3019eac3
DE
22567 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22568
22569 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22570
22571 baton->per_cu = cu->per_cu;
22572 gdb_assert (baton->per_cu);
22573 /* We don't know how long the location list is, but make sure we
22574 don't run off the edge of the section. */
3019eac3
DE
22575 baton->size = section->size - DW_UNSND (attr);
22576 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22577 baton->base_address = cu->base_address;
f664829e 22578 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22579}
22580
4c2df51b 22581static void
ff39bb5e 22582dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22583 struct dwarf2_cu *cu, int is_block)
4c2df51b 22584{
bb5ed363 22585 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22586 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22587
3690dd37 22588 if (attr_form_is_section_offset (attr)
3019eac3 22589 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22590 the section. If so, fall through to the complaint in the
22591 other branch. */
3019eac3 22592 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22593 {
0d53c4c4 22594 struct dwarf2_loclist_baton *baton;
4c2df51b 22595
8d749320 22596 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22597
8cf6f0b1 22598 fill_in_loclist_baton (cu, baton, attr);
be391dca 22599
d00adf39 22600 if (cu->base_known == 0)
0d53c4c4 22601 complaint (&symfile_complaints,
3e43a32a
MS
22602 _("Location list used without "
22603 "specifying the CU base address."));
4c2df51b 22604
f1e6e072
TT
22605 SYMBOL_ACLASS_INDEX (sym) = (is_block
22606 ? dwarf2_loclist_block_index
22607 : dwarf2_loclist_index);
0d53c4c4
DJ
22608 SYMBOL_LOCATION_BATON (sym) = baton;
22609 }
22610 else
22611 {
22612 struct dwarf2_locexpr_baton *baton;
22613
8d749320 22614 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22615 baton->per_cu = cu->per_cu;
22616 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22617
22618 if (attr_form_is_block (attr))
22619 {
22620 /* Note that we're just copying the block's data pointer
22621 here, not the actual data. We're still pointing into the
6502dd73
DJ
22622 info_buffer for SYM's objfile; right now we never release
22623 that buffer, but when we do clean up properly this may
22624 need to change. */
0d53c4c4
DJ
22625 baton->size = DW_BLOCK (attr)->size;
22626 baton->data = DW_BLOCK (attr)->data;
22627 }
22628 else
22629 {
22630 dwarf2_invalid_attrib_class_complaint ("location description",
22631 SYMBOL_NATURAL_NAME (sym));
22632 baton->size = 0;
0d53c4c4 22633 }
6e70227d 22634
f1e6e072
TT
22635 SYMBOL_ACLASS_INDEX (sym) = (is_block
22636 ? dwarf2_locexpr_block_index
22637 : dwarf2_locexpr_index);
0d53c4c4
DJ
22638 SYMBOL_LOCATION_BATON (sym) = baton;
22639 }
4c2df51b 22640}
6502dd73 22641
9aa1f1e3
TT
22642/* Return the OBJFILE associated with the compilation unit CU. If CU
22643 came from a separate debuginfo file, then the master objfile is
22644 returned. */
ae0d2f24
UW
22645
22646struct objfile *
22647dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22648{
9291a0cd 22649 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22650
22651 /* Return the master objfile, so that we can report and look up the
22652 correct file containing this variable. */
22653 if (objfile->separate_debug_objfile_backlink)
22654 objfile = objfile->separate_debug_objfile_backlink;
22655
22656 return objfile;
22657}
22658
96408a79
SA
22659/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22660 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22661 CU_HEADERP first. */
22662
22663static const struct comp_unit_head *
22664per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22665 struct dwarf2_per_cu_data *per_cu)
22666{
d521ce57 22667 const gdb_byte *info_ptr;
96408a79
SA
22668
22669 if (per_cu->cu)
22670 return &per_cu->cu->header;
22671
9c541725 22672 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22673
22674 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22675 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22676 rcuh_kind::COMPILE);
96408a79
SA
22677
22678 return cu_headerp;
22679}
22680
ae0d2f24
UW
22681/* Return the address size given in the compilation unit header for CU. */
22682
98714339 22683int
ae0d2f24
UW
22684dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22685{
96408a79
SA
22686 struct comp_unit_head cu_header_local;
22687 const struct comp_unit_head *cu_headerp;
c471e790 22688
96408a79
SA
22689 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22690
22691 return cu_headerp->addr_size;
ae0d2f24
UW
22692}
22693
9eae7c52
TT
22694/* Return the offset size given in the compilation unit header for CU. */
22695
22696int
22697dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22698{
96408a79
SA
22699 struct comp_unit_head cu_header_local;
22700 const struct comp_unit_head *cu_headerp;
9c6c53f7 22701
96408a79
SA
22702 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22703
22704 return cu_headerp->offset_size;
22705}
22706
22707/* See its dwarf2loc.h declaration. */
22708
22709int
22710dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22711{
22712 struct comp_unit_head cu_header_local;
22713 const struct comp_unit_head *cu_headerp;
22714
22715 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22716
22717 if (cu_headerp->version == 2)
22718 return cu_headerp->addr_size;
22719 else
22720 return cu_headerp->offset_size;
181cebd4
JK
22721}
22722
9aa1f1e3
TT
22723/* Return the text offset of the CU. The returned offset comes from
22724 this CU's objfile. If this objfile came from a separate debuginfo
22725 file, then the offset may be different from the corresponding
22726 offset in the parent objfile. */
22727
22728CORE_ADDR
22729dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22730{
bb3fa9d0 22731 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22732
22733 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22734}
22735
43988095
JK
22736/* Return DWARF version number of PER_CU. */
22737
22738short
22739dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22740{
22741 return per_cu->dwarf_version;
22742}
22743
348e048f
DE
22744/* Locate the .debug_info compilation unit from CU's objfile which contains
22745 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22746
22747static struct dwarf2_per_cu_data *
9c541725 22748dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22749 unsigned int offset_in_dwz,
ae038cb0
DJ
22750 struct objfile *objfile)
22751{
22752 struct dwarf2_per_cu_data *this_cu;
22753 int low, high;
36586728 22754 const sect_offset *cu_off;
ae038cb0 22755
ae038cb0
DJ
22756 low = 0;
22757 high = dwarf2_per_objfile->n_comp_units - 1;
22758 while (high > low)
22759 {
36586728 22760 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22761 int mid = low + (high - low) / 2;
9a619af0 22762
36586728 22763 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22764 cu_off = &mid_cu->sect_off;
36586728 22765 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22766 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22767 high = mid;
22768 else
22769 low = mid + 1;
22770 }
22771 gdb_assert (low == high);
36586728 22772 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22773 cu_off = &this_cu->sect_off;
22774 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22775 {
36586728 22776 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22777 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22778 "offset 0x%x [in module %s]"),
22779 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22780
9c541725
PA
22781 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22782 <= sect_off);
ae038cb0
DJ
22783 return dwarf2_per_objfile->all_comp_units[low-1];
22784 }
22785 else
22786 {
22787 this_cu = dwarf2_per_objfile->all_comp_units[low];
22788 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22789 && sect_off >= this_cu->sect_off + this_cu->length)
22790 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22791 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22792 return this_cu;
22793 }
22794}
22795
23745b47 22796/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22797
9816fde3 22798static void
23745b47 22799init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22800{
9816fde3 22801 memset (cu, 0, sizeof (*cu));
23745b47
DE
22802 per_cu->cu = cu;
22803 cu->per_cu = per_cu;
22804 cu->objfile = per_cu->objfile;
93311388 22805 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22806}
22807
22808/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22809
22810static void
95554aad
TT
22811prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22812 enum language pretend_language)
9816fde3
JK
22813{
22814 struct attribute *attr;
22815
22816 /* Set the language we're debugging. */
22817 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22818 if (attr)
22819 set_cu_language (DW_UNSND (attr), cu);
22820 else
9cded63f 22821 {
95554aad 22822 cu->language = pretend_language;
9cded63f
TT
22823 cu->language_defn = language_def (cu->language);
22824 }
dee91e82 22825
7d45c7c3 22826 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22827}
22828
ae038cb0
DJ
22829/* Release one cached compilation unit, CU. We unlink it from the tree
22830 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22831 the caller is responsible for that.
22832 NOTE: DATA is a void * because this function is also used as a
22833 cleanup routine. */
ae038cb0
DJ
22834
22835static void
68dc6402 22836free_heap_comp_unit (void *data)
ae038cb0 22837{
9a3c8263 22838 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22839
23745b47
DE
22840 gdb_assert (cu->per_cu != NULL);
22841 cu->per_cu->cu = NULL;
ae038cb0
DJ
22842 cu->per_cu = NULL;
22843
22844 obstack_free (&cu->comp_unit_obstack, NULL);
22845
22846 xfree (cu);
22847}
22848
72bf9492 22849/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22850 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22851 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22852
22853static void
22854free_stack_comp_unit (void *data)
22855{
9a3c8263 22856 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22857
23745b47
DE
22858 gdb_assert (cu->per_cu != NULL);
22859 cu->per_cu->cu = NULL;
22860 cu->per_cu = NULL;
22861
72bf9492
DJ
22862 obstack_free (&cu->comp_unit_obstack, NULL);
22863 cu->partial_dies = NULL;
ae038cb0
DJ
22864}
22865
22866/* Free all cached compilation units. */
22867
22868static void
22869free_cached_comp_units (void *data)
22870{
330cdd98 22871 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22872}
22873
22874/* Increase the age counter on each cached compilation unit, and free
22875 any that are too old. */
22876
22877static void
22878age_cached_comp_units (void)
22879{
22880 struct dwarf2_per_cu_data *per_cu, **last_chain;
22881
22882 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22883 per_cu = dwarf2_per_objfile->read_in_chain;
22884 while (per_cu != NULL)
22885 {
22886 per_cu->cu->last_used ++;
b4f54984 22887 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22888 dwarf2_mark (per_cu->cu);
22889 per_cu = per_cu->cu->read_in_chain;
22890 }
22891
22892 per_cu = dwarf2_per_objfile->read_in_chain;
22893 last_chain = &dwarf2_per_objfile->read_in_chain;
22894 while (per_cu != NULL)
22895 {
22896 struct dwarf2_per_cu_data *next_cu;
22897
22898 next_cu = per_cu->cu->read_in_chain;
22899
22900 if (!per_cu->cu->mark)
22901 {
68dc6402 22902 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22903 *last_chain = next_cu;
22904 }
22905 else
22906 last_chain = &per_cu->cu->read_in_chain;
22907
22908 per_cu = next_cu;
22909 }
22910}
22911
22912/* Remove a single compilation unit from the cache. */
22913
22914static void
dee91e82 22915free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22916{
22917 struct dwarf2_per_cu_data *per_cu, **last_chain;
22918
22919 per_cu = dwarf2_per_objfile->read_in_chain;
22920 last_chain = &dwarf2_per_objfile->read_in_chain;
22921 while (per_cu != NULL)
22922 {
22923 struct dwarf2_per_cu_data *next_cu;
22924
22925 next_cu = per_cu->cu->read_in_chain;
22926
dee91e82 22927 if (per_cu == target_per_cu)
ae038cb0 22928 {
68dc6402 22929 free_heap_comp_unit (per_cu->cu);
dee91e82 22930 per_cu->cu = NULL;
ae038cb0
DJ
22931 *last_chain = next_cu;
22932 break;
22933 }
22934 else
22935 last_chain = &per_cu->cu->read_in_chain;
22936
22937 per_cu = next_cu;
22938 }
22939}
22940
fe3e1990
DJ
22941/* Release all extra memory associated with OBJFILE. */
22942
22943void
22944dwarf2_free_objfile (struct objfile *objfile)
22945{
9a3c8263
SM
22946 dwarf2_per_objfile
22947 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22948 dwarf2_objfile_data_key);
fe3e1990
DJ
22949
22950 if (dwarf2_per_objfile == NULL)
22951 return;
22952
330cdd98 22953 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
22954}
22955
dee91e82
DE
22956/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22957 We store these in a hash table separate from the DIEs, and preserve them
22958 when the DIEs are flushed out of cache.
22959
22960 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22961 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22962 or the type may come from a DWO file. Furthermore, while it's more logical
22963 to use per_cu->section+offset, with Fission the section with the data is in
22964 the DWO file but we don't know that section at the point we need it.
22965 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22966 because we can enter the lookup routine, get_die_type_at_offset, from
22967 outside this file, and thus won't necessarily have PER_CU->cu.
22968 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22969
dee91e82 22970struct dwarf2_per_cu_offset_and_type
1c379e20 22971{
dee91e82 22972 const struct dwarf2_per_cu_data *per_cu;
9c541725 22973 sect_offset sect_off;
1c379e20
DJ
22974 struct type *type;
22975};
22976
dee91e82 22977/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22978
22979static hashval_t
dee91e82 22980per_cu_offset_and_type_hash (const void *item)
1c379e20 22981{
9a3c8263
SM
22982 const struct dwarf2_per_cu_offset_and_type *ofs
22983 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22984
9c541725 22985 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22986}
22987
dee91e82 22988/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22989
22990static int
dee91e82 22991per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22992{
9a3c8263
SM
22993 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22994 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22995 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22996 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22997
dee91e82 22998 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 22999 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23000}
23001
23002/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23003 table if necessary. For convenience, return TYPE.
23004
23005 The DIEs reading must have careful ordering to:
23006 * Not cause infite loops trying to read in DIEs as a prerequisite for
23007 reading current DIE.
23008 * Not trying to dereference contents of still incompletely read in types
23009 while reading in other DIEs.
23010 * Enable referencing still incompletely read in types just by a pointer to
23011 the type without accessing its fields.
23012
23013 Therefore caller should follow these rules:
23014 * Try to fetch any prerequisite types we may need to build this DIE type
23015 before building the type and calling set_die_type.
e71ec853 23016 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23017 possible before fetching more types to complete the current type.
23018 * Make the type as complete as possible before fetching more types. */
1c379e20 23019
f792889a 23020static struct type *
1c379e20
DJ
23021set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23022{
dee91e82 23023 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23024 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23025 struct attribute *attr;
23026 struct dynamic_prop prop;
1c379e20 23027
b4ba55a1
JB
23028 /* For Ada types, make sure that the gnat-specific data is always
23029 initialized (if not already set). There are a few types where
23030 we should not be doing so, because the type-specific area is
23031 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23032 where the type-specific area is used to store the floatformat).
23033 But this is not a problem, because the gnat-specific information
23034 is actually not needed for these types. */
23035 if (need_gnat_info (cu)
23036 && TYPE_CODE (type) != TYPE_CODE_FUNC
23037 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23038 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23039 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23040 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23041 && !HAVE_GNAT_AUX_INFO (type))
23042 INIT_GNAT_SPECIFIC (type);
23043
3f2f83dd
KB
23044 /* Read DW_AT_allocated and set in type. */
23045 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23046 if (attr_form_is_block (attr))
23047 {
23048 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23049 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23050 }
23051 else if (attr != NULL)
23052 {
23053 complaint (&symfile_complaints,
9c541725
PA
23054 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23055 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23056 to_underlying (die->sect_off));
3f2f83dd
KB
23057 }
23058
23059 /* Read DW_AT_associated and set in type. */
23060 attr = dwarf2_attr (die, DW_AT_associated, cu);
23061 if (attr_form_is_block (attr))
23062 {
23063 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23064 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23065 }
23066 else if (attr != NULL)
23067 {
23068 complaint (&symfile_complaints,
9c541725
PA
23069 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23070 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23071 to_underlying (die->sect_off));
3f2f83dd
KB
23072 }
23073
3cdcd0ce
JB
23074 /* Read DW_AT_data_location and set in type. */
23075 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23076 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23077 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23078
dee91e82 23079 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23080 {
dee91e82
DE
23081 dwarf2_per_objfile->die_type_hash =
23082 htab_create_alloc_ex (127,
23083 per_cu_offset_and_type_hash,
23084 per_cu_offset_and_type_eq,
23085 NULL,
23086 &objfile->objfile_obstack,
23087 hashtab_obstack_allocate,
23088 dummy_obstack_deallocate);
f792889a 23089 }
1c379e20 23090
dee91e82 23091 ofs.per_cu = cu->per_cu;
9c541725 23092 ofs.sect_off = die->sect_off;
1c379e20 23093 ofs.type = type;
dee91e82
DE
23094 slot = (struct dwarf2_per_cu_offset_and_type **)
23095 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23096 if (*slot)
23097 complaint (&symfile_complaints,
23098 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23099 to_underlying (die->sect_off));
8d749320
SM
23100 *slot = XOBNEW (&objfile->objfile_obstack,
23101 struct dwarf2_per_cu_offset_and_type);
1c379e20 23102 **slot = ofs;
f792889a 23103 return type;
1c379e20
DJ
23104}
23105
9c541725 23106/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23107 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23108
23109static struct type *
9c541725 23110get_die_type_at_offset (sect_offset sect_off,
673bfd45 23111 struct dwarf2_per_cu_data *per_cu)
1c379e20 23112{
dee91e82 23113 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23114
dee91e82 23115 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23116 return NULL;
1c379e20 23117
dee91e82 23118 ofs.per_cu = per_cu;
9c541725 23119 ofs.sect_off = sect_off;
9a3c8263
SM
23120 slot = ((struct dwarf2_per_cu_offset_and_type *)
23121 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23122 if (slot)
23123 return slot->type;
23124 else
23125 return NULL;
23126}
23127
02142a6c 23128/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23129 or return NULL if DIE does not have a saved type. */
23130
23131static struct type *
23132get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23133{
9c541725 23134 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23135}
23136
10b3939b
DJ
23137/* Add a dependence relationship from CU to REF_PER_CU. */
23138
23139static void
23140dwarf2_add_dependence (struct dwarf2_cu *cu,
23141 struct dwarf2_per_cu_data *ref_per_cu)
23142{
23143 void **slot;
23144
23145 if (cu->dependencies == NULL)
23146 cu->dependencies
23147 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23148 NULL, &cu->comp_unit_obstack,
23149 hashtab_obstack_allocate,
23150 dummy_obstack_deallocate);
23151
23152 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23153 if (*slot == NULL)
23154 *slot = ref_per_cu;
23155}
1c379e20 23156
f504f079
DE
23157/* Subroutine of dwarf2_mark to pass to htab_traverse.
23158 Set the mark field in every compilation unit in the
ae038cb0
DJ
23159 cache that we must keep because we are keeping CU. */
23160
10b3939b
DJ
23161static int
23162dwarf2_mark_helper (void **slot, void *data)
23163{
23164 struct dwarf2_per_cu_data *per_cu;
23165
23166 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23167
23168 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23169 reading of the chain. As such dependencies remain valid it is not much
23170 useful to track and undo them during QUIT cleanups. */
23171 if (per_cu->cu == NULL)
23172 return 1;
23173
10b3939b
DJ
23174 if (per_cu->cu->mark)
23175 return 1;
23176 per_cu->cu->mark = 1;
23177
23178 if (per_cu->cu->dependencies != NULL)
23179 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23180
23181 return 1;
23182}
23183
f504f079
DE
23184/* Set the mark field in CU and in every other compilation unit in the
23185 cache that we must keep because we are keeping CU. */
23186
ae038cb0
DJ
23187static void
23188dwarf2_mark (struct dwarf2_cu *cu)
23189{
23190 if (cu->mark)
23191 return;
23192 cu->mark = 1;
10b3939b
DJ
23193 if (cu->dependencies != NULL)
23194 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23195}
23196
23197static void
23198dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23199{
23200 while (per_cu)
23201 {
23202 per_cu->cu->mark = 0;
23203 per_cu = per_cu->cu->read_in_chain;
23204 }
72bf9492
DJ
23205}
23206
72bf9492
DJ
23207/* Trivial hash function for partial_die_info: the hash value of a DIE
23208 is its offset in .debug_info for this objfile. */
23209
23210static hashval_t
23211partial_die_hash (const void *item)
23212{
9a3c8263
SM
23213 const struct partial_die_info *part_die
23214 = (const struct partial_die_info *) item;
9a619af0 23215
9c541725 23216 return to_underlying (part_die->sect_off);
72bf9492
DJ
23217}
23218
23219/* Trivial comparison function for partial_die_info structures: two DIEs
23220 are equal if they have the same offset. */
23221
23222static int
23223partial_die_eq (const void *item_lhs, const void *item_rhs)
23224{
9a3c8263
SM
23225 const struct partial_die_info *part_die_lhs
23226 = (const struct partial_die_info *) item_lhs;
23227 const struct partial_die_info *part_die_rhs
23228 = (const struct partial_die_info *) item_rhs;
9a619af0 23229
9c541725 23230 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23231}
23232
b4f54984
DE
23233static struct cmd_list_element *set_dwarf_cmdlist;
23234static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23235
23236static void
981a3fb3 23237set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23238{
b4f54984 23239 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23240 gdb_stdout);
ae038cb0
DJ
23241}
23242
23243static void
981a3fb3 23244show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23245{
b4f54984 23246 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23247}
23248
4bf44c1c 23249/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23250
23251static void
c1bd65d0 23252dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23253{
9a3c8263 23254 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23255 int ix;
8b70b953 23256
626f2d1c
TT
23257 /* Make sure we don't accidentally use dwarf2_per_objfile while
23258 cleaning up. */
23259 dwarf2_per_objfile = NULL;
23260
59b0c7c1
JB
23261 for (ix = 0; ix < data->n_comp_units; ++ix)
23262 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23263
59b0c7c1 23264 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23265 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23266 data->all_type_units[ix]->per_cu.imported_symtabs);
23267 xfree (data->all_type_units);
95554aad 23268
8b70b953 23269 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23270
23271 if (data->dwo_files)
23272 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23273 if (data->dwp_file)
23274 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23275
23276 if (data->dwz_file && data->dwz_file->dwz_bfd)
23277 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23278}
23279
23280\f
ae2de4f8 23281/* The "save gdb-index" command. */
9291a0cd 23282
bc8f2430
JK
23283/* In-memory buffer to prepare data to be written later to a file. */
23284class data_buf
9291a0cd 23285{
bc8f2430 23286public:
bc8f2430
JK
23287 /* Copy DATA to the end of the buffer. */
23288 template<typename T>
23289 void append_data (const T &data)
23290 {
23291 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23292 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23293 grow (sizeof (data)));
bc8f2430 23294 }
b89be57b 23295
c2f134ac
PA
23296 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23297 terminating zero is appended too. */
bc8f2430
JK
23298 void append_cstr0 (const char *cstr)
23299 {
23300 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23301 std::copy (cstr, cstr + size, grow (size));
23302 }
23303
23304 /* Accept a host-format integer in VAL and append it to the buffer
23305 as a target-format integer which is LEN bytes long. */
23306 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23307 {
23308 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23309 }
9291a0cd 23310
bc8f2430
JK
23311 /* Return the size of the buffer. */
23312 size_t size () const
23313 {
23314 return m_vec.size ();
23315 }
23316
23317 /* Write the buffer to FILE. */
23318 void file_write (FILE *file) const
23319 {
a81e6d4d
PA
23320 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23321 error (_("couldn't write data to file"));
bc8f2430
JK
23322 }
23323
23324private:
c2f134ac
PA
23325 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23326 the start of the new block. */
23327 gdb_byte *grow (size_t size)
23328 {
23329 m_vec.resize (m_vec.size () + size);
23330 return &*m_vec.end () - size;
23331 }
23332
d5722aa2 23333 gdb::byte_vector m_vec;
bc8f2430 23334};
9291a0cd
TT
23335
23336/* An entry in the symbol table. */
23337struct symtab_index_entry
23338{
23339 /* The name of the symbol. */
23340 const char *name;
23341 /* The offset of the name in the constant pool. */
23342 offset_type index_offset;
23343 /* A sorted vector of the indices of all the CUs that hold an object
23344 of this name. */
bc8f2430 23345 std::vector<offset_type> cu_indices;
9291a0cd
TT
23346};
23347
23348/* The symbol table. This is a power-of-2-sized hash table. */
23349struct mapped_symtab
23350{
bc8f2430
JK
23351 mapped_symtab ()
23352 {
23353 data.resize (1024);
23354 }
b89be57b 23355
bc8f2430 23356 offset_type n_elements = 0;
4b76cda9 23357 std::vector<symtab_index_entry> data;
bc8f2430 23358};
9291a0cd 23359
bc8f2430 23360/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23361 the slot.
23362
23363 Function is used only during write_hash_table so no index format backward
23364 compatibility is needed. */
b89be57b 23365
4b76cda9 23366static symtab_index_entry &
9291a0cd
TT
23367find_slot (struct mapped_symtab *symtab, const char *name)
23368{
559a7a62 23369 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23370
bc8f2430
JK
23371 index = hash & (symtab->data.size () - 1);
23372 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23373
23374 for (;;)
23375 {
4b76cda9
PA
23376 if (symtab->data[index].name == NULL
23377 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23378 return symtab->data[index];
23379 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23380 }
23381}
23382
23383/* Expand SYMTAB's hash table. */
b89be57b 23384
9291a0cd
TT
23385static void
23386hash_expand (struct mapped_symtab *symtab)
23387{
bc8f2430 23388 auto old_entries = std::move (symtab->data);
9291a0cd 23389
bc8f2430
JK
23390 symtab->data.clear ();
23391 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23392
bc8f2430 23393 for (auto &it : old_entries)
4b76cda9 23394 if (it.name != NULL)
bc8f2430 23395 {
4b76cda9 23396 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23397 ref = std::move (it);
23398 }
9291a0cd
TT
23399}
23400
156942c7
DE
23401/* Add an entry to SYMTAB. NAME is the name of the symbol.
23402 CU_INDEX is the index of the CU in which the symbol appears.
23403 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23404
9291a0cd
TT
23405static void
23406add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23407 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23408 offset_type cu_index)
23409{
156942c7 23410 offset_type cu_index_and_attrs;
9291a0cd
TT
23411
23412 ++symtab->n_elements;
bc8f2430 23413 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23414 hash_expand (symtab);
23415
4b76cda9
PA
23416 symtab_index_entry &slot = find_slot (symtab, name);
23417 if (slot.name == NULL)
9291a0cd 23418 {
4b76cda9 23419 slot.name = name;
156942c7 23420 /* index_offset is set later. */
9291a0cd 23421 }
156942c7
DE
23422
23423 cu_index_and_attrs = 0;
23424 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23425 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23426 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23427
23428 /* We don't want to record an index value twice as we want to avoid the
23429 duplication.
23430 We process all global symbols and then all static symbols
23431 (which would allow us to avoid the duplication by only having to check
23432 the last entry pushed), but a symbol could have multiple kinds in one CU.
23433 To keep things simple we don't worry about the duplication here and
23434 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23435 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23436}
23437
23438/* Sort and remove duplicates of all symbols' cu_indices lists. */
23439
23440static void
23441uniquify_cu_indices (struct mapped_symtab *symtab)
23442{
4b76cda9 23443 for (auto &entry : symtab->data)
156942c7 23444 {
4b76cda9 23445 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23446 {
4b76cda9 23447 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23448 std::sort (cu_indices.begin (), cu_indices.end ());
23449 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23450 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23451 }
23452 }
9291a0cd
TT
23453}
23454
bc8f2430
JK
23455/* A form of 'const char *' suitable for container keys. Only the
23456 pointer is stored. The strings themselves are compared, not the
23457 pointers. */
23458class c_str_view
9291a0cd 23459{
bc8f2430
JK
23460public:
23461 c_str_view (const char *cstr)
23462 : m_cstr (cstr)
23463 {}
9291a0cd 23464
bc8f2430
JK
23465 bool operator== (const c_str_view &other) const
23466 {
23467 return strcmp (m_cstr, other.m_cstr) == 0;
23468 }
9291a0cd 23469
bc8f2430
JK
23470private:
23471 friend class c_str_view_hasher;
23472 const char *const m_cstr;
23473};
9291a0cd 23474
bc8f2430
JK
23475/* A std::unordered_map::hasher for c_str_view that uses the right
23476 hash function for strings in a mapped index. */
23477class c_str_view_hasher
23478{
23479public:
23480 size_t operator () (const c_str_view &x) const
23481 {
23482 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23483 }
23484};
b89be57b 23485
bc8f2430
JK
23486/* A std::unordered_map::hasher for std::vector<>. */
23487template<typename T>
23488class vector_hasher
9291a0cd 23489{
bc8f2430
JK
23490public:
23491 size_t operator () (const std::vector<T> &key) const
23492 {
23493 return iterative_hash (key.data (),
23494 sizeof (key.front ()) * key.size (), 0);
23495 }
23496};
9291a0cd 23497
bc8f2430
JK
23498/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23499 constant pool entries going into the data buffer CPOOL. */
3876f04e 23500
bc8f2430
JK
23501static void
23502write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23503{
23504 {
23505 /* Elements are sorted vectors of the indices of all the CUs that
23506 hold an object of this name. */
23507 std::unordered_map<std::vector<offset_type>, offset_type,
23508 vector_hasher<offset_type>>
23509 symbol_hash_table;
23510
23511 /* We add all the index vectors to the constant pool first, to
23512 ensure alignment is ok. */
4b76cda9 23513 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23514 {
4b76cda9 23515 if (entry.name == NULL)
bc8f2430 23516 continue;
4b76cda9 23517 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23518
23519 /* Finding before inserting is faster than always trying to
23520 insert, because inserting always allocates a node, does the
23521 lookup, and then destroys the new node if another node
23522 already had the same key. C++17 try_emplace will avoid
23523 this. */
23524 const auto found
4b76cda9 23525 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23526 if (found != symbol_hash_table.end ())
23527 {
4b76cda9 23528 entry.index_offset = found->second;
70a1152b
PA
23529 continue;
23530 }
23531
4b76cda9
PA
23532 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23533 entry.index_offset = cpool.size ();
23534 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23535 for (const auto index : entry.cu_indices)
23536 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23537 }
23538 }
9291a0cd
TT
23539
23540 /* Now write out the hash table. */
bc8f2430 23541 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23542 for (const auto &entry : symtab->data)
9291a0cd
TT
23543 {
23544 offset_type str_off, vec_off;
23545
4b76cda9 23546 if (entry.name != NULL)
9291a0cd 23547 {
4b76cda9 23548 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23549 if (insertpair.second)
4b76cda9 23550 cpool.append_cstr0 (entry.name);
bc8f2430 23551 str_off = insertpair.first->second;
4b76cda9 23552 vec_off = entry.index_offset;
9291a0cd
TT
23553 }
23554 else
23555 {
23556 /* While 0 is a valid constant pool index, it is not valid
23557 to have 0 for both offsets. */
23558 str_off = 0;
23559 vec_off = 0;
23560 }
23561
bc8f2430
JK
23562 output.append_data (MAYBE_SWAP (str_off));
23563 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23564 }
9291a0cd
TT
23565}
23566
bc8f2430 23567typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23568
23569/* Helper struct for building the address table. */
23570struct addrmap_index_data
23571{
bc8f2430
JK
23572 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23573 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23574 {}
23575
0a5429f6 23576 struct objfile *objfile;
bc8f2430
JK
23577 data_buf &addr_vec;
23578 psym_index_map &cu_index_htab;
0a5429f6
DE
23579
23580 /* Non-zero if the previous_* fields are valid.
23581 We can't write an entry until we see the next entry (since it is only then
23582 that we know the end of the entry). */
23583 int previous_valid;
23584 /* Index of the CU in the table of all CUs in the index file. */
23585 unsigned int previous_cu_index;
0963b4bd 23586 /* Start address of the CU. */
0a5429f6
DE
23587 CORE_ADDR previous_cu_start;
23588};
23589
bc8f2430 23590/* Write an address entry to ADDR_VEC. */
b89be57b 23591
9291a0cd 23592static void
bc8f2430 23593add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23594 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23595{
9291a0cd
TT
23596 CORE_ADDR baseaddr;
23597
23598 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23599
c2f134ac
PA
23600 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23601 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23602 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23603}
23604
23605/* Worker function for traversing an addrmap to build the address table. */
23606
23607static int
23608add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23609{
9a3c8263
SM
23610 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23611 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23612
23613 if (data->previous_valid)
bc8f2430 23614 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23615 data->previous_cu_start, start_addr,
23616 data->previous_cu_index);
23617
23618 data->previous_cu_start = start_addr;
23619 if (pst != NULL)
23620 {
bc8f2430
JK
23621 const auto it = data->cu_index_htab.find (pst);
23622 gdb_assert (it != data->cu_index_htab.cend ());
23623 data->previous_cu_index = it->second;
0a5429f6
DE
23624 data->previous_valid = 1;
23625 }
23626 else
bc8f2430 23627 data->previous_valid = 0;
0a5429f6
DE
23628
23629 return 0;
23630}
23631
bc8f2430 23632/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23633 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23634 in the index file. */
23635
23636static void
bc8f2430
JK
23637write_address_map (struct objfile *objfile, data_buf &addr_vec,
23638 psym_index_map &cu_index_htab)
0a5429f6 23639{
bc8f2430 23640 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23641
23642 /* When writing the address table, we have to cope with the fact that
23643 the addrmap iterator only provides the start of a region; we have to
23644 wait until the next invocation to get the start of the next region. */
23645
23646 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23647 addrmap_index_data.previous_valid = 0;
23648
23649 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23650 &addrmap_index_data);
23651
23652 /* It's highly unlikely the last entry (end address = 0xff...ff)
23653 is valid, but we should still handle it.
23654 The end address is recorded as the start of the next region, but that
23655 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23656 anyway. */
23657 if (addrmap_index_data.previous_valid)
bc8f2430 23658 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23659 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23660 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23661}
23662
156942c7
DE
23663/* Return the symbol kind of PSYM. */
23664
23665static gdb_index_symbol_kind
23666symbol_kind (struct partial_symbol *psym)
23667{
23668 domain_enum domain = PSYMBOL_DOMAIN (psym);
23669 enum address_class aclass = PSYMBOL_CLASS (psym);
23670
23671 switch (domain)
23672 {
23673 case VAR_DOMAIN:
23674 switch (aclass)
23675 {
23676 case LOC_BLOCK:
23677 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23678 case LOC_TYPEDEF:
23679 return GDB_INDEX_SYMBOL_KIND_TYPE;
23680 case LOC_COMPUTED:
23681 case LOC_CONST_BYTES:
23682 case LOC_OPTIMIZED_OUT:
23683 case LOC_STATIC:
23684 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23685 case LOC_CONST:
23686 /* Note: It's currently impossible to recognize psyms as enum values
23687 short of reading the type info. For now punt. */
23688 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23689 default:
23690 /* There are other LOC_FOO values that one might want to classify
23691 as variables, but dwarf2read.c doesn't currently use them. */
23692 return GDB_INDEX_SYMBOL_KIND_OTHER;
23693 }
23694 case STRUCT_DOMAIN:
23695 return GDB_INDEX_SYMBOL_KIND_TYPE;
23696 default:
23697 return GDB_INDEX_SYMBOL_KIND_OTHER;
23698 }
23699}
23700
9291a0cd 23701/* Add a list of partial symbols to SYMTAB. */
b89be57b 23702
9291a0cd
TT
23703static void
23704write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23705 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23706 struct partial_symbol **psymp,
23707 int count,
987d643c
TT
23708 offset_type cu_index,
23709 int is_static)
9291a0cd
TT
23710{
23711 for (; count-- > 0; ++psymp)
23712 {
156942c7 23713 struct partial_symbol *psym = *psymp;
987d643c 23714
156942c7 23715 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23716 error (_("Ada is not currently supported by the index"));
987d643c 23717
987d643c 23718 /* Only add a given psymbol once. */
bc8f2430 23719 if (psyms_seen.insert (psym).second)
987d643c 23720 {
156942c7
DE
23721 gdb_index_symbol_kind kind = symbol_kind (psym);
23722
156942c7
DE
23723 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23724 is_static, kind, cu_index);
987d643c 23725 }
9291a0cd
TT
23726 }
23727}
23728
1fd400ff
TT
23729/* A helper struct used when iterating over debug_types. */
23730struct signatured_type_index_data
23731{
bc8f2430
JK
23732 signatured_type_index_data (data_buf &types_list_,
23733 std::unordered_set<partial_symbol *> &psyms_seen_)
23734 : types_list (types_list_), psyms_seen (psyms_seen_)
23735 {}
23736
1fd400ff
TT
23737 struct objfile *objfile;
23738 struct mapped_symtab *symtab;
bc8f2430
JK
23739 data_buf &types_list;
23740 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23741 int cu_index;
23742};
23743
23744/* A helper function that writes a single signatured_type to an
23745 obstack. */
b89be57b 23746
1fd400ff
TT
23747static int
23748write_one_signatured_type (void **slot, void *d)
23749{
9a3c8263
SM
23750 struct signatured_type_index_data *info
23751 = (struct signatured_type_index_data *) d;
1fd400ff 23752 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23753 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23754
23755 write_psymbols (info->symtab,
987d643c 23756 info->psyms_seen,
af5bf4ad 23757 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
23758 psymtab->n_global_syms, info->cu_index,
23759 0);
1fd400ff 23760 write_psymbols (info->symtab,
987d643c 23761 info->psyms_seen,
af5bf4ad 23762 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
23763 psymtab->n_static_syms, info->cu_index,
23764 1);
1fd400ff 23765
c2f134ac
PA
23766 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23767 to_underlying (entry->per_cu.sect_off));
23768 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23769 to_underlying (entry->type_offset_in_tu));
23770 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23771
23772 ++info->cu_index;
23773
23774 return 1;
23775}
23776
e8f8bcb3
PA
23777/* Recurse into all "included" dependencies and count their symbols as
23778 if they appeared in this psymtab. */
23779
23780static void
23781recursively_count_psymbols (struct partial_symtab *psymtab,
23782 size_t &psyms_seen)
23783{
23784 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23785 if (psymtab->dependencies[i]->user != NULL)
23786 recursively_count_psymbols (psymtab->dependencies[i],
23787 psyms_seen);
23788
23789 psyms_seen += psymtab->n_global_syms;
23790 psyms_seen += psymtab->n_static_syms;
23791}
23792
95554aad
TT
23793/* Recurse into all "included" dependencies and write their symbols as
23794 if they appeared in this psymtab. */
23795
23796static void
23797recursively_write_psymbols (struct objfile *objfile,
23798 struct partial_symtab *psymtab,
23799 struct mapped_symtab *symtab,
bc8f2430 23800 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23801 offset_type cu_index)
23802{
23803 int i;
23804
23805 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23806 if (psymtab->dependencies[i]->user != NULL)
23807 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23808 symtab, psyms_seen, cu_index);
23809
23810 write_psymbols (symtab,
23811 psyms_seen,
af5bf4ad 23812 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
23813 psymtab->n_global_syms, cu_index,
23814 0);
23815 write_psymbols (symtab,
23816 psyms_seen,
af5bf4ad 23817 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
23818 psymtab->n_static_syms, cu_index,
23819 1);
23820}
23821
9291a0cd 23822/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23823
9291a0cd
TT
23824static void
23825write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23826{
9291a0cd
TT
23827 if (dwarf2_per_objfile->using_index)
23828 error (_("Cannot use an index to create the index"));
23829
8b70b953
TT
23830 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23831 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23832
260b681b
DE
23833 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23834 return;
23835
bc8f2430 23836 struct stat st;
4262abfb
JK
23837 if (stat (objfile_name (objfile), &st) < 0)
23838 perror_with_name (objfile_name (objfile));
9291a0cd 23839
bc8f2430
JK
23840 std::string filename (std::string (dir) + SLASH_STRING
23841 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23842
d419f42d 23843 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23844 if (!out_file)
bc8f2430 23845 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23846
16b7a719
PA
23847 /* Order matters here; we want FILE to be closed before FILENAME is
23848 unlinked, because on MS-Windows one cannot delete a file that is
23849 still open. (Don't call anything here that might throw until
23850 file_closer is created.) */
bc8f2430 23851 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23852 gdb_file_up close_out_file (out_file);
9291a0cd 23853
bc8f2430
JK
23854 mapped_symtab symtab;
23855 data_buf cu_list;
987d643c 23856
0a5429f6
DE
23857 /* While we're scanning CU's create a table that maps a psymtab pointer
23858 (which is what addrmap records) to its index (which is what is recorded
23859 in the index file). This will later be needed to write the address
23860 table. */
bc8f2430
JK
23861 psym_index_map cu_index_htab;
23862 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23863
23864 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23865 work here. Also, the debug_types entries do not appear in
23866 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23867
23868 /* The psyms_seen set is potentially going to be largish (~40k
23869 elements when indexing a -g3 build of GDB itself). Estimate the
23870 number of elements in order to avoid too many rehashes, which
23871 require rebuilding buckets and thus many trips to
23872 malloc/free. */
23873 size_t psyms_count = 0;
23874 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23875 {
23876 struct dwarf2_per_cu_data *per_cu
23877 = dwarf2_per_objfile->all_comp_units[i];
23878 struct partial_symtab *psymtab = per_cu->v.psymtab;
23879
23880 if (psymtab != NULL && psymtab->user == NULL)
23881 recursively_count_psymbols (psymtab, psyms_count);
23882 }
23883 /* Generating an index for gdb itself shows a ratio of
23884 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23885 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23886 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23887 {
3e43a32a
MS
23888 struct dwarf2_per_cu_data *per_cu
23889 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23890 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23891
92fac807
JK
23892 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23893 It may be referenced from a local scope but in such case it does not
23894 need to be present in .gdb_index. */
23895 if (psymtab == NULL)
23896 continue;
23897
95554aad 23898 if (psymtab->user == NULL)
bc8f2430
JK
23899 recursively_write_psymbols (objfile, psymtab, &symtab,
23900 psyms_seen, i);
9291a0cd 23901
bc8f2430
JK
23902 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23903 gdb_assert (insertpair.second);
9291a0cd 23904
c2f134ac
PA
23905 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23906 to_underlying (per_cu->sect_off));
23907 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23908 }
23909
0a5429f6 23910 /* Dump the address map. */
bc8f2430
JK
23911 data_buf addr_vec;
23912 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23913
1fd400ff 23914 /* Write out the .debug_type entries, if any. */
bc8f2430 23915 data_buf types_cu_list;
1fd400ff
TT
23916 if (dwarf2_per_objfile->signatured_types)
23917 {
bc8f2430
JK
23918 signatured_type_index_data sig_data (types_cu_list,
23919 psyms_seen);
1fd400ff
TT
23920
23921 sig_data.objfile = objfile;
bc8f2430 23922 sig_data.symtab = &symtab;
1fd400ff
TT
23923 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23924 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23925 write_one_signatured_type, &sig_data);
23926 }
23927
156942c7
DE
23928 /* Now that we've processed all symbols we can shrink their cu_indices
23929 lists. */
bc8f2430 23930 uniquify_cu_indices (&symtab);
156942c7 23931
bc8f2430
JK
23932 data_buf symtab_vec, constant_pool;
23933 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23934
bc8f2430
JK
23935 data_buf contents;
23936 const offset_type size_of_contents = 6 * sizeof (offset_type);
23937 offset_type total_len = size_of_contents;
9291a0cd
TT
23938
23939 /* The version number. */
bc8f2430 23940 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
23941
23942 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
23943 contents.append_data (MAYBE_SWAP (total_len));
23944 total_len += cu_list.size ();
9291a0cd 23945
1fd400ff 23946 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
23947 contents.append_data (MAYBE_SWAP (total_len));
23948 total_len += types_cu_list.size ();
1fd400ff 23949
9291a0cd 23950 /* The offset of the address table from the start of the file. */
bc8f2430
JK
23951 contents.append_data (MAYBE_SWAP (total_len));
23952 total_len += addr_vec.size ();
9291a0cd
TT
23953
23954 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
23955 contents.append_data (MAYBE_SWAP (total_len));
23956 total_len += symtab_vec.size ();
9291a0cd
TT
23957
23958 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
23959 contents.append_data (MAYBE_SWAP (total_len));
23960 total_len += constant_pool.size ();
9291a0cd 23961
bc8f2430 23962 gdb_assert (contents.size () == size_of_contents);
9291a0cd 23963
bc8f2430
JK
23964 contents.file_write (out_file);
23965 cu_list.file_write (out_file);
23966 types_cu_list.file_write (out_file);
23967 addr_vec.file_write (out_file);
23968 symtab_vec.file_write (out_file);
23969 constant_pool.file_write (out_file);
9291a0cd 23970
bef155c3
TT
23971 /* We want to keep the file. */
23972 unlink_file.keep ();
9291a0cd
TT
23973}
23974
90476074
TT
23975/* Implementation of the `save gdb-index' command.
23976
23977 Note that the file format used by this command is documented in the
23978 GDB manual. Any changes here must be documented there. */
11570e71 23979
9291a0cd 23980static void
8384c356 23981save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
23982{
23983 struct objfile *objfile;
23984
23985 if (!arg || !*arg)
96d19272 23986 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23987
23988 ALL_OBJFILES (objfile)
23989 {
23990 struct stat st;
23991
23992 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23993 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23994 continue;
23995
9a3c8263
SM
23996 dwarf2_per_objfile
23997 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23998 dwarf2_objfile_data_key);
9291a0cd
TT
23999 if (dwarf2_per_objfile)
24000 {
9291a0cd 24001
492d29ea 24002 TRY
9291a0cd
TT
24003 {
24004 write_psymtabs_to_index (objfile, arg);
24005 }
492d29ea
PA
24006 CATCH (except, RETURN_MASK_ERROR)
24007 {
24008 exception_fprintf (gdb_stderr, except,
24009 _("Error while writing index for `%s': "),
24010 objfile_name (objfile));
24011 }
24012 END_CATCH
9291a0cd
TT
24013 }
24014 }
dce234bc
PP
24015}
24016
9291a0cd
TT
24017\f
24018
b4f54984 24019int dwarf_always_disassemble;
9eae7c52
TT
24020
24021static void
b4f54984
DE
24022show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24023 struct cmd_list_element *c, const char *value)
9eae7c52 24024{
3e43a32a
MS
24025 fprintf_filtered (file,
24026 _("Whether to always disassemble "
24027 "DWARF expressions is %s.\n"),
9eae7c52
TT
24028 value);
24029}
24030
900e11f9
JK
24031static void
24032show_check_physname (struct ui_file *file, int from_tty,
24033 struct cmd_list_element *c, const char *value)
24034{
24035 fprintf_filtered (file,
24036 _("Whether to check \"physname\" is %s.\n"),
24037 value);
24038}
24039
6502dd73
DJ
24040void
24041_initialize_dwarf2_read (void)
24042{
96d19272
JK
24043 struct cmd_list_element *c;
24044
dce234bc 24045 dwarf2_objfile_data_key
c1bd65d0 24046 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24047
b4f54984
DE
24048 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24049Set DWARF specific variables.\n\
24050Configure DWARF variables such as the cache size"),
24051 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24052 0/*allow-unknown*/, &maintenance_set_cmdlist);
24053
b4f54984
DE
24054 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24055Show DWARF specific variables\n\
24056Show DWARF variables such as the cache size"),
24057 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24058 0/*allow-unknown*/, &maintenance_show_cmdlist);
24059
24060 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24061 &dwarf_max_cache_age, _("\
24062Set the upper bound on the age of cached DWARF compilation units."), _("\
24063Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24064A higher limit means that cached compilation units will be stored\n\
24065in memory longer, and more total memory will be used. Zero disables\n\
24066caching, which can slow down startup."),
2c5b56ce 24067 NULL,
b4f54984
DE
24068 show_dwarf_max_cache_age,
24069 &set_dwarf_cmdlist,
24070 &show_dwarf_cmdlist);
d97bc12b 24071
9eae7c52 24072 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24073 &dwarf_always_disassemble, _("\
9eae7c52
TT
24074Set whether `info address' always disassembles DWARF expressions."), _("\
24075Show whether `info address' always disassembles DWARF expressions."), _("\
24076When enabled, DWARF expressions are always printed in an assembly-like\n\
24077syntax. When disabled, expressions will be printed in a more\n\
24078conversational style, when possible."),
24079 NULL,
b4f54984
DE
24080 show_dwarf_always_disassemble,
24081 &set_dwarf_cmdlist,
24082 &show_dwarf_cmdlist);
24083
24084 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24085Set debugging of the DWARF reader."), _("\
24086Show debugging of the DWARF reader."), _("\
24087When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24088reading and symtab expansion. A value of 1 (one) provides basic\n\
24089information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24090 NULL,
24091 NULL,
24092 &setdebuglist, &showdebuglist);
24093
b4f54984
DE
24094 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24095Set debugging of the DWARF DIE reader."), _("\
24096Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24097When enabled (non-zero), DIEs are dumped after they are read in.\n\
24098The value is the maximum depth to print."),
ccce17b0
YQ
24099 NULL,
24100 NULL,
24101 &setdebuglist, &showdebuglist);
9291a0cd 24102
27e0867f
DE
24103 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24104Set debugging of the dwarf line reader."), _("\
24105Show debugging of the dwarf line reader."), _("\
24106When enabled (non-zero), line number entries are dumped as they are read in.\n\
24107A value of 1 (one) provides basic information.\n\
24108A value greater than 1 provides more verbose information."),
24109 NULL,
24110 NULL,
24111 &setdebuglist, &showdebuglist);
24112
900e11f9
JK
24113 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24114Set cross-checking of \"physname\" code against demangler."), _("\
24115Show cross-checking of \"physname\" code against demangler."), _("\
24116When enabled, GDB's internal \"physname\" code is checked against\n\
24117the demangler."),
24118 NULL, show_check_physname,
24119 &setdebuglist, &showdebuglist);
24120
e615022a
DE
24121 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24122 no_class, &use_deprecated_index_sections, _("\
24123Set whether to use deprecated gdb_index sections."), _("\
24124Show whether to use deprecated gdb_index sections."), _("\
24125When enabled, deprecated .gdb_index sections are used anyway.\n\
24126Normally they are ignored either because of a missing feature or\n\
24127performance issue.\n\
24128Warning: This option must be enabled before gdb reads the file."),
24129 NULL,
24130 NULL,
24131 &setlist, &showlist);
24132
96d19272 24133 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24134 _("\
fc1a9d6e 24135Save a gdb-index file.\n\
11570e71 24136Usage: save gdb-index DIRECTORY"),
96d19272
JK
24137 &save_cmdlist);
24138 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24139
24140 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24141 &dwarf2_locexpr_funcs);
24142 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24143 &dwarf2_loclist_funcs);
24144
24145 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24146 &dwarf2_block_frame_base_locexpr_funcs);
24147 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24148 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24149}
This page took 4.135839 seconds and 4 git commands to generate.