Honor an existing CC_FOR_BUILD in the environment for sim.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
bbf2f4df 77#include "filename-seen-cache.h"
c906108c 78#include <fcntl.h>
c906108c 79#include <sys/types.h>
325fac50 80#include <algorithm>
bc8f2430
JK
81#include <unordered_set>
82#include <unordered_map>
d8151005 83
34eaf542
TT
84typedef struct symbol *symbolp;
85DEF_VEC_P (symbolp);
86
73be47f5
DE
87/* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
b4f54984
DE
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90static unsigned int dwarf_read_debug = 0;
45cfd468 91
d97bc12b 92/* When non-zero, dump DIEs after they are read in. */
b4f54984 93static unsigned int dwarf_die_debug = 0;
d97bc12b 94
27e0867f
DE
95/* When non-zero, dump line number entries as they are read in. */
96static unsigned int dwarf_line_debug = 0;
97
900e11f9
JK
98/* When non-zero, cross-check physname against demangler. */
99static int check_physname = 0;
100
481860b3 101/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 102static int use_deprecated_index_sections = 0;
481860b3 103
6502dd73
DJ
104static const struct objfile_data *dwarf2_objfile_data_key;
105
f1e6e072
TT
106/* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108static int dwarf2_locexpr_index;
109static int dwarf2_loclist_index;
110static int dwarf2_locexpr_block_index;
111static int dwarf2_loclist_block_index;
112
73869dc2
DE
113/* A descriptor for dwarf sections.
114
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
119
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
124 input section.
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
128
dce234bc
PP
129struct dwarf2_section_info
130{
73869dc2
DE
131 union
132 {
e5aa3347 133 /* If this is a real section, the bfd section. */
049412e3 134 asection *section;
73869dc2 135 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 136 section. */
73869dc2
DE
137 struct dwarf2_section_info *containing_section;
138 } s;
19ac8c2e 139 /* Pointer to section data, only valid if readin. */
d521ce57 140 const gdb_byte *buffer;
73869dc2 141 /* The size of the section, real or virtual. */
dce234bc 142 bfd_size_type size;
73869dc2
DE
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
be391dca 146 /* True if we have tried to read this section. */
73869dc2
DE
147 char readin;
148 /* True if this is a virtual section, False otherwise.
049412e3 149 This specifies which of s.section and s.containing_section to use. */
73869dc2 150 char is_virtual;
dce234bc
PP
151};
152
8b70b953
TT
153typedef struct dwarf2_section_info dwarf2_section_info_def;
154DEF_VEC_O (dwarf2_section_info_def);
155
9291a0cd
TT
156/* All offsets in the index are of this type. It must be
157 architecture-independent. */
158typedef uint32_t offset_type;
159
160DEF_VEC_I (offset_type);
161
156942c7
DE
162/* Ensure only legit values are used. */
163#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
167 } while (0)
168
169/* Ensure only legit values are used. */
170#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 do { \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177/* Ensure we don't use more than the alloted nuber of bits for the CU. */
178#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 do { \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
182 } while (0)
183
9291a0cd
TT
184/* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186struct mapped_index
187{
559a7a62
JK
188 /* Index data format version. */
189 int version;
190
9291a0cd
TT
191 /* The total length of the buffer. */
192 off_t total_size;
b11b1f88 193
9291a0cd
TT
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
b11b1f88 196
9291a0cd
TT
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
b11b1f88 199
3876f04e
DE
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
b11b1f88 202
9291a0cd 203 /* Size in slots, each slot is 2 offset_types. */
3876f04e 204 offset_type symbol_table_slots;
b11b1f88 205
9291a0cd
TT
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
208};
209
95554aad
TT
210typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211DEF_VEC_P (dwarf2_per_cu_ptr);
212
52059ffd
TT
213struct tu_stats
214{
215 int nr_uniq_abbrev_tables;
216 int nr_symtabs;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
220};
221
9cdd5dbd
DE
222/* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
224
6502dd73
DJ
225struct dwarf2_per_objfile
226{
330cdd98
PA
227 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
228 dwarf2 section names, or is NULL if the standard ELF names are
229 used. */
230 dwarf2_per_objfile (struct objfile *objfile,
231 const dwarf2_debug_sections *names);
ae038cb0 232
330cdd98
PA
233 ~dwarf2_per_objfile ();
234
235 /* Disable copy. */
236 dwarf2_per_objfile (const dwarf2_per_objfile &) = delete;
237 void operator= (const dwarf2_per_objfile &) = delete;
238
239 /* Free all cached compilation units. */
240 void free_cached_comp_units ();
241private:
242 /* This function is mapped across the sections and remembers the
243 offset and size of each of the debugging sections we are
244 interested in. */
245 void locate_sections (bfd *abfd, asection *sectp,
246 const dwarf2_debug_sections &names);
247
248public:
249 dwarf2_section_info info {};
250 dwarf2_section_info abbrev {};
251 dwarf2_section_info line {};
252 dwarf2_section_info loc {};
253 dwarf2_section_info loclists {};
254 dwarf2_section_info macinfo {};
255 dwarf2_section_info macro {};
256 dwarf2_section_info str {};
257 dwarf2_section_info line_str {};
258 dwarf2_section_info ranges {};
259 dwarf2_section_info rnglists {};
260 dwarf2_section_info addr {};
261 dwarf2_section_info frame {};
262 dwarf2_section_info eh_frame {};
263 dwarf2_section_info gdb_index {};
264
265 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 266
be391dca 267 /* Back link. */
330cdd98 268 struct objfile *objfile = NULL;
be391dca 269
d467dd73 270 /* Table of all the compilation units. This is used to locate
10b3939b 271 the target compilation unit of a particular reference. */
330cdd98 272 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
273
274 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 275 int n_comp_units = 0;
ae038cb0 276
1fd400ff 277 /* The number of .debug_types-related CUs. */
330cdd98 278 int n_type_units = 0;
1fd400ff 279
6aa5f3a6
DE
280 /* The number of elements allocated in all_type_units.
281 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 282 int n_allocated_type_units = 0;
6aa5f3a6 283
a2ce51a0
DE
284 /* The .debug_types-related CUs (TUs).
285 This is stored in malloc space because we may realloc it. */
330cdd98 286 struct signatured_type **all_type_units = NULL;
1fd400ff 287
f4dc4d17
DE
288 /* Table of struct type_unit_group objects.
289 The hash key is the DW_AT_stmt_list value. */
330cdd98 290 htab_t type_unit_groups {};
72dca2f5 291
348e048f
DE
292 /* A table mapping .debug_types signatures to its signatured_type entry.
293 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 294 htab_t signatured_types {};
348e048f 295
f4dc4d17
DE
296 /* Type unit statistics, to see how well the scaling improvements
297 are doing. */
330cdd98 298 struct tu_stats tu_stats {};
f4dc4d17
DE
299
300 /* A chain of compilation units that are currently read in, so that
301 they can be freed later. */
330cdd98 302 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 303
3019eac3
DE
304 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
305 This is NULL if the table hasn't been allocated yet. */
330cdd98 306 htab_t dwo_files {};
3019eac3 307
330cdd98
PA
308 /* True if we've checked for whether there is a DWP file. */
309 bool dwp_checked = false;
80626a55
DE
310
311 /* The DWP file if there is one, or NULL. */
330cdd98 312 struct dwp_file *dwp_file = NULL;
80626a55 313
36586728
TT
314 /* The shared '.dwz' file, if one exists. This is used when the
315 original data was compressed using 'dwz -m'. */
330cdd98 316 struct dwz_file *dwz_file = NULL;
36586728 317
330cdd98 318 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 319 VMA of 0. */
330cdd98 320 bool has_section_at_zero = false;
9291a0cd 321
ae2de4f8
DE
322 /* True if we are using the mapped index,
323 or we are faking it for OBJF_READNOW's sake. */
330cdd98 324 bool using_index = false;
9291a0cd 325
ae2de4f8 326 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 327 mapped_index *index_table = NULL;
98bfdba5 328
7b9f3c50 329 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
330 TUs typically share line table entries with a CU, so we maintain a
331 separate table of all line table entries to support the sharing.
332 Note that while there can be way more TUs than CUs, we've already
333 sorted all the TUs into "type unit groups", grouped by their
334 DW_AT_stmt_list value. Therefore the only sharing done here is with a
335 CU and its associated TU group if there is one. */
330cdd98 336 htab_t quick_file_names_table {};
7b9f3c50 337
98bfdba5
PA
338 /* Set during partial symbol reading, to prevent queueing of full
339 symbols. */
330cdd98 340 bool reading_partial_symbols = false;
673bfd45 341
dee91e82 342 /* Table mapping type DIEs to their struct type *.
673bfd45 343 This is NULL if not allocated yet.
02142a6c 344 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 345 htab_t die_type_hash {};
95554aad
TT
346
347 /* The CUs we recently read. */
330cdd98 348 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
349
350 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 351 htab_t line_header_hash {};
bbf2f4df
PA
352
353 /* Table containing all filenames. This is an optional because the
354 table is lazily constructed on first access. */
355 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
356};
357
358static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 359
251d32d9 360/* Default names of the debugging sections. */
c906108c 361
233a11ab
CS
362/* Note that if the debugging section has been compressed, it might
363 have a name like .zdebug_info. */
364
9cdd5dbd
DE
365static const struct dwarf2_debug_sections dwarf2_elf_names =
366{
251d32d9
TG
367 { ".debug_info", ".zdebug_info" },
368 { ".debug_abbrev", ".zdebug_abbrev" },
369 { ".debug_line", ".zdebug_line" },
370 { ".debug_loc", ".zdebug_loc" },
43988095 371 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 372 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 373 { ".debug_macro", ".zdebug_macro" },
251d32d9 374 { ".debug_str", ".zdebug_str" },
43988095 375 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 376 { ".debug_ranges", ".zdebug_ranges" },
43988095 377 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 378 { ".debug_types", ".zdebug_types" },
3019eac3 379 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
380 { ".debug_frame", ".zdebug_frame" },
381 { ".eh_frame", NULL },
24d3216f
TT
382 { ".gdb_index", ".zgdb_index" },
383 23
251d32d9 384};
c906108c 385
80626a55 386/* List of DWO/DWP sections. */
3019eac3 387
80626a55 388static const struct dwop_section_names
3019eac3
DE
389{
390 struct dwarf2_section_names abbrev_dwo;
391 struct dwarf2_section_names info_dwo;
392 struct dwarf2_section_names line_dwo;
393 struct dwarf2_section_names loc_dwo;
43988095 394 struct dwarf2_section_names loclists_dwo;
09262596
DE
395 struct dwarf2_section_names macinfo_dwo;
396 struct dwarf2_section_names macro_dwo;
3019eac3
DE
397 struct dwarf2_section_names str_dwo;
398 struct dwarf2_section_names str_offsets_dwo;
399 struct dwarf2_section_names types_dwo;
80626a55
DE
400 struct dwarf2_section_names cu_index;
401 struct dwarf2_section_names tu_index;
3019eac3 402}
80626a55 403dwop_section_names =
3019eac3
DE
404{
405 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
406 { ".debug_info.dwo", ".zdebug_info.dwo" },
407 { ".debug_line.dwo", ".zdebug_line.dwo" },
408 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 409 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
410 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
411 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
412 { ".debug_str.dwo", ".zdebug_str.dwo" },
413 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
414 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
415 { ".debug_cu_index", ".zdebug_cu_index" },
416 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
417};
418
c906108c
SS
419/* local data types */
420
107d2387
AC
421/* The data in a compilation unit header, after target2host
422 translation, looks like this. */
c906108c 423struct comp_unit_head
a738430d 424{
c764a876 425 unsigned int length;
a738430d 426 short version;
a738430d
MK
427 unsigned char addr_size;
428 unsigned char signed_addr_p;
9c541725 429 sect_offset abbrev_sect_off;
57349743 430
a738430d
MK
431 /* Size of file offsets; either 4 or 8. */
432 unsigned int offset_size;
57349743 433
a738430d
MK
434 /* Size of the length field; either 4 or 12. */
435 unsigned int initial_length_size;
57349743 436
43988095
JK
437 enum dwarf_unit_type unit_type;
438
a738430d
MK
439 /* Offset to the first byte of this compilation unit header in the
440 .debug_info section, for resolving relative reference dies. */
9c541725 441 sect_offset sect_off;
57349743 442
d00adf39
DE
443 /* Offset to first die in this cu from the start of the cu.
444 This will be the first byte following the compilation unit header. */
9c541725 445 cu_offset first_die_cu_offset;
43988095
JK
446
447 /* 64-bit signature of this type unit - it is valid only for
448 UNIT_TYPE DW_UT_type. */
449 ULONGEST signature;
450
451 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 452 cu_offset type_cu_offset_in_tu;
a738430d 453};
c906108c 454
3da10d80
KS
455/* Type used for delaying computation of method physnames.
456 See comments for compute_delayed_physnames. */
457struct delayed_method_info
458{
459 /* The type to which the method is attached, i.e., its parent class. */
460 struct type *type;
461
462 /* The index of the method in the type's function fieldlists. */
463 int fnfield_index;
464
465 /* The index of the method in the fieldlist. */
466 int index;
467
468 /* The name of the DIE. */
469 const char *name;
470
471 /* The DIE associated with this method. */
472 struct die_info *die;
473};
474
475typedef struct delayed_method_info delayed_method_info;
476DEF_VEC_O (delayed_method_info);
477
e7c27a73
DJ
478/* Internal state when decoding a particular compilation unit. */
479struct dwarf2_cu
480{
481 /* The objfile containing this compilation unit. */
482 struct objfile *objfile;
483
d00adf39 484 /* The header of the compilation unit. */
e7c27a73 485 struct comp_unit_head header;
e142c38c 486
d00adf39
DE
487 /* Base address of this compilation unit. */
488 CORE_ADDR base_address;
489
490 /* Non-zero if base_address has been set. */
491 int base_known;
492
e142c38c
DJ
493 /* The language we are debugging. */
494 enum language language;
495 const struct language_defn *language_defn;
496
b0f35d58
DL
497 const char *producer;
498
e142c38c
DJ
499 /* The generic symbol table building routines have separate lists for
500 file scope symbols and all all other scopes (local scopes). So
501 we need to select the right one to pass to add_symbol_to_list().
502 We do it by keeping a pointer to the correct list in list_in_scope.
503
504 FIXME: The original dwarf code just treated the file scope as the
505 first local scope, and all other local scopes as nested local
506 scopes, and worked fine. Check to see if we really need to
507 distinguish these in buildsym.c. */
508 struct pending **list_in_scope;
509
433df2d4
DE
510 /* The abbrev table for this CU.
511 Normally this points to the abbrev table in the objfile.
512 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
513 struct abbrev_table *abbrev_table;
72bf9492 514
b64f50a1
JK
515 /* Hash table holding all the loaded partial DIEs
516 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
517 htab_t partial_dies;
518
519 /* Storage for things with the same lifetime as this read-in compilation
520 unit, including partial DIEs. */
521 struct obstack comp_unit_obstack;
522
ae038cb0
DJ
523 /* When multiple dwarf2_cu structures are living in memory, this field
524 chains them all together, so that they can be released efficiently.
525 We will probably also want a generation counter so that most-recently-used
526 compilation units are cached... */
527 struct dwarf2_per_cu_data *read_in_chain;
528
69d751e3 529 /* Backlink to our per_cu entry. */
ae038cb0
DJ
530 struct dwarf2_per_cu_data *per_cu;
531
532 /* How many compilation units ago was this CU last referenced? */
533 int last_used;
534
b64f50a1
JK
535 /* A hash table of DIE cu_offset for following references with
536 die_info->offset.sect_off as hash. */
51545339 537 htab_t die_hash;
10b3939b
DJ
538
539 /* Full DIEs if read in. */
540 struct die_info *dies;
541
542 /* A set of pointers to dwarf2_per_cu_data objects for compilation
543 units referenced by this one. Only set during full symbol processing;
544 partial symbol tables do not have dependencies. */
545 htab_t dependencies;
546
cb1df416
DJ
547 /* Header data from the line table, during full symbol processing. */
548 struct line_header *line_header;
4c8aa72d
PA
549 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
550 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
551 this is the DW_TAG_compile_unit die for this CU. We'll hold on
552 to the line header as long as this DIE is being processed. See
553 process_die_scope. */
554 die_info *line_header_die_owner;
cb1df416 555
3da10d80
KS
556 /* A list of methods which need to have physnames computed
557 after all type information has been read. */
558 VEC (delayed_method_info) *method_list;
559
96408a79
SA
560 /* To be copied to symtab->call_site_htab. */
561 htab_t call_site_htab;
562
034e5797
DE
563 /* Non-NULL if this CU came from a DWO file.
564 There is an invariant here that is important to remember:
565 Except for attributes copied from the top level DIE in the "main"
566 (or "stub") file in preparation for reading the DWO file
567 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
568 Either there isn't a DWO file (in which case this is NULL and the point
569 is moot), or there is and either we're not going to read it (in which
570 case this is NULL) or there is and we are reading it (in which case this
571 is non-NULL). */
3019eac3
DE
572 struct dwo_unit *dwo_unit;
573
574 /* The DW_AT_addr_base attribute if present, zero otherwise
575 (zero is a valid value though).
1dbab08b 576 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
577 ULONGEST addr_base;
578
2e3cf129
DE
579 /* The DW_AT_ranges_base attribute if present, zero otherwise
580 (zero is a valid value though).
1dbab08b 581 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 582 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
583 be used without needing to know whether DWO files are in use or not.
584 N.B. This does not apply to DW_AT_ranges appearing in
585 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
586 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
587 DW_AT_ranges_base *would* have to be applied, and we'd have to care
588 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
589 ULONGEST ranges_base;
590
ae038cb0
DJ
591 /* Mark used when releasing cached dies. */
592 unsigned int mark : 1;
593
8be455d7
JK
594 /* This CU references .debug_loc. See the symtab->locations_valid field.
595 This test is imperfect as there may exist optimized debug code not using
596 any location list and still facing inlining issues if handled as
597 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 598 unsigned int has_loclist : 1;
ba919b58 599
1b80a9fa
JK
600 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
601 if all the producer_is_* fields are valid. This information is cached
602 because profiling CU expansion showed excessive time spent in
603 producer_is_gxx_lt_4_6. */
ba919b58
TT
604 unsigned int checked_producer : 1;
605 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 606 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 607 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
608
609 /* When set, the file that we're processing is known to have
610 debugging info for C++ namespaces. GCC 3.3.x did not produce
611 this information, but later versions do. */
612
613 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
614};
615
10b3939b
DJ
616/* Persistent data held for a compilation unit, even when not
617 processing it. We put a pointer to this structure in the
28dee7f5 618 read_symtab_private field of the psymtab. */
10b3939b 619
ae038cb0
DJ
620struct dwarf2_per_cu_data
621{
36586728 622 /* The start offset and length of this compilation unit.
45452591 623 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
624 initial_length_size.
625 If the DIE refers to a DWO file, this is always of the original die,
626 not the DWO file. */
9c541725 627 sect_offset sect_off;
36586728 628 unsigned int length;
ae038cb0 629
43988095
JK
630 /* DWARF standard version this data has been read from (such as 4 or 5). */
631 short dwarf_version;
632
ae038cb0
DJ
633 /* Flag indicating this compilation unit will be read in before
634 any of the current compilation units are processed. */
c764a876 635 unsigned int queued : 1;
ae038cb0 636
0d99eb77
DE
637 /* This flag will be set when reading partial DIEs if we need to load
638 absolutely all DIEs for this compilation unit, instead of just the ones
639 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
640 hash table and don't find it. */
641 unsigned int load_all_dies : 1;
642
0186c6a7
DE
643 /* Non-zero if this CU is from .debug_types.
644 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
645 this is non-zero. */
3019eac3
DE
646 unsigned int is_debug_types : 1;
647
36586728
TT
648 /* Non-zero if this CU is from the .dwz file. */
649 unsigned int is_dwz : 1;
650
a2ce51a0
DE
651 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
652 This flag is only valid if is_debug_types is true.
653 We can't read a CU directly from a DWO file: There are required
654 attributes in the stub. */
655 unsigned int reading_dwo_directly : 1;
656
7ee85ab1
DE
657 /* Non-zero if the TU has been read.
658 This is used to assist the "Stay in DWO Optimization" for Fission:
659 When reading a DWO, it's faster to read TUs from the DWO instead of
660 fetching them from random other DWOs (due to comdat folding).
661 If the TU has already been read, the optimization is unnecessary
662 (and unwise - we don't want to change where gdb thinks the TU lives
663 "midflight").
664 This flag is only valid if is_debug_types is true. */
665 unsigned int tu_read : 1;
666
3019eac3
DE
667 /* The section this CU/TU lives in.
668 If the DIE refers to a DWO file, this is always the original die,
669 not the DWO file. */
8a0459fd 670 struct dwarf2_section_info *section;
348e048f 671
17ea53c3 672 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
673 of the CU cache it gets reset to NULL again. This is left as NULL for
674 dummy CUs (a CU header, but nothing else). */
ae038cb0 675 struct dwarf2_cu *cu;
1c379e20 676
9cdd5dbd
DE
677 /* The corresponding objfile.
678 Normally we can get the objfile from dwarf2_per_objfile.
679 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
680 struct objfile *objfile;
681
fffbe6a8
YQ
682 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
683 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
684 union
685 {
686 /* The partial symbol table associated with this compilation unit,
95554aad 687 or NULL for unread partial units. */
9291a0cd
TT
688 struct partial_symtab *psymtab;
689
690 /* Data needed by the "quick" functions. */
691 struct dwarf2_per_cu_quick_data *quick;
692 } v;
95554aad 693
796a7ff8
DE
694 /* The CUs we import using DW_TAG_imported_unit. This is filled in
695 while reading psymtabs, used to compute the psymtab dependencies,
696 and then cleared. Then it is filled in again while reading full
697 symbols, and only deleted when the objfile is destroyed.
698
699 This is also used to work around a difference between the way gold
700 generates .gdb_index version <=7 and the way gdb does. Arguably this
701 is a gold bug. For symbols coming from TUs, gold records in the index
702 the CU that includes the TU instead of the TU itself. This breaks
703 dw2_lookup_symbol: It assumes that if the index says symbol X lives
704 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
705 will find X. Alas TUs live in their own symtab, so after expanding CU Y
706 we need to look in TU Z to find X. Fortunately, this is akin to
707 DW_TAG_imported_unit, so we just use the same mechanism: For
708 .gdb_index version <=7 this also records the TUs that the CU referred
709 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
710 indices so we only pay a price for gold generated indices.
711 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 712 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
713};
714
348e048f
DE
715/* Entry in the signatured_types hash table. */
716
717struct signatured_type
718{
42e7ad6c 719 /* The "per_cu" object of this type.
ac9ec31b 720 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
721 N.B.: This is the first member so that it's easy to convert pointers
722 between them. */
723 struct dwarf2_per_cu_data per_cu;
724
3019eac3 725 /* The type's signature. */
348e048f
DE
726 ULONGEST signature;
727
3019eac3 728 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
729 If this TU is a DWO stub and the definition lives in a DWO file
730 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
731 cu_offset type_offset_in_tu;
732
733 /* Offset in the section of the type's DIE.
734 If the definition lives in a DWO file, this is the offset in the
735 .debug_types.dwo section.
736 The value is zero until the actual value is known.
737 Zero is otherwise not a valid section offset. */
738 sect_offset type_offset_in_section;
0186c6a7
DE
739
740 /* Type units are grouped by their DW_AT_stmt_list entry so that they
741 can share them. This points to the containing symtab. */
742 struct type_unit_group *type_unit_group;
ac9ec31b
DE
743
744 /* The type.
745 The first time we encounter this type we fully read it in and install it
746 in the symbol tables. Subsequent times we only need the type. */
747 struct type *type;
a2ce51a0
DE
748
749 /* Containing DWO unit.
750 This field is valid iff per_cu.reading_dwo_directly. */
751 struct dwo_unit *dwo_unit;
348e048f
DE
752};
753
0186c6a7
DE
754typedef struct signatured_type *sig_type_ptr;
755DEF_VEC_P (sig_type_ptr);
756
094b34ac
DE
757/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
758 This includes type_unit_group and quick_file_names. */
759
760struct stmt_list_hash
761{
762 /* The DWO unit this table is from or NULL if there is none. */
763 struct dwo_unit *dwo_unit;
764
765 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 766 sect_offset line_sect_off;
094b34ac
DE
767};
768
f4dc4d17
DE
769/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
770 an object of this type. */
771
772struct type_unit_group
773{
0186c6a7 774 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
775 To simplify things we create an artificial CU that "includes" all the
776 type units using this stmt_list so that the rest of the code still has
777 a "per_cu" handle on the symtab.
778 This PER_CU is recognized by having no section. */
8a0459fd 779#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
780 struct dwarf2_per_cu_data per_cu;
781
0186c6a7
DE
782 /* The TUs that share this DW_AT_stmt_list entry.
783 This is added to while parsing type units to build partial symtabs,
784 and is deleted afterwards and not used again. */
785 VEC (sig_type_ptr) *tus;
f4dc4d17 786
43f3e411 787 /* The compunit symtab.
094b34ac 788 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
789 so we create an essentially anonymous symtab as the compunit symtab. */
790 struct compunit_symtab *compunit_symtab;
f4dc4d17 791
094b34ac
DE
792 /* The data used to construct the hash key. */
793 struct stmt_list_hash hash;
f4dc4d17
DE
794
795 /* The number of symtabs from the line header.
796 The value here must match line_header.num_file_names. */
797 unsigned int num_symtabs;
798
799 /* The symbol tables for this TU (obtained from the files listed in
800 DW_AT_stmt_list).
801 WARNING: The order of entries here must match the order of entries
802 in the line header. After the first TU using this type_unit_group, the
803 line header for the subsequent TUs is recreated from this. This is done
804 because we need to use the same symtabs for each TU using the same
805 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
806 there's no guarantee the line header doesn't have duplicate entries. */
807 struct symtab **symtabs;
808};
809
73869dc2 810/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
811
812struct dwo_sections
813{
814 struct dwarf2_section_info abbrev;
3019eac3
DE
815 struct dwarf2_section_info line;
816 struct dwarf2_section_info loc;
43988095 817 struct dwarf2_section_info loclists;
09262596
DE
818 struct dwarf2_section_info macinfo;
819 struct dwarf2_section_info macro;
3019eac3
DE
820 struct dwarf2_section_info str;
821 struct dwarf2_section_info str_offsets;
80626a55
DE
822 /* In the case of a virtual DWO file, these two are unused. */
823 struct dwarf2_section_info info;
3019eac3
DE
824 VEC (dwarf2_section_info_def) *types;
825};
826
c88ee1f0 827/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
828
829struct dwo_unit
830{
831 /* Backlink to the containing struct dwo_file. */
832 struct dwo_file *dwo_file;
833
834 /* The "id" that distinguishes this CU/TU.
835 .debug_info calls this "dwo_id", .debug_types calls this "signature".
836 Since signatures came first, we stick with it for consistency. */
837 ULONGEST signature;
838
839 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 840 struct dwarf2_section_info *section;
3019eac3 841
9c541725
PA
842 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
843 sect_offset sect_off;
3019eac3
DE
844 unsigned int length;
845
846 /* For types, offset in the type's DIE of the type defined by this TU. */
847 cu_offset type_offset_in_tu;
848};
849
73869dc2
DE
850/* include/dwarf2.h defines the DWP section codes.
851 It defines a max value but it doesn't define a min value, which we
852 use for error checking, so provide one. */
853
854enum dwp_v2_section_ids
855{
856 DW_SECT_MIN = 1
857};
858
80626a55 859/* Data for one DWO file.
57d63ce2
DE
860
861 This includes virtual DWO files (a virtual DWO file is a DWO file as it
862 appears in a DWP file). DWP files don't really have DWO files per se -
863 comdat folding of types "loses" the DWO file they came from, and from
864 a high level view DWP files appear to contain a mass of random types.
865 However, to maintain consistency with the non-DWP case we pretend DWP
866 files contain virtual DWO files, and we assign each TU with one virtual
867 DWO file (generally based on the line and abbrev section offsets -
868 a heuristic that seems to work in practice). */
3019eac3
DE
869
870struct dwo_file
871{
0ac5b59e 872 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
873 For virtual DWO files the name is constructed from the section offsets
874 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
875 from related CU+TUs. */
0ac5b59e
DE
876 const char *dwo_name;
877
878 /* The DW_AT_comp_dir attribute. */
879 const char *comp_dir;
3019eac3 880
80626a55
DE
881 /* The bfd, when the file is open. Otherwise this is NULL.
882 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
883 bfd *dbfd;
3019eac3 884
73869dc2
DE
885 /* The sections that make up this DWO file.
886 Remember that for virtual DWO files in DWP V2, these are virtual
887 sections (for lack of a better name). */
3019eac3
DE
888 struct dwo_sections sections;
889
33c5cd75
DB
890 /* The CUs in the file.
891 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
892 an extension to handle LLVM's Link Time Optimization output (where
893 multiple source files may be compiled into a single object/dwo pair). */
894 htab_t cus;
3019eac3
DE
895
896 /* Table of TUs in the file.
897 Each element is a struct dwo_unit. */
898 htab_t tus;
899};
900
80626a55
DE
901/* These sections are what may appear in a DWP file. */
902
903struct dwp_sections
904{
73869dc2 905 /* These are used by both DWP version 1 and 2. */
80626a55
DE
906 struct dwarf2_section_info str;
907 struct dwarf2_section_info cu_index;
908 struct dwarf2_section_info tu_index;
73869dc2
DE
909
910 /* These are only used by DWP version 2 files.
911 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
912 sections are referenced by section number, and are not recorded here.
913 In DWP version 2 there is at most one copy of all these sections, each
914 section being (effectively) comprised of the concatenation of all of the
915 individual sections that exist in the version 1 format.
916 To keep the code simple we treat each of these concatenated pieces as a
917 section itself (a virtual section?). */
918 struct dwarf2_section_info abbrev;
919 struct dwarf2_section_info info;
920 struct dwarf2_section_info line;
921 struct dwarf2_section_info loc;
922 struct dwarf2_section_info macinfo;
923 struct dwarf2_section_info macro;
924 struct dwarf2_section_info str_offsets;
925 struct dwarf2_section_info types;
80626a55
DE
926};
927
73869dc2
DE
928/* These sections are what may appear in a virtual DWO file in DWP version 1.
929 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 930
73869dc2 931struct virtual_v1_dwo_sections
80626a55
DE
932{
933 struct dwarf2_section_info abbrev;
934 struct dwarf2_section_info line;
935 struct dwarf2_section_info loc;
936 struct dwarf2_section_info macinfo;
937 struct dwarf2_section_info macro;
938 struct dwarf2_section_info str_offsets;
939 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 940 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
941 struct dwarf2_section_info info_or_types;
942};
943
73869dc2
DE
944/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
945 In version 2, the sections of the DWO files are concatenated together
946 and stored in one section of that name. Thus each ELF section contains
947 several "virtual" sections. */
948
949struct virtual_v2_dwo_sections
950{
951 bfd_size_type abbrev_offset;
952 bfd_size_type abbrev_size;
953
954 bfd_size_type line_offset;
955 bfd_size_type line_size;
956
957 bfd_size_type loc_offset;
958 bfd_size_type loc_size;
959
960 bfd_size_type macinfo_offset;
961 bfd_size_type macinfo_size;
962
963 bfd_size_type macro_offset;
964 bfd_size_type macro_size;
965
966 bfd_size_type str_offsets_offset;
967 bfd_size_type str_offsets_size;
968
969 /* Each DWP hash table entry records one CU or one TU.
970 That is recorded here, and copied to dwo_unit.section. */
971 bfd_size_type info_or_types_offset;
972 bfd_size_type info_or_types_size;
973};
974
80626a55
DE
975/* Contents of DWP hash tables. */
976
977struct dwp_hash_table
978{
73869dc2 979 uint32_t version, nr_columns;
80626a55 980 uint32_t nr_units, nr_slots;
73869dc2
DE
981 const gdb_byte *hash_table, *unit_table;
982 union
983 {
984 struct
985 {
986 const gdb_byte *indices;
987 } v1;
988 struct
989 {
990 /* This is indexed by column number and gives the id of the section
991 in that column. */
992#define MAX_NR_V2_DWO_SECTIONS \
993 (1 /* .debug_info or .debug_types */ \
994 + 1 /* .debug_abbrev */ \
995 + 1 /* .debug_line */ \
996 + 1 /* .debug_loc */ \
997 + 1 /* .debug_str_offsets */ \
998 + 1 /* .debug_macro or .debug_macinfo */)
999 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1000 const gdb_byte *offsets;
1001 const gdb_byte *sizes;
1002 } v2;
1003 } section_pool;
80626a55
DE
1004};
1005
1006/* Data for one DWP file. */
1007
1008struct dwp_file
1009{
1010 /* Name of the file. */
1011 const char *name;
1012
73869dc2
DE
1013 /* File format version. */
1014 int version;
1015
93417882 1016 /* The bfd. */
80626a55
DE
1017 bfd *dbfd;
1018
1019 /* Section info for this file. */
1020 struct dwp_sections sections;
1021
57d63ce2 1022 /* Table of CUs in the file. */
80626a55
DE
1023 const struct dwp_hash_table *cus;
1024
1025 /* Table of TUs in the file. */
1026 const struct dwp_hash_table *tus;
1027
19ac8c2e
DE
1028 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1029 htab_t loaded_cus;
1030 htab_t loaded_tus;
80626a55 1031
73869dc2
DE
1032 /* Table to map ELF section numbers to their sections.
1033 This is only needed for the DWP V1 file format. */
80626a55
DE
1034 unsigned int num_sections;
1035 asection **elf_sections;
1036};
1037
36586728
TT
1038/* This represents a '.dwz' file. */
1039
1040struct dwz_file
1041{
1042 /* A dwz file can only contain a few sections. */
1043 struct dwarf2_section_info abbrev;
1044 struct dwarf2_section_info info;
1045 struct dwarf2_section_info str;
1046 struct dwarf2_section_info line;
1047 struct dwarf2_section_info macro;
2ec9a5e0 1048 struct dwarf2_section_info gdb_index;
36586728
TT
1049
1050 /* The dwz's BFD. */
1051 bfd *dwz_bfd;
1052};
1053
0963b4bd
MS
1054/* Struct used to pass misc. parameters to read_die_and_children, et
1055 al. which are used for both .debug_info and .debug_types dies.
1056 All parameters here are unchanging for the life of the call. This
dee91e82 1057 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1058
1059struct die_reader_specs
1060{
a32a8923 1061 /* The bfd of die_section. */
93311388
DE
1062 bfd* abfd;
1063
1064 /* The CU of the DIE we are parsing. */
1065 struct dwarf2_cu *cu;
1066
80626a55 1067 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1068 struct dwo_file *dwo_file;
1069
dee91e82 1070 /* The section the die comes from.
3019eac3 1071 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1072 struct dwarf2_section_info *die_section;
1073
1074 /* die_section->buffer. */
d521ce57 1075 const gdb_byte *buffer;
f664829e
DE
1076
1077 /* The end of the buffer. */
1078 const gdb_byte *buffer_end;
a2ce51a0
DE
1079
1080 /* The value of the DW_AT_comp_dir attribute. */
1081 const char *comp_dir;
93311388
DE
1082};
1083
fd820528 1084/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1085typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1086 const gdb_byte *info_ptr,
dee91e82
DE
1087 struct die_info *comp_unit_die,
1088 int has_children,
1089 void *data);
1090
ecfb656c
PA
1091/* A 1-based directory index. This is a strong typedef to prevent
1092 accidentally using a directory index as a 0-based index into an
1093 array/vector. */
1094enum class dir_index : unsigned int {};
1095
1096/* Likewise, a 1-based file name index. */
1097enum class file_name_index : unsigned int {};
1098
52059ffd
TT
1099struct file_entry
1100{
fff8551c
PA
1101 file_entry () = default;
1102
ecfb656c 1103 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1104 unsigned int mod_time_, unsigned int length_)
1105 : name (name_),
ecfb656c 1106 d_index (d_index_),
fff8551c
PA
1107 mod_time (mod_time_),
1108 length (length_)
1109 {}
1110
ecfb656c
PA
1111 /* Return the include directory at D_INDEX stored in LH. Returns
1112 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1113 const char *include_dir (const line_header *lh) const;
1114
fff8551c
PA
1115 /* The file name. Note this is an observing pointer. The memory is
1116 owned by debug_line_buffer. */
1117 const char *name {};
1118
8c43009f 1119 /* The directory index (1-based). */
ecfb656c 1120 dir_index d_index {};
fff8551c
PA
1121
1122 unsigned int mod_time {};
1123
1124 unsigned int length {};
1125
1126 /* True if referenced by the Line Number Program. */
1127 bool included_p {};
1128
83769d0b 1129 /* The associated symbol table, if any. */
fff8551c 1130 struct symtab *symtab {};
52059ffd
TT
1131};
1132
debd256d
JB
1133/* The line number information for a compilation unit (found in the
1134 .debug_line section) begins with a "statement program header",
1135 which contains the following information. */
1136struct line_header
1137{
fff8551c
PA
1138 line_header ()
1139 : offset_in_dwz {}
1140 {}
1141
1142 /* Add an entry to the include directory table. */
1143 void add_include_dir (const char *include_dir);
1144
1145 /* Add an entry to the file name table. */
ecfb656c 1146 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1147 unsigned int mod_time, unsigned int length);
1148
ecfb656c 1149 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1150 is out of bounds. */
ecfb656c 1151 const char *include_dir_at (dir_index index) const
8c43009f 1152 {
ecfb656c
PA
1153 /* Convert directory index number (1-based) to vector index
1154 (0-based). */
1155 size_t vec_index = to_underlying (index) - 1;
1156
1157 if (vec_index >= include_dirs.size ())
8c43009f 1158 return NULL;
ecfb656c 1159 return include_dirs[vec_index];
8c43009f
PA
1160 }
1161
ecfb656c 1162 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1163 is out of bounds. */
ecfb656c 1164 file_entry *file_name_at (file_name_index index)
8c43009f 1165 {
ecfb656c
PA
1166 /* Convert file name index number (1-based) to vector index
1167 (0-based). */
1168 size_t vec_index = to_underlying (index) - 1;
1169
1170 if (vec_index >= file_names.size ())
fff8551c 1171 return NULL;
ecfb656c 1172 return &file_names[vec_index];
fff8551c
PA
1173 }
1174
1175 /* Const version of the above. */
1176 const file_entry *file_name_at (unsigned int index) const
1177 {
1178 if (index >= file_names.size ())
8c43009f
PA
1179 return NULL;
1180 return &file_names[index];
1181 }
1182
527f3840 1183 /* Offset of line number information in .debug_line section. */
9c541725 1184 sect_offset sect_off {};
527f3840
JK
1185
1186 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1187 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1188
1189 unsigned int total_length {};
1190 unsigned short version {};
1191 unsigned int header_length {};
1192 unsigned char minimum_instruction_length {};
1193 unsigned char maximum_ops_per_instruction {};
1194 unsigned char default_is_stmt {};
1195 int line_base {};
1196 unsigned char line_range {};
1197 unsigned char opcode_base {};
debd256d
JB
1198
1199 /* standard_opcode_lengths[i] is the number of operands for the
1200 standard opcode whose value is i. This means that
1201 standard_opcode_lengths[0] is unused, and the last meaningful
1202 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1203 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1204
fff8551c
PA
1205 /* The include_directories table. Note these are observing
1206 pointers. The memory is owned by debug_line_buffer. */
1207 std::vector<const char *> include_dirs;
debd256d 1208
fff8551c
PA
1209 /* The file_names table. */
1210 std::vector<file_entry> file_names;
debd256d
JB
1211
1212 /* The start and end of the statement program following this
6502dd73 1213 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1214 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1215};
c906108c 1216
fff8551c
PA
1217typedef std::unique_ptr<line_header> line_header_up;
1218
8c43009f
PA
1219const char *
1220file_entry::include_dir (const line_header *lh) const
1221{
ecfb656c 1222 return lh->include_dir_at (d_index);
8c43009f
PA
1223}
1224
c906108c 1225/* When we construct a partial symbol table entry we only
0963b4bd 1226 need this much information. */
c906108c
SS
1227struct partial_die_info
1228 {
72bf9492 1229 /* Offset of this DIE. */
9c541725 1230 sect_offset sect_off;
72bf9492
DJ
1231
1232 /* DWARF-2 tag for this DIE. */
1233 ENUM_BITFIELD(dwarf_tag) tag : 16;
1234
72bf9492
DJ
1235 /* Assorted flags describing the data found in this DIE. */
1236 unsigned int has_children : 1;
1237 unsigned int is_external : 1;
1238 unsigned int is_declaration : 1;
1239 unsigned int has_type : 1;
1240 unsigned int has_specification : 1;
1241 unsigned int has_pc_info : 1;
481860b3 1242 unsigned int may_be_inlined : 1;
72bf9492 1243
0c1b455e
TT
1244 /* This DIE has been marked DW_AT_main_subprogram. */
1245 unsigned int main_subprogram : 1;
1246
72bf9492
DJ
1247 /* Flag set if the SCOPE field of this structure has been
1248 computed. */
1249 unsigned int scope_set : 1;
1250
fa4028e9
JB
1251 /* Flag set if the DIE has a byte_size attribute. */
1252 unsigned int has_byte_size : 1;
1253
ff908ebf
AW
1254 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1255 unsigned int has_const_value : 1;
1256
98bfdba5
PA
1257 /* Flag set if any of the DIE's children are template arguments. */
1258 unsigned int has_template_arguments : 1;
1259
abc72ce4
DE
1260 /* Flag set if fixup_partial_die has been called on this die. */
1261 unsigned int fixup_called : 1;
1262
36586728
TT
1263 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1264 unsigned int is_dwz : 1;
1265
1266 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1267 unsigned int spec_is_dwz : 1;
1268
72bf9492 1269 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1270 sometimes a default name for unnamed DIEs. */
15d034d0 1271 const char *name;
72bf9492 1272
abc72ce4
DE
1273 /* The linkage name, if present. */
1274 const char *linkage_name;
1275
72bf9492
DJ
1276 /* The scope to prepend to our children. This is generally
1277 allocated on the comp_unit_obstack, so will disappear
1278 when this compilation unit leaves the cache. */
15d034d0 1279 const char *scope;
72bf9492 1280
95554aad
TT
1281 /* Some data associated with the partial DIE. The tag determines
1282 which field is live. */
1283 union
1284 {
1285 /* The location description associated with this DIE, if any. */
1286 struct dwarf_block *locdesc;
1287 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1288 sect_offset sect_off;
95554aad 1289 } d;
72bf9492
DJ
1290
1291 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1292 CORE_ADDR lowpc;
1293 CORE_ADDR highpc;
72bf9492 1294
93311388 1295 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1296 DW_AT_sibling, if any. */
abc72ce4
DE
1297 /* NOTE: This member isn't strictly necessary, read_partial_die could
1298 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1299 const gdb_byte *sibling;
72bf9492
DJ
1300
1301 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1302 DW_AT_specification (or DW_AT_abstract_origin or
1303 DW_AT_extension). */
b64f50a1 1304 sect_offset spec_offset;
72bf9492
DJ
1305
1306 /* Pointers to this DIE's parent, first child, and next sibling,
1307 if any. */
1308 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1309 };
1310
0963b4bd 1311/* This data structure holds the information of an abbrev. */
c906108c
SS
1312struct abbrev_info
1313 {
1314 unsigned int number; /* number identifying abbrev */
1315 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1316 unsigned short has_children; /* boolean */
1317 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1318 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1319 struct abbrev_info *next; /* next in chain */
1320 };
1321
1322struct attr_abbrev
1323 {
9d25dd43
DE
1324 ENUM_BITFIELD(dwarf_attribute) name : 16;
1325 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1326
1327 /* It is valid only if FORM is DW_FORM_implicit_const. */
1328 LONGEST implicit_const;
c906108c
SS
1329 };
1330
433df2d4
DE
1331/* Size of abbrev_table.abbrev_hash_table. */
1332#define ABBREV_HASH_SIZE 121
1333
1334/* Top level data structure to contain an abbreviation table. */
1335
1336struct abbrev_table
1337{
f4dc4d17
DE
1338 /* Where the abbrev table came from.
1339 This is used as a sanity check when the table is used. */
9c541725 1340 sect_offset sect_off;
433df2d4
DE
1341
1342 /* Storage for the abbrev table. */
1343 struct obstack abbrev_obstack;
1344
1345 /* Hash table of abbrevs.
1346 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1347 It could be statically allocated, but the previous code didn't so we
1348 don't either. */
1349 struct abbrev_info **abbrevs;
1350};
1351
0963b4bd 1352/* Attributes have a name and a value. */
b60c80d6
DJ
1353struct attribute
1354 {
9d25dd43 1355 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1356 ENUM_BITFIELD(dwarf_form) form : 15;
1357
1358 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1359 field should be in u.str (existing only for DW_STRING) but it is kept
1360 here for better struct attribute alignment. */
1361 unsigned int string_is_canonical : 1;
1362
b60c80d6
DJ
1363 union
1364 {
15d034d0 1365 const char *str;
b60c80d6 1366 struct dwarf_block *blk;
43bbcdc2
PH
1367 ULONGEST unsnd;
1368 LONGEST snd;
b60c80d6 1369 CORE_ADDR addr;
ac9ec31b 1370 ULONGEST signature;
b60c80d6
DJ
1371 }
1372 u;
1373 };
1374
0963b4bd 1375/* This data structure holds a complete die structure. */
c906108c
SS
1376struct die_info
1377 {
76815b17
DE
1378 /* DWARF-2 tag for this DIE. */
1379 ENUM_BITFIELD(dwarf_tag) tag : 16;
1380
1381 /* Number of attributes */
98bfdba5
PA
1382 unsigned char num_attrs;
1383
1384 /* True if we're presently building the full type name for the
1385 type derived from this DIE. */
1386 unsigned char building_fullname : 1;
76815b17 1387
adde2bff
DE
1388 /* True if this die is in process. PR 16581. */
1389 unsigned char in_process : 1;
1390
76815b17
DE
1391 /* Abbrev number */
1392 unsigned int abbrev;
1393
93311388 1394 /* Offset in .debug_info or .debug_types section. */
9c541725 1395 sect_offset sect_off;
78ba4af6
JB
1396
1397 /* The dies in a compilation unit form an n-ary tree. PARENT
1398 points to this die's parent; CHILD points to the first child of
1399 this node; and all the children of a given node are chained
4950bc1c 1400 together via their SIBLING fields. */
639d11d3
DC
1401 struct die_info *child; /* Its first child, if any. */
1402 struct die_info *sibling; /* Its next sibling, if any. */
1403 struct die_info *parent; /* Its parent, if any. */
c906108c 1404
b60c80d6
DJ
1405 /* An array of attributes, with NUM_ATTRS elements. There may be
1406 zero, but it's not common and zero-sized arrays are not
1407 sufficiently portable C. */
1408 struct attribute attrs[1];
c906108c
SS
1409 };
1410
0963b4bd 1411/* Get at parts of an attribute structure. */
c906108c
SS
1412
1413#define DW_STRING(attr) ((attr)->u.str)
8285870a 1414#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1415#define DW_UNSND(attr) ((attr)->u.unsnd)
1416#define DW_BLOCK(attr) ((attr)->u.blk)
1417#define DW_SND(attr) ((attr)->u.snd)
1418#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1419#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1420
0963b4bd 1421/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1422struct dwarf_block
1423 {
56eb65bd 1424 size_t size;
1d6edc3c
JK
1425
1426 /* Valid only if SIZE is not zero. */
d521ce57 1427 const gdb_byte *data;
c906108c
SS
1428 };
1429
c906108c
SS
1430#ifndef ATTR_ALLOC_CHUNK
1431#define ATTR_ALLOC_CHUNK 4
1432#endif
1433
c906108c
SS
1434/* Allocate fields for structs, unions and enums in this size. */
1435#ifndef DW_FIELD_ALLOC_CHUNK
1436#define DW_FIELD_ALLOC_CHUNK 4
1437#endif
1438
c906108c
SS
1439/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1440 but this would require a corresponding change in unpack_field_as_long
1441 and friends. */
1442static int bits_per_byte = 8;
1443
52059ffd
TT
1444struct nextfield
1445{
1446 struct nextfield *next;
1447 int accessibility;
1448 int virtuality;
1449 struct field field;
1450};
1451
1452struct nextfnfield
1453{
1454 struct nextfnfield *next;
1455 struct fn_field fnfield;
1456};
1457
1458struct fnfieldlist
1459{
1460 const char *name;
1461 int length;
1462 struct nextfnfield *head;
1463};
1464
1465struct typedef_field_list
1466{
1467 struct typedef_field field;
1468 struct typedef_field_list *next;
1469};
1470
c906108c
SS
1471/* The routines that read and process dies for a C struct or C++ class
1472 pass lists of data member fields and lists of member function fields
1473 in an instance of a field_info structure, as defined below. */
1474struct field_info
c5aa993b 1475 {
0963b4bd 1476 /* List of data member and baseclasses fields. */
52059ffd 1477 struct nextfield *fields, *baseclasses;
c906108c 1478
7d0ccb61 1479 /* Number of fields (including baseclasses). */
c5aa993b 1480 int nfields;
c906108c 1481
c5aa993b
JM
1482 /* Number of baseclasses. */
1483 int nbaseclasses;
c906108c 1484
c5aa993b
JM
1485 /* Set if the accesibility of one of the fields is not public. */
1486 int non_public_fields;
c906108c 1487
c5aa993b
JM
1488 /* Member function fields array, entries are allocated in the order they
1489 are encountered in the object file. */
52059ffd 1490 struct nextfnfield *fnfields;
c906108c 1491
c5aa993b
JM
1492 /* Member function fieldlist array, contains name of possibly overloaded
1493 member function, number of overloaded member functions and a pointer
1494 to the head of the member function field chain. */
52059ffd 1495 struct fnfieldlist *fnfieldlists;
c906108c 1496
c5aa993b
JM
1497 /* Number of entries in the fnfieldlists array. */
1498 int nfnfields;
98751a41
JK
1499
1500 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1501 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1502 struct typedef_field_list *typedef_field_list;
98751a41 1503 unsigned typedef_field_list_count;
c5aa993b 1504 };
c906108c 1505
10b3939b
DJ
1506/* One item on the queue of compilation units to read in full symbols
1507 for. */
1508struct dwarf2_queue_item
1509{
1510 struct dwarf2_per_cu_data *per_cu;
95554aad 1511 enum language pretend_language;
10b3939b
DJ
1512 struct dwarf2_queue_item *next;
1513};
1514
1515/* The current queue. */
1516static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1517
ae038cb0
DJ
1518/* Loaded secondary compilation units are kept in memory until they
1519 have not been referenced for the processing of this many
1520 compilation units. Set this to zero to disable caching. Cache
1521 sizes of up to at least twenty will improve startup time for
1522 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1523static int dwarf_max_cache_age = 5;
920d2a44 1524static void
b4f54984
DE
1525show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1526 struct cmd_list_element *c, const char *value)
920d2a44 1527{
3e43a32a 1528 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1529 "DWARF compilation units is %s.\n"),
920d2a44
AC
1530 value);
1531}
4390d890 1532\f
c906108c
SS
1533/* local function prototypes */
1534
a32a8923
DE
1535static const char *get_section_name (const struct dwarf2_section_info *);
1536
1537static const char *get_section_file_name (const struct dwarf2_section_info *);
1538
918dd910
JK
1539static void dwarf2_find_base_address (struct die_info *die,
1540 struct dwarf2_cu *cu);
1541
0018ea6f
DE
1542static struct partial_symtab *create_partial_symtab
1543 (struct dwarf2_per_cu_data *per_cu, const char *name);
1544
f1902523
JK
1545static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1546 const gdb_byte *info_ptr,
1547 struct die_info *type_unit_die,
1548 int has_children, void *data);
1549
c67a9c90 1550static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1551
72bf9492
DJ
1552static void scan_partial_symbols (struct partial_die_info *,
1553 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1554 int, struct dwarf2_cu *);
c906108c 1555
72bf9492
DJ
1556static void add_partial_symbol (struct partial_die_info *,
1557 struct dwarf2_cu *);
63d06c5c 1558
72bf9492
DJ
1559static void add_partial_namespace (struct partial_die_info *pdi,
1560 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1561 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1562
5d7cb8df 1563static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1564 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1565 struct dwarf2_cu *cu);
1566
72bf9492
DJ
1567static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1568 struct dwarf2_cu *cu);
91c24f0a 1569
bc30ff58
JB
1570static void add_partial_subprogram (struct partial_die_info *pdi,
1571 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1572 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1573
257e7a09
YQ
1574static void dwarf2_read_symtab (struct partial_symtab *,
1575 struct objfile *);
c906108c 1576
a14ed312 1577static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1578
433df2d4
DE
1579static struct abbrev_info *abbrev_table_lookup_abbrev
1580 (const struct abbrev_table *, unsigned int);
1581
1582static struct abbrev_table *abbrev_table_read_table
1583 (struct dwarf2_section_info *, sect_offset);
1584
1585static void abbrev_table_free (struct abbrev_table *);
1586
f4dc4d17
DE
1587static void abbrev_table_free_cleanup (void *);
1588
dee91e82
DE
1589static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1590 struct dwarf2_section_info *);
c906108c 1591
f3dd6933 1592static void dwarf2_free_abbrev_table (void *);
c906108c 1593
d521ce57 1594static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1595
dee91e82 1596static struct partial_die_info *load_partial_dies
d521ce57 1597 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1598
d521ce57
TT
1599static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1600 struct partial_die_info *,
1601 struct abbrev_info *,
1602 unsigned int,
1603 const gdb_byte *);
c906108c 1604
36586728 1605static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1606 struct dwarf2_cu *);
72bf9492
DJ
1607
1608static void fixup_partial_die (struct partial_die_info *,
1609 struct dwarf2_cu *);
1610
d521ce57
TT
1611static const gdb_byte *read_attribute (const struct die_reader_specs *,
1612 struct attribute *, struct attr_abbrev *,
1613 const gdb_byte *);
a8329558 1614
a1855c1d 1615static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1616
a1855c1d 1617static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1618
a1855c1d 1619static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1620
a1855c1d 1621static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1622
a1855c1d 1623static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1624
d521ce57 1625static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1626 unsigned int *);
c906108c 1627
d521ce57 1628static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1629
1630static LONGEST read_checked_initial_length_and_offset
d521ce57 1631 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1632 unsigned int *, unsigned int *);
613e1657 1633
d521ce57
TT
1634static LONGEST read_offset (bfd *, const gdb_byte *,
1635 const struct comp_unit_head *,
c764a876
DE
1636 unsigned int *);
1637
d521ce57 1638static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1639
f4dc4d17
DE
1640static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1641 sect_offset);
1642
d521ce57 1643static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1644
d521ce57 1645static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1646
d521ce57
TT
1647static const char *read_indirect_string (bfd *, const gdb_byte *,
1648 const struct comp_unit_head *,
1649 unsigned int *);
4bdf3d34 1650
43988095
JK
1651static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1652 const struct comp_unit_head *,
1653 unsigned int *);
36586728 1654
43988095 1655static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1656
d521ce57 1657static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1658
d521ce57
TT
1659static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1660 const gdb_byte *,
3019eac3
DE
1661 unsigned int *);
1662
d521ce57 1663static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1664 ULONGEST str_index);
3019eac3 1665
e142c38c 1666static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1667
e142c38c
DJ
1668static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1669 struct dwarf2_cu *);
c906108c 1670
348e048f 1671static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1672 unsigned int);
348e048f 1673
7d45c7c3
KB
1674static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1675 struct dwarf2_cu *cu);
1676
05cf31d1
JB
1677static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1678 struct dwarf2_cu *cu);
1679
e142c38c 1680static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1681
e142c38c 1682static struct die_info *die_specification (struct die_info *die,
f2f0e013 1683 struct dwarf2_cu **);
63d06c5c 1684
9c541725 1685static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1686 struct dwarf2_cu *cu);
debd256d 1687
f3f5162e 1688static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1689 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1690 CORE_ADDR, int decode_mapping);
c906108c 1691
4d663531 1692static void dwarf2_start_subfile (const char *, const char *);
c906108c 1693
43f3e411
DE
1694static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1695 const char *, const char *,
1696 CORE_ADDR);
f4dc4d17 1697
a14ed312 1698static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1699 struct dwarf2_cu *);
c906108c 1700
34eaf542
TT
1701static struct symbol *new_symbol_full (struct die_info *, struct type *,
1702 struct dwarf2_cu *, struct symbol *);
1703
ff39bb5e 1704static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1705 struct dwarf2_cu *);
c906108c 1706
ff39bb5e 1707static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1708 struct type *type,
1709 const char *name,
1710 struct obstack *obstack,
12df843f 1711 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1712 const gdb_byte **bytes,
98bfdba5 1713 struct dwarf2_locexpr_baton **baton);
2df3850c 1714
e7c27a73 1715static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1716
b4ba55a1
JB
1717static int need_gnat_info (struct dwarf2_cu *);
1718
3e43a32a
MS
1719static struct type *die_descriptive_type (struct die_info *,
1720 struct dwarf2_cu *);
b4ba55a1
JB
1721
1722static void set_descriptive_type (struct type *, struct die_info *,
1723 struct dwarf2_cu *);
1724
e7c27a73
DJ
1725static struct type *die_containing_type (struct die_info *,
1726 struct dwarf2_cu *);
c906108c 1727
ff39bb5e 1728static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1729 struct dwarf2_cu *);
c906108c 1730
f792889a 1731static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1732
673bfd45
DE
1733static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1734
0d5cff50 1735static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1736
6e70227d 1737static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1738 const char *suffix, int physname,
1739 struct dwarf2_cu *cu);
63d06c5c 1740
e7c27a73 1741static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1742
348e048f
DE
1743static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1744
e7c27a73 1745static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1746
e7c27a73 1747static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1748
96408a79
SA
1749static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1750
ff013f42
JK
1751static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1752 struct dwarf2_cu *, struct partial_symtab *);
1753
3a2b436a 1754/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1755 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1756enum pc_bounds_kind
1757{
e385593e 1758 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1759 PC_BOUNDS_NOT_PRESENT,
1760
e385593e
JK
1761 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1762 were present but they do not form a valid range of PC addresses. */
1763 PC_BOUNDS_INVALID,
1764
3a2b436a
JK
1765 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1766 PC_BOUNDS_RANGES,
1767
1768 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1769 PC_BOUNDS_HIGH_LOW,
1770};
1771
1772static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1773 CORE_ADDR *, CORE_ADDR *,
1774 struct dwarf2_cu *,
1775 struct partial_symtab *);
c906108c 1776
fae299cd
DC
1777static void get_scope_pc_bounds (struct die_info *,
1778 CORE_ADDR *, CORE_ADDR *,
1779 struct dwarf2_cu *);
1780
801e3a5b
JB
1781static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1782 CORE_ADDR, struct dwarf2_cu *);
1783
a14ed312 1784static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1785 struct dwarf2_cu *);
c906108c 1786
a14ed312 1787static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1788 struct type *, struct dwarf2_cu *);
c906108c 1789
a14ed312 1790static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1791 struct die_info *, struct type *,
e7c27a73 1792 struct dwarf2_cu *);
c906108c 1793
a14ed312 1794static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1795 struct type *,
1796 struct dwarf2_cu *);
c906108c 1797
134d01f1 1798static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1799
e7c27a73 1800static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1801
e7c27a73 1802static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1803
5d7cb8df
JK
1804static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1805
22cee43f
PMR
1806static struct using_direct **using_directives (enum language);
1807
27aa8d6a
SW
1808static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1809
74921315
KS
1810static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1811
f55ee35c
JK
1812static struct type *read_module_type (struct die_info *die,
1813 struct dwarf2_cu *cu);
1814
38d518c9 1815static const char *namespace_name (struct die_info *die,
e142c38c 1816 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1817
134d01f1 1818static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1819
e7c27a73 1820static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1821
6e70227d 1822static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1823 struct dwarf2_cu *);
1824
bf6af496 1825static struct die_info *read_die_and_siblings_1
d521ce57 1826 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1827 struct die_info *);
639d11d3 1828
dee91e82 1829static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1830 const gdb_byte *info_ptr,
1831 const gdb_byte **new_info_ptr,
639d11d3
DC
1832 struct die_info *parent);
1833
d521ce57
TT
1834static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1835 struct die_info **, const gdb_byte *,
1836 int *, int);
3019eac3 1837
d521ce57
TT
1838static const gdb_byte *read_full_die (const struct die_reader_specs *,
1839 struct die_info **, const gdb_byte *,
1840 int *);
93311388 1841
e7c27a73 1842static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1843
15d034d0
TT
1844static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1845 struct obstack *);
71c25dea 1846
15d034d0 1847static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1848
15d034d0 1849static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1850 struct die_info *die,
1851 struct dwarf2_cu *cu);
1852
ca69b9e6
DE
1853static const char *dwarf2_physname (const char *name, struct die_info *die,
1854 struct dwarf2_cu *cu);
1855
e142c38c 1856static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1857 struct dwarf2_cu **);
9219021c 1858
f39c6ffd 1859static const char *dwarf_tag_name (unsigned int);
c906108c 1860
f39c6ffd 1861static const char *dwarf_attr_name (unsigned int);
c906108c 1862
f39c6ffd 1863static const char *dwarf_form_name (unsigned int);
c906108c 1864
a121b7c1 1865static const char *dwarf_bool_name (unsigned int);
c906108c 1866
f39c6ffd 1867static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1868
f9aca02d 1869static struct die_info *sibling_die (struct die_info *);
c906108c 1870
d97bc12b
DE
1871static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1872
1873static void dump_die_for_error (struct die_info *);
1874
1875static void dump_die_1 (struct ui_file *, int level, int max_level,
1876 struct die_info *);
c906108c 1877
d97bc12b 1878/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1879
51545339 1880static void store_in_ref_table (struct die_info *,
10b3939b 1881 struct dwarf2_cu *);
c906108c 1882
ff39bb5e 1883static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1884
ff39bb5e 1885static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1886
348e048f 1887static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1888 const struct attribute *,
348e048f
DE
1889 struct dwarf2_cu **);
1890
10b3939b 1891static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1892 const struct attribute *,
f2f0e013 1893 struct dwarf2_cu **);
c906108c 1894
348e048f 1895static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1896 const struct attribute *,
348e048f
DE
1897 struct dwarf2_cu **);
1898
ac9ec31b
DE
1899static struct type *get_signatured_type (struct die_info *, ULONGEST,
1900 struct dwarf2_cu *);
1901
1902static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1903 const struct attribute *,
ac9ec31b
DE
1904 struct dwarf2_cu *);
1905
e5fe5e75 1906static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1907
52dc124a 1908static void read_signatured_type (struct signatured_type *);
348e048f 1909
63e43d3a
PMR
1910static int attr_to_dynamic_prop (const struct attribute *attr,
1911 struct die_info *die, struct dwarf2_cu *cu,
1912 struct dynamic_prop *prop);
1913
c906108c
SS
1914/* memory allocation interface */
1915
7b5a2f43 1916static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1917
b60c80d6 1918static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1919
43f3e411 1920static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1921
6e5a29e1 1922static int attr_form_is_block (const struct attribute *);
8e19ed76 1923
6e5a29e1 1924static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1925
6e5a29e1 1926static int attr_form_is_constant (const struct attribute *);
3690dd37 1927
6e5a29e1 1928static int attr_form_is_ref (const struct attribute *);
7771576e 1929
8cf6f0b1
TT
1930static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1931 struct dwarf2_loclist_baton *baton,
ff39bb5e 1932 const struct attribute *attr);
8cf6f0b1 1933
ff39bb5e 1934static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1935 struct symbol *sym,
f1e6e072
TT
1936 struct dwarf2_cu *cu,
1937 int is_block);
4c2df51b 1938
d521ce57
TT
1939static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1940 const gdb_byte *info_ptr,
1941 struct abbrev_info *abbrev);
4bb7a0a7 1942
72bf9492
DJ
1943static void free_stack_comp_unit (void *);
1944
72bf9492
DJ
1945static hashval_t partial_die_hash (const void *item);
1946
1947static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1948
ae038cb0 1949static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1950 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1951
9816fde3 1952static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1953 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1954
1955static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1956 struct die_info *comp_unit_die,
1957 enum language pretend_language);
93311388 1958
68dc6402 1959static void free_heap_comp_unit (void *);
ae038cb0
DJ
1960
1961static void free_cached_comp_units (void *);
1962
1963static void age_cached_comp_units (void);
1964
dee91e82 1965static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1966
f792889a
DJ
1967static struct type *set_die_type (struct die_info *, struct type *,
1968 struct dwarf2_cu *);
1c379e20 1969
ae038cb0
DJ
1970static void create_all_comp_units (struct objfile *);
1971
0e50663e 1972static int create_all_type_units (struct objfile *);
1fd400ff 1973
95554aad
TT
1974static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1975 enum language);
10b3939b 1976
95554aad
TT
1977static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1978 enum language);
10b3939b 1979
f4dc4d17
DE
1980static void process_full_type_unit (struct dwarf2_per_cu_data *,
1981 enum language);
1982
10b3939b
DJ
1983static void dwarf2_add_dependence (struct dwarf2_cu *,
1984 struct dwarf2_per_cu_data *);
1985
ae038cb0
DJ
1986static void dwarf2_mark (struct dwarf2_cu *);
1987
1988static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1989
b64f50a1 1990static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1991 struct dwarf2_per_cu_data *);
673bfd45 1992
f792889a 1993static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1994
9291a0cd
TT
1995static void dwarf2_release_queue (void *dummy);
1996
95554aad
TT
1997static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1998 enum language pretend_language);
1999
a0f42c21 2000static void process_queue (void);
9291a0cd 2001
d721ba37
PA
2002/* The return type of find_file_and_directory. Note, the enclosed
2003 string pointers are only valid while this object is valid. */
2004
2005struct file_and_directory
2006{
2007 /* The filename. This is never NULL. */
2008 const char *name;
2009
2010 /* The compilation directory. NULL if not known. If we needed to
2011 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2012 points directly to the DW_AT_comp_dir string attribute owned by
2013 the obstack that owns the DIE. */
2014 const char *comp_dir;
2015
2016 /* If we needed to build a new string for comp_dir, this is what
2017 owns the storage. */
2018 std::string comp_dir_storage;
2019};
2020
2021static file_and_directory find_file_and_directory (struct die_info *die,
2022 struct dwarf2_cu *cu);
9291a0cd
TT
2023
2024static char *file_full_name (int file, struct line_header *lh,
2025 const char *comp_dir);
2026
43988095
JK
2027/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2028enum class rcuh_kind { COMPILE, TYPE };
2029
d521ce57 2030static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2031 (struct comp_unit_head *header,
2032 struct dwarf2_section_info *section,
d521ce57 2033 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2034 rcuh_kind section_kind);
36586728 2035
fd820528 2036static void init_cutu_and_read_dies
f4dc4d17
DE
2037 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2038 int use_existing_cu, int keep,
3019eac3
DE
2039 die_reader_func_ftype *die_reader_func, void *data);
2040
dee91e82
DE
2041static void init_cutu_and_read_dies_simple
2042 (struct dwarf2_per_cu_data *this_cu,
2043 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2044
673bfd45 2045static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2046
3019eac3
DE
2047static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2048
57d63ce2
DE
2049static struct dwo_unit *lookup_dwo_unit_in_dwp
2050 (struct dwp_file *dwp_file, const char *comp_dir,
2051 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2052
2053static struct dwp_file *get_dwp_file (void);
2054
3019eac3 2055static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2056 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2057
2058static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2059 (struct signatured_type *, const char *, const char *);
3019eac3 2060
89e63ee4
DE
2061static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2062
3019eac3
DE
2063static void free_dwo_file_cleanup (void *);
2064
95554aad
TT
2065static void process_cu_includes (void);
2066
1b80a9fa 2067static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2068
2069static void free_line_header_voidp (void *arg);
4390d890
DE
2070\f
2071/* Various complaints about symbol reading that don't abort the process. */
2072
2073static void
2074dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2075{
2076 complaint (&symfile_complaints,
2077 _("statement list doesn't fit in .debug_line section"));
2078}
2079
2080static void
2081dwarf2_debug_line_missing_file_complaint (void)
2082{
2083 complaint (&symfile_complaints,
2084 _(".debug_line section has line data without a file"));
2085}
2086
2087static void
2088dwarf2_debug_line_missing_end_sequence_complaint (void)
2089{
2090 complaint (&symfile_complaints,
2091 _(".debug_line section has line "
2092 "program sequence without an end"));
2093}
2094
2095static void
2096dwarf2_complex_location_expr_complaint (void)
2097{
2098 complaint (&symfile_complaints, _("location expression too complex"));
2099}
2100
2101static void
2102dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2103 int arg3)
2104{
2105 complaint (&symfile_complaints,
2106 _("const value length mismatch for '%s', got %d, expected %d"),
2107 arg1, arg2, arg3);
2108}
2109
2110static void
2111dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2112{
2113 complaint (&symfile_complaints,
2114 _("debug info runs off end of %s section"
2115 " [in module %s]"),
a32a8923
DE
2116 get_section_name (section),
2117 get_section_file_name (section));
4390d890 2118}
1b80a9fa 2119
4390d890
DE
2120static void
2121dwarf2_macro_malformed_definition_complaint (const char *arg1)
2122{
2123 complaint (&symfile_complaints,
2124 _("macro debug info contains a "
2125 "malformed macro definition:\n`%s'"),
2126 arg1);
2127}
2128
2129static void
2130dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2131{
2132 complaint (&symfile_complaints,
2133 _("invalid attribute class or form for '%s' in '%s'"),
2134 arg1, arg2);
2135}
527f3840
JK
2136
2137/* Hash function for line_header_hash. */
2138
2139static hashval_t
2140line_header_hash (const struct line_header *ofs)
2141{
9c541725 2142 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2143}
2144
2145/* Hash function for htab_create_alloc_ex for line_header_hash. */
2146
2147static hashval_t
2148line_header_hash_voidp (const void *item)
2149{
9a3c8263 2150 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2151
2152 return line_header_hash (ofs);
2153}
2154
2155/* Equality function for line_header_hash. */
2156
2157static int
2158line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2159{
9a3c8263
SM
2160 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2161 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2162
9c541725 2163 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2164 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2165}
2166
4390d890 2167\f
9291a0cd
TT
2168#if WORDS_BIGENDIAN
2169
2170/* Convert VALUE between big- and little-endian. */
2171static offset_type
2172byte_swap (offset_type value)
2173{
2174 offset_type result;
2175
2176 result = (value & 0xff) << 24;
2177 result |= (value & 0xff00) << 8;
2178 result |= (value & 0xff0000) >> 8;
2179 result |= (value & 0xff000000) >> 24;
2180 return result;
2181}
2182
2183#define MAYBE_SWAP(V) byte_swap (V)
2184
2185#else
bc8f2430 2186#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2187#endif /* WORDS_BIGENDIAN */
2188
31aa7e4e
JB
2189/* Read the given attribute value as an address, taking the attribute's
2190 form into account. */
2191
2192static CORE_ADDR
2193attr_value_as_address (struct attribute *attr)
2194{
2195 CORE_ADDR addr;
2196
2197 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2198 {
2199 /* Aside from a few clearly defined exceptions, attributes that
2200 contain an address must always be in DW_FORM_addr form.
2201 Unfortunately, some compilers happen to be violating this
2202 requirement by encoding addresses using other forms, such
2203 as DW_FORM_data4 for example. For those broken compilers,
2204 we try to do our best, without any guarantee of success,
2205 to interpret the address correctly. It would also be nice
2206 to generate a complaint, but that would require us to maintain
2207 a list of legitimate cases where a non-address form is allowed,
2208 as well as update callers to pass in at least the CU's DWARF
2209 version. This is more overhead than what we're willing to
2210 expand for a pretty rare case. */
2211 addr = DW_UNSND (attr);
2212 }
2213 else
2214 addr = DW_ADDR (attr);
2215
2216 return addr;
2217}
2218
9291a0cd
TT
2219/* The suffix for an index file. */
2220#define INDEX_SUFFIX ".gdb-index"
2221
330cdd98
PA
2222/* See declaration. */
2223
2224dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2225 const dwarf2_debug_sections *names)
2226 : objfile (objfile_)
2227{
2228 if (names == NULL)
2229 names = &dwarf2_elf_names;
2230
2231 bfd *obfd = objfile->obfd;
2232
2233 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2234 locate_sections (obfd, sec, *names);
2235}
2236
2237dwarf2_per_objfile::~dwarf2_per_objfile ()
2238{
2239 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2240 free_cached_comp_units ();
2241
2242 if (quick_file_names_table)
2243 htab_delete (quick_file_names_table);
2244
2245 if (line_header_hash)
2246 htab_delete (line_header_hash);
2247
2248 /* Everything else should be on the objfile obstack. */
2249}
2250
2251/* See declaration. */
2252
2253void
2254dwarf2_per_objfile::free_cached_comp_units ()
2255{
2256 dwarf2_per_cu_data *per_cu = read_in_chain;
2257 dwarf2_per_cu_data **last_chain = &read_in_chain;
2258 while (per_cu != NULL)
2259 {
2260 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2261
2262 free_heap_comp_unit (per_cu->cu);
2263 *last_chain = next_cu;
2264 per_cu = next_cu;
2265 }
2266}
2267
c906108c 2268/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2269 information and return true if we have enough to do something.
2270 NAMES points to the dwarf2 section names, or is NULL if the standard
2271 ELF names are used. */
c906108c
SS
2272
2273int
251d32d9
TG
2274dwarf2_has_info (struct objfile *objfile,
2275 const struct dwarf2_debug_sections *names)
c906108c 2276{
9a3c8263
SM
2277 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2278 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2279 if (!dwarf2_per_objfile)
2280 {
2281 /* Initialize per-objfile state. */
2282 struct dwarf2_per_objfile *data
8d749320 2283 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2284
330cdd98
PA
2285 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2286 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2287 }
73869dc2 2288 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2289 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2290 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2291 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2292}
2293
2294/* Return the containing section of virtual section SECTION. */
2295
2296static struct dwarf2_section_info *
2297get_containing_section (const struct dwarf2_section_info *section)
2298{
2299 gdb_assert (section->is_virtual);
2300 return section->s.containing_section;
c906108c
SS
2301}
2302
a32a8923
DE
2303/* Return the bfd owner of SECTION. */
2304
2305static struct bfd *
2306get_section_bfd_owner (const struct dwarf2_section_info *section)
2307{
73869dc2
DE
2308 if (section->is_virtual)
2309 {
2310 section = get_containing_section (section);
2311 gdb_assert (!section->is_virtual);
2312 }
049412e3 2313 return section->s.section->owner;
a32a8923
DE
2314}
2315
2316/* Return the bfd section of SECTION.
2317 Returns NULL if the section is not present. */
2318
2319static asection *
2320get_section_bfd_section (const struct dwarf2_section_info *section)
2321{
73869dc2
DE
2322 if (section->is_virtual)
2323 {
2324 section = get_containing_section (section);
2325 gdb_assert (!section->is_virtual);
2326 }
049412e3 2327 return section->s.section;
a32a8923
DE
2328}
2329
2330/* Return the name of SECTION. */
2331
2332static const char *
2333get_section_name (const struct dwarf2_section_info *section)
2334{
2335 asection *sectp = get_section_bfd_section (section);
2336
2337 gdb_assert (sectp != NULL);
2338 return bfd_section_name (get_section_bfd_owner (section), sectp);
2339}
2340
2341/* Return the name of the file SECTION is in. */
2342
2343static const char *
2344get_section_file_name (const struct dwarf2_section_info *section)
2345{
2346 bfd *abfd = get_section_bfd_owner (section);
2347
2348 return bfd_get_filename (abfd);
2349}
2350
2351/* Return the id of SECTION.
2352 Returns 0 if SECTION doesn't exist. */
2353
2354static int
2355get_section_id (const struct dwarf2_section_info *section)
2356{
2357 asection *sectp = get_section_bfd_section (section);
2358
2359 if (sectp == NULL)
2360 return 0;
2361 return sectp->id;
2362}
2363
2364/* Return the flags of SECTION.
73869dc2 2365 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2366
2367static int
2368get_section_flags (const struct dwarf2_section_info *section)
2369{
2370 asection *sectp = get_section_bfd_section (section);
2371
2372 gdb_assert (sectp != NULL);
2373 return bfd_get_section_flags (sectp->owner, sectp);
2374}
2375
251d32d9
TG
2376/* When loading sections, we look either for uncompressed section or for
2377 compressed section names. */
233a11ab
CS
2378
2379static int
251d32d9
TG
2380section_is_p (const char *section_name,
2381 const struct dwarf2_section_names *names)
233a11ab 2382{
251d32d9
TG
2383 if (names->normal != NULL
2384 && strcmp (section_name, names->normal) == 0)
2385 return 1;
2386 if (names->compressed != NULL
2387 && strcmp (section_name, names->compressed) == 0)
2388 return 1;
2389 return 0;
233a11ab
CS
2390}
2391
330cdd98 2392/* See declaration. */
c906108c 2393
330cdd98
PA
2394void
2395dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2396 const dwarf2_debug_sections &names)
c906108c 2397{
dc7650b8 2398 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2399
dc7650b8
JK
2400 if ((aflag & SEC_HAS_CONTENTS) == 0)
2401 {
2402 }
330cdd98 2403 else if (section_is_p (sectp->name, &names.info))
c906108c 2404 {
330cdd98
PA
2405 this->info.s.section = sectp;
2406 this->info.size = bfd_get_section_size (sectp);
c906108c 2407 }
330cdd98 2408 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2409 {
330cdd98
PA
2410 this->abbrev.s.section = sectp;
2411 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2412 }
330cdd98 2413 else if (section_is_p (sectp->name, &names.line))
c906108c 2414 {
330cdd98
PA
2415 this->line.s.section = sectp;
2416 this->line.size = bfd_get_section_size (sectp);
c906108c 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.loc))
c906108c 2419 {
330cdd98
PA
2420 this->loc.s.section = sectp;
2421 this->loc.size = bfd_get_section_size (sectp);
c906108c 2422 }
330cdd98 2423 else if (section_is_p (sectp->name, &names.loclists))
43988095 2424 {
330cdd98
PA
2425 this->loclists.s.section = sectp;
2426 this->loclists.size = bfd_get_section_size (sectp);
43988095 2427 }
330cdd98 2428 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2429 {
330cdd98
PA
2430 this->macinfo.s.section = sectp;
2431 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2432 }
330cdd98 2433 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2434 {
330cdd98
PA
2435 this->macro.s.section = sectp;
2436 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2437 }
330cdd98 2438 else if (section_is_p (sectp->name, &names.str))
c906108c 2439 {
330cdd98
PA
2440 this->str.s.section = sectp;
2441 this->str.size = bfd_get_section_size (sectp);
c906108c 2442 }
330cdd98 2443 else if (section_is_p (sectp->name, &names.line_str))
43988095 2444 {
330cdd98
PA
2445 this->line_str.s.section = sectp;
2446 this->line_str.size = bfd_get_section_size (sectp);
43988095 2447 }
330cdd98 2448 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2449 {
330cdd98
PA
2450 this->addr.s.section = sectp;
2451 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2452 }
330cdd98 2453 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2454 {
330cdd98
PA
2455 this->frame.s.section = sectp;
2456 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2457 }
330cdd98 2458 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2459 {
330cdd98
PA
2460 this->eh_frame.s.section = sectp;
2461 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2462 }
330cdd98 2463 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2464 {
330cdd98
PA
2465 this->ranges.s.section = sectp;
2466 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2467 }
330cdd98 2468 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2469 {
330cdd98
PA
2470 this->rnglists.s.section = sectp;
2471 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2472 }
330cdd98 2473 else if (section_is_p (sectp->name, &names.types))
348e048f 2474 {
8b70b953
TT
2475 struct dwarf2_section_info type_section;
2476
2477 memset (&type_section, 0, sizeof (type_section));
049412e3 2478 type_section.s.section = sectp;
8b70b953
TT
2479 type_section.size = bfd_get_section_size (sectp);
2480
330cdd98 2481 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2482 &type_section);
348e048f 2483 }
330cdd98 2484 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2485 {
330cdd98
PA
2486 this->gdb_index.s.section = sectp;
2487 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2488 }
dce234bc 2489
b4e1fd61 2490 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2491 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2492 this->has_section_at_zero = true;
c906108c
SS
2493}
2494
fceca515
DE
2495/* A helper function that decides whether a section is empty,
2496 or not present. */
9e0ac564
TT
2497
2498static int
19ac8c2e 2499dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2500{
73869dc2
DE
2501 if (section->is_virtual)
2502 return section->size == 0;
049412e3 2503 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2504}
2505
3019eac3
DE
2506/* Read the contents of the section INFO.
2507 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2508 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2509 of the DWO file.
dce234bc 2510 If the section is compressed, uncompress it before returning. */
c906108c 2511
dce234bc
PP
2512static void
2513dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2514{
a32a8923 2515 asection *sectp;
3019eac3 2516 bfd *abfd;
dce234bc 2517 gdb_byte *buf, *retbuf;
c906108c 2518
be391dca
TT
2519 if (info->readin)
2520 return;
dce234bc 2521 info->buffer = NULL;
be391dca 2522 info->readin = 1;
188dd5d6 2523
9e0ac564 2524 if (dwarf2_section_empty_p (info))
dce234bc 2525 return;
c906108c 2526
a32a8923 2527 sectp = get_section_bfd_section (info);
3019eac3 2528
73869dc2
DE
2529 /* If this is a virtual section we need to read in the real one first. */
2530 if (info->is_virtual)
2531 {
2532 struct dwarf2_section_info *containing_section =
2533 get_containing_section (info);
2534
2535 gdb_assert (sectp != NULL);
2536 if ((sectp->flags & SEC_RELOC) != 0)
2537 {
2538 error (_("Dwarf Error: DWP format V2 with relocations is not"
2539 " supported in section %s [in module %s]"),
2540 get_section_name (info), get_section_file_name (info));
2541 }
2542 dwarf2_read_section (objfile, containing_section);
2543 /* Other code should have already caught virtual sections that don't
2544 fit. */
2545 gdb_assert (info->virtual_offset + info->size
2546 <= containing_section->size);
2547 /* If the real section is empty or there was a problem reading the
2548 section we shouldn't get here. */
2549 gdb_assert (containing_section->buffer != NULL);
2550 info->buffer = containing_section->buffer + info->virtual_offset;
2551 return;
2552 }
2553
4bf44c1c
TT
2554 /* If the section has relocations, we must read it ourselves.
2555 Otherwise we attach it to the BFD. */
2556 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2557 {
d521ce57 2558 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2559 return;
dce234bc 2560 }
dce234bc 2561
224c3ddb 2562 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2563 info->buffer = buf;
dce234bc
PP
2564
2565 /* When debugging .o files, we may need to apply relocations; see
2566 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2567 We never compress sections in .o files, so we only need to
2568 try this when the section is not compressed. */
ac8035ab 2569 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2570 if (retbuf != NULL)
2571 {
2572 info->buffer = retbuf;
2573 return;
2574 }
2575
a32a8923
DE
2576 abfd = get_section_bfd_owner (info);
2577 gdb_assert (abfd != NULL);
2578
dce234bc
PP
2579 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2580 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2581 {
2582 error (_("Dwarf Error: Can't read DWARF data"
2583 " in section %s [in module %s]"),
2584 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2585 }
dce234bc
PP
2586}
2587
9e0ac564
TT
2588/* A helper function that returns the size of a section in a safe way.
2589 If you are positive that the section has been read before using the
2590 size, then it is safe to refer to the dwarf2_section_info object's
2591 "size" field directly. In other cases, you must call this
2592 function, because for compressed sections the size field is not set
2593 correctly until the section has been read. */
2594
2595static bfd_size_type
2596dwarf2_section_size (struct objfile *objfile,
2597 struct dwarf2_section_info *info)
2598{
2599 if (!info->readin)
2600 dwarf2_read_section (objfile, info);
2601 return info->size;
2602}
2603
dce234bc 2604/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2605 SECTION_NAME. */
af34e669 2606
dce234bc 2607void
3017a003
TG
2608dwarf2_get_section_info (struct objfile *objfile,
2609 enum dwarf2_section_enum sect,
d521ce57 2610 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2611 bfd_size_type *sizep)
2612{
2613 struct dwarf2_per_objfile *data
9a3c8263
SM
2614 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2615 dwarf2_objfile_data_key);
dce234bc 2616 struct dwarf2_section_info *info;
a3b2a86b
TT
2617
2618 /* We may see an objfile without any DWARF, in which case we just
2619 return nothing. */
2620 if (data == NULL)
2621 {
2622 *sectp = NULL;
2623 *bufp = NULL;
2624 *sizep = 0;
2625 return;
2626 }
3017a003
TG
2627 switch (sect)
2628 {
2629 case DWARF2_DEBUG_FRAME:
2630 info = &data->frame;
2631 break;
2632 case DWARF2_EH_FRAME:
2633 info = &data->eh_frame;
2634 break;
2635 default:
2636 gdb_assert_not_reached ("unexpected section");
2637 }
dce234bc 2638
9e0ac564 2639 dwarf2_read_section (objfile, info);
dce234bc 2640
a32a8923 2641 *sectp = get_section_bfd_section (info);
dce234bc
PP
2642 *bufp = info->buffer;
2643 *sizep = info->size;
2644}
2645
36586728
TT
2646/* A helper function to find the sections for a .dwz file. */
2647
2648static void
2649locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2650{
9a3c8263 2651 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2652
2653 /* Note that we only support the standard ELF names, because .dwz
2654 is ELF-only (at the time of writing). */
2655 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2656 {
049412e3 2657 dwz_file->abbrev.s.section = sectp;
36586728
TT
2658 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2659 }
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2661 {
049412e3 2662 dwz_file->info.s.section = sectp;
36586728
TT
2663 dwz_file->info.size = bfd_get_section_size (sectp);
2664 }
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2666 {
049412e3 2667 dwz_file->str.s.section = sectp;
36586728
TT
2668 dwz_file->str.size = bfd_get_section_size (sectp);
2669 }
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2671 {
049412e3 2672 dwz_file->line.s.section = sectp;
36586728
TT
2673 dwz_file->line.size = bfd_get_section_size (sectp);
2674 }
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2676 {
049412e3 2677 dwz_file->macro.s.section = sectp;
36586728
TT
2678 dwz_file->macro.size = bfd_get_section_size (sectp);
2679 }
2ec9a5e0
TT
2680 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2681 {
049412e3 2682 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2683 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2684 }
36586728
TT
2685}
2686
4db1a1dc
TT
2687/* Open the separate '.dwz' debug file, if needed. Return NULL if
2688 there is no .gnu_debugaltlink section in the file. Error if there
2689 is such a section but the file cannot be found. */
36586728
TT
2690
2691static struct dwz_file *
2692dwarf2_get_dwz_file (void)
2693{
4db1a1dc 2694 char *data;
36586728
TT
2695 struct cleanup *cleanup;
2696 const char *filename;
2697 struct dwz_file *result;
acd13123 2698 bfd_size_type buildid_len_arg;
dc294be5
TT
2699 size_t buildid_len;
2700 bfd_byte *buildid;
36586728
TT
2701
2702 if (dwarf2_per_objfile->dwz_file != NULL)
2703 return dwarf2_per_objfile->dwz_file;
2704
4db1a1dc
TT
2705 bfd_set_error (bfd_error_no_error);
2706 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2707 &buildid_len_arg, &buildid);
4db1a1dc
TT
2708 if (data == NULL)
2709 {
2710 if (bfd_get_error () == bfd_error_no_error)
2711 return NULL;
2712 error (_("could not read '.gnu_debugaltlink' section: %s"),
2713 bfd_errmsg (bfd_get_error ()));
2714 }
36586728 2715 cleanup = make_cleanup (xfree, data);
dc294be5 2716 make_cleanup (xfree, buildid);
36586728 2717
acd13123
TT
2718 buildid_len = (size_t) buildid_len_arg;
2719
f9d83a0b 2720 filename = (const char *) data;
d721ba37
PA
2721
2722 std::string abs_storage;
36586728
TT
2723 if (!IS_ABSOLUTE_PATH (filename))
2724 {
14278e1f
TT
2725 gdb::unique_xmalloc_ptr<char> abs
2726 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2727
14278e1f 2728 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2729 filename = abs_storage.c_str ();
36586728
TT
2730 }
2731
dc294be5
TT
2732 /* First try the file name given in the section. If that doesn't
2733 work, try to use the build-id instead. */
192b62ce 2734 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2735 if (dwz_bfd != NULL)
36586728 2736 {
192b62ce
TT
2737 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2738 dwz_bfd.release ();
36586728
TT
2739 }
2740
dc294be5
TT
2741 if (dwz_bfd == NULL)
2742 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2743
2744 if (dwz_bfd == NULL)
2745 error (_("could not find '.gnu_debugaltlink' file for %s"),
2746 objfile_name (dwarf2_per_objfile->objfile));
2747
36586728
TT
2748 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2749 struct dwz_file);
192b62ce 2750 result->dwz_bfd = dwz_bfd.release ();
36586728 2751
192b62ce 2752 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2753
2754 do_cleanups (cleanup);
2755
192b62ce 2756 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2757 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2758 return result;
2759}
9291a0cd 2760\f
7b9f3c50
DE
2761/* DWARF quick_symbols_functions support. */
2762
2763/* TUs can share .debug_line entries, and there can be a lot more TUs than
2764 unique line tables, so we maintain a separate table of all .debug_line
2765 derived entries to support the sharing.
2766 All the quick functions need is the list of file names. We discard the
2767 line_header when we're done and don't need to record it here. */
2768struct quick_file_names
2769{
094b34ac
DE
2770 /* The data used to construct the hash key. */
2771 struct stmt_list_hash hash;
7b9f3c50
DE
2772
2773 /* The number of entries in file_names, real_names. */
2774 unsigned int num_file_names;
2775
2776 /* The file names from the line table, after being run through
2777 file_full_name. */
2778 const char **file_names;
2779
2780 /* The file names from the line table after being run through
2781 gdb_realpath. These are computed lazily. */
2782 const char **real_names;
2783};
2784
2785/* When using the index (and thus not using psymtabs), each CU has an
2786 object of this type. This is used to hold information needed by
2787 the various "quick" methods. */
2788struct dwarf2_per_cu_quick_data
2789{
2790 /* The file table. This can be NULL if there was no file table
2791 or it's currently not read in.
2792 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2793 struct quick_file_names *file_names;
2794
2795 /* The corresponding symbol table. This is NULL if symbols for this
2796 CU have not yet been read. */
43f3e411 2797 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2798
2799 /* A temporary mark bit used when iterating over all CUs in
2800 expand_symtabs_matching. */
2801 unsigned int mark : 1;
2802
2803 /* True if we've tried to read the file table and found there isn't one.
2804 There will be no point in trying to read it again next time. */
2805 unsigned int no_file_data : 1;
2806};
2807
094b34ac
DE
2808/* Utility hash function for a stmt_list_hash. */
2809
2810static hashval_t
2811hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2812{
2813 hashval_t v = 0;
2814
2815 if (stmt_list_hash->dwo_unit != NULL)
2816 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2817 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2818 return v;
2819}
2820
2821/* Utility equality function for a stmt_list_hash. */
2822
2823static int
2824eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2825 const struct stmt_list_hash *rhs)
2826{
2827 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2828 return 0;
2829 if (lhs->dwo_unit != NULL
2830 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2831 return 0;
2832
9c541725 2833 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2834}
2835
7b9f3c50
DE
2836/* Hash function for a quick_file_names. */
2837
2838static hashval_t
2839hash_file_name_entry (const void *e)
2840{
9a3c8263
SM
2841 const struct quick_file_names *file_data
2842 = (const struct quick_file_names *) e;
7b9f3c50 2843
094b34ac 2844 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2845}
2846
2847/* Equality function for a quick_file_names. */
2848
2849static int
2850eq_file_name_entry (const void *a, const void *b)
2851{
9a3c8263
SM
2852 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2853 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2854
094b34ac 2855 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2856}
2857
2858/* Delete function for a quick_file_names. */
2859
2860static void
2861delete_file_name_entry (void *e)
2862{
9a3c8263 2863 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2864 int i;
2865
2866 for (i = 0; i < file_data->num_file_names; ++i)
2867 {
2868 xfree ((void*) file_data->file_names[i]);
2869 if (file_data->real_names)
2870 xfree ((void*) file_data->real_names[i]);
2871 }
2872
2873 /* The space for the struct itself lives on objfile_obstack,
2874 so we don't free it here. */
2875}
2876
2877/* Create a quick_file_names hash table. */
2878
2879static htab_t
2880create_quick_file_names_table (unsigned int nr_initial_entries)
2881{
2882 return htab_create_alloc (nr_initial_entries,
2883 hash_file_name_entry, eq_file_name_entry,
2884 delete_file_name_entry, xcalloc, xfree);
2885}
9291a0cd 2886
918dd910
JK
2887/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2888 have to be created afterwards. You should call age_cached_comp_units after
2889 processing PER_CU->CU. dw2_setup must have been already called. */
2890
2891static void
2892load_cu (struct dwarf2_per_cu_data *per_cu)
2893{
3019eac3 2894 if (per_cu->is_debug_types)
e5fe5e75 2895 load_full_type_unit (per_cu);
918dd910 2896 else
95554aad 2897 load_full_comp_unit (per_cu, language_minimal);
918dd910 2898
cc12ce38
DE
2899 if (per_cu->cu == NULL)
2900 return; /* Dummy CU. */
2dc860c0
DE
2901
2902 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2903}
2904
a0f42c21 2905/* Read in the symbols for PER_CU. */
2fdf6df6 2906
9291a0cd 2907static void
a0f42c21 2908dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2909{
2910 struct cleanup *back_to;
2911
f4dc4d17
DE
2912 /* Skip type_unit_groups, reading the type units they contain
2913 is handled elsewhere. */
2914 if (IS_TYPE_UNIT_GROUP (per_cu))
2915 return;
2916
9291a0cd
TT
2917 back_to = make_cleanup (dwarf2_release_queue, NULL);
2918
95554aad 2919 if (dwarf2_per_objfile->using_index
43f3e411 2920 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2921 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2922 {
2923 queue_comp_unit (per_cu, language_minimal);
2924 load_cu (per_cu);
89e63ee4
DE
2925
2926 /* If we just loaded a CU from a DWO, and we're working with an index
2927 that may badly handle TUs, load all the TUs in that DWO as well.
2928 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2929 if (!per_cu->is_debug_types
cc12ce38 2930 && per_cu->cu != NULL
89e63ee4
DE
2931 && per_cu->cu->dwo_unit != NULL
2932 && dwarf2_per_objfile->index_table != NULL
2933 && dwarf2_per_objfile->index_table->version <= 7
2934 /* DWP files aren't supported yet. */
2935 && get_dwp_file () == NULL)
2936 queue_and_load_all_dwo_tus (per_cu);
95554aad 2937 }
9291a0cd 2938
a0f42c21 2939 process_queue ();
9291a0cd
TT
2940
2941 /* Age the cache, releasing compilation units that have not
2942 been used recently. */
2943 age_cached_comp_units ();
2944
2945 do_cleanups (back_to);
2946}
2947
2948/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2949 the objfile from which this CU came. Returns the resulting symbol
2950 table. */
2fdf6df6 2951
43f3e411 2952static struct compunit_symtab *
a0f42c21 2953dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2954{
95554aad 2955 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2956 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2957 {
2958 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2959 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2960 dw2_do_instantiate_symtab (per_cu);
95554aad 2961 process_cu_includes ();
9291a0cd
TT
2962 do_cleanups (back_to);
2963 }
f194fefb 2964
43f3e411 2965 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2966}
2967
8832e7e3 2968/* Return the CU/TU given its index.
f4dc4d17
DE
2969
2970 This is intended for loops like:
2971
2972 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2973 + dwarf2_per_objfile->n_type_units); ++i)
2974 {
8832e7e3 2975 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2976
2977 ...;
2978 }
2979*/
2fdf6df6 2980
1fd400ff 2981static struct dwarf2_per_cu_data *
8832e7e3 2982dw2_get_cutu (int index)
1fd400ff
TT
2983{
2984 if (index >= dwarf2_per_objfile->n_comp_units)
2985 {
f4dc4d17 2986 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2987 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2988 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2989 }
2990
2991 return dwarf2_per_objfile->all_comp_units[index];
2992}
2993
8832e7e3
DE
2994/* Return the CU given its index.
2995 This differs from dw2_get_cutu in that it's for when you know INDEX
2996 refers to a CU. */
f4dc4d17
DE
2997
2998static struct dwarf2_per_cu_data *
8832e7e3 2999dw2_get_cu (int index)
f4dc4d17 3000{
8832e7e3 3001 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3002
1fd400ff
TT
3003 return dwarf2_per_objfile->all_comp_units[index];
3004}
3005
2ec9a5e0
TT
3006/* A helper for create_cus_from_index that handles a given list of
3007 CUs. */
2fdf6df6 3008
74a0d9f6 3009static void
2ec9a5e0
TT
3010create_cus_from_index_list (struct objfile *objfile,
3011 const gdb_byte *cu_list, offset_type n_elements,
3012 struct dwarf2_section_info *section,
3013 int is_dwz,
3014 int base_offset)
9291a0cd
TT
3015{
3016 offset_type i;
9291a0cd 3017
2ec9a5e0 3018 for (i = 0; i < n_elements; i += 2)
9291a0cd 3019 {
74a0d9f6 3020 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3021
3022 sect_offset sect_off
3023 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3024 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3025 cu_list += 2 * 8;
3026
9c541725
PA
3027 dwarf2_per_cu_data *the_cu
3028 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3029 struct dwarf2_per_cu_data);
3030 the_cu->sect_off = sect_off;
9291a0cd
TT
3031 the_cu->length = length;
3032 the_cu->objfile = objfile;
8a0459fd 3033 the_cu->section = section;
9291a0cd
TT
3034 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3035 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3036 the_cu->is_dwz = is_dwz;
3037 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3038 }
9291a0cd
TT
3039}
3040
2ec9a5e0 3041/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3042 the CU objects for this objfile. */
2ec9a5e0 3043
74a0d9f6 3044static void
2ec9a5e0
TT
3045create_cus_from_index (struct objfile *objfile,
3046 const gdb_byte *cu_list, offset_type cu_list_elements,
3047 const gdb_byte *dwz_list, offset_type dwz_elements)
3048{
3049 struct dwz_file *dwz;
3050
3051 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3052 dwarf2_per_objfile->all_comp_units =
3053 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3054 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3055
74a0d9f6
JK
3056 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3057 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3058
3059 if (dwz_elements == 0)
74a0d9f6 3060 return;
2ec9a5e0
TT
3061
3062 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3063 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3064 cu_list_elements / 2);
2ec9a5e0
TT
3065}
3066
1fd400ff 3067/* Create the signatured type hash table from the index. */
673bfd45 3068
74a0d9f6 3069static void
673bfd45 3070create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3071 struct dwarf2_section_info *section,
673bfd45
DE
3072 const gdb_byte *bytes,
3073 offset_type elements)
1fd400ff
TT
3074{
3075 offset_type i;
673bfd45 3076 htab_t sig_types_hash;
1fd400ff 3077
6aa5f3a6
DE
3078 dwarf2_per_objfile->n_type_units
3079 = dwarf2_per_objfile->n_allocated_type_units
3080 = elements / 3;
8d749320
SM
3081 dwarf2_per_objfile->all_type_units =
3082 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3083
673bfd45 3084 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3085
3086 for (i = 0; i < elements; i += 3)
3087 {
52dc124a 3088 struct signatured_type *sig_type;
9c541725 3089 ULONGEST signature;
1fd400ff 3090 void **slot;
9c541725 3091 cu_offset type_offset_in_tu;
1fd400ff 3092
74a0d9f6 3093 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3094 sect_offset sect_off
3095 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3096 type_offset_in_tu
3097 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3098 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3099 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3100 bytes += 3 * 8;
3101
52dc124a 3102 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3103 struct signatured_type);
52dc124a 3104 sig_type->signature = signature;
9c541725 3105 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3106 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3107 sig_type->per_cu.section = section;
9c541725 3108 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3109 sig_type->per_cu.objfile = objfile;
3110 sig_type->per_cu.v.quick
1fd400ff
TT
3111 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3112 struct dwarf2_per_cu_quick_data);
3113
52dc124a
DE
3114 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3115 *slot = sig_type;
1fd400ff 3116
b4dd5633 3117 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3118 }
3119
673bfd45 3120 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3121}
3122
9291a0cd
TT
3123/* Read the address map data from the mapped index, and use it to
3124 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3125
9291a0cd
TT
3126static void
3127create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3128{
3e29f34a 3129 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3130 const gdb_byte *iter, *end;
9291a0cd 3131 struct addrmap *mutable_map;
9291a0cd
TT
3132 CORE_ADDR baseaddr;
3133
8268c778
PA
3134 auto_obstack temp_obstack;
3135
9291a0cd
TT
3136 mutable_map = addrmap_create_mutable (&temp_obstack);
3137
3138 iter = index->address_table;
3139 end = iter + index->address_table_size;
3140
3141 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3142
3143 while (iter < end)
3144 {
3145 ULONGEST hi, lo, cu_index;
3146 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3147 iter += 8;
3148 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3149 iter += 8;
3150 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3151 iter += 4;
f652bce2 3152
24a55014 3153 if (lo > hi)
f652bce2 3154 {
24a55014
DE
3155 complaint (&symfile_complaints,
3156 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3157 hex_string (lo), hex_string (hi));
24a55014 3158 continue;
f652bce2 3159 }
24a55014
DE
3160
3161 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3162 {
3163 complaint (&symfile_complaints,
3164 _(".gdb_index address table has invalid CU number %u"),
3165 (unsigned) cu_index);
24a55014 3166 continue;
f652bce2 3167 }
24a55014 3168
3e29f34a
MR
3169 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3170 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3171 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3172 }
3173
3174 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3175 &objfile->objfile_obstack);
9291a0cd
TT
3176}
3177
59d7bcaf
JK
3178/* The hash function for strings in the mapped index. This is the same as
3179 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3180 implementation. This is necessary because the hash function is tied to the
3181 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3182 SYMBOL_HASH_NEXT.
3183
3184 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3185
9291a0cd 3186static hashval_t
559a7a62 3187mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3188{
3189 const unsigned char *str = (const unsigned char *) p;
3190 hashval_t r = 0;
3191 unsigned char c;
3192
3193 while ((c = *str++) != 0)
559a7a62
JK
3194 {
3195 if (index_version >= 5)
3196 c = tolower (c);
3197 r = r * 67 + c - 113;
3198 }
9291a0cd
TT
3199
3200 return r;
3201}
3202
3203/* Find a slot in the mapped index INDEX for the object named NAME.
3204 If NAME is found, set *VEC_OUT to point to the CU vector in the
3205 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3206
9291a0cd
TT
3207static int
3208find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3209 offset_type **vec_out)
3210{
0cf03b49
JK
3211 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3212 offset_type hash;
9291a0cd 3213 offset_type slot, step;
559a7a62 3214 int (*cmp) (const char *, const char *);
9291a0cd 3215
0cf03b49 3216 if (current_language->la_language == language_cplus
45280282
IB
3217 || current_language->la_language == language_fortran
3218 || current_language->la_language == language_d)
0cf03b49
JK
3219 {
3220 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3221 not contain any. */
a8719064 3222
72998fb3 3223 if (strchr (name, '(') != NULL)
0cf03b49 3224 {
72998fb3 3225 char *without_params = cp_remove_params (name);
0cf03b49 3226
72998fb3
DE
3227 if (without_params != NULL)
3228 {
3229 make_cleanup (xfree, without_params);
3230 name = without_params;
3231 }
0cf03b49
JK
3232 }
3233 }
3234
559a7a62 3235 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3236 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3237 simulate our NAME being searched is also lowercased. */
3238 hash = mapped_index_string_hash ((index->version == 4
3239 && case_sensitivity == case_sensitive_off
3240 ? 5 : index->version),
3241 name);
3242
3876f04e
DE
3243 slot = hash & (index->symbol_table_slots - 1);
3244 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3245 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3246
3247 for (;;)
3248 {
3249 /* Convert a slot number to an offset into the table. */
3250 offset_type i = 2 * slot;
3251 const char *str;
3876f04e 3252 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3253 {
3254 do_cleanups (back_to);
3255 return 0;
3256 }
9291a0cd 3257
3876f04e 3258 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3259 if (!cmp (name, str))
9291a0cd
TT
3260 {
3261 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3262 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3263 do_cleanups (back_to);
9291a0cd
TT
3264 return 1;
3265 }
3266
3876f04e 3267 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3268 }
3269}
3270
2ec9a5e0
TT
3271/* A helper function that reads the .gdb_index from SECTION and fills
3272 in MAP. FILENAME is the name of the file containing the section;
3273 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3274 ok to use deprecated sections.
3275
3276 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3277 out parameters that are filled in with information about the CU and
3278 TU lists in the section.
3279
3280 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3281
9291a0cd 3282static int
2ec9a5e0
TT
3283read_index_from_section (struct objfile *objfile,
3284 const char *filename,
3285 int deprecated_ok,
3286 struct dwarf2_section_info *section,
3287 struct mapped_index *map,
3288 const gdb_byte **cu_list,
3289 offset_type *cu_list_elements,
3290 const gdb_byte **types_list,
3291 offset_type *types_list_elements)
9291a0cd 3292{
948f8e3d 3293 const gdb_byte *addr;
2ec9a5e0 3294 offset_type version;
b3b272e1 3295 offset_type *metadata;
1fd400ff 3296 int i;
9291a0cd 3297
2ec9a5e0 3298 if (dwarf2_section_empty_p (section))
9291a0cd 3299 return 0;
82430852
JK
3300
3301 /* Older elfutils strip versions could keep the section in the main
3302 executable while splitting it for the separate debug info file. */
a32a8923 3303 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3304 return 0;
3305
2ec9a5e0 3306 dwarf2_read_section (objfile, section);
9291a0cd 3307
2ec9a5e0 3308 addr = section->buffer;
9291a0cd 3309 /* Version check. */
1fd400ff 3310 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3311 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3312 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3313 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3314 indices. */
831adc1f 3315 if (version < 4)
481860b3
GB
3316 {
3317 static int warning_printed = 0;
3318 if (!warning_printed)
3319 {
3320 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3321 filename);
481860b3
GB
3322 warning_printed = 1;
3323 }
3324 return 0;
3325 }
3326 /* Index version 4 uses a different hash function than index version
3327 5 and later.
3328
3329 Versions earlier than 6 did not emit psymbols for inlined
3330 functions. Using these files will cause GDB not to be able to
3331 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3332 indices unless the user has done
3333 "set use-deprecated-index-sections on". */
2ec9a5e0 3334 if (version < 6 && !deprecated_ok)
481860b3
GB
3335 {
3336 static int warning_printed = 0;
3337 if (!warning_printed)
3338 {
e615022a
DE
3339 warning (_("\
3340Skipping deprecated .gdb_index section in %s.\n\
3341Do \"set use-deprecated-index-sections on\" before the file is read\n\
3342to use the section anyway."),
2ec9a5e0 3343 filename);
481860b3
GB
3344 warning_printed = 1;
3345 }
3346 return 0;
3347 }
796a7ff8 3348 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3349 of the TU (for symbols coming from TUs),
3350 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3351 Plus gold-generated indices can have duplicate entries for global symbols,
3352 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3353 These are just performance bugs, and we can't distinguish gdb-generated
3354 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3355
481860b3 3356 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3357 longer backward compatible. */
796a7ff8 3358 if (version > 8)
594e8718 3359 return 0;
9291a0cd 3360
559a7a62 3361 map->version = version;
2ec9a5e0 3362 map->total_size = section->size;
9291a0cd
TT
3363
3364 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3365
3366 i = 0;
2ec9a5e0
TT
3367 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3368 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3369 / 8);
1fd400ff
TT
3370 ++i;
3371
2ec9a5e0
TT
3372 *types_list = addr + MAYBE_SWAP (metadata[i]);
3373 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3374 - MAYBE_SWAP (metadata[i]))
3375 / 8);
987d643c 3376 ++i;
1fd400ff
TT
3377
3378 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3379 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3380 - MAYBE_SWAP (metadata[i]));
3381 ++i;
3382
3876f04e
DE
3383 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3384 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3385 - MAYBE_SWAP (metadata[i]))
3386 / (2 * sizeof (offset_type)));
1fd400ff 3387 ++i;
9291a0cd 3388
f9d83a0b 3389 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3390
2ec9a5e0
TT
3391 return 1;
3392}
3393
3394
3395/* Read the index file. If everything went ok, initialize the "quick"
3396 elements of all the CUs and return 1. Otherwise, return 0. */
3397
3398static int
3399dwarf2_read_index (struct objfile *objfile)
3400{
3401 struct mapped_index local_map, *map;
3402 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3403 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3404 struct dwz_file *dwz;
2ec9a5e0 3405
4262abfb 3406 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3407 use_deprecated_index_sections,
3408 &dwarf2_per_objfile->gdb_index, &local_map,
3409 &cu_list, &cu_list_elements,
3410 &types_list, &types_list_elements))
3411 return 0;
3412
0fefef59 3413 /* Don't use the index if it's empty. */
2ec9a5e0 3414 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3415 return 0;
3416
2ec9a5e0
TT
3417 /* If there is a .dwz file, read it so we can get its CU list as
3418 well. */
4db1a1dc
TT
3419 dwz = dwarf2_get_dwz_file ();
3420 if (dwz != NULL)
2ec9a5e0 3421 {
2ec9a5e0
TT
3422 struct mapped_index dwz_map;
3423 const gdb_byte *dwz_types_ignore;
3424 offset_type dwz_types_elements_ignore;
3425
3426 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3427 1,
3428 &dwz->gdb_index, &dwz_map,
3429 &dwz_list, &dwz_list_elements,
3430 &dwz_types_ignore,
3431 &dwz_types_elements_ignore))
3432 {
3433 warning (_("could not read '.gdb_index' section from %s; skipping"),
3434 bfd_get_filename (dwz->dwz_bfd));
3435 return 0;
3436 }
3437 }
3438
74a0d9f6
JK
3439 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3440 dwz_list_elements);
1fd400ff 3441
8b70b953
TT
3442 if (types_list_elements)
3443 {
3444 struct dwarf2_section_info *section;
3445
3446 /* We can only handle a single .debug_types when we have an
3447 index. */
3448 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3449 return 0;
3450
3451 section = VEC_index (dwarf2_section_info_def,
3452 dwarf2_per_objfile->types, 0);
3453
74a0d9f6
JK
3454 create_signatured_type_table_from_index (objfile, section, types_list,
3455 types_list_elements);
8b70b953 3456 }
9291a0cd 3457
2ec9a5e0
TT
3458 create_addrmap_from_index (objfile, &local_map);
3459
8d749320 3460 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3461 *map = local_map;
9291a0cd
TT
3462
3463 dwarf2_per_objfile->index_table = map;
3464 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3465 dwarf2_per_objfile->quick_file_names_table =
3466 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3467
3468 return 1;
3469}
3470
3471/* A helper for the "quick" functions which sets the global
3472 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3473
9291a0cd
TT
3474static void
3475dw2_setup (struct objfile *objfile)
3476{
9a3c8263
SM
3477 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3478 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3479 gdb_assert (dwarf2_per_objfile);
3480}
3481
dee91e82 3482/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3483
dee91e82
DE
3484static void
3485dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3486 const gdb_byte *info_ptr,
dee91e82
DE
3487 struct die_info *comp_unit_die,
3488 int has_children,
3489 void *data)
9291a0cd 3490{
dee91e82
DE
3491 struct dwarf2_cu *cu = reader->cu;
3492 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3493 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3494 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3495 struct attribute *attr;
dee91e82 3496 int i;
7b9f3c50
DE
3497 void **slot;
3498 struct quick_file_names *qfn;
9291a0cd 3499
0186c6a7
DE
3500 gdb_assert (! this_cu->is_debug_types);
3501
07261596
TT
3502 /* Our callers never want to match partial units -- instead they
3503 will match the enclosing full CU. */
3504 if (comp_unit_die->tag == DW_TAG_partial_unit)
3505 {
3506 this_cu->v.quick->no_file_data = 1;
3507 return;
3508 }
3509
0186c6a7 3510 lh_cu = this_cu;
7b9f3c50 3511 slot = NULL;
dee91e82 3512
fff8551c 3513 line_header_up lh;
9c541725 3514 sect_offset line_offset {};
fff8551c 3515
dee91e82 3516 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3517 if (attr)
3518 {
7b9f3c50
DE
3519 struct quick_file_names find_entry;
3520
9c541725 3521 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3522
3523 /* We may have already read in this line header (TU line header sharing).
3524 If we have we're done. */
094b34ac 3525 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3526 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3527 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3528 &find_entry, INSERT);
3529 if (*slot != NULL)
3530 {
9a3c8263 3531 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3532 return;
7b9f3c50
DE
3533 }
3534
3019eac3 3535 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3536 }
3537 if (lh == NULL)
3538 {
094b34ac 3539 lh_cu->v.quick->no_file_data = 1;
dee91e82 3540 return;
9291a0cd
TT
3541 }
3542
8d749320 3543 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3544 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3545 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3546 gdb_assert (slot != NULL);
3547 *slot = qfn;
9291a0cd 3548
d721ba37 3549 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3550
fff8551c 3551 qfn->num_file_names = lh->file_names.size ();
8d749320 3552 qfn->file_names =
fff8551c
PA
3553 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3554 for (i = 0; i < lh->file_names.size (); ++i)
3555 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3556 qfn->real_names = NULL;
9291a0cd 3557
094b34ac 3558 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3559}
3560
3561/* A helper for the "quick" functions which attempts to read the line
3562 table for THIS_CU. */
3563
3564static struct quick_file_names *
e4a48d9d 3565dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3566{
0186c6a7
DE
3567 /* This should never be called for TUs. */
3568 gdb_assert (! this_cu->is_debug_types);
3569 /* Nor type unit groups. */
3570 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3571
dee91e82
DE
3572 if (this_cu->v.quick->file_names != NULL)
3573 return this_cu->v.quick->file_names;
3574 /* If we know there is no line data, no point in looking again. */
3575 if (this_cu->v.quick->no_file_data)
3576 return NULL;
3577
0186c6a7 3578 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3579
3580 if (this_cu->v.quick->no_file_data)
3581 return NULL;
3582 return this_cu->v.quick->file_names;
9291a0cd
TT
3583}
3584
3585/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3586 real path for a given file name from the line table. */
2fdf6df6 3587
9291a0cd 3588static const char *
7b9f3c50
DE
3589dw2_get_real_path (struct objfile *objfile,
3590 struct quick_file_names *qfn, int index)
9291a0cd 3591{
7b9f3c50
DE
3592 if (qfn->real_names == NULL)
3593 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3594 qfn->num_file_names, const char *);
9291a0cd 3595
7b9f3c50 3596 if (qfn->real_names[index] == NULL)
14278e1f 3597 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3598
7b9f3c50 3599 return qfn->real_names[index];
9291a0cd
TT
3600}
3601
3602static struct symtab *
3603dw2_find_last_source_symtab (struct objfile *objfile)
3604{
43f3e411 3605 struct compunit_symtab *cust;
9291a0cd 3606 int index;
ae2de4f8 3607
9291a0cd
TT
3608 dw2_setup (objfile);
3609 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3610 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3611 if (cust == NULL)
3612 return NULL;
3613 return compunit_primary_filetab (cust);
9291a0cd
TT
3614}
3615
7b9f3c50
DE
3616/* Traversal function for dw2_forget_cached_source_info. */
3617
3618static int
3619dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3620{
7b9f3c50 3621 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3622
7b9f3c50 3623 if (file_data->real_names)
9291a0cd 3624 {
7b9f3c50 3625 int i;
9291a0cd 3626
7b9f3c50 3627 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3628 {
7b9f3c50
DE
3629 xfree ((void*) file_data->real_names[i]);
3630 file_data->real_names[i] = NULL;
9291a0cd
TT
3631 }
3632 }
7b9f3c50
DE
3633
3634 return 1;
3635}
3636
3637static void
3638dw2_forget_cached_source_info (struct objfile *objfile)
3639{
3640 dw2_setup (objfile);
3641
3642 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3643 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3644}
3645
f8eba3c6
TT
3646/* Helper function for dw2_map_symtabs_matching_filename that expands
3647 the symtabs and calls the iterator. */
3648
3649static int
3650dw2_map_expand_apply (struct objfile *objfile,
3651 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3652 const char *name, const char *real_path,
14bc53a8 3653 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3654{
43f3e411 3655 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3656
3657 /* Don't visit already-expanded CUs. */
43f3e411 3658 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3659 return 0;
3660
3661 /* This may expand more than one symtab, and we want to iterate over
3662 all of them. */
a0f42c21 3663 dw2_instantiate_symtab (per_cu);
f8eba3c6 3664
14bc53a8
PA
3665 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3666 last_made, callback);
f8eba3c6
TT
3667}
3668
3669/* Implementation of the map_symtabs_matching_filename method. */
3670
14bc53a8
PA
3671static bool
3672dw2_map_symtabs_matching_filename
3673 (struct objfile *objfile, const char *name, const char *real_path,
3674 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3675{
3676 int i;
c011a4f4 3677 const char *name_basename = lbasename (name);
9291a0cd
TT
3678
3679 dw2_setup (objfile);
ae2de4f8 3680
848e3e78
DE
3681 /* The rule is CUs specify all the files, including those used by
3682 any TU, so there's no need to scan TUs here. */
f4dc4d17 3683
848e3e78 3684 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3685 {
3686 int j;
8832e7e3 3687 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3688 struct quick_file_names *file_data;
9291a0cd 3689
3d7bb9d9 3690 /* We only need to look at symtabs not already expanded. */
43f3e411 3691 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3692 continue;
3693
e4a48d9d 3694 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3695 if (file_data == NULL)
9291a0cd
TT
3696 continue;
3697
7b9f3c50 3698 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3699 {
7b9f3c50 3700 const char *this_name = file_data->file_names[j];
da235a7c 3701 const char *this_real_name;
9291a0cd 3702
af529f8f 3703 if (compare_filenames_for_search (this_name, name))
9291a0cd 3704 {
f5b95b50 3705 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3706 callback))
3707 return true;
288e77a7 3708 continue;
4aac40c8 3709 }
9291a0cd 3710
c011a4f4
DE
3711 /* Before we invoke realpath, which can get expensive when many
3712 files are involved, do a quick comparison of the basenames. */
3713 if (! basenames_may_differ
3714 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3715 continue;
3716
da235a7c
JK
3717 this_real_name = dw2_get_real_path (objfile, file_data, j);
3718 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3719 {
da235a7c 3720 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3721 callback))
3722 return true;
288e77a7 3723 continue;
da235a7c 3724 }
9291a0cd 3725
da235a7c
JK
3726 if (real_path != NULL)
3727 {
af529f8f
JK
3728 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3729 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3730 if (this_real_name != NULL
af529f8f 3731 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3732 {
f5b95b50 3733 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3734 callback))
3735 return true;
288e77a7 3736 continue;
9291a0cd
TT
3737 }
3738 }
3739 }
3740 }
3741
14bc53a8 3742 return false;
9291a0cd
TT
3743}
3744
da51c347
DE
3745/* Struct used to manage iterating over all CUs looking for a symbol. */
3746
3747struct dw2_symtab_iterator
9291a0cd 3748{
da51c347
DE
3749 /* The internalized form of .gdb_index. */
3750 struct mapped_index *index;
3751 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3752 int want_specific_block;
3753 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3754 Unused if !WANT_SPECIFIC_BLOCK. */
3755 int block_index;
3756 /* The kind of symbol we're looking for. */
3757 domain_enum domain;
3758 /* The list of CUs from the index entry of the symbol,
3759 or NULL if not found. */
3760 offset_type *vec;
3761 /* The next element in VEC to look at. */
3762 int next;
3763 /* The number of elements in VEC, or zero if there is no match. */
3764 int length;
8943b874
DE
3765 /* Have we seen a global version of the symbol?
3766 If so we can ignore all further global instances.
3767 This is to work around gold/15646, inefficient gold-generated
3768 indices. */
3769 int global_seen;
da51c347 3770};
9291a0cd 3771
da51c347
DE
3772/* Initialize the index symtab iterator ITER.
3773 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3774 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3775
9291a0cd 3776static void
da51c347
DE
3777dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3778 struct mapped_index *index,
3779 int want_specific_block,
3780 int block_index,
3781 domain_enum domain,
3782 const char *name)
3783{
3784 iter->index = index;
3785 iter->want_specific_block = want_specific_block;
3786 iter->block_index = block_index;
3787 iter->domain = domain;
3788 iter->next = 0;
8943b874 3789 iter->global_seen = 0;
da51c347
DE
3790
3791 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3792 iter->length = MAYBE_SWAP (*iter->vec);
3793 else
3794 {
3795 iter->vec = NULL;
3796 iter->length = 0;
3797 }
3798}
3799
3800/* Return the next matching CU or NULL if there are no more. */
3801
3802static struct dwarf2_per_cu_data *
3803dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3804{
3805 for ( ; iter->next < iter->length; ++iter->next)
3806 {
3807 offset_type cu_index_and_attrs =
3808 MAYBE_SWAP (iter->vec[iter->next + 1]);
3809 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3810 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3811 int want_static = iter->block_index != GLOBAL_BLOCK;
3812 /* This value is only valid for index versions >= 7. */
3813 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3814 gdb_index_symbol_kind symbol_kind =
3815 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3816 /* Only check the symbol attributes if they're present.
3817 Indices prior to version 7 don't record them,
3818 and indices >= 7 may elide them for certain symbols
3819 (gold does this). */
3820 int attrs_valid =
3821 (iter->index->version >= 7
3822 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3823
3190f0c6
DE
3824 /* Don't crash on bad data. */
3825 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3826 + dwarf2_per_objfile->n_type_units))
3827 {
3828 complaint (&symfile_complaints,
3829 _(".gdb_index entry has bad CU index"
4262abfb
JK
3830 " [in module %s]"),
3831 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3832 continue;
3833 }
3834
8832e7e3 3835 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3836
da51c347 3837 /* Skip if already read in. */
43f3e411 3838 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3839 continue;
3840
8943b874
DE
3841 /* Check static vs global. */
3842 if (attrs_valid)
3843 {
3844 if (iter->want_specific_block
3845 && want_static != is_static)
3846 continue;
3847 /* Work around gold/15646. */
3848 if (!is_static && iter->global_seen)
3849 continue;
3850 if (!is_static)
3851 iter->global_seen = 1;
3852 }
da51c347
DE
3853
3854 /* Only check the symbol's kind if it has one. */
3855 if (attrs_valid)
3856 {
3857 switch (iter->domain)
3858 {
3859 case VAR_DOMAIN:
3860 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3861 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3862 /* Some types are also in VAR_DOMAIN. */
3863 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3864 continue;
3865 break;
3866 case STRUCT_DOMAIN:
3867 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3868 continue;
3869 break;
3870 case LABEL_DOMAIN:
3871 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3872 continue;
3873 break;
3874 default:
3875 break;
3876 }
3877 }
3878
3879 ++iter->next;
3880 return per_cu;
3881 }
3882
3883 return NULL;
3884}
3885
43f3e411 3886static struct compunit_symtab *
da51c347
DE
3887dw2_lookup_symbol (struct objfile *objfile, int block_index,
3888 const char *name, domain_enum domain)
9291a0cd 3889{
43f3e411 3890 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3891 struct mapped_index *index;
3892
9291a0cd
TT
3893 dw2_setup (objfile);
3894
156942c7
DE
3895 index = dwarf2_per_objfile->index_table;
3896
da51c347 3897 /* index is NULL if OBJF_READNOW. */
156942c7 3898 if (index)
9291a0cd 3899 {
da51c347
DE
3900 struct dw2_symtab_iterator iter;
3901 struct dwarf2_per_cu_data *per_cu;
3902
3903 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3904
da51c347 3905 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3906 {
b2e2f908 3907 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3908 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3909 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3910 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3911
b2e2f908
DE
3912 sym = block_find_symbol (block, name, domain,
3913 block_find_non_opaque_type_preferred,
3914 &with_opaque);
3915
da51c347
DE
3916 /* Some caution must be observed with overloaded functions
3917 and methods, since the index will not contain any overload
3918 information (but NAME might contain it). */
da51c347 3919
b2e2f908 3920 if (sym != NULL
a778f165 3921 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
3922 return stab;
3923 if (with_opaque != NULL
a778f165 3924 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 3925 stab_best = stab;
da51c347
DE
3926
3927 /* Keep looking through other CUs. */
9291a0cd
TT
3928 }
3929 }
9291a0cd 3930
da51c347 3931 return stab_best;
9291a0cd
TT
3932}
3933
3934static void
3935dw2_print_stats (struct objfile *objfile)
3936{
e4a48d9d 3937 int i, total, count;
9291a0cd
TT
3938
3939 dw2_setup (objfile);
e4a48d9d 3940 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3941 count = 0;
e4a48d9d 3942 for (i = 0; i < total; ++i)
9291a0cd 3943 {
8832e7e3 3944 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3945
43f3e411 3946 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3947 ++count;
3948 }
e4a48d9d 3949 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3950 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3951}
3952
779bd270
DE
3953/* This dumps minimal information about the index.
3954 It is called via "mt print objfiles".
3955 One use is to verify .gdb_index has been loaded by the
3956 gdb.dwarf2/gdb-index.exp testcase. */
3957
9291a0cd
TT
3958static void
3959dw2_dump (struct objfile *objfile)
3960{
779bd270
DE
3961 dw2_setup (objfile);
3962 gdb_assert (dwarf2_per_objfile->using_index);
3963 printf_filtered (".gdb_index:");
3964 if (dwarf2_per_objfile->index_table != NULL)
3965 {
3966 printf_filtered (" version %d\n",
3967 dwarf2_per_objfile->index_table->version);
3968 }
3969 else
3970 printf_filtered (" faked for \"readnow\"\n");
3971 printf_filtered ("\n");
9291a0cd
TT
3972}
3973
3974static void
3189cb12
DE
3975dw2_relocate (struct objfile *objfile,
3976 const struct section_offsets *new_offsets,
3977 const struct section_offsets *delta)
9291a0cd
TT
3978{
3979 /* There's nothing to relocate here. */
3980}
3981
3982static void
3983dw2_expand_symtabs_for_function (struct objfile *objfile,
3984 const char *func_name)
3985{
da51c347
DE
3986 struct mapped_index *index;
3987
3988 dw2_setup (objfile);
3989
3990 index = dwarf2_per_objfile->index_table;
3991
3992 /* index is NULL if OBJF_READNOW. */
3993 if (index)
3994 {
3995 struct dw2_symtab_iterator iter;
3996 struct dwarf2_per_cu_data *per_cu;
3997
3998 /* Note: It doesn't matter what we pass for block_index here. */
3999 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4000 func_name);
4001
4002 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4003 dw2_instantiate_symtab (per_cu);
4004 }
9291a0cd
TT
4005}
4006
4007static void
4008dw2_expand_all_symtabs (struct objfile *objfile)
4009{
4010 int i;
4011
4012 dw2_setup (objfile);
1fd400ff
TT
4013
4014 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4015 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4016 {
8832e7e3 4017 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4018
a0f42c21 4019 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4020 }
4021}
4022
4023static void
652a8996
JK
4024dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4025 const char *fullname)
9291a0cd
TT
4026{
4027 int i;
4028
4029 dw2_setup (objfile);
d4637a04
DE
4030
4031 /* We don't need to consider type units here.
4032 This is only called for examining code, e.g. expand_line_sal.
4033 There can be an order of magnitude (or more) more type units
4034 than comp units, and we avoid them if we can. */
4035
4036 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4037 {
4038 int j;
8832e7e3 4039 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4040 struct quick_file_names *file_data;
9291a0cd 4041
3d7bb9d9 4042 /* We only need to look at symtabs not already expanded. */
43f3e411 4043 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4044 continue;
4045
e4a48d9d 4046 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4047 if (file_data == NULL)
9291a0cd
TT
4048 continue;
4049
7b9f3c50 4050 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4051 {
652a8996
JK
4052 const char *this_fullname = file_data->file_names[j];
4053
4054 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4055 {
a0f42c21 4056 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4057 break;
4058 }
4059 }
4060 }
4061}
4062
9291a0cd 4063static void
ade7ed9e 4064dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4065 const char * name, domain_enum domain,
ade7ed9e 4066 int global,
40658b94
PH
4067 int (*callback) (struct block *,
4068 struct symbol *, void *),
2edb89d3
JK
4069 void *data, symbol_compare_ftype *match,
4070 symbol_compare_ftype *ordered_compare)
9291a0cd 4071{
40658b94 4072 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4073 current language is Ada for a non-Ada objfile using GNU index. As Ada
4074 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4075}
4076
4077static void
f8eba3c6
TT
4078dw2_expand_symtabs_matching
4079 (struct objfile *objfile,
14bc53a8
PA
4080 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4081 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4082 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4083 enum search_domain kind)
9291a0cd
TT
4084{
4085 int i;
4086 offset_type iter;
4b5246aa 4087 struct mapped_index *index;
9291a0cd
TT
4088
4089 dw2_setup (objfile);
ae2de4f8
DE
4090
4091 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4092 if (!dwarf2_per_objfile->index_table)
4093 return;
4b5246aa 4094 index = dwarf2_per_objfile->index_table;
9291a0cd 4095
7b08b9eb 4096 if (file_matcher != NULL)
24c79950 4097 {
fc4007c9
TT
4098 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4099 htab_eq_pointer,
4100 NULL, xcalloc, xfree));
4101 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4102 htab_eq_pointer,
4103 NULL, xcalloc, xfree));
24c79950 4104
848e3e78
DE
4105 /* The rule is CUs specify all the files, including those used by
4106 any TU, so there's no need to scan TUs here. */
4107
4108 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4109 {
4110 int j;
8832e7e3 4111 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4112 struct quick_file_names *file_data;
4113 void **slot;
7b08b9eb 4114
61d96d7e
DE
4115 QUIT;
4116
24c79950 4117 per_cu->v.quick->mark = 0;
3d7bb9d9 4118
24c79950 4119 /* We only need to look at symtabs not already expanded. */
43f3e411 4120 if (per_cu->v.quick->compunit_symtab)
24c79950 4121 continue;
7b08b9eb 4122
e4a48d9d 4123 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4124 if (file_data == NULL)
4125 continue;
7b08b9eb 4126
fc4007c9 4127 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4128 continue;
fc4007c9 4129 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4130 {
4131 per_cu->v.quick->mark = 1;
4132 continue;
4133 }
4134
4135 for (j = 0; j < file_data->num_file_names; ++j)
4136 {
da235a7c
JK
4137 const char *this_real_name;
4138
14bc53a8 4139 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4140 {
4141 per_cu->v.quick->mark = 1;
4142 break;
4143 }
da235a7c
JK
4144
4145 /* Before we invoke realpath, which can get expensive when many
4146 files are involved, do a quick comparison of the basenames. */
4147 if (!basenames_may_differ
4148 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4149 true))
da235a7c
JK
4150 continue;
4151
4152 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4153 if (file_matcher (this_real_name, false))
da235a7c
JK
4154 {
4155 per_cu->v.quick->mark = 1;
4156 break;
4157 }
24c79950
TT
4158 }
4159
4160 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4161 ? visited_found.get ()
4162 : visited_not_found.get (),
24c79950
TT
4163 file_data, INSERT);
4164 *slot = file_data;
4165 }
24c79950 4166 }
9291a0cd 4167
3876f04e 4168 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4169 {
4170 offset_type idx = 2 * iter;
4171 const char *name;
4172 offset_type *vec, vec_len, vec_idx;
8943b874 4173 int global_seen = 0;
9291a0cd 4174
61d96d7e
DE
4175 QUIT;
4176
3876f04e 4177 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4178 continue;
4179
3876f04e 4180 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4181
14bc53a8 4182 if (!symbol_matcher (name))
9291a0cd
TT
4183 continue;
4184
4185 /* The name was matched, now expand corresponding CUs that were
4186 marked. */
4b5246aa 4187 vec = (offset_type *) (index->constant_pool
3876f04e 4188 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4189 vec_len = MAYBE_SWAP (vec[0]);
4190 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4191 {
e254ef6a 4192 struct dwarf2_per_cu_data *per_cu;
156942c7 4193 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4194 /* This value is only valid for index versions >= 7. */
4195 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4196 gdb_index_symbol_kind symbol_kind =
4197 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4198 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4199 /* Only check the symbol attributes if they're present.
4200 Indices prior to version 7 don't record them,
4201 and indices >= 7 may elide them for certain symbols
4202 (gold does this). */
4203 int attrs_valid =
4204 (index->version >= 7
4205 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4206
8943b874
DE
4207 /* Work around gold/15646. */
4208 if (attrs_valid)
4209 {
4210 if (!is_static && global_seen)
4211 continue;
4212 if (!is_static)
4213 global_seen = 1;
4214 }
4215
3190f0c6
DE
4216 /* Only check the symbol's kind if it has one. */
4217 if (attrs_valid)
156942c7
DE
4218 {
4219 switch (kind)
4220 {
4221 case VARIABLES_DOMAIN:
4222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4223 continue;
4224 break;
4225 case FUNCTIONS_DOMAIN:
4226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4227 continue;
4228 break;
4229 case TYPES_DOMAIN:
4230 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4231 continue;
4232 break;
4233 default:
4234 break;
4235 }
4236 }
4237
3190f0c6
DE
4238 /* Don't crash on bad data. */
4239 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4240 + dwarf2_per_objfile->n_type_units))
4241 {
4242 complaint (&symfile_complaints,
4243 _(".gdb_index entry has bad CU index"
4262abfb 4244 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4245 continue;
4246 }
4247
8832e7e3 4248 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4249 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4250 {
4251 int symtab_was_null =
4252 (per_cu->v.quick->compunit_symtab == NULL);
4253
4254 dw2_instantiate_symtab (per_cu);
4255
4256 if (expansion_notify != NULL
4257 && symtab_was_null
4258 && per_cu->v.quick->compunit_symtab != NULL)
4259 {
14bc53a8 4260 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4261 }
4262 }
9291a0cd
TT
4263 }
4264 }
4265}
4266
43f3e411 4267/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4268 symtab. */
4269
43f3e411
DE
4270static struct compunit_symtab *
4271recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4272 CORE_ADDR pc)
9703b513
TT
4273{
4274 int i;
4275
43f3e411
DE
4276 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4277 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4278 return cust;
9703b513 4279
43f3e411 4280 if (cust->includes == NULL)
a3ec0bb1
DE
4281 return NULL;
4282
43f3e411 4283 for (i = 0; cust->includes[i]; ++i)
9703b513 4284 {
43f3e411 4285 struct compunit_symtab *s = cust->includes[i];
9703b513 4286
43f3e411 4287 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4288 if (s != NULL)
4289 return s;
4290 }
4291
4292 return NULL;
4293}
4294
43f3e411
DE
4295static struct compunit_symtab *
4296dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4297 struct bound_minimal_symbol msymbol,
4298 CORE_ADDR pc,
4299 struct obj_section *section,
4300 int warn_if_readin)
9291a0cd
TT
4301{
4302 struct dwarf2_per_cu_data *data;
43f3e411 4303 struct compunit_symtab *result;
9291a0cd
TT
4304
4305 dw2_setup (objfile);
4306
4307 if (!objfile->psymtabs_addrmap)
4308 return NULL;
4309
9a3c8263
SM
4310 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4311 pc);
9291a0cd
TT
4312 if (!data)
4313 return NULL;
4314
43f3e411 4315 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4316 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4317 paddress (get_objfile_arch (objfile), pc));
4318
43f3e411
DE
4319 result
4320 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4321 pc);
9703b513
TT
4322 gdb_assert (result != NULL);
4323 return result;
9291a0cd
TT
4324}
4325
9291a0cd 4326static void
44b13c5a 4327dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4328 void *data, int need_fullname)
9291a0cd 4329{
9291a0cd 4330 dw2_setup (objfile);
ae2de4f8 4331
bbf2f4df 4332 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4333 {
bbf2f4df 4334 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4335
bbf2f4df
PA
4336 htab_up visited (htab_create_alloc (10,
4337 htab_hash_pointer, htab_eq_pointer,
4338 NULL, xcalloc, xfree));
24c79950 4339
bbf2f4df
PA
4340 /* The rule is CUs specify all the files, including those used
4341 by any TU, so there's no need to scan TUs here. We can
4342 ignore file names coming from already-expanded CUs. */
24c79950 4343
bbf2f4df
PA
4344 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4345 {
4346 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4347
bbf2f4df
PA
4348 if (per_cu->v.quick->compunit_symtab)
4349 {
4350 void **slot = htab_find_slot (visited.get (),
4351 per_cu->v.quick->file_names,
4352 INSERT);
9291a0cd 4353
bbf2f4df
PA
4354 *slot = per_cu->v.quick->file_names;
4355 }
24c79950 4356 }
24c79950 4357
bbf2f4df 4358 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4359 {
bbf2f4df
PA
4360 int j;
4361 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4362 struct quick_file_names *file_data;
4363 void **slot;
4364
4365 /* We only need to look at symtabs not already expanded. */
4366 if (per_cu->v.quick->compunit_symtab)
4367 continue;
74e2f255 4368
bbf2f4df
PA
4369 file_data = dw2_get_file_names (per_cu);
4370 if (file_data == NULL)
4371 continue;
4372
4373 slot = htab_find_slot (visited.get (), file_data, INSERT);
4374 if (*slot)
4375 {
4376 /* Already visited. */
4377 continue;
4378 }
4379 *slot = file_data;
4380
4381 for (int j = 0; j < file_data->num_file_names; ++j)
4382 {
4383 const char *filename = file_data->file_names[j];
4384 dwarf2_per_objfile->filenames_cache->seen (filename);
4385 }
9291a0cd
TT
4386 }
4387 }
bbf2f4df
PA
4388
4389 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4390 {
14278e1f 4391 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4392
4393 if (need_fullname)
4394 this_real_name = gdb_realpath (filename);
14278e1f 4395 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4396 });
9291a0cd
TT
4397}
4398
4399static int
4400dw2_has_symbols (struct objfile *objfile)
4401{
4402 return 1;
4403}
4404
4405const struct quick_symbol_functions dwarf2_gdb_index_functions =
4406{
4407 dw2_has_symbols,
4408 dw2_find_last_source_symtab,
4409 dw2_forget_cached_source_info,
f8eba3c6 4410 dw2_map_symtabs_matching_filename,
9291a0cd 4411 dw2_lookup_symbol,
9291a0cd
TT
4412 dw2_print_stats,
4413 dw2_dump,
4414 dw2_relocate,
4415 dw2_expand_symtabs_for_function,
4416 dw2_expand_all_symtabs,
652a8996 4417 dw2_expand_symtabs_with_fullname,
40658b94 4418 dw2_map_matching_symbols,
9291a0cd 4419 dw2_expand_symtabs_matching,
43f3e411 4420 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4421 dw2_map_symbol_filenames
4422};
4423
4424/* Initialize for reading DWARF for this objfile. Return 0 if this
4425 file will use psymtabs, or 1 if using the GNU index. */
4426
4427int
4428dwarf2_initialize_objfile (struct objfile *objfile)
4429{
4430 /* If we're about to read full symbols, don't bother with the
4431 indices. In this case we also don't care if some other debug
4432 format is making psymtabs, because they are all about to be
4433 expanded anyway. */
4434 if ((objfile->flags & OBJF_READNOW))
4435 {
4436 int i;
4437
4438 dwarf2_per_objfile->using_index = 1;
4439 create_all_comp_units (objfile);
0e50663e 4440 create_all_type_units (objfile);
7b9f3c50
DE
4441 dwarf2_per_objfile->quick_file_names_table =
4442 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4443
1fd400ff 4444 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4445 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4446 {
8832e7e3 4447 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4448
e254ef6a
DE
4449 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4450 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4451 }
4452
4453 /* Return 1 so that gdb sees the "quick" functions. However,
4454 these functions will be no-ops because we will have expanded
4455 all symtabs. */
4456 return 1;
4457 }
4458
4459 if (dwarf2_read_index (objfile))
4460 return 1;
4461
9291a0cd
TT
4462 return 0;
4463}
4464
4465\f
4466
dce234bc
PP
4467/* Build a partial symbol table. */
4468
4469void
f29dff0a 4470dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4471{
c9bf0622 4472
f29dff0a 4473 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4474 {
4475 init_psymbol_list (objfile, 1024);
4476 }
4477
492d29ea 4478 TRY
c9bf0622
TT
4479 {
4480 /* This isn't really ideal: all the data we allocate on the
4481 objfile's obstack is still uselessly kept around. However,
4482 freeing it seems unsafe. */
906768f9 4483 psymtab_discarder psymtabs (objfile);
c9bf0622 4484 dwarf2_build_psymtabs_hard (objfile);
906768f9 4485 psymtabs.keep ();
c9bf0622 4486 }
492d29ea
PA
4487 CATCH (except, RETURN_MASK_ERROR)
4488 {
4489 exception_print (gdb_stderr, except);
4490 }
4491 END_CATCH
c906108c 4492}
c906108c 4493
1ce1cefd
DE
4494/* Return the total length of the CU described by HEADER. */
4495
4496static unsigned int
4497get_cu_length (const struct comp_unit_head *header)
4498{
4499 return header->initial_length_size + header->length;
4500}
4501
9c541725 4502/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4503
9c541725
PA
4504static inline bool
4505offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4506{
9c541725
PA
4507 sect_offset bottom = cu_header->sect_off;
4508 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4509
9c541725 4510 return sect_off >= bottom && sect_off < top;
45452591
DE
4511}
4512
3b80fe9b
DE
4513/* Find the base address of the compilation unit for range lists and
4514 location lists. It will normally be specified by DW_AT_low_pc.
4515 In DWARF-3 draft 4, the base address could be overridden by
4516 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4517 compilation units with discontinuous ranges. */
4518
4519static void
4520dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4521{
4522 struct attribute *attr;
4523
4524 cu->base_known = 0;
4525 cu->base_address = 0;
4526
4527 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4528 if (attr)
4529 {
31aa7e4e 4530 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4531 cu->base_known = 1;
4532 }
4533 else
4534 {
4535 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4536 if (attr)
4537 {
31aa7e4e 4538 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4539 cu->base_known = 1;
4540 }
4541 }
4542}
4543
93311388 4544/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4545 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4546 NOTE: This leaves members offset, first_die_offset to be filled in
4547 by the caller. */
107d2387 4548
d521ce57 4549static const gdb_byte *
107d2387 4550read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4551 const gdb_byte *info_ptr,
4552 struct dwarf2_section_info *section,
4553 rcuh_kind section_kind)
107d2387
AC
4554{
4555 int signed_addr;
891d2f0b 4556 unsigned int bytes_read;
43988095
JK
4557 const char *filename = get_section_file_name (section);
4558 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4559
4560 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4561 cu_header->initial_length_size = bytes_read;
4562 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4563 info_ptr += bytes_read;
107d2387
AC
4564 cu_header->version = read_2_bytes (abfd, info_ptr);
4565 info_ptr += 2;
43988095
JK
4566 if (cu_header->version < 5)
4567 switch (section_kind)
4568 {
4569 case rcuh_kind::COMPILE:
4570 cu_header->unit_type = DW_UT_compile;
4571 break;
4572 case rcuh_kind::TYPE:
4573 cu_header->unit_type = DW_UT_type;
4574 break;
4575 default:
4576 internal_error (__FILE__, __LINE__,
4577 _("read_comp_unit_head: invalid section_kind"));
4578 }
4579 else
4580 {
4581 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4582 (read_1_byte (abfd, info_ptr));
4583 info_ptr += 1;
4584 switch (cu_header->unit_type)
4585 {
4586 case DW_UT_compile:
4587 if (section_kind != rcuh_kind::COMPILE)
4588 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4589 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4590 filename);
4591 break;
4592 case DW_UT_type:
4593 section_kind = rcuh_kind::TYPE;
4594 break;
4595 default:
4596 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4597 "(is %d, should be %d or %d) [in module %s]"),
4598 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4599 }
4600
4601 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4602 info_ptr += 1;
4603 }
9c541725
PA
4604 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4605 cu_header,
4606 &bytes_read);
613e1657 4607 info_ptr += bytes_read;
43988095
JK
4608 if (cu_header->version < 5)
4609 {
4610 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4611 info_ptr += 1;
4612 }
107d2387
AC
4613 signed_addr = bfd_get_sign_extend_vma (abfd);
4614 if (signed_addr < 0)
8e65ff28 4615 internal_error (__FILE__, __LINE__,
e2e0b3e5 4616 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4617 cu_header->signed_addr_p = signed_addr;
c764a876 4618
43988095
JK
4619 if (section_kind == rcuh_kind::TYPE)
4620 {
4621 LONGEST type_offset;
4622
4623 cu_header->signature = read_8_bytes (abfd, info_ptr);
4624 info_ptr += 8;
4625
4626 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4627 info_ptr += bytes_read;
9c541725
PA
4628 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4629 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4630 error (_("Dwarf Error: Too big type_offset in compilation unit "
4631 "header (is %s) [in module %s]"), plongest (type_offset),
4632 filename);
4633 }
4634
107d2387
AC
4635 return info_ptr;
4636}
4637
36586728
TT
4638/* Helper function that returns the proper abbrev section for
4639 THIS_CU. */
4640
4641static struct dwarf2_section_info *
4642get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4643{
4644 struct dwarf2_section_info *abbrev;
4645
4646 if (this_cu->is_dwz)
4647 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4648 else
4649 abbrev = &dwarf2_per_objfile->abbrev;
4650
4651 return abbrev;
4652}
4653
9ff913ba
DE
4654/* Subroutine of read_and_check_comp_unit_head and
4655 read_and_check_type_unit_head to simplify them.
4656 Perform various error checking on the header. */
4657
4658static void
4659error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4660 struct dwarf2_section_info *section,
4661 struct dwarf2_section_info *abbrev_section)
9ff913ba 4662{
a32a8923 4663 const char *filename = get_section_file_name (section);
9ff913ba 4664
43988095 4665 if (header->version < 2 || header->version > 5)
9ff913ba 4666 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4667 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4668 filename);
4669
9c541725 4670 if (to_underlying (header->abbrev_sect_off)
36586728 4671 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4672 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4673 "(offset 0x%x + 6) [in module %s]"),
4674 to_underlying (header->abbrev_sect_off),
4675 to_underlying (header->sect_off),
9ff913ba
DE
4676 filename);
4677
9c541725 4678 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4679 avoid potential 32-bit overflow. */
9c541725 4680 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4681 > section->size)
9c541725
PA
4682 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4683 "(offset 0x%x + 0) [in module %s]"),
4684 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4685 filename);
4686}
4687
4688/* Read in a CU/TU header and perform some basic error checking.
4689 The contents of the header are stored in HEADER.
4690 The result is a pointer to the start of the first DIE. */
adabb602 4691
d521ce57 4692static const gdb_byte *
9ff913ba
DE
4693read_and_check_comp_unit_head (struct comp_unit_head *header,
4694 struct dwarf2_section_info *section,
4bdcc0c1 4695 struct dwarf2_section_info *abbrev_section,
d521ce57 4696 const gdb_byte *info_ptr,
43988095 4697 rcuh_kind section_kind)
72bf9492 4698{
d521ce57 4699 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4700 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4701
9c541725 4702 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4703
43988095 4704 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4705
9c541725 4706 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4707
4bdcc0c1 4708 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4709
4710 return info_ptr;
348e048f
DE
4711}
4712
f4dc4d17
DE
4713/* Fetch the abbreviation table offset from a comp or type unit header. */
4714
4715static sect_offset
4716read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4717 sect_offset sect_off)
f4dc4d17 4718{
a32a8923 4719 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4720 const gdb_byte *info_ptr;
ac298888 4721 unsigned int initial_length_size, offset_size;
43988095 4722 uint16_t version;
f4dc4d17
DE
4723
4724 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4725 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4726 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4727 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4728 info_ptr += initial_length_size;
4729
4730 version = read_2_bytes (abfd, info_ptr);
4731 info_ptr += 2;
4732 if (version >= 5)
4733 {
4734 /* Skip unit type and address size. */
4735 info_ptr += 2;
4736 }
4737
9c541725 4738 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4739}
4740
aaa75496
JB
4741/* Allocate a new partial symtab for file named NAME and mark this new
4742 partial symtab as being an include of PST. */
4743
4744static void
d521ce57 4745dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4746 struct objfile *objfile)
4747{
4748 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4749
fbd9ab74
JK
4750 if (!IS_ABSOLUTE_PATH (subpst->filename))
4751 {
4752 /* It shares objfile->objfile_obstack. */
4753 subpst->dirname = pst->dirname;
4754 }
4755
aaa75496
JB
4756 subpst->textlow = 0;
4757 subpst->texthigh = 0;
4758
8d749320
SM
4759 subpst->dependencies
4760 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4761 subpst->dependencies[0] = pst;
4762 subpst->number_of_dependencies = 1;
4763
4764 subpst->globals_offset = 0;
4765 subpst->n_global_syms = 0;
4766 subpst->statics_offset = 0;
4767 subpst->n_static_syms = 0;
43f3e411 4768 subpst->compunit_symtab = NULL;
aaa75496
JB
4769 subpst->read_symtab = pst->read_symtab;
4770 subpst->readin = 0;
4771
4772 /* No private part is necessary for include psymtabs. This property
4773 can be used to differentiate between such include psymtabs and
10b3939b 4774 the regular ones. */
58a9656e 4775 subpst->read_symtab_private = NULL;
aaa75496
JB
4776}
4777
4778/* Read the Line Number Program data and extract the list of files
4779 included by the source file represented by PST. Build an include
d85a05f0 4780 partial symtab for each of these included files. */
aaa75496
JB
4781
4782static void
4783dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4784 struct die_info *die,
4785 struct partial_symtab *pst)
aaa75496 4786{
fff8551c 4787 line_header_up lh;
d85a05f0 4788 struct attribute *attr;
aaa75496 4789
d85a05f0
DJ
4790 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4791 if (attr)
9c541725 4792 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4793 if (lh == NULL)
4794 return; /* No linetable, so no includes. */
4795
c6da4cef 4796 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4797 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4798}
4799
348e048f 4800static hashval_t
52dc124a 4801hash_signatured_type (const void *item)
348e048f 4802{
9a3c8263
SM
4803 const struct signatured_type *sig_type
4804 = (const struct signatured_type *) item;
9a619af0 4805
348e048f 4806 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4807 return sig_type->signature;
348e048f
DE
4808}
4809
4810static int
52dc124a 4811eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4812{
9a3c8263
SM
4813 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4814 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4815
348e048f
DE
4816 return lhs->signature == rhs->signature;
4817}
4818
1fd400ff
TT
4819/* Allocate a hash table for signatured types. */
4820
4821static htab_t
673bfd45 4822allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4823{
4824 return htab_create_alloc_ex (41,
52dc124a
DE
4825 hash_signatured_type,
4826 eq_signatured_type,
1fd400ff
TT
4827 NULL,
4828 &objfile->objfile_obstack,
4829 hashtab_obstack_allocate,
4830 dummy_obstack_deallocate);
4831}
4832
d467dd73 4833/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4834
4835static int
d467dd73 4836add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4837{
9a3c8263
SM
4838 struct signatured_type *sigt = (struct signatured_type *) *slot;
4839 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4840
b4dd5633 4841 **datap = sigt;
1fd400ff
TT
4842 ++*datap;
4843
4844 return 1;
4845}
4846
78d4d2c5 4847/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4848 and fill them into TYPES_HTAB. It will process only type units,
4849 therefore DW_UT_type. */
c88ee1f0 4850
78d4d2c5
JK
4851static void
4852create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4853 dwarf2_section_info *section, htab_t &types_htab,
4854 rcuh_kind section_kind)
348e048f 4855{
3019eac3 4856 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4857 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4858 bfd *abfd;
4859 const gdb_byte *info_ptr, *end_ptr;
348e048f 4860
4bdcc0c1
DE
4861 abbrev_section = (dwo_file != NULL
4862 ? &dwo_file->sections.abbrev
4863 : &dwarf2_per_objfile->abbrev);
4864
b4f54984 4865 if (dwarf_read_debug)
43988095
JK
4866 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4867 get_section_name (section),
a32a8923 4868 get_section_file_name (abbrev_section));
09406207 4869
78d4d2c5
JK
4870 dwarf2_read_section (objfile, section);
4871 info_ptr = section->buffer;
348e048f 4872
78d4d2c5
JK
4873 if (info_ptr == NULL)
4874 return;
348e048f 4875
78d4d2c5
JK
4876 /* We can't set abfd until now because the section may be empty or
4877 not present, in which case the bfd is unknown. */
4878 abfd = get_section_bfd_owner (section);
348e048f 4879
78d4d2c5
JK
4880 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4881 because we don't need to read any dies: the signature is in the
4882 header. */
3019eac3 4883
78d4d2c5
JK
4884 end_ptr = info_ptr + section->size;
4885 while (info_ptr < end_ptr)
4886 {
78d4d2c5
JK
4887 struct signatured_type *sig_type;
4888 struct dwo_unit *dwo_tu;
4889 void **slot;
4890 const gdb_byte *ptr = info_ptr;
4891 struct comp_unit_head header;
4892 unsigned int length;
8b70b953 4893
9c541725 4894 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4895
a49dd8dd
JK
4896 /* Initialize it due to a false compiler warning. */
4897 header.signature = -1;
9c541725 4898 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4899
78d4d2c5
JK
4900 /* We need to read the type's signature in order to build the hash
4901 table, but we don't need anything else just yet. */
348e048f 4902
43988095
JK
4903 ptr = read_and_check_comp_unit_head (&header, section,
4904 abbrev_section, ptr, section_kind);
348e048f 4905
78d4d2c5 4906 length = get_cu_length (&header);
6caca83c 4907
78d4d2c5
JK
4908 /* Skip dummy type units. */
4909 if (ptr >= info_ptr + length
43988095
JK
4910 || peek_abbrev_code (abfd, ptr) == 0
4911 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4912 {
4913 info_ptr += length;
4914 continue;
4915 }
dee91e82 4916
78d4d2c5
JK
4917 if (types_htab == NULL)
4918 {
4919 if (dwo_file)
4920 types_htab = allocate_dwo_unit_table (objfile);
4921 else
4922 types_htab = allocate_signatured_type_table (objfile);
4923 }
8b70b953 4924
78d4d2c5
JK
4925 if (dwo_file)
4926 {
4927 sig_type = NULL;
4928 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4929 struct dwo_unit);
4930 dwo_tu->dwo_file = dwo_file;
43988095 4931 dwo_tu->signature = header.signature;
9c541725 4932 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4933 dwo_tu->section = section;
9c541725 4934 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4935 dwo_tu->length = length;
4936 }
4937 else
4938 {
4939 /* N.B.: type_offset is not usable if this type uses a DWO file.
4940 The real type_offset is in the DWO file. */
4941 dwo_tu = NULL;
4942 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4943 struct signatured_type);
43988095 4944 sig_type->signature = header.signature;
9c541725 4945 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4946 sig_type->per_cu.objfile = objfile;
4947 sig_type->per_cu.is_debug_types = 1;
4948 sig_type->per_cu.section = section;
9c541725 4949 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4950 sig_type->per_cu.length = length;
4951 }
4952
4953 slot = htab_find_slot (types_htab,
4954 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4955 INSERT);
4956 gdb_assert (slot != NULL);
4957 if (*slot != NULL)
4958 {
9c541725 4959 sect_offset dup_sect_off;
0349ea22 4960
3019eac3
DE
4961 if (dwo_file)
4962 {
78d4d2c5
JK
4963 const struct dwo_unit *dup_tu
4964 = (const struct dwo_unit *) *slot;
4965
9c541725 4966 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4967 }
4968 else
4969 {
78d4d2c5
JK
4970 const struct signatured_type *dup_tu
4971 = (const struct signatured_type *) *slot;
4972
9c541725 4973 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4974 }
8b70b953 4975
78d4d2c5
JK
4976 complaint (&symfile_complaints,
4977 _("debug type entry at offset 0x%x is duplicate to"
4978 " the entry at offset 0x%x, signature %s"),
9c541725 4979 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4980 hex_string (header.signature));
78d4d2c5
JK
4981 }
4982 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4983
78d4d2c5
JK
4984 if (dwarf_read_debug > 1)
4985 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4986 to_underlying (sect_off),
43988095 4987 hex_string (header.signature));
3019eac3 4988
78d4d2c5
JK
4989 info_ptr += length;
4990 }
4991}
3019eac3 4992
78d4d2c5
JK
4993/* Create the hash table of all entries in the .debug_types
4994 (or .debug_types.dwo) section(s).
4995 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4996 otherwise it is NULL.
b3c8eb43 4997
78d4d2c5 4998 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4999
78d4d2c5 5000 Note: This function processes DWO files only, not DWP files. */
348e048f 5001
78d4d2c5
JK
5002static void
5003create_debug_types_hash_table (struct dwo_file *dwo_file,
5004 VEC (dwarf2_section_info_def) *types,
5005 htab_t &types_htab)
5006{
5007 int ix;
5008 struct dwarf2_section_info *section;
5009
5010 if (VEC_empty (dwarf2_section_info_def, types))
5011 return;
348e048f 5012
78d4d2c5
JK
5013 for (ix = 0;
5014 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5015 ++ix)
43988095
JK
5016 create_debug_type_hash_table (dwo_file, section, types_htab,
5017 rcuh_kind::TYPE);
3019eac3
DE
5018}
5019
5020/* Create the hash table of all entries in the .debug_types section,
5021 and initialize all_type_units.
5022 The result is zero if there is an error (e.g. missing .debug_types section),
5023 otherwise non-zero. */
5024
5025static int
5026create_all_type_units (struct objfile *objfile)
5027{
78d4d2c5 5028 htab_t types_htab = NULL;
b4dd5633 5029 struct signatured_type **iter;
3019eac3 5030
43988095
JK
5031 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5032 rcuh_kind::COMPILE);
78d4d2c5 5033 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5034 if (types_htab == NULL)
5035 {
5036 dwarf2_per_objfile->signatured_types = NULL;
5037 return 0;
5038 }
5039
348e048f
DE
5040 dwarf2_per_objfile->signatured_types = types_htab;
5041
6aa5f3a6
DE
5042 dwarf2_per_objfile->n_type_units
5043 = dwarf2_per_objfile->n_allocated_type_units
5044 = htab_elements (types_htab);
8d749320
SM
5045 dwarf2_per_objfile->all_type_units =
5046 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5047 iter = &dwarf2_per_objfile->all_type_units[0];
5048 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5049 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5050 == dwarf2_per_objfile->n_type_units);
1fd400ff 5051
348e048f
DE
5052 return 1;
5053}
5054
6aa5f3a6
DE
5055/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5056 If SLOT is non-NULL, it is the entry to use in the hash table.
5057 Otherwise we find one. */
5058
5059static struct signatured_type *
5060add_type_unit (ULONGEST sig, void **slot)
5061{
5062 struct objfile *objfile = dwarf2_per_objfile->objfile;
5063 int n_type_units = dwarf2_per_objfile->n_type_units;
5064 struct signatured_type *sig_type;
5065
5066 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5067 ++n_type_units;
5068 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5069 {
5070 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5071 dwarf2_per_objfile->n_allocated_type_units = 1;
5072 dwarf2_per_objfile->n_allocated_type_units *= 2;
5073 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5074 = XRESIZEVEC (struct signatured_type *,
5075 dwarf2_per_objfile->all_type_units,
5076 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5077 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5078 }
5079 dwarf2_per_objfile->n_type_units = n_type_units;
5080
5081 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5082 struct signatured_type);
5083 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5084 sig_type->signature = sig;
5085 sig_type->per_cu.is_debug_types = 1;
5086 if (dwarf2_per_objfile->using_index)
5087 {
5088 sig_type->per_cu.v.quick =
5089 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5090 struct dwarf2_per_cu_quick_data);
5091 }
5092
5093 if (slot == NULL)
5094 {
5095 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5096 sig_type, INSERT);
5097 }
5098 gdb_assert (*slot == NULL);
5099 *slot = sig_type;
5100 /* The rest of sig_type must be filled in by the caller. */
5101 return sig_type;
5102}
5103
a2ce51a0
DE
5104/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5105 Fill in SIG_ENTRY with DWO_ENTRY. */
5106
5107static void
5108fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5109 struct signatured_type *sig_entry,
5110 struct dwo_unit *dwo_entry)
5111{
7ee85ab1 5112 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5113 gdb_assert (! sig_entry->per_cu.queued);
5114 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5115 if (dwarf2_per_objfile->using_index)
5116 {
5117 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5118 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5119 }
5120 else
5121 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5122 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5123 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5124 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5125 gdb_assert (sig_entry->dwo_unit == NULL);
5126
5127 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5128 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5129 sig_entry->per_cu.length = dwo_entry->length;
5130 sig_entry->per_cu.reading_dwo_directly = 1;
5131 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5132 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5133 sig_entry->dwo_unit = dwo_entry;
5134}
5135
5136/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5137 If we haven't read the TU yet, create the signatured_type data structure
5138 for a TU to be read in directly from a DWO file, bypassing the stub.
5139 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5140 using .gdb_index, then when reading a CU we want to stay in the DWO file
5141 containing that CU. Otherwise we could end up reading several other DWO
5142 files (due to comdat folding) to process the transitive closure of all the
5143 mentioned TUs, and that can be slow. The current DWO file will have every
5144 type signature that it needs.
a2ce51a0
DE
5145 We only do this for .gdb_index because in the psymtab case we already have
5146 to read all the DWOs to build the type unit groups. */
5147
5148static struct signatured_type *
5149lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5150{
5151 struct objfile *objfile = dwarf2_per_objfile->objfile;
5152 struct dwo_file *dwo_file;
5153 struct dwo_unit find_dwo_entry, *dwo_entry;
5154 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5155 void **slot;
a2ce51a0
DE
5156
5157 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5158
6aa5f3a6
DE
5159 /* If TU skeletons have been removed then we may not have read in any
5160 TUs yet. */
5161 if (dwarf2_per_objfile->signatured_types == NULL)
5162 {
5163 dwarf2_per_objfile->signatured_types
5164 = allocate_signatured_type_table (objfile);
5165 }
a2ce51a0
DE
5166
5167 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5168 Use the global signatured_types array to do our own comdat-folding
5169 of types. If this is the first time we're reading this TU, and
5170 the TU has an entry in .gdb_index, replace the recorded data from
5171 .gdb_index with this TU. */
a2ce51a0 5172
a2ce51a0 5173 find_sig_entry.signature = sig;
6aa5f3a6
DE
5174 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5175 &find_sig_entry, INSERT);
9a3c8263 5176 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5177
5178 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5179 read. Don't reassign the global entry to point to this DWO if that's
5180 the case. Also note that if the TU is already being read, it may not
5181 have come from a DWO, the program may be a mix of Fission-compiled
5182 code and non-Fission-compiled code. */
5183
5184 /* Have we already tried to read this TU?
5185 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5186 needn't exist in the global table yet). */
5187 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5188 return sig_entry;
5189
6aa5f3a6
DE
5190 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5191 dwo_unit of the TU itself. */
5192 dwo_file = cu->dwo_unit->dwo_file;
5193
a2ce51a0
DE
5194 /* Ok, this is the first time we're reading this TU. */
5195 if (dwo_file->tus == NULL)
5196 return NULL;
5197 find_dwo_entry.signature = sig;
9a3c8263 5198 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5199 if (dwo_entry == NULL)
5200 return NULL;
5201
6aa5f3a6
DE
5202 /* If the global table doesn't have an entry for this TU, add one. */
5203 if (sig_entry == NULL)
5204 sig_entry = add_type_unit (sig, slot);
5205
a2ce51a0 5206 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5207 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5208 return sig_entry;
5209}
5210
a2ce51a0
DE
5211/* Subroutine of lookup_signatured_type.
5212 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5213 then try the DWP file. If the TU stub (skeleton) has been removed then
5214 it won't be in .gdb_index. */
a2ce51a0
DE
5215
5216static struct signatured_type *
5217lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5218{
5219 struct objfile *objfile = dwarf2_per_objfile->objfile;
5220 struct dwp_file *dwp_file = get_dwp_file ();
5221 struct dwo_unit *dwo_entry;
5222 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5223 void **slot;
a2ce51a0
DE
5224
5225 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5226 gdb_assert (dwp_file != NULL);
5227
6aa5f3a6
DE
5228 /* If TU skeletons have been removed then we may not have read in any
5229 TUs yet. */
5230 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5231 {
6aa5f3a6
DE
5232 dwarf2_per_objfile->signatured_types
5233 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5234 }
5235
6aa5f3a6
DE
5236 find_sig_entry.signature = sig;
5237 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5238 &find_sig_entry, INSERT);
9a3c8263 5239 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5240
5241 /* Have we already tried to read this TU?
5242 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5243 needn't exist in the global table yet). */
5244 if (sig_entry != NULL)
5245 return sig_entry;
5246
a2ce51a0
DE
5247 if (dwp_file->tus == NULL)
5248 return NULL;
57d63ce2
DE
5249 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5250 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5251 if (dwo_entry == NULL)
5252 return NULL;
5253
6aa5f3a6 5254 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5255 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5256
a2ce51a0
DE
5257 return sig_entry;
5258}
5259
380bca97 5260/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5261 Returns NULL if signature SIG is not present in the table.
5262 It is up to the caller to complain about this. */
348e048f
DE
5263
5264static struct signatured_type *
a2ce51a0 5265lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5266{
a2ce51a0
DE
5267 if (cu->dwo_unit
5268 && dwarf2_per_objfile->using_index)
5269 {
5270 /* We're in a DWO/DWP file, and we're using .gdb_index.
5271 These cases require special processing. */
5272 if (get_dwp_file () == NULL)
5273 return lookup_dwo_signatured_type (cu, sig);
5274 else
5275 return lookup_dwp_signatured_type (cu, sig);
5276 }
5277 else
5278 {
5279 struct signatured_type find_entry, *entry;
348e048f 5280
a2ce51a0
DE
5281 if (dwarf2_per_objfile->signatured_types == NULL)
5282 return NULL;
5283 find_entry.signature = sig;
9a3c8263
SM
5284 entry = ((struct signatured_type *)
5285 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5286 return entry;
5287 }
348e048f 5288}
42e7ad6c
DE
5289\f
5290/* Low level DIE reading support. */
348e048f 5291
d85a05f0
DJ
5292/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5293
5294static void
5295init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5296 struct dwarf2_cu *cu,
3019eac3
DE
5297 struct dwarf2_section_info *section,
5298 struct dwo_file *dwo_file)
d85a05f0 5299{
fceca515 5300 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5301 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5302 reader->cu = cu;
3019eac3 5303 reader->dwo_file = dwo_file;
dee91e82
DE
5304 reader->die_section = section;
5305 reader->buffer = section->buffer;
f664829e 5306 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5307 reader->comp_dir = NULL;
d85a05f0
DJ
5308}
5309
b0c7bfa9
DE
5310/* Subroutine of init_cutu_and_read_dies to simplify it.
5311 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5312 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5313 already.
5314
5315 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5316 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5317 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5318 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5319 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5320 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5321 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5322 are filled in with the info of the DIE from the DWO file.
5323 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5324 provided an abbrev table to use.
5325 The result is non-zero if a valid (non-dummy) DIE was found. */
5326
5327static int
5328read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5329 struct dwo_unit *dwo_unit,
5330 int abbrev_table_provided,
5331 struct die_info *stub_comp_unit_die,
a2ce51a0 5332 const char *stub_comp_dir,
b0c7bfa9 5333 struct die_reader_specs *result_reader,
d521ce57 5334 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5335 struct die_info **result_comp_unit_die,
5336 int *result_has_children)
5337{
5338 struct objfile *objfile = dwarf2_per_objfile->objfile;
5339 struct dwarf2_cu *cu = this_cu->cu;
5340 struct dwarf2_section_info *section;
5341 bfd *abfd;
d521ce57 5342 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5343 ULONGEST signature; /* Or dwo_id. */
5344 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5345 int i,num_extra_attrs;
5346 struct dwarf2_section_info *dwo_abbrev_section;
5347 struct attribute *attr;
5348 struct die_info *comp_unit_die;
5349
b0aeadb3
DE
5350 /* At most one of these may be provided. */
5351 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5352
b0c7bfa9
DE
5353 /* These attributes aren't processed until later:
5354 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5355 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5356 referenced later. However, these attributes are found in the stub
5357 which we won't have later. In order to not impose this complication
5358 on the rest of the code, we read them here and copy them to the
5359 DWO CU/TU die. */
b0c7bfa9
DE
5360
5361 stmt_list = NULL;
5362 low_pc = NULL;
5363 high_pc = NULL;
5364 ranges = NULL;
5365 comp_dir = NULL;
5366
5367 if (stub_comp_unit_die != NULL)
5368 {
5369 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5370 DWO file. */
5371 if (! this_cu->is_debug_types)
5372 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5373 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5374 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5375 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5376 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5377
5378 /* There should be a DW_AT_addr_base attribute here (if needed).
5379 We need the value before we can process DW_FORM_GNU_addr_index. */
5380 cu->addr_base = 0;
5381 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5382 if (attr)
5383 cu->addr_base = DW_UNSND (attr);
5384
5385 /* There should be a DW_AT_ranges_base attribute here (if needed).
5386 We need the value before we can process DW_AT_ranges. */
5387 cu->ranges_base = 0;
5388 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5389 if (attr)
5390 cu->ranges_base = DW_UNSND (attr);
5391 }
a2ce51a0
DE
5392 else if (stub_comp_dir != NULL)
5393 {
5394 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5395 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5396 comp_dir->name = DW_AT_comp_dir;
5397 comp_dir->form = DW_FORM_string;
5398 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5399 DW_STRING (comp_dir) = stub_comp_dir;
5400 }
b0c7bfa9
DE
5401
5402 /* Set up for reading the DWO CU/TU. */
5403 cu->dwo_unit = dwo_unit;
5404 section = dwo_unit->section;
5405 dwarf2_read_section (objfile, section);
a32a8923 5406 abfd = get_section_bfd_owner (section);
9c541725
PA
5407 begin_info_ptr = info_ptr = (section->buffer
5408 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5409 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5410 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5411
5412 if (this_cu->is_debug_types)
5413 {
b0c7bfa9
DE
5414 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5415
43988095 5416 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5417 dwo_abbrev_section,
43988095 5418 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5419 /* This is not an assert because it can be caused by bad debug info. */
43988095 5420 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5421 {
5422 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5423 " TU at offset 0x%x [in module %s]"),
5424 hex_string (sig_type->signature),
43988095 5425 hex_string (cu->header.signature),
9c541725 5426 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5427 bfd_get_filename (abfd));
5428 }
9c541725 5429 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5430 /* For DWOs coming from DWP files, we don't know the CU length
5431 nor the type's offset in the TU until now. */
5432 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5433 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5434
5435 /* Establish the type offset that can be used to lookup the type.
5436 For DWO files, we don't know it until now. */
9c541725
PA
5437 sig_type->type_offset_in_section
5438 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5439 }
5440 else
5441 {
5442 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5443 dwo_abbrev_section,
43988095 5444 info_ptr, rcuh_kind::COMPILE);
9c541725 5445 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5446 /* For DWOs coming from DWP files, we don't know the CU length
5447 until now. */
5448 dwo_unit->length = get_cu_length (&cu->header);
5449 }
5450
02142a6c
DE
5451 /* Replace the CU's original abbrev table with the DWO's.
5452 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5453 if (abbrev_table_provided)
5454 {
5455 /* Don't free the provided abbrev table, the caller of
5456 init_cutu_and_read_dies owns it. */
5457 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5458 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5459 make_cleanup (dwarf2_free_abbrev_table, cu);
5460 }
5461 else
5462 {
5463 dwarf2_free_abbrev_table (cu);
5464 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5465 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5466 }
5467
5468 /* Read in the die, but leave space to copy over the attributes
5469 from the stub. This has the benefit of simplifying the rest of
5470 the code - all the work to maintain the illusion of a single
5471 DW_TAG_{compile,type}_unit DIE is done here. */
5472 num_extra_attrs = ((stmt_list != NULL)
5473 + (low_pc != NULL)
5474 + (high_pc != NULL)
5475 + (ranges != NULL)
5476 + (comp_dir != NULL));
5477 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5478 result_has_children, num_extra_attrs);
5479
5480 /* Copy over the attributes from the stub to the DIE we just read in. */
5481 comp_unit_die = *result_comp_unit_die;
5482 i = comp_unit_die->num_attrs;
5483 if (stmt_list != NULL)
5484 comp_unit_die->attrs[i++] = *stmt_list;
5485 if (low_pc != NULL)
5486 comp_unit_die->attrs[i++] = *low_pc;
5487 if (high_pc != NULL)
5488 comp_unit_die->attrs[i++] = *high_pc;
5489 if (ranges != NULL)
5490 comp_unit_die->attrs[i++] = *ranges;
5491 if (comp_dir != NULL)
5492 comp_unit_die->attrs[i++] = *comp_dir;
5493 comp_unit_die->num_attrs += num_extra_attrs;
5494
b4f54984 5495 if (dwarf_die_debug)
bf6af496
DE
5496 {
5497 fprintf_unfiltered (gdb_stdlog,
5498 "Read die from %s@0x%x of %s:\n",
a32a8923 5499 get_section_name (section),
bf6af496
DE
5500 (unsigned) (begin_info_ptr - section->buffer),
5501 bfd_get_filename (abfd));
b4f54984 5502 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5503 }
5504
a2ce51a0
DE
5505 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5506 TUs by skipping the stub and going directly to the entry in the DWO file.
5507 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5508 to get it via circuitous means. Blech. */
5509 if (comp_dir != NULL)
5510 result_reader->comp_dir = DW_STRING (comp_dir);
5511
b0c7bfa9
DE
5512 /* Skip dummy compilation units. */
5513 if (info_ptr >= begin_info_ptr + dwo_unit->length
5514 || peek_abbrev_code (abfd, info_ptr) == 0)
5515 return 0;
5516
5517 *result_info_ptr = info_ptr;
5518 return 1;
5519}
5520
5521/* Subroutine of init_cutu_and_read_dies to simplify it.
5522 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5523 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5524
5525static struct dwo_unit *
5526lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5527 struct die_info *comp_unit_die)
5528{
5529 struct dwarf2_cu *cu = this_cu->cu;
5530 struct attribute *attr;
5531 ULONGEST signature;
5532 struct dwo_unit *dwo_unit;
5533 const char *comp_dir, *dwo_name;
5534
a2ce51a0
DE
5535 gdb_assert (cu != NULL);
5536
b0c7bfa9 5537 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5538 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5539 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5540
5541 if (this_cu->is_debug_types)
5542 {
5543 struct signatured_type *sig_type;
5544
5545 /* Since this_cu is the first member of struct signatured_type,
5546 we can go from a pointer to one to a pointer to the other. */
5547 sig_type = (struct signatured_type *) this_cu;
5548 signature = sig_type->signature;
5549 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5550 }
5551 else
5552 {
5553 struct attribute *attr;
5554
5555 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5556 if (! attr)
5557 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5558 " [in module %s]"),
4262abfb 5559 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5560 signature = DW_UNSND (attr);
5561 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5562 signature);
5563 }
5564
b0c7bfa9
DE
5565 return dwo_unit;
5566}
5567
a2ce51a0 5568/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5569 See it for a description of the parameters.
5570 Read a TU directly from a DWO file, bypassing the stub.
5571
5572 Note: This function could be a little bit simpler if we shared cleanups
5573 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5574 to do, so we keep this function self-contained. Or we could move this
5575 into our caller, but it's complex enough already. */
a2ce51a0
DE
5576
5577static void
6aa5f3a6
DE
5578init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5579 int use_existing_cu, int keep,
a2ce51a0
DE
5580 die_reader_func_ftype *die_reader_func,
5581 void *data)
5582{
5583 struct dwarf2_cu *cu;
5584 struct signatured_type *sig_type;
6aa5f3a6 5585 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5586 struct die_reader_specs reader;
5587 const gdb_byte *info_ptr;
5588 struct die_info *comp_unit_die;
5589 int has_children;
5590
5591 /* Verify we can do the following downcast, and that we have the
5592 data we need. */
5593 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5594 sig_type = (struct signatured_type *) this_cu;
5595 gdb_assert (sig_type->dwo_unit != NULL);
5596
5597 cleanups = make_cleanup (null_cleanup, NULL);
5598
6aa5f3a6
DE
5599 if (use_existing_cu && this_cu->cu != NULL)
5600 {
5601 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5602 cu = this_cu->cu;
5603 /* There's no need to do the rereading_dwo_cu handling that
5604 init_cutu_and_read_dies does since we don't read the stub. */
5605 }
5606 else
5607 {
5608 /* If !use_existing_cu, this_cu->cu must be NULL. */
5609 gdb_assert (this_cu->cu == NULL);
8d749320 5610 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5611 init_one_comp_unit (cu, this_cu);
5612 /* If an error occurs while loading, release our storage. */
5613 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5614 }
5615
5616 /* A future optimization, if needed, would be to use an existing
5617 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5618 could share abbrev tables. */
a2ce51a0
DE
5619
5620 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5621 0 /* abbrev_table_provided */,
5622 NULL /* stub_comp_unit_die */,
5623 sig_type->dwo_unit->dwo_file->comp_dir,
5624 &reader, &info_ptr,
5625 &comp_unit_die, &has_children) == 0)
5626 {
5627 /* Dummy die. */
5628 do_cleanups (cleanups);
5629 return;
5630 }
5631
5632 /* All the "real" work is done here. */
5633 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5634
6aa5f3a6 5635 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5636 but the alternative is making the latter more complex.
5637 This function is only for the special case of using DWO files directly:
5638 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5639 if (free_cu_cleanup != NULL)
a2ce51a0 5640 {
6aa5f3a6
DE
5641 if (keep)
5642 {
5643 /* We've successfully allocated this compilation unit. Let our
5644 caller clean it up when finished with it. */
5645 discard_cleanups (free_cu_cleanup);
a2ce51a0 5646
6aa5f3a6
DE
5647 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5648 So we have to manually free the abbrev table. */
5649 dwarf2_free_abbrev_table (cu);
a2ce51a0 5650
6aa5f3a6
DE
5651 /* Link this CU into read_in_chain. */
5652 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5653 dwarf2_per_objfile->read_in_chain = this_cu;
5654 }
5655 else
5656 do_cleanups (free_cu_cleanup);
a2ce51a0 5657 }
a2ce51a0
DE
5658
5659 do_cleanups (cleanups);
5660}
5661
fd820528 5662/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5663 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5664
f4dc4d17
DE
5665 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5666 Otherwise the table specified in the comp unit header is read in and used.
5667 This is an optimization for when we already have the abbrev table.
5668
dee91e82
DE
5669 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5670 Otherwise, a new CU is allocated with xmalloc.
5671
5672 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5673 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5674
5675 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5676 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5677
70221824 5678static void
fd820528 5679init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5680 struct abbrev_table *abbrev_table,
fd820528
DE
5681 int use_existing_cu, int keep,
5682 die_reader_func_ftype *die_reader_func,
5683 void *data)
c906108c 5684{
dee91e82 5685 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5686 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5687 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5688 struct dwarf2_cu *cu;
d521ce57 5689 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5690 struct die_reader_specs reader;
d85a05f0 5691 struct die_info *comp_unit_die;
dee91e82 5692 int has_children;
d85a05f0 5693 struct attribute *attr;
365156ad 5694 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5695 struct signatured_type *sig_type = NULL;
4bdcc0c1 5696 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5697 /* Non-zero if CU currently points to a DWO file and we need to
5698 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5699 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5700 int rereading_dwo_cu = 0;
c906108c 5701
b4f54984 5702 if (dwarf_die_debug)
09406207
DE
5703 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5704 this_cu->is_debug_types ? "type" : "comp",
9c541725 5705 to_underlying (this_cu->sect_off));
09406207 5706
dee91e82
DE
5707 if (use_existing_cu)
5708 gdb_assert (keep);
23745b47 5709
a2ce51a0
DE
5710 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5711 file (instead of going through the stub), short-circuit all of this. */
5712 if (this_cu->reading_dwo_directly)
5713 {
5714 /* Narrow down the scope of possibilities to have to understand. */
5715 gdb_assert (this_cu->is_debug_types);
5716 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5717 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5718 die_reader_func, data);
a2ce51a0
DE
5719 return;
5720 }
5721
dee91e82
DE
5722 cleanups = make_cleanup (null_cleanup, NULL);
5723
5724 /* This is cheap if the section is already read in. */
5725 dwarf2_read_section (objfile, section);
5726
9c541725 5727 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5728
5729 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5730
5731 if (use_existing_cu && this_cu->cu != NULL)
5732 {
5733 cu = this_cu->cu;
42e7ad6c
DE
5734 /* If this CU is from a DWO file we need to start over, we need to
5735 refetch the attributes from the skeleton CU.
5736 This could be optimized by retrieving those attributes from when we
5737 were here the first time: the previous comp_unit_die was stored in
5738 comp_unit_obstack. But there's no data yet that we need this
5739 optimization. */
5740 if (cu->dwo_unit != NULL)
5741 rereading_dwo_cu = 1;
dee91e82
DE
5742 }
5743 else
5744 {
5745 /* If !use_existing_cu, this_cu->cu must be NULL. */
5746 gdb_assert (this_cu->cu == NULL);
8d749320 5747 cu = XNEW (struct dwarf2_cu);
dee91e82 5748 init_one_comp_unit (cu, this_cu);
dee91e82 5749 /* If an error occurs while loading, release our storage. */
365156ad 5750 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5751 }
dee91e82 5752
b0c7bfa9 5753 /* Get the header. */
9c541725 5754 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5755 {
5756 /* We already have the header, there's no need to read it in again. */
9c541725 5757 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5758 }
5759 else
5760 {
3019eac3 5761 if (this_cu->is_debug_types)
dee91e82 5762 {
43988095 5763 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5764 abbrev_section, info_ptr,
43988095 5765 rcuh_kind::TYPE);
dee91e82 5766
42e7ad6c
DE
5767 /* Since per_cu is the first member of struct signatured_type,
5768 we can go from a pointer to one to a pointer to the other. */
5769 sig_type = (struct signatured_type *) this_cu;
43988095 5770 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5771 gdb_assert (sig_type->type_offset_in_tu
5772 == cu->header.type_cu_offset_in_tu);
5773 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5774
42e7ad6c
DE
5775 /* LENGTH has not been set yet for type units if we're
5776 using .gdb_index. */
1ce1cefd 5777 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5778
5779 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5780 sig_type->type_offset_in_section =
5781 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5782
5783 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5784 }
5785 else
5786 {
4bdcc0c1
DE
5787 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5788 abbrev_section,
43988095
JK
5789 info_ptr,
5790 rcuh_kind::COMPILE);
dee91e82 5791
9c541725 5792 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5793 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5794 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5795 }
5796 }
10b3939b 5797
6caca83c 5798 /* Skip dummy compilation units. */
dee91e82 5799 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5800 || peek_abbrev_code (abfd, info_ptr) == 0)
5801 {
dee91e82 5802 do_cleanups (cleanups);
21b2bd31 5803 return;
6caca83c
CC
5804 }
5805
433df2d4
DE
5806 /* If we don't have them yet, read the abbrevs for this compilation unit.
5807 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5808 done. Note that it's important that if the CU had an abbrev table
5809 on entry we don't free it when we're done: Somewhere up the call stack
5810 it may be in use. */
f4dc4d17
DE
5811 if (abbrev_table != NULL)
5812 {
5813 gdb_assert (cu->abbrev_table == NULL);
9c541725 5814 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5815 cu->abbrev_table = abbrev_table;
5816 }
5817 else if (cu->abbrev_table == NULL)
dee91e82 5818 {
4bdcc0c1 5819 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5820 make_cleanup (dwarf2_free_abbrev_table, cu);
5821 }
42e7ad6c
DE
5822 else if (rereading_dwo_cu)
5823 {
5824 dwarf2_free_abbrev_table (cu);
5825 dwarf2_read_abbrevs (cu, abbrev_section);
5826 }
af703f96 5827
dee91e82 5828 /* Read the top level CU/TU die. */
3019eac3 5829 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5830 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5831
b0c7bfa9
DE
5832 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5833 from the DWO file.
5834 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5835 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5836 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5837 if (attr)
5838 {
3019eac3 5839 struct dwo_unit *dwo_unit;
b0c7bfa9 5840 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5841
5842 if (has_children)
6a506a2d
DE
5843 {
5844 complaint (&symfile_complaints,
5845 _("compilation unit with DW_AT_GNU_dwo_name"
5846 " has children (offset 0x%x) [in module %s]"),
9c541725 5847 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5848 }
b0c7bfa9 5849 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5850 if (dwo_unit != NULL)
3019eac3 5851 {
6a506a2d
DE
5852 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5853 abbrev_table != NULL,
a2ce51a0 5854 comp_unit_die, NULL,
6a506a2d
DE
5855 &reader, &info_ptr,
5856 &dwo_comp_unit_die, &has_children) == 0)
5857 {
5858 /* Dummy die. */
5859 do_cleanups (cleanups);
5860 return;
5861 }
5862 comp_unit_die = dwo_comp_unit_die;
5863 }
5864 else
5865 {
5866 /* Yikes, we couldn't find the rest of the DIE, we only have
5867 the stub. A complaint has already been logged. There's
5868 not much more we can do except pass on the stub DIE to
5869 die_reader_func. We don't want to throw an error on bad
5870 debug info. */
3019eac3
DE
5871 }
5872 }
5873
b0c7bfa9 5874 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5875 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5876
b0c7bfa9 5877 /* Done, clean up. */
365156ad 5878 if (free_cu_cleanup != NULL)
348e048f 5879 {
365156ad
TT
5880 if (keep)
5881 {
5882 /* We've successfully allocated this compilation unit. Let our
5883 caller clean it up when finished with it. */
5884 discard_cleanups (free_cu_cleanup);
dee91e82 5885
365156ad
TT
5886 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5887 So we have to manually free the abbrev table. */
5888 dwarf2_free_abbrev_table (cu);
dee91e82 5889
365156ad
TT
5890 /* Link this CU into read_in_chain. */
5891 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5892 dwarf2_per_objfile->read_in_chain = this_cu;
5893 }
5894 else
5895 do_cleanups (free_cu_cleanup);
348e048f 5896 }
365156ad
TT
5897
5898 do_cleanups (cleanups);
dee91e82
DE
5899}
5900
33e80786
DE
5901/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5902 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5903 to have already done the lookup to find the DWO file).
dee91e82
DE
5904
5905 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5906 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5907
5908 We fill in THIS_CU->length.
5909
5910 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5911 linker) then DIE_READER_FUNC will not get called.
5912
5913 THIS_CU->cu is always freed when done.
3019eac3
DE
5914 This is done in order to not leave THIS_CU->cu in a state where we have
5915 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5916
5917static void
5918init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5919 struct dwo_file *dwo_file,
dee91e82
DE
5920 die_reader_func_ftype *die_reader_func,
5921 void *data)
5922{
5923 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5924 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5925 bfd *abfd = get_section_bfd_owner (section);
33e80786 5926 struct dwarf2_section_info *abbrev_section;
dee91e82 5927 struct dwarf2_cu cu;
d521ce57 5928 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5929 struct die_reader_specs reader;
5930 struct cleanup *cleanups;
5931 struct die_info *comp_unit_die;
5932 int has_children;
5933
b4f54984 5934 if (dwarf_die_debug)
09406207
DE
5935 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5936 this_cu->is_debug_types ? "type" : "comp",
9c541725 5937 to_underlying (this_cu->sect_off));
09406207 5938
dee91e82
DE
5939 gdb_assert (this_cu->cu == NULL);
5940
33e80786
DE
5941 abbrev_section = (dwo_file != NULL
5942 ? &dwo_file->sections.abbrev
5943 : get_abbrev_section_for_cu (this_cu));
5944
dee91e82
DE
5945 /* This is cheap if the section is already read in. */
5946 dwarf2_read_section (objfile, section);
5947
5948 init_one_comp_unit (&cu, this_cu);
5949
5950 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5951
9c541725 5952 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5953 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5954 abbrev_section, info_ptr,
43988095
JK
5955 (this_cu->is_debug_types
5956 ? rcuh_kind::TYPE
5957 : rcuh_kind::COMPILE));
dee91e82 5958
1ce1cefd 5959 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5960
5961 /* Skip dummy compilation units. */
5962 if (info_ptr >= begin_info_ptr + this_cu->length
5963 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5964 {
dee91e82 5965 do_cleanups (cleanups);
21b2bd31 5966 return;
93311388 5967 }
72bf9492 5968
dee91e82
DE
5969 dwarf2_read_abbrevs (&cu, abbrev_section);
5970 make_cleanup (dwarf2_free_abbrev_table, &cu);
5971
3019eac3 5972 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5973 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5974
5975 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5976
5977 do_cleanups (cleanups);
5978}
5979
3019eac3
DE
5980/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5981 does not lookup the specified DWO file.
5982 This cannot be used to read DWO files.
dee91e82
DE
5983
5984 THIS_CU->cu is always freed when done.
3019eac3
DE
5985 This is done in order to not leave THIS_CU->cu in a state where we have
5986 to care whether it refers to the "main" CU or the DWO CU.
5987 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5988
5989static void
5990init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5991 die_reader_func_ftype *die_reader_func,
5992 void *data)
5993{
33e80786 5994 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5995}
0018ea6f
DE
5996\f
5997/* Type Unit Groups.
dee91e82 5998
0018ea6f
DE
5999 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6000 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6001 so that all types coming from the same compilation (.o file) are grouped
6002 together. A future step could be to put the types in the same symtab as
6003 the CU the types ultimately came from. */
ff013f42 6004
f4dc4d17
DE
6005static hashval_t
6006hash_type_unit_group (const void *item)
6007{
9a3c8263
SM
6008 const struct type_unit_group *tu_group
6009 = (const struct type_unit_group *) item;
f4dc4d17 6010
094b34ac 6011 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6012}
348e048f
DE
6013
6014static int
f4dc4d17 6015eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6016{
9a3c8263
SM
6017 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6018 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6019
094b34ac 6020 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6021}
348e048f 6022
f4dc4d17
DE
6023/* Allocate a hash table for type unit groups. */
6024
6025static htab_t
6026allocate_type_unit_groups_table (void)
6027{
6028 return htab_create_alloc_ex (3,
6029 hash_type_unit_group,
6030 eq_type_unit_group,
6031 NULL,
6032 &dwarf2_per_objfile->objfile->objfile_obstack,
6033 hashtab_obstack_allocate,
6034 dummy_obstack_deallocate);
6035}
dee91e82 6036
f4dc4d17
DE
6037/* Type units that don't have DW_AT_stmt_list are grouped into their own
6038 partial symtabs. We combine several TUs per psymtab to not let the size
6039 of any one psymtab grow too big. */
6040#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6041#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6042
094b34ac 6043/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6044 Create the type_unit_group object used to hold one or more TUs. */
6045
6046static struct type_unit_group *
094b34ac 6047create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6048{
6049 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6050 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6051 struct type_unit_group *tu_group;
f4dc4d17
DE
6052
6053 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6054 struct type_unit_group);
094b34ac 6055 per_cu = &tu_group->per_cu;
f4dc4d17 6056 per_cu->objfile = objfile;
f4dc4d17 6057
094b34ac
DE
6058 if (dwarf2_per_objfile->using_index)
6059 {
6060 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6061 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6062 }
6063 else
6064 {
9c541725 6065 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6066 struct partial_symtab *pst;
6067 char *name;
6068
6069 /* Give the symtab a useful name for debug purposes. */
6070 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6071 name = xstrprintf ("<type_units_%d>",
6072 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6073 else
6074 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6075
6076 pst = create_partial_symtab (per_cu, name);
6077 pst->anonymous = 1;
f4dc4d17 6078
094b34ac
DE
6079 xfree (name);
6080 }
f4dc4d17 6081
094b34ac 6082 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6083 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6084
6085 return tu_group;
6086}
6087
094b34ac
DE
6088/* Look up the type_unit_group for type unit CU, and create it if necessary.
6089 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6090
6091static struct type_unit_group *
ff39bb5e 6092get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6093{
6094 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6095 struct type_unit_group *tu_group;
6096 void **slot;
6097 unsigned int line_offset;
6098 struct type_unit_group type_unit_group_for_lookup;
6099
6100 if (dwarf2_per_objfile->type_unit_groups == NULL)
6101 {
6102 dwarf2_per_objfile->type_unit_groups =
6103 allocate_type_unit_groups_table ();
6104 }
6105
6106 /* Do we need to create a new group, or can we use an existing one? */
6107
6108 if (stmt_list)
6109 {
6110 line_offset = DW_UNSND (stmt_list);
6111 ++tu_stats->nr_symtab_sharers;
6112 }
6113 else
6114 {
6115 /* Ugh, no stmt_list. Rare, but we have to handle it.
6116 We can do various things here like create one group per TU or
6117 spread them over multiple groups to split up the expansion work.
6118 To avoid worst case scenarios (too many groups or too large groups)
6119 we, umm, group them in bunches. */
6120 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6121 | (tu_stats->nr_stmt_less_type_units
6122 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6123 ++tu_stats->nr_stmt_less_type_units;
6124 }
6125
094b34ac 6126 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6127 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6128 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6129 &type_unit_group_for_lookup, INSERT);
6130 if (*slot != NULL)
6131 {
9a3c8263 6132 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6133 gdb_assert (tu_group != NULL);
6134 }
6135 else
6136 {
9c541725 6137 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6138 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6139 *slot = tu_group;
6140 ++tu_stats->nr_symtabs;
6141 }
6142
6143 return tu_group;
6144}
0018ea6f
DE
6145\f
6146/* Partial symbol tables. */
6147
6148/* Create a psymtab named NAME and assign it to PER_CU.
6149
6150 The caller must fill in the following details:
6151 dirname, textlow, texthigh. */
6152
6153static struct partial_symtab *
6154create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6155{
6156 struct objfile *objfile = per_cu->objfile;
6157 struct partial_symtab *pst;
6158
18a94d75 6159 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6160 objfile->global_psymbols.next,
6161 objfile->static_psymbols.next);
6162
6163 pst->psymtabs_addrmap_supported = 1;
6164
6165 /* This is the glue that links PST into GDB's symbol API. */
6166 pst->read_symtab_private = per_cu;
6167 pst->read_symtab = dwarf2_read_symtab;
6168 per_cu->v.psymtab = pst;
6169
6170 return pst;
6171}
6172
b93601f3
TT
6173/* The DATA object passed to process_psymtab_comp_unit_reader has this
6174 type. */
6175
6176struct process_psymtab_comp_unit_data
6177{
6178 /* True if we are reading a DW_TAG_partial_unit. */
6179
6180 int want_partial_unit;
6181
6182 /* The "pretend" language that is used if the CU doesn't declare a
6183 language. */
6184
6185 enum language pretend_language;
6186};
6187
0018ea6f
DE
6188/* die_reader_func for process_psymtab_comp_unit. */
6189
6190static void
6191process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6192 const gdb_byte *info_ptr,
0018ea6f
DE
6193 struct die_info *comp_unit_die,
6194 int has_children,
6195 void *data)
6196{
6197 struct dwarf2_cu *cu = reader->cu;
6198 struct objfile *objfile = cu->objfile;
3e29f34a 6199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6200 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6201 CORE_ADDR baseaddr;
6202 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6203 struct partial_symtab *pst;
3a2b436a 6204 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6205 const char *filename;
9a3c8263
SM
6206 struct process_psymtab_comp_unit_data *info
6207 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6208
b93601f3 6209 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6210 return;
6211
6212 gdb_assert (! per_cu->is_debug_types);
6213
b93601f3 6214 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6215
6216 cu->list_in_scope = &file_symbols;
6217
6218 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6219 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6220 if (filename == NULL)
0018ea6f 6221 filename = "";
0018ea6f
DE
6222
6223 pst = create_partial_symtab (per_cu, filename);
6224
6225 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6226 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6227
6228 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6229
6230 dwarf2_find_base_address (comp_unit_die, cu);
6231
6232 /* Possibly set the default values of LOWPC and HIGHPC from
6233 `DW_AT_ranges'. */
3a2b436a
JK
6234 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6235 &best_highpc, cu, pst);
6236 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6237 /* Store the contiguous range if it is not empty; it can be empty for
6238 CUs with no code. */
6239 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6240 gdbarch_adjust_dwarf2_addr (gdbarch,
6241 best_lowpc + baseaddr),
6242 gdbarch_adjust_dwarf2_addr (gdbarch,
6243 best_highpc + baseaddr) - 1,
6244 pst);
0018ea6f
DE
6245
6246 /* Check if comp unit has_children.
6247 If so, read the rest of the partial symbols from this comp unit.
6248 If not, there's no more debug_info for this comp unit. */
6249 if (has_children)
6250 {
6251 struct partial_die_info *first_die;
6252 CORE_ADDR lowpc, highpc;
6253
6254 lowpc = ((CORE_ADDR) -1);
6255 highpc = ((CORE_ADDR) 0);
6256
6257 first_die = load_partial_dies (reader, info_ptr, 1);
6258
6259 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6260 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6261
6262 /* If we didn't find a lowpc, set it to highpc to avoid
6263 complaints from `maint check'. */
6264 if (lowpc == ((CORE_ADDR) -1))
6265 lowpc = highpc;
6266
6267 /* If the compilation unit didn't have an explicit address range,
6268 then use the information extracted from its child dies. */
e385593e 6269 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6270 {
6271 best_lowpc = lowpc;
6272 best_highpc = highpc;
6273 }
6274 }
3e29f34a
MR
6275 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6276 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6277
8763cede 6278 end_psymtab_common (objfile, pst);
0018ea6f
DE
6279
6280 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6281 {
6282 int i;
6283 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6284 struct dwarf2_per_cu_data *iter;
6285
6286 /* Fill in 'dependencies' here; we fill in 'users' in a
6287 post-pass. */
6288 pst->number_of_dependencies = len;
8d749320
SM
6289 pst->dependencies =
6290 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6291 for (i = 0;
6292 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6293 i, iter);
6294 ++i)
6295 pst->dependencies[i] = iter->v.psymtab;
6296
6297 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6298 }
6299
6300 /* Get the list of files included in the current compilation unit,
6301 and build a psymtab for each of them. */
6302 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6303
b4f54984 6304 if (dwarf_read_debug)
0018ea6f
DE
6305 {
6306 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6307
6308 fprintf_unfiltered (gdb_stdlog,
6309 "Psymtab for %s unit @0x%x: %s - %s"
6310 ", %d global, %d static syms\n",
6311 per_cu->is_debug_types ? "type" : "comp",
9c541725 6312 to_underlying (per_cu->sect_off),
0018ea6f
DE
6313 paddress (gdbarch, pst->textlow),
6314 paddress (gdbarch, pst->texthigh),
6315 pst->n_global_syms, pst->n_static_syms);
6316 }
6317}
6318
6319/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6320 Process compilation unit THIS_CU for a psymtab. */
6321
6322static void
6323process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6324 int want_partial_unit,
6325 enum language pretend_language)
0018ea6f
DE
6326{
6327 /* If this compilation unit was already read in, free the
6328 cached copy in order to read it in again. This is
6329 necessary because we skipped some symbols when we first
6330 read in the compilation unit (see load_partial_dies).
6331 This problem could be avoided, but the benefit is unclear. */
6332 if (this_cu->cu != NULL)
6333 free_one_cached_comp_unit (this_cu);
6334
f1902523
JK
6335 if (this_cu->is_debug_types)
6336 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6337 NULL);
6338 else
6339 {
6340 process_psymtab_comp_unit_data info;
6341 info.want_partial_unit = want_partial_unit;
6342 info.pretend_language = pretend_language;
6343 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6344 process_psymtab_comp_unit_reader, &info);
6345 }
0018ea6f
DE
6346
6347 /* Age out any secondary CUs. */
6348 age_cached_comp_units ();
6349}
f4dc4d17
DE
6350
6351/* Reader function for build_type_psymtabs. */
6352
6353static void
6354build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6355 const gdb_byte *info_ptr,
f4dc4d17
DE
6356 struct die_info *type_unit_die,
6357 int has_children,
6358 void *data)
6359{
6360 struct objfile *objfile = dwarf2_per_objfile->objfile;
6361 struct dwarf2_cu *cu = reader->cu;
6362 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6363 struct signatured_type *sig_type;
f4dc4d17
DE
6364 struct type_unit_group *tu_group;
6365 struct attribute *attr;
6366 struct partial_die_info *first_die;
6367 CORE_ADDR lowpc, highpc;
6368 struct partial_symtab *pst;
6369
6370 gdb_assert (data == NULL);
0186c6a7
DE
6371 gdb_assert (per_cu->is_debug_types);
6372 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6373
6374 if (! has_children)
6375 return;
6376
6377 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6378 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6379
0186c6a7 6380 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6381
6382 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6383 cu->list_in_scope = &file_symbols;
6384 pst = create_partial_symtab (per_cu, "");
6385 pst->anonymous = 1;
6386
6387 first_die = load_partial_dies (reader, info_ptr, 1);
6388
6389 lowpc = (CORE_ADDR) -1;
6390 highpc = (CORE_ADDR) 0;
6391 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6392
8763cede 6393 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6394}
6395
73051182
DE
6396/* Struct used to sort TUs by their abbreviation table offset. */
6397
6398struct tu_abbrev_offset
6399{
6400 struct signatured_type *sig_type;
6401 sect_offset abbrev_offset;
6402};
6403
6404/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6405
6406static int
6407sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6408{
9a3c8263
SM
6409 const struct tu_abbrev_offset * const *a
6410 = (const struct tu_abbrev_offset * const*) ap;
6411 const struct tu_abbrev_offset * const *b
6412 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6413 sect_offset aoff = (*a)->abbrev_offset;
6414 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6415
6416 return (aoff > boff) - (aoff < boff);
6417}
6418
6419/* Efficiently read all the type units.
6420 This does the bulk of the work for build_type_psymtabs.
6421
6422 The efficiency is because we sort TUs by the abbrev table they use and
6423 only read each abbrev table once. In one program there are 200K TUs
6424 sharing 8K abbrev tables.
6425
6426 The main purpose of this function is to support building the
6427 dwarf2_per_objfile->type_unit_groups table.
6428 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6429 can collapse the search space by grouping them by stmt_list.
6430 The savings can be significant, in the same program from above the 200K TUs
6431 share 8K stmt_list tables.
6432
6433 FUNC is expected to call get_type_unit_group, which will create the
6434 struct type_unit_group if necessary and add it to
6435 dwarf2_per_objfile->type_unit_groups. */
6436
6437static void
6438build_type_psymtabs_1 (void)
6439{
73051182
DE
6440 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6441 struct cleanup *cleanups;
6442 struct abbrev_table *abbrev_table;
6443 sect_offset abbrev_offset;
6444 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6445 int i;
6446
6447 /* It's up to the caller to not call us multiple times. */
6448 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6449
6450 if (dwarf2_per_objfile->n_type_units == 0)
6451 return;
6452
6453 /* TUs typically share abbrev tables, and there can be way more TUs than
6454 abbrev tables. Sort by abbrev table to reduce the number of times we
6455 read each abbrev table in.
6456 Alternatives are to punt or to maintain a cache of abbrev tables.
6457 This is simpler and efficient enough for now.
6458
6459 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6460 symtab to use). Typically TUs with the same abbrev offset have the same
6461 stmt_list value too so in practice this should work well.
6462
6463 The basic algorithm here is:
6464
6465 sort TUs by abbrev table
6466 for each TU with same abbrev table:
6467 read abbrev table if first user
6468 read TU top level DIE
6469 [IWBN if DWO skeletons had DW_AT_stmt_list]
6470 call FUNC */
6471
b4f54984 6472 if (dwarf_read_debug)
73051182
DE
6473 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6474
6475 /* Sort in a separate table to maintain the order of all_type_units
6476 for .gdb_index: TU indices directly index all_type_units. */
6477 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6478 dwarf2_per_objfile->n_type_units);
6479 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6480 {
6481 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6482
6483 sorted_by_abbrev[i].sig_type = sig_type;
6484 sorted_by_abbrev[i].abbrev_offset =
6485 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6486 sig_type->per_cu.sect_off);
73051182
DE
6487 }
6488 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6489 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6490 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6491
9c541725 6492 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6493 abbrev_table = NULL;
6494 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6495
6496 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6497 {
6498 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6499
6500 /* Switch to the next abbrev table if necessary. */
6501 if (abbrev_table == NULL
9c541725 6502 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6503 {
6504 if (abbrev_table != NULL)
6505 {
6506 abbrev_table_free (abbrev_table);
6507 /* Reset to NULL in case abbrev_table_read_table throws
6508 an error: abbrev_table_free_cleanup will get called. */
6509 abbrev_table = NULL;
6510 }
6511 abbrev_offset = tu->abbrev_offset;
6512 abbrev_table =
6513 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6514 abbrev_offset);
6515 ++tu_stats->nr_uniq_abbrev_tables;
6516 }
6517
6518 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6519 build_type_psymtabs_reader, NULL);
6520 }
6521
73051182 6522 do_cleanups (cleanups);
6aa5f3a6 6523}
73051182 6524
6aa5f3a6
DE
6525/* Print collected type unit statistics. */
6526
6527static void
6528print_tu_stats (void)
6529{
6530 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6531
6532 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6533 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6534 dwarf2_per_objfile->n_type_units);
6535 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6536 tu_stats->nr_uniq_abbrev_tables);
6537 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6538 tu_stats->nr_symtabs);
6539 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6540 tu_stats->nr_symtab_sharers);
6541 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6542 tu_stats->nr_stmt_less_type_units);
6543 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6544 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6545}
6546
f4dc4d17
DE
6547/* Traversal function for build_type_psymtabs. */
6548
6549static int
6550build_type_psymtab_dependencies (void **slot, void *info)
6551{
6552 struct objfile *objfile = dwarf2_per_objfile->objfile;
6553 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6554 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6555 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6556 int len = VEC_length (sig_type_ptr, tu_group->tus);
6557 struct signatured_type *iter;
f4dc4d17
DE
6558 int i;
6559
6560 gdb_assert (len > 0);
0186c6a7 6561 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6562
6563 pst->number_of_dependencies = len;
8d749320
SM
6564 pst->dependencies =
6565 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6566 for (i = 0;
0186c6a7 6567 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6568 ++i)
6569 {
0186c6a7
DE
6570 gdb_assert (iter->per_cu.is_debug_types);
6571 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6572 iter->type_unit_group = tu_group;
f4dc4d17
DE
6573 }
6574
0186c6a7 6575 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6576
6577 return 1;
6578}
6579
6580/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6581 Build partial symbol tables for the .debug_types comp-units. */
6582
6583static void
6584build_type_psymtabs (struct objfile *objfile)
6585{
0e50663e 6586 if (! create_all_type_units (objfile))
348e048f
DE
6587 return;
6588
73051182 6589 build_type_psymtabs_1 ();
6aa5f3a6 6590}
f4dc4d17 6591
6aa5f3a6
DE
6592/* Traversal function for process_skeletonless_type_unit.
6593 Read a TU in a DWO file and build partial symbols for it. */
6594
6595static int
6596process_skeletonless_type_unit (void **slot, void *info)
6597{
6598 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6599 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6600 struct signatured_type find_entry, *entry;
6601
6602 /* If this TU doesn't exist in the global table, add it and read it in. */
6603
6604 if (dwarf2_per_objfile->signatured_types == NULL)
6605 {
6606 dwarf2_per_objfile->signatured_types
6607 = allocate_signatured_type_table (objfile);
6608 }
6609
6610 find_entry.signature = dwo_unit->signature;
6611 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6612 INSERT);
6613 /* If we've already seen this type there's nothing to do. What's happening
6614 is we're doing our own version of comdat-folding here. */
6615 if (*slot != NULL)
6616 return 1;
6617
6618 /* This does the job that create_all_type_units would have done for
6619 this TU. */
6620 entry = add_type_unit (dwo_unit->signature, slot);
6621 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6622 *slot = entry;
6623
6624 /* This does the job that build_type_psymtabs_1 would have done. */
6625 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6626 build_type_psymtabs_reader, NULL);
6627
6628 return 1;
6629}
6630
6631/* Traversal function for process_skeletonless_type_units. */
6632
6633static int
6634process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6635{
6636 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6637
6638 if (dwo_file->tus != NULL)
6639 {
6640 htab_traverse_noresize (dwo_file->tus,
6641 process_skeletonless_type_unit, info);
6642 }
6643
6644 return 1;
6645}
6646
6647/* Scan all TUs of DWO files, verifying we've processed them.
6648 This is needed in case a TU was emitted without its skeleton.
6649 Note: This can't be done until we know what all the DWO files are. */
6650
6651static void
6652process_skeletonless_type_units (struct objfile *objfile)
6653{
6654 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6655 if (get_dwp_file () == NULL
6656 && dwarf2_per_objfile->dwo_files != NULL)
6657 {
6658 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6659 process_dwo_file_for_skeletonless_type_units,
6660 objfile);
6661 }
348e048f
DE
6662}
6663
60606b2c
TT
6664/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6665
6666static void
6667psymtabs_addrmap_cleanup (void *o)
6668{
9a3c8263 6669 struct objfile *objfile = (struct objfile *) o;
ec61707d 6670
60606b2c
TT
6671 objfile->psymtabs_addrmap = NULL;
6672}
6673
95554aad
TT
6674/* Compute the 'user' field for each psymtab in OBJFILE. */
6675
6676static void
6677set_partial_user (struct objfile *objfile)
6678{
6679 int i;
6680
6681 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6682 {
8832e7e3 6683 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6684 struct partial_symtab *pst = per_cu->v.psymtab;
6685 int j;
6686
36586728
TT
6687 if (pst == NULL)
6688 continue;
6689
95554aad
TT
6690 for (j = 0; j < pst->number_of_dependencies; ++j)
6691 {
6692 /* Set the 'user' field only if it is not already set. */
6693 if (pst->dependencies[j]->user == NULL)
6694 pst->dependencies[j]->user = pst;
6695 }
6696 }
6697}
6698
93311388
DE
6699/* Build the partial symbol table by doing a quick pass through the
6700 .debug_info and .debug_abbrev sections. */
72bf9492 6701
93311388 6702static void
c67a9c90 6703dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6704{
60606b2c 6705 struct cleanup *back_to, *addrmap_cleanup;
21b2bd31 6706 int i;
93311388 6707
b4f54984 6708 if (dwarf_read_debug)
45cfd468
DE
6709 {
6710 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6711 objfile_name (objfile));
45cfd468
DE
6712 }
6713
98bfdba5
PA
6714 dwarf2_per_objfile->reading_partial_symbols = 1;
6715
be391dca 6716 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6717
93311388
DE
6718 /* Any cached compilation units will be linked by the per-objfile
6719 read_in_chain. Make sure to free them when we're done. */
6720 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6721
348e048f
DE
6722 build_type_psymtabs (objfile);
6723
93311388 6724 create_all_comp_units (objfile);
c906108c 6725
60606b2c
TT
6726 /* Create a temporary address map on a temporary obstack. We later
6727 copy this to the final obstack. */
8268c778 6728 auto_obstack temp_obstack;
60606b2c
TT
6729 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6730 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6731
21b2bd31 6732 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6733 {
8832e7e3 6734 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6735
b93601f3 6736 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6737 }
ff013f42 6738
6aa5f3a6
DE
6739 /* This has to wait until we read the CUs, we need the list of DWOs. */
6740 process_skeletonless_type_units (objfile);
6741
6742 /* Now that all TUs have been processed we can fill in the dependencies. */
6743 if (dwarf2_per_objfile->type_unit_groups != NULL)
6744 {
6745 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6746 build_type_psymtab_dependencies, NULL);
6747 }
6748
b4f54984 6749 if (dwarf_read_debug)
6aa5f3a6
DE
6750 print_tu_stats ();
6751
95554aad
TT
6752 set_partial_user (objfile);
6753
ff013f42
JK
6754 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6755 &objfile->objfile_obstack);
60606b2c 6756 discard_cleanups (addrmap_cleanup);
ff013f42 6757
ae038cb0 6758 do_cleanups (back_to);
45cfd468 6759
b4f54984 6760 if (dwarf_read_debug)
45cfd468 6761 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6762 objfile_name (objfile));
ae038cb0
DJ
6763}
6764
3019eac3 6765/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6766
6767static void
dee91e82 6768load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6769 const gdb_byte *info_ptr,
dee91e82
DE
6770 struct die_info *comp_unit_die,
6771 int has_children,
6772 void *data)
ae038cb0 6773{
dee91e82 6774 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6775
95554aad 6776 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6777
ae038cb0
DJ
6778 /* Check if comp unit has_children.
6779 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6780 If not, there's no more debug_info for this comp unit. */
d85a05f0 6781 if (has_children)
dee91e82
DE
6782 load_partial_dies (reader, info_ptr, 0);
6783}
98bfdba5 6784
dee91e82
DE
6785/* Load the partial DIEs for a secondary CU into memory.
6786 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6787
dee91e82
DE
6788static void
6789load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6790{
f4dc4d17
DE
6791 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6792 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6793}
6794
ae038cb0 6795static void
36586728
TT
6796read_comp_units_from_section (struct objfile *objfile,
6797 struct dwarf2_section_info *section,
f1902523 6798 struct dwarf2_section_info *abbrev_section,
36586728
TT
6799 unsigned int is_dwz,
6800 int *n_allocated,
6801 int *n_comp_units,
6802 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6803{
d521ce57 6804 const gdb_byte *info_ptr;
a32a8923 6805 bfd *abfd = get_section_bfd_owner (section);
be391dca 6806
b4f54984 6807 if (dwarf_read_debug)
bf6af496 6808 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6809 get_section_name (section),
6810 get_section_file_name (section));
bf6af496 6811
36586728 6812 dwarf2_read_section (objfile, section);
ae038cb0 6813
36586728 6814 info_ptr = section->buffer;
6e70227d 6815
36586728 6816 while (info_ptr < section->buffer + section->size)
ae038cb0 6817 {
ae038cb0 6818 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6819
9c541725 6820 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6821
f1902523
JK
6822 comp_unit_head cu_header;
6823 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6824 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6825
6826 /* Save the compilation unit for later lookup. */
f1902523
JK
6827 if (cu_header.unit_type != DW_UT_type)
6828 {
6829 this_cu = XOBNEW (&objfile->objfile_obstack,
6830 struct dwarf2_per_cu_data);
6831 memset (this_cu, 0, sizeof (*this_cu));
6832 }
6833 else
6834 {
6835 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6836 struct signatured_type);
6837 memset (sig_type, 0, sizeof (*sig_type));
6838 sig_type->signature = cu_header.signature;
6839 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6840 this_cu = &sig_type->per_cu;
6841 }
6842 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6843 this_cu->sect_off = sect_off;
f1902523 6844 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6845 this_cu->is_dwz = is_dwz;
9291a0cd 6846 this_cu->objfile = objfile;
8a0459fd 6847 this_cu->section = section;
ae038cb0 6848
36586728 6849 if (*n_comp_units == *n_allocated)
ae038cb0 6850 {
36586728 6851 *n_allocated *= 2;
224c3ddb
SM
6852 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6853 *all_comp_units, *n_allocated);
ae038cb0 6854 }
36586728
TT
6855 (*all_comp_units)[*n_comp_units] = this_cu;
6856 ++*n_comp_units;
ae038cb0
DJ
6857
6858 info_ptr = info_ptr + this_cu->length;
6859 }
36586728
TT
6860}
6861
6862/* Create a list of all compilation units in OBJFILE.
6863 This is only done for -readnow and building partial symtabs. */
6864
6865static void
6866create_all_comp_units (struct objfile *objfile)
6867{
6868 int n_allocated;
6869 int n_comp_units;
6870 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6871 struct dwz_file *dwz;
36586728
TT
6872
6873 n_comp_units = 0;
6874 n_allocated = 10;
8d749320 6875 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6876
f1902523
JK
6877 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6878 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6879 &n_allocated, &n_comp_units, &all_comp_units);
6880
4db1a1dc
TT
6881 dwz = dwarf2_get_dwz_file ();
6882 if (dwz != NULL)
f1902523 6883 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6884 &n_allocated, &n_comp_units,
6885 &all_comp_units);
ae038cb0 6886
8d749320
SM
6887 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6888 struct dwarf2_per_cu_data *,
6889 n_comp_units);
ae038cb0
DJ
6890 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6891 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6892 xfree (all_comp_units);
6893 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6894}
6895
5734ee8b 6896/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6897 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6898 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6899 DW_AT_ranges). See the comments of add_partial_subprogram on how
6900 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6901
72bf9492
DJ
6902static void
6903scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6904 CORE_ADDR *highpc, int set_addrmap,
6905 struct dwarf2_cu *cu)
c906108c 6906{
72bf9492 6907 struct partial_die_info *pdi;
c906108c 6908
91c24f0a
DC
6909 /* Now, march along the PDI's, descending into ones which have
6910 interesting children but skipping the children of the other ones,
6911 until we reach the end of the compilation unit. */
c906108c 6912
72bf9492 6913 pdi = first_die;
91c24f0a 6914
72bf9492
DJ
6915 while (pdi != NULL)
6916 {
6917 fixup_partial_die (pdi, cu);
c906108c 6918
f55ee35c 6919 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6920 children, so we need to look at them. Ditto for anonymous
6921 enums. */
933c6fe4 6922
72bf9492 6923 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6924 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6925 || pdi->tag == DW_TAG_imported_unit)
c906108c 6926 {
72bf9492 6927 switch (pdi->tag)
c906108c
SS
6928 {
6929 case DW_TAG_subprogram:
cdc07690 6930 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6931 break;
72929c62 6932 case DW_TAG_constant:
c906108c
SS
6933 case DW_TAG_variable:
6934 case DW_TAG_typedef:
91c24f0a 6935 case DW_TAG_union_type:
72bf9492 6936 if (!pdi->is_declaration)
63d06c5c 6937 {
72bf9492 6938 add_partial_symbol (pdi, cu);
63d06c5c
DC
6939 }
6940 break;
c906108c 6941 case DW_TAG_class_type:
680b30c7 6942 case DW_TAG_interface_type:
c906108c 6943 case DW_TAG_structure_type:
72bf9492 6944 if (!pdi->is_declaration)
c906108c 6945 {
72bf9492 6946 add_partial_symbol (pdi, cu);
c906108c 6947 }
e98c9e7c
TT
6948 if (cu->language == language_rust && pdi->has_children)
6949 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6950 set_addrmap, cu);
c906108c 6951 break;
91c24f0a 6952 case DW_TAG_enumeration_type:
72bf9492
DJ
6953 if (!pdi->is_declaration)
6954 add_partial_enumeration (pdi, cu);
c906108c
SS
6955 break;
6956 case DW_TAG_base_type:
a02abb62 6957 case DW_TAG_subrange_type:
c906108c 6958 /* File scope base type definitions are added to the partial
c5aa993b 6959 symbol table. */
72bf9492 6960 add_partial_symbol (pdi, cu);
c906108c 6961 break;
d9fa45fe 6962 case DW_TAG_namespace:
cdc07690 6963 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6964 break;
5d7cb8df 6965 case DW_TAG_module:
cdc07690 6966 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6967 break;
95554aad
TT
6968 case DW_TAG_imported_unit:
6969 {
6970 struct dwarf2_per_cu_data *per_cu;
6971
f4dc4d17
DE
6972 /* For now we don't handle imported units in type units. */
6973 if (cu->per_cu->is_debug_types)
6974 {
6975 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6976 " supported in type units [in module %s]"),
4262abfb 6977 objfile_name (cu->objfile));
f4dc4d17
DE
6978 }
6979
9c541725 6980 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6981 pdi->is_dwz,
95554aad
TT
6982 cu->objfile);
6983
6984 /* Go read the partial unit, if needed. */
6985 if (per_cu->v.psymtab == NULL)
b93601f3 6986 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6987
f4dc4d17 6988 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6989 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6990 }
6991 break;
74921315
KS
6992 case DW_TAG_imported_declaration:
6993 add_partial_symbol (pdi, cu);
6994 break;
c906108c
SS
6995 default:
6996 break;
6997 }
6998 }
6999
72bf9492
DJ
7000 /* If the die has a sibling, skip to the sibling. */
7001
7002 pdi = pdi->die_sibling;
7003 }
7004}
7005
7006/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7007
72bf9492 7008 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7009 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7010 Enumerators are an exception; they use the scope of their parent
7011 enumeration type, i.e. the name of the enumeration type is not
7012 prepended to the enumerator.
91c24f0a 7013
72bf9492
DJ
7014 There are two complexities. One is DW_AT_specification; in this
7015 case "parent" means the parent of the target of the specification,
7016 instead of the direct parent of the DIE. The other is compilers
7017 which do not emit DW_TAG_namespace; in this case we try to guess
7018 the fully qualified name of structure types from their members'
7019 linkage names. This must be done using the DIE's children rather
7020 than the children of any DW_AT_specification target. We only need
7021 to do this for structures at the top level, i.e. if the target of
7022 any DW_AT_specification (if any; otherwise the DIE itself) does not
7023 have a parent. */
7024
7025/* Compute the scope prefix associated with PDI's parent, in
7026 compilation unit CU. The result will be allocated on CU's
7027 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7028 field. NULL is returned if no prefix is necessary. */
15d034d0 7029static const char *
72bf9492
DJ
7030partial_die_parent_scope (struct partial_die_info *pdi,
7031 struct dwarf2_cu *cu)
7032{
15d034d0 7033 const char *grandparent_scope;
72bf9492 7034 struct partial_die_info *parent, *real_pdi;
91c24f0a 7035
72bf9492
DJ
7036 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7037 then this means the parent of the specification DIE. */
7038
7039 real_pdi = pdi;
72bf9492 7040 while (real_pdi->has_specification)
36586728
TT
7041 real_pdi = find_partial_die (real_pdi->spec_offset,
7042 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7043
7044 parent = real_pdi->die_parent;
7045 if (parent == NULL)
7046 return NULL;
7047
7048 if (parent->scope_set)
7049 return parent->scope;
7050
7051 fixup_partial_die (parent, cu);
7052
10b3939b 7053 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7054
acebe513
UW
7055 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7056 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7057 Work around this problem here. */
7058 if (cu->language == language_cplus
6e70227d 7059 && parent->tag == DW_TAG_namespace
acebe513
UW
7060 && strcmp (parent->name, "::") == 0
7061 && grandparent_scope == NULL)
7062 {
7063 parent->scope = NULL;
7064 parent->scope_set = 1;
7065 return NULL;
7066 }
7067
9c6c53f7
SA
7068 if (pdi->tag == DW_TAG_enumerator)
7069 /* Enumerators should not get the name of the enumeration as a prefix. */
7070 parent->scope = grandparent_scope;
7071 else if (parent->tag == DW_TAG_namespace
f55ee35c 7072 || parent->tag == DW_TAG_module
72bf9492
DJ
7073 || parent->tag == DW_TAG_structure_type
7074 || parent->tag == DW_TAG_class_type
680b30c7 7075 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7076 || parent->tag == DW_TAG_union_type
7077 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7078 {
7079 if (grandparent_scope == NULL)
7080 parent->scope = parent->name;
7081 else
3e43a32a
MS
7082 parent->scope = typename_concat (&cu->comp_unit_obstack,
7083 grandparent_scope,
f55ee35c 7084 parent->name, 0, cu);
72bf9492 7085 }
72bf9492
DJ
7086 else
7087 {
7088 /* FIXME drow/2004-04-01: What should we be doing with
7089 function-local names? For partial symbols, we should probably be
7090 ignoring them. */
7091 complaint (&symfile_complaints,
e2e0b3e5 7092 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7093 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7094 parent->scope = grandparent_scope;
c906108c
SS
7095 }
7096
72bf9492
DJ
7097 parent->scope_set = 1;
7098 return parent->scope;
7099}
7100
7101/* Return the fully scoped name associated with PDI, from compilation unit
7102 CU. The result will be allocated with malloc. */
4568ecf9 7103
72bf9492
DJ
7104static char *
7105partial_die_full_name (struct partial_die_info *pdi,
7106 struct dwarf2_cu *cu)
7107{
15d034d0 7108 const char *parent_scope;
72bf9492 7109
98bfdba5
PA
7110 /* If this is a template instantiation, we can not work out the
7111 template arguments from partial DIEs. So, unfortunately, we have
7112 to go through the full DIEs. At least any work we do building
7113 types here will be reused if full symbols are loaded later. */
7114 if (pdi->has_template_arguments)
7115 {
7116 fixup_partial_die (pdi, cu);
7117
7118 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7119 {
7120 struct die_info *die;
7121 struct attribute attr;
7122 struct dwarf2_cu *ref_cu = cu;
7123
b64f50a1 7124 /* DW_FORM_ref_addr is using section offset. */
b4069958 7125 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7126 attr.form = DW_FORM_ref_addr;
9c541725 7127 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7128 die = follow_die_ref (NULL, &attr, &ref_cu);
7129
7130 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7131 }
7132 }
7133
72bf9492
DJ
7134 parent_scope = partial_die_parent_scope (pdi, cu);
7135 if (parent_scope == NULL)
7136 return NULL;
7137 else
f55ee35c 7138 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7139}
7140
7141static void
72bf9492 7142add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7143{
e7c27a73 7144 struct objfile *objfile = cu->objfile;
3e29f34a 7145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7146 CORE_ADDR addr = 0;
15d034d0 7147 const char *actual_name = NULL;
e142c38c 7148 CORE_ADDR baseaddr;
15d034d0 7149 char *built_actual_name;
e142c38c
DJ
7150
7151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7152
15d034d0
TT
7153 built_actual_name = partial_die_full_name (pdi, cu);
7154 if (built_actual_name != NULL)
7155 actual_name = built_actual_name;
63d06c5c 7156
72bf9492
DJ
7157 if (actual_name == NULL)
7158 actual_name = pdi->name;
7159
c906108c
SS
7160 switch (pdi->tag)
7161 {
7162 case DW_TAG_subprogram:
3e29f34a 7163 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7164 if (pdi->is_external || cu->language == language_ada)
c906108c 7165 {
2cfa0c8d
JB
7166 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7167 of the global scope. But in Ada, we want to be able to access
7168 nested procedures globally. So all Ada subprograms are stored
7169 in the global scope. */
f47fb265 7170 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7171 built_actual_name != NULL,
f47fb265
MS
7172 VAR_DOMAIN, LOC_BLOCK,
7173 &objfile->global_psymbols,
1762568f 7174 addr, cu->language, objfile);
c906108c
SS
7175 }
7176 else
7177 {
f47fb265 7178 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7179 built_actual_name != NULL,
f47fb265
MS
7180 VAR_DOMAIN, LOC_BLOCK,
7181 &objfile->static_psymbols,
1762568f 7182 addr, cu->language, objfile);
c906108c 7183 }
0c1b455e
TT
7184
7185 if (pdi->main_subprogram && actual_name != NULL)
7186 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7187 break;
72929c62
JB
7188 case DW_TAG_constant:
7189 {
7190 struct psymbol_allocation_list *list;
7191
7192 if (pdi->is_external)
7193 list = &objfile->global_psymbols;
7194 else
7195 list = &objfile->static_psymbols;
f47fb265 7196 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7197 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7198 list, 0, cu->language, objfile);
72929c62
JB
7199 }
7200 break;
c906108c 7201 case DW_TAG_variable:
95554aad
TT
7202 if (pdi->d.locdesc)
7203 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7204
95554aad 7205 if (pdi->d.locdesc
caac4577
JG
7206 && addr == 0
7207 && !dwarf2_per_objfile->has_section_at_zero)
7208 {
7209 /* A global or static variable may also have been stripped
7210 out by the linker if unused, in which case its address
7211 will be nullified; do not add such variables into partial
7212 symbol table then. */
7213 }
7214 else if (pdi->is_external)
c906108c
SS
7215 {
7216 /* Global Variable.
7217 Don't enter into the minimal symbol tables as there is
7218 a minimal symbol table entry from the ELF symbols already.
7219 Enter into partial symbol table if it has a location
7220 descriptor or a type.
7221 If the location descriptor is missing, new_symbol will create
7222 a LOC_UNRESOLVED symbol, the address of the variable will then
7223 be determined from the minimal symbol table whenever the variable
7224 is referenced.
7225 The address for the partial symbol table entry is not
7226 used by GDB, but it comes in handy for debugging partial symbol
7227 table building. */
7228
95554aad 7229 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7230 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7231 built_actual_name != NULL,
f47fb265
MS
7232 VAR_DOMAIN, LOC_STATIC,
7233 &objfile->global_psymbols,
1762568f 7234 addr + baseaddr,
f47fb265 7235 cu->language, objfile);
c906108c
SS
7236 }
7237 else
7238 {
ff908ebf
AW
7239 int has_loc = pdi->d.locdesc != NULL;
7240
7241 /* Static Variable. Skip symbols whose value we cannot know (those
7242 without location descriptors or constant values). */
7243 if (!has_loc && !pdi->has_const_value)
decbce07 7244 {
15d034d0 7245 xfree (built_actual_name);
decbce07
MS
7246 return;
7247 }
ff908ebf 7248
f47fb265 7249 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7250 built_actual_name != NULL,
f47fb265
MS
7251 VAR_DOMAIN, LOC_STATIC,
7252 &objfile->static_psymbols,
ff908ebf 7253 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7254 cu->language, objfile);
c906108c
SS
7255 }
7256 break;
7257 case DW_TAG_typedef:
7258 case DW_TAG_base_type:
a02abb62 7259 case DW_TAG_subrange_type:
38d518c9 7260 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7261 built_actual_name != NULL,
176620f1 7262 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7263 &objfile->static_psymbols,
1762568f 7264 0, cu->language, objfile);
c906108c 7265 break;
74921315 7266 case DW_TAG_imported_declaration:
72bf9492
DJ
7267 case DW_TAG_namespace:
7268 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7269 built_actual_name != NULL,
72bf9492
DJ
7270 VAR_DOMAIN, LOC_TYPEDEF,
7271 &objfile->global_psymbols,
1762568f 7272 0, cu->language, objfile);
72bf9492 7273 break;
530e8392
KB
7274 case DW_TAG_module:
7275 add_psymbol_to_list (actual_name, strlen (actual_name),
7276 built_actual_name != NULL,
7277 MODULE_DOMAIN, LOC_TYPEDEF,
7278 &objfile->global_psymbols,
1762568f 7279 0, cu->language, objfile);
530e8392 7280 break;
c906108c 7281 case DW_TAG_class_type:
680b30c7 7282 case DW_TAG_interface_type:
c906108c
SS
7283 case DW_TAG_structure_type:
7284 case DW_TAG_union_type:
7285 case DW_TAG_enumeration_type:
fa4028e9
JB
7286 /* Skip external references. The DWARF standard says in the section
7287 about "Structure, Union, and Class Type Entries": "An incomplete
7288 structure, union or class type is represented by a structure,
7289 union or class entry that does not have a byte size attribute
7290 and that has a DW_AT_declaration attribute." */
7291 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7292 {
15d034d0 7293 xfree (built_actual_name);
decbce07
MS
7294 return;
7295 }
fa4028e9 7296
63d06c5c
DC
7297 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7298 static vs. global. */
38d518c9 7299 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7300 built_actual_name != NULL,
176620f1 7301 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7302 cu->language == language_cplus
63d06c5c
DC
7303 ? &objfile->global_psymbols
7304 : &objfile->static_psymbols,
1762568f 7305 0, cu->language, objfile);
c906108c 7306
c906108c
SS
7307 break;
7308 case DW_TAG_enumerator:
38d518c9 7309 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7310 built_actual_name != NULL,
176620f1 7311 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7312 cu->language == language_cplus
f6fe98ef
DJ
7313 ? &objfile->global_psymbols
7314 : &objfile->static_psymbols,
1762568f 7315 0, cu->language, objfile);
c906108c
SS
7316 break;
7317 default:
7318 break;
7319 }
5c4e30ca 7320
15d034d0 7321 xfree (built_actual_name);
c906108c
SS
7322}
7323
5c4e30ca
DC
7324/* Read a partial die corresponding to a namespace; also, add a symbol
7325 corresponding to that namespace to the symbol table. NAMESPACE is
7326 the name of the enclosing namespace. */
91c24f0a 7327
72bf9492
DJ
7328static void
7329add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7330 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7331 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7332{
72bf9492 7333 /* Add a symbol for the namespace. */
e7c27a73 7334
72bf9492 7335 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7336
7337 /* Now scan partial symbols in that namespace. */
7338
91c24f0a 7339 if (pdi->has_children)
cdc07690 7340 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7341}
7342
5d7cb8df
JK
7343/* Read a partial die corresponding to a Fortran module. */
7344
7345static void
7346add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7347 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7348{
530e8392
KB
7349 /* Add a symbol for the namespace. */
7350
7351 add_partial_symbol (pdi, cu);
7352
f55ee35c 7353 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7354
7355 if (pdi->has_children)
cdc07690 7356 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7357}
7358
bc30ff58
JB
7359/* Read a partial die corresponding to a subprogram and create a partial
7360 symbol for that subprogram. When the CU language allows it, this
7361 routine also defines a partial symbol for each nested subprogram
cdc07690 7362 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7363 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7364 and highest PC values found in PDI.
6e70227d 7365
cdc07690
YQ
7366 PDI may also be a lexical block, in which case we simply search
7367 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7368 Again, this is only performed when the CU language allows this
7369 type of definitions. */
7370
7371static void
7372add_partial_subprogram (struct partial_die_info *pdi,
7373 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7374 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7375{
7376 if (pdi->tag == DW_TAG_subprogram)
7377 {
7378 if (pdi->has_pc_info)
7379 {
7380 if (pdi->lowpc < *lowpc)
7381 *lowpc = pdi->lowpc;
7382 if (pdi->highpc > *highpc)
7383 *highpc = pdi->highpc;
cdc07690 7384 if (set_addrmap)
5734ee8b 7385 {
5734ee8b 7386 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7387 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7388 CORE_ADDR baseaddr;
7389 CORE_ADDR highpc;
7390 CORE_ADDR lowpc;
5734ee8b
DJ
7391
7392 baseaddr = ANOFFSET (objfile->section_offsets,
7393 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7394 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7395 pdi->lowpc + baseaddr);
7396 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7397 pdi->highpc + baseaddr);
7398 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7399 cu->per_cu->v.psymtab);
5734ee8b 7400 }
481860b3
GB
7401 }
7402
7403 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7404 {
bc30ff58 7405 if (!pdi->is_declaration)
e8d05480
JB
7406 /* Ignore subprogram DIEs that do not have a name, they are
7407 illegal. Do not emit a complaint at this point, we will
7408 do so when we convert this psymtab into a symtab. */
7409 if (pdi->name)
7410 add_partial_symbol (pdi, cu);
bc30ff58
JB
7411 }
7412 }
6e70227d 7413
bc30ff58
JB
7414 if (! pdi->has_children)
7415 return;
7416
7417 if (cu->language == language_ada)
7418 {
7419 pdi = pdi->die_child;
7420 while (pdi != NULL)
7421 {
7422 fixup_partial_die (pdi, cu);
7423 if (pdi->tag == DW_TAG_subprogram
7424 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7425 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7426 pdi = pdi->die_sibling;
7427 }
7428 }
7429}
7430
91c24f0a
DC
7431/* Read a partial die corresponding to an enumeration type. */
7432
72bf9492
DJ
7433static void
7434add_partial_enumeration (struct partial_die_info *enum_pdi,
7435 struct dwarf2_cu *cu)
91c24f0a 7436{
72bf9492 7437 struct partial_die_info *pdi;
91c24f0a
DC
7438
7439 if (enum_pdi->name != NULL)
72bf9492
DJ
7440 add_partial_symbol (enum_pdi, cu);
7441
7442 pdi = enum_pdi->die_child;
7443 while (pdi)
91c24f0a 7444 {
72bf9492 7445 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7446 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7447 else
72bf9492
DJ
7448 add_partial_symbol (pdi, cu);
7449 pdi = pdi->die_sibling;
91c24f0a 7450 }
91c24f0a
DC
7451}
7452
6caca83c
CC
7453/* Return the initial uleb128 in the die at INFO_PTR. */
7454
7455static unsigned int
d521ce57 7456peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7457{
7458 unsigned int bytes_read;
7459
7460 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7461}
7462
4bb7a0a7
DJ
7463/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7464 Return the corresponding abbrev, or NULL if the number is zero (indicating
7465 an empty DIE). In either case *BYTES_READ will be set to the length of
7466 the initial number. */
7467
7468static struct abbrev_info *
d521ce57 7469peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7470 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7471{
7472 bfd *abfd = cu->objfile->obfd;
7473 unsigned int abbrev_number;
7474 struct abbrev_info *abbrev;
7475
7476 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7477
7478 if (abbrev_number == 0)
7479 return NULL;
7480
433df2d4 7481 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7482 if (!abbrev)
7483 {
422b9917
DE
7484 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7485 " at offset 0x%x [in module %s]"),
7486 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7487 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7488 }
7489
7490 return abbrev;
7491}
7492
93311388
DE
7493/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7494 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7495 DIE. Any children of the skipped DIEs will also be skipped. */
7496
d521ce57
TT
7497static const gdb_byte *
7498skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7499{
dee91e82 7500 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7501 struct abbrev_info *abbrev;
7502 unsigned int bytes_read;
7503
7504 while (1)
7505 {
7506 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7507 if (abbrev == NULL)
7508 return info_ptr + bytes_read;
7509 else
dee91e82 7510 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7511 }
7512}
7513
93311388
DE
7514/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7515 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7516 abbrev corresponding to that skipped uleb128 should be passed in
7517 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7518 children. */
7519
d521ce57
TT
7520static const gdb_byte *
7521skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7522 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7523{
7524 unsigned int bytes_read;
7525 struct attribute attr;
dee91e82
DE
7526 bfd *abfd = reader->abfd;
7527 struct dwarf2_cu *cu = reader->cu;
d521ce57 7528 const gdb_byte *buffer = reader->buffer;
f664829e 7529 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7530 unsigned int form, i;
7531
7532 for (i = 0; i < abbrev->num_attrs; i++)
7533 {
7534 /* The only abbrev we care about is DW_AT_sibling. */
7535 if (abbrev->attrs[i].name == DW_AT_sibling)
7536 {
dee91e82 7537 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7538 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7539 complaint (&symfile_complaints,
7540 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7541 else
b9502d3f 7542 {
9c541725
PA
7543 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7544 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7545
7546 if (sibling_ptr < info_ptr)
7547 complaint (&symfile_complaints,
7548 _("DW_AT_sibling points backwards"));
22869d73
KS
7549 else if (sibling_ptr > reader->buffer_end)
7550 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7551 else
7552 return sibling_ptr;
7553 }
4bb7a0a7
DJ
7554 }
7555
7556 /* If it isn't DW_AT_sibling, skip this attribute. */
7557 form = abbrev->attrs[i].form;
7558 skip_attribute:
7559 switch (form)
7560 {
4bb7a0a7 7561 case DW_FORM_ref_addr:
ae411497
TT
7562 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7563 and later it is offset sized. */
7564 if (cu->header.version == 2)
7565 info_ptr += cu->header.addr_size;
7566 else
7567 info_ptr += cu->header.offset_size;
7568 break;
36586728
TT
7569 case DW_FORM_GNU_ref_alt:
7570 info_ptr += cu->header.offset_size;
7571 break;
ae411497 7572 case DW_FORM_addr:
4bb7a0a7
DJ
7573 info_ptr += cu->header.addr_size;
7574 break;
7575 case DW_FORM_data1:
7576 case DW_FORM_ref1:
7577 case DW_FORM_flag:
7578 info_ptr += 1;
7579 break;
2dc7f7b3 7580 case DW_FORM_flag_present:
43988095 7581 case DW_FORM_implicit_const:
2dc7f7b3 7582 break;
4bb7a0a7
DJ
7583 case DW_FORM_data2:
7584 case DW_FORM_ref2:
7585 info_ptr += 2;
7586 break;
7587 case DW_FORM_data4:
7588 case DW_FORM_ref4:
7589 info_ptr += 4;
7590 break;
7591 case DW_FORM_data8:
7592 case DW_FORM_ref8:
55f1336d 7593 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7594 info_ptr += 8;
7595 break;
0224619f
JK
7596 case DW_FORM_data16:
7597 info_ptr += 16;
7598 break;
4bb7a0a7 7599 case DW_FORM_string:
9b1c24c8 7600 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7601 info_ptr += bytes_read;
7602 break;
2dc7f7b3 7603 case DW_FORM_sec_offset:
4bb7a0a7 7604 case DW_FORM_strp:
36586728 7605 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7606 info_ptr += cu->header.offset_size;
7607 break;
2dc7f7b3 7608 case DW_FORM_exprloc:
4bb7a0a7
DJ
7609 case DW_FORM_block:
7610 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7611 info_ptr += bytes_read;
7612 break;
7613 case DW_FORM_block1:
7614 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7615 break;
7616 case DW_FORM_block2:
7617 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7618 break;
7619 case DW_FORM_block4:
7620 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7621 break;
7622 case DW_FORM_sdata:
7623 case DW_FORM_udata:
7624 case DW_FORM_ref_udata:
3019eac3
DE
7625 case DW_FORM_GNU_addr_index:
7626 case DW_FORM_GNU_str_index:
d521ce57 7627 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7628 break;
7629 case DW_FORM_indirect:
7630 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7631 info_ptr += bytes_read;
7632 /* We need to continue parsing from here, so just go back to
7633 the top. */
7634 goto skip_attribute;
7635
7636 default:
3e43a32a
MS
7637 error (_("Dwarf Error: Cannot handle %s "
7638 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7639 dwarf_form_name (form),
7640 bfd_get_filename (abfd));
7641 }
7642 }
7643
7644 if (abbrev->has_children)
dee91e82 7645 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7646 else
7647 return info_ptr;
7648}
7649
93311388 7650/* Locate ORIG_PDI's sibling.
dee91e82 7651 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7652
d521ce57 7653static const gdb_byte *
dee91e82
DE
7654locate_pdi_sibling (const struct die_reader_specs *reader,
7655 struct partial_die_info *orig_pdi,
d521ce57 7656 const gdb_byte *info_ptr)
91c24f0a
DC
7657{
7658 /* Do we know the sibling already? */
72bf9492 7659
91c24f0a
DC
7660 if (orig_pdi->sibling)
7661 return orig_pdi->sibling;
7662
7663 /* Are there any children to deal with? */
7664
7665 if (!orig_pdi->has_children)
7666 return info_ptr;
7667
4bb7a0a7 7668 /* Skip the children the long way. */
91c24f0a 7669
dee91e82 7670 return skip_children (reader, info_ptr);
91c24f0a
DC
7671}
7672
257e7a09 7673/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7674 not NULL. */
c906108c
SS
7675
7676static void
257e7a09
YQ
7677dwarf2_read_symtab (struct partial_symtab *self,
7678 struct objfile *objfile)
c906108c 7679{
257e7a09 7680 if (self->readin)
c906108c 7681 {
442e4d9c 7682 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7683 self->filename);
442e4d9c
YQ
7684 }
7685 else
7686 {
7687 if (info_verbose)
c906108c 7688 {
442e4d9c 7689 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7690 self->filename);
442e4d9c 7691 gdb_flush (gdb_stdout);
c906108c 7692 }
c906108c 7693
442e4d9c 7694 /* Restore our global data. */
9a3c8263
SM
7695 dwarf2_per_objfile
7696 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7697 dwarf2_objfile_data_key);
10b3939b 7698
442e4d9c
YQ
7699 /* If this psymtab is constructed from a debug-only objfile, the
7700 has_section_at_zero flag will not necessarily be correct. We
7701 can get the correct value for this flag by looking at the data
7702 associated with the (presumably stripped) associated objfile. */
7703 if (objfile->separate_debug_objfile_backlink)
7704 {
7705 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7706 = ((struct dwarf2_per_objfile *)
7707 objfile_data (objfile->separate_debug_objfile_backlink,
7708 dwarf2_objfile_data_key));
9a619af0 7709
442e4d9c
YQ
7710 dwarf2_per_objfile->has_section_at_zero
7711 = dpo_backlink->has_section_at_zero;
7712 }
b2ab525c 7713
442e4d9c 7714 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7715
257e7a09 7716 psymtab_to_symtab_1 (self);
c906108c 7717
442e4d9c
YQ
7718 /* Finish up the debug error message. */
7719 if (info_verbose)
7720 printf_filtered (_("done.\n"));
c906108c 7721 }
95554aad
TT
7722
7723 process_cu_includes ();
c906108c 7724}
9cdd5dbd
DE
7725\f
7726/* Reading in full CUs. */
c906108c 7727
10b3939b
DJ
7728/* Add PER_CU to the queue. */
7729
7730static void
95554aad
TT
7731queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7732 enum language pretend_language)
10b3939b
DJ
7733{
7734 struct dwarf2_queue_item *item;
7735
7736 per_cu->queued = 1;
8d749320 7737 item = XNEW (struct dwarf2_queue_item);
10b3939b 7738 item->per_cu = per_cu;
95554aad 7739 item->pretend_language = pretend_language;
10b3939b
DJ
7740 item->next = NULL;
7741
7742 if (dwarf2_queue == NULL)
7743 dwarf2_queue = item;
7744 else
7745 dwarf2_queue_tail->next = item;
7746
7747 dwarf2_queue_tail = item;
7748}
7749
89e63ee4
DE
7750/* If PER_CU is not yet queued, add it to the queue.
7751 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7752 dependency.
0907af0c 7753 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7754 meaning either PER_CU is already queued or it is already loaded.
7755
7756 N.B. There is an invariant here that if a CU is queued then it is loaded.
7757 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7758
7759static int
89e63ee4 7760maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7761 struct dwarf2_per_cu_data *per_cu,
7762 enum language pretend_language)
7763{
7764 /* We may arrive here during partial symbol reading, if we need full
7765 DIEs to process an unusual case (e.g. template arguments). Do
7766 not queue PER_CU, just tell our caller to load its DIEs. */
7767 if (dwarf2_per_objfile->reading_partial_symbols)
7768 {
7769 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7770 return 1;
7771 return 0;
7772 }
7773
7774 /* Mark the dependence relation so that we don't flush PER_CU
7775 too early. */
89e63ee4
DE
7776 if (dependent_cu != NULL)
7777 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7778
7779 /* If it's already on the queue, we have nothing to do. */
7780 if (per_cu->queued)
7781 return 0;
7782
7783 /* If the compilation unit is already loaded, just mark it as
7784 used. */
7785 if (per_cu->cu != NULL)
7786 {
7787 per_cu->cu->last_used = 0;
7788 return 0;
7789 }
7790
7791 /* Add it to the queue. */
7792 queue_comp_unit (per_cu, pretend_language);
7793
7794 return 1;
7795}
7796
10b3939b
DJ
7797/* Process the queue. */
7798
7799static void
a0f42c21 7800process_queue (void)
10b3939b
DJ
7801{
7802 struct dwarf2_queue_item *item, *next_item;
7803
b4f54984 7804 if (dwarf_read_debug)
45cfd468
DE
7805 {
7806 fprintf_unfiltered (gdb_stdlog,
7807 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7808 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7809 }
7810
03dd20cc
DJ
7811 /* The queue starts out with one item, but following a DIE reference
7812 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7813 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7814 {
cc12ce38
DE
7815 if ((dwarf2_per_objfile->using_index
7816 ? !item->per_cu->v.quick->compunit_symtab
7817 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7818 /* Skip dummy CUs. */
7819 && item->per_cu->cu != NULL)
f4dc4d17
DE
7820 {
7821 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7822 unsigned int debug_print_threshold;
247f5c4f 7823 char buf[100];
f4dc4d17 7824
247f5c4f 7825 if (per_cu->is_debug_types)
f4dc4d17 7826 {
247f5c4f
DE
7827 struct signatured_type *sig_type =
7828 (struct signatured_type *) per_cu;
7829
7830 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7831 hex_string (sig_type->signature),
9c541725 7832 to_underlying (per_cu->sect_off));
73be47f5
DE
7833 /* There can be 100s of TUs.
7834 Only print them in verbose mode. */
7835 debug_print_threshold = 2;
f4dc4d17 7836 }
247f5c4f 7837 else
73be47f5 7838 {
9c541725
PA
7839 sprintf (buf, "CU at offset 0x%x",
7840 to_underlying (per_cu->sect_off));
73be47f5
DE
7841 debug_print_threshold = 1;
7842 }
247f5c4f 7843
b4f54984 7844 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7845 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7846
7847 if (per_cu->is_debug_types)
7848 process_full_type_unit (per_cu, item->pretend_language);
7849 else
7850 process_full_comp_unit (per_cu, item->pretend_language);
7851
b4f54984 7852 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7853 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7854 }
10b3939b
DJ
7855
7856 item->per_cu->queued = 0;
7857 next_item = item->next;
7858 xfree (item);
7859 }
7860
7861 dwarf2_queue_tail = NULL;
45cfd468 7862
b4f54984 7863 if (dwarf_read_debug)
45cfd468
DE
7864 {
7865 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7866 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7867 }
10b3939b
DJ
7868}
7869
7870/* Free all allocated queue entries. This function only releases anything if
7871 an error was thrown; if the queue was processed then it would have been
7872 freed as we went along. */
7873
7874static void
7875dwarf2_release_queue (void *dummy)
7876{
7877 struct dwarf2_queue_item *item, *last;
7878
7879 item = dwarf2_queue;
7880 while (item)
7881 {
7882 /* Anything still marked queued is likely to be in an
7883 inconsistent state, so discard it. */
7884 if (item->per_cu->queued)
7885 {
7886 if (item->per_cu->cu != NULL)
dee91e82 7887 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7888 item->per_cu->queued = 0;
7889 }
7890
7891 last = item;
7892 item = item->next;
7893 xfree (last);
7894 }
7895
7896 dwarf2_queue = dwarf2_queue_tail = NULL;
7897}
7898
7899/* Read in full symbols for PST, and anything it depends on. */
7900
c906108c 7901static void
fba45db2 7902psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7903{
10b3939b 7904 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7905 int i;
7906
95554aad
TT
7907 if (pst->readin)
7908 return;
7909
aaa75496 7910 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7911 if (!pst->dependencies[i]->readin
7912 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7913 {
7914 /* Inform about additional files that need to be read in. */
7915 if (info_verbose)
7916 {
a3f17187 7917 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7918 fputs_filtered (" ", gdb_stdout);
7919 wrap_here ("");
7920 fputs_filtered ("and ", gdb_stdout);
7921 wrap_here ("");
7922 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7923 wrap_here (""); /* Flush output. */
aaa75496
JB
7924 gdb_flush (gdb_stdout);
7925 }
7926 psymtab_to_symtab_1 (pst->dependencies[i]);
7927 }
7928
9a3c8263 7929 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7930
7931 if (per_cu == NULL)
aaa75496
JB
7932 {
7933 /* It's an include file, no symbols to read for it.
7934 Everything is in the parent symtab. */
7935 pst->readin = 1;
7936 return;
7937 }
c906108c 7938
a0f42c21 7939 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7940}
7941
dee91e82
DE
7942/* Trivial hash function for die_info: the hash value of a DIE
7943 is its offset in .debug_info for this objfile. */
10b3939b 7944
dee91e82
DE
7945static hashval_t
7946die_hash (const void *item)
10b3939b 7947{
9a3c8263 7948 const struct die_info *die = (const struct die_info *) item;
6502dd73 7949
9c541725 7950 return to_underlying (die->sect_off);
dee91e82 7951}
63d06c5c 7952
dee91e82
DE
7953/* Trivial comparison function for die_info structures: two DIEs
7954 are equal if they have the same offset. */
98bfdba5 7955
dee91e82
DE
7956static int
7957die_eq (const void *item_lhs, const void *item_rhs)
7958{
9a3c8263
SM
7959 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7960 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7961
9c541725 7962 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7963}
c906108c 7964
dee91e82
DE
7965/* die_reader_func for load_full_comp_unit.
7966 This is identical to read_signatured_type_reader,
7967 but is kept separate for now. */
c906108c 7968
dee91e82
DE
7969static void
7970load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7971 const gdb_byte *info_ptr,
dee91e82
DE
7972 struct die_info *comp_unit_die,
7973 int has_children,
7974 void *data)
7975{
7976 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7977 enum language *language_ptr = (enum language *) data;
6caca83c 7978
dee91e82
DE
7979 gdb_assert (cu->die_hash == NULL);
7980 cu->die_hash =
7981 htab_create_alloc_ex (cu->header.length / 12,
7982 die_hash,
7983 die_eq,
7984 NULL,
7985 &cu->comp_unit_obstack,
7986 hashtab_obstack_allocate,
7987 dummy_obstack_deallocate);
e142c38c 7988
dee91e82
DE
7989 if (has_children)
7990 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7991 &info_ptr, comp_unit_die);
7992 cu->dies = comp_unit_die;
7993 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7994
7995 /* We try not to read any attributes in this function, because not
9cdd5dbd 7996 all CUs needed for references have been loaded yet, and symbol
10b3939b 7997 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7998 or we won't be able to build types correctly.
7999 Similarly, if we do not read the producer, we can not apply
8000 producer-specific interpretation. */
95554aad 8001 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8002}
10b3939b 8003
dee91e82 8004/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8005
dee91e82 8006static void
95554aad
TT
8007load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8008 enum language pretend_language)
dee91e82 8009{
3019eac3 8010 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8011
f4dc4d17
DE
8012 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8013 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8014}
8015
3da10d80
KS
8016/* Add a DIE to the delayed physname list. */
8017
8018static void
8019add_to_method_list (struct type *type, int fnfield_index, int index,
8020 const char *name, struct die_info *die,
8021 struct dwarf2_cu *cu)
8022{
8023 struct delayed_method_info mi;
8024 mi.type = type;
8025 mi.fnfield_index = fnfield_index;
8026 mi.index = index;
8027 mi.name = name;
8028 mi.die = die;
8029 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8030}
8031
8032/* A cleanup for freeing the delayed method list. */
8033
8034static void
8035free_delayed_list (void *ptr)
8036{
8037 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8038 if (cu->method_list != NULL)
8039 {
8040 VEC_free (delayed_method_info, cu->method_list);
8041 cu->method_list = NULL;
8042 }
8043}
8044
3693fdb3
PA
8045/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8046 "const" / "volatile". If so, decrements LEN by the length of the
8047 modifier and return true. Otherwise return false. */
8048
8049template<size_t N>
8050static bool
8051check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8052{
8053 size_t mod_len = sizeof (mod) - 1;
8054 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8055 {
8056 len -= mod_len;
8057 return true;
8058 }
8059 return false;
8060}
8061
3da10d80
KS
8062/* Compute the physnames of any methods on the CU's method list.
8063
8064 The computation of method physnames is delayed in order to avoid the
8065 (bad) condition that one of the method's formal parameters is of an as yet
8066 incomplete type. */
8067
8068static void
8069compute_delayed_physnames (struct dwarf2_cu *cu)
8070{
8071 int i;
8072 struct delayed_method_info *mi;
3693fdb3
PA
8073
8074 /* Only C++ delays computing physnames. */
8075 if (VEC_empty (delayed_method_info, cu->method_list))
8076 return;
8077 gdb_assert (cu->language == language_cplus);
8078
3da10d80
KS
8079 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8080 {
1d06ead6 8081 const char *physname;
3da10d80
KS
8082 struct fn_fieldlist *fn_flp
8083 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8084 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8085 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8086 = physname ? physname : "";
3693fdb3
PA
8087
8088 /* Since there's no tag to indicate whether a method is a
8089 const/volatile overload, extract that information out of the
8090 demangled name. */
8091 if (physname != NULL)
8092 {
8093 size_t len = strlen (physname);
8094
8095 while (1)
8096 {
8097 if (physname[len] == ')') /* shortcut */
8098 break;
8099 else if (check_modifier (physname, len, " const"))
8100 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8101 else if (check_modifier (physname, len, " volatile"))
8102 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8103 else
8104 break;
8105 }
8106 }
3da10d80
KS
8107 }
8108}
8109
a766d390
DE
8110/* Go objects should be embedded in a DW_TAG_module DIE,
8111 and it's not clear if/how imported objects will appear.
8112 To keep Go support simple until that's worked out,
8113 go back through what we've read and create something usable.
8114 We could do this while processing each DIE, and feels kinda cleaner,
8115 but that way is more invasive.
8116 This is to, for example, allow the user to type "p var" or "b main"
8117 without having to specify the package name, and allow lookups
8118 of module.object to work in contexts that use the expression
8119 parser. */
8120
8121static void
8122fixup_go_packaging (struct dwarf2_cu *cu)
8123{
8124 char *package_name = NULL;
8125 struct pending *list;
8126 int i;
8127
8128 for (list = global_symbols; list != NULL; list = list->next)
8129 {
8130 for (i = 0; i < list->nsyms; ++i)
8131 {
8132 struct symbol *sym = list->symbol[i];
8133
8134 if (SYMBOL_LANGUAGE (sym) == language_go
8135 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8136 {
8137 char *this_package_name = go_symbol_package_name (sym);
8138
8139 if (this_package_name == NULL)
8140 continue;
8141 if (package_name == NULL)
8142 package_name = this_package_name;
8143 else
8144 {
8145 if (strcmp (package_name, this_package_name) != 0)
8146 complaint (&symfile_complaints,
8147 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8148 (symbol_symtab (sym) != NULL
8149 ? symtab_to_filename_for_display
8150 (symbol_symtab (sym))
4262abfb 8151 : objfile_name (cu->objfile)),
a766d390
DE
8152 this_package_name, package_name);
8153 xfree (this_package_name);
8154 }
8155 }
8156 }
8157 }
8158
8159 if (package_name != NULL)
8160 {
8161 struct objfile *objfile = cu->objfile;
34a68019 8162 const char *saved_package_name
224c3ddb
SM
8163 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8164 package_name,
8165 strlen (package_name));
19f392bc
UW
8166 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8167 saved_package_name);
a766d390
DE
8168 struct symbol *sym;
8169
8170 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8171
e623cf5d 8172 sym = allocate_symbol (objfile);
f85f34ed 8173 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8174 SYMBOL_SET_NAMES (sym, saved_package_name,
8175 strlen (saved_package_name), 0, objfile);
a766d390
DE
8176 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8177 e.g., "main" finds the "main" module and not C's main(). */
8178 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8179 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8180 SYMBOL_TYPE (sym) = type;
8181
8182 add_symbol_to_list (sym, &global_symbols);
8183
8184 xfree (package_name);
8185 }
8186}
8187
95554aad
TT
8188/* Return the symtab for PER_CU. This works properly regardless of
8189 whether we're using the index or psymtabs. */
8190
43f3e411
DE
8191static struct compunit_symtab *
8192get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8193{
8194 return (dwarf2_per_objfile->using_index
43f3e411
DE
8195 ? per_cu->v.quick->compunit_symtab
8196 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8197}
8198
8199/* A helper function for computing the list of all symbol tables
8200 included by PER_CU. */
8201
8202static void
43f3e411 8203recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8204 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8205 struct dwarf2_per_cu_data *per_cu,
43f3e411 8206 struct compunit_symtab *immediate_parent)
95554aad
TT
8207{
8208 void **slot;
8209 int ix;
43f3e411 8210 struct compunit_symtab *cust;
95554aad
TT
8211 struct dwarf2_per_cu_data *iter;
8212
8213 slot = htab_find_slot (all_children, per_cu, INSERT);
8214 if (*slot != NULL)
8215 {
8216 /* This inclusion and its children have been processed. */
8217 return;
8218 }
8219
8220 *slot = per_cu;
8221 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8222 cust = get_compunit_symtab (per_cu);
8223 if (cust != NULL)
ec94af83
DE
8224 {
8225 /* If this is a type unit only add its symbol table if we haven't
8226 seen it yet (type unit per_cu's can share symtabs). */
8227 if (per_cu->is_debug_types)
8228 {
43f3e411 8229 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8230 if (*slot == NULL)
8231 {
43f3e411
DE
8232 *slot = cust;
8233 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8234 if (cust->user == NULL)
8235 cust->user = immediate_parent;
ec94af83
DE
8236 }
8237 }
8238 else
f9125b6c 8239 {
43f3e411
DE
8240 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8241 if (cust->user == NULL)
8242 cust->user = immediate_parent;
f9125b6c 8243 }
ec94af83 8244 }
95554aad
TT
8245
8246 for (ix = 0;
796a7ff8 8247 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8248 ++ix)
ec94af83
DE
8249 {
8250 recursively_compute_inclusions (result, all_children,
43f3e411 8251 all_type_symtabs, iter, cust);
ec94af83 8252 }
95554aad
TT
8253}
8254
43f3e411 8255/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8256 PER_CU. */
8257
8258static void
43f3e411 8259compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8260{
f4dc4d17
DE
8261 gdb_assert (! per_cu->is_debug_types);
8262
796a7ff8 8263 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8264 {
8265 int ix, len;
ec94af83 8266 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8267 struct compunit_symtab *compunit_symtab_iter;
8268 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8269 htab_t all_children, all_type_symtabs;
43f3e411 8270 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8271
8272 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8273 if (cust == NULL)
95554aad
TT
8274 return;
8275
8276 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8277 NULL, xcalloc, xfree);
ec94af83
DE
8278 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8279 NULL, xcalloc, xfree);
95554aad
TT
8280
8281 for (ix = 0;
796a7ff8 8282 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8283 ix, per_cu_iter);
95554aad 8284 ++ix)
ec94af83
DE
8285 {
8286 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8287 all_type_symtabs, per_cu_iter,
43f3e411 8288 cust);
ec94af83 8289 }
95554aad 8290
ec94af83 8291 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8292 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8293 cust->includes
8d749320
SM
8294 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8295 struct compunit_symtab *, len + 1);
95554aad 8296 for (ix = 0;
43f3e411
DE
8297 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8298 compunit_symtab_iter);
95554aad 8299 ++ix)
43f3e411
DE
8300 cust->includes[ix] = compunit_symtab_iter;
8301 cust->includes[len] = NULL;
95554aad 8302
43f3e411 8303 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8304 htab_delete (all_children);
ec94af83 8305 htab_delete (all_type_symtabs);
95554aad
TT
8306 }
8307}
8308
8309/* Compute the 'includes' field for the symtabs of all the CUs we just
8310 read. */
8311
8312static void
8313process_cu_includes (void)
8314{
8315 int ix;
8316 struct dwarf2_per_cu_data *iter;
8317
8318 for (ix = 0;
8319 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8320 ix, iter);
8321 ++ix)
f4dc4d17
DE
8322 {
8323 if (! iter->is_debug_types)
43f3e411 8324 compute_compunit_symtab_includes (iter);
f4dc4d17 8325 }
95554aad
TT
8326
8327 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8328}
8329
9cdd5dbd 8330/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8331 already been loaded into memory. */
8332
8333static void
95554aad
TT
8334process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8335 enum language pretend_language)
10b3939b 8336{
10b3939b 8337 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8338 struct objfile *objfile = per_cu->objfile;
3e29f34a 8339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8340 CORE_ADDR lowpc, highpc;
43f3e411 8341 struct compunit_symtab *cust;
3da10d80 8342 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8343 CORE_ADDR baseaddr;
4359dff1 8344 struct block *static_block;
3e29f34a 8345 CORE_ADDR addr;
10b3939b
DJ
8346
8347 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8348
10b3939b
DJ
8349 buildsym_init ();
8350 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8351 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8352
8353 cu->list_in_scope = &file_symbols;
c906108c 8354
95554aad
TT
8355 cu->language = pretend_language;
8356 cu->language_defn = language_def (cu->language);
8357
c906108c 8358 /* Do line number decoding in read_file_scope () */
10b3939b 8359 process_die (cu->dies, cu);
c906108c 8360
a766d390
DE
8361 /* For now fudge the Go package. */
8362 if (cu->language == language_go)
8363 fixup_go_packaging (cu);
8364
3da10d80
KS
8365 /* Now that we have processed all the DIEs in the CU, all the types
8366 should be complete, and it should now be safe to compute all of the
8367 physnames. */
8368 compute_delayed_physnames (cu);
8369 do_cleanups (delayed_list_cleanup);
8370
fae299cd
DC
8371 /* Some compilers don't define a DW_AT_high_pc attribute for the
8372 compilation unit. If the DW_AT_high_pc is missing, synthesize
8373 it, by scanning the DIE's below the compilation unit. */
10b3939b 8374 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8375
3e29f34a
MR
8376 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8377 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8378
8379 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8380 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8381 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8382 addrmap to help ensure it has an accurate map of pc values belonging to
8383 this comp unit. */
8384 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8385
43f3e411
DE
8386 cust = end_symtab_from_static_block (static_block,
8387 SECT_OFF_TEXT (objfile), 0);
c906108c 8388
43f3e411 8389 if (cust != NULL)
c906108c 8390 {
df15bd07 8391 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8392
8be455d7
JK
8393 /* Set symtab language to language from DW_AT_language. If the
8394 compilation is from a C file generated by language preprocessors, do
8395 not set the language if it was already deduced by start_subfile. */
43f3e411 8396 if (!(cu->language == language_c
40e3ad0e 8397 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8398 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8399
8400 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8401 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8402 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8403 there were bugs in prologue debug info, fixed later in GCC-4.5
8404 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8405
8406 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8407 needed, it would be wrong due to missing DW_AT_producer there.
8408
8409 Still one can confuse GDB by using non-standard GCC compilation
8410 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8411 */
ab260dad 8412 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8413 cust->locations_valid = 1;
e0d00bc7
JK
8414
8415 if (gcc_4_minor >= 5)
43f3e411 8416 cust->epilogue_unwind_valid = 1;
96408a79 8417
43f3e411 8418 cust->call_site_htab = cu->call_site_htab;
c906108c 8419 }
9291a0cd
TT
8420
8421 if (dwarf2_per_objfile->using_index)
43f3e411 8422 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8423 else
8424 {
8425 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8426 pst->compunit_symtab = cust;
9291a0cd
TT
8427 pst->readin = 1;
8428 }
c906108c 8429
95554aad
TT
8430 /* Push it for inclusion processing later. */
8431 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8432
c906108c 8433 do_cleanups (back_to);
f4dc4d17 8434}
45cfd468 8435
f4dc4d17
DE
8436/* Generate full symbol information for type unit PER_CU, whose DIEs have
8437 already been loaded into memory. */
8438
8439static void
8440process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8441 enum language pretend_language)
8442{
8443 struct dwarf2_cu *cu = per_cu->cu;
8444 struct objfile *objfile = per_cu->objfile;
43f3e411 8445 struct compunit_symtab *cust;
f4dc4d17 8446 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8447 struct signatured_type *sig_type;
8448
8449 gdb_assert (per_cu->is_debug_types);
8450 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8451
8452 buildsym_init ();
8453 back_to = make_cleanup (really_free_pendings, NULL);
8454 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8455
8456 cu->list_in_scope = &file_symbols;
8457
8458 cu->language = pretend_language;
8459 cu->language_defn = language_def (cu->language);
8460
8461 /* The symbol tables are set up in read_type_unit_scope. */
8462 process_die (cu->dies, cu);
8463
8464 /* For now fudge the Go package. */
8465 if (cu->language == language_go)
8466 fixup_go_packaging (cu);
8467
8468 /* Now that we have processed all the DIEs in the CU, all the types
8469 should be complete, and it should now be safe to compute all of the
8470 physnames. */
8471 compute_delayed_physnames (cu);
8472 do_cleanups (delayed_list_cleanup);
8473
8474 /* TUs share symbol tables.
8475 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8476 of it with end_expandable_symtab. Otherwise, complete the addition of
8477 this TU's symbols to the existing symtab. */
43f3e411 8478 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8479 {
43f3e411
DE
8480 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8481 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8482
43f3e411 8483 if (cust != NULL)
f4dc4d17
DE
8484 {
8485 /* Set symtab language to language from DW_AT_language. If the
8486 compilation is from a C file generated by language preprocessors,
8487 do not set the language if it was already deduced by
8488 start_subfile. */
43f3e411
DE
8489 if (!(cu->language == language_c
8490 && COMPUNIT_FILETABS (cust)->language != language_c))
8491 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8492 }
8493 }
8494 else
8495 {
0ab9ce85 8496 augment_type_symtab ();
43f3e411 8497 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8498 }
8499
8500 if (dwarf2_per_objfile->using_index)
43f3e411 8501 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8502 else
8503 {
8504 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8505 pst->compunit_symtab = cust;
f4dc4d17 8506 pst->readin = 1;
45cfd468 8507 }
f4dc4d17
DE
8508
8509 do_cleanups (back_to);
c906108c
SS
8510}
8511
95554aad
TT
8512/* Process an imported unit DIE. */
8513
8514static void
8515process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8516{
8517 struct attribute *attr;
8518
f4dc4d17
DE
8519 /* For now we don't handle imported units in type units. */
8520 if (cu->per_cu->is_debug_types)
8521 {
8522 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8523 " supported in type units [in module %s]"),
4262abfb 8524 objfile_name (cu->objfile));
f4dc4d17
DE
8525 }
8526
95554aad
TT
8527 attr = dwarf2_attr (die, DW_AT_import, cu);
8528 if (attr != NULL)
8529 {
9c541725
PA
8530 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8531 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8532 dwarf2_per_cu_data *per_cu
8533 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8534
69d751e3 8535 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8536 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8537 load_full_comp_unit (per_cu, cu->language);
8538
796a7ff8 8539 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8540 per_cu);
8541 }
8542}
8543
4c8aa72d
PA
8544/* RAII object that represents a process_die scope: i.e.,
8545 starts/finishes processing a DIE. */
8546class process_die_scope
adde2bff 8547{
4c8aa72d
PA
8548public:
8549 process_die_scope (die_info *die, dwarf2_cu *cu)
8550 : m_die (die), m_cu (cu)
8551 {
8552 /* We should only be processing DIEs not already in process. */
8553 gdb_assert (!m_die->in_process);
8554 m_die->in_process = true;
8555 }
8c3cb9fa 8556
4c8aa72d
PA
8557 ~process_die_scope ()
8558 {
8559 m_die->in_process = false;
8560
8561 /* If we're done processing the DIE for the CU that owns the line
8562 header, we don't need the line header anymore. */
8563 if (m_cu->line_header_die_owner == m_die)
8564 {
8565 delete m_cu->line_header;
8566 m_cu->line_header = NULL;
8567 m_cu->line_header_die_owner = NULL;
8568 }
8569 }
8570
8571private:
8572 die_info *m_die;
8573 dwarf2_cu *m_cu;
8574};
adde2bff 8575
c906108c
SS
8576/* Process a die and its children. */
8577
8578static void
e7c27a73 8579process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8580{
4c8aa72d 8581 process_die_scope scope (die, cu);
adde2bff 8582
c906108c
SS
8583 switch (die->tag)
8584 {
8585 case DW_TAG_padding:
8586 break;
8587 case DW_TAG_compile_unit:
95554aad 8588 case DW_TAG_partial_unit:
e7c27a73 8589 read_file_scope (die, cu);
c906108c 8590 break;
348e048f
DE
8591 case DW_TAG_type_unit:
8592 read_type_unit_scope (die, cu);
8593 break;
c906108c 8594 case DW_TAG_subprogram:
c906108c 8595 case DW_TAG_inlined_subroutine:
edb3359d 8596 read_func_scope (die, cu);
c906108c
SS
8597 break;
8598 case DW_TAG_lexical_block:
14898363
L
8599 case DW_TAG_try_block:
8600 case DW_TAG_catch_block:
e7c27a73 8601 read_lexical_block_scope (die, cu);
c906108c 8602 break;
216f72a1 8603 case DW_TAG_call_site:
96408a79
SA
8604 case DW_TAG_GNU_call_site:
8605 read_call_site_scope (die, cu);
8606 break;
c906108c 8607 case DW_TAG_class_type:
680b30c7 8608 case DW_TAG_interface_type:
c906108c
SS
8609 case DW_TAG_structure_type:
8610 case DW_TAG_union_type:
134d01f1 8611 process_structure_scope (die, cu);
c906108c
SS
8612 break;
8613 case DW_TAG_enumeration_type:
134d01f1 8614 process_enumeration_scope (die, cu);
c906108c 8615 break;
134d01f1 8616
f792889a
DJ
8617 /* These dies have a type, but processing them does not create
8618 a symbol or recurse to process the children. Therefore we can
8619 read them on-demand through read_type_die. */
c906108c 8620 case DW_TAG_subroutine_type:
72019c9c 8621 case DW_TAG_set_type:
c906108c 8622 case DW_TAG_array_type:
c906108c 8623 case DW_TAG_pointer_type:
c906108c 8624 case DW_TAG_ptr_to_member_type:
c906108c 8625 case DW_TAG_reference_type:
4297a3f0 8626 case DW_TAG_rvalue_reference_type:
c906108c 8627 case DW_TAG_string_type:
c906108c 8628 break;
134d01f1 8629
c906108c 8630 case DW_TAG_base_type:
a02abb62 8631 case DW_TAG_subrange_type:
cb249c71 8632 case DW_TAG_typedef:
134d01f1
DJ
8633 /* Add a typedef symbol for the type definition, if it has a
8634 DW_AT_name. */
f792889a 8635 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8636 break;
c906108c 8637 case DW_TAG_common_block:
e7c27a73 8638 read_common_block (die, cu);
c906108c
SS
8639 break;
8640 case DW_TAG_common_inclusion:
8641 break;
d9fa45fe 8642 case DW_TAG_namespace:
4d4ec4e5 8643 cu->processing_has_namespace_info = 1;
e7c27a73 8644 read_namespace (die, cu);
d9fa45fe 8645 break;
5d7cb8df 8646 case DW_TAG_module:
4d4ec4e5 8647 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8648 read_module (die, cu);
8649 break;
d9fa45fe 8650 case DW_TAG_imported_declaration:
74921315
KS
8651 cu->processing_has_namespace_info = 1;
8652 if (read_namespace_alias (die, cu))
8653 break;
8654 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8655 case DW_TAG_imported_module:
4d4ec4e5 8656 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8657 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8658 || cu->language != language_fortran))
8659 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8660 dwarf_tag_name (die->tag));
8661 read_import_statement (die, cu);
d9fa45fe 8662 break;
95554aad
TT
8663
8664 case DW_TAG_imported_unit:
8665 process_imported_unit_die (die, cu);
8666 break;
8667
c906108c 8668 default:
e7c27a73 8669 new_symbol (die, NULL, cu);
c906108c
SS
8670 break;
8671 }
8672}
ca69b9e6
DE
8673\f
8674/* DWARF name computation. */
c906108c 8675
94af9270
KS
8676/* A helper function for dwarf2_compute_name which determines whether DIE
8677 needs to have the name of the scope prepended to the name listed in the
8678 die. */
8679
8680static int
8681die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8682{
1c809c68
TT
8683 struct attribute *attr;
8684
94af9270
KS
8685 switch (die->tag)
8686 {
8687 case DW_TAG_namespace:
8688 case DW_TAG_typedef:
8689 case DW_TAG_class_type:
8690 case DW_TAG_interface_type:
8691 case DW_TAG_structure_type:
8692 case DW_TAG_union_type:
8693 case DW_TAG_enumeration_type:
8694 case DW_TAG_enumerator:
8695 case DW_TAG_subprogram:
08a76f8a 8696 case DW_TAG_inlined_subroutine:
94af9270 8697 case DW_TAG_member:
74921315 8698 case DW_TAG_imported_declaration:
94af9270
KS
8699 return 1;
8700
8701 case DW_TAG_variable:
c2b0a229 8702 case DW_TAG_constant:
94af9270
KS
8703 /* We only need to prefix "globally" visible variables. These include
8704 any variable marked with DW_AT_external or any variable that
8705 lives in a namespace. [Variables in anonymous namespaces
8706 require prefixing, but they are not DW_AT_external.] */
8707
8708 if (dwarf2_attr (die, DW_AT_specification, cu))
8709 {
8710 struct dwarf2_cu *spec_cu = cu;
9a619af0 8711
94af9270
KS
8712 return die_needs_namespace (die_specification (die, &spec_cu),
8713 spec_cu);
8714 }
8715
1c809c68 8716 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8717 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8718 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8719 return 0;
8720 /* A variable in a lexical block of some kind does not need a
8721 namespace, even though in C++ such variables may be external
8722 and have a mangled name. */
8723 if (die->parent->tag == DW_TAG_lexical_block
8724 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8725 || die->parent->tag == DW_TAG_catch_block
8726 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8727 return 0;
8728 return 1;
94af9270
KS
8729
8730 default:
8731 return 0;
8732 }
8733}
8734
8735/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8736 compute the physname for the object, which include a method's:
9c37b5ae 8737 - formal parameters (C++),
a766d390 8738 - receiver type (Go),
a766d390
DE
8739
8740 The term "physname" is a bit confusing.
8741 For C++, for example, it is the demangled name.
8742 For Go, for example, it's the mangled name.
94af9270 8743
af6b7be1
JB
8744 For Ada, return the DIE's linkage name rather than the fully qualified
8745 name. PHYSNAME is ignored..
8746
94af9270
KS
8747 The result is allocated on the objfile_obstack and canonicalized. */
8748
8749static const char *
15d034d0
TT
8750dwarf2_compute_name (const char *name,
8751 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8752 int physname)
8753{
bb5ed363
DE
8754 struct objfile *objfile = cu->objfile;
8755
94af9270
KS
8756 if (name == NULL)
8757 name = dwarf2_name (die, cu);
8758
2ee7123e
DE
8759 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8760 but otherwise compute it by typename_concat inside GDB.
8761 FIXME: Actually this is not really true, or at least not always true.
8762 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8763 Fortran names because there is no mangling standard. So new_symbol_full
8764 will set the demangled name to the result of dwarf2_full_name, and it is
8765 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8766 if (cu->language == language_ada
8767 || (cu->language == language_fortran && physname))
8768 {
8769 /* For Ada unit, we prefer the linkage name over the name, as
8770 the former contains the exported name, which the user expects
8771 to be able to reference. Ideally, we want the user to be able
8772 to reference this entity using either natural or linkage name,
8773 but we haven't started looking at this enhancement yet. */
2ee7123e 8774 const char *linkage_name;
f55ee35c 8775
2ee7123e
DE
8776 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8777 if (linkage_name == NULL)
8778 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8779 if (linkage_name != NULL)
8780 return linkage_name;
f55ee35c
JK
8781 }
8782
94af9270
KS
8783 /* These are the only languages we know how to qualify names in. */
8784 if (name != NULL
9c37b5ae 8785 && (cu->language == language_cplus
c44af4eb
TT
8786 || cu->language == language_fortran || cu->language == language_d
8787 || cu->language == language_rust))
94af9270
KS
8788 {
8789 if (die_needs_namespace (die, cu))
8790 {
8791 long length;
0d5cff50 8792 const char *prefix;
34a68019 8793 const char *canonical_name = NULL;
94af9270 8794
d7e74731
PA
8795 string_file buf;
8796
94af9270 8797 prefix = determine_prefix (die, cu);
94af9270
KS
8798 if (*prefix != '\0')
8799 {
f55ee35c
JK
8800 char *prefixed_name = typename_concat (NULL, prefix, name,
8801 physname, cu);
9a619af0 8802
d7e74731 8803 buf.puts (prefixed_name);
94af9270
KS
8804 xfree (prefixed_name);
8805 }
8806 else
d7e74731 8807 buf.puts (name);
94af9270 8808
98bfdba5
PA
8809 /* Template parameters may be specified in the DIE's DW_AT_name, or
8810 as children with DW_TAG_template_type_param or
8811 DW_TAG_value_type_param. If the latter, add them to the name
8812 here. If the name already has template parameters, then
8813 skip this step; some versions of GCC emit both, and
8814 it is more efficient to use the pre-computed name.
8815
8816 Something to keep in mind about this process: it is very
8817 unlikely, or in some cases downright impossible, to produce
8818 something that will match the mangled name of a function.
8819 If the definition of the function has the same debug info,
8820 we should be able to match up with it anyway. But fallbacks
8821 using the minimal symbol, for instance to find a method
8822 implemented in a stripped copy of libstdc++, will not work.
8823 If we do not have debug info for the definition, we will have to
8824 match them up some other way.
8825
8826 When we do name matching there is a related problem with function
8827 templates; two instantiated function templates are allowed to
8828 differ only by their return types, which we do not add here. */
8829
8830 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8831 {
8832 struct attribute *attr;
8833 struct die_info *child;
8834 int first = 1;
8835
8836 die->building_fullname = 1;
8837
8838 for (child = die->child; child != NULL; child = child->sibling)
8839 {
8840 struct type *type;
12df843f 8841 LONGEST value;
d521ce57 8842 const gdb_byte *bytes;
98bfdba5
PA
8843 struct dwarf2_locexpr_baton *baton;
8844 struct value *v;
8845
8846 if (child->tag != DW_TAG_template_type_param
8847 && child->tag != DW_TAG_template_value_param)
8848 continue;
8849
8850 if (first)
8851 {
d7e74731 8852 buf.puts ("<");
98bfdba5
PA
8853 first = 0;
8854 }
8855 else
d7e74731 8856 buf.puts (", ");
98bfdba5
PA
8857
8858 attr = dwarf2_attr (child, DW_AT_type, cu);
8859 if (attr == NULL)
8860 {
8861 complaint (&symfile_complaints,
8862 _("template parameter missing DW_AT_type"));
d7e74731 8863 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8864 continue;
8865 }
8866 type = die_type (child, cu);
8867
8868 if (child->tag == DW_TAG_template_type_param)
8869 {
d7e74731 8870 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8871 continue;
8872 }
8873
8874 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8875 if (attr == NULL)
8876 {
8877 complaint (&symfile_complaints,
3e43a32a
MS
8878 _("template parameter missing "
8879 "DW_AT_const_value"));
d7e74731 8880 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8881 continue;
8882 }
8883
8884 dwarf2_const_value_attr (attr, type, name,
8885 &cu->comp_unit_obstack, cu,
8886 &value, &bytes, &baton);
8887
8888 if (TYPE_NOSIGN (type))
8889 /* GDB prints characters as NUMBER 'CHAR'. If that's
8890 changed, this can use value_print instead. */
d7e74731 8891 c_printchar (value, type, &buf);
98bfdba5
PA
8892 else
8893 {
8894 struct value_print_options opts;
8895
8896 if (baton != NULL)
8897 v = dwarf2_evaluate_loc_desc (type, NULL,
8898 baton->data,
8899 baton->size,
8900 baton->per_cu);
8901 else if (bytes != NULL)
8902 {
8903 v = allocate_value (type);
8904 memcpy (value_contents_writeable (v), bytes,
8905 TYPE_LENGTH (type));
8906 }
8907 else
8908 v = value_from_longest (type, value);
8909
3e43a32a
MS
8910 /* Specify decimal so that we do not depend on
8911 the radix. */
98bfdba5
PA
8912 get_formatted_print_options (&opts, 'd');
8913 opts.raw = 1;
d7e74731 8914 value_print (v, &buf, &opts);
98bfdba5
PA
8915 release_value (v);
8916 value_free (v);
8917 }
8918 }
8919
8920 die->building_fullname = 0;
8921
8922 if (!first)
8923 {
8924 /* Close the argument list, with a space if necessary
8925 (nested templates). */
d7e74731
PA
8926 if (!buf.empty () && buf.string ().back () == '>')
8927 buf.puts (" >");
98bfdba5 8928 else
d7e74731 8929 buf.puts (">");
98bfdba5
PA
8930 }
8931 }
8932
9c37b5ae 8933 /* For C++ methods, append formal parameter type
94af9270 8934 information, if PHYSNAME. */
6e70227d 8935
94af9270 8936 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8937 && cu->language == language_cplus)
94af9270
KS
8938 {
8939 struct type *type = read_type_die (die, cu);
8940
d7e74731 8941 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8942 &type_print_raw_options);
94af9270 8943
9c37b5ae 8944 if (cu->language == language_cplus)
94af9270 8945 {
60430eff
DJ
8946 /* Assume that an artificial first parameter is
8947 "this", but do not crash if it is not. RealView
8948 marks unnamed (and thus unused) parameters as
8949 artificial; there is no way to differentiate
8950 the two cases. */
94af9270
KS
8951 if (TYPE_NFIELDS (type) > 0
8952 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8953 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8954 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8955 0))))
d7e74731 8956 buf.puts (" const");
94af9270
KS
8957 }
8958 }
8959
d7e74731 8960 const std::string &intermediate_name = buf.string ();
94af9270
KS
8961
8962 if (cu->language == language_cplus)
34a68019 8963 canonical_name
322a8516 8964 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8965 &objfile->per_bfd->storage_obstack);
8966
8967 /* If we only computed INTERMEDIATE_NAME, or if
8968 INTERMEDIATE_NAME is already canonical, then we need to
8969 copy it to the appropriate obstack. */
322a8516 8970 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8971 name = ((const char *)
8972 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8973 intermediate_name.c_str (),
8974 intermediate_name.length ()));
34a68019
TT
8975 else
8976 name = canonical_name;
94af9270
KS
8977 }
8978 }
8979
8980 return name;
8981}
8982
0114d602
DJ
8983/* Return the fully qualified name of DIE, based on its DW_AT_name.
8984 If scope qualifiers are appropriate they will be added. The result
34a68019 8985 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8986 not have a name. NAME may either be from a previous call to
8987 dwarf2_name or NULL.
8988
9c37b5ae 8989 The output string will be canonicalized (if C++). */
0114d602
DJ
8990
8991static const char *
15d034d0 8992dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8993{
94af9270
KS
8994 return dwarf2_compute_name (name, die, cu, 0);
8995}
0114d602 8996
94af9270
KS
8997/* Construct a physname for the given DIE in CU. NAME may either be
8998 from a previous call to dwarf2_name or NULL. The result will be
8999 allocated on the objfile_objstack or NULL if the DIE does not have a
9000 name.
0114d602 9001
9c37b5ae 9002 The output string will be canonicalized (if C++). */
0114d602 9003
94af9270 9004static const char *
15d034d0 9005dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9006{
bb5ed363 9007 struct objfile *objfile = cu->objfile;
900e11f9
JK
9008 const char *retval, *mangled = NULL, *canon = NULL;
9009 struct cleanup *back_to;
9010 int need_copy = 1;
9011
9012 /* In this case dwarf2_compute_name is just a shortcut not building anything
9013 on its own. */
9014 if (!die_needs_namespace (die, cu))
9015 return dwarf2_compute_name (name, die, cu, 1);
9016
9017 back_to = make_cleanup (null_cleanup, NULL);
9018
7d45c7c3
KB
9019 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9020 if (mangled == NULL)
9021 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 9022
e98c9e7c
TT
9023 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9024 See https://github.com/rust-lang/rust/issues/32925. */
9025 if (cu->language == language_rust && mangled != NULL
9026 && strchr (mangled, '{') != NULL)
9027 mangled = NULL;
9028
900e11f9
JK
9029 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9030 has computed. */
7d45c7c3 9031 if (mangled != NULL)
900e11f9
JK
9032 {
9033 char *demangled;
9034
900e11f9
JK
9035 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9036 type. It is easier for GDB users to search for such functions as
9037 `name(params)' than `long name(params)'. In such case the minimal
9038 symbol names do not match the full symbol names but for template
9039 functions there is never a need to look up their definition from their
9040 declaration so the only disadvantage remains the minimal symbol
9041 variant `long name(params)' does not have the proper inferior type.
9042 */
9043
a766d390
DE
9044 if (cu->language == language_go)
9045 {
9046 /* This is a lie, but we already lie to the caller new_symbol_full.
9047 new_symbol_full assumes we return the mangled name.
9048 This just undoes that lie until things are cleaned up. */
9049 demangled = NULL;
9050 }
9051 else
9052 {
8de20a37 9053 demangled = gdb_demangle (mangled,
9c37b5ae 9054 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 9055 }
900e11f9
JK
9056 if (demangled)
9057 {
9058 make_cleanup (xfree, demangled);
9059 canon = demangled;
9060 }
9061 else
9062 {
9063 canon = mangled;
9064 need_copy = 0;
9065 }
9066 }
9067
9068 if (canon == NULL || check_physname)
9069 {
9070 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9071
9072 if (canon != NULL && strcmp (physname, canon) != 0)
9073 {
9074 /* It may not mean a bug in GDB. The compiler could also
9075 compute DW_AT_linkage_name incorrectly. But in such case
9076 GDB would need to be bug-to-bug compatible. */
9077
9078 complaint (&symfile_complaints,
9079 _("Computed physname <%s> does not match demangled <%s> "
9080 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9081 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9082 objfile_name (objfile));
900e11f9
JK
9083
9084 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9085 is available here - over computed PHYSNAME. It is safer
9086 against both buggy GDB and buggy compilers. */
9087
9088 retval = canon;
9089 }
9090 else
9091 {
9092 retval = physname;
9093 need_copy = 0;
9094 }
9095 }
9096 else
9097 retval = canon;
9098
9099 if (need_copy)
224c3ddb
SM
9100 retval = ((const char *)
9101 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9102 retval, strlen (retval)));
900e11f9
JK
9103
9104 do_cleanups (back_to);
9105 return retval;
0114d602
DJ
9106}
9107
74921315
KS
9108/* Inspect DIE in CU for a namespace alias. If one exists, record
9109 a new symbol for it.
9110
9111 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9112
9113static int
9114read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9115{
9116 struct attribute *attr;
9117
9118 /* If the die does not have a name, this is not a namespace
9119 alias. */
9120 attr = dwarf2_attr (die, DW_AT_name, cu);
9121 if (attr != NULL)
9122 {
9123 int num;
9124 struct die_info *d = die;
9125 struct dwarf2_cu *imported_cu = cu;
9126
9127 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9128 keep inspecting DIEs until we hit the underlying import. */
9129#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9130 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9131 {
9132 attr = dwarf2_attr (d, DW_AT_import, cu);
9133 if (attr == NULL)
9134 break;
9135
9136 d = follow_die_ref (d, attr, &imported_cu);
9137 if (d->tag != DW_TAG_imported_declaration)
9138 break;
9139 }
9140
9141 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9142 {
9143 complaint (&symfile_complaints,
9144 _("DIE at 0x%x has too many recursively imported "
9c541725 9145 "declarations"), to_underlying (d->sect_off));
74921315
KS
9146 return 0;
9147 }
9148
9149 if (attr != NULL)
9150 {
9151 struct type *type;
9c541725 9152 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9153
9c541725 9154 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9155 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9156 {
9157 /* This declaration is a global namespace alias. Add
9158 a symbol for it whose type is the aliased namespace. */
9159 new_symbol (die, type, cu);
9160 return 1;
9161 }
9162 }
9163 }
9164
9165 return 0;
9166}
9167
22cee43f
PMR
9168/* Return the using directives repository (global or local?) to use in the
9169 current context for LANGUAGE.
9170
9171 For Ada, imported declarations can materialize renamings, which *may* be
9172 global. However it is impossible (for now?) in DWARF to distinguish
9173 "external" imported declarations and "static" ones. As all imported
9174 declarations seem to be static in all other languages, make them all CU-wide
9175 global only in Ada. */
9176
9177static struct using_direct **
9178using_directives (enum language language)
9179{
9180 if (language == language_ada && context_stack_depth == 0)
9181 return &global_using_directives;
9182 else
9183 return &local_using_directives;
9184}
9185
27aa8d6a
SW
9186/* Read the import statement specified by the given die and record it. */
9187
9188static void
9189read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9190{
bb5ed363 9191 struct objfile *objfile = cu->objfile;
27aa8d6a 9192 struct attribute *import_attr;
32019081 9193 struct die_info *imported_die, *child_die;
de4affc9 9194 struct dwarf2_cu *imported_cu;
27aa8d6a 9195 const char *imported_name;
794684b6 9196 const char *imported_name_prefix;
13387711
SW
9197 const char *canonical_name;
9198 const char *import_alias;
9199 const char *imported_declaration = NULL;
794684b6 9200 const char *import_prefix;
32019081
JK
9201 VEC (const_char_ptr) *excludes = NULL;
9202 struct cleanup *cleanups;
13387711 9203
27aa8d6a
SW
9204 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9205 if (import_attr == NULL)
9206 {
9207 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9208 dwarf_tag_name (die->tag));
9209 return;
9210 }
9211
de4affc9
CC
9212 imported_cu = cu;
9213 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9214 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9215 if (imported_name == NULL)
9216 {
9217 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9218
9219 The import in the following code:
9220 namespace A
9221 {
9222 typedef int B;
9223 }
9224
9225 int main ()
9226 {
9227 using A::B;
9228 B b;
9229 return b;
9230 }
9231
9232 ...
9233 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9234 <52> DW_AT_decl_file : 1
9235 <53> DW_AT_decl_line : 6
9236 <54> DW_AT_import : <0x75>
9237 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9238 <59> DW_AT_name : B
9239 <5b> DW_AT_decl_file : 1
9240 <5c> DW_AT_decl_line : 2
9241 <5d> DW_AT_type : <0x6e>
9242 ...
9243 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9244 <76> DW_AT_byte_size : 4
9245 <77> DW_AT_encoding : 5 (signed)
9246
9247 imports the wrong die ( 0x75 instead of 0x58 ).
9248 This case will be ignored until the gcc bug is fixed. */
9249 return;
9250 }
9251
82856980
SW
9252 /* Figure out the local name after import. */
9253 import_alias = dwarf2_name (die, cu);
27aa8d6a 9254
794684b6
SW
9255 /* Figure out where the statement is being imported to. */
9256 import_prefix = determine_prefix (die, cu);
9257
9258 /* Figure out what the scope of the imported die is and prepend it
9259 to the name of the imported die. */
de4affc9 9260 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9261
f55ee35c
JK
9262 if (imported_die->tag != DW_TAG_namespace
9263 && imported_die->tag != DW_TAG_module)
794684b6 9264 {
13387711
SW
9265 imported_declaration = imported_name;
9266 canonical_name = imported_name_prefix;
794684b6 9267 }
13387711 9268 else if (strlen (imported_name_prefix) > 0)
12aaed36 9269 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9270 imported_name_prefix,
9271 (cu->language == language_d ? "." : "::"),
9272 imported_name, (char *) NULL);
13387711
SW
9273 else
9274 canonical_name = imported_name;
794684b6 9275
32019081
JK
9276 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9277
9278 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9279 for (child_die = die->child; child_die && child_die->tag;
9280 child_die = sibling_die (child_die))
9281 {
9282 /* DWARF-4: A Fortran use statement with a “rename list” may be
9283 represented by an imported module entry with an import attribute
9284 referring to the module and owned entries corresponding to those
9285 entities that are renamed as part of being imported. */
9286
9287 if (child_die->tag != DW_TAG_imported_declaration)
9288 {
9289 complaint (&symfile_complaints,
9290 _("child DW_TAG_imported_declaration expected "
9291 "- DIE at 0x%x [in module %s]"),
9c541725 9292 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9293 continue;
9294 }
9295
9296 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9297 if (import_attr == NULL)
9298 {
9299 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9300 dwarf_tag_name (child_die->tag));
9301 continue;
9302 }
9303
9304 imported_cu = cu;
9305 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9306 &imported_cu);
9307 imported_name = dwarf2_name (imported_die, imported_cu);
9308 if (imported_name == NULL)
9309 {
9310 complaint (&symfile_complaints,
9311 _("child DW_TAG_imported_declaration has unknown "
9312 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9313 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9314 continue;
9315 }
9316
9317 VEC_safe_push (const_char_ptr, excludes, imported_name);
9318
9319 process_die (child_die, cu);
9320 }
9321
22cee43f
PMR
9322 add_using_directive (using_directives (cu->language),
9323 import_prefix,
9324 canonical_name,
9325 import_alias,
9326 imported_declaration,
9327 excludes,
9328 0,
9329 &objfile->objfile_obstack);
32019081
JK
9330
9331 do_cleanups (cleanups);
27aa8d6a
SW
9332}
9333
1b80a9fa
JK
9334/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9335 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9336 this, it was first present in GCC release 4.3.0. */
9337
9338static int
9339producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9340{
9341 if (!cu->checked_producer)
9342 check_producer (cu);
9343
9344 return cu->producer_is_gcc_lt_4_3;
9345}
9346
d721ba37
PA
9347static file_and_directory
9348find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9349{
d721ba37
PA
9350 file_and_directory res;
9351
9291a0cd
TT
9352 /* Find the filename. Do not use dwarf2_name here, since the filename
9353 is not a source language identifier. */
d721ba37
PA
9354 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9355 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9356
d721ba37
PA
9357 if (res.comp_dir == NULL
9358 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9359 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9360 {
d721ba37
PA
9361 res.comp_dir_storage = ldirname (res.name);
9362 if (!res.comp_dir_storage.empty ())
9363 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9364 }
d721ba37 9365 if (res.comp_dir != NULL)
9291a0cd
TT
9366 {
9367 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9368 directory, get rid of it. */
d721ba37 9369 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9370
d721ba37
PA
9371 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9372 res.comp_dir = cp + 1;
9291a0cd
TT
9373 }
9374
d721ba37
PA
9375 if (res.name == NULL)
9376 res.name = "<unknown>";
9377
9378 return res;
9291a0cd
TT
9379}
9380
f4dc4d17
DE
9381/* Handle DW_AT_stmt_list for a compilation unit.
9382 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9383 COMP_DIR is the compilation directory. LOWPC is passed to
9384 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9385
9386static void
9387handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9388 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9389{
527f3840 9390 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9391 struct attribute *attr;
527f3840
JK
9392 struct line_header line_header_local;
9393 hashval_t line_header_local_hash;
9394 unsigned u;
9395 void **slot;
9396 int decode_mapping;
2ab95328 9397
f4dc4d17
DE
9398 gdb_assert (! cu->per_cu->is_debug_types);
9399
2ab95328 9400 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9401 if (attr == NULL)
9402 return;
9403
9c541725 9404 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9405
9406 /* The line header hash table is only created if needed (it exists to
9407 prevent redundant reading of the line table for partial_units).
9408 If we're given a partial_unit, we'll need it. If we're given a
9409 compile_unit, then use the line header hash table if it's already
9410 created, but don't create one just yet. */
9411
9412 if (dwarf2_per_objfile->line_header_hash == NULL
9413 && die->tag == DW_TAG_partial_unit)
2ab95328 9414 {
527f3840
JK
9415 dwarf2_per_objfile->line_header_hash
9416 = htab_create_alloc_ex (127, line_header_hash_voidp,
9417 line_header_eq_voidp,
9418 free_line_header_voidp,
9419 &objfile->objfile_obstack,
9420 hashtab_obstack_allocate,
9421 dummy_obstack_deallocate);
9422 }
2ab95328 9423
9c541725 9424 line_header_local.sect_off = line_offset;
527f3840
JK
9425 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9426 line_header_local_hash = line_header_hash (&line_header_local);
9427 if (dwarf2_per_objfile->line_header_hash != NULL)
9428 {
9429 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9430 &line_header_local,
9431 line_header_local_hash, NO_INSERT);
9432
9433 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9434 is not present in *SLOT (since if there is something in *SLOT then
9435 it will be for a partial_unit). */
9436 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9437 {
527f3840 9438 gdb_assert (*slot != NULL);
9a3c8263 9439 cu->line_header = (struct line_header *) *slot;
527f3840 9440 return;
dee91e82 9441 }
2ab95328 9442 }
527f3840
JK
9443
9444 /* dwarf_decode_line_header does not yet provide sufficient information.
9445 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9446 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9447 if (lh == NULL)
527f3840 9448 return;
4c8aa72d
PA
9449
9450 cu->line_header = lh.release ();
9451 cu->line_header_die_owner = die;
527f3840
JK
9452
9453 if (dwarf2_per_objfile->line_header_hash == NULL)
9454 slot = NULL;
9455 else
9456 {
9457 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9458 &line_header_local,
9459 line_header_local_hash, INSERT);
9460 gdb_assert (slot != NULL);
9461 }
9462 if (slot != NULL && *slot == NULL)
9463 {
9464 /* This newly decoded line number information unit will be owned
9465 by line_header_hash hash table. */
9466 *slot = cu->line_header;
4c8aa72d 9467 cu->line_header_die_owner = NULL;
527f3840
JK
9468 }
9469 else
9470 {
9471 /* We cannot free any current entry in (*slot) as that struct line_header
9472 may be already used by multiple CUs. Create only temporary decoded
9473 line_header for this CU - it may happen at most once for each line
9474 number information unit. And if we're not using line_header_hash
9475 then this is what we want as well. */
9476 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9477 }
9478 decode_mapping = (die->tag != DW_TAG_partial_unit);
9479 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9480 decode_mapping);
fff8551c 9481
2ab95328
TT
9482}
9483
95554aad 9484/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9485
c906108c 9486static void
e7c27a73 9487read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9488{
dee91e82 9489 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9491 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9492 CORE_ADDR highpc = ((CORE_ADDR) 0);
9493 struct attribute *attr;
c906108c 9494 struct die_info *child_die;
e142c38c 9495 CORE_ADDR baseaddr;
6e70227d 9496
e142c38c 9497 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9498
fae299cd 9499 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9500
9501 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9502 from finish_block. */
2acceee2 9503 if (lowpc == ((CORE_ADDR) -1))
c906108c 9504 lowpc = highpc;
3e29f34a 9505 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9506
d721ba37 9507 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9508
95554aad 9509 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9510
f4b8a18d
KW
9511 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9512 standardised yet. As a workaround for the language detection we fall
9513 back to the DW_AT_producer string. */
9514 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9515 cu->language = language_opencl;
9516
3019eac3
DE
9517 /* Similar hack for Go. */
9518 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9519 set_cu_language (DW_LANG_Go, cu);
9520
d721ba37 9521 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9522
9523 /* Decode line number information if present. We do this before
9524 processing child DIEs, so that the line header table is available
9525 for DW_AT_decl_file. */
d721ba37 9526 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9527
9528 /* Process all dies in compilation unit. */
9529 if (die->child != NULL)
9530 {
9531 child_die = die->child;
9532 while (child_die && child_die->tag)
9533 {
9534 process_die (child_die, cu);
9535 child_die = sibling_die (child_die);
9536 }
9537 }
9538
9539 /* Decode macro information, if present. Dwarf 2 macro information
9540 refers to information in the line number info statement program
9541 header, so we can only read it if we've read the header
9542 successfully. */
0af92d60
JK
9543 attr = dwarf2_attr (die, DW_AT_macros, cu);
9544 if (attr == NULL)
9545 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9546 if (attr && cu->line_header)
9547 {
9548 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9549 complaint (&symfile_complaints,
0af92d60 9550 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9551
43f3e411 9552 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9553 }
9554 else
9555 {
9556 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9557 if (attr && cu->line_header)
9558 {
9559 unsigned int macro_offset = DW_UNSND (attr);
9560
43f3e411 9561 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9562 }
9563 }
3019eac3
DE
9564}
9565
f4dc4d17
DE
9566/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9567 Create the set of symtabs used by this TU, or if this TU is sharing
9568 symtabs with another TU and the symtabs have already been created
9569 then restore those symtabs in the line header.
9570 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9571
9572static void
f4dc4d17 9573setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9574{
f4dc4d17
DE
9575 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9576 struct type_unit_group *tu_group;
9577 int first_time;
3019eac3 9578 struct attribute *attr;
9c541725 9579 unsigned int i;
0186c6a7 9580 struct signatured_type *sig_type;
3019eac3 9581
f4dc4d17 9582 gdb_assert (per_cu->is_debug_types);
0186c6a7 9583 sig_type = (struct signatured_type *) per_cu;
3019eac3 9584
f4dc4d17 9585 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9586
f4dc4d17 9587 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9588 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9589 if (sig_type->type_unit_group == NULL)
9590 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9591 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9592
9593 /* If we've already processed this stmt_list there's no real need to
9594 do it again, we could fake it and just recreate the part we need
9595 (file name,index -> symtab mapping). If data shows this optimization
9596 is useful we can do it then. */
43f3e411 9597 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9598
9599 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9600 debug info. */
fff8551c 9601 line_header_up lh;
f4dc4d17 9602 if (attr != NULL)
3019eac3 9603 {
9c541725 9604 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9605 lh = dwarf_decode_line_header (line_offset, cu);
9606 }
9607 if (lh == NULL)
9608 {
9609 if (first_time)
9610 dwarf2_start_symtab (cu, "", NULL, 0);
9611 else
9612 {
9613 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9614 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9615 }
f4dc4d17 9616 return;
3019eac3
DE
9617 }
9618
4c8aa72d
PA
9619 cu->line_header = lh.release ();
9620 cu->line_header_die_owner = die;
3019eac3 9621
f4dc4d17
DE
9622 if (first_time)
9623 {
43f3e411 9624 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9625
1fd60fc0
DE
9626 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9627 still initializing it, and our caller (a few levels up)
9628 process_full_type_unit still needs to know if this is the first
9629 time. */
9630
4c8aa72d
PA
9631 tu_group->num_symtabs = cu->line_header->file_names.size ();
9632 tu_group->symtabs = XNEWVEC (struct symtab *,
9633 cu->line_header->file_names.size ());
3019eac3 9634
4c8aa72d 9635 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9636 {
4c8aa72d 9637 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9638
4c8aa72d 9639 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9640
f4dc4d17
DE
9641 if (current_subfile->symtab == NULL)
9642 {
4c8aa72d
PA
9643 /* NOTE: start_subfile will recognize when it's been
9644 passed a file it has already seen. So we can't
9645 assume there's a simple mapping from
9646 cu->line_header->file_names to subfiles, plus
9647 cu->line_header->file_names may contain dups. */
43f3e411
DE
9648 current_subfile->symtab
9649 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9650 }
9651
8c43009f
PA
9652 fe.symtab = current_subfile->symtab;
9653 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9654 }
9655 }
9656 else
3019eac3 9657 {
0ab9ce85 9658 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9659
4c8aa72d 9660 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9661 {
4c8aa72d 9662 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 9663
4c8aa72d 9664 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9665 }
3019eac3
DE
9666 }
9667
f4dc4d17
DE
9668 /* The main symtab is allocated last. Type units don't have DW_AT_name
9669 so they don't have a "real" (so to speak) symtab anyway.
9670 There is later code that will assign the main symtab to all symbols
9671 that don't have one. We need to handle the case of a symbol with a
9672 missing symtab (DW_AT_decl_file) anyway. */
9673}
3019eac3 9674
f4dc4d17
DE
9675/* Process DW_TAG_type_unit.
9676 For TUs we want to skip the first top level sibling if it's not the
9677 actual type being defined by this TU. In this case the first top
9678 level sibling is there to provide context only. */
3019eac3 9679
f4dc4d17
DE
9680static void
9681read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9682{
9683 struct die_info *child_die;
3019eac3 9684
f4dc4d17
DE
9685 prepare_one_comp_unit (cu, die, language_minimal);
9686
9687 /* Initialize (or reinitialize) the machinery for building symtabs.
9688 We do this before processing child DIEs, so that the line header table
9689 is available for DW_AT_decl_file. */
9690 setup_type_unit_groups (die, cu);
9691
9692 if (die->child != NULL)
9693 {
9694 child_die = die->child;
9695 while (child_die && child_die->tag)
9696 {
9697 process_die (child_die, cu);
9698 child_die = sibling_die (child_die);
9699 }
9700 }
3019eac3
DE
9701}
9702\f
80626a55
DE
9703/* DWO/DWP files.
9704
9705 http://gcc.gnu.org/wiki/DebugFission
9706 http://gcc.gnu.org/wiki/DebugFissionDWP
9707
9708 To simplify handling of both DWO files ("object" files with the DWARF info)
9709 and DWP files (a file with the DWOs packaged up into one file), we treat
9710 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9711
9712static hashval_t
9713hash_dwo_file (const void *item)
9714{
9a3c8263 9715 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9716 hashval_t hash;
3019eac3 9717
a2ce51a0
DE
9718 hash = htab_hash_string (dwo_file->dwo_name);
9719 if (dwo_file->comp_dir != NULL)
9720 hash += htab_hash_string (dwo_file->comp_dir);
9721 return hash;
3019eac3
DE
9722}
9723
9724static int
9725eq_dwo_file (const void *item_lhs, const void *item_rhs)
9726{
9a3c8263
SM
9727 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9728 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9729
a2ce51a0
DE
9730 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9731 return 0;
9732 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9733 return lhs->comp_dir == rhs->comp_dir;
9734 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9735}
9736
9737/* Allocate a hash table for DWO files. */
9738
9739static htab_t
9740allocate_dwo_file_hash_table (void)
9741{
9742 struct objfile *objfile = dwarf2_per_objfile->objfile;
9743
9744 return htab_create_alloc_ex (41,
9745 hash_dwo_file,
9746 eq_dwo_file,
9747 NULL,
9748 &objfile->objfile_obstack,
9749 hashtab_obstack_allocate,
9750 dummy_obstack_deallocate);
9751}
9752
80626a55
DE
9753/* Lookup DWO file DWO_NAME. */
9754
9755static void **
0ac5b59e 9756lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9757{
9758 struct dwo_file find_entry;
9759 void **slot;
9760
9761 if (dwarf2_per_objfile->dwo_files == NULL)
9762 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9763
9764 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9765 find_entry.dwo_name = dwo_name;
9766 find_entry.comp_dir = comp_dir;
80626a55
DE
9767 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9768
9769 return slot;
9770}
9771
3019eac3
DE
9772static hashval_t
9773hash_dwo_unit (const void *item)
9774{
9a3c8263 9775 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9776
9777 /* This drops the top 32 bits of the id, but is ok for a hash. */
9778 return dwo_unit->signature;
9779}
9780
9781static int
9782eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9783{
9a3c8263
SM
9784 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9785 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9786
9787 /* The signature is assumed to be unique within the DWO file.
9788 So while object file CU dwo_id's always have the value zero,
9789 that's OK, assuming each object file DWO file has only one CU,
9790 and that's the rule for now. */
9791 return lhs->signature == rhs->signature;
9792}
9793
9794/* Allocate a hash table for DWO CUs,TUs.
9795 There is one of these tables for each of CUs,TUs for each DWO file. */
9796
9797static htab_t
9798allocate_dwo_unit_table (struct objfile *objfile)
9799{
9800 /* Start out with a pretty small number.
9801 Generally DWO files contain only one CU and maybe some TUs. */
9802 return htab_create_alloc_ex (3,
9803 hash_dwo_unit,
9804 eq_dwo_unit,
9805 NULL,
9806 &objfile->objfile_obstack,
9807 hashtab_obstack_allocate,
9808 dummy_obstack_deallocate);
9809}
9810
80626a55 9811/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9812
19c3d4c9 9813struct create_dwo_cu_data
3019eac3
DE
9814{
9815 struct dwo_file *dwo_file;
19c3d4c9 9816 struct dwo_unit dwo_unit;
3019eac3
DE
9817};
9818
19c3d4c9 9819/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9820
9821static void
19c3d4c9
DE
9822create_dwo_cu_reader (const struct die_reader_specs *reader,
9823 const gdb_byte *info_ptr,
9824 struct die_info *comp_unit_die,
9825 int has_children,
9826 void *datap)
3019eac3
DE
9827{
9828 struct dwarf2_cu *cu = reader->cu;
9c541725 9829 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9830 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9831 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9832 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9833 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9834 struct attribute *attr;
3019eac3
DE
9835
9836 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9837 if (attr == NULL)
9838 {
19c3d4c9
DE
9839 complaint (&symfile_complaints,
9840 _("Dwarf Error: debug entry at offset 0x%x is missing"
9841 " its dwo_id [in module %s]"),
9c541725 9842 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9843 return;
9844 }
9845
3019eac3
DE
9846 dwo_unit->dwo_file = dwo_file;
9847 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9848 dwo_unit->section = section;
9c541725 9849 dwo_unit->sect_off = sect_off;
3019eac3
DE
9850 dwo_unit->length = cu->per_cu->length;
9851
b4f54984 9852 if (dwarf_read_debug)
4031ecc5 9853 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9854 to_underlying (sect_off),
9855 hex_string (dwo_unit->signature));
3019eac3
DE
9856}
9857
33c5cd75 9858/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9859 Note: This function processes DWO files only, not DWP files. */
3019eac3 9860
33c5cd75
DB
9861static void
9862create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9863 htab_t &cus_htab)
3019eac3
DE
9864{
9865 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9866 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9867 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9868
33c5cd75
DB
9869 dwarf2_read_section (objfile, &section);
9870 info_ptr = section.buffer;
3019eac3
DE
9871
9872 if (info_ptr == NULL)
33c5cd75 9873 return;
3019eac3 9874
b4f54984 9875 if (dwarf_read_debug)
19c3d4c9
DE
9876 {
9877 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9878 get_section_name (&section),
9879 get_section_file_name (&section));
19c3d4c9 9880 }
3019eac3 9881
33c5cd75 9882 end_ptr = info_ptr + section.size;
3019eac3
DE
9883 while (info_ptr < end_ptr)
9884 {
9885 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9886 struct create_dwo_cu_data create_dwo_cu_data;
9887 struct dwo_unit *dwo_unit;
9888 void **slot;
9889 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9890
19c3d4c9
DE
9891 memset (&create_dwo_cu_data.dwo_unit, 0,
9892 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9893 memset (&per_cu, 0, sizeof (per_cu));
9894 per_cu.objfile = objfile;
9895 per_cu.is_debug_types = 0;
33c5cd75
DB
9896 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9897 per_cu.section = &section;
c5ed0576 9898 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9899
9900 init_cutu_and_read_dies_no_follow (
9901 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9902 info_ptr += per_cu.length;
9903
9904 // If the unit could not be parsed, skip it.
9905 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9906 continue;
3019eac3 9907
33c5cd75
DB
9908 if (cus_htab == NULL)
9909 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 9910
33c5cd75
DB
9911 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9912 *dwo_unit = create_dwo_cu_data.dwo_unit;
9913 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9914 gdb_assert (slot != NULL);
9915 if (*slot != NULL)
19c3d4c9 9916 {
33c5cd75
DB
9917 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9918 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 9919
33c5cd75
DB
9920 complaint (&symfile_complaints,
9921 _("debug cu entry at offset 0x%x is duplicate to"
9922 " the entry at offset 0x%x, signature %s"),
9923 to_underlying (sect_off), to_underlying (dup_sect_off),
9924 hex_string (dwo_unit->signature));
19c3d4c9 9925 }
33c5cd75 9926 *slot = (void *)dwo_unit;
3019eac3 9927 }
3019eac3
DE
9928}
9929
80626a55
DE
9930/* DWP file .debug_{cu,tu}_index section format:
9931 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9932
d2415c6c
DE
9933 DWP Version 1:
9934
80626a55
DE
9935 Both index sections have the same format, and serve to map a 64-bit
9936 signature to a set of section numbers. Each section begins with a header,
9937 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9938 indexes, and a pool of 32-bit section numbers. The index sections will be
9939 aligned at 8-byte boundaries in the file.
9940
d2415c6c
DE
9941 The index section header consists of:
9942
9943 V, 32 bit version number
9944 -, 32 bits unused
9945 N, 32 bit number of compilation units or type units in the index
9946 M, 32 bit number of slots in the hash table
80626a55 9947
d2415c6c 9948 Numbers are recorded using the byte order of the application binary.
80626a55 9949
d2415c6c
DE
9950 The hash table begins at offset 16 in the section, and consists of an array
9951 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9952 order of the application binary). Unused slots in the hash table are 0.
9953 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9954
d2415c6c
DE
9955 The parallel table begins immediately after the hash table
9956 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9957 array of 32-bit indexes (using the byte order of the application binary),
9958 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9959 table contains a 32-bit index into the pool of section numbers. For unused
9960 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9961
73869dc2
DE
9962 The pool of section numbers begins immediately following the hash table
9963 (at offset 16 + 12 * M from the beginning of the section). The pool of
9964 section numbers consists of an array of 32-bit words (using the byte order
9965 of the application binary). Each item in the array is indexed starting
9966 from 0. The hash table entry provides the index of the first section
9967 number in the set. Additional section numbers in the set follow, and the
9968 set is terminated by a 0 entry (section number 0 is not used in ELF).
9969
9970 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9971 section must be the first entry in the set, and the .debug_abbrev.dwo must
9972 be the second entry. Other members of the set may follow in any order.
9973
9974 ---
9975
9976 DWP Version 2:
9977
9978 DWP Version 2 combines all the .debug_info, etc. sections into one,
9979 and the entries in the index tables are now offsets into these sections.
9980 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9981 section.
9982
9983 Index Section Contents:
9984 Header
9985 Hash Table of Signatures dwp_hash_table.hash_table
9986 Parallel Table of Indices dwp_hash_table.unit_table
9987 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9988 Table of Section Sizes dwp_hash_table.v2.sizes
9989
9990 The index section header consists of:
9991
9992 V, 32 bit version number
9993 L, 32 bit number of columns in the table of section offsets
9994 N, 32 bit number of compilation units or type units in the index
9995 M, 32 bit number of slots in the hash table
9996
9997 Numbers are recorded using the byte order of the application binary.
9998
9999 The hash table has the same format as version 1.
10000 The parallel table of indices has the same format as version 1,
10001 except that the entries are origin-1 indices into the table of sections
10002 offsets and the table of section sizes.
10003
10004 The table of offsets begins immediately following the parallel table
10005 (at offset 16 + 12 * M from the beginning of the section). The table is
10006 a two-dimensional array of 32-bit words (using the byte order of the
10007 application binary), with L columns and N+1 rows, in row-major order.
10008 Each row in the array is indexed starting from 0. The first row provides
10009 a key to the remaining rows: each column in this row provides an identifier
10010 for a debug section, and the offsets in the same column of subsequent rows
10011 refer to that section. The section identifiers are:
10012
10013 DW_SECT_INFO 1 .debug_info.dwo
10014 DW_SECT_TYPES 2 .debug_types.dwo
10015 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10016 DW_SECT_LINE 4 .debug_line.dwo
10017 DW_SECT_LOC 5 .debug_loc.dwo
10018 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10019 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10020 DW_SECT_MACRO 8 .debug_macro.dwo
10021
10022 The offsets provided by the CU and TU index sections are the base offsets
10023 for the contributions made by each CU or TU to the corresponding section
10024 in the package file. Each CU and TU header contains an abbrev_offset
10025 field, used to find the abbreviations table for that CU or TU within the
10026 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10027 be interpreted as relative to the base offset given in the index section.
10028 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10029 should be interpreted as relative to the base offset for .debug_line.dwo,
10030 and offsets into other debug sections obtained from DWARF attributes should
10031 also be interpreted as relative to the corresponding base offset.
10032
10033 The table of sizes begins immediately following the table of offsets.
10034 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10035 with L columns and N rows, in row-major order. Each row in the array is
10036 indexed starting from 1 (row 0 is shared by the two tables).
10037
10038 ---
10039
10040 Hash table lookup is handled the same in version 1 and 2:
10041
10042 We assume that N and M will not exceed 2^32 - 1.
10043 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10044
d2415c6c
DE
10045 Given a 64-bit compilation unit signature or a type signature S, an entry
10046 in the hash table is located as follows:
80626a55 10047
d2415c6c
DE
10048 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10049 the low-order k bits all set to 1.
80626a55 10050
d2415c6c 10051 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10052
d2415c6c
DE
10053 3) If the hash table entry at index H matches the signature, use that
10054 entry. If the hash table entry at index H is unused (all zeroes),
10055 terminate the search: the signature is not present in the table.
80626a55 10056
d2415c6c 10057 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10058
d2415c6c 10059 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10060 to stop at an unused slot or find the match. */
80626a55
DE
10061
10062/* Create a hash table to map DWO IDs to their CU/TU entry in
10063 .debug_{info,types}.dwo in DWP_FILE.
10064 Returns NULL if there isn't one.
10065 Note: This function processes DWP files only, not DWO files. */
10066
10067static struct dwp_hash_table *
10068create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10069{
10070 struct objfile *objfile = dwarf2_per_objfile->objfile;
10071 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10072 const gdb_byte *index_ptr, *index_end;
80626a55 10073 struct dwarf2_section_info *index;
73869dc2 10074 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10075 struct dwp_hash_table *htab;
10076
10077 if (is_debug_types)
10078 index = &dwp_file->sections.tu_index;
10079 else
10080 index = &dwp_file->sections.cu_index;
10081
10082 if (dwarf2_section_empty_p (index))
10083 return NULL;
10084 dwarf2_read_section (objfile, index);
10085
10086 index_ptr = index->buffer;
10087 index_end = index_ptr + index->size;
10088
10089 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10090 index_ptr += 4;
10091 if (version == 2)
10092 nr_columns = read_4_bytes (dbfd, index_ptr);
10093 else
10094 nr_columns = 0;
10095 index_ptr += 4;
80626a55
DE
10096 nr_units = read_4_bytes (dbfd, index_ptr);
10097 index_ptr += 4;
10098 nr_slots = read_4_bytes (dbfd, index_ptr);
10099 index_ptr += 4;
10100
73869dc2 10101 if (version != 1 && version != 2)
80626a55 10102 {
21aa081e 10103 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10104 " [in module %s]"),
21aa081e 10105 pulongest (version), dwp_file->name);
80626a55
DE
10106 }
10107 if (nr_slots != (nr_slots & -nr_slots))
10108 {
21aa081e 10109 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10110 " is not power of 2 [in module %s]"),
21aa081e 10111 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10112 }
10113
10114 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10115 htab->version = version;
10116 htab->nr_columns = nr_columns;
80626a55
DE
10117 htab->nr_units = nr_units;
10118 htab->nr_slots = nr_slots;
10119 htab->hash_table = index_ptr;
10120 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10121
10122 /* Exit early if the table is empty. */
10123 if (nr_slots == 0 || nr_units == 0
10124 || (version == 2 && nr_columns == 0))
10125 {
10126 /* All must be zero. */
10127 if (nr_slots != 0 || nr_units != 0
10128 || (version == 2 && nr_columns != 0))
10129 {
10130 complaint (&symfile_complaints,
10131 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10132 " all zero [in modules %s]"),
10133 dwp_file->name);
10134 }
10135 return htab;
10136 }
10137
10138 if (version == 1)
10139 {
10140 htab->section_pool.v1.indices =
10141 htab->unit_table + sizeof (uint32_t) * nr_slots;
10142 /* It's harder to decide whether the section is too small in v1.
10143 V1 is deprecated anyway so we punt. */
10144 }
10145 else
10146 {
10147 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10148 int *ids = htab->section_pool.v2.section_ids;
10149 /* Reverse map for error checking. */
10150 int ids_seen[DW_SECT_MAX + 1];
10151 int i;
10152
10153 if (nr_columns < 2)
10154 {
10155 error (_("Dwarf Error: bad DWP hash table, too few columns"
10156 " in section table [in module %s]"),
10157 dwp_file->name);
10158 }
10159 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10160 {
10161 error (_("Dwarf Error: bad DWP hash table, too many columns"
10162 " in section table [in module %s]"),
10163 dwp_file->name);
10164 }
10165 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10166 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10167 for (i = 0; i < nr_columns; ++i)
10168 {
10169 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10170
10171 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10172 {
10173 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10174 " in section table [in module %s]"),
10175 id, dwp_file->name);
10176 }
10177 if (ids_seen[id] != -1)
10178 {
10179 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10180 " id %d in section table [in module %s]"),
10181 id, dwp_file->name);
10182 }
10183 ids_seen[id] = i;
10184 ids[i] = id;
10185 }
10186 /* Must have exactly one info or types section. */
10187 if (((ids_seen[DW_SECT_INFO] != -1)
10188 + (ids_seen[DW_SECT_TYPES] != -1))
10189 != 1)
10190 {
10191 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10192 " DWO info/types section [in module %s]"),
10193 dwp_file->name);
10194 }
10195 /* Must have an abbrev section. */
10196 if (ids_seen[DW_SECT_ABBREV] == -1)
10197 {
10198 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10199 " section [in module %s]"),
10200 dwp_file->name);
10201 }
10202 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10203 htab->section_pool.v2.sizes =
10204 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10205 * nr_units * nr_columns);
10206 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10207 * nr_units * nr_columns))
10208 > index_end)
10209 {
10210 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10211 " [in module %s]"),
10212 dwp_file->name);
10213 }
10214 }
80626a55
DE
10215
10216 return htab;
10217}
10218
10219/* Update SECTIONS with the data from SECTP.
10220
10221 This function is like the other "locate" section routines that are
10222 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10223 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10224
10225 The result is non-zero for success, or zero if an error was found. */
10226
10227static int
73869dc2
DE
10228locate_v1_virtual_dwo_sections (asection *sectp,
10229 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10230{
10231 const struct dwop_section_names *names = &dwop_section_names;
10232
10233 if (section_is_p (sectp->name, &names->abbrev_dwo))
10234 {
10235 /* There can be only one. */
049412e3 10236 if (sections->abbrev.s.section != NULL)
80626a55 10237 return 0;
049412e3 10238 sections->abbrev.s.section = sectp;
80626a55
DE
10239 sections->abbrev.size = bfd_get_section_size (sectp);
10240 }
10241 else if (section_is_p (sectp->name, &names->info_dwo)
10242 || section_is_p (sectp->name, &names->types_dwo))
10243 {
10244 /* There can be only one. */
049412e3 10245 if (sections->info_or_types.s.section != NULL)
80626a55 10246 return 0;
049412e3 10247 sections->info_or_types.s.section = sectp;
80626a55
DE
10248 sections->info_or_types.size = bfd_get_section_size (sectp);
10249 }
10250 else if (section_is_p (sectp->name, &names->line_dwo))
10251 {
10252 /* There can be only one. */
049412e3 10253 if (sections->line.s.section != NULL)
80626a55 10254 return 0;
049412e3 10255 sections->line.s.section = sectp;
80626a55
DE
10256 sections->line.size = bfd_get_section_size (sectp);
10257 }
10258 else if (section_is_p (sectp->name, &names->loc_dwo))
10259 {
10260 /* There can be only one. */
049412e3 10261 if (sections->loc.s.section != NULL)
80626a55 10262 return 0;
049412e3 10263 sections->loc.s.section = sectp;
80626a55
DE
10264 sections->loc.size = bfd_get_section_size (sectp);
10265 }
10266 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10267 {
10268 /* There can be only one. */
049412e3 10269 if (sections->macinfo.s.section != NULL)
80626a55 10270 return 0;
049412e3 10271 sections->macinfo.s.section = sectp;
80626a55
DE
10272 sections->macinfo.size = bfd_get_section_size (sectp);
10273 }
10274 else if (section_is_p (sectp->name, &names->macro_dwo))
10275 {
10276 /* There can be only one. */
049412e3 10277 if (sections->macro.s.section != NULL)
80626a55 10278 return 0;
049412e3 10279 sections->macro.s.section = sectp;
80626a55
DE
10280 sections->macro.size = bfd_get_section_size (sectp);
10281 }
10282 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10283 {
10284 /* There can be only one. */
049412e3 10285 if (sections->str_offsets.s.section != NULL)
80626a55 10286 return 0;
049412e3 10287 sections->str_offsets.s.section = sectp;
80626a55
DE
10288 sections->str_offsets.size = bfd_get_section_size (sectp);
10289 }
10290 else
10291 {
10292 /* No other kind of section is valid. */
10293 return 0;
10294 }
10295
10296 return 1;
10297}
10298
73869dc2
DE
10299/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10300 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10301 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10302 This is for DWP version 1 files. */
80626a55
DE
10303
10304static struct dwo_unit *
73869dc2
DE
10305create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10306 uint32_t unit_index,
10307 const char *comp_dir,
10308 ULONGEST signature, int is_debug_types)
80626a55
DE
10309{
10310 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10311 const struct dwp_hash_table *dwp_htab =
10312 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10313 bfd *dbfd = dwp_file->dbfd;
10314 const char *kind = is_debug_types ? "TU" : "CU";
10315 struct dwo_file *dwo_file;
10316 struct dwo_unit *dwo_unit;
73869dc2 10317 struct virtual_v1_dwo_sections sections;
80626a55
DE
10318 void **dwo_file_slot;
10319 char *virtual_dwo_name;
80626a55
DE
10320 struct cleanup *cleanups;
10321 int i;
10322
73869dc2
DE
10323 gdb_assert (dwp_file->version == 1);
10324
b4f54984 10325 if (dwarf_read_debug)
80626a55 10326 {
73869dc2 10327 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10328 kind,
73869dc2 10329 pulongest (unit_index), hex_string (signature),
80626a55
DE
10330 dwp_file->name);
10331 }
10332
19ac8c2e 10333 /* Fetch the sections of this DWO unit.
80626a55
DE
10334 Put a limit on the number of sections we look for so that bad data
10335 doesn't cause us to loop forever. */
10336
73869dc2 10337#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10338 (1 /* .debug_info or .debug_types */ \
10339 + 1 /* .debug_abbrev */ \
10340 + 1 /* .debug_line */ \
10341 + 1 /* .debug_loc */ \
10342 + 1 /* .debug_str_offsets */ \
19ac8c2e 10343 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10344 + 1 /* trailing zero */)
10345
10346 memset (&sections, 0, sizeof (sections));
10347 cleanups = make_cleanup (null_cleanup, 0);
10348
73869dc2 10349 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10350 {
10351 asection *sectp;
10352 uint32_t section_nr =
10353 read_4_bytes (dbfd,
73869dc2
DE
10354 dwp_htab->section_pool.v1.indices
10355 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10356
10357 if (section_nr == 0)
10358 break;
10359 if (section_nr >= dwp_file->num_sections)
10360 {
10361 error (_("Dwarf Error: bad DWP hash table, section number too large"
10362 " [in module %s]"),
10363 dwp_file->name);
10364 }
10365
10366 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10367 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10368 {
10369 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10370 " [in module %s]"),
10371 dwp_file->name);
10372 }
10373 }
10374
10375 if (i < 2
a32a8923
DE
10376 || dwarf2_section_empty_p (&sections.info_or_types)
10377 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10378 {
10379 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10380 " [in module %s]"),
10381 dwp_file->name);
10382 }
73869dc2 10383 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10384 {
10385 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10386 " [in module %s]"),
10387 dwp_file->name);
10388 }
10389
10390 /* It's easier for the rest of the code if we fake a struct dwo_file and
10391 have dwo_unit "live" in that. At least for now.
10392
10393 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10394 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10395 file, we can combine them back into a virtual DWO file to save space
10396 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10397 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10398
2792b94d
PM
10399 virtual_dwo_name =
10400 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10401 get_section_id (&sections.abbrev),
10402 get_section_id (&sections.line),
10403 get_section_id (&sections.loc),
10404 get_section_id (&sections.str_offsets));
80626a55
DE
10405 make_cleanup (xfree, virtual_dwo_name);
10406 /* Can we use an existing virtual DWO file? */
0ac5b59e 10407 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10408 /* Create one if necessary. */
10409 if (*dwo_file_slot == NULL)
10410 {
b4f54984 10411 if (dwarf_read_debug)
80626a55
DE
10412 {
10413 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10414 virtual_dwo_name);
10415 }
10416 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10417 dwo_file->dwo_name
10418 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10419 virtual_dwo_name,
10420 strlen (virtual_dwo_name));
0ac5b59e 10421 dwo_file->comp_dir = comp_dir;
80626a55
DE
10422 dwo_file->sections.abbrev = sections.abbrev;
10423 dwo_file->sections.line = sections.line;
10424 dwo_file->sections.loc = sections.loc;
10425 dwo_file->sections.macinfo = sections.macinfo;
10426 dwo_file->sections.macro = sections.macro;
10427 dwo_file->sections.str_offsets = sections.str_offsets;
10428 /* The "str" section is global to the entire DWP file. */
10429 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10430 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10431 there's no need to record it in dwo_file.
10432 Also, we can't simply record type sections in dwo_file because
10433 we record a pointer into the vector in dwo_unit. As we collect more
10434 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10435 for it, invalidating all copies of pointers into the previous
10436 contents. */
80626a55
DE
10437 *dwo_file_slot = dwo_file;
10438 }
10439 else
10440 {
b4f54984 10441 if (dwarf_read_debug)
80626a55
DE
10442 {
10443 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10444 virtual_dwo_name);
10445 }
9a3c8263 10446 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10447 }
10448 do_cleanups (cleanups);
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;
10524 char *virtual_dwo_name;
73869dc2
DE
10525 struct cleanup *cleanups;
10526 int i;
10527
10528 gdb_assert (dwp_file->version == 2);
10529
b4f54984 10530 if (dwarf_read_debug)
73869dc2
DE
10531 {
10532 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10533 kind,
10534 pulongest (unit_index), hex_string (signature),
10535 dwp_file->name);
10536 }
10537
10538 /* Fetch the section offsets of this DWO unit. */
10539
10540 memset (&sections, 0, sizeof (sections));
10541 cleanups = make_cleanup (null_cleanup, 0);
10542
10543 for (i = 0; i < dwp_htab->nr_columns; ++i)
10544 {
10545 uint32_t offset = read_4_bytes (dbfd,
10546 dwp_htab->section_pool.v2.offsets
10547 + (((unit_index - 1) * dwp_htab->nr_columns
10548 + i)
10549 * sizeof (uint32_t)));
10550 uint32_t size = read_4_bytes (dbfd,
10551 dwp_htab->section_pool.v2.sizes
10552 + (((unit_index - 1) * dwp_htab->nr_columns
10553 + i)
10554 * sizeof (uint32_t)));
10555
10556 switch (dwp_htab->section_pool.v2.section_ids[i])
10557 {
10558 case DW_SECT_INFO:
10559 case DW_SECT_TYPES:
10560 sections.info_or_types_offset = offset;
10561 sections.info_or_types_size = size;
10562 break;
10563 case DW_SECT_ABBREV:
10564 sections.abbrev_offset = offset;
10565 sections.abbrev_size = size;
10566 break;
10567 case DW_SECT_LINE:
10568 sections.line_offset = offset;
10569 sections.line_size = size;
10570 break;
10571 case DW_SECT_LOC:
10572 sections.loc_offset = offset;
10573 sections.loc_size = size;
10574 break;
10575 case DW_SECT_STR_OFFSETS:
10576 sections.str_offsets_offset = offset;
10577 sections.str_offsets_size = size;
10578 break;
10579 case DW_SECT_MACINFO:
10580 sections.macinfo_offset = offset;
10581 sections.macinfo_size = size;
10582 break;
10583 case DW_SECT_MACRO:
10584 sections.macro_offset = offset;
10585 sections.macro_size = size;
10586 break;
10587 }
10588 }
10589
10590 /* It's easier for the rest of the code if we fake a struct dwo_file and
10591 have dwo_unit "live" in that. At least for now.
10592
10593 The DWP file can be made up of a random collection of CUs and TUs.
10594 However, for each CU + set of TUs that came from the same original DWO
10595 file, we can combine them back into a virtual DWO file to save space
10596 (fewer struct dwo_file objects to allocate). Remember that for really
10597 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10598
10599 virtual_dwo_name =
10600 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10601 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10602 (long) (sections.line_size ? sections.line_offset : 0),
10603 (long) (sections.loc_size ? sections.loc_offset : 0),
10604 (long) (sections.str_offsets_size
10605 ? sections.str_offsets_offset : 0));
10606 make_cleanup (xfree, virtual_dwo_name);
10607 /* Can we use an existing virtual DWO file? */
10608 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10609 /* Create one if necessary. */
10610 if (*dwo_file_slot == NULL)
10611 {
b4f54984 10612 if (dwarf_read_debug)
73869dc2
DE
10613 {
10614 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10615 virtual_dwo_name);
10616 }
10617 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10618 dwo_file->dwo_name
10619 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10620 virtual_dwo_name,
10621 strlen (virtual_dwo_name));
73869dc2
DE
10622 dwo_file->comp_dir = comp_dir;
10623 dwo_file->sections.abbrev =
10624 create_dwp_v2_section (&dwp_file->sections.abbrev,
10625 sections.abbrev_offset, sections.abbrev_size);
10626 dwo_file->sections.line =
10627 create_dwp_v2_section (&dwp_file->sections.line,
10628 sections.line_offset, sections.line_size);
10629 dwo_file->sections.loc =
10630 create_dwp_v2_section (&dwp_file->sections.loc,
10631 sections.loc_offset, sections.loc_size);
10632 dwo_file->sections.macinfo =
10633 create_dwp_v2_section (&dwp_file->sections.macinfo,
10634 sections.macinfo_offset, sections.macinfo_size);
10635 dwo_file->sections.macro =
10636 create_dwp_v2_section (&dwp_file->sections.macro,
10637 sections.macro_offset, sections.macro_size);
10638 dwo_file->sections.str_offsets =
10639 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10640 sections.str_offsets_offset,
10641 sections.str_offsets_size);
10642 /* The "str" section is global to the entire DWP file. */
10643 dwo_file->sections.str = dwp_file->sections.str;
10644 /* The info or types section is assigned below to dwo_unit,
10645 there's no need to record it in dwo_file.
10646 Also, we can't simply record type sections in dwo_file because
10647 we record a pointer into the vector in dwo_unit. As we collect more
10648 types we'll grow the vector and eventually have to reallocate space
10649 for it, invalidating all copies of pointers into the previous
10650 contents. */
10651 *dwo_file_slot = dwo_file;
10652 }
10653 else
10654 {
b4f54984 10655 if (dwarf_read_debug)
73869dc2
DE
10656 {
10657 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10658 virtual_dwo_name);
10659 }
9a3c8263 10660 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10661 }
10662 do_cleanups (cleanups);
10663
10664 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10665 dwo_unit->dwo_file = dwo_file;
10666 dwo_unit->signature = signature;
8d749320
SM
10667 dwo_unit->section =
10668 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10669 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10670 ? &dwp_file->sections.types
10671 : &dwp_file->sections.info,
10672 sections.info_or_types_offset,
10673 sections.info_or_types_size);
10674 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10675
10676 return dwo_unit;
10677}
10678
57d63ce2
DE
10679/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10680 Returns NULL if the signature isn't found. */
80626a55
DE
10681
10682static struct dwo_unit *
57d63ce2
DE
10683lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10684 ULONGEST signature, int is_debug_types)
80626a55 10685{
57d63ce2
DE
10686 const struct dwp_hash_table *dwp_htab =
10687 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10688 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10689 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10690 uint32_t hash = signature & mask;
10691 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10692 unsigned int i;
10693 void **slot;
870f88f7 10694 struct dwo_unit find_dwo_cu;
80626a55
DE
10695
10696 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10697 find_dwo_cu.signature = signature;
19ac8c2e
DE
10698 slot = htab_find_slot (is_debug_types
10699 ? dwp_file->loaded_tus
10700 : dwp_file->loaded_cus,
10701 &find_dwo_cu, INSERT);
80626a55
DE
10702
10703 if (*slot != NULL)
9a3c8263 10704 return (struct dwo_unit *) *slot;
80626a55
DE
10705
10706 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10707 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10708 {
10709 ULONGEST signature_in_table;
10710
10711 signature_in_table =
57d63ce2 10712 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10713 if (signature_in_table == signature)
10714 {
57d63ce2
DE
10715 uint32_t unit_index =
10716 read_4_bytes (dbfd,
10717 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10718
73869dc2
DE
10719 if (dwp_file->version == 1)
10720 {
10721 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10722 comp_dir, signature,
10723 is_debug_types);
10724 }
10725 else
10726 {
10727 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10728 comp_dir, signature,
10729 is_debug_types);
10730 }
9a3c8263 10731 return (struct dwo_unit *) *slot;
80626a55
DE
10732 }
10733 if (signature_in_table == 0)
10734 return NULL;
10735 hash = (hash + hash2) & mask;
10736 }
10737
10738 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10739 " [in module %s]"),
10740 dwp_file->name);
10741}
10742
ab5088bf 10743/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10744 Open the file specified by FILE_NAME and hand it off to BFD for
10745 preliminary analysis. Return a newly initialized bfd *, which
10746 includes a canonicalized copy of FILE_NAME.
80626a55 10747 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10748 SEARCH_CWD is true if the current directory is to be searched.
10749 It will be searched before debug-file-directory.
13aaf454
DE
10750 If successful, the file is added to the bfd include table of the
10751 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10752 If unable to find/open the file, return NULL.
3019eac3
DE
10753 NOTE: This function is derived from symfile_bfd_open. */
10754
192b62ce 10755static gdb_bfd_ref_ptr
6ac97d4c 10756try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10757{
80626a55 10758 int desc, flags;
3019eac3 10759 char *absolute_name;
9c02c129
DE
10760 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10761 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10762 to debug_file_directory. */
10763 char *search_path;
10764 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10765
6ac97d4c
DE
10766 if (search_cwd)
10767 {
10768 if (*debug_file_directory != '\0')
10769 search_path = concat (".", dirname_separator_string,
b36cec19 10770 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10771 else
10772 search_path = xstrdup (".");
10773 }
9c02c129 10774 else
6ac97d4c 10775 search_path = xstrdup (debug_file_directory);
3019eac3 10776
492c0ab7 10777 flags = OPF_RETURN_REALPATH;
80626a55
DE
10778 if (is_dwp)
10779 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10780 desc = openp (search_path, flags, file_name,
3019eac3 10781 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10782 xfree (search_path);
3019eac3
DE
10783 if (desc < 0)
10784 return NULL;
10785
192b62ce 10786 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10787 xfree (absolute_name);
9c02c129
DE
10788 if (sym_bfd == NULL)
10789 return NULL;
192b62ce 10790 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10791
192b62ce
TT
10792 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10793 return NULL;
3019eac3 10794
13aaf454
DE
10795 /* Success. Record the bfd as having been included by the objfile's bfd.
10796 This is important because things like demangled_names_hash lives in the
10797 objfile's per_bfd space and may have references to things like symbol
10798 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10799 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10800
3019eac3
DE
10801 return sym_bfd;
10802}
10803
ab5088bf 10804/* Try to open DWO file FILE_NAME.
3019eac3
DE
10805 COMP_DIR is the DW_AT_comp_dir attribute.
10806 The result is the bfd handle of the file.
10807 If there is a problem finding or opening the file, return NULL.
10808 Upon success, the canonicalized path of the file is stored in the bfd,
10809 same as symfile_bfd_open. */
10810
192b62ce 10811static gdb_bfd_ref_ptr
ab5088bf 10812open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10813{
80626a55 10814 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10815 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10816
10817 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10818
10819 if (comp_dir != NULL)
10820 {
b36cec19
PA
10821 char *path_to_try = concat (comp_dir, SLASH_STRING,
10822 file_name, (char *) NULL);
3019eac3
DE
10823
10824 /* NOTE: If comp_dir is a relative path, this will also try the
10825 search path, which seems useful. */
192b62ce
TT
10826 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10827 1 /*search_cwd*/));
3019eac3
DE
10828 xfree (path_to_try);
10829 if (abfd != NULL)
10830 return abfd;
10831 }
10832
10833 /* That didn't work, try debug-file-directory, which, despite its name,
10834 is a list of paths. */
10835
10836 if (*debug_file_directory == '\0')
10837 return NULL;
10838
6ac97d4c 10839 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10840}
10841
80626a55
DE
10842/* This function is mapped across the sections and remembers the offset and
10843 size of each of the DWO debugging sections we are interested in. */
10844
10845static void
10846dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10847{
9a3c8263 10848 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10849 const struct dwop_section_names *names = &dwop_section_names;
10850
10851 if (section_is_p (sectp->name, &names->abbrev_dwo))
10852 {
049412e3 10853 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10854 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10855 }
10856 else if (section_is_p (sectp->name, &names->info_dwo))
10857 {
049412e3 10858 dwo_sections->info.s.section = sectp;
80626a55
DE
10859 dwo_sections->info.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->line_dwo))
10862 {
049412e3 10863 dwo_sections->line.s.section = sectp;
80626a55
DE
10864 dwo_sections->line.size = bfd_get_section_size (sectp);
10865 }
10866 else if (section_is_p (sectp->name, &names->loc_dwo))
10867 {
049412e3 10868 dwo_sections->loc.s.section = sectp;
80626a55
DE
10869 dwo_sections->loc.size = bfd_get_section_size (sectp);
10870 }
10871 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10872 {
049412e3 10873 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10874 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10875 }
10876 else if (section_is_p (sectp->name, &names->macro_dwo))
10877 {
049412e3 10878 dwo_sections->macro.s.section = sectp;
80626a55
DE
10879 dwo_sections->macro.size = bfd_get_section_size (sectp);
10880 }
10881 else if (section_is_p (sectp->name, &names->str_dwo))
10882 {
049412e3 10883 dwo_sections->str.s.section = sectp;
80626a55
DE
10884 dwo_sections->str.size = bfd_get_section_size (sectp);
10885 }
10886 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10887 {
049412e3 10888 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10889 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10890 }
10891 else if (section_is_p (sectp->name, &names->types_dwo))
10892 {
10893 struct dwarf2_section_info type_section;
10894
10895 memset (&type_section, 0, sizeof (type_section));
049412e3 10896 type_section.s.section = sectp;
80626a55
DE
10897 type_section.size = bfd_get_section_size (sectp);
10898 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10899 &type_section);
10900 }
10901}
10902
ab5088bf 10903/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10904 by PER_CU. This is for the non-DWP case.
80626a55 10905 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10906
10907static struct dwo_file *
0ac5b59e
DE
10908open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10909 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10910{
10911 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10912 struct dwo_file *dwo_file;
3019eac3
DE
10913 struct cleanup *cleanups;
10914
192b62ce 10915 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10916 if (dbfd == NULL)
10917 {
b4f54984 10918 if (dwarf_read_debug)
80626a55
DE
10919 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10920 return NULL;
10921 }
10922 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10923 dwo_file->dwo_name = dwo_name;
10924 dwo_file->comp_dir = comp_dir;
192b62ce 10925 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10926
10927 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10928
192b62ce
TT
10929 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10930 &dwo_file->sections);
3019eac3 10931
33c5cd75 10932 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 10933
78d4d2c5
JK
10934 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10935 dwo_file->tus);
3019eac3
DE
10936
10937 discard_cleanups (cleanups);
10938
b4f54984 10939 if (dwarf_read_debug)
80626a55
DE
10940 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10941
3019eac3
DE
10942 return dwo_file;
10943}
10944
80626a55 10945/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10946 size of each of the DWP debugging sections common to version 1 and 2 that
10947 we are interested in. */
3019eac3 10948
80626a55 10949static void
73869dc2
DE
10950dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10951 void *dwp_file_ptr)
3019eac3 10952{
9a3c8263 10953 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10954 const struct dwop_section_names *names = &dwop_section_names;
10955 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10956
80626a55 10957 /* Record the ELF section number for later lookup: this is what the
73869dc2 10958 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10959 gdb_assert (elf_section_nr < dwp_file->num_sections);
10960 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10961
80626a55
DE
10962 /* Look for specific sections that we need. */
10963 if (section_is_p (sectp->name, &names->str_dwo))
10964 {
049412e3 10965 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10966 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10967 }
10968 else if (section_is_p (sectp->name, &names->cu_index))
10969 {
049412e3 10970 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10971 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10972 }
10973 else if (section_is_p (sectp->name, &names->tu_index))
10974 {
049412e3 10975 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10976 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10977 }
10978}
3019eac3 10979
73869dc2
DE
10980/* This function is mapped across the sections and remembers the offset and
10981 size of each of the DWP version 2 debugging sections that we are interested
10982 in. This is split into a separate function because we don't know if we
10983 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10984
10985static void
10986dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10987{
9a3c8263 10988 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10989 const struct dwop_section_names *names = &dwop_section_names;
10990 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10991
10992 /* Record the ELF section number for later lookup: this is what the
10993 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10994 gdb_assert (elf_section_nr < dwp_file->num_sections);
10995 dwp_file->elf_sections[elf_section_nr] = sectp;
10996
10997 /* Look for specific sections that we need. */
10998 if (section_is_p (sectp->name, &names->abbrev_dwo))
10999 {
049412e3 11000 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11001 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11002 }
11003 else if (section_is_p (sectp->name, &names->info_dwo))
11004 {
049412e3 11005 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11006 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11007 }
11008 else if (section_is_p (sectp->name, &names->line_dwo))
11009 {
049412e3 11010 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11011 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11012 }
11013 else if (section_is_p (sectp->name, &names->loc_dwo))
11014 {
049412e3 11015 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11016 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11017 }
11018 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11019 {
049412e3 11020 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11021 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11022 }
11023 else if (section_is_p (sectp->name, &names->macro_dwo))
11024 {
049412e3 11025 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11026 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11027 }
11028 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11029 {
049412e3 11030 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11031 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11032 }
11033 else if (section_is_p (sectp->name, &names->types_dwo))
11034 {
049412e3 11035 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11036 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11037 }
11038}
11039
80626a55 11040/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11041
80626a55
DE
11042static hashval_t
11043hash_dwp_loaded_cutus (const void *item)
11044{
9a3c8263 11045 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11046
80626a55
DE
11047 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11048 return dwo_unit->signature;
3019eac3
DE
11049}
11050
80626a55 11051/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11052
80626a55
DE
11053static int
11054eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11055{
9a3c8263
SM
11056 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11057 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11058
80626a55
DE
11059 return dua->signature == dub->signature;
11060}
3019eac3 11061
80626a55 11062/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11063
80626a55
DE
11064static htab_t
11065allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11066{
11067 return htab_create_alloc_ex (3,
11068 hash_dwp_loaded_cutus,
11069 eq_dwp_loaded_cutus,
11070 NULL,
11071 &objfile->objfile_obstack,
11072 hashtab_obstack_allocate,
11073 dummy_obstack_deallocate);
11074}
3019eac3 11075
ab5088bf
DE
11076/* Try to open DWP file FILE_NAME.
11077 The result is the bfd handle of the file.
11078 If there is a problem finding or opening the file, return NULL.
11079 Upon success, the canonicalized path of the file is stored in the bfd,
11080 same as symfile_bfd_open. */
11081
192b62ce 11082static gdb_bfd_ref_ptr
ab5088bf
DE
11083open_dwp_file (const char *file_name)
11084{
192b62ce
TT
11085 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11086 1 /*search_cwd*/));
6ac97d4c
DE
11087 if (abfd != NULL)
11088 return abfd;
11089
11090 /* Work around upstream bug 15652.
11091 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11092 [Whether that's a "bug" is debatable, but it is getting in our way.]
11093 We have no real idea where the dwp file is, because gdb's realpath-ing
11094 of the executable's path may have discarded the needed info.
11095 [IWBN if the dwp file name was recorded in the executable, akin to
11096 .gnu_debuglink, but that doesn't exist yet.]
11097 Strip the directory from FILE_NAME and search again. */
11098 if (*debug_file_directory != '\0')
11099 {
11100 /* Don't implicitly search the current directory here.
11101 If the user wants to search "." to handle this case,
11102 it must be added to debug-file-directory. */
11103 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11104 0 /*search_cwd*/);
11105 }
11106
11107 return NULL;
ab5088bf
DE
11108}
11109
80626a55
DE
11110/* Initialize the use of the DWP file for the current objfile.
11111 By convention the name of the DWP file is ${objfile}.dwp.
11112 The result is NULL if it can't be found. */
a766d390 11113
80626a55 11114static struct dwp_file *
ab5088bf 11115open_and_init_dwp_file (void)
80626a55
DE
11116{
11117 struct objfile *objfile = dwarf2_per_objfile->objfile;
11118 struct dwp_file *dwp_file;
80626a55 11119
82bf32bc
JK
11120 /* Try to find first .dwp for the binary file before any symbolic links
11121 resolving. */
6c447423
DE
11122
11123 /* If the objfile is a debug file, find the name of the real binary
11124 file and get the name of dwp file from there. */
d721ba37 11125 std::string dwp_name;
6c447423
DE
11126 if (objfile->separate_debug_objfile_backlink != NULL)
11127 {
11128 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11129 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11130
d721ba37 11131 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11132 }
11133 else
d721ba37
PA
11134 dwp_name = objfile->original_name;
11135
11136 dwp_name += ".dwp";
80626a55 11137
d721ba37 11138 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11139 if (dbfd == NULL
11140 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11141 {
11142 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11143 dwp_name = objfile_name (objfile);
11144 dwp_name += ".dwp";
11145 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11146 }
11147
80626a55
DE
11148 if (dbfd == NULL)
11149 {
b4f54984 11150 if (dwarf_read_debug)
d721ba37 11151 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11152 return NULL;
3019eac3 11153 }
80626a55 11154 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11155 dwp_file->name = bfd_get_filename (dbfd.get ());
11156 dwp_file->dbfd = dbfd.release ();
c906108c 11157
80626a55 11158 /* +1: section 0 is unused */
192b62ce 11159 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11160 dwp_file->elf_sections =
11161 OBSTACK_CALLOC (&objfile->objfile_obstack,
11162 dwp_file->num_sections, asection *);
11163
192b62ce
TT
11164 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11165 dwp_file);
80626a55
DE
11166
11167 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11168
11169 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11170
73869dc2
DE
11171 /* The DWP file version is stored in the hash table. Oh well. */
11172 if (dwp_file->cus->version != dwp_file->tus->version)
11173 {
11174 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11175 pretty bizarre. We use pulongest here because that's the established
4d65956b 11176 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11177 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11178 " TU version %s [in DWP file %s]"),
11179 pulongest (dwp_file->cus->version),
d721ba37 11180 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11181 }
11182 dwp_file->version = dwp_file->cus->version;
11183
11184 if (dwp_file->version == 2)
192b62ce
TT
11185 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11186 dwp_file);
73869dc2 11187
19ac8c2e
DE
11188 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11189 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11190
b4f54984 11191 if (dwarf_read_debug)
80626a55
DE
11192 {
11193 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11194 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11195 " %s CUs, %s TUs\n",
11196 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11197 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11198 }
11199
11200 return dwp_file;
3019eac3 11201}
c906108c 11202
ab5088bf
DE
11203/* Wrapper around open_and_init_dwp_file, only open it once. */
11204
11205static struct dwp_file *
11206get_dwp_file (void)
11207{
11208 if (! dwarf2_per_objfile->dwp_checked)
11209 {
11210 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11211 dwarf2_per_objfile->dwp_checked = 1;
11212 }
11213 return dwarf2_per_objfile->dwp_file;
11214}
11215
80626a55
DE
11216/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11217 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11218 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11219 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11220 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11221
11222 This is called, for example, when wanting to read a variable with a
11223 complex location. Therefore we don't want to do file i/o for every call.
11224 Therefore we don't want to look for a DWO file on every call.
11225 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11226 then we check if we've already seen DWO_NAME, and only THEN do we check
11227 for a DWO file.
11228
1c658ad5 11229 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11230 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11231
3019eac3 11232static struct dwo_unit *
80626a55
DE
11233lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11234 const char *dwo_name, const char *comp_dir,
11235 ULONGEST signature, int is_debug_types)
3019eac3
DE
11236{
11237 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11238 const char *kind = is_debug_types ? "TU" : "CU";
11239 void **dwo_file_slot;
3019eac3 11240 struct dwo_file *dwo_file;
80626a55 11241 struct dwp_file *dwp_file;
cb1df416 11242
6a506a2d
DE
11243 /* First see if there's a DWP file.
11244 If we have a DWP file but didn't find the DWO inside it, don't
11245 look for the original DWO file. It makes gdb behave differently
11246 depending on whether one is debugging in the build tree. */
cf2c3c16 11247
ab5088bf 11248 dwp_file = get_dwp_file ();
80626a55 11249 if (dwp_file != NULL)
cf2c3c16 11250 {
80626a55
DE
11251 const struct dwp_hash_table *dwp_htab =
11252 is_debug_types ? dwp_file->tus : dwp_file->cus;
11253
11254 if (dwp_htab != NULL)
11255 {
11256 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11257 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11258 signature, is_debug_types);
80626a55
DE
11259
11260 if (dwo_cutu != NULL)
11261 {
b4f54984 11262 if (dwarf_read_debug)
80626a55
DE
11263 {
11264 fprintf_unfiltered (gdb_stdlog,
11265 "Virtual DWO %s %s found: @%s\n",
11266 kind, hex_string (signature),
11267 host_address_to_string (dwo_cutu));
11268 }
11269 return dwo_cutu;
11270 }
11271 }
11272 }
6a506a2d 11273 else
80626a55 11274 {
6a506a2d 11275 /* No DWP file, look for the DWO file. */
80626a55 11276
6a506a2d
DE
11277 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11278 if (*dwo_file_slot == NULL)
80626a55 11279 {
6a506a2d
DE
11280 /* Read in the file and build a table of the CUs/TUs it contains. */
11281 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11282 }
6a506a2d 11283 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11284 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11285
6a506a2d 11286 if (dwo_file != NULL)
19c3d4c9 11287 {
6a506a2d
DE
11288 struct dwo_unit *dwo_cutu = NULL;
11289
11290 if (is_debug_types && dwo_file->tus)
11291 {
11292 struct dwo_unit find_dwo_cutu;
11293
11294 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11295 find_dwo_cutu.signature = signature;
9a3c8263
SM
11296 dwo_cutu
11297 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11298 }
33c5cd75 11299 else if (!is_debug_types && dwo_file->cus)
80626a55 11300 {
33c5cd75
DB
11301 struct dwo_unit find_dwo_cutu;
11302
11303 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11304 find_dwo_cutu.signature = signature;
11305 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11306 &find_dwo_cutu);
6a506a2d
DE
11307 }
11308
11309 if (dwo_cutu != NULL)
11310 {
b4f54984 11311 if (dwarf_read_debug)
6a506a2d
DE
11312 {
11313 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11314 kind, dwo_name, hex_string (signature),
11315 host_address_to_string (dwo_cutu));
11316 }
11317 return dwo_cutu;
80626a55
DE
11318 }
11319 }
2e276125 11320 }
9cdd5dbd 11321
80626a55
DE
11322 /* We didn't find it. This could mean a dwo_id mismatch, or
11323 someone deleted the DWO/DWP file, or the search path isn't set up
11324 correctly to find the file. */
11325
b4f54984 11326 if (dwarf_read_debug)
80626a55
DE
11327 {
11328 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11329 kind, dwo_name, hex_string (signature));
11330 }
3019eac3 11331
6656a72d
DE
11332 /* This is a warning and not a complaint because it can be caused by
11333 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11334 {
11335 /* Print the name of the DWP file if we looked there, helps the user
11336 better diagnose the problem. */
11337 char *dwp_text = NULL;
11338 struct cleanup *cleanups;
11339
11340 if (dwp_file != NULL)
11341 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11342 cleanups = make_cleanup (xfree, dwp_text);
11343
11344 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11345 " [in module %s]"),
11346 kind, dwo_name, hex_string (signature),
11347 dwp_text != NULL ? dwp_text : "",
11348 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11349 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11350
11351 do_cleanups (cleanups);
11352 }
3019eac3 11353 return NULL;
5fb290d7
DJ
11354}
11355
80626a55
DE
11356/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11357 See lookup_dwo_cutu_unit for details. */
11358
11359static struct dwo_unit *
11360lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11361 const char *dwo_name, const char *comp_dir,
11362 ULONGEST signature)
11363{
11364 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11365}
11366
11367/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11368 See lookup_dwo_cutu_unit for details. */
11369
11370static struct dwo_unit *
11371lookup_dwo_type_unit (struct signatured_type *this_tu,
11372 const char *dwo_name, const char *comp_dir)
11373{
11374 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11375}
11376
89e63ee4
DE
11377/* Traversal function for queue_and_load_all_dwo_tus. */
11378
11379static int
11380queue_and_load_dwo_tu (void **slot, void *info)
11381{
11382 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11383 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11384 ULONGEST signature = dwo_unit->signature;
11385 struct signatured_type *sig_type =
11386 lookup_dwo_signatured_type (per_cu->cu, signature);
11387
11388 if (sig_type != NULL)
11389 {
11390 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11391
11392 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11393 a real dependency of PER_CU on SIG_TYPE. That is detected later
11394 while processing PER_CU. */
11395 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11396 load_full_type_unit (sig_cu);
11397 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11398 }
11399
11400 return 1;
11401}
11402
11403/* Queue all TUs contained in the DWO of PER_CU to be read in.
11404 The DWO may have the only definition of the type, though it may not be
11405 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11406 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11407
11408static void
11409queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11410{
11411 struct dwo_unit *dwo_unit;
11412 struct dwo_file *dwo_file;
11413
11414 gdb_assert (!per_cu->is_debug_types);
11415 gdb_assert (get_dwp_file () == NULL);
11416 gdb_assert (per_cu->cu != NULL);
11417
11418 dwo_unit = per_cu->cu->dwo_unit;
11419 gdb_assert (dwo_unit != NULL);
11420
11421 dwo_file = dwo_unit->dwo_file;
11422 if (dwo_file->tus != NULL)
11423 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11424}
11425
3019eac3
DE
11426/* Free all resources associated with DWO_FILE.
11427 Close the DWO file and munmap the sections.
11428 All memory should be on the objfile obstack. */
348e048f
DE
11429
11430static void
3019eac3 11431free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11432{
348e048f 11433
5c6fa7ab 11434 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11435 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11436
3019eac3
DE
11437 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11438}
348e048f 11439
3019eac3 11440/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11441
3019eac3
DE
11442static void
11443free_dwo_file_cleanup (void *arg)
11444{
11445 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11446 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11447
3019eac3
DE
11448 free_dwo_file (dwo_file, objfile);
11449}
348e048f 11450
3019eac3 11451/* Traversal function for free_dwo_files. */
2ab95328 11452
3019eac3
DE
11453static int
11454free_dwo_file_from_slot (void **slot, void *info)
11455{
11456 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11457 struct objfile *objfile = (struct objfile *) info;
348e048f 11458
3019eac3 11459 free_dwo_file (dwo_file, objfile);
348e048f 11460
3019eac3
DE
11461 return 1;
11462}
348e048f 11463
3019eac3 11464/* Free all resources associated with DWO_FILES. */
348e048f 11465
3019eac3
DE
11466static void
11467free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11468{
11469 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11470}
3019eac3
DE
11471\f
11472/* Read in various DIEs. */
348e048f 11473
d389af10
JK
11474/* qsort helper for inherit_abstract_dies. */
11475
11476static int
11477unsigned_int_compar (const void *ap, const void *bp)
11478{
11479 unsigned int a = *(unsigned int *) ap;
11480 unsigned int b = *(unsigned int *) bp;
11481
11482 return (a > b) - (b > a);
11483}
11484
11485/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11486 Inherit only the children of the DW_AT_abstract_origin DIE not being
11487 already referenced by DW_AT_abstract_origin from the children of the
11488 current DIE. */
d389af10
JK
11489
11490static void
11491inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11492{
11493 struct die_info *child_die;
11494 unsigned die_children_count;
11495 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11496 sect_offset *offsets;
11497 sect_offset *offsets_end, *offsetp;
d389af10
JK
11498 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11499 struct die_info *origin_die;
11500 /* Iterator of the ORIGIN_DIE children. */
11501 struct die_info *origin_child_die;
11502 struct cleanup *cleanups;
11503 struct attribute *attr;
cd02d79d
PA
11504 struct dwarf2_cu *origin_cu;
11505 struct pending **origin_previous_list_in_scope;
d389af10
JK
11506
11507 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11508 if (!attr)
11509 return;
11510
cd02d79d
PA
11511 /* Note that following die references may follow to a die in a
11512 different cu. */
11513
11514 origin_cu = cu;
11515 origin_die = follow_die_ref (die, attr, &origin_cu);
11516
11517 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11518 symbols in. */
11519 origin_previous_list_in_scope = origin_cu->list_in_scope;
11520 origin_cu->list_in_scope = cu->list_in_scope;
11521
edb3359d
DJ
11522 if (die->tag != origin_die->tag
11523 && !(die->tag == DW_TAG_inlined_subroutine
11524 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11525 complaint (&symfile_complaints,
11526 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11527 to_underlying (die->sect_off),
11528 to_underlying (origin_die->sect_off));
d389af10
JK
11529
11530 child_die = die->child;
11531 die_children_count = 0;
11532 while (child_die && child_die->tag)
11533 {
11534 child_die = sibling_die (child_die);
11535 die_children_count++;
11536 }
8d749320 11537 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11538 cleanups = make_cleanup (xfree, offsets);
11539
11540 offsets_end = offsets;
3ea89b92
PMR
11541 for (child_die = die->child;
11542 child_die && child_die->tag;
11543 child_die = sibling_die (child_die))
11544 {
11545 struct die_info *child_origin_die;
11546 struct dwarf2_cu *child_origin_cu;
11547
11548 /* We are trying to process concrete instance entries:
216f72a1 11549 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11550 it's not relevant to our analysis here. i.e. detecting DIEs that are
11551 present in the abstract instance but not referenced in the concrete
11552 one. */
216f72a1
JK
11553 if (child_die->tag == DW_TAG_call_site
11554 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11555 continue;
11556
c38f313d
DJ
11557 /* For each CHILD_DIE, find the corresponding child of
11558 ORIGIN_DIE. If there is more than one layer of
11559 DW_AT_abstract_origin, follow them all; there shouldn't be,
11560 but GCC versions at least through 4.4 generate this (GCC PR
11561 40573). */
3ea89b92
PMR
11562 child_origin_die = child_die;
11563 child_origin_cu = cu;
c38f313d
DJ
11564 while (1)
11565 {
cd02d79d
PA
11566 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11567 child_origin_cu);
c38f313d
DJ
11568 if (attr == NULL)
11569 break;
cd02d79d
PA
11570 child_origin_die = follow_die_ref (child_origin_die, attr,
11571 &child_origin_cu);
c38f313d
DJ
11572 }
11573
d389af10
JK
11574 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11575 counterpart may exist. */
c38f313d 11576 if (child_origin_die != child_die)
d389af10 11577 {
edb3359d
DJ
11578 if (child_die->tag != child_origin_die->tag
11579 && !(child_die->tag == DW_TAG_inlined_subroutine
11580 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11581 complaint (&symfile_complaints,
11582 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11583 "different tags"),
11584 to_underlying (child_die->sect_off),
11585 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11586 if (child_origin_die->parent != origin_die)
11587 complaint (&symfile_complaints,
11588 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11589 "different parents"),
11590 to_underlying (child_die->sect_off),
11591 to_underlying (child_origin_die->sect_off));
c38f313d 11592 else
9c541725 11593 *offsets_end++ = child_origin_die->sect_off;
d389af10 11594 }
d389af10
JK
11595 }
11596 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11597 unsigned_int_compar);
11598 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11599 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11600 complaint (&symfile_complaints,
11601 _("Multiple children of DIE 0x%x refer "
11602 "to DIE 0x%x as their abstract origin"),
9c541725 11603 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11604
11605 offsetp = offsets;
11606 origin_child_die = origin_die->child;
11607 while (origin_child_die && origin_child_die->tag)
11608 {
11609 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11610 while (offsetp < offsets_end
9c541725 11611 && *offsetp < origin_child_die->sect_off)
d389af10 11612 offsetp++;
b64f50a1 11613 if (offsetp >= offsets_end
9c541725 11614 || *offsetp > origin_child_die->sect_off)
d389af10 11615 {
adde2bff
DE
11616 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11617 Check whether we're already processing ORIGIN_CHILD_DIE.
11618 This can happen with mutually referenced abstract_origins.
11619 PR 16581. */
11620 if (!origin_child_die->in_process)
11621 process_die (origin_child_die, origin_cu);
d389af10
JK
11622 }
11623 origin_child_die = sibling_die (origin_child_die);
11624 }
cd02d79d 11625 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11626
11627 do_cleanups (cleanups);
11628}
11629
c906108c 11630static void
e7c27a73 11631read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11632{
e7c27a73 11633 struct objfile *objfile = cu->objfile;
3e29f34a 11634 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11635 struct context_stack *newobj;
c906108c
SS
11636 CORE_ADDR lowpc;
11637 CORE_ADDR highpc;
11638 struct die_info *child_die;
edb3359d 11639 struct attribute *attr, *call_line, *call_file;
15d034d0 11640 const char *name;
e142c38c 11641 CORE_ADDR baseaddr;
801e3a5b 11642 struct block *block;
edb3359d 11643 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11644 VEC (symbolp) *template_args = NULL;
11645 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11646
11647 if (inlined_func)
11648 {
11649 /* If we do not have call site information, we can't show the
11650 caller of this inlined function. That's too confusing, so
11651 only use the scope for local variables. */
11652 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11653 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11654 if (call_line == NULL || call_file == NULL)
11655 {
11656 read_lexical_block_scope (die, cu);
11657 return;
11658 }
11659 }
c906108c 11660
e142c38c
DJ
11661 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11662
94af9270 11663 name = dwarf2_name (die, cu);
c906108c 11664
e8d05480
JB
11665 /* Ignore functions with missing or empty names. These are actually
11666 illegal according to the DWARF standard. */
11667 if (name == NULL)
11668 {
11669 complaint (&symfile_complaints,
b64f50a1 11670 _("missing name for subprogram DIE at %d"),
9c541725 11671 to_underlying (die->sect_off));
e8d05480
JB
11672 return;
11673 }
11674
11675 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11676 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11677 <= PC_BOUNDS_INVALID)
e8d05480 11678 {
ae4d0c03
PM
11679 attr = dwarf2_attr (die, DW_AT_external, cu);
11680 if (!attr || !DW_UNSND (attr))
11681 complaint (&symfile_complaints,
3e43a32a
MS
11682 _("cannot get low and high bounds "
11683 "for subprogram DIE at %d"),
9c541725 11684 to_underlying (die->sect_off));
e8d05480
JB
11685 return;
11686 }
c906108c 11687
3e29f34a
MR
11688 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11689 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11690
34eaf542
TT
11691 /* If we have any template arguments, then we must allocate a
11692 different sort of symbol. */
11693 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11694 {
11695 if (child_die->tag == DW_TAG_template_type_param
11696 || child_die->tag == DW_TAG_template_value_param)
11697 {
e623cf5d 11698 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11699 templ_func->base.is_cplus_template_function = 1;
11700 break;
11701 }
11702 }
11703
fe978cb0
PA
11704 newobj = push_context (0, lowpc);
11705 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11706 (struct symbol *) templ_func);
4c2df51b 11707
4cecd739
DJ
11708 /* If there is a location expression for DW_AT_frame_base, record
11709 it. */
e142c38c 11710 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11711 if (attr)
fe978cb0 11712 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11713
63e43d3a
PMR
11714 /* If there is a location for the static link, record it. */
11715 newobj->static_link = NULL;
11716 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11717 if (attr)
11718 {
224c3ddb
SM
11719 newobj->static_link
11720 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11721 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11722 }
11723
e142c38c 11724 cu->list_in_scope = &local_symbols;
c906108c 11725
639d11d3 11726 if (die->child != NULL)
c906108c 11727 {
639d11d3 11728 child_die = die->child;
c906108c
SS
11729 while (child_die && child_die->tag)
11730 {
34eaf542
TT
11731 if (child_die->tag == DW_TAG_template_type_param
11732 || child_die->tag == DW_TAG_template_value_param)
11733 {
11734 struct symbol *arg = new_symbol (child_die, NULL, cu);
11735
f1078f66
DJ
11736 if (arg != NULL)
11737 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11738 }
11739 else
11740 process_die (child_die, cu);
c906108c
SS
11741 child_die = sibling_die (child_die);
11742 }
11743 }
11744
d389af10
JK
11745 inherit_abstract_dies (die, cu);
11746
4a811a97
UW
11747 /* If we have a DW_AT_specification, we might need to import using
11748 directives from the context of the specification DIE. See the
11749 comment in determine_prefix. */
11750 if (cu->language == language_cplus
11751 && dwarf2_attr (die, DW_AT_specification, cu))
11752 {
11753 struct dwarf2_cu *spec_cu = cu;
11754 struct die_info *spec_die = die_specification (die, &spec_cu);
11755
11756 while (spec_die)
11757 {
11758 child_die = spec_die->child;
11759 while (child_die && child_die->tag)
11760 {
11761 if (child_die->tag == DW_TAG_imported_module)
11762 process_die (child_die, spec_cu);
11763 child_die = sibling_die (child_die);
11764 }
11765
11766 /* In some cases, GCC generates specification DIEs that
11767 themselves contain DW_AT_specification attributes. */
11768 spec_die = die_specification (spec_die, &spec_cu);
11769 }
11770 }
11771
fe978cb0 11772 newobj = pop_context ();
c906108c 11773 /* Make a block for the local symbols within. */
fe978cb0 11774 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11775 newobj->static_link, lowpc, highpc);
801e3a5b 11776
df8a16a1 11777 /* For C++, set the block's scope. */
45280282
IB
11778 if ((cu->language == language_cplus
11779 || cu->language == language_fortran
c44af4eb
TT
11780 || cu->language == language_d
11781 || cu->language == language_rust)
4d4ec4e5 11782 && cu->processing_has_namespace_info)
195a3f6c
TT
11783 block_set_scope (block, determine_prefix (die, cu),
11784 &objfile->objfile_obstack);
df8a16a1 11785
801e3a5b
JB
11786 /* If we have address ranges, record them. */
11787 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11788
fe978cb0 11789 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11790
34eaf542
TT
11791 /* Attach template arguments to function. */
11792 if (! VEC_empty (symbolp, template_args))
11793 {
11794 gdb_assert (templ_func != NULL);
11795
11796 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11797 templ_func->template_arguments
8d749320
SM
11798 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11799 templ_func->n_template_arguments);
34eaf542
TT
11800 memcpy (templ_func->template_arguments,
11801 VEC_address (symbolp, template_args),
11802 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11803 VEC_free (symbolp, template_args);
11804 }
11805
208d8187
JB
11806 /* In C++, we can have functions nested inside functions (e.g., when
11807 a function declares a class that has methods). This means that
11808 when we finish processing a function scope, we may need to go
11809 back to building a containing block's symbol lists. */
fe978cb0 11810 local_symbols = newobj->locals;
22cee43f 11811 local_using_directives = newobj->local_using_directives;
208d8187 11812
921e78cf
JB
11813 /* If we've finished processing a top-level function, subsequent
11814 symbols go in the file symbol list. */
11815 if (outermost_context_p ())
e142c38c 11816 cu->list_in_scope = &file_symbols;
c906108c
SS
11817}
11818
11819/* Process all the DIES contained within a lexical block scope. Start
11820 a new scope, process the dies, and then close the scope. */
11821
11822static void
e7c27a73 11823read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11824{
e7c27a73 11825 struct objfile *objfile = cu->objfile;
3e29f34a 11826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11827 struct context_stack *newobj;
c906108c
SS
11828 CORE_ADDR lowpc, highpc;
11829 struct die_info *child_die;
e142c38c
DJ
11830 CORE_ADDR baseaddr;
11831
11832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11833
11834 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11835 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11836 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11837 be nasty. Might be easier to properly extend generic blocks to
af34e669 11838 describe ranges. */
e385593e
JK
11839 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11840 {
11841 case PC_BOUNDS_NOT_PRESENT:
11842 /* DW_TAG_lexical_block has no attributes, process its children as if
11843 there was no wrapping by that DW_TAG_lexical_block.
11844 GCC does no longer produces such DWARF since GCC r224161. */
11845 for (child_die = die->child;
11846 child_die != NULL && child_die->tag;
11847 child_die = sibling_die (child_die))
11848 process_die (child_die, cu);
11849 return;
11850 case PC_BOUNDS_INVALID:
11851 return;
11852 }
3e29f34a
MR
11853 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11854 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11855
11856 push_context (0, lowpc);
639d11d3 11857 if (die->child != NULL)
c906108c 11858 {
639d11d3 11859 child_die = die->child;
c906108c
SS
11860 while (child_die && child_die->tag)
11861 {
e7c27a73 11862 process_die (child_die, cu);
c906108c
SS
11863 child_die = sibling_die (child_die);
11864 }
11865 }
3ea89b92 11866 inherit_abstract_dies (die, cu);
fe978cb0 11867 newobj = pop_context ();
c906108c 11868
22cee43f 11869 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11870 {
801e3a5b 11871 struct block *block
63e43d3a 11872 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11873 newobj->start_addr, highpc);
801e3a5b
JB
11874
11875 /* Note that recording ranges after traversing children, as we
11876 do here, means that recording a parent's ranges entails
11877 walking across all its children's ranges as they appear in
11878 the address map, which is quadratic behavior.
11879
11880 It would be nicer to record the parent's ranges before
11881 traversing its children, simply overriding whatever you find
11882 there. But since we don't even decide whether to create a
11883 block until after we've traversed its children, that's hard
11884 to do. */
11885 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11886 }
fe978cb0 11887 local_symbols = newobj->locals;
22cee43f 11888 local_using_directives = newobj->local_using_directives;
c906108c
SS
11889}
11890
216f72a1 11891/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11892
11893static void
11894read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11895{
11896 struct objfile *objfile = cu->objfile;
11897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11898 CORE_ADDR pc, baseaddr;
11899 struct attribute *attr;
11900 struct call_site *call_site, call_site_local;
11901 void **slot;
11902 int nparams;
11903 struct die_info *child_die;
11904
11905 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11906
216f72a1
JK
11907 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11908 if (attr == NULL)
11909 {
11910 /* This was a pre-DWARF-5 GNU extension alias
11911 for DW_AT_call_return_pc. */
11912 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11913 }
96408a79
SA
11914 if (!attr)
11915 {
11916 complaint (&symfile_complaints,
216f72a1 11917 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11918 "DIE 0x%x [in module %s]"),
9c541725 11919 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11920 return;
11921 }
31aa7e4e 11922 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11923 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11924
11925 if (cu->call_site_htab == NULL)
11926 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11927 NULL, &objfile->objfile_obstack,
11928 hashtab_obstack_allocate, NULL);
11929 call_site_local.pc = pc;
11930 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11931 if (*slot != NULL)
11932 {
11933 complaint (&symfile_complaints,
216f72a1 11934 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11935 "DIE 0x%x [in module %s]"),
9c541725 11936 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11937 objfile_name (objfile));
96408a79
SA
11938 return;
11939 }
11940
11941 /* Count parameters at the caller. */
11942
11943 nparams = 0;
11944 for (child_die = die->child; child_die && child_die->tag;
11945 child_die = sibling_die (child_die))
11946 {
216f72a1
JK
11947 if (child_die->tag != DW_TAG_call_site_parameter
11948 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11949 {
11950 complaint (&symfile_complaints,
216f72a1
JK
11951 _("Tag %d is not DW_TAG_call_site_parameter in "
11952 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11953 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11954 objfile_name (objfile));
96408a79
SA
11955 continue;
11956 }
11957
11958 nparams++;
11959 }
11960
224c3ddb
SM
11961 call_site
11962 = ((struct call_site *)
11963 obstack_alloc (&objfile->objfile_obstack,
11964 sizeof (*call_site)
11965 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11966 *slot = call_site;
11967 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11968 call_site->pc = pc;
11969
216f72a1
JK
11970 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11971 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11972 {
11973 struct die_info *func_die;
11974
11975 /* Skip also over DW_TAG_inlined_subroutine. */
11976 for (func_die = die->parent;
11977 func_die && func_die->tag != DW_TAG_subprogram
11978 && func_die->tag != DW_TAG_subroutine_type;
11979 func_die = func_die->parent);
11980
216f72a1
JK
11981 /* DW_AT_call_all_calls is a superset
11982 of DW_AT_call_all_tail_calls. */
96408a79 11983 if (func_die
216f72a1 11984 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11985 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11986 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11987 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11988 {
11989 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11990 not complete. But keep CALL_SITE for look ups via call_site_htab,
11991 both the initial caller containing the real return address PC and
11992 the final callee containing the current PC of a chain of tail
11993 calls do not need to have the tail call list complete. But any
11994 function candidate for a virtual tail call frame searched via
11995 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11996 determined unambiguously. */
11997 }
11998 else
11999 {
12000 struct type *func_type = NULL;
12001
12002 if (func_die)
12003 func_type = get_die_type (func_die, cu);
12004 if (func_type != NULL)
12005 {
12006 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12007
12008 /* Enlist this call site to the function. */
12009 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12010 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12011 }
12012 else
12013 complaint (&symfile_complaints,
216f72a1 12014 _("Cannot find function owning DW_TAG_call_site "
96408a79 12015 "DIE 0x%x [in module %s]"),
9c541725 12016 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12017 }
12018 }
12019
216f72a1
JK
12020 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12021 if (attr == NULL)
12022 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12023 if (attr == NULL)
12024 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12025 if (attr == NULL)
216f72a1
JK
12026 {
12027 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12028 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12029 }
96408a79
SA
12030 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12031 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12032 /* Keep NULL DWARF_BLOCK. */;
12033 else if (attr_form_is_block (attr))
12034 {
12035 struct dwarf2_locexpr_baton *dlbaton;
12036
8d749320 12037 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12038 dlbaton->data = DW_BLOCK (attr)->data;
12039 dlbaton->size = DW_BLOCK (attr)->size;
12040 dlbaton->per_cu = cu->per_cu;
12041
12042 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12043 }
7771576e 12044 else if (attr_form_is_ref (attr))
96408a79 12045 {
96408a79
SA
12046 struct dwarf2_cu *target_cu = cu;
12047 struct die_info *target_die;
12048
ac9ec31b 12049 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12050 gdb_assert (target_cu->objfile == objfile);
12051 if (die_is_declaration (target_die, target_cu))
12052 {
7d45c7c3 12053 const char *target_physname;
9112db09
JK
12054
12055 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
12056 target_physname = dwarf2_string_attr (target_die,
12057 DW_AT_linkage_name,
12058 target_cu);
12059 if (target_physname == NULL)
12060 target_physname = dwarf2_string_attr (target_die,
12061 DW_AT_MIPS_linkage_name,
12062 target_cu);
12063 if (target_physname == NULL)
9112db09 12064 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12065 if (target_physname == NULL)
12066 complaint (&symfile_complaints,
216f72a1 12067 _("DW_AT_call_target target DIE has invalid "
96408a79 12068 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12069 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12070 else
7d455152 12071 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12072 }
12073 else
12074 {
12075 CORE_ADDR lowpc;
12076
12077 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12078 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12079 <= PC_BOUNDS_INVALID)
96408a79 12080 complaint (&symfile_complaints,
216f72a1 12081 _("DW_AT_call_target target DIE has invalid "
96408a79 12082 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12083 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12084 else
3e29f34a
MR
12085 {
12086 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12087 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12088 }
96408a79
SA
12089 }
12090 }
12091 else
12092 complaint (&symfile_complaints,
216f72a1 12093 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12094 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12095 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12096
12097 call_site->per_cu = cu->per_cu;
12098
12099 for (child_die = die->child;
12100 child_die && child_die->tag;
12101 child_die = sibling_die (child_die))
12102 {
96408a79 12103 struct call_site_parameter *parameter;
1788b2d3 12104 struct attribute *loc, *origin;
96408a79 12105
216f72a1
JK
12106 if (child_die->tag != DW_TAG_call_site_parameter
12107 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12108 {
12109 /* Already printed the complaint above. */
12110 continue;
12111 }
12112
12113 gdb_assert (call_site->parameter_count < nparams);
12114 parameter = &call_site->parameter[call_site->parameter_count];
12115
1788b2d3
JK
12116 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12117 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12118 register is contained in DW_AT_call_value. */
96408a79 12119
24c5c679 12120 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12121 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12122 if (origin == NULL)
12123 {
12124 /* This was a pre-DWARF-5 GNU extension alias
12125 for DW_AT_call_parameter. */
12126 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12127 }
7771576e 12128 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12129 {
1788b2d3 12130 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12131
12132 sect_offset sect_off
12133 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12134 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12135 {
12136 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12137 binding can be done only inside one CU. Such referenced DIE
12138 therefore cannot be even moved to DW_TAG_partial_unit. */
12139 complaint (&symfile_complaints,
216f72a1
JK
12140 _("DW_AT_call_parameter offset is not in CU for "
12141 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12142 to_underlying (child_die->sect_off),
12143 objfile_name (objfile));
d76b7dbc
JK
12144 continue;
12145 }
9c541725
PA
12146 parameter->u.param_cu_off
12147 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12148 }
12149 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12150 {
12151 complaint (&symfile_complaints,
12152 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12153 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12154 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12155 continue;
12156 }
24c5c679 12157 else
96408a79 12158 {
24c5c679
JK
12159 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12160 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12161 if (parameter->u.dwarf_reg != -1)
12162 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12163 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12164 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12165 &parameter->u.fb_offset))
12166 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12167 else
12168 {
12169 complaint (&symfile_complaints,
12170 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12171 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12172 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12173 "[in module %s]"),
9c541725
PA
12174 to_underlying (child_die->sect_off),
12175 objfile_name (objfile));
24c5c679
JK
12176 continue;
12177 }
96408a79
SA
12178 }
12179
216f72a1
JK
12180 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12181 if (attr == NULL)
12182 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12183 if (!attr_form_is_block (attr))
12184 {
12185 complaint (&symfile_complaints,
216f72a1
JK
12186 _("No DW_FORM_block* DW_AT_call_value for "
12187 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12188 to_underlying (child_die->sect_off),
12189 objfile_name (objfile));
96408a79
SA
12190 continue;
12191 }
12192 parameter->value = DW_BLOCK (attr)->data;
12193 parameter->value_size = DW_BLOCK (attr)->size;
12194
12195 /* Parameters are not pre-cleared by memset above. */
12196 parameter->data_value = NULL;
12197 parameter->data_value_size = 0;
12198 call_site->parameter_count++;
12199
216f72a1
JK
12200 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12201 if (attr == NULL)
12202 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12203 if (attr)
12204 {
12205 if (!attr_form_is_block (attr))
12206 complaint (&symfile_complaints,
216f72a1
JK
12207 _("No DW_FORM_block* DW_AT_call_data_value for "
12208 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12209 to_underlying (child_die->sect_off),
12210 objfile_name (objfile));
96408a79
SA
12211 else
12212 {
12213 parameter->data_value = DW_BLOCK (attr)->data;
12214 parameter->data_value_size = DW_BLOCK (attr)->size;
12215 }
12216 }
12217 }
12218}
12219
43988095
JK
12220/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12221 reading .debug_rnglists.
12222 Callback's type should be:
12223 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12224 Return true if the attributes are present and valid, otherwise,
12225 return false. */
12226
12227template <typename Callback>
12228static bool
12229dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12230 Callback &&callback)
12231{
12232 struct objfile *objfile = cu->objfile;
12233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12234 struct comp_unit_head *cu_header = &cu->header;
12235 bfd *obfd = objfile->obfd;
12236 unsigned int addr_size = cu_header->addr_size;
12237 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12238 /* Base address selection entry. */
12239 CORE_ADDR base;
12240 int found_base;
12241 unsigned int dummy;
12242 const gdb_byte *buffer;
12243 CORE_ADDR low = 0;
12244 CORE_ADDR high = 0;
12245 CORE_ADDR baseaddr;
12246 bool overflow = false;
12247
12248 found_base = cu->base_known;
12249 base = cu->base_address;
12250
12251 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12252 if (offset >= dwarf2_per_objfile->rnglists.size)
12253 {
12254 complaint (&symfile_complaints,
12255 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12256 offset);
12257 return false;
12258 }
12259 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12260
12261 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12262
12263 while (1)
12264 {
7814882a
JK
12265 /* Initialize it due to a false compiler warning. */
12266 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12267 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12268 + dwarf2_per_objfile->rnglists.size);
12269 unsigned int bytes_read;
12270
12271 if (buffer == buf_end)
12272 {
12273 overflow = true;
12274 break;
12275 }
12276 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12277 switch (rlet)
12278 {
12279 case DW_RLE_end_of_list:
12280 break;
12281 case DW_RLE_base_address:
12282 if (buffer + cu->header.addr_size > buf_end)
12283 {
12284 overflow = true;
12285 break;
12286 }
12287 base = read_address (obfd, buffer, cu, &bytes_read);
12288 found_base = 1;
12289 buffer += bytes_read;
12290 break;
12291 case DW_RLE_start_length:
12292 if (buffer + 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 = (range_beginning
12300 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12301 buffer += bytes_read;
12302 if (buffer > buf_end)
12303 {
12304 overflow = true;
12305 break;
12306 }
12307 break;
12308 case DW_RLE_offset_pair:
12309 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12310 buffer += bytes_read;
12311 if (buffer > buf_end)
12312 {
12313 overflow = true;
12314 break;
12315 }
12316 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12317 buffer += bytes_read;
12318 if (buffer > buf_end)
12319 {
12320 overflow = true;
12321 break;
12322 }
12323 break;
12324 case DW_RLE_start_end:
12325 if (buffer + 2 * cu->header.addr_size > buf_end)
12326 {
12327 overflow = true;
12328 break;
12329 }
12330 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12331 buffer += bytes_read;
12332 range_end = read_address (obfd, buffer, cu, &bytes_read);
12333 buffer += bytes_read;
12334 break;
12335 default:
12336 complaint (&symfile_complaints,
12337 _("Invalid .debug_rnglists data (no base address)"));
12338 return false;
12339 }
12340 if (rlet == DW_RLE_end_of_list || overflow)
12341 break;
12342 if (rlet == DW_RLE_base_address)
12343 continue;
12344
12345 if (!found_base)
12346 {
12347 /* We have no valid base address for the ranges
12348 data. */
12349 complaint (&symfile_complaints,
12350 _("Invalid .debug_rnglists data (no base address)"));
12351 return false;
12352 }
12353
12354 if (range_beginning > range_end)
12355 {
12356 /* Inverted range entries are invalid. */
12357 complaint (&symfile_complaints,
12358 _("Invalid .debug_rnglists data (inverted range)"));
12359 return false;
12360 }
12361
12362 /* Empty range entries have no effect. */
12363 if (range_beginning == range_end)
12364 continue;
12365
12366 range_beginning += base;
12367 range_end += base;
12368
12369 /* A not-uncommon case of bad debug info.
12370 Don't pollute the addrmap with bad data. */
12371 if (range_beginning + baseaddr == 0
12372 && !dwarf2_per_objfile->has_section_at_zero)
12373 {
12374 complaint (&symfile_complaints,
12375 _(".debug_rnglists entry has start address of zero"
12376 " [in module %s]"), objfile_name (objfile));
12377 continue;
12378 }
12379
12380 callback (range_beginning, range_end);
12381 }
12382
12383 if (overflow)
12384 {
12385 complaint (&symfile_complaints,
12386 _("Offset %d is not terminated "
12387 "for DW_AT_ranges attribute"),
12388 offset);
12389 return false;
12390 }
12391
12392 return true;
12393}
12394
12395/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12396 Callback's type should be:
12397 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12398 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12399
43988095 12400template <typename Callback>
43039443 12401static int
5f46c5a5 12402dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12403 Callback &&callback)
43039443
JK
12404{
12405 struct objfile *objfile = cu->objfile;
3e29f34a 12406 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12407 struct comp_unit_head *cu_header = &cu->header;
12408 bfd *obfd = objfile->obfd;
12409 unsigned int addr_size = cu_header->addr_size;
12410 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12411 /* Base address selection entry. */
12412 CORE_ADDR base;
12413 int found_base;
12414 unsigned int dummy;
d521ce57 12415 const gdb_byte *buffer;
ff013f42 12416 CORE_ADDR baseaddr;
43039443 12417
43988095
JK
12418 if (cu_header->version >= 5)
12419 return dwarf2_rnglists_process (offset, cu, callback);
12420
d00adf39
DE
12421 found_base = cu->base_known;
12422 base = cu->base_address;
43039443 12423
be391dca 12424 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12425 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12426 {
12427 complaint (&symfile_complaints,
12428 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12429 offset);
12430 return 0;
12431 }
dce234bc 12432 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12433
e7030f15 12434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12435
43039443
JK
12436 while (1)
12437 {
12438 CORE_ADDR range_beginning, range_end;
12439
12440 range_beginning = read_address (obfd, buffer, cu, &dummy);
12441 buffer += addr_size;
12442 range_end = read_address (obfd, buffer, cu, &dummy);
12443 buffer += addr_size;
12444 offset += 2 * addr_size;
12445
12446 /* An end of list marker is a pair of zero addresses. */
12447 if (range_beginning == 0 && range_end == 0)
12448 /* Found the end of list entry. */
12449 break;
12450
12451 /* Each base address selection entry is a pair of 2 values.
12452 The first is the largest possible address, the second is
12453 the base address. Check for a base address here. */
12454 if ((range_beginning & mask) == mask)
12455 {
28d2bfb9
AB
12456 /* If we found the largest possible address, then we already
12457 have the base address in range_end. */
12458 base = range_end;
43039443
JK
12459 found_base = 1;
12460 continue;
12461 }
12462
12463 if (!found_base)
12464 {
12465 /* We have no valid base address for the ranges
12466 data. */
12467 complaint (&symfile_complaints,
12468 _("Invalid .debug_ranges data (no base address)"));
12469 return 0;
12470 }
12471
9277c30c
UW
12472 if (range_beginning > range_end)
12473 {
12474 /* Inverted range entries are invalid. */
12475 complaint (&symfile_complaints,
12476 _("Invalid .debug_ranges data (inverted range)"));
12477 return 0;
12478 }
12479
12480 /* Empty range entries have no effect. */
12481 if (range_beginning == range_end)
12482 continue;
12483
43039443
JK
12484 range_beginning += base;
12485 range_end += base;
12486
01093045
DE
12487 /* A not-uncommon case of bad debug info.
12488 Don't pollute the addrmap with bad data. */
12489 if (range_beginning + baseaddr == 0
12490 && !dwarf2_per_objfile->has_section_at_zero)
12491 {
12492 complaint (&symfile_complaints,
12493 _(".debug_ranges entry has start address of zero"
4262abfb 12494 " [in module %s]"), objfile_name (objfile));
01093045
DE
12495 continue;
12496 }
12497
5f46c5a5
JK
12498 callback (range_beginning, range_end);
12499 }
12500
12501 return 1;
12502}
12503
12504/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12505 Return 1 if the attributes are present and valid, otherwise, return 0.
12506 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12507
12508static int
12509dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12510 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12511 struct partial_symtab *ranges_pst)
12512{
12513 struct objfile *objfile = cu->objfile;
12514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12515 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12516 SECT_OFF_TEXT (objfile));
12517 int low_set = 0;
12518 CORE_ADDR low = 0;
12519 CORE_ADDR high = 0;
12520 int retval;
12521
12522 retval = dwarf2_ranges_process (offset, cu,
12523 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12524 {
9277c30c 12525 if (ranges_pst != NULL)
3e29f34a
MR
12526 {
12527 CORE_ADDR lowpc;
12528 CORE_ADDR highpc;
12529
12530 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12531 range_beginning + baseaddr);
12532 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12533 range_end + baseaddr);
12534 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12535 ranges_pst);
12536 }
ff013f42 12537
43039443
JK
12538 /* FIXME: This is recording everything as a low-high
12539 segment of consecutive addresses. We should have a
12540 data structure for discontiguous block ranges
12541 instead. */
12542 if (! low_set)
12543 {
12544 low = range_beginning;
12545 high = range_end;
12546 low_set = 1;
12547 }
12548 else
12549 {
12550 if (range_beginning < low)
12551 low = range_beginning;
12552 if (range_end > high)
12553 high = range_end;
12554 }
5f46c5a5
JK
12555 });
12556 if (!retval)
12557 return 0;
43039443
JK
12558
12559 if (! low_set)
12560 /* If the first entry is an end-of-list marker, the range
12561 describes an empty scope, i.e. no instructions. */
12562 return 0;
12563
12564 if (low_return)
12565 *low_return = low;
12566 if (high_return)
12567 *high_return = high;
12568 return 1;
12569}
12570
3a2b436a
JK
12571/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12572 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12573 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12574
3a2b436a 12575static enum pc_bounds_kind
af34e669 12576dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12577 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12578 struct partial_symtab *pst)
c906108c
SS
12579{
12580 struct attribute *attr;
91da1414 12581 struct attribute *attr_high;
af34e669
DJ
12582 CORE_ADDR low = 0;
12583 CORE_ADDR high = 0;
e385593e 12584 enum pc_bounds_kind ret;
c906108c 12585
91da1414
MW
12586 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12587 if (attr_high)
af34e669 12588 {
e142c38c 12589 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12590 if (attr)
91da1414 12591 {
31aa7e4e
JB
12592 low = attr_value_as_address (attr);
12593 high = attr_value_as_address (attr_high);
12594 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12595 high += low;
91da1414 12596 }
af34e669
DJ
12597 else
12598 /* Found high w/o low attribute. */
e385593e 12599 return PC_BOUNDS_INVALID;
af34e669
DJ
12600
12601 /* Found consecutive range of addresses. */
3a2b436a 12602 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12603 }
c906108c 12604 else
af34e669 12605 {
e142c38c 12606 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12607 if (attr != NULL)
12608 {
ab435259
DE
12609 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12610 We take advantage of the fact that DW_AT_ranges does not appear
12611 in DW_TAG_compile_unit of DWO files. */
12612 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12613 unsigned int ranges_offset = (DW_UNSND (attr)
12614 + (need_ranges_base
12615 ? cu->ranges_base
12616 : 0));
2e3cf129 12617
af34e669 12618 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12619 .debug_ranges section. */
2e3cf129 12620 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12621 return PC_BOUNDS_INVALID;
43039443 12622 /* Found discontinuous range of addresses. */
3a2b436a 12623 ret = PC_BOUNDS_RANGES;
af34e669 12624 }
e385593e
JK
12625 else
12626 return PC_BOUNDS_NOT_PRESENT;
af34e669 12627 }
c906108c 12628
9373cf26
JK
12629 /* read_partial_die has also the strict LOW < HIGH requirement. */
12630 if (high <= low)
e385593e 12631 return PC_BOUNDS_INVALID;
c906108c
SS
12632
12633 /* When using the GNU linker, .gnu.linkonce. sections are used to
12634 eliminate duplicate copies of functions and vtables and such.
12635 The linker will arbitrarily choose one and discard the others.
12636 The AT_*_pc values for such functions refer to local labels in
12637 these sections. If the section from that file was discarded, the
12638 labels are not in the output, so the relocs get a value of 0.
12639 If this is a discarded function, mark the pc bounds as invalid,
12640 so that GDB will ignore it. */
72dca2f5 12641 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12642 return PC_BOUNDS_INVALID;
c906108c
SS
12643
12644 *lowpc = low;
96408a79
SA
12645 if (highpc)
12646 *highpc = high;
af34e669 12647 return ret;
c906108c
SS
12648}
12649
b084d499
JB
12650/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12651 its low and high PC addresses. Do nothing if these addresses could not
12652 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12653 and HIGHPC to the high address if greater than HIGHPC. */
12654
12655static void
12656dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12657 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12658 struct dwarf2_cu *cu)
12659{
12660 CORE_ADDR low, high;
12661 struct die_info *child = die->child;
12662
e385593e 12663 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12664 {
325fac50
PA
12665 *lowpc = std::min (*lowpc, low);
12666 *highpc = std::max (*highpc, high);
b084d499
JB
12667 }
12668
12669 /* If the language does not allow nested subprograms (either inside
12670 subprograms or lexical blocks), we're done. */
12671 if (cu->language != language_ada)
12672 return;
6e70227d 12673
b084d499
JB
12674 /* Check all the children of the given DIE. If it contains nested
12675 subprograms, then check their pc bounds. Likewise, we need to
12676 check lexical blocks as well, as they may also contain subprogram
12677 definitions. */
12678 while (child && child->tag)
12679 {
12680 if (child->tag == DW_TAG_subprogram
12681 || child->tag == DW_TAG_lexical_block)
12682 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12683 child = sibling_die (child);
12684 }
12685}
12686
fae299cd
DC
12687/* Get the low and high pc's represented by the scope DIE, and store
12688 them in *LOWPC and *HIGHPC. If the correct values can't be
12689 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12690
12691static void
12692get_scope_pc_bounds (struct die_info *die,
12693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12694 struct dwarf2_cu *cu)
12695{
12696 CORE_ADDR best_low = (CORE_ADDR) -1;
12697 CORE_ADDR best_high = (CORE_ADDR) 0;
12698 CORE_ADDR current_low, current_high;
12699
3a2b436a 12700 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12701 >= PC_BOUNDS_RANGES)
fae299cd
DC
12702 {
12703 best_low = current_low;
12704 best_high = current_high;
12705 }
12706 else
12707 {
12708 struct die_info *child = die->child;
12709
12710 while (child && child->tag)
12711 {
12712 switch (child->tag) {
12713 case DW_TAG_subprogram:
b084d499 12714 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12715 break;
12716 case DW_TAG_namespace:
f55ee35c 12717 case DW_TAG_module:
fae299cd
DC
12718 /* FIXME: carlton/2004-01-16: Should we do this for
12719 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12720 that current GCC's always emit the DIEs corresponding
12721 to definitions of methods of classes as children of a
12722 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12723 the DIEs giving the declarations, which could be
12724 anywhere). But I don't see any reason why the
12725 standards says that they have to be there. */
12726 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12727
12728 if (current_low != ((CORE_ADDR) -1))
12729 {
325fac50
PA
12730 best_low = std::min (best_low, current_low);
12731 best_high = std::max (best_high, current_high);
fae299cd
DC
12732 }
12733 break;
12734 default:
0963b4bd 12735 /* Ignore. */
fae299cd
DC
12736 break;
12737 }
12738
12739 child = sibling_die (child);
12740 }
12741 }
12742
12743 *lowpc = best_low;
12744 *highpc = best_high;
12745}
12746
801e3a5b
JB
12747/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12748 in DIE. */
380bca97 12749
801e3a5b
JB
12750static void
12751dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12752 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12753{
bb5ed363 12754 struct objfile *objfile = cu->objfile;
3e29f34a 12755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12756 struct attribute *attr;
91da1414 12757 struct attribute *attr_high;
801e3a5b 12758
91da1414
MW
12759 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12760 if (attr_high)
801e3a5b 12761 {
801e3a5b
JB
12762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12763 if (attr)
12764 {
31aa7e4e
JB
12765 CORE_ADDR low = attr_value_as_address (attr);
12766 CORE_ADDR high = attr_value_as_address (attr_high);
12767
12768 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12769 high += low;
9a619af0 12770
3e29f34a
MR
12771 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12772 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12773 record_block_range (block, low, high - 1);
801e3a5b
JB
12774 }
12775 }
12776
12777 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12778 if (attr)
12779 {
bb5ed363 12780 bfd *obfd = objfile->obfd;
ab435259
DE
12781 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12782 We take advantage of the fact that DW_AT_ranges does not appear
12783 in DW_TAG_compile_unit of DWO files. */
12784 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12785
12786 /* The value of the DW_AT_ranges attribute is the offset of the
12787 address range list in the .debug_ranges section. */
ab435259
DE
12788 unsigned long offset = (DW_UNSND (attr)
12789 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12790 const gdb_byte *buffer;
801e3a5b
JB
12791
12792 /* For some target architectures, but not others, the
12793 read_address function sign-extends the addresses it returns.
12794 To recognize base address selection entries, we need a
12795 mask. */
12796 unsigned int addr_size = cu->header.addr_size;
12797 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12798
12799 /* The base address, to which the next pair is relative. Note
12800 that this 'base' is a DWARF concept: most entries in a range
12801 list are relative, to reduce the number of relocs against the
12802 debugging information. This is separate from this function's
12803 'baseaddr' argument, which GDB uses to relocate debugging
12804 information from a shared library based on the address at
12805 which the library was loaded. */
d00adf39
DE
12806 CORE_ADDR base = cu->base_address;
12807 int base_known = cu->base_known;
801e3a5b 12808
5f46c5a5
JK
12809 dwarf2_ranges_process (offset, cu,
12810 [&] (CORE_ADDR start, CORE_ADDR end)
12811 {
58fdfd2c
JK
12812 start += baseaddr;
12813 end += baseaddr;
5f46c5a5
JK
12814 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12815 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12816 record_block_range (block, start, end - 1);
12817 });
801e3a5b
JB
12818 }
12819}
12820
685b1105
JK
12821/* Check whether the producer field indicates either of GCC < 4.6, or the
12822 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12823
685b1105
JK
12824static void
12825check_producer (struct dwarf2_cu *cu)
60d5a603 12826{
38360086 12827 int major, minor;
60d5a603
JK
12828
12829 if (cu->producer == NULL)
12830 {
12831 /* For unknown compilers expect their behavior is DWARF version
12832 compliant.
12833
12834 GCC started to support .debug_types sections by -gdwarf-4 since
12835 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12836 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12837 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12838 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12839 }
b1ffba5a 12840 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12841 {
38360086
MW
12842 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12843 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12844 }
61012eef 12845 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12846 cu->producer_is_icc = 1;
12847 else
12848 {
12849 /* For other non-GCC compilers, expect their behavior is DWARF version
12850 compliant. */
60d5a603
JK
12851 }
12852
ba919b58 12853 cu->checked_producer = 1;
685b1105 12854}
ba919b58 12855
685b1105
JK
12856/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12857 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12858 during 4.6.0 experimental. */
12859
12860static int
12861producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12862{
12863 if (!cu->checked_producer)
12864 check_producer (cu);
12865
12866 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12867}
12868
12869/* Return the default accessibility type if it is not overriden by
12870 DW_AT_accessibility. */
12871
12872static enum dwarf_access_attribute
12873dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12874{
12875 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12876 {
12877 /* The default DWARF 2 accessibility for members is public, the default
12878 accessibility for inheritance is private. */
12879
12880 if (die->tag != DW_TAG_inheritance)
12881 return DW_ACCESS_public;
12882 else
12883 return DW_ACCESS_private;
12884 }
12885 else
12886 {
12887 /* DWARF 3+ defines the default accessibility a different way. The same
12888 rules apply now for DW_TAG_inheritance as for the members and it only
12889 depends on the container kind. */
12890
12891 if (die->parent->tag == DW_TAG_class_type)
12892 return DW_ACCESS_private;
12893 else
12894 return DW_ACCESS_public;
12895 }
12896}
12897
74ac6d43
TT
12898/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12899 offset. If the attribute was not found return 0, otherwise return
12900 1. If it was found but could not properly be handled, set *OFFSET
12901 to 0. */
12902
12903static int
12904handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12905 LONGEST *offset)
12906{
12907 struct attribute *attr;
12908
12909 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12910 if (attr != NULL)
12911 {
12912 *offset = 0;
12913
12914 /* Note that we do not check for a section offset first here.
12915 This is because DW_AT_data_member_location is new in DWARF 4,
12916 so if we see it, we can assume that a constant form is really
12917 a constant and not a section offset. */
12918 if (attr_form_is_constant (attr))
12919 *offset = dwarf2_get_attr_constant_value (attr, 0);
12920 else if (attr_form_is_section_offset (attr))
12921 dwarf2_complex_location_expr_complaint ();
12922 else if (attr_form_is_block (attr))
12923 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12924 else
12925 dwarf2_complex_location_expr_complaint ();
12926
12927 return 1;
12928 }
12929
12930 return 0;
12931}
12932
c906108c
SS
12933/* Add an aggregate field to the field list. */
12934
12935static void
107d2387 12936dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12937 struct dwarf2_cu *cu)
6e70227d 12938{
e7c27a73 12939 struct objfile *objfile = cu->objfile;
5e2b427d 12940 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12941 struct nextfield *new_field;
12942 struct attribute *attr;
12943 struct field *fp;
15d034d0 12944 const char *fieldname = "";
c906108c
SS
12945
12946 /* Allocate a new field list entry and link it in. */
8d749320 12947 new_field = XNEW (struct nextfield);
b8c9b27d 12948 make_cleanup (xfree, new_field);
c906108c 12949 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12950
12951 if (die->tag == DW_TAG_inheritance)
12952 {
12953 new_field->next = fip->baseclasses;
12954 fip->baseclasses = new_field;
12955 }
12956 else
12957 {
12958 new_field->next = fip->fields;
12959 fip->fields = new_field;
12960 }
c906108c
SS
12961 fip->nfields++;
12962
e142c38c 12963 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12964 if (attr)
12965 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12966 else
12967 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12968 if (new_field->accessibility != DW_ACCESS_public)
12969 fip->non_public_fields = 1;
60d5a603 12970
e142c38c 12971 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12972 if (attr)
12973 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12974 else
12975 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12976
12977 fp = &new_field->field;
a9a9bd0f 12978
e142c38c 12979 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12980 {
74ac6d43
TT
12981 LONGEST offset;
12982
a9a9bd0f 12983 /* Data member other than a C++ static data member. */
6e70227d 12984
c906108c 12985 /* Get type of field. */
e7c27a73 12986 fp->type = die_type (die, cu);
c906108c 12987
d6a843b5 12988 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12989
c906108c 12990 /* Get bit size of field (zero if none). */
e142c38c 12991 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12992 if (attr)
12993 {
12994 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12995 }
12996 else
12997 {
12998 FIELD_BITSIZE (*fp) = 0;
12999 }
13000
13001 /* Get bit offset of field. */
74ac6d43
TT
13002 if (handle_data_member_location (die, cu, &offset))
13003 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13004 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13005 if (attr)
13006 {
5e2b427d 13007 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13008 {
13009 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13010 additional bit offset from the MSB of the containing
13011 anonymous object to the MSB of the field. We don't
13012 have to do anything special since we don't need to
13013 know the size of the anonymous object. */
f41f5e61 13014 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13015 }
13016 else
13017 {
13018 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13019 MSB of the anonymous object, subtract off the number of
13020 bits from the MSB of the field to the MSB of the
13021 object, and then subtract off the number of bits of
13022 the field itself. The result is the bit offset of
13023 the LSB of the field. */
c906108c
SS
13024 int anonymous_size;
13025 int bit_offset = DW_UNSND (attr);
13026
e142c38c 13027 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13028 if (attr)
13029 {
13030 /* The size of the anonymous object containing
13031 the bit field is explicit, so use the
13032 indicated size (in bytes). */
13033 anonymous_size = DW_UNSND (attr);
13034 }
13035 else
13036 {
13037 /* The size of the anonymous object containing
13038 the bit field must be inferred from the type
13039 attribute of the data member containing the
13040 bit field. */
13041 anonymous_size = TYPE_LENGTH (fp->type);
13042 }
f41f5e61
PA
13043 SET_FIELD_BITPOS (*fp,
13044 (FIELD_BITPOS (*fp)
13045 + anonymous_size * bits_per_byte
13046 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13047 }
13048 }
da5b30da
AA
13049 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13050 if (attr != NULL)
13051 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13052 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13053
13054 /* Get name of field. */
39cbfefa
DJ
13055 fieldname = dwarf2_name (die, cu);
13056 if (fieldname == NULL)
13057 fieldname = "";
d8151005
DJ
13058
13059 /* The name is already allocated along with this objfile, so we don't
13060 need to duplicate it for the type. */
13061 fp->name = fieldname;
c906108c
SS
13062
13063 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13064 pointer or virtual base class pointer) to private. */
e142c38c 13065 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13066 {
d48cc9dd 13067 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13068 new_field->accessibility = DW_ACCESS_private;
13069 fip->non_public_fields = 1;
13070 }
13071 }
a9a9bd0f 13072 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13073 {
a9a9bd0f
DC
13074 /* C++ static member. */
13075
13076 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13077 is a declaration, but all versions of G++ as of this writing
13078 (so through at least 3.2.1) incorrectly generate
13079 DW_TAG_variable tags. */
6e70227d 13080
ff355380 13081 const char *physname;
c906108c 13082
a9a9bd0f 13083 /* Get name of field. */
39cbfefa
DJ
13084 fieldname = dwarf2_name (die, cu);
13085 if (fieldname == NULL)
c906108c
SS
13086 return;
13087
254e6b9e 13088 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13089 if (attr
13090 /* Only create a symbol if this is an external value.
13091 new_symbol checks this and puts the value in the global symbol
13092 table, which we want. If it is not external, new_symbol
13093 will try to put the value in cu->list_in_scope which is wrong. */
13094 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13095 {
13096 /* A static const member, not much different than an enum as far as
13097 we're concerned, except that we can support more types. */
13098 new_symbol (die, NULL, cu);
13099 }
13100
2df3850c 13101 /* Get physical name. */
ff355380 13102 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13103
d8151005
DJ
13104 /* The name is already allocated along with this objfile, so we don't
13105 need to duplicate it for the type. */
13106 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13107 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13108 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13109 }
13110 else if (die->tag == DW_TAG_inheritance)
13111 {
74ac6d43 13112 LONGEST offset;
d4b96c9a 13113
74ac6d43
TT
13114 /* C++ base class field. */
13115 if (handle_data_member_location (die, cu, &offset))
13116 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13117 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13118 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13119 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13120 fip->nbaseclasses++;
13121 }
13122}
13123
98751a41
JK
13124/* Add a typedef defined in the scope of the FIP's class. */
13125
13126static void
13127dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13128 struct dwarf2_cu *cu)
6e70227d 13129{
98751a41 13130 struct typedef_field_list *new_field;
98751a41 13131 struct typedef_field *fp;
98751a41
JK
13132
13133 /* Allocate a new field list entry and link it in. */
8d749320 13134 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13135 make_cleanup (xfree, new_field);
13136
13137 gdb_assert (die->tag == DW_TAG_typedef);
13138
13139 fp = &new_field->field;
13140
13141 /* Get name of field. */
13142 fp->name = dwarf2_name (die, cu);
13143 if (fp->name == NULL)
13144 return;
13145
13146 fp->type = read_type_die (die, cu);
13147
13148 new_field->next = fip->typedef_field_list;
13149 fip->typedef_field_list = new_field;
13150 fip->typedef_field_list_count++;
13151}
13152
c906108c
SS
13153/* Create the vector of fields, and attach it to the type. */
13154
13155static void
fba45db2 13156dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13157 struct dwarf2_cu *cu)
c906108c
SS
13158{
13159 int nfields = fip->nfields;
13160
13161 /* Record the field count, allocate space for the array of fields,
13162 and create blank accessibility bitfields if necessary. */
13163 TYPE_NFIELDS (type) = nfields;
13164 TYPE_FIELDS (type) = (struct field *)
13165 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13166 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13167
b4ba55a1 13168 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13169 {
13170 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13171
13172 TYPE_FIELD_PRIVATE_BITS (type) =
13173 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13174 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13175
13176 TYPE_FIELD_PROTECTED_BITS (type) =
13177 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13178 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13179
774b6a14
TT
13180 TYPE_FIELD_IGNORE_BITS (type) =
13181 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13182 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13183 }
13184
13185 /* If the type has baseclasses, allocate and clear a bit vector for
13186 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13187 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13188 {
13189 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13190 unsigned char *pointer;
c906108c
SS
13191
13192 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13193 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13194 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13195 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13196 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13197 }
13198
3e43a32a
MS
13199 /* Copy the saved-up fields into the field vector. Start from the head of
13200 the list, adding to the tail of the field array, so that they end up in
13201 the same order in the array in which they were added to the list. */
c906108c
SS
13202 while (nfields-- > 0)
13203 {
7d0ccb61
DJ
13204 struct nextfield *fieldp;
13205
13206 if (fip->fields)
13207 {
13208 fieldp = fip->fields;
13209 fip->fields = fieldp->next;
13210 }
13211 else
13212 {
13213 fieldp = fip->baseclasses;
13214 fip->baseclasses = fieldp->next;
13215 }
13216
13217 TYPE_FIELD (type, nfields) = fieldp->field;
13218 switch (fieldp->accessibility)
c906108c 13219 {
c5aa993b 13220 case DW_ACCESS_private:
b4ba55a1
JB
13221 if (cu->language != language_ada)
13222 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13223 break;
c906108c 13224
c5aa993b 13225 case DW_ACCESS_protected:
b4ba55a1
JB
13226 if (cu->language != language_ada)
13227 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13228 break;
c906108c 13229
c5aa993b
JM
13230 case DW_ACCESS_public:
13231 break;
c906108c 13232
c5aa993b
JM
13233 default:
13234 /* Unknown accessibility. Complain and treat it as public. */
13235 {
e2e0b3e5 13236 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13237 fieldp->accessibility);
c5aa993b
JM
13238 }
13239 break;
c906108c
SS
13240 }
13241 if (nfields < fip->nbaseclasses)
13242 {
7d0ccb61 13243 switch (fieldp->virtuality)
c906108c 13244 {
c5aa993b
JM
13245 case DW_VIRTUALITY_virtual:
13246 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13247 if (cu->language == language_ada)
a73c6dcd 13248 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13249 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13250 break;
c906108c
SS
13251 }
13252 }
c906108c
SS
13253 }
13254}
13255
7d27a96d
TT
13256/* Return true if this member function is a constructor, false
13257 otherwise. */
13258
13259static int
13260dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13261{
13262 const char *fieldname;
fe978cb0 13263 const char *type_name;
7d27a96d
TT
13264 int len;
13265
13266 if (die->parent == NULL)
13267 return 0;
13268
13269 if (die->parent->tag != DW_TAG_structure_type
13270 && die->parent->tag != DW_TAG_union_type
13271 && die->parent->tag != DW_TAG_class_type)
13272 return 0;
13273
13274 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13275 type_name = dwarf2_name (die->parent, cu);
13276 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13277 return 0;
13278
13279 len = strlen (fieldname);
fe978cb0
PA
13280 return (strncmp (fieldname, type_name, len) == 0
13281 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13282}
13283
c906108c
SS
13284/* Add a member function to the proper fieldlist. */
13285
13286static void
107d2387 13287dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13288 struct type *type, struct dwarf2_cu *cu)
c906108c 13289{
e7c27a73 13290 struct objfile *objfile = cu->objfile;
c906108c
SS
13291 struct attribute *attr;
13292 struct fnfieldlist *flp;
13293 int i;
13294 struct fn_field *fnp;
15d034d0 13295 const char *fieldname;
c906108c 13296 struct nextfnfield *new_fnfield;
f792889a 13297 struct type *this_type;
60d5a603 13298 enum dwarf_access_attribute accessibility;
c906108c 13299
b4ba55a1 13300 if (cu->language == language_ada)
a73c6dcd 13301 error (_("unexpected member function in Ada type"));
b4ba55a1 13302
2df3850c 13303 /* Get name of member function. */
39cbfefa
DJ
13304 fieldname = dwarf2_name (die, cu);
13305 if (fieldname == NULL)
2df3850c 13306 return;
c906108c 13307
c906108c
SS
13308 /* Look up member function name in fieldlist. */
13309 for (i = 0; i < fip->nfnfields; i++)
13310 {
27bfe10e 13311 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13312 break;
13313 }
13314
13315 /* Create new list element if necessary. */
13316 if (i < fip->nfnfields)
13317 flp = &fip->fnfieldlists[i];
13318 else
13319 {
13320 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13321 {
13322 fip->fnfieldlists = (struct fnfieldlist *)
13323 xrealloc (fip->fnfieldlists,
13324 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13325 * sizeof (struct fnfieldlist));
c906108c 13326 if (fip->nfnfields == 0)
c13c43fd 13327 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13328 }
13329 flp = &fip->fnfieldlists[fip->nfnfields];
13330 flp->name = fieldname;
13331 flp->length = 0;
13332 flp->head = NULL;
3da10d80 13333 i = fip->nfnfields++;
c906108c
SS
13334 }
13335
13336 /* Create a new member function field and chain it to the field list
0963b4bd 13337 entry. */
8d749320 13338 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13339 make_cleanup (xfree, new_fnfield);
c906108c
SS
13340 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13341 new_fnfield->next = flp->head;
13342 flp->head = new_fnfield;
13343 flp->length++;
13344
13345 /* Fill in the member function field info. */
13346 fnp = &new_fnfield->fnfield;
3da10d80
KS
13347
13348 /* Delay processing of the physname until later. */
9c37b5ae 13349 if (cu->language == language_cplus)
3da10d80
KS
13350 {
13351 add_to_method_list (type, i, flp->length - 1, fieldname,
13352 die, cu);
13353 }
13354 else
13355 {
1d06ead6 13356 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13357 fnp->physname = physname ? physname : "";
13358 }
13359
c906108c 13360 fnp->type = alloc_type (objfile);
f792889a
DJ
13361 this_type = read_type_die (die, cu);
13362 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13363 {
f792889a 13364 int nparams = TYPE_NFIELDS (this_type);
c906108c 13365
f792889a 13366 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13367 of the method itself (TYPE_CODE_METHOD). */
13368 smash_to_method_type (fnp->type, type,
f792889a
DJ
13369 TYPE_TARGET_TYPE (this_type),
13370 TYPE_FIELDS (this_type),
13371 TYPE_NFIELDS (this_type),
13372 TYPE_VARARGS (this_type));
c906108c
SS
13373
13374 /* Handle static member functions.
c5aa993b 13375 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13376 member functions. G++ helps GDB by marking the first
13377 parameter for non-static member functions (which is the this
13378 pointer) as artificial. We obtain this information from
13379 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13380 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13381 fnp->voffset = VOFFSET_STATIC;
13382 }
13383 else
e2e0b3e5 13384 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13385 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13386
13387 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13388 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13389 fnp->fcontext = die_containing_type (die, cu);
c906108c 13390
3e43a32a
MS
13391 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13392 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13393
13394 /* Get accessibility. */
e142c38c 13395 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13396 if (attr)
aead7601 13397 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13398 else
13399 accessibility = dwarf2_default_access_attribute (die, cu);
13400 switch (accessibility)
c906108c 13401 {
60d5a603
JK
13402 case DW_ACCESS_private:
13403 fnp->is_private = 1;
13404 break;
13405 case DW_ACCESS_protected:
13406 fnp->is_protected = 1;
13407 break;
c906108c
SS
13408 }
13409
b02dede2 13410 /* Check for artificial methods. */
e142c38c 13411 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13412 if (attr && DW_UNSND (attr) != 0)
13413 fnp->is_artificial = 1;
13414
7d27a96d
TT
13415 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13416
0d564a31 13417 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13418 function. For older versions of GCC, this is an offset in the
13419 appropriate virtual table, as specified by DW_AT_containing_type.
13420 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13421 to the object address. */
13422
e142c38c 13423 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13424 if (attr)
8e19ed76 13425 {
aec5aa8b 13426 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13427 {
aec5aa8b
TT
13428 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13429 {
13430 /* Old-style GCC. */
13431 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13432 }
13433 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13434 || (DW_BLOCK (attr)->size > 1
13435 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13436 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13437 {
aec5aa8b
TT
13438 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13439 if ((fnp->voffset % cu->header.addr_size) != 0)
13440 dwarf2_complex_location_expr_complaint ();
13441 else
13442 fnp->voffset /= cu->header.addr_size;
13443 fnp->voffset += 2;
13444 }
13445 else
13446 dwarf2_complex_location_expr_complaint ();
13447
13448 if (!fnp->fcontext)
7e993ebf
KS
13449 {
13450 /* If there is no `this' field and no DW_AT_containing_type,
13451 we cannot actually find a base class context for the
13452 vtable! */
13453 if (TYPE_NFIELDS (this_type) == 0
13454 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13455 {
13456 complaint (&symfile_complaints,
13457 _("cannot determine context for virtual member "
13458 "function \"%s\" (offset %d)"),
9c541725 13459 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13460 }
13461 else
13462 {
13463 fnp->fcontext
13464 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13465 }
13466 }
aec5aa8b 13467 }
3690dd37 13468 else if (attr_form_is_section_offset (attr))
8e19ed76 13469 {
4d3c2250 13470 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13471 }
13472 else
13473 {
4d3c2250
KB
13474 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13475 fieldname);
8e19ed76 13476 }
0d564a31 13477 }
d48cc9dd
DJ
13478 else
13479 {
13480 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13481 if (attr && DW_UNSND (attr))
13482 {
13483 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13484 complaint (&symfile_complaints,
3e43a32a
MS
13485 _("Member function \"%s\" (offset %d) is virtual "
13486 "but the vtable offset is not specified"),
9c541725 13487 fieldname, to_underlying (die->sect_off));
9655fd1a 13488 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13489 TYPE_CPLUS_DYNAMIC (type) = 1;
13490 }
13491 }
c906108c
SS
13492}
13493
13494/* Create the vector of member function fields, and attach it to the type. */
13495
13496static void
fba45db2 13497dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13498 struct dwarf2_cu *cu)
c906108c
SS
13499{
13500 struct fnfieldlist *flp;
c906108c
SS
13501 int i;
13502
b4ba55a1 13503 if (cu->language == language_ada)
a73c6dcd 13504 error (_("unexpected member functions in Ada type"));
b4ba55a1 13505
c906108c
SS
13506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13507 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13508 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13509
13510 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13511 {
13512 struct nextfnfield *nfp = flp->head;
13513 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13514 int k;
13515
13516 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13517 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13518 fn_flp->fn_fields = (struct fn_field *)
13519 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13520 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13521 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13522 }
13523
13524 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13525}
13526
1168df01
JB
13527/* Returns non-zero if NAME is the name of a vtable member in CU's
13528 language, zero otherwise. */
13529static int
13530is_vtable_name (const char *name, struct dwarf2_cu *cu)
13531{
13532 static const char vptr[] = "_vptr";
987504bb 13533 static const char vtable[] = "vtable";
1168df01 13534
9c37b5ae
TT
13535 /* Look for the C++ form of the vtable. */
13536 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13537 return 1;
13538
13539 return 0;
13540}
13541
c0dd20ea 13542/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13543 functions, with the ABI-specified layout. If TYPE describes
13544 such a structure, smash it into a member function type.
61049d3b
DJ
13545
13546 GCC shouldn't do this; it should just output pointer to member DIEs.
13547 This is GCC PR debug/28767. */
c0dd20ea 13548
0b92b5bb
TT
13549static void
13550quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13551{
09e2d7c7 13552 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13553
13554 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13555 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13556 return;
c0dd20ea
DJ
13557
13558 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13559 if (TYPE_FIELD_NAME (type, 0) == NULL
13560 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13561 || TYPE_FIELD_NAME (type, 1) == NULL
13562 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13563 return;
c0dd20ea
DJ
13564
13565 /* Find the type of the method. */
0b92b5bb 13566 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13567 if (pfn_type == NULL
13568 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13569 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13570 return;
c0dd20ea
DJ
13571
13572 /* Look for the "this" argument. */
13573 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13574 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13575 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13576 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13577 return;
c0dd20ea 13578
09e2d7c7 13579 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13580 new_type = alloc_type (objfile);
09e2d7c7 13581 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13582 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13583 TYPE_VARARGS (pfn_type));
0b92b5bb 13584 smash_to_methodptr_type (type, new_type);
c0dd20ea 13585}
1168df01 13586
685b1105
JK
13587/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13588 (icc). */
13589
13590static int
13591producer_is_icc (struct dwarf2_cu *cu)
13592{
13593 if (!cu->checked_producer)
13594 check_producer (cu);
13595
13596 return cu->producer_is_icc;
13597}
13598
c906108c 13599/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13600 (definition) to create a type for the structure or union. Fill in
13601 the type's name and general properties; the members will not be
83655187
DE
13602 processed until process_structure_scope. A symbol table entry for
13603 the type will also not be done until process_structure_scope (assuming
13604 the type has a name).
c906108c 13605
c767944b
DJ
13606 NOTE: we need to call these functions regardless of whether or not the
13607 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13608 structure or union. This gets the type entered into our set of
83655187 13609 user defined types. */
c906108c 13610
f792889a 13611static struct type *
134d01f1 13612read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13613{
e7c27a73 13614 struct objfile *objfile = cu->objfile;
c906108c
SS
13615 struct type *type;
13616 struct attribute *attr;
15d034d0 13617 const char *name;
c906108c 13618
348e048f
DE
13619 /* If the definition of this type lives in .debug_types, read that type.
13620 Don't follow DW_AT_specification though, that will take us back up
13621 the chain and we want to go down. */
45e58e77 13622 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13623 if (attr)
13624 {
ac9ec31b 13625 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13626
ac9ec31b 13627 /* The type's CU may not be the same as CU.
02142a6c 13628 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13629 return set_die_type (die, type, cu);
13630 }
13631
c0dd20ea 13632 type = alloc_type (objfile);
c906108c 13633 INIT_CPLUS_SPECIFIC (type);
93311388 13634
39cbfefa
DJ
13635 name = dwarf2_name (die, cu);
13636 if (name != NULL)
c906108c 13637 {
987504bb 13638 if (cu->language == language_cplus
c44af4eb
TT
13639 || cu->language == language_d
13640 || cu->language == language_rust)
63d06c5c 13641 {
15d034d0 13642 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13643
13644 /* dwarf2_full_name might have already finished building the DIE's
13645 type. If so, there is no need to continue. */
13646 if (get_die_type (die, cu) != NULL)
13647 return get_die_type (die, cu);
13648
13649 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13650 if (die->tag == DW_TAG_structure_type
13651 || die->tag == DW_TAG_class_type)
13652 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13653 }
13654 else
13655 {
d8151005
DJ
13656 /* The name is already allocated along with this objfile, so
13657 we don't need to duplicate it for the type. */
7d455152 13658 TYPE_TAG_NAME (type) = name;
94af9270
KS
13659 if (die->tag == DW_TAG_class_type)
13660 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13661 }
c906108c
SS
13662 }
13663
13664 if (die->tag == DW_TAG_structure_type)
13665 {
13666 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13667 }
13668 else if (die->tag == DW_TAG_union_type)
13669 {
13670 TYPE_CODE (type) = TYPE_CODE_UNION;
13671 }
13672 else
13673 {
4753d33b 13674 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13675 }
13676
0cc2414c
TT
13677 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13678 TYPE_DECLARED_CLASS (type) = 1;
13679
e142c38c 13680 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13681 if (attr)
13682 {
155bfbd3
JB
13683 if (attr_form_is_constant (attr))
13684 TYPE_LENGTH (type) = DW_UNSND (attr);
13685 else
13686 {
13687 /* For the moment, dynamic type sizes are not supported
13688 by GDB's struct type. The actual size is determined
13689 on-demand when resolving the type of a given object,
13690 so set the type's length to zero for now. Otherwise,
13691 we record an expression as the length, and that expression
13692 could lead to a very large value, which could eventually
13693 lead to us trying to allocate that much memory when creating
13694 a value of that type. */
13695 TYPE_LENGTH (type) = 0;
13696 }
c906108c
SS
13697 }
13698 else
13699 {
13700 TYPE_LENGTH (type) = 0;
13701 }
13702
422b1cb0 13703 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13704 {
13705 /* ICC does not output the required DW_AT_declaration
13706 on incomplete types, but gives them a size of zero. */
422b1cb0 13707 TYPE_STUB (type) = 1;
685b1105
JK
13708 }
13709 else
13710 TYPE_STUB_SUPPORTED (type) = 1;
13711
dc718098 13712 if (die_is_declaration (die, cu))
876cecd0 13713 TYPE_STUB (type) = 1;
a6c727b2
DJ
13714 else if (attr == NULL && die->child == NULL
13715 && producer_is_realview (cu->producer))
13716 /* RealView does not output the required DW_AT_declaration
13717 on incomplete types. */
13718 TYPE_STUB (type) = 1;
dc718098 13719
c906108c
SS
13720 /* We need to add the type field to the die immediately so we don't
13721 infinitely recurse when dealing with pointers to the structure
0963b4bd 13722 type within the structure itself. */
1c379e20 13723 set_die_type (die, type, cu);
c906108c 13724
7e314c57
JK
13725 /* set_die_type should be already done. */
13726 set_descriptive_type (type, die, cu);
13727
c767944b
DJ
13728 return type;
13729}
13730
13731/* Finish creating a structure or union type, including filling in
13732 its members and creating a symbol for it. */
13733
13734static void
13735process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13736{
13737 struct objfile *objfile = cu->objfile;
ca040673 13738 struct die_info *child_die;
c767944b
DJ
13739 struct type *type;
13740
13741 type = get_die_type (die, cu);
13742 if (type == NULL)
13743 type = read_structure_type (die, cu);
13744
e142c38c 13745 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13746 {
13747 struct field_info fi;
34eaf542 13748 VEC (symbolp) *template_args = NULL;
c767944b 13749 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13750
13751 memset (&fi, 0, sizeof (struct field_info));
13752
639d11d3 13753 child_die = die->child;
c906108c
SS
13754
13755 while (child_die && child_die->tag)
13756 {
a9a9bd0f
DC
13757 if (child_die->tag == DW_TAG_member
13758 || child_die->tag == DW_TAG_variable)
c906108c 13759 {
a9a9bd0f
DC
13760 /* NOTE: carlton/2002-11-05: A C++ static data member
13761 should be a DW_TAG_member that is a declaration, but
13762 all versions of G++ as of this writing (so through at
13763 least 3.2.1) incorrectly generate DW_TAG_variable
13764 tags for them instead. */
e7c27a73 13765 dwarf2_add_field (&fi, child_die, cu);
c906108c 13766 }
8713b1b1 13767 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13768 {
e98c9e7c
TT
13769 /* Rust doesn't have member functions in the C++ sense.
13770 However, it does emit ordinary functions as children
13771 of a struct DIE. */
13772 if (cu->language == language_rust)
13773 read_func_scope (child_die, cu);
13774 else
13775 {
13776 /* C++ member function. */
13777 dwarf2_add_member_fn (&fi, child_die, type, cu);
13778 }
c906108c
SS
13779 }
13780 else if (child_die->tag == DW_TAG_inheritance)
13781 {
13782 /* C++ base class field. */
e7c27a73 13783 dwarf2_add_field (&fi, child_die, cu);
c906108c 13784 }
98751a41
JK
13785 else if (child_die->tag == DW_TAG_typedef)
13786 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13787 else if (child_die->tag == DW_TAG_template_type_param
13788 || child_die->tag == DW_TAG_template_value_param)
13789 {
13790 struct symbol *arg = new_symbol (child_die, NULL, cu);
13791
f1078f66
DJ
13792 if (arg != NULL)
13793 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13794 }
13795
c906108c
SS
13796 child_die = sibling_die (child_die);
13797 }
13798
34eaf542
TT
13799 /* Attach template arguments to type. */
13800 if (! VEC_empty (symbolp, template_args))
13801 {
13802 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13803 TYPE_N_TEMPLATE_ARGUMENTS (type)
13804 = VEC_length (symbolp, template_args);
13805 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13806 = XOBNEWVEC (&objfile->objfile_obstack,
13807 struct symbol *,
13808 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13809 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13810 VEC_address (symbolp, template_args),
13811 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13812 * sizeof (struct symbol *)));
13813 VEC_free (symbolp, template_args);
13814 }
13815
c906108c
SS
13816 /* Attach fields and member functions to the type. */
13817 if (fi.nfields)
e7c27a73 13818 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13819 if (fi.nfnfields)
13820 {
e7c27a73 13821 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13822
c5aa993b 13823 /* Get the type which refers to the base class (possibly this
c906108c 13824 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13825 class from the DW_AT_containing_type attribute. This use of
13826 DW_AT_containing_type is a GNU extension. */
c906108c 13827
e142c38c 13828 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13829 {
e7c27a73 13830 struct type *t = die_containing_type (die, cu);
c906108c 13831
ae6ae975 13832 set_type_vptr_basetype (type, t);
c906108c
SS
13833 if (type == t)
13834 {
c906108c
SS
13835 int i;
13836
13837 /* Our own class provides vtbl ptr. */
13838 for (i = TYPE_NFIELDS (t) - 1;
13839 i >= TYPE_N_BASECLASSES (t);
13840 --i)
13841 {
0d5cff50 13842 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13843
1168df01 13844 if (is_vtable_name (fieldname, cu))
c906108c 13845 {
ae6ae975 13846 set_type_vptr_fieldno (type, i);
c906108c
SS
13847 break;
13848 }
13849 }
13850
13851 /* Complain if virtual function table field not found. */
13852 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13853 complaint (&symfile_complaints,
3e43a32a
MS
13854 _("virtual function table pointer "
13855 "not found when defining class '%s'"),
4d3c2250
KB
13856 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13857 "");
c906108c
SS
13858 }
13859 else
13860 {
ae6ae975 13861 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13862 }
13863 }
f6235d4c 13864 else if (cu->producer
61012eef 13865 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13866 {
13867 /* The IBM XLC compiler does not provide direct indication
13868 of the containing type, but the vtable pointer is
13869 always named __vfp. */
13870
13871 int i;
13872
13873 for (i = TYPE_NFIELDS (type) - 1;
13874 i >= TYPE_N_BASECLASSES (type);
13875 --i)
13876 {
13877 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13878 {
ae6ae975
DE
13879 set_type_vptr_fieldno (type, i);
13880 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13881 break;
13882 }
13883 }
13884 }
c906108c 13885 }
98751a41
JK
13886
13887 /* Copy fi.typedef_field_list linked list elements content into the
13888 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13889 if (fi.typedef_field_list)
13890 {
13891 int i = fi.typedef_field_list_count;
13892
a0d7a4ff 13893 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13894 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13895 = ((struct typedef_field *)
13896 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13897 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13898
13899 /* Reverse the list order to keep the debug info elements order. */
13900 while (--i >= 0)
13901 {
13902 struct typedef_field *dest, *src;
6e70227d 13903
98751a41
JK
13904 dest = &TYPE_TYPEDEF_FIELD (type, i);
13905 src = &fi.typedef_field_list->field;
13906 fi.typedef_field_list = fi.typedef_field_list->next;
13907 *dest = *src;
13908 }
13909 }
c767944b
DJ
13910
13911 do_cleanups (back_to);
c906108c 13912 }
63d06c5c 13913
bb5ed363 13914 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13915
90aeadfc
DC
13916 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13917 snapshots) has been known to create a die giving a declaration
13918 for a class that has, as a child, a die giving a definition for a
13919 nested class. So we have to process our children even if the
13920 current die is a declaration. Normally, of course, a declaration
13921 won't have any children at all. */
134d01f1 13922
ca040673
DE
13923 child_die = die->child;
13924
90aeadfc
DC
13925 while (child_die != NULL && child_die->tag)
13926 {
13927 if (child_die->tag == DW_TAG_member
13928 || child_die->tag == DW_TAG_variable
34eaf542
TT
13929 || child_die->tag == DW_TAG_inheritance
13930 || child_die->tag == DW_TAG_template_value_param
13931 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13932 {
90aeadfc 13933 /* Do nothing. */
134d01f1 13934 }
90aeadfc
DC
13935 else
13936 process_die (child_die, cu);
134d01f1 13937
90aeadfc 13938 child_die = sibling_die (child_die);
134d01f1
DJ
13939 }
13940
fa4028e9
JB
13941 /* Do not consider external references. According to the DWARF standard,
13942 these DIEs are identified by the fact that they have no byte_size
13943 attribute, and a declaration attribute. */
13944 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13945 || !die_is_declaration (die, cu))
c767944b 13946 new_symbol (die, type, cu);
134d01f1
DJ
13947}
13948
55426c9d
JB
13949/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13950 update TYPE using some information only available in DIE's children. */
13951
13952static void
13953update_enumeration_type_from_children (struct die_info *die,
13954 struct type *type,
13955 struct dwarf2_cu *cu)
13956{
60f7655a 13957 struct die_info *child_die;
55426c9d
JB
13958 int unsigned_enum = 1;
13959 int flag_enum = 1;
13960 ULONGEST mask = 0;
55426c9d 13961
8268c778 13962 auto_obstack obstack;
55426c9d 13963
60f7655a
DE
13964 for (child_die = die->child;
13965 child_die != NULL && child_die->tag;
13966 child_die = sibling_die (child_die))
55426c9d
JB
13967 {
13968 struct attribute *attr;
13969 LONGEST value;
13970 const gdb_byte *bytes;
13971 struct dwarf2_locexpr_baton *baton;
13972 const char *name;
60f7655a 13973
55426c9d
JB
13974 if (child_die->tag != DW_TAG_enumerator)
13975 continue;
13976
13977 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13978 if (attr == NULL)
13979 continue;
13980
13981 name = dwarf2_name (child_die, cu);
13982 if (name == NULL)
13983 name = "<anonymous enumerator>";
13984
13985 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13986 &value, &bytes, &baton);
13987 if (value < 0)
13988 {
13989 unsigned_enum = 0;
13990 flag_enum = 0;
13991 }
13992 else if ((mask & value) != 0)
13993 flag_enum = 0;
13994 else
13995 mask |= value;
13996
13997 /* If we already know that the enum type is neither unsigned, nor
13998 a flag type, no need to look at the rest of the enumerates. */
13999 if (!unsigned_enum && !flag_enum)
14000 break;
55426c9d
JB
14001 }
14002
14003 if (unsigned_enum)
14004 TYPE_UNSIGNED (type) = 1;
14005 if (flag_enum)
14006 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14007}
14008
134d01f1
DJ
14009/* Given a DW_AT_enumeration_type die, set its type. We do not
14010 complete the type's fields yet, or create any symbols. */
c906108c 14011
f792889a 14012static struct type *
134d01f1 14013read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14014{
e7c27a73 14015 struct objfile *objfile = cu->objfile;
c906108c 14016 struct type *type;
c906108c 14017 struct attribute *attr;
0114d602 14018 const char *name;
134d01f1 14019
348e048f
DE
14020 /* If the definition of this type lives in .debug_types, read that type.
14021 Don't follow DW_AT_specification though, that will take us back up
14022 the chain and we want to go down. */
45e58e77 14023 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14024 if (attr)
14025 {
ac9ec31b 14026 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14027
ac9ec31b 14028 /* The type's CU may not be the same as CU.
02142a6c 14029 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14030 return set_die_type (die, type, cu);
14031 }
14032
c906108c
SS
14033 type = alloc_type (objfile);
14034
14035 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14036 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14037 if (name != NULL)
7d455152 14038 TYPE_TAG_NAME (type) = name;
c906108c 14039
0626fc76
TT
14040 attr = dwarf2_attr (die, DW_AT_type, cu);
14041 if (attr != NULL)
14042 {
14043 struct type *underlying_type = die_type (die, cu);
14044
14045 TYPE_TARGET_TYPE (type) = underlying_type;
14046 }
14047
e142c38c 14048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14049 if (attr)
14050 {
14051 TYPE_LENGTH (type) = DW_UNSND (attr);
14052 }
14053 else
14054 {
14055 TYPE_LENGTH (type) = 0;
14056 }
14057
137033e9
JB
14058 /* The enumeration DIE can be incomplete. In Ada, any type can be
14059 declared as private in the package spec, and then defined only
14060 inside the package body. Such types are known as Taft Amendment
14061 Types. When another package uses such a type, an incomplete DIE
14062 may be generated by the compiler. */
02eb380e 14063 if (die_is_declaration (die, cu))
876cecd0 14064 TYPE_STUB (type) = 1;
02eb380e 14065
0626fc76
TT
14066 /* Finish the creation of this type by using the enum's children.
14067 We must call this even when the underlying type has been provided
14068 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14069 update_enumeration_type_from_children (die, type, cu);
14070
0626fc76
TT
14071 /* If this type has an underlying type that is not a stub, then we
14072 may use its attributes. We always use the "unsigned" attribute
14073 in this situation, because ordinarily we guess whether the type
14074 is unsigned -- but the guess can be wrong and the underlying type
14075 can tell us the reality. However, we defer to a local size
14076 attribute if one exists, because this lets the compiler override
14077 the underlying type if needed. */
14078 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14079 {
14080 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14081 if (TYPE_LENGTH (type) == 0)
14082 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14083 }
14084
3d567982
TT
14085 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14086
f792889a 14087 return set_die_type (die, type, cu);
134d01f1
DJ
14088}
14089
14090/* Given a pointer to a die which begins an enumeration, process all
14091 the dies that define the members of the enumeration, and create the
14092 symbol for the enumeration type.
14093
14094 NOTE: We reverse the order of the element list. */
14095
14096static void
14097process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14098{
f792889a 14099 struct type *this_type;
134d01f1 14100
f792889a
DJ
14101 this_type = get_die_type (die, cu);
14102 if (this_type == NULL)
14103 this_type = read_enumeration_type (die, cu);
9dc481d3 14104
639d11d3 14105 if (die->child != NULL)
c906108c 14106 {
9dc481d3
DE
14107 struct die_info *child_die;
14108 struct symbol *sym;
14109 struct field *fields = NULL;
14110 int num_fields = 0;
15d034d0 14111 const char *name;
9dc481d3 14112
639d11d3 14113 child_die = die->child;
c906108c
SS
14114 while (child_die && child_die->tag)
14115 {
14116 if (child_die->tag != DW_TAG_enumerator)
14117 {
e7c27a73 14118 process_die (child_die, cu);
c906108c
SS
14119 }
14120 else
14121 {
39cbfefa
DJ
14122 name = dwarf2_name (child_die, cu);
14123 if (name)
c906108c 14124 {
f792889a 14125 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14126
14127 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14128 {
14129 fields = (struct field *)
14130 xrealloc (fields,
14131 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14132 * sizeof (struct field));
c906108c
SS
14133 }
14134
3567439c 14135 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14136 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14137 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14138 FIELD_BITSIZE (fields[num_fields]) = 0;
14139
14140 num_fields++;
14141 }
14142 }
14143
14144 child_die = sibling_die (child_die);
14145 }
14146
14147 if (num_fields)
14148 {
f792889a
DJ
14149 TYPE_NFIELDS (this_type) = num_fields;
14150 TYPE_FIELDS (this_type) = (struct field *)
14151 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14152 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14153 sizeof (struct field) * num_fields);
b8c9b27d 14154 xfree (fields);
c906108c 14155 }
c906108c 14156 }
134d01f1 14157
6c83ed52
TT
14158 /* If we are reading an enum from a .debug_types unit, and the enum
14159 is a declaration, and the enum is not the signatured type in the
14160 unit, then we do not want to add a symbol for it. Adding a
14161 symbol would in some cases obscure the true definition of the
14162 enum, giving users an incomplete type when the definition is
14163 actually available. Note that we do not want to do this for all
14164 enums which are just declarations, because C++0x allows forward
14165 enum declarations. */
3019eac3 14166 if (cu->per_cu->is_debug_types
6c83ed52
TT
14167 && die_is_declaration (die, cu))
14168 {
52dc124a 14169 struct signatured_type *sig_type;
6c83ed52 14170
c0f78cd4 14171 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14172 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14173 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14174 return;
14175 }
14176
f792889a 14177 new_symbol (die, this_type, cu);
c906108c
SS
14178}
14179
14180/* Extract all information from a DW_TAG_array_type DIE and put it in
14181 the DIE's type field. For now, this only handles one dimensional
14182 arrays. */
14183
f792889a 14184static struct type *
e7c27a73 14185read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14186{
e7c27a73 14187 struct objfile *objfile = cu->objfile;
c906108c 14188 struct die_info *child_die;
7e314c57 14189 struct type *type;
c906108c
SS
14190 struct type *element_type, *range_type, *index_type;
14191 struct type **range_types = NULL;
14192 struct attribute *attr;
14193 int ndim = 0;
14194 struct cleanup *back_to;
15d034d0 14195 const char *name;
dc53a7ad 14196 unsigned int bit_stride = 0;
c906108c 14197
e7c27a73 14198 element_type = die_type (die, cu);
c906108c 14199
7e314c57
JK
14200 /* The die_type call above may have already set the type for this DIE. */
14201 type = get_die_type (die, cu);
14202 if (type)
14203 return type;
14204
dc53a7ad
JB
14205 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14206 if (attr != NULL)
14207 bit_stride = DW_UNSND (attr) * 8;
14208
14209 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14210 if (attr != NULL)
14211 bit_stride = DW_UNSND (attr);
14212
c906108c
SS
14213 /* Irix 6.2 native cc creates array types without children for
14214 arrays with unspecified length. */
639d11d3 14215 if (die->child == NULL)
c906108c 14216 {
46bf5051 14217 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14218 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14219 type = create_array_type_with_stride (NULL, element_type, range_type,
14220 bit_stride);
f792889a 14221 return set_die_type (die, type, cu);
c906108c
SS
14222 }
14223
14224 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14225 child_die = die->child;
c906108c
SS
14226 while (child_die && child_die->tag)
14227 {
14228 if (child_die->tag == DW_TAG_subrange_type)
14229 {
f792889a 14230 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14231
f792889a 14232 if (child_type != NULL)
a02abb62 14233 {
0963b4bd
MS
14234 /* The range type was succesfully read. Save it for the
14235 array type creation. */
a02abb62
JB
14236 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14237 {
14238 range_types = (struct type **)
14239 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14240 * sizeof (struct type *));
14241 if (ndim == 0)
14242 make_cleanup (free_current_contents, &range_types);
14243 }
f792889a 14244 range_types[ndim++] = child_type;
a02abb62 14245 }
c906108c
SS
14246 }
14247 child_die = sibling_die (child_die);
14248 }
14249
14250 /* Dwarf2 dimensions are output from left to right, create the
14251 necessary array types in backwards order. */
7ca2d3a3 14252
c906108c 14253 type = element_type;
7ca2d3a3
DL
14254
14255 if (read_array_order (die, cu) == DW_ORD_col_major)
14256 {
14257 int i = 0;
9a619af0 14258
7ca2d3a3 14259 while (i < ndim)
dc53a7ad
JB
14260 type = create_array_type_with_stride (NULL, type, range_types[i++],
14261 bit_stride);
7ca2d3a3
DL
14262 }
14263 else
14264 {
14265 while (ndim-- > 0)
dc53a7ad
JB
14266 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14267 bit_stride);
7ca2d3a3 14268 }
c906108c 14269
f5f8a009
EZ
14270 /* Understand Dwarf2 support for vector types (like they occur on
14271 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14272 array type. This is not part of the Dwarf2/3 standard yet, but a
14273 custom vendor extension. The main difference between a regular
14274 array and the vector variant is that vectors are passed by value
14275 to functions. */
e142c38c 14276 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14277 if (attr)
ea37ba09 14278 make_vector_type (type);
f5f8a009 14279
dbc98a8b
KW
14280 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14281 implementation may choose to implement triple vectors using this
14282 attribute. */
14283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14284 if (attr)
14285 {
14286 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14287 TYPE_LENGTH (type) = DW_UNSND (attr);
14288 else
3e43a32a
MS
14289 complaint (&symfile_complaints,
14290 _("DW_AT_byte_size for array type smaller "
14291 "than the total size of elements"));
dbc98a8b
KW
14292 }
14293
39cbfefa
DJ
14294 name = dwarf2_name (die, cu);
14295 if (name)
14296 TYPE_NAME (type) = name;
6e70227d 14297
0963b4bd 14298 /* Install the type in the die. */
7e314c57
JK
14299 set_die_type (die, type, cu);
14300
14301 /* set_die_type should be already done. */
b4ba55a1
JB
14302 set_descriptive_type (type, die, cu);
14303
c906108c
SS
14304 do_cleanups (back_to);
14305
7e314c57 14306 return type;
c906108c
SS
14307}
14308
7ca2d3a3 14309static enum dwarf_array_dim_ordering
6e70227d 14310read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14311{
14312 struct attribute *attr;
14313
14314 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14315
aead7601
SM
14316 if (attr)
14317 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14318
0963b4bd
MS
14319 /* GNU F77 is a special case, as at 08/2004 array type info is the
14320 opposite order to the dwarf2 specification, but data is still
14321 laid out as per normal fortran.
7ca2d3a3 14322
0963b4bd
MS
14323 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14324 version checking. */
7ca2d3a3 14325
905e0470
PM
14326 if (cu->language == language_fortran
14327 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14328 {
14329 return DW_ORD_row_major;
14330 }
14331
6e70227d 14332 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14333 {
14334 case array_column_major:
14335 return DW_ORD_col_major;
14336 case array_row_major:
14337 default:
14338 return DW_ORD_row_major;
14339 };
14340}
14341
72019c9c 14342/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14343 the DIE's type field. */
72019c9c 14344
f792889a 14345static struct type *
72019c9c
GM
14346read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14347{
7e314c57
JK
14348 struct type *domain_type, *set_type;
14349 struct attribute *attr;
f792889a 14350
7e314c57
JK
14351 domain_type = die_type (die, cu);
14352
14353 /* The die_type call above may have already set the type for this DIE. */
14354 set_type = get_die_type (die, cu);
14355 if (set_type)
14356 return set_type;
14357
14358 set_type = create_set_type (NULL, domain_type);
14359
14360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14361 if (attr)
14362 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14363
f792889a 14364 return set_die_type (die, set_type, cu);
72019c9c 14365}
7ca2d3a3 14366
0971de02
TT
14367/* A helper for read_common_block that creates a locexpr baton.
14368 SYM is the symbol which we are marking as computed.
14369 COMMON_DIE is the DIE for the common block.
14370 COMMON_LOC is the location expression attribute for the common
14371 block itself.
14372 MEMBER_LOC is the location expression attribute for the particular
14373 member of the common block that we are processing.
14374 CU is the CU from which the above come. */
14375
14376static void
14377mark_common_block_symbol_computed (struct symbol *sym,
14378 struct die_info *common_die,
14379 struct attribute *common_loc,
14380 struct attribute *member_loc,
14381 struct dwarf2_cu *cu)
14382{
14383 struct objfile *objfile = dwarf2_per_objfile->objfile;
14384 struct dwarf2_locexpr_baton *baton;
14385 gdb_byte *ptr;
14386 unsigned int cu_off;
14387 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14388 LONGEST offset = 0;
14389
14390 gdb_assert (common_loc && member_loc);
14391 gdb_assert (attr_form_is_block (common_loc));
14392 gdb_assert (attr_form_is_block (member_loc)
14393 || attr_form_is_constant (member_loc));
14394
8d749320 14395 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14396 baton->per_cu = cu->per_cu;
14397 gdb_assert (baton->per_cu);
14398
14399 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14400
14401 if (attr_form_is_constant (member_loc))
14402 {
14403 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14404 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14405 }
14406 else
14407 baton->size += DW_BLOCK (member_loc)->size;
14408
224c3ddb 14409 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14410 baton->data = ptr;
14411
14412 *ptr++ = DW_OP_call4;
9c541725 14413 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14414 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14415 ptr += 4;
14416
14417 if (attr_form_is_constant (member_loc))
14418 {
14419 *ptr++ = DW_OP_addr;
14420 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14421 ptr += cu->header.addr_size;
14422 }
14423 else
14424 {
14425 /* We have to copy the data here, because DW_OP_call4 will only
14426 use a DW_AT_location attribute. */
14427 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14428 ptr += DW_BLOCK (member_loc)->size;
14429 }
14430
14431 *ptr++ = DW_OP_plus;
14432 gdb_assert (ptr - baton->data == baton->size);
14433
0971de02 14434 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14435 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14436}
14437
4357ac6c
TT
14438/* Create appropriate locally-scoped variables for all the
14439 DW_TAG_common_block entries. Also create a struct common_block
14440 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14441 is used to sepate the common blocks name namespace from regular
14442 variable names. */
c906108c
SS
14443
14444static void
e7c27a73 14445read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14446{
0971de02
TT
14447 struct attribute *attr;
14448
14449 attr = dwarf2_attr (die, DW_AT_location, cu);
14450 if (attr)
14451 {
14452 /* Support the .debug_loc offsets. */
14453 if (attr_form_is_block (attr))
14454 {
14455 /* Ok. */
14456 }
14457 else if (attr_form_is_section_offset (attr))
14458 {
14459 dwarf2_complex_location_expr_complaint ();
14460 attr = NULL;
14461 }
14462 else
14463 {
14464 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14465 "common block member");
14466 attr = NULL;
14467 }
14468 }
14469
639d11d3 14470 if (die->child != NULL)
c906108c 14471 {
4357ac6c
TT
14472 struct objfile *objfile = cu->objfile;
14473 struct die_info *child_die;
14474 size_t n_entries = 0, size;
14475 struct common_block *common_block;
14476 struct symbol *sym;
74ac6d43 14477
4357ac6c
TT
14478 for (child_die = die->child;
14479 child_die && child_die->tag;
14480 child_die = sibling_die (child_die))
14481 ++n_entries;
14482
14483 size = (sizeof (struct common_block)
14484 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14485 common_block
14486 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14487 size);
4357ac6c
TT
14488 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14489 common_block->n_entries = 0;
14490
14491 for (child_die = die->child;
14492 child_die && child_die->tag;
14493 child_die = sibling_die (child_die))
14494 {
14495 /* Create the symbol in the DW_TAG_common_block block in the current
14496 symbol scope. */
e7c27a73 14497 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14498 if (sym != NULL)
14499 {
14500 struct attribute *member_loc;
14501
14502 common_block->contents[common_block->n_entries++] = sym;
14503
14504 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14505 cu);
14506 if (member_loc)
14507 {
14508 /* GDB has handled this for a long time, but it is
14509 not specified by DWARF. It seems to have been
14510 emitted by gfortran at least as recently as:
14511 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14512 complaint (&symfile_complaints,
14513 _("Variable in common block has "
14514 "DW_AT_data_member_location "
14515 "- DIE at 0x%x [in module %s]"),
9c541725 14516 to_underlying (child_die->sect_off),
4262abfb 14517 objfile_name (cu->objfile));
0971de02
TT
14518
14519 if (attr_form_is_section_offset (member_loc))
14520 dwarf2_complex_location_expr_complaint ();
14521 else if (attr_form_is_constant (member_loc)
14522 || attr_form_is_block (member_loc))
14523 {
14524 if (attr)
14525 mark_common_block_symbol_computed (sym, die, attr,
14526 member_loc, cu);
14527 }
14528 else
14529 dwarf2_complex_location_expr_complaint ();
14530 }
14531 }
c906108c 14532 }
4357ac6c
TT
14533
14534 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14535 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14536 }
14537}
14538
0114d602 14539/* Create a type for a C++ namespace. */
d9fa45fe 14540
0114d602
DJ
14541static struct type *
14542read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14543{
e7c27a73 14544 struct objfile *objfile = cu->objfile;
0114d602 14545 const char *previous_prefix, *name;
9219021c 14546 int is_anonymous;
0114d602
DJ
14547 struct type *type;
14548
14549 /* For extensions, reuse the type of the original namespace. */
14550 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14551 {
14552 struct die_info *ext_die;
14553 struct dwarf2_cu *ext_cu = cu;
9a619af0 14554
0114d602
DJ
14555 ext_die = dwarf2_extension (die, &ext_cu);
14556 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14557
14558 /* EXT_CU may not be the same as CU.
02142a6c 14559 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14560 return set_die_type (die, type, cu);
14561 }
9219021c 14562
e142c38c 14563 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14564
14565 /* Now build the name of the current namespace. */
14566
0114d602
DJ
14567 previous_prefix = determine_prefix (die, cu);
14568 if (previous_prefix[0] != '\0')
14569 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14570 previous_prefix, name, 0, cu);
0114d602
DJ
14571
14572 /* Create the type. */
19f392bc 14573 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14574 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14575
60531b24 14576 return set_die_type (die, type, cu);
0114d602
DJ
14577}
14578
22cee43f 14579/* Read a namespace scope. */
0114d602
DJ
14580
14581static void
14582read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14583{
14584 struct objfile *objfile = cu->objfile;
0114d602 14585 int is_anonymous;
9219021c 14586
5c4e30ca
DC
14587 /* Add a symbol associated to this if we haven't seen the namespace
14588 before. Also, add a using directive if it's an anonymous
14589 namespace. */
9219021c 14590
f2f0e013 14591 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14592 {
14593 struct type *type;
14594
0114d602 14595 type = read_type_die (die, cu);
e7c27a73 14596 new_symbol (die, type, cu);
5c4e30ca 14597
e8e80198 14598 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14599 if (is_anonymous)
0114d602
DJ
14600 {
14601 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14602
22cee43f
PMR
14603 add_using_directive (using_directives (cu->language),
14604 previous_prefix, TYPE_NAME (type), NULL,
14605 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14606 }
5c4e30ca 14607 }
9219021c 14608
639d11d3 14609 if (die->child != NULL)
d9fa45fe 14610 {
639d11d3 14611 struct die_info *child_die = die->child;
6e70227d 14612
d9fa45fe
DC
14613 while (child_die && child_die->tag)
14614 {
e7c27a73 14615 process_die (child_die, cu);
d9fa45fe
DC
14616 child_die = sibling_die (child_die);
14617 }
14618 }
38d518c9
EZ
14619}
14620
f55ee35c
JK
14621/* Read a Fortran module as type. This DIE can be only a declaration used for
14622 imported module. Still we need that type as local Fortran "use ... only"
14623 declaration imports depend on the created type in determine_prefix. */
14624
14625static struct type *
14626read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14627{
14628 struct objfile *objfile = cu->objfile;
15d034d0 14629 const char *module_name;
f55ee35c
JK
14630 struct type *type;
14631
14632 module_name = dwarf2_name (die, cu);
14633 if (!module_name)
3e43a32a
MS
14634 complaint (&symfile_complaints,
14635 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14636 to_underlying (die->sect_off));
19f392bc 14637 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14638
14639 /* determine_prefix uses TYPE_TAG_NAME. */
14640 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14641
14642 return set_die_type (die, type, cu);
14643}
14644
5d7cb8df
JK
14645/* Read a Fortran module. */
14646
14647static void
14648read_module (struct die_info *die, struct dwarf2_cu *cu)
14649{
14650 struct die_info *child_die = die->child;
530e8392
KB
14651 struct type *type;
14652
14653 type = read_type_die (die, cu);
14654 new_symbol (die, type, cu);
5d7cb8df 14655
5d7cb8df
JK
14656 while (child_die && child_die->tag)
14657 {
14658 process_die (child_die, cu);
14659 child_die = sibling_die (child_die);
14660 }
14661}
14662
38d518c9
EZ
14663/* Return the name of the namespace represented by DIE. Set
14664 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14665 namespace. */
14666
14667static const char *
e142c38c 14668namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14669{
14670 struct die_info *current_die;
14671 const char *name = NULL;
14672
14673 /* Loop through the extensions until we find a name. */
14674
14675 for (current_die = die;
14676 current_die != NULL;
f2f0e013 14677 current_die = dwarf2_extension (die, &cu))
38d518c9 14678 {
96553a0c
DE
14679 /* We don't use dwarf2_name here so that we can detect the absence
14680 of a name -> anonymous namespace. */
7d45c7c3 14681 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14682
38d518c9
EZ
14683 if (name != NULL)
14684 break;
14685 }
14686
14687 /* Is it an anonymous namespace? */
14688
14689 *is_anonymous = (name == NULL);
14690 if (*is_anonymous)
2b1dbab0 14691 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14692
14693 return name;
d9fa45fe
DC
14694}
14695
c906108c
SS
14696/* Extract all information from a DW_TAG_pointer_type DIE and add to
14697 the user defined type vector. */
14698
f792889a 14699static struct type *
e7c27a73 14700read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14701{
5e2b427d 14702 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14703 struct comp_unit_head *cu_header = &cu->header;
c906108c 14704 struct type *type;
8b2dbe47
KB
14705 struct attribute *attr_byte_size;
14706 struct attribute *attr_address_class;
14707 int byte_size, addr_class;
7e314c57
JK
14708 struct type *target_type;
14709
14710 target_type = die_type (die, cu);
c906108c 14711
7e314c57
JK
14712 /* The die_type call above may have already set the type for this DIE. */
14713 type = get_die_type (die, cu);
14714 if (type)
14715 return type;
14716
14717 type = lookup_pointer_type (target_type);
8b2dbe47 14718
e142c38c 14719 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14720 if (attr_byte_size)
14721 byte_size = DW_UNSND (attr_byte_size);
c906108c 14722 else
8b2dbe47
KB
14723 byte_size = cu_header->addr_size;
14724
e142c38c 14725 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14726 if (attr_address_class)
14727 addr_class = DW_UNSND (attr_address_class);
14728 else
14729 addr_class = DW_ADDR_none;
14730
14731 /* If the pointer size or address class is different than the
14732 default, create a type variant marked as such and set the
14733 length accordingly. */
14734 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14735 {
5e2b427d 14736 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14737 {
14738 int type_flags;
14739
849957d9 14740 type_flags = gdbarch_address_class_type_flags
5e2b427d 14741 (gdbarch, byte_size, addr_class);
876cecd0
TT
14742 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14743 == 0);
8b2dbe47
KB
14744 type = make_type_with_address_space (type, type_flags);
14745 }
14746 else if (TYPE_LENGTH (type) != byte_size)
14747 {
3e43a32a
MS
14748 complaint (&symfile_complaints,
14749 _("invalid pointer size %d"), byte_size);
8b2dbe47 14750 }
6e70227d 14751 else
9a619af0
MS
14752 {
14753 /* Should we also complain about unhandled address classes? */
14754 }
c906108c 14755 }
8b2dbe47
KB
14756
14757 TYPE_LENGTH (type) = byte_size;
f792889a 14758 return set_die_type (die, type, cu);
c906108c
SS
14759}
14760
14761/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14762 the user defined type vector. */
14763
f792889a 14764static struct type *
e7c27a73 14765read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14766{
14767 struct type *type;
14768 struct type *to_type;
14769 struct type *domain;
14770
e7c27a73
DJ
14771 to_type = die_type (die, cu);
14772 domain = die_containing_type (die, cu);
0d5de010 14773
7e314c57
JK
14774 /* The calls above may have already set the type for this DIE. */
14775 type = get_die_type (die, cu);
14776 if (type)
14777 return type;
14778
0d5de010
DJ
14779 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14780 type = lookup_methodptr_type (to_type);
7078baeb
TT
14781 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14782 {
14783 struct type *new_type = alloc_type (cu->objfile);
14784
14785 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14786 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14787 TYPE_VARARGS (to_type));
14788 type = lookup_methodptr_type (new_type);
14789 }
0d5de010
DJ
14790 else
14791 type = lookup_memberptr_type (to_type, domain);
c906108c 14792
f792889a 14793 return set_die_type (die, type, cu);
c906108c
SS
14794}
14795
4297a3f0 14796/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14797 the user defined type vector. */
14798
f792889a 14799static struct type *
4297a3f0
AV
14800read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14801 enum type_code refcode)
c906108c 14802{
e7c27a73 14803 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14804 struct type *type, *target_type;
c906108c
SS
14805 struct attribute *attr;
14806
4297a3f0
AV
14807 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14808
7e314c57
JK
14809 target_type = die_type (die, cu);
14810
14811 /* The die_type call above may have already set the type for this DIE. */
14812 type = get_die_type (die, cu);
14813 if (type)
14814 return type;
14815
4297a3f0 14816 type = lookup_reference_type (target_type, refcode);
e142c38c 14817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14818 if (attr)
14819 {
14820 TYPE_LENGTH (type) = DW_UNSND (attr);
14821 }
14822 else
14823 {
107d2387 14824 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14825 }
f792889a 14826 return set_die_type (die, type, cu);
c906108c
SS
14827}
14828
cf363f18
MW
14829/* Add the given cv-qualifiers to the element type of the array. GCC
14830 outputs DWARF type qualifiers that apply to an array, not the
14831 element type. But GDB relies on the array element type to carry
14832 the cv-qualifiers. This mimics section 6.7.3 of the C99
14833 specification. */
14834
14835static struct type *
14836add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14837 struct type *base_type, int cnst, int voltl)
14838{
14839 struct type *el_type, *inner_array;
14840
14841 base_type = copy_type (base_type);
14842 inner_array = base_type;
14843
14844 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14845 {
14846 TYPE_TARGET_TYPE (inner_array) =
14847 copy_type (TYPE_TARGET_TYPE (inner_array));
14848 inner_array = TYPE_TARGET_TYPE (inner_array);
14849 }
14850
14851 el_type = TYPE_TARGET_TYPE (inner_array);
14852 cnst |= TYPE_CONST (el_type);
14853 voltl |= TYPE_VOLATILE (el_type);
14854 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14855
14856 return set_die_type (die, base_type, cu);
14857}
14858
f792889a 14859static struct type *
e7c27a73 14860read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14861{
f792889a 14862 struct type *base_type, *cv_type;
c906108c 14863
e7c27a73 14864 base_type = die_type (die, cu);
7e314c57
JK
14865
14866 /* The die_type call above may have already set the type for this DIE. */
14867 cv_type = get_die_type (die, cu);
14868 if (cv_type)
14869 return cv_type;
14870
2f608a3a
KW
14871 /* In case the const qualifier is applied to an array type, the element type
14872 is so qualified, not the array type (section 6.7.3 of C99). */
14873 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14874 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14875
f792889a
DJ
14876 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14877 return set_die_type (die, cv_type, cu);
c906108c
SS
14878}
14879
f792889a 14880static struct type *
e7c27a73 14881read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14882{
f792889a 14883 struct type *base_type, *cv_type;
c906108c 14884
e7c27a73 14885 base_type = die_type (die, cu);
7e314c57
JK
14886
14887 /* The die_type call above may have already set the type for this DIE. */
14888 cv_type = get_die_type (die, cu);
14889 if (cv_type)
14890 return cv_type;
14891
cf363f18
MW
14892 /* In case the volatile qualifier is applied to an array type, the
14893 element type is so qualified, not the array type (section 6.7.3
14894 of C99). */
14895 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14896 return add_array_cv_type (die, cu, base_type, 0, 1);
14897
f792889a
DJ
14898 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14899 return set_die_type (die, cv_type, cu);
c906108c
SS
14900}
14901
06d66ee9
TT
14902/* Handle DW_TAG_restrict_type. */
14903
14904static struct type *
14905read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14906{
14907 struct type *base_type, *cv_type;
14908
14909 base_type = die_type (die, cu);
14910
14911 /* The die_type call above may have already set the type for this DIE. */
14912 cv_type = get_die_type (die, cu);
14913 if (cv_type)
14914 return cv_type;
14915
14916 cv_type = make_restrict_type (base_type);
14917 return set_die_type (die, cv_type, cu);
14918}
14919
a2c2acaf
MW
14920/* Handle DW_TAG_atomic_type. */
14921
14922static struct type *
14923read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14924{
14925 struct type *base_type, *cv_type;
14926
14927 base_type = die_type (die, cu);
14928
14929 /* The die_type call above may have already set the type for this DIE. */
14930 cv_type = get_die_type (die, cu);
14931 if (cv_type)
14932 return cv_type;
14933
14934 cv_type = make_atomic_type (base_type);
14935 return set_die_type (die, cv_type, cu);
14936}
14937
c906108c
SS
14938/* Extract all information from a DW_TAG_string_type DIE and add to
14939 the user defined type vector. It isn't really a user defined type,
14940 but it behaves like one, with other DIE's using an AT_user_def_type
14941 attribute to reference it. */
14942
f792889a 14943static struct type *
e7c27a73 14944read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14945{
e7c27a73 14946 struct objfile *objfile = cu->objfile;
3b7538c0 14947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14948 struct type *type, *range_type, *index_type, *char_type;
14949 struct attribute *attr;
14950 unsigned int length;
14951
e142c38c 14952 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14953 if (attr)
14954 {
14955 length = DW_UNSND (attr);
14956 }
14957 else
14958 {
0963b4bd 14959 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14960 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14961 if (attr)
14962 {
14963 length = DW_UNSND (attr);
14964 }
14965 else
14966 {
14967 length = 1;
14968 }
c906108c 14969 }
6ccb9162 14970
46bf5051 14971 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14972 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14973 char_type = language_string_char_type (cu->language_defn, gdbarch);
14974 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14975
f792889a 14976 return set_die_type (die, type, cu);
c906108c
SS
14977}
14978
4d804846
JB
14979/* Assuming that DIE corresponds to a function, returns nonzero
14980 if the function is prototyped. */
14981
14982static int
14983prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14984{
14985 struct attribute *attr;
14986
14987 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14988 if (attr && (DW_UNSND (attr) != 0))
14989 return 1;
14990
14991 /* The DWARF standard implies that the DW_AT_prototyped attribute
14992 is only meaninful for C, but the concept also extends to other
14993 languages that allow unprototyped functions (Eg: Objective C).
14994 For all other languages, assume that functions are always
14995 prototyped. */
14996 if (cu->language != language_c
14997 && cu->language != language_objc
14998 && cu->language != language_opencl)
14999 return 1;
15000
15001 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15002 prototyped and unprototyped functions; default to prototyped,
15003 since that is more common in modern code (and RealView warns
15004 about unprototyped functions). */
15005 if (producer_is_realview (cu->producer))
15006 return 1;
15007
15008 return 0;
15009}
15010
c906108c
SS
15011/* Handle DIES due to C code like:
15012
15013 struct foo
c5aa993b
JM
15014 {
15015 int (*funcp)(int a, long l);
15016 int b;
15017 };
c906108c 15018
0963b4bd 15019 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15020
f792889a 15021static struct type *
e7c27a73 15022read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15023{
bb5ed363 15024 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15025 struct type *type; /* Type that this function returns. */
15026 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15027 struct attribute *attr;
15028
e7c27a73 15029 type = die_type (die, cu);
7e314c57
JK
15030
15031 /* The die_type call above may have already set the type for this DIE. */
15032 ftype = get_die_type (die, cu);
15033 if (ftype)
15034 return ftype;
15035
0c8b41f1 15036 ftype = lookup_function_type (type);
c906108c 15037
4d804846 15038 if (prototyped_function_p (die, cu))
a6c727b2 15039 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15040
c055b101
CV
15041 /* Store the calling convention in the type if it's available in
15042 the subroutine die. Otherwise set the calling convention to
15043 the default value DW_CC_normal. */
15044 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15045 if (attr)
15046 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15047 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15048 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15049 else
15050 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15051
743649fd
MW
15052 /* Record whether the function returns normally to its caller or not
15053 if the DWARF producer set that information. */
15054 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15055 if (attr && (DW_UNSND (attr) != 0))
15056 TYPE_NO_RETURN (ftype) = 1;
15057
76c10ea2
GM
15058 /* We need to add the subroutine type to the die immediately so
15059 we don't infinitely recurse when dealing with parameters
0963b4bd 15060 declared as the same subroutine type. */
76c10ea2 15061 set_die_type (die, ftype, cu);
6e70227d 15062
639d11d3 15063 if (die->child != NULL)
c906108c 15064 {
bb5ed363 15065 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15066 struct die_info *child_die;
8072405b 15067 int nparams, iparams;
c906108c
SS
15068
15069 /* Count the number of parameters.
15070 FIXME: GDB currently ignores vararg functions, but knows about
15071 vararg member functions. */
8072405b 15072 nparams = 0;
639d11d3 15073 child_die = die->child;
c906108c
SS
15074 while (child_die && child_die->tag)
15075 {
15076 if (child_die->tag == DW_TAG_formal_parameter)
15077 nparams++;
15078 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15079 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15080 child_die = sibling_die (child_die);
15081 }
15082
15083 /* Allocate storage for parameters and fill them in. */
15084 TYPE_NFIELDS (ftype) = nparams;
15085 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15086 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15087
8072405b
JK
15088 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15089 even if we error out during the parameters reading below. */
15090 for (iparams = 0; iparams < nparams; iparams++)
15091 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15092
15093 iparams = 0;
639d11d3 15094 child_die = die->child;
c906108c
SS
15095 while (child_die && child_die->tag)
15096 {
15097 if (child_die->tag == DW_TAG_formal_parameter)
15098 {
3ce3b1ba
PA
15099 struct type *arg_type;
15100
15101 /* DWARF version 2 has no clean way to discern C++
15102 static and non-static member functions. G++ helps
15103 GDB by marking the first parameter for non-static
15104 member functions (which is the this pointer) as
15105 artificial. We pass this information to
15106 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15107
15108 DWARF version 3 added DW_AT_object_pointer, which GCC
15109 4.5 does not yet generate. */
e142c38c 15110 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15111 if (attr)
15112 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15113 else
9c37b5ae 15114 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15115 arg_type = die_type (child_die, cu);
15116
15117 /* RealView does not mark THIS as const, which the testsuite
15118 expects. GCC marks THIS as const in method definitions,
15119 but not in the class specifications (GCC PR 43053). */
15120 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15121 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15122 {
15123 int is_this = 0;
15124 struct dwarf2_cu *arg_cu = cu;
15125 const char *name = dwarf2_name (child_die, cu);
15126
15127 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15128 if (attr)
15129 {
15130 /* If the compiler emits this, use it. */
15131 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15132 is_this = 1;
15133 }
15134 else if (name && strcmp (name, "this") == 0)
15135 /* Function definitions will have the argument names. */
15136 is_this = 1;
15137 else if (name == NULL && iparams == 0)
15138 /* Declarations may not have the names, so like
15139 elsewhere in GDB, assume an artificial first
15140 argument is "this". */
15141 is_this = 1;
15142
15143 if (is_this)
15144 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15145 arg_type, 0);
15146 }
15147
15148 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15149 iparams++;
15150 }
15151 child_die = sibling_die (child_die);
15152 }
15153 }
15154
76c10ea2 15155 return ftype;
c906108c
SS
15156}
15157
f792889a 15158static struct type *
e7c27a73 15159read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15160{
e7c27a73 15161 struct objfile *objfile = cu->objfile;
0114d602 15162 const char *name = NULL;
3c8e0968 15163 struct type *this_type, *target_type;
c906108c 15164
94af9270 15165 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15166 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15167 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15168 set_die_type (die, this_type, cu);
3c8e0968
DE
15169 target_type = die_type (die, cu);
15170 if (target_type != this_type)
15171 TYPE_TARGET_TYPE (this_type) = target_type;
15172 else
15173 {
15174 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15175 spec and cause infinite loops in GDB. */
15176 complaint (&symfile_complaints,
15177 _("Self-referential DW_TAG_typedef "
15178 "- DIE at 0x%x [in module %s]"),
9c541725 15179 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15180 TYPE_TARGET_TYPE (this_type) = NULL;
15181 }
f792889a 15182 return this_type;
c906108c
SS
15183}
15184
9b790ce7
UW
15185/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15186 (which may be different from NAME) to the architecture back-end to allow
15187 it to guess the correct format if necessary. */
15188
15189static struct type *
15190dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15191 const char *name_hint)
15192{
15193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15194 const struct floatformat **format;
15195 struct type *type;
15196
15197 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15198 if (format)
15199 type = init_float_type (objfile, bits, name, format);
15200 else
15201 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15202
15203 return type;
15204}
15205
c906108c
SS
15206/* Find a representation of a given base type and install
15207 it in the TYPE field of the die. */
15208
f792889a 15209static struct type *
e7c27a73 15210read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15211{
e7c27a73 15212 struct objfile *objfile = cu->objfile;
c906108c
SS
15213 struct type *type;
15214 struct attribute *attr;
19f392bc 15215 int encoding = 0, bits = 0;
15d034d0 15216 const char *name;
c906108c 15217
e142c38c 15218 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15219 if (attr)
15220 {
15221 encoding = DW_UNSND (attr);
15222 }
e142c38c 15223 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15224 if (attr)
15225 {
19f392bc 15226 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15227 }
39cbfefa 15228 name = dwarf2_name (die, cu);
6ccb9162 15229 if (!name)
c906108c 15230 {
6ccb9162
UW
15231 complaint (&symfile_complaints,
15232 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15233 }
6ccb9162
UW
15234
15235 switch (encoding)
c906108c 15236 {
6ccb9162
UW
15237 case DW_ATE_address:
15238 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15239 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15240 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15241 break;
15242 case DW_ATE_boolean:
19f392bc 15243 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15244 break;
15245 case DW_ATE_complex_float:
9b790ce7 15246 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15247 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15248 break;
15249 case DW_ATE_decimal_float:
19f392bc 15250 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15251 break;
15252 case DW_ATE_float:
9b790ce7 15253 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15254 break;
15255 case DW_ATE_signed:
19f392bc 15256 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15257 break;
15258 case DW_ATE_unsigned:
3b2b8fea
TT
15259 if (cu->language == language_fortran
15260 && name
61012eef 15261 && startswith (name, "character("))
19f392bc
UW
15262 type = init_character_type (objfile, bits, 1, name);
15263 else
15264 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15265 break;
15266 case DW_ATE_signed_char:
6e70227d 15267 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15268 || cu->language == language_pascal
15269 || cu->language == language_fortran)
19f392bc
UW
15270 type = init_character_type (objfile, bits, 0, name);
15271 else
15272 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15273 break;
15274 case DW_ATE_unsigned_char:
868a0084 15275 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15276 || cu->language == language_pascal
c44af4eb
TT
15277 || cu->language == language_fortran
15278 || cu->language == language_rust)
19f392bc
UW
15279 type = init_character_type (objfile, bits, 1, name);
15280 else
15281 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15282 break;
75079b2b 15283 case DW_ATE_UTF:
53e710ac
PA
15284 {
15285 gdbarch *arch = get_objfile_arch (objfile);
15286
15287 if (bits == 16)
15288 type = builtin_type (arch)->builtin_char16;
15289 else if (bits == 32)
15290 type = builtin_type (arch)->builtin_char32;
15291 else
15292 {
15293 complaint (&symfile_complaints,
15294 _("unsupported DW_ATE_UTF bit size: '%d'"),
15295 bits);
15296 type = init_integer_type (objfile, bits, 1, name);
15297 }
15298 return set_die_type (die, type, cu);
15299 }
75079b2b
TT
15300 break;
15301
6ccb9162
UW
15302 default:
15303 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15304 dwarf_type_encoding_name (encoding));
19f392bc
UW
15305 type = init_type (objfile, TYPE_CODE_ERROR,
15306 bits / TARGET_CHAR_BIT, name);
6ccb9162 15307 break;
c906108c 15308 }
6ccb9162 15309
0114d602 15310 if (name && strcmp (name, "char") == 0)
876cecd0 15311 TYPE_NOSIGN (type) = 1;
0114d602 15312
f792889a 15313 return set_die_type (die, type, cu);
c906108c
SS
15314}
15315
80180f79
SA
15316/* Parse dwarf attribute if it's a block, reference or constant and put the
15317 resulting value of the attribute into struct bound_prop.
15318 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15319
15320static int
15321attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15322 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15323{
15324 struct dwarf2_property_baton *baton;
15325 struct obstack *obstack = &cu->objfile->objfile_obstack;
15326
15327 if (attr == NULL || prop == NULL)
15328 return 0;
15329
15330 if (attr_form_is_block (attr))
15331 {
8d749320 15332 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15333 baton->referenced_type = NULL;
15334 baton->locexpr.per_cu = cu->per_cu;
15335 baton->locexpr.size = DW_BLOCK (attr)->size;
15336 baton->locexpr.data = DW_BLOCK (attr)->data;
15337 prop->data.baton = baton;
15338 prop->kind = PROP_LOCEXPR;
15339 gdb_assert (prop->data.baton != NULL);
15340 }
15341 else if (attr_form_is_ref (attr))
15342 {
15343 struct dwarf2_cu *target_cu = cu;
15344 struct die_info *target_die;
15345 struct attribute *target_attr;
15346
15347 target_die = follow_die_ref (die, attr, &target_cu);
15348 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15349 if (target_attr == NULL)
15350 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15351 target_cu);
80180f79
SA
15352 if (target_attr == NULL)
15353 return 0;
15354
df25ebbd 15355 switch (target_attr->name)
80180f79 15356 {
df25ebbd
JB
15357 case DW_AT_location:
15358 if (attr_form_is_section_offset (target_attr))
15359 {
8d749320 15360 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15361 baton->referenced_type = die_type (target_die, target_cu);
15362 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15363 prop->data.baton = baton;
15364 prop->kind = PROP_LOCLIST;
15365 gdb_assert (prop->data.baton != NULL);
15366 }
15367 else if (attr_form_is_block (target_attr))
15368 {
8d749320 15369 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15370 baton->referenced_type = die_type (target_die, target_cu);
15371 baton->locexpr.per_cu = cu->per_cu;
15372 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15373 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15374 prop->data.baton = baton;
15375 prop->kind = PROP_LOCEXPR;
15376 gdb_assert (prop->data.baton != NULL);
15377 }
15378 else
15379 {
15380 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15381 "dynamic property");
15382 return 0;
15383 }
15384 break;
15385 case DW_AT_data_member_location:
15386 {
15387 LONGEST offset;
15388
15389 if (!handle_data_member_location (target_die, target_cu,
15390 &offset))
15391 return 0;
15392
8d749320 15393 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15394 baton->referenced_type = read_type_die (target_die->parent,
15395 target_cu);
df25ebbd
JB
15396 baton->offset_info.offset = offset;
15397 baton->offset_info.type = die_type (target_die, target_cu);
15398 prop->data.baton = baton;
15399 prop->kind = PROP_ADDR_OFFSET;
15400 break;
15401 }
80180f79
SA
15402 }
15403 }
15404 else if (attr_form_is_constant (attr))
15405 {
15406 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15407 prop->kind = PROP_CONST;
15408 }
15409 else
15410 {
15411 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15412 dwarf2_name (die, cu));
15413 return 0;
15414 }
15415
15416 return 1;
15417}
15418
a02abb62
JB
15419/* Read the given DW_AT_subrange DIE. */
15420
f792889a 15421static struct type *
a02abb62
JB
15422read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15423{
4c9ad8c2 15424 struct type *base_type, *orig_base_type;
a02abb62
JB
15425 struct type *range_type;
15426 struct attribute *attr;
729efb13 15427 struct dynamic_prop low, high;
4fae6e18 15428 int low_default_is_valid;
c451ebe5 15429 int high_bound_is_count = 0;
15d034d0 15430 const char *name;
43bbcdc2 15431 LONGEST negative_mask;
e77813c8 15432
4c9ad8c2
TT
15433 orig_base_type = die_type (die, cu);
15434 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15435 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15436 creating the range type, but we use the result of check_typedef
15437 when examining properties of the type. */
15438 base_type = check_typedef (orig_base_type);
a02abb62 15439
7e314c57
JK
15440 /* The die_type call above may have already set the type for this DIE. */
15441 range_type = get_die_type (die, cu);
15442 if (range_type)
15443 return range_type;
15444
729efb13
SA
15445 low.kind = PROP_CONST;
15446 high.kind = PROP_CONST;
15447 high.data.const_val = 0;
15448
4fae6e18
JK
15449 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15450 omitting DW_AT_lower_bound. */
15451 switch (cu->language)
6e70227d 15452 {
4fae6e18
JK
15453 case language_c:
15454 case language_cplus:
729efb13 15455 low.data.const_val = 0;
4fae6e18
JK
15456 low_default_is_valid = 1;
15457 break;
15458 case language_fortran:
729efb13 15459 low.data.const_val = 1;
4fae6e18
JK
15460 low_default_is_valid = 1;
15461 break;
15462 case language_d:
4fae6e18 15463 case language_objc:
c44af4eb 15464 case language_rust:
729efb13 15465 low.data.const_val = 0;
4fae6e18
JK
15466 low_default_is_valid = (cu->header.version >= 4);
15467 break;
15468 case language_ada:
15469 case language_m2:
15470 case language_pascal:
729efb13 15471 low.data.const_val = 1;
4fae6e18
JK
15472 low_default_is_valid = (cu->header.version >= 4);
15473 break;
15474 default:
729efb13 15475 low.data.const_val = 0;
4fae6e18
JK
15476 low_default_is_valid = 0;
15477 break;
a02abb62
JB
15478 }
15479
e142c38c 15480 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15481 if (attr)
11c1ba78 15482 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15483 else if (!low_default_is_valid)
15484 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15485 "- DIE at 0x%x [in module %s]"),
9c541725 15486 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15487
e142c38c 15488 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15489 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15490 {
15491 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15492 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15493 {
c451ebe5
SA
15494 /* If bounds are constant do the final calculation here. */
15495 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15496 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15497 else
15498 high_bound_is_count = 1;
c2ff108b 15499 }
e77813c8
PM
15500 }
15501
15502 /* Dwarf-2 specifications explicitly allows to create subrange types
15503 without specifying a base type.
15504 In that case, the base type must be set to the type of
15505 the lower bound, upper bound or count, in that order, if any of these
15506 three attributes references an object that has a type.
15507 If no base type is found, the Dwarf-2 specifications say that
15508 a signed integer type of size equal to the size of an address should
15509 be used.
15510 For the following C code: `extern char gdb_int [];'
15511 GCC produces an empty range DIE.
15512 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15513 high bound or count are not yet handled by this code. */
e77813c8
PM
15514 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15515 {
15516 struct objfile *objfile = cu->objfile;
15517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15518 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15519 struct type *int_type = objfile_type (objfile)->builtin_int;
15520
15521 /* Test "int", "long int", and "long long int" objfile types,
15522 and select the first one having a size above or equal to the
15523 architecture address size. */
15524 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15525 base_type = int_type;
15526 else
15527 {
15528 int_type = objfile_type (objfile)->builtin_long;
15529 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15530 base_type = int_type;
15531 else
15532 {
15533 int_type = objfile_type (objfile)->builtin_long_long;
15534 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15535 base_type = int_type;
15536 }
15537 }
15538 }
a02abb62 15539
dbb9c2b1
JB
15540 /* Normally, the DWARF producers are expected to use a signed
15541 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15542 But this is unfortunately not always the case, as witnessed
15543 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15544 is used instead. To work around that ambiguity, we treat
15545 the bounds as signed, and thus sign-extend their values, when
15546 the base type is signed. */
6e70227d 15547 negative_mask =
66c6502d 15548 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15549 if (low.kind == PROP_CONST
15550 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15551 low.data.const_val |= negative_mask;
15552 if (high.kind == PROP_CONST
15553 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15554 high.data.const_val |= negative_mask;
43bbcdc2 15555
729efb13 15556 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15557
c451ebe5
SA
15558 if (high_bound_is_count)
15559 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15560
c2ff108b
JK
15561 /* Ada expects an empty array on no boundary attributes. */
15562 if (attr == NULL && cu->language != language_ada)
729efb13 15563 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15564
39cbfefa
DJ
15565 name = dwarf2_name (die, cu);
15566 if (name)
15567 TYPE_NAME (range_type) = name;
6e70227d 15568
e142c38c 15569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15570 if (attr)
15571 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15572
7e314c57
JK
15573 set_die_type (die, range_type, cu);
15574
15575 /* set_die_type should be already done. */
b4ba55a1
JB
15576 set_descriptive_type (range_type, die, cu);
15577
7e314c57 15578 return range_type;
a02abb62 15579}
6e70227d 15580
f792889a 15581static struct type *
81a17f79
JB
15582read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15583{
15584 struct type *type;
81a17f79 15585
81a17f79
JB
15586 /* For now, we only support the C meaning of an unspecified type: void. */
15587
19f392bc 15588 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15589 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15590
f792889a 15591 return set_die_type (die, type, cu);
81a17f79 15592}
a02abb62 15593
639d11d3
DC
15594/* Read a single die and all its descendents. Set the die's sibling
15595 field to NULL; set other fields in the die correctly, and set all
15596 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15597 location of the info_ptr after reading all of those dies. PARENT
15598 is the parent of the die in question. */
15599
15600static struct die_info *
dee91e82 15601read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15602 const gdb_byte *info_ptr,
15603 const gdb_byte **new_info_ptr,
dee91e82 15604 struct die_info *parent)
639d11d3
DC
15605{
15606 struct die_info *die;
d521ce57 15607 const gdb_byte *cur_ptr;
639d11d3
DC
15608 int has_children;
15609
bf6af496 15610 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15611 if (die == NULL)
15612 {
15613 *new_info_ptr = cur_ptr;
15614 return NULL;
15615 }
93311388 15616 store_in_ref_table (die, reader->cu);
639d11d3
DC
15617
15618 if (has_children)
bf6af496 15619 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15620 else
15621 {
15622 die->child = NULL;
15623 *new_info_ptr = cur_ptr;
15624 }
15625
15626 die->sibling = NULL;
15627 die->parent = parent;
15628 return die;
15629}
15630
15631/* Read a die, all of its descendents, and all of its siblings; set
15632 all of the fields of all of the dies correctly. Arguments are as
15633 in read_die_and_children. */
15634
15635static struct die_info *
bf6af496 15636read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15637 const gdb_byte *info_ptr,
15638 const gdb_byte **new_info_ptr,
bf6af496 15639 struct die_info *parent)
639d11d3
DC
15640{
15641 struct die_info *first_die, *last_sibling;
d521ce57 15642 const gdb_byte *cur_ptr;
639d11d3 15643
c906108c 15644 cur_ptr = info_ptr;
639d11d3
DC
15645 first_die = last_sibling = NULL;
15646
15647 while (1)
c906108c 15648 {
639d11d3 15649 struct die_info *die
dee91e82 15650 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15651
1d325ec1 15652 if (die == NULL)
c906108c 15653 {
639d11d3
DC
15654 *new_info_ptr = cur_ptr;
15655 return first_die;
c906108c 15656 }
1d325ec1
DJ
15657
15658 if (!first_die)
15659 first_die = die;
c906108c 15660 else
1d325ec1
DJ
15661 last_sibling->sibling = die;
15662
15663 last_sibling = die;
c906108c 15664 }
c906108c
SS
15665}
15666
bf6af496
DE
15667/* Read a die, all of its descendents, and all of its siblings; set
15668 all of the fields of all of the dies correctly. Arguments are as
15669 in read_die_and_children.
15670 This the main entry point for reading a DIE and all its children. */
15671
15672static struct die_info *
15673read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15674 const gdb_byte *info_ptr,
15675 const gdb_byte **new_info_ptr,
bf6af496
DE
15676 struct die_info *parent)
15677{
15678 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15679 new_info_ptr, parent);
15680
b4f54984 15681 if (dwarf_die_debug)
bf6af496
DE
15682 {
15683 fprintf_unfiltered (gdb_stdlog,
15684 "Read die from %s@0x%x of %s:\n",
a32a8923 15685 get_section_name (reader->die_section),
bf6af496
DE
15686 (unsigned) (info_ptr - reader->die_section->buffer),
15687 bfd_get_filename (reader->abfd));
b4f54984 15688 dump_die (die, dwarf_die_debug);
bf6af496
DE
15689 }
15690
15691 return die;
15692}
15693
3019eac3
DE
15694/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15695 attributes.
15696 The caller is responsible for filling in the extra attributes
15697 and updating (*DIEP)->num_attrs.
15698 Set DIEP to point to a newly allocated die with its information,
15699 except for its child, sibling, and parent fields.
15700 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15701
d521ce57 15702static const gdb_byte *
3019eac3 15703read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15704 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15705 int *has_children, int num_extra_attrs)
93311388 15706{
b64f50a1 15707 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15708 struct abbrev_info *abbrev;
15709 struct die_info *die;
15710 struct dwarf2_cu *cu = reader->cu;
15711 bfd *abfd = reader->abfd;
15712
9c541725 15713 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15714 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15715 info_ptr += bytes_read;
15716 if (!abbrev_number)
15717 {
15718 *diep = NULL;
15719 *has_children = 0;
15720 return info_ptr;
15721 }
15722
433df2d4 15723 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15724 if (!abbrev)
348e048f
DE
15725 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15726 abbrev_number,
15727 bfd_get_filename (abfd));
15728
3019eac3 15729 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15730 die->sect_off = sect_off;
93311388
DE
15731 die->tag = abbrev->tag;
15732 die->abbrev = abbrev_number;
15733
3019eac3
DE
15734 /* Make the result usable.
15735 The caller needs to update num_attrs after adding the extra
15736 attributes. */
93311388
DE
15737 die->num_attrs = abbrev->num_attrs;
15738
15739 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15740 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15741 info_ptr);
93311388
DE
15742
15743 *diep = die;
15744 *has_children = abbrev->has_children;
15745 return info_ptr;
15746}
15747
3019eac3
DE
15748/* Read a die and all its attributes.
15749 Set DIEP to point to a newly allocated die with its information,
15750 except for its child, sibling, and parent fields.
15751 Set HAS_CHILDREN to tell whether the die has children or not. */
15752
d521ce57 15753static const gdb_byte *
3019eac3 15754read_full_die (const struct die_reader_specs *reader,
d521ce57 15755 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15756 int *has_children)
15757{
d521ce57 15758 const gdb_byte *result;
bf6af496
DE
15759
15760 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15761
b4f54984 15762 if (dwarf_die_debug)
bf6af496
DE
15763 {
15764 fprintf_unfiltered (gdb_stdlog,
15765 "Read die from %s@0x%x of %s:\n",
a32a8923 15766 get_section_name (reader->die_section),
bf6af496
DE
15767 (unsigned) (info_ptr - reader->die_section->buffer),
15768 bfd_get_filename (reader->abfd));
b4f54984 15769 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15770 }
15771
15772 return result;
3019eac3 15773}
433df2d4
DE
15774\f
15775/* Abbreviation tables.
3019eac3 15776
433df2d4 15777 In DWARF version 2, the description of the debugging information is
c906108c
SS
15778 stored in a separate .debug_abbrev section. Before we read any
15779 dies from a section we read in all abbreviations and install them
433df2d4
DE
15780 in a hash table. */
15781
15782/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15783
15784static struct abbrev_info *
15785abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15786{
15787 struct abbrev_info *abbrev;
15788
8d749320 15789 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15790 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15791
433df2d4
DE
15792 return abbrev;
15793}
15794
15795/* Add an abbreviation to the table. */
c906108c
SS
15796
15797static void
433df2d4
DE
15798abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15799 unsigned int abbrev_number,
15800 struct abbrev_info *abbrev)
15801{
15802 unsigned int hash_number;
15803
15804 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15805 abbrev->next = abbrev_table->abbrevs[hash_number];
15806 abbrev_table->abbrevs[hash_number] = abbrev;
15807}
dee91e82 15808
433df2d4
DE
15809/* Look up an abbrev in the table.
15810 Returns NULL if the abbrev is not found. */
15811
15812static struct abbrev_info *
15813abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15814 unsigned int abbrev_number)
c906108c 15815{
433df2d4
DE
15816 unsigned int hash_number;
15817 struct abbrev_info *abbrev;
15818
15819 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15820 abbrev = abbrev_table->abbrevs[hash_number];
15821
15822 while (abbrev)
15823 {
15824 if (abbrev->number == abbrev_number)
15825 return abbrev;
15826 abbrev = abbrev->next;
15827 }
15828 return NULL;
15829}
15830
15831/* Read in an abbrev table. */
15832
15833static struct abbrev_table *
15834abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15835 sect_offset sect_off)
433df2d4
DE
15836{
15837 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15838 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15839 struct abbrev_table *abbrev_table;
d521ce57 15840 const gdb_byte *abbrev_ptr;
c906108c
SS
15841 struct abbrev_info *cur_abbrev;
15842 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15843 unsigned int abbrev_form;
f3dd6933
DJ
15844 struct attr_abbrev *cur_attrs;
15845 unsigned int allocated_attrs;
c906108c 15846
70ba0933 15847 abbrev_table = XNEW (struct abbrev_table);
9c541725 15848 abbrev_table->sect_off = sect_off;
433df2d4 15849 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15850 abbrev_table->abbrevs =
15851 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15852 ABBREV_HASH_SIZE);
433df2d4
DE
15853 memset (abbrev_table->abbrevs, 0,
15854 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15855
433df2d4 15856 dwarf2_read_section (objfile, section);
9c541725 15857 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15858 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15859 abbrev_ptr += bytes_read;
15860
f3dd6933 15861 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15862 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15863
0963b4bd 15864 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15865 while (abbrev_number)
15866 {
433df2d4 15867 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15868
15869 /* read in abbrev header */
15870 cur_abbrev->number = abbrev_number;
aead7601
SM
15871 cur_abbrev->tag
15872 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15873 abbrev_ptr += bytes_read;
15874 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15875 abbrev_ptr += 1;
15876
15877 /* now read in declarations */
22d2f3ab 15878 for (;;)
c906108c 15879 {
43988095
JK
15880 LONGEST implicit_const;
15881
22d2f3ab
JK
15882 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15883 abbrev_ptr += bytes_read;
15884 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15885 abbrev_ptr += bytes_read;
43988095
JK
15886 if (abbrev_form == DW_FORM_implicit_const)
15887 {
15888 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15889 &bytes_read);
15890 abbrev_ptr += bytes_read;
15891 }
15892 else
15893 {
15894 /* Initialize it due to a false compiler warning. */
15895 implicit_const = -1;
15896 }
22d2f3ab
JK
15897
15898 if (abbrev_name == 0)
15899 break;
15900
f3dd6933 15901 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15902 {
f3dd6933
DJ
15903 allocated_attrs += ATTR_ALLOC_CHUNK;
15904 cur_attrs
224c3ddb 15905 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15906 }
ae038cb0 15907
aead7601
SM
15908 cur_attrs[cur_abbrev->num_attrs].name
15909 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15910 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15911 = (enum dwarf_form) abbrev_form;
43988095 15912 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15913 ++cur_abbrev->num_attrs;
c906108c
SS
15914 }
15915
8d749320
SM
15916 cur_abbrev->attrs =
15917 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15918 cur_abbrev->num_attrs);
f3dd6933
DJ
15919 memcpy (cur_abbrev->attrs, cur_attrs,
15920 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15921
433df2d4 15922 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15923
15924 /* Get next abbreviation.
15925 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15926 always properly terminated with an abbrev number of 0.
15927 Exit loop if we encounter an abbreviation which we have
15928 already read (which means we are about to read the abbreviations
15929 for the next compile unit) or if the end of the abbreviation
15930 table is reached. */
433df2d4 15931 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15932 break;
15933 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15934 abbrev_ptr += bytes_read;
433df2d4 15935 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15936 break;
15937 }
f3dd6933
DJ
15938
15939 xfree (cur_attrs);
433df2d4 15940 return abbrev_table;
c906108c
SS
15941}
15942
433df2d4 15943/* Free the resources held by ABBREV_TABLE. */
c906108c 15944
c906108c 15945static void
433df2d4 15946abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15947{
433df2d4
DE
15948 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15949 xfree (abbrev_table);
c906108c
SS
15950}
15951
f4dc4d17
DE
15952/* Same as abbrev_table_free but as a cleanup.
15953 We pass in a pointer to the pointer to the table so that we can
15954 set the pointer to NULL when we're done. It also simplifies
73051182 15955 build_type_psymtabs_1. */
f4dc4d17
DE
15956
15957static void
15958abbrev_table_free_cleanup (void *table_ptr)
15959{
9a3c8263 15960 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15961
15962 if (*abbrev_table_ptr != NULL)
15963 abbrev_table_free (*abbrev_table_ptr);
15964 *abbrev_table_ptr = NULL;
15965}
15966
433df2d4
DE
15967/* Read the abbrev table for CU from ABBREV_SECTION. */
15968
15969static void
15970dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15971 struct dwarf2_section_info *abbrev_section)
c906108c 15972{
433df2d4 15973 cu->abbrev_table =
9c541725 15974 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15975}
c906108c 15976
433df2d4 15977/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15978
433df2d4
DE
15979static void
15980dwarf2_free_abbrev_table (void *ptr_to_cu)
15981{
9a3c8263 15982 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15983
a2ce51a0
DE
15984 if (cu->abbrev_table != NULL)
15985 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15986 /* Set this to NULL so that we SEGV if we try to read it later,
15987 and also because free_comp_unit verifies this is NULL. */
15988 cu->abbrev_table = NULL;
15989}
15990\f
72bf9492
DJ
15991/* Returns nonzero if TAG represents a type that we might generate a partial
15992 symbol for. */
15993
15994static int
15995is_type_tag_for_partial (int tag)
15996{
15997 switch (tag)
15998 {
15999#if 0
16000 /* Some types that would be reasonable to generate partial symbols for,
16001 that we don't at present. */
16002 case DW_TAG_array_type:
16003 case DW_TAG_file_type:
16004 case DW_TAG_ptr_to_member_type:
16005 case DW_TAG_set_type:
16006 case DW_TAG_string_type:
16007 case DW_TAG_subroutine_type:
16008#endif
16009 case DW_TAG_base_type:
16010 case DW_TAG_class_type:
680b30c7 16011 case DW_TAG_interface_type:
72bf9492
DJ
16012 case DW_TAG_enumeration_type:
16013 case DW_TAG_structure_type:
16014 case DW_TAG_subrange_type:
16015 case DW_TAG_typedef:
16016 case DW_TAG_union_type:
16017 return 1;
16018 default:
16019 return 0;
16020 }
16021}
16022
16023/* Load all DIEs that are interesting for partial symbols into memory. */
16024
16025static struct partial_die_info *
dee91e82 16026load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16027 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16028{
dee91e82 16029 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16030 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16031 struct partial_die_info *part_die;
16032 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16033 struct abbrev_info *abbrev;
16034 unsigned int bytes_read;
5afb4e99 16035 unsigned int load_all = 0;
72bf9492
DJ
16036 int nesting_level = 1;
16037
16038 parent_die = NULL;
16039 last_die = NULL;
16040
7adf1e79
DE
16041 gdb_assert (cu->per_cu != NULL);
16042 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16043 load_all = 1;
16044
72bf9492
DJ
16045 cu->partial_dies
16046 = htab_create_alloc_ex (cu->header.length / 12,
16047 partial_die_hash,
16048 partial_die_eq,
16049 NULL,
16050 &cu->comp_unit_obstack,
16051 hashtab_obstack_allocate,
16052 dummy_obstack_deallocate);
16053
8d749320 16054 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16055
16056 while (1)
16057 {
16058 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16059
16060 /* A NULL abbrev means the end of a series of children. */
16061 if (abbrev == NULL)
16062 {
16063 if (--nesting_level == 0)
16064 {
16065 /* PART_DIE was probably the last thing allocated on the
16066 comp_unit_obstack, so we could call obstack_free
16067 here. We don't do that because the waste is small,
16068 and will be cleaned up when we're done with this
16069 compilation unit. This way, we're also more robust
16070 against other users of the comp_unit_obstack. */
16071 return first_die;
16072 }
16073 info_ptr += bytes_read;
16074 last_die = parent_die;
16075 parent_die = parent_die->die_parent;
16076 continue;
16077 }
16078
98bfdba5
PA
16079 /* Check for template arguments. We never save these; if
16080 they're seen, we just mark the parent, and go on our way. */
16081 if (parent_die != NULL
16082 && cu->language == language_cplus
16083 && (abbrev->tag == DW_TAG_template_type_param
16084 || abbrev->tag == DW_TAG_template_value_param))
16085 {
16086 parent_die->has_template_arguments = 1;
16087
16088 if (!load_all)
16089 {
16090 /* We don't need a partial DIE for the template argument. */
dee91e82 16091 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16092 continue;
16093 }
16094 }
16095
0d99eb77 16096 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16097 Skip their other children. */
16098 if (!load_all
16099 && cu->language == language_cplus
16100 && parent_die != NULL
16101 && parent_die->tag == DW_TAG_subprogram)
16102 {
dee91e82 16103 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16104 continue;
16105 }
16106
5afb4e99
DJ
16107 /* Check whether this DIE is interesting enough to save. Normally
16108 we would not be interested in members here, but there may be
16109 later variables referencing them via DW_AT_specification (for
16110 static members). */
16111 if (!load_all
16112 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16113 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16114 && abbrev->tag != DW_TAG_enumerator
16115 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16116 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16117 && abbrev->tag != DW_TAG_variable
5afb4e99 16118 && abbrev->tag != DW_TAG_namespace
f55ee35c 16119 && abbrev->tag != DW_TAG_module
95554aad 16120 && abbrev->tag != DW_TAG_member
74921315
KS
16121 && abbrev->tag != DW_TAG_imported_unit
16122 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16123 {
16124 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16125 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16126 continue;
16127 }
16128
dee91e82
DE
16129 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16130 info_ptr);
72bf9492
DJ
16131
16132 /* This two-pass algorithm for processing partial symbols has a
16133 high cost in cache pressure. Thus, handle some simple cases
16134 here which cover the majority of C partial symbols. DIEs
16135 which neither have specification tags in them, nor could have
16136 specification tags elsewhere pointing at them, can simply be
16137 processed and discarded.
16138
16139 This segment is also optional; scan_partial_symbols and
16140 add_partial_symbol will handle these DIEs if we chain
16141 them in normally. When compilers which do not emit large
16142 quantities of duplicate debug information are more common,
16143 this code can probably be removed. */
16144
16145 /* Any complete simple types at the top level (pretty much all
16146 of them, for a language without namespaces), can be processed
16147 directly. */
16148 if (parent_die == NULL
16149 && part_die->has_specification == 0
16150 && part_die->is_declaration == 0
d8228535 16151 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16152 || part_die->tag == DW_TAG_base_type
16153 || part_die->tag == DW_TAG_subrange_type))
16154 {
16155 if (building_psymtab && part_die->name != NULL)
04a679b8 16156 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16157 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16158 &objfile->static_psymbols,
1762568f 16159 0, cu->language, objfile);
dee91e82 16160 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16161 continue;
16162 }
16163
d8228535
JK
16164 /* The exception for DW_TAG_typedef with has_children above is
16165 a workaround of GCC PR debug/47510. In the case of this complaint
16166 type_name_no_tag_or_error will error on such types later.
16167
16168 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16169 it could not find the child DIEs referenced later, this is checked
16170 above. In correct DWARF DW_TAG_typedef should have no children. */
16171
16172 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16173 complaint (&symfile_complaints,
16174 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16175 "- DIE at 0x%x [in module %s]"),
9c541725 16176 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16177
72bf9492
DJ
16178 /* If we're at the second level, and we're an enumerator, and
16179 our parent has no specification (meaning possibly lives in a
16180 namespace elsewhere), then we can add the partial symbol now
16181 instead of queueing it. */
16182 if (part_die->tag == DW_TAG_enumerator
16183 && parent_die != NULL
16184 && parent_die->die_parent == NULL
16185 && parent_die->tag == DW_TAG_enumeration_type
16186 && parent_die->has_specification == 0)
16187 {
16188 if (part_die->name == NULL)
3e43a32a
MS
16189 complaint (&symfile_complaints,
16190 _("malformed enumerator DIE ignored"));
72bf9492 16191 else if (building_psymtab)
04a679b8 16192 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16193 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16194 cu->language == language_cplus
bb5ed363
DE
16195 ? &objfile->global_psymbols
16196 : &objfile->static_psymbols,
1762568f 16197 0, cu->language, objfile);
72bf9492 16198
dee91e82 16199 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16200 continue;
16201 }
16202
16203 /* We'll save this DIE so link it in. */
16204 part_die->die_parent = parent_die;
16205 part_die->die_sibling = NULL;
16206 part_die->die_child = NULL;
16207
16208 if (last_die && last_die == parent_die)
16209 last_die->die_child = part_die;
16210 else if (last_die)
16211 last_die->die_sibling = part_die;
16212
16213 last_die = part_die;
16214
16215 if (first_die == NULL)
16216 first_die = part_die;
16217
16218 /* Maybe add the DIE to the hash table. Not all DIEs that we
16219 find interesting need to be in the hash table, because we
16220 also have the parent/sibling/child chains; only those that we
16221 might refer to by offset later during partial symbol reading.
16222
16223 For now this means things that might have be the target of a
16224 DW_AT_specification, DW_AT_abstract_origin, or
16225 DW_AT_extension. DW_AT_extension will refer only to
16226 namespaces; DW_AT_abstract_origin refers to functions (and
16227 many things under the function DIE, but we do not recurse
16228 into function DIEs during partial symbol reading) and
16229 possibly variables as well; DW_AT_specification refers to
16230 declarations. Declarations ought to have the DW_AT_declaration
16231 flag. It happens that GCC forgets to put it in sometimes, but
16232 only for functions, not for types.
16233
16234 Adding more things than necessary to the hash table is harmless
16235 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16236 wasted time in find_partial_die, when we reread the compilation
16237 unit with load_all_dies set. */
72bf9492 16238
5afb4e99 16239 if (load_all
72929c62 16240 || abbrev->tag == DW_TAG_constant
5afb4e99 16241 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16242 || abbrev->tag == DW_TAG_variable
16243 || abbrev->tag == DW_TAG_namespace
16244 || part_die->is_declaration)
16245 {
16246 void **slot;
16247
16248 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16249 to_underlying (part_die->sect_off),
16250 INSERT);
72bf9492
DJ
16251 *slot = part_die;
16252 }
16253
8d749320 16254 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16255
16256 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16257 we have no reason to follow the children of structures; for other
98bfdba5
PA
16258 languages we have to, so that we can get at method physnames
16259 to infer fully qualified class names, for DW_AT_specification,
16260 and for C++ template arguments. For C++, we also look one level
16261 inside functions to find template arguments (if the name of the
16262 function does not already contain the template arguments).
bc30ff58
JB
16263
16264 For Ada, we need to scan the children of subprograms and lexical
16265 blocks as well because Ada allows the definition of nested
16266 entities that could be interesting for the debugger, such as
16267 nested subprograms for instance. */
72bf9492 16268 if (last_die->has_children
5afb4e99
DJ
16269 && (load_all
16270 || last_die->tag == DW_TAG_namespace
f55ee35c 16271 || last_die->tag == DW_TAG_module
72bf9492 16272 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16273 || (cu->language == language_cplus
16274 && last_die->tag == DW_TAG_subprogram
16275 && (last_die->name == NULL
16276 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16277 || (cu->language != language_c
16278 && (last_die->tag == DW_TAG_class_type
680b30c7 16279 || last_die->tag == DW_TAG_interface_type
72bf9492 16280 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16281 || last_die->tag == DW_TAG_union_type))
16282 || (cu->language == language_ada
16283 && (last_die->tag == DW_TAG_subprogram
16284 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16285 {
16286 nesting_level++;
16287 parent_die = last_die;
16288 continue;
16289 }
16290
16291 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16292 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16293
16294 /* Back to the top, do it again. */
16295 }
16296}
16297
c906108c
SS
16298/* Read a minimal amount of information into the minimal die structure. */
16299
d521ce57 16300static const gdb_byte *
dee91e82
DE
16301read_partial_die (const struct die_reader_specs *reader,
16302 struct partial_die_info *part_die,
16303 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16304 const gdb_byte *info_ptr)
c906108c 16305{
dee91e82 16306 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16307 struct objfile *objfile = cu->objfile;
d521ce57 16308 const gdb_byte *buffer = reader->buffer;
fa238c03 16309 unsigned int i;
c906108c 16310 struct attribute attr;
c5aa993b 16311 int has_low_pc_attr = 0;
c906108c 16312 int has_high_pc_attr = 0;
91da1414 16313 int high_pc_relative = 0;
c906108c 16314
72bf9492 16315 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16316
9c541725 16317 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16318
16319 info_ptr += abbrev_len;
16320
16321 if (abbrev == NULL)
16322 return info_ptr;
16323
c906108c
SS
16324 part_die->tag = abbrev->tag;
16325 part_die->has_children = abbrev->has_children;
c906108c
SS
16326
16327 for (i = 0; i < abbrev->num_attrs; ++i)
16328 {
dee91e82 16329 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16330
16331 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16332 partial symbol table. */
c906108c
SS
16333 switch (attr.name)
16334 {
16335 case DW_AT_name:
71c25dea
TT
16336 switch (part_die->tag)
16337 {
16338 case DW_TAG_compile_unit:
95554aad 16339 case DW_TAG_partial_unit:
348e048f 16340 case DW_TAG_type_unit:
71c25dea
TT
16341 /* Compilation units have a DW_AT_name that is a filename, not
16342 a source language identifier. */
16343 case DW_TAG_enumeration_type:
16344 case DW_TAG_enumerator:
16345 /* These tags always have simple identifiers already; no need
16346 to canonicalize them. */
16347 part_die->name = DW_STRING (&attr);
16348 break;
16349 default:
16350 part_die->name
16351 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16352 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16353 break;
16354 }
c906108c 16355 break;
31ef98ae 16356 case DW_AT_linkage_name:
c906108c 16357 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16358 /* Note that both forms of linkage name might appear. We
16359 assume they will be the same, and we only store the last
16360 one we see. */
94af9270
KS
16361 if (cu->language == language_ada)
16362 part_die->name = DW_STRING (&attr);
abc72ce4 16363 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16364 break;
16365 case DW_AT_low_pc:
16366 has_low_pc_attr = 1;
31aa7e4e 16367 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16368 break;
16369 case DW_AT_high_pc:
16370 has_high_pc_attr = 1;
31aa7e4e
JB
16371 part_die->highpc = attr_value_as_address (&attr);
16372 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16373 high_pc_relative = 1;
c906108c
SS
16374 break;
16375 case DW_AT_location:
0963b4bd 16376 /* Support the .debug_loc offsets. */
8e19ed76
PS
16377 if (attr_form_is_block (&attr))
16378 {
95554aad 16379 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16380 }
3690dd37 16381 else if (attr_form_is_section_offset (&attr))
8e19ed76 16382 {
4d3c2250 16383 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16384 }
16385 else
16386 {
4d3c2250
KB
16387 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16388 "partial symbol information");
8e19ed76 16389 }
c906108c 16390 break;
c906108c
SS
16391 case DW_AT_external:
16392 part_die->is_external = DW_UNSND (&attr);
16393 break;
16394 case DW_AT_declaration:
16395 part_die->is_declaration = DW_UNSND (&attr);
16396 break;
16397 case DW_AT_type:
16398 part_die->has_type = 1;
16399 break;
16400 case DW_AT_abstract_origin:
16401 case DW_AT_specification:
72bf9492
DJ
16402 case DW_AT_extension:
16403 part_die->has_specification = 1;
c764a876 16404 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16405 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16406 || cu->per_cu->is_dwz);
c906108c
SS
16407 break;
16408 case DW_AT_sibling:
16409 /* Ignore absolute siblings, they might point outside of
16410 the current compile unit. */
16411 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16412 complaint (&symfile_complaints,
16413 _("ignoring absolute DW_AT_sibling"));
c906108c 16414 else
b9502d3f 16415 {
9c541725
PA
16416 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16417 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16418
16419 if (sibling_ptr < info_ptr)
16420 complaint (&symfile_complaints,
16421 _("DW_AT_sibling points backwards"));
22869d73
KS
16422 else if (sibling_ptr > reader->buffer_end)
16423 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16424 else
16425 part_die->sibling = sibling_ptr;
16426 }
c906108c 16427 break;
fa4028e9
JB
16428 case DW_AT_byte_size:
16429 part_die->has_byte_size = 1;
16430 break;
ff908ebf
AW
16431 case DW_AT_const_value:
16432 part_die->has_const_value = 1;
16433 break;
68511cec
CES
16434 case DW_AT_calling_convention:
16435 /* DWARF doesn't provide a way to identify a program's source-level
16436 entry point. DW_AT_calling_convention attributes are only meant
16437 to describe functions' calling conventions.
16438
16439 However, because it's a necessary piece of information in
0c1b455e
TT
16440 Fortran, and before DWARF 4 DW_CC_program was the only
16441 piece of debugging information whose definition refers to
16442 a 'main program' at all, several compilers marked Fortran
16443 main programs with DW_CC_program --- even when those
16444 functions use the standard calling conventions.
16445
16446 Although DWARF now specifies a way to provide this
16447 information, we support this practice for backward
16448 compatibility. */
68511cec 16449 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16450 && cu->language == language_fortran)
16451 part_die->main_subprogram = 1;
68511cec 16452 break;
481860b3
GB
16453 case DW_AT_inline:
16454 if (DW_UNSND (&attr) == DW_INL_inlined
16455 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16456 part_die->may_be_inlined = 1;
16457 break;
95554aad
TT
16458
16459 case DW_AT_import:
16460 if (part_die->tag == DW_TAG_imported_unit)
36586728 16461 {
9c541725 16462 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16463 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16464 || cu->per_cu->is_dwz);
16465 }
95554aad
TT
16466 break;
16467
0c1b455e
TT
16468 case DW_AT_main_subprogram:
16469 part_die->main_subprogram = DW_UNSND (&attr);
16470 break;
16471
c906108c
SS
16472 default:
16473 break;
16474 }
16475 }
16476
91da1414
MW
16477 if (high_pc_relative)
16478 part_die->highpc += part_die->lowpc;
16479
9373cf26
JK
16480 if (has_low_pc_attr && has_high_pc_attr)
16481 {
16482 /* When using the GNU linker, .gnu.linkonce. sections are used to
16483 eliminate duplicate copies of functions and vtables and such.
16484 The linker will arbitrarily choose one and discard the others.
16485 The AT_*_pc values for such functions refer to local labels in
16486 these sections. If the section from that file was discarded, the
16487 labels are not in the output, so the relocs get a value of 0.
16488 If this is a discarded function, mark the pc bounds as invalid,
16489 so that GDB will ignore it. */
16490 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16491 {
bb5ed363 16492 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16493
16494 complaint (&symfile_complaints,
16495 _("DW_AT_low_pc %s is zero "
16496 "for DIE at 0x%x [in module %s]"),
16497 paddress (gdbarch, part_die->lowpc),
9c541725 16498 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16499 }
16500 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16501 else if (part_die->lowpc >= part_die->highpc)
16502 {
bb5ed363 16503 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16504
16505 complaint (&symfile_complaints,
16506 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16507 "for DIE at 0x%x [in module %s]"),
16508 paddress (gdbarch, part_die->lowpc),
16509 paddress (gdbarch, part_die->highpc),
9c541725
PA
16510 to_underlying (part_die->sect_off),
16511 objfile_name (objfile));
9373cf26
JK
16512 }
16513 else
16514 part_die->has_pc_info = 1;
16515 }
85cbf3d3 16516
c906108c
SS
16517 return info_ptr;
16518}
16519
72bf9492
DJ
16520/* Find a cached partial DIE at OFFSET in CU. */
16521
16522static struct partial_die_info *
9c541725 16523find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16524{
16525 struct partial_die_info *lookup_die = NULL;
16526 struct partial_die_info part_die;
16527
9c541725 16528 part_die.sect_off = sect_off;
9a3c8263
SM
16529 lookup_die = ((struct partial_die_info *)
16530 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16531 to_underlying (sect_off)));
72bf9492 16532
72bf9492
DJ
16533 return lookup_die;
16534}
16535
348e048f
DE
16536/* Find a partial DIE at OFFSET, which may or may not be in CU,
16537 except in the case of .debug_types DIEs which do not reference
16538 outside their CU (they do however referencing other types via
55f1336d 16539 DW_FORM_ref_sig8). */
72bf9492
DJ
16540
16541static struct partial_die_info *
9c541725 16542find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16543{
bb5ed363 16544 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16545 struct dwarf2_per_cu_data *per_cu = NULL;
16546 struct partial_die_info *pd = NULL;
72bf9492 16547
36586728 16548 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16549 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16550 {
9c541725 16551 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16552 if (pd != NULL)
16553 return pd;
0d99eb77
DE
16554 /* We missed recording what we needed.
16555 Load all dies and try again. */
16556 per_cu = cu->per_cu;
5afb4e99 16557 }
0d99eb77
DE
16558 else
16559 {
16560 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16561 if (cu->per_cu->is_debug_types)
0d99eb77 16562 {
9c541725
PA
16563 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16564 " external reference to offset 0x%x [in module %s].\n"),
16565 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16566 bfd_get_filename (objfile->obfd));
16567 }
9c541725 16568 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16569 objfile);
72bf9492 16570
0d99eb77
DE
16571 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16572 load_partial_comp_unit (per_cu);
ae038cb0 16573
0d99eb77 16574 per_cu->cu->last_used = 0;
9c541725 16575 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16576 }
5afb4e99 16577
dee91e82
DE
16578 /* If we didn't find it, and not all dies have been loaded,
16579 load them all and try again. */
16580
5afb4e99
DJ
16581 if (pd == NULL && per_cu->load_all_dies == 0)
16582 {
5afb4e99 16583 per_cu->load_all_dies = 1;
fd820528
DE
16584
16585 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16586 THIS_CU->cu may already be in use. So we can't just free it and
16587 replace its DIEs with the ones we read in. Instead, we leave those
16588 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16589 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16590 set. */
dee91e82 16591 load_partial_comp_unit (per_cu);
5afb4e99 16592
9c541725 16593 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16594 }
16595
16596 if (pd == NULL)
16597 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16598 _("could not find partial DIE 0x%x "
16599 "in cache [from module %s]\n"),
9c541725 16600 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16601 return pd;
72bf9492
DJ
16602}
16603
abc72ce4
DE
16604/* See if we can figure out if the class lives in a namespace. We do
16605 this by looking for a member function; its demangled name will
16606 contain namespace info, if there is any. */
16607
16608static void
16609guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16610 struct dwarf2_cu *cu)
16611{
16612 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16613 what template types look like, because the demangler
16614 frequently doesn't give the same name as the debug info. We
16615 could fix this by only using the demangled name to get the
16616 prefix (but see comment in read_structure_type). */
16617
16618 struct partial_die_info *real_pdi;
16619 struct partial_die_info *child_pdi;
16620
16621 /* If this DIE (this DIE's specification, if any) has a parent, then
16622 we should not do this. We'll prepend the parent's fully qualified
16623 name when we create the partial symbol. */
16624
16625 real_pdi = struct_pdi;
16626 while (real_pdi->has_specification)
36586728
TT
16627 real_pdi = find_partial_die (real_pdi->spec_offset,
16628 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16629
16630 if (real_pdi->die_parent != NULL)
16631 return;
16632
16633 for (child_pdi = struct_pdi->die_child;
16634 child_pdi != NULL;
16635 child_pdi = child_pdi->die_sibling)
16636 {
16637 if (child_pdi->tag == DW_TAG_subprogram
16638 && child_pdi->linkage_name != NULL)
16639 {
16640 char *actual_class_name
16641 = language_class_name_from_physname (cu->language_defn,
16642 child_pdi->linkage_name);
16643 if (actual_class_name != NULL)
16644 {
16645 struct_pdi->name
224c3ddb
SM
16646 = ((const char *)
16647 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16648 actual_class_name,
16649 strlen (actual_class_name)));
abc72ce4
DE
16650 xfree (actual_class_name);
16651 }
16652 break;
16653 }
16654 }
16655}
16656
72bf9492
DJ
16657/* Adjust PART_DIE before generating a symbol for it. This function
16658 may set the is_external flag or change the DIE's name. */
16659
16660static void
16661fixup_partial_die (struct partial_die_info *part_die,
16662 struct dwarf2_cu *cu)
16663{
abc72ce4
DE
16664 /* Once we've fixed up a die, there's no point in doing so again.
16665 This also avoids a memory leak if we were to call
16666 guess_partial_die_structure_name multiple times. */
16667 if (part_die->fixup_called)
16668 return;
16669
72bf9492
DJ
16670 /* If we found a reference attribute and the DIE has no name, try
16671 to find a name in the referred to DIE. */
16672
16673 if (part_die->name == NULL && part_die->has_specification)
16674 {
16675 struct partial_die_info *spec_die;
72bf9492 16676
36586728
TT
16677 spec_die = find_partial_die (part_die->spec_offset,
16678 part_die->spec_is_dwz, cu);
72bf9492 16679
10b3939b 16680 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16681
16682 if (spec_die->name)
16683 {
16684 part_die->name = spec_die->name;
16685
16686 /* Copy DW_AT_external attribute if it is set. */
16687 if (spec_die->is_external)
16688 part_die->is_external = spec_die->is_external;
16689 }
16690 }
16691
16692 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16693
16694 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16695 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16696
abc72ce4
DE
16697 /* If there is no parent die to provide a namespace, and there are
16698 children, see if we can determine the namespace from their linkage
122d1940 16699 name. */
abc72ce4 16700 if (cu->language == language_cplus
8b70b953 16701 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16702 && part_die->die_parent == NULL
16703 && part_die->has_children
16704 && (part_die->tag == DW_TAG_class_type
16705 || part_die->tag == DW_TAG_structure_type
16706 || part_die->tag == DW_TAG_union_type))
16707 guess_partial_die_structure_name (part_die, cu);
16708
53832f31
TT
16709 /* GCC might emit a nameless struct or union that has a linkage
16710 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16711 if (part_die->name == NULL
96408a79
SA
16712 && (part_die->tag == DW_TAG_class_type
16713 || part_die->tag == DW_TAG_interface_type
16714 || part_die->tag == DW_TAG_structure_type
16715 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16716 && part_die->linkage_name != NULL)
16717 {
16718 char *demangled;
16719
8de20a37 16720 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16721 if (demangled)
16722 {
96408a79
SA
16723 const char *base;
16724
16725 /* Strip any leading namespaces/classes, keep only the base name.
16726 DW_AT_name for named DIEs does not contain the prefixes. */
16727 base = strrchr (demangled, ':');
16728 if (base && base > demangled && base[-1] == ':')
16729 base++;
16730 else
16731 base = demangled;
16732
34a68019 16733 part_die->name
224c3ddb
SM
16734 = ((const char *)
16735 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16736 base, strlen (base)));
53832f31
TT
16737 xfree (demangled);
16738 }
16739 }
16740
abc72ce4 16741 part_die->fixup_called = 1;
72bf9492
DJ
16742}
16743
a8329558 16744/* Read an attribute value described by an attribute form. */
c906108c 16745
d521ce57 16746static const gdb_byte *
dee91e82
DE
16747read_attribute_value (const struct die_reader_specs *reader,
16748 struct attribute *attr, unsigned form,
43988095 16749 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16750{
dee91e82 16751 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16752 struct objfile *objfile = cu->objfile;
16753 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16754 bfd *abfd = reader->abfd;
e7c27a73 16755 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16756 unsigned int bytes_read;
16757 struct dwarf_block *blk;
16758
aead7601 16759 attr->form = (enum dwarf_form) form;
a8329558 16760 switch (form)
c906108c 16761 {
c906108c 16762 case DW_FORM_ref_addr:
ae411497 16763 if (cu->header.version == 2)
4568ecf9 16764 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16765 else
4568ecf9
DE
16766 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16767 &cu->header, &bytes_read);
ae411497
TT
16768 info_ptr += bytes_read;
16769 break;
36586728
TT
16770 case DW_FORM_GNU_ref_alt:
16771 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16772 info_ptr += bytes_read;
16773 break;
ae411497 16774 case DW_FORM_addr:
e7c27a73 16775 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16776 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16777 info_ptr += bytes_read;
c906108c
SS
16778 break;
16779 case DW_FORM_block2:
7b5a2f43 16780 blk = dwarf_alloc_block (cu);
c906108c
SS
16781 blk->size = read_2_bytes (abfd, info_ptr);
16782 info_ptr += 2;
16783 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16784 info_ptr += blk->size;
16785 DW_BLOCK (attr) = blk;
16786 break;
16787 case DW_FORM_block4:
7b5a2f43 16788 blk = dwarf_alloc_block (cu);
c906108c
SS
16789 blk->size = read_4_bytes (abfd, info_ptr);
16790 info_ptr += 4;
16791 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16792 info_ptr += blk->size;
16793 DW_BLOCK (attr) = blk;
16794 break;
16795 case DW_FORM_data2:
16796 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16797 info_ptr += 2;
16798 break;
16799 case DW_FORM_data4:
16800 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16801 info_ptr += 4;
16802 break;
16803 case DW_FORM_data8:
16804 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16805 info_ptr += 8;
16806 break;
0224619f
JK
16807 case DW_FORM_data16:
16808 blk = dwarf_alloc_block (cu);
16809 blk->size = 16;
16810 blk->data = read_n_bytes (abfd, info_ptr, 16);
16811 info_ptr += 16;
16812 DW_BLOCK (attr) = blk;
16813 break;
2dc7f7b3
TT
16814 case DW_FORM_sec_offset:
16815 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16816 info_ptr += bytes_read;
16817 break;
c906108c 16818 case DW_FORM_string:
9b1c24c8 16819 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16820 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16821 info_ptr += bytes_read;
16822 break;
4bdf3d34 16823 case DW_FORM_strp:
36586728
TT
16824 if (!cu->per_cu->is_dwz)
16825 {
16826 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16827 &bytes_read);
16828 DW_STRING_IS_CANONICAL (attr) = 0;
16829 info_ptr += bytes_read;
16830 break;
16831 }
16832 /* FALLTHROUGH */
43988095
JK
16833 case DW_FORM_line_strp:
16834 if (!cu->per_cu->is_dwz)
16835 {
16836 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16837 cu_header, &bytes_read);
16838 DW_STRING_IS_CANONICAL (attr) = 0;
16839 info_ptr += bytes_read;
16840 break;
16841 }
16842 /* FALLTHROUGH */
36586728
TT
16843 case DW_FORM_GNU_strp_alt:
16844 {
16845 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16846 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16847 &bytes_read);
16848
16849 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16850 DW_STRING_IS_CANONICAL (attr) = 0;
16851 info_ptr += bytes_read;
16852 }
4bdf3d34 16853 break;
2dc7f7b3 16854 case DW_FORM_exprloc:
c906108c 16855 case DW_FORM_block:
7b5a2f43 16856 blk = dwarf_alloc_block (cu);
c906108c
SS
16857 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16858 info_ptr += bytes_read;
16859 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16860 info_ptr += blk->size;
16861 DW_BLOCK (attr) = blk;
16862 break;
16863 case DW_FORM_block1:
7b5a2f43 16864 blk = dwarf_alloc_block (cu);
c906108c
SS
16865 blk->size = read_1_byte (abfd, info_ptr);
16866 info_ptr += 1;
16867 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16868 info_ptr += blk->size;
16869 DW_BLOCK (attr) = blk;
16870 break;
16871 case DW_FORM_data1:
16872 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16873 info_ptr += 1;
16874 break;
16875 case DW_FORM_flag:
16876 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16877 info_ptr += 1;
16878 break;
2dc7f7b3
TT
16879 case DW_FORM_flag_present:
16880 DW_UNSND (attr) = 1;
16881 break;
c906108c
SS
16882 case DW_FORM_sdata:
16883 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16884 info_ptr += bytes_read;
16885 break;
16886 case DW_FORM_udata:
16887 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16888 info_ptr += bytes_read;
16889 break;
16890 case DW_FORM_ref1:
9c541725 16891 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16892 + read_1_byte (abfd, info_ptr));
c906108c
SS
16893 info_ptr += 1;
16894 break;
16895 case DW_FORM_ref2:
9c541725 16896 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16897 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16898 info_ptr += 2;
16899 break;
16900 case DW_FORM_ref4:
9c541725 16901 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16902 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16903 info_ptr += 4;
16904 break;
613e1657 16905 case DW_FORM_ref8:
9c541725 16906 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16907 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16908 info_ptr += 8;
16909 break;
55f1336d 16910 case DW_FORM_ref_sig8:
ac9ec31b 16911 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16912 info_ptr += 8;
16913 break;
c906108c 16914 case DW_FORM_ref_udata:
9c541725 16915 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16916 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16917 info_ptr += bytes_read;
16918 break;
c906108c 16919 case DW_FORM_indirect:
a8329558
KW
16920 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16921 info_ptr += bytes_read;
43988095
JK
16922 if (form == DW_FORM_implicit_const)
16923 {
16924 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16925 info_ptr += bytes_read;
16926 }
16927 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16928 info_ptr);
16929 break;
16930 case DW_FORM_implicit_const:
16931 DW_SND (attr) = implicit_const;
a8329558 16932 break;
3019eac3
DE
16933 case DW_FORM_GNU_addr_index:
16934 if (reader->dwo_file == NULL)
16935 {
16936 /* For now flag a hard error.
16937 Later we can turn this into a complaint. */
16938 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16939 dwarf_form_name (form),
16940 bfd_get_filename (abfd));
16941 }
16942 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16943 info_ptr += bytes_read;
16944 break;
16945 case DW_FORM_GNU_str_index:
16946 if (reader->dwo_file == NULL)
16947 {
16948 /* For now flag a hard error.
16949 Later we can turn this into a complaint if warranted. */
16950 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16951 dwarf_form_name (form),
16952 bfd_get_filename (abfd));
16953 }
16954 {
16955 ULONGEST str_index =
16956 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16957
342587c4 16958 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16959 DW_STRING_IS_CANONICAL (attr) = 0;
16960 info_ptr += bytes_read;
16961 }
16962 break;
c906108c 16963 default:
8a3fe4f8 16964 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16965 dwarf_form_name (form),
16966 bfd_get_filename (abfd));
c906108c 16967 }
28e94949 16968
36586728 16969 /* Super hack. */
7771576e 16970 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16971 attr->form = DW_FORM_GNU_ref_alt;
16972
28e94949
JB
16973 /* We have seen instances where the compiler tried to emit a byte
16974 size attribute of -1 which ended up being encoded as an unsigned
16975 0xffffffff. Although 0xffffffff is technically a valid size value,
16976 an object of this size seems pretty unlikely so we can relatively
16977 safely treat these cases as if the size attribute was invalid and
16978 treat them as zero by default. */
16979 if (attr->name == DW_AT_byte_size
16980 && form == DW_FORM_data4
16981 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16982 {
16983 complaint
16984 (&symfile_complaints,
43bbcdc2
PH
16985 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16986 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16987 DW_UNSND (attr) = 0;
16988 }
28e94949 16989
c906108c
SS
16990 return info_ptr;
16991}
16992
a8329558
KW
16993/* Read an attribute described by an abbreviated attribute. */
16994
d521ce57 16995static const gdb_byte *
dee91e82
DE
16996read_attribute (const struct die_reader_specs *reader,
16997 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16998 const gdb_byte *info_ptr)
a8329558
KW
16999{
17000 attr->name = abbrev->name;
43988095
JK
17001 return read_attribute_value (reader, attr, abbrev->form,
17002 abbrev->implicit_const, info_ptr);
a8329558
KW
17003}
17004
0963b4bd 17005/* Read dwarf information from a buffer. */
c906108c
SS
17006
17007static unsigned int
a1855c1d 17008read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17009{
fe1b8b76 17010 return bfd_get_8 (abfd, buf);
c906108c
SS
17011}
17012
17013static int
a1855c1d 17014read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17015{
fe1b8b76 17016 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17017}
17018
17019static unsigned int
a1855c1d 17020read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17021{
fe1b8b76 17022 return bfd_get_16 (abfd, buf);
c906108c
SS
17023}
17024
21ae7a4d 17025static int
a1855c1d 17026read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17027{
17028 return bfd_get_signed_16 (abfd, buf);
17029}
17030
c906108c 17031static unsigned int
a1855c1d 17032read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17033{
fe1b8b76 17034 return bfd_get_32 (abfd, buf);
c906108c
SS
17035}
17036
21ae7a4d 17037static int
a1855c1d 17038read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17039{
17040 return bfd_get_signed_32 (abfd, buf);
17041}
17042
93311388 17043static ULONGEST
a1855c1d 17044read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17045{
fe1b8b76 17046 return bfd_get_64 (abfd, buf);
c906108c
SS
17047}
17048
17049static CORE_ADDR
d521ce57 17050read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17051 unsigned int *bytes_read)
c906108c 17052{
e7c27a73 17053 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17054 CORE_ADDR retval = 0;
17055
107d2387 17056 if (cu_header->signed_addr_p)
c906108c 17057 {
107d2387
AC
17058 switch (cu_header->addr_size)
17059 {
17060 case 2:
fe1b8b76 17061 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17062 break;
17063 case 4:
fe1b8b76 17064 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17065 break;
17066 case 8:
fe1b8b76 17067 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17068 break;
17069 default:
8e65ff28 17070 internal_error (__FILE__, __LINE__,
e2e0b3e5 17071 _("read_address: bad switch, signed [in module %s]"),
659b0389 17072 bfd_get_filename (abfd));
107d2387
AC
17073 }
17074 }
17075 else
17076 {
17077 switch (cu_header->addr_size)
17078 {
17079 case 2:
fe1b8b76 17080 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17081 break;
17082 case 4:
fe1b8b76 17083 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17084 break;
17085 case 8:
fe1b8b76 17086 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17087 break;
17088 default:
8e65ff28 17089 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17090 _("read_address: bad switch, "
17091 "unsigned [in module %s]"),
659b0389 17092 bfd_get_filename (abfd));
107d2387 17093 }
c906108c 17094 }
64367e0a 17095
107d2387
AC
17096 *bytes_read = cu_header->addr_size;
17097 return retval;
c906108c
SS
17098}
17099
f7ef9339 17100/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17101 specification allows the initial length to take up either 4 bytes
17102 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17103 bytes describe the length and all offsets will be 8 bytes in length
17104 instead of 4.
17105
f7ef9339
KB
17106 An older, non-standard 64-bit format is also handled by this
17107 function. The older format in question stores the initial length
17108 as an 8-byte quantity without an escape value. Lengths greater
17109 than 2^32 aren't very common which means that the initial 4 bytes
17110 is almost always zero. Since a length value of zero doesn't make
17111 sense for the 32-bit format, this initial zero can be considered to
17112 be an escape value which indicates the presence of the older 64-bit
17113 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17114 greater than 4GB. If it becomes necessary to handle lengths
17115 somewhat larger than 4GB, we could allow other small values (such
17116 as the non-sensical values of 1, 2, and 3) to also be used as
17117 escape values indicating the presence of the old format.
f7ef9339 17118
917c78fc
MK
17119 The value returned via bytes_read should be used to increment the
17120 relevant pointer after calling read_initial_length().
c764a876 17121
613e1657
KB
17122 [ Note: read_initial_length() and read_offset() are based on the
17123 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17124 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17125 from:
17126
f7ef9339 17127 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17128
613e1657
KB
17129 This document is only a draft and is subject to change. (So beware.)
17130
f7ef9339 17131 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17132 determined empirically by examining 64-bit ELF files produced by
17133 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17134
17135 - Kevin, July 16, 2002
613e1657
KB
17136 ] */
17137
17138static LONGEST
d521ce57 17139read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17140{
fe1b8b76 17141 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17142
dd373385 17143 if (length == 0xffffffff)
613e1657 17144 {
fe1b8b76 17145 length = bfd_get_64 (abfd, buf + 4);
613e1657 17146 *bytes_read = 12;
613e1657 17147 }
dd373385 17148 else if (length == 0)
f7ef9339 17149 {
dd373385 17150 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17151 length = bfd_get_64 (abfd, buf);
f7ef9339 17152 *bytes_read = 8;
f7ef9339 17153 }
613e1657
KB
17154 else
17155 {
17156 *bytes_read = 4;
613e1657
KB
17157 }
17158
c764a876
DE
17159 return length;
17160}
dd373385 17161
c764a876
DE
17162/* Cover function for read_initial_length.
17163 Returns the length of the object at BUF, and stores the size of the
17164 initial length in *BYTES_READ and stores the size that offsets will be in
17165 *OFFSET_SIZE.
17166 If the initial length size is not equivalent to that specified in
17167 CU_HEADER then issue a complaint.
17168 This is useful when reading non-comp-unit headers. */
dd373385 17169
c764a876 17170static LONGEST
d521ce57 17171read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17172 const struct comp_unit_head *cu_header,
17173 unsigned int *bytes_read,
17174 unsigned int *offset_size)
17175{
17176 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17177
17178 gdb_assert (cu_header->initial_length_size == 4
17179 || cu_header->initial_length_size == 8
17180 || cu_header->initial_length_size == 12);
17181
17182 if (cu_header->initial_length_size != *bytes_read)
17183 complaint (&symfile_complaints,
17184 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17185
c764a876 17186 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17187 return length;
613e1657
KB
17188}
17189
17190/* Read an offset from the data stream. The size of the offset is
917c78fc 17191 given by cu_header->offset_size. */
613e1657
KB
17192
17193static LONGEST
d521ce57
TT
17194read_offset (bfd *abfd, const gdb_byte *buf,
17195 const struct comp_unit_head *cu_header,
891d2f0b 17196 unsigned int *bytes_read)
c764a876
DE
17197{
17198 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17199
c764a876
DE
17200 *bytes_read = cu_header->offset_size;
17201 return offset;
17202}
17203
17204/* Read an offset from the data stream. */
17205
17206static LONGEST
d521ce57 17207read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17208{
17209 LONGEST retval = 0;
17210
c764a876 17211 switch (offset_size)
613e1657
KB
17212 {
17213 case 4:
fe1b8b76 17214 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17215 break;
17216 case 8:
fe1b8b76 17217 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17218 break;
17219 default:
8e65ff28 17220 internal_error (__FILE__, __LINE__,
c764a876 17221 _("read_offset_1: bad switch [in module %s]"),
659b0389 17222 bfd_get_filename (abfd));
613e1657
KB
17223 }
17224
917c78fc 17225 return retval;
613e1657
KB
17226}
17227
d521ce57
TT
17228static const gdb_byte *
17229read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17230{
17231 /* If the size of a host char is 8 bits, we can return a pointer
17232 to the buffer, otherwise we have to copy the data to a buffer
17233 allocated on the temporary obstack. */
4bdf3d34 17234 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17235 return buf;
c906108c
SS
17236}
17237
d521ce57
TT
17238static const char *
17239read_direct_string (bfd *abfd, const gdb_byte *buf,
17240 unsigned int *bytes_read_ptr)
c906108c
SS
17241{
17242 /* If the size of a host char is 8 bits, we can return a pointer
17243 to the string, otherwise we have to copy the string to a buffer
17244 allocated on the temporary obstack. */
4bdf3d34 17245 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17246 if (*buf == '\0')
17247 {
17248 *bytes_read_ptr = 1;
17249 return NULL;
17250 }
d521ce57
TT
17251 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17252 return (const char *) buf;
4bdf3d34
JJ
17253}
17254
43988095
JK
17255/* Return pointer to string at section SECT offset STR_OFFSET with error
17256 reporting strings FORM_NAME and SECT_NAME. */
17257
d521ce57 17258static const char *
43988095
JK
17259read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17260 struct dwarf2_section_info *sect,
17261 const char *form_name,
17262 const char *sect_name)
17263{
17264 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17265 if (sect->buffer == NULL)
17266 error (_("%s used without %s section [in module %s]"),
17267 form_name, sect_name, bfd_get_filename (abfd));
17268 if (str_offset >= sect->size)
17269 error (_("%s pointing outside of %s section [in module %s]"),
17270 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17271 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17272 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17273 return NULL;
43988095
JK
17274 return (const char *) (sect->buffer + str_offset);
17275}
17276
17277/* Return pointer to string at .debug_str offset STR_OFFSET. */
17278
17279static const char *
17280read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17281{
17282 return read_indirect_string_at_offset_from (abfd, str_offset,
17283 &dwarf2_per_objfile->str,
17284 "DW_FORM_strp", ".debug_str");
17285}
17286
17287/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17288
17289static const char *
17290read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17291{
17292 return read_indirect_string_at_offset_from (abfd, str_offset,
17293 &dwarf2_per_objfile->line_str,
17294 "DW_FORM_line_strp",
17295 ".debug_line_str");
c906108c
SS
17296}
17297
36586728
TT
17298/* Read a string at offset STR_OFFSET in the .debug_str section from
17299 the .dwz file DWZ. Throw an error if the offset is too large. If
17300 the string consists of a single NUL byte, return NULL; otherwise
17301 return a pointer to the string. */
17302
d521ce57 17303static const char *
36586728
TT
17304read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17305{
17306 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17307
17308 if (dwz->str.buffer == NULL)
17309 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17310 "section [in module %s]"),
17311 bfd_get_filename (dwz->dwz_bfd));
17312 if (str_offset >= dwz->str.size)
17313 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17314 ".debug_str section [in module %s]"),
17315 bfd_get_filename (dwz->dwz_bfd));
17316 gdb_assert (HOST_CHAR_BIT == 8);
17317 if (dwz->str.buffer[str_offset] == '\0')
17318 return NULL;
d521ce57 17319 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17320}
17321
43988095
JK
17322/* Return pointer to string at .debug_str offset as read from BUF.
17323 BUF is assumed to be in a compilation unit described by CU_HEADER.
17324 Return *BYTES_READ_PTR count of bytes read from BUF. */
17325
d521ce57
TT
17326static const char *
17327read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17328 const struct comp_unit_head *cu_header,
17329 unsigned int *bytes_read_ptr)
17330{
17331 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17332
17333 return read_indirect_string_at_offset (abfd, str_offset);
17334}
17335
43988095
JK
17336/* Return pointer to string at .debug_line_str offset as read from BUF.
17337 BUF is assumed to be in a compilation unit described by CU_HEADER.
17338 Return *BYTES_READ_PTR count of bytes read from BUF. */
17339
17340static const char *
17341read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17342 const struct comp_unit_head *cu_header,
17343 unsigned int *bytes_read_ptr)
17344{
17345 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17346
17347 return read_indirect_line_string_at_offset (abfd, str_offset);
17348}
17349
17350ULONGEST
d521ce57 17351read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17352 unsigned int *bytes_read_ptr)
c906108c 17353{
12df843f 17354 ULONGEST result;
ce5d95e1 17355 unsigned int num_read;
870f88f7 17356 int shift;
c906108c
SS
17357 unsigned char byte;
17358
17359 result = 0;
17360 shift = 0;
17361 num_read = 0;
c906108c
SS
17362 while (1)
17363 {
fe1b8b76 17364 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17365 buf++;
17366 num_read++;
12df843f 17367 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17368 if ((byte & 128) == 0)
17369 {
17370 break;
17371 }
17372 shift += 7;
17373 }
17374 *bytes_read_ptr = num_read;
17375 return result;
17376}
17377
12df843f 17378static LONGEST
d521ce57
TT
17379read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17380 unsigned int *bytes_read_ptr)
c906108c 17381{
12df843f 17382 LONGEST result;
870f88f7 17383 int shift, num_read;
c906108c
SS
17384 unsigned char byte;
17385
17386 result = 0;
17387 shift = 0;
c906108c 17388 num_read = 0;
c906108c
SS
17389 while (1)
17390 {
fe1b8b76 17391 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17392 buf++;
17393 num_read++;
12df843f 17394 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17395 shift += 7;
17396 if ((byte & 128) == 0)
17397 {
17398 break;
17399 }
17400 }
77e0b926 17401 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17402 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17403 *bytes_read_ptr = num_read;
17404 return result;
17405}
17406
3019eac3
DE
17407/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17408 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17409 ADDR_SIZE is the size of addresses from the CU header. */
17410
17411static CORE_ADDR
17412read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17413{
17414 struct objfile *objfile = dwarf2_per_objfile->objfile;
17415 bfd *abfd = objfile->obfd;
17416 const gdb_byte *info_ptr;
17417
17418 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17419 if (dwarf2_per_objfile->addr.buffer == NULL)
17420 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17421 objfile_name (objfile));
3019eac3
DE
17422 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17423 error (_("DW_FORM_addr_index pointing outside of "
17424 ".debug_addr section [in module %s]"),
4262abfb 17425 objfile_name (objfile));
3019eac3
DE
17426 info_ptr = (dwarf2_per_objfile->addr.buffer
17427 + addr_base + addr_index * addr_size);
17428 if (addr_size == 4)
17429 return bfd_get_32 (abfd, info_ptr);
17430 else
17431 return bfd_get_64 (abfd, info_ptr);
17432}
17433
17434/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17435
17436static CORE_ADDR
17437read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17438{
17439 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17440}
17441
17442/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17443
17444static CORE_ADDR
d521ce57 17445read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17446 unsigned int *bytes_read)
17447{
17448 bfd *abfd = cu->objfile->obfd;
17449 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17450
17451 return read_addr_index (cu, addr_index);
17452}
17453
17454/* Data structure to pass results from dwarf2_read_addr_index_reader
17455 back to dwarf2_read_addr_index. */
17456
17457struct dwarf2_read_addr_index_data
17458{
17459 ULONGEST addr_base;
17460 int addr_size;
17461};
17462
17463/* die_reader_func for dwarf2_read_addr_index. */
17464
17465static void
17466dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17467 const gdb_byte *info_ptr,
3019eac3
DE
17468 struct die_info *comp_unit_die,
17469 int has_children,
17470 void *data)
17471{
17472 struct dwarf2_cu *cu = reader->cu;
17473 struct dwarf2_read_addr_index_data *aidata =
17474 (struct dwarf2_read_addr_index_data *) data;
17475
17476 aidata->addr_base = cu->addr_base;
17477 aidata->addr_size = cu->header.addr_size;
17478}
17479
17480/* Given an index in .debug_addr, fetch the value.
17481 NOTE: This can be called during dwarf expression evaluation,
17482 long after the debug information has been read, and thus per_cu->cu
17483 may no longer exist. */
17484
17485CORE_ADDR
17486dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17487 unsigned int addr_index)
17488{
17489 struct objfile *objfile = per_cu->objfile;
17490 struct dwarf2_cu *cu = per_cu->cu;
17491 ULONGEST addr_base;
17492 int addr_size;
17493
17494 /* This is intended to be called from outside this file. */
17495 dw2_setup (objfile);
17496
17497 /* We need addr_base and addr_size.
17498 If we don't have PER_CU->cu, we have to get it.
17499 Nasty, but the alternative is storing the needed info in PER_CU,
17500 which at this point doesn't seem justified: it's not clear how frequently
17501 it would get used and it would increase the size of every PER_CU.
17502 Entry points like dwarf2_per_cu_addr_size do a similar thing
17503 so we're not in uncharted territory here.
17504 Alas we need to be a bit more complicated as addr_base is contained
17505 in the DIE.
17506
17507 We don't need to read the entire CU(/TU).
17508 We just need the header and top level die.
a1b64ce1 17509
3019eac3 17510 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17511 For now we skip this optimization. */
3019eac3
DE
17512
17513 if (cu != NULL)
17514 {
17515 addr_base = cu->addr_base;
17516 addr_size = cu->header.addr_size;
17517 }
17518 else
17519 {
17520 struct dwarf2_read_addr_index_data aidata;
17521
a1b64ce1
DE
17522 /* Note: We can't use init_cutu_and_read_dies_simple here,
17523 we need addr_base. */
17524 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17525 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17526 addr_base = aidata.addr_base;
17527 addr_size = aidata.addr_size;
17528 }
17529
17530 return read_addr_index_1 (addr_index, addr_base, addr_size);
17531}
17532
57d63ce2
DE
17533/* Given a DW_FORM_GNU_str_index, fetch the string.
17534 This is only used by the Fission support. */
3019eac3 17535
d521ce57 17536static const char *
342587c4 17537read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17538{
17539 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17540 const char *objf_name = objfile_name (objfile);
3019eac3 17541 bfd *abfd = objfile->obfd;
342587c4 17542 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17543 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17544 struct dwarf2_section_info *str_offsets_section =
17545 &reader->dwo_file->sections.str_offsets;
d521ce57 17546 const gdb_byte *info_ptr;
3019eac3 17547 ULONGEST str_offset;
57d63ce2 17548 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17549
73869dc2
DE
17550 dwarf2_read_section (objfile, str_section);
17551 dwarf2_read_section (objfile, str_offsets_section);
17552 if (str_section->buffer == NULL)
57d63ce2 17553 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17554 " in CU at offset 0x%x [in module %s]"),
17555 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17556 if (str_offsets_section->buffer == NULL)
57d63ce2 17557 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17558 " in CU at offset 0x%x [in module %s]"),
17559 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17560 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17561 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17562 " section in CU at offset 0x%x [in module %s]"),
17563 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17564 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17565 + str_index * cu->header.offset_size);
17566 if (cu->header.offset_size == 4)
17567 str_offset = bfd_get_32 (abfd, info_ptr);
17568 else
17569 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17570 if (str_offset >= str_section->size)
57d63ce2 17571 error (_("Offset from %s pointing outside of"
9c541725
PA
17572 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17573 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17574 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17575}
17576
3019eac3
DE
17577/* Return the length of an LEB128 number in BUF. */
17578
17579static int
17580leb128_size (const gdb_byte *buf)
17581{
17582 const gdb_byte *begin = buf;
17583 gdb_byte byte;
17584
17585 while (1)
17586 {
17587 byte = *buf++;
17588 if ((byte & 128) == 0)
17589 return buf - begin;
17590 }
17591}
17592
c906108c 17593static void
e142c38c 17594set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17595{
17596 switch (lang)
17597 {
17598 case DW_LANG_C89:
76bee0cc 17599 case DW_LANG_C99:
0cfd832f 17600 case DW_LANG_C11:
c906108c 17601 case DW_LANG_C:
d1be3247 17602 case DW_LANG_UPC:
e142c38c 17603 cu->language = language_c;
c906108c 17604 break;
9c37b5ae 17605 case DW_LANG_Java:
c906108c 17606 case DW_LANG_C_plus_plus:
0cfd832f
MW
17607 case DW_LANG_C_plus_plus_11:
17608 case DW_LANG_C_plus_plus_14:
e142c38c 17609 cu->language = language_cplus;
c906108c 17610 break;
6aecb9c2
JB
17611 case DW_LANG_D:
17612 cu->language = language_d;
17613 break;
c906108c
SS
17614 case DW_LANG_Fortran77:
17615 case DW_LANG_Fortran90:
b21b22e0 17616 case DW_LANG_Fortran95:
f7de9aab
MW
17617 case DW_LANG_Fortran03:
17618 case DW_LANG_Fortran08:
e142c38c 17619 cu->language = language_fortran;
c906108c 17620 break;
a766d390
DE
17621 case DW_LANG_Go:
17622 cu->language = language_go;
17623 break;
c906108c 17624 case DW_LANG_Mips_Assembler:
e142c38c 17625 cu->language = language_asm;
c906108c
SS
17626 break;
17627 case DW_LANG_Ada83:
8aaf0b47 17628 case DW_LANG_Ada95:
bc5f45f8
JB
17629 cu->language = language_ada;
17630 break;
72019c9c
GM
17631 case DW_LANG_Modula2:
17632 cu->language = language_m2;
17633 break;
fe8e67fd
PM
17634 case DW_LANG_Pascal83:
17635 cu->language = language_pascal;
17636 break;
22566fbd
DJ
17637 case DW_LANG_ObjC:
17638 cu->language = language_objc;
17639 break;
c44af4eb
TT
17640 case DW_LANG_Rust:
17641 case DW_LANG_Rust_old:
17642 cu->language = language_rust;
17643 break;
c906108c
SS
17644 case DW_LANG_Cobol74:
17645 case DW_LANG_Cobol85:
c906108c 17646 default:
e142c38c 17647 cu->language = language_minimal;
c906108c
SS
17648 break;
17649 }
e142c38c 17650 cu->language_defn = language_def (cu->language);
c906108c
SS
17651}
17652
17653/* Return the named attribute or NULL if not there. */
17654
17655static struct attribute *
e142c38c 17656dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17657{
a48e046c 17658 for (;;)
c906108c 17659 {
a48e046c
TT
17660 unsigned int i;
17661 struct attribute *spec = NULL;
17662
17663 for (i = 0; i < die->num_attrs; ++i)
17664 {
17665 if (die->attrs[i].name == name)
17666 return &die->attrs[i];
17667 if (die->attrs[i].name == DW_AT_specification
17668 || die->attrs[i].name == DW_AT_abstract_origin)
17669 spec = &die->attrs[i];
17670 }
17671
17672 if (!spec)
17673 break;
c906108c 17674
f2f0e013 17675 die = follow_die_ref (die, spec, &cu);
f2f0e013 17676 }
c5aa993b 17677
c906108c
SS
17678 return NULL;
17679}
17680
348e048f
DE
17681/* Return the named attribute or NULL if not there,
17682 but do not follow DW_AT_specification, etc.
17683 This is for use in contexts where we're reading .debug_types dies.
17684 Following DW_AT_specification, DW_AT_abstract_origin will take us
17685 back up the chain, and we want to go down. */
17686
17687static struct attribute *
45e58e77 17688dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17689{
17690 unsigned int i;
17691
17692 for (i = 0; i < die->num_attrs; ++i)
17693 if (die->attrs[i].name == name)
17694 return &die->attrs[i];
17695
17696 return NULL;
17697}
17698
7d45c7c3
KB
17699/* Return the string associated with a string-typed attribute, or NULL if it
17700 is either not found or is of an incorrect type. */
17701
17702static const char *
17703dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17704{
17705 struct attribute *attr;
17706 const char *str = NULL;
17707
17708 attr = dwarf2_attr (die, name, cu);
17709
17710 if (attr != NULL)
17711 {
43988095 17712 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17713 || attr->form == DW_FORM_string
17714 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17715 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17716 str = DW_STRING (attr);
17717 else
17718 complaint (&symfile_complaints,
17719 _("string type expected for attribute %s for "
17720 "DIE at 0x%x in module %s"),
9c541725 17721 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17722 objfile_name (cu->objfile));
17723 }
17724
17725 return str;
17726}
17727
05cf31d1
JB
17728/* Return non-zero iff the attribute NAME is defined for the given DIE,
17729 and holds a non-zero value. This function should only be used for
2dc7f7b3 17730 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17731
17732static int
17733dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17734{
17735 struct attribute *attr = dwarf2_attr (die, name, cu);
17736
17737 return (attr && DW_UNSND (attr));
17738}
17739
3ca72b44 17740static int
e142c38c 17741die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17742{
05cf31d1
JB
17743 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17744 which value is non-zero. However, we have to be careful with
17745 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17746 (via dwarf2_flag_true_p) follows this attribute. So we may
17747 end up accidently finding a declaration attribute that belongs
17748 to a different DIE referenced by the specification attribute,
17749 even though the given DIE does not have a declaration attribute. */
17750 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17751 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17752}
17753
63d06c5c 17754/* Return the die giving the specification for DIE, if there is
f2f0e013 17755 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17756 containing the return value on output. If there is no
17757 specification, but there is an abstract origin, that is
17758 returned. */
63d06c5c
DC
17759
17760static struct die_info *
f2f0e013 17761die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17762{
f2f0e013
DJ
17763 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17764 *spec_cu);
63d06c5c 17765
edb3359d
DJ
17766 if (spec_attr == NULL)
17767 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17768
63d06c5c
DC
17769 if (spec_attr == NULL)
17770 return NULL;
17771 else
f2f0e013 17772 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17773}
c906108c 17774
527f3840
JK
17775/* Stub for free_line_header to match void * callback types. */
17776
17777static void
17778free_line_header_voidp (void *arg)
17779{
9a3c8263 17780 struct line_header *lh = (struct line_header *) arg;
527f3840 17781
fff8551c 17782 delete lh;
527f3840
JK
17783}
17784
fff8551c
PA
17785void
17786line_header::add_include_dir (const char *include_dir)
c906108c 17787{
27e0867f 17788 if (dwarf_line_debug >= 2)
fff8551c
PA
17789 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17790 include_dirs.size () + 1, include_dir);
27e0867f 17791
fff8551c 17792 include_dirs.push_back (include_dir);
debd256d 17793}
6e70227d 17794
fff8551c
PA
17795void
17796line_header::add_file_name (const char *name,
ecfb656c 17797 dir_index d_index,
fff8551c
PA
17798 unsigned int mod_time,
17799 unsigned int length)
debd256d 17800{
27e0867f
DE
17801 if (dwarf_line_debug >= 2)
17802 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17803 (unsigned) file_names.size () + 1, name);
27e0867f 17804
ecfb656c 17805 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17806}
6e70227d 17807
83769d0b 17808/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17809
17810static struct dwarf2_section_info *
17811get_debug_line_section (struct dwarf2_cu *cu)
17812{
17813 struct dwarf2_section_info *section;
17814
17815 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17816 DWO file. */
17817 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17818 section = &cu->dwo_unit->dwo_file->sections.line;
17819 else if (cu->per_cu->is_dwz)
17820 {
17821 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17822
17823 section = &dwz->line;
17824 }
17825 else
17826 section = &dwarf2_per_objfile->line;
17827
17828 return section;
17829}
17830
43988095
JK
17831/* Read directory or file name entry format, starting with byte of
17832 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17833 entries count and the entries themselves in the described entry
17834 format. */
17835
17836static void
17837read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17838 struct line_header *lh,
17839 const struct comp_unit_head *cu_header,
17840 void (*callback) (struct line_header *lh,
17841 const char *name,
ecfb656c 17842 dir_index d_index,
43988095
JK
17843 unsigned int mod_time,
17844 unsigned int length))
17845{
17846 gdb_byte format_count, formati;
17847 ULONGEST data_count, datai;
17848 const gdb_byte *buf = *bufp;
17849 const gdb_byte *format_header_data;
17850 int i;
17851 unsigned int bytes_read;
17852
17853 format_count = read_1_byte (abfd, buf);
17854 buf += 1;
17855 format_header_data = buf;
17856 for (formati = 0; formati < format_count; formati++)
17857 {
17858 read_unsigned_leb128 (abfd, buf, &bytes_read);
17859 buf += bytes_read;
17860 read_unsigned_leb128 (abfd, buf, &bytes_read);
17861 buf += bytes_read;
17862 }
17863
17864 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17865 buf += bytes_read;
17866 for (datai = 0; datai < data_count; datai++)
17867 {
17868 const gdb_byte *format = format_header_data;
17869 struct file_entry fe;
17870
43988095
JK
17871 for (formati = 0; formati < format_count; formati++)
17872 {
ecfb656c 17873 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17874 format += bytes_read;
43988095 17875
ecfb656c 17876 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17877 format += bytes_read;
ecfb656c
PA
17878
17879 gdb::optional<const char *> string;
17880 gdb::optional<unsigned int> uint;
17881
43988095
JK
17882 switch (form)
17883 {
17884 case DW_FORM_string:
ecfb656c 17885 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17886 buf += bytes_read;
17887 break;
17888
17889 case DW_FORM_line_strp:
ecfb656c
PA
17890 string.emplace (read_indirect_line_string (abfd, buf,
17891 cu_header,
17892 &bytes_read));
43988095
JK
17893 buf += bytes_read;
17894 break;
17895
17896 case DW_FORM_data1:
ecfb656c 17897 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17898 buf += 1;
17899 break;
17900
17901 case DW_FORM_data2:
ecfb656c 17902 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17903 buf += 2;
17904 break;
17905
17906 case DW_FORM_data4:
ecfb656c 17907 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17908 buf += 4;
17909 break;
17910
17911 case DW_FORM_data8:
ecfb656c 17912 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17913 buf += 8;
17914 break;
17915
17916 case DW_FORM_udata:
ecfb656c 17917 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17918 buf += bytes_read;
17919 break;
17920
17921 case DW_FORM_block:
17922 /* It is valid only for DW_LNCT_timestamp which is ignored by
17923 current GDB. */
17924 break;
17925 }
ecfb656c
PA
17926
17927 switch (content_type)
17928 {
17929 case DW_LNCT_path:
17930 if (string.has_value ())
17931 fe.name = *string;
17932 break;
17933 case DW_LNCT_directory_index:
17934 if (uint.has_value ())
17935 fe.d_index = (dir_index) *uint;
17936 break;
17937 case DW_LNCT_timestamp:
17938 if (uint.has_value ())
17939 fe.mod_time = *uint;
17940 break;
17941 case DW_LNCT_size:
17942 if (uint.has_value ())
17943 fe.length = *uint;
17944 break;
17945 case DW_LNCT_MD5:
17946 break;
17947 default:
17948 complaint (&symfile_complaints,
17949 _("Unknown format content type %s"),
17950 pulongest (content_type));
17951 }
43988095
JK
17952 }
17953
ecfb656c 17954 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17955 }
17956
17957 *bufp = buf;
17958}
17959
debd256d 17960/* Read the statement program header starting at OFFSET in
3019eac3 17961 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17962 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17963 Returns NULL if there is a problem reading the header, e.g., if it
17964 has a version we don't understand.
debd256d
JB
17965
17966 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17967 the returned object point into the dwarf line section buffer,
17968 and must not be freed. */
ae2de4f8 17969
fff8551c 17970static line_header_up
9c541725 17971dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17972{
d521ce57 17973 const gdb_byte *line_ptr;
c764a876 17974 unsigned int bytes_read, offset_size;
debd256d 17975 int i;
d521ce57 17976 const char *cur_dir, *cur_file;
3019eac3
DE
17977 struct dwarf2_section_info *section;
17978 bfd *abfd;
17979
36586728 17980 section = get_debug_line_section (cu);
3019eac3
DE
17981 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17982 if (section->buffer == NULL)
debd256d 17983 {
3019eac3
DE
17984 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17985 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17986 else
17987 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17988 return 0;
17989 }
17990
fceca515
DE
17991 /* We can't do this until we know the section is non-empty.
17992 Only then do we know we have such a section. */
a32a8923 17993 abfd = get_section_bfd_owner (section);
fceca515 17994
a738430d
MK
17995 /* Make sure that at least there's room for the total_length field.
17996 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17997 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17998 {
4d3c2250 17999 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18000 return 0;
18001 }
18002
fff8551c 18003 line_header_up lh (new line_header ());
debd256d 18004
9c541725 18005 lh->sect_off = sect_off;
527f3840
JK
18006 lh->offset_in_dwz = cu->per_cu->is_dwz;
18007
9c541725 18008 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18009
a738430d 18010 /* Read in the header. */
6e70227d 18011 lh->total_length =
c764a876
DE
18012 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18013 &bytes_read, &offset_size);
debd256d 18014 line_ptr += bytes_read;
3019eac3 18015 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18016 {
4d3c2250 18017 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18018 return 0;
18019 }
18020 lh->statement_program_end = line_ptr + lh->total_length;
18021 lh->version = read_2_bytes (abfd, line_ptr);
18022 line_ptr += 2;
43988095 18023 if (lh->version > 5)
cd366ee8
DE
18024 {
18025 /* This is a version we don't understand. The format could have
18026 changed in ways we don't handle properly so just punt. */
18027 complaint (&symfile_complaints,
18028 _("unsupported version in .debug_line section"));
18029 return NULL;
18030 }
43988095
JK
18031 if (lh->version >= 5)
18032 {
18033 gdb_byte segment_selector_size;
18034
18035 /* Skip address size. */
18036 read_1_byte (abfd, line_ptr);
18037 line_ptr += 1;
18038
18039 segment_selector_size = read_1_byte (abfd, line_ptr);
18040 line_ptr += 1;
18041 if (segment_selector_size != 0)
18042 {
18043 complaint (&symfile_complaints,
18044 _("unsupported segment selector size %u "
18045 "in .debug_line section"),
18046 segment_selector_size);
18047 return NULL;
18048 }
18049 }
c764a876
DE
18050 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18051 line_ptr += offset_size;
debd256d
JB
18052 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18053 line_ptr += 1;
2dc7f7b3
TT
18054 if (lh->version >= 4)
18055 {
18056 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18057 line_ptr += 1;
18058 }
18059 else
18060 lh->maximum_ops_per_instruction = 1;
18061
18062 if (lh->maximum_ops_per_instruction == 0)
18063 {
18064 lh->maximum_ops_per_instruction = 1;
18065 complaint (&symfile_complaints,
3e43a32a
MS
18066 _("invalid maximum_ops_per_instruction "
18067 "in `.debug_line' section"));
2dc7f7b3
TT
18068 }
18069
debd256d
JB
18070 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18071 line_ptr += 1;
18072 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18073 line_ptr += 1;
18074 lh->line_range = read_1_byte (abfd, line_ptr);
18075 line_ptr += 1;
18076 lh->opcode_base = read_1_byte (abfd, line_ptr);
18077 line_ptr += 1;
fff8551c 18078 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18079
18080 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18081 for (i = 1; i < lh->opcode_base; ++i)
18082 {
18083 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18084 line_ptr += 1;
18085 }
18086
43988095 18087 if (lh->version >= 5)
debd256d 18088 {
43988095 18089 /* Read directory table. */
fff8551c
PA
18090 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18091 [] (struct line_header *lh, const char *name,
ecfb656c 18092 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18093 unsigned int length)
18094 {
18095 lh->add_include_dir (name);
18096 });
debd256d 18097
43988095 18098 /* Read file name table. */
fff8551c
PA
18099 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18100 [] (struct line_header *lh, const char *name,
ecfb656c 18101 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18102 unsigned int length)
18103 {
ecfb656c 18104 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18105 });
43988095
JK
18106 }
18107 else
debd256d 18108 {
43988095
JK
18109 /* Read directory table. */
18110 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18111 {
18112 line_ptr += bytes_read;
fff8551c 18113 lh->add_include_dir (cur_dir);
43988095 18114 }
debd256d
JB
18115 line_ptr += bytes_read;
18116
43988095
JK
18117 /* Read file name table. */
18118 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18119 {
ecfb656c
PA
18120 unsigned int mod_time, length;
18121 dir_index d_index;
43988095
JK
18122
18123 line_ptr += bytes_read;
ecfb656c 18124 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18125 line_ptr += bytes_read;
18126 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18127 line_ptr += bytes_read;
18128 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18129 line_ptr += bytes_read;
18130
ecfb656c 18131 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18132 }
18133 line_ptr += bytes_read;
debd256d 18134 }
6e70227d 18135 lh->statement_program_start = line_ptr;
debd256d 18136
3019eac3 18137 if (line_ptr > (section->buffer + section->size))
4d3c2250 18138 complaint (&symfile_complaints,
3e43a32a
MS
18139 _("line number info header doesn't "
18140 "fit in `.debug_line' section"));
debd256d 18141
debd256d
JB
18142 return lh;
18143}
c906108c 18144
c6da4cef
DE
18145/* Subroutine of dwarf_decode_lines to simplify it.
18146 Return the file name of the psymtab for included file FILE_INDEX
18147 in line header LH of PST.
18148 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18149 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18150 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18151
18152 The function creates dangling cleanup registration. */
c6da4cef 18153
d521ce57 18154static const char *
c6da4cef
DE
18155psymtab_include_file_name (const struct line_header *lh, int file_index,
18156 const struct partial_symtab *pst,
18157 const char *comp_dir)
18158{
8c43009f 18159 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18160 const char *include_name = fe.name;
18161 const char *include_name_to_compare = include_name;
72b9f47f
TT
18162 const char *pst_filename;
18163 char *copied_name = NULL;
c6da4cef
DE
18164 int file_is_pst;
18165
8c43009f 18166 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18167
18168 if (!IS_ABSOLUTE_PATH (include_name)
18169 && (dir_name != NULL || comp_dir != NULL))
18170 {
18171 /* Avoid creating a duplicate psymtab for PST.
18172 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18173 Before we do the comparison, however, we need to account
18174 for DIR_NAME and COMP_DIR.
18175 First prepend dir_name (if non-NULL). If we still don't
18176 have an absolute path prepend comp_dir (if non-NULL).
18177 However, the directory we record in the include-file's
18178 psymtab does not contain COMP_DIR (to match the
18179 corresponding symtab(s)).
18180
18181 Example:
18182
18183 bash$ cd /tmp
18184 bash$ gcc -g ./hello.c
18185 include_name = "hello.c"
18186 dir_name = "."
18187 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18188 DW_AT_name = "./hello.c"
18189
18190 */
c6da4cef
DE
18191
18192 if (dir_name != NULL)
18193 {
d521ce57
TT
18194 char *tem = concat (dir_name, SLASH_STRING,
18195 include_name, (char *)NULL);
18196
18197 make_cleanup (xfree, tem);
18198 include_name = tem;
c6da4cef 18199 include_name_to_compare = include_name;
c6da4cef
DE
18200 }
18201 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18202 {
d521ce57
TT
18203 char *tem = concat (comp_dir, SLASH_STRING,
18204 include_name, (char *)NULL);
18205
18206 make_cleanup (xfree, tem);
18207 include_name_to_compare = tem;
c6da4cef
DE
18208 }
18209 }
18210
18211 pst_filename = pst->filename;
18212 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18213 {
72b9f47f
TT
18214 copied_name = concat (pst->dirname, SLASH_STRING,
18215 pst_filename, (char *)NULL);
18216 pst_filename = copied_name;
c6da4cef
DE
18217 }
18218
1e3fad37 18219 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18220
72b9f47f
TT
18221 if (copied_name != NULL)
18222 xfree (copied_name);
c6da4cef
DE
18223
18224 if (file_is_pst)
18225 return NULL;
18226 return include_name;
18227}
18228
d9b3de22
DE
18229/* State machine to track the state of the line number program. */
18230
6f77053d 18231class lnp_state_machine
d9b3de22 18232{
6f77053d
PA
18233public:
18234 /* Initialize a machine state for the start of a line number
18235 program. */
18236 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18237
8c43009f
PA
18238 file_entry *current_file ()
18239 {
18240 /* lh->file_names is 0-based, but the file name numbers in the
18241 statement program are 1-based. */
6f77053d
PA
18242 return m_line_header->file_name_at (m_file);
18243 }
18244
18245 /* Record the line in the state machine. END_SEQUENCE is true if
18246 we're processing the end of a sequence. */
18247 void record_line (bool end_sequence);
18248
18249 /* Check address and if invalid nop-out the rest of the lines in this
18250 sequence. */
18251 void check_line_address (struct dwarf2_cu *cu,
18252 const gdb_byte *line_ptr,
18253 CORE_ADDR lowpc, CORE_ADDR address);
18254
18255 void handle_set_discriminator (unsigned int discriminator)
18256 {
18257 m_discriminator = discriminator;
18258 m_line_has_non_zero_discriminator |= discriminator != 0;
18259 }
18260
18261 /* Handle DW_LNE_set_address. */
18262 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18263 {
18264 m_op_index = 0;
18265 address += baseaddr;
18266 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18267 }
18268
18269 /* Handle DW_LNS_advance_pc. */
18270 void handle_advance_pc (CORE_ADDR adjust);
18271
18272 /* Handle a special opcode. */
18273 void handle_special_opcode (unsigned char op_code);
18274
18275 /* Handle DW_LNS_advance_line. */
18276 void handle_advance_line (int line_delta)
18277 {
18278 advance_line (line_delta);
18279 }
18280
18281 /* Handle DW_LNS_set_file. */
18282 void handle_set_file (file_name_index file);
18283
18284 /* Handle DW_LNS_negate_stmt. */
18285 void handle_negate_stmt ()
18286 {
18287 m_is_stmt = !m_is_stmt;
18288 }
18289
18290 /* Handle DW_LNS_const_add_pc. */
18291 void handle_const_add_pc ();
18292
18293 /* Handle DW_LNS_fixed_advance_pc. */
18294 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18295 {
18296 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18297 m_op_index = 0;
18298 }
18299
18300 /* Handle DW_LNS_copy. */
18301 void handle_copy ()
18302 {
18303 record_line (false);
18304 m_discriminator = 0;
18305 }
18306
18307 /* Handle DW_LNE_end_sequence. */
18308 void handle_end_sequence ()
18309 {
18310 m_record_line_callback = ::record_line;
18311 }
18312
18313private:
18314 /* Advance the line by LINE_DELTA. */
18315 void advance_line (int line_delta)
18316 {
18317 m_line += line_delta;
18318
18319 if (line_delta != 0)
18320 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18321 }
18322
6f77053d
PA
18323 gdbarch *m_gdbarch;
18324
18325 /* True if we're recording lines.
18326 Otherwise we're building partial symtabs and are just interested in
18327 finding include files mentioned by the line number program. */
18328 bool m_record_lines_p;
18329
8c43009f 18330 /* The line number header. */
6f77053d 18331 line_header *m_line_header;
8c43009f 18332
6f77053d
PA
18333 /* These are part of the standard DWARF line number state machine,
18334 and initialized according to the DWARF spec. */
d9b3de22 18335
6f77053d 18336 unsigned char m_op_index = 0;
8c43009f 18337 /* The line table index (1-based) of the current file. */
6f77053d
PA
18338 file_name_index m_file = (file_name_index) 1;
18339 unsigned int m_line = 1;
18340
18341 /* These are initialized in the constructor. */
18342
18343 CORE_ADDR m_address;
18344 bool m_is_stmt;
18345 unsigned int m_discriminator;
d9b3de22
DE
18346
18347 /* Additional bits of state we need to track. */
18348
18349 /* The last file that we called dwarf2_start_subfile for.
18350 This is only used for TLLs. */
6f77053d 18351 unsigned int m_last_file = 0;
d9b3de22 18352 /* The last file a line number was recorded for. */
6f77053d 18353 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18354
18355 /* The function to call to record a line. */
6f77053d 18356 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18357
18358 /* The last line number that was recorded, used to coalesce
18359 consecutive entries for the same line. This can happen, for
18360 example, when discriminators are present. PR 17276. */
6f77053d
PA
18361 unsigned int m_last_line = 0;
18362 bool m_line_has_non_zero_discriminator = false;
8c43009f 18363};
d9b3de22 18364
6f77053d
PA
18365void
18366lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18367{
18368 CORE_ADDR addr_adj = (((m_op_index + adjust)
18369 / m_line_header->maximum_ops_per_instruction)
18370 * m_line_header->minimum_instruction_length);
18371 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18372 m_op_index = ((m_op_index + adjust)
18373 % m_line_header->maximum_ops_per_instruction);
18374}
d9b3de22 18375
6f77053d
PA
18376void
18377lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18378{
6f77053d
PA
18379 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18380 CORE_ADDR addr_adj = (((m_op_index
18381 + (adj_opcode / m_line_header->line_range))
18382 / m_line_header->maximum_ops_per_instruction)
18383 * m_line_header->minimum_instruction_length);
18384 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18385 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18386 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18387
6f77053d
PA
18388 int line_delta = (m_line_header->line_base
18389 + (adj_opcode % m_line_header->line_range));
18390 advance_line (line_delta);
18391 record_line (false);
18392 m_discriminator = 0;
18393}
d9b3de22 18394
6f77053d
PA
18395void
18396lnp_state_machine::handle_set_file (file_name_index file)
18397{
18398 m_file = file;
18399
18400 const file_entry *fe = current_file ();
18401 if (fe == NULL)
18402 dwarf2_debug_line_missing_file_complaint ();
18403 else if (m_record_lines_p)
18404 {
18405 const char *dir = fe->include_dir (m_line_header);
18406
18407 m_last_subfile = current_subfile;
18408 m_line_has_non_zero_discriminator = m_discriminator != 0;
18409 dwarf2_start_subfile (fe->name, dir);
18410 }
18411}
18412
18413void
18414lnp_state_machine::handle_const_add_pc ()
18415{
18416 CORE_ADDR adjust
18417 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18418
18419 CORE_ADDR addr_adj
18420 = (((m_op_index + adjust)
18421 / m_line_header->maximum_ops_per_instruction)
18422 * m_line_header->minimum_instruction_length);
18423
18424 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18425 m_op_index = ((m_op_index + adjust)
18426 % m_line_header->maximum_ops_per_instruction);
18427}
d9b3de22 18428
c91513d8
PP
18429/* Ignore this record_line request. */
18430
18431static void
18432noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18433{
18434 return;
18435}
18436
a05a36a5
DE
18437/* Return non-zero if we should add LINE to the line number table.
18438 LINE is the line to add, LAST_LINE is the last line that was added,
18439 LAST_SUBFILE is the subfile for LAST_LINE.
18440 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18441 had a non-zero discriminator.
18442
18443 We have to be careful in the presence of discriminators.
18444 E.g., for this line:
18445
18446 for (i = 0; i < 100000; i++);
18447
18448 clang can emit four line number entries for that one line,
18449 each with a different discriminator.
18450 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18451
18452 However, we want gdb to coalesce all four entries into one.
18453 Otherwise the user could stepi into the middle of the line and
18454 gdb would get confused about whether the pc really was in the
18455 middle of the line.
18456
18457 Things are further complicated by the fact that two consecutive
18458 line number entries for the same line is a heuristic used by gcc
18459 to denote the end of the prologue. So we can't just discard duplicate
18460 entries, we have to be selective about it. The heuristic we use is
18461 that we only collapse consecutive entries for the same line if at least
18462 one of those entries has a non-zero discriminator. PR 17276.
18463
18464 Note: Addresses in the line number state machine can never go backwards
18465 within one sequence, thus this coalescing is ok. */
18466
18467static int
18468dwarf_record_line_p (unsigned int line, unsigned int last_line,
18469 int line_has_non_zero_discriminator,
18470 struct subfile *last_subfile)
18471{
18472 if (current_subfile != last_subfile)
18473 return 1;
18474 if (line != last_line)
18475 return 1;
18476 /* Same line for the same file that we've seen already.
18477 As a last check, for pr 17276, only record the line if the line
18478 has never had a non-zero discriminator. */
18479 if (!line_has_non_zero_discriminator)
18480 return 1;
18481 return 0;
18482}
18483
252a6764
DE
18484/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18485 in the line table of subfile SUBFILE. */
18486
18487static void
d9b3de22
DE
18488dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18489 unsigned int line, CORE_ADDR address,
18490 record_line_ftype p_record_line)
252a6764
DE
18491{
18492 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18493
27e0867f
DE
18494 if (dwarf_line_debug)
18495 {
18496 fprintf_unfiltered (gdb_stdlog,
18497 "Recording line %u, file %s, address %s\n",
18498 line, lbasename (subfile->name),
18499 paddress (gdbarch, address));
18500 }
18501
d5962de5 18502 (*p_record_line) (subfile, line, addr);
252a6764
DE
18503}
18504
18505/* Subroutine of dwarf_decode_lines_1 to simplify it.
18506 Mark the end of a set of line number records.
d9b3de22 18507 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18508 If SUBFILE is NULL the request is ignored. */
18509
18510static void
18511dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18512 CORE_ADDR address, record_line_ftype p_record_line)
18513{
27e0867f
DE
18514 if (subfile == NULL)
18515 return;
18516
18517 if (dwarf_line_debug)
18518 {
18519 fprintf_unfiltered (gdb_stdlog,
18520 "Finishing current line, file %s, address %s\n",
18521 lbasename (subfile->name),
18522 paddress (gdbarch, address));
18523 }
18524
d9b3de22
DE
18525 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18526}
18527
6f77053d
PA
18528void
18529lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18530{
d9b3de22
DE
18531 if (dwarf_line_debug)
18532 {
18533 fprintf_unfiltered (gdb_stdlog,
18534 "Processing actual line %u: file %u,"
18535 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18536 m_line, to_underlying (m_file),
18537 paddress (m_gdbarch, m_address),
18538 m_is_stmt, m_discriminator);
d9b3de22
DE
18539 }
18540
6f77053d 18541 file_entry *fe = current_file ();
8c43009f
PA
18542
18543 if (fe == NULL)
d9b3de22
DE
18544 dwarf2_debug_line_missing_file_complaint ();
18545 /* For now we ignore lines not starting on an instruction boundary.
18546 But not when processing end_sequence for compatibility with the
18547 previous version of the code. */
6f77053d 18548 else if (m_op_index == 0 || end_sequence)
d9b3de22 18549 {
8c43009f 18550 fe->included_p = 1;
6f77053d 18551 if (m_record_lines_p && m_is_stmt)
d9b3de22 18552 {
6f77053d 18553 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18554 {
6f77053d
PA
18555 dwarf_finish_line (m_gdbarch, m_last_subfile,
18556 m_address, m_record_line_callback);
d9b3de22
DE
18557 }
18558
18559 if (!end_sequence)
18560 {
6f77053d
PA
18561 if (dwarf_record_line_p (m_line, m_last_line,
18562 m_line_has_non_zero_discriminator,
18563 m_last_subfile))
d9b3de22 18564 {
6f77053d
PA
18565 dwarf_record_line_1 (m_gdbarch, current_subfile,
18566 m_line, m_address,
18567 m_record_line_callback);
d9b3de22 18568 }
6f77053d
PA
18569 m_last_subfile = current_subfile;
18570 m_last_line = m_line;
d9b3de22
DE
18571 }
18572 }
18573 }
18574}
18575
6f77053d
PA
18576lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18577 bool record_lines_p)
d9b3de22 18578{
6f77053d
PA
18579 m_gdbarch = arch;
18580 m_record_lines_p = record_lines_p;
18581 m_line_header = lh;
d9b3de22 18582
6f77053d 18583 m_record_line_callback = ::record_line;
d9b3de22 18584
d9b3de22
DE
18585 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18586 was a line entry for it so that the backend has a chance to adjust it
18587 and also record it in case it needs it. This is currently used by MIPS
18588 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18589 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18590 m_is_stmt = lh->default_is_stmt;
18591 m_discriminator = 0;
252a6764
DE
18592}
18593
6f77053d
PA
18594void
18595lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18596 const gdb_byte *line_ptr,
18597 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18598{
18599 /* If address < lowpc then it's not a usable value, it's outside the
18600 pc range of the CU. However, we restrict the test to only address
18601 values of zero to preserve GDB's previous behaviour which is to
18602 handle the specific case of a function being GC'd by the linker. */
18603
18604 if (address == 0 && address < lowpc)
18605 {
18606 /* This line table is for a function which has been
18607 GCd by the linker. Ignore it. PR gdb/12528 */
18608
18609 struct objfile *objfile = cu->objfile;
18610 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18611
18612 complaint (&symfile_complaints,
18613 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18614 line_offset, objfile_name (objfile));
6f77053d
PA
18615 m_record_line_callback = noop_record_line;
18616 /* Note: record_line_callback is left as noop_record_line until
18617 we see DW_LNE_end_sequence. */
924c2928
DE
18618 }
18619}
18620
f3f5162e 18621/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18622 Process the line number information in LH.
18623 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18624 program in order to set included_p for every referenced header. */
debd256d 18625
c906108c 18626static void
43f3e411
DE
18627dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18628 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18629{
d521ce57
TT
18630 const gdb_byte *line_ptr, *extended_end;
18631 const gdb_byte *line_end;
a8c50c1f 18632 unsigned int bytes_read, extended_len;
699ca60a 18633 unsigned char op_code, extended_op;
e142c38c
DJ
18634 CORE_ADDR baseaddr;
18635 struct objfile *objfile = cu->objfile;
f3f5162e 18636 bfd *abfd = objfile->obfd;
fbf65064 18637 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18638 /* True if we're recording line info (as opposed to building partial
18639 symtabs and just interested in finding include files mentioned by
18640 the line number program). */
18641 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18642
18643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18644
debd256d
JB
18645 line_ptr = lh->statement_program_start;
18646 line_end = lh->statement_program_end;
c906108c
SS
18647
18648 /* Read the statement sequences until there's nothing left. */
18649 while (line_ptr < line_end)
18650 {
6f77053d
PA
18651 /* The DWARF line number program state machine. Reset the state
18652 machine at the start of each sequence. */
18653 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18654 bool end_sequence = false;
d9b3de22 18655
8c43009f 18656 if (record_lines_p)
c906108c 18657 {
8c43009f
PA
18658 /* Start a subfile for the current file of the state
18659 machine. */
18660 const file_entry *fe = state_machine.current_file ();
18661
18662 if (fe != NULL)
18663 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18664 }
18665
a738430d 18666 /* Decode the table. */
d9b3de22 18667 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18668 {
18669 op_code = read_1_byte (abfd, line_ptr);
18670 line_ptr += 1;
9aa1fe7e 18671
debd256d 18672 if (op_code >= lh->opcode_base)
6e70227d 18673 {
8e07a239 18674 /* Special opcode. */
6f77053d 18675 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18676 }
18677 else switch (op_code)
c906108c
SS
18678 {
18679 case DW_LNS_extended_op:
3e43a32a
MS
18680 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18681 &bytes_read);
473b7be6 18682 line_ptr += bytes_read;
a8c50c1f 18683 extended_end = line_ptr + extended_len;
c906108c
SS
18684 extended_op = read_1_byte (abfd, line_ptr);
18685 line_ptr += 1;
18686 switch (extended_op)
18687 {
18688 case DW_LNE_end_sequence:
6f77053d
PA
18689 state_machine.handle_end_sequence ();
18690 end_sequence = true;
c906108c
SS
18691 break;
18692 case DW_LNE_set_address:
d9b3de22
DE
18693 {
18694 CORE_ADDR address
18695 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18696 line_ptr += bytes_read;
6f77053d
PA
18697
18698 state_machine.check_line_address (cu, line_ptr,
18699 lowpc, address);
18700 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18701 }
c906108c
SS
18702 break;
18703 case DW_LNE_define_file:
debd256d 18704 {
d521ce57 18705 const char *cur_file;
ecfb656c
PA
18706 unsigned int mod_time, length;
18707 dir_index dindex;
6e70227d 18708
3e43a32a
MS
18709 cur_file = read_direct_string (abfd, line_ptr,
18710 &bytes_read);
debd256d 18711 line_ptr += bytes_read;
ecfb656c 18712 dindex = (dir_index)
debd256d
JB
18713 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18714 line_ptr += bytes_read;
18715 mod_time =
18716 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18717 line_ptr += bytes_read;
18718 length =
18719 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18720 line_ptr += bytes_read;
ecfb656c 18721 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18722 }
c906108c 18723 break;
d0c6ba3d 18724 case DW_LNE_set_discriminator:
6f77053d
PA
18725 {
18726 /* The discriminator is not interesting to the
18727 debugger; just ignore it. We still need to
18728 check its value though:
18729 if there are consecutive entries for the same
18730 (non-prologue) line we want to coalesce them.
18731 PR 17276. */
18732 unsigned int discr
18733 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18734 line_ptr += bytes_read;
18735
18736 state_machine.handle_set_discriminator (discr);
18737 }
d0c6ba3d 18738 break;
c906108c 18739 default:
4d3c2250 18740 complaint (&symfile_complaints,
e2e0b3e5 18741 _("mangled .debug_line section"));
debd256d 18742 return;
c906108c 18743 }
a8c50c1f
DJ
18744 /* Make sure that we parsed the extended op correctly. If e.g.
18745 we expected a different address size than the producer used,
18746 we may have read the wrong number of bytes. */
18747 if (line_ptr != extended_end)
18748 {
18749 complaint (&symfile_complaints,
18750 _("mangled .debug_line section"));
18751 return;
18752 }
c906108c
SS
18753 break;
18754 case DW_LNS_copy:
6f77053d 18755 state_machine.handle_copy ();
c906108c
SS
18756 break;
18757 case DW_LNS_advance_pc:
2dc7f7b3
TT
18758 {
18759 CORE_ADDR adjust
18760 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18761 line_ptr += bytes_read;
6f77053d
PA
18762
18763 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18764 }
c906108c
SS
18765 break;
18766 case DW_LNS_advance_line:
a05a36a5
DE
18767 {
18768 int line_delta
18769 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18770 line_ptr += bytes_read;
6f77053d
PA
18771
18772 state_machine.handle_advance_line (line_delta);
a05a36a5 18773 }
c906108c
SS
18774 break;
18775 case DW_LNS_set_file:
d9b3de22 18776 {
6f77053d 18777 file_name_index file
ecfb656c
PA
18778 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18779 &bytes_read);
d9b3de22 18780 line_ptr += bytes_read;
8c43009f 18781
6f77053d 18782 state_machine.handle_set_file (file);
d9b3de22 18783 }
c906108c
SS
18784 break;
18785 case DW_LNS_set_column:
0ad93d4f 18786 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18787 line_ptr += bytes_read;
18788 break;
18789 case DW_LNS_negate_stmt:
6f77053d 18790 state_machine.handle_negate_stmt ();
c906108c
SS
18791 break;
18792 case DW_LNS_set_basic_block:
c906108c 18793 break;
c2c6d25f
JM
18794 /* Add to the address register of the state machine the
18795 address increment value corresponding to special opcode
a738430d
MK
18796 255. I.e., this value is scaled by the minimum
18797 instruction length since special opcode 255 would have
b021a221 18798 scaled the increment. */
c906108c 18799 case DW_LNS_const_add_pc:
6f77053d 18800 state_machine.handle_const_add_pc ();
c906108c
SS
18801 break;
18802 case DW_LNS_fixed_advance_pc:
3e29f34a 18803 {
6f77053d 18804 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18805 line_ptr += 2;
6f77053d
PA
18806
18807 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18808 }
c906108c 18809 break;
9aa1fe7e 18810 default:
a738430d
MK
18811 {
18812 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18813 int i;
a738430d 18814
debd256d 18815 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18816 {
18817 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18818 line_ptr += bytes_read;
18819 }
18820 }
c906108c
SS
18821 }
18822 }
d9b3de22
DE
18823
18824 if (!end_sequence)
18825 dwarf2_debug_line_missing_end_sequence_complaint ();
18826
18827 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18828 in which case we still finish recording the last line). */
6f77053d 18829 state_machine.record_line (true);
c906108c 18830 }
f3f5162e
DE
18831}
18832
18833/* Decode the Line Number Program (LNP) for the given line_header
18834 structure and CU. The actual information extracted and the type
18835 of structures created from the LNP depends on the value of PST.
18836
18837 1. If PST is NULL, then this procedure uses the data from the program
18838 to create all necessary symbol tables, and their linetables.
18839
18840 2. If PST is not NULL, this procedure reads the program to determine
18841 the list of files included by the unit represented by PST, and
18842 builds all the associated partial symbol tables.
18843
18844 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18845 It is used for relative paths in the line table.
18846 NOTE: When processing partial symtabs (pst != NULL),
18847 comp_dir == pst->dirname.
18848
18849 NOTE: It is important that psymtabs have the same file name (via strcmp)
18850 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18851 symtab we don't use it in the name of the psymtabs we create.
18852 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18853 A good testcase for this is mb-inline.exp.
18854
527f3840
JK
18855 LOWPC is the lowest address in CU (or 0 if not known).
18856
18857 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18858 for its PC<->lines mapping information. Otherwise only the filename
18859 table is read in. */
f3f5162e
DE
18860
18861static void
18862dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18863 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18864 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18865{
18866 struct objfile *objfile = cu->objfile;
18867 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18868
527f3840
JK
18869 if (decode_mapping)
18870 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18871
18872 if (decode_for_pst_p)
18873 {
18874 int file_index;
18875
18876 /* Now that we're done scanning the Line Header Program, we can
18877 create the psymtab of each included file. */
fff8551c 18878 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18879 if (lh->file_names[file_index].included_p == 1)
18880 {
d521ce57 18881 const char *include_name =
c6da4cef
DE
18882 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18883 if (include_name != NULL)
aaa75496
JB
18884 dwarf2_create_include_psymtab (include_name, pst, objfile);
18885 }
18886 }
cb1df416
DJ
18887 else
18888 {
18889 /* Make sure a symtab is created for every file, even files
18890 which contain only variables (i.e. no code with associated
18891 line numbers). */
43f3e411 18892 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18893 int i;
cb1df416 18894
fff8551c 18895 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18896 {
8c43009f 18897 file_entry &fe = lh->file_names[i];
9a619af0 18898
8c43009f 18899 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18900
cb1df416 18901 if (current_subfile->symtab == NULL)
43f3e411
DE
18902 {
18903 current_subfile->symtab
18904 = allocate_symtab (cust, current_subfile->name);
18905 }
8c43009f 18906 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18907 }
18908 }
c906108c
SS
18909}
18910
18911/* Start a subfile for DWARF. FILENAME is the name of the file and
18912 DIRNAME the name of the source directory which contains FILENAME
4d663531 18913 or NULL if not known.
c906108c
SS
18914 This routine tries to keep line numbers from identical absolute and
18915 relative file names in a common subfile.
18916
18917 Using the `list' example from the GDB testsuite, which resides in
18918 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18919 of /srcdir/list0.c yields the following debugging information for list0.c:
18920
c5aa993b 18921 DW_AT_name: /srcdir/list0.c
4d663531 18922 DW_AT_comp_dir: /compdir
357e46e7 18923 files.files[0].name: list0.h
c5aa993b 18924 files.files[0].dir: /srcdir
357e46e7 18925 files.files[1].name: list0.c
c5aa993b 18926 files.files[1].dir: /srcdir
c906108c
SS
18927
18928 The line number information for list0.c has to end up in a single
4f1520fb
FR
18929 subfile, so that `break /srcdir/list0.c:1' works as expected.
18930 start_subfile will ensure that this happens provided that we pass the
18931 concatenation of files.files[1].dir and files.files[1].name as the
18932 subfile's name. */
c906108c
SS
18933
18934static void
4d663531 18935dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18936{
d521ce57 18937 char *copy = NULL;
4f1520fb 18938
4d663531 18939 /* In order not to lose the line information directory,
4f1520fb
FR
18940 we concatenate it to the filename when it makes sense.
18941 Note that the Dwarf3 standard says (speaking of filenames in line
18942 information): ``The directory index is ignored for file names
18943 that represent full path names''. Thus ignoring dirname in the
18944 `else' branch below isn't an issue. */
c906108c 18945
d5166ae1 18946 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18947 {
18948 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18949 filename = copy;
18950 }
c906108c 18951
4d663531 18952 start_subfile (filename);
4f1520fb 18953
d521ce57
TT
18954 if (copy != NULL)
18955 xfree (copy);
c906108c
SS
18956}
18957
f4dc4d17
DE
18958/* Start a symtab for DWARF.
18959 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18960
43f3e411 18961static struct compunit_symtab *
f4dc4d17 18962dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18963 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18964{
43f3e411
DE
18965 struct compunit_symtab *cust
18966 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18967
f4dc4d17
DE
18968 record_debugformat ("DWARF 2");
18969 record_producer (cu->producer);
18970
18971 /* We assume that we're processing GCC output. */
18972 processing_gcc_compilation = 2;
18973
4d4ec4e5 18974 cu->processing_has_namespace_info = 0;
43f3e411
DE
18975
18976 return cust;
f4dc4d17
DE
18977}
18978
4c2df51b
DJ
18979static void
18980var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18981 struct dwarf2_cu *cu)
4c2df51b 18982{
e7c27a73
DJ
18983 struct objfile *objfile = cu->objfile;
18984 struct comp_unit_head *cu_header = &cu->header;
18985
4c2df51b
DJ
18986 /* NOTE drow/2003-01-30: There used to be a comment and some special
18987 code here to turn a symbol with DW_AT_external and a
18988 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18989 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18990 with some versions of binutils) where shared libraries could have
18991 relocations against symbols in their debug information - the
18992 minimal symbol would have the right address, but the debug info
18993 would not. It's no longer necessary, because we will explicitly
18994 apply relocations when we read in the debug information now. */
18995
18996 /* A DW_AT_location attribute with no contents indicates that a
18997 variable has been optimized away. */
18998 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18999 {
f1e6e072 19000 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19001 return;
19002 }
19003
19004 /* Handle one degenerate form of location expression specially, to
19005 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19006 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19007 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19008
19009 if (attr_form_is_block (attr)
3019eac3
DE
19010 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19011 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19012 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19013 && (DW_BLOCK (attr)->size
19014 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19015 {
891d2f0b 19016 unsigned int dummy;
4c2df51b 19017
3019eac3
DE
19018 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19019 SYMBOL_VALUE_ADDRESS (sym) =
19020 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19021 else
19022 SYMBOL_VALUE_ADDRESS (sym) =
19023 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19024 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19025 fixup_symbol_section (sym, objfile);
19026 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19027 SYMBOL_SECTION (sym));
4c2df51b
DJ
19028 return;
19029 }
19030
19031 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19032 expression evaluator, and use LOC_COMPUTED only when necessary
19033 (i.e. when the value of a register or memory location is
19034 referenced, or a thread-local block, etc.). Then again, it might
19035 not be worthwhile. I'm assuming that it isn't unless performance
19036 or memory numbers show me otherwise. */
19037
f1e6e072 19038 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19039
f1e6e072 19040 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19041 cu->has_loclist = 1;
4c2df51b
DJ
19042}
19043
c906108c
SS
19044/* Given a pointer to a DWARF information entry, figure out if we need
19045 to make a symbol table entry for it, and if so, create a new entry
19046 and return a pointer to it.
19047 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19048 used the passed type.
19049 If SPACE is not NULL, use it to hold the new symbol. If it is
19050 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19051
19052static struct symbol *
34eaf542
TT
19053new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19054 struct symbol *space)
c906108c 19055{
e7c27a73 19056 struct objfile *objfile = cu->objfile;
3e29f34a 19057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19058 struct symbol *sym = NULL;
15d034d0 19059 const char *name;
c906108c
SS
19060 struct attribute *attr = NULL;
19061 struct attribute *attr2 = NULL;
e142c38c 19062 CORE_ADDR baseaddr;
e37fd15a
SW
19063 struct pending **list_to_add = NULL;
19064
edb3359d 19065 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19066
19067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19068
94af9270 19069 name = dwarf2_name (die, cu);
c906108c
SS
19070 if (name)
19071 {
94af9270 19072 const char *linkagename;
34eaf542 19073 int suppress_add = 0;
94af9270 19074
34eaf542
TT
19075 if (space)
19076 sym = space;
19077 else
e623cf5d 19078 sym = allocate_symbol (objfile);
c906108c 19079 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19080
19081 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19082 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19083 linkagename = dwarf2_physname (name, die, cu);
19084 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19085
f55ee35c
JK
19086 /* Fortran does not have mangling standard and the mangling does differ
19087 between gfortran, iFort etc. */
19088 if (cu->language == language_fortran
b250c185 19089 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19090 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19091 dwarf2_full_name (name, die, cu),
29df156d 19092 NULL);
f55ee35c 19093
c906108c 19094 /* Default assumptions.
c5aa993b 19095 Use the passed type or decode it from the die. */
176620f1 19096 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19097 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19098 if (type != NULL)
19099 SYMBOL_TYPE (sym) = type;
19100 else
e7c27a73 19101 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19102 attr = dwarf2_attr (die,
19103 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19104 cu);
c906108c
SS
19105 if (attr)
19106 {
19107 SYMBOL_LINE (sym) = DW_UNSND (attr);
19108 }
cb1df416 19109
edb3359d
DJ
19110 attr = dwarf2_attr (die,
19111 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19112 cu);
cb1df416
DJ
19113 if (attr)
19114 {
ecfb656c 19115 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19116 struct file_entry *fe;
9a619af0 19117
ecfb656c
PA
19118 if (cu->line_header != NULL)
19119 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19120 else
19121 fe = NULL;
19122
19123 if (fe == NULL)
cb1df416
DJ
19124 complaint (&symfile_complaints,
19125 _("file index out of range"));
8c43009f
PA
19126 else
19127 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19128 }
19129
c906108c
SS
19130 switch (die->tag)
19131 {
19132 case DW_TAG_label:
e142c38c 19133 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19134 if (attr)
3e29f34a
MR
19135 {
19136 CORE_ADDR addr;
19137
19138 addr = attr_value_as_address (attr);
19139 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19140 SYMBOL_VALUE_ADDRESS (sym) = addr;
19141 }
0f5238ed
TT
19142 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19143 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19144 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19145 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19146 break;
19147 case DW_TAG_subprogram:
19148 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19149 finish_block. */
f1e6e072 19150 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19151 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19152 if ((attr2 && (DW_UNSND (attr2) != 0))
19153 || cu->language == language_ada)
c906108c 19154 {
2cfa0c8d
JB
19155 /* Subprograms marked external are stored as a global symbol.
19156 Ada subprograms, whether marked external or not, are always
19157 stored as a global symbol, because we want to be able to
19158 access them globally. For instance, we want to be able
19159 to break on a nested subprogram without having to
19160 specify the context. */
e37fd15a 19161 list_to_add = &global_symbols;
c906108c
SS
19162 }
19163 else
19164 {
e37fd15a 19165 list_to_add = cu->list_in_scope;
c906108c
SS
19166 }
19167 break;
edb3359d
DJ
19168 case DW_TAG_inlined_subroutine:
19169 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19170 finish_block. */
f1e6e072 19171 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19172 SYMBOL_INLINED (sym) = 1;
481860b3 19173 list_to_add = cu->list_in_scope;
edb3359d 19174 break;
34eaf542
TT
19175 case DW_TAG_template_value_param:
19176 suppress_add = 1;
19177 /* Fall through. */
72929c62 19178 case DW_TAG_constant:
c906108c 19179 case DW_TAG_variable:
254e6b9e 19180 case DW_TAG_member:
0963b4bd
MS
19181 /* Compilation with minimal debug info may result in
19182 variables with missing type entries. Change the
19183 misleading `void' type to something sensible. */
c906108c 19184 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19185 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19186
e142c38c 19187 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19188 /* In the case of DW_TAG_member, we should only be called for
19189 static const members. */
19190 if (die->tag == DW_TAG_member)
19191 {
3863f96c
DE
19192 /* dwarf2_add_field uses die_is_declaration,
19193 so we do the same. */
254e6b9e
DE
19194 gdb_assert (die_is_declaration (die, cu));
19195 gdb_assert (attr);
19196 }
c906108c
SS
19197 if (attr)
19198 {
e7c27a73 19199 dwarf2_const_value (attr, sym, cu);
e142c38c 19200 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19201 if (!suppress_add)
34eaf542
TT
19202 {
19203 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19204 list_to_add = &global_symbols;
34eaf542 19205 else
e37fd15a 19206 list_to_add = cu->list_in_scope;
34eaf542 19207 }
c906108c
SS
19208 break;
19209 }
e142c38c 19210 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19211 if (attr)
19212 {
e7c27a73 19213 var_decode_location (attr, sym, cu);
e142c38c 19214 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19215
19216 /* Fortran explicitly imports any global symbols to the local
19217 scope by DW_TAG_common_block. */
19218 if (cu->language == language_fortran && die->parent
19219 && die->parent->tag == DW_TAG_common_block)
19220 attr2 = NULL;
19221
caac4577
JG
19222 if (SYMBOL_CLASS (sym) == LOC_STATIC
19223 && SYMBOL_VALUE_ADDRESS (sym) == 0
19224 && !dwarf2_per_objfile->has_section_at_zero)
19225 {
19226 /* When a static variable is eliminated by the linker,
19227 the corresponding debug information is not stripped
19228 out, but the variable address is set to null;
19229 do not add such variables into symbol table. */
19230 }
19231 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19232 {
f55ee35c
JK
19233 /* Workaround gfortran PR debug/40040 - it uses
19234 DW_AT_location for variables in -fPIC libraries which may
19235 get overriden by other libraries/executable and get
19236 a different address. Resolve it by the minimal symbol
19237 which may come from inferior's executable using copy
19238 relocation. Make this workaround only for gfortran as for
19239 other compilers GDB cannot guess the minimal symbol
19240 Fortran mangling kind. */
19241 if (cu->language == language_fortran && die->parent
19242 && die->parent->tag == DW_TAG_module
19243 && cu->producer
28586665 19244 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19245 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19246
1c809c68
TT
19247 /* A variable with DW_AT_external is never static,
19248 but it may be block-scoped. */
19249 list_to_add = (cu->list_in_scope == &file_symbols
19250 ? &global_symbols : cu->list_in_scope);
1c809c68 19251 }
c906108c 19252 else
e37fd15a 19253 list_to_add = cu->list_in_scope;
c906108c
SS
19254 }
19255 else
19256 {
19257 /* We do not know the address of this symbol.
c5aa993b
JM
19258 If it is an external symbol and we have type information
19259 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19260 The address of the variable will then be determined from
19261 the minimal symbol table whenever the variable is
19262 referenced. */
e142c38c 19263 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19264
19265 /* Fortran explicitly imports any global symbols to the local
19266 scope by DW_TAG_common_block. */
19267 if (cu->language == language_fortran && die->parent
19268 && die->parent->tag == DW_TAG_common_block)
19269 {
19270 /* SYMBOL_CLASS doesn't matter here because
19271 read_common_block is going to reset it. */
19272 if (!suppress_add)
19273 list_to_add = cu->list_in_scope;
19274 }
19275 else if (attr2 && (DW_UNSND (attr2) != 0)
19276 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19277 {
0fe7935b
DJ
19278 /* A variable with DW_AT_external is never static, but it
19279 may be block-scoped. */
19280 list_to_add = (cu->list_in_scope == &file_symbols
19281 ? &global_symbols : cu->list_in_scope);
19282
f1e6e072 19283 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19284 }
442ddf59
JK
19285 else if (!die_is_declaration (die, cu))
19286 {
19287 /* Use the default LOC_OPTIMIZED_OUT class. */
19288 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19289 if (!suppress_add)
19290 list_to_add = cu->list_in_scope;
442ddf59 19291 }
c906108c
SS
19292 }
19293 break;
19294 case DW_TAG_formal_parameter:
edb3359d
DJ
19295 /* If we are inside a function, mark this as an argument. If
19296 not, we might be looking at an argument to an inlined function
19297 when we do not have enough information to show inlined frames;
19298 pretend it's a local variable in that case so that the user can
19299 still see it. */
19300 if (context_stack_depth > 0
19301 && context_stack[context_stack_depth - 1].name != NULL)
19302 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19303 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19304 if (attr)
19305 {
e7c27a73 19306 var_decode_location (attr, sym, cu);
c906108c 19307 }
e142c38c 19308 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19309 if (attr)
19310 {
e7c27a73 19311 dwarf2_const_value (attr, sym, cu);
c906108c 19312 }
f346a30d 19313
e37fd15a 19314 list_to_add = cu->list_in_scope;
c906108c
SS
19315 break;
19316 case DW_TAG_unspecified_parameters:
19317 /* From varargs functions; gdb doesn't seem to have any
19318 interest in this information, so just ignore it for now.
19319 (FIXME?) */
19320 break;
34eaf542
TT
19321 case DW_TAG_template_type_param:
19322 suppress_add = 1;
19323 /* Fall through. */
c906108c 19324 case DW_TAG_class_type:
680b30c7 19325 case DW_TAG_interface_type:
c906108c
SS
19326 case DW_TAG_structure_type:
19327 case DW_TAG_union_type:
72019c9c 19328 case DW_TAG_set_type:
c906108c 19329 case DW_TAG_enumeration_type:
f1e6e072 19330 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19331 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19332
63d06c5c 19333 {
9c37b5ae 19334 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19335 really ever be static objects: otherwise, if you try
19336 to, say, break of a class's method and you're in a file
19337 which doesn't mention that class, it won't work unless
19338 the check for all static symbols in lookup_symbol_aux
19339 saves you. See the OtherFileClass tests in
19340 gdb.c++/namespace.exp. */
19341
e37fd15a 19342 if (!suppress_add)
34eaf542 19343 {
34eaf542 19344 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19345 && cu->language == language_cplus
34eaf542 19346 ? &global_symbols : cu->list_in_scope);
63d06c5c 19347
64382290 19348 /* The semantics of C++ state that "struct foo {
9c37b5ae 19349 ... }" also defines a typedef for "foo". */
64382290 19350 if (cu->language == language_cplus
45280282 19351 || cu->language == language_ada
c44af4eb
TT
19352 || cu->language == language_d
19353 || cu->language == language_rust)
64382290
TT
19354 {
19355 /* The symbol's name is already allocated along
19356 with this objfile, so we don't need to
19357 duplicate it for the type. */
19358 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19359 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19360 }
63d06c5c
DC
19361 }
19362 }
c906108c
SS
19363 break;
19364 case DW_TAG_typedef:
f1e6e072 19365 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19366 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19367 list_to_add = cu->list_in_scope;
63d06c5c 19368 break;
c906108c 19369 case DW_TAG_base_type:
a02abb62 19370 case DW_TAG_subrange_type:
f1e6e072 19371 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19372 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19373 list_to_add = cu->list_in_scope;
c906108c
SS
19374 break;
19375 case DW_TAG_enumerator:
e142c38c 19376 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19377 if (attr)
19378 {
e7c27a73 19379 dwarf2_const_value (attr, sym, cu);
c906108c 19380 }
63d06c5c
DC
19381 {
19382 /* NOTE: carlton/2003-11-10: See comment above in the
19383 DW_TAG_class_type, etc. block. */
19384
e142c38c 19385 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19386 && cu->language == language_cplus
e142c38c 19387 ? &global_symbols : cu->list_in_scope);
63d06c5c 19388 }
c906108c 19389 break;
74921315 19390 case DW_TAG_imported_declaration:
5c4e30ca 19391 case DW_TAG_namespace:
f1e6e072 19392 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19393 list_to_add = &global_symbols;
5c4e30ca 19394 break;
530e8392
KB
19395 case DW_TAG_module:
19396 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19397 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19398 list_to_add = &global_symbols;
19399 break;
4357ac6c 19400 case DW_TAG_common_block:
f1e6e072 19401 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19402 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19403 add_symbol_to_list (sym, cu->list_in_scope);
19404 break;
c906108c
SS
19405 default:
19406 /* Not a tag we recognize. Hopefully we aren't processing
19407 trash data, but since we must specifically ignore things
19408 we don't recognize, there is nothing else we should do at
0963b4bd 19409 this point. */
e2e0b3e5 19410 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19411 dwarf_tag_name (die->tag));
c906108c
SS
19412 break;
19413 }
df8a16a1 19414
e37fd15a
SW
19415 if (suppress_add)
19416 {
19417 sym->hash_next = objfile->template_symbols;
19418 objfile->template_symbols = sym;
19419 list_to_add = NULL;
19420 }
19421
19422 if (list_to_add != NULL)
19423 add_symbol_to_list (sym, list_to_add);
19424
df8a16a1
DJ
19425 /* For the benefit of old versions of GCC, check for anonymous
19426 namespaces based on the demangled name. */
4d4ec4e5 19427 if (!cu->processing_has_namespace_info
94af9270 19428 && cu->language == language_cplus)
a10964d1 19429 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19430 }
19431 return (sym);
19432}
19433
34eaf542
TT
19434/* A wrapper for new_symbol_full that always allocates a new symbol. */
19435
19436static struct symbol *
19437new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19438{
19439 return new_symbol_full (die, type, cu, NULL);
19440}
19441
98bfdba5
PA
19442/* Given an attr with a DW_FORM_dataN value in host byte order,
19443 zero-extend it as appropriate for the symbol's type. The DWARF
19444 standard (v4) is not entirely clear about the meaning of using
19445 DW_FORM_dataN for a constant with a signed type, where the type is
19446 wider than the data. The conclusion of a discussion on the DWARF
19447 list was that this is unspecified. We choose to always zero-extend
19448 because that is the interpretation long in use by GCC. */
c906108c 19449
98bfdba5 19450static gdb_byte *
ff39bb5e 19451dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19452 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19453{
e7c27a73 19454 struct objfile *objfile = cu->objfile;
e17a4113
UW
19455 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19456 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19457 LONGEST l = DW_UNSND (attr);
19458
19459 if (bits < sizeof (*value) * 8)
19460 {
19461 l &= ((LONGEST) 1 << bits) - 1;
19462 *value = l;
19463 }
19464 else if (bits == sizeof (*value) * 8)
19465 *value = l;
19466 else
19467 {
224c3ddb 19468 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19469 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19470 return bytes;
19471 }
19472
19473 return NULL;
19474}
19475
19476/* Read a constant value from an attribute. Either set *VALUE, or if
19477 the value does not fit in *VALUE, set *BYTES - either already
19478 allocated on the objfile obstack, or newly allocated on OBSTACK,
19479 or, set *BATON, if we translated the constant to a location
19480 expression. */
19481
19482static void
ff39bb5e 19483dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19484 const char *name, struct obstack *obstack,
19485 struct dwarf2_cu *cu,
d521ce57 19486 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19487 struct dwarf2_locexpr_baton **baton)
19488{
19489 struct objfile *objfile = cu->objfile;
19490 struct comp_unit_head *cu_header = &cu->header;
c906108c 19491 struct dwarf_block *blk;
98bfdba5
PA
19492 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19493 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19494
19495 *value = 0;
19496 *bytes = NULL;
19497 *baton = NULL;
c906108c
SS
19498
19499 switch (attr->form)
19500 {
19501 case DW_FORM_addr:
3019eac3 19502 case DW_FORM_GNU_addr_index:
ac56253d 19503 {
ac56253d
TT
19504 gdb_byte *data;
19505
98bfdba5
PA
19506 if (TYPE_LENGTH (type) != cu_header->addr_size)
19507 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19508 cu_header->addr_size,
98bfdba5 19509 TYPE_LENGTH (type));
ac56253d
TT
19510 /* Symbols of this form are reasonably rare, so we just
19511 piggyback on the existing location code rather than writing
19512 a new implementation of symbol_computed_ops. */
8d749320 19513 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19514 (*baton)->per_cu = cu->per_cu;
19515 gdb_assert ((*baton)->per_cu);
ac56253d 19516
98bfdba5 19517 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19518 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19519 (*baton)->data = data;
ac56253d
TT
19520
19521 data[0] = DW_OP_addr;
19522 store_unsigned_integer (&data[1], cu_header->addr_size,
19523 byte_order, DW_ADDR (attr));
19524 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19525 }
c906108c 19526 break;
4ac36638 19527 case DW_FORM_string:
93b5768b 19528 case DW_FORM_strp:
3019eac3 19529 case DW_FORM_GNU_str_index:
36586728 19530 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19531 /* DW_STRING is already allocated on the objfile obstack, point
19532 directly to it. */
d521ce57 19533 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19534 break;
c906108c
SS
19535 case DW_FORM_block1:
19536 case DW_FORM_block2:
19537 case DW_FORM_block4:
19538 case DW_FORM_block:
2dc7f7b3 19539 case DW_FORM_exprloc:
0224619f 19540 case DW_FORM_data16:
c906108c 19541 blk = DW_BLOCK (attr);
98bfdba5
PA
19542 if (TYPE_LENGTH (type) != blk->size)
19543 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19544 TYPE_LENGTH (type));
19545 *bytes = blk->data;
c906108c 19546 break;
2df3850c
JM
19547
19548 /* The DW_AT_const_value attributes are supposed to carry the
19549 symbol's value "represented as it would be on the target
19550 architecture." By the time we get here, it's already been
19551 converted to host endianness, so we just need to sign- or
19552 zero-extend it as appropriate. */
19553 case DW_FORM_data1:
3aef2284 19554 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19555 break;
c906108c 19556 case DW_FORM_data2:
3aef2284 19557 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19558 break;
c906108c 19559 case DW_FORM_data4:
3aef2284 19560 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19561 break;
c906108c 19562 case DW_FORM_data8:
3aef2284 19563 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19564 break;
19565
c906108c 19566 case DW_FORM_sdata:
663c44ac 19567 case DW_FORM_implicit_const:
98bfdba5 19568 *value = DW_SND (attr);
2df3850c
JM
19569 break;
19570
c906108c 19571 case DW_FORM_udata:
98bfdba5 19572 *value = DW_UNSND (attr);
c906108c 19573 break;
2df3850c 19574
c906108c 19575 default:
4d3c2250 19576 complaint (&symfile_complaints,
e2e0b3e5 19577 _("unsupported const value attribute form: '%s'"),
4d3c2250 19578 dwarf_form_name (attr->form));
98bfdba5 19579 *value = 0;
c906108c
SS
19580 break;
19581 }
19582}
19583
2df3850c 19584
98bfdba5
PA
19585/* Copy constant value from an attribute to a symbol. */
19586
2df3850c 19587static void
ff39bb5e 19588dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19589 struct dwarf2_cu *cu)
2df3850c 19590{
98bfdba5 19591 struct objfile *objfile = cu->objfile;
12df843f 19592 LONGEST value;
d521ce57 19593 const gdb_byte *bytes;
98bfdba5 19594 struct dwarf2_locexpr_baton *baton;
2df3850c 19595
98bfdba5
PA
19596 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19597 SYMBOL_PRINT_NAME (sym),
19598 &objfile->objfile_obstack, cu,
19599 &value, &bytes, &baton);
2df3850c 19600
98bfdba5
PA
19601 if (baton != NULL)
19602 {
98bfdba5 19603 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19604 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19605 }
19606 else if (bytes != NULL)
19607 {
19608 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19609 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19610 }
19611 else
19612 {
19613 SYMBOL_VALUE (sym) = value;
f1e6e072 19614 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19615 }
2df3850c
JM
19616}
19617
c906108c
SS
19618/* Return the type of the die in question using its DW_AT_type attribute. */
19619
19620static struct type *
e7c27a73 19621die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19622{
c906108c 19623 struct attribute *type_attr;
c906108c 19624
e142c38c 19625 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19626 if (!type_attr)
19627 {
19628 /* A missing DW_AT_type represents a void type. */
46bf5051 19629 return objfile_type (cu->objfile)->builtin_void;
c906108c 19630 }
348e048f 19631
673bfd45 19632 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19633}
19634
b4ba55a1
JB
19635/* True iff CU's producer generates GNAT Ada auxiliary information
19636 that allows to find parallel types through that information instead
19637 of having to do expensive parallel lookups by type name. */
19638
19639static int
19640need_gnat_info (struct dwarf2_cu *cu)
19641{
19642 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19643 of GNAT produces this auxiliary information, without any indication
19644 that it is produced. Part of enhancing the FSF version of GNAT
19645 to produce that information will be to put in place an indicator
19646 that we can use in order to determine whether the descriptive type
19647 info is available or not. One suggestion that has been made is
19648 to use a new attribute, attached to the CU die. For now, assume
19649 that the descriptive type info is not available. */
19650 return 0;
19651}
19652
b4ba55a1
JB
19653/* Return the auxiliary type of the die in question using its
19654 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19655 attribute is not present. */
19656
19657static struct type *
19658die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19659{
b4ba55a1 19660 struct attribute *type_attr;
b4ba55a1
JB
19661
19662 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19663 if (!type_attr)
19664 return NULL;
19665
673bfd45 19666 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19667}
19668
19669/* If DIE has a descriptive_type attribute, then set the TYPE's
19670 descriptive type accordingly. */
19671
19672static void
19673set_descriptive_type (struct type *type, struct die_info *die,
19674 struct dwarf2_cu *cu)
19675{
19676 struct type *descriptive_type = die_descriptive_type (die, cu);
19677
19678 if (descriptive_type)
19679 {
19680 ALLOCATE_GNAT_AUX_TYPE (type);
19681 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19682 }
19683}
19684
c906108c
SS
19685/* Return the containing type of the die in question using its
19686 DW_AT_containing_type attribute. */
19687
19688static struct type *
e7c27a73 19689die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19690{
c906108c 19691 struct attribute *type_attr;
c906108c 19692
e142c38c 19693 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19694 if (!type_attr)
19695 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19696 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19697
673bfd45 19698 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19699}
19700
ac9ec31b
DE
19701/* Return an error marker type to use for the ill formed type in DIE/CU. */
19702
19703static struct type *
19704build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19705{
19706 struct objfile *objfile = dwarf2_per_objfile->objfile;
19707 char *message, *saved;
19708
19709 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19710 objfile_name (objfile),
9c541725
PA
19711 to_underlying (cu->header.sect_off),
19712 to_underlying (die->sect_off));
224c3ddb
SM
19713 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19714 message, strlen (message));
ac9ec31b
DE
19715 xfree (message);
19716
19f392bc 19717 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19718}
19719
673bfd45 19720/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19721 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19722 DW_AT_containing_type.
673bfd45
DE
19723 If there is no type substitute an error marker. */
19724
c906108c 19725static struct type *
ff39bb5e 19726lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19727 struct dwarf2_cu *cu)
c906108c 19728{
bb5ed363 19729 struct objfile *objfile = cu->objfile;
f792889a
DJ
19730 struct type *this_type;
19731
ac9ec31b
DE
19732 gdb_assert (attr->name == DW_AT_type
19733 || attr->name == DW_AT_GNAT_descriptive_type
19734 || attr->name == DW_AT_containing_type);
19735
673bfd45
DE
19736 /* First see if we have it cached. */
19737
36586728
TT
19738 if (attr->form == DW_FORM_GNU_ref_alt)
19739 {
19740 struct dwarf2_per_cu_data *per_cu;
9c541725 19741 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19742
9c541725
PA
19743 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19744 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19745 }
7771576e 19746 else if (attr_form_is_ref (attr))
673bfd45 19747 {
9c541725 19748 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19749
9c541725 19750 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19751 }
55f1336d 19752 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19753 {
ac9ec31b 19754 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19755
ac9ec31b 19756 return get_signatured_type (die, signature, cu);
673bfd45
DE
19757 }
19758 else
19759 {
ac9ec31b
DE
19760 complaint (&symfile_complaints,
19761 _("Dwarf Error: Bad type attribute %s in DIE"
19762 " at 0x%x [in module %s]"),
9c541725 19763 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19764 objfile_name (objfile));
ac9ec31b 19765 return build_error_marker_type (cu, die);
673bfd45
DE
19766 }
19767
19768 /* If not cached we need to read it in. */
19769
19770 if (this_type == NULL)
19771 {
ac9ec31b 19772 struct die_info *type_die = NULL;
673bfd45
DE
19773 struct dwarf2_cu *type_cu = cu;
19774
7771576e 19775 if (attr_form_is_ref (attr))
ac9ec31b
DE
19776 type_die = follow_die_ref (die, attr, &type_cu);
19777 if (type_die == NULL)
19778 return build_error_marker_type (cu, die);
19779 /* If we find the type now, it's probably because the type came
3019eac3
DE
19780 from an inter-CU reference and the type's CU got expanded before
19781 ours. */
ac9ec31b 19782 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19783 }
19784
19785 /* If we still don't have a type use an error marker. */
19786
19787 if (this_type == NULL)
ac9ec31b 19788 return build_error_marker_type (cu, die);
673bfd45 19789
f792889a 19790 return this_type;
c906108c
SS
19791}
19792
673bfd45
DE
19793/* Return the type in DIE, CU.
19794 Returns NULL for invalid types.
19795
02142a6c 19796 This first does a lookup in die_type_hash,
673bfd45
DE
19797 and only reads the die in if necessary.
19798
19799 NOTE: This can be called when reading in partial or full symbols. */
19800
f792889a 19801static struct type *
e7c27a73 19802read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19803{
f792889a
DJ
19804 struct type *this_type;
19805
19806 this_type = get_die_type (die, cu);
19807 if (this_type)
19808 return this_type;
19809
673bfd45
DE
19810 return read_type_die_1 (die, cu);
19811}
19812
19813/* Read the type in DIE, CU.
19814 Returns NULL for invalid types. */
19815
19816static struct type *
19817read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19818{
19819 struct type *this_type = NULL;
19820
c906108c
SS
19821 switch (die->tag)
19822 {
19823 case DW_TAG_class_type:
680b30c7 19824 case DW_TAG_interface_type:
c906108c
SS
19825 case DW_TAG_structure_type:
19826 case DW_TAG_union_type:
f792889a 19827 this_type = read_structure_type (die, cu);
c906108c
SS
19828 break;
19829 case DW_TAG_enumeration_type:
f792889a 19830 this_type = read_enumeration_type (die, cu);
c906108c
SS
19831 break;
19832 case DW_TAG_subprogram:
19833 case DW_TAG_subroutine_type:
edb3359d 19834 case DW_TAG_inlined_subroutine:
f792889a 19835 this_type = read_subroutine_type (die, cu);
c906108c
SS
19836 break;
19837 case DW_TAG_array_type:
f792889a 19838 this_type = read_array_type (die, cu);
c906108c 19839 break;
72019c9c 19840 case DW_TAG_set_type:
f792889a 19841 this_type = read_set_type (die, cu);
72019c9c 19842 break;
c906108c 19843 case DW_TAG_pointer_type:
f792889a 19844 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19845 break;
19846 case DW_TAG_ptr_to_member_type:
f792889a 19847 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19848 break;
19849 case DW_TAG_reference_type:
4297a3f0
AV
19850 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19851 break;
19852 case DW_TAG_rvalue_reference_type:
19853 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19854 break;
19855 case DW_TAG_const_type:
f792889a 19856 this_type = read_tag_const_type (die, cu);
c906108c
SS
19857 break;
19858 case DW_TAG_volatile_type:
f792889a 19859 this_type = read_tag_volatile_type (die, cu);
c906108c 19860 break;
06d66ee9
TT
19861 case DW_TAG_restrict_type:
19862 this_type = read_tag_restrict_type (die, cu);
19863 break;
c906108c 19864 case DW_TAG_string_type:
f792889a 19865 this_type = read_tag_string_type (die, cu);
c906108c
SS
19866 break;
19867 case DW_TAG_typedef:
f792889a 19868 this_type = read_typedef (die, cu);
c906108c 19869 break;
a02abb62 19870 case DW_TAG_subrange_type:
f792889a 19871 this_type = read_subrange_type (die, cu);
a02abb62 19872 break;
c906108c 19873 case DW_TAG_base_type:
f792889a 19874 this_type = read_base_type (die, cu);
c906108c 19875 break;
81a17f79 19876 case DW_TAG_unspecified_type:
f792889a 19877 this_type = read_unspecified_type (die, cu);
81a17f79 19878 break;
0114d602
DJ
19879 case DW_TAG_namespace:
19880 this_type = read_namespace_type (die, cu);
19881 break;
f55ee35c
JK
19882 case DW_TAG_module:
19883 this_type = read_module_type (die, cu);
19884 break;
a2c2acaf
MW
19885 case DW_TAG_atomic_type:
19886 this_type = read_tag_atomic_type (die, cu);
19887 break;
c906108c 19888 default:
3e43a32a
MS
19889 complaint (&symfile_complaints,
19890 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19891 dwarf_tag_name (die->tag));
c906108c
SS
19892 break;
19893 }
63d06c5c 19894
f792889a 19895 return this_type;
63d06c5c
DC
19896}
19897
abc72ce4
DE
19898/* See if we can figure out if the class lives in a namespace. We do
19899 this by looking for a member function; its demangled name will
19900 contain namespace info, if there is any.
19901 Return the computed name or NULL.
19902 Space for the result is allocated on the objfile's obstack.
19903 This is the full-die version of guess_partial_die_structure_name.
19904 In this case we know DIE has no useful parent. */
19905
19906static char *
19907guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19908{
19909 struct die_info *spec_die;
19910 struct dwarf2_cu *spec_cu;
19911 struct die_info *child;
19912
19913 spec_cu = cu;
19914 spec_die = die_specification (die, &spec_cu);
19915 if (spec_die != NULL)
19916 {
19917 die = spec_die;
19918 cu = spec_cu;
19919 }
19920
19921 for (child = die->child;
19922 child != NULL;
19923 child = child->sibling)
19924 {
19925 if (child->tag == DW_TAG_subprogram)
19926 {
7d45c7c3 19927 const char *linkage_name;
abc72ce4 19928
7d45c7c3
KB
19929 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19930 if (linkage_name == NULL)
19931 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19932 cu);
19933 if (linkage_name != NULL)
abc72ce4
DE
19934 {
19935 char *actual_name
19936 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19937 linkage_name);
abc72ce4
DE
19938 char *name = NULL;
19939
19940 if (actual_name != NULL)
19941 {
15d034d0 19942 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19943
19944 if (die_name != NULL
19945 && strcmp (die_name, actual_name) != 0)
19946 {
19947 /* Strip off the class name from the full name.
19948 We want the prefix. */
19949 int die_name_len = strlen (die_name);
19950 int actual_name_len = strlen (actual_name);
19951
19952 /* Test for '::' as a sanity check. */
19953 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19954 && actual_name[actual_name_len
19955 - die_name_len - 1] == ':')
224c3ddb
SM
19956 name = (char *) obstack_copy0 (
19957 &cu->objfile->per_bfd->storage_obstack,
19958 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19959 }
19960 }
19961 xfree (actual_name);
19962 return name;
19963 }
19964 }
19965 }
19966
19967 return NULL;
19968}
19969
96408a79
SA
19970/* GCC might emit a nameless typedef that has a linkage name. Determine the
19971 prefix part in such case. See
19972 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19973
a121b7c1 19974static const char *
96408a79
SA
19975anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19976{
19977 struct attribute *attr;
e6a959d6 19978 const char *base;
96408a79
SA
19979
19980 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19981 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19982 return NULL;
19983
7d45c7c3 19984 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19985 return NULL;
19986
19987 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19988 if (attr == NULL)
19989 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19990 if (attr == NULL || DW_STRING (attr) == NULL)
19991 return NULL;
19992
19993 /* dwarf2_name had to be already called. */
19994 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19995
19996 /* Strip the base name, keep any leading namespaces/classes. */
19997 base = strrchr (DW_STRING (attr), ':');
19998 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19999 return "";
20000
224c3ddb
SM
20001 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20002 DW_STRING (attr),
20003 &base[-1] - DW_STRING (attr));
96408a79
SA
20004}
20005
fdde2d81 20006/* Return the name of the namespace/class that DIE is defined within,
0114d602 20007 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20008
0114d602
DJ
20009 For example, if we're within the method foo() in the following
20010 code:
20011
20012 namespace N {
20013 class C {
20014 void foo () {
20015 }
20016 };
20017 }
20018
20019 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20020
0d5cff50 20021static const char *
e142c38c 20022determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20023{
0114d602
DJ
20024 struct die_info *parent, *spec_die;
20025 struct dwarf2_cu *spec_cu;
20026 struct type *parent_type;
a121b7c1 20027 const char *retval;
63d06c5c 20028
9c37b5ae 20029 if (cu->language != language_cplus
c44af4eb
TT
20030 && cu->language != language_fortran && cu->language != language_d
20031 && cu->language != language_rust)
0114d602
DJ
20032 return "";
20033
96408a79
SA
20034 retval = anonymous_struct_prefix (die, cu);
20035 if (retval)
20036 return retval;
20037
0114d602
DJ
20038 /* We have to be careful in the presence of DW_AT_specification.
20039 For example, with GCC 3.4, given the code
20040
20041 namespace N {
20042 void foo() {
20043 // Definition of N::foo.
20044 }
20045 }
20046
20047 then we'll have a tree of DIEs like this:
20048
20049 1: DW_TAG_compile_unit
20050 2: DW_TAG_namespace // N
20051 3: DW_TAG_subprogram // declaration of N::foo
20052 4: DW_TAG_subprogram // definition of N::foo
20053 DW_AT_specification // refers to die #3
20054
20055 Thus, when processing die #4, we have to pretend that we're in
20056 the context of its DW_AT_specification, namely the contex of die
20057 #3. */
20058 spec_cu = cu;
20059 spec_die = die_specification (die, &spec_cu);
20060 if (spec_die == NULL)
20061 parent = die->parent;
20062 else
63d06c5c 20063 {
0114d602
DJ
20064 parent = spec_die->parent;
20065 cu = spec_cu;
63d06c5c 20066 }
0114d602
DJ
20067
20068 if (parent == NULL)
20069 return "";
98bfdba5
PA
20070 else if (parent->building_fullname)
20071 {
20072 const char *name;
20073 const char *parent_name;
20074
20075 /* It has been seen on RealView 2.2 built binaries,
20076 DW_TAG_template_type_param types actually _defined_ as
20077 children of the parent class:
20078
20079 enum E {};
20080 template class <class Enum> Class{};
20081 Class<enum E> class_e;
20082
20083 1: DW_TAG_class_type (Class)
20084 2: DW_TAG_enumeration_type (E)
20085 3: DW_TAG_enumerator (enum1:0)
20086 3: DW_TAG_enumerator (enum2:1)
20087 ...
20088 2: DW_TAG_template_type_param
20089 DW_AT_type DW_FORM_ref_udata (E)
20090
20091 Besides being broken debug info, it can put GDB into an
20092 infinite loop. Consider:
20093
20094 When we're building the full name for Class<E>, we'll start
20095 at Class, and go look over its template type parameters,
20096 finding E. We'll then try to build the full name of E, and
20097 reach here. We're now trying to build the full name of E,
20098 and look over the parent DIE for containing scope. In the
20099 broken case, if we followed the parent DIE of E, we'd again
20100 find Class, and once again go look at its template type
20101 arguments, etc., etc. Simply don't consider such parent die
20102 as source-level parent of this die (it can't be, the language
20103 doesn't allow it), and break the loop here. */
20104 name = dwarf2_name (die, cu);
20105 parent_name = dwarf2_name (parent, cu);
20106 complaint (&symfile_complaints,
20107 _("template param type '%s' defined within parent '%s'"),
20108 name ? name : "<unknown>",
20109 parent_name ? parent_name : "<unknown>");
20110 return "";
20111 }
63d06c5c 20112 else
0114d602
DJ
20113 switch (parent->tag)
20114 {
63d06c5c 20115 case DW_TAG_namespace:
0114d602 20116 parent_type = read_type_die (parent, cu);
acebe513
UW
20117 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20118 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20119 Work around this problem here. */
20120 if (cu->language == language_cplus
20121 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20122 return "";
0114d602
DJ
20123 /* We give a name to even anonymous namespaces. */
20124 return TYPE_TAG_NAME (parent_type);
63d06c5c 20125 case DW_TAG_class_type:
680b30c7 20126 case DW_TAG_interface_type:
63d06c5c 20127 case DW_TAG_structure_type:
0114d602 20128 case DW_TAG_union_type:
f55ee35c 20129 case DW_TAG_module:
0114d602
DJ
20130 parent_type = read_type_die (parent, cu);
20131 if (TYPE_TAG_NAME (parent_type) != NULL)
20132 return TYPE_TAG_NAME (parent_type);
20133 else
20134 /* An anonymous structure is only allowed non-static data
20135 members; no typedefs, no member functions, et cetera.
20136 So it does not need a prefix. */
20137 return "";
abc72ce4 20138 case DW_TAG_compile_unit:
95554aad 20139 case DW_TAG_partial_unit:
abc72ce4
DE
20140 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20141 if (cu->language == language_cplus
8b70b953 20142 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20143 && die->child != NULL
20144 && (die->tag == DW_TAG_class_type
20145 || die->tag == DW_TAG_structure_type
20146 || die->tag == DW_TAG_union_type))
20147 {
20148 char *name = guess_full_die_structure_name (die, cu);
20149 if (name != NULL)
20150 return name;
20151 }
20152 return "";
3d567982
TT
20153 case DW_TAG_enumeration_type:
20154 parent_type = read_type_die (parent, cu);
20155 if (TYPE_DECLARED_CLASS (parent_type))
20156 {
20157 if (TYPE_TAG_NAME (parent_type) != NULL)
20158 return TYPE_TAG_NAME (parent_type);
20159 return "";
20160 }
20161 /* Fall through. */
63d06c5c 20162 default:
8176b9b8 20163 return determine_prefix (parent, cu);
63d06c5c 20164 }
63d06c5c
DC
20165}
20166
3e43a32a
MS
20167/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20168 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20169 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20170 an obconcat, otherwise allocate storage for the result. The CU argument is
20171 used to determine the language and hence, the appropriate separator. */
987504bb 20172
f55ee35c 20173#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20174
20175static char *
f55ee35c
JK
20176typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20177 int physname, struct dwarf2_cu *cu)
63d06c5c 20178{
f55ee35c 20179 const char *lead = "";
5c315b68 20180 const char *sep;
63d06c5c 20181
3e43a32a
MS
20182 if (suffix == NULL || suffix[0] == '\0'
20183 || prefix == NULL || prefix[0] == '\0')
987504bb 20184 sep = "";
45280282
IB
20185 else if (cu->language == language_d)
20186 {
20187 /* For D, the 'main' function could be defined in any module, but it
20188 should never be prefixed. */
20189 if (strcmp (suffix, "D main") == 0)
20190 {
20191 prefix = "";
20192 sep = "";
20193 }
20194 else
20195 sep = ".";
20196 }
f55ee35c
JK
20197 else if (cu->language == language_fortran && physname)
20198 {
20199 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20200 DW_AT_MIPS_linkage_name is preferred and used instead. */
20201
20202 lead = "__";
20203 sep = "_MOD_";
20204 }
987504bb
JJ
20205 else
20206 sep = "::";
63d06c5c 20207
6dd47d34
DE
20208 if (prefix == NULL)
20209 prefix = "";
20210 if (suffix == NULL)
20211 suffix = "";
20212
987504bb
JJ
20213 if (obs == NULL)
20214 {
3e43a32a 20215 char *retval
224c3ddb
SM
20216 = ((char *)
20217 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20218
f55ee35c
JK
20219 strcpy (retval, lead);
20220 strcat (retval, prefix);
6dd47d34
DE
20221 strcat (retval, sep);
20222 strcat (retval, suffix);
63d06c5c
DC
20223 return retval;
20224 }
987504bb
JJ
20225 else
20226 {
20227 /* We have an obstack. */
f55ee35c 20228 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20229 }
63d06c5c
DC
20230}
20231
c906108c
SS
20232/* Return sibling of die, NULL if no sibling. */
20233
f9aca02d 20234static struct die_info *
fba45db2 20235sibling_die (struct die_info *die)
c906108c 20236{
639d11d3 20237 return die->sibling;
c906108c
SS
20238}
20239
71c25dea
TT
20240/* Get name of a die, return NULL if not found. */
20241
15d034d0
TT
20242static const char *
20243dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20244 struct obstack *obstack)
20245{
20246 if (name && cu->language == language_cplus)
20247 {
2f408ecb 20248 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20249
2f408ecb 20250 if (!canon_name.empty ())
71c25dea 20251 {
2f408ecb
PA
20252 if (canon_name != name)
20253 name = (const char *) obstack_copy0 (obstack,
20254 canon_name.c_str (),
20255 canon_name.length ());
71c25dea
TT
20256 }
20257 }
20258
20259 return name;
c906108c
SS
20260}
20261
96553a0c
DE
20262/* Get name of a die, return NULL if not found.
20263 Anonymous namespaces are converted to their magic string. */
9219021c 20264
15d034d0 20265static const char *
e142c38c 20266dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20267{
20268 struct attribute *attr;
20269
e142c38c 20270 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20271 if ((!attr || !DW_STRING (attr))
96553a0c 20272 && die->tag != DW_TAG_namespace
53832f31
TT
20273 && die->tag != DW_TAG_class_type
20274 && die->tag != DW_TAG_interface_type
20275 && die->tag != DW_TAG_structure_type
20276 && die->tag != DW_TAG_union_type)
71c25dea
TT
20277 return NULL;
20278
20279 switch (die->tag)
20280 {
20281 case DW_TAG_compile_unit:
95554aad 20282 case DW_TAG_partial_unit:
71c25dea
TT
20283 /* Compilation units have a DW_AT_name that is a filename, not
20284 a source language identifier. */
20285 case DW_TAG_enumeration_type:
20286 case DW_TAG_enumerator:
20287 /* These tags always have simple identifiers already; no need
20288 to canonicalize them. */
20289 return DW_STRING (attr);
907af001 20290
96553a0c
DE
20291 case DW_TAG_namespace:
20292 if (attr != NULL && DW_STRING (attr) != NULL)
20293 return DW_STRING (attr);
20294 return CP_ANONYMOUS_NAMESPACE_STR;
20295
907af001
UW
20296 case DW_TAG_class_type:
20297 case DW_TAG_interface_type:
20298 case DW_TAG_structure_type:
20299 case DW_TAG_union_type:
20300 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20301 structures or unions. These were of the form "._%d" in GCC 4.1,
20302 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20303 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20304 if (attr && DW_STRING (attr)
61012eef
GB
20305 && (startswith (DW_STRING (attr), "._")
20306 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20307 return NULL;
53832f31
TT
20308
20309 /* GCC might emit a nameless typedef that has a linkage name. See
20310 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20311 if (!attr || DW_STRING (attr) == NULL)
20312 {
df5c6c50 20313 char *demangled = NULL;
53832f31
TT
20314
20315 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20316 if (attr == NULL)
20317 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20318
20319 if (attr == NULL || DW_STRING (attr) == NULL)
20320 return NULL;
20321
df5c6c50
JK
20322 /* Avoid demangling DW_STRING (attr) the second time on a second
20323 call for the same DIE. */
20324 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20325 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20326
20327 if (demangled)
20328 {
e6a959d6 20329 const char *base;
96408a79 20330
53832f31 20331 /* FIXME: we already did this for the partial symbol... */
34a68019 20332 DW_STRING (attr)
224c3ddb
SM
20333 = ((const char *)
20334 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20335 demangled, strlen (demangled)));
53832f31
TT
20336 DW_STRING_IS_CANONICAL (attr) = 1;
20337 xfree (demangled);
96408a79
SA
20338
20339 /* Strip any leading namespaces/classes, keep only the base name.
20340 DW_AT_name for named DIEs does not contain the prefixes. */
20341 base = strrchr (DW_STRING (attr), ':');
20342 if (base && base > DW_STRING (attr) && base[-1] == ':')
20343 return &base[1];
20344 else
20345 return DW_STRING (attr);
53832f31
TT
20346 }
20347 }
907af001
UW
20348 break;
20349
71c25dea 20350 default:
907af001
UW
20351 break;
20352 }
20353
20354 if (!DW_STRING_IS_CANONICAL (attr))
20355 {
20356 DW_STRING (attr)
20357 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20358 &cu->objfile->per_bfd->storage_obstack);
907af001 20359 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20360 }
907af001 20361 return DW_STRING (attr);
9219021c
DC
20362}
20363
20364/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20365 is none. *EXT_CU is the CU containing DIE on input, and the CU
20366 containing the return value on output. */
9219021c
DC
20367
20368static struct die_info *
f2f0e013 20369dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20370{
20371 struct attribute *attr;
9219021c 20372
f2f0e013 20373 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20374 if (attr == NULL)
20375 return NULL;
20376
f2f0e013 20377 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20378}
20379
c906108c
SS
20380/* Convert a DIE tag into its string name. */
20381
f39c6ffd 20382static const char *
aa1ee363 20383dwarf_tag_name (unsigned tag)
c906108c 20384{
f39c6ffd
TT
20385 const char *name = get_DW_TAG_name (tag);
20386
20387 if (name == NULL)
20388 return "DW_TAG_<unknown>";
20389
20390 return name;
c906108c
SS
20391}
20392
20393/* Convert a DWARF attribute code into its string name. */
20394
f39c6ffd 20395static const char *
aa1ee363 20396dwarf_attr_name (unsigned attr)
c906108c 20397{
f39c6ffd
TT
20398 const char *name;
20399
c764a876 20400#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20401 if (attr == DW_AT_MIPS_fde)
20402 return "DW_AT_MIPS_fde";
20403#else
20404 if (attr == DW_AT_HP_block_index)
20405 return "DW_AT_HP_block_index";
c764a876 20406#endif
f39c6ffd
TT
20407
20408 name = get_DW_AT_name (attr);
20409
20410 if (name == NULL)
20411 return "DW_AT_<unknown>";
20412
20413 return name;
c906108c
SS
20414}
20415
20416/* Convert a DWARF value form code into its string name. */
20417
f39c6ffd 20418static const char *
aa1ee363 20419dwarf_form_name (unsigned form)
c906108c 20420{
f39c6ffd
TT
20421 const char *name = get_DW_FORM_name (form);
20422
20423 if (name == NULL)
20424 return "DW_FORM_<unknown>";
20425
20426 return name;
c906108c
SS
20427}
20428
a121b7c1 20429static const char *
fba45db2 20430dwarf_bool_name (unsigned mybool)
c906108c
SS
20431{
20432 if (mybool)
20433 return "TRUE";
20434 else
20435 return "FALSE";
20436}
20437
20438/* Convert a DWARF type code into its string name. */
20439
f39c6ffd 20440static const char *
aa1ee363 20441dwarf_type_encoding_name (unsigned enc)
c906108c 20442{
f39c6ffd 20443 const char *name = get_DW_ATE_name (enc);
c906108c 20444
f39c6ffd
TT
20445 if (name == NULL)
20446 return "DW_ATE_<unknown>";
c906108c 20447
f39c6ffd 20448 return name;
c906108c 20449}
c906108c 20450
f9aca02d 20451static void
d97bc12b 20452dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20453{
20454 unsigned int i;
20455
d97bc12b
DE
20456 print_spaces (indent, f);
20457 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20458 dwarf_tag_name (die->tag), die->abbrev,
20459 to_underlying (die->sect_off));
d97bc12b
DE
20460
20461 if (die->parent != NULL)
20462 {
20463 print_spaces (indent, f);
20464 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20465 to_underlying (die->parent->sect_off));
d97bc12b
DE
20466 }
20467
20468 print_spaces (indent, f);
20469 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20470 dwarf_bool_name (die->child != NULL));
c906108c 20471
d97bc12b
DE
20472 print_spaces (indent, f);
20473 fprintf_unfiltered (f, " attributes:\n");
20474
c906108c
SS
20475 for (i = 0; i < die->num_attrs; ++i)
20476 {
d97bc12b
DE
20477 print_spaces (indent, f);
20478 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20479 dwarf_attr_name (die->attrs[i].name),
20480 dwarf_form_name (die->attrs[i].form));
d97bc12b 20481
c906108c
SS
20482 switch (die->attrs[i].form)
20483 {
c906108c 20484 case DW_FORM_addr:
3019eac3 20485 case DW_FORM_GNU_addr_index:
d97bc12b 20486 fprintf_unfiltered (f, "address: ");
5af949e3 20487 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20488 break;
20489 case DW_FORM_block2:
20490 case DW_FORM_block4:
20491 case DW_FORM_block:
20492 case DW_FORM_block1:
56eb65bd
SP
20493 fprintf_unfiltered (f, "block: size %s",
20494 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20495 break;
2dc7f7b3 20496 case DW_FORM_exprloc:
56eb65bd
SP
20497 fprintf_unfiltered (f, "expression: size %s",
20498 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20499 break;
0224619f
JK
20500 case DW_FORM_data16:
20501 fprintf_unfiltered (f, "constant of 16 bytes");
20502 break;
4568ecf9
DE
20503 case DW_FORM_ref_addr:
20504 fprintf_unfiltered (f, "ref address: ");
20505 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20506 break;
36586728
TT
20507 case DW_FORM_GNU_ref_alt:
20508 fprintf_unfiltered (f, "alt ref address: ");
20509 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20510 break;
10b3939b
DJ
20511 case DW_FORM_ref1:
20512 case DW_FORM_ref2:
20513 case DW_FORM_ref4:
4568ecf9
DE
20514 case DW_FORM_ref8:
20515 case DW_FORM_ref_udata:
d97bc12b 20516 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20517 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20518 break;
c906108c
SS
20519 case DW_FORM_data1:
20520 case DW_FORM_data2:
20521 case DW_FORM_data4:
ce5d95e1 20522 case DW_FORM_data8:
c906108c
SS
20523 case DW_FORM_udata:
20524 case DW_FORM_sdata:
43bbcdc2
PH
20525 fprintf_unfiltered (f, "constant: %s",
20526 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20527 break;
2dc7f7b3
TT
20528 case DW_FORM_sec_offset:
20529 fprintf_unfiltered (f, "section offset: %s",
20530 pulongest (DW_UNSND (&die->attrs[i])));
20531 break;
55f1336d 20532 case DW_FORM_ref_sig8:
ac9ec31b
DE
20533 fprintf_unfiltered (f, "signature: %s",
20534 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20535 break;
c906108c 20536 case DW_FORM_string:
4bdf3d34 20537 case DW_FORM_strp:
43988095 20538 case DW_FORM_line_strp:
3019eac3 20539 case DW_FORM_GNU_str_index:
36586728 20540 case DW_FORM_GNU_strp_alt:
8285870a 20541 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20542 DW_STRING (&die->attrs[i])
8285870a
JK
20543 ? DW_STRING (&die->attrs[i]) : "",
20544 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20545 break;
20546 case DW_FORM_flag:
20547 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20548 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20549 else
d97bc12b 20550 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20551 break;
2dc7f7b3
TT
20552 case DW_FORM_flag_present:
20553 fprintf_unfiltered (f, "flag: TRUE");
20554 break;
a8329558 20555 case DW_FORM_indirect:
0963b4bd
MS
20556 /* The reader will have reduced the indirect form to
20557 the "base form" so this form should not occur. */
3e43a32a
MS
20558 fprintf_unfiltered (f,
20559 "unexpected attribute form: DW_FORM_indirect");
a8329558 20560 break;
663c44ac
JK
20561 case DW_FORM_implicit_const:
20562 fprintf_unfiltered (f, "constant: %s",
20563 plongest (DW_SND (&die->attrs[i])));
20564 break;
c906108c 20565 default:
d97bc12b 20566 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20567 die->attrs[i].form);
d97bc12b 20568 break;
c906108c 20569 }
d97bc12b 20570 fprintf_unfiltered (f, "\n");
c906108c
SS
20571 }
20572}
20573
f9aca02d 20574static void
d97bc12b 20575dump_die_for_error (struct die_info *die)
c906108c 20576{
d97bc12b
DE
20577 dump_die_shallow (gdb_stderr, 0, die);
20578}
20579
20580static void
20581dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20582{
20583 int indent = level * 4;
20584
20585 gdb_assert (die != NULL);
20586
20587 if (level >= max_level)
20588 return;
20589
20590 dump_die_shallow (f, indent, die);
20591
20592 if (die->child != NULL)
c906108c 20593 {
d97bc12b
DE
20594 print_spaces (indent, f);
20595 fprintf_unfiltered (f, " Children:");
20596 if (level + 1 < max_level)
20597 {
20598 fprintf_unfiltered (f, "\n");
20599 dump_die_1 (f, level + 1, max_level, die->child);
20600 }
20601 else
20602 {
3e43a32a
MS
20603 fprintf_unfiltered (f,
20604 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20605 }
20606 }
20607
20608 if (die->sibling != NULL && level > 0)
20609 {
20610 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20611 }
20612}
20613
d97bc12b
DE
20614/* This is called from the pdie macro in gdbinit.in.
20615 It's not static so gcc will keep a copy callable from gdb. */
20616
20617void
20618dump_die (struct die_info *die, int max_level)
20619{
20620 dump_die_1 (gdb_stdlog, 0, max_level, die);
20621}
20622
f9aca02d 20623static void
51545339 20624store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20625{
51545339 20626 void **slot;
c906108c 20627
9c541725
PA
20628 slot = htab_find_slot_with_hash (cu->die_hash, die,
20629 to_underlying (die->sect_off),
b64f50a1 20630 INSERT);
51545339
DJ
20631
20632 *slot = die;
c906108c
SS
20633}
20634
b64f50a1
JK
20635/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20636 required kind. */
20637
20638static sect_offset
ff39bb5e 20639dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20640{
7771576e 20641 if (attr_form_is_ref (attr))
9c541725 20642 return (sect_offset) DW_UNSND (attr);
93311388
DE
20643
20644 complaint (&symfile_complaints,
20645 _("unsupported die ref attribute form: '%s'"),
20646 dwarf_form_name (attr->form));
9c541725 20647 return {};
c906108c
SS
20648}
20649
43bbcdc2
PH
20650/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20651 * the value held by the attribute is not constant. */
a02abb62 20652
43bbcdc2 20653static LONGEST
ff39bb5e 20654dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20655{
663c44ac 20656 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20657 return DW_SND (attr);
20658 else if (attr->form == DW_FORM_udata
20659 || attr->form == DW_FORM_data1
20660 || attr->form == DW_FORM_data2
20661 || attr->form == DW_FORM_data4
20662 || attr->form == DW_FORM_data8)
20663 return DW_UNSND (attr);
20664 else
20665 {
0224619f 20666 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20667 complaint (&symfile_complaints,
20668 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20669 dwarf_form_name (attr->form));
20670 return default_value;
20671 }
20672}
20673
348e048f
DE
20674/* Follow reference or signature attribute ATTR of SRC_DIE.
20675 On entry *REF_CU is the CU of SRC_DIE.
20676 On exit *REF_CU is the CU of the result. */
20677
20678static struct die_info *
ff39bb5e 20679follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20680 struct dwarf2_cu **ref_cu)
20681{
20682 struct die_info *die;
20683
7771576e 20684 if (attr_form_is_ref (attr))
348e048f 20685 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20686 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20687 die = follow_die_sig (src_die, attr, ref_cu);
20688 else
20689 {
20690 dump_die_for_error (src_die);
20691 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20692 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20693 }
20694
20695 return die;
03dd20cc
DJ
20696}
20697
5c631832 20698/* Follow reference OFFSET.
673bfd45
DE
20699 On entry *REF_CU is the CU of the source die referencing OFFSET.
20700 On exit *REF_CU is the CU of the result.
20701 Returns NULL if OFFSET is invalid. */
f504f079 20702
f9aca02d 20703static struct die_info *
9c541725 20704follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20705 struct dwarf2_cu **ref_cu)
c906108c 20706{
10b3939b 20707 struct die_info temp_die;
f2f0e013 20708 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20709
348e048f
DE
20710 gdb_assert (cu->per_cu != NULL);
20711
98bfdba5
PA
20712 target_cu = cu;
20713
3019eac3 20714 if (cu->per_cu->is_debug_types)
348e048f
DE
20715 {
20716 /* .debug_types CUs cannot reference anything outside their CU.
20717 If they need to, they have to reference a signatured type via
55f1336d 20718 DW_FORM_ref_sig8. */
9c541725 20719 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20720 return NULL;
348e048f 20721 }
36586728 20722 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20723 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20724 {
20725 struct dwarf2_per_cu_data *per_cu;
9a619af0 20726
9c541725 20727 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20728 cu->objfile);
03dd20cc
DJ
20729
20730 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20731 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20732 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20733
10b3939b
DJ
20734 target_cu = per_cu->cu;
20735 }
98bfdba5
PA
20736 else if (cu->dies == NULL)
20737 {
20738 /* We're loading full DIEs during partial symbol reading. */
20739 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20740 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20741 }
c906108c 20742
f2f0e013 20743 *ref_cu = target_cu;
9c541725 20744 temp_die.sect_off = sect_off;
9a3c8263 20745 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20746 &temp_die,
20747 to_underlying (sect_off));
5c631832 20748}
10b3939b 20749
5c631832
JK
20750/* Follow reference attribute ATTR of SRC_DIE.
20751 On entry *REF_CU is the CU of SRC_DIE.
20752 On exit *REF_CU is the CU of the result. */
20753
20754static struct die_info *
ff39bb5e 20755follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20756 struct dwarf2_cu **ref_cu)
20757{
9c541725 20758 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20759 struct dwarf2_cu *cu = *ref_cu;
20760 struct die_info *die;
20761
9c541725 20762 die = follow_die_offset (sect_off,
36586728
TT
20763 (attr->form == DW_FORM_GNU_ref_alt
20764 || cu->per_cu->is_dwz),
20765 ref_cu);
5c631832
JK
20766 if (!die)
20767 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20768 "at 0x%x [in module %s]"),
9c541725 20769 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20770 objfile_name (cu->objfile));
348e048f 20771
5c631832
JK
20772 return die;
20773}
20774
9c541725 20775/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20776 Returned value is intended for DW_OP_call*. Returned
20777 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20778
20779struct dwarf2_locexpr_baton
9c541725 20780dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20781 struct dwarf2_per_cu_data *per_cu,
20782 CORE_ADDR (*get_frame_pc) (void *baton),
20783 void *baton)
5c631832 20784{
918dd910 20785 struct dwarf2_cu *cu;
5c631832
JK
20786 struct die_info *die;
20787 struct attribute *attr;
20788 struct dwarf2_locexpr_baton retval;
20789
8cf6f0b1
TT
20790 dw2_setup (per_cu->objfile);
20791
918dd910
JK
20792 if (per_cu->cu == NULL)
20793 load_cu (per_cu);
20794 cu = per_cu->cu;
cc12ce38
DE
20795 if (cu == NULL)
20796 {
20797 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20798 Instead just throw an error, not much else we can do. */
20799 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20800 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20801 }
918dd910 20802
9c541725 20803 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20804 if (!die)
20805 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20806 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20807
20808 attr = dwarf2_attr (die, DW_AT_location, cu);
20809 if (!attr)
20810 {
e103e986
JK
20811 /* DWARF: "If there is no such attribute, then there is no effect.".
20812 DATA is ignored if SIZE is 0. */
5c631832 20813
e103e986 20814 retval.data = NULL;
5c631832
JK
20815 retval.size = 0;
20816 }
8cf6f0b1
TT
20817 else if (attr_form_is_section_offset (attr))
20818 {
20819 struct dwarf2_loclist_baton loclist_baton;
20820 CORE_ADDR pc = (*get_frame_pc) (baton);
20821 size_t size;
20822
20823 fill_in_loclist_baton (cu, &loclist_baton, attr);
20824
20825 retval.data = dwarf2_find_location_expression (&loclist_baton,
20826 &size, pc);
20827 retval.size = size;
20828 }
5c631832
JK
20829 else
20830 {
20831 if (!attr_form_is_block (attr))
20832 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20833 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20834 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20835
20836 retval.data = DW_BLOCK (attr)->data;
20837 retval.size = DW_BLOCK (attr)->size;
20838 }
20839 retval.per_cu = cu->per_cu;
918dd910 20840
918dd910
JK
20841 age_cached_comp_units ();
20842
5c631832 20843 return retval;
348e048f
DE
20844}
20845
8b9737bf
TT
20846/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20847 offset. */
20848
20849struct dwarf2_locexpr_baton
20850dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20851 struct dwarf2_per_cu_data *per_cu,
20852 CORE_ADDR (*get_frame_pc) (void *baton),
20853 void *baton)
20854{
9c541725 20855 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20856
9c541725 20857 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20858}
20859
b6807d98
TT
20860/* Write a constant of a given type as target-ordered bytes into
20861 OBSTACK. */
20862
20863static const gdb_byte *
20864write_constant_as_bytes (struct obstack *obstack,
20865 enum bfd_endian byte_order,
20866 struct type *type,
20867 ULONGEST value,
20868 LONGEST *len)
20869{
20870 gdb_byte *result;
20871
20872 *len = TYPE_LENGTH (type);
224c3ddb 20873 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20874 store_unsigned_integer (result, *len, byte_order, value);
20875
20876 return result;
20877}
20878
20879/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20880 pointer to the constant bytes and set LEN to the length of the
20881 data. If memory is needed, allocate it on OBSTACK. If the DIE
20882 does not have a DW_AT_const_value, return NULL. */
20883
20884const gdb_byte *
9c541725 20885dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20886 struct dwarf2_per_cu_data *per_cu,
20887 struct obstack *obstack,
20888 LONGEST *len)
20889{
20890 struct dwarf2_cu *cu;
20891 struct die_info *die;
20892 struct attribute *attr;
20893 const gdb_byte *result = NULL;
20894 struct type *type;
20895 LONGEST value;
20896 enum bfd_endian byte_order;
20897
20898 dw2_setup (per_cu->objfile);
20899
20900 if (per_cu->cu == NULL)
20901 load_cu (per_cu);
20902 cu = per_cu->cu;
cc12ce38
DE
20903 if (cu == NULL)
20904 {
20905 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20906 Instead just throw an error, not much else we can do. */
20907 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20908 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20909 }
b6807d98 20910
9c541725 20911 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20912 if (!die)
20913 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20914 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20915
20916
20917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20918 if (attr == NULL)
20919 return NULL;
20920
20921 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20922 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20923
20924 switch (attr->form)
20925 {
20926 case DW_FORM_addr:
20927 case DW_FORM_GNU_addr_index:
20928 {
20929 gdb_byte *tem;
20930
20931 *len = cu->header.addr_size;
224c3ddb 20932 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20933 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20934 result = tem;
20935 }
20936 break;
20937 case DW_FORM_string:
20938 case DW_FORM_strp:
20939 case DW_FORM_GNU_str_index:
20940 case DW_FORM_GNU_strp_alt:
20941 /* DW_STRING is already allocated on the objfile obstack, point
20942 directly to it. */
20943 result = (const gdb_byte *) DW_STRING (attr);
20944 *len = strlen (DW_STRING (attr));
20945 break;
20946 case DW_FORM_block1:
20947 case DW_FORM_block2:
20948 case DW_FORM_block4:
20949 case DW_FORM_block:
20950 case DW_FORM_exprloc:
0224619f 20951 case DW_FORM_data16:
b6807d98
TT
20952 result = DW_BLOCK (attr)->data;
20953 *len = DW_BLOCK (attr)->size;
20954 break;
20955
20956 /* The DW_AT_const_value attributes are supposed to carry the
20957 symbol's value "represented as it would be on the target
20958 architecture." By the time we get here, it's already been
20959 converted to host endianness, so we just need to sign- or
20960 zero-extend it as appropriate. */
20961 case DW_FORM_data1:
20962 type = die_type (die, cu);
20963 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20964 if (result == NULL)
20965 result = write_constant_as_bytes (obstack, byte_order,
20966 type, value, len);
20967 break;
20968 case DW_FORM_data2:
20969 type = die_type (die, cu);
20970 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20971 if (result == NULL)
20972 result = write_constant_as_bytes (obstack, byte_order,
20973 type, value, len);
20974 break;
20975 case DW_FORM_data4:
20976 type = die_type (die, cu);
20977 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20978 if (result == NULL)
20979 result = write_constant_as_bytes (obstack, byte_order,
20980 type, value, len);
20981 break;
20982 case DW_FORM_data8:
20983 type = die_type (die, cu);
20984 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20985 if (result == NULL)
20986 result = write_constant_as_bytes (obstack, byte_order,
20987 type, value, len);
20988 break;
20989
20990 case DW_FORM_sdata:
663c44ac 20991 case DW_FORM_implicit_const:
b6807d98
TT
20992 type = die_type (die, cu);
20993 result = write_constant_as_bytes (obstack, byte_order,
20994 type, DW_SND (attr), len);
20995 break;
20996
20997 case DW_FORM_udata:
20998 type = die_type (die, cu);
20999 result = write_constant_as_bytes (obstack, byte_order,
21000 type, DW_UNSND (attr), len);
21001 break;
21002
21003 default:
21004 complaint (&symfile_complaints,
21005 _("unsupported const value attribute form: '%s'"),
21006 dwarf_form_name (attr->form));
21007 break;
21008 }
21009
21010 return result;
21011}
21012
7942e96e
AA
21013/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21014 valid type for this die is found. */
21015
21016struct type *
9c541725 21017dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21018 struct dwarf2_per_cu_data *per_cu)
21019{
21020 struct dwarf2_cu *cu;
21021 struct die_info *die;
21022
21023 dw2_setup (per_cu->objfile);
21024
21025 if (per_cu->cu == NULL)
21026 load_cu (per_cu);
21027 cu = per_cu->cu;
21028 if (!cu)
21029 return NULL;
21030
9c541725 21031 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21032 if (!die)
21033 return NULL;
21034
21035 return die_type (die, cu);
21036}
21037
8a9b8146
TT
21038/* Return the type of the DIE at DIE_OFFSET in the CU named by
21039 PER_CU. */
21040
21041struct type *
b64f50a1 21042dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21043 struct dwarf2_per_cu_data *per_cu)
21044{
8a9b8146 21045 dw2_setup (per_cu->objfile);
b64f50a1 21046
9c541725 21047 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21048 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21049}
21050
ac9ec31b 21051/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21052 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21053 On exit *REF_CU is the CU of the result.
21054 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21055
21056static struct die_info *
ac9ec31b
DE
21057follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21058 struct dwarf2_cu **ref_cu)
348e048f 21059{
348e048f 21060 struct die_info temp_die;
348e048f
DE
21061 struct dwarf2_cu *sig_cu;
21062 struct die_info *die;
21063
ac9ec31b
DE
21064 /* While it might be nice to assert sig_type->type == NULL here,
21065 we can get here for DW_AT_imported_declaration where we need
21066 the DIE not the type. */
348e048f
DE
21067
21068 /* If necessary, add it to the queue and load its DIEs. */
21069
95554aad 21070 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21071 read_signatured_type (sig_type);
348e048f 21072
348e048f 21073 sig_cu = sig_type->per_cu.cu;
69d751e3 21074 gdb_assert (sig_cu != NULL);
9c541725
PA
21075 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21076 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21077 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21078 to_underlying (temp_die.sect_off));
348e048f
DE
21079 if (die)
21080 {
796a7ff8
DE
21081 /* For .gdb_index version 7 keep track of included TUs.
21082 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21083 if (dwarf2_per_objfile->index_table != NULL
21084 && dwarf2_per_objfile->index_table->version <= 7)
21085 {
21086 VEC_safe_push (dwarf2_per_cu_ptr,
21087 (*ref_cu)->per_cu->imported_symtabs,
21088 sig_cu->per_cu);
21089 }
21090
348e048f
DE
21091 *ref_cu = sig_cu;
21092 return die;
21093 }
21094
ac9ec31b
DE
21095 return NULL;
21096}
21097
21098/* Follow signatured type referenced by ATTR in SRC_DIE.
21099 On entry *REF_CU is the CU of SRC_DIE.
21100 On exit *REF_CU is the CU of the result.
21101 The result is the DIE of the type.
21102 If the referenced type cannot be found an error is thrown. */
21103
21104static struct die_info *
ff39bb5e 21105follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21106 struct dwarf2_cu **ref_cu)
21107{
21108 ULONGEST signature = DW_SIGNATURE (attr);
21109 struct signatured_type *sig_type;
21110 struct die_info *die;
21111
21112 gdb_assert (attr->form == DW_FORM_ref_sig8);
21113
a2ce51a0 21114 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21115 /* sig_type will be NULL if the signatured type is missing from
21116 the debug info. */
21117 if (sig_type == NULL)
21118 {
21119 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21120 " from DIE at 0x%x [in module %s]"),
9c541725 21121 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21122 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21123 }
21124
21125 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21126 if (die == NULL)
21127 {
21128 dump_die_for_error (src_die);
21129 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21130 " from DIE at 0x%x [in module %s]"),
9c541725 21131 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21132 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21133 }
21134
21135 return die;
21136}
21137
21138/* Get the type specified by SIGNATURE referenced in DIE/CU,
21139 reading in and processing the type unit if necessary. */
21140
21141static struct type *
21142get_signatured_type (struct die_info *die, ULONGEST signature,
21143 struct dwarf2_cu *cu)
21144{
21145 struct signatured_type *sig_type;
21146 struct dwarf2_cu *type_cu;
21147 struct die_info *type_die;
21148 struct type *type;
21149
a2ce51a0 21150 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21151 /* sig_type will be NULL if the signatured type is missing from
21152 the debug info. */
21153 if (sig_type == NULL)
21154 {
21155 complaint (&symfile_complaints,
21156 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21157 " from DIE at 0x%x [in module %s]"),
9c541725 21158 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21159 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21160 return build_error_marker_type (cu, die);
21161 }
21162
21163 /* If we already know the type we're done. */
21164 if (sig_type->type != NULL)
21165 return sig_type->type;
21166
21167 type_cu = cu;
21168 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21169 if (type_die != NULL)
21170 {
21171 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21172 is created. This is important, for example, because for c++ classes
21173 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21174 type = read_type_die (type_die, type_cu);
21175 if (type == NULL)
21176 {
21177 complaint (&symfile_complaints,
21178 _("Dwarf Error: Cannot build signatured type %s"
21179 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21180 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21181 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21182 type = build_error_marker_type (cu, die);
21183 }
21184 }
21185 else
21186 {
21187 complaint (&symfile_complaints,
21188 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21189 " from DIE at 0x%x [in module %s]"),
9c541725 21190 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21191 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21192 type = build_error_marker_type (cu, die);
21193 }
21194 sig_type->type = type;
21195
21196 return type;
21197}
21198
21199/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21200 reading in and processing the type unit if necessary. */
21201
21202static struct type *
ff39bb5e 21203get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21204 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21205{
21206 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21207 if (attr_form_is_ref (attr))
ac9ec31b
DE
21208 {
21209 struct dwarf2_cu *type_cu = cu;
21210 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21211
21212 return read_type_die (type_die, type_cu);
21213 }
21214 else if (attr->form == DW_FORM_ref_sig8)
21215 {
21216 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21217 }
21218 else
21219 {
21220 complaint (&symfile_complaints,
21221 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21222 " at 0x%x [in module %s]"),
9c541725 21223 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21224 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21225 return build_error_marker_type (cu, die);
21226 }
348e048f
DE
21227}
21228
e5fe5e75 21229/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21230
21231static void
e5fe5e75 21232load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21233{
52dc124a 21234 struct signatured_type *sig_type;
348e048f 21235
f4dc4d17
DE
21236 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21237 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21238
6721b2ec
DE
21239 /* We have the per_cu, but we need the signatured_type.
21240 Fortunately this is an easy translation. */
21241 gdb_assert (per_cu->is_debug_types);
21242 sig_type = (struct signatured_type *) per_cu;
348e048f 21243
6721b2ec 21244 gdb_assert (per_cu->cu == NULL);
348e048f 21245
52dc124a 21246 read_signatured_type (sig_type);
348e048f 21247
6721b2ec 21248 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21249}
21250
dee91e82
DE
21251/* die_reader_func for read_signatured_type.
21252 This is identical to load_full_comp_unit_reader,
21253 but is kept separate for now. */
348e048f
DE
21254
21255static void
dee91e82 21256read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21257 const gdb_byte *info_ptr,
dee91e82
DE
21258 struct die_info *comp_unit_die,
21259 int has_children,
21260 void *data)
348e048f 21261{
dee91e82 21262 struct dwarf2_cu *cu = reader->cu;
348e048f 21263
dee91e82
DE
21264 gdb_assert (cu->die_hash == NULL);
21265 cu->die_hash =
21266 htab_create_alloc_ex (cu->header.length / 12,
21267 die_hash,
21268 die_eq,
21269 NULL,
21270 &cu->comp_unit_obstack,
21271 hashtab_obstack_allocate,
21272 dummy_obstack_deallocate);
348e048f 21273
dee91e82
DE
21274 if (has_children)
21275 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21276 &info_ptr, comp_unit_die);
21277 cu->dies = comp_unit_die;
21278 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21279
21280 /* We try not to read any attributes in this function, because not
9cdd5dbd 21281 all CUs needed for references have been loaded yet, and symbol
348e048f 21282 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21283 or we won't be able to build types correctly.
21284 Similarly, if we do not read the producer, we can not apply
21285 producer-specific interpretation. */
95554aad 21286 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21287}
348e048f 21288
3019eac3
DE
21289/* Read in a signatured type and build its CU and DIEs.
21290 If the type is a stub for the real type in a DWO file,
21291 read in the real type from the DWO file as well. */
dee91e82
DE
21292
21293static void
21294read_signatured_type (struct signatured_type *sig_type)
21295{
21296 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21297
3019eac3 21298 gdb_assert (per_cu->is_debug_types);
dee91e82 21299 gdb_assert (per_cu->cu == NULL);
348e048f 21300
f4dc4d17
DE
21301 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21302 read_signatured_type_reader, NULL);
7ee85ab1 21303 sig_type->per_cu.tu_read = 1;
c906108c
SS
21304}
21305
c906108c
SS
21306/* Decode simple location descriptions.
21307 Given a pointer to a dwarf block that defines a location, compute
21308 the location and return the value.
21309
4cecd739
DJ
21310 NOTE drow/2003-11-18: This function is called in two situations
21311 now: for the address of static or global variables (partial symbols
21312 only) and for offsets into structures which are expected to be
21313 (more or less) constant. The partial symbol case should go away,
21314 and only the constant case should remain. That will let this
21315 function complain more accurately. A few special modes are allowed
21316 without complaint for global variables (for instance, global
21317 register values and thread-local values).
c906108c
SS
21318
21319 A location description containing no operations indicates that the
4cecd739 21320 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21321 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21322 callers will only want a very basic result and this can become a
21ae7a4d
JK
21323 complaint.
21324
21325 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21326
21327static CORE_ADDR
e7c27a73 21328decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21329{
e7c27a73 21330 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21331 size_t i;
21332 size_t size = blk->size;
d521ce57 21333 const gdb_byte *data = blk->data;
21ae7a4d
JK
21334 CORE_ADDR stack[64];
21335 int stacki;
21336 unsigned int bytes_read, unsnd;
21337 gdb_byte op;
c906108c 21338
21ae7a4d
JK
21339 i = 0;
21340 stacki = 0;
21341 stack[stacki] = 0;
21342 stack[++stacki] = 0;
21343
21344 while (i < size)
21345 {
21346 op = data[i++];
21347 switch (op)
21348 {
21349 case DW_OP_lit0:
21350 case DW_OP_lit1:
21351 case DW_OP_lit2:
21352 case DW_OP_lit3:
21353 case DW_OP_lit4:
21354 case DW_OP_lit5:
21355 case DW_OP_lit6:
21356 case DW_OP_lit7:
21357 case DW_OP_lit8:
21358 case DW_OP_lit9:
21359 case DW_OP_lit10:
21360 case DW_OP_lit11:
21361 case DW_OP_lit12:
21362 case DW_OP_lit13:
21363 case DW_OP_lit14:
21364 case DW_OP_lit15:
21365 case DW_OP_lit16:
21366 case DW_OP_lit17:
21367 case DW_OP_lit18:
21368 case DW_OP_lit19:
21369 case DW_OP_lit20:
21370 case DW_OP_lit21:
21371 case DW_OP_lit22:
21372 case DW_OP_lit23:
21373 case DW_OP_lit24:
21374 case DW_OP_lit25:
21375 case DW_OP_lit26:
21376 case DW_OP_lit27:
21377 case DW_OP_lit28:
21378 case DW_OP_lit29:
21379 case DW_OP_lit30:
21380 case DW_OP_lit31:
21381 stack[++stacki] = op - DW_OP_lit0;
21382 break;
f1bea926 21383
21ae7a4d
JK
21384 case DW_OP_reg0:
21385 case DW_OP_reg1:
21386 case DW_OP_reg2:
21387 case DW_OP_reg3:
21388 case DW_OP_reg4:
21389 case DW_OP_reg5:
21390 case DW_OP_reg6:
21391 case DW_OP_reg7:
21392 case DW_OP_reg8:
21393 case DW_OP_reg9:
21394 case DW_OP_reg10:
21395 case DW_OP_reg11:
21396 case DW_OP_reg12:
21397 case DW_OP_reg13:
21398 case DW_OP_reg14:
21399 case DW_OP_reg15:
21400 case DW_OP_reg16:
21401 case DW_OP_reg17:
21402 case DW_OP_reg18:
21403 case DW_OP_reg19:
21404 case DW_OP_reg20:
21405 case DW_OP_reg21:
21406 case DW_OP_reg22:
21407 case DW_OP_reg23:
21408 case DW_OP_reg24:
21409 case DW_OP_reg25:
21410 case DW_OP_reg26:
21411 case DW_OP_reg27:
21412 case DW_OP_reg28:
21413 case DW_OP_reg29:
21414 case DW_OP_reg30:
21415 case DW_OP_reg31:
21416 stack[++stacki] = op - DW_OP_reg0;
21417 if (i < size)
21418 dwarf2_complex_location_expr_complaint ();
21419 break;
c906108c 21420
21ae7a4d
JK
21421 case DW_OP_regx:
21422 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21423 i += bytes_read;
21424 stack[++stacki] = unsnd;
21425 if (i < size)
21426 dwarf2_complex_location_expr_complaint ();
21427 break;
c906108c 21428
21ae7a4d
JK
21429 case DW_OP_addr:
21430 stack[++stacki] = read_address (objfile->obfd, &data[i],
21431 cu, &bytes_read);
21432 i += bytes_read;
21433 break;
d53d4ac5 21434
21ae7a4d
JK
21435 case DW_OP_const1u:
21436 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21437 i += 1;
21438 break;
21439
21440 case DW_OP_const1s:
21441 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21442 i += 1;
21443 break;
21444
21445 case DW_OP_const2u:
21446 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21447 i += 2;
21448 break;
21449
21450 case DW_OP_const2s:
21451 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21452 i += 2;
21453 break;
d53d4ac5 21454
21ae7a4d
JK
21455 case DW_OP_const4u:
21456 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21457 i += 4;
21458 break;
21459
21460 case DW_OP_const4s:
21461 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21462 i += 4;
21463 break;
21464
585861ea
JK
21465 case DW_OP_const8u:
21466 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21467 i += 8;
21468 break;
21469
21ae7a4d
JK
21470 case DW_OP_constu:
21471 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21472 &bytes_read);
21473 i += bytes_read;
21474 break;
21475
21476 case DW_OP_consts:
21477 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21478 i += bytes_read;
21479 break;
21480
21481 case DW_OP_dup:
21482 stack[stacki + 1] = stack[stacki];
21483 stacki++;
21484 break;
21485
21486 case DW_OP_plus:
21487 stack[stacki - 1] += stack[stacki];
21488 stacki--;
21489 break;
21490
21491 case DW_OP_plus_uconst:
21492 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21493 &bytes_read);
21494 i += bytes_read;
21495 break;
21496
21497 case DW_OP_minus:
21498 stack[stacki - 1] -= stack[stacki];
21499 stacki--;
21500 break;
21501
21502 case DW_OP_deref:
21503 /* If we're not the last op, then we definitely can't encode
21504 this using GDB's address_class enum. This is valid for partial
21505 global symbols, although the variable's address will be bogus
21506 in the psymtab. */
21507 if (i < size)
21508 dwarf2_complex_location_expr_complaint ();
21509 break;
21510
21511 case DW_OP_GNU_push_tls_address:
4aa4e28b 21512 case DW_OP_form_tls_address:
21ae7a4d
JK
21513 /* The top of the stack has the offset from the beginning
21514 of the thread control block at which the variable is located. */
21515 /* Nothing should follow this operator, so the top of stack would
21516 be returned. */
21517 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21518 address will be bogus in the psymtab. Make it always at least
21519 non-zero to not look as a variable garbage collected by linker
21520 which have DW_OP_addr 0. */
21ae7a4d
JK
21521 if (i < size)
21522 dwarf2_complex_location_expr_complaint ();
585861ea 21523 stack[stacki]++;
21ae7a4d
JK
21524 break;
21525
21526 case DW_OP_GNU_uninit:
21527 break;
21528
3019eac3 21529 case DW_OP_GNU_addr_index:
49f6c839 21530 case DW_OP_GNU_const_index:
3019eac3
DE
21531 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21532 &bytes_read);
21533 i += bytes_read;
21534 break;
21535
21ae7a4d
JK
21536 default:
21537 {
f39c6ffd 21538 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21539
21540 if (name)
21541 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21542 name);
21543 else
21544 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21545 op);
21546 }
21547
21548 return (stack[stacki]);
d53d4ac5 21549 }
3c6e0cb3 21550
21ae7a4d
JK
21551 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21552 outside of the allocated space. Also enforce minimum>0. */
21553 if (stacki >= ARRAY_SIZE (stack) - 1)
21554 {
21555 complaint (&symfile_complaints,
21556 _("location description stack overflow"));
21557 return 0;
21558 }
21559
21560 if (stacki <= 0)
21561 {
21562 complaint (&symfile_complaints,
21563 _("location description stack underflow"));
21564 return 0;
21565 }
21566 }
21567 return (stack[stacki]);
c906108c
SS
21568}
21569
21570/* memory allocation interface */
21571
c906108c 21572static struct dwarf_block *
7b5a2f43 21573dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21574{
8d749320 21575 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21576}
21577
c906108c 21578static struct die_info *
b60c80d6 21579dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21580{
21581 struct die_info *die;
b60c80d6
DJ
21582 size_t size = sizeof (struct die_info);
21583
21584 if (num_attrs > 1)
21585 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21586
b60c80d6 21587 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21588 memset (die, 0, sizeof (struct die_info));
21589 return (die);
21590}
2e276125
JB
21591
21592\f
21593/* Macro support. */
21594
233d95b5
JK
21595/* Return file name relative to the compilation directory of file number I in
21596 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21597 responsible for freeing it. */
233d95b5 21598
2e276125 21599static char *
233d95b5 21600file_file_name (int file, struct line_header *lh)
2e276125 21601{
6a83a1e6
EZ
21602 /* Is the file number a valid index into the line header's file name
21603 table? Remember that file numbers start with one, not zero. */
fff8551c 21604 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21605 {
8c43009f 21606 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21607
8c43009f
PA
21608 if (!IS_ABSOLUTE_PATH (fe.name))
21609 {
21610 const char *dir = fe.include_dir (lh);
21611 if (dir != NULL)
21612 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21613 }
21614 return xstrdup (fe.name);
6a83a1e6 21615 }
2e276125
JB
21616 else
21617 {
6a83a1e6
EZ
21618 /* The compiler produced a bogus file number. We can at least
21619 record the macro definitions made in the file, even if we
21620 won't be able to find the file by name. */
21621 char fake_name[80];
9a619af0 21622
8c042590
PM
21623 xsnprintf (fake_name, sizeof (fake_name),
21624 "<bad macro file number %d>", file);
2e276125 21625
6e70227d 21626 complaint (&symfile_complaints,
6a83a1e6
EZ
21627 _("bad file number in macro information (%d)"),
21628 file);
2e276125 21629
6a83a1e6 21630 return xstrdup (fake_name);
2e276125
JB
21631 }
21632}
21633
233d95b5
JK
21634/* Return the full name of file number I in *LH's file name table.
21635 Use COMP_DIR as the name of the current directory of the
21636 compilation. The result is allocated using xmalloc; the caller is
21637 responsible for freeing it. */
21638static char *
21639file_full_name (int file, struct line_header *lh, const char *comp_dir)
21640{
21641 /* Is the file number a valid index into the line header's file name
21642 table? Remember that file numbers start with one, not zero. */
fff8551c 21643 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21644 {
21645 char *relative = file_file_name (file, lh);
21646
21647 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21648 return relative;
b36cec19
PA
21649 return reconcat (relative, comp_dir, SLASH_STRING,
21650 relative, (char *) NULL);
233d95b5
JK
21651 }
21652 else
21653 return file_file_name (file, lh);
21654}
21655
2e276125
JB
21656
21657static struct macro_source_file *
21658macro_start_file (int file, int line,
21659 struct macro_source_file *current_file,
43f3e411 21660 struct line_header *lh)
2e276125 21661{
233d95b5
JK
21662 /* File name relative to the compilation directory of this source file. */
21663 char *file_name = file_file_name (file, lh);
2e276125 21664
2e276125 21665 if (! current_file)
abc9d0dc 21666 {
fc474241
DE
21667 /* Note: We don't create a macro table for this compilation unit
21668 at all until we actually get a filename. */
43f3e411 21669 struct macro_table *macro_table = get_macro_table ();
fc474241 21670
abc9d0dc
TT
21671 /* If we have no current file, then this must be the start_file
21672 directive for the compilation unit's main source file. */
fc474241
DE
21673 current_file = macro_set_main (macro_table, file_name);
21674 macro_define_special (macro_table);
abc9d0dc 21675 }
2e276125 21676 else
233d95b5 21677 current_file = macro_include (current_file, line, file_name);
2e276125 21678
233d95b5 21679 xfree (file_name);
6e70227d 21680
2e276125
JB
21681 return current_file;
21682}
21683
21684
21685/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21686 followed by a null byte. */
21687static char *
21688copy_string (const char *buf, int len)
21689{
224c3ddb 21690 char *s = (char *) xmalloc (len + 1);
9a619af0 21691
2e276125
JB
21692 memcpy (s, buf, len);
21693 s[len] = '\0';
2e276125
JB
21694 return s;
21695}
21696
21697
21698static const char *
21699consume_improper_spaces (const char *p, const char *body)
21700{
21701 if (*p == ' ')
21702 {
4d3c2250 21703 complaint (&symfile_complaints,
3e43a32a
MS
21704 _("macro definition contains spaces "
21705 "in formal argument list:\n`%s'"),
4d3c2250 21706 body);
2e276125
JB
21707
21708 while (*p == ' ')
21709 p++;
21710 }
21711
21712 return p;
21713}
21714
21715
21716static void
21717parse_macro_definition (struct macro_source_file *file, int line,
21718 const char *body)
21719{
21720 const char *p;
21721
21722 /* The body string takes one of two forms. For object-like macro
21723 definitions, it should be:
21724
21725 <macro name> " " <definition>
21726
21727 For function-like macro definitions, it should be:
21728
21729 <macro name> "() " <definition>
21730 or
21731 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21732
21733 Spaces may appear only where explicitly indicated, and in the
21734 <definition>.
21735
21736 The Dwarf 2 spec says that an object-like macro's name is always
21737 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21738 the space when the macro's definition is the empty string.
2e276125
JB
21739
21740 The Dwarf 2 spec says that there should be no spaces between the
21741 formal arguments in a function-like macro's formal argument list,
21742 but versions of GCC around March 2002 include spaces after the
21743 commas. */
21744
21745
21746 /* Find the extent of the macro name. The macro name is terminated
21747 by either a space or null character (for an object-like macro) or
21748 an opening paren (for a function-like macro). */
21749 for (p = body; *p; p++)
21750 if (*p == ' ' || *p == '(')
21751 break;
21752
21753 if (*p == ' ' || *p == '\0')
21754 {
21755 /* It's an object-like macro. */
21756 int name_len = p - body;
21757 char *name = copy_string (body, name_len);
21758 const char *replacement;
21759
21760 if (*p == ' ')
21761 replacement = body + name_len + 1;
21762 else
21763 {
4d3c2250 21764 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21765 replacement = body + name_len;
21766 }
6e70227d 21767
2e276125
JB
21768 macro_define_object (file, line, name, replacement);
21769
21770 xfree (name);
21771 }
21772 else if (*p == '(')
21773 {
21774 /* It's a function-like macro. */
21775 char *name = copy_string (body, p - body);
21776 int argc = 0;
21777 int argv_size = 1;
8d749320 21778 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21779
21780 p++;
21781
21782 p = consume_improper_spaces (p, body);
21783
21784 /* Parse the formal argument list. */
21785 while (*p && *p != ')')
21786 {
21787 /* Find the extent of the current argument name. */
21788 const char *arg_start = p;
21789
21790 while (*p && *p != ',' && *p != ')' && *p != ' ')
21791 p++;
21792
21793 if (! *p || p == arg_start)
4d3c2250 21794 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21795 else
21796 {
21797 /* Make sure argv has room for the new argument. */
21798 if (argc >= argv_size)
21799 {
21800 argv_size *= 2;
224c3ddb 21801 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21802 }
21803
21804 argv[argc++] = copy_string (arg_start, p - arg_start);
21805 }
21806
21807 p = consume_improper_spaces (p, body);
21808
21809 /* Consume the comma, if present. */
21810 if (*p == ',')
21811 {
21812 p++;
21813
21814 p = consume_improper_spaces (p, body);
21815 }
21816 }
21817
21818 if (*p == ')')
21819 {
21820 p++;
21821
21822 if (*p == ' ')
21823 /* Perfectly formed definition, no complaints. */
21824 macro_define_function (file, line, name,
6e70227d 21825 argc, (const char **) argv,
2e276125
JB
21826 p + 1);
21827 else if (*p == '\0')
21828 {
21829 /* Complain, but do define it. */
4d3c2250 21830 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21831 macro_define_function (file, line, name,
6e70227d 21832 argc, (const char **) argv,
2e276125
JB
21833 p);
21834 }
21835 else
21836 /* Just complain. */
4d3c2250 21837 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21838 }
21839 else
21840 /* Just complain. */
4d3c2250 21841 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21842
21843 xfree (name);
21844 {
21845 int i;
21846
21847 for (i = 0; i < argc; i++)
21848 xfree (argv[i]);
21849 }
21850 xfree (argv);
21851 }
21852 else
4d3c2250 21853 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21854}
21855
cf2c3c16
TT
21856/* Skip some bytes from BYTES according to the form given in FORM.
21857 Returns the new pointer. */
2e276125 21858
d521ce57
TT
21859static const gdb_byte *
21860skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21861 enum dwarf_form form,
21862 unsigned int offset_size,
21863 struct dwarf2_section_info *section)
2e276125 21864{
cf2c3c16 21865 unsigned int bytes_read;
2e276125 21866
cf2c3c16 21867 switch (form)
2e276125 21868 {
cf2c3c16
TT
21869 case DW_FORM_data1:
21870 case DW_FORM_flag:
21871 ++bytes;
21872 break;
21873
21874 case DW_FORM_data2:
21875 bytes += 2;
21876 break;
21877
21878 case DW_FORM_data4:
21879 bytes += 4;
21880 break;
21881
21882 case DW_FORM_data8:
21883 bytes += 8;
21884 break;
21885
0224619f
JK
21886 case DW_FORM_data16:
21887 bytes += 16;
21888 break;
21889
cf2c3c16
TT
21890 case DW_FORM_string:
21891 read_direct_string (abfd, bytes, &bytes_read);
21892 bytes += bytes_read;
21893 break;
21894
21895 case DW_FORM_sec_offset:
21896 case DW_FORM_strp:
36586728 21897 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21898 bytes += offset_size;
21899 break;
21900
21901 case DW_FORM_block:
21902 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21903 bytes += bytes_read;
21904 break;
21905
21906 case DW_FORM_block1:
21907 bytes += 1 + read_1_byte (abfd, bytes);
21908 break;
21909 case DW_FORM_block2:
21910 bytes += 2 + read_2_bytes (abfd, bytes);
21911 break;
21912 case DW_FORM_block4:
21913 bytes += 4 + read_4_bytes (abfd, bytes);
21914 break;
21915
21916 case DW_FORM_sdata:
21917 case DW_FORM_udata:
3019eac3
DE
21918 case DW_FORM_GNU_addr_index:
21919 case DW_FORM_GNU_str_index:
d521ce57 21920 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21921 if (bytes == NULL)
21922 {
21923 dwarf2_section_buffer_overflow_complaint (section);
21924 return NULL;
21925 }
cf2c3c16
TT
21926 break;
21927
663c44ac
JK
21928 case DW_FORM_implicit_const:
21929 break;
21930
cf2c3c16
TT
21931 default:
21932 {
21933 complain:
21934 complaint (&symfile_complaints,
21935 _("invalid form 0x%x in `%s'"),
a32a8923 21936 form, get_section_name (section));
cf2c3c16
TT
21937 return NULL;
21938 }
2e276125
JB
21939 }
21940
cf2c3c16
TT
21941 return bytes;
21942}
757a13d0 21943
cf2c3c16
TT
21944/* A helper for dwarf_decode_macros that handles skipping an unknown
21945 opcode. Returns an updated pointer to the macro data buffer; or,
21946 on error, issues a complaint and returns NULL. */
757a13d0 21947
d521ce57 21948static const gdb_byte *
cf2c3c16 21949skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21950 const gdb_byte **opcode_definitions,
21951 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21952 bfd *abfd,
21953 unsigned int offset_size,
21954 struct dwarf2_section_info *section)
21955{
21956 unsigned int bytes_read, i;
21957 unsigned long arg;
d521ce57 21958 const gdb_byte *defn;
2e276125 21959
cf2c3c16 21960 if (opcode_definitions[opcode] == NULL)
2e276125 21961 {
cf2c3c16
TT
21962 complaint (&symfile_complaints,
21963 _("unrecognized DW_MACFINO opcode 0x%x"),
21964 opcode);
21965 return NULL;
21966 }
2e276125 21967
cf2c3c16
TT
21968 defn = opcode_definitions[opcode];
21969 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21970 defn += bytes_read;
2e276125 21971
cf2c3c16
TT
21972 for (i = 0; i < arg; ++i)
21973 {
aead7601
SM
21974 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21975 (enum dwarf_form) defn[i], offset_size,
f664829e 21976 section);
cf2c3c16
TT
21977 if (mac_ptr == NULL)
21978 {
21979 /* skip_form_bytes already issued the complaint. */
21980 return NULL;
21981 }
21982 }
757a13d0 21983
cf2c3c16
TT
21984 return mac_ptr;
21985}
757a13d0 21986
cf2c3c16
TT
21987/* A helper function which parses the header of a macro section.
21988 If the macro section is the extended (for now called "GNU") type,
21989 then this updates *OFFSET_SIZE. Returns a pointer to just after
21990 the header, or issues a complaint and returns NULL on error. */
757a13d0 21991
d521ce57
TT
21992static const gdb_byte *
21993dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21994 bfd *abfd,
d521ce57 21995 const gdb_byte *mac_ptr,
cf2c3c16
TT
21996 unsigned int *offset_size,
21997 int section_is_gnu)
21998{
21999 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22000
cf2c3c16
TT
22001 if (section_is_gnu)
22002 {
22003 unsigned int version, flags;
757a13d0 22004
cf2c3c16 22005 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22006 if (version != 4 && version != 5)
cf2c3c16
TT
22007 {
22008 complaint (&symfile_complaints,
22009 _("unrecognized version `%d' in .debug_macro section"),
22010 version);
22011 return NULL;
22012 }
22013 mac_ptr += 2;
757a13d0 22014
cf2c3c16
TT
22015 flags = read_1_byte (abfd, mac_ptr);
22016 ++mac_ptr;
22017 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22018
cf2c3c16
TT
22019 if ((flags & 2) != 0)
22020 /* We don't need the line table offset. */
22021 mac_ptr += *offset_size;
757a13d0 22022
cf2c3c16
TT
22023 /* Vendor opcode descriptions. */
22024 if ((flags & 4) != 0)
22025 {
22026 unsigned int i, count;
757a13d0 22027
cf2c3c16
TT
22028 count = read_1_byte (abfd, mac_ptr);
22029 ++mac_ptr;
22030 for (i = 0; i < count; ++i)
22031 {
22032 unsigned int opcode, bytes_read;
22033 unsigned long arg;
22034
22035 opcode = read_1_byte (abfd, mac_ptr);
22036 ++mac_ptr;
22037 opcode_definitions[opcode] = mac_ptr;
22038 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22039 mac_ptr += bytes_read;
22040 mac_ptr += arg;
22041 }
757a13d0 22042 }
cf2c3c16 22043 }
757a13d0 22044
cf2c3c16
TT
22045 return mac_ptr;
22046}
757a13d0 22047
cf2c3c16 22048/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22049 including DW_MACRO_import. */
cf2c3c16
TT
22050
22051static void
d521ce57
TT
22052dwarf_decode_macro_bytes (bfd *abfd,
22053 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22054 struct macro_source_file *current_file,
43f3e411 22055 struct line_header *lh,
cf2c3c16 22056 struct dwarf2_section_info *section,
36586728 22057 int section_is_gnu, int section_is_dwz,
cf2c3c16 22058 unsigned int offset_size,
8fc3fc34 22059 htab_t include_hash)
cf2c3c16 22060{
4d663531 22061 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22062 enum dwarf_macro_record_type macinfo_type;
22063 int at_commandline;
d521ce57 22064 const gdb_byte *opcode_definitions[256];
757a13d0 22065
cf2c3c16
TT
22066 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22067 &offset_size, section_is_gnu);
22068 if (mac_ptr == NULL)
22069 {
22070 /* We already issued a complaint. */
22071 return;
22072 }
757a13d0
JK
22073
22074 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22075 GDB is still reading the definitions from command line. First
22076 DW_MACINFO_start_file will need to be ignored as it was already executed
22077 to create CURRENT_FILE for the main source holding also the command line
22078 definitions. On first met DW_MACINFO_start_file this flag is reset to
22079 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22080
22081 at_commandline = 1;
22082
22083 do
22084 {
22085 /* Do we at least have room for a macinfo type byte? */
22086 if (mac_ptr >= mac_end)
22087 {
f664829e 22088 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22089 break;
22090 }
22091
aead7601 22092 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22093 mac_ptr++;
22094
cf2c3c16
TT
22095 /* Note that we rely on the fact that the corresponding GNU and
22096 DWARF constants are the same. */
757a13d0
JK
22097 switch (macinfo_type)
22098 {
22099 /* A zero macinfo type indicates the end of the macro
22100 information. */
22101 case 0:
22102 break;
2e276125 22103
0af92d60
JK
22104 case DW_MACRO_define:
22105 case DW_MACRO_undef:
22106 case DW_MACRO_define_strp:
22107 case DW_MACRO_undef_strp:
22108 case DW_MACRO_define_sup:
22109 case DW_MACRO_undef_sup:
2e276125 22110 {
891d2f0b 22111 unsigned int bytes_read;
2e276125 22112 int line;
d521ce57 22113 const char *body;
cf2c3c16 22114 int is_define;
2e276125 22115
cf2c3c16
TT
22116 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22117 mac_ptr += bytes_read;
22118
0af92d60
JK
22119 if (macinfo_type == DW_MACRO_define
22120 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22121 {
22122 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22123 mac_ptr += bytes_read;
22124 }
22125 else
22126 {
22127 LONGEST str_offset;
22128
22129 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22130 mac_ptr += offset_size;
2e276125 22131
0af92d60
JK
22132 if (macinfo_type == DW_MACRO_define_sup
22133 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22134 || section_is_dwz)
36586728
TT
22135 {
22136 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22137
22138 body = read_indirect_string_from_dwz (dwz, str_offset);
22139 }
22140 else
22141 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22142 }
22143
0af92d60
JK
22144 is_define = (macinfo_type == DW_MACRO_define
22145 || macinfo_type == DW_MACRO_define_strp
22146 || macinfo_type == DW_MACRO_define_sup);
2e276125 22147 if (! current_file)
757a13d0
JK
22148 {
22149 /* DWARF violation as no main source is present. */
22150 complaint (&symfile_complaints,
22151 _("debug info with no main source gives macro %s "
22152 "on line %d: %s"),
cf2c3c16
TT
22153 is_define ? _("definition") : _("undefinition"),
22154 line, body);
757a13d0
JK
22155 break;
22156 }
3e43a32a
MS
22157 if ((line == 0 && !at_commandline)
22158 || (line != 0 && at_commandline))
4d3c2250 22159 complaint (&symfile_complaints,
757a13d0
JK
22160 _("debug info gives %s macro %s with %s line %d: %s"),
22161 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22162 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22163 line == 0 ? _("zero") : _("non-zero"), line, body);
22164
cf2c3c16 22165 if (is_define)
757a13d0 22166 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22167 else
22168 {
0af92d60
JK
22169 gdb_assert (macinfo_type == DW_MACRO_undef
22170 || macinfo_type == DW_MACRO_undef_strp
22171 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22172 macro_undef (current_file, line, body);
22173 }
2e276125
JB
22174 }
22175 break;
22176
0af92d60 22177 case DW_MACRO_start_file:
2e276125 22178 {
891d2f0b 22179 unsigned int bytes_read;
2e276125
JB
22180 int line, file;
22181
22182 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22183 mac_ptr += bytes_read;
22184 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22185 mac_ptr += bytes_read;
22186
3e43a32a
MS
22187 if ((line == 0 && !at_commandline)
22188 || (line != 0 && at_commandline))
757a13d0
JK
22189 complaint (&symfile_complaints,
22190 _("debug info gives source %d included "
22191 "from %s at %s line %d"),
22192 file, at_commandline ? _("command-line") : _("file"),
22193 line == 0 ? _("zero") : _("non-zero"), line);
22194
22195 if (at_commandline)
22196 {
0af92d60 22197 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22198 pass one. */
757a13d0
JK
22199 at_commandline = 0;
22200 }
22201 else
43f3e411 22202 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22203 }
22204 break;
22205
0af92d60 22206 case DW_MACRO_end_file:
2e276125 22207 if (! current_file)
4d3c2250 22208 complaint (&symfile_complaints,
3e43a32a
MS
22209 _("macro debug info has an unmatched "
22210 "`close_file' directive"));
2e276125
JB
22211 else
22212 {
22213 current_file = current_file->included_by;
22214 if (! current_file)
22215 {
cf2c3c16 22216 enum dwarf_macro_record_type next_type;
2e276125
JB
22217
22218 /* GCC circa March 2002 doesn't produce the zero
22219 type byte marking the end of the compilation
22220 unit. Complain if it's not there, but exit no
22221 matter what. */
22222
22223 /* Do we at least have room for a macinfo type byte? */
22224 if (mac_ptr >= mac_end)
22225 {
f664829e 22226 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22227 return;
22228 }
22229
22230 /* We don't increment mac_ptr here, so this is just
22231 a look-ahead. */
aead7601
SM
22232 next_type
22233 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22234 mac_ptr);
2e276125 22235 if (next_type != 0)
4d3c2250 22236 complaint (&symfile_complaints,
3e43a32a
MS
22237 _("no terminating 0-type entry for "
22238 "macros in `.debug_macinfo' section"));
2e276125
JB
22239
22240 return;
22241 }
22242 }
22243 break;
22244
0af92d60
JK
22245 case DW_MACRO_import:
22246 case DW_MACRO_import_sup:
cf2c3c16
TT
22247 {
22248 LONGEST offset;
8fc3fc34 22249 void **slot;
a036ba48
TT
22250 bfd *include_bfd = abfd;
22251 struct dwarf2_section_info *include_section = section;
d521ce57 22252 const gdb_byte *include_mac_end = mac_end;
a036ba48 22253 int is_dwz = section_is_dwz;
d521ce57 22254 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22255
22256 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22257 mac_ptr += offset_size;
22258
0af92d60 22259 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22260 {
22261 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22262
4d663531 22263 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22264
a036ba48 22265 include_section = &dwz->macro;
a32a8923 22266 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22267 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22268 is_dwz = 1;
22269 }
22270
22271 new_mac_ptr = include_section->buffer + offset;
22272 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22273
8fc3fc34
TT
22274 if (*slot != NULL)
22275 {
22276 /* This has actually happened; see
22277 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22278 complaint (&symfile_complaints,
0af92d60 22279 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22280 ".debug_macro section"));
22281 }
22282 else
22283 {
d521ce57 22284 *slot = (void *) new_mac_ptr;
36586728 22285
a036ba48 22286 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22287 include_mac_end, current_file, lh,
36586728 22288 section, section_is_gnu, is_dwz,
4d663531 22289 offset_size, include_hash);
8fc3fc34 22290
d521ce57 22291 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22292 }
cf2c3c16
TT
22293 }
22294 break;
22295
2e276125 22296 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22297 if (!section_is_gnu)
22298 {
22299 unsigned int bytes_read;
2e276125 22300
ac298888
TT
22301 /* This reads the constant, but since we don't recognize
22302 any vendor extensions, we ignore it. */
22303 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22304 mac_ptr += bytes_read;
22305 read_direct_string (abfd, mac_ptr, &bytes_read);
22306 mac_ptr += bytes_read;
2e276125 22307
cf2c3c16
TT
22308 /* We don't recognize any vendor extensions. */
22309 break;
22310 }
22311 /* FALLTHROUGH */
22312
22313 default:
22314 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22315 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22316 section);
22317 if (mac_ptr == NULL)
22318 return;
22319 break;
2e276125 22320 }
757a13d0 22321 } while (macinfo_type != 0);
2e276125 22322}
8e19ed76 22323
cf2c3c16 22324static void
09262596 22325dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22326 int section_is_gnu)
cf2c3c16 22327{
bb5ed363 22328 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22329 struct line_header *lh = cu->line_header;
22330 bfd *abfd;
d521ce57 22331 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22332 struct macro_source_file *current_file = 0;
22333 enum dwarf_macro_record_type macinfo_type;
22334 unsigned int offset_size = cu->header.offset_size;
d521ce57 22335 const gdb_byte *opcode_definitions[256];
8fc3fc34 22336 struct cleanup *cleanup;
8fc3fc34 22337 void **slot;
09262596
DE
22338 struct dwarf2_section_info *section;
22339 const char *section_name;
22340
22341 if (cu->dwo_unit != NULL)
22342 {
22343 if (section_is_gnu)
22344 {
22345 section = &cu->dwo_unit->dwo_file->sections.macro;
22346 section_name = ".debug_macro.dwo";
22347 }
22348 else
22349 {
22350 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22351 section_name = ".debug_macinfo.dwo";
22352 }
22353 }
22354 else
22355 {
22356 if (section_is_gnu)
22357 {
22358 section = &dwarf2_per_objfile->macro;
22359 section_name = ".debug_macro";
22360 }
22361 else
22362 {
22363 section = &dwarf2_per_objfile->macinfo;
22364 section_name = ".debug_macinfo";
22365 }
22366 }
cf2c3c16 22367
bb5ed363 22368 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22369 if (section->buffer == NULL)
22370 {
fceca515 22371 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22372 return;
22373 }
a32a8923 22374 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22375
22376 /* First pass: Find the name of the base filename.
22377 This filename is needed in order to process all macros whose definition
22378 (or undefinition) comes from the command line. These macros are defined
22379 before the first DW_MACINFO_start_file entry, and yet still need to be
22380 associated to the base file.
22381
22382 To determine the base file name, we scan the macro definitions until we
22383 reach the first DW_MACINFO_start_file entry. We then initialize
22384 CURRENT_FILE accordingly so that any macro definition found before the
22385 first DW_MACINFO_start_file can still be associated to the base file. */
22386
22387 mac_ptr = section->buffer + offset;
22388 mac_end = section->buffer + section->size;
22389
22390 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22391 &offset_size, section_is_gnu);
22392 if (mac_ptr == NULL)
22393 {
22394 /* We already issued a complaint. */
22395 return;
22396 }
22397
22398 do
22399 {
22400 /* Do we at least have room for a macinfo type byte? */
22401 if (mac_ptr >= mac_end)
22402 {
22403 /* Complaint is printed during the second pass as GDB will probably
22404 stop the first pass earlier upon finding
22405 DW_MACINFO_start_file. */
22406 break;
22407 }
22408
aead7601 22409 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22410 mac_ptr++;
22411
22412 /* Note that we rely on the fact that the corresponding GNU and
22413 DWARF constants are the same. */
22414 switch (macinfo_type)
22415 {
22416 /* A zero macinfo type indicates the end of the macro
22417 information. */
22418 case 0:
22419 break;
22420
0af92d60
JK
22421 case DW_MACRO_define:
22422 case DW_MACRO_undef:
cf2c3c16
TT
22423 /* Only skip the data by MAC_PTR. */
22424 {
22425 unsigned int bytes_read;
22426
22427 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22428 mac_ptr += bytes_read;
22429 read_direct_string (abfd, mac_ptr, &bytes_read);
22430 mac_ptr += bytes_read;
22431 }
22432 break;
22433
0af92d60 22434 case DW_MACRO_start_file:
cf2c3c16
TT
22435 {
22436 unsigned int bytes_read;
22437 int line, file;
22438
22439 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22440 mac_ptr += bytes_read;
22441 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22442 mac_ptr += bytes_read;
22443
43f3e411 22444 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22445 }
22446 break;
22447
0af92d60 22448 case DW_MACRO_end_file:
cf2c3c16
TT
22449 /* No data to skip by MAC_PTR. */
22450 break;
22451
0af92d60
JK
22452 case DW_MACRO_define_strp:
22453 case DW_MACRO_undef_strp:
22454 case DW_MACRO_define_sup:
22455 case DW_MACRO_undef_sup:
cf2c3c16
TT
22456 {
22457 unsigned int bytes_read;
22458
22459 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22460 mac_ptr += bytes_read;
22461 mac_ptr += offset_size;
22462 }
22463 break;
22464
0af92d60
JK
22465 case DW_MACRO_import:
22466 case DW_MACRO_import_sup:
cf2c3c16 22467 /* Note that, according to the spec, a transparent include
0af92d60 22468 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22469 skip this opcode. */
22470 mac_ptr += offset_size;
22471 break;
22472
22473 case DW_MACINFO_vendor_ext:
22474 /* Only skip the data by MAC_PTR. */
22475 if (!section_is_gnu)
22476 {
22477 unsigned int bytes_read;
22478
22479 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22480 mac_ptr += bytes_read;
22481 read_direct_string (abfd, mac_ptr, &bytes_read);
22482 mac_ptr += bytes_read;
22483 }
22484 /* FALLTHROUGH */
22485
22486 default:
22487 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22488 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22489 section);
22490 if (mac_ptr == NULL)
22491 return;
22492 break;
22493 }
22494 } while (macinfo_type != 0 && current_file == NULL);
22495
22496 /* Second pass: Process all entries.
22497
22498 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22499 command-line macro definitions/undefinitions. This flag is unset when we
22500 reach the first DW_MACINFO_start_file entry. */
22501
fc4007c9
TT
22502 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22503 htab_eq_pointer,
22504 NULL, xcalloc, xfree));
8fc3fc34 22505 mac_ptr = section->buffer + offset;
fc4007c9 22506 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22507 *slot = (void *) mac_ptr;
8fc3fc34 22508 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22509 current_file, lh, section,
fc4007c9
TT
22510 section_is_gnu, 0, offset_size,
22511 include_hash.get ());
cf2c3c16
TT
22512}
22513
8e19ed76 22514/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22515 if so return true else false. */
380bca97 22516
8e19ed76 22517static int
6e5a29e1 22518attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22519{
22520 return (attr == NULL ? 0 :
22521 attr->form == DW_FORM_block1
22522 || attr->form == DW_FORM_block2
22523 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22524 || attr->form == DW_FORM_block
22525 || attr->form == DW_FORM_exprloc);
8e19ed76 22526}
4c2df51b 22527
c6a0999f
JB
22528/* Return non-zero if ATTR's value is a section offset --- classes
22529 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22530 You may use DW_UNSND (attr) to retrieve such offsets.
22531
22532 Section 7.5.4, "Attribute Encodings", explains that no attribute
22533 may have a value that belongs to more than one of these classes; it
22534 would be ambiguous if we did, because we use the same forms for all
22535 of them. */
380bca97 22536
3690dd37 22537static int
6e5a29e1 22538attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22539{
22540 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22541 || attr->form == DW_FORM_data8
22542 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22543}
22544
3690dd37
JB
22545/* Return non-zero if ATTR's value falls in the 'constant' class, or
22546 zero otherwise. When this function returns true, you can apply
22547 dwarf2_get_attr_constant_value to it.
22548
22549 However, note that for some attributes you must check
22550 attr_form_is_section_offset before using this test. DW_FORM_data4
22551 and DW_FORM_data8 are members of both the constant class, and of
22552 the classes that contain offsets into other debug sections
22553 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22554 that, if an attribute's can be either a constant or one of the
22555 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22556 taken as section offsets, not constants.
22557
22558 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22559 cannot handle that. */
380bca97 22560
3690dd37 22561static int
6e5a29e1 22562attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22563{
22564 switch (attr->form)
22565 {
22566 case DW_FORM_sdata:
22567 case DW_FORM_udata:
22568 case DW_FORM_data1:
22569 case DW_FORM_data2:
22570 case DW_FORM_data4:
22571 case DW_FORM_data8:
663c44ac 22572 case DW_FORM_implicit_const:
3690dd37
JB
22573 return 1;
22574 default:
22575 return 0;
22576 }
22577}
22578
7771576e
SA
22579
22580/* DW_ADDR is always stored already as sect_offset; despite for the forms
22581 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22582
22583static int
6e5a29e1 22584attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22585{
22586 switch (attr->form)
22587 {
22588 case DW_FORM_ref_addr:
22589 case DW_FORM_ref1:
22590 case DW_FORM_ref2:
22591 case DW_FORM_ref4:
22592 case DW_FORM_ref8:
22593 case DW_FORM_ref_udata:
22594 case DW_FORM_GNU_ref_alt:
22595 return 1;
22596 default:
22597 return 0;
22598 }
22599}
22600
3019eac3
DE
22601/* Return the .debug_loc section to use for CU.
22602 For DWO files use .debug_loc.dwo. */
22603
22604static struct dwarf2_section_info *
22605cu_debug_loc_section (struct dwarf2_cu *cu)
22606{
22607 if (cu->dwo_unit)
43988095
JK
22608 {
22609 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22610
22611 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22612 }
22613 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22614 : &dwarf2_per_objfile->loc);
3019eac3
DE
22615}
22616
8cf6f0b1
TT
22617/* A helper function that fills in a dwarf2_loclist_baton. */
22618
22619static void
22620fill_in_loclist_baton (struct dwarf2_cu *cu,
22621 struct dwarf2_loclist_baton *baton,
ff39bb5e 22622 const struct attribute *attr)
8cf6f0b1 22623{
3019eac3
DE
22624 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22625
22626 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22627
22628 baton->per_cu = cu->per_cu;
22629 gdb_assert (baton->per_cu);
22630 /* We don't know how long the location list is, but make sure we
22631 don't run off the edge of the section. */
3019eac3
DE
22632 baton->size = section->size - DW_UNSND (attr);
22633 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22634 baton->base_address = cu->base_address;
f664829e 22635 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22636}
22637
4c2df51b 22638static void
ff39bb5e 22639dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22640 struct dwarf2_cu *cu, int is_block)
4c2df51b 22641{
bb5ed363 22642 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22643 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22644
3690dd37 22645 if (attr_form_is_section_offset (attr)
3019eac3 22646 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22647 the section. If so, fall through to the complaint in the
22648 other branch. */
3019eac3 22649 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22650 {
0d53c4c4 22651 struct dwarf2_loclist_baton *baton;
4c2df51b 22652
8d749320 22653 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22654
8cf6f0b1 22655 fill_in_loclist_baton (cu, baton, attr);
be391dca 22656
d00adf39 22657 if (cu->base_known == 0)
0d53c4c4 22658 complaint (&symfile_complaints,
3e43a32a
MS
22659 _("Location list used without "
22660 "specifying the CU base address."));
4c2df51b 22661
f1e6e072
TT
22662 SYMBOL_ACLASS_INDEX (sym) = (is_block
22663 ? dwarf2_loclist_block_index
22664 : dwarf2_loclist_index);
0d53c4c4
DJ
22665 SYMBOL_LOCATION_BATON (sym) = baton;
22666 }
22667 else
22668 {
22669 struct dwarf2_locexpr_baton *baton;
22670
8d749320 22671 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22672 baton->per_cu = cu->per_cu;
22673 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22674
22675 if (attr_form_is_block (attr))
22676 {
22677 /* Note that we're just copying the block's data pointer
22678 here, not the actual data. We're still pointing into the
6502dd73
DJ
22679 info_buffer for SYM's objfile; right now we never release
22680 that buffer, but when we do clean up properly this may
22681 need to change. */
0d53c4c4
DJ
22682 baton->size = DW_BLOCK (attr)->size;
22683 baton->data = DW_BLOCK (attr)->data;
22684 }
22685 else
22686 {
22687 dwarf2_invalid_attrib_class_complaint ("location description",
22688 SYMBOL_NATURAL_NAME (sym));
22689 baton->size = 0;
0d53c4c4 22690 }
6e70227d 22691
f1e6e072
TT
22692 SYMBOL_ACLASS_INDEX (sym) = (is_block
22693 ? dwarf2_locexpr_block_index
22694 : dwarf2_locexpr_index);
0d53c4c4
DJ
22695 SYMBOL_LOCATION_BATON (sym) = baton;
22696 }
4c2df51b 22697}
6502dd73 22698
9aa1f1e3
TT
22699/* Return the OBJFILE associated with the compilation unit CU. If CU
22700 came from a separate debuginfo file, then the master objfile is
22701 returned. */
ae0d2f24
UW
22702
22703struct objfile *
22704dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22705{
9291a0cd 22706 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22707
22708 /* Return the master objfile, so that we can report and look up the
22709 correct file containing this variable. */
22710 if (objfile->separate_debug_objfile_backlink)
22711 objfile = objfile->separate_debug_objfile_backlink;
22712
22713 return objfile;
22714}
22715
96408a79
SA
22716/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22717 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22718 CU_HEADERP first. */
22719
22720static const struct comp_unit_head *
22721per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22722 struct dwarf2_per_cu_data *per_cu)
22723{
d521ce57 22724 const gdb_byte *info_ptr;
96408a79
SA
22725
22726 if (per_cu->cu)
22727 return &per_cu->cu->header;
22728
9c541725 22729 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22730
22731 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22732 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22733 rcuh_kind::COMPILE);
96408a79
SA
22734
22735 return cu_headerp;
22736}
22737
ae0d2f24
UW
22738/* Return the address size given in the compilation unit header for CU. */
22739
98714339 22740int
ae0d2f24
UW
22741dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22742{
96408a79
SA
22743 struct comp_unit_head cu_header_local;
22744 const struct comp_unit_head *cu_headerp;
c471e790 22745
96408a79
SA
22746 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22747
22748 return cu_headerp->addr_size;
ae0d2f24
UW
22749}
22750
9eae7c52
TT
22751/* Return the offset size given in the compilation unit header for CU. */
22752
22753int
22754dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22755{
96408a79
SA
22756 struct comp_unit_head cu_header_local;
22757 const struct comp_unit_head *cu_headerp;
9c6c53f7 22758
96408a79
SA
22759 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22760
22761 return cu_headerp->offset_size;
22762}
22763
22764/* See its dwarf2loc.h declaration. */
22765
22766int
22767dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22768{
22769 struct comp_unit_head cu_header_local;
22770 const struct comp_unit_head *cu_headerp;
22771
22772 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22773
22774 if (cu_headerp->version == 2)
22775 return cu_headerp->addr_size;
22776 else
22777 return cu_headerp->offset_size;
181cebd4
JK
22778}
22779
9aa1f1e3
TT
22780/* Return the text offset of the CU. The returned offset comes from
22781 this CU's objfile. If this objfile came from a separate debuginfo
22782 file, then the offset may be different from the corresponding
22783 offset in the parent objfile. */
22784
22785CORE_ADDR
22786dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22787{
bb3fa9d0 22788 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22789
22790 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22791}
22792
43988095
JK
22793/* Return DWARF version number of PER_CU. */
22794
22795short
22796dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22797{
22798 return per_cu->dwarf_version;
22799}
22800
348e048f
DE
22801/* Locate the .debug_info compilation unit from CU's objfile which contains
22802 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22803
22804static struct dwarf2_per_cu_data *
9c541725 22805dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22806 unsigned int offset_in_dwz,
ae038cb0
DJ
22807 struct objfile *objfile)
22808{
22809 struct dwarf2_per_cu_data *this_cu;
22810 int low, high;
36586728 22811 const sect_offset *cu_off;
ae038cb0 22812
ae038cb0
DJ
22813 low = 0;
22814 high = dwarf2_per_objfile->n_comp_units - 1;
22815 while (high > low)
22816 {
36586728 22817 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22818 int mid = low + (high - low) / 2;
9a619af0 22819
36586728 22820 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22821 cu_off = &mid_cu->sect_off;
36586728 22822 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22823 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22824 high = mid;
22825 else
22826 low = mid + 1;
22827 }
22828 gdb_assert (low == high);
36586728 22829 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22830 cu_off = &this_cu->sect_off;
22831 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22832 {
36586728 22833 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22834 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22835 "offset 0x%x [in module %s]"),
22836 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22837
9c541725
PA
22838 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22839 <= sect_off);
ae038cb0
DJ
22840 return dwarf2_per_objfile->all_comp_units[low-1];
22841 }
22842 else
22843 {
22844 this_cu = dwarf2_per_objfile->all_comp_units[low];
22845 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22846 && sect_off >= this_cu->sect_off + this_cu->length)
22847 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22848 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22849 return this_cu;
22850 }
22851}
22852
23745b47 22853/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22854
9816fde3 22855static void
23745b47 22856init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22857{
9816fde3 22858 memset (cu, 0, sizeof (*cu));
23745b47
DE
22859 per_cu->cu = cu;
22860 cu->per_cu = per_cu;
22861 cu->objfile = per_cu->objfile;
93311388 22862 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22863}
22864
22865/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22866
22867static void
95554aad
TT
22868prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22869 enum language pretend_language)
9816fde3
JK
22870{
22871 struct attribute *attr;
22872
22873 /* Set the language we're debugging. */
22874 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22875 if (attr)
22876 set_cu_language (DW_UNSND (attr), cu);
22877 else
9cded63f 22878 {
95554aad 22879 cu->language = pretend_language;
9cded63f
TT
22880 cu->language_defn = language_def (cu->language);
22881 }
dee91e82 22882
7d45c7c3 22883 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22884}
22885
ae038cb0
DJ
22886/* Release one cached compilation unit, CU. We unlink it from the tree
22887 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22888 the caller is responsible for that.
22889 NOTE: DATA is a void * because this function is also used as a
22890 cleanup routine. */
ae038cb0
DJ
22891
22892static void
68dc6402 22893free_heap_comp_unit (void *data)
ae038cb0 22894{
9a3c8263 22895 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22896
23745b47
DE
22897 gdb_assert (cu->per_cu != NULL);
22898 cu->per_cu->cu = NULL;
ae038cb0
DJ
22899 cu->per_cu = NULL;
22900
22901 obstack_free (&cu->comp_unit_obstack, NULL);
22902
22903 xfree (cu);
22904}
22905
72bf9492 22906/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22907 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22908 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22909
22910static void
22911free_stack_comp_unit (void *data)
22912{
9a3c8263 22913 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22914
23745b47
DE
22915 gdb_assert (cu->per_cu != NULL);
22916 cu->per_cu->cu = NULL;
22917 cu->per_cu = NULL;
22918
72bf9492
DJ
22919 obstack_free (&cu->comp_unit_obstack, NULL);
22920 cu->partial_dies = NULL;
ae038cb0
DJ
22921}
22922
22923/* Free all cached compilation units. */
22924
22925static void
22926free_cached_comp_units (void *data)
22927{
330cdd98 22928 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22929}
22930
22931/* Increase the age counter on each cached compilation unit, and free
22932 any that are too old. */
22933
22934static void
22935age_cached_comp_units (void)
22936{
22937 struct dwarf2_per_cu_data *per_cu, **last_chain;
22938
22939 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22940 per_cu = dwarf2_per_objfile->read_in_chain;
22941 while (per_cu != NULL)
22942 {
22943 per_cu->cu->last_used ++;
b4f54984 22944 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22945 dwarf2_mark (per_cu->cu);
22946 per_cu = per_cu->cu->read_in_chain;
22947 }
22948
22949 per_cu = dwarf2_per_objfile->read_in_chain;
22950 last_chain = &dwarf2_per_objfile->read_in_chain;
22951 while (per_cu != NULL)
22952 {
22953 struct dwarf2_per_cu_data *next_cu;
22954
22955 next_cu = per_cu->cu->read_in_chain;
22956
22957 if (!per_cu->cu->mark)
22958 {
68dc6402 22959 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22960 *last_chain = next_cu;
22961 }
22962 else
22963 last_chain = &per_cu->cu->read_in_chain;
22964
22965 per_cu = next_cu;
22966 }
22967}
22968
22969/* Remove a single compilation unit from the cache. */
22970
22971static void
dee91e82 22972free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22973{
22974 struct dwarf2_per_cu_data *per_cu, **last_chain;
22975
22976 per_cu = dwarf2_per_objfile->read_in_chain;
22977 last_chain = &dwarf2_per_objfile->read_in_chain;
22978 while (per_cu != NULL)
22979 {
22980 struct dwarf2_per_cu_data *next_cu;
22981
22982 next_cu = per_cu->cu->read_in_chain;
22983
dee91e82 22984 if (per_cu == target_per_cu)
ae038cb0 22985 {
68dc6402 22986 free_heap_comp_unit (per_cu->cu);
dee91e82 22987 per_cu->cu = NULL;
ae038cb0
DJ
22988 *last_chain = next_cu;
22989 break;
22990 }
22991 else
22992 last_chain = &per_cu->cu->read_in_chain;
22993
22994 per_cu = next_cu;
22995 }
22996}
22997
fe3e1990
DJ
22998/* Release all extra memory associated with OBJFILE. */
22999
23000void
23001dwarf2_free_objfile (struct objfile *objfile)
23002{
9a3c8263
SM
23003 dwarf2_per_objfile
23004 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23005 dwarf2_objfile_data_key);
fe3e1990
DJ
23006
23007 if (dwarf2_per_objfile == NULL)
23008 return;
23009
330cdd98 23010 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23011}
23012
dee91e82
DE
23013/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23014 We store these in a hash table separate from the DIEs, and preserve them
23015 when the DIEs are flushed out of cache.
23016
23017 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23018 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23019 or the type may come from a DWO file. Furthermore, while it's more logical
23020 to use per_cu->section+offset, with Fission the section with the data is in
23021 the DWO file but we don't know that section at the point we need it.
23022 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23023 because we can enter the lookup routine, get_die_type_at_offset, from
23024 outside this file, and thus won't necessarily have PER_CU->cu.
23025 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23026
dee91e82 23027struct dwarf2_per_cu_offset_and_type
1c379e20 23028{
dee91e82 23029 const struct dwarf2_per_cu_data *per_cu;
9c541725 23030 sect_offset sect_off;
1c379e20
DJ
23031 struct type *type;
23032};
23033
dee91e82 23034/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23035
23036static hashval_t
dee91e82 23037per_cu_offset_and_type_hash (const void *item)
1c379e20 23038{
9a3c8263
SM
23039 const struct dwarf2_per_cu_offset_and_type *ofs
23040 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23041
9c541725 23042 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23043}
23044
dee91e82 23045/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23046
23047static int
dee91e82 23048per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23049{
9a3c8263
SM
23050 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23051 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23052 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23053 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23054
dee91e82 23055 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23056 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23057}
23058
23059/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23060 table if necessary. For convenience, return TYPE.
23061
23062 The DIEs reading must have careful ordering to:
23063 * Not cause infite loops trying to read in DIEs as a prerequisite for
23064 reading current DIE.
23065 * Not trying to dereference contents of still incompletely read in types
23066 while reading in other DIEs.
23067 * Enable referencing still incompletely read in types just by a pointer to
23068 the type without accessing its fields.
23069
23070 Therefore caller should follow these rules:
23071 * Try to fetch any prerequisite types we may need to build this DIE type
23072 before building the type and calling set_die_type.
e71ec853 23073 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23074 possible before fetching more types to complete the current type.
23075 * Make the type as complete as possible before fetching more types. */
1c379e20 23076
f792889a 23077static struct type *
1c379e20
DJ
23078set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23079{
dee91e82 23080 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23081 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23082 struct attribute *attr;
23083 struct dynamic_prop prop;
1c379e20 23084
b4ba55a1
JB
23085 /* For Ada types, make sure that the gnat-specific data is always
23086 initialized (if not already set). There are a few types where
23087 we should not be doing so, because the type-specific area is
23088 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23089 where the type-specific area is used to store the floatformat).
23090 But this is not a problem, because the gnat-specific information
23091 is actually not needed for these types. */
23092 if (need_gnat_info (cu)
23093 && TYPE_CODE (type) != TYPE_CODE_FUNC
23094 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23095 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23096 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23097 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23098 && !HAVE_GNAT_AUX_INFO (type))
23099 INIT_GNAT_SPECIFIC (type);
23100
3f2f83dd
KB
23101 /* Read DW_AT_allocated and set in type. */
23102 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23103 if (attr_form_is_block (attr))
23104 {
23105 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23106 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23107 }
23108 else if (attr != NULL)
23109 {
23110 complaint (&symfile_complaints,
9c541725
PA
23111 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23112 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23113 to_underlying (die->sect_off));
3f2f83dd
KB
23114 }
23115
23116 /* Read DW_AT_associated and set in type. */
23117 attr = dwarf2_attr (die, DW_AT_associated, cu);
23118 if (attr_form_is_block (attr))
23119 {
23120 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23121 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23122 }
23123 else if (attr != NULL)
23124 {
23125 complaint (&symfile_complaints,
9c541725
PA
23126 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23127 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23128 to_underlying (die->sect_off));
3f2f83dd
KB
23129 }
23130
3cdcd0ce
JB
23131 /* Read DW_AT_data_location and set in type. */
23132 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23133 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23134 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23135
dee91e82 23136 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23137 {
dee91e82
DE
23138 dwarf2_per_objfile->die_type_hash =
23139 htab_create_alloc_ex (127,
23140 per_cu_offset_and_type_hash,
23141 per_cu_offset_and_type_eq,
23142 NULL,
23143 &objfile->objfile_obstack,
23144 hashtab_obstack_allocate,
23145 dummy_obstack_deallocate);
f792889a 23146 }
1c379e20 23147
dee91e82 23148 ofs.per_cu = cu->per_cu;
9c541725 23149 ofs.sect_off = die->sect_off;
1c379e20 23150 ofs.type = type;
dee91e82
DE
23151 slot = (struct dwarf2_per_cu_offset_and_type **)
23152 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23153 if (*slot)
23154 complaint (&symfile_complaints,
23155 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23156 to_underlying (die->sect_off));
8d749320
SM
23157 *slot = XOBNEW (&objfile->objfile_obstack,
23158 struct dwarf2_per_cu_offset_and_type);
1c379e20 23159 **slot = ofs;
f792889a 23160 return type;
1c379e20
DJ
23161}
23162
9c541725 23163/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23164 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23165
23166static struct type *
9c541725 23167get_die_type_at_offset (sect_offset sect_off,
673bfd45 23168 struct dwarf2_per_cu_data *per_cu)
1c379e20 23169{
dee91e82 23170 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23171
dee91e82 23172 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23173 return NULL;
1c379e20 23174
dee91e82 23175 ofs.per_cu = per_cu;
9c541725 23176 ofs.sect_off = sect_off;
9a3c8263
SM
23177 slot = ((struct dwarf2_per_cu_offset_and_type *)
23178 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23179 if (slot)
23180 return slot->type;
23181 else
23182 return NULL;
23183}
23184
02142a6c 23185/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23186 or return NULL if DIE does not have a saved type. */
23187
23188static struct type *
23189get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23190{
9c541725 23191 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23192}
23193
10b3939b
DJ
23194/* Add a dependence relationship from CU to REF_PER_CU. */
23195
23196static void
23197dwarf2_add_dependence (struct dwarf2_cu *cu,
23198 struct dwarf2_per_cu_data *ref_per_cu)
23199{
23200 void **slot;
23201
23202 if (cu->dependencies == NULL)
23203 cu->dependencies
23204 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23205 NULL, &cu->comp_unit_obstack,
23206 hashtab_obstack_allocate,
23207 dummy_obstack_deallocate);
23208
23209 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23210 if (*slot == NULL)
23211 *slot = ref_per_cu;
23212}
1c379e20 23213
f504f079
DE
23214/* Subroutine of dwarf2_mark to pass to htab_traverse.
23215 Set the mark field in every compilation unit in the
ae038cb0
DJ
23216 cache that we must keep because we are keeping CU. */
23217
10b3939b
DJ
23218static int
23219dwarf2_mark_helper (void **slot, void *data)
23220{
23221 struct dwarf2_per_cu_data *per_cu;
23222
23223 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23224
23225 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23226 reading of the chain. As such dependencies remain valid it is not much
23227 useful to track and undo them during QUIT cleanups. */
23228 if (per_cu->cu == NULL)
23229 return 1;
23230
10b3939b
DJ
23231 if (per_cu->cu->mark)
23232 return 1;
23233 per_cu->cu->mark = 1;
23234
23235 if (per_cu->cu->dependencies != NULL)
23236 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23237
23238 return 1;
23239}
23240
f504f079
DE
23241/* Set the mark field in CU and in every other compilation unit in the
23242 cache that we must keep because we are keeping CU. */
23243
ae038cb0
DJ
23244static void
23245dwarf2_mark (struct dwarf2_cu *cu)
23246{
23247 if (cu->mark)
23248 return;
23249 cu->mark = 1;
10b3939b
DJ
23250 if (cu->dependencies != NULL)
23251 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23252}
23253
23254static void
23255dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23256{
23257 while (per_cu)
23258 {
23259 per_cu->cu->mark = 0;
23260 per_cu = per_cu->cu->read_in_chain;
23261 }
72bf9492
DJ
23262}
23263
72bf9492
DJ
23264/* Trivial hash function for partial_die_info: the hash value of a DIE
23265 is its offset in .debug_info for this objfile. */
23266
23267static hashval_t
23268partial_die_hash (const void *item)
23269{
9a3c8263
SM
23270 const struct partial_die_info *part_die
23271 = (const struct partial_die_info *) item;
9a619af0 23272
9c541725 23273 return to_underlying (part_die->sect_off);
72bf9492
DJ
23274}
23275
23276/* Trivial comparison function for partial_die_info structures: two DIEs
23277 are equal if they have the same offset. */
23278
23279static int
23280partial_die_eq (const void *item_lhs, const void *item_rhs)
23281{
9a3c8263
SM
23282 const struct partial_die_info *part_die_lhs
23283 = (const struct partial_die_info *) item_lhs;
23284 const struct partial_die_info *part_die_rhs
23285 = (const struct partial_die_info *) item_rhs;
9a619af0 23286
9c541725 23287 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23288}
23289
b4f54984
DE
23290static struct cmd_list_element *set_dwarf_cmdlist;
23291static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23292
23293static void
b4f54984 23294set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23295{
b4f54984 23296 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23297 gdb_stdout);
ae038cb0
DJ
23298}
23299
23300static void
b4f54984 23301show_dwarf_cmd (char *args, int from_tty)
6e70227d 23302{
b4f54984 23303 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23304}
23305
4bf44c1c 23306/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23307
23308static void
c1bd65d0 23309dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23310{
9a3c8263 23311 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23312 int ix;
8b70b953 23313
626f2d1c
TT
23314 /* Make sure we don't accidentally use dwarf2_per_objfile while
23315 cleaning up. */
23316 dwarf2_per_objfile = NULL;
23317
59b0c7c1
JB
23318 for (ix = 0; ix < data->n_comp_units; ++ix)
23319 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23320
59b0c7c1 23321 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23322 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23323 data->all_type_units[ix]->per_cu.imported_symtabs);
23324 xfree (data->all_type_units);
95554aad 23325
8b70b953 23326 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23327
23328 if (data->dwo_files)
23329 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23330 if (data->dwp_file)
23331 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23332
23333 if (data->dwz_file && data->dwz_file->dwz_bfd)
23334 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23335}
23336
23337\f
ae2de4f8 23338/* The "save gdb-index" command. */
9291a0cd 23339
bc8f2430
JK
23340/* In-memory buffer to prepare data to be written later to a file. */
23341class data_buf
9291a0cd 23342{
bc8f2430 23343public:
bc8f2430
JK
23344 /* Copy DATA to the end of the buffer. */
23345 template<typename T>
23346 void append_data (const T &data)
23347 {
23348 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23349 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23350 grow (sizeof (data)));
bc8f2430 23351 }
b89be57b 23352
c2f134ac
PA
23353 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23354 terminating zero is appended too. */
bc8f2430
JK
23355 void append_cstr0 (const char *cstr)
23356 {
23357 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23358 std::copy (cstr, cstr + size, grow (size));
23359 }
23360
23361 /* Accept a host-format integer in VAL and append it to the buffer
23362 as a target-format integer which is LEN bytes long. */
23363 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23364 {
23365 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23366 }
9291a0cd 23367
bc8f2430
JK
23368 /* Return the size of the buffer. */
23369 size_t size () const
23370 {
23371 return m_vec.size ();
23372 }
23373
23374 /* Write the buffer to FILE. */
23375 void file_write (FILE *file) const
23376 {
a81e6d4d
PA
23377 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23378 error (_("couldn't write data to file"));
bc8f2430
JK
23379 }
23380
23381private:
c2f134ac
PA
23382 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23383 the start of the new block. */
23384 gdb_byte *grow (size_t size)
23385 {
23386 m_vec.resize (m_vec.size () + size);
23387 return &*m_vec.end () - size;
23388 }
23389
d5722aa2 23390 gdb::byte_vector m_vec;
bc8f2430 23391};
9291a0cd
TT
23392
23393/* An entry in the symbol table. */
23394struct symtab_index_entry
23395{
23396 /* The name of the symbol. */
23397 const char *name;
23398 /* The offset of the name in the constant pool. */
23399 offset_type index_offset;
23400 /* A sorted vector of the indices of all the CUs that hold an object
23401 of this name. */
bc8f2430 23402 std::vector<offset_type> cu_indices;
9291a0cd
TT
23403};
23404
23405/* The symbol table. This is a power-of-2-sized hash table. */
23406struct mapped_symtab
23407{
bc8f2430
JK
23408 mapped_symtab ()
23409 {
23410 data.resize (1024);
23411 }
b89be57b 23412
bc8f2430 23413 offset_type n_elements = 0;
4b76cda9 23414 std::vector<symtab_index_entry> data;
bc8f2430 23415};
9291a0cd 23416
bc8f2430 23417/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23418 the slot.
23419
23420 Function is used only during write_hash_table so no index format backward
23421 compatibility is needed. */
b89be57b 23422
4b76cda9 23423static symtab_index_entry &
9291a0cd
TT
23424find_slot (struct mapped_symtab *symtab, const char *name)
23425{
559a7a62 23426 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23427
bc8f2430
JK
23428 index = hash & (symtab->data.size () - 1);
23429 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23430
23431 for (;;)
23432 {
4b76cda9
PA
23433 if (symtab->data[index].name == NULL
23434 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23435 return symtab->data[index];
23436 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23437 }
23438}
23439
23440/* Expand SYMTAB's hash table. */
b89be57b 23441
9291a0cd
TT
23442static void
23443hash_expand (struct mapped_symtab *symtab)
23444{
bc8f2430 23445 auto old_entries = std::move (symtab->data);
9291a0cd 23446
bc8f2430
JK
23447 symtab->data.clear ();
23448 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23449
bc8f2430 23450 for (auto &it : old_entries)
4b76cda9 23451 if (it.name != NULL)
bc8f2430 23452 {
4b76cda9 23453 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23454 ref = std::move (it);
23455 }
9291a0cd
TT
23456}
23457
156942c7
DE
23458/* Add an entry to SYMTAB. NAME is the name of the symbol.
23459 CU_INDEX is the index of the CU in which the symbol appears.
23460 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23461
9291a0cd
TT
23462static void
23463add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23464 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23465 offset_type cu_index)
23466{
156942c7 23467 offset_type cu_index_and_attrs;
9291a0cd
TT
23468
23469 ++symtab->n_elements;
bc8f2430 23470 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23471 hash_expand (symtab);
23472
4b76cda9
PA
23473 symtab_index_entry &slot = find_slot (symtab, name);
23474 if (slot.name == NULL)
9291a0cd 23475 {
4b76cda9 23476 slot.name = name;
156942c7 23477 /* index_offset is set later. */
9291a0cd 23478 }
156942c7
DE
23479
23480 cu_index_and_attrs = 0;
23481 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23482 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23483 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23484
23485 /* We don't want to record an index value twice as we want to avoid the
23486 duplication.
23487 We process all global symbols and then all static symbols
23488 (which would allow us to avoid the duplication by only having to check
23489 the last entry pushed), but a symbol could have multiple kinds in one CU.
23490 To keep things simple we don't worry about the duplication here and
23491 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23492 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23493}
23494
23495/* Sort and remove duplicates of all symbols' cu_indices lists. */
23496
23497static void
23498uniquify_cu_indices (struct mapped_symtab *symtab)
23499{
4b76cda9 23500 for (auto &entry : symtab->data)
156942c7 23501 {
4b76cda9 23502 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23503 {
4b76cda9 23504 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23505 std::sort (cu_indices.begin (), cu_indices.end ());
23506 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23507 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23508 }
23509 }
9291a0cd
TT
23510}
23511
bc8f2430
JK
23512/* A form of 'const char *' suitable for container keys. Only the
23513 pointer is stored. The strings themselves are compared, not the
23514 pointers. */
23515class c_str_view
9291a0cd 23516{
bc8f2430
JK
23517public:
23518 c_str_view (const char *cstr)
23519 : m_cstr (cstr)
23520 {}
9291a0cd 23521
bc8f2430
JK
23522 bool operator== (const c_str_view &other) const
23523 {
23524 return strcmp (m_cstr, other.m_cstr) == 0;
23525 }
9291a0cd 23526
bc8f2430
JK
23527private:
23528 friend class c_str_view_hasher;
23529 const char *const m_cstr;
23530};
9291a0cd 23531
bc8f2430
JK
23532/* A std::unordered_map::hasher for c_str_view that uses the right
23533 hash function for strings in a mapped index. */
23534class c_str_view_hasher
23535{
23536public:
23537 size_t operator () (const c_str_view &x) const
23538 {
23539 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23540 }
23541};
b89be57b 23542
bc8f2430
JK
23543/* A std::unordered_map::hasher for std::vector<>. */
23544template<typename T>
23545class vector_hasher
9291a0cd 23546{
bc8f2430
JK
23547public:
23548 size_t operator () (const std::vector<T> &key) const
23549 {
23550 return iterative_hash (key.data (),
23551 sizeof (key.front ()) * key.size (), 0);
23552 }
23553};
9291a0cd 23554
bc8f2430
JK
23555/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23556 constant pool entries going into the data buffer CPOOL. */
3876f04e 23557
bc8f2430
JK
23558static void
23559write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23560{
23561 {
23562 /* Elements are sorted vectors of the indices of all the CUs that
23563 hold an object of this name. */
23564 std::unordered_map<std::vector<offset_type>, offset_type,
23565 vector_hasher<offset_type>>
23566 symbol_hash_table;
23567
23568 /* We add all the index vectors to the constant pool first, to
23569 ensure alignment is ok. */
4b76cda9 23570 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23571 {
4b76cda9 23572 if (entry.name == NULL)
bc8f2430 23573 continue;
4b76cda9 23574 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23575
23576 /* Finding before inserting is faster than always trying to
23577 insert, because inserting always allocates a node, does the
23578 lookup, and then destroys the new node if another node
23579 already had the same key. C++17 try_emplace will avoid
23580 this. */
23581 const auto found
4b76cda9 23582 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23583 if (found != symbol_hash_table.end ())
23584 {
4b76cda9 23585 entry.index_offset = found->second;
70a1152b
PA
23586 continue;
23587 }
23588
4b76cda9
PA
23589 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23590 entry.index_offset = cpool.size ();
23591 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23592 for (const auto index : entry.cu_indices)
23593 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23594 }
23595 }
9291a0cd
TT
23596
23597 /* Now write out the hash table. */
bc8f2430 23598 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23599 for (const auto &entry : symtab->data)
9291a0cd
TT
23600 {
23601 offset_type str_off, vec_off;
23602
4b76cda9 23603 if (entry.name != NULL)
9291a0cd 23604 {
4b76cda9 23605 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23606 if (insertpair.second)
4b76cda9 23607 cpool.append_cstr0 (entry.name);
bc8f2430 23608 str_off = insertpair.first->second;
4b76cda9 23609 vec_off = entry.index_offset;
9291a0cd
TT
23610 }
23611 else
23612 {
23613 /* While 0 is a valid constant pool index, it is not valid
23614 to have 0 for both offsets. */
23615 str_off = 0;
23616 vec_off = 0;
23617 }
23618
bc8f2430
JK
23619 output.append_data (MAYBE_SWAP (str_off));
23620 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23621 }
9291a0cd
TT
23622}
23623
bc8f2430 23624typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23625
23626/* Helper struct for building the address table. */
23627struct addrmap_index_data
23628{
bc8f2430
JK
23629 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23630 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23631 {}
23632
0a5429f6 23633 struct objfile *objfile;
bc8f2430
JK
23634 data_buf &addr_vec;
23635 psym_index_map &cu_index_htab;
0a5429f6
DE
23636
23637 /* Non-zero if the previous_* fields are valid.
23638 We can't write an entry until we see the next entry (since it is only then
23639 that we know the end of the entry). */
23640 int previous_valid;
23641 /* Index of the CU in the table of all CUs in the index file. */
23642 unsigned int previous_cu_index;
0963b4bd 23643 /* Start address of the CU. */
0a5429f6
DE
23644 CORE_ADDR previous_cu_start;
23645};
23646
bc8f2430 23647/* Write an address entry to ADDR_VEC. */
b89be57b 23648
9291a0cd 23649static void
bc8f2430 23650add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23651 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23652{
9291a0cd
TT
23653 CORE_ADDR baseaddr;
23654
23655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23656
c2f134ac
PA
23657 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23658 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23659 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23660}
23661
23662/* Worker function for traversing an addrmap to build the address table. */
23663
23664static int
23665add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23666{
9a3c8263
SM
23667 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23668 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23669
23670 if (data->previous_valid)
bc8f2430 23671 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23672 data->previous_cu_start, start_addr,
23673 data->previous_cu_index);
23674
23675 data->previous_cu_start = start_addr;
23676 if (pst != NULL)
23677 {
bc8f2430
JK
23678 const auto it = data->cu_index_htab.find (pst);
23679 gdb_assert (it != data->cu_index_htab.cend ());
23680 data->previous_cu_index = it->second;
0a5429f6
DE
23681 data->previous_valid = 1;
23682 }
23683 else
bc8f2430 23684 data->previous_valid = 0;
0a5429f6
DE
23685
23686 return 0;
23687}
23688
bc8f2430 23689/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23690 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23691 in the index file. */
23692
23693static void
bc8f2430
JK
23694write_address_map (struct objfile *objfile, data_buf &addr_vec,
23695 psym_index_map &cu_index_htab)
0a5429f6 23696{
bc8f2430 23697 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23698
23699 /* When writing the address table, we have to cope with the fact that
23700 the addrmap iterator only provides the start of a region; we have to
23701 wait until the next invocation to get the start of the next region. */
23702
23703 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23704 addrmap_index_data.previous_valid = 0;
23705
23706 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23707 &addrmap_index_data);
23708
23709 /* It's highly unlikely the last entry (end address = 0xff...ff)
23710 is valid, but we should still handle it.
23711 The end address is recorded as the start of the next region, but that
23712 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23713 anyway. */
23714 if (addrmap_index_data.previous_valid)
bc8f2430 23715 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23716 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23717 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23718}
23719
156942c7
DE
23720/* Return the symbol kind of PSYM. */
23721
23722static gdb_index_symbol_kind
23723symbol_kind (struct partial_symbol *psym)
23724{
23725 domain_enum domain = PSYMBOL_DOMAIN (psym);
23726 enum address_class aclass = PSYMBOL_CLASS (psym);
23727
23728 switch (domain)
23729 {
23730 case VAR_DOMAIN:
23731 switch (aclass)
23732 {
23733 case LOC_BLOCK:
23734 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23735 case LOC_TYPEDEF:
23736 return GDB_INDEX_SYMBOL_KIND_TYPE;
23737 case LOC_COMPUTED:
23738 case LOC_CONST_BYTES:
23739 case LOC_OPTIMIZED_OUT:
23740 case LOC_STATIC:
23741 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23742 case LOC_CONST:
23743 /* Note: It's currently impossible to recognize psyms as enum values
23744 short of reading the type info. For now punt. */
23745 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23746 default:
23747 /* There are other LOC_FOO values that one might want to classify
23748 as variables, but dwarf2read.c doesn't currently use them. */
23749 return GDB_INDEX_SYMBOL_KIND_OTHER;
23750 }
23751 case STRUCT_DOMAIN:
23752 return GDB_INDEX_SYMBOL_KIND_TYPE;
23753 default:
23754 return GDB_INDEX_SYMBOL_KIND_OTHER;
23755 }
23756}
23757
9291a0cd 23758/* Add a list of partial symbols to SYMTAB. */
b89be57b 23759
9291a0cd
TT
23760static void
23761write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23762 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23763 struct partial_symbol **psymp,
23764 int count,
987d643c
TT
23765 offset_type cu_index,
23766 int is_static)
9291a0cd
TT
23767{
23768 for (; count-- > 0; ++psymp)
23769 {
156942c7 23770 struct partial_symbol *psym = *psymp;
987d643c 23771
156942c7 23772 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23773 error (_("Ada is not currently supported by the index"));
987d643c 23774
987d643c 23775 /* Only add a given psymbol once. */
bc8f2430 23776 if (psyms_seen.insert (psym).second)
987d643c 23777 {
156942c7
DE
23778 gdb_index_symbol_kind kind = symbol_kind (psym);
23779
156942c7
DE
23780 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23781 is_static, kind, cu_index);
987d643c 23782 }
9291a0cd
TT
23783 }
23784}
23785
1fd400ff
TT
23786/* A helper struct used when iterating over debug_types. */
23787struct signatured_type_index_data
23788{
bc8f2430
JK
23789 signatured_type_index_data (data_buf &types_list_,
23790 std::unordered_set<partial_symbol *> &psyms_seen_)
23791 : types_list (types_list_), psyms_seen (psyms_seen_)
23792 {}
23793
1fd400ff
TT
23794 struct objfile *objfile;
23795 struct mapped_symtab *symtab;
bc8f2430
JK
23796 data_buf &types_list;
23797 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23798 int cu_index;
23799};
23800
23801/* A helper function that writes a single signatured_type to an
23802 obstack. */
b89be57b 23803
1fd400ff
TT
23804static int
23805write_one_signatured_type (void **slot, void *d)
23806{
9a3c8263
SM
23807 struct signatured_type_index_data *info
23808 = (struct signatured_type_index_data *) d;
1fd400ff 23809 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23810 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23811
23812 write_psymbols (info->symtab,
987d643c 23813 info->psyms_seen,
3e43a32a
MS
23814 info->objfile->global_psymbols.list
23815 + psymtab->globals_offset,
987d643c
TT
23816 psymtab->n_global_syms, info->cu_index,
23817 0);
1fd400ff 23818 write_psymbols (info->symtab,
987d643c 23819 info->psyms_seen,
3e43a32a
MS
23820 info->objfile->static_psymbols.list
23821 + psymtab->statics_offset,
987d643c
TT
23822 psymtab->n_static_syms, info->cu_index,
23823 1);
1fd400ff 23824
c2f134ac
PA
23825 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23826 to_underlying (entry->per_cu.sect_off));
23827 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23828 to_underlying (entry->type_offset_in_tu));
23829 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23830
23831 ++info->cu_index;
23832
23833 return 1;
23834}
23835
e8f8bcb3
PA
23836/* Recurse into all "included" dependencies and count their symbols as
23837 if they appeared in this psymtab. */
23838
23839static void
23840recursively_count_psymbols (struct partial_symtab *psymtab,
23841 size_t &psyms_seen)
23842{
23843 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23844 if (psymtab->dependencies[i]->user != NULL)
23845 recursively_count_psymbols (psymtab->dependencies[i],
23846 psyms_seen);
23847
23848 psyms_seen += psymtab->n_global_syms;
23849 psyms_seen += psymtab->n_static_syms;
23850}
23851
95554aad
TT
23852/* Recurse into all "included" dependencies and write their symbols as
23853 if they appeared in this psymtab. */
23854
23855static void
23856recursively_write_psymbols (struct objfile *objfile,
23857 struct partial_symtab *psymtab,
23858 struct mapped_symtab *symtab,
bc8f2430 23859 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23860 offset_type cu_index)
23861{
23862 int i;
23863
23864 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23865 if (psymtab->dependencies[i]->user != NULL)
23866 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23867 symtab, psyms_seen, cu_index);
23868
23869 write_psymbols (symtab,
23870 psyms_seen,
23871 objfile->global_psymbols.list + psymtab->globals_offset,
23872 psymtab->n_global_syms, cu_index,
23873 0);
23874 write_psymbols (symtab,
23875 psyms_seen,
23876 objfile->static_psymbols.list + psymtab->statics_offset,
23877 psymtab->n_static_syms, cu_index,
23878 1);
23879}
23880
9291a0cd 23881/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23882
9291a0cd
TT
23883static void
23884write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23885{
9291a0cd
TT
23886 if (dwarf2_per_objfile->using_index)
23887 error (_("Cannot use an index to create the index"));
23888
8b70b953
TT
23889 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23890 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23891
260b681b
DE
23892 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23893 return;
23894
bc8f2430 23895 struct stat st;
4262abfb
JK
23896 if (stat (objfile_name (objfile), &st) < 0)
23897 perror_with_name (objfile_name (objfile));
9291a0cd 23898
bc8f2430
JK
23899 std::string filename (std::string (dir) + SLASH_STRING
23900 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23901
d419f42d 23902 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23903 if (!out_file)
bc8f2430 23904 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23905
16b7a719
PA
23906 /* Order matters here; we want FILE to be closed before FILENAME is
23907 unlinked, because on MS-Windows one cannot delete a file that is
23908 still open. (Don't call anything here that might throw until
23909 file_closer is created.) */
bc8f2430 23910 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23911 gdb_file_up close_out_file (out_file);
9291a0cd 23912
bc8f2430
JK
23913 mapped_symtab symtab;
23914 data_buf cu_list;
987d643c 23915
0a5429f6
DE
23916 /* While we're scanning CU's create a table that maps a psymtab pointer
23917 (which is what addrmap records) to its index (which is what is recorded
23918 in the index file). This will later be needed to write the address
23919 table. */
bc8f2430
JK
23920 psym_index_map cu_index_htab;
23921 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23922
23923 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23924 work here. Also, the debug_types entries do not appear in
23925 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23926
23927 /* The psyms_seen set is potentially going to be largish (~40k
23928 elements when indexing a -g3 build of GDB itself). Estimate the
23929 number of elements in order to avoid too many rehashes, which
23930 require rebuilding buckets and thus many trips to
23931 malloc/free. */
23932 size_t psyms_count = 0;
23933 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23934 {
23935 struct dwarf2_per_cu_data *per_cu
23936 = dwarf2_per_objfile->all_comp_units[i];
23937 struct partial_symtab *psymtab = per_cu->v.psymtab;
23938
23939 if (psymtab != NULL && psymtab->user == NULL)
23940 recursively_count_psymbols (psymtab, psyms_count);
23941 }
23942 /* Generating an index for gdb itself shows a ratio of
23943 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23944 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23945 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23946 {
3e43a32a
MS
23947 struct dwarf2_per_cu_data *per_cu
23948 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23949 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23950
92fac807
JK
23951 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23952 It may be referenced from a local scope but in such case it does not
23953 need to be present in .gdb_index. */
23954 if (psymtab == NULL)
23955 continue;
23956
95554aad 23957 if (psymtab->user == NULL)
bc8f2430
JK
23958 recursively_write_psymbols (objfile, psymtab, &symtab,
23959 psyms_seen, i);
9291a0cd 23960
bc8f2430
JK
23961 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23962 gdb_assert (insertpair.second);
9291a0cd 23963
c2f134ac
PA
23964 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23965 to_underlying (per_cu->sect_off));
23966 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23967 }
23968
0a5429f6 23969 /* Dump the address map. */
bc8f2430
JK
23970 data_buf addr_vec;
23971 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23972
1fd400ff 23973 /* Write out the .debug_type entries, if any. */
bc8f2430 23974 data_buf types_cu_list;
1fd400ff
TT
23975 if (dwarf2_per_objfile->signatured_types)
23976 {
bc8f2430
JK
23977 signatured_type_index_data sig_data (types_cu_list,
23978 psyms_seen);
1fd400ff
TT
23979
23980 sig_data.objfile = objfile;
bc8f2430 23981 sig_data.symtab = &symtab;
1fd400ff
TT
23982 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23983 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23984 write_one_signatured_type, &sig_data);
23985 }
23986
156942c7
DE
23987 /* Now that we've processed all symbols we can shrink their cu_indices
23988 lists. */
bc8f2430 23989 uniquify_cu_indices (&symtab);
156942c7 23990
bc8f2430
JK
23991 data_buf symtab_vec, constant_pool;
23992 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23993
bc8f2430
JK
23994 data_buf contents;
23995 const offset_type size_of_contents = 6 * sizeof (offset_type);
23996 offset_type total_len = size_of_contents;
9291a0cd
TT
23997
23998 /* The version number. */
bc8f2430 23999 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24000
24001 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24002 contents.append_data (MAYBE_SWAP (total_len));
24003 total_len += cu_list.size ();
9291a0cd 24004
1fd400ff 24005 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24006 contents.append_data (MAYBE_SWAP (total_len));
24007 total_len += types_cu_list.size ();
1fd400ff 24008
9291a0cd 24009 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24010 contents.append_data (MAYBE_SWAP (total_len));
24011 total_len += addr_vec.size ();
9291a0cd
TT
24012
24013 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24014 contents.append_data (MAYBE_SWAP (total_len));
24015 total_len += symtab_vec.size ();
9291a0cd
TT
24016
24017 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24018 contents.append_data (MAYBE_SWAP (total_len));
24019 total_len += constant_pool.size ();
9291a0cd 24020
bc8f2430 24021 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24022
bc8f2430
JK
24023 contents.file_write (out_file);
24024 cu_list.file_write (out_file);
24025 types_cu_list.file_write (out_file);
24026 addr_vec.file_write (out_file);
24027 symtab_vec.file_write (out_file);
24028 constant_pool.file_write (out_file);
9291a0cd 24029
bef155c3
TT
24030 /* We want to keep the file. */
24031 unlink_file.keep ();
9291a0cd
TT
24032}
24033
90476074
TT
24034/* Implementation of the `save gdb-index' command.
24035
24036 Note that the file format used by this command is documented in the
24037 GDB manual. Any changes here must be documented there. */
11570e71 24038
9291a0cd
TT
24039static void
24040save_gdb_index_command (char *arg, int from_tty)
24041{
24042 struct objfile *objfile;
24043
24044 if (!arg || !*arg)
96d19272 24045 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24046
24047 ALL_OBJFILES (objfile)
24048 {
24049 struct stat st;
24050
24051 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24052 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24053 continue;
24054
9a3c8263
SM
24055 dwarf2_per_objfile
24056 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24057 dwarf2_objfile_data_key);
9291a0cd
TT
24058 if (dwarf2_per_objfile)
24059 {
9291a0cd 24060
492d29ea 24061 TRY
9291a0cd
TT
24062 {
24063 write_psymtabs_to_index (objfile, arg);
24064 }
492d29ea
PA
24065 CATCH (except, RETURN_MASK_ERROR)
24066 {
24067 exception_fprintf (gdb_stderr, except,
24068 _("Error while writing index for `%s': "),
24069 objfile_name (objfile));
24070 }
24071 END_CATCH
9291a0cd
TT
24072 }
24073 }
dce234bc
PP
24074}
24075
9291a0cd
TT
24076\f
24077
b4f54984 24078int dwarf_always_disassemble;
9eae7c52
TT
24079
24080static void
b4f54984
DE
24081show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24082 struct cmd_list_element *c, const char *value)
9eae7c52 24083{
3e43a32a
MS
24084 fprintf_filtered (file,
24085 _("Whether to always disassemble "
24086 "DWARF expressions is %s.\n"),
9eae7c52
TT
24087 value);
24088}
24089
900e11f9
JK
24090static void
24091show_check_physname (struct ui_file *file, int from_tty,
24092 struct cmd_list_element *c, const char *value)
24093{
24094 fprintf_filtered (file,
24095 _("Whether to check \"physname\" is %s.\n"),
24096 value);
24097}
24098
6502dd73
DJ
24099void _initialize_dwarf2_read (void);
24100
24101void
24102_initialize_dwarf2_read (void)
24103{
96d19272
JK
24104 struct cmd_list_element *c;
24105
dce234bc 24106 dwarf2_objfile_data_key
c1bd65d0 24107 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24108
b4f54984
DE
24109 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24110Set DWARF specific variables.\n\
24111Configure DWARF variables such as the cache size"),
24112 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24113 0/*allow-unknown*/, &maintenance_set_cmdlist);
24114
b4f54984
DE
24115 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24116Show DWARF specific variables\n\
24117Show DWARF variables such as the cache size"),
24118 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24119 0/*allow-unknown*/, &maintenance_show_cmdlist);
24120
24121 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24122 &dwarf_max_cache_age, _("\
24123Set the upper bound on the age of cached DWARF compilation units."), _("\
24124Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24125A higher limit means that cached compilation units will be stored\n\
24126in memory longer, and more total memory will be used. Zero disables\n\
24127caching, which can slow down startup."),
2c5b56ce 24128 NULL,
b4f54984
DE
24129 show_dwarf_max_cache_age,
24130 &set_dwarf_cmdlist,
24131 &show_dwarf_cmdlist);
d97bc12b 24132
9eae7c52 24133 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24134 &dwarf_always_disassemble, _("\
9eae7c52
TT
24135Set whether `info address' always disassembles DWARF expressions."), _("\
24136Show whether `info address' always disassembles DWARF expressions."), _("\
24137When enabled, DWARF expressions are always printed in an assembly-like\n\
24138syntax. When disabled, expressions will be printed in a more\n\
24139conversational style, when possible."),
24140 NULL,
b4f54984
DE
24141 show_dwarf_always_disassemble,
24142 &set_dwarf_cmdlist,
24143 &show_dwarf_cmdlist);
24144
24145 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24146Set debugging of the DWARF reader."), _("\
24147Show debugging of the DWARF reader."), _("\
24148When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24149reading and symtab expansion. A value of 1 (one) provides basic\n\
24150information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24151 NULL,
24152 NULL,
24153 &setdebuglist, &showdebuglist);
24154
b4f54984
DE
24155 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24156Set debugging of the DWARF DIE reader."), _("\
24157Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24158When enabled (non-zero), DIEs are dumped after they are read in.\n\
24159The value is the maximum depth to print."),
ccce17b0
YQ
24160 NULL,
24161 NULL,
24162 &setdebuglist, &showdebuglist);
9291a0cd 24163
27e0867f
DE
24164 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24165Set debugging of the dwarf line reader."), _("\
24166Show debugging of the dwarf line reader."), _("\
24167When enabled (non-zero), line number entries are dumped as they are read in.\n\
24168A value of 1 (one) provides basic information.\n\
24169A value greater than 1 provides more verbose information."),
24170 NULL,
24171 NULL,
24172 &setdebuglist, &showdebuglist);
24173
900e11f9
JK
24174 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24175Set cross-checking of \"physname\" code against demangler."), _("\
24176Show cross-checking of \"physname\" code against demangler."), _("\
24177When enabled, GDB's internal \"physname\" code is checked against\n\
24178the demangler."),
24179 NULL, show_check_physname,
24180 &setdebuglist, &showdebuglist);
24181
e615022a
DE
24182 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24183 no_class, &use_deprecated_index_sections, _("\
24184Set whether to use deprecated gdb_index sections."), _("\
24185Show whether to use deprecated gdb_index sections."), _("\
24186When enabled, deprecated .gdb_index sections are used anyway.\n\
24187Normally they are ignored either because of a missing feature or\n\
24188performance issue.\n\
24189Warning: This option must be enabled before gdb reads the file."),
24190 NULL,
24191 NULL,
24192 &setlist, &showlist);
24193
96d19272 24194 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24195 _("\
fc1a9d6e 24196Save a gdb-index file.\n\
11570e71 24197Usage: save gdb-index DIRECTORY"),
96d19272
JK
24198 &save_cmdlist);
24199 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24200
24201 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24202 &dwarf2_locexpr_funcs);
24203 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24204 &dwarf2_loclist_funcs);
24205
24206 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24207 &dwarf2_block_frame_base_locexpr_funcs);
24208 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24209 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24210}
This page took 6.838313 seconds and 4 git commands to generate.