]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
Introduce lookup_name_info and generalize Ada's FULL/WILD name matching
[thirdparty/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
bbf2f4df 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
c906108c 79#include <fcntl.h>
c906108c 80#include <sys/types.h>
325fac50 81#include <algorithm>
bc8f2430
JK
82#include <unordered_set>
83#include <unordered_map>
d8151005 84
34eaf542
TT
85typedef struct symbol *symbolp;
86DEF_VEC_P (symbolp);
87
73be47f5
DE
88/* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
b4f54984
DE
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91static unsigned int dwarf_read_debug = 0;
45cfd468 92
d97bc12b 93/* When non-zero, dump DIEs after they are read in. */
b4f54984 94static unsigned int dwarf_die_debug = 0;
d97bc12b 95
27e0867f
DE
96/* When non-zero, dump line number entries as they are read in. */
97static unsigned int dwarf_line_debug = 0;
98
900e11f9
JK
99/* When non-zero, cross-check physname against demangler. */
100static int check_physname = 0;
101
481860b3 102/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 103static int use_deprecated_index_sections = 0;
481860b3 104
6502dd73
DJ
105static const struct objfile_data *dwarf2_objfile_data_key;
106
f1e6e072
TT
107/* The "aclass" indices for various kinds of computed DWARF symbols. */
108
109static int dwarf2_locexpr_index;
110static int dwarf2_loclist_index;
111static int dwarf2_locexpr_block_index;
112static int dwarf2_loclist_block_index;
113
73869dc2
DE
114/* A descriptor for dwarf sections.
115
116 S.ASECTION, SIZE are typically initialized when the objfile is first
117 scanned. BUFFER, READIN are filled in later when the section is read.
118 If the section contained compressed data then SIZE is updated to record
119 the uncompressed size of the section.
120
121 DWP file format V2 introduces a wrinkle that is easiest to handle by
122 creating the concept of virtual sections contained within a real section.
123 In DWP V2 the sections of the input DWO files are concatenated together
124 into one section, but section offsets are kept relative to the original
125 input section.
126 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
127 the real section this "virtual" section is contained in, and BUFFER,SIZE
128 describe the virtual section. */
129
dce234bc
PP
130struct dwarf2_section_info
131{
73869dc2
DE
132 union
133 {
e5aa3347 134 /* If this is a real section, the bfd section. */
049412e3 135 asection *section;
73869dc2 136 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 137 section. */
73869dc2
DE
138 struct dwarf2_section_info *containing_section;
139 } s;
19ac8c2e 140 /* Pointer to section data, only valid if readin. */
d521ce57 141 const gdb_byte *buffer;
73869dc2 142 /* The size of the section, real or virtual. */
dce234bc 143 bfd_size_type size;
73869dc2
DE
144 /* If this is a virtual section, the offset in the real section.
145 Only valid if is_virtual. */
146 bfd_size_type virtual_offset;
be391dca 147 /* True if we have tried to read this section. */
73869dc2
DE
148 char readin;
149 /* True if this is a virtual section, False otherwise.
049412e3 150 This specifies which of s.section and s.containing_section to use. */
73869dc2 151 char is_virtual;
dce234bc
PP
152};
153
8b70b953
TT
154typedef struct dwarf2_section_info dwarf2_section_info_def;
155DEF_VEC_O (dwarf2_section_info_def);
156
9291a0cd
TT
157/* All offsets in the index are of this type. It must be
158 architecture-independent. */
159typedef uint32_t offset_type;
160
161DEF_VEC_I (offset_type);
162
156942c7
DE
163/* Ensure only legit values are used. */
164#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((unsigned int) (value) <= 1); \
167 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
168 } while (0)
169
170/* Ensure only legit values are used. */
171#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
172 do { \
173 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
174 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
175 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
178/* Ensure we don't use more than the alloted nuber of bits for the CU. */
179#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
180 do { \
181 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
182 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
183 } while (0)
184
9291a0cd
TT
185/* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187struct mapped_index
188{
559a7a62
JK
189 /* Index data format version. */
190 int version;
191
9291a0cd
TT
192 /* The total length of the buffer. */
193 off_t total_size;
b11b1f88 194
9291a0cd
TT
195 /* A pointer to the address table data. */
196 const gdb_byte *address_table;
b11b1f88 197
9291a0cd
TT
198 /* Size of the address table data in bytes. */
199 offset_type address_table_size;
b11b1f88 200
3876f04e
DE
201 /* The symbol table, implemented as a hash table. */
202 const offset_type *symbol_table;
b11b1f88 203
9291a0cd 204 /* Size in slots, each slot is 2 offset_types. */
3876f04e 205 offset_type symbol_table_slots;
b11b1f88 206
9291a0cd
TT
207 /* A pointer to the constant pool. */
208 const char *constant_pool;
209};
210
95554aad
TT
211typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
212DEF_VEC_P (dwarf2_per_cu_ptr);
213
52059ffd
TT
214struct tu_stats
215{
216 int nr_uniq_abbrev_tables;
217 int nr_symtabs;
218 int nr_symtab_sharers;
219 int nr_stmt_less_type_units;
220 int nr_all_type_units_reallocs;
221};
222
9cdd5dbd
DE
223/* Collection of data recorded per objfile.
224 This hangs off of dwarf2_objfile_data_key. */
225
6502dd73
DJ
226struct dwarf2_per_objfile
227{
330cdd98
PA
228 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
229 dwarf2 section names, or is NULL if the standard ELF names are
230 used. */
231 dwarf2_per_objfile (struct objfile *objfile,
232 const dwarf2_debug_sections *names);
ae038cb0 233
330cdd98
PA
234 ~dwarf2_per_objfile ();
235
d6541620 236 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
237
238 /* Free all cached compilation units. */
239 void free_cached_comp_units ();
240private:
241 /* This function is mapped across the sections and remembers the
242 offset and size of each of the debugging sections we are
243 interested in. */
244 void locate_sections (bfd *abfd, asection *sectp,
245 const dwarf2_debug_sections &names);
246
247public:
248 dwarf2_section_info info {};
249 dwarf2_section_info abbrev {};
250 dwarf2_section_info line {};
251 dwarf2_section_info loc {};
252 dwarf2_section_info loclists {};
253 dwarf2_section_info macinfo {};
254 dwarf2_section_info macro {};
255 dwarf2_section_info str {};
256 dwarf2_section_info line_str {};
257 dwarf2_section_info ranges {};
258 dwarf2_section_info rnglists {};
259 dwarf2_section_info addr {};
260 dwarf2_section_info frame {};
261 dwarf2_section_info eh_frame {};
262 dwarf2_section_info gdb_index {};
263
264 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 265
be391dca 266 /* Back link. */
330cdd98 267 struct objfile *objfile = NULL;
be391dca 268
d467dd73 269 /* Table of all the compilation units. This is used to locate
10b3939b 270 the target compilation unit of a particular reference. */
330cdd98 271 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
272
273 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 274 int n_comp_units = 0;
ae038cb0 275
1fd400ff 276 /* The number of .debug_types-related CUs. */
330cdd98 277 int n_type_units = 0;
1fd400ff 278
6aa5f3a6
DE
279 /* The number of elements allocated in all_type_units.
280 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 281 int n_allocated_type_units = 0;
6aa5f3a6 282
a2ce51a0
DE
283 /* The .debug_types-related CUs (TUs).
284 This is stored in malloc space because we may realloc it. */
330cdd98 285 struct signatured_type **all_type_units = NULL;
1fd400ff 286
f4dc4d17
DE
287 /* Table of struct type_unit_group objects.
288 The hash key is the DW_AT_stmt_list value. */
330cdd98 289 htab_t type_unit_groups {};
72dca2f5 290
348e048f
DE
291 /* A table mapping .debug_types signatures to its signatured_type entry.
292 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 293 htab_t signatured_types {};
348e048f 294
f4dc4d17
DE
295 /* Type unit statistics, to see how well the scaling improvements
296 are doing. */
330cdd98 297 struct tu_stats tu_stats {};
f4dc4d17
DE
298
299 /* A chain of compilation units that are currently read in, so that
300 they can be freed later. */
330cdd98 301 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 302
3019eac3
DE
303 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
304 This is NULL if the table hasn't been allocated yet. */
330cdd98 305 htab_t dwo_files {};
3019eac3 306
330cdd98
PA
307 /* True if we've checked for whether there is a DWP file. */
308 bool dwp_checked = false;
80626a55
DE
309
310 /* The DWP file if there is one, or NULL. */
330cdd98 311 struct dwp_file *dwp_file = NULL;
80626a55 312
36586728
TT
313 /* The shared '.dwz' file, if one exists. This is used when the
314 original data was compressed using 'dwz -m'. */
330cdd98 315 struct dwz_file *dwz_file = NULL;
36586728 316
330cdd98 317 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 318 VMA of 0. */
330cdd98 319 bool has_section_at_zero = false;
9291a0cd 320
ae2de4f8
DE
321 /* True if we are using the mapped index,
322 or we are faking it for OBJF_READNOW's sake. */
330cdd98 323 bool using_index = false;
9291a0cd 324
ae2de4f8 325 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 326 mapped_index *index_table = NULL;
98bfdba5 327
7b9f3c50 328 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
329 TUs typically share line table entries with a CU, so we maintain a
330 separate table of all line table entries to support the sharing.
331 Note that while there can be way more TUs than CUs, we've already
332 sorted all the TUs into "type unit groups", grouped by their
333 DW_AT_stmt_list value. Therefore the only sharing done here is with a
334 CU and its associated TU group if there is one. */
330cdd98 335 htab_t quick_file_names_table {};
7b9f3c50 336
98bfdba5
PA
337 /* Set during partial symbol reading, to prevent queueing of full
338 symbols. */
330cdd98 339 bool reading_partial_symbols = false;
673bfd45 340
dee91e82 341 /* Table mapping type DIEs to their struct type *.
673bfd45 342 This is NULL if not allocated yet.
02142a6c 343 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 344 htab_t die_type_hash {};
95554aad
TT
345
346 /* The CUs we recently read. */
330cdd98 347 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
348
349 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 350 htab_t line_header_hash {};
bbf2f4df
PA
351
352 /* Table containing all filenames. This is an optional because the
353 table is lazily constructed on first access. */
354 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
355};
356
357static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 358
251d32d9 359/* Default names of the debugging sections. */
c906108c 360
233a11ab
CS
361/* Note that if the debugging section has been compressed, it might
362 have a name like .zdebug_info. */
363
9cdd5dbd
DE
364static const struct dwarf2_debug_sections dwarf2_elf_names =
365{
251d32d9
TG
366 { ".debug_info", ".zdebug_info" },
367 { ".debug_abbrev", ".zdebug_abbrev" },
368 { ".debug_line", ".zdebug_line" },
369 { ".debug_loc", ".zdebug_loc" },
43988095 370 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 371 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 372 { ".debug_macro", ".zdebug_macro" },
251d32d9 373 { ".debug_str", ".zdebug_str" },
43988095 374 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 375 { ".debug_ranges", ".zdebug_ranges" },
43988095 376 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 377 { ".debug_types", ".zdebug_types" },
3019eac3 378 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
379 { ".debug_frame", ".zdebug_frame" },
380 { ".eh_frame", NULL },
24d3216f
TT
381 { ".gdb_index", ".zgdb_index" },
382 23
251d32d9 383};
c906108c 384
80626a55 385/* List of DWO/DWP sections. */
3019eac3 386
80626a55 387static const struct dwop_section_names
3019eac3
DE
388{
389 struct dwarf2_section_names abbrev_dwo;
390 struct dwarf2_section_names info_dwo;
391 struct dwarf2_section_names line_dwo;
392 struct dwarf2_section_names loc_dwo;
43988095 393 struct dwarf2_section_names loclists_dwo;
09262596
DE
394 struct dwarf2_section_names macinfo_dwo;
395 struct dwarf2_section_names macro_dwo;
3019eac3
DE
396 struct dwarf2_section_names str_dwo;
397 struct dwarf2_section_names str_offsets_dwo;
398 struct dwarf2_section_names types_dwo;
80626a55
DE
399 struct dwarf2_section_names cu_index;
400 struct dwarf2_section_names tu_index;
3019eac3 401}
80626a55 402dwop_section_names =
3019eac3
DE
403{
404 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
405 { ".debug_info.dwo", ".zdebug_info.dwo" },
406 { ".debug_line.dwo", ".zdebug_line.dwo" },
407 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 408 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
409 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
410 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
411 { ".debug_str.dwo", ".zdebug_str.dwo" },
412 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
413 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
414 { ".debug_cu_index", ".zdebug_cu_index" },
415 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
416};
417
c906108c
SS
418/* local data types */
419
107d2387
AC
420/* The data in a compilation unit header, after target2host
421 translation, looks like this. */
c906108c 422struct comp_unit_head
a738430d 423{
c764a876 424 unsigned int length;
a738430d 425 short version;
a738430d
MK
426 unsigned char addr_size;
427 unsigned char signed_addr_p;
9c541725 428 sect_offset abbrev_sect_off;
57349743 429
a738430d
MK
430 /* Size of file offsets; either 4 or 8. */
431 unsigned int offset_size;
57349743 432
a738430d
MK
433 /* Size of the length field; either 4 or 12. */
434 unsigned int initial_length_size;
57349743 435
43988095
JK
436 enum dwarf_unit_type unit_type;
437
a738430d
MK
438 /* Offset to the first byte of this compilation unit header in the
439 .debug_info section, for resolving relative reference dies. */
9c541725 440 sect_offset sect_off;
57349743 441
d00adf39
DE
442 /* Offset to first die in this cu from the start of the cu.
443 This will be the first byte following the compilation unit header. */
9c541725 444 cu_offset first_die_cu_offset;
43988095
JK
445
446 /* 64-bit signature of this type unit - it is valid only for
447 UNIT_TYPE DW_UT_type. */
448 ULONGEST signature;
449
450 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 451 cu_offset type_cu_offset_in_tu;
a738430d 452};
c906108c 453
3da10d80
KS
454/* Type used for delaying computation of method physnames.
455 See comments for compute_delayed_physnames. */
456struct delayed_method_info
457{
458 /* The type to which the method is attached, i.e., its parent class. */
459 struct type *type;
460
461 /* The index of the method in the type's function fieldlists. */
462 int fnfield_index;
463
464 /* The index of the method in the fieldlist. */
465 int index;
466
467 /* The name of the DIE. */
468 const char *name;
469
470 /* The DIE associated with this method. */
471 struct die_info *die;
472};
473
474typedef struct delayed_method_info delayed_method_info;
475DEF_VEC_O (delayed_method_info);
476
e7c27a73
DJ
477/* Internal state when decoding a particular compilation unit. */
478struct dwarf2_cu
479{
480 /* The objfile containing this compilation unit. */
481 struct objfile *objfile;
482
d00adf39 483 /* The header of the compilation unit. */
e7c27a73 484 struct comp_unit_head header;
e142c38c 485
d00adf39
DE
486 /* Base address of this compilation unit. */
487 CORE_ADDR base_address;
488
489 /* Non-zero if base_address has been set. */
490 int base_known;
491
e142c38c
DJ
492 /* The language we are debugging. */
493 enum language language;
494 const struct language_defn *language_defn;
495
b0f35d58
DL
496 const char *producer;
497
e142c38c
DJ
498 /* The generic symbol table building routines have separate lists for
499 file scope symbols and all all other scopes (local scopes). So
500 we need to select the right one to pass to add_symbol_to_list().
501 We do it by keeping a pointer to the correct list in list_in_scope.
502
503 FIXME: The original dwarf code just treated the file scope as the
504 first local scope, and all other local scopes as nested local
505 scopes, and worked fine. Check to see if we really need to
506 distinguish these in buildsym.c. */
507 struct pending **list_in_scope;
508
433df2d4
DE
509 /* The abbrev table for this CU.
510 Normally this points to the abbrev table in the objfile.
511 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
512 struct abbrev_table *abbrev_table;
72bf9492 513
b64f50a1
JK
514 /* Hash table holding all the loaded partial DIEs
515 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
516 htab_t partial_dies;
517
518 /* Storage for things with the same lifetime as this read-in compilation
519 unit, including partial DIEs. */
520 struct obstack comp_unit_obstack;
521
ae038cb0
DJ
522 /* When multiple dwarf2_cu structures are living in memory, this field
523 chains them all together, so that they can be released efficiently.
524 We will probably also want a generation counter so that most-recently-used
525 compilation units are cached... */
526 struct dwarf2_per_cu_data *read_in_chain;
527
69d751e3 528 /* Backlink to our per_cu entry. */
ae038cb0
DJ
529 struct dwarf2_per_cu_data *per_cu;
530
531 /* How many compilation units ago was this CU last referenced? */
532 int last_used;
533
b64f50a1
JK
534 /* A hash table of DIE cu_offset for following references with
535 die_info->offset.sect_off as hash. */
51545339 536 htab_t die_hash;
10b3939b
DJ
537
538 /* Full DIEs if read in. */
539 struct die_info *dies;
540
541 /* A set of pointers to dwarf2_per_cu_data objects for compilation
542 units referenced by this one. Only set during full symbol processing;
543 partial symbol tables do not have dependencies. */
544 htab_t dependencies;
545
cb1df416
DJ
546 /* Header data from the line table, during full symbol processing. */
547 struct line_header *line_header;
4c8aa72d
PA
548 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
549 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
550 this is the DW_TAG_compile_unit die for this CU. We'll hold on
551 to the line header as long as this DIE is being processed. See
552 process_die_scope. */
553 die_info *line_header_die_owner;
cb1df416 554
3da10d80
KS
555 /* A list of methods which need to have physnames computed
556 after all type information has been read. */
557 VEC (delayed_method_info) *method_list;
558
96408a79
SA
559 /* To be copied to symtab->call_site_htab. */
560 htab_t call_site_htab;
561
034e5797
DE
562 /* Non-NULL if this CU came from a DWO file.
563 There is an invariant here that is important to remember:
564 Except for attributes copied from the top level DIE in the "main"
565 (or "stub") file in preparation for reading the DWO file
566 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
567 Either there isn't a DWO file (in which case this is NULL and the point
568 is moot), or there is and either we're not going to read it (in which
569 case this is NULL) or there is and we are reading it (in which case this
570 is non-NULL). */
3019eac3
DE
571 struct dwo_unit *dwo_unit;
572
573 /* The DW_AT_addr_base attribute if present, zero otherwise
574 (zero is a valid value though).
1dbab08b 575 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
576 ULONGEST addr_base;
577
2e3cf129
DE
578 /* The DW_AT_ranges_base attribute if present, zero otherwise
579 (zero is a valid value though).
1dbab08b 580 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 581 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
582 be used without needing to know whether DWO files are in use or not.
583 N.B. This does not apply to DW_AT_ranges appearing in
584 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
585 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
586 DW_AT_ranges_base *would* have to be applied, and we'd have to care
587 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
588 ULONGEST ranges_base;
589
ae038cb0
DJ
590 /* Mark used when releasing cached dies. */
591 unsigned int mark : 1;
592
8be455d7
JK
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 597 unsigned int has_loclist : 1;
ba919b58 598
1b80a9fa
JK
599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
ba919b58
TT
603 unsigned int checked_producer : 1;
604 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 605 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 606 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
607
608 /* When set, the file that we're processing is known to have
609 debugging info for C++ namespaces. GCC 3.3.x did not produce
610 this information, but later versions do. */
611
612 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
613};
614
10b3939b
DJ
615/* Persistent data held for a compilation unit, even when not
616 processing it. We put a pointer to this structure in the
28dee7f5 617 read_symtab_private field of the psymtab. */
10b3939b 618
ae038cb0
DJ
619struct dwarf2_per_cu_data
620{
36586728 621 /* The start offset and length of this compilation unit.
45452591 622 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
623 initial_length_size.
624 If the DIE refers to a DWO file, this is always of the original die,
625 not the DWO file. */
9c541725 626 sect_offset sect_off;
36586728 627 unsigned int length;
ae038cb0 628
43988095
JK
629 /* DWARF standard version this data has been read from (such as 4 or 5). */
630 short dwarf_version;
631
ae038cb0
DJ
632 /* Flag indicating this compilation unit will be read in before
633 any of the current compilation units are processed. */
c764a876 634 unsigned int queued : 1;
ae038cb0 635
0d99eb77
DE
636 /* This flag will be set when reading partial DIEs if we need to load
637 absolutely all DIEs for this compilation unit, instead of just the ones
638 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
639 hash table and don't find it. */
640 unsigned int load_all_dies : 1;
641
0186c6a7
DE
642 /* Non-zero if this CU is from .debug_types.
643 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
644 this is non-zero. */
3019eac3
DE
645 unsigned int is_debug_types : 1;
646
36586728
TT
647 /* Non-zero if this CU is from the .dwz file. */
648 unsigned int is_dwz : 1;
649
a2ce51a0
DE
650 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
651 This flag is only valid if is_debug_types is true.
652 We can't read a CU directly from a DWO file: There are required
653 attributes in the stub. */
654 unsigned int reading_dwo_directly : 1;
655
7ee85ab1
DE
656 /* Non-zero if the TU has been read.
657 This is used to assist the "Stay in DWO Optimization" for Fission:
658 When reading a DWO, it's faster to read TUs from the DWO instead of
659 fetching them from random other DWOs (due to comdat folding).
660 If the TU has already been read, the optimization is unnecessary
661 (and unwise - we don't want to change where gdb thinks the TU lives
662 "midflight").
663 This flag is only valid if is_debug_types is true. */
664 unsigned int tu_read : 1;
665
3019eac3
DE
666 /* The section this CU/TU lives in.
667 If the DIE refers to a DWO file, this is always the original die,
668 not the DWO file. */
8a0459fd 669 struct dwarf2_section_info *section;
348e048f 670
17ea53c3 671 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
672 of the CU cache it gets reset to NULL again. This is left as NULL for
673 dummy CUs (a CU header, but nothing else). */
ae038cb0 674 struct dwarf2_cu *cu;
1c379e20 675
9cdd5dbd
DE
676 /* The corresponding objfile.
677 Normally we can get the objfile from dwarf2_per_objfile.
678 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
679 struct objfile *objfile;
680
fffbe6a8
YQ
681 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
682 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
683 union
684 {
685 /* The partial symbol table associated with this compilation unit,
95554aad 686 or NULL for unread partial units. */
9291a0cd
TT
687 struct partial_symtab *psymtab;
688
689 /* Data needed by the "quick" functions. */
690 struct dwarf2_per_cu_quick_data *quick;
691 } v;
95554aad 692
796a7ff8
DE
693 /* The CUs we import using DW_TAG_imported_unit. This is filled in
694 while reading psymtabs, used to compute the psymtab dependencies,
695 and then cleared. Then it is filled in again while reading full
696 symbols, and only deleted when the objfile is destroyed.
697
698 This is also used to work around a difference between the way gold
699 generates .gdb_index version <=7 and the way gdb does. Arguably this
700 is a gold bug. For symbols coming from TUs, gold records in the index
701 the CU that includes the TU instead of the TU itself. This breaks
702 dw2_lookup_symbol: It assumes that if the index says symbol X lives
703 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
704 will find X. Alas TUs live in their own symtab, so after expanding CU Y
705 we need to look in TU Z to find X. Fortunately, this is akin to
706 DW_TAG_imported_unit, so we just use the same mechanism: For
707 .gdb_index version <=7 this also records the TUs that the CU referred
708 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
709 indices so we only pay a price for gold generated indices.
710 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 711 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
712};
713
348e048f
DE
714/* Entry in the signatured_types hash table. */
715
716struct signatured_type
717{
42e7ad6c 718 /* The "per_cu" object of this type.
ac9ec31b 719 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
720 N.B.: This is the first member so that it's easy to convert pointers
721 between them. */
722 struct dwarf2_per_cu_data per_cu;
723
3019eac3 724 /* The type's signature. */
348e048f
DE
725 ULONGEST signature;
726
3019eac3 727 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
728 If this TU is a DWO stub and the definition lives in a DWO file
729 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
730 cu_offset type_offset_in_tu;
731
732 /* Offset in the section of the type's DIE.
733 If the definition lives in a DWO file, this is the offset in the
734 .debug_types.dwo section.
735 The value is zero until the actual value is known.
736 Zero is otherwise not a valid section offset. */
737 sect_offset type_offset_in_section;
0186c6a7
DE
738
739 /* Type units are grouped by their DW_AT_stmt_list entry so that they
740 can share them. This points to the containing symtab. */
741 struct type_unit_group *type_unit_group;
ac9ec31b
DE
742
743 /* The type.
744 The first time we encounter this type we fully read it in and install it
745 in the symbol tables. Subsequent times we only need the type. */
746 struct type *type;
a2ce51a0
DE
747
748 /* Containing DWO unit.
749 This field is valid iff per_cu.reading_dwo_directly. */
750 struct dwo_unit *dwo_unit;
348e048f
DE
751};
752
0186c6a7
DE
753typedef struct signatured_type *sig_type_ptr;
754DEF_VEC_P (sig_type_ptr);
755
094b34ac
DE
756/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
757 This includes type_unit_group and quick_file_names. */
758
759struct stmt_list_hash
760{
761 /* The DWO unit this table is from or NULL if there is none. */
762 struct dwo_unit *dwo_unit;
763
764 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 765 sect_offset line_sect_off;
094b34ac
DE
766};
767
f4dc4d17
DE
768/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
769 an object of this type. */
770
771struct type_unit_group
772{
0186c6a7 773 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
774 To simplify things we create an artificial CU that "includes" all the
775 type units using this stmt_list so that the rest of the code still has
776 a "per_cu" handle on the symtab.
777 This PER_CU is recognized by having no section. */
8a0459fd 778#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
779 struct dwarf2_per_cu_data per_cu;
780
0186c6a7
DE
781 /* The TUs that share this DW_AT_stmt_list entry.
782 This is added to while parsing type units to build partial symtabs,
783 and is deleted afterwards and not used again. */
784 VEC (sig_type_ptr) *tus;
f4dc4d17 785
43f3e411 786 /* The compunit symtab.
094b34ac 787 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
788 so we create an essentially anonymous symtab as the compunit symtab. */
789 struct compunit_symtab *compunit_symtab;
f4dc4d17 790
094b34ac
DE
791 /* The data used to construct the hash key. */
792 struct stmt_list_hash hash;
f4dc4d17
DE
793
794 /* The number of symtabs from the line header.
795 The value here must match line_header.num_file_names. */
796 unsigned int num_symtabs;
797
798 /* The symbol tables for this TU (obtained from the files listed in
799 DW_AT_stmt_list).
800 WARNING: The order of entries here must match the order of entries
801 in the line header. After the first TU using this type_unit_group, the
802 line header for the subsequent TUs is recreated from this. This is done
803 because we need to use the same symtabs for each TU using the same
804 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
805 there's no guarantee the line header doesn't have duplicate entries. */
806 struct symtab **symtabs;
807};
808
73869dc2 809/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
810
811struct dwo_sections
812{
813 struct dwarf2_section_info abbrev;
3019eac3
DE
814 struct dwarf2_section_info line;
815 struct dwarf2_section_info loc;
43988095 816 struct dwarf2_section_info loclists;
09262596
DE
817 struct dwarf2_section_info macinfo;
818 struct dwarf2_section_info macro;
3019eac3
DE
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info str_offsets;
80626a55
DE
821 /* In the case of a virtual DWO file, these two are unused. */
822 struct dwarf2_section_info info;
3019eac3
DE
823 VEC (dwarf2_section_info_def) *types;
824};
825
c88ee1f0 826/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
827
828struct dwo_unit
829{
830 /* Backlink to the containing struct dwo_file. */
831 struct dwo_file *dwo_file;
832
833 /* The "id" that distinguishes this CU/TU.
834 .debug_info calls this "dwo_id", .debug_types calls this "signature".
835 Since signatures came first, we stick with it for consistency. */
836 ULONGEST signature;
837
838 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 839 struct dwarf2_section_info *section;
3019eac3 840
9c541725
PA
841 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
842 sect_offset sect_off;
3019eac3
DE
843 unsigned int length;
844
845 /* For types, offset in the type's DIE of the type defined by this TU. */
846 cu_offset type_offset_in_tu;
847};
848
73869dc2
DE
849/* include/dwarf2.h defines the DWP section codes.
850 It defines a max value but it doesn't define a min value, which we
851 use for error checking, so provide one. */
852
853enum dwp_v2_section_ids
854{
855 DW_SECT_MIN = 1
856};
857
80626a55 858/* Data for one DWO file.
57d63ce2
DE
859
860 This includes virtual DWO files (a virtual DWO file is a DWO file as it
861 appears in a DWP file). DWP files don't really have DWO files per se -
862 comdat folding of types "loses" the DWO file they came from, and from
863 a high level view DWP files appear to contain a mass of random types.
864 However, to maintain consistency with the non-DWP case we pretend DWP
865 files contain virtual DWO files, and we assign each TU with one virtual
866 DWO file (generally based on the line and abbrev section offsets -
867 a heuristic that seems to work in practice). */
3019eac3
DE
868
869struct dwo_file
870{
0ac5b59e 871 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
872 For virtual DWO files the name is constructed from the section offsets
873 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
874 from related CU+TUs. */
0ac5b59e
DE
875 const char *dwo_name;
876
877 /* The DW_AT_comp_dir attribute. */
878 const char *comp_dir;
3019eac3 879
80626a55
DE
880 /* The bfd, when the file is open. Otherwise this is NULL.
881 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
882 bfd *dbfd;
3019eac3 883
73869dc2
DE
884 /* The sections that make up this DWO file.
885 Remember that for virtual DWO files in DWP V2, these are virtual
886 sections (for lack of a better name). */
3019eac3
DE
887 struct dwo_sections sections;
888
33c5cd75
DB
889 /* The CUs in the file.
890 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
891 an extension to handle LLVM's Link Time Optimization output (where
892 multiple source files may be compiled into a single object/dwo pair). */
893 htab_t cus;
3019eac3
DE
894
895 /* Table of TUs in the file.
896 Each element is a struct dwo_unit. */
897 htab_t tus;
898};
899
80626a55
DE
900/* These sections are what may appear in a DWP file. */
901
902struct dwp_sections
903{
73869dc2 904 /* These are used by both DWP version 1 and 2. */
80626a55
DE
905 struct dwarf2_section_info str;
906 struct dwarf2_section_info cu_index;
907 struct dwarf2_section_info tu_index;
73869dc2
DE
908
909 /* These are only used by DWP version 2 files.
910 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
911 sections are referenced by section number, and are not recorded here.
912 In DWP version 2 there is at most one copy of all these sections, each
913 section being (effectively) comprised of the concatenation of all of the
914 individual sections that exist in the version 1 format.
915 To keep the code simple we treat each of these concatenated pieces as a
916 section itself (a virtual section?). */
917 struct dwarf2_section_info abbrev;
918 struct dwarf2_section_info info;
919 struct dwarf2_section_info line;
920 struct dwarf2_section_info loc;
921 struct dwarf2_section_info macinfo;
922 struct dwarf2_section_info macro;
923 struct dwarf2_section_info str_offsets;
924 struct dwarf2_section_info types;
80626a55
DE
925};
926
73869dc2
DE
927/* These sections are what may appear in a virtual DWO file in DWP version 1.
928 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 929
73869dc2 930struct virtual_v1_dwo_sections
80626a55
DE
931{
932 struct dwarf2_section_info abbrev;
933 struct dwarf2_section_info line;
934 struct dwarf2_section_info loc;
935 struct dwarf2_section_info macinfo;
936 struct dwarf2_section_info macro;
937 struct dwarf2_section_info str_offsets;
938 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 939 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
940 struct dwarf2_section_info info_or_types;
941};
942
73869dc2
DE
943/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
944 In version 2, the sections of the DWO files are concatenated together
945 and stored in one section of that name. Thus each ELF section contains
946 several "virtual" sections. */
947
948struct virtual_v2_dwo_sections
949{
950 bfd_size_type abbrev_offset;
951 bfd_size_type abbrev_size;
952
953 bfd_size_type line_offset;
954 bfd_size_type line_size;
955
956 bfd_size_type loc_offset;
957 bfd_size_type loc_size;
958
959 bfd_size_type macinfo_offset;
960 bfd_size_type macinfo_size;
961
962 bfd_size_type macro_offset;
963 bfd_size_type macro_size;
964
965 bfd_size_type str_offsets_offset;
966 bfd_size_type str_offsets_size;
967
968 /* Each DWP hash table entry records one CU or one TU.
969 That is recorded here, and copied to dwo_unit.section. */
970 bfd_size_type info_or_types_offset;
971 bfd_size_type info_or_types_size;
972};
973
80626a55
DE
974/* Contents of DWP hash tables. */
975
976struct dwp_hash_table
977{
73869dc2 978 uint32_t version, nr_columns;
80626a55 979 uint32_t nr_units, nr_slots;
73869dc2
DE
980 const gdb_byte *hash_table, *unit_table;
981 union
982 {
983 struct
984 {
985 const gdb_byte *indices;
986 } v1;
987 struct
988 {
989 /* This is indexed by column number and gives the id of the section
990 in that column. */
991#define MAX_NR_V2_DWO_SECTIONS \
992 (1 /* .debug_info or .debug_types */ \
993 + 1 /* .debug_abbrev */ \
994 + 1 /* .debug_line */ \
995 + 1 /* .debug_loc */ \
996 + 1 /* .debug_str_offsets */ \
997 + 1 /* .debug_macro or .debug_macinfo */)
998 int section_ids[MAX_NR_V2_DWO_SECTIONS];
999 const gdb_byte *offsets;
1000 const gdb_byte *sizes;
1001 } v2;
1002 } section_pool;
80626a55
DE
1003};
1004
1005/* Data for one DWP file. */
1006
1007struct dwp_file
1008{
1009 /* Name of the file. */
1010 const char *name;
1011
73869dc2
DE
1012 /* File format version. */
1013 int version;
1014
93417882 1015 /* The bfd. */
80626a55
DE
1016 bfd *dbfd;
1017
1018 /* Section info for this file. */
1019 struct dwp_sections sections;
1020
57d63ce2 1021 /* Table of CUs in the file. */
80626a55
DE
1022 const struct dwp_hash_table *cus;
1023
1024 /* Table of TUs in the file. */
1025 const struct dwp_hash_table *tus;
1026
19ac8c2e
DE
1027 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1028 htab_t loaded_cus;
1029 htab_t loaded_tus;
80626a55 1030
73869dc2
DE
1031 /* Table to map ELF section numbers to their sections.
1032 This is only needed for the DWP V1 file format. */
80626a55
DE
1033 unsigned int num_sections;
1034 asection **elf_sections;
1035};
1036
36586728
TT
1037/* This represents a '.dwz' file. */
1038
1039struct dwz_file
1040{
1041 /* A dwz file can only contain a few sections. */
1042 struct dwarf2_section_info abbrev;
1043 struct dwarf2_section_info info;
1044 struct dwarf2_section_info str;
1045 struct dwarf2_section_info line;
1046 struct dwarf2_section_info macro;
2ec9a5e0 1047 struct dwarf2_section_info gdb_index;
36586728
TT
1048
1049 /* The dwz's BFD. */
1050 bfd *dwz_bfd;
1051};
1052
0963b4bd
MS
1053/* Struct used to pass misc. parameters to read_die_and_children, et
1054 al. which are used for both .debug_info and .debug_types dies.
1055 All parameters here are unchanging for the life of the call. This
dee91e82 1056 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1057
1058struct die_reader_specs
1059{
a32a8923 1060 /* The bfd of die_section. */
93311388
DE
1061 bfd* abfd;
1062
1063 /* The CU of the DIE we are parsing. */
1064 struct dwarf2_cu *cu;
1065
80626a55 1066 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1067 struct dwo_file *dwo_file;
1068
dee91e82 1069 /* The section the die comes from.
3019eac3 1070 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1071 struct dwarf2_section_info *die_section;
1072
1073 /* die_section->buffer. */
d521ce57 1074 const gdb_byte *buffer;
f664829e
DE
1075
1076 /* The end of the buffer. */
1077 const gdb_byte *buffer_end;
a2ce51a0
DE
1078
1079 /* The value of the DW_AT_comp_dir attribute. */
1080 const char *comp_dir;
93311388
DE
1081};
1082
fd820528 1083/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1084typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1085 const gdb_byte *info_ptr,
dee91e82
DE
1086 struct die_info *comp_unit_die,
1087 int has_children,
1088 void *data);
1089
ecfb656c
PA
1090/* A 1-based directory index. This is a strong typedef to prevent
1091 accidentally using a directory index as a 0-based index into an
1092 array/vector. */
1093enum class dir_index : unsigned int {};
1094
1095/* Likewise, a 1-based file name index. */
1096enum class file_name_index : unsigned int {};
1097
52059ffd
TT
1098struct file_entry
1099{
fff8551c
PA
1100 file_entry () = default;
1101
ecfb656c 1102 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1103 unsigned int mod_time_, unsigned int length_)
1104 : name (name_),
ecfb656c 1105 d_index (d_index_),
fff8551c
PA
1106 mod_time (mod_time_),
1107 length (length_)
1108 {}
1109
ecfb656c
PA
1110 /* Return the include directory at D_INDEX stored in LH. Returns
1111 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1112 const char *include_dir (const line_header *lh) const;
1113
fff8551c
PA
1114 /* The file name. Note this is an observing pointer. The memory is
1115 owned by debug_line_buffer. */
1116 const char *name {};
1117
8c43009f 1118 /* The directory index (1-based). */
ecfb656c 1119 dir_index d_index {};
fff8551c
PA
1120
1121 unsigned int mod_time {};
1122
1123 unsigned int length {};
1124
1125 /* True if referenced by the Line Number Program. */
1126 bool included_p {};
1127
83769d0b 1128 /* The associated symbol table, if any. */
fff8551c 1129 struct symtab *symtab {};
52059ffd
TT
1130};
1131
debd256d
JB
1132/* The line number information for a compilation unit (found in the
1133 .debug_line section) begins with a "statement program header",
1134 which contains the following information. */
1135struct line_header
1136{
fff8551c
PA
1137 line_header ()
1138 : offset_in_dwz {}
1139 {}
1140
1141 /* Add an entry to the include directory table. */
1142 void add_include_dir (const char *include_dir);
1143
1144 /* Add an entry to the file name table. */
ecfb656c 1145 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1146 unsigned int mod_time, unsigned int length);
1147
ecfb656c 1148 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1149 is out of bounds. */
ecfb656c 1150 const char *include_dir_at (dir_index index) const
8c43009f 1151 {
ecfb656c
PA
1152 /* Convert directory index number (1-based) to vector index
1153 (0-based). */
1154 size_t vec_index = to_underlying (index) - 1;
1155
1156 if (vec_index >= include_dirs.size ())
8c43009f 1157 return NULL;
ecfb656c 1158 return include_dirs[vec_index];
8c43009f
PA
1159 }
1160
ecfb656c 1161 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1162 is out of bounds. */
ecfb656c 1163 file_entry *file_name_at (file_name_index index)
8c43009f 1164 {
ecfb656c
PA
1165 /* Convert file name index number (1-based) to vector index
1166 (0-based). */
1167 size_t vec_index = to_underlying (index) - 1;
1168
1169 if (vec_index >= file_names.size ())
fff8551c 1170 return NULL;
ecfb656c 1171 return &file_names[vec_index];
fff8551c
PA
1172 }
1173
1174 /* Const version of the above. */
1175 const file_entry *file_name_at (unsigned int index) const
1176 {
1177 if (index >= file_names.size ())
8c43009f
PA
1178 return NULL;
1179 return &file_names[index];
1180 }
1181
527f3840 1182 /* Offset of line number information in .debug_line section. */
9c541725 1183 sect_offset sect_off {};
527f3840
JK
1184
1185 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1186 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1187
1188 unsigned int total_length {};
1189 unsigned short version {};
1190 unsigned int header_length {};
1191 unsigned char minimum_instruction_length {};
1192 unsigned char maximum_ops_per_instruction {};
1193 unsigned char default_is_stmt {};
1194 int line_base {};
1195 unsigned char line_range {};
1196 unsigned char opcode_base {};
debd256d
JB
1197
1198 /* standard_opcode_lengths[i] is the number of operands for the
1199 standard opcode whose value is i. This means that
1200 standard_opcode_lengths[0] is unused, and the last meaningful
1201 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1202 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1203
fff8551c
PA
1204 /* The include_directories table. Note these are observing
1205 pointers. The memory is owned by debug_line_buffer. */
1206 std::vector<const char *> include_dirs;
debd256d 1207
fff8551c
PA
1208 /* The file_names table. */
1209 std::vector<file_entry> file_names;
debd256d
JB
1210
1211 /* The start and end of the statement program following this
6502dd73 1212 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1213 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1214};
c906108c 1215
fff8551c
PA
1216typedef std::unique_ptr<line_header> line_header_up;
1217
8c43009f
PA
1218const char *
1219file_entry::include_dir (const line_header *lh) const
1220{
ecfb656c 1221 return lh->include_dir_at (d_index);
8c43009f
PA
1222}
1223
c906108c 1224/* When we construct a partial symbol table entry we only
0963b4bd 1225 need this much information. */
c906108c
SS
1226struct partial_die_info
1227 {
72bf9492 1228 /* Offset of this DIE. */
9c541725 1229 sect_offset sect_off;
72bf9492
DJ
1230
1231 /* DWARF-2 tag for this DIE. */
1232 ENUM_BITFIELD(dwarf_tag) tag : 16;
1233
72bf9492
DJ
1234 /* Assorted flags describing the data found in this DIE. */
1235 unsigned int has_children : 1;
1236 unsigned int is_external : 1;
1237 unsigned int is_declaration : 1;
1238 unsigned int has_type : 1;
1239 unsigned int has_specification : 1;
1240 unsigned int has_pc_info : 1;
481860b3 1241 unsigned int may_be_inlined : 1;
72bf9492 1242
0c1b455e
TT
1243 /* This DIE has been marked DW_AT_main_subprogram. */
1244 unsigned int main_subprogram : 1;
1245
72bf9492
DJ
1246 /* Flag set if the SCOPE field of this structure has been
1247 computed. */
1248 unsigned int scope_set : 1;
1249
fa4028e9
JB
1250 /* Flag set if the DIE has a byte_size attribute. */
1251 unsigned int has_byte_size : 1;
1252
ff908ebf
AW
1253 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1254 unsigned int has_const_value : 1;
1255
98bfdba5
PA
1256 /* Flag set if any of the DIE's children are template arguments. */
1257 unsigned int has_template_arguments : 1;
1258
abc72ce4
DE
1259 /* Flag set if fixup_partial_die has been called on this die. */
1260 unsigned int fixup_called : 1;
1261
36586728
TT
1262 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1263 unsigned int is_dwz : 1;
1264
1265 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1266 unsigned int spec_is_dwz : 1;
1267
72bf9492 1268 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1269 sometimes a default name for unnamed DIEs. */
15d034d0 1270 const char *name;
72bf9492 1271
abc72ce4
DE
1272 /* The linkage name, if present. */
1273 const char *linkage_name;
1274
72bf9492
DJ
1275 /* The scope to prepend to our children. This is generally
1276 allocated on the comp_unit_obstack, so will disappear
1277 when this compilation unit leaves the cache. */
15d034d0 1278 const char *scope;
72bf9492 1279
95554aad
TT
1280 /* Some data associated with the partial DIE. The tag determines
1281 which field is live. */
1282 union
1283 {
1284 /* The location description associated with this DIE, if any. */
1285 struct dwarf_block *locdesc;
1286 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1287 sect_offset sect_off;
95554aad 1288 } d;
72bf9492
DJ
1289
1290 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1291 CORE_ADDR lowpc;
1292 CORE_ADDR highpc;
72bf9492 1293
93311388 1294 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1295 DW_AT_sibling, if any. */
abc72ce4
DE
1296 /* NOTE: This member isn't strictly necessary, read_partial_die could
1297 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1298 const gdb_byte *sibling;
72bf9492
DJ
1299
1300 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1301 DW_AT_specification (or DW_AT_abstract_origin or
1302 DW_AT_extension). */
b64f50a1 1303 sect_offset spec_offset;
72bf9492
DJ
1304
1305 /* Pointers to this DIE's parent, first child, and next sibling,
1306 if any. */
1307 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1308 };
1309
0963b4bd 1310/* This data structure holds the information of an abbrev. */
c906108c
SS
1311struct abbrev_info
1312 {
1313 unsigned int number; /* number identifying abbrev */
1314 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1315 unsigned short has_children; /* boolean */
1316 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1317 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1318 struct abbrev_info *next; /* next in chain */
1319 };
1320
1321struct attr_abbrev
1322 {
9d25dd43
DE
1323 ENUM_BITFIELD(dwarf_attribute) name : 16;
1324 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1325
1326 /* It is valid only if FORM is DW_FORM_implicit_const. */
1327 LONGEST implicit_const;
c906108c
SS
1328 };
1329
433df2d4
DE
1330/* Size of abbrev_table.abbrev_hash_table. */
1331#define ABBREV_HASH_SIZE 121
1332
1333/* Top level data structure to contain an abbreviation table. */
1334
1335struct abbrev_table
1336{
f4dc4d17
DE
1337 /* Where the abbrev table came from.
1338 This is used as a sanity check when the table is used. */
9c541725 1339 sect_offset sect_off;
433df2d4
DE
1340
1341 /* Storage for the abbrev table. */
1342 struct obstack abbrev_obstack;
1343
1344 /* Hash table of abbrevs.
1345 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1346 It could be statically allocated, but the previous code didn't so we
1347 don't either. */
1348 struct abbrev_info **abbrevs;
1349};
1350
0963b4bd 1351/* Attributes have a name and a value. */
b60c80d6
DJ
1352struct attribute
1353 {
9d25dd43 1354 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1355 ENUM_BITFIELD(dwarf_form) form : 15;
1356
1357 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1358 field should be in u.str (existing only for DW_STRING) but it is kept
1359 here for better struct attribute alignment. */
1360 unsigned int string_is_canonical : 1;
1361
b60c80d6
DJ
1362 union
1363 {
15d034d0 1364 const char *str;
b60c80d6 1365 struct dwarf_block *blk;
43bbcdc2
PH
1366 ULONGEST unsnd;
1367 LONGEST snd;
b60c80d6 1368 CORE_ADDR addr;
ac9ec31b 1369 ULONGEST signature;
b60c80d6
DJ
1370 }
1371 u;
1372 };
1373
0963b4bd 1374/* This data structure holds a complete die structure. */
c906108c
SS
1375struct die_info
1376 {
76815b17
DE
1377 /* DWARF-2 tag for this DIE. */
1378 ENUM_BITFIELD(dwarf_tag) tag : 16;
1379
1380 /* Number of attributes */
98bfdba5
PA
1381 unsigned char num_attrs;
1382
1383 /* True if we're presently building the full type name for the
1384 type derived from this DIE. */
1385 unsigned char building_fullname : 1;
76815b17 1386
adde2bff
DE
1387 /* True if this die is in process. PR 16581. */
1388 unsigned char in_process : 1;
1389
76815b17
DE
1390 /* Abbrev number */
1391 unsigned int abbrev;
1392
93311388 1393 /* Offset in .debug_info or .debug_types section. */
9c541725 1394 sect_offset sect_off;
78ba4af6
JB
1395
1396 /* The dies in a compilation unit form an n-ary tree. PARENT
1397 points to this die's parent; CHILD points to the first child of
1398 this node; and all the children of a given node are chained
4950bc1c 1399 together via their SIBLING fields. */
639d11d3
DC
1400 struct die_info *child; /* Its first child, if any. */
1401 struct die_info *sibling; /* Its next sibling, if any. */
1402 struct die_info *parent; /* Its parent, if any. */
c906108c 1403
b60c80d6
DJ
1404 /* An array of attributes, with NUM_ATTRS elements. There may be
1405 zero, but it's not common and zero-sized arrays are not
1406 sufficiently portable C. */
1407 struct attribute attrs[1];
c906108c
SS
1408 };
1409
0963b4bd 1410/* Get at parts of an attribute structure. */
c906108c
SS
1411
1412#define DW_STRING(attr) ((attr)->u.str)
8285870a 1413#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1414#define DW_UNSND(attr) ((attr)->u.unsnd)
1415#define DW_BLOCK(attr) ((attr)->u.blk)
1416#define DW_SND(attr) ((attr)->u.snd)
1417#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1418#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1419
0963b4bd 1420/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1421struct dwarf_block
1422 {
56eb65bd 1423 size_t size;
1d6edc3c
JK
1424
1425 /* Valid only if SIZE is not zero. */
d521ce57 1426 const gdb_byte *data;
c906108c
SS
1427 };
1428
c906108c
SS
1429#ifndef ATTR_ALLOC_CHUNK
1430#define ATTR_ALLOC_CHUNK 4
1431#endif
1432
c906108c
SS
1433/* Allocate fields for structs, unions and enums in this size. */
1434#ifndef DW_FIELD_ALLOC_CHUNK
1435#define DW_FIELD_ALLOC_CHUNK 4
1436#endif
1437
c906108c
SS
1438/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1439 but this would require a corresponding change in unpack_field_as_long
1440 and friends. */
1441static int bits_per_byte = 8;
1442
52059ffd
TT
1443struct nextfield
1444{
1445 struct nextfield *next;
1446 int accessibility;
1447 int virtuality;
1448 struct field field;
1449};
1450
1451struct nextfnfield
1452{
1453 struct nextfnfield *next;
1454 struct fn_field fnfield;
1455};
1456
1457struct fnfieldlist
1458{
1459 const char *name;
1460 int length;
1461 struct nextfnfield *head;
1462};
1463
1464struct typedef_field_list
1465{
1466 struct typedef_field field;
1467 struct typedef_field_list *next;
1468};
1469
c906108c
SS
1470/* The routines that read and process dies for a C struct or C++ class
1471 pass lists of data member fields and lists of member function fields
1472 in an instance of a field_info structure, as defined below. */
1473struct field_info
c5aa993b 1474 {
0963b4bd 1475 /* List of data member and baseclasses fields. */
52059ffd 1476 struct nextfield *fields, *baseclasses;
c906108c 1477
7d0ccb61 1478 /* Number of fields (including baseclasses). */
c5aa993b 1479 int nfields;
c906108c 1480
c5aa993b
JM
1481 /* Number of baseclasses. */
1482 int nbaseclasses;
c906108c 1483
c5aa993b
JM
1484 /* Set if the accesibility of one of the fields is not public. */
1485 int non_public_fields;
c906108c 1486
c5aa993b
JM
1487 /* Member function fieldlist array, contains name of possibly overloaded
1488 member function, number of overloaded member functions and a pointer
1489 to the head of the member function field chain. */
52059ffd 1490 struct fnfieldlist *fnfieldlists;
c906108c 1491
c5aa993b
JM
1492 /* Number of entries in the fnfieldlists array. */
1493 int nfnfields;
98751a41
JK
1494
1495 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1496 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1497 struct typedef_field_list *typedef_field_list;
98751a41 1498 unsigned typedef_field_list_count;
c5aa993b 1499 };
c906108c 1500
10b3939b
DJ
1501/* One item on the queue of compilation units to read in full symbols
1502 for. */
1503struct dwarf2_queue_item
1504{
1505 struct dwarf2_per_cu_data *per_cu;
95554aad 1506 enum language pretend_language;
10b3939b
DJ
1507 struct dwarf2_queue_item *next;
1508};
1509
1510/* The current queue. */
1511static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1512
ae038cb0
DJ
1513/* Loaded secondary compilation units are kept in memory until they
1514 have not been referenced for the processing of this many
1515 compilation units. Set this to zero to disable caching. Cache
1516 sizes of up to at least twenty will improve startup time for
1517 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1518static int dwarf_max_cache_age = 5;
920d2a44 1519static void
b4f54984
DE
1520show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1521 struct cmd_list_element *c, const char *value)
920d2a44 1522{
3e43a32a 1523 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1524 "DWARF compilation units is %s.\n"),
920d2a44
AC
1525 value);
1526}
4390d890 1527\f
c906108c
SS
1528/* local function prototypes */
1529
a32a8923
DE
1530static const char *get_section_name (const struct dwarf2_section_info *);
1531
1532static const char *get_section_file_name (const struct dwarf2_section_info *);
1533
918dd910
JK
1534static void dwarf2_find_base_address (struct die_info *die,
1535 struct dwarf2_cu *cu);
1536
0018ea6f
DE
1537static struct partial_symtab *create_partial_symtab
1538 (struct dwarf2_per_cu_data *per_cu, const char *name);
1539
f1902523
JK
1540static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct die_info *type_unit_die,
1543 int has_children, void *data);
1544
c67a9c90 1545static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1546
72bf9492
DJ
1547static void scan_partial_symbols (struct partial_die_info *,
1548 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1549 int, struct dwarf2_cu *);
c906108c 1550
72bf9492
DJ
1551static void add_partial_symbol (struct partial_die_info *,
1552 struct dwarf2_cu *);
63d06c5c 1553
72bf9492
DJ
1554static void add_partial_namespace (struct partial_die_info *pdi,
1555 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1556 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1557
5d7cb8df 1558static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1559 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1560 struct dwarf2_cu *cu);
1561
72bf9492
DJ
1562static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1563 struct dwarf2_cu *cu);
91c24f0a 1564
bc30ff58
JB
1565static void add_partial_subprogram (struct partial_die_info *pdi,
1566 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1567 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1568
257e7a09
YQ
1569static void dwarf2_read_symtab (struct partial_symtab *,
1570 struct objfile *);
c906108c 1571
a14ed312 1572static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1573
433df2d4
DE
1574static struct abbrev_info *abbrev_table_lookup_abbrev
1575 (const struct abbrev_table *, unsigned int);
1576
1577static struct abbrev_table *abbrev_table_read_table
1578 (struct dwarf2_section_info *, sect_offset);
1579
1580static void abbrev_table_free (struct abbrev_table *);
1581
f4dc4d17
DE
1582static void abbrev_table_free_cleanup (void *);
1583
dee91e82
DE
1584static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1585 struct dwarf2_section_info *);
c906108c 1586
f3dd6933 1587static void dwarf2_free_abbrev_table (void *);
c906108c 1588
d521ce57 1589static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1590
dee91e82 1591static struct partial_die_info *load_partial_dies
d521ce57 1592 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1593
d521ce57
TT
1594static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1595 struct partial_die_info *,
1596 struct abbrev_info *,
1597 unsigned int,
1598 const gdb_byte *);
c906108c 1599
36586728 1600static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1601 struct dwarf2_cu *);
72bf9492
DJ
1602
1603static void fixup_partial_die (struct partial_die_info *,
1604 struct dwarf2_cu *);
1605
d521ce57
TT
1606static const gdb_byte *read_attribute (const struct die_reader_specs *,
1607 struct attribute *, struct attr_abbrev *,
1608 const gdb_byte *);
a8329558 1609
a1855c1d 1610static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1611
a1855c1d 1612static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1613
a1855c1d 1614static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1615
a1855c1d 1616static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1617
a1855c1d 1618static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1619
d521ce57 1620static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1621 unsigned int *);
c906108c 1622
d521ce57 1623static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1624
1625static LONGEST read_checked_initial_length_and_offset
d521ce57 1626 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1627 unsigned int *, unsigned int *);
613e1657 1628
d521ce57
TT
1629static LONGEST read_offset (bfd *, const gdb_byte *,
1630 const struct comp_unit_head *,
c764a876
DE
1631 unsigned int *);
1632
d521ce57 1633static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1634
f4dc4d17
DE
1635static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1636 sect_offset);
1637
d521ce57 1638static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1639
d521ce57 1640static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1641
d521ce57
TT
1642static const char *read_indirect_string (bfd *, const gdb_byte *,
1643 const struct comp_unit_head *,
1644 unsigned int *);
4bdf3d34 1645
43988095
JK
1646static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1647 const struct comp_unit_head *,
1648 unsigned int *);
36586728 1649
43988095 1650static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1651
d521ce57 1652static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1653
d521ce57
TT
1654static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1655 const gdb_byte *,
3019eac3
DE
1656 unsigned int *);
1657
d521ce57 1658static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1659 ULONGEST str_index);
3019eac3 1660
e142c38c 1661static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1662
e142c38c
DJ
1663static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1664 struct dwarf2_cu *);
c906108c 1665
348e048f 1666static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1667 unsigned int);
348e048f 1668
7d45c7c3
KB
1669static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1670 struct dwarf2_cu *cu);
1671
05cf31d1
JB
1672static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1673 struct dwarf2_cu *cu);
1674
e142c38c 1675static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1676
e142c38c 1677static struct die_info *die_specification (struct die_info *die,
f2f0e013 1678 struct dwarf2_cu **);
63d06c5c 1679
9c541725 1680static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1681 struct dwarf2_cu *cu);
debd256d 1682
f3f5162e 1683static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1684 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1685 CORE_ADDR, int decode_mapping);
c906108c 1686
4d663531 1687static void dwarf2_start_subfile (const char *, const char *);
c906108c 1688
43f3e411
DE
1689static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1690 const char *, const char *,
1691 CORE_ADDR);
f4dc4d17 1692
a14ed312 1693static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1694 struct dwarf2_cu *);
c906108c 1695
34eaf542
TT
1696static struct symbol *new_symbol_full (struct die_info *, struct type *,
1697 struct dwarf2_cu *, struct symbol *);
1698
ff39bb5e 1699static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1700 struct dwarf2_cu *);
c906108c 1701
ff39bb5e 1702static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1703 struct type *type,
1704 const char *name,
1705 struct obstack *obstack,
12df843f 1706 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1707 const gdb_byte **bytes,
98bfdba5 1708 struct dwarf2_locexpr_baton **baton);
2df3850c 1709
e7c27a73 1710static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1711
b4ba55a1
JB
1712static int need_gnat_info (struct dwarf2_cu *);
1713
3e43a32a
MS
1714static struct type *die_descriptive_type (struct die_info *,
1715 struct dwarf2_cu *);
b4ba55a1
JB
1716
1717static void set_descriptive_type (struct type *, struct die_info *,
1718 struct dwarf2_cu *);
1719
e7c27a73
DJ
1720static struct type *die_containing_type (struct die_info *,
1721 struct dwarf2_cu *);
c906108c 1722
ff39bb5e 1723static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1724 struct dwarf2_cu *);
c906108c 1725
f792889a 1726static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1727
673bfd45
DE
1728static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1729
0d5cff50 1730static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1731
6e70227d 1732static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1733 const char *suffix, int physname,
1734 struct dwarf2_cu *cu);
63d06c5c 1735
e7c27a73 1736static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1737
348e048f
DE
1738static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1739
e7c27a73 1740static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1741
e7c27a73 1742static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1743
96408a79
SA
1744static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1745
ff013f42
JK
1746static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1747 struct dwarf2_cu *, struct partial_symtab *);
1748
3a2b436a 1749/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1750 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1751enum pc_bounds_kind
1752{
e385593e 1753 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1754 PC_BOUNDS_NOT_PRESENT,
1755
e385593e
JK
1756 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1757 were present but they do not form a valid range of PC addresses. */
1758 PC_BOUNDS_INVALID,
1759
3a2b436a
JK
1760 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1761 PC_BOUNDS_RANGES,
1762
1763 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1764 PC_BOUNDS_HIGH_LOW,
1765};
1766
1767static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1768 CORE_ADDR *, CORE_ADDR *,
1769 struct dwarf2_cu *,
1770 struct partial_symtab *);
c906108c 1771
fae299cd
DC
1772static void get_scope_pc_bounds (struct die_info *,
1773 CORE_ADDR *, CORE_ADDR *,
1774 struct dwarf2_cu *);
1775
801e3a5b
JB
1776static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1777 CORE_ADDR, struct dwarf2_cu *);
1778
a14ed312 1779static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1780 struct dwarf2_cu *);
c906108c 1781
a14ed312 1782static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1783 struct type *, struct dwarf2_cu *);
c906108c 1784
a14ed312 1785static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1786 struct die_info *, struct type *,
e7c27a73 1787 struct dwarf2_cu *);
c906108c 1788
a14ed312 1789static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1790 struct type *,
1791 struct dwarf2_cu *);
c906108c 1792
134d01f1 1793static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1794
e7c27a73 1795static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1796
e7c27a73 1797static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1798
5d7cb8df
JK
1799static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1800
22cee43f
PMR
1801static struct using_direct **using_directives (enum language);
1802
27aa8d6a
SW
1803static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1804
74921315
KS
1805static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1806
f55ee35c
JK
1807static struct type *read_module_type (struct die_info *die,
1808 struct dwarf2_cu *cu);
1809
38d518c9 1810static const char *namespace_name (struct die_info *die,
e142c38c 1811 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1812
134d01f1 1813static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1814
e7c27a73 1815static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1816
6e70227d 1817static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1818 struct dwarf2_cu *);
1819
bf6af496 1820static struct die_info *read_die_and_siblings_1
d521ce57 1821 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1822 struct die_info *);
639d11d3 1823
dee91e82 1824static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1825 const gdb_byte *info_ptr,
1826 const gdb_byte **new_info_ptr,
639d11d3
DC
1827 struct die_info *parent);
1828
d521ce57
TT
1829static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1830 struct die_info **, const gdb_byte *,
1831 int *, int);
3019eac3 1832
d521ce57
TT
1833static const gdb_byte *read_full_die (const struct die_reader_specs *,
1834 struct die_info **, const gdb_byte *,
1835 int *);
93311388 1836
e7c27a73 1837static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1838
15d034d0
TT
1839static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1840 struct obstack *);
71c25dea 1841
15d034d0 1842static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1843
15d034d0 1844static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1845 struct die_info *die,
1846 struct dwarf2_cu *cu);
1847
ca69b9e6
DE
1848static const char *dwarf2_physname (const char *name, struct die_info *die,
1849 struct dwarf2_cu *cu);
1850
e142c38c 1851static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1852 struct dwarf2_cu **);
9219021c 1853
f39c6ffd 1854static const char *dwarf_tag_name (unsigned int);
c906108c 1855
f39c6ffd 1856static const char *dwarf_attr_name (unsigned int);
c906108c 1857
f39c6ffd 1858static const char *dwarf_form_name (unsigned int);
c906108c 1859
a121b7c1 1860static const char *dwarf_bool_name (unsigned int);
c906108c 1861
f39c6ffd 1862static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1863
f9aca02d 1864static struct die_info *sibling_die (struct die_info *);
c906108c 1865
d97bc12b
DE
1866static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1867
1868static void dump_die_for_error (struct die_info *);
1869
1870static void dump_die_1 (struct ui_file *, int level, int max_level,
1871 struct die_info *);
c906108c 1872
d97bc12b 1873/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1874
51545339 1875static void store_in_ref_table (struct die_info *,
10b3939b 1876 struct dwarf2_cu *);
c906108c 1877
ff39bb5e 1878static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1879
ff39bb5e 1880static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1881
348e048f 1882static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1883 const struct attribute *,
348e048f
DE
1884 struct dwarf2_cu **);
1885
10b3939b 1886static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1887 const struct attribute *,
f2f0e013 1888 struct dwarf2_cu **);
c906108c 1889
348e048f 1890static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1891 const struct attribute *,
348e048f
DE
1892 struct dwarf2_cu **);
1893
ac9ec31b
DE
1894static struct type *get_signatured_type (struct die_info *, ULONGEST,
1895 struct dwarf2_cu *);
1896
1897static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1898 const struct attribute *,
ac9ec31b
DE
1899 struct dwarf2_cu *);
1900
e5fe5e75 1901static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1902
52dc124a 1903static void read_signatured_type (struct signatured_type *);
348e048f 1904
63e43d3a
PMR
1905static int attr_to_dynamic_prop (const struct attribute *attr,
1906 struct die_info *die, struct dwarf2_cu *cu,
1907 struct dynamic_prop *prop);
1908
c906108c
SS
1909/* memory allocation interface */
1910
7b5a2f43 1911static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1912
b60c80d6 1913static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1914
43f3e411 1915static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1916
6e5a29e1 1917static int attr_form_is_block (const struct attribute *);
8e19ed76 1918
6e5a29e1 1919static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1920
6e5a29e1 1921static int attr_form_is_constant (const struct attribute *);
3690dd37 1922
6e5a29e1 1923static int attr_form_is_ref (const struct attribute *);
7771576e 1924
8cf6f0b1
TT
1925static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1926 struct dwarf2_loclist_baton *baton,
ff39bb5e 1927 const struct attribute *attr);
8cf6f0b1 1928
ff39bb5e 1929static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1930 struct symbol *sym,
f1e6e072
TT
1931 struct dwarf2_cu *cu,
1932 int is_block);
4c2df51b 1933
d521ce57
TT
1934static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1935 const gdb_byte *info_ptr,
1936 struct abbrev_info *abbrev);
4bb7a0a7 1937
72bf9492
DJ
1938static void free_stack_comp_unit (void *);
1939
72bf9492
DJ
1940static hashval_t partial_die_hash (const void *item);
1941
1942static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1943
ae038cb0 1944static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1945 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1946
9816fde3 1947static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1948 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1949
1950static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1951 struct die_info *comp_unit_die,
1952 enum language pretend_language);
93311388 1953
68dc6402 1954static void free_heap_comp_unit (void *);
ae038cb0
DJ
1955
1956static void free_cached_comp_units (void *);
1957
1958static void age_cached_comp_units (void);
1959
dee91e82 1960static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1961
f792889a
DJ
1962static struct type *set_die_type (struct die_info *, struct type *,
1963 struct dwarf2_cu *);
1c379e20 1964
ae038cb0
DJ
1965static void create_all_comp_units (struct objfile *);
1966
0e50663e 1967static int create_all_type_units (struct objfile *);
1fd400ff 1968
95554aad
TT
1969static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1970 enum language);
10b3939b 1971
95554aad
TT
1972static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1973 enum language);
10b3939b 1974
f4dc4d17
DE
1975static void process_full_type_unit (struct dwarf2_per_cu_data *,
1976 enum language);
1977
10b3939b
DJ
1978static void dwarf2_add_dependence (struct dwarf2_cu *,
1979 struct dwarf2_per_cu_data *);
1980
ae038cb0
DJ
1981static void dwarf2_mark (struct dwarf2_cu *);
1982
1983static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1984
b64f50a1 1985static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1986 struct dwarf2_per_cu_data *);
673bfd45 1987
f792889a 1988static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1989
9291a0cd
TT
1990static void dwarf2_release_queue (void *dummy);
1991
95554aad
TT
1992static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1993 enum language pretend_language);
1994
a0f42c21 1995static void process_queue (void);
9291a0cd 1996
d721ba37
PA
1997/* The return type of find_file_and_directory. Note, the enclosed
1998 string pointers are only valid while this object is valid. */
1999
2000struct file_and_directory
2001{
2002 /* The filename. This is never NULL. */
2003 const char *name;
2004
2005 /* The compilation directory. NULL if not known. If we needed to
2006 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2007 points directly to the DW_AT_comp_dir string attribute owned by
2008 the obstack that owns the DIE. */
2009 const char *comp_dir;
2010
2011 /* If we needed to build a new string for comp_dir, this is what
2012 owns the storage. */
2013 std::string comp_dir_storage;
2014};
2015
2016static file_and_directory find_file_and_directory (struct die_info *die,
2017 struct dwarf2_cu *cu);
9291a0cd
TT
2018
2019static char *file_full_name (int file, struct line_header *lh,
2020 const char *comp_dir);
2021
43988095
JK
2022/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2023enum class rcuh_kind { COMPILE, TYPE };
2024
d521ce57 2025static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2026 (struct comp_unit_head *header,
2027 struct dwarf2_section_info *section,
d521ce57 2028 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2029 rcuh_kind section_kind);
36586728 2030
fd820528 2031static void init_cutu_and_read_dies
f4dc4d17
DE
2032 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2033 int use_existing_cu, int keep,
3019eac3
DE
2034 die_reader_func_ftype *die_reader_func, void *data);
2035
dee91e82
DE
2036static void init_cutu_and_read_dies_simple
2037 (struct dwarf2_per_cu_data *this_cu,
2038 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2039
673bfd45 2040static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2041
3019eac3
DE
2042static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2043
57d63ce2
DE
2044static struct dwo_unit *lookup_dwo_unit_in_dwp
2045 (struct dwp_file *dwp_file, const char *comp_dir,
2046 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2047
2048static struct dwp_file *get_dwp_file (void);
2049
3019eac3 2050static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2051 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2052
2053static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2054 (struct signatured_type *, const char *, const char *);
3019eac3 2055
89e63ee4
DE
2056static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2057
3019eac3
DE
2058static void free_dwo_file_cleanup (void *);
2059
95554aad
TT
2060static void process_cu_includes (void);
2061
1b80a9fa 2062static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2063
2064static void free_line_header_voidp (void *arg);
4390d890
DE
2065\f
2066/* Various complaints about symbol reading that don't abort the process. */
2067
2068static void
2069dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2070{
2071 complaint (&symfile_complaints,
2072 _("statement list doesn't fit in .debug_line section"));
2073}
2074
2075static void
2076dwarf2_debug_line_missing_file_complaint (void)
2077{
2078 complaint (&symfile_complaints,
2079 _(".debug_line section has line data without a file"));
2080}
2081
2082static void
2083dwarf2_debug_line_missing_end_sequence_complaint (void)
2084{
2085 complaint (&symfile_complaints,
2086 _(".debug_line section has line "
2087 "program sequence without an end"));
2088}
2089
2090static void
2091dwarf2_complex_location_expr_complaint (void)
2092{
2093 complaint (&symfile_complaints, _("location expression too complex"));
2094}
2095
2096static void
2097dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2098 int arg3)
2099{
2100 complaint (&symfile_complaints,
2101 _("const value length mismatch for '%s', got %d, expected %d"),
2102 arg1, arg2, arg3);
2103}
2104
2105static void
2106dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2107{
2108 complaint (&symfile_complaints,
2109 _("debug info runs off end of %s section"
2110 " [in module %s]"),
a32a8923
DE
2111 get_section_name (section),
2112 get_section_file_name (section));
4390d890 2113}
1b80a9fa 2114
4390d890
DE
2115static void
2116dwarf2_macro_malformed_definition_complaint (const char *arg1)
2117{
2118 complaint (&symfile_complaints,
2119 _("macro debug info contains a "
2120 "malformed macro definition:\n`%s'"),
2121 arg1);
2122}
2123
2124static void
2125dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2126{
2127 complaint (&symfile_complaints,
2128 _("invalid attribute class or form for '%s' in '%s'"),
2129 arg1, arg2);
2130}
527f3840
JK
2131
2132/* Hash function for line_header_hash. */
2133
2134static hashval_t
2135line_header_hash (const struct line_header *ofs)
2136{
9c541725 2137 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2138}
2139
2140/* Hash function for htab_create_alloc_ex for line_header_hash. */
2141
2142static hashval_t
2143line_header_hash_voidp (const void *item)
2144{
9a3c8263 2145 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2146
2147 return line_header_hash (ofs);
2148}
2149
2150/* Equality function for line_header_hash. */
2151
2152static int
2153line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2154{
9a3c8263
SM
2155 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2156 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2157
9c541725 2158 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2159 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2160}
2161
4390d890 2162\f
9291a0cd
TT
2163#if WORDS_BIGENDIAN
2164
2165/* Convert VALUE between big- and little-endian. */
2166static offset_type
2167byte_swap (offset_type value)
2168{
2169 offset_type result;
2170
2171 result = (value & 0xff) << 24;
2172 result |= (value & 0xff00) << 8;
2173 result |= (value & 0xff0000) >> 8;
2174 result |= (value & 0xff000000) >> 24;
2175 return result;
2176}
2177
2178#define MAYBE_SWAP(V) byte_swap (V)
2179
2180#else
bc8f2430 2181#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2182#endif /* WORDS_BIGENDIAN */
2183
31aa7e4e
JB
2184/* Read the given attribute value as an address, taking the attribute's
2185 form into account. */
2186
2187static CORE_ADDR
2188attr_value_as_address (struct attribute *attr)
2189{
2190 CORE_ADDR addr;
2191
2192 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2193 {
2194 /* Aside from a few clearly defined exceptions, attributes that
2195 contain an address must always be in DW_FORM_addr form.
2196 Unfortunately, some compilers happen to be violating this
2197 requirement by encoding addresses using other forms, such
2198 as DW_FORM_data4 for example. For those broken compilers,
2199 we try to do our best, without any guarantee of success,
2200 to interpret the address correctly. It would also be nice
2201 to generate a complaint, but that would require us to maintain
2202 a list of legitimate cases where a non-address form is allowed,
2203 as well as update callers to pass in at least the CU's DWARF
2204 version. This is more overhead than what we're willing to
2205 expand for a pretty rare case. */
2206 addr = DW_UNSND (attr);
2207 }
2208 else
2209 addr = DW_ADDR (attr);
2210
2211 return addr;
2212}
2213
9291a0cd
TT
2214/* The suffix for an index file. */
2215#define INDEX_SUFFIX ".gdb-index"
2216
330cdd98
PA
2217/* See declaration. */
2218
2219dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2220 const dwarf2_debug_sections *names)
2221 : objfile (objfile_)
2222{
2223 if (names == NULL)
2224 names = &dwarf2_elf_names;
2225
2226 bfd *obfd = objfile->obfd;
2227
2228 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2229 locate_sections (obfd, sec, *names);
2230}
2231
2232dwarf2_per_objfile::~dwarf2_per_objfile ()
2233{
2234 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2235 free_cached_comp_units ();
2236
2237 if (quick_file_names_table)
2238 htab_delete (quick_file_names_table);
2239
2240 if (line_header_hash)
2241 htab_delete (line_header_hash);
2242
2243 /* Everything else should be on the objfile obstack. */
2244}
2245
2246/* See declaration. */
2247
2248void
2249dwarf2_per_objfile::free_cached_comp_units ()
2250{
2251 dwarf2_per_cu_data *per_cu = read_in_chain;
2252 dwarf2_per_cu_data **last_chain = &read_in_chain;
2253 while (per_cu != NULL)
2254 {
2255 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2256
2257 free_heap_comp_unit (per_cu->cu);
2258 *last_chain = next_cu;
2259 per_cu = next_cu;
2260 }
2261}
2262
c906108c 2263/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2264 information and return true if we have enough to do something.
2265 NAMES points to the dwarf2 section names, or is NULL if the standard
2266 ELF names are used. */
c906108c
SS
2267
2268int
251d32d9
TG
2269dwarf2_has_info (struct objfile *objfile,
2270 const struct dwarf2_debug_sections *names)
c906108c 2271{
9a3c8263
SM
2272 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2273 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2274 if (!dwarf2_per_objfile)
2275 {
2276 /* Initialize per-objfile state. */
2277 struct dwarf2_per_objfile *data
8d749320 2278 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2279
330cdd98
PA
2280 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2281 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2282 }
73869dc2 2283 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2284 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2285 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2286 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2287}
2288
2289/* Return the containing section of virtual section SECTION. */
2290
2291static struct dwarf2_section_info *
2292get_containing_section (const struct dwarf2_section_info *section)
2293{
2294 gdb_assert (section->is_virtual);
2295 return section->s.containing_section;
c906108c
SS
2296}
2297
a32a8923
DE
2298/* Return the bfd owner of SECTION. */
2299
2300static struct bfd *
2301get_section_bfd_owner (const struct dwarf2_section_info *section)
2302{
73869dc2
DE
2303 if (section->is_virtual)
2304 {
2305 section = get_containing_section (section);
2306 gdb_assert (!section->is_virtual);
2307 }
049412e3 2308 return section->s.section->owner;
a32a8923
DE
2309}
2310
2311/* Return the bfd section of SECTION.
2312 Returns NULL if the section is not present. */
2313
2314static asection *
2315get_section_bfd_section (const struct dwarf2_section_info *section)
2316{
73869dc2
DE
2317 if (section->is_virtual)
2318 {
2319 section = get_containing_section (section);
2320 gdb_assert (!section->is_virtual);
2321 }
049412e3 2322 return section->s.section;
a32a8923
DE
2323}
2324
2325/* Return the name of SECTION. */
2326
2327static const char *
2328get_section_name (const struct dwarf2_section_info *section)
2329{
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 gdb_assert (sectp != NULL);
2333 return bfd_section_name (get_section_bfd_owner (section), sectp);
2334}
2335
2336/* Return the name of the file SECTION is in. */
2337
2338static const char *
2339get_section_file_name (const struct dwarf2_section_info *section)
2340{
2341 bfd *abfd = get_section_bfd_owner (section);
2342
2343 return bfd_get_filename (abfd);
2344}
2345
2346/* Return the id of SECTION.
2347 Returns 0 if SECTION doesn't exist. */
2348
2349static int
2350get_section_id (const struct dwarf2_section_info *section)
2351{
2352 asection *sectp = get_section_bfd_section (section);
2353
2354 if (sectp == NULL)
2355 return 0;
2356 return sectp->id;
2357}
2358
2359/* Return the flags of SECTION.
73869dc2 2360 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2361
2362static int
2363get_section_flags (const struct dwarf2_section_info *section)
2364{
2365 asection *sectp = get_section_bfd_section (section);
2366
2367 gdb_assert (sectp != NULL);
2368 return bfd_get_section_flags (sectp->owner, sectp);
2369}
2370
251d32d9
TG
2371/* When loading sections, we look either for uncompressed section or for
2372 compressed section names. */
233a11ab
CS
2373
2374static int
251d32d9
TG
2375section_is_p (const char *section_name,
2376 const struct dwarf2_section_names *names)
233a11ab 2377{
251d32d9
TG
2378 if (names->normal != NULL
2379 && strcmp (section_name, names->normal) == 0)
2380 return 1;
2381 if (names->compressed != NULL
2382 && strcmp (section_name, names->compressed) == 0)
2383 return 1;
2384 return 0;
233a11ab
CS
2385}
2386
330cdd98 2387/* See declaration. */
c906108c 2388
330cdd98
PA
2389void
2390dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2391 const dwarf2_debug_sections &names)
c906108c 2392{
dc7650b8 2393 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2394
dc7650b8
JK
2395 if ((aflag & SEC_HAS_CONTENTS) == 0)
2396 {
2397 }
330cdd98 2398 else if (section_is_p (sectp->name, &names.info))
c906108c 2399 {
330cdd98
PA
2400 this->info.s.section = sectp;
2401 this->info.size = bfd_get_section_size (sectp);
c906108c 2402 }
330cdd98 2403 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2404 {
330cdd98
PA
2405 this->abbrev.s.section = sectp;
2406 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2407 }
330cdd98 2408 else if (section_is_p (sectp->name, &names.line))
c906108c 2409 {
330cdd98
PA
2410 this->line.s.section = sectp;
2411 this->line.size = bfd_get_section_size (sectp);
c906108c 2412 }
330cdd98 2413 else if (section_is_p (sectp->name, &names.loc))
c906108c 2414 {
330cdd98
PA
2415 this->loc.s.section = sectp;
2416 this->loc.size = bfd_get_section_size (sectp);
c906108c 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.loclists))
43988095 2419 {
330cdd98
PA
2420 this->loclists.s.section = sectp;
2421 this->loclists.size = bfd_get_section_size (sectp);
43988095 2422 }
330cdd98 2423 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2424 {
330cdd98
PA
2425 this->macinfo.s.section = sectp;
2426 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2427 }
330cdd98 2428 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2429 {
330cdd98
PA
2430 this->macro.s.section = sectp;
2431 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2432 }
330cdd98 2433 else if (section_is_p (sectp->name, &names.str))
c906108c 2434 {
330cdd98
PA
2435 this->str.s.section = sectp;
2436 this->str.size = bfd_get_section_size (sectp);
c906108c 2437 }
330cdd98 2438 else if (section_is_p (sectp->name, &names.line_str))
43988095 2439 {
330cdd98
PA
2440 this->line_str.s.section = sectp;
2441 this->line_str.size = bfd_get_section_size (sectp);
43988095 2442 }
330cdd98 2443 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2444 {
330cdd98
PA
2445 this->addr.s.section = sectp;
2446 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2447 }
330cdd98 2448 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2449 {
330cdd98
PA
2450 this->frame.s.section = sectp;
2451 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2452 }
330cdd98 2453 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2454 {
330cdd98
PA
2455 this->eh_frame.s.section = sectp;
2456 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2457 }
330cdd98 2458 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2459 {
330cdd98
PA
2460 this->ranges.s.section = sectp;
2461 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2462 }
330cdd98 2463 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2464 {
330cdd98
PA
2465 this->rnglists.s.section = sectp;
2466 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2467 }
330cdd98 2468 else if (section_is_p (sectp->name, &names.types))
348e048f 2469 {
8b70b953
TT
2470 struct dwarf2_section_info type_section;
2471
2472 memset (&type_section, 0, sizeof (type_section));
049412e3 2473 type_section.s.section = sectp;
8b70b953
TT
2474 type_section.size = bfd_get_section_size (sectp);
2475
330cdd98 2476 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2477 &type_section);
348e048f 2478 }
330cdd98 2479 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2480 {
330cdd98
PA
2481 this->gdb_index.s.section = sectp;
2482 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2483 }
dce234bc 2484
b4e1fd61 2485 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2486 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2487 this->has_section_at_zero = true;
c906108c
SS
2488}
2489
fceca515
DE
2490/* A helper function that decides whether a section is empty,
2491 or not present. */
9e0ac564
TT
2492
2493static int
19ac8c2e 2494dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2495{
73869dc2
DE
2496 if (section->is_virtual)
2497 return section->size == 0;
049412e3 2498 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2499}
2500
3019eac3
DE
2501/* Read the contents of the section INFO.
2502 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2503 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2504 of the DWO file.
dce234bc 2505 If the section is compressed, uncompress it before returning. */
c906108c 2506
dce234bc
PP
2507static void
2508dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2509{
a32a8923 2510 asection *sectp;
3019eac3 2511 bfd *abfd;
dce234bc 2512 gdb_byte *buf, *retbuf;
c906108c 2513
be391dca
TT
2514 if (info->readin)
2515 return;
dce234bc 2516 info->buffer = NULL;
be391dca 2517 info->readin = 1;
188dd5d6 2518
9e0ac564 2519 if (dwarf2_section_empty_p (info))
dce234bc 2520 return;
c906108c 2521
a32a8923 2522 sectp = get_section_bfd_section (info);
3019eac3 2523
73869dc2
DE
2524 /* If this is a virtual section we need to read in the real one first. */
2525 if (info->is_virtual)
2526 {
2527 struct dwarf2_section_info *containing_section =
2528 get_containing_section (info);
2529
2530 gdb_assert (sectp != NULL);
2531 if ((sectp->flags & SEC_RELOC) != 0)
2532 {
2533 error (_("Dwarf Error: DWP format V2 with relocations is not"
2534 " supported in section %s [in module %s]"),
2535 get_section_name (info), get_section_file_name (info));
2536 }
2537 dwarf2_read_section (objfile, containing_section);
2538 /* Other code should have already caught virtual sections that don't
2539 fit. */
2540 gdb_assert (info->virtual_offset + info->size
2541 <= containing_section->size);
2542 /* If the real section is empty or there was a problem reading the
2543 section we shouldn't get here. */
2544 gdb_assert (containing_section->buffer != NULL);
2545 info->buffer = containing_section->buffer + info->virtual_offset;
2546 return;
2547 }
2548
4bf44c1c
TT
2549 /* If the section has relocations, we must read it ourselves.
2550 Otherwise we attach it to the BFD. */
2551 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2552 {
d521ce57 2553 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2554 return;
dce234bc 2555 }
dce234bc 2556
224c3ddb 2557 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2558 info->buffer = buf;
dce234bc
PP
2559
2560 /* When debugging .o files, we may need to apply relocations; see
2561 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2562 We never compress sections in .o files, so we only need to
2563 try this when the section is not compressed. */
ac8035ab 2564 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2565 if (retbuf != NULL)
2566 {
2567 info->buffer = retbuf;
2568 return;
2569 }
2570
a32a8923
DE
2571 abfd = get_section_bfd_owner (info);
2572 gdb_assert (abfd != NULL);
2573
dce234bc
PP
2574 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2575 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2576 {
2577 error (_("Dwarf Error: Can't read DWARF data"
2578 " in section %s [in module %s]"),
2579 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2580 }
dce234bc
PP
2581}
2582
9e0ac564
TT
2583/* A helper function that returns the size of a section in a safe way.
2584 If you are positive that the section has been read before using the
2585 size, then it is safe to refer to the dwarf2_section_info object's
2586 "size" field directly. In other cases, you must call this
2587 function, because for compressed sections the size field is not set
2588 correctly until the section has been read. */
2589
2590static bfd_size_type
2591dwarf2_section_size (struct objfile *objfile,
2592 struct dwarf2_section_info *info)
2593{
2594 if (!info->readin)
2595 dwarf2_read_section (objfile, info);
2596 return info->size;
2597}
2598
dce234bc 2599/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2600 SECTION_NAME. */
af34e669 2601
dce234bc 2602void
3017a003
TG
2603dwarf2_get_section_info (struct objfile *objfile,
2604 enum dwarf2_section_enum sect,
d521ce57 2605 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2606 bfd_size_type *sizep)
2607{
2608 struct dwarf2_per_objfile *data
9a3c8263
SM
2609 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2610 dwarf2_objfile_data_key);
dce234bc 2611 struct dwarf2_section_info *info;
a3b2a86b
TT
2612
2613 /* We may see an objfile without any DWARF, in which case we just
2614 return nothing. */
2615 if (data == NULL)
2616 {
2617 *sectp = NULL;
2618 *bufp = NULL;
2619 *sizep = 0;
2620 return;
2621 }
3017a003
TG
2622 switch (sect)
2623 {
2624 case DWARF2_DEBUG_FRAME:
2625 info = &data->frame;
2626 break;
2627 case DWARF2_EH_FRAME:
2628 info = &data->eh_frame;
2629 break;
2630 default:
2631 gdb_assert_not_reached ("unexpected section");
2632 }
dce234bc 2633
9e0ac564 2634 dwarf2_read_section (objfile, info);
dce234bc 2635
a32a8923 2636 *sectp = get_section_bfd_section (info);
dce234bc
PP
2637 *bufp = info->buffer;
2638 *sizep = info->size;
2639}
2640
36586728
TT
2641/* A helper function to find the sections for a .dwz file. */
2642
2643static void
2644locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2645{
9a3c8263 2646 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2647
2648 /* Note that we only support the standard ELF names, because .dwz
2649 is ELF-only (at the time of writing). */
2650 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2651 {
049412e3 2652 dwz_file->abbrev.s.section = sectp;
36586728
TT
2653 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2654 }
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2656 {
049412e3 2657 dwz_file->info.s.section = sectp;
36586728
TT
2658 dwz_file->info.size = bfd_get_section_size (sectp);
2659 }
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2661 {
049412e3 2662 dwz_file->str.s.section = sectp;
36586728
TT
2663 dwz_file->str.size = bfd_get_section_size (sectp);
2664 }
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2666 {
049412e3 2667 dwz_file->line.s.section = sectp;
36586728
TT
2668 dwz_file->line.size = bfd_get_section_size (sectp);
2669 }
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2671 {
049412e3 2672 dwz_file->macro.s.section = sectp;
36586728
TT
2673 dwz_file->macro.size = bfd_get_section_size (sectp);
2674 }
2ec9a5e0
TT
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2676 {
049412e3 2677 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2678 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2679 }
36586728
TT
2680}
2681
4db1a1dc
TT
2682/* Open the separate '.dwz' debug file, if needed. Return NULL if
2683 there is no .gnu_debugaltlink section in the file. Error if there
2684 is such a section but the file cannot be found. */
36586728
TT
2685
2686static struct dwz_file *
2687dwarf2_get_dwz_file (void)
2688{
36586728
TT
2689 const char *filename;
2690 struct dwz_file *result;
acd13123 2691 bfd_size_type buildid_len_arg;
dc294be5
TT
2692 size_t buildid_len;
2693 bfd_byte *buildid;
36586728
TT
2694
2695 if (dwarf2_per_objfile->dwz_file != NULL)
2696 return dwarf2_per_objfile->dwz_file;
2697
4db1a1dc 2698 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2699 gdb::unique_xmalloc_ptr<char> data
2700 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2701 &buildid_len_arg, &buildid));
4db1a1dc
TT
2702 if (data == NULL)
2703 {
2704 if (bfd_get_error () == bfd_error_no_error)
2705 return NULL;
2706 error (_("could not read '.gnu_debugaltlink' section: %s"),
2707 bfd_errmsg (bfd_get_error ()));
2708 }
791afaa2
TT
2709
2710 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2711
acd13123
TT
2712 buildid_len = (size_t) buildid_len_arg;
2713
791afaa2 2714 filename = data.get ();
d721ba37
PA
2715
2716 std::string abs_storage;
36586728
TT
2717 if (!IS_ABSOLUTE_PATH (filename))
2718 {
14278e1f
TT
2719 gdb::unique_xmalloc_ptr<char> abs
2720 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2721
14278e1f 2722 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2723 filename = abs_storage.c_str ();
36586728
TT
2724 }
2725
dc294be5
TT
2726 /* First try the file name given in the section. If that doesn't
2727 work, try to use the build-id instead. */
192b62ce 2728 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2729 if (dwz_bfd != NULL)
36586728 2730 {
192b62ce
TT
2731 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2732 dwz_bfd.release ();
36586728
TT
2733 }
2734
dc294be5
TT
2735 if (dwz_bfd == NULL)
2736 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2737
2738 if (dwz_bfd == NULL)
2739 error (_("could not find '.gnu_debugaltlink' file for %s"),
2740 objfile_name (dwarf2_per_objfile->objfile));
2741
36586728
TT
2742 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2743 struct dwz_file);
192b62ce 2744 result->dwz_bfd = dwz_bfd.release ();
36586728 2745
192b62ce 2746 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2747
192b62ce 2748 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2749 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2750 return result;
2751}
9291a0cd 2752\f
7b9f3c50
DE
2753/* DWARF quick_symbols_functions support. */
2754
2755/* TUs can share .debug_line entries, and there can be a lot more TUs than
2756 unique line tables, so we maintain a separate table of all .debug_line
2757 derived entries to support the sharing.
2758 All the quick functions need is the list of file names. We discard the
2759 line_header when we're done and don't need to record it here. */
2760struct quick_file_names
2761{
094b34ac
DE
2762 /* The data used to construct the hash key. */
2763 struct stmt_list_hash hash;
7b9f3c50
DE
2764
2765 /* The number of entries in file_names, real_names. */
2766 unsigned int num_file_names;
2767
2768 /* The file names from the line table, after being run through
2769 file_full_name. */
2770 const char **file_names;
2771
2772 /* The file names from the line table after being run through
2773 gdb_realpath. These are computed lazily. */
2774 const char **real_names;
2775};
2776
2777/* When using the index (and thus not using psymtabs), each CU has an
2778 object of this type. This is used to hold information needed by
2779 the various "quick" methods. */
2780struct dwarf2_per_cu_quick_data
2781{
2782 /* The file table. This can be NULL if there was no file table
2783 or it's currently not read in.
2784 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2785 struct quick_file_names *file_names;
2786
2787 /* The corresponding symbol table. This is NULL if symbols for this
2788 CU have not yet been read. */
43f3e411 2789 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2790
2791 /* A temporary mark bit used when iterating over all CUs in
2792 expand_symtabs_matching. */
2793 unsigned int mark : 1;
2794
2795 /* True if we've tried to read the file table and found there isn't one.
2796 There will be no point in trying to read it again next time. */
2797 unsigned int no_file_data : 1;
2798};
2799
094b34ac
DE
2800/* Utility hash function for a stmt_list_hash. */
2801
2802static hashval_t
2803hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2804{
2805 hashval_t v = 0;
2806
2807 if (stmt_list_hash->dwo_unit != NULL)
2808 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2809 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2810 return v;
2811}
2812
2813/* Utility equality function for a stmt_list_hash. */
2814
2815static int
2816eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2817 const struct stmt_list_hash *rhs)
2818{
2819 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2820 return 0;
2821 if (lhs->dwo_unit != NULL
2822 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2823 return 0;
2824
9c541725 2825 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2826}
2827
7b9f3c50
DE
2828/* Hash function for a quick_file_names. */
2829
2830static hashval_t
2831hash_file_name_entry (const void *e)
2832{
9a3c8263
SM
2833 const struct quick_file_names *file_data
2834 = (const struct quick_file_names *) e;
7b9f3c50 2835
094b34ac 2836 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2837}
2838
2839/* Equality function for a quick_file_names. */
2840
2841static int
2842eq_file_name_entry (const void *a, const void *b)
2843{
9a3c8263
SM
2844 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2845 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2846
094b34ac 2847 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2848}
2849
2850/* Delete function for a quick_file_names. */
2851
2852static void
2853delete_file_name_entry (void *e)
2854{
9a3c8263 2855 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2856 int i;
2857
2858 for (i = 0; i < file_data->num_file_names; ++i)
2859 {
2860 xfree ((void*) file_data->file_names[i]);
2861 if (file_data->real_names)
2862 xfree ((void*) file_data->real_names[i]);
2863 }
2864
2865 /* The space for the struct itself lives on objfile_obstack,
2866 so we don't free it here. */
2867}
2868
2869/* Create a quick_file_names hash table. */
2870
2871static htab_t
2872create_quick_file_names_table (unsigned int nr_initial_entries)
2873{
2874 return htab_create_alloc (nr_initial_entries,
2875 hash_file_name_entry, eq_file_name_entry,
2876 delete_file_name_entry, xcalloc, xfree);
2877}
9291a0cd 2878
918dd910
JK
2879/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2880 have to be created afterwards. You should call age_cached_comp_units after
2881 processing PER_CU->CU. dw2_setup must have been already called. */
2882
2883static void
2884load_cu (struct dwarf2_per_cu_data *per_cu)
2885{
3019eac3 2886 if (per_cu->is_debug_types)
e5fe5e75 2887 load_full_type_unit (per_cu);
918dd910 2888 else
95554aad 2889 load_full_comp_unit (per_cu, language_minimal);
918dd910 2890
cc12ce38
DE
2891 if (per_cu->cu == NULL)
2892 return; /* Dummy CU. */
2dc860c0
DE
2893
2894 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2895}
2896
a0f42c21 2897/* Read in the symbols for PER_CU. */
2fdf6df6 2898
9291a0cd 2899static void
a0f42c21 2900dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2901{
2902 struct cleanup *back_to;
2903
f4dc4d17
DE
2904 /* Skip type_unit_groups, reading the type units they contain
2905 is handled elsewhere. */
2906 if (IS_TYPE_UNIT_GROUP (per_cu))
2907 return;
2908
9291a0cd
TT
2909 back_to = make_cleanup (dwarf2_release_queue, NULL);
2910
95554aad 2911 if (dwarf2_per_objfile->using_index
43f3e411 2912 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2913 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2914 {
2915 queue_comp_unit (per_cu, language_minimal);
2916 load_cu (per_cu);
89e63ee4
DE
2917
2918 /* If we just loaded a CU from a DWO, and we're working with an index
2919 that may badly handle TUs, load all the TUs in that DWO as well.
2920 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2921 if (!per_cu->is_debug_types
cc12ce38 2922 && per_cu->cu != NULL
89e63ee4
DE
2923 && per_cu->cu->dwo_unit != NULL
2924 && dwarf2_per_objfile->index_table != NULL
2925 && dwarf2_per_objfile->index_table->version <= 7
2926 /* DWP files aren't supported yet. */
2927 && get_dwp_file () == NULL)
2928 queue_and_load_all_dwo_tus (per_cu);
95554aad 2929 }
9291a0cd 2930
a0f42c21 2931 process_queue ();
9291a0cd
TT
2932
2933 /* Age the cache, releasing compilation units that have not
2934 been used recently. */
2935 age_cached_comp_units ();
2936
2937 do_cleanups (back_to);
2938}
2939
2940/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2941 the objfile from which this CU came. Returns the resulting symbol
2942 table. */
2fdf6df6 2943
43f3e411 2944static struct compunit_symtab *
a0f42c21 2945dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2946{
95554aad 2947 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2948 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2949 {
2950 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2951 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2952 dw2_do_instantiate_symtab (per_cu);
95554aad 2953 process_cu_includes ();
9291a0cd
TT
2954 do_cleanups (back_to);
2955 }
f194fefb 2956
43f3e411 2957 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2958}
2959
8832e7e3 2960/* Return the CU/TU given its index.
f4dc4d17
DE
2961
2962 This is intended for loops like:
2963
2964 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2965 + dwarf2_per_objfile->n_type_units); ++i)
2966 {
8832e7e3 2967 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2968
2969 ...;
2970 }
2971*/
2fdf6df6 2972
1fd400ff 2973static struct dwarf2_per_cu_data *
8832e7e3 2974dw2_get_cutu (int index)
1fd400ff
TT
2975{
2976 if (index >= dwarf2_per_objfile->n_comp_units)
2977 {
f4dc4d17 2978 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2979 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2980 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2981 }
2982
2983 return dwarf2_per_objfile->all_comp_units[index];
2984}
2985
8832e7e3
DE
2986/* Return the CU given its index.
2987 This differs from dw2_get_cutu in that it's for when you know INDEX
2988 refers to a CU. */
f4dc4d17
DE
2989
2990static struct dwarf2_per_cu_data *
8832e7e3 2991dw2_get_cu (int index)
f4dc4d17 2992{
8832e7e3 2993 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2994
1fd400ff
TT
2995 return dwarf2_per_objfile->all_comp_units[index];
2996}
2997
2ec9a5e0
TT
2998/* A helper for create_cus_from_index that handles a given list of
2999 CUs. */
2fdf6df6 3000
74a0d9f6 3001static void
2ec9a5e0
TT
3002create_cus_from_index_list (struct objfile *objfile,
3003 const gdb_byte *cu_list, offset_type n_elements,
3004 struct dwarf2_section_info *section,
3005 int is_dwz,
3006 int base_offset)
9291a0cd
TT
3007{
3008 offset_type i;
9291a0cd 3009
2ec9a5e0 3010 for (i = 0; i < n_elements; i += 2)
9291a0cd 3011 {
74a0d9f6 3012 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3013
3014 sect_offset sect_off
3015 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3016 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3017 cu_list += 2 * 8;
3018
9c541725
PA
3019 dwarf2_per_cu_data *the_cu
3020 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3021 struct dwarf2_per_cu_data);
3022 the_cu->sect_off = sect_off;
9291a0cd
TT
3023 the_cu->length = length;
3024 the_cu->objfile = objfile;
8a0459fd 3025 the_cu->section = section;
9291a0cd
TT
3026 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3027 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3028 the_cu->is_dwz = is_dwz;
3029 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3030 }
9291a0cd
TT
3031}
3032
2ec9a5e0 3033/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3034 the CU objects for this objfile. */
2ec9a5e0 3035
74a0d9f6 3036static void
2ec9a5e0
TT
3037create_cus_from_index (struct objfile *objfile,
3038 const gdb_byte *cu_list, offset_type cu_list_elements,
3039 const gdb_byte *dwz_list, offset_type dwz_elements)
3040{
3041 struct dwz_file *dwz;
3042
3043 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3044 dwarf2_per_objfile->all_comp_units =
3045 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3046 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3047
74a0d9f6
JK
3048 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3049 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3050
3051 if (dwz_elements == 0)
74a0d9f6 3052 return;
2ec9a5e0
TT
3053
3054 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3055 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3056 cu_list_elements / 2);
2ec9a5e0
TT
3057}
3058
1fd400ff 3059/* Create the signatured type hash table from the index. */
673bfd45 3060
74a0d9f6 3061static void
673bfd45 3062create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3063 struct dwarf2_section_info *section,
673bfd45
DE
3064 const gdb_byte *bytes,
3065 offset_type elements)
1fd400ff
TT
3066{
3067 offset_type i;
673bfd45 3068 htab_t sig_types_hash;
1fd400ff 3069
6aa5f3a6
DE
3070 dwarf2_per_objfile->n_type_units
3071 = dwarf2_per_objfile->n_allocated_type_units
3072 = elements / 3;
8d749320
SM
3073 dwarf2_per_objfile->all_type_units =
3074 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3075
673bfd45 3076 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3077
3078 for (i = 0; i < elements; i += 3)
3079 {
52dc124a 3080 struct signatured_type *sig_type;
9c541725 3081 ULONGEST signature;
1fd400ff 3082 void **slot;
9c541725 3083 cu_offset type_offset_in_tu;
1fd400ff 3084
74a0d9f6 3085 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3086 sect_offset sect_off
3087 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3088 type_offset_in_tu
3089 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3090 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3091 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3092 bytes += 3 * 8;
3093
52dc124a 3094 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3095 struct signatured_type);
52dc124a 3096 sig_type->signature = signature;
9c541725 3097 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3098 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3099 sig_type->per_cu.section = section;
9c541725 3100 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3101 sig_type->per_cu.objfile = objfile;
3102 sig_type->per_cu.v.quick
1fd400ff
TT
3103 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3104 struct dwarf2_per_cu_quick_data);
3105
52dc124a
DE
3106 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3107 *slot = sig_type;
1fd400ff 3108
b4dd5633 3109 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3110 }
3111
673bfd45 3112 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3113}
3114
9291a0cd
TT
3115/* Read the address map data from the mapped index, and use it to
3116 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3117
9291a0cd
TT
3118static void
3119create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3120{
3e29f34a 3121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3122 const gdb_byte *iter, *end;
9291a0cd 3123 struct addrmap *mutable_map;
9291a0cd
TT
3124 CORE_ADDR baseaddr;
3125
8268c778
PA
3126 auto_obstack temp_obstack;
3127
9291a0cd
TT
3128 mutable_map = addrmap_create_mutable (&temp_obstack);
3129
3130 iter = index->address_table;
3131 end = iter + index->address_table_size;
3132
3133 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3134
3135 while (iter < end)
3136 {
3137 ULONGEST hi, lo, cu_index;
3138 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3139 iter += 8;
3140 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3141 iter += 8;
3142 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3143 iter += 4;
f652bce2 3144
24a55014 3145 if (lo > hi)
f652bce2 3146 {
24a55014
DE
3147 complaint (&symfile_complaints,
3148 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3149 hex_string (lo), hex_string (hi));
24a55014 3150 continue;
f652bce2 3151 }
24a55014
DE
3152
3153 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3154 {
3155 complaint (&symfile_complaints,
3156 _(".gdb_index address table has invalid CU number %u"),
3157 (unsigned) cu_index);
24a55014 3158 continue;
f652bce2 3159 }
24a55014 3160
3e29f34a
MR
3161 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3162 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3163 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3164 }
3165
3166 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3167 &objfile->objfile_obstack);
9291a0cd
TT
3168}
3169
59d7bcaf
JK
3170/* The hash function for strings in the mapped index. This is the same as
3171 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3172 implementation. This is necessary because the hash function is tied to the
3173 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3174 SYMBOL_HASH_NEXT.
3175
3176 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3177
9291a0cd 3178static hashval_t
559a7a62 3179mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3180{
3181 const unsigned char *str = (const unsigned char *) p;
3182 hashval_t r = 0;
3183 unsigned char c;
3184
3185 while ((c = *str++) != 0)
559a7a62
JK
3186 {
3187 if (index_version >= 5)
3188 c = tolower (c);
3189 r = r * 67 + c - 113;
3190 }
9291a0cd
TT
3191
3192 return r;
3193}
3194
3195/* Find a slot in the mapped index INDEX for the object named NAME.
3196 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3197 constant pool and return true. If NAME cannot be found, return
3198 false. */
2fdf6df6 3199
109483d9 3200static bool
9291a0cd
TT
3201find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3202 offset_type **vec_out)
3203{
0cf03b49 3204 offset_type hash;
9291a0cd 3205 offset_type slot, step;
559a7a62 3206 int (*cmp) (const char *, const char *);
9291a0cd 3207
791afaa2 3208 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3209 if (current_language->la_language == language_cplus
45280282
IB
3210 || current_language->la_language == language_fortran
3211 || current_language->la_language == language_d)
0cf03b49
JK
3212 {
3213 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3214 not contain any. */
a8719064 3215
72998fb3 3216 if (strchr (name, '(') != NULL)
0cf03b49 3217 {
109483d9 3218 without_params = cp_remove_params (name);
0cf03b49 3219
72998fb3 3220 if (without_params != NULL)
791afaa2 3221 name = without_params.get ();
0cf03b49
JK
3222 }
3223 }
3224
559a7a62 3225 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3226 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3227 simulate our NAME being searched is also lowercased. */
3228 hash = mapped_index_string_hash ((index->version == 4
3229 && case_sensitivity == case_sensitive_off
3230 ? 5 : index->version),
3231 name);
3232
3876f04e
DE
3233 slot = hash & (index->symbol_table_slots - 1);
3234 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3235 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3236
3237 for (;;)
3238 {
3239 /* Convert a slot number to an offset into the table. */
3240 offset_type i = 2 * slot;
3241 const char *str;
3876f04e 3242 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3243 return false;
9291a0cd 3244
3876f04e 3245 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3246 if (!cmp (name, str))
9291a0cd
TT
3247 {
3248 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3249 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3250 return true;
9291a0cd
TT
3251 }
3252
3876f04e 3253 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3254 }
3255}
3256
2ec9a5e0
TT
3257/* A helper function that reads the .gdb_index from SECTION and fills
3258 in MAP. FILENAME is the name of the file containing the section;
3259 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3260 ok to use deprecated sections.
3261
3262 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3263 out parameters that are filled in with information about the CU and
3264 TU lists in the section.
3265
3266 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3267
9291a0cd 3268static int
2ec9a5e0
TT
3269read_index_from_section (struct objfile *objfile,
3270 const char *filename,
3271 int deprecated_ok,
3272 struct dwarf2_section_info *section,
3273 struct mapped_index *map,
3274 const gdb_byte **cu_list,
3275 offset_type *cu_list_elements,
3276 const gdb_byte **types_list,
3277 offset_type *types_list_elements)
9291a0cd 3278{
948f8e3d 3279 const gdb_byte *addr;
2ec9a5e0 3280 offset_type version;
b3b272e1 3281 offset_type *metadata;
1fd400ff 3282 int i;
9291a0cd 3283
2ec9a5e0 3284 if (dwarf2_section_empty_p (section))
9291a0cd 3285 return 0;
82430852
JK
3286
3287 /* Older elfutils strip versions could keep the section in the main
3288 executable while splitting it for the separate debug info file. */
a32a8923 3289 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3290 return 0;
3291
2ec9a5e0 3292 dwarf2_read_section (objfile, section);
9291a0cd 3293
2ec9a5e0 3294 addr = section->buffer;
9291a0cd 3295 /* Version check. */
1fd400ff 3296 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3297 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3298 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3299 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3300 indices. */
831adc1f 3301 if (version < 4)
481860b3
GB
3302 {
3303 static int warning_printed = 0;
3304 if (!warning_printed)
3305 {
3306 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3307 filename);
481860b3
GB
3308 warning_printed = 1;
3309 }
3310 return 0;
3311 }
3312 /* Index version 4 uses a different hash function than index version
3313 5 and later.
3314
3315 Versions earlier than 6 did not emit psymbols for inlined
3316 functions. Using these files will cause GDB not to be able to
3317 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3318 indices unless the user has done
3319 "set use-deprecated-index-sections on". */
2ec9a5e0 3320 if (version < 6 && !deprecated_ok)
481860b3
GB
3321 {
3322 static int warning_printed = 0;
3323 if (!warning_printed)
3324 {
e615022a
DE
3325 warning (_("\
3326Skipping deprecated .gdb_index section in %s.\n\
3327Do \"set use-deprecated-index-sections on\" before the file is read\n\
3328to use the section anyway."),
2ec9a5e0 3329 filename);
481860b3
GB
3330 warning_printed = 1;
3331 }
3332 return 0;
3333 }
796a7ff8 3334 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3335 of the TU (for symbols coming from TUs),
3336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3337 Plus gold-generated indices can have duplicate entries for global symbols,
3338 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3339 These are just performance bugs, and we can't distinguish gdb-generated
3340 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3341
481860b3 3342 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3343 longer backward compatible. */
796a7ff8 3344 if (version > 8)
594e8718 3345 return 0;
9291a0cd 3346
559a7a62 3347 map->version = version;
2ec9a5e0 3348 map->total_size = section->size;
9291a0cd
TT
3349
3350 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3351
3352 i = 0;
2ec9a5e0
TT
3353 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3354 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3355 / 8);
1fd400ff
TT
3356 ++i;
3357
2ec9a5e0
TT
3358 *types_list = addr + MAYBE_SWAP (metadata[i]);
3359 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3360 - MAYBE_SWAP (metadata[i]))
3361 / 8);
987d643c 3362 ++i;
1fd400ff
TT
3363
3364 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3365 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3366 - MAYBE_SWAP (metadata[i]));
3367 ++i;
3368
3876f04e
DE
3369 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3370 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3371 - MAYBE_SWAP (metadata[i]))
3372 / (2 * sizeof (offset_type)));
1fd400ff 3373 ++i;
9291a0cd 3374
f9d83a0b 3375 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3376
2ec9a5e0
TT
3377 return 1;
3378}
3379
3380
3381/* Read the index file. If everything went ok, initialize the "quick"
3382 elements of all the CUs and return 1. Otherwise, return 0. */
3383
3384static int
3385dwarf2_read_index (struct objfile *objfile)
3386{
3387 struct mapped_index local_map, *map;
3388 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3389 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3390 struct dwz_file *dwz;
2ec9a5e0 3391
4262abfb 3392 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3393 use_deprecated_index_sections,
3394 &dwarf2_per_objfile->gdb_index, &local_map,
3395 &cu_list, &cu_list_elements,
3396 &types_list, &types_list_elements))
3397 return 0;
3398
0fefef59 3399 /* Don't use the index if it's empty. */
2ec9a5e0 3400 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3401 return 0;
3402
2ec9a5e0
TT
3403 /* If there is a .dwz file, read it so we can get its CU list as
3404 well. */
4db1a1dc
TT
3405 dwz = dwarf2_get_dwz_file ();
3406 if (dwz != NULL)
2ec9a5e0 3407 {
2ec9a5e0
TT
3408 struct mapped_index dwz_map;
3409 const gdb_byte *dwz_types_ignore;
3410 offset_type dwz_types_elements_ignore;
3411
3412 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3413 1,
3414 &dwz->gdb_index, &dwz_map,
3415 &dwz_list, &dwz_list_elements,
3416 &dwz_types_ignore,
3417 &dwz_types_elements_ignore))
3418 {
3419 warning (_("could not read '.gdb_index' section from %s; skipping"),
3420 bfd_get_filename (dwz->dwz_bfd));
3421 return 0;
3422 }
3423 }
3424
74a0d9f6
JK
3425 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3426 dwz_list_elements);
1fd400ff 3427
8b70b953
TT
3428 if (types_list_elements)
3429 {
3430 struct dwarf2_section_info *section;
3431
3432 /* We can only handle a single .debug_types when we have an
3433 index. */
3434 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3435 return 0;
3436
3437 section = VEC_index (dwarf2_section_info_def,
3438 dwarf2_per_objfile->types, 0);
3439
74a0d9f6
JK
3440 create_signatured_type_table_from_index (objfile, section, types_list,
3441 types_list_elements);
8b70b953 3442 }
9291a0cd 3443
2ec9a5e0
TT
3444 create_addrmap_from_index (objfile, &local_map);
3445
8d749320 3446 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3447 *map = local_map;
9291a0cd
TT
3448
3449 dwarf2_per_objfile->index_table = map;
3450 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3451 dwarf2_per_objfile->quick_file_names_table =
3452 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3453
3454 return 1;
3455}
3456
3457/* A helper for the "quick" functions which sets the global
3458 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3459
9291a0cd
TT
3460static void
3461dw2_setup (struct objfile *objfile)
3462{
9a3c8263
SM
3463 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3464 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3465 gdb_assert (dwarf2_per_objfile);
3466}
3467
dee91e82 3468/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3469
dee91e82
DE
3470static void
3471dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3472 const gdb_byte *info_ptr,
dee91e82
DE
3473 struct die_info *comp_unit_die,
3474 int has_children,
3475 void *data)
9291a0cd 3476{
dee91e82
DE
3477 struct dwarf2_cu *cu = reader->cu;
3478 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3479 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3480 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3481 struct attribute *attr;
dee91e82 3482 int i;
7b9f3c50
DE
3483 void **slot;
3484 struct quick_file_names *qfn;
9291a0cd 3485
0186c6a7
DE
3486 gdb_assert (! this_cu->is_debug_types);
3487
07261596
TT
3488 /* Our callers never want to match partial units -- instead they
3489 will match the enclosing full CU. */
3490 if (comp_unit_die->tag == DW_TAG_partial_unit)
3491 {
3492 this_cu->v.quick->no_file_data = 1;
3493 return;
3494 }
3495
0186c6a7 3496 lh_cu = this_cu;
7b9f3c50 3497 slot = NULL;
dee91e82 3498
fff8551c 3499 line_header_up lh;
9c541725 3500 sect_offset line_offset {};
fff8551c 3501
dee91e82 3502 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3503 if (attr)
3504 {
7b9f3c50
DE
3505 struct quick_file_names find_entry;
3506
9c541725 3507 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3508
3509 /* We may have already read in this line header (TU line header sharing).
3510 If we have we're done. */
094b34ac 3511 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3512 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3513 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3514 &find_entry, INSERT);
3515 if (*slot != NULL)
3516 {
9a3c8263 3517 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3518 return;
7b9f3c50
DE
3519 }
3520
3019eac3 3521 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3522 }
3523 if (lh == NULL)
3524 {
094b34ac 3525 lh_cu->v.quick->no_file_data = 1;
dee91e82 3526 return;
9291a0cd
TT
3527 }
3528
8d749320 3529 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3530 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3531 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3532 gdb_assert (slot != NULL);
3533 *slot = qfn;
9291a0cd 3534
d721ba37 3535 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3536
fff8551c 3537 qfn->num_file_names = lh->file_names.size ();
8d749320 3538 qfn->file_names =
fff8551c
PA
3539 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3540 for (i = 0; i < lh->file_names.size (); ++i)
3541 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3542 qfn->real_names = NULL;
9291a0cd 3543
094b34ac 3544 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3545}
3546
3547/* A helper for the "quick" functions which attempts to read the line
3548 table for THIS_CU. */
3549
3550static struct quick_file_names *
e4a48d9d 3551dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3552{
0186c6a7
DE
3553 /* This should never be called for TUs. */
3554 gdb_assert (! this_cu->is_debug_types);
3555 /* Nor type unit groups. */
3556 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3557
dee91e82
DE
3558 if (this_cu->v.quick->file_names != NULL)
3559 return this_cu->v.quick->file_names;
3560 /* If we know there is no line data, no point in looking again. */
3561 if (this_cu->v.quick->no_file_data)
3562 return NULL;
3563
0186c6a7 3564 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3565
3566 if (this_cu->v.quick->no_file_data)
3567 return NULL;
3568 return this_cu->v.quick->file_names;
9291a0cd
TT
3569}
3570
3571/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3572 real path for a given file name from the line table. */
2fdf6df6 3573
9291a0cd 3574static const char *
7b9f3c50
DE
3575dw2_get_real_path (struct objfile *objfile,
3576 struct quick_file_names *qfn, int index)
9291a0cd 3577{
7b9f3c50
DE
3578 if (qfn->real_names == NULL)
3579 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3580 qfn->num_file_names, const char *);
9291a0cd 3581
7b9f3c50 3582 if (qfn->real_names[index] == NULL)
14278e1f 3583 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3584
7b9f3c50 3585 return qfn->real_names[index];
9291a0cd
TT
3586}
3587
3588static struct symtab *
3589dw2_find_last_source_symtab (struct objfile *objfile)
3590{
43f3e411 3591 struct compunit_symtab *cust;
9291a0cd 3592 int index;
ae2de4f8 3593
9291a0cd
TT
3594 dw2_setup (objfile);
3595 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3596 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3597 if (cust == NULL)
3598 return NULL;
3599 return compunit_primary_filetab (cust);
9291a0cd
TT
3600}
3601
7b9f3c50
DE
3602/* Traversal function for dw2_forget_cached_source_info. */
3603
3604static int
3605dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3606{
7b9f3c50 3607 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3608
7b9f3c50 3609 if (file_data->real_names)
9291a0cd 3610 {
7b9f3c50 3611 int i;
9291a0cd 3612
7b9f3c50 3613 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3614 {
7b9f3c50
DE
3615 xfree ((void*) file_data->real_names[i]);
3616 file_data->real_names[i] = NULL;
9291a0cd
TT
3617 }
3618 }
7b9f3c50
DE
3619
3620 return 1;
3621}
3622
3623static void
3624dw2_forget_cached_source_info (struct objfile *objfile)
3625{
3626 dw2_setup (objfile);
3627
3628 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3629 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3630}
3631
f8eba3c6
TT
3632/* Helper function for dw2_map_symtabs_matching_filename that expands
3633 the symtabs and calls the iterator. */
3634
3635static int
3636dw2_map_expand_apply (struct objfile *objfile,
3637 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3638 const char *name, const char *real_path,
14bc53a8 3639 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3640{
43f3e411 3641 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3642
3643 /* Don't visit already-expanded CUs. */
43f3e411 3644 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3645 return 0;
3646
3647 /* This may expand more than one symtab, and we want to iterate over
3648 all of them. */
a0f42c21 3649 dw2_instantiate_symtab (per_cu);
f8eba3c6 3650
14bc53a8
PA
3651 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3652 last_made, callback);
f8eba3c6
TT
3653}
3654
3655/* Implementation of the map_symtabs_matching_filename method. */
3656
14bc53a8
PA
3657static bool
3658dw2_map_symtabs_matching_filename
3659 (struct objfile *objfile, const char *name, const char *real_path,
3660 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3661{
3662 int i;
c011a4f4 3663 const char *name_basename = lbasename (name);
9291a0cd
TT
3664
3665 dw2_setup (objfile);
ae2de4f8 3666
848e3e78
DE
3667 /* The rule is CUs specify all the files, including those used by
3668 any TU, so there's no need to scan TUs here. */
f4dc4d17 3669
848e3e78 3670 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3671 {
3672 int j;
8832e7e3 3673 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3674 struct quick_file_names *file_data;
9291a0cd 3675
3d7bb9d9 3676 /* We only need to look at symtabs not already expanded. */
43f3e411 3677 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3678 continue;
3679
e4a48d9d 3680 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3681 if (file_data == NULL)
9291a0cd
TT
3682 continue;
3683
7b9f3c50 3684 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3685 {
7b9f3c50 3686 const char *this_name = file_data->file_names[j];
da235a7c 3687 const char *this_real_name;
9291a0cd 3688
af529f8f 3689 if (compare_filenames_for_search (this_name, name))
9291a0cd 3690 {
f5b95b50 3691 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3692 callback))
3693 return true;
288e77a7 3694 continue;
4aac40c8 3695 }
9291a0cd 3696
c011a4f4
DE
3697 /* Before we invoke realpath, which can get expensive when many
3698 files are involved, do a quick comparison of the basenames. */
3699 if (! basenames_may_differ
3700 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3701 continue;
3702
da235a7c
JK
3703 this_real_name = dw2_get_real_path (objfile, file_data, j);
3704 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3705 {
da235a7c 3706 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3707 callback))
3708 return true;
288e77a7 3709 continue;
da235a7c 3710 }
9291a0cd 3711
da235a7c
JK
3712 if (real_path != NULL)
3713 {
af529f8f
JK
3714 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3715 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3716 if (this_real_name != NULL
af529f8f 3717 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3718 {
f5b95b50 3719 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3720 callback))
3721 return true;
288e77a7 3722 continue;
9291a0cd
TT
3723 }
3724 }
3725 }
3726 }
3727
14bc53a8 3728 return false;
9291a0cd
TT
3729}
3730
da51c347
DE
3731/* Struct used to manage iterating over all CUs looking for a symbol. */
3732
3733struct dw2_symtab_iterator
9291a0cd 3734{
da51c347
DE
3735 /* The internalized form of .gdb_index. */
3736 struct mapped_index *index;
3737 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3738 int want_specific_block;
3739 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3740 Unused if !WANT_SPECIFIC_BLOCK. */
3741 int block_index;
3742 /* The kind of symbol we're looking for. */
3743 domain_enum domain;
3744 /* The list of CUs from the index entry of the symbol,
3745 or NULL if not found. */
3746 offset_type *vec;
3747 /* The next element in VEC to look at. */
3748 int next;
3749 /* The number of elements in VEC, or zero if there is no match. */
3750 int length;
8943b874
DE
3751 /* Have we seen a global version of the symbol?
3752 If so we can ignore all further global instances.
3753 This is to work around gold/15646, inefficient gold-generated
3754 indices. */
3755 int global_seen;
da51c347 3756};
9291a0cd 3757
da51c347
DE
3758/* Initialize the index symtab iterator ITER.
3759 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3760 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3761
9291a0cd 3762static void
da51c347
DE
3763dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3764 struct mapped_index *index,
3765 int want_specific_block,
3766 int block_index,
3767 domain_enum domain,
3768 const char *name)
3769{
3770 iter->index = index;
3771 iter->want_specific_block = want_specific_block;
3772 iter->block_index = block_index;
3773 iter->domain = domain;
3774 iter->next = 0;
8943b874 3775 iter->global_seen = 0;
da51c347
DE
3776
3777 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3778 iter->length = MAYBE_SWAP (*iter->vec);
3779 else
3780 {
3781 iter->vec = NULL;
3782 iter->length = 0;
3783 }
3784}
3785
3786/* Return the next matching CU or NULL if there are no more. */
3787
3788static struct dwarf2_per_cu_data *
3789dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3790{
3791 for ( ; iter->next < iter->length; ++iter->next)
3792 {
3793 offset_type cu_index_and_attrs =
3794 MAYBE_SWAP (iter->vec[iter->next + 1]);
3795 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3796 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3797 int want_static = iter->block_index != GLOBAL_BLOCK;
3798 /* This value is only valid for index versions >= 7. */
3799 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3800 gdb_index_symbol_kind symbol_kind =
3801 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3802 /* Only check the symbol attributes if they're present.
3803 Indices prior to version 7 don't record them,
3804 and indices >= 7 may elide them for certain symbols
3805 (gold does this). */
3806 int attrs_valid =
3807 (iter->index->version >= 7
3808 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3809
3190f0c6
DE
3810 /* Don't crash on bad data. */
3811 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3812 + dwarf2_per_objfile->n_type_units))
3813 {
3814 complaint (&symfile_complaints,
3815 _(".gdb_index entry has bad CU index"
4262abfb
JK
3816 " [in module %s]"),
3817 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3818 continue;
3819 }
3820
8832e7e3 3821 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3822
da51c347 3823 /* Skip if already read in. */
43f3e411 3824 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3825 continue;
3826
8943b874
DE
3827 /* Check static vs global. */
3828 if (attrs_valid)
3829 {
3830 if (iter->want_specific_block
3831 && want_static != is_static)
3832 continue;
3833 /* Work around gold/15646. */
3834 if (!is_static && iter->global_seen)
3835 continue;
3836 if (!is_static)
3837 iter->global_seen = 1;
3838 }
da51c347
DE
3839
3840 /* Only check the symbol's kind if it has one. */
3841 if (attrs_valid)
3842 {
3843 switch (iter->domain)
3844 {
3845 case VAR_DOMAIN:
3846 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3847 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3848 /* Some types are also in VAR_DOMAIN. */
3849 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3850 continue;
3851 break;
3852 case STRUCT_DOMAIN:
3853 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3854 continue;
3855 break;
3856 case LABEL_DOMAIN:
3857 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3858 continue;
3859 break;
3860 default:
3861 break;
3862 }
3863 }
3864
3865 ++iter->next;
3866 return per_cu;
3867 }
3868
3869 return NULL;
3870}
3871
43f3e411 3872static struct compunit_symtab *
da51c347
DE
3873dw2_lookup_symbol (struct objfile *objfile, int block_index,
3874 const char *name, domain_enum domain)
9291a0cd 3875{
43f3e411 3876 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3877 struct mapped_index *index;
3878
9291a0cd
TT
3879 dw2_setup (objfile);
3880
b5ec771e
PA
3881 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3882
156942c7
DE
3883 index = dwarf2_per_objfile->index_table;
3884
da51c347 3885 /* index is NULL if OBJF_READNOW. */
156942c7 3886 if (index)
9291a0cd 3887 {
da51c347
DE
3888 struct dw2_symtab_iterator iter;
3889 struct dwarf2_per_cu_data *per_cu;
3890
3891 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3892
da51c347 3893 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3894 {
b2e2f908 3895 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3896 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3897 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3898 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3899
b2e2f908
DE
3900 sym = block_find_symbol (block, name, domain,
3901 block_find_non_opaque_type_preferred,
3902 &with_opaque);
3903
da51c347
DE
3904 /* Some caution must be observed with overloaded functions
3905 and methods, since the index will not contain any overload
3906 information (but NAME might contain it). */
da51c347 3907
b2e2f908 3908 if (sym != NULL
b5ec771e 3909 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3910 return stab;
3911 if (with_opaque != NULL
b5ec771e 3912 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3913 stab_best = stab;
da51c347
DE
3914
3915 /* Keep looking through other CUs. */
9291a0cd
TT
3916 }
3917 }
9291a0cd 3918
da51c347 3919 return stab_best;
9291a0cd
TT
3920}
3921
3922static void
3923dw2_print_stats (struct objfile *objfile)
3924{
e4a48d9d 3925 int i, total, count;
9291a0cd
TT
3926
3927 dw2_setup (objfile);
e4a48d9d 3928 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3929 count = 0;
e4a48d9d 3930 for (i = 0; i < total; ++i)
9291a0cd 3931 {
8832e7e3 3932 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3933
43f3e411 3934 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3935 ++count;
3936 }
e4a48d9d 3937 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3938 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3939}
3940
779bd270
DE
3941/* This dumps minimal information about the index.
3942 It is called via "mt print objfiles".
3943 One use is to verify .gdb_index has been loaded by the
3944 gdb.dwarf2/gdb-index.exp testcase. */
3945
9291a0cd
TT
3946static void
3947dw2_dump (struct objfile *objfile)
3948{
779bd270
DE
3949 dw2_setup (objfile);
3950 gdb_assert (dwarf2_per_objfile->using_index);
3951 printf_filtered (".gdb_index:");
3952 if (dwarf2_per_objfile->index_table != NULL)
3953 {
3954 printf_filtered (" version %d\n",
3955 dwarf2_per_objfile->index_table->version);
3956 }
3957 else
3958 printf_filtered (" faked for \"readnow\"\n");
3959 printf_filtered ("\n");
9291a0cd
TT
3960}
3961
3962static void
3189cb12
DE
3963dw2_relocate (struct objfile *objfile,
3964 const struct section_offsets *new_offsets,
3965 const struct section_offsets *delta)
9291a0cd
TT
3966{
3967 /* There's nothing to relocate here. */
3968}
3969
3970static void
3971dw2_expand_symtabs_for_function (struct objfile *objfile,
3972 const char *func_name)
3973{
da51c347
DE
3974 struct mapped_index *index;
3975
3976 dw2_setup (objfile);
3977
3978 index = dwarf2_per_objfile->index_table;
3979
3980 /* index is NULL if OBJF_READNOW. */
3981 if (index)
3982 {
3983 struct dw2_symtab_iterator iter;
3984 struct dwarf2_per_cu_data *per_cu;
3985
3986 /* Note: It doesn't matter what we pass for block_index here. */
3987 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3988 func_name);
3989
3990 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3991 dw2_instantiate_symtab (per_cu);
3992 }
9291a0cd
TT
3993}
3994
3995static void
3996dw2_expand_all_symtabs (struct objfile *objfile)
3997{
3998 int i;
3999
4000 dw2_setup (objfile);
1fd400ff
TT
4001
4002 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4003 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4004 {
8832e7e3 4005 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4006
a0f42c21 4007 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4008 }
4009}
4010
4011static void
652a8996
JK
4012dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4013 const char *fullname)
9291a0cd
TT
4014{
4015 int i;
4016
4017 dw2_setup (objfile);
d4637a04
DE
4018
4019 /* We don't need to consider type units here.
4020 This is only called for examining code, e.g. expand_line_sal.
4021 There can be an order of magnitude (or more) more type units
4022 than comp units, and we avoid them if we can. */
4023
4024 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4025 {
4026 int j;
8832e7e3 4027 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4028 struct quick_file_names *file_data;
9291a0cd 4029
3d7bb9d9 4030 /* We only need to look at symtabs not already expanded. */
43f3e411 4031 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4032 continue;
4033
e4a48d9d 4034 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4035 if (file_data == NULL)
9291a0cd
TT
4036 continue;
4037
7b9f3c50 4038 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4039 {
652a8996
JK
4040 const char *this_fullname = file_data->file_names[j];
4041
4042 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4043 {
a0f42c21 4044 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4045 break;
4046 }
4047 }
4048 }
4049}
4050
9291a0cd 4051static void
ade7ed9e 4052dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4053 const char * name, domain_enum domain,
ade7ed9e 4054 int global,
40658b94
PH
4055 int (*callback) (struct block *,
4056 struct symbol *, void *),
b5ec771e 4057 void *data, symbol_name_match_type match,
2edb89d3 4058 symbol_compare_ftype *ordered_compare)
9291a0cd 4059{
40658b94 4060 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4061 current language is Ada for a non-Ada objfile using GNU index. As Ada
4062 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4063}
4064
b5ec771e
PA
4065/* Symbol name matcher for .gdb_index names.
4066
4067 Symbol names in .gdb_index have a few particularities:
4068
4069 - There's no indication of which is the language of each symbol.
4070
4071 Since each language has its own symbol name matching algorithm,
4072 and we don't know which language is the right one, we must match
4073 each symbol against all languages.
4074
4075 - Symbol names in the index have no overload (parameter)
4076 information. I.e., in C++, "foo(int)" and "foo(long)" both
4077 appear as "foo" in the index, for example.
4078
4079 This means that the lookup names passed to the symbol name
4080 matcher functions must have no parameter information either
4081 because (e.g.) symbol search name "foo" does not match
4082 lookup-name "foo(int)" [while swapping search name for lookup
4083 name would match].
4084*/
4085class gdb_index_symbol_name_matcher
4086{
4087public:
4088 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4089 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4090
4091 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4092 Returns true if any matcher matches. */
4093 bool matches (const char *symbol_name);
4094
4095private:
4096 /* A reference to the lookup name we're matching against. */
4097 const lookup_name_info &m_lookup_name;
4098
4099 /* A vector holding all the different symbol name matchers, for all
4100 languages. */
4101 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4102};
4103
4104gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4105 (const lookup_name_info &lookup_name)
4106 : m_lookup_name (lookup_name)
4107{
4108 /* Prepare the vector of comparison functions upfront, to avoid
4109 doing the same work for each symbol. Care is taken to avoid
4110 matching with the same matcher more than once if/when multiple
4111 languages use the same matcher function. */
4112 auto &matchers = m_symbol_name_matcher_funcs;
4113 matchers.reserve (nr_languages);
4114
4115 matchers.push_back (default_symbol_name_matcher);
4116
4117 for (int i = 0; i < nr_languages; i++)
4118 {
4119 const language_defn *lang = language_def ((enum language) i);
4120 if (lang->la_get_symbol_name_matcher != NULL)
4121 {
4122 symbol_name_matcher_ftype *name_matcher
4123 = lang->la_get_symbol_name_matcher (m_lookup_name);
4124
4125 /* Don't insert the same comparison routine more than once.
4126 Note that we do this linear walk instead of a cheaper
4127 sorted insert, or use a std::set or something like that,
4128 because relative order of function addresses is not
4129 stable. This is not a problem in practice because the
4130 number of supported languages is low, and the cost here
4131 is tiny compared to the number of searches we'll do
4132 afterwards using this object. */
4133 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4134 == matchers.end ())
4135 matchers.push_back (name_matcher);
4136 }
4137 }
4138}
4139
4140bool
4141gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4142{
4143 for (auto matches_name : m_symbol_name_matcher_funcs)
4144 if (matches_name (symbol_name, m_lookup_name, NULL))
4145 return true;
4146
4147 return false;
4148}
4149
9291a0cd 4150static void
f8eba3c6
TT
4151dw2_expand_symtabs_matching
4152 (struct objfile *objfile,
14bc53a8 4153 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
b5ec771e 4154 const lookup_name_info &lookup_name,
14bc53a8
PA
4155 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4156 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4157 enum search_domain kind)
9291a0cd
TT
4158{
4159 int i;
4160 offset_type iter;
4b5246aa 4161 struct mapped_index *index;
9291a0cd
TT
4162
4163 dw2_setup (objfile);
ae2de4f8
DE
4164
4165 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4166 if (!dwarf2_per_objfile->index_table)
4167 return;
4b5246aa 4168 index = dwarf2_per_objfile->index_table;
9291a0cd 4169
7b08b9eb 4170 if (file_matcher != NULL)
24c79950 4171 {
fc4007c9
TT
4172 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4173 htab_eq_pointer,
4174 NULL, xcalloc, xfree));
4175 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4176 htab_eq_pointer,
4177 NULL, xcalloc, xfree));
24c79950 4178
848e3e78
DE
4179 /* The rule is CUs specify all the files, including those used by
4180 any TU, so there's no need to scan TUs here. */
4181
4182 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4183 {
4184 int j;
8832e7e3 4185 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4186 struct quick_file_names *file_data;
4187 void **slot;
7b08b9eb 4188
61d96d7e
DE
4189 QUIT;
4190
24c79950 4191 per_cu->v.quick->mark = 0;
3d7bb9d9 4192
24c79950 4193 /* We only need to look at symtabs not already expanded. */
43f3e411 4194 if (per_cu->v.quick->compunit_symtab)
24c79950 4195 continue;
7b08b9eb 4196
e4a48d9d 4197 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4198 if (file_data == NULL)
4199 continue;
7b08b9eb 4200
fc4007c9 4201 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4202 continue;
fc4007c9 4203 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4204 {
4205 per_cu->v.quick->mark = 1;
4206 continue;
4207 }
4208
4209 for (j = 0; j < file_data->num_file_names; ++j)
4210 {
da235a7c
JK
4211 const char *this_real_name;
4212
14bc53a8 4213 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4214 {
4215 per_cu->v.quick->mark = 1;
4216 break;
4217 }
da235a7c
JK
4218
4219 /* Before we invoke realpath, which can get expensive when many
4220 files are involved, do a quick comparison of the basenames. */
4221 if (!basenames_may_differ
4222 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4223 true))
da235a7c
JK
4224 continue;
4225
4226 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4227 if (file_matcher (this_real_name, false))
da235a7c
JK
4228 {
4229 per_cu->v.quick->mark = 1;
4230 break;
4231 }
24c79950
TT
4232 }
4233
4234 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4235 ? visited_found.get ()
4236 : visited_not_found.get (),
24c79950
TT
4237 file_data, INSERT);
4238 *slot = file_data;
4239 }
24c79950 4240 }
9291a0cd 4241
b5ec771e
PA
4242 gdb_index_symbol_name_matcher lookup_name_matcher (lookup_name);
4243
3876f04e 4244 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4245 {
4246 offset_type idx = 2 * iter;
4247 const char *name;
4248 offset_type *vec, vec_len, vec_idx;
8943b874 4249 int global_seen = 0;
9291a0cd 4250
61d96d7e
DE
4251 QUIT;
4252
3876f04e 4253 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4254 continue;
4255
3876f04e 4256 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4257
b5ec771e
PA
4258 if (!lookup_name_matcher.matches (name)
4259 || (symbol_matcher != NULL && !symbol_matcher (name)))
9291a0cd
TT
4260 continue;
4261
4262 /* The name was matched, now expand corresponding CUs that were
4263 marked. */
4b5246aa 4264 vec = (offset_type *) (index->constant_pool
3876f04e 4265 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4266 vec_len = MAYBE_SWAP (vec[0]);
4267 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4268 {
e254ef6a 4269 struct dwarf2_per_cu_data *per_cu;
156942c7 4270 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4271 /* This value is only valid for index versions >= 7. */
4272 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4273 gdb_index_symbol_kind symbol_kind =
4274 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4275 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4276 /* Only check the symbol attributes if they're present.
4277 Indices prior to version 7 don't record them,
4278 and indices >= 7 may elide them for certain symbols
4279 (gold does this). */
4280 int attrs_valid =
4281 (index->version >= 7
4282 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4283
8943b874
DE
4284 /* Work around gold/15646. */
4285 if (attrs_valid)
4286 {
4287 if (!is_static && global_seen)
4288 continue;
4289 if (!is_static)
4290 global_seen = 1;
4291 }
4292
3190f0c6
DE
4293 /* Only check the symbol's kind if it has one. */
4294 if (attrs_valid)
156942c7
DE
4295 {
4296 switch (kind)
4297 {
4298 case VARIABLES_DOMAIN:
4299 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4300 continue;
4301 break;
4302 case FUNCTIONS_DOMAIN:
4303 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4304 continue;
4305 break;
4306 case TYPES_DOMAIN:
4307 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4308 continue;
4309 break;
4310 default:
4311 break;
4312 }
4313 }
4314
3190f0c6
DE
4315 /* Don't crash on bad data. */
4316 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4317 + dwarf2_per_objfile->n_type_units))
4318 {
4319 complaint (&symfile_complaints,
4320 _(".gdb_index entry has bad CU index"
4262abfb 4321 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4322 continue;
4323 }
4324
8832e7e3 4325 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4326 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4327 {
4328 int symtab_was_null =
4329 (per_cu->v.quick->compunit_symtab == NULL);
4330
4331 dw2_instantiate_symtab (per_cu);
4332
4333 if (expansion_notify != NULL
4334 && symtab_was_null
4335 && per_cu->v.quick->compunit_symtab != NULL)
4336 {
14bc53a8 4337 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4338 }
4339 }
9291a0cd
TT
4340 }
4341 }
4342}
4343
43f3e411 4344/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4345 symtab. */
4346
43f3e411
DE
4347static struct compunit_symtab *
4348recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4349 CORE_ADDR pc)
9703b513
TT
4350{
4351 int i;
4352
43f3e411
DE
4353 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4354 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4355 return cust;
9703b513 4356
43f3e411 4357 if (cust->includes == NULL)
a3ec0bb1
DE
4358 return NULL;
4359
43f3e411 4360 for (i = 0; cust->includes[i]; ++i)
9703b513 4361 {
43f3e411 4362 struct compunit_symtab *s = cust->includes[i];
9703b513 4363
43f3e411 4364 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4365 if (s != NULL)
4366 return s;
4367 }
4368
4369 return NULL;
4370}
4371
43f3e411
DE
4372static struct compunit_symtab *
4373dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4374 struct bound_minimal_symbol msymbol,
4375 CORE_ADDR pc,
4376 struct obj_section *section,
4377 int warn_if_readin)
9291a0cd
TT
4378{
4379 struct dwarf2_per_cu_data *data;
43f3e411 4380 struct compunit_symtab *result;
9291a0cd
TT
4381
4382 dw2_setup (objfile);
4383
4384 if (!objfile->psymtabs_addrmap)
4385 return NULL;
4386
9a3c8263
SM
4387 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4388 pc);
9291a0cd
TT
4389 if (!data)
4390 return NULL;
4391
43f3e411 4392 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4393 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4394 paddress (get_objfile_arch (objfile), pc));
4395
43f3e411
DE
4396 result
4397 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4398 pc);
9703b513
TT
4399 gdb_assert (result != NULL);
4400 return result;
9291a0cd
TT
4401}
4402
9291a0cd 4403static void
44b13c5a 4404dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4405 void *data, int need_fullname)
9291a0cd 4406{
9291a0cd 4407 dw2_setup (objfile);
ae2de4f8 4408
bbf2f4df 4409 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4410 {
bbf2f4df 4411 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4412
bbf2f4df
PA
4413 htab_up visited (htab_create_alloc (10,
4414 htab_hash_pointer, htab_eq_pointer,
4415 NULL, xcalloc, xfree));
24c79950 4416
bbf2f4df
PA
4417 /* The rule is CUs specify all the files, including those used
4418 by any TU, so there's no need to scan TUs here. We can
4419 ignore file names coming from already-expanded CUs. */
24c79950 4420
bbf2f4df
PA
4421 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4422 {
4423 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4424
bbf2f4df
PA
4425 if (per_cu->v.quick->compunit_symtab)
4426 {
4427 void **slot = htab_find_slot (visited.get (),
4428 per_cu->v.quick->file_names,
4429 INSERT);
9291a0cd 4430
bbf2f4df
PA
4431 *slot = per_cu->v.quick->file_names;
4432 }
24c79950 4433 }
24c79950 4434
bbf2f4df 4435 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4436 {
bbf2f4df
PA
4437 int j;
4438 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4439 struct quick_file_names *file_data;
4440 void **slot;
4441
4442 /* We only need to look at symtabs not already expanded. */
4443 if (per_cu->v.quick->compunit_symtab)
4444 continue;
74e2f255 4445
bbf2f4df
PA
4446 file_data = dw2_get_file_names (per_cu);
4447 if (file_data == NULL)
4448 continue;
4449
4450 slot = htab_find_slot (visited.get (), file_data, INSERT);
4451 if (*slot)
4452 {
4453 /* Already visited. */
4454 continue;
4455 }
4456 *slot = file_data;
4457
4458 for (int j = 0; j < file_data->num_file_names; ++j)
4459 {
4460 const char *filename = file_data->file_names[j];
4461 dwarf2_per_objfile->filenames_cache->seen (filename);
4462 }
9291a0cd
TT
4463 }
4464 }
bbf2f4df
PA
4465
4466 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4467 {
14278e1f 4468 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4469
4470 if (need_fullname)
4471 this_real_name = gdb_realpath (filename);
14278e1f 4472 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4473 });
9291a0cd
TT
4474}
4475
4476static int
4477dw2_has_symbols (struct objfile *objfile)
4478{
4479 return 1;
4480}
4481
4482const struct quick_symbol_functions dwarf2_gdb_index_functions =
4483{
4484 dw2_has_symbols,
4485 dw2_find_last_source_symtab,
4486 dw2_forget_cached_source_info,
f8eba3c6 4487 dw2_map_symtabs_matching_filename,
9291a0cd 4488 dw2_lookup_symbol,
9291a0cd
TT
4489 dw2_print_stats,
4490 dw2_dump,
4491 dw2_relocate,
4492 dw2_expand_symtabs_for_function,
4493 dw2_expand_all_symtabs,
652a8996 4494 dw2_expand_symtabs_with_fullname,
40658b94 4495 dw2_map_matching_symbols,
9291a0cd 4496 dw2_expand_symtabs_matching,
43f3e411 4497 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4498 dw2_map_symbol_filenames
4499};
4500
4501/* Initialize for reading DWARF for this objfile. Return 0 if this
4502 file will use psymtabs, or 1 if using the GNU index. */
4503
4504int
4505dwarf2_initialize_objfile (struct objfile *objfile)
4506{
4507 /* If we're about to read full symbols, don't bother with the
4508 indices. In this case we also don't care if some other debug
4509 format is making psymtabs, because they are all about to be
4510 expanded anyway. */
4511 if ((objfile->flags & OBJF_READNOW))
4512 {
4513 int i;
4514
4515 dwarf2_per_objfile->using_index = 1;
4516 create_all_comp_units (objfile);
0e50663e 4517 create_all_type_units (objfile);
7b9f3c50
DE
4518 dwarf2_per_objfile->quick_file_names_table =
4519 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4520
1fd400ff 4521 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4522 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4523 {
8832e7e3 4524 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4525
e254ef6a
DE
4526 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4527 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4528 }
4529
4530 /* Return 1 so that gdb sees the "quick" functions. However,
4531 these functions will be no-ops because we will have expanded
4532 all symtabs. */
4533 return 1;
4534 }
4535
4536 if (dwarf2_read_index (objfile))
4537 return 1;
4538
9291a0cd
TT
4539 return 0;
4540}
4541
4542\f
4543
dce234bc
PP
4544/* Build a partial symbol table. */
4545
4546void
f29dff0a 4547dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4548{
c9bf0622 4549
af5bf4ad
SM
4550 if (objfile->global_psymbols.capacity () == 0
4551 && objfile->static_psymbols.capacity () == 0)
4552 init_psymbol_list (objfile, 1024);
c906108c 4553
492d29ea 4554 TRY
c9bf0622
TT
4555 {
4556 /* This isn't really ideal: all the data we allocate on the
4557 objfile's obstack is still uselessly kept around. However,
4558 freeing it seems unsafe. */
906768f9 4559 psymtab_discarder psymtabs (objfile);
c9bf0622 4560 dwarf2_build_psymtabs_hard (objfile);
906768f9 4561 psymtabs.keep ();
c9bf0622 4562 }
492d29ea
PA
4563 CATCH (except, RETURN_MASK_ERROR)
4564 {
4565 exception_print (gdb_stderr, except);
4566 }
4567 END_CATCH
c906108c 4568}
c906108c 4569
1ce1cefd
DE
4570/* Return the total length of the CU described by HEADER. */
4571
4572static unsigned int
4573get_cu_length (const struct comp_unit_head *header)
4574{
4575 return header->initial_length_size + header->length;
4576}
4577
9c541725 4578/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4579
9c541725
PA
4580static inline bool
4581offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4582{
9c541725
PA
4583 sect_offset bottom = cu_header->sect_off;
4584 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4585
9c541725 4586 return sect_off >= bottom && sect_off < top;
45452591
DE
4587}
4588
3b80fe9b
DE
4589/* Find the base address of the compilation unit for range lists and
4590 location lists. It will normally be specified by DW_AT_low_pc.
4591 In DWARF-3 draft 4, the base address could be overridden by
4592 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4593 compilation units with discontinuous ranges. */
4594
4595static void
4596dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4597{
4598 struct attribute *attr;
4599
4600 cu->base_known = 0;
4601 cu->base_address = 0;
4602
4603 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4604 if (attr)
4605 {
31aa7e4e 4606 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4607 cu->base_known = 1;
4608 }
4609 else
4610 {
4611 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4612 if (attr)
4613 {
31aa7e4e 4614 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4615 cu->base_known = 1;
4616 }
4617 }
4618}
4619
93311388 4620/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4621 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4622 NOTE: This leaves members offset, first_die_offset to be filled in
4623 by the caller. */
107d2387 4624
d521ce57 4625static const gdb_byte *
107d2387 4626read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4627 const gdb_byte *info_ptr,
4628 struct dwarf2_section_info *section,
4629 rcuh_kind section_kind)
107d2387
AC
4630{
4631 int signed_addr;
891d2f0b 4632 unsigned int bytes_read;
43988095
JK
4633 const char *filename = get_section_file_name (section);
4634 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4635
4636 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4637 cu_header->initial_length_size = bytes_read;
4638 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4639 info_ptr += bytes_read;
107d2387
AC
4640 cu_header->version = read_2_bytes (abfd, info_ptr);
4641 info_ptr += 2;
43988095
JK
4642 if (cu_header->version < 5)
4643 switch (section_kind)
4644 {
4645 case rcuh_kind::COMPILE:
4646 cu_header->unit_type = DW_UT_compile;
4647 break;
4648 case rcuh_kind::TYPE:
4649 cu_header->unit_type = DW_UT_type;
4650 break;
4651 default:
4652 internal_error (__FILE__, __LINE__,
4653 _("read_comp_unit_head: invalid section_kind"));
4654 }
4655 else
4656 {
4657 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4658 (read_1_byte (abfd, info_ptr));
4659 info_ptr += 1;
4660 switch (cu_header->unit_type)
4661 {
4662 case DW_UT_compile:
4663 if (section_kind != rcuh_kind::COMPILE)
4664 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4665 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4666 filename);
4667 break;
4668 case DW_UT_type:
4669 section_kind = rcuh_kind::TYPE;
4670 break;
4671 default:
4672 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4673 "(is %d, should be %d or %d) [in module %s]"),
4674 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4675 }
4676
4677 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4678 info_ptr += 1;
4679 }
9c541725
PA
4680 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4681 cu_header,
4682 &bytes_read);
613e1657 4683 info_ptr += bytes_read;
43988095
JK
4684 if (cu_header->version < 5)
4685 {
4686 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4687 info_ptr += 1;
4688 }
107d2387
AC
4689 signed_addr = bfd_get_sign_extend_vma (abfd);
4690 if (signed_addr < 0)
8e65ff28 4691 internal_error (__FILE__, __LINE__,
e2e0b3e5 4692 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4693 cu_header->signed_addr_p = signed_addr;
c764a876 4694
43988095
JK
4695 if (section_kind == rcuh_kind::TYPE)
4696 {
4697 LONGEST type_offset;
4698
4699 cu_header->signature = read_8_bytes (abfd, info_ptr);
4700 info_ptr += 8;
4701
4702 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4703 info_ptr += bytes_read;
9c541725
PA
4704 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4705 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4706 error (_("Dwarf Error: Too big type_offset in compilation unit "
4707 "header (is %s) [in module %s]"), plongest (type_offset),
4708 filename);
4709 }
4710
107d2387
AC
4711 return info_ptr;
4712}
4713
36586728
TT
4714/* Helper function that returns the proper abbrev section for
4715 THIS_CU. */
4716
4717static struct dwarf2_section_info *
4718get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4719{
4720 struct dwarf2_section_info *abbrev;
4721
4722 if (this_cu->is_dwz)
4723 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4724 else
4725 abbrev = &dwarf2_per_objfile->abbrev;
4726
4727 return abbrev;
4728}
4729
9ff913ba
DE
4730/* Subroutine of read_and_check_comp_unit_head and
4731 read_and_check_type_unit_head to simplify them.
4732 Perform various error checking on the header. */
4733
4734static void
4735error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4736 struct dwarf2_section_info *section,
4737 struct dwarf2_section_info *abbrev_section)
9ff913ba 4738{
a32a8923 4739 const char *filename = get_section_file_name (section);
9ff913ba 4740
43988095 4741 if (header->version < 2 || header->version > 5)
9ff913ba 4742 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4743 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4744 filename);
4745
9c541725 4746 if (to_underlying (header->abbrev_sect_off)
36586728 4747 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4748 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4749 "(offset 0x%x + 6) [in module %s]"),
4750 to_underlying (header->abbrev_sect_off),
4751 to_underlying (header->sect_off),
9ff913ba
DE
4752 filename);
4753
9c541725 4754 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4755 avoid potential 32-bit overflow. */
9c541725 4756 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4757 > section->size)
9c541725
PA
4758 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4759 "(offset 0x%x + 0) [in module %s]"),
4760 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4761 filename);
4762}
4763
4764/* Read in a CU/TU header and perform some basic error checking.
4765 The contents of the header are stored in HEADER.
4766 The result is a pointer to the start of the first DIE. */
adabb602 4767
d521ce57 4768static const gdb_byte *
9ff913ba
DE
4769read_and_check_comp_unit_head (struct comp_unit_head *header,
4770 struct dwarf2_section_info *section,
4bdcc0c1 4771 struct dwarf2_section_info *abbrev_section,
d521ce57 4772 const gdb_byte *info_ptr,
43988095 4773 rcuh_kind section_kind)
72bf9492 4774{
d521ce57 4775 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4776 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4777
9c541725 4778 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4779
43988095 4780 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4781
9c541725 4782 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4783
4bdcc0c1 4784 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4785
4786 return info_ptr;
348e048f
DE
4787}
4788
f4dc4d17
DE
4789/* Fetch the abbreviation table offset from a comp or type unit header. */
4790
4791static sect_offset
4792read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4793 sect_offset sect_off)
f4dc4d17 4794{
a32a8923 4795 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4796 const gdb_byte *info_ptr;
ac298888 4797 unsigned int initial_length_size, offset_size;
43988095 4798 uint16_t version;
f4dc4d17
DE
4799
4800 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4801 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4802 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4803 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4804 info_ptr += initial_length_size;
4805
4806 version = read_2_bytes (abfd, info_ptr);
4807 info_ptr += 2;
4808 if (version >= 5)
4809 {
4810 /* Skip unit type and address size. */
4811 info_ptr += 2;
4812 }
4813
9c541725 4814 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4815}
4816
aaa75496
JB
4817/* Allocate a new partial symtab for file named NAME and mark this new
4818 partial symtab as being an include of PST. */
4819
4820static void
d521ce57 4821dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4822 struct objfile *objfile)
4823{
4824 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4825
fbd9ab74
JK
4826 if (!IS_ABSOLUTE_PATH (subpst->filename))
4827 {
4828 /* It shares objfile->objfile_obstack. */
4829 subpst->dirname = pst->dirname;
4830 }
4831
aaa75496
JB
4832 subpst->textlow = 0;
4833 subpst->texthigh = 0;
4834
8d749320
SM
4835 subpst->dependencies
4836 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4837 subpst->dependencies[0] = pst;
4838 subpst->number_of_dependencies = 1;
4839
4840 subpst->globals_offset = 0;
4841 subpst->n_global_syms = 0;
4842 subpst->statics_offset = 0;
4843 subpst->n_static_syms = 0;
43f3e411 4844 subpst->compunit_symtab = NULL;
aaa75496
JB
4845 subpst->read_symtab = pst->read_symtab;
4846 subpst->readin = 0;
4847
4848 /* No private part is necessary for include psymtabs. This property
4849 can be used to differentiate between such include psymtabs and
10b3939b 4850 the regular ones. */
58a9656e 4851 subpst->read_symtab_private = NULL;
aaa75496
JB
4852}
4853
4854/* Read the Line Number Program data and extract the list of files
4855 included by the source file represented by PST. Build an include
d85a05f0 4856 partial symtab for each of these included files. */
aaa75496
JB
4857
4858static void
4859dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4860 struct die_info *die,
4861 struct partial_symtab *pst)
aaa75496 4862{
fff8551c 4863 line_header_up lh;
d85a05f0 4864 struct attribute *attr;
aaa75496 4865
d85a05f0
DJ
4866 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4867 if (attr)
9c541725 4868 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4869 if (lh == NULL)
4870 return; /* No linetable, so no includes. */
4871
c6da4cef 4872 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4873 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4874}
4875
348e048f 4876static hashval_t
52dc124a 4877hash_signatured_type (const void *item)
348e048f 4878{
9a3c8263
SM
4879 const struct signatured_type *sig_type
4880 = (const struct signatured_type *) item;
9a619af0 4881
348e048f 4882 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4883 return sig_type->signature;
348e048f
DE
4884}
4885
4886static int
52dc124a 4887eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4888{
9a3c8263
SM
4889 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4890 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4891
348e048f
DE
4892 return lhs->signature == rhs->signature;
4893}
4894
1fd400ff
TT
4895/* Allocate a hash table for signatured types. */
4896
4897static htab_t
673bfd45 4898allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4899{
4900 return htab_create_alloc_ex (41,
52dc124a
DE
4901 hash_signatured_type,
4902 eq_signatured_type,
1fd400ff
TT
4903 NULL,
4904 &objfile->objfile_obstack,
4905 hashtab_obstack_allocate,
4906 dummy_obstack_deallocate);
4907}
4908
d467dd73 4909/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4910
4911static int
d467dd73 4912add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4913{
9a3c8263
SM
4914 struct signatured_type *sigt = (struct signatured_type *) *slot;
4915 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4916
b4dd5633 4917 **datap = sigt;
1fd400ff
TT
4918 ++*datap;
4919
4920 return 1;
4921}
4922
78d4d2c5 4923/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4924 and fill them into TYPES_HTAB. It will process only type units,
4925 therefore DW_UT_type. */
c88ee1f0 4926
78d4d2c5
JK
4927static void
4928create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4929 dwarf2_section_info *section, htab_t &types_htab,
4930 rcuh_kind section_kind)
348e048f 4931{
3019eac3 4932 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4933 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4934 bfd *abfd;
4935 const gdb_byte *info_ptr, *end_ptr;
348e048f 4936
4bdcc0c1
DE
4937 abbrev_section = (dwo_file != NULL
4938 ? &dwo_file->sections.abbrev
4939 : &dwarf2_per_objfile->abbrev);
4940
b4f54984 4941 if (dwarf_read_debug)
43988095
JK
4942 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4943 get_section_name (section),
a32a8923 4944 get_section_file_name (abbrev_section));
09406207 4945
78d4d2c5
JK
4946 dwarf2_read_section (objfile, section);
4947 info_ptr = section->buffer;
348e048f 4948
78d4d2c5
JK
4949 if (info_ptr == NULL)
4950 return;
348e048f 4951
78d4d2c5
JK
4952 /* We can't set abfd until now because the section may be empty or
4953 not present, in which case the bfd is unknown. */
4954 abfd = get_section_bfd_owner (section);
348e048f 4955
78d4d2c5
JK
4956 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4957 because we don't need to read any dies: the signature is in the
4958 header. */
3019eac3 4959
78d4d2c5
JK
4960 end_ptr = info_ptr + section->size;
4961 while (info_ptr < end_ptr)
4962 {
78d4d2c5
JK
4963 struct signatured_type *sig_type;
4964 struct dwo_unit *dwo_tu;
4965 void **slot;
4966 const gdb_byte *ptr = info_ptr;
4967 struct comp_unit_head header;
4968 unsigned int length;
8b70b953 4969
9c541725 4970 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4971
a49dd8dd
JK
4972 /* Initialize it due to a false compiler warning. */
4973 header.signature = -1;
9c541725 4974 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4975
78d4d2c5
JK
4976 /* We need to read the type's signature in order to build the hash
4977 table, but we don't need anything else just yet. */
348e048f 4978
43988095
JK
4979 ptr = read_and_check_comp_unit_head (&header, section,
4980 abbrev_section, ptr, section_kind);
348e048f 4981
78d4d2c5 4982 length = get_cu_length (&header);
6caca83c 4983
78d4d2c5
JK
4984 /* Skip dummy type units. */
4985 if (ptr >= info_ptr + length
43988095
JK
4986 || peek_abbrev_code (abfd, ptr) == 0
4987 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4988 {
4989 info_ptr += length;
4990 continue;
4991 }
dee91e82 4992
78d4d2c5
JK
4993 if (types_htab == NULL)
4994 {
4995 if (dwo_file)
4996 types_htab = allocate_dwo_unit_table (objfile);
4997 else
4998 types_htab = allocate_signatured_type_table (objfile);
4999 }
8b70b953 5000
78d4d2c5
JK
5001 if (dwo_file)
5002 {
5003 sig_type = NULL;
5004 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5005 struct dwo_unit);
5006 dwo_tu->dwo_file = dwo_file;
43988095 5007 dwo_tu->signature = header.signature;
9c541725 5008 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 5009 dwo_tu->section = section;
9c541725 5010 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
5011 dwo_tu->length = length;
5012 }
5013 else
5014 {
5015 /* N.B.: type_offset is not usable if this type uses a DWO file.
5016 The real type_offset is in the DWO file. */
5017 dwo_tu = NULL;
5018 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5019 struct signatured_type);
43988095 5020 sig_type->signature = header.signature;
9c541725 5021 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
5022 sig_type->per_cu.objfile = objfile;
5023 sig_type->per_cu.is_debug_types = 1;
5024 sig_type->per_cu.section = section;
9c541725 5025 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
5026 sig_type->per_cu.length = length;
5027 }
5028
5029 slot = htab_find_slot (types_htab,
5030 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5031 INSERT);
5032 gdb_assert (slot != NULL);
5033 if (*slot != NULL)
5034 {
9c541725 5035 sect_offset dup_sect_off;
0349ea22 5036
3019eac3
DE
5037 if (dwo_file)
5038 {
78d4d2c5
JK
5039 const struct dwo_unit *dup_tu
5040 = (const struct dwo_unit *) *slot;
5041
9c541725 5042 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
5043 }
5044 else
5045 {
78d4d2c5
JK
5046 const struct signatured_type *dup_tu
5047 = (const struct signatured_type *) *slot;
5048
9c541725 5049 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 5050 }
8b70b953 5051
78d4d2c5
JK
5052 complaint (&symfile_complaints,
5053 _("debug type entry at offset 0x%x is duplicate to"
5054 " the entry at offset 0x%x, signature %s"),
9c541725 5055 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 5056 hex_string (header.signature));
78d4d2c5
JK
5057 }
5058 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 5059
78d4d2c5
JK
5060 if (dwarf_read_debug > 1)
5061 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 5062 to_underlying (sect_off),
43988095 5063 hex_string (header.signature));
3019eac3 5064
78d4d2c5
JK
5065 info_ptr += length;
5066 }
5067}
3019eac3 5068
78d4d2c5
JK
5069/* Create the hash table of all entries in the .debug_types
5070 (or .debug_types.dwo) section(s).
5071 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5072 otherwise it is NULL.
b3c8eb43 5073
78d4d2c5 5074 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5075
78d4d2c5 5076 Note: This function processes DWO files only, not DWP files. */
348e048f 5077
78d4d2c5
JK
5078static void
5079create_debug_types_hash_table (struct dwo_file *dwo_file,
5080 VEC (dwarf2_section_info_def) *types,
5081 htab_t &types_htab)
5082{
5083 int ix;
5084 struct dwarf2_section_info *section;
5085
5086 if (VEC_empty (dwarf2_section_info_def, types))
5087 return;
348e048f 5088
78d4d2c5
JK
5089 for (ix = 0;
5090 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5091 ++ix)
43988095
JK
5092 create_debug_type_hash_table (dwo_file, section, types_htab,
5093 rcuh_kind::TYPE);
3019eac3
DE
5094}
5095
5096/* Create the hash table of all entries in the .debug_types section,
5097 and initialize all_type_units.
5098 The result is zero if there is an error (e.g. missing .debug_types section),
5099 otherwise non-zero. */
5100
5101static int
5102create_all_type_units (struct objfile *objfile)
5103{
78d4d2c5 5104 htab_t types_htab = NULL;
b4dd5633 5105 struct signatured_type **iter;
3019eac3 5106
43988095
JK
5107 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5108 rcuh_kind::COMPILE);
78d4d2c5 5109 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5110 if (types_htab == NULL)
5111 {
5112 dwarf2_per_objfile->signatured_types = NULL;
5113 return 0;
5114 }
5115
348e048f
DE
5116 dwarf2_per_objfile->signatured_types = types_htab;
5117
6aa5f3a6
DE
5118 dwarf2_per_objfile->n_type_units
5119 = dwarf2_per_objfile->n_allocated_type_units
5120 = htab_elements (types_htab);
8d749320
SM
5121 dwarf2_per_objfile->all_type_units =
5122 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5123 iter = &dwarf2_per_objfile->all_type_units[0];
5124 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5125 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5126 == dwarf2_per_objfile->n_type_units);
1fd400ff 5127
348e048f
DE
5128 return 1;
5129}
5130
6aa5f3a6
DE
5131/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5132 If SLOT is non-NULL, it is the entry to use in the hash table.
5133 Otherwise we find one. */
5134
5135static struct signatured_type *
5136add_type_unit (ULONGEST sig, void **slot)
5137{
5138 struct objfile *objfile = dwarf2_per_objfile->objfile;
5139 int n_type_units = dwarf2_per_objfile->n_type_units;
5140 struct signatured_type *sig_type;
5141
5142 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5143 ++n_type_units;
5144 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5145 {
5146 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5147 dwarf2_per_objfile->n_allocated_type_units = 1;
5148 dwarf2_per_objfile->n_allocated_type_units *= 2;
5149 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5150 = XRESIZEVEC (struct signatured_type *,
5151 dwarf2_per_objfile->all_type_units,
5152 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5153 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5154 }
5155 dwarf2_per_objfile->n_type_units = n_type_units;
5156
5157 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5158 struct signatured_type);
5159 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5160 sig_type->signature = sig;
5161 sig_type->per_cu.is_debug_types = 1;
5162 if (dwarf2_per_objfile->using_index)
5163 {
5164 sig_type->per_cu.v.quick =
5165 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5166 struct dwarf2_per_cu_quick_data);
5167 }
5168
5169 if (slot == NULL)
5170 {
5171 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5172 sig_type, INSERT);
5173 }
5174 gdb_assert (*slot == NULL);
5175 *slot = sig_type;
5176 /* The rest of sig_type must be filled in by the caller. */
5177 return sig_type;
5178}
5179
a2ce51a0
DE
5180/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5181 Fill in SIG_ENTRY with DWO_ENTRY. */
5182
5183static void
5184fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5185 struct signatured_type *sig_entry,
5186 struct dwo_unit *dwo_entry)
5187{
7ee85ab1 5188 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5189 gdb_assert (! sig_entry->per_cu.queued);
5190 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5191 if (dwarf2_per_objfile->using_index)
5192 {
5193 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5194 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5195 }
5196 else
5197 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5198 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5199 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5200 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5201 gdb_assert (sig_entry->dwo_unit == NULL);
5202
5203 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5204 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5205 sig_entry->per_cu.length = dwo_entry->length;
5206 sig_entry->per_cu.reading_dwo_directly = 1;
5207 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5208 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5209 sig_entry->dwo_unit = dwo_entry;
5210}
5211
5212/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5213 If we haven't read the TU yet, create the signatured_type data structure
5214 for a TU to be read in directly from a DWO file, bypassing the stub.
5215 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5216 using .gdb_index, then when reading a CU we want to stay in the DWO file
5217 containing that CU. Otherwise we could end up reading several other DWO
5218 files (due to comdat folding) to process the transitive closure of all the
5219 mentioned TUs, and that can be slow. The current DWO file will have every
5220 type signature that it needs.
a2ce51a0
DE
5221 We only do this for .gdb_index because in the psymtab case we already have
5222 to read all the DWOs to build the type unit groups. */
5223
5224static struct signatured_type *
5225lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5226{
5227 struct objfile *objfile = dwarf2_per_objfile->objfile;
5228 struct dwo_file *dwo_file;
5229 struct dwo_unit find_dwo_entry, *dwo_entry;
5230 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5231 void **slot;
a2ce51a0
DE
5232
5233 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5234
6aa5f3a6
DE
5235 /* If TU skeletons have been removed then we may not have read in any
5236 TUs yet. */
5237 if (dwarf2_per_objfile->signatured_types == NULL)
5238 {
5239 dwarf2_per_objfile->signatured_types
5240 = allocate_signatured_type_table (objfile);
5241 }
a2ce51a0
DE
5242
5243 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5244 Use the global signatured_types array to do our own comdat-folding
5245 of types. If this is the first time we're reading this TU, and
5246 the TU has an entry in .gdb_index, replace the recorded data from
5247 .gdb_index with this TU. */
a2ce51a0 5248
a2ce51a0 5249 find_sig_entry.signature = sig;
6aa5f3a6
DE
5250 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5251 &find_sig_entry, INSERT);
9a3c8263 5252 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5253
5254 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5255 read. Don't reassign the global entry to point to this DWO if that's
5256 the case. Also note that if the TU is already being read, it may not
5257 have come from a DWO, the program may be a mix of Fission-compiled
5258 code and non-Fission-compiled code. */
5259
5260 /* Have we already tried to read this TU?
5261 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5262 needn't exist in the global table yet). */
5263 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5264 return sig_entry;
5265
6aa5f3a6
DE
5266 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5267 dwo_unit of the TU itself. */
5268 dwo_file = cu->dwo_unit->dwo_file;
5269
a2ce51a0
DE
5270 /* Ok, this is the first time we're reading this TU. */
5271 if (dwo_file->tus == NULL)
5272 return NULL;
5273 find_dwo_entry.signature = sig;
9a3c8263 5274 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5275 if (dwo_entry == NULL)
5276 return NULL;
5277
6aa5f3a6
DE
5278 /* If the global table doesn't have an entry for this TU, add one. */
5279 if (sig_entry == NULL)
5280 sig_entry = add_type_unit (sig, slot);
5281
a2ce51a0 5282 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5283 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5284 return sig_entry;
5285}
5286
a2ce51a0
DE
5287/* Subroutine of lookup_signatured_type.
5288 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5289 then try the DWP file. If the TU stub (skeleton) has been removed then
5290 it won't be in .gdb_index. */
a2ce51a0
DE
5291
5292static struct signatured_type *
5293lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5294{
5295 struct objfile *objfile = dwarf2_per_objfile->objfile;
5296 struct dwp_file *dwp_file = get_dwp_file ();
5297 struct dwo_unit *dwo_entry;
5298 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5299 void **slot;
a2ce51a0
DE
5300
5301 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5302 gdb_assert (dwp_file != NULL);
5303
6aa5f3a6
DE
5304 /* If TU skeletons have been removed then we may not have read in any
5305 TUs yet. */
5306 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5307 {
6aa5f3a6
DE
5308 dwarf2_per_objfile->signatured_types
5309 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5310 }
5311
6aa5f3a6
DE
5312 find_sig_entry.signature = sig;
5313 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5314 &find_sig_entry, INSERT);
9a3c8263 5315 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5316
5317 /* Have we already tried to read this TU?
5318 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5319 needn't exist in the global table yet). */
5320 if (sig_entry != NULL)
5321 return sig_entry;
5322
a2ce51a0
DE
5323 if (dwp_file->tus == NULL)
5324 return NULL;
57d63ce2
DE
5325 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5326 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5327 if (dwo_entry == NULL)
5328 return NULL;
5329
6aa5f3a6 5330 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5331 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5332
a2ce51a0
DE
5333 return sig_entry;
5334}
5335
380bca97 5336/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5337 Returns NULL if signature SIG is not present in the table.
5338 It is up to the caller to complain about this. */
348e048f
DE
5339
5340static struct signatured_type *
a2ce51a0 5341lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5342{
a2ce51a0
DE
5343 if (cu->dwo_unit
5344 && dwarf2_per_objfile->using_index)
5345 {
5346 /* We're in a DWO/DWP file, and we're using .gdb_index.
5347 These cases require special processing. */
5348 if (get_dwp_file () == NULL)
5349 return lookup_dwo_signatured_type (cu, sig);
5350 else
5351 return lookup_dwp_signatured_type (cu, sig);
5352 }
5353 else
5354 {
5355 struct signatured_type find_entry, *entry;
348e048f 5356
a2ce51a0
DE
5357 if (dwarf2_per_objfile->signatured_types == NULL)
5358 return NULL;
5359 find_entry.signature = sig;
9a3c8263
SM
5360 entry = ((struct signatured_type *)
5361 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5362 return entry;
5363 }
348e048f 5364}
42e7ad6c
DE
5365\f
5366/* Low level DIE reading support. */
348e048f 5367
d85a05f0
DJ
5368/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5369
5370static void
5371init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5372 struct dwarf2_cu *cu,
3019eac3
DE
5373 struct dwarf2_section_info *section,
5374 struct dwo_file *dwo_file)
d85a05f0 5375{
fceca515 5376 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5377 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5378 reader->cu = cu;
3019eac3 5379 reader->dwo_file = dwo_file;
dee91e82
DE
5380 reader->die_section = section;
5381 reader->buffer = section->buffer;
f664829e 5382 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5383 reader->comp_dir = NULL;
d85a05f0
DJ
5384}
5385
b0c7bfa9
DE
5386/* Subroutine of init_cutu_and_read_dies to simplify it.
5387 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5388 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5389 already.
5390
5391 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5392 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5393 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5394 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5395 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5396 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5397 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5398 are filled in with the info of the DIE from the DWO file.
5399 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5400 provided an abbrev table to use.
5401 The result is non-zero if a valid (non-dummy) DIE was found. */
5402
5403static int
5404read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5405 struct dwo_unit *dwo_unit,
5406 int abbrev_table_provided,
5407 struct die_info *stub_comp_unit_die,
a2ce51a0 5408 const char *stub_comp_dir,
b0c7bfa9 5409 struct die_reader_specs *result_reader,
d521ce57 5410 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5411 struct die_info **result_comp_unit_die,
5412 int *result_has_children)
5413{
5414 struct objfile *objfile = dwarf2_per_objfile->objfile;
5415 struct dwarf2_cu *cu = this_cu->cu;
5416 struct dwarf2_section_info *section;
5417 bfd *abfd;
d521ce57 5418 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5419 ULONGEST signature; /* Or dwo_id. */
5420 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5421 int i,num_extra_attrs;
5422 struct dwarf2_section_info *dwo_abbrev_section;
5423 struct attribute *attr;
5424 struct die_info *comp_unit_die;
5425
b0aeadb3
DE
5426 /* At most one of these may be provided. */
5427 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5428
b0c7bfa9
DE
5429 /* These attributes aren't processed until later:
5430 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5431 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5432 referenced later. However, these attributes are found in the stub
5433 which we won't have later. In order to not impose this complication
5434 on the rest of the code, we read them here and copy them to the
5435 DWO CU/TU die. */
b0c7bfa9
DE
5436
5437 stmt_list = NULL;
5438 low_pc = NULL;
5439 high_pc = NULL;
5440 ranges = NULL;
5441 comp_dir = NULL;
5442
5443 if (stub_comp_unit_die != NULL)
5444 {
5445 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5446 DWO file. */
5447 if (! this_cu->is_debug_types)
5448 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5449 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5450 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5451 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5452 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5453
5454 /* There should be a DW_AT_addr_base attribute here (if needed).
5455 We need the value before we can process DW_FORM_GNU_addr_index. */
5456 cu->addr_base = 0;
5457 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5458 if (attr)
5459 cu->addr_base = DW_UNSND (attr);
5460
5461 /* There should be a DW_AT_ranges_base attribute here (if needed).
5462 We need the value before we can process DW_AT_ranges. */
5463 cu->ranges_base = 0;
5464 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5465 if (attr)
5466 cu->ranges_base = DW_UNSND (attr);
5467 }
a2ce51a0
DE
5468 else if (stub_comp_dir != NULL)
5469 {
5470 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5471 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5472 comp_dir->name = DW_AT_comp_dir;
5473 comp_dir->form = DW_FORM_string;
5474 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5475 DW_STRING (comp_dir) = stub_comp_dir;
5476 }
b0c7bfa9
DE
5477
5478 /* Set up for reading the DWO CU/TU. */
5479 cu->dwo_unit = dwo_unit;
5480 section = dwo_unit->section;
5481 dwarf2_read_section (objfile, section);
a32a8923 5482 abfd = get_section_bfd_owner (section);
9c541725
PA
5483 begin_info_ptr = info_ptr = (section->buffer
5484 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5485 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5486 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5487
5488 if (this_cu->is_debug_types)
5489 {
b0c7bfa9
DE
5490 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5491
43988095 5492 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5493 dwo_abbrev_section,
43988095 5494 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5495 /* This is not an assert because it can be caused by bad debug info. */
43988095 5496 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5497 {
5498 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5499 " TU at offset 0x%x [in module %s]"),
5500 hex_string (sig_type->signature),
43988095 5501 hex_string (cu->header.signature),
9c541725 5502 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5503 bfd_get_filename (abfd));
5504 }
9c541725 5505 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5506 /* For DWOs coming from DWP files, we don't know the CU length
5507 nor the type's offset in the TU until now. */
5508 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5509 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5510
5511 /* Establish the type offset that can be used to lookup the type.
5512 For DWO files, we don't know it until now. */
9c541725
PA
5513 sig_type->type_offset_in_section
5514 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5515 }
5516 else
5517 {
5518 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5519 dwo_abbrev_section,
43988095 5520 info_ptr, rcuh_kind::COMPILE);
9c541725 5521 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5522 /* For DWOs coming from DWP files, we don't know the CU length
5523 until now. */
5524 dwo_unit->length = get_cu_length (&cu->header);
5525 }
5526
02142a6c
DE
5527 /* Replace the CU's original abbrev table with the DWO's.
5528 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5529 if (abbrev_table_provided)
5530 {
5531 /* Don't free the provided abbrev table, the caller of
5532 init_cutu_and_read_dies owns it. */
5533 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5534 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5535 make_cleanup (dwarf2_free_abbrev_table, cu);
5536 }
5537 else
5538 {
5539 dwarf2_free_abbrev_table (cu);
5540 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5541 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5542 }
5543
5544 /* Read in the die, but leave space to copy over the attributes
5545 from the stub. This has the benefit of simplifying the rest of
5546 the code - all the work to maintain the illusion of a single
5547 DW_TAG_{compile,type}_unit DIE is done here. */
5548 num_extra_attrs = ((stmt_list != NULL)
5549 + (low_pc != NULL)
5550 + (high_pc != NULL)
5551 + (ranges != NULL)
5552 + (comp_dir != NULL));
5553 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5554 result_has_children, num_extra_attrs);
5555
5556 /* Copy over the attributes from the stub to the DIE we just read in. */
5557 comp_unit_die = *result_comp_unit_die;
5558 i = comp_unit_die->num_attrs;
5559 if (stmt_list != NULL)
5560 comp_unit_die->attrs[i++] = *stmt_list;
5561 if (low_pc != NULL)
5562 comp_unit_die->attrs[i++] = *low_pc;
5563 if (high_pc != NULL)
5564 comp_unit_die->attrs[i++] = *high_pc;
5565 if (ranges != NULL)
5566 comp_unit_die->attrs[i++] = *ranges;
5567 if (comp_dir != NULL)
5568 comp_unit_die->attrs[i++] = *comp_dir;
5569 comp_unit_die->num_attrs += num_extra_attrs;
5570
b4f54984 5571 if (dwarf_die_debug)
bf6af496
DE
5572 {
5573 fprintf_unfiltered (gdb_stdlog,
5574 "Read die from %s@0x%x of %s:\n",
a32a8923 5575 get_section_name (section),
bf6af496
DE
5576 (unsigned) (begin_info_ptr - section->buffer),
5577 bfd_get_filename (abfd));
b4f54984 5578 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5579 }
5580
a2ce51a0
DE
5581 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5582 TUs by skipping the stub and going directly to the entry in the DWO file.
5583 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5584 to get it via circuitous means. Blech. */
5585 if (comp_dir != NULL)
5586 result_reader->comp_dir = DW_STRING (comp_dir);
5587
b0c7bfa9
DE
5588 /* Skip dummy compilation units. */
5589 if (info_ptr >= begin_info_ptr + dwo_unit->length
5590 || peek_abbrev_code (abfd, info_ptr) == 0)
5591 return 0;
5592
5593 *result_info_ptr = info_ptr;
5594 return 1;
5595}
5596
5597/* Subroutine of init_cutu_and_read_dies to simplify it.
5598 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5599 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5600
5601static struct dwo_unit *
5602lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5603 struct die_info *comp_unit_die)
5604{
5605 struct dwarf2_cu *cu = this_cu->cu;
5606 struct attribute *attr;
5607 ULONGEST signature;
5608 struct dwo_unit *dwo_unit;
5609 const char *comp_dir, *dwo_name;
5610
a2ce51a0
DE
5611 gdb_assert (cu != NULL);
5612
b0c7bfa9 5613 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5614 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5615 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5616
5617 if (this_cu->is_debug_types)
5618 {
5619 struct signatured_type *sig_type;
5620
5621 /* Since this_cu is the first member of struct signatured_type,
5622 we can go from a pointer to one to a pointer to the other. */
5623 sig_type = (struct signatured_type *) this_cu;
5624 signature = sig_type->signature;
5625 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5626 }
5627 else
5628 {
5629 struct attribute *attr;
5630
5631 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5632 if (! attr)
5633 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5634 " [in module %s]"),
4262abfb 5635 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5636 signature = DW_UNSND (attr);
5637 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5638 signature);
5639 }
5640
b0c7bfa9
DE
5641 return dwo_unit;
5642}
5643
a2ce51a0 5644/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5645 See it for a description of the parameters.
5646 Read a TU directly from a DWO file, bypassing the stub.
5647
5648 Note: This function could be a little bit simpler if we shared cleanups
5649 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5650 to do, so we keep this function self-contained. Or we could move this
5651 into our caller, but it's complex enough already. */
a2ce51a0
DE
5652
5653static void
6aa5f3a6
DE
5654init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5655 int use_existing_cu, int keep,
a2ce51a0
DE
5656 die_reader_func_ftype *die_reader_func,
5657 void *data)
5658{
5659 struct dwarf2_cu *cu;
5660 struct signatured_type *sig_type;
6aa5f3a6 5661 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5662 struct die_reader_specs reader;
5663 const gdb_byte *info_ptr;
5664 struct die_info *comp_unit_die;
5665 int has_children;
5666
5667 /* Verify we can do the following downcast, and that we have the
5668 data we need. */
5669 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5670 sig_type = (struct signatured_type *) this_cu;
5671 gdb_assert (sig_type->dwo_unit != NULL);
5672
5673 cleanups = make_cleanup (null_cleanup, NULL);
5674
6aa5f3a6
DE
5675 if (use_existing_cu && this_cu->cu != NULL)
5676 {
5677 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5678 cu = this_cu->cu;
5679 /* There's no need to do the rereading_dwo_cu handling that
5680 init_cutu_and_read_dies does since we don't read the stub. */
5681 }
5682 else
5683 {
5684 /* If !use_existing_cu, this_cu->cu must be NULL. */
5685 gdb_assert (this_cu->cu == NULL);
8d749320 5686 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5687 init_one_comp_unit (cu, this_cu);
5688 /* If an error occurs while loading, release our storage. */
5689 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5690 }
5691
5692 /* A future optimization, if needed, would be to use an existing
5693 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5694 could share abbrev tables. */
a2ce51a0
DE
5695
5696 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5697 0 /* abbrev_table_provided */,
5698 NULL /* stub_comp_unit_die */,
5699 sig_type->dwo_unit->dwo_file->comp_dir,
5700 &reader, &info_ptr,
5701 &comp_unit_die, &has_children) == 0)
5702 {
5703 /* Dummy die. */
5704 do_cleanups (cleanups);
5705 return;
5706 }
5707
5708 /* All the "real" work is done here. */
5709 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5710
6aa5f3a6 5711 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5712 but the alternative is making the latter more complex.
5713 This function is only for the special case of using DWO files directly:
5714 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5715 if (free_cu_cleanup != NULL)
a2ce51a0 5716 {
6aa5f3a6
DE
5717 if (keep)
5718 {
5719 /* We've successfully allocated this compilation unit. Let our
5720 caller clean it up when finished with it. */
5721 discard_cleanups (free_cu_cleanup);
a2ce51a0 5722
6aa5f3a6
DE
5723 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5724 So we have to manually free the abbrev table. */
5725 dwarf2_free_abbrev_table (cu);
a2ce51a0 5726
6aa5f3a6
DE
5727 /* Link this CU into read_in_chain. */
5728 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5729 dwarf2_per_objfile->read_in_chain = this_cu;
5730 }
5731 else
5732 do_cleanups (free_cu_cleanup);
a2ce51a0 5733 }
a2ce51a0
DE
5734
5735 do_cleanups (cleanups);
5736}
5737
fd820528 5738/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5739 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5740
f4dc4d17
DE
5741 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5742 Otherwise the table specified in the comp unit header is read in and used.
5743 This is an optimization for when we already have the abbrev table.
5744
dee91e82
DE
5745 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5746 Otherwise, a new CU is allocated with xmalloc.
5747
5748 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5749 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5750
5751 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5752 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5753
70221824 5754static void
fd820528 5755init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5756 struct abbrev_table *abbrev_table,
fd820528
DE
5757 int use_existing_cu, int keep,
5758 die_reader_func_ftype *die_reader_func,
5759 void *data)
c906108c 5760{
dee91e82 5761 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5762 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5763 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5764 struct dwarf2_cu *cu;
d521ce57 5765 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5766 struct die_reader_specs reader;
d85a05f0 5767 struct die_info *comp_unit_die;
dee91e82 5768 int has_children;
d85a05f0 5769 struct attribute *attr;
365156ad 5770 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5771 struct signatured_type *sig_type = NULL;
4bdcc0c1 5772 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5773 /* Non-zero if CU currently points to a DWO file and we need to
5774 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5775 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5776 int rereading_dwo_cu = 0;
c906108c 5777
b4f54984 5778 if (dwarf_die_debug)
09406207
DE
5779 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5780 this_cu->is_debug_types ? "type" : "comp",
9c541725 5781 to_underlying (this_cu->sect_off));
09406207 5782
dee91e82
DE
5783 if (use_existing_cu)
5784 gdb_assert (keep);
23745b47 5785
a2ce51a0
DE
5786 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5787 file (instead of going through the stub), short-circuit all of this. */
5788 if (this_cu->reading_dwo_directly)
5789 {
5790 /* Narrow down the scope of possibilities to have to understand. */
5791 gdb_assert (this_cu->is_debug_types);
5792 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5793 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5794 die_reader_func, data);
a2ce51a0
DE
5795 return;
5796 }
5797
dee91e82
DE
5798 cleanups = make_cleanup (null_cleanup, NULL);
5799
5800 /* This is cheap if the section is already read in. */
5801 dwarf2_read_section (objfile, section);
5802
9c541725 5803 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5804
5805 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5806
5807 if (use_existing_cu && this_cu->cu != NULL)
5808 {
5809 cu = this_cu->cu;
42e7ad6c
DE
5810 /* If this CU is from a DWO file we need to start over, we need to
5811 refetch the attributes from the skeleton CU.
5812 This could be optimized by retrieving those attributes from when we
5813 were here the first time: the previous comp_unit_die was stored in
5814 comp_unit_obstack. But there's no data yet that we need this
5815 optimization. */
5816 if (cu->dwo_unit != NULL)
5817 rereading_dwo_cu = 1;
dee91e82
DE
5818 }
5819 else
5820 {
5821 /* If !use_existing_cu, this_cu->cu must be NULL. */
5822 gdb_assert (this_cu->cu == NULL);
8d749320 5823 cu = XNEW (struct dwarf2_cu);
dee91e82 5824 init_one_comp_unit (cu, this_cu);
dee91e82 5825 /* If an error occurs while loading, release our storage. */
365156ad 5826 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5827 }
dee91e82 5828
b0c7bfa9 5829 /* Get the header. */
9c541725 5830 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5831 {
5832 /* We already have the header, there's no need to read it in again. */
9c541725 5833 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5834 }
5835 else
5836 {
3019eac3 5837 if (this_cu->is_debug_types)
dee91e82 5838 {
43988095 5839 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5840 abbrev_section, info_ptr,
43988095 5841 rcuh_kind::TYPE);
dee91e82 5842
42e7ad6c
DE
5843 /* Since per_cu is the first member of struct signatured_type,
5844 we can go from a pointer to one to a pointer to the other. */
5845 sig_type = (struct signatured_type *) this_cu;
43988095 5846 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5847 gdb_assert (sig_type->type_offset_in_tu
5848 == cu->header.type_cu_offset_in_tu);
5849 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5850
42e7ad6c
DE
5851 /* LENGTH has not been set yet for type units if we're
5852 using .gdb_index. */
1ce1cefd 5853 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5854
5855 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5856 sig_type->type_offset_in_section =
5857 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5858
5859 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5860 }
5861 else
5862 {
4bdcc0c1
DE
5863 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5864 abbrev_section,
43988095
JK
5865 info_ptr,
5866 rcuh_kind::COMPILE);
dee91e82 5867
9c541725 5868 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5869 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5870 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5871 }
5872 }
10b3939b 5873
6caca83c 5874 /* Skip dummy compilation units. */
dee91e82 5875 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5876 || peek_abbrev_code (abfd, info_ptr) == 0)
5877 {
dee91e82 5878 do_cleanups (cleanups);
21b2bd31 5879 return;
6caca83c
CC
5880 }
5881
433df2d4
DE
5882 /* If we don't have them yet, read the abbrevs for this compilation unit.
5883 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5884 done. Note that it's important that if the CU had an abbrev table
5885 on entry we don't free it when we're done: Somewhere up the call stack
5886 it may be in use. */
f4dc4d17
DE
5887 if (abbrev_table != NULL)
5888 {
5889 gdb_assert (cu->abbrev_table == NULL);
9c541725 5890 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5891 cu->abbrev_table = abbrev_table;
5892 }
5893 else if (cu->abbrev_table == NULL)
dee91e82 5894 {
4bdcc0c1 5895 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5896 make_cleanup (dwarf2_free_abbrev_table, cu);
5897 }
42e7ad6c
DE
5898 else if (rereading_dwo_cu)
5899 {
5900 dwarf2_free_abbrev_table (cu);
5901 dwarf2_read_abbrevs (cu, abbrev_section);
5902 }
af703f96 5903
dee91e82 5904 /* Read the top level CU/TU die. */
3019eac3 5905 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5906 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5907
b0c7bfa9
DE
5908 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5909 from the DWO file.
5910 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5911 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5912 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5913 if (attr)
5914 {
3019eac3 5915 struct dwo_unit *dwo_unit;
b0c7bfa9 5916 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5917
5918 if (has_children)
6a506a2d
DE
5919 {
5920 complaint (&symfile_complaints,
5921 _("compilation unit with DW_AT_GNU_dwo_name"
5922 " has children (offset 0x%x) [in module %s]"),
9c541725 5923 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5924 }
b0c7bfa9 5925 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5926 if (dwo_unit != NULL)
3019eac3 5927 {
6a506a2d
DE
5928 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5929 abbrev_table != NULL,
a2ce51a0 5930 comp_unit_die, NULL,
6a506a2d
DE
5931 &reader, &info_ptr,
5932 &dwo_comp_unit_die, &has_children) == 0)
5933 {
5934 /* Dummy die. */
5935 do_cleanups (cleanups);
5936 return;
5937 }
5938 comp_unit_die = dwo_comp_unit_die;
5939 }
5940 else
5941 {
5942 /* Yikes, we couldn't find the rest of the DIE, we only have
5943 the stub. A complaint has already been logged. There's
5944 not much more we can do except pass on the stub DIE to
5945 die_reader_func. We don't want to throw an error on bad
5946 debug info. */
3019eac3
DE
5947 }
5948 }
5949
b0c7bfa9 5950 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5951 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5952
b0c7bfa9 5953 /* Done, clean up. */
365156ad 5954 if (free_cu_cleanup != NULL)
348e048f 5955 {
365156ad
TT
5956 if (keep)
5957 {
5958 /* We've successfully allocated this compilation unit. Let our
5959 caller clean it up when finished with it. */
5960 discard_cleanups (free_cu_cleanup);
dee91e82 5961
365156ad
TT
5962 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5963 So we have to manually free the abbrev table. */
5964 dwarf2_free_abbrev_table (cu);
dee91e82 5965
365156ad
TT
5966 /* Link this CU into read_in_chain. */
5967 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5968 dwarf2_per_objfile->read_in_chain = this_cu;
5969 }
5970 else
5971 do_cleanups (free_cu_cleanup);
348e048f 5972 }
365156ad
TT
5973
5974 do_cleanups (cleanups);
dee91e82
DE
5975}
5976
33e80786
DE
5977/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5978 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5979 to have already done the lookup to find the DWO file).
dee91e82
DE
5980
5981 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5982 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5983
5984 We fill in THIS_CU->length.
5985
5986 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5987 linker) then DIE_READER_FUNC will not get called.
5988
5989 THIS_CU->cu is always freed when done.
3019eac3
DE
5990 This is done in order to not leave THIS_CU->cu in a state where we have
5991 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5992
5993static void
5994init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5995 struct dwo_file *dwo_file,
dee91e82
DE
5996 die_reader_func_ftype *die_reader_func,
5997 void *data)
5998{
5999 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6000 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6001 bfd *abfd = get_section_bfd_owner (section);
33e80786 6002 struct dwarf2_section_info *abbrev_section;
dee91e82 6003 struct dwarf2_cu cu;
d521ce57 6004 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
6005 struct die_reader_specs reader;
6006 struct cleanup *cleanups;
6007 struct die_info *comp_unit_die;
6008 int has_children;
6009
b4f54984 6010 if (dwarf_die_debug)
09406207
DE
6011 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6012 this_cu->is_debug_types ? "type" : "comp",
9c541725 6013 to_underlying (this_cu->sect_off));
09406207 6014
dee91e82
DE
6015 gdb_assert (this_cu->cu == NULL);
6016
33e80786
DE
6017 abbrev_section = (dwo_file != NULL
6018 ? &dwo_file->sections.abbrev
6019 : get_abbrev_section_for_cu (this_cu));
6020
dee91e82
DE
6021 /* This is cheap if the section is already read in. */
6022 dwarf2_read_section (objfile, section);
6023
6024 init_one_comp_unit (&cu, this_cu);
6025
6026 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6027
9c541725 6028 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
6029 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6030 abbrev_section, info_ptr,
43988095
JK
6031 (this_cu->is_debug_types
6032 ? rcuh_kind::TYPE
6033 : rcuh_kind::COMPILE));
dee91e82 6034
1ce1cefd 6035 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
6036
6037 /* Skip dummy compilation units. */
6038 if (info_ptr >= begin_info_ptr + this_cu->length
6039 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 6040 {
dee91e82 6041 do_cleanups (cleanups);
21b2bd31 6042 return;
93311388 6043 }
72bf9492 6044
dee91e82
DE
6045 dwarf2_read_abbrevs (&cu, abbrev_section);
6046 make_cleanup (dwarf2_free_abbrev_table, &cu);
6047
3019eac3 6048 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
6049 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6050
6051 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6052
6053 do_cleanups (cleanups);
6054}
6055
3019eac3
DE
6056/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6057 does not lookup the specified DWO file.
6058 This cannot be used to read DWO files.
dee91e82
DE
6059
6060 THIS_CU->cu is always freed when done.
3019eac3
DE
6061 This is done in order to not leave THIS_CU->cu in a state where we have
6062 to care whether it refers to the "main" CU or the DWO CU.
6063 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
6064
6065static void
6066init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6067 die_reader_func_ftype *die_reader_func,
6068 void *data)
6069{
33e80786 6070 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 6071}
0018ea6f
DE
6072\f
6073/* Type Unit Groups.
dee91e82 6074
0018ea6f
DE
6075 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6076 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6077 so that all types coming from the same compilation (.o file) are grouped
6078 together. A future step could be to put the types in the same symtab as
6079 the CU the types ultimately came from. */
ff013f42 6080
f4dc4d17
DE
6081static hashval_t
6082hash_type_unit_group (const void *item)
6083{
9a3c8263
SM
6084 const struct type_unit_group *tu_group
6085 = (const struct type_unit_group *) item;
f4dc4d17 6086
094b34ac 6087 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6088}
348e048f
DE
6089
6090static int
f4dc4d17 6091eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6092{
9a3c8263
SM
6093 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6094 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6095
094b34ac 6096 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6097}
348e048f 6098
f4dc4d17
DE
6099/* Allocate a hash table for type unit groups. */
6100
6101static htab_t
6102allocate_type_unit_groups_table (void)
6103{
6104 return htab_create_alloc_ex (3,
6105 hash_type_unit_group,
6106 eq_type_unit_group,
6107 NULL,
6108 &dwarf2_per_objfile->objfile->objfile_obstack,
6109 hashtab_obstack_allocate,
6110 dummy_obstack_deallocate);
6111}
dee91e82 6112
f4dc4d17
DE
6113/* Type units that don't have DW_AT_stmt_list are grouped into their own
6114 partial symtabs. We combine several TUs per psymtab to not let the size
6115 of any one psymtab grow too big. */
6116#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6117#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6118
094b34ac 6119/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6120 Create the type_unit_group object used to hold one or more TUs. */
6121
6122static struct type_unit_group *
094b34ac 6123create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6124{
6125 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6126 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6127 struct type_unit_group *tu_group;
f4dc4d17
DE
6128
6129 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6130 struct type_unit_group);
094b34ac 6131 per_cu = &tu_group->per_cu;
f4dc4d17 6132 per_cu->objfile = objfile;
f4dc4d17 6133
094b34ac
DE
6134 if (dwarf2_per_objfile->using_index)
6135 {
6136 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6137 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6138 }
6139 else
6140 {
9c541725 6141 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6142 struct partial_symtab *pst;
6143 char *name;
6144
6145 /* Give the symtab a useful name for debug purposes. */
6146 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6147 name = xstrprintf ("<type_units_%d>",
6148 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6149 else
6150 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6151
6152 pst = create_partial_symtab (per_cu, name);
6153 pst->anonymous = 1;
f4dc4d17 6154
094b34ac
DE
6155 xfree (name);
6156 }
f4dc4d17 6157
094b34ac 6158 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6159 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6160
6161 return tu_group;
6162}
6163
094b34ac
DE
6164/* Look up the type_unit_group for type unit CU, and create it if necessary.
6165 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6166
6167static struct type_unit_group *
ff39bb5e 6168get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6169{
6170 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6171 struct type_unit_group *tu_group;
6172 void **slot;
6173 unsigned int line_offset;
6174 struct type_unit_group type_unit_group_for_lookup;
6175
6176 if (dwarf2_per_objfile->type_unit_groups == NULL)
6177 {
6178 dwarf2_per_objfile->type_unit_groups =
6179 allocate_type_unit_groups_table ();
6180 }
6181
6182 /* Do we need to create a new group, or can we use an existing one? */
6183
6184 if (stmt_list)
6185 {
6186 line_offset = DW_UNSND (stmt_list);
6187 ++tu_stats->nr_symtab_sharers;
6188 }
6189 else
6190 {
6191 /* Ugh, no stmt_list. Rare, but we have to handle it.
6192 We can do various things here like create one group per TU or
6193 spread them over multiple groups to split up the expansion work.
6194 To avoid worst case scenarios (too many groups or too large groups)
6195 we, umm, group them in bunches. */
6196 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6197 | (tu_stats->nr_stmt_less_type_units
6198 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6199 ++tu_stats->nr_stmt_less_type_units;
6200 }
6201
094b34ac 6202 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6203 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6204 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6205 &type_unit_group_for_lookup, INSERT);
6206 if (*slot != NULL)
6207 {
9a3c8263 6208 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6209 gdb_assert (tu_group != NULL);
6210 }
6211 else
6212 {
9c541725 6213 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6214 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6215 *slot = tu_group;
6216 ++tu_stats->nr_symtabs;
6217 }
6218
6219 return tu_group;
6220}
0018ea6f
DE
6221\f
6222/* Partial symbol tables. */
6223
6224/* Create a psymtab named NAME and assign it to PER_CU.
6225
6226 The caller must fill in the following details:
6227 dirname, textlow, texthigh. */
6228
6229static struct partial_symtab *
6230create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6231{
6232 struct objfile *objfile = per_cu->objfile;
6233 struct partial_symtab *pst;
6234
18a94d75 6235 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
6236 objfile->global_psymbols,
6237 objfile->static_psymbols);
0018ea6f
DE
6238
6239 pst->psymtabs_addrmap_supported = 1;
6240
6241 /* This is the glue that links PST into GDB's symbol API. */
6242 pst->read_symtab_private = per_cu;
6243 pst->read_symtab = dwarf2_read_symtab;
6244 per_cu->v.psymtab = pst;
6245
6246 return pst;
6247}
6248
b93601f3
TT
6249/* The DATA object passed to process_psymtab_comp_unit_reader has this
6250 type. */
6251
6252struct process_psymtab_comp_unit_data
6253{
6254 /* True if we are reading a DW_TAG_partial_unit. */
6255
6256 int want_partial_unit;
6257
6258 /* The "pretend" language that is used if the CU doesn't declare a
6259 language. */
6260
6261 enum language pretend_language;
6262};
6263
0018ea6f
DE
6264/* die_reader_func for process_psymtab_comp_unit. */
6265
6266static void
6267process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6268 const gdb_byte *info_ptr,
0018ea6f
DE
6269 struct die_info *comp_unit_die,
6270 int has_children,
6271 void *data)
6272{
6273 struct dwarf2_cu *cu = reader->cu;
6274 struct objfile *objfile = cu->objfile;
3e29f34a 6275 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6276 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6277 CORE_ADDR baseaddr;
6278 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6279 struct partial_symtab *pst;
3a2b436a 6280 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6281 const char *filename;
9a3c8263
SM
6282 struct process_psymtab_comp_unit_data *info
6283 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6284
b93601f3 6285 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6286 return;
6287
6288 gdb_assert (! per_cu->is_debug_types);
6289
b93601f3 6290 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6291
6292 cu->list_in_scope = &file_symbols;
6293
6294 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6295 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6296 if (filename == NULL)
0018ea6f 6297 filename = "";
0018ea6f
DE
6298
6299 pst = create_partial_symtab (per_cu, filename);
6300
6301 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6302 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6303
6304 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6305
6306 dwarf2_find_base_address (comp_unit_die, cu);
6307
6308 /* Possibly set the default values of LOWPC and HIGHPC from
6309 `DW_AT_ranges'. */
3a2b436a
JK
6310 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6311 &best_highpc, cu, pst);
6312 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6313 /* Store the contiguous range if it is not empty; it can be empty for
6314 CUs with no code. */
6315 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6316 gdbarch_adjust_dwarf2_addr (gdbarch,
6317 best_lowpc + baseaddr),
6318 gdbarch_adjust_dwarf2_addr (gdbarch,
6319 best_highpc + baseaddr) - 1,
6320 pst);
0018ea6f
DE
6321
6322 /* Check if comp unit has_children.
6323 If so, read the rest of the partial symbols from this comp unit.
6324 If not, there's no more debug_info for this comp unit. */
6325 if (has_children)
6326 {
6327 struct partial_die_info *first_die;
6328 CORE_ADDR lowpc, highpc;
6329
6330 lowpc = ((CORE_ADDR) -1);
6331 highpc = ((CORE_ADDR) 0);
6332
6333 first_die = load_partial_dies (reader, info_ptr, 1);
6334
6335 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6336 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6337
6338 /* If we didn't find a lowpc, set it to highpc to avoid
6339 complaints from `maint check'. */
6340 if (lowpc == ((CORE_ADDR) -1))
6341 lowpc = highpc;
6342
6343 /* If the compilation unit didn't have an explicit address range,
6344 then use the information extracted from its child dies. */
e385593e 6345 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6346 {
6347 best_lowpc = lowpc;
6348 best_highpc = highpc;
6349 }
6350 }
3e29f34a
MR
6351 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6352 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6353
8763cede 6354 end_psymtab_common (objfile, pst);
0018ea6f
DE
6355
6356 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6357 {
6358 int i;
6359 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6360 struct dwarf2_per_cu_data *iter;
6361
6362 /* Fill in 'dependencies' here; we fill in 'users' in a
6363 post-pass. */
6364 pst->number_of_dependencies = len;
8d749320
SM
6365 pst->dependencies =
6366 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6367 for (i = 0;
6368 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6369 i, iter);
6370 ++i)
6371 pst->dependencies[i] = iter->v.psymtab;
6372
6373 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6374 }
6375
6376 /* Get the list of files included in the current compilation unit,
6377 and build a psymtab for each of them. */
6378 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6379
b4f54984 6380 if (dwarf_read_debug)
0018ea6f
DE
6381 {
6382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6383
6384 fprintf_unfiltered (gdb_stdlog,
6385 "Psymtab for %s unit @0x%x: %s - %s"
6386 ", %d global, %d static syms\n",
6387 per_cu->is_debug_types ? "type" : "comp",
9c541725 6388 to_underlying (per_cu->sect_off),
0018ea6f
DE
6389 paddress (gdbarch, pst->textlow),
6390 paddress (gdbarch, pst->texthigh),
6391 pst->n_global_syms, pst->n_static_syms);
6392 }
6393}
6394
6395/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6396 Process compilation unit THIS_CU for a psymtab. */
6397
6398static void
6399process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6400 int want_partial_unit,
6401 enum language pretend_language)
0018ea6f
DE
6402{
6403 /* If this compilation unit was already read in, free the
6404 cached copy in order to read it in again. This is
6405 necessary because we skipped some symbols when we first
6406 read in the compilation unit (see load_partial_dies).
6407 This problem could be avoided, but the benefit is unclear. */
6408 if (this_cu->cu != NULL)
6409 free_one_cached_comp_unit (this_cu);
6410
f1902523
JK
6411 if (this_cu->is_debug_types)
6412 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6413 NULL);
6414 else
6415 {
6416 process_psymtab_comp_unit_data info;
6417 info.want_partial_unit = want_partial_unit;
6418 info.pretend_language = pretend_language;
6419 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6420 process_psymtab_comp_unit_reader, &info);
6421 }
0018ea6f
DE
6422
6423 /* Age out any secondary CUs. */
6424 age_cached_comp_units ();
6425}
f4dc4d17
DE
6426
6427/* Reader function for build_type_psymtabs. */
6428
6429static void
6430build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6431 const gdb_byte *info_ptr,
f4dc4d17
DE
6432 struct die_info *type_unit_die,
6433 int has_children,
6434 void *data)
6435{
6436 struct objfile *objfile = dwarf2_per_objfile->objfile;
6437 struct dwarf2_cu *cu = reader->cu;
6438 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6439 struct signatured_type *sig_type;
f4dc4d17
DE
6440 struct type_unit_group *tu_group;
6441 struct attribute *attr;
6442 struct partial_die_info *first_die;
6443 CORE_ADDR lowpc, highpc;
6444 struct partial_symtab *pst;
6445
6446 gdb_assert (data == NULL);
0186c6a7
DE
6447 gdb_assert (per_cu->is_debug_types);
6448 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6449
6450 if (! has_children)
6451 return;
6452
6453 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6454 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6455
0186c6a7 6456 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6457
6458 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6459 cu->list_in_scope = &file_symbols;
6460 pst = create_partial_symtab (per_cu, "");
6461 pst->anonymous = 1;
6462
6463 first_die = load_partial_dies (reader, info_ptr, 1);
6464
6465 lowpc = (CORE_ADDR) -1;
6466 highpc = (CORE_ADDR) 0;
6467 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6468
8763cede 6469 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6470}
6471
73051182
DE
6472/* Struct used to sort TUs by their abbreviation table offset. */
6473
6474struct tu_abbrev_offset
6475{
6476 struct signatured_type *sig_type;
6477 sect_offset abbrev_offset;
6478};
6479
6480/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6481
6482static int
6483sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6484{
9a3c8263
SM
6485 const struct tu_abbrev_offset * const *a
6486 = (const struct tu_abbrev_offset * const*) ap;
6487 const struct tu_abbrev_offset * const *b
6488 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6489 sect_offset aoff = (*a)->abbrev_offset;
6490 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6491
6492 return (aoff > boff) - (aoff < boff);
6493}
6494
6495/* Efficiently read all the type units.
6496 This does the bulk of the work for build_type_psymtabs.
6497
6498 The efficiency is because we sort TUs by the abbrev table they use and
6499 only read each abbrev table once. In one program there are 200K TUs
6500 sharing 8K abbrev tables.
6501
6502 The main purpose of this function is to support building the
6503 dwarf2_per_objfile->type_unit_groups table.
6504 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6505 can collapse the search space by grouping them by stmt_list.
6506 The savings can be significant, in the same program from above the 200K TUs
6507 share 8K stmt_list tables.
6508
6509 FUNC is expected to call get_type_unit_group, which will create the
6510 struct type_unit_group if necessary and add it to
6511 dwarf2_per_objfile->type_unit_groups. */
6512
6513static void
6514build_type_psymtabs_1 (void)
6515{
73051182
DE
6516 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6517 struct cleanup *cleanups;
6518 struct abbrev_table *abbrev_table;
6519 sect_offset abbrev_offset;
6520 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6521 int i;
6522
6523 /* It's up to the caller to not call us multiple times. */
6524 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6525
6526 if (dwarf2_per_objfile->n_type_units == 0)
6527 return;
6528
6529 /* TUs typically share abbrev tables, and there can be way more TUs than
6530 abbrev tables. Sort by abbrev table to reduce the number of times we
6531 read each abbrev table in.
6532 Alternatives are to punt or to maintain a cache of abbrev tables.
6533 This is simpler and efficient enough for now.
6534
6535 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6536 symtab to use). Typically TUs with the same abbrev offset have the same
6537 stmt_list value too so in practice this should work well.
6538
6539 The basic algorithm here is:
6540
6541 sort TUs by abbrev table
6542 for each TU with same abbrev table:
6543 read abbrev table if first user
6544 read TU top level DIE
6545 [IWBN if DWO skeletons had DW_AT_stmt_list]
6546 call FUNC */
6547
b4f54984 6548 if (dwarf_read_debug)
73051182
DE
6549 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6550
6551 /* Sort in a separate table to maintain the order of all_type_units
6552 for .gdb_index: TU indices directly index all_type_units. */
6553 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6554 dwarf2_per_objfile->n_type_units);
6555 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6556 {
6557 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6558
6559 sorted_by_abbrev[i].sig_type = sig_type;
6560 sorted_by_abbrev[i].abbrev_offset =
6561 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6562 sig_type->per_cu.sect_off);
73051182
DE
6563 }
6564 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6565 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6566 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6567
9c541725 6568 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6569 abbrev_table = NULL;
6570 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6571
6572 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6573 {
6574 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6575
6576 /* Switch to the next abbrev table if necessary. */
6577 if (abbrev_table == NULL
9c541725 6578 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6579 {
6580 if (abbrev_table != NULL)
6581 {
6582 abbrev_table_free (abbrev_table);
6583 /* Reset to NULL in case abbrev_table_read_table throws
6584 an error: abbrev_table_free_cleanup will get called. */
6585 abbrev_table = NULL;
6586 }
6587 abbrev_offset = tu->abbrev_offset;
6588 abbrev_table =
6589 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6590 abbrev_offset);
6591 ++tu_stats->nr_uniq_abbrev_tables;
6592 }
6593
6594 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6595 build_type_psymtabs_reader, NULL);
6596 }
6597
73051182 6598 do_cleanups (cleanups);
6aa5f3a6 6599}
73051182 6600
6aa5f3a6
DE
6601/* Print collected type unit statistics. */
6602
6603static void
6604print_tu_stats (void)
6605{
6606 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6607
6608 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6609 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6610 dwarf2_per_objfile->n_type_units);
6611 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6612 tu_stats->nr_uniq_abbrev_tables);
6613 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6614 tu_stats->nr_symtabs);
6615 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6616 tu_stats->nr_symtab_sharers);
6617 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6618 tu_stats->nr_stmt_less_type_units);
6619 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6620 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6621}
6622
f4dc4d17
DE
6623/* Traversal function for build_type_psymtabs. */
6624
6625static int
6626build_type_psymtab_dependencies (void **slot, void *info)
6627{
6628 struct objfile *objfile = dwarf2_per_objfile->objfile;
6629 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6630 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6631 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6632 int len = VEC_length (sig_type_ptr, tu_group->tus);
6633 struct signatured_type *iter;
f4dc4d17
DE
6634 int i;
6635
6636 gdb_assert (len > 0);
0186c6a7 6637 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6638
6639 pst->number_of_dependencies = len;
8d749320
SM
6640 pst->dependencies =
6641 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6642 for (i = 0;
0186c6a7 6643 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6644 ++i)
6645 {
0186c6a7
DE
6646 gdb_assert (iter->per_cu.is_debug_types);
6647 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6648 iter->type_unit_group = tu_group;
f4dc4d17
DE
6649 }
6650
0186c6a7 6651 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6652
6653 return 1;
6654}
6655
6656/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6657 Build partial symbol tables for the .debug_types comp-units. */
6658
6659static void
6660build_type_psymtabs (struct objfile *objfile)
6661{
0e50663e 6662 if (! create_all_type_units (objfile))
348e048f
DE
6663 return;
6664
73051182 6665 build_type_psymtabs_1 ();
6aa5f3a6 6666}
f4dc4d17 6667
6aa5f3a6
DE
6668/* Traversal function for process_skeletonless_type_unit.
6669 Read a TU in a DWO file and build partial symbols for it. */
6670
6671static int
6672process_skeletonless_type_unit (void **slot, void *info)
6673{
6674 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6675 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6676 struct signatured_type find_entry, *entry;
6677
6678 /* If this TU doesn't exist in the global table, add it and read it in. */
6679
6680 if (dwarf2_per_objfile->signatured_types == NULL)
6681 {
6682 dwarf2_per_objfile->signatured_types
6683 = allocate_signatured_type_table (objfile);
6684 }
6685
6686 find_entry.signature = dwo_unit->signature;
6687 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6688 INSERT);
6689 /* If we've already seen this type there's nothing to do. What's happening
6690 is we're doing our own version of comdat-folding here. */
6691 if (*slot != NULL)
6692 return 1;
6693
6694 /* This does the job that create_all_type_units would have done for
6695 this TU. */
6696 entry = add_type_unit (dwo_unit->signature, slot);
6697 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6698 *slot = entry;
6699
6700 /* This does the job that build_type_psymtabs_1 would have done. */
6701 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6702 build_type_psymtabs_reader, NULL);
6703
6704 return 1;
6705}
6706
6707/* Traversal function for process_skeletonless_type_units. */
6708
6709static int
6710process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6711{
6712 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6713
6714 if (dwo_file->tus != NULL)
6715 {
6716 htab_traverse_noresize (dwo_file->tus,
6717 process_skeletonless_type_unit, info);
6718 }
6719
6720 return 1;
6721}
6722
6723/* Scan all TUs of DWO files, verifying we've processed them.
6724 This is needed in case a TU was emitted without its skeleton.
6725 Note: This can't be done until we know what all the DWO files are. */
6726
6727static void
6728process_skeletonless_type_units (struct objfile *objfile)
6729{
6730 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6731 if (get_dwp_file () == NULL
6732 && dwarf2_per_objfile->dwo_files != NULL)
6733 {
6734 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6735 process_dwo_file_for_skeletonless_type_units,
6736 objfile);
6737 }
348e048f
DE
6738}
6739
95554aad
TT
6740/* Compute the 'user' field for each psymtab in OBJFILE. */
6741
6742static void
6743set_partial_user (struct objfile *objfile)
6744{
6745 int i;
6746
6747 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6748 {
8832e7e3 6749 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6750 struct partial_symtab *pst = per_cu->v.psymtab;
6751 int j;
6752
36586728
TT
6753 if (pst == NULL)
6754 continue;
6755
95554aad
TT
6756 for (j = 0; j < pst->number_of_dependencies; ++j)
6757 {
6758 /* Set the 'user' field only if it is not already set. */
6759 if (pst->dependencies[j]->user == NULL)
6760 pst->dependencies[j]->user = pst;
6761 }
6762 }
6763}
6764
93311388
DE
6765/* Build the partial symbol table by doing a quick pass through the
6766 .debug_info and .debug_abbrev sections. */
72bf9492 6767
93311388 6768static void
c67a9c90 6769dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6770{
791afaa2 6771 struct cleanup *back_to;
21b2bd31 6772 int i;
93311388 6773
b4f54984 6774 if (dwarf_read_debug)
45cfd468
DE
6775 {
6776 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6777 objfile_name (objfile));
45cfd468
DE
6778 }
6779
98bfdba5
PA
6780 dwarf2_per_objfile->reading_partial_symbols = 1;
6781
be391dca 6782 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6783
93311388
DE
6784 /* Any cached compilation units will be linked by the per-objfile
6785 read_in_chain. Make sure to free them when we're done. */
6786 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6787
348e048f
DE
6788 build_type_psymtabs (objfile);
6789
93311388 6790 create_all_comp_units (objfile);
c906108c 6791
60606b2c
TT
6792 /* Create a temporary address map on a temporary obstack. We later
6793 copy this to the final obstack. */
8268c778 6794 auto_obstack temp_obstack;
791afaa2
TT
6795
6796 scoped_restore save_psymtabs_addrmap
6797 = make_scoped_restore (&objfile->psymtabs_addrmap,
6798 addrmap_create_mutable (&temp_obstack));
72bf9492 6799
21b2bd31 6800 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6801 {
8832e7e3 6802 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6803
b93601f3 6804 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6805 }
ff013f42 6806
6aa5f3a6
DE
6807 /* This has to wait until we read the CUs, we need the list of DWOs. */
6808 process_skeletonless_type_units (objfile);
6809
6810 /* Now that all TUs have been processed we can fill in the dependencies. */
6811 if (dwarf2_per_objfile->type_unit_groups != NULL)
6812 {
6813 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6814 build_type_psymtab_dependencies, NULL);
6815 }
6816
b4f54984 6817 if (dwarf_read_debug)
6aa5f3a6
DE
6818 print_tu_stats ();
6819
95554aad
TT
6820 set_partial_user (objfile);
6821
ff013f42
JK
6822 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6823 &objfile->objfile_obstack);
791afaa2
TT
6824 /* At this point we want to keep the address map. */
6825 save_psymtabs_addrmap.release ();
ff013f42 6826
ae038cb0 6827 do_cleanups (back_to);
45cfd468 6828
b4f54984 6829 if (dwarf_read_debug)
45cfd468 6830 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6831 objfile_name (objfile));
ae038cb0
DJ
6832}
6833
3019eac3 6834/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6835
6836static void
dee91e82 6837load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6838 const gdb_byte *info_ptr,
dee91e82
DE
6839 struct die_info *comp_unit_die,
6840 int has_children,
6841 void *data)
ae038cb0 6842{
dee91e82 6843 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6844
95554aad 6845 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6846
ae038cb0
DJ
6847 /* Check if comp unit has_children.
6848 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6849 If not, there's no more debug_info for this comp unit. */
d85a05f0 6850 if (has_children)
dee91e82
DE
6851 load_partial_dies (reader, info_ptr, 0);
6852}
98bfdba5 6853
dee91e82
DE
6854/* Load the partial DIEs for a secondary CU into memory.
6855 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6856
dee91e82
DE
6857static void
6858load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6859{
f4dc4d17
DE
6860 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6861 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6862}
6863
ae038cb0 6864static void
36586728
TT
6865read_comp_units_from_section (struct objfile *objfile,
6866 struct dwarf2_section_info *section,
f1902523 6867 struct dwarf2_section_info *abbrev_section,
36586728
TT
6868 unsigned int is_dwz,
6869 int *n_allocated,
6870 int *n_comp_units,
6871 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6872{
d521ce57 6873 const gdb_byte *info_ptr;
a32a8923 6874 bfd *abfd = get_section_bfd_owner (section);
be391dca 6875
b4f54984 6876 if (dwarf_read_debug)
bf6af496 6877 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6878 get_section_name (section),
6879 get_section_file_name (section));
bf6af496 6880
36586728 6881 dwarf2_read_section (objfile, section);
ae038cb0 6882
36586728 6883 info_ptr = section->buffer;
6e70227d 6884
36586728 6885 while (info_ptr < section->buffer + section->size)
ae038cb0 6886 {
ae038cb0 6887 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6888
9c541725 6889 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 6890
f1902523
JK
6891 comp_unit_head cu_header;
6892 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6893 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
6894
6895 /* Save the compilation unit for later lookup. */
f1902523
JK
6896 if (cu_header.unit_type != DW_UT_type)
6897 {
6898 this_cu = XOBNEW (&objfile->objfile_obstack,
6899 struct dwarf2_per_cu_data);
6900 memset (this_cu, 0, sizeof (*this_cu));
6901 }
6902 else
6903 {
6904 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6905 struct signatured_type);
6906 memset (sig_type, 0, sizeof (*sig_type));
6907 sig_type->signature = cu_header.signature;
6908 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6909 this_cu = &sig_type->per_cu;
6910 }
6911 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 6912 this_cu->sect_off = sect_off;
f1902523 6913 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 6914 this_cu->is_dwz = is_dwz;
9291a0cd 6915 this_cu->objfile = objfile;
8a0459fd 6916 this_cu->section = section;
ae038cb0 6917
36586728 6918 if (*n_comp_units == *n_allocated)
ae038cb0 6919 {
36586728 6920 *n_allocated *= 2;
224c3ddb
SM
6921 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6922 *all_comp_units, *n_allocated);
ae038cb0 6923 }
36586728
TT
6924 (*all_comp_units)[*n_comp_units] = this_cu;
6925 ++*n_comp_units;
ae038cb0
DJ
6926
6927 info_ptr = info_ptr + this_cu->length;
6928 }
36586728
TT
6929}
6930
6931/* Create a list of all compilation units in OBJFILE.
6932 This is only done for -readnow and building partial symtabs. */
6933
6934static void
6935create_all_comp_units (struct objfile *objfile)
6936{
6937 int n_allocated;
6938 int n_comp_units;
6939 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6940 struct dwz_file *dwz;
36586728
TT
6941
6942 n_comp_units = 0;
6943 n_allocated = 10;
8d749320 6944 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 6945
f1902523
JK
6946 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6947 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
6948 &n_allocated, &n_comp_units, &all_comp_units);
6949
4db1a1dc
TT
6950 dwz = dwarf2_get_dwz_file ();
6951 if (dwz != NULL)
f1902523 6952 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
6953 &n_allocated, &n_comp_units,
6954 &all_comp_units);
ae038cb0 6955
8d749320
SM
6956 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6957 struct dwarf2_per_cu_data *,
6958 n_comp_units);
ae038cb0
DJ
6959 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6960 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6961 xfree (all_comp_units);
6962 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6963}
6964
5734ee8b 6965/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6966 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6967 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6968 DW_AT_ranges). See the comments of add_partial_subprogram on how
6969 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6970
72bf9492
DJ
6971static void
6972scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6973 CORE_ADDR *highpc, int set_addrmap,
6974 struct dwarf2_cu *cu)
c906108c 6975{
72bf9492 6976 struct partial_die_info *pdi;
c906108c 6977
91c24f0a
DC
6978 /* Now, march along the PDI's, descending into ones which have
6979 interesting children but skipping the children of the other ones,
6980 until we reach the end of the compilation unit. */
c906108c 6981
72bf9492 6982 pdi = first_die;
91c24f0a 6983
72bf9492
DJ
6984 while (pdi != NULL)
6985 {
6986 fixup_partial_die (pdi, cu);
c906108c 6987
f55ee35c 6988 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6989 children, so we need to look at them. Ditto for anonymous
6990 enums. */
933c6fe4 6991
72bf9492 6992 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6993 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6994 || pdi->tag == DW_TAG_imported_unit)
c906108c 6995 {
72bf9492 6996 switch (pdi->tag)
c906108c
SS
6997 {
6998 case DW_TAG_subprogram:
cdc07690 6999 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7000 break;
72929c62 7001 case DW_TAG_constant:
c906108c
SS
7002 case DW_TAG_variable:
7003 case DW_TAG_typedef:
91c24f0a 7004 case DW_TAG_union_type:
72bf9492 7005 if (!pdi->is_declaration)
63d06c5c 7006 {
72bf9492 7007 add_partial_symbol (pdi, cu);
63d06c5c
DC
7008 }
7009 break;
c906108c 7010 case DW_TAG_class_type:
680b30c7 7011 case DW_TAG_interface_type:
c906108c 7012 case DW_TAG_structure_type:
72bf9492 7013 if (!pdi->is_declaration)
c906108c 7014 {
72bf9492 7015 add_partial_symbol (pdi, cu);
c906108c 7016 }
e98c9e7c
TT
7017 if (cu->language == language_rust && pdi->has_children)
7018 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7019 set_addrmap, cu);
c906108c 7020 break;
91c24f0a 7021 case DW_TAG_enumeration_type:
72bf9492
DJ
7022 if (!pdi->is_declaration)
7023 add_partial_enumeration (pdi, cu);
c906108c
SS
7024 break;
7025 case DW_TAG_base_type:
a02abb62 7026 case DW_TAG_subrange_type:
c906108c 7027 /* File scope base type definitions are added to the partial
c5aa993b 7028 symbol table. */
72bf9492 7029 add_partial_symbol (pdi, cu);
c906108c 7030 break;
d9fa45fe 7031 case DW_TAG_namespace:
cdc07690 7032 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7033 break;
5d7cb8df 7034 case DW_TAG_module:
cdc07690 7035 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7036 break;
95554aad
TT
7037 case DW_TAG_imported_unit:
7038 {
7039 struct dwarf2_per_cu_data *per_cu;
7040
f4dc4d17
DE
7041 /* For now we don't handle imported units in type units. */
7042 if (cu->per_cu->is_debug_types)
7043 {
7044 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7045 " supported in type units [in module %s]"),
4262abfb 7046 objfile_name (cu->objfile));
f4dc4d17
DE
7047 }
7048
9c541725 7049 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 7050 pdi->is_dwz,
95554aad
TT
7051 cu->objfile);
7052
7053 /* Go read the partial unit, if needed. */
7054 if (per_cu->v.psymtab == NULL)
b93601f3 7055 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 7056
f4dc4d17 7057 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 7058 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
7059 }
7060 break;
74921315
KS
7061 case DW_TAG_imported_declaration:
7062 add_partial_symbol (pdi, cu);
7063 break;
c906108c
SS
7064 default:
7065 break;
7066 }
7067 }
7068
72bf9492
DJ
7069 /* If the die has a sibling, skip to the sibling. */
7070
7071 pdi = pdi->die_sibling;
7072 }
7073}
7074
7075/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7076
72bf9492 7077 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7078 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7079 Enumerators are an exception; they use the scope of their parent
7080 enumeration type, i.e. the name of the enumeration type is not
7081 prepended to the enumerator.
91c24f0a 7082
72bf9492
DJ
7083 There are two complexities. One is DW_AT_specification; in this
7084 case "parent" means the parent of the target of the specification,
7085 instead of the direct parent of the DIE. The other is compilers
7086 which do not emit DW_TAG_namespace; in this case we try to guess
7087 the fully qualified name of structure types from their members'
7088 linkage names. This must be done using the DIE's children rather
7089 than the children of any DW_AT_specification target. We only need
7090 to do this for structures at the top level, i.e. if the target of
7091 any DW_AT_specification (if any; otherwise the DIE itself) does not
7092 have a parent. */
7093
7094/* Compute the scope prefix associated with PDI's parent, in
7095 compilation unit CU. The result will be allocated on CU's
7096 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7097 field. NULL is returned if no prefix is necessary. */
15d034d0 7098static const char *
72bf9492
DJ
7099partial_die_parent_scope (struct partial_die_info *pdi,
7100 struct dwarf2_cu *cu)
7101{
15d034d0 7102 const char *grandparent_scope;
72bf9492 7103 struct partial_die_info *parent, *real_pdi;
91c24f0a 7104
72bf9492
DJ
7105 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7106 then this means the parent of the specification DIE. */
7107
7108 real_pdi = pdi;
72bf9492 7109 while (real_pdi->has_specification)
36586728
TT
7110 real_pdi = find_partial_die (real_pdi->spec_offset,
7111 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7112
7113 parent = real_pdi->die_parent;
7114 if (parent == NULL)
7115 return NULL;
7116
7117 if (parent->scope_set)
7118 return parent->scope;
7119
7120 fixup_partial_die (parent, cu);
7121
10b3939b 7122 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7123
acebe513
UW
7124 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7125 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7126 Work around this problem here. */
7127 if (cu->language == language_cplus
6e70227d 7128 && parent->tag == DW_TAG_namespace
acebe513
UW
7129 && strcmp (parent->name, "::") == 0
7130 && grandparent_scope == NULL)
7131 {
7132 parent->scope = NULL;
7133 parent->scope_set = 1;
7134 return NULL;
7135 }
7136
9c6c53f7
SA
7137 if (pdi->tag == DW_TAG_enumerator)
7138 /* Enumerators should not get the name of the enumeration as a prefix. */
7139 parent->scope = grandparent_scope;
7140 else if (parent->tag == DW_TAG_namespace
f55ee35c 7141 || parent->tag == DW_TAG_module
72bf9492
DJ
7142 || parent->tag == DW_TAG_structure_type
7143 || parent->tag == DW_TAG_class_type
680b30c7 7144 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7145 || parent->tag == DW_TAG_union_type
7146 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7147 {
7148 if (grandparent_scope == NULL)
7149 parent->scope = parent->name;
7150 else
3e43a32a
MS
7151 parent->scope = typename_concat (&cu->comp_unit_obstack,
7152 grandparent_scope,
f55ee35c 7153 parent->name, 0, cu);
72bf9492 7154 }
72bf9492
DJ
7155 else
7156 {
7157 /* FIXME drow/2004-04-01: What should we be doing with
7158 function-local names? For partial symbols, we should probably be
7159 ignoring them. */
7160 complaint (&symfile_complaints,
e2e0b3e5 7161 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7162 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7163 parent->scope = grandparent_scope;
c906108c
SS
7164 }
7165
72bf9492
DJ
7166 parent->scope_set = 1;
7167 return parent->scope;
7168}
7169
7170/* Return the fully scoped name associated with PDI, from compilation unit
7171 CU. The result will be allocated with malloc. */
4568ecf9 7172
72bf9492
DJ
7173static char *
7174partial_die_full_name (struct partial_die_info *pdi,
7175 struct dwarf2_cu *cu)
7176{
15d034d0 7177 const char *parent_scope;
72bf9492 7178
98bfdba5
PA
7179 /* If this is a template instantiation, we can not work out the
7180 template arguments from partial DIEs. So, unfortunately, we have
7181 to go through the full DIEs. At least any work we do building
7182 types here will be reused if full symbols are loaded later. */
7183 if (pdi->has_template_arguments)
7184 {
7185 fixup_partial_die (pdi, cu);
7186
7187 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7188 {
7189 struct die_info *die;
7190 struct attribute attr;
7191 struct dwarf2_cu *ref_cu = cu;
7192
b64f50a1 7193 /* DW_FORM_ref_addr is using section offset. */
b4069958 7194 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7195 attr.form = DW_FORM_ref_addr;
9c541725 7196 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7197 die = follow_die_ref (NULL, &attr, &ref_cu);
7198
7199 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7200 }
7201 }
7202
72bf9492
DJ
7203 parent_scope = partial_die_parent_scope (pdi, cu);
7204 if (parent_scope == NULL)
7205 return NULL;
7206 else
f55ee35c 7207 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7208}
7209
7210static void
72bf9492 7211add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7212{
e7c27a73 7213 struct objfile *objfile = cu->objfile;
3e29f34a 7214 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7215 CORE_ADDR addr = 0;
15d034d0 7216 const char *actual_name = NULL;
e142c38c 7217 CORE_ADDR baseaddr;
15d034d0 7218 char *built_actual_name;
e142c38c
DJ
7219
7220 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7221
15d034d0
TT
7222 built_actual_name = partial_die_full_name (pdi, cu);
7223 if (built_actual_name != NULL)
7224 actual_name = built_actual_name;
63d06c5c 7225
72bf9492
DJ
7226 if (actual_name == NULL)
7227 actual_name = pdi->name;
7228
c906108c
SS
7229 switch (pdi->tag)
7230 {
7231 case DW_TAG_subprogram:
3e29f34a 7232 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7233 if (pdi->is_external || cu->language == language_ada)
c906108c 7234 {
2cfa0c8d
JB
7235 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7236 of the global scope. But in Ada, we want to be able to access
7237 nested procedures globally. So all Ada subprograms are stored
7238 in the global scope. */
f47fb265 7239 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7240 built_actual_name != NULL,
f47fb265
MS
7241 VAR_DOMAIN, LOC_BLOCK,
7242 &objfile->global_psymbols,
1762568f 7243 addr, cu->language, objfile);
c906108c
SS
7244 }
7245 else
7246 {
f47fb265 7247 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7248 built_actual_name != NULL,
f47fb265
MS
7249 VAR_DOMAIN, LOC_BLOCK,
7250 &objfile->static_psymbols,
1762568f 7251 addr, cu->language, objfile);
c906108c 7252 }
0c1b455e
TT
7253
7254 if (pdi->main_subprogram && actual_name != NULL)
7255 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7256 break;
72929c62
JB
7257 case DW_TAG_constant:
7258 {
af5bf4ad 7259 std::vector<partial_symbol *> *list;
72929c62
JB
7260
7261 if (pdi->is_external)
7262 list = &objfile->global_psymbols;
7263 else
7264 list = &objfile->static_psymbols;
f47fb265 7265 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7266 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7267 list, 0, cu->language, objfile);
72929c62
JB
7268 }
7269 break;
c906108c 7270 case DW_TAG_variable:
95554aad
TT
7271 if (pdi->d.locdesc)
7272 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7273
95554aad 7274 if (pdi->d.locdesc
caac4577
JG
7275 && addr == 0
7276 && !dwarf2_per_objfile->has_section_at_zero)
7277 {
7278 /* A global or static variable may also have been stripped
7279 out by the linker if unused, in which case its address
7280 will be nullified; do not add such variables into partial
7281 symbol table then. */
7282 }
7283 else if (pdi->is_external)
c906108c
SS
7284 {
7285 /* Global Variable.
7286 Don't enter into the minimal symbol tables as there is
7287 a minimal symbol table entry from the ELF symbols already.
7288 Enter into partial symbol table if it has a location
7289 descriptor or a type.
7290 If the location descriptor is missing, new_symbol will create
7291 a LOC_UNRESOLVED symbol, the address of the variable will then
7292 be determined from the minimal symbol table whenever the variable
7293 is referenced.
7294 The address for the partial symbol table entry is not
7295 used by GDB, but it comes in handy for debugging partial symbol
7296 table building. */
7297
95554aad 7298 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7299 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7300 built_actual_name != NULL,
f47fb265
MS
7301 VAR_DOMAIN, LOC_STATIC,
7302 &objfile->global_psymbols,
1762568f 7303 addr + baseaddr,
f47fb265 7304 cu->language, objfile);
c906108c
SS
7305 }
7306 else
7307 {
ff908ebf
AW
7308 int has_loc = pdi->d.locdesc != NULL;
7309
7310 /* Static Variable. Skip symbols whose value we cannot know (those
7311 without location descriptors or constant values). */
7312 if (!has_loc && !pdi->has_const_value)
decbce07 7313 {
15d034d0 7314 xfree (built_actual_name);
decbce07
MS
7315 return;
7316 }
ff908ebf 7317
f47fb265 7318 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7319 built_actual_name != NULL,
f47fb265
MS
7320 VAR_DOMAIN, LOC_STATIC,
7321 &objfile->static_psymbols,
ff908ebf 7322 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7323 cu->language, objfile);
c906108c
SS
7324 }
7325 break;
7326 case DW_TAG_typedef:
7327 case DW_TAG_base_type:
a02abb62 7328 case DW_TAG_subrange_type:
38d518c9 7329 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7330 built_actual_name != NULL,
176620f1 7331 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7332 &objfile->static_psymbols,
1762568f 7333 0, cu->language, objfile);
c906108c 7334 break;
74921315 7335 case DW_TAG_imported_declaration:
72bf9492
DJ
7336 case DW_TAG_namespace:
7337 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7338 built_actual_name != NULL,
72bf9492
DJ
7339 VAR_DOMAIN, LOC_TYPEDEF,
7340 &objfile->global_psymbols,
1762568f 7341 0, cu->language, objfile);
72bf9492 7342 break;
530e8392
KB
7343 case DW_TAG_module:
7344 add_psymbol_to_list (actual_name, strlen (actual_name),
7345 built_actual_name != NULL,
7346 MODULE_DOMAIN, LOC_TYPEDEF,
7347 &objfile->global_psymbols,
1762568f 7348 0, cu->language, objfile);
530e8392 7349 break;
c906108c 7350 case DW_TAG_class_type:
680b30c7 7351 case DW_TAG_interface_type:
c906108c
SS
7352 case DW_TAG_structure_type:
7353 case DW_TAG_union_type:
7354 case DW_TAG_enumeration_type:
fa4028e9
JB
7355 /* Skip external references. The DWARF standard says in the section
7356 about "Structure, Union, and Class Type Entries": "An incomplete
7357 structure, union or class type is represented by a structure,
7358 union or class entry that does not have a byte size attribute
7359 and that has a DW_AT_declaration attribute." */
7360 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7361 {
15d034d0 7362 xfree (built_actual_name);
decbce07
MS
7363 return;
7364 }
fa4028e9 7365
63d06c5c
DC
7366 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7367 static vs. global. */
38d518c9 7368 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7369 built_actual_name != NULL,
176620f1 7370 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7371 cu->language == language_cplus
63d06c5c
DC
7372 ? &objfile->global_psymbols
7373 : &objfile->static_psymbols,
1762568f 7374 0, cu->language, objfile);
c906108c 7375
c906108c
SS
7376 break;
7377 case DW_TAG_enumerator:
38d518c9 7378 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7379 built_actual_name != NULL,
176620f1 7380 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7381 cu->language == language_cplus
f6fe98ef
DJ
7382 ? &objfile->global_psymbols
7383 : &objfile->static_psymbols,
1762568f 7384 0, cu->language, objfile);
c906108c
SS
7385 break;
7386 default:
7387 break;
7388 }
5c4e30ca 7389
15d034d0 7390 xfree (built_actual_name);
c906108c
SS
7391}
7392
5c4e30ca
DC
7393/* Read a partial die corresponding to a namespace; also, add a symbol
7394 corresponding to that namespace to the symbol table. NAMESPACE is
7395 the name of the enclosing namespace. */
91c24f0a 7396
72bf9492
DJ
7397static void
7398add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7399 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7400 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7401{
72bf9492 7402 /* Add a symbol for the namespace. */
e7c27a73 7403
72bf9492 7404 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7405
7406 /* Now scan partial symbols in that namespace. */
7407
91c24f0a 7408 if (pdi->has_children)
cdc07690 7409 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7410}
7411
5d7cb8df
JK
7412/* Read a partial die corresponding to a Fortran module. */
7413
7414static void
7415add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7416 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7417{
530e8392
KB
7418 /* Add a symbol for the namespace. */
7419
7420 add_partial_symbol (pdi, cu);
7421
f55ee35c 7422 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7423
7424 if (pdi->has_children)
cdc07690 7425 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7426}
7427
bc30ff58
JB
7428/* Read a partial die corresponding to a subprogram and create a partial
7429 symbol for that subprogram. When the CU language allows it, this
7430 routine also defines a partial symbol for each nested subprogram
cdc07690 7431 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7432 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7433 and highest PC values found in PDI.
6e70227d 7434
cdc07690
YQ
7435 PDI may also be a lexical block, in which case we simply search
7436 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7437 Again, this is only performed when the CU language allows this
7438 type of definitions. */
7439
7440static void
7441add_partial_subprogram (struct partial_die_info *pdi,
7442 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7443 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7444{
7445 if (pdi->tag == DW_TAG_subprogram)
7446 {
7447 if (pdi->has_pc_info)
7448 {
7449 if (pdi->lowpc < *lowpc)
7450 *lowpc = pdi->lowpc;
7451 if (pdi->highpc > *highpc)
7452 *highpc = pdi->highpc;
cdc07690 7453 if (set_addrmap)
5734ee8b 7454 {
5734ee8b 7455 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7457 CORE_ADDR baseaddr;
7458 CORE_ADDR highpc;
7459 CORE_ADDR lowpc;
5734ee8b
DJ
7460
7461 baseaddr = ANOFFSET (objfile->section_offsets,
7462 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7463 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7464 pdi->lowpc + baseaddr);
7465 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7466 pdi->highpc + baseaddr);
7467 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7468 cu->per_cu->v.psymtab);
5734ee8b 7469 }
481860b3
GB
7470 }
7471
7472 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7473 {
bc30ff58 7474 if (!pdi->is_declaration)
e8d05480
JB
7475 /* Ignore subprogram DIEs that do not have a name, they are
7476 illegal. Do not emit a complaint at this point, we will
7477 do so when we convert this psymtab into a symtab. */
7478 if (pdi->name)
7479 add_partial_symbol (pdi, cu);
bc30ff58
JB
7480 }
7481 }
6e70227d 7482
bc30ff58
JB
7483 if (! pdi->has_children)
7484 return;
7485
7486 if (cu->language == language_ada)
7487 {
7488 pdi = pdi->die_child;
7489 while (pdi != NULL)
7490 {
7491 fixup_partial_die (pdi, cu);
7492 if (pdi->tag == DW_TAG_subprogram
7493 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7494 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7495 pdi = pdi->die_sibling;
7496 }
7497 }
7498}
7499
91c24f0a
DC
7500/* Read a partial die corresponding to an enumeration type. */
7501
72bf9492
DJ
7502static void
7503add_partial_enumeration (struct partial_die_info *enum_pdi,
7504 struct dwarf2_cu *cu)
91c24f0a 7505{
72bf9492 7506 struct partial_die_info *pdi;
91c24f0a
DC
7507
7508 if (enum_pdi->name != NULL)
72bf9492
DJ
7509 add_partial_symbol (enum_pdi, cu);
7510
7511 pdi = enum_pdi->die_child;
7512 while (pdi)
91c24f0a 7513 {
72bf9492 7514 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7515 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7516 else
72bf9492
DJ
7517 add_partial_symbol (pdi, cu);
7518 pdi = pdi->die_sibling;
91c24f0a 7519 }
91c24f0a
DC
7520}
7521
6caca83c
CC
7522/* Return the initial uleb128 in the die at INFO_PTR. */
7523
7524static unsigned int
d521ce57 7525peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7526{
7527 unsigned int bytes_read;
7528
7529 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7530}
7531
4bb7a0a7
DJ
7532/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7533 Return the corresponding abbrev, or NULL if the number is zero (indicating
7534 an empty DIE). In either case *BYTES_READ will be set to the length of
7535 the initial number. */
7536
7537static struct abbrev_info *
d521ce57 7538peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7539 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7540{
7541 bfd *abfd = cu->objfile->obfd;
7542 unsigned int abbrev_number;
7543 struct abbrev_info *abbrev;
7544
7545 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7546
7547 if (abbrev_number == 0)
7548 return NULL;
7549
433df2d4 7550 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7551 if (!abbrev)
7552 {
422b9917
DE
7553 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7554 " at offset 0x%x [in module %s]"),
7555 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7556 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7557 }
7558
7559 return abbrev;
7560}
7561
93311388
DE
7562/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7563 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7564 DIE. Any children of the skipped DIEs will also be skipped. */
7565
d521ce57
TT
7566static const gdb_byte *
7567skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7568{
dee91e82 7569 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7570 struct abbrev_info *abbrev;
7571 unsigned int bytes_read;
7572
7573 while (1)
7574 {
7575 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7576 if (abbrev == NULL)
7577 return info_ptr + bytes_read;
7578 else
dee91e82 7579 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7580 }
7581}
7582
93311388
DE
7583/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7584 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7585 abbrev corresponding to that skipped uleb128 should be passed in
7586 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7587 children. */
7588
d521ce57
TT
7589static const gdb_byte *
7590skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7591 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7592{
7593 unsigned int bytes_read;
7594 struct attribute attr;
dee91e82
DE
7595 bfd *abfd = reader->abfd;
7596 struct dwarf2_cu *cu = reader->cu;
d521ce57 7597 const gdb_byte *buffer = reader->buffer;
f664829e 7598 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7599 unsigned int form, i;
7600
7601 for (i = 0; i < abbrev->num_attrs; i++)
7602 {
7603 /* The only abbrev we care about is DW_AT_sibling. */
7604 if (abbrev->attrs[i].name == DW_AT_sibling)
7605 {
dee91e82 7606 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7607 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7608 complaint (&symfile_complaints,
7609 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7610 else
b9502d3f 7611 {
9c541725
PA
7612 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7613 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7614
7615 if (sibling_ptr < info_ptr)
7616 complaint (&symfile_complaints,
7617 _("DW_AT_sibling points backwards"));
22869d73
KS
7618 else if (sibling_ptr > reader->buffer_end)
7619 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7620 else
7621 return sibling_ptr;
7622 }
4bb7a0a7
DJ
7623 }
7624
7625 /* If it isn't DW_AT_sibling, skip this attribute. */
7626 form = abbrev->attrs[i].form;
7627 skip_attribute:
7628 switch (form)
7629 {
4bb7a0a7 7630 case DW_FORM_ref_addr:
ae411497
TT
7631 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7632 and later it is offset sized. */
7633 if (cu->header.version == 2)
7634 info_ptr += cu->header.addr_size;
7635 else
7636 info_ptr += cu->header.offset_size;
7637 break;
36586728
TT
7638 case DW_FORM_GNU_ref_alt:
7639 info_ptr += cu->header.offset_size;
7640 break;
ae411497 7641 case DW_FORM_addr:
4bb7a0a7
DJ
7642 info_ptr += cu->header.addr_size;
7643 break;
7644 case DW_FORM_data1:
7645 case DW_FORM_ref1:
7646 case DW_FORM_flag:
7647 info_ptr += 1;
7648 break;
2dc7f7b3 7649 case DW_FORM_flag_present:
43988095 7650 case DW_FORM_implicit_const:
2dc7f7b3 7651 break;
4bb7a0a7
DJ
7652 case DW_FORM_data2:
7653 case DW_FORM_ref2:
7654 info_ptr += 2;
7655 break;
7656 case DW_FORM_data4:
7657 case DW_FORM_ref4:
7658 info_ptr += 4;
7659 break;
7660 case DW_FORM_data8:
7661 case DW_FORM_ref8:
55f1336d 7662 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7663 info_ptr += 8;
7664 break;
0224619f
JK
7665 case DW_FORM_data16:
7666 info_ptr += 16;
7667 break;
4bb7a0a7 7668 case DW_FORM_string:
9b1c24c8 7669 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7670 info_ptr += bytes_read;
7671 break;
2dc7f7b3 7672 case DW_FORM_sec_offset:
4bb7a0a7 7673 case DW_FORM_strp:
36586728 7674 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7675 info_ptr += cu->header.offset_size;
7676 break;
2dc7f7b3 7677 case DW_FORM_exprloc:
4bb7a0a7
DJ
7678 case DW_FORM_block:
7679 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7680 info_ptr += bytes_read;
7681 break;
7682 case DW_FORM_block1:
7683 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7684 break;
7685 case DW_FORM_block2:
7686 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7687 break;
7688 case DW_FORM_block4:
7689 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7690 break;
7691 case DW_FORM_sdata:
7692 case DW_FORM_udata:
7693 case DW_FORM_ref_udata:
3019eac3
DE
7694 case DW_FORM_GNU_addr_index:
7695 case DW_FORM_GNU_str_index:
d521ce57 7696 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7697 break;
7698 case DW_FORM_indirect:
7699 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7700 info_ptr += bytes_read;
7701 /* We need to continue parsing from here, so just go back to
7702 the top. */
7703 goto skip_attribute;
7704
7705 default:
3e43a32a
MS
7706 error (_("Dwarf Error: Cannot handle %s "
7707 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7708 dwarf_form_name (form),
7709 bfd_get_filename (abfd));
7710 }
7711 }
7712
7713 if (abbrev->has_children)
dee91e82 7714 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7715 else
7716 return info_ptr;
7717}
7718
93311388 7719/* Locate ORIG_PDI's sibling.
dee91e82 7720 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7721
d521ce57 7722static const gdb_byte *
dee91e82
DE
7723locate_pdi_sibling (const struct die_reader_specs *reader,
7724 struct partial_die_info *orig_pdi,
d521ce57 7725 const gdb_byte *info_ptr)
91c24f0a
DC
7726{
7727 /* Do we know the sibling already? */
72bf9492 7728
91c24f0a
DC
7729 if (orig_pdi->sibling)
7730 return orig_pdi->sibling;
7731
7732 /* Are there any children to deal with? */
7733
7734 if (!orig_pdi->has_children)
7735 return info_ptr;
7736
4bb7a0a7 7737 /* Skip the children the long way. */
91c24f0a 7738
dee91e82 7739 return skip_children (reader, info_ptr);
91c24f0a
DC
7740}
7741
257e7a09 7742/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7743 not NULL. */
c906108c
SS
7744
7745static void
257e7a09
YQ
7746dwarf2_read_symtab (struct partial_symtab *self,
7747 struct objfile *objfile)
c906108c 7748{
257e7a09 7749 if (self->readin)
c906108c 7750 {
442e4d9c 7751 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7752 self->filename);
442e4d9c
YQ
7753 }
7754 else
7755 {
7756 if (info_verbose)
c906108c 7757 {
442e4d9c 7758 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7759 self->filename);
442e4d9c 7760 gdb_flush (gdb_stdout);
c906108c 7761 }
c906108c 7762
442e4d9c 7763 /* Restore our global data. */
9a3c8263
SM
7764 dwarf2_per_objfile
7765 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7766 dwarf2_objfile_data_key);
10b3939b 7767
442e4d9c
YQ
7768 /* If this psymtab is constructed from a debug-only objfile, the
7769 has_section_at_zero flag will not necessarily be correct. We
7770 can get the correct value for this flag by looking at the data
7771 associated with the (presumably stripped) associated objfile. */
7772 if (objfile->separate_debug_objfile_backlink)
7773 {
7774 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7775 = ((struct dwarf2_per_objfile *)
7776 objfile_data (objfile->separate_debug_objfile_backlink,
7777 dwarf2_objfile_data_key));
9a619af0 7778
442e4d9c
YQ
7779 dwarf2_per_objfile->has_section_at_zero
7780 = dpo_backlink->has_section_at_zero;
7781 }
b2ab525c 7782
442e4d9c 7783 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7784
257e7a09 7785 psymtab_to_symtab_1 (self);
c906108c 7786
442e4d9c
YQ
7787 /* Finish up the debug error message. */
7788 if (info_verbose)
7789 printf_filtered (_("done.\n"));
c906108c 7790 }
95554aad
TT
7791
7792 process_cu_includes ();
c906108c 7793}
9cdd5dbd
DE
7794\f
7795/* Reading in full CUs. */
c906108c 7796
10b3939b
DJ
7797/* Add PER_CU to the queue. */
7798
7799static void
95554aad
TT
7800queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7801 enum language pretend_language)
10b3939b
DJ
7802{
7803 struct dwarf2_queue_item *item;
7804
7805 per_cu->queued = 1;
8d749320 7806 item = XNEW (struct dwarf2_queue_item);
10b3939b 7807 item->per_cu = per_cu;
95554aad 7808 item->pretend_language = pretend_language;
10b3939b
DJ
7809 item->next = NULL;
7810
7811 if (dwarf2_queue == NULL)
7812 dwarf2_queue = item;
7813 else
7814 dwarf2_queue_tail->next = item;
7815
7816 dwarf2_queue_tail = item;
7817}
7818
89e63ee4
DE
7819/* If PER_CU is not yet queued, add it to the queue.
7820 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7821 dependency.
0907af0c 7822 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7823 meaning either PER_CU is already queued or it is already loaded.
7824
7825 N.B. There is an invariant here that if a CU is queued then it is loaded.
7826 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7827
7828static int
89e63ee4 7829maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7830 struct dwarf2_per_cu_data *per_cu,
7831 enum language pretend_language)
7832{
7833 /* We may arrive here during partial symbol reading, if we need full
7834 DIEs to process an unusual case (e.g. template arguments). Do
7835 not queue PER_CU, just tell our caller to load its DIEs. */
7836 if (dwarf2_per_objfile->reading_partial_symbols)
7837 {
7838 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7839 return 1;
7840 return 0;
7841 }
7842
7843 /* Mark the dependence relation so that we don't flush PER_CU
7844 too early. */
89e63ee4
DE
7845 if (dependent_cu != NULL)
7846 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7847
7848 /* If it's already on the queue, we have nothing to do. */
7849 if (per_cu->queued)
7850 return 0;
7851
7852 /* If the compilation unit is already loaded, just mark it as
7853 used. */
7854 if (per_cu->cu != NULL)
7855 {
7856 per_cu->cu->last_used = 0;
7857 return 0;
7858 }
7859
7860 /* Add it to the queue. */
7861 queue_comp_unit (per_cu, pretend_language);
7862
7863 return 1;
7864}
7865
10b3939b
DJ
7866/* Process the queue. */
7867
7868static void
a0f42c21 7869process_queue (void)
10b3939b
DJ
7870{
7871 struct dwarf2_queue_item *item, *next_item;
7872
b4f54984 7873 if (dwarf_read_debug)
45cfd468
DE
7874 {
7875 fprintf_unfiltered (gdb_stdlog,
7876 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7877 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7878 }
7879
03dd20cc
DJ
7880 /* The queue starts out with one item, but following a DIE reference
7881 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7882 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7883 {
cc12ce38
DE
7884 if ((dwarf2_per_objfile->using_index
7885 ? !item->per_cu->v.quick->compunit_symtab
7886 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7887 /* Skip dummy CUs. */
7888 && item->per_cu->cu != NULL)
f4dc4d17
DE
7889 {
7890 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7891 unsigned int debug_print_threshold;
247f5c4f 7892 char buf[100];
f4dc4d17 7893
247f5c4f 7894 if (per_cu->is_debug_types)
f4dc4d17 7895 {
247f5c4f
DE
7896 struct signatured_type *sig_type =
7897 (struct signatured_type *) per_cu;
7898
7899 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7900 hex_string (sig_type->signature),
9c541725 7901 to_underlying (per_cu->sect_off));
73be47f5
DE
7902 /* There can be 100s of TUs.
7903 Only print them in verbose mode. */
7904 debug_print_threshold = 2;
f4dc4d17 7905 }
247f5c4f 7906 else
73be47f5 7907 {
9c541725
PA
7908 sprintf (buf, "CU at offset 0x%x",
7909 to_underlying (per_cu->sect_off));
73be47f5
DE
7910 debug_print_threshold = 1;
7911 }
247f5c4f 7912
b4f54984 7913 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7914 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7915
7916 if (per_cu->is_debug_types)
7917 process_full_type_unit (per_cu, item->pretend_language);
7918 else
7919 process_full_comp_unit (per_cu, item->pretend_language);
7920
b4f54984 7921 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7922 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7923 }
10b3939b
DJ
7924
7925 item->per_cu->queued = 0;
7926 next_item = item->next;
7927 xfree (item);
7928 }
7929
7930 dwarf2_queue_tail = NULL;
45cfd468 7931
b4f54984 7932 if (dwarf_read_debug)
45cfd468
DE
7933 {
7934 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7935 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7936 }
10b3939b
DJ
7937}
7938
7939/* Free all allocated queue entries. This function only releases anything if
7940 an error was thrown; if the queue was processed then it would have been
7941 freed as we went along. */
7942
7943static void
7944dwarf2_release_queue (void *dummy)
7945{
7946 struct dwarf2_queue_item *item, *last;
7947
7948 item = dwarf2_queue;
7949 while (item)
7950 {
7951 /* Anything still marked queued is likely to be in an
7952 inconsistent state, so discard it. */
7953 if (item->per_cu->queued)
7954 {
7955 if (item->per_cu->cu != NULL)
dee91e82 7956 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7957 item->per_cu->queued = 0;
7958 }
7959
7960 last = item;
7961 item = item->next;
7962 xfree (last);
7963 }
7964
7965 dwarf2_queue = dwarf2_queue_tail = NULL;
7966}
7967
7968/* Read in full symbols for PST, and anything it depends on. */
7969
c906108c 7970static void
fba45db2 7971psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7972{
10b3939b 7973 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7974 int i;
7975
95554aad
TT
7976 if (pst->readin)
7977 return;
7978
aaa75496 7979 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7980 if (!pst->dependencies[i]->readin
7981 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7982 {
7983 /* Inform about additional files that need to be read in. */
7984 if (info_verbose)
7985 {
a3f17187 7986 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7987 fputs_filtered (" ", gdb_stdout);
7988 wrap_here ("");
7989 fputs_filtered ("and ", gdb_stdout);
7990 wrap_here ("");
7991 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7992 wrap_here (""); /* Flush output. */
aaa75496
JB
7993 gdb_flush (gdb_stdout);
7994 }
7995 psymtab_to_symtab_1 (pst->dependencies[i]);
7996 }
7997
9a3c8263 7998 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7999
8000 if (per_cu == NULL)
aaa75496
JB
8001 {
8002 /* It's an include file, no symbols to read for it.
8003 Everything is in the parent symtab. */
8004 pst->readin = 1;
8005 return;
8006 }
c906108c 8007
a0f42c21 8008 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
8009}
8010
dee91e82
DE
8011/* Trivial hash function for die_info: the hash value of a DIE
8012 is its offset in .debug_info for this objfile. */
10b3939b 8013
dee91e82
DE
8014static hashval_t
8015die_hash (const void *item)
10b3939b 8016{
9a3c8263 8017 const struct die_info *die = (const struct die_info *) item;
6502dd73 8018
9c541725 8019 return to_underlying (die->sect_off);
dee91e82 8020}
63d06c5c 8021
dee91e82
DE
8022/* Trivial comparison function for die_info structures: two DIEs
8023 are equal if they have the same offset. */
98bfdba5 8024
dee91e82
DE
8025static int
8026die_eq (const void *item_lhs, const void *item_rhs)
8027{
9a3c8263
SM
8028 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8029 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8030
9c541725 8031 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8032}
c906108c 8033
dee91e82
DE
8034/* die_reader_func for load_full_comp_unit.
8035 This is identical to read_signatured_type_reader,
8036 but is kept separate for now. */
c906108c 8037
dee91e82
DE
8038static void
8039load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8040 const gdb_byte *info_ptr,
dee91e82
DE
8041 struct die_info *comp_unit_die,
8042 int has_children,
8043 void *data)
8044{
8045 struct dwarf2_cu *cu = reader->cu;
9a3c8263 8046 enum language *language_ptr = (enum language *) data;
6caca83c 8047
dee91e82
DE
8048 gdb_assert (cu->die_hash == NULL);
8049 cu->die_hash =
8050 htab_create_alloc_ex (cu->header.length / 12,
8051 die_hash,
8052 die_eq,
8053 NULL,
8054 &cu->comp_unit_obstack,
8055 hashtab_obstack_allocate,
8056 dummy_obstack_deallocate);
e142c38c 8057
dee91e82
DE
8058 if (has_children)
8059 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8060 &info_ptr, comp_unit_die);
8061 cu->dies = comp_unit_die;
8062 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8063
8064 /* We try not to read any attributes in this function, because not
9cdd5dbd 8065 all CUs needed for references have been loaded yet, and symbol
10b3939b 8066 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8067 or we won't be able to build types correctly.
8068 Similarly, if we do not read the producer, we can not apply
8069 producer-specific interpretation. */
95554aad 8070 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8071}
10b3939b 8072
dee91e82 8073/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8074
dee91e82 8075static void
95554aad
TT
8076load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8077 enum language pretend_language)
dee91e82 8078{
3019eac3 8079 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8080
f4dc4d17
DE
8081 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8082 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8083}
8084
3da10d80
KS
8085/* Add a DIE to the delayed physname list. */
8086
8087static void
8088add_to_method_list (struct type *type, int fnfield_index, int index,
8089 const char *name, struct die_info *die,
8090 struct dwarf2_cu *cu)
8091{
8092 struct delayed_method_info mi;
8093 mi.type = type;
8094 mi.fnfield_index = fnfield_index;
8095 mi.index = index;
8096 mi.name = name;
8097 mi.die = die;
8098 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8099}
8100
8101/* A cleanup for freeing the delayed method list. */
8102
8103static void
8104free_delayed_list (void *ptr)
8105{
8106 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8107 if (cu->method_list != NULL)
8108 {
8109 VEC_free (delayed_method_info, cu->method_list);
8110 cu->method_list = NULL;
8111 }
8112}
8113
3693fdb3
PA
8114/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8115 "const" / "volatile". If so, decrements LEN by the length of the
8116 modifier and return true. Otherwise return false. */
8117
8118template<size_t N>
8119static bool
8120check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8121{
8122 size_t mod_len = sizeof (mod) - 1;
8123 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8124 {
8125 len -= mod_len;
8126 return true;
8127 }
8128 return false;
8129}
8130
3da10d80
KS
8131/* Compute the physnames of any methods on the CU's method list.
8132
8133 The computation of method physnames is delayed in order to avoid the
8134 (bad) condition that one of the method's formal parameters is of an as yet
8135 incomplete type. */
8136
8137static void
8138compute_delayed_physnames (struct dwarf2_cu *cu)
8139{
8140 int i;
8141 struct delayed_method_info *mi;
3693fdb3
PA
8142
8143 /* Only C++ delays computing physnames. */
8144 if (VEC_empty (delayed_method_info, cu->method_list))
8145 return;
8146 gdb_assert (cu->language == language_cplus);
8147
3da10d80
KS
8148 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8149 {
1d06ead6 8150 const char *physname;
3da10d80
KS
8151 struct fn_fieldlist *fn_flp
8152 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8153 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8154 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8155 = physname ? physname : "";
3693fdb3
PA
8156
8157 /* Since there's no tag to indicate whether a method is a
8158 const/volatile overload, extract that information out of the
8159 demangled name. */
8160 if (physname != NULL)
8161 {
8162 size_t len = strlen (physname);
8163
8164 while (1)
8165 {
8166 if (physname[len] == ')') /* shortcut */
8167 break;
8168 else if (check_modifier (physname, len, " const"))
8169 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8170 else if (check_modifier (physname, len, " volatile"))
8171 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8172 else
8173 break;
8174 }
8175 }
3da10d80
KS
8176 }
8177}
8178
a766d390
DE
8179/* Go objects should be embedded in a DW_TAG_module DIE,
8180 and it's not clear if/how imported objects will appear.
8181 To keep Go support simple until that's worked out,
8182 go back through what we've read and create something usable.
8183 We could do this while processing each DIE, and feels kinda cleaner,
8184 but that way is more invasive.
8185 This is to, for example, allow the user to type "p var" or "b main"
8186 without having to specify the package name, and allow lookups
8187 of module.object to work in contexts that use the expression
8188 parser. */
8189
8190static void
8191fixup_go_packaging (struct dwarf2_cu *cu)
8192{
8193 char *package_name = NULL;
8194 struct pending *list;
8195 int i;
8196
8197 for (list = global_symbols; list != NULL; list = list->next)
8198 {
8199 for (i = 0; i < list->nsyms; ++i)
8200 {
8201 struct symbol *sym = list->symbol[i];
8202
8203 if (SYMBOL_LANGUAGE (sym) == language_go
8204 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8205 {
8206 char *this_package_name = go_symbol_package_name (sym);
8207
8208 if (this_package_name == NULL)
8209 continue;
8210 if (package_name == NULL)
8211 package_name = this_package_name;
8212 else
8213 {
8214 if (strcmp (package_name, this_package_name) != 0)
8215 complaint (&symfile_complaints,
8216 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8217 (symbol_symtab (sym) != NULL
8218 ? symtab_to_filename_for_display
8219 (symbol_symtab (sym))
4262abfb 8220 : objfile_name (cu->objfile)),
a766d390
DE
8221 this_package_name, package_name);
8222 xfree (this_package_name);
8223 }
8224 }
8225 }
8226 }
8227
8228 if (package_name != NULL)
8229 {
8230 struct objfile *objfile = cu->objfile;
34a68019 8231 const char *saved_package_name
224c3ddb
SM
8232 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8233 package_name,
8234 strlen (package_name));
19f392bc
UW
8235 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8236 saved_package_name);
a766d390
DE
8237 struct symbol *sym;
8238
8239 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8240
e623cf5d 8241 sym = allocate_symbol (objfile);
f85f34ed 8242 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8243 SYMBOL_SET_NAMES (sym, saved_package_name,
8244 strlen (saved_package_name), 0, objfile);
a766d390
DE
8245 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8246 e.g., "main" finds the "main" module and not C's main(). */
8247 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8248 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8249 SYMBOL_TYPE (sym) = type;
8250
8251 add_symbol_to_list (sym, &global_symbols);
8252
8253 xfree (package_name);
8254 }
8255}
8256
95554aad
TT
8257/* Return the symtab for PER_CU. This works properly regardless of
8258 whether we're using the index or psymtabs. */
8259
43f3e411
DE
8260static struct compunit_symtab *
8261get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8262{
8263 return (dwarf2_per_objfile->using_index
43f3e411
DE
8264 ? per_cu->v.quick->compunit_symtab
8265 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8266}
8267
8268/* A helper function for computing the list of all symbol tables
8269 included by PER_CU. */
8270
8271static void
43f3e411 8272recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8273 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8274 struct dwarf2_per_cu_data *per_cu,
43f3e411 8275 struct compunit_symtab *immediate_parent)
95554aad
TT
8276{
8277 void **slot;
8278 int ix;
43f3e411 8279 struct compunit_symtab *cust;
95554aad
TT
8280 struct dwarf2_per_cu_data *iter;
8281
8282 slot = htab_find_slot (all_children, per_cu, INSERT);
8283 if (*slot != NULL)
8284 {
8285 /* This inclusion and its children have been processed. */
8286 return;
8287 }
8288
8289 *slot = per_cu;
8290 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8291 cust = get_compunit_symtab (per_cu);
8292 if (cust != NULL)
ec94af83
DE
8293 {
8294 /* If this is a type unit only add its symbol table if we haven't
8295 seen it yet (type unit per_cu's can share symtabs). */
8296 if (per_cu->is_debug_types)
8297 {
43f3e411 8298 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8299 if (*slot == NULL)
8300 {
43f3e411
DE
8301 *slot = cust;
8302 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8303 if (cust->user == NULL)
8304 cust->user = immediate_parent;
ec94af83
DE
8305 }
8306 }
8307 else
f9125b6c 8308 {
43f3e411
DE
8309 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8310 if (cust->user == NULL)
8311 cust->user = immediate_parent;
f9125b6c 8312 }
ec94af83 8313 }
95554aad
TT
8314
8315 for (ix = 0;
796a7ff8 8316 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8317 ++ix)
ec94af83
DE
8318 {
8319 recursively_compute_inclusions (result, all_children,
43f3e411 8320 all_type_symtabs, iter, cust);
ec94af83 8321 }
95554aad
TT
8322}
8323
43f3e411 8324/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8325 PER_CU. */
8326
8327static void
43f3e411 8328compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8329{
f4dc4d17
DE
8330 gdb_assert (! per_cu->is_debug_types);
8331
796a7ff8 8332 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8333 {
8334 int ix, len;
ec94af83 8335 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8336 struct compunit_symtab *compunit_symtab_iter;
8337 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8338 htab_t all_children, all_type_symtabs;
43f3e411 8339 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8340
8341 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8342 if (cust == NULL)
95554aad
TT
8343 return;
8344
8345 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8346 NULL, xcalloc, xfree);
ec94af83
DE
8347 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8348 NULL, xcalloc, xfree);
95554aad
TT
8349
8350 for (ix = 0;
796a7ff8 8351 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8352 ix, per_cu_iter);
95554aad 8353 ++ix)
ec94af83
DE
8354 {
8355 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8356 all_type_symtabs, per_cu_iter,
43f3e411 8357 cust);
ec94af83 8358 }
95554aad 8359
ec94af83 8360 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8361 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8362 cust->includes
8d749320
SM
8363 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8364 struct compunit_symtab *, len + 1);
95554aad 8365 for (ix = 0;
43f3e411
DE
8366 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8367 compunit_symtab_iter);
95554aad 8368 ++ix)
43f3e411
DE
8369 cust->includes[ix] = compunit_symtab_iter;
8370 cust->includes[len] = NULL;
95554aad 8371
43f3e411 8372 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8373 htab_delete (all_children);
ec94af83 8374 htab_delete (all_type_symtabs);
95554aad
TT
8375 }
8376}
8377
8378/* Compute the 'includes' field for the symtabs of all the CUs we just
8379 read. */
8380
8381static void
8382process_cu_includes (void)
8383{
8384 int ix;
8385 struct dwarf2_per_cu_data *iter;
8386
8387 for (ix = 0;
8388 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8389 ix, iter);
8390 ++ix)
f4dc4d17
DE
8391 {
8392 if (! iter->is_debug_types)
43f3e411 8393 compute_compunit_symtab_includes (iter);
f4dc4d17 8394 }
95554aad
TT
8395
8396 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8397}
8398
9cdd5dbd 8399/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8400 already been loaded into memory. */
8401
8402static void
95554aad
TT
8403process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8404 enum language pretend_language)
10b3939b 8405{
10b3939b 8406 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8407 struct objfile *objfile = per_cu->objfile;
3e29f34a 8408 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8409 CORE_ADDR lowpc, highpc;
43f3e411 8410 struct compunit_symtab *cust;
33c7c59d 8411 struct cleanup *delayed_list_cleanup;
10b3939b 8412 CORE_ADDR baseaddr;
4359dff1 8413 struct block *static_block;
3e29f34a 8414 CORE_ADDR addr;
10b3939b
DJ
8415
8416 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8417
10b3939b 8418 buildsym_init ();
33c7c59d 8419 scoped_free_pendings free_pending;
3da10d80 8420 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8421
8422 cu->list_in_scope = &file_symbols;
c906108c 8423
95554aad
TT
8424 cu->language = pretend_language;
8425 cu->language_defn = language_def (cu->language);
8426
c906108c 8427 /* Do line number decoding in read_file_scope () */
10b3939b 8428 process_die (cu->dies, cu);
c906108c 8429
a766d390
DE
8430 /* For now fudge the Go package. */
8431 if (cu->language == language_go)
8432 fixup_go_packaging (cu);
8433
3da10d80
KS
8434 /* Now that we have processed all the DIEs in the CU, all the types
8435 should be complete, and it should now be safe to compute all of the
8436 physnames. */
8437 compute_delayed_physnames (cu);
8438 do_cleanups (delayed_list_cleanup);
8439
fae299cd
DC
8440 /* Some compilers don't define a DW_AT_high_pc attribute for the
8441 compilation unit. If the DW_AT_high_pc is missing, synthesize
8442 it, by scanning the DIE's below the compilation unit. */
10b3939b 8443 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8444
3e29f34a
MR
8445 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8446 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8447
8448 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8449 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8450 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8451 addrmap to help ensure it has an accurate map of pc values belonging to
8452 this comp unit. */
8453 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8454
43f3e411
DE
8455 cust = end_symtab_from_static_block (static_block,
8456 SECT_OFF_TEXT (objfile), 0);
c906108c 8457
43f3e411 8458 if (cust != NULL)
c906108c 8459 {
df15bd07 8460 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8461
8be455d7
JK
8462 /* Set symtab language to language from DW_AT_language. If the
8463 compilation is from a C file generated by language preprocessors, do
8464 not set the language if it was already deduced by start_subfile. */
43f3e411 8465 if (!(cu->language == language_c
40e3ad0e 8466 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8467 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8468
8469 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8470 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8471 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8472 there were bugs in prologue debug info, fixed later in GCC-4.5
8473 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8474
8475 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8476 needed, it would be wrong due to missing DW_AT_producer there.
8477
8478 Still one can confuse GDB by using non-standard GCC compilation
8479 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8480 */
ab260dad 8481 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8482 cust->locations_valid = 1;
e0d00bc7
JK
8483
8484 if (gcc_4_minor >= 5)
43f3e411 8485 cust->epilogue_unwind_valid = 1;
96408a79 8486
43f3e411 8487 cust->call_site_htab = cu->call_site_htab;
c906108c 8488 }
9291a0cd
TT
8489
8490 if (dwarf2_per_objfile->using_index)
43f3e411 8491 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8492 else
8493 {
8494 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8495 pst->compunit_symtab = cust;
9291a0cd
TT
8496 pst->readin = 1;
8497 }
c906108c 8498
95554aad
TT
8499 /* Push it for inclusion processing later. */
8500 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 8501}
45cfd468 8502
f4dc4d17
DE
8503/* Generate full symbol information for type unit PER_CU, whose DIEs have
8504 already been loaded into memory. */
8505
8506static void
8507process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8508 enum language pretend_language)
8509{
8510 struct dwarf2_cu *cu = per_cu->cu;
8511 struct objfile *objfile = per_cu->objfile;
43f3e411 8512 struct compunit_symtab *cust;
33c7c59d 8513 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
8514 struct signatured_type *sig_type;
8515
8516 gdb_assert (per_cu->is_debug_types);
8517 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8518
8519 buildsym_init ();
33c7c59d 8520 scoped_free_pendings free_pending;
f4dc4d17
DE
8521 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8522
8523 cu->list_in_scope = &file_symbols;
8524
8525 cu->language = pretend_language;
8526 cu->language_defn = language_def (cu->language);
8527
8528 /* The symbol tables are set up in read_type_unit_scope. */
8529 process_die (cu->dies, cu);
8530
8531 /* For now fudge the Go package. */
8532 if (cu->language == language_go)
8533 fixup_go_packaging (cu);
8534
8535 /* Now that we have processed all the DIEs in the CU, all the types
8536 should be complete, and it should now be safe to compute all of the
8537 physnames. */
8538 compute_delayed_physnames (cu);
8539 do_cleanups (delayed_list_cleanup);
8540
8541 /* TUs share symbol tables.
8542 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8543 of it with end_expandable_symtab. Otherwise, complete the addition of
8544 this TU's symbols to the existing symtab. */
43f3e411 8545 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8546 {
43f3e411
DE
8547 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8548 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8549
43f3e411 8550 if (cust != NULL)
f4dc4d17
DE
8551 {
8552 /* Set symtab language to language from DW_AT_language. If the
8553 compilation is from a C file generated by language preprocessors,
8554 do not set the language if it was already deduced by
8555 start_subfile. */
43f3e411
DE
8556 if (!(cu->language == language_c
8557 && COMPUNIT_FILETABS (cust)->language != language_c))
8558 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8559 }
8560 }
8561 else
8562 {
0ab9ce85 8563 augment_type_symtab ();
43f3e411 8564 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8565 }
8566
8567 if (dwarf2_per_objfile->using_index)
43f3e411 8568 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8569 else
8570 {
8571 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8572 pst->compunit_symtab = cust;
f4dc4d17 8573 pst->readin = 1;
45cfd468 8574 }
c906108c
SS
8575}
8576
95554aad
TT
8577/* Process an imported unit DIE. */
8578
8579static void
8580process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8581{
8582 struct attribute *attr;
8583
f4dc4d17
DE
8584 /* For now we don't handle imported units in type units. */
8585 if (cu->per_cu->is_debug_types)
8586 {
8587 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8588 " supported in type units [in module %s]"),
4262abfb 8589 objfile_name (cu->objfile));
f4dc4d17
DE
8590 }
8591
95554aad
TT
8592 attr = dwarf2_attr (die, DW_AT_import, cu);
8593 if (attr != NULL)
8594 {
9c541725
PA
8595 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8596 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8597 dwarf2_per_cu_data *per_cu
8598 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8599
69d751e3 8600 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8601 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8602 load_full_comp_unit (per_cu, cu->language);
8603
796a7ff8 8604 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8605 per_cu);
8606 }
8607}
8608
4c8aa72d
PA
8609/* RAII object that represents a process_die scope: i.e.,
8610 starts/finishes processing a DIE. */
8611class process_die_scope
adde2bff 8612{
4c8aa72d
PA
8613public:
8614 process_die_scope (die_info *die, dwarf2_cu *cu)
8615 : m_die (die), m_cu (cu)
8616 {
8617 /* We should only be processing DIEs not already in process. */
8618 gdb_assert (!m_die->in_process);
8619 m_die->in_process = true;
8620 }
8c3cb9fa 8621
4c8aa72d
PA
8622 ~process_die_scope ()
8623 {
8624 m_die->in_process = false;
8625
8626 /* If we're done processing the DIE for the CU that owns the line
8627 header, we don't need the line header anymore. */
8628 if (m_cu->line_header_die_owner == m_die)
8629 {
8630 delete m_cu->line_header;
8631 m_cu->line_header = NULL;
8632 m_cu->line_header_die_owner = NULL;
8633 }
8634 }
8635
8636private:
8637 die_info *m_die;
8638 dwarf2_cu *m_cu;
8639};
adde2bff 8640
c906108c
SS
8641/* Process a die and its children. */
8642
8643static void
e7c27a73 8644process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8645{
4c8aa72d 8646 process_die_scope scope (die, cu);
adde2bff 8647
c906108c
SS
8648 switch (die->tag)
8649 {
8650 case DW_TAG_padding:
8651 break;
8652 case DW_TAG_compile_unit:
95554aad 8653 case DW_TAG_partial_unit:
e7c27a73 8654 read_file_scope (die, cu);
c906108c 8655 break;
348e048f
DE
8656 case DW_TAG_type_unit:
8657 read_type_unit_scope (die, cu);
8658 break;
c906108c 8659 case DW_TAG_subprogram:
c906108c 8660 case DW_TAG_inlined_subroutine:
edb3359d 8661 read_func_scope (die, cu);
c906108c
SS
8662 break;
8663 case DW_TAG_lexical_block:
14898363
L
8664 case DW_TAG_try_block:
8665 case DW_TAG_catch_block:
e7c27a73 8666 read_lexical_block_scope (die, cu);
c906108c 8667 break;
216f72a1 8668 case DW_TAG_call_site:
96408a79
SA
8669 case DW_TAG_GNU_call_site:
8670 read_call_site_scope (die, cu);
8671 break;
c906108c 8672 case DW_TAG_class_type:
680b30c7 8673 case DW_TAG_interface_type:
c906108c
SS
8674 case DW_TAG_structure_type:
8675 case DW_TAG_union_type:
134d01f1 8676 process_structure_scope (die, cu);
c906108c
SS
8677 break;
8678 case DW_TAG_enumeration_type:
134d01f1 8679 process_enumeration_scope (die, cu);
c906108c 8680 break;
134d01f1 8681
f792889a
DJ
8682 /* These dies have a type, but processing them does not create
8683 a symbol or recurse to process the children. Therefore we can
8684 read them on-demand through read_type_die. */
c906108c 8685 case DW_TAG_subroutine_type:
72019c9c 8686 case DW_TAG_set_type:
c906108c 8687 case DW_TAG_array_type:
c906108c 8688 case DW_TAG_pointer_type:
c906108c 8689 case DW_TAG_ptr_to_member_type:
c906108c 8690 case DW_TAG_reference_type:
4297a3f0 8691 case DW_TAG_rvalue_reference_type:
c906108c 8692 case DW_TAG_string_type:
c906108c 8693 break;
134d01f1 8694
c906108c 8695 case DW_TAG_base_type:
a02abb62 8696 case DW_TAG_subrange_type:
cb249c71 8697 case DW_TAG_typedef:
134d01f1
DJ
8698 /* Add a typedef symbol for the type definition, if it has a
8699 DW_AT_name. */
f792889a 8700 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8701 break;
c906108c 8702 case DW_TAG_common_block:
e7c27a73 8703 read_common_block (die, cu);
c906108c
SS
8704 break;
8705 case DW_TAG_common_inclusion:
8706 break;
d9fa45fe 8707 case DW_TAG_namespace:
4d4ec4e5 8708 cu->processing_has_namespace_info = 1;
e7c27a73 8709 read_namespace (die, cu);
d9fa45fe 8710 break;
5d7cb8df 8711 case DW_TAG_module:
4d4ec4e5 8712 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8713 read_module (die, cu);
8714 break;
d9fa45fe 8715 case DW_TAG_imported_declaration:
74921315
KS
8716 cu->processing_has_namespace_info = 1;
8717 if (read_namespace_alias (die, cu))
8718 break;
8719 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8720 case DW_TAG_imported_module:
4d4ec4e5 8721 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8722 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8723 || cu->language != language_fortran))
8724 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8725 dwarf_tag_name (die->tag));
8726 read_import_statement (die, cu);
d9fa45fe 8727 break;
95554aad
TT
8728
8729 case DW_TAG_imported_unit:
8730 process_imported_unit_die (die, cu);
8731 break;
8732
c906108c 8733 default:
e7c27a73 8734 new_symbol (die, NULL, cu);
c906108c
SS
8735 break;
8736 }
8737}
ca69b9e6
DE
8738\f
8739/* DWARF name computation. */
c906108c 8740
94af9270
KS
8741/* A helper function for dwarf2_compute_name which determines whether DIE
8742 needs to have the name of the scope prepended to the name listed in the
8743 die. */
8744
8745static int
8746die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8747{
1c809c68
TT
8748 struct attribute *attr;
8749
94af9270
KS
8750 switch (die->tag)
8751 {
8752 case DW_TAG_namespace:
8753 case DW_TAG_typedef:
8754 case DW_TAG_class_type:
8755 case DW_TAG_interface_type:
8756 case DW_TAG_structure_type:
8757 case DW_TAG_union_type:
8758 case DW_TAG_enumeration_type:
8759 case DW_TAG_enumerator:
8760 case DW_TAG_subprogram:
08a76f8a 8761 case DW_TAG_inlined_subroutine:
94af9270 8762 case DW_TAG_member:
74921315 8763 case DW_TAG_imported_declaration:
94af9270
KS
8764 return 1;
8765
8766 case DW_TAG_variable:
c2b0a229 8767 case DW_TAG_constant:
94af9270
KS
8768 /* We only need to prefix "globally" visible variables. These include
8769 any variable marked with DW_AT_external or any variable that
8770 lives in a namespace. [Variables in anonymous namespaces
8771 require prefixing, but they are not DW_AT_external.] */
8772
8773 if (dwarf2_attr (die, DW_AT_specification, cu))
8774 {
8775 struct dwarf2_cu *spec_cu = cu;
9a619af0 8776
94af9270
KS
8777 return die_needs_namespace (die_specification (die, &spec_cu),
8778 spec_cu);
8779 }
8780
1c809c68 8781 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8782 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8783 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8784 return 0;
8785 /* A variable in a lexical block of some kind does not need a
8786 namespace, even though in C++ such variables may be external
8787 and have a mangled name. */
8788 if (die->parent->tag == DW_TAG_lexical_block
8789 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8790 || die->parent->tag == DW_TAG_catch_block
8791 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8792 return 0;
8793 return 1;
94af9270
KS
8794
8795 default:
8796 return 0;
8797 }
8798}
8799
73b9be8b
KS
8800/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8801 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8802 defined for the given DIE. */
8803
8804static struct attribute *
8805dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8806{
8807 struct attribute *attr;
8808
8809 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8810 if (attr == NULL)
8811 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8812
8813 return attr;
8814}
8815
8816/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8817 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8818 defined for the given DIE. */
8819
8820static const char *
8821dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8822{
8823 const char *linkage_name;
8824
8825 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8826 if (linkage_name == NULL)
8827 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8828
8829 return linkage_name;
8830}
8831
94af9270 8832/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8833 compute the physname for the object, which include a method's:
9c37b5ae 8834 - formal parameters (C++),
a766d390 8835 - receiver type (Go),
a766d390
DE
8836
8837 The term "physname" is a bit confusing.
8838 For C++, for example, it is the demangled name.
8839 For Go, for example, it's the mangled name.
94af9270 8840
af6b7be1
JB
8841 For Ada, return the DIE's linkage name rather than the fully qualified
8842 name. PHYSNAME is ignored..
8843
94af9270
KS
8844 The result is allocated on the objfile_obstack and canonicalized. */
8845
8846static const char *
15d034d0
TT
8847dwarf2_compute_name (const char *name,
8848 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8849 int physname)
8850{
bb5ed363
DE
8851 struct objfile *objfile = cu->objfile;
8852
94af9270
KS
8853 if (name == NULL)
8854 name = dwarf2_name (die, cu);
8855
2ee7123e
DE
8856 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8857 but otherwise compute it by typename_concat inside GDB.
8858 FIXME: Actually this is not really true, or at least not always true.
8859 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8860 Fortran names because there is no mangling standard. So new_symbol_full
8861 will set the demangled name to the result of dwarf2_full_name, and it is
8862 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8863 if (cu->language == language_ada
8864 || (cu->language == language_fortran && physname))
8865 {
8866 /* For Ada unit, we prefer the linkage name over the name, as
8867 the former contains the exported name, which the user expects
8868 to be able to reference. Ideally, we want the user to be able
8869 to reference this entity using either natural or linkage name,
8870 but we haven't started looking at this enhancement yet. */
73b9be8b 8871 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 8872
2ee7123e
DE
8873 if (linkage_name != NULL)
8874 return linkage_name;
f55ee35c
JK
8875 }
8876
94af9270
KS
8877 /* These are the only languages we know how to qualify names in. */
8878 if (name != NULL
9c37b5ae 8879 && (cu->language == language_cplus
c44af4eb
TT
8880 || cu->language == language_fortran || cu->language == language_d
8881 || cu->language == language_rust))
94af9270
KS
8882 {
8883 if (die_needs_namespace (die, cu))
8884 {
8885 long length;
0d5cff50 8886 const char *prefix;
34a68019 8887 const char *canonical_name = NULL;
94af9270 8888
d7e74731
PA
8889 string_file buf;
8890
94af9270 8891 prefix = determine_prefix (die, cu);
94af9270
KS
8892 if (*prefix != '\0')
8893 {
f55ee35c
JK
8894 char *prefixed_name = typename_concat (NULL, prefix, name,
8895 physname, cu);
9a619af0 8896
d7e74731 8897 buf.puts (prefixed_name);
94af9270
KS
8898 xfree (prefixed_name);
8899 }
8900 else
d7e74731 8901 buf.puts (name);
94af9270 8902
98bfdba5
PA
8903 /* Template parameters may be specified in the DIE's DW_AT_name, or
8904 as children with DW_TAG_template_type_param or
8905 DW_TAG_value_type_param. If the latter, add them to the name
8906 here. If the name already has template parameters, then
8907 skip this step; some versions of GCC emit both, and
8908 it is more efficient to use the pre-computed name.
8909
8910 Something to keep in mind about this process: it is very
8911 unlikely, or in some cases downright impossible, to produce
8912 something that will match the mangled name of a function.
8913 If the definition of the function has the same debug info,
8914 we should be able to match up with it anyway. But fallbacks
8915 using the minimal symbol, for instance to find a method
8916 implemented in a stripped copy of libstdc++, will not work.
8917 If we do not have debug info for the definition, we will have to
8918 match them up some other way.
8919
8920 When we do name matching there is a related problem with function
8921 templates; two instantiated function templates are allowed to
8922 differ only by their return types, which we do not add here. */
8923
8924 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8925 {
8926 struct attribute *attr;
8927 struct die_info *child;
8928 int first = 1;
8929
8930 die->building_fullname = 1;
8931
8932 for (child = die->child; child != NULL; child = child->sibling)
8933 {
8934 struct type *type;
12df843f 8935 LONGEST value;
d521ce57 8936 const gdb_byte *bytes;
98bfdba5
PA
8937 struct dwarf2_locexpr_baton *baton;
8938 struct value *v;
8939
8940 if (child->tag != DW_TAG_template_type_param
8941 && child->tag != DW_TAG_template_value_param)
8942 continue;
8943
8944 if (first)
8945 {
d7e74731 8946 buf.puts ("<");
98bfdba5
PA
8947 first = 0;
8948 }
8949 else
d7e74731 8950 buf.puts (", ");
98bfdba5
PA
8951
8952 attr = dwarf2_attr (child, DW_AT_type, cu);
8953 if (attr == NULL)
8954 {
8955 complaint (&symfile_complaints,
8956 _("template parameter missing DW_AT_type"));
d7e74731 8957 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8958 continue;
8959 }
8960 type = die_type (child, cu);
8961
8962 if (child->tag == DW_TAG_template_type_param)
8963 {
d7e74731 8964 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8965 continue;
8966 }
8967
8968 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8969 if (attr == NULL)
8970 {
8971 complaint (&symfile_complaints,
3e43a32a
MS
8972 _("template parameter missing "
8973 "DW_AT_const_value"));
d7e74731 8974 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8975 continue;
8976 }
8977
8978 dwarf2_const_value_attr (attr, type, name,
8979 &cu->comp_unit_obstack, cu,
8980 &value, &bytes, &baton);
8981
8982 if (TYPE_NOSIGN (type))
8983 /* GDB prints characters as NUMBER 'CHAR'. If that's
8984 changed, this can use value_print instead. */
d7e74731 8985 c_printchar (value, type, &buf);
98bfdba5
PA
8986 else
8987 {
8988 struct value_print_options opts;
8989
8990 if (baton != NULL)
8991 v = dwarf2_evaluate_loc_desc (type, NULL,
8992 baton->data,
8993 baton->size,
8994 baton->per_cu);
8995 else if (bytes != NULL)
8996 {
8997 v = allocate_value (type);
8998 memcpy (value_contents_writeable (v), bytes,
8999 TYPE_LENGTH (type));
9000 }
9001 else
9002 v = value_from_longest (type, value);
9003
3e43a32a
MS
9004 /* Specify decimal so that we do not depend on
9005 the radix. */
98bfdba5
PA
9006 get_formatted_print_options (&opts, 'd');
9007 opts.raw = 1;
d7e74731 9008 value_print (v, &buf, &opts);
98bfdba5
PA
9009 release_value (v);
9010 value_free (v);
9011 }
9012 }
9013
9014 die->building_fullname = 0;
9015
9016 if (!first)
9017 {
9018 /* Close the argument list, with a space if necessary
9019 (nested templates). */
d7e74731
PA
9020 if (!buf.empty () && buf.string ().back () == '>')
9021 buf.puts (" >");
98bfdba5 9022 else
d7e74731 9023 buf.puts (">");
98bfdba5
PA
9024 }
9025 }
9026
9c37b5ae 9027 /* For C++ methods, append formal parameter type
94af9270 9028 information, if PHYSNAME. */
6e70227d 9029
94af9270 9030 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9031 && cu->language == language_cplus)
94af9270
KS
9032 {
9033 struct type *type = read_type_die (die, cu);
9034
d7e74731 9035 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9036 &type_print_raw_options);
94af9270 9037
9c37b5ae 9038 if (cu->language == language_cplus)
94af9270 9039 {
60430eff
DJ
9040 /* Assume that an artificial first parameter is
9041 "this", but do not crash if it is not. RealView
9042 marks unnamed (and thus unused) parameters as
9043 artificial; there is no way to differentiate
9044 the two cases. */
94af9270
KS
9045 if (TYPE_NFIELDS (type) > 0
9046 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9047 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9048 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9049 0))))
d7e74731 9050 buf.puts (" const");
94af9270
KS
9051 }
9052 }
9053
d7e74731 9054 const std::string &intermediate_name = buf.string ();
94af9270
KS
9055
9056 if (cu->language == language_cplus)
34a68019 9057 canonical_name
322a8516 9058 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9059 &objfile->per_bfd->storage_obstack);
9060
9061 /* If we only computed INTERMEDIATE_NAME, or if
9062 INTERMEDIATE_NAME is already canonical, then we need to
9063 copy it to the appropriate obstack. */
322a8516 9064 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9065 name = ((const char *)
9066 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9067 intermediate_name.c_str (),
9068 intermediate_name.length ()));
34a68019
TT
9069 else
9070 name = canonical_name;
94af9270
KS
9071 }
9072 }
9073
9074 return name;
9075}
9076
0114d602
DJ
9077/* Return the fully qualified name of DIE, based on its DW_AT_name.
9078 If scope qualifiers are appropriate they will be added. The result
34a68019 9079 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9080 not have a name. NAME may either be from a previous call to
9081 dwarf2_name or NULL.
9082
9c37b5ae 9083 The output string will be canonicalized (if C++). */
0114d602
DJ
9084
9085static const char *
15d034d0 9086dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9087{
94af9270
KS
9088 return dwarf2_compute_name (name, die, cu, 0);
9089}
0114d602 9090
94af9270
KS
9091/* Construct a physname for the given DIE in CU. NAME may either be
9092 from a previous call to dwarf2_name or NULL. The result will be
9093 allocated on the objfile_objstack or NULL if the DIE does not have a
9094 name.
0114d602 9095
9c37b5ae 9096 The output string will be canonicalized (if C++). */
0114d602 9097
94af9270 9098static const char *
15d034d0 9099dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9100{
bb5ed363 9101 struct objfile *objfile = cu->objfile;
900e11f9 9102 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9103 int need_copy = 1;
9104
9105 /* In this case dwarf2_compute_name is just a shortcut not building anything
9106 on its own. */
9107 if (!die_needs_namespace (die, cu))
9108 return dwarf2_compute_name (name, die, cu, 1);
9109
73b9be8b 9110 mangled = dw2_linkage_name (die, cu);
900e11f9 9111
e98c9e7c
TT
9112 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9113 See https://github.com/rust-lang/rust/issues/32925. */
9114 if (cu->language == language_rust && mangled != NULL
9115 && strchr (mangled, '{') != NULL)
9116 mangled = NULL;
9117
900e11f9
JK
9118 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9119 has computed. */
791afaa2 9120 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9121 if (mangled != NULL)
900e11f9 9122 {
900e11f9
JK
9123 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9124 type. It is easier for GDB users to search for such functions as
9125 `name(params)' than `long name(params)'. In such case the minimal
9126 symbol names do not match the full symbol names but for template
9127 functions there is never a need to look up their definition from their
9128 declaration so the only disadvantage remains the minimal symbol
9129 variant `long name(params)' does not have the proper inferior type.
9130 */
9131
a766d390
DE
9132 if (cu->language == language_go)
9133 {
9134 /* This is a lie, but we already lie to the caller new_symbol_full.
9135 new_symbol_full assumes we return the mangled name.
9136 This just undoes that lie until things are cleaned up. */
a766d390
DE
9137 }
9138 else
9139 {
791afaa2
TT
9140 demangled.reset (gdb_demangle (mangled,
9141 (DMGL_PARAMS | DMGL_ANSI
9142 | DMGL_RET_DROP)));
a766d390 9143 }
900e11f9 9144 if (demangled)
791afaa2 9145 canon = demangled.get ();
900e11f9
JK
9146 else
9147 {
9148 canon = mangled;
9149 need_copy = 0;
9150 }
9151 }
9152
9153 if (canon == NULL || check_physname)
9154 {
9155 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9156
9157 if (canon != NULL && strcmp (physname, canon) != 0)
9158 {
9159 /* It may not mean a bug in GDB. The compiler could also
9160 compute DW_AT_linkage_name incorrectly. But in such case
9161 GDB would need to be bug-to-bug compatible. */
9162
9163 complaint (&symfile_complaints,
9164 _("Computed physname <%s> does not match demangled <%s> "
9165 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9166 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9167 objfile_name (objfile));
900e11f9
JK
9168
9169 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9170 is available here - over computed PHYSNAME. It is safer
9171 against both buggy GDB and buggy compilers. */
9172
9173 retval = canon;
9174 }
9175 else
9176 {
9177 retval = physname;
9178 need_copy = 0;
9179 }
9180 }
9181 else
9182 retval = canon;
9183
9184 if (need_copy)
224c3ddb
SM
9185 retval = ((const char *)
9186 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9187 retval, strlen (retval)));
900e11f9 9188
900e11f9 9189 return retval;
0114d602
DJ
9190}
9191
74921315
KS
9192/* Inspect DIE in CU for a namespace alias. If one exists, record
9193 a new symbol for it.
9194
9195 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9196
9197static int
9198read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9199{
9200 struct attribute *attr;
9201
9202 /* If the die does not have a name, this is not a namespace
9203 alias. */
9204 attr = dwarf2_attr (die, DW_AT_name, cu);
9205 if (attr != NULL)
9206 {
9207 int num;
9208 struct die_info *d = die;
9209 struct dwarf2_cu *imported_cu = cu;
9210
9211 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9212 keep inspecting DIEs until we hit the underlying import. */
9213#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9214 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9215 {
9216 attr = dwarf2_attr (d, DW_AT_import, cu);
9217 if (attr == NULL)
9218 break;
9219
9220 d = follow_die_ref (d, attr, &imported_cu);
9221 if (d->tag != DW_TAG_imported_declaration)
9222 break;
9223 }
9224
9225 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9226 {
9227 complaint (&symfile_complaints,
9228 _("DIE at 0x%x has too many recursively imported "
9c541725 9229 "declarations"), to_underlying (d->sect_off));
74921315
KS
9230 return 0;
9231 }
9232
9233 if (attr != NULL)
9234 {
9235 struct type *type;
9c541725 9236 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9237
9c541725 9238 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9239 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9240 {
9241 /* This declaration is a global namespace alias. Add
9242 a symbol for it whose type is the aliased namespace. */
9243 new_symbol (die, type, cu);
9244 return 1;
9245 }
9246 }
9247 }
9248
9249 return 0;
9250}
9251
22cee43f
PMR
9252/* Return the using directives repository (global or local?) to use in the
9253 current context for LANGUAGE.
9254
9255 For Ada, imported declarations can materialize renamings, which *may* be
9256 global. However it is impossible (for now?) in DWARF to distinguish
9257 "external" imported declarations and "static" ones. As all imported
9258 declarations seem to be static in all other languages, make them all CU-wide
9259 global only in Ada. */
9260
9261static struct using_direct **
9262using_directives (enum language language)
9263{
9264 if (language == language_ada && context_stack_depth == 0)
9265 return &global_using_directives;
9266 else
9267 return &local_using_directives;
9268}
9269
27aa8d6a
SW
9270/* Read the import statement specified by the given die and record it. */
9271
9272static void
9273read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9274{
bb5ed363 9275 struct objfile *objfile = cu->objfile;
27aa8d6a 9276 struct attribute *import_attr;
32019081 9277 struct die_info *imported_die, *child_die;
de4affc9 9278 struct dwarf2_cu *imported_cu;
27aa8d6a 9279 const char *imported_name;
794684b6 9280 const char *imported_name_prefix;
13387711
SW
9281 const char *canonical_name;
9282 const char *import_alias;
9283 const char *imported_declaration = NULL;
794684b6 9284 const char *import_prefix;
eb1e02fd 9285 std::vector<const char *> excludes;
13387711 9286
27aa8d6a
SW
9287 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9288 if (import_attr == NULL)
9289 {
9290 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9291 dwarf_tag_name (die->tag));
9292 return;
9293 }
9294
de4affc9
CC
9295 imported_cu = cu;
9296 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9297 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9298 if (imported_name == NULL)
9299 {
9300 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9301
9302 The import in the following code:
9303 namespace A
9304 {
9305 typedef int B;
9306 }
9307
9308 int main ()
9309 {
9310 using A::B;
9311 B b;
9312 return b;
9313 }
9314
9315 ...
9316 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9317 <52> DW_AT_decl_file : 1
9318 <53> DW_AT_decl_line : 6
9319 <54> DW_AT_import : <0x75>
9320 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9321 <59> DW_AT_name : B
9322 <5b> DW_AT_decl_file : 1
9323 <5c> DW_AT_decl_line : 2
9324 <5d> DW_AT_type : <0x6e>
9325 ...
9326 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9327 <76> DW_AT_byte_size : 4
9328 <77> DW_AT_encoding : 5 (signed)
9329
9330 imports the wrong die ( 0x75 instead of 0x58 ).
9331 This case will be ignored until the gcc bug is fixed. */
9332 return;
9333 }
9334
82856980
SW
9335 /* Figure out the local name after import. */
9336 import_alias = dwarf2_name (die, cu);
27aa8d6a 9337
794684b6
SW
9338 /* Figure out where the statement is being imported to. */
9339 import_prefix = determine_prefix (die, cu);
9340
9341 /* Figure out what the scope of the imported die is and prepend it
9342 to the name of the imported die. */
de4affc9 9343 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9344
f55ee35c
JK
9345 if (imported_die->tag != DW_TAG_namespace
9346 && imported_die->tag != DW_TAG_module)
794684b6 9347 {
13387711
SW
9348 imported_declaration = imported_name;
9349 canonical_name = imported_name_prefix;
794684b6 9350 }
13387711 9351 else if (strlen (imported_name_prefix) > 0)
12aaed36 9352 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9353 imported_name_prefix,
9354 (cu->language == language_d ? "." : "::"),
9355 imported_name, (char *) NULL);
13387711
SW
9356 else
9357 canonical_name = imported_name;
794684b6 9358
32019081
JK
9359 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9360 for (child_die = die->child; child_die && child_die->tag;
9361 child_die = sibling_die (child_die))
9362 {
9363 /* DWARF-4: A Fortran use statement with a “rename list” may be
9364 represented by an imported module entry with an import attribute
9365 referring to the module and owned entries corresponding to those
9366 entities that are renamed as part of being imported. */
9367
9368 if (child_die->tag != DW_TAG_imported_declaration)
9369 {
9370 complaint (&symfile_complaints,
9371 _("child DW_TAG_imported_declaration expected "
9372 "- DIE at 0x%x [in module %s]"),
9c541725 9373 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9374 continue;
9375 }
9376
9377 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9378 if (import_attr == NULL)
9379 {
9380 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9381 dwarf_tag_name (child_die->tag));
9382 continue;
9383 }
9384
9385 imported_cu = cu;
9386 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9387 &imported_cu);
9388 imported_name = dwarf2_name (imported_die, imported_cu);
9389 if (imported_name == NULL)
9390 {
9391 complaint (&symfile_complaints,
9392 _("child DW_TAG_imported_declaration has unknown "
9393 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9394 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9395 continue;
9396 }
9397
eb1e02fd 9398 excludes.push_back (imported_name);
32019081
JK
9399
9400 process_die (child_die, cu);
9401 }
9402
22cee43f
PMR
9403 add_using_directive (using_directives (cu->language),
9404 import_prefix,
9405 canonical_name,
9406 import_alias,
9407 imported_declaration,
9408 excludes,
9409 0,
9410 &objfile->objfile_obstack);
27aa8d6a
SW
9411}
9412
5230b05a
WT
9413/* ICC<14 does not output the required DW_AT_declaration on incomplete
9414 types, but gives them a size of zero. Starting with version 14,
9415 ICC is compatible with GCC. */
9416
9417static int
9418producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9419{
9420 if (!cu->checked_producer)
9421 check_producer (cu);
9422
9423 return cu->producer_is_icc_lt_14;
9424}
9425
1b80a9fa
JK
9426/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9427 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9428 this, it was first present in GCC release 4.3.0. */
9429
9430static int
9431producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9432{
9433 if (!cu->checked_producer)
9434 check_producer (cu);
9435
9436 return cu->producer_is_gcc_lt_4_3;
9437}
9438
d721ba37
PA
9439static file_and_directory
9440find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9441{
d721ba37
PA
9442 file_and_directory res;
9443
9291a0cd
TT
9444 /* Find the filename. Do not use dwarf2_name here, since the filename
9445 is not a source language identifier. */
d721ba37
PA
9446 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9447 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9448
d721ba37
PA
9449 if (res.comp_dir == NULL
9450 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9451 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9452 {
d721ba37
PA
9453 res.comp_dir_storage = ldirname (res.name);
9454 if (!res.comp_dir_storage.empty ())
9455 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9456 }
d721ba37 9457 if (res.comp_dir != NULL)
9291a0cd
TT
9458 {
9459 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9460 directory, get rid of it. */
d721ba37 9461 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9462
d721ba37
PA
9463 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9464 res.comp_dir = cp + 1;
9291a0cd
TT
9465 }
9466
d721ba37
PA
9467 if (res.name == NULL)
9468 res.name = "<unknown>";
9469
9470 return res;
9291a0cd
TT
9471}
9472
f4dc4d17
DE
9473/* Handle DW_AT_stmt_list for a compilation unit.
9474 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9475 COMP_DIR is the compilation directory. LOWPC is passed to
9476 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9477
9478static void
9479handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9480 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9481{
527f3840 9482 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9483 struct attribute *attr;
527f3840
JK
9484 struct line_header line_header_local;
9485 hashval_t line_header_local_hash;
9486 unsigned u;
9487 void **slot;
9488 int decode_mapping;
2ab95328 9489
f4dc4d17
DE
9490 gdb_assert (! cu->per_cu->is_debug_types);
9491
2ab95328 9492 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9493 if (attr == NULL)
9494 return;
9495
9c541725 9496 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9497
9498 /* The line header hash table is only created if needed (it exists to
9499 prevent redundant reading of the line table for partial_units).
9500 If we're given a partial_unit, we'll need it. If we're given a
9501 compile_unit, then use the line header hash table if it's already
9502 created, but don't create one just yet. */
9503
9504 if (dwarf2_per_objfile->line_header_hash == NULL
9505 && die->tag == DW_TAG_partial_unit)
2ab95328 9506 {
527f3840
JK
9507 dwarf2_per_objfile->line_header_hash
9508 = htab_create_alloc_ex (127, line_header_hash_voidp,
9509 line_header_eq_voidp,
9510 free_line_header_voidp,
9511 &objfile->objfile_obstack,
9512 hashtab_obstack_allocate,
9513 dummy_obstack_deallocate);
9514 }
2ab95328 9515
9c541725 9516 line_header_local.sect_off = line_offset;
527f3840
JK
9517 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9518 line_header_local_hash = line_header_hash (&line_header_local);
9519 if (dwarf2_per_objfile->line_header_hash != NULL)
9520 {
9521 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9522 &line_header_local,
9523 line_header_local_hash, NO_INSERT);
9524
9525 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9526 is not present in *SLOT (since if there is something in *SLOT then
9527 it will be for a partial_unit). */
9528 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9529 {
527f3840 9530 gdb_assert (*slot != NULL);
9a3c8263 9531 cu->line_header = (struct line_header *) *slot;
527f3840 9532 return;
dee91e82 9533 }
2ab95328 9534 }
527f3840
JK
9535
9536 /* dwarf_decode_line_header does not yet provide sufficient information.
9537 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9538 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9539 if (lh == NULL)
527f3840 9540 return;
4c8aa72d
PA
9541
9542 cu->line_header = lh.release ();
9543 cu->line_header_die_owner = die;
527f3840
JK
9544
9545 if (dwarf2_per_objfile->line_header_hash == NULL)
9546 slot = NULL;
9547 else
9548 {
9549 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9550 &line_header_local,
9551 line_header_local_hash, INSERT);
9552 gdb_assert (slot != NULL);
9553 }
9554 if (slot != NULL && *slot == NULL)
9555 {
9556 /* This newly decoded line number information unit will be owned
9557 by line_header_hash hash table. */
9558 *slot = cu->line_header;
4c8aa72d 9559 cu->line_header_die_owner = NULL;
527f3840
JK
9560 }
9561 else
9562 {
9563 /* We cannot free any current entry in (*slot) as that struct line_header
9564 may be already used by multiple CUs. Create only temporary decoded
9565 line_header for this CU - it may happen at most once for each line
9566 number information unit. And if we're not using line_header_hash
9567 then this is what we want as well. */
9568 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9569 }
9570 decode_mapping = (die->tag != DW_TAG_partial_unit);
9571 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9572 decode_mapping);
fff8551c 9573
2ab95328
TT
9574}
9575
95554aad 9576/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9577
c906108c 9578static void
e7c27a73 9579read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9580{
dee91e82 9581 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9582 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9583 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9584 CORE_ADDR highpc = ((CORE_ADDR) 0);
9585 struct attribute *attr;
c906108c 9586 struct die_info *child_die;
e142c38c 9587 CORE_ADDR baseaddr;
6e70227d 9588
e142c38c 9589 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9590
fae299cd 9591 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9592
9593 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9594 from finish_block. */
2acceee2 9595 if (lowpc == ((CORE_ADDR) -1))
c906108c 9596 lowpc = highpc;
3e29f34a 9597 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9598
d721ba37 9599 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9600
95554aad 9601 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9602
f4b8a18d
KW
9603 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9604 standardised yet. As a workaround for the language detection we fall
9605 back to the DW_AT_producer string. */
9606 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9607 cu->language = language_opencl;
9608
3019eac3
DE
9609 /* Similar hack for Go. */
9610 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9611 set_cu_language (DW_LANG_Go, cu);
9612
d721ba37 9613 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9614
9615 /* Decode line number information if present. We do this before
9616 processing child DIEs, so that the line header table is available
9617 for DW_AT_decl_file. */
d721ba37 9618 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9619
9620 /* Process all dies in compilation unit. */
9621 if (die->child != NULL)
9622 {
9623 child_die = die->child;
9624 while (child_die && child_die->tag)
9625 {
9626 process_die (child_die, cu);
9627 child_die = sibling_die (child_die);
9628 }
9629 }
9630
9631 /* Decode macro information, if present. Dwarf 2 macro information
9632 refers to information in the line number info statement program
9633 header, so we can only read it if we've read the header
9634 successfully. */
0af92d60
JK
9635 attr = dwarf2_attr (die, DW_AT_macros, cu);
9636 if (attr == NULL)
9637 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9638 if (attr && cu->line_header)
9639 {
9640 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9641 complaint (&symfile_complaints,
0af92d60 9642 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9643
43f3e411 9644 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9645 }
9646 else
9647 {
9648 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9649 if (attr && cu->line_header)
9650 {
9651 unsigned int macro_offset = DW_UNSND (attr);
9652
43f3e411 9653 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9654 }
9655 }
3019eac3
DE
9656}
9657
f4dc4d17
DE
9658/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9659 Create the set of symtabs used by this TU, or if this TU is sharing
9660 symtabs with another TU and the symtabs have already been created
9661 then restore those symtabs in the line header.
9662 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9663
9664static void
f4dc4d17 9665setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9666{
f4dc4d17
DE
9667 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9668 struct type_unit_group *tu_group;
9669 int first_time;
3019eac3 9670 struct attribute *attr;
9c541725 9671 unsigned int i;
0186c6a7 9672 struct signatured_type *sig_type;
3019eac3 9673
f4dc4d17 9674 gdb_assert (per_cu->is_debug_types);
0186c6a7 9675 sig_type = (struct signatured_type *) per_cu;
3019eac3 9676
f4dc4d17 9677 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9678
f4dc4d17 9679 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9680 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9681 if (sig_type->type_unit_group == NULL)
9682 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9683 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9684
9685 /* If we've already processed this stmt_list there's no real need to
9686 do it again, we could fake it and just recreate the part we need
9687 (file name,index -> symtab mapping). If data shows this optimization
9688 is useful we can do it then. */
43f3e411 9689 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9690
9691 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9692 debug info. */
fff8551c 9693 line_header_up lh;
f4dc4d17 9694 if (attr != NULL)
3019eac3 9695 {
9c541725 9696 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9697 lh = dwarf_decode_line_header (line_offset, cu);
9698 }
9699 if (lh == NULL)
9700 {
9701 if (first_time)
9702 dwarf2_start_symtab (cu, "", NULL, 0);
9703 else
9704 {
9705 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9706 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9707 }
f4dc4d17 9708 return;
3019eac3
DE
9709 }
9710
4c8aa72d
PA
9711 cu->line_header = lh.release ();
9712 cu->line_header_die_owner = die;
3019eac3 9713
f4dc4d17
DE
9714 if (first_time)
9715 {
43f3e411 9716 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9717
1fd60fc0
DE
9718 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9719 still initializing it, and our caller (a few levels up)
9720 process_full_type_unit still needs to know if this is the first
9721 time. */
9722
4c8aa72d
PA
9723 tu_group->num_symtabs = cu->line_header->file_names.size ();
9724 tu_group->symtabs = XNEWVEC (struct symtab *,
9725 cu->line_header->file_names.size ());
3019eac3 9726
4c8aa72d 9727 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9728 {
4c8aa72d 9729 file_entry &fe = cu->line_header->file_names[i];
3019eac3 9730
4c8aa72d 9731 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 9732
f4dc4d17
DE
9733 if (current_subfile->symtab == NULL)
9734 {
4c8aa72d
PA
9735 /* NOTE: start_subfile will recognize when it's been
9736 passed a file it has already seen. So we can't
9737 assume there's a simple mapping from
9738 cu->line_header->file_names to subfiles, plus
9739 cu->line_header->file_names may contain dups. */
43f3e411
DE
9740 current_subfile->symtab
9741 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9742 }
9743
8c43009f
PA
9744 fe.symtab = current_subfile->symtab;
9745 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9746 }
9747 }
9748 else
3019eac3 9749 {
0ab9ce85 9750 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9751
4c8aa72d 9752 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 9753 {
4c8aa72d 9754 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 9755
4c8aa72d 9756 fe.symtab = tu_group->symtabs[i];
f4dc4d17 9757 }
3019eac3
DE
9758 }
9759
f4dc4d17
DE
9760 /* The main symtab is allocated last. Type units don't have DW_AT_name
9761 so they don't have a "real" (so to speak) symtab anyway.
9762 There is later code that will assign the main symtab to all symbols
9763 that don't have one. We need to handle the case of a symbol with a
9764 missing symtab (DW_AT_decl_file) anyway. */
9765}
3019eac3 9766
f4dc4d17
DE
9767/* Process DW_TAG_type_unit.
9768 For TUs we want to skip the first top level sibling if it's not the
9769 actual type being defined by this TU. In this case the first top
9770 level sibling is there to provide context only. */
3019eac3 9771
f4dc4d17
DE
9772static void
9773read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9774{
9775 struct die_info *child_die;
3019eac3 9776
f4dc4d17
DE
9777 prepare_one_comp_unit (cu, die, language_minimal);
9778
9779 /* Initialize (or reinitialize) the machinery for building symtabs.
9780 We do this before processing child DIEs, so that the line header table
9781 is available for DW_AT_decl_file. */
9782 setup_type_unit_groups (die, cu);
9783
9784 if (die->child != NULL)
9785 {
9786 child_die = die->child;
9787 while (child_die && child_die->tag)
9788 {
9789 process_die (child_die, cu);
9790 child_die = sibling_die (child_die);
9791 }
9792 }
3019eac3
DE
9793}
9794\f
80626a55
DE
9795/* DWO/DWP files.
9796
9797 http://gcc.gnu.org/wiki/DebugFission
9798 http://gcc.gnu.org/wiki/DebugFissionDWP
9799
9800 To simplify handling of both DWO files ("object" files with the DWARF info)
9801 and DWP files (a file with the DWOs packaged up into one file), we treat
9802 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9803
9804static hashval_t
9805hash_dwo_file (const void *item)
9806{
9a3c8263 9807 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9808 hashval_t hash;
3019eac3 9809
a2ce51a0
DE
9810 hash = htab_hash_string (dwo_file->dwo_name);
9811 if (dwo_file->comp_dir != NULL)
9812 hash += htab_hash_string (dwo_file->comp_dir);
9813 return hash;
3019eac3
DE
9814}
9815
9816static int
9817eq_dwo_file (const void *item_lhs, const void *item_rhs)
9818{
9a3c8263
SM
9819 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9820 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9821
a2ce51a0
DE
9822 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9823 return 0;
9824 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9825 return lhs->comp_dir == rhs->comp_dir;
9826 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9827}
9828
9829/* Allocate a hash table for DWO files. */
9830
9831static htab_t
9832allocate_dwo_file_hash_table (void)
9833{
9834 struct objfile *objfile = dwarf2_per_objfile->objfile;
9835
9836 return htab_create_alloc_ex (41,
9837 hash_dwo_file,
9838 eq_dwo_file,
9839 NULL,
9840 &objfile->objfile_obstack,
9841 hashtab_obstack_allocate,
9842 dummy_obstack_deallocate);
9843}
9844
80626a55
DE
9845/* Lookup DWO file DWO_NAME. */
9846
9847static void **
0ac5b59e 9848lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9849{
9850 struct dwo_file find_entry;
9851 void **slot;
9852
9853 if (dwarf2_per_objfile->dwo_files == NULL)
9854 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9855
9856 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9857 find_entry.dwo_name = dwo_name;
9858 find_entry.comp_dir = comp_dir;
80626a55
DE
9859 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9860
9861 return slot;
9862}
9863
3019eac3
DE
9864static hashval_t
9865hash_dwo_unit (const void *item)
9866{
9a3c8263 9867 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9868
9869 /* This drops the top 32 bits of the id, but is ok for a hash. */
9870 return dwo_unit->signature;
9871}
9872
9873static int
9874eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9875{
9a3c8263
SM
9876 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9877 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9878
9879 /* The signature is assumed to be unique within the DWO file.
9880 So while object file CU dwo_id's always have the value zero,
9881 that's OK, assuming each object file DWO file has only one CU,
9882 and that's the rule for now. */
9883 return lhs->signature == rhs->signature;
9884}
9885
9886/* Allocate a hash table for DWO CUs,TUs.
9887 There is one of these tables for each of CUs,TUs for each DWO file. */
9888
9889static htab_t
9890allocate_dwo_unit_table (struct objfile *objfile)
9891{
9892 /* Start out with a pretty small number.
9893 Generally DWO files contain only one CU and maybe some TUs. */
9894 return htab_create_alloc_ex (3,
9895 hash_dwo_unit,
9896 eq_dwo_unit,
9897 NULL,
9898 &objfile->objfile_obstack,
9899 hashtab_obstack_allocate,
9900 dummy_obstack_deallocate);
9901}
9902
80626a55 9903/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9904
19c3d4c9 9905struct create_dwo_cu_data
3019eac3
DE
9906{
9907 struct dwo_file *dwo_file;
19c3d4c9 9908 struct dwo_unit dwo_unit;
3019eac3
DE
9909};
9910
19c3d4c9 9911/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9912
9913static void
19c3d4c9
DE
9914create_dwo_cu_reader (const struct die_reader_specs *reader,
9915 const gdb_byte *info_ptr,
9916 struct die_info *comp_unit_die,
9917 int has_children,
9918 void *datap)
3019eac3
DE
9919{
9920 struct dwarf2_cu *cu = reader->cu;
9c541725 9921 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9922 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9923 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9924 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9925 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9926 struct attribute *attr;
3019eac3
DE
9927
9928 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9929 if (attr == NULL)
9930 {
19c3d4c9
DE
9931 complaint (&symfile_complaints,
9932 _("Dwarf Error: debug entry at offset 0x%x is missing"
9933 " its dwo_id [in module %s]"),
9c541725 9934 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9935 return;
9936 }
9937
3019eac3
DE
9938 dwo_unit->dwo_file = dwo_file;
9939 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9940 dwo_unit->section = section;
9c541725 9941 dwo_unit->sect_off = sect_off;
3019eac3
DE
9942 dwo_unit->length = cu->per_cu->length;
9943
b4f54984 9944 if (dwarf_read_debug)
4031ecc5 9945 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9946 to_underlying (sect_off),
9947 hex_string (dwo_unit->signature));
3019eac3
DE
9948}
9949
33c5cd75 9950/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 9951 Note: This function processes DWO files only, not DWP files. */
3019eac3 9952
33c5cd75
DB
9953static void
9954create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
9955 htab_t &cus_htab)
3019eac3
DE
9956{
9957 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 9958 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 9959 const gdb_byte *info_ptr, *end_ptr;
3019eac3 9960
33c5cd75
DB
9961 dwarf2_read_section (objfile, &section);
9962 info_ptr = section.buffer;
3019eac3
DE
9963
9964 if (info_ptr == NULL)
33c5cd75 9965 return;
3019eac3 9966
b4f54984 9967 if (dwarf_read_debug)
19c3d4c9
DE
9968 {
9969 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
9970 get_section_name (&section),
9971 get_section_file_name (&section));
19c3d4c9 9972 }
3019eac3 9973
33c5cd75 9974 end_ptr = info_ptr + section.size;
3019eac3
DE
9975 while (info_ptr < end_ptr)
9976 {
9977 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
9978 struct create_dwo_cu_data create_dwo_cu_data;
9979 struct dwo_unit *dwo_unit;
9980 void **slot;
9981 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 9982
19c3d4c9
DE
9983 memset (&create_dwo_cu_data.dwo_unit, 0,
9984 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9985 memset (&per_cu, 0, sizeof (per_cu));
9986 per_cu.objfile = objfile;
9987 per_cu.is_debug_types = 0;
33c5cd75
DB
9988 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9989 per_cu.section = &section;
c5ed0576 9990 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
9991
9992 init_cutu_and_read_dies_no_follow (
9993 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9994 info_ptr += per_cu.length;
9995
9996 // If the unit could not be parsed, skip it.
9997 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9998 continue;
3019eac3 9999
33c5cd75
DB
10000 if (cus_htab == NULL)
10001 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10002
33c5cd75
DB
10003 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10004 *dwo_unit = create_dwo_cu_data.dwo_unit;
10005 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10006 gdb_assert (slot != NULL);
10007 if (*slot != NULL)
19c3d4c9 10008 {
33c5cd75
DB
10009 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10010 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10011
33c5cd75
DB
10012 complaint (&symfile_complaints,
10013 _("debug cu entry at offset 0x%x is duplicate to"
10014 " the entry at offset 0x%x, signature %s"),
10015 to_underlying (sect_off), to_underlying (dup_sect_off),
10016 hex_string (dwo_unit->signature));
19c3d4c9 10017 }
33c5cd75 10018 *slot = (void *)dwo_unit;
3019eac3 10019 }
3019eac3
DE
10020}
10021
80626a55
DE
10022/* DWP file .debug_{cu,tu}_index section format:
10023 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10024
d2415c6c
DE
10025 DWP Version 1:
10026
80626a55
DE
10027 Both index sections have the same format, and serve to map a 64-bit
10028 signature to a set of section numbers. Each section begins with a header,
10029 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10030 indexes, and a pool of 32-bit section numbers. The index sections will be
10031 aligned at 8-byte boundaries in the file.
10032
d2415c6c
DE
10033 The index section header consists of:
10034
10035 V, 32 bit version number
10036 -, 32 bits unused
10037 N, 32 bit number of compilation units or type units in the index
10038 M, 32 bit number of slots in the hash table
80626a55 10039
d2415c6c 10040 Numbers are recorded using the byte order of the application binary.
80626a55 10041
d2415c6c
DE
10042 The hash table begins at offset 16 in the section, and consists of an array
10043 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10044 order of the application binary). Unused slots in the hash table are 0.
10045 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10046
d2415c6c
DE
10047 The parallel table begins immediately after the hash table
10048 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10049 array of 32-bit indexes (using the byte order of the application binary),
10050 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10051 table contains a 32-bit index into the pool of section numbers. For unused
10052 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10053
73869dc2
DE
10054 The pool of section numbers begins immediately following the hash table
10055 (at offset 16 + 12 * M from the beginning of the section). The pool of
10056 section numbers consists of an array of 32-bit words (using the byte order
10057 of the application binary). Each item in the array is indexed starting
10058 from 0. The hash table entry provides the index of the first section
10059 number in the set. Additional section numbers in the set follow, and the
10060 set is terminated by a 0 entry (section number 0 is not used in ELF).
10061
10062 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10063 section must be the first entry in the set, and the .debug_abbrev.dwo must
10064 be the second entry. Other members of the set may follow in any order.
10065
10066 ---
10067
10068 DWP Version 2:
10069
10070 DWP Version 2 combines all the .debug_info, etc. sections into one,
10071 and the entries in the index tables are now offsets into these sections.
10072 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10073 section.
10074
10075 Index Section Contents:
10076 Header
10077 Hash Table of Signatures dwp_hash_table.hash_table
10078 Parallel Table of Indices dwp_hash_table.unit_table
10079 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10080 Table of Section Sizes dwp_hash_table.v2.sizes
10081
10082 The index section header consists of:
10083
10084 V, 32 bit version number
10085 L, 32 bit number of columns in the table of section offsets
10086 N, 32 bit number of compilation units or type units in the index
10087 M, 32 bit number of slots in the hash table
10088
10089 Numbers are recorded using the byte order of the application binary.
10090
10091 The hash table has the same format as version 1.
10092 The parallel table of indices has the same format as version 1,
10093 except that the entries are origin-1 indices into the table of sections
10094 offsets and the table of section sizes.
10095
10096 The table of offsets begins immediately following the parallel table
10097 (at offset 16 + 12 * M from the beginning of the section). The table is
10098 a two-dimensional array of 32-bit words (using the byte order of the
10099 application binary), with L columns and N+1 rows, in row-major order.
10100 Each row in the array is indexed starting from 0. The first row provides
10101 a key to the remaining rows: each column in this row provides an identifier
10102 for a debug section, and the offsets in the same column of subsequent rows
10103 refer to that section. The section identifiers are:
10104
10105 DW_SECT_INFO 1 .debug_info.dwo
10106 DW_SECT_TYPES 2 .debug_types.dwo
10107 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10108 DW_SECT_LINE 4 .debug_line.dwo
10109 DW_SECT_LOC 5 .debug_loc.dwo
10110 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10111 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10112 DW_SECT_MACRO 8 .debug_macro.dwo
10113
10114 The offsets provided by the CU and TU index sections are the base offsets
10115 for the contributions made by each CU or TU to the corresponding section
10116 in the package file. Each CU and TU header contains an abbrev_offset
10117 field, used to find the abbreviations table for that CU or TU within the
10118 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10119 be interpreted as relative to the base offset given in the index section.
10120 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10121 should be interpreted as relative to the base offset for .debug_line.dwo,
10122 and offsets into other debug sections obtained from DWARF attributes should
10123 also be interpreted as relative to the corresponding base offset.
10124
10125 The table of sizes begins immediately following the table of offsets.
10126 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10127 with L columns and N rows, in row-major order. Each row in the array is
10128 indexed starting from 1 (row 0 is shared by the two tables).
10129
10130 ---
10131
10132 Hash table lookup is handled the same in version 1 and 2:
10133
10134 We assume that N and M will not exceed 2^32 - 1.
10135 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10136
d2415c6c
DE
10137 Given a 64-bit compilation unit signature or a type signature S, an entry
10138 in the hash table is located as follows:
80626a55 10139
d2415c6c
DE
10140 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10141 the low-order k bits all set to 1.
80626a55 10142
d2415c6c 10143 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10144
d2415c6c
DE
10145 3) If the hash table entry at index H matches the signature, use that
10146 entry. If the hash table entry at index H is unused (all zeroes),
10147 terminate the search: the signature is not present in the table.
80626a55 10148
d2415c6c 10149 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10150
d2415c6c 10151 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10152 to stop at an unused slot or find the match. */
80626a55
DE
10153
10154/* Create a hash table to map DWO IDs to their CU/TU entry in
10155 .debug_{info,types}.dwo in DWP_FILE.
10156 Returns NULL if there isn't one.
10157 Note: This function processes DWP files only, not DWO files. */
10158
10159static struct dwp_hash_table *
10160create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10161{
10162 struct objfile *objfile = dwarf2_per_objfile->objfile;
10163 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10164 const gdb_byte *index_ptr, *index_end;
80626a55 10165 struct dwarf2_section_info *index;
73869dc2 10166 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10167 struct dwp_hash_table *htab;
10168
10169 if (is_debug_types)
10170 index = &dwp_file->sections.tu_index;
10171 else
10172 index = &dwp_file->sections.cu_index;
10173
10174 if (dwarf2_section_empty_p (index))
10175 return NULL;
10176 dwarf2_read_section (objfile, index);
10177
10178 index_ptr = index->buffer;
10179 index_end = index_ptr + index->size;
10180
10181 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10182 index_ptr += 4;
10183 if (version == 2)
10184 nr_columns = read_4_bytes (dbfd, index_ptr);
10185 else
10186 nr_columns = 0;
10187 index_ptr += 4;
80626a55
DE
10188 nr_units = read_4_bytes (dbfd, index_ptr);
10189 index_ptr += 4;
10190 nr_slots = read_4_bytes (dbfd, index_ptr);
10191 index_ptr += 4;
10192
73869dc2 10193 if (version != 1 && version != 2)
80626a55 10194 {
21aa081e 10195 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10196 " [in module %s]"),
21aa081e 10197 pulongest (version), dwp_file->name);
80626a55
DE
10198 }
10199 if (nr_slots != (nr_slots & -nr_slots))
10200 {
21aa081e 10201 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10202 " is not power of 2 [in module %s]"),
21aa081e 10203 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10204 }
10205
10206 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10207 htab->version = version;
10208 htab->nr_columns = nr_columns;
80626a55
DE
10209 htab->nr_units = nr_units;
10210 htab->nr_slots = nr_slots;
10211 htab->hash_table = index_ptr;
10212 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10213
10214 /* Exit early if the table is empty. */
10215 if (nr_slots == 0 || nr_units == 0
10216 || (version == 2 && nr_columns == 0))
10217 {
10218 /* All must be zero. */
10219 if (nr_slots != 0 || nr_units != 0
10220 || (version == 2 && nr_columns != 0))
10221 {
10222 complaint (&symfile_complaints,
10223 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10224 " all zero [in modules %s]"),
10225 dwp_file->name);
10226 }
10227 return htab;
10228 }
10229
10230 if (version == 1)
10231 {
10232 htab->section_pool.v1.indices =
10233 htab->unit_table + sizeof (uint32_t) * nr_slots;
10234 /* It's harder to decide whether the section is too small in v1.
10235 V1 is deprecated anyway so we punt. */
10236 }
10237 else
10238 {
10239 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10240 int *ids = htab->section_pool.v2.section_ids;
10241 /* Reverse map for error checking. */
10242 int ids_seen[DW_SECT_MAX + 1];
10243 int i;
10244
10245 if (nr_columns < 2)
10246 {
10247 error (_("Dwarf Error: bad DWP hash table, too few columns"
10248 " in section table [in module %s]"),
10249 dwp_file->name);
10250 }
10251 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10252 {
10253 error (_("Dwarf Error: bad DWP hash table, too many columns"
10254 " in section table [in module %s]"),
10255 dwp_file->name);
10256 }
10257 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10258 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10259 for (i = 0; i < nr_columns; ++i)
10260 {
10261 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10262
10263 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10264 {
10265 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10266 " in section table [in module %s]"),
10267 id, dwp_file->name);
10268 }
10269 if (ids_seen[id] != -1)
10270 {
10271 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10272 " id %d in section table [in module %s]"),
10273 id, dwp_file->name);
10274 }
10275 ids_seen[id] = i;
10276 ids[i] = id;
10277 }
10278 /* Must have exactly one info or types section. */
10279 if (((ids_seen[DW_SECT_INFO] != -1)
10280 + (ids_seen[DW_SECT_TYPES] != -1))
10281 != 1)
10282 {
10283 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10284 " DWO info/types section [in module %s]"),
10285 dwp_file->name);
10286 }
10287 /* Must have an abbrev section. */
10288 if (ids_seen[DW_SECT_ABBREV] == -1)
10289 {
10290 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10291 " section [in module %s]"),
10292 dwp_file->name);
10293 }
10294 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10295 htab->section_pool.v2.sizes =
10296 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10297 * nr_units * nr_columns);
10298 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10299 * nr_units * nr_columns))
10300 > index_end)
10301 {
10302 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10303 " [in module %s]"),
10304 dwp_file->name);
10305 }
10306 }
80626a55
DE
10307
10308 return htab;
10309}
10310
10311/* Update SECTIONS with the data from SECTP.
10312
10313 This function is like the other "locate" section routines that are
10314 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10315 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10316
10317 The result is non-zero for success, or zero if an error was found. */
10318
10319static int
73869dc2
DE
10320locate_v1_virtual_dwo_sections (asection *sectp,
10321 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10322{
10323 const struct dwop_section_names *names = &dwop_section_names;
10324
10325 if (section_is_p (sectp->name, &names->abbrev_dwo))
10326 {
10327 /* There can be only one. */
049412e3 10328 if (sections->abbrev.s.section != NULL)
80626a55 10329 return 0;
049412e3 10330 sections->abbrev.s.section = sectp;
80626a55
DE
10331 sections->abbrev.size = bfd_get_section_size (sectp);
10332 }
10333 else if (section_is_p (sectp->name, &names->info_dwo)
10334 || section_is_p (sectp->name, &names->types_dwo))
10335 {
10336 /* There can be only one. */
049412e3 10337 if (sections->info_or_types.s.section != NULL)
80626a55 10338 return 0;
049412e3 10339 sections->info_or_types.s.section = sectp;
80626a55
DE
10340 sections->info_or_types.size = bfd_get_section_size (sectp);
10341 }
10342 else if (section_is_p (sectp->name, &names->line_dwo))
10343 {
10344 /* There can be only one. */
049412e3 10345 if (sections->line.s.section != NULL)
80626a55 10346 return 0;
049412e3 10347 sections->line.s.section = sectp;
80626a55
DE
10348 sections->line.size = bfd_get_section_size (sectp);
10349 }
10350 else if (section_is_p (sectp->name, &names->loc_dwo))
10351 {
10352 /* There can be only one. */
049412e3 10353 if (sections->loc.s.section != NULL)
80626a55 10354 return 0;
049412e3 10355 sections->loc.s.section = sectp;
80626a55
DE
10356 sections->loc.size = bfd_get_section_size (sectp);
10357 }
10358 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10359 {
10360 /* There can be only one. */
049412e3 10361 if (sections->macinfo.s.section != NULL)
80626a55 10362 return 0;
049412e3 10363 sections->macinfo.s.section = sectp;
80626a55
DE
10364 sections->macinfo.size = bfd_get_section_size (sectp);
10365 }
10366 else if (section_is_p (sectp->name, &names->macro_dwo))
10367 {
10368 /* There can be only one. */
049412e3 10369 if (sections->macro.s.section != NULL)
80626a55 10370 return 0;
049412e3 10371 sections->macro.s.section = sectp;
80626a55
DE
10372 sections->macro.size = bfd_get_section_size (sectp);
10373 }
10374 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10375 {
10376 /* There can be only one. */
049412e3 10377 if (sections->str_offsets.s.section != NULL)
80626a55 10378 return 0;
049412e3 10379 sections->str_offsets.s.section = sectp;
80626a55
DE
10380 sections->str_offsets.size = bfd_get_section_size (sectp);
10381 }
10382 else
10383 {
10384 /* No other kind of section is valid. */
10385 return 0;
10386 }
10387
10388 return 1;
10389}
10390
73869dc2
DE
10391/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10392 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10393 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10394 This is for DWP version 1 files. */
80626a55
DE
10395
10396static struct dwo_unit *
73869dc2
DE
10397create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10398 uint32_t unit_index,
10399 const char *comp_dir,
10400 ULONGEST signature, int is_debug_types)
80626a55
DE
10401{
10402 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10403 const struct dwp_hash_table *dwp_htab =
10404 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10405 bfd *dbfd = dwp_file->dbfd;
10406 const char *kind = is_debug_types ? "TU" : "CU";
10407 struct dwo_file *dwo_file;
10408 struct dwo_unit *dwo_unit;
73869dc2 10409 struct virtual_v1_dwo_sections sections;
80626a55 10410 void **dwo_file_slot;
80626a55
DE
10411 int i;
10412
73869dc2
DE
10413 gdb_assert (dwp_file->version == 1);
10414
b4f54984 10415 if (dwarf_read_debug)
80626a55 10416 {
73869dc2 10417 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10418 kind,
73869dc2 10419 pulongest (unit_index), hex_string (signature),
80626a55
DE
10420 dwp_file->name);
10421 }
10422
19ac8c2e 10423 /* Fetch the sections of this DWO unit.
80626a55
DE
10424 Put a limit on the number of sections we look for so that bad data
10425 doesn't cause us to loop forever. */
10426
73869dc2 10427#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10428 (1 /* .debug_info or .debug_types */ \
10429 + 1 /* .debug_abbrev */ \
10430 + 1 /* .debug_line */ \
10431 + 1 /* .debug_loc */ \
10432 + 1 /* .debug_str_offsets */ \
19ac8c2e 10433 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10434 + 1 /* trailing zero */)
10435
10436 memset (&sections, 0, sizeof (sections));
80626a55 10437
73869dc2 10438 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10439 {
10440 asection *sectp;
10441 uint32_t section_nr =
10442 read_4_bytes (dbfd,
73869dc2
DE
10443 dwp_htab->section_pool.v1.indices
10444 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10445
10446 if (section_nr == 0)
10447 break;
10448 if (section_nr >= dwp_file->num_sections)
10449 {
10450 error (_("Dwarf Error: bad DWP hash table, section number too large"
10451 " [in module %s]"),
10452 dwp_file->name);
10453 }
10454
10455 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10456 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10457 {
10458 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10459 " [in module %s]"),
10460 dwp_file->name);
10461 }
10462 }
10463
10464 if (i < 2
a32a8923
DE
10465 || dwarf2_section_empty_p (&sections.info_or_types)
10466 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10467 {
10468 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10469 " [in module %s]"),
10470 dwp_file->name);
10471 }
73869dc2 10472 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10473 {
10474 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10475 " [in module %s]"),
10476 dwp_file->name);
10477 }
10478
10479 /* It's easier for the rest of the code if we fake a struct dwo_file and
10480 have dwo_unit "live" in that. At least for now.
10481
10482 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10483 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10484 file, we can combine them back into a virtual DWO file to save space
10485 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10486 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10487
791afaa2
TT
10488 std::string virtual_dwo_name =
10489 string_printf ("virtual-dwo/%d-%d-%d-%d",
10490 get_section_id (&sections.abbrev),
10491 get_section_id (&sections.line),
10492 get_section_id (&sections.loc),
10493 get_section_id (&sections.str_offsets));
80626a55 10494 /* Can we use an existing virtual DWO file? */
791afaa2 10495 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
10496 /* Create one if necessary. */
10497 if (*dwo_file_slot == NULL)
10498 {
b4f54984 10499 if (dwarf_read_debug)
80626a55
DE
10500 {
10501 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 10502 virtual_dwo_name.c_str ());
80626a55
DE
10503 }
10504 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10505 dwo_file->dwo_name
10506 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
10507 virtual_dwo_name.c_str (),
10508 virtual_dwo_name.size ());
0ac5b59e 10509 dwo_file->comp_dir = comp_dir;
80626a55
DE
10510 dwo_file->sections.abbrev = sections.abbrev;
10511 dwo_file->sections.line = sections.line;
10512 dwo_file->sections.loc = sections.loc;
10513 dwo_file->sections.macinfo = sections.macinfo;
10514 dwo_file->sections.macro = sections.macro;
10515 dwo_file->sections.str_offsets = sections.str_offsets;
10516 /* The "str" section is global to the entire DWP file. */
10517 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10518 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10519 there's no need to record it in dwo_file.
10520 Also, we can't simply record type sections in dwo_file because
10521 we record a pointer into the vector in dwo_unit. As we collect more
10522 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10523 for it, invalidating all copies of pointers into the previous
10524 contents. */
80626a55
DE
10525 *dwo_file_slot = dwo_file;
10526 }
10527 else
10528 {
b4f54984 10529 if (dwarf_read_debug)
80626a55
DE
10530 {
10531 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 10532 virtual_dwo_name.c_str ());
80626a55 10533 }
9a3c8263 10534 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 10535 }
80626a55
DE
10536
10537 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10538 dwo_unit->dwo_file = dwo_file;
10539 dwo_unit->signature = signature;
8d749320
SM
10540 dwo_unit->section =
10541 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10542 *dwo_unit->section = sections.info_or_types;
57d63ce2 10543 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10544
10545 return dwo_unit;
10546}
10547
73869dc2
DE
10548/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10549 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10550 piece within that section used by a TU/CU, return a virtual section
10551 of just that piece. */
10552
10553static struct dwarf2_section_info
10554create_dwp_v2_section (struct dwarf2_section_info *section,
10555 bfd_size_type offset, bfd_size_type size)
10556{
10557 struct dwarf2_section_info result;
10558 asection *sectp;
10559
10560 gdb_assert (section != NULL);
10561 gdb_assert (!section->is_virtual);
10562
10563 memset (&result, 0, sizeof (result));
10564 result.s.containing_section = section;
10565 result.is_virtual = 1;
10566
10567 if (size == 0)
10568 return result;
10569
10570 sectp = get_section_bfd_section (section);
10571
10572 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10573 bounds of the real section. This is a pretty-rare event, so just
10574 flag an error (easier) instead of a warning and trying to cope. */
10575 if (sectp == NULL
10576 || offset + size > bfd_get_section_size (sectp))
10577 {
10578 bfd *abfd = sectp->owner;
10579
10580 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10581 " in section %s [in module %s]"),
10582 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10583 objfile_name (dwarf2_per_objfile->objfile));
10584 }
10585
10586 result.virtual_offset = offset;
10587 result.size = size;
10588 return result;
10589}
10590
10591/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10592 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10593 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10594 This is for DWP version 2 files. */
10595
10596static struct dwo_unit *
10597create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10598 uint32_t unit_index,
10599 const char *comp_dir,
10600 ULONGEST signature, int is_debug_types)
10601{
10602 struct objfile *objfile = dwarf2_per_objfile->objfile;
10603 const struct dwp_hash_table *dwp_htab =
10604 is_debug_types ? dwp_file->tus : dwp_file->cus;
10605 bfd *dbfd = dwp_file->dbfd;
10606 const char *kind = is_debug_types ? "TU" : "CU";
10607 struct dwo_file *dwo_file;
10608 struct dwo_unit *dwo_unit;
10609 struct virtual_v2_dwo_sections sections;
10610 void **dwo_file_slot;
73869dc2
DE
10611 int i;
10612
10613 gdb_assert (dwp_file->version == 2);
10614
b4f54984 10615 if (dwarf_read_debug)
73869dc2
DE
10616 {
10617 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10618 kind,
10619 pulongest (unit_index), hex_string (signature),
10620 dwp_file->name);
10621 }
10622
10623 /* Fetch the section offsets of this DWO unit. */
10624
10625 memset (&sections, 0, sizeof (sections));
73869dc2
DE
10626
10627 for (i = 0; i < dwp_htab->nr_columns; ++i)
10628 {
10629 uint32_t offset = read_4_bytes (dbfd,
10630 dwp_htab->section_pool.v2.offsets
10631 + (((unit_index - 1) * dwp_htab->nr_columns
10632 + i)
10633 * sizeof (uint32_t)));
10634 uint32_t size = read_4_bytes (dbfd,
10635 dwp_htab->section_pool.v2.sizes
10636 + (((unit_index - 1) * dwp_htab->nr_columns
10637 + i)
10638 * sizeof (uint32_t)));
10639
10640 switch (dwp_htab->section_pool.v2.section_ids[i])
10641 {
10642 case DW_SECT_INFO:
10643 case DW_SECT_TYPES:
10644 sections.info_or_types_offset = offset;
10645 sections.info_or_types_size = size;
10646 break;
10647 case DW_SECT_ABBREV:
10648 sections.abbrev_offset = offset;
10649 sections.abbrev_size = size;
10650 break;
10651 case DW_SECT_LINE:
10652 sections.line_offset = offset;
10653 sections.line_size = size;
10654 break;
10655 case DW_SECT_LOC:
10656 sections.loc_offset = offset;
10657 sections.loc_size = size;
10658 break;
10659 case DW_SECT_STR_OFFSETS:
10660 sections.str_offsets_offset = offset;
10661 sections.str_offsets_size = size;
10662 break;
10663 case DW_SECT_MACINFO:
10664 sections.macinfo_offset = offset;
10665 sections.macinfo_size = size;
10666 break;
10667 case DW_SECT_MACRO:
10668 sections.macro_offset = offset;
10669 sections.macro_size = size;
10670 break;
10671 }
10672 }
10673
10674 /* It's easier for the rest of the code if we fake a struct dwo_file and
10675 have dwo_unit "live" in that. At least for now.
10676
10677 The DWP file can be made up of a random collection of CUs and TUs.
10678 However, for each CU + set of TUs that came from the same original DWO
10679 file, we can combine them back into a virtual DWO file to save space
10680 (fewer struct dwo_file objects to allocate). Remember that for really
10681 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10682
791afaa2
TT
10683 std::string virtual_dwo_name =
10684 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10685 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10686 (long) (sections.line_size ? sections.line_offset : 0),
10687 (long) (sections.loc_size ? sections.loc_offset : 0),
10688 (long) (sections.str_offsets_size
10689 ? sections.str_offsets_offset : 0));
73869dc2 10690 /* Can we use an existing virtual DWO file? */
791afaa2 10691 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
10692 /* Create one if necessary. */
10693 if (*dwo_file_slot == NULL)
10694 {
b4f54984 10695 if (dwarf_read_debug)
73869dc2
DE
10696 {
10697 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 10698 virtual_dwo_name.c_str ());
73869dc2
DE
10699 }
10700 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10701 dwo_file->dwo_name
10702 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
10703 virtual_dwo_name.c_str (),
10704 virtual_dwo_name.size ());
73869dc2
DE
10705 dwo_file->comp_dir = comp_dir;
10706 dwo_file->sections.abbrev =
10707 create_dwp_v2_section (&dwp_file->sections.abbrev,
10708 sections.abbrev_offset, sections.abbrev_size);
10709 dwo_file->sections.line =
10710 create_dwp_v2_section (&dwp_file->sections.line,
10711 sections.line_offset, sections.line_size);
10712 dwo_file->sections.loc =
10713 create_dwp_v2_section (&dwp_file->sections.loc,
10714 sections.loc_offset, sections.loc_size);
10715 dwo_file->sections.macinfo =
10716 create_dwp_v2_section (&dwp_file->sections.macinfo,
10717 sections.macinfo_offset, sections.macinfo_size);
10718 dwo_file->sections.macro =
10719 create_dwp_v2_section (&dwp_file->sections.macro,
10720 sections.macro_offset, sections.macro_size);
10721 dwo_file->sections.str_offsets =
10722 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10723 sections.str_offsets_offset,
10724 sections.str_offsets_size);
10725 /* The "str" section is global to the entire DWP file. */
10726 dwo_file->sections.str = dwp_file->sections.str;
10727 /* The info or types section is assigned below to dwo_unit,
10728 there's no need to record it in dwo_file.
10729 Also, we can't simply record type sections in dwo_file because
10730 we record a pointer into the vector in dwo_unit. As we collect more
10731 types we'll grow the vector and eventually have to reallocate space
10732 for it, invalidating all copies of pointers into the previous
10733 contents. */
10734 *dwo_file_slot = dwo_file;
10735 }
10736 else
10737 {
b4f54984 10738 if (dwarf_read_debug)
73869dc2
DE
10739 {
10740 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 10741 virtual_dwo_name.c_str ());
73869dc2 10742 }
9a3c8263 10743 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 10744 }
73869dc2
DE
10745
10746 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10747 dwo_unit->dwo_file = dwo_file;
10748 dwo_unit->signature = signature;
8d749320
SM
10749 dwo_unit->section =
10750 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10751 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10752 ? &dwp_file->sections.types
10753 : &dwp_file->sections.info,
10754 sections.info_or_types_offset,
10755 sections.info_or_types_size);
10756 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10757
10758 return dwo_unit;
10759}
10760
57d63ce2
DE
10761/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10762 Returns NULL if the signature isn't found. */
80626a55
DE
10763
10764static struct dwo_unit *
57d63ce2
DE
10765lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10766 ULONGEST signature, int is_debug_types)
80626a55 10767{
57d63ce2
DE
10768 const struct dwp_hash_table *dwp_htab =
10769 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10770 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10771 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10772 uint32_t hash = signature & mask;
10773 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10774 unsigned int i;
10775 void **slot;
870f88f7 10776 struct dwo_unit find_dwo_cu;
80626a55
DE
10777
10778 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10779 find_dwo_cu.signature = signature;
19ac8c2e
DE
10780 slot = htab_find_slot (is_debug_types
10781 ? dwp_file->loaded_tus
10782 : dwp_file->loaded_cus,
10783 &find_dwo_cu, INSERT);
80626a55
DE
10784
10785 if (*slot != NULL)
9a3c8263 10786 return (struct dwo_unit *) *slot;
80626a55
DE
10787
10788 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10789 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10790 {
10791 ULONGEST signature_in_table;
10792
10793 signature_in_table =
57d63ce2 10794 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10795 if (signature_in_table == signature)
10796 {
57d63ce2
DE
10797 uint32_t unit_index =
10798 read_4_bytes (dbfd,
10799 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10800
73869dc2
DE
10801 if (dwp_file->version == 1)
10802 {
10803 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10804 comp_dir, signature,
10805 is_debug_types);
10806 }
10807 else
10808 {
10809 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10810 comp_dir, signature,
10811 is_debug_types);
10812 }
9a3c8263 10813 return (struct dwo_unit *) *slot;
80626a55
DE
10814 }
10815 if (signature_in_table == 0)
10816 return NULL;
10817 hash = (hash + hash2) & mask;
10818 }
10819
10820 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10821 " [in module %s]"),
10822 dwp_file->name);
10823}
10824
ab5088bf 10825/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10826 Open the file specified by FILE_NAME and hand it off to BFD for
10827 preliminary analysis. Return a newly initialized bfd *, which
10828 includes a canonicalized copy of FILE_NAME.
80626a55 10829 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10830 SEARCH_CWD is true if the current directory is to be searched.
10831 It will be searched before debug-file-directory.
13aaf454
DE
10832 If successful, the file is added to the bfd include table of the
10833 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10834 If unable to find/open the file, return NULL.
3019eac3
DE
10835 NOTE: This function is derived from symfile_bfd_open. */
10836
192b62ce 10837static gdb_bfd_ref_ptr
6ac97d4c 10838try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10839{
80626a55 10840 int desc, flags;
3019eac3 10841 char *absolute_name;
9c02c129
DE
10842 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10843 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10844 to debug_file_directory. */
10845 char *search_path;
10846 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10847
6ac97d4c
DE
10848 if (search_cwd)
10849 {
10850 if (*debug_file_directory != '\0')
10851 search_path = concat (".", dirname_separator_string,
b36cec19 10852 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10853 else
10854 search_path = xstrdup (".");
10855 }
9c02c129 10856 else
6ac97d4c 10857 search_path = xstrdup (debug_file_directory);
3019eac3 10858
492c0ab7 10859 flags = OPF_RETURN_REALPATH;
80626a55
DE
10860 if (is_dwp)
10861 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10862 desc = openp (search_path, flags, file_name,
3019eac3 10863 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10864 xfree (search_path);
3019eac3
DE
10865 if (desc < 0)
10866 return NULL;
10867
192b62ce 10868 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10869 xfree (absolute_name);
9c02c129
DE
10870 if (sym_bfd == NULL)
10871 return NULL;
192b62ce 10872 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10873
192b62ce
TT
10874 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10875 return NULL;
3019eac3 10876
13aaf454
DE
10877 /* Success. Record the bfd as having been included by the objfile's bfd.
10878 This is important because things like demangled_names_hash lives in the
10879 objfile's per_bfd space and may have references to things like symbol
10880 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10881 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10882
3019eac3
DE
10883 return sym_bfd;
10884}
10885
ab5088bf 10886/* Try to open DWO file FILE_NAME.
3019eac3
DE
10887 COMP_DIR is the DW_AT_comp_dir attribute.
10888 The result is the bfd handle of the file.
10889 If there is a problem finding or opening the file, return NULL.
10890 Upon success, the canonicalized path of the file is stored in the bfd,
10891 same as symfile_bfd_open. */
10892
192b62ce 10893static gdb_bfd_ref_ptr
ab5088bf 10894open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10895{
80626a55 10896 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10897 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10898
10899 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10900
10901 if (comp_dir != NULL)
10902 {
b36cec19
PA
10903 char *path_to_try = concat (comp_dir, SLASH_STRING,
10904 file_name, (char *) NULL);
3019eac3
DE
10905
10906 /* NOTE: If comp_dir is a relative path, this will also try the
10907 search path, which seems useful. */
192b62ce
TT
10908 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10909 1 /*search_cwd*/));
3019eac3
DE
10910 xfree (path_to_try);
10911 if (abfd != NULL)
10912 return abfd;
10913 }
10914
10915 /* That didn't work, try debug-file-directory, which, despite its name,
10916 is a list of paths. */
10917
10918 if (*debug_file_directory == '\0')
10919 return NULL;
10920
6ac97d4c 10921 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10922}
10923
80626a55
DE
10924/* This function is mapped across the sections and remembers the offset and
10925 size of each of the DWO debugging sections we are interested in. */
10926
10927static void
10928dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10929{
9a3c8263 10930 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10931 const struct dwop_section_names *names = &dwop_section_names;
10932
10933 if (section_is_p (sectp->name, &names->abbrev_dwo))
10934 {
049412e3 10935 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10936 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10937 }
10938 else if (section_is_p (sectp->name, &names->info_dwo))
10939 {
049412e3 10940 dwo_sections->info.s.section = sectp;
80626a55
DE
10941 dwo_sections->info.size = bfd_get_section_size (sectp);
10942 }
10943 else if (section_is_p (sectp->name, &names->line_dwo))
10944 {
049412e3 10945 dwo_sections->line.s.section = sectp;
80626a55
DE
10946 dwo_sections->line.size = bfd_get_section_size (sectp);
10947 }
10948 else if (section_is_p (sectp->name, &names->loc_dwo))
10949 {
049412e3 10950 dwo_sections->loc.s.section = sectp;
80626a55
DE
10951 dwo_sections->loc.size = bfd_get_section_size (sectp);
10952 }
10953 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10954 {
049412e3 10955 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10956 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10957 }
10958 else if (section_is_p (sectp->name, &names->macro_dwo))
10959 {
049412e3 10960 dwo_sections->macro.s.section = sectp;
80626a55
DE
10961 dwo_sections->macro.size = bfd_get_section_size (sectp);
10962 }
10963 else if (section_is_p (sectp->name, &names->str_dwo))
10964 {
049412e3 10965 dwo_sections->str.s.section = sectp;
80626a55
DE
10966 dwo_sections->str.size = bfd_get_section_size (sectp);
10967 }
10968 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10969 {
049412e3 10970 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10971 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10972 }
10973 else if (section_is_p (sectp->name, &names->types_dwo))
10974 {
10975 struct dwarf2_section_info type_section;
10976
10977 memset (&type_section, 0, sizeof (type_section));
049412e3 10978 type_section.s.section = sectp;
80626a55
DE
10979 type_section.size = bfd_get_section_size (sectp);
10980 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10981 &type_section);
10982 }
10983}
10984
ab5088bf 10985/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10986 by PER_CU. This is for the non-DWP case.
80626a55 10987 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10988
10989static struct dwo_file *
0ac5b59e
DE
10990open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10991 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10992{
10993 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10994 struct dwo_file *dwo_file;
3019eac3
DE
10995 struct cleanup *cleanups;
10996
192b62ce 10997 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10998 if (dbfd == NULL)
10999 {
b4f54984 11000 if (dwarf_read_debug)
80626a55
DE
11001 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11002 return NULL;
11003 }
11004 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11005 dwo_file->dwo_name = dwo_name;
11006 dwo_file->comp_dir = comp_dir;
192b62ce 11007 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11008
11009 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11010
192b62ce
TT
11011 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11012 &dwo_file->sections);
3019eac3 11013
33c5cd75 11014 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11015
78d4d2c5
JK
11016 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11017 dwo_file->tus);
3019eac3
DE
11018
11019 discard_cleanups (cleanups);
11020
b4f54984 11021 if (dwarf_read_debug)
80626a55
DE
11022 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11023
3019eac3
DE
11024 return dwo_file;
11025}
11026
80626a55 11027/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11028 size of each of the DWP debugging sections common to version 1 and 2 that
11029 we are interested in. */
3019eac3 11030
80626a55 11031static void
73869dc2
DE
11032dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11033 void *dwp_file_ptr)
3019eac3 11034{
9a3c8263 11035 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11036 const struct dwop_section_names *names = &dwop_section_names;
11037 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11038
80626a55 11039 /* Record the ELF section number for later lookup: this is what the
73869dc2 11040 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11041 gdb_assert (elf_section_nr < dwp_file->num_sections);
11042 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11043
80626a55
DE
11044 /* Look for specific sections that we need. */
11045 if (section_is_p (sectp->name, &names->str_dwo))
11046 {
049412e3 11047 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11048 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11049 }
11050 else if (section_is_p (sectp->name, &names->cu_index))
11051 {
049412e3 11052 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11053 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11054 }
11055 else if (section_is_p (sectp->name, &names->tu_index))
11056 {
049412e3 11057 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11058 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11059 }
11060}
3019eac3 11061
73869dc2
DE
11062/* This function is mapped across the sections and remembers the offset and
11063 size of each of the DWP version 2 debugging sections that we are interested
11064 in. This is split into a separate function because we don't know if we
11065 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11066
11067static void
11068dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11069{
9a3c8263 11070 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11071 const struct dwop_section_names *names = &dwop_section_names;
11072 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11073
11074 /* Record the ELF section number for later lookup: this is what the
11075 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11076 gdb_assert (elf_section_nr < dwp_file->num_sections);
11077 dwp_file->elf_sections[elf_section_nr] = sectp;
11078
11079 /* Look for specific sections that we need. */
11080 if (section_is_p (sectp->name, &names->abbrev_dwo))
11081 {
049412e3 11082 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11083 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11084 }
11085 else if (section_is_p (sectp->name, &names->info_dwo))
11086 {
049412e3 11087 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11088 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11089 }
11090 else if (section_is_p (sectp->name, &names->line_dwo))
11091 {
049412e3 11092 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11093 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11094 }
11095 else if (section_is_p (sectp->name, &names->loc_dwo))
11096 {
049412e3 11097 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11098 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11099 }
11100 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11101 {
049412e3 11102 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11103 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11104 }
11105 else if (section_is_p (sectp->name, &names->macro_dwo))
11106 {
049412e3 11107 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11108 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11109 }
11110 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11111 {
049412e3 11112 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11113 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11114 }
11115 else if (section_is_p (sectp->name, &names->types_dwo))
11116 {
049412e3 11117 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11118 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11119 }
11120}
11121
80626a55 11122/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11123
80626a55
DE
11124static hashval_t
11125hash_dwp_loaded_cutus (const void *item)
11126{
9a3c8263 11127 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11128
80626a55
DE
11129 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11130 return dwo_unit->signature;
3019eac3
DE
11131}
11132
80626a55 11133/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11134
80626a55
DE
11135static int
11136eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11137{
9a3c8263
SM
11138 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11139 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11140
80626a55
DE
11141 return dua->signature == dub->signature;
11142}
3019eac3 11143
80626a55 11144/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11145
80626a55
DE
11146static htab_t
11147allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11148{
11149 return htab_create_alloc_ex (3,
11150 hash_dwp_loaded_cutus,
11151 eq_dwp_loaded_cutus,
11152 NULL,
11153 &objfile->objfile_obstack,
11154 hashtab_obstack_allocate,
11155 dummy_obstack_deallocate);
11156}
3019eac3 11157
ab5088bf
DE
11158/* Try to open DWP file FILE_NAME.
11159 The result is the bfd handle of the file.
11160 If there is a problem finding or opening the file, return NULL.
11161 Upon success, the canonicalized path of the file is stored in the bfd,
11162 same as symfile_bfd_open. */
11163
192b62ce 11164static gdb_bfd_ref_ptr
ab5088bf
DE
11165open_dwp_file (const char *file_name)
11166{
192b62ce
TT
11167 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11168 1 /*search_cwd*/));
6ac97d4c
DE
11169 if (abfd != NULL)
11170 return abfd;
11171
11172 /* Work around upstream bug 15652.
11173 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11174 [Whether that's a "bug" is debatable, but it is getting in our way.]
11175 We have no real idea where the dwp file is, because gdb's realpath-ing
11176 of the executable's path may have discarded the needed info.
11177 [IWBN if the dwp file name was recorded in the executable, akin to
11178 .gnu_debuglink, but that doesn't exist yet.]
11179 Strip the directory from FILE_NAME and search again. */
11180 if (*debug_file_directory != '\0')
11181 {
11182 /* Don't implicitly search the current directory here.
11183 If the user wants to search "." to handle this case,
11184 it must be added to debug-file-directory. */
11185 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11186 0 /*search_cwd*/);
11187 }
11188
11189 return NULL;
ab5088bf
DE
11190}
11191
80626a55
DE
11192/* Initialize the use of the DWP file for the current objfile.
11193 By convention the name of the DWP file is ${objfile}.dwp.
11194 The result is NULL if it can't be found. */
a766d390 11195
80626a55 11196static struct dwp_file *
ab5088bf 11197open_and_init_dwp_file (void)
80626a55
DE
11198{
11199 struct objfile *objfile = dwarf2_per_objfile->objfile;
11200 struct dwp_file *dwp_file;
80626a55 11201
82bf32bc
JK
11202 /* Try to find first .dwp for the binary file before any symbolic links
11203 resolving. */
6c447423
DE
11204
11205 /* If the objfile is a debug file, find the name of the real binary
11206 file and get the name of dwp file from there. */
d721ba37 11207 std::string dwp_name;
6c447423
DE
11208 if (objfile->separate_debug_objfile_backlink != NULL)
11209 {
11210 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11211 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11212
d721ba37 11213 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11214 }
11215 else
d721ba37
PA
11216 dwp_name = objfile->original_name;
11217
11218 dwp_name += ".dwp";
80626a55 11219
d721ba37 11220 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11221 if (dbfd == NULL
11222 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11223 {
11224 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11225 dwp_name = objfile_name (objfile);
11226 dwp_name += ".dwp";
11227 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11228 }
11229
80626a55
DE
11230 if (dbfd == NULL)
11231 {
b4f54984 11232 if (dwarf_read_debug)
d721ba37 11233 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11234 return NULL;
3019eac3 11235 }
80626a55 11236 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11237 dwp_file->name = bfd_get_filename (dbfd.get ());
11238 dwp_file->dbfd = dbfd.release ();
c906108c 11239
80626a55 11240 /* +1: section 0 is unused */
192b62ce 11241 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11242 dwp_file->elf_sections =
11243 OBSTACK_CALLOC (&objfile->objfile_obstack,
11244 dwp_file->num_sections, asection *);
11245
192b62ce
TT
11246 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11247 dwp_file);
80626a55
DE
11248
11249 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11250
11251 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11252
73869dc2 11253 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11254 if (dwp_file->cus && dwp_file->tus
11255 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11256 {
11257 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11258 pretty bizarre. We use pulongest here because that's the established
4d65956b 11259 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11260 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11261 " TU version %s [in DWP file %s]"),
11262 pulongest (dwp_file->cus->version),
d721ba37 11263 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11264 }
08302ed2
DE
11265
11266 if (dwp_file->cus)
11267 dwp_file->version = dwp_file->cus->version;
11268 else if (dwp_file->tus)
11269 dwp_file->version = dwp_file->tus->version;
11270 else
11271 dwp_file->version = 2;
73869dc2
DE
11272
11273 if (dwp_file->version == 2)
192b62ce
TT
11274 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11275 dwp_file);
73869dc2 11276
19ac8c2e
DE
11277 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11278 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11279
b4f54984 11280 if (dwarf_read_debug)
80626a55
DE
11281 {
11282 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11283 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11284 " %s CUs, %s TUs\n",
11285 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11286 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11287 }
11288
11289 return dwp_file;
3019eac3 11290}
c906108c 11291
ab5088bf
DE
11292/* Wrapper around open_and_init_dwp_file, only open it once. */
11293
11294static struct dwp_file *
11295get_dwp_file (void)
11296{
11297 if (! dwarf2_per_objfile->dwp_checked)
11298 {
11299 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11300 dwarf2_per_objfile->dwp_checked = 1;
11301 }
11302 return dwarf2_per_objfile->dwp_file;
11303}
11304
80626a55
DE
11305/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11306 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11307 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11308 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11309 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11310
11311 This is called, for example, when wanting to read a variable with a
11312 complex location. Therefore we don't want to do file i/o for every call.
11313 Therefore we don't want to look for a DWO file on every call.
11314 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11315 then we check if we've already seen DWO_NAME, and only THEN do we check
11316 for a DWO file.
11317
1c658ad5 11318 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11319 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11320
3019eac3 11321static struct dwo_unit *
80626a55
DE
11322lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11323 const char *dwo_name, const char *comp_dir,
11324 ULONGEST signature, int is_debug_types)
3019eac3
DE
11325{
11326 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11327 const char *kind = is_debug_types ? "TU" : "CU";
11328 void **dwo_file_slot;
3019eac3 11329 struct dwo_file *dwo_file;
80626a55 11330 struct dwp_file *dwp_file;
cb1df416 11331
6a506a2d
DE
11332 /* First see if there's a DWP file.
11333 If we have a DWP file but didn't find the DWO inside it, don't
11334 look for the original DWO file. It makes gdb behave differently
11335 depending on whether one is debugging in the build tree. */
cf2c3c16 11336
ab5088bf 11337 dwp_file = get_dwp_file ();
80626a55 11338 if (dwp_file != NULL)
cf2c3c16 11339 {
80626a55
DE
11340 const struct dwp_hash_table *dwp_htab =
11341 is_debug_types ? dwp_file->tus : dwp_file->cus;
11342
11343 if (dwp_htab != NULL)
11344 {
11345 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11346 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11347 signature, is_debug_types);
80626a55
DE
11348
11349 if (dwo_cutu != NULL)
11350 {
b4f54984 11351 if (dwarf_read_debug)
80626a55
DE
11352 {
11353 fprintf_unfiltered (gdb_stdlog,
11354 "Virtual DWO %s %s found: @%s\n",
11355 kind, hex_string (signature),
11356 host_address_to_string (dwo_cutu));
11357 }
11358 return dwo_cutu;
11359 }
11360 }
11361 }
6a506a2d 11362 else
80626a55 11363 {
6a506a2d 11364 /* No DWP file, look for the DWO file. */
80626a55 11365
6a506a2d
DE
11366 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11367 if (*dwo_file_slot == NULL)
80626a55 11368 {
6a506a2d
DE
11369 /* Read in the file and build a table of the CUs/TUs it contains. */
11370 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11371 }
6a506a2d 11372 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11373 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11374
6a506a2d 11375 if (dwo_file != NULL)
19c3d4c9 11376 {
6a506a2d
DE
11377 struct dwo_unit *dwo_cutu = NULL;
11378
11379 if (is_debug_types && dwo_file->tus)
11380 {
11381 struct dwo_unit find_dwo_cutu;
11382
11383 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11384 find_dwo_cutu.signature = signature;
9a3c8263
SM
11385 dwo_cutu
11386 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11387 }
33c5cd75 11388 else if (!is_debug_types && dwo_file->cus)
80626a55 11389 {
33c5cd75
DB
11390 struct dwo_unit find_dwo_cutu;
11391
11392 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11393 find_dwo_cutu.signature = signature;
11394 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11395 &find_dwo_cutu);
6a506a2d
DE
11396 }
11397
11398 if (dwo_cutu != NULL)
11399 {
b4f54984 11400 if (dwarf_read_debug)
6a506a2d
DE
11401 {
11402 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11403 kind, dwo_name, hex_string (signature),
11404 host_address_to_string (dwo_cutu));
11405 }
11406 return dwo_cutu;
80626a55
DE
11407 }
11408 }
2e276125 11409 }
9cdd5dbd 11410
80626a55
DE
11411 /* We didn't find it. This could mean a dwo_id mismatch, or
11412 someone deleted the DWO/DWP file, or the search path isn't set up
11413 correctly to find the file. */
11414
b4f54984 11415 if (dwarf_read_debug)
80626a55
DE
11416 {
11417 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11418 kind, dwo_name, hex_string (signature));
11419 }
3019eac3 11420
6656a72d
DE
11421 /* This is a warning and not a complaint because it can be caused by
11422 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11423 {
11424 /* Print the name of the DWP file if we looked there, helps the user
11425 better diagnose the problem. */
791afaa2 11426 std::string dwp_text;
43942612
DE
11427
11428 if (dwp_file != NULL)
791afaa2
TT
11429 dwp_text = string_printf (" [in DWP file %s]",
11430 lbasename (dwp_file->name));
43942612
DE
11431
11432 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11433 " [in module %s]"),
11434 kind, dwo_name, hex_string (signature),
791afaa2 11435 dwp_text.c_str (),
43942612 11436 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11437 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 11438 }
3019eac3 11439 return NULL;
5fb290d7
DJ
11440}
11441
80626a55
DE
11442/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11443 See lookup_dwo_cutu_unit for details. */
11444
11445static struct dwo_unit *
11446lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11447 const char *dwo_name, const char *comp_dir,
11448 ULONGEST signature)
11449{
11450 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11451}
11452
11453/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11454 See lookup_dwo_cutu_unit for details. */
11455
11456static struct dwo_unit *
11457lookup_dwo_type_unit (struct signatured_type *this_tu,
11458 const char *dwo_name, const char *comp_dir)
11459{
11460 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11461}
11462
89e63ee4
DE
11463/* Traversal function for queue_and_load_all_dwo_tus. */
11464
11465static int
11466queue_and_load_dwo_tu (void **slot, void *info)
11467{
11468 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11469 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11470 ULONGEST signature = dwo_unit->signature;
11471 struct signatured_type *sig_type =
11472 lookup_dwo_signatured_type (per_cu->cu, signature);
11473
11474 if (sig_type != NULL)
11475 {
11476 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11477
11478 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11479 a real dependency of PER_CU on SIG_TYPE. That is detected later
11480 while processing PER_CU. */
11481 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11482 load_full_type_unit (sig_cu);
11483 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11484 }
11485
11486 return 1;
11487}
11488
11489/* Queue all TUs contained in the DWO of PER_CU to be read in.
11490 The DWO may have the only definition of the type, though it may not be
11491 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11492 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11493
11494static void
11495queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11496{
11497 struct dwo_unit *dwo_unit;
11498 struct dwo_file *dwo_file;
11499
11500 gdb_assert (!per_cu->is_debug_types);
11501 gdb_assert (get_dwp_file () == NULL);
11502 gdb_assert (per_cu->cu != NULL);
11503
11504 dwo_unit = per_cu->cu->dwo_unit;
11505 gdb_assert (dwo_unit != NULL);
11506
11507 dwo_file = dwo_unit->dwo_file;
11508 if (dwo_file->tus != NULL)
11509 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11510}
11511
3019eac3
DE
11512/* Free all resources associated with DWO_FILE.
11513 Close the DWO file and munmap the sections.
11514 All memory should be on the objfile obstack. */
348e048f
DE
11515
11516static void
3019eac3 11517free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11518{
348e048f 11519
5c6fa7ab 11520 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11521 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11522
3019eac3
DE
11523 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11524}
348e048f 11525
3019eac3 11526/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11527
3019eac3
DE
11528static void
11529free_dwo_file_cleanup (void *arg)
11530{
11531 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11532 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11533
3019eac3
DE
11534 free_dwo_file (dwo_file, objfile);
11535}
348e048f 11536
3019eac3 11537/* Traversal function for free_dwo_files. */
2ab95328 11538
3019eac3
DE
11539static int
11540free_dwo_file_from_slot (void **slot, void *info)
11541{
11542 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11543 struct objfile *objfile = (struct objfile *) info;
348e048f 11544
3019eac3 11545 free_dwo_file (dwo_file, objfile);
348e048f 11546
3019eac3
DE
11547 return 1;
11548}
348e048f 11549
3019eac3 11550/* Free all resources associated with DWO_FILES. */
348e048f 11551
3019eac3
DE
11552static void
11553free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11554{
11555 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11556}
3019eac3
DE
11557\f
11558/* Read in various DIEs. */
348e048f 11559
d389af10 11560/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11561 Inherit only the children of the DW_AT_abstract_origin DIE not being
11562 already referenced by DW_AT_abstract_origin from the children of the
11563 current DIE. */
d389af10
JK
11564
11565static void
11566inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11567{
11568 struct die_info *child_die;
791afaa2 11569 sect_offset *offsetp;
d389af10
JK
11570 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11571 struct die_info *origin_die;
11572 /* Iterator of the ORIGIN_DIE children. */
11573 struct die_info *origin_child_die;
d389af10 11574 struct attribute *attr;
cd02d79d
PA
11575 struct dwarf2_cu *origin_cu;
11576 struct pending **origin_previous_list_in_scope;
d389af10
JK
11577
11578 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11579 if (!attr)
11580 return;
11581
cd02d79d
PA
11582 /* Note that following die references may follow to a die in a
11583 different cu. */
11584
11585 origin_cu = cu;
11586 origin_die = follow_die_ref (die, attr, &origin_cu);
11587
11588 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11589 symbols in. */
11590 origin_previous_list_in_scope = origin_cu->list_in_scope;
11591 origin_cu->list_in_scope = cu->list_in_scope;
11592
edb3359d
DJ
11593 if (die->tag != origin_die->tag
11594 && !(die->tag == DW_TAG_inlined_subroutine
11595 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11596 complaint (&symfile_complaints,
11597 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11598 to_underlying (die->sect_off),
11599 to_underlying (origin_die->sect_off));
d389af10 11600
791afaa2 11601 std::vector<sect_offset> offsets;
d389af10 11602
3ea89b92
PMR
11603 for (child_die = die->child;
11604 child_die && child_die->tag;
11605 child_die = sibling_die (child_die))
11606 {
11607 struct die_info *child_origin_die;
11608 struct dwarf2_cu *child_origin_cu;
11609
11610 /* We are trying to process concrete instance entries:
216f72a1 11611 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11612 it's not relevant to our analysis here. i.e. detecting DIEs that are
11613 present in the abstract instance but not referenced in the concrete
11614 one. */
216f72a1
JK
11615 if (child_die->tag == DW_TAG_call_site
11616 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11617 continue;
11618
c38f313d
DJ
11619 /* For each CHILD_DIE, find the corresponding child of
11620 ORIGIN_DIE. If there is more than one layer of
11621 DW_AT_abstract_origin, follow them all; there shouldn't be,
11622 but GCC versions at least through 4.4 generate this (GCC PR
11623 40573). */
3ea89b92
PMR
11624 child_origin_die = child_die;
11625 child_origin_cu = cu;
c38f313d
DJ
11626 while (1)
11627 {
cd02d79d
PA
11628 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11629 child_origin_cu);
c38f313d
DJ
11630 if (attr == NULL)
11631 break;
cd02d79d
PA
11632 child_origin_die = follow_die_ref (child_origin_die, attr,
11633 &child_origin_cu);
c38f313d
DJ
11634 }
11635
d389af10
JK
11636 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11637 counterpart may exist. */
c38f313d 11638 if (child_origin_die != child_die)
d389af10 11639 {
edb3359d
DJ
11640 if (child_die->tag != child_origin_die->tag
11641 && !(child_die->tag == DW_TAG_inlined_subroutine
11642 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11643 complaint (&symfile_complaints,
11644 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11645 "different tags"),
11646 to_underlying (child_die->sect_off),
11647 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11648 if (child_origin_die->parent != origin_die)
11649 complaint (&symfile_complaints,
11650 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11651 "different parents"),
11652 to_underlying (child_die->sect_off),
11653 to_underlying (child_origin_die->sect_off));
c38f313d 11654 else
791afaa2 11655 offsets.push_back (child_origin_die->sect_off);
d389af10 11656 }
d389af10 11657 }
791afaa2
TT
11658 std::sort (offsets.begin (), offsets.end ());
11659 sect_offset *offsets_end = offsets.data () + offsets.size ();
11660 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 11661 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11662 complaint (&symfile_complaints,
11663 _("Multiple children of DIE 0x%x refer "
11664 "to DIE 0x%x as their abstract origin"),
9c541725 11665 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 11666
791afaa2 11667 offsetp = offsets.data ();
d389af10
JK
11668 origin_child_die = origin_die->child;
11669 while (origin_child_die && origin_child_die->tag)
11670 {
11671 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11672 while (offsetp < offsets_end
9c541725 11673 && *offsetp < origin_child_die->sect_off)
d389af10 11674 offsetp++;
b64f50a1 11675 if (offsetp >= offsets_end
9c541725 11676 || *offsetp > origin_child_die->sect_off)
d389af10 11677 {
adde2bff
DE
11678 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11679 Check whether we're already processing ORIGIN_CHILD_DIE.
11680 This can happen with mutually referenced abstract_origins.
11681 PR 16581. */
11682 if (!origin_child_die->in_process)
11683 process_die (origin_child_die, origin_cu);
d389af10
JK
11684 }
11685 origin_child_die = sibling_die (origin_child_die);
11686 }
cd02d79d 11687 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11688}
11689
c906108c 11690static void
e7c27a73 11691read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11692{
e7c27a73 11693 struct objfile *objfile = cu->objfile;
3e29f34a 11694 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11695 struct context_stack *newobj;
c906108c
SS
11696 CORE_ADDR lowpc;
11697 CORE_ADDR highpc;
11698 struct die_info *child_die;
edb3359d 11699 struct attribute *attr, *call_line, *call_file;
15d034d0 11700 const char *name;
e142c38c 11701 CORE_ADDR baseaddr;
801e3a5b 11702 struct block *block;
edb3359d 11703 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11704 VEC (symbolp) *template_args = NULL;
11705 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11706
11707 if (inlined_func)
11708 {
11709 /* If we do not have call site information, we can't show the
11710 caller of this inlined function. That's too confusing, so
11711 only use the scope for local variables. */
11712 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11713 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11714 if (call_line == NULL || call_file == NULL)
11715 {
11716 read_lexical_block_scope (die, cu);
11717 return;
11718 }
11719 }
c906108c 11720
e142c38c
DJ
11721 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11722
94af9270 11723 name = dwarf2_name (die, cu);
c906108c 11724
e8d05480
JB
11725 /* Ignore functions with missing or empty names. These are actually
11726 illegal according to the DWARF standard. */
11727 if (name == NULL)
11728 {
11729 complaint (&symfile_complaints,
b64f50a1 11730 _("missing name for subprogram DIE at %d"),
9c541725 11731 to_underlying (die->sect_off));
e8d05480
JB
11732 return;
11733 }
11734
11735 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11736 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11737 <= PC_BOUNDS_INVALID)
e8d05480 11738 {
ae4d0c03
PM
11739 attr = dwarf2_attr (die, DW_AT_external, cu);
11740 if (!attr || !DW_UNSND (attr))
11741 complaint (&symfile_complaints,
3e43a32a
MS
11742 _("cannot get low and high bounds "
11743 "for subprogram DIE at %d"),
9c541725 11744 to_underlying (die->sect_off));
e8d05480
JB
11745 return;
11746 }
c906108c 11747
3e29f34a
MR
11748 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11749 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11750
34eaf542
TT
11751 /* If we have any template arguments, then we must allocate a
11752 different sort of symbol. */
11753 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11754 {
11755 if (child_die->tag == DW_TAG_template_type_param
11756 || child_die->tag == DW_TAG_template_value_param)
11757 {
e623cf5d 11758 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11759 templ_func->base.is_cplus_template_function = 1;
11760 break;
11761 }
11762 }
11763
fe978cb0
PA
11764 newobj = push_context (0, lowpc);
11765 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11766 (struct symbol *) templ_func);
4c2df51b 11767
4cecd739
DJ
11768 /* If there is a location expression for DW_AT_frame_base, record
11769 it. */
e142c38c 11770 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11771 if (attr)
fe978cb0 11772 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11773
63e43d3a
PMR
11774 /* If there is a location for the static link, record it. */
11775 newobj->static_link = NULL;
11776 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11777 if (attr)
11778 {
224c3ddb
SM
11779 newobj->static_link
11780 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11781 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11782 }
11783
e142c38c 11784 cu->list_in_scope = &local_symbols;
c906108c 11785
639d11d3 11786 if (die->child != NULL)
c906108c 11787 {
639d11d3 11788 child_die = die->child;
c906108c
SS
11789 while (child_die && child_die->tag)
11790 {
34eaf542
TT
11791 if (child_die->tag == DW_TAG_template_type_param
11792 || child_die->tag == DW_TAG_template_value_param)
11793 {
11794 struct symbol *arg = new_symbol (child_die, NULL, cu);
11795
f1078f66
DJ
11796 if (arg != NULL)
11797 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11798 }
11799 else
11800 process_die (child_die, cu);
c906108c
SS
11801 child_die = sibling_die (child_die);
11802 }
11803 }
11804
d389af10
JK
11805 inherit_abstract_dies (die, cu);
11806
4a811a97
UW
11807 /* If we have a DW_AT_specification, we might need to import using
11808 directives from the context of the specification DIE. See the
11809 comment in determine_prefix. */
11810 if (cu->language == language_cplus
11811 && dwarf2_attr (die, DW_AT_specification, cu))
11812 {
11813 struct dwarf2_cu *spec_cu = cu;
11814 struct die_info *spec_die = die_specification (die, &spec_cu);
11815
11816 while (spec_die)
11817 {
11818 child_die = spec_die->child;
11819 while (child_die && child_die->tag)
11820 {
11821 if (child_die->tag == DW_TAG_imported_module)
11822 process_die (child_die, spec_cu);
11823 child_die = sibling_die (child_die);
11824 }
11825
11826 /* In some cases, GCC generates specification DIEs that
11827 themselves contain DW_AT_specification attributes. */
11828 spec_die = die_specification (spec_die, &spec_cu);
11829 }
11830 }
11831
fe978cb0 11832 newobj = pop_context ();
c906108c 11833 /* Make a block for the local symbols within. */
fe978cb0 11834 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11835 newobj->static_link, lowpc, highpc);
801e3a5b 11836
df8a16a1 11837 /* For C++, set the block's scope. */
45280282
IB
11838 if ((cu->language == language_cplus
11839 || cu->language == language_fortran
c44af4eb
TT
11840 || cu->language == language_d
11841 || cu->language == language_rust)
4d4ec4e5 11842 && cu->processing_has_namespace_info)
195a3f6c
TT
11843 block_set_scope (block, determine_prefix (die, cu),
11844 &objfile->objfile_obstack);
df8a16a1 11845
801e3a5b
JB
11846 /* If we have address ranges, record them. */
11847 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11848
fe978cb0 11849 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11850
34eaf542
TT
11851 /* Attach template arguments to function. */
11852 if (! VEC_empty (symbolp, template_args))
11853 {
11854 gdb_assert (templ_func != NULL);
11855
11856 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11857 templ_func->template_arguments
8d749320
SM
11858 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11859 templ_func->n_template_arguments);
34eaf542
TT
11860 memcpy (templ_func->template_arguments,
11861 VEC_address (symbolp, template_args),
11862 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11863 VEC_free (symbolp, template_args);
11864 }
11865
208d8187
JB
11866 /* In C++, we can have functions nested inside functions (e.g., when
11867 a function declares a class that has methods). This means that
11868 when we finish processing a function scope, we may need to go
11869 back to building a containing block's symbol lists. */
fe978cb0 11870 local_symbols = newobj->locals;
22cee43f 11871 local_using_directives = newobj->local_using_directives;
208d8187 11872
921e78cf
JB
11873 /* If we've finished processing a top-level function, subsequent
11874 symbols go in the file symbol list. */
11875 if (outermost_context_p ())
e142c38c 11876 cu->list_in_scope = &file_symbols;
c906108c
SS
11877}
11878
11879/* Process all the DIES contained within a lexical block scope. Start
11880 a new scope, process the dies, and then close the scope. */
11881
11882static void
e7c27a73 11883read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11884{
e7c27a73 11885 struct objfile *objfile = cu->objfile;
3e29f34a 11886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11887 struct context_stack *newobj;
c906108c
SS
11888 CORE_ADDR lowpc, highpc;
11889 struct die_info *child_die;
e142c38c
DJ
11890 CORE_ADDR baseaddr;
11891
11892 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11893
11894 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11895 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11896 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11897 be nasty. Might be easier to properly extend generic blocks to
af34e669 11898 describe ranges. */
e385593e
JK
11899 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11900 {
11901 case PC_BOUNDS_NOT_PRESENT:
11902 /* DW_TAG_lexical_block has no attributes, process its children as if
11903 there was no wrapping by that DW_TAG_lexical_block.
11904 GCC does no longer produces such DWARF since GCC r224161. */
11905 for (child_die = die->child;
11906 child_die != NULL && child_die->tag;
11907 child_die = sibling_die (child_die))
11908 process_die (child_die, cu);
11909 return;
11910 case PC_BOUNDS_INVALID:
11911 return;
11912 }
3e29f34a
MR
11913 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11914 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11915
11916 push_context (0, lowpc);
639d11d3 11917 if (die->child != NULL)
c906108c 11918 {
639d11d3 11919 child_die = die->child;
c906108c
SS
11920 while (child_die && child_die->tag)
11921 {
e7c27a73 11922 process_die (child_die, cu);
c906108c
SS
11923 child_die = sibling_die (child_die);
11924 }
11925 }
3ea89b92 11926 inherit_abstract_dies (die, cu);
fe978cb0 11927 newobj = pop_context ();
c906108c 11928
22cee43f 11929 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11930 {
801e3a5b 11931 struct block *block
63e43d3a 11932 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11933 newobj->start_addr, highpc);
801e3a5b
JB
11934
11935 /* Note that recording ranges after traversing children, as we
11936 do here, means that recording a parent's ranges entails
11937 walking across all its children's ranges as they appear in
11938 the address map, which is quadratic behavior.
11939
11940 It would be nicer to record the parent's ranges before
11941 traversing its children, simply overriding whatever you find
11942 there. But since we don't even decide whether to create a
11943 block until after we've traversed its children, that's hard
11944 to do. */
11945 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11946 }
fe978cb0 11947 local_symbols = newobj->locals;
22cee43f 11948 local_using_directives = newobj->local_using_directives;
c906108c
SS
11949}
11950
216f72a1 11951/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11952
11953static void
11954read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11955{
11956 struct objfile *objfile = cu->objfile;
11957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11958 CORE_ADDR pc, baseaddr;
11959 struct attribute *attr;
11960 struct call_site *call_site, call_site_local;
11961 void **slot;
11962 int nparams;
11963 struct die_info *child_die;
11964
11965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11966
216f72a1
JK
11967 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11968 if (attr == NULL)
11969 {
11970 /* This was a pre-DWARF-5 GNU extension alias
11971 for DW_AT_call_return_pc. */
11972 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11973 }
96408a79
SA
11974 if (!attr)
11975 {
11976 complaint (&symfile_complaints,
216f72a1 11977 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11978 "DIE 0x%x [in module %s]"),
9c541725 11979 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11980 return;
11981 }
31aa7e4e 11982 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11983 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11984
11985 if (cu->call_site_htab == NULL)
11986 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11987 NULL, &objfile->objfile_obstack,
11988 hashtab_obstack_allocate, NULL);
11989 call_site_local.pc = pc;
11990 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11991 if (*slot != NULL)
11992 {
11993 complaint (&symfile_complaints,
216f72a1 11994 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11995 "DIE 0x%x [in module %s]"),
9c541725 11996 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11997 objfile_name (objfile));
96408a79
SA
11998 return;
11999 }
12000
12001 /* Count parameters at the caller. */
12002
12003 nparams = 0;
12004 for (child_die = die->child; child_die && child_die->tag;
12005 child_die = sibling_die (child_die))
12006 {
216f72a1
JK
12007 if (child_die->tag != DW_TAG_call_site_parameter
12008 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12009 {
12010 complaint (&symfile_complaints,
216f72a1
JK
12011 _("Tag %d is not DW_TAG_call_site_parameter in "
12012 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12013 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12014 objfile_name (objfile));
96408a79
SA
12015 continue;
12016 }
12017
12018 nparams++;
12019 }
12020
224c3ddb
SM
12021 call_site
12022 = ((struct call_site *)
12023 obstack_alloc (&objfile->objfile_obstack,
12024 sizeof (*call_site)
12025 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12026 *slot = call_site;
12027 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12028 call_site->pc = pc;
12029
216f72a1
JK
12030 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12031 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12032 {
12033 struct die_info *func_die;
12034
12035 /* Skip also over DW_TAG_inlined_subroutine. */
12036 for (func_die = die->parent;
12037 func_die && func_die->tag != DW_TAG_subprogram
12038 && func_die->tag != DW_TAG_subroutine_type;
12039 func_die = func_die->parent);
12040
216f72a1
JK
12041 /* DW_AT_call_all_calls is a superset
12042 of DW_AT_call_all_tail_calls. */
96408a79 12043 if (func_die
216f72a1 12044 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12045 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12046 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12047 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12048 {
12049 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12050 not complete. But keep CALL_SITE for look ups via call_site_htab,
12051 both the initial caller containing the real return address PC and
12052 the final callee containing the current PC of a chain of tail
12053 calls do not need to have the tail call list complete. But any
12054 function candidate for a virtual tail call frame searched via
12055 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12056 determined unambiguously. */
12057 }
12058 else
12059 {
12060 struct type *func_type = NULL;
12061
12062 if (func_die)
12063 func_type = get_die_type (func_die, cu);
12064 if (func_type != NULL)
12065 {
12066 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12067
12068 /* Enlist this call site to the function. */
12069 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12070 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12071 }
12072 else
12073 complaint (&symfile_complaints,
216f72a1 12074 _("Cannot find function owning DW_TAG_call_site "
96408a79 12075 "DIE 0x%x [in module %s]"),
9c541725 12076 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12077 }
12078 }
12079
216f72a1
JK
12080 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12081 if (attr == NULL)
12082 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12083 if (attr == NULL)
12084 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12085 if (attr == NULL)
216f72a1
JK
12086 {
12087 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12088 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12089 }
96408a79
SA
12090 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12091 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12092 /* Keep NULL DWARF_BLOCK. */;
12093 else if (attr_form_is_block (attr))
12094 {
12095 struct dwarf2_locexpr_baton *dlbaton;
12096
8d749320 12097 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12098 dlbaton->data = DW_BLOCK (attr)->data;
12099 dlbaton->size = DW_BLOCK (attr)->size;
12100 dlbaton->per_cu = cu->per_cu;
12101
12102 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12103 }
7771576e 12104 else if (attr_form_is_ref (attr))
96408a79 12105 {
96408a79
SA
12106 struct dwarf2_cu *target_cu = cu;
12107 struct die_info *target_die;
12108
ac9ec31b 12109 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12110 gdb_assert (target_cu->objfile == objfile);
12111 if (die_is_declaration (target_die, target_cu))
12112 {
7d45c7c3 12113 const char *target_physname;
9112db09
JK
12114
12115 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12116 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12117 if (target_physname == NULL)
9112db09 12118 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12119 if (target_physname == NULL)
12120 complaint (&symfile_complaints,
216f72a1 12121 _("DW_AT_call_target target DIE has invalid "
96408a79 12122 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12123 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12124 else
7d455152 12125 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12126 }
12127 else
12128 {
12129 CORE_ADDR lowpc;
12130
12131 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12132 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12133 <= PC_BOUNDS_INVALID)
96408a79 12134 complaint (&symfile_complaints,
216f72a1 12135 _("DW_AT_call_target target DIE has invalid "
96408a79 12136 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12137 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12138 else
3e29f34a
MR
12139 {
12140 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12141 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12142 }
96408a79
SA
12143 }
12144 }
12145 else
12146 complaint (&symfile_complaints,
216f72a1 12147 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12148 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12149 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12150
12151 call_site->per_cu = cu->per_cu;
12152
12153 for (child_die = die->child;
12154 child_die && child_die->tag;
12155 child_die = sibling_die (child_die))
12156 {
96408a79 12157 struct call_site_parameter *parameter;
1788b2d3 12158 struct attribute *loc, *origin;
96408a79 12159
216f72a1
JK
12160 if (child_die->tag != DW_TAG_call_site_parameter
12161 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12162 {
12163 /* Already printed the complaint above. */
12164 continue;
12165 }
12166
12167 gdb_assert (call_site->parameter_count < nparams);
12168 parameter = &call_site->parameter[call_site->parameter_count];
12169
1788b2d3
JK
12170 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12171 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12172 register is contained in DW_AT_call_value. */
96408a79 12173
24c5c679 12174 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12175 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12176 if (origin == NULL)
12177 {
12178 /* This was a pre-DWARF-5 GNU extension alias
12179 for DW_AT_call_parameter. */
12180 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12181 }
7771576e 12182 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12183 {
1788b2d3 12184 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12185
12186 sect_offset sect_off
12187 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12188 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12189 {
12190 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12191 binding can be done only inside one CU. Such referenced DIE
12192 therefore cannot be even moved to DW_TAG_partial_unit. */
12193 complaint (&symfile_complaints,
216f72a1
JK
12194 _("DW_AT_call_parameter offset is not in CU for "
12195 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12196 to_underlying (child_die->sect_off),
12197 objfile_name (objfile));
d76b7dbc
JK
12198 continue;
12199 }
9c541725
PA
12200 parameter->u.param_cu_off
12201 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12202 }
12203 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12204 {
12205 complaint (&symfile_complaints,
12206 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12207 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12208 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12209 continue;
12210 }
24c5c679 12211 else
96408a79 12212 {
24c5c679
JK
12213 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12214 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12215 if (parameter->u.dwarf_reg != -1)
12216 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12217 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12218 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12219 &parameter->u.fb_offset))
12220 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12221 else
12222 {
12223 complaint (&symfile_complaints,
12224 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12225 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12226 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12227 "[in module %s]"),
9c541725
PA
12228 to_underlying (child_die->sect_off),
12229 objfile_name (objfile));
24c5c679
JK
12230 continue;
12231 }
96408a79
SA
12232 }
12233
216f72a1
JK
12234 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12235 if (attr == NULL)
12236 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12237 if (!attr_form_is_block (attr))
12238 {
12239 complaint (&symfile_complaints,
216f72a1
JK
12240 _("No DW_FORM_block* DW_AT_call_value for "
12241 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12242 to_underlying (child_die->sect_off),
12243 objfile_name (objfile));
96408a79
SA
12244 continue;
12245 }
12246 parameter->value = DW_BLOCK (attr)->data;
12247 parameter->value_size = DW_BLOCK (attr)->size;
12248
12249 /* Parameters are not pre-cleared by memset above. */
12250 parameter->data_value = NULL;
12251 parameter->data_value_size = 0;
12252 call_site->parameter_count++;
12253
216f72a1
JK
12254 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12255 if (attr == NULL)
12256 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12257 if (attr)
12258 {
12259 if (!attr_form_is_block (attr))
12260 complaint (&symfile_complaints,
216f72a1
JK
12261 _("No DW_FORM_block* DW_AT_call_data_value for "
12262 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12263 to_underlying (child_die->sect_off),
12264 objfile_name (objfile));
96408a79
SA
12265 else
12266 {
12267 parameter->data_value = DW_BLOCK (attr)->data;
12268 parameter->data_value_size = DW_BLOCK (attr)->size;
12269 }
12270 }
12271 }
12272}
12273
43988095
JK
12274/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12275 reading .debug_rnglists.
12276 Callback's type should be:
12277 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12278 Return true if the attributes are present and valid, otherwise,
12279 return false. */
12280
12281template <typename Callback>
12282static bool
12283dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12284 Callback &&callback)
12285{
12286 struct objfile *objfile = cu->objfile;
12287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12288 struct comp_unit_head *cu_header = &cu->header;
12289 bfd *obfd = objfile->obfd;
12290 unsigned int addr_size = cu_header->addr_size;
12291 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12292 /* Base address selection entry. */
12293 CORE_ADDR base;
12294 int found_base;
12295 unsigned int dummy;
12296 const gdb_byte *buffer;
12297 CORE_ADDR low = 0;
12298 CORE_ADDR high = 0;
12299 CORE_ADDR baseaddr;
12300 bool overflow = false;
12301
12302 found_base = cu->base_known;
12303 base = cu->base_address;
12304
12305 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12306 if (offset >= dwarf2_per_objfile->rnglists.size)
12307 {
12308 complaint (&symfile_complaints,
12309 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12310 offset);
12311 return false;
12312 }
12313 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12314
12315 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12316
12317 while (1)
12318 {
7814882a
JK
12319 /* Initialize it due to a false compiler warning. */
12320 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12321 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12322 + dwarf2_per_objfile->rnglists.size);
12323 unsigned int bytes_read;
12324
12325 if (buffer == buf_end)
12326 {
12327 overflow = true;
12328 break;
12329 }
12330 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12331 switch (rlet)
12332 {
12333 case DW_RLE_end_of_list:
12334 break;
12335 case DW_RLE_base_address:
12336 if (buffer + cu->header.addr_size > buf_end)
12337 {
12338 overflow = true;
12339 break;
12340 }
12341 base = read_address (obfd, buffer, cu, &bytes_read);
12342 found_base = 1;
12343 buffer += bytes_read;
12344 break;
12345 case DW_RLE_start_length:
12346 if (buffer + cu->header.addr_size > buf_end)
12347 {
12348 overflow = true;
12349 break;
12350 }
12351 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12352 buffer += bytes_read;
12353 range_end = (range_beginning
12354 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12355 buffer += bytes_read;
12356 if (buffer > buf_end)
12357 {
12358 overflow = true;
12359 break;
12360 }
12361 break;
12362 case DW_RLE_offset_pair:
12363 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12364 buffer += bytes_read;
12365 if (buffer > buf_end)
12366 {
12367 overflow = true;
12368 break;
12369 }
12370 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12371 buffer += bytes_read;
12372 if (buffer > buf_end)
12373 {
12374 overflow = true;
12375 break;
12376 }
12377 break;
12378 case DW_RLE_start_end:
12379 if (buffer + 2 * cu->header.addr_size > buf_end)
12380 {
12381 overflow = true;
12382 break;
12383 }
12384 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12385 buffer += bytes_read;
12386 range_end = read_address (obfd, buffer, cu, &bytes_read);
12387 buffer += bytes_read;
12388 break;
12389 default:
12390 complaint (&symfile_complaints,
12391 _("Invalid .debug_rnglists data (no base address)"));
12392 return false;
12393 }
12394 if (rlet == DW_RLE_end_of_list || overflow)
12395 break;
12396 if (rlet == DW_RLE_base_address)
12397 continue;
12398
12399 if (!found_base)
12400 {
12401 /* We have no valid base address for the ranges
12402 data. */
12403 complaint (&symfile_complaints,
12404 _("Invalid .debug_rnglists data (no base address)"));
12405 return false;
12406 }
12407
12408 if (range_beginning > range_end)
12409 {
12410 /* Inverted range entries are invalid. */
12411 complaint (&symfile_complaints,
12412 _("Invalid .debug_rnglists data (inverted range)"));
12413 return false;
12414 }
12415
12416 /* Empty range entries have no effect. */
12417 if (range_beginning == range_end)
12418 continue;
12419
12420 range_beginning += base;
12421 range_end += base;
12422
12423 /* A not-uncommon case of bad debug info.
12424 Don't pollute the addrmap with bad data. */
12425 if (range_beginning + baseaddr == 0
12426 && !dwarf2_per_objfile->has_section_at_zero)
12427 {
12428 complaint (&symfile_complaints,
12429 _(".debug_rnglists entry has start address of zero"
12430 " [in module %s]"), objfile_name (objfile));
12431 continue;
12432 }
12433
12434 callback (range_beginning, range_end);
12435 }
12436
12437 if (overflow)
12438 {
12439 complaint (&symfile_complaints,
12440 _("Offset %d is not terminated "
12441 "for DW_AT_ranges attribute"),
12442 offset);
12443 return false;
12444 }
12445
12446 return true;
12447}
12448
12449/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12450 Callback's type should be:
12451 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12452 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12453
43988095 12454template <typename Callback>
43039443 12455static int
5f46c5a5 12456dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12457 Callback &&callback)
43039443
JK
12458{
12459 struct objfile *objfile = cu->objfile;
3e29f34a 12460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12461 struct comp_unit_head *cu_header = &cu->header;
12462 bfd *obfd = objfile->obfd;
12463 unsigned int addr_size = cu_header->addr_size;
12464 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12465 /* Base address selection entry. */
12466 CORE_ADDR base;
12467 int found_base;
12468 unsigned int dummy;
d521ce57 12469 const gdb_byte *buffer;
ff013f42 12470 CORE_ADDR baseaddr;
43039443 12471
43988095
JK
12472 if (cu_header->version >= 5)
12473 return dwarf2_rnglists_process (offset, cu, callback);
12474
d00adf39
DE
12475 found_base = cu->base_known;
12476 base = cu->base_address;
43039443 12477
be391dca 12478 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12479 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12480 {
12481 complaint (&symfile_complaints,
12482 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12483 offset);
12484 return 0;
12485 }
dce234bc 12486 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12487
e7030f15 12488 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12489
43039443
JK
12490 while (1)
12491 {
12492 CORE_ADDR range_beginning, range_end;
12493
12494 range_beginning = read_address (obfd, buffer, cu, &dummy);
12495 buffer += addr_size;
12496 range_end = read_address (obfd, buffer, cu, &dummy);
12497 buffer += addr_size;
12498 offset += 2 * addr_size;
12499
12500 /* An end of list marker is a pair of zero addresses. */
12501 if (range_beginning == 0 && range_end == 0)
12502 /* Found the end of list entry. */
12503 break;
12504
12505 /* Each base address selection entry is a pair of 2 values.
12506 The first is the largest possible address, the second is
12507 the base address. Check for a base address here. */
12508 if ((range_beginning & mask) == mask)
12509 {
28d2bfb9
AB
12510 /* If we found the largest possible address, then we already
12511 have the base address in range_end. */
12512 base = range_end;
43039443
JK
12513 found_base = 1;
12514 continue;
12515 }
12516
12517 if (!found_base)
12518 {
12519 /* We have no valid base address for the ranges
12520 data. */
12521 complaint (&symfile_complaints,
12522 _("Invalid .debug_ranges data (no base address)"));
12523 return 0;
12524 }
12525
9277c30c
UW
12526 if (range_beginning > range_end)
12527 {
12528 /* Inverted range entries are invalid. */
12529 complaint (&symfile_complaints,
12530 _("Invalid .debug_ranges data (inverted range)"));
12531 return 0;
12532 }
12533
12534 /* Empty range entries have no effect. */
12535 if (range_beginning == range_end)
12536 continue;
12537
43039443
JK
12538 range_beginning += base;
12539 range_end += base;
12540
01093045
DE
12541 /* A not-uncommon case of bad debug info.
12542 Don't pollute the addrmap with bad data. */
12543 if (range_beginning + baseaddr == 0
12544 && !dwarf2_per_objfile->has_section_at_zero)
12545 {
12546 complaint (&symfile_complaints,
12547 _(".debug_ranges entry has start address of zero"
4262abfb 12548 " [in module %s]"), objfile_name (objfile));
01093045
DE
12549 continue;
12550 }
12551
5f46c5a5
JK
12552 callback (range_beginning, range_end);
12553 }
12554
12555 return 1;
12556}
12557
12558/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12559 Return 1 if the attributes are present and valid, otherwise, return 0.
12560 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12561
12562static int
12563dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12564 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12565 struct partial_symtab *ranges_pst)
12566{
12567 struct objfile *objfile = cu->objfile;
12568 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12569 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12570 SECT_OFF_TEXT (objfile));
12571 int low_set = 0;
12572 CORE_ADDR low = 0;
12573 CORE_ADDR high = 0;
12574 int retval;
12575
12576 retval = dwarf2_ranges_process (offset, cu,
12577 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12578 {
9277c30c 12579 if (ranges_pst != NULL)
3e29f34a
MR
12580 {
12581 CORE_ADDR lowpc;
12582 CORE_ADDR highpc;
12583
12584 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12585 range_beginning + baseaddr);
12586 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12587 range_end + baseaddr);
12588 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12589 ranges_pst);
12590 }
ff013f42 12591
43039443
JK
12592 /* FIXME: This is recording everything as a low-high
12593 segment of consecutive addresses. We should have a
12594 data structure for discontiguous block ranges
12595 instead. */
12596 if (! low_set)
12597 {
12598 low = range_beginning;
12599 high = range_end;
12600 low_set = 1;
12601 }
12602 else
12603 {
12604 if (range_beginning < low)
12605 low = range_beginning;
12606 if (range_end > high)
12607 high = range_end;
12608 }
5f46c5a5
JK
12609 });
12610 if (!retval)
12611 return 0;
43039443
JK
12612
12613 if (! low_set)
12614 /* If the first entry is an end-of-list marker, the range
12615 describes an empty scope, i.e. no instructions. */
12616 return 0;
12617
12618 if (low_return)
12619 *low_return = low;
12620 if (high_return)
12621 *high_return = high;
12622 return 1;
12623}
12624
3a2b436a
JK
12625/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12626 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12627 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12628
3a2b436a 12629static enum pc_bounds_kind
af34e669 12630dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12631 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12632 struct partial_symtab *pst)
c906108c
SS
12633{
12634 struct attribute *attr;
91da1414 12635 struct attribute *attr_high;
af34e669
DJ
12636 CORE_ADDR low = 0;
12637 CORE_ADDR high = 0;
e385593e 12638 enum pc_bounds_kind ret;
c906108c 12639
91da1414
MW
12640 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12641 if (attr_high)
af34e669 12642 {
e142c38c 12643 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12644 if (attr)
91da1414 12645 {
31aa7e4e
JB
12646 low = attr_value_as_address (attr);
12647 high = attr_value_as_address (attr_high);
12648 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12649 high += low;
91da1414 12650 }
af34e669
DJ
12651 else
12652 /* Found high w/o low attribute. */
e385593e 12653 return PC_BOUNDS_INVALID;
af34e669
DJ
12654
12655 /* Found consecutive range of addresses. */
3a2b436a 12656 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12657 }
c906108c 12658 else
af34e669 12659 {
e142c38c 12660 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12661 if (attr != NULL)
12662 {
ab435259
DE
12663 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12664 We take advantage of the fact that DW_AT_ranges does not appear
12665 in DW_TAG_compile_unit of DWO files. */
12666 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12667 unsigned int ranges_offset = (DW_UNSND (attr)
12668 + (need_ranges_base
12669 ? cu->ranges_base
12670 : 0));
2e3cf129 12671
af34e669 12672 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12673 .debug_ranges section. */
2e3cf129 12674 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12675 return PC_BOUNDS_INVALID;
43039443 12676 /* Found discontinuous range of addresses. */
3a2b436a 12677 ret = PC_BOUNDS_RANGES;
af34e669 12678 }
e385593e
JK
12679 else
12680 return PC_BOUNDS_NOT_PRESENT;
af34e669 12681 }
c906108c 12682
9373cf26
JK
12683 /* read_partial_die has also the strict LOW < HIGH requirement. */
12684 if (high <= low)
e385593e 12685 return PC_BOUNDS_INVALID;
c906108c
SS
12686
12687 /* When using the GNU linker, .gnu.linkonce. sections are used to
12688 eliminate duplicate copies of functions and vtables and such.
12689 The linker will arbitrarily choose one and discard the others.
12690 The AT_*_pc values for such functions refer to local labels in
12691 these sections. If the section from that file was discarded, the
12692 labels are not in the output, so the relocs get a value of 0.
12693 If this is a discarded function, mark the pc bounds as invalid,
12694 so that GDB will ignore it. */
72dca2f5 12695 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12696 return PC_BOUNDS_INVALID;
c906108c
SS
12697
12698 *lowpc = low;
96408a79
SA
12699 if (highpc)
12700 *highpc = high;
af34e669 12701 return ret;
c906108c
SS
12702}
12703
b084d499
JB
12704/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12705 its low and high PC addresses. Do nothing if these addresses could not
12706 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12707 and HIGHPC to the high address if greater than HIGHPC. */
12708
12709static void
12710dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12711 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12712 struct dwarf2_cu *cu)
12713{
12714 CORE_ADDR low, high;
12715 struct die_info *child = die->child;
12716
e385593e 12717 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12718 {
325fac50
PA
12719 *lowpc = std::min (*lowpc, low);
12720 *highpc = std::max (*highpc, high);
b084d499
JB
12721 }
12722
12723 /* If the language does not allow nested subprograms (either inside
12724 subprograms or lexical blocks), we're done. */
12725 if (cu->language != language_ada)
12726 return;
6e70227d 12727
b084d499
JB
12728 /* Check all the children of the given DIE. If it contains nested
12729 subprograms, then check their pc bounds. Likewise, we need to
12730 check lexical blocks as well, as they may also contain subprogram
12731 definitions. */
12732 while (child && child->tag)
12733 {
12734 if (child->tag == DW_TAG_subprogram
12735 || child->tag == DW_TAG_lexical_block)
12736 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12737 child = sibling_die (child);
12738 }
12739}
12740
fae299cd
DC
12741/* Get the low and high pc's represented by the scope DIE, and store
12742 them in *LOWPC and *HIGHPC. If the correct values can't be
12743 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12744
12745static void
12746get_scope_pc_bounds (struct die_info *die,
12747 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12748 struct dwarf2_cu *cu)
12749{
12750 CORE_ADDR best_low = (CORE_ADDR) -1;
12751 CORE_ADDR best_high = (CORE_ADDR) 0;
12752 CORE_ADDR current_low, current_high;
12753
3a2b436a 12754 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12755 >= PC_BOUNDS_RANGES)
fae299cd
DC
12756 {
12757 best_low = current_low;
12758 best_high = current_high;
12759 }
12760 else
12761 {
12762 struct die_info *child = die->child;
12763
12764 while (child && child->tag)
12765 {
12766 switch (child->tag) {
12767 case DW_TAG_subprogram:
b084d499 12768 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12769 break;
12770 case DW_TAG_namespace:
f55ee35c 12771 case DW_TAG_module:
fae299cd
DC
12772 /* FIXME: carlton/2004-01-16: Should we do this for
12773 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12774 that current GCC's always emit the DIEs corresponding
12775 to definitions of methods of classes as children of a
12776 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12777 the DIEs giving the declarations, which could be
12778 anywhere). But I don't see any reason why the
12779 standards says that they have to be there. */
12780 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12781
12782 if (current_low != ((CORE_ADDR) -1))
12783 {
325fac50
PA
12784 best_low = std::min (best_low, current_low);
12785 best_high = std::max (best_high, current_high);
fae299cd
DC
12786 }
12787 break;
12788 default:
0963b4bd 12789 /* Ignore. */
fae299cd
DC
12790 break;
12791 }
12792
12793 child = sibling_die (child);
12794 }
12795 }
12796
12797 *lowpc = best_low;
12798 *highpc = best_high;
12799}
12800
801e3a5b
JB
12801/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12802 in DIE. */
380bca97 12803
801e3a5b
JB
12804static void
12805dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12806 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12807{
bb5ed363 12808 struct objfile *objfile = cu->objfile;
3e29f34a 12809 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12810 struct attribute *attr;
91da1414 12811 struct attribute *attr_high;
801e3a5b 12812
91da1414
MW
12813 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12814 if (attr_high)
801e3a5b 12815 {
801e3a5b
JB
12816 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12817 if (attr)
12818 {
31aa7e4e
JB
12819 CORE_ADDR low = attr_value_as_address (attr);
12820 CORE_ADDR high = attr_value_as_address (attr_high);
12821
12822 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12823 high += low;
9a619af0 12824
3e29f34a
MR
12825 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12826 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12827 record_block_range (block, low, high - 1);
801e3a5b
JB
12828 }
12829 }
12830
12831 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12832 if (attr)
12833 {
bb5ed363 12834 bfd *obfd = objfile->obfd;
ab435259
DE
12835 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12836 We take advantage of the fact that DW_AT_ranges does not appear
12837 in DW_TAG_compile_unit of DWO files. */
12838 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12839
12840 /* The value of the DW_AT_ranges attribute is the offset of the
12841 address range list in the .debug_ranges section. */
ab435259
DE
12842 unsigned long offset = (DW_UNSND (attr)
12843 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12844 const gdb_byte *buffer;
801e3a5b
JB
12845
12846 /* For some target architectures, but not others, the
12847 read_address function sign-extends the addresses it returns.
12848 To recognize base address selection entries, we need a
12849 mask. */
12850 unsigned int addr_size = cu->header.addr_size;
12851 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12852
12853 /* The base address, to which the next pair is relative. Note
12854 that this 'base' is a DWARF concept: most entries in a range
12855 list are relative, to reduce the number of relocs against the
12856 debugging information. This is separate from this function's
12857 'baseaddr' argument, which GDB uses to relocate debugging
12858 information from a shared library based on the address at
12859 which the library was loaded. */
d00adf39
DE
12860 CORE_ADDR base = cu->base_address;
12861 int base_known = cu->base_known;
801e3a5b 12862
5f46c5a5
JK
12863 dwarf2_ranges_process (offset, cu,
12864 [&] (CORE_ADDR start, CORE_ADDR end)
12865 {
58fdfd2c
JK
12866 start += baseaddr;
12867 end += baseaddr;
5f46c5a5
JK
12868 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12869 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12870 record_block_range (block, start, end - 1);
12871 });
801e3a5b
JB
12872 }
12873}
12874
685b1105
JK
12875/* Check whether the producer field indicates either of GCC < 4.6, or the
12876 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12877
685b1105
JK
12878static void
12879check_producer (struct dwarf2_cu *cu)
60d5a603 12880{
38360086 12881 int major, minor;
60d5a603
JK
12882
12883 if (cu->producer == NULL)
12884 {
12885 /* For unknown compilers expect their behavior is DWARF version
12886 compliant.
12887
12888 GCC started to support .debug_types sections by -gdwarf-4 since
12889 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12890 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12891 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12892 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12893 }
b1ffba5a 12894 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12895 {
38360086
MW
12896 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12897 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12898 }
5230b05a
WT
12899 else if (producer_is_icc (cu->producer, &major, &minor))
12900 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
12901 else
12902 {
12903 /* For other non-GCC compilers, expect their behavior is DWARF version
12904 compliant. */
60d5a603
JK
12905 }
12906
ba919b58 12907 cu->checked_producer = 1;
685b1105 12908}
ba919b58 12909
685b1105
JK
12910/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12911 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12912 during 4.6.0 experimental. */
12913
12914static int
12915producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12916{
12917 if (!cu->checked_producer)
12918 check_producer (cu);
12919
12920 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12921}
12922
12923/* Return the default accessibility type if it is not overriden by
12924 DW_AT_accessibility. */
12925
12926static enum dwarf_access_attribute
12927dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12928{
12929 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12930 {
12931 /* The default DWARF 2 accessibility for members is public, the default
12932 accessibility for inheritance is private. */
12933
12934 if (die->tag != DW_TAG_inheritance)
12935 return DW_ACCESS_public;
12936 else
12937 return DW_ACCESS_private;
12938 }
12939 else
12940 {
12941 /* DWARF 3+ defines the default accessibility a different way. The same
12942 rules apply now for DW_TAG_inheritance as for the members and it only
12943 depends on the container kind. */
12944
12945 if (die->parent->tag == DW_TAG_class_type)
12946 return DW_ACCESS_private;
12947 else
12948 return DW_ACCESS_public;
12949 }
12950}
12951
74ac6d43
TT
12952/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12953 offset. If the attribute was not found return 0, otherwise return
12954 1. If it was found but could not properly be handled, set *OFFSET
12955 to 0. */
12956
12957static int
12958handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12959 LONGEST *offset)
12960{
12961 struct attribute *attr;
12962
12963 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12964 if (attr != NULL)
12965 {
12966 *offset = 0;
12967
12968 /* Note that we do not check for a section offset first here.
12969 This is because DW_AT_data_member_location is new in DWARF 4,
12970 so if we see it, we can assume that a constant form is really
12971 a constant and not a section offset. */
12972 if (attr_form_is_constant (attr))
12973 *offset = dwarf2_get_attr_constant_value (attr, 0);
12974 else if (attr_form_is_section_offset (attr))
12975 dwarf2_complex_location_expr_complaint ();
12976 else if (attr_form_is_block (attr))
12977 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12978 else
12979 dwarf2_complex_location_expr_complaint ();
12980
12981 return 1;
12982 }
12983
12984 return 0;
12985}
12986
c906108c
SS
12987/* Add an aggregate field to the field list. */
12988
12989static void
107d2387 12990dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12991 struct dwarf2_cu *cu)
6e70227d 12992{
e7c27a73 12993 struct objfile *objfile = cu->objfile;
5e2b427d 12994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12995 struct nextfield *new_field;
12996 struct attribute *attr;
12997 struct field *fp;
15d034d0 12998 const char *fieldname = "";
c906108c
SS
12999
13000 /* Allocate a new field list entry and link it in. */
8d749320 13001 new_field = XNEW (struct nextfield);
b8c9b27d 13002 make_cleanup (xfree, new_field);
c906108c 13003 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13004
13005 if (die->tag == DW_TAG_inheritance)
13006 {
13007 new_field->next = fip->baseclasses;
13008 fip->baseclasses = new_field;
13009 }
13010 else
13011 {
13012 new_field->next = fip->fields;
13013 fip->fields = new_field;
13014 }
c906108c
SS
13015 fip->nfields++;
13016
e142c38c 13017 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13018 if (attr)
13019 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13020 else
13021 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13022 if (new_field->accessibility != DW_ACCESS_public)
13023 fip->non_public_fields = 1;
60d5a603 13024
e142c38c 13025 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13026 if (attr)
13027 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13028 else
13029 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13030
13031 fp = &new_field->field;
a9a9bd0f 13032
e142c38c 13033 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13034 {
74ac6d43
TT
13035 LONGEST offset;
13036
a9a9bd0f 13037 /* Data member other than a C++ static data member. */
6e70227d 13038
c906108c 13039 /* Get type of field. */
e7c27a73 13040 fp->type = die_type (die, cu);
c906108c 13041
d6a843b5 13042 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13043
c906108c 13044 /* Get bit size of field (zero if none). */
e142c38c 13045 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13046 if (attr)
13047 {
13048 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13049 }
13050 else
13051 {
13052 FIELD_BITSIZE (*fp) = 0;
13053 }
13054
13055 /* Get bit offset of field. */
74ac6d43
TT
13056 if (handle_data_member_location (die, cu, &offset))
13057 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13058 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13059 if (attr)
13060 {
5e2b427d 13061 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13062 {
13063 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13064 additional bit offset from the MSB of the containing
13065 anonymous object to the MSB of the field. We don't
13066 have to do anything special since we don't need to
13067 know the size of the anonymous object. */
f41f5e61 13068 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13069 }
13070 else
13071 {
13072 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13073 MSB of the anonymous object, subtract off the number of
13074 bits from the MSB of the field to the MSB of the
13075 object, and then subtract off the number of bits of
13076 the field itself. The result is the bit offset of
13077 the LSB of the field. */
c906108c
SS
13078 int anonymous_size;
13079 int bit_offset = DW_UNSND (attr);
13080
e142c38c 13081 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13082 if (attr)
13083 {
13084 /* The size of the anonymous object containing
13085 the bit field is explicit, so use the
13086 indicated size (in bytes). */
13087 anonymous_size = DW_UNSND (attr);
13088 }
13089 else
13090 {
13091 /* The size of the anonymous object containing
13092 the bit field must be inferred from the type
13093 attribute of the data member containing the
13094 bit field. */
13095 anonymous_size = TYPE_LENGTH (fp->type);
13096 }
f41f5e61
PA
13097 SET_FIELD_BITPOS (*fp,
13098 (FIELD_BITPOS (*fp)
13099 + anonymous_size * bits_per_byte
13100 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13101 }
13102 }
da5b30da
AA
13103 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13104 if (attr != NULL)
13105 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13106 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13107
13108 /* Get name of field. */
39cbfefa
DJ
13109 fieldname = dwarf2_name (die, cu);
13110 if (fieldname == NULL)
13111 fieldname = "";
d8151005
DJ
13112
13113 /* The name is already allocated along with this objfile, so we don't
13114 need to duplicate it for the type. */
13115 fp->name = fieldname;
c906108c
SS
13116
13117 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13118 pointer or virtual base class pointer) to private. */
e142c38c 13119 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13120 {
d48cc9dd 13121 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13122 new_field->accessibility = DW_ACCESS_private;
13123 fip->non_public_fields = 1;
13124 }
13125 }
a9a9bd0f 13126 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13127 {
a9a9bd0f
DC
13128 /* C++ static member. */
13129
13130 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13131 is a declaration, but all versions of G++ as of this writing
13132 (so through at least 3.2.1) incorrectly generate
13133 DW_TAG_variable tags. */
6e70227d 13134
ff355380 13135 const char *physname;
c906108c 13136
a9a9bd0f 13137 /* Get name of field. */
39cbfefa
DJ
13138 fieldname = dwarf2_name (die, cu);
13139 if (fieldname == NULL)
c906108c
SS
13140 return;
13141
254e6b9e 13142 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13143 if (attr
13144 /* Only create a symbol if this is an external value.
13145 new_symbol checks this and puts the value in the global symbol
13146 table, which we want. If it is not external, new_symbol
13147 will try to put the value in cu->list_in_scope which is wrong. */
13148 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13149 {
13150 /* A static const member, not much different than an enum as far as
13151 we're concerned, except that we can support more types. */
13152 new_symbol (die, NULL, cu);
13153 }
13154
2df3850c 13155 /* Get physical name. */
ff355380 13156 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13157
d8151005
DJ
13158 /* The name is already allocated along with this objfile, so we don't
13159 need to duplicate it for the type. */
13160 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13161 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13162 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13163 }
13164 else if (die->tag == DW_TAG_inheritance)
13165 {
74ac6d43 13166 LONGEST offset;
d4b96c9a 13167
74ac6d43
TT
13168 /* C++ base class field. */
13169 if (handle_data_member_location (die, cu, &offset))
13170 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13171 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13172 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13173 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13174 fip->nbaseclasses++;
13175 }
13176}
13177
98751a41
JK
13178/* Add a typedef defined in the scope of the FIP's class. */
13179
13180static void
13181dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13182 struct dwarf2_cu *cu)
6e70227d 13183{
98751a41 13184 struct typedef_field_list *new_field;
98751a41 13185 struct typedef_field *fp;
98751a41
JK
13186
13187 /* Allocate a new field list entry and link it in. */
8d749320 13188 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13189 make_cleanup (xfree, new_field);
13190
13191 gdb_assert (die->tag == DW_TAG_typedef);
13192
13193 fp = &new_field->field;
13194
13195 /* Get name of field. */
13196 fp->name = dwarf2_name (die, cu);
13197 if (fp->name == NULL)
13198 return;
13199
13200 fp->type = read_type_die (die, cu);
13201
c191a687
KS
13202 /* Save accessibility. */
13203 enum dwarf_access_attribute accessibility;
13204 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13205 if (attr != NULL)
13206 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13207 else
13208 accessibility = dwarf2_default_access_attribute (die, cu);
13209 switch (accessibility)
13210 {
13211 case DW_ACCESS_public:
13212 /* The assumed value if neither private nor protected. */
13213 break;
13214 case DW_ACCESS_private:
13215 fp->is_private = 1;
13216 break;
13217 case DW_ACCESS_protected:
13218 fp->is_protected = 1;
13219 break;
13220 default:
37534686
KS
13221 complaint (&symfile_complaints,
13222 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
13223 }
13224
98751a41
JK
13225 new_field->next = fip->typedef_field_list;
13226 fip->typedef_field_list = new_field;
13227 fip->typedef_field_list_count++;
13228}
13229
c906108c
SS
13230/* Create the vector of fields, and attach it to the type. */
13231
13232static void
fba45db2 13233dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13234 struct dwarf2_cu *cu)
c906108c
SS
13235{
13236 int nfields = fip->nfields;
13237
13238 /* Record the field count, allocate space for the array of fields,
13239 and create blank accessibility bitfields if necessary. */
13240 TYPE_NFIELDS (type) = nfields;
13241 TYPE_FIELDS (type) = (struct field *)
13242 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13243 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13244
b4ba55a1 13245 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13246 {
13247 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13248
13249 TYPE_FIELD_PRIVATE_BITS (type) =
13250 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13251 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13252
13253 TYPE_FIELD_PROTECTED_BITS (type) =
13254 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13255 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13256
774b6a14
TT
13257 TYPE_FIELD_IGNORE_BITS (type) =
13258 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13259 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13260 }
13261
13262 /* If the type has baseclasses, allocate and clear a bit vector for
13263 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13264 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13265 {
13266 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13267 unsigned char *pointer;
c906108c
SS
13268
13269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13270 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13271 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13272 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13273 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13274 }
13275
3e43a32a
MS
13276 /* Copy the saved-up fields into the field vector. Start from the head of
13277 the list, adding to the tail of the field array, so that they end up in
13278 the same order in the array in which they were added to the list. */
c906108c
SS
13279 while (nfields-- > 0)
13280 {
7d0ccb61
DJ
13281 struct nextfield *fieldp;
13282
13283 if (fip->fields)
13284 {
13285 fieldp = fip->fields;
13286 fip->fields = fieldp->next;
13287 }
13288 else
13289 {
13290 fieldp = fip->baseclasses;
13291 fip->baseclasses = fieldp->next;
13292 }
13293
13294 TYPE_FIELD (type, nfields) = fieldp->field;
13295 switch (fieldp->accessibility)
c906108c 13296 {
c5aa993b 13297 case DW_ACCESS_private:
b4ba55a1
JB
13298 if (cu->language != language_ada)
13299 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13300 break;
c906108c 13301
c5aa993b 13302 case DW_ACCESS_protected:
b4ba55a1
JB
13303 if (cu->language != language_ada)
13304 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13305 break;
c906108c 13306
c5aa993b
JM
13307 case DW_ACCESS_public:
13308 break;
c906108c 13309
c5aa993b
JM
13310 default:
13311 /* Unknown accessibility. Complain and treat it as public. */
13312 {
e2e0b3e5 13313 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13314 fieldp->accessibility);
c5aa993b
JM
13315 }
13316 break;
c906108c
SS
13317 }
13318 if (nfields < fip->nbaseclasses)
13319 {
7d0ccb61 13320 switch (fieldp->virtuality)
c906108c 13321 {
c5aa993b
JM
13322 case DW_VIRTUALITY_virtual:
13323 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13324 if (cu->language == language_ada)
a73c6dcd 13325 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13326 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13327 break;
c906108c
SS
13328 }
13329 }
c906108c
SS
13330 }
13331}
13332
7d27a96d
TT
13333/* Return true if this member function is a constructor, false
13334 otherwise. */
13335
13336static int
13337dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13338{
13339 const char *fieldname;
fe978cb0 13340 const char *type_name;
7d27a96d
TT
13341 int len;
13342
13343 if (die->parent == NULL)
13344 return 0;
13345
13346 if (die->parent->tag != DW_TAG_structure_type
13347 && die->parent->tag != DW_TAG_union_type
13348 && die->parent->tag != DW_TAG_class_type)
13349 return 0;
13350
13351 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13352 type_name = dwarf2_name (die->parent, cu);
13353 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13354 return 0;
13355
13356 len = strlen (fieldname);
fe978cb0
PA
13357 return (strncmp (fieldname, type_name, len) == 0
13358 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13359}
13360
c906108c
SS
13361/* Add a member function to the proper fieldlist. */
13362
13363static void
107d2387 13364dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13365 struct type *type, struct dwarf2_cu *cu)
c906108c 13366{
e7c27a73 13367 struct objfile *objfile = cu->objfile;
c906108c
SS
13368 struct attribute *attr;
13369 struct fnfieldlist *flp;
13370 int i;
13371 struct fn_field *fnp;
15d034d0 13372 const char *fieldname;
c906108c 13373 struct nextfnfield *new_fnfield;
f792889a 13374 struct type *this_type;
60d5a603 13375 enum dwarf_access_attribute accessibility;
c906108c 13376
b4ba55a1 13377 if (cu->language == language_ada)
a73c6dcd 13378 error (_("unexpected member function in Ada type"));
b4ba55a1 13379
2df3850c 13380 /* Get name of member function. */
39cbfefa
DJ
13381 fieldname = dwarf2_name (die, cu);
13382 if (fieldname == NULL)
2df3850c 13383 return;
c906108c 13384
c906108c
SS
13385 /* Look up member function name in fieldlist. */
13386 for (i = 0; i < fip->nfnfields; i++)
13387 {
27bfe10e 13388 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13389 break;
13390 }
13391
13392 /* Create new list element if necessary. */
13393 if (i < fip->nfnfields)
13394 flp = &fip->fnfieldlists[i];
13395 else
13396 {
13397 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13398 {
13399 fip->fnfieldlists = (struct fnfieldlist *)
13400 xrealloc (fip->fnfieldlists,
13401 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13402 * sizeof (struct fnfieldlist));
c906108c 13403 if (fip->nfnfields == 0)
c13c43fd 13404 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13405 }
13406 flp = &fip->fnfieldlists[fip->nfnfields];
13407 flp->name = fieldname;
13408 flp->length = 0;
13409 flp->head = NULL;
3da10d80 13410 i = fip->nfnfields++;
c906108c
SS
13411 }
13412
13413 /* Create a new member function field and chain it to the field list
0963b4bd 13414 entry. */
8d749320 13415 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13416 make_cleanup (xfree, new_fnfield);
c906108c
SS
13417 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13418 new_fnfield->next = flp->head;
13419 flp->head = new_fnfield;
13420 flp->length++;
13421
13422 /* Fill in the member function field info. */
13423 fnp = &new_fnfield->fnfield;
3da10d80
KS
13424
13425 /* Delay processing of the physname until later. */
9c37b5ae 13426 if (cu->language == language_cplus)
3da10d80
KS
13427 {
13428 add_to_method_list (type, i, flp->length - 1, fieldname,
13429 die, cu);
13430 }
13431 else
13432 {
1d06ead6 13433 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13434 fnp->physname = physname ? physname : "";
13435 }
13436
c906108c 13437 fnp->type = alloc_type (objfile);
f792889a
DJ
13438 this_type = read_type_die (die, cu);
13439 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13440 {
f792889a 13441 int nparams = TYPE_NFIELDS (this_type);
c906108c 13442
f792889a 13443 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13444 of the method itself (TYPE_CODE_METHOD). */
13445 smash_to_method_type (fnp->type, type,
f792889a
DJ
13446 TYPE_TARGET_TYPE (this_type),
13447 TYPE_FIELDS (this_type),
13448 TYPE_NFIELDS (this_type),
13449 TYPE_VARARGS (this_type));
c906108c
SS
13450
13451 /* Handle static member functions.
c5aa993b 13452 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13453 member functions. G++ helps GDB by marking the first
13454 parameter for non-static member functions (which is the this
13455 pointer) as artificial. We obtain this information from
13456 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13457 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13458 fnp->voffset = VOFFSET_STATIC;
13459 }
13460 else
e2e0b3e5 13461 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13462 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13463
13464 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13466 fnp->fcontext = die_containing_type (die, cu);
c906108c 13467
3e43a32a
MS
13468 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13469 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13470
13471 /* Get accessibility. */
e142c38c 13472 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13473 if (attr)
aead7601 13474 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13475 else
13476 accessibility = dwarf2_default_access_attribute (die, cu);
13477 switch (accessibility)
c906108c 13478 {
60d5a603
JK
13479 case DW_ACCESS_private:
13480 fnp->is_private = 1;
13481 break;
13482 case DW_ACCESS_protected:
13483 fnp->is_protected = 1;
13484 break;
c906108c
SS
13485 }
13486
b02dede2 13487 /* Check for artificial methods. */
e142c38c 13488 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13489 if (attr && DW_UNSND (attr) != 0)
13490 fnp->is_artificial = 1;
13491
7d27a96d
TT
13492 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13493
0d564a31 13494 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13495 function. For older versions of GCC, this is an offset in the
13496 appropriate virtual table, as specified by DW_AT_containing_type.
13497 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13498 to the object address. */
13499
e142c38c 13500 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13501 if (attr)
8e19ed76 13502 {
aec5aa8b 13503 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13504 {
aec5aa8b
TT
13505 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13506 {
13507 /* Old-style GCC. */
13508 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13509 }
13510 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13511 || (DW_BLOCK (attr)->size > 1
13512 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13513 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13514 {
aec5aa8b
TT
13515 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13516 if ((fnp->voffset % cu->header.addr_size) != 0)
13517 dwarf2_complex_location_expr_complaint ();
13518 else
13519 fnp->voffset /= cu->header.addr_size;
13520 fnp->voffset += 2;
13521 }
13522 else
13523 dwarf2_complex_location_expr_complaint ();
13524
13525 if (!fnp->fcontext)
7e993ebf
KS
13526 {
13527 /* If there is no `this' field and no DW_AT_containing_type,
13528 we cannot actually find a base class context for the
13529 vtable! */
13530 if (TYPE_NFIELDS (this_type) == 0
13531 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13532 {
13533 complaint (&symfile_complaints,
13534 _("cannot determine context for virtual member "
13535 "function \"%s\" (offset %d)"),
9c541725 13536 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13537 }
13538 else
13539 {
13540 fnp->fcontext
13541 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13542 }
13543 }
aec5aa8b 13544 }
3690dd37 13545 else if (attr_form_is_section_offset (attr))
8e19ed76 13546 {
4d3c2250 13547 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13548 }
13549 else
13550 {
4d3c2250
KB
13551 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13552 fieldname);
8e19ed76 13553 }
0d564a31 13554 }
d48cc9dd
DJ
13555 else
13556 {
13557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13558 if (attr && DW_UNSND (attr))
13559 {
13560 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13561 complaint (&symfile_complaints,
3e43a32a
MS
13562 _("Member function \"%s\" (offset %d) is virtual "
13563 "but the vtable offset is not specified"),
9c541725 13564 fieldname, to_underlying (die->sect_off));
9655fd1a 13565 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13566 TYPE_CPLUS_DYNAMIC (type) = 1;
13567 }
13568 }
c906108c
SS
13569}
13570
13571/* Create the vector of member function fields, and attach it to the type. */
13572
13573static void
fba45db2 13574dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13575 struct dwarf2_cu *cu)
c906108c
SS
13576{
13577 struct fnfieldlist *flp;
c906108c
SS
13578 int i;
13579
b4ba55a1 13580 if (cu->language == language_ada)
a73c6dcd 13581 error (_("unexpected member functions in Ada type"));
b4ba55a1 13582
c906108c
SS
13583 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13584 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13585 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13586
13587 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13588 {
13589 struct nextfnfield *nfp = flp->head;
13590 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13591 int k;
13592
13593 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13594 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13595 fn_flp->fn_fields = (struct fn_field *)
13596 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13597 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13598 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13599 }
13600
13601 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13602}
13603
1168df01
JB
13604/* Returns non-zero if NAME is the name of a vtable member in CU's
13605 language, zero otherwise. */
13606static int
13607is_vtable_name (const char *name, struct dwarf2_cu *cu)
13608{
13609 static const char vptr[] = "_vptr";
987504bb 13610 static const char vtable[] = "vtable";
1168df01 13611
9c37b5ae
TT
13612 /* Look for the C++ form of the vtable. */
13613 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13614 return 1;
13615
13616 return 0;
13617}
13618
c0dd20ea 13619/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13620 functions, with the ABI-specified layout. If TYPE describes
13621 such a structure, smash it into a member function type.
61049d3b
DJ
13622
13623 GCC shouldn't do this; it should just output pointer to member DIEs.
13624 This is GCC PR debug/28767. */
c0dd20ea 13625
0b92b5bb
TT
13626static void
13627quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13628{
09e2d7c7 13629 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13630
13631 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13632 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13633 return;
c0dd20ea
DJ
13634
13635 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13636 if (TYPE_FIELD_NAME (type, 0) == NULL
13637 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13638 || TYPE_FIELD_NAME (type, 1) == NULL
13639 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13640 return;
c0dd20ea
DJ
13641
13642 /* Find the type of the method. */
0b92b5bb 13643 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13644 if (pfn_type == NULL
13645 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13646 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13647 return;
c0dd20ea
DJ
13648
13649 /* Look for the "this" argument. */
13650 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13651 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13652 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13653 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13654 return;
c0dd20ea 13655
09e2d7c7 13656 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13657 new_type = alloc_type (objfile);
09e2d7c7 13658 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13659 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13660 TYPE_VARARGS (pfn_type));
0b92b5bb 13661 smash_to_methodptr_type (type, new_type);
c0dd20ea 13662}
1168df01 13663
685b1105 13664
c906108c 13665/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13666 (definition) to create a type for the structure or union. Fill in
13667 the type's name and general properties; the members will not be
83655187
DE
13668 processed until process_structure_scope. A symbol table entry for
13669 the type will also not be done until process_structure_scope (assuming
13670 the type has a name).
c906108c 13671
c767944b
DJ
13672 NOTE: we need to call these functions regardless of whether or not the
13673 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13674 structure or union. This gets the type entered into our set of
83655187 13675 user defined types. */
c906108c 13676
f792889a 13677static struct type *
134d01f1 13678read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13679{
e7c27a73 13680 struct objfile *objfile = cu->objfile;
c906108c
SS
13681 struct type *type;
13682 struct attribute *attr;
15d034d0 13683 const char *name;
c906108c 13684
348e048f
DE
13685 /* If the definition of this type lives in .debug_types, read that type.
13686 Don't follow DW_AT_specification though, that will take us back up
13687 the chain and we want to go down. */
45e58e77 13688 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13689 if (attr)
13690 {
ac9ec31b 13691 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13692
ac9ec31b 13693 /* The type's CU may not be the same as CU.
02142a6c 13694 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13695 return set_die_type (die, type, cu);
13696 }
13697
c0dd20ea 13698 type = alloc_type (objfile);
c906108c 13699 INIT_CPLUS_SPECIFIC (type);
93311388 13700
39cbfefa
DJ
13701 name = dwarf2_name (die, cu);
13702 if (name != NULL)
c906108c 13703 {
987504bb 13704 if (cu->language == language_cplus
c44af4eb
TT
13705 || cu->language == language_d
13706 || cu->language == language_rust)
63d06c5c 13707 {
15d034d0 13708 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13709
13710 /* dwarf2_full_name might have already finished building the DIE's
13711 type. If so, there is no need to continue. */
13712 if (get_die_type (die, cu) != NULL)
13713 return get_die_type (die, cu);
13714
13715 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13716 if (die->tag == DW_TAG_structure_type
13717 || die->tag == DW_TAG_class_type)
13718 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13719 }
13720 else
13721 {
d8151005
DJ
13722 /* The name is already allocated along with this objfile, so
13723 we don't need to duplicate it for the type. */
7d455152 13724 TYPE_TAG_NAME (type) = name;
94af9270
KS
13725 if (die->tag == DW_TAG_class_type)
13726 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13727 }
c906108c
SS
13728 }
13729
13730 if (die->tag == DW_TAG_structure_type)
13731 {
13732 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13733 }
13734 else if (die->tag == DW_TAG_union_type)
13735 {
13736 TYPE_CODE (type) = TYPE_CODE_UNION;
13737 }
13738 else
13739 {
4753d33b 13740 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13741 }
13742
0cc2414c
TT
13743 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13744 TYPE_DECLARED_CLASS (type) = 1;
13745
e142c38c 13746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13747 if (attr)
13748 {
155bfbd3
JB
13749 if (attr_form_is_constant (attr))
13750 TYPE_LENGTH (type) = DW_UNSND (attr);
13751 else
13752 {
13753 /* For the moment, dynamic type sizes are not supported
13754 by GDB's struct type. The actual size is determined
13755 on-demand when resolving the type of a given object,
13756 so set the type's length to zero for now. Otherwise,
13757 we record an expression as the length, and that expression
13758 could lead to a very large value, which could eventually
13759 lead to us trying to allocate that much memory when creating
13760 a value of that type. */
13761 TYPE_LENGTH (type) = 0;
13762 }
c906108c
SS
13763 }
13764 else
13765 {
13766 TYPE_LENGTH (type) = 0;
13767 }
13768
5230b05a 13769 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 13770 {
5230b05a
WT
13771 /* ICC<14 does not output the required DW_AT_declaration on
13772 incomplete types, but gives them a size of zero. */
422b1cb0 13773 TYPE_STUB (type) = 1;
685b1105
JK
13774 }
13775 else
13776 TYPE_STUB_SUPPORTED (type) = 1;
13777
dc718098 13778 if (die_is_declaration (die, cu))
876cecd0 13779 TYPE_STUB (type) = 1;
a6c727b2
DJ
13780 else if (attr == NULL && die->child == NULL
13781 && producer_is_realview (cu->producer))
13782 /* RealView does not output the required DW_AT_declaration
13783 on incomplete types. */
13784 TYPE_STUB (type) = 1;
dc718098 13785
c906108c
SS
13786 /* We need to add the type field to the die immediately so we don't
13787 infinitely recurse when dealing with pointers to the structure
0963b4bd 13788 type within the structure itself. */
1c379e20 13789 set_die_type (die, type, cu);
c906108c 13790
7e314c57
JK
13791 /* set_die_type should be already done. */
13792 set_descriptive_type (type, die, cu);
13793
c767944b
DJ
13794 return type;
13795}
13796
13797/* Finish creating a structure or union type, including filling in
13798 its members and creating a symbol for it. */
13799
13800static void
13801process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13802{
13803 struct objfile *objfile = cu->objfile;
ca040673 13804 struct die_info *child_die;
c767944b
DJ
13805 struct type *type;
13806
13807 type = get_die_type (die, cu);
13808 if (type == NULL)
13809 type = read_structure_type (die, cu);
13810
e142c38c 13811 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13812 {
13813 struct field_info fi;
34eaf542 13814 VEC (symbolp) *template_args = NULL;
c767944b 13815 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13816
13817 memset (&fi, 0, sizeof (struct field_info));
13818
639d11d3 13819 child_die = die->child;
c906108c
SS
13820
13821 while (child_die && child_die->tag)
13822 {
a9a9bd0f
DC
13823 if (child_die->tag == DW_TAG_member
13824 || child_die->tag == DW_TAG_variable)
c906108c 13825 {
a9a9bd0f
DC
13826 /* NOTE: carlton/2002-11-05: A C++ static data member
13827 should be a DW_TAG_member that is a declaration, but
13828 all versions of G++ as of this writing (so through at
13829 least 3.2.1) incorrectly generate DW_TAG_variable
13830 tags for them instead. */
e7c27a73 13831 dwarf2_add_field (&fi, child_die, cu);
c906108c 13832 }
8713b1b1 13833 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13834 {
e98c9e7c
TT
13835 /* Rust doesn't have member functions in the C++ sense.
13836 However, it does emit ordinary functions as children
13837 of a struct DIE. */
13838 if (cu->language == language_rust)
13839 read_func_scope (child_die, cu);
13840 else
13841 {
13842 /* C++ member function. */
13843 dwarf2_add_member_fn (&fi, child_die, type, cu);
13844 }
c906108c
SS
13845 }
13846 else if (child_die->tag == DW_TAG_inheritance)
13847 {
13848 /* C++ base class field. */
e7c27a73 13849 dwarf2_add_field (&fi, child_die, cu);
c906108c 13850 }
98751a41
JK
13851 else if (child_die->tag == DW_TAG_typedef)
13852 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13853 else if (child_die->tag == DW_TAG_template_type_param
13854 || child_die->tag == DW_TAG_template_value_param)
13855 {
13856 struct symbol *arg = new_symbol (child_die, NULL, cu);
13857
f1078f66
DJ
13858 if (arg != NULL)
13859 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13860 }
13861
c906108c
SS
13862 child_die = sibling_die (child_die);
13863 }
13864
34eaf542
TT
13865 /* Attach template arguments to type. */
13866 if (! VEC_empty (symbolp, template_args))
13867 {
13868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13869 TYPE_N_TEMPLATE_ARGUMENTS (type)
13870 = VEC_length (symbolp, template_args);
13871 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13872 = XOBNEWVEC (&objfile->objfile_obstack,
13873 struct symbol *,
13874 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13875 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13876 VEC_address (symbolp, template_args),
13877 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13878 * sizeof (struct symbol *)));
13879 VEC_free (symbolp, template_args);
13880 }
13881
c906108c
SS
13882 /* Attach fields and member functions to the type. */
13883 if (fi.nfields)
e7c27a73 13884 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13885 if (fi.nfnfields)
13886 {
e7c27a73 13887 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13888
c5aa993b 13889 /* Get the type which refers to the base class (possibly this
c906108c 13890 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13891 class from the DW_AT_containing_type attribute. This use of
13892 DW_AT_containing_type is a GNU extension. */
c906108c 13893
e142c38c 13894 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13895 {
e7c27a73 13896 struct type *t = die_containing_type (die, cu);
c906108c 13897
ae6ae975 13898 set_type_vptr_basetype (type, t);
c906108c
SS
13899 if (type == t)
13900 {
c906108c
SS
13901 int i;
13902
13903 /* Our own class provides vtbl ptr. */
13904 for (i = TYPE_NFIELDS (t) - 1;
13905 i >= TYPE_N_BASECLASSES (t);
13906 --i)
13907 {
0d5cff50 13908 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13909
1168df01 13910 if (is_vtable_name (fieldname, cu))
c906108c 13911 {
ae6ae975 13912 set_type_vptr_fieldno (type, i);
c906108c
SS
13913 break;
13914 }
13915 }
13916
13917 /* Complain if virtual function table field not found. */
13918 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13919 complaint (&symfile_complaints,
3e43a32a
MS
13920 _("virtual function table pointer "
13921 "not found when defining class '%s'"),
4d3c2250
KB
13922 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13923 "");
c906108c
SS
13924 }
13925 else
13926 {
ae6ae975 13927 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13928 }
13929 }
f6235d4c 13930 else if (cu->producer
61012eef 13931 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13932 {
13933 /* The IBM XLC compiler does not provide direct indication
13934 of the containing type, but the vtable pointer is
13935 always named __vfp. */
13936
13937 int i;
13938
13939 for (i = TYPE_NFIELDS (type) - 1;
13940 i >= TYPE_N_BASECLASSES (type);
13941 --i)
13942 {
13943 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13944 {
ae6ae975
DE
13945 set_type_vptr_fieldno (type, i);
13946 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13947 break;
13948 }
13949 }
13950 }
c906108c 13951 }
98751a41
JK
13952
13953 /* Copy fi.typedef_field_list linked list elements content into the
13954 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13955 if (fi.typedef_field_list)
13956 {
13957 int i = fi.typedef_field_list_count;
13958
a0d7a4ff 13959 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13960 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13961 = ((struct typedef_field *)
13962 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13963 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13964
13965 /* Reverse the list order to keep the debug info elements order. */
13966 while (--i >= 0)
13967 {
13968 struct typedef_field *dest, *src;
6e70227d 13969
98751a41
JK
13970 dest = &TYPE_TYPEDEF_FIELD (type, i);
13971 src = &fi.typedef_field_list->field;
13972 fi.typedef_field_list = fi.typedef_field_list->next;
13973 *dest = *src;
13974 }
13975 }
c767944b
DJ
13976
13977 do_cleanups (back_to);
c906108c 13978 }
63d06c5c 13979
bb5ed363 13980 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13981
90aeadfc
DC
13982 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13983 snapshots) has been known to create a die giving a declaration
13984 for a class that has, as a child, a die giving a definition for a
13985 nested class. So we have to process our children even if the
13986 current die is a declaration. Normally, of course, a declaration
13987 won't have any children at all. */
134d01f1 13988
ca040673
DE
13989 child_die = die->child;
13990
90aeadfc
DC
13991 while (child_die != NULL && child_die->tag)
13992 {
13993 if (child_die->tag == DW_TAG_member
13994 || child_die->tag == DW_TAG_variable
34eaf542
TT
13995 || child_die->tag == DW_TAG_inheritance
13996 || child_die->tag == DW_TAG_template_value_param
13997 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13998 {
90aeadfc 13999 /* Do nothing. */
134d01f1 14000 }
90aeadfc
DC
14001 else
14002 process_die (child_die, cu);
134d01f1 14003
90aeadfc 14004 child_die = sibling_die (child_die);
134d01f1
DJ
14005 }
14006
fa4028e9
JB
14007 /* Do not consider external references. According to the DWARF standard,
14008 these DIEs are identified by the fact that they have no byte_size
14009 attribute, and a declaration attribute. */
14010 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14011 || !die_is_declaration (die, cu))
c767944b 14012 new_symbol (die, type, cu);
134d01f1
DJ
14013}
14014
55426c9d
JB
14015/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14016 update TYPE using some information only available in DIE's children. */
14017
14018static void
14019update_enumeration_type_from_children (struct die_info *die,
14020 struct type *type,
14021 struct dwarf2_cu *cu)
14022{
60f7655a 14023 struct die_info *child_die;
55426c9d
JB
14024 int unsigned_enum = 1;
14025 int flag_enum = 1;
14026 ULONGEST mask = 0;
55426c9d 14027
8268c778 14028 auto_obstack obstack;
55426c9d 14029
60f7655a
DE
14030 for (child_die = die->child;
14031 child_die != NULL && child_die->tag;
14032 child_die = sibling_die (child_die))
55426c9d
JB
14033 {
14034 struct attribute *attr;
14035 LONGEST value;
14036 const gdb_byte *bytes;
14037 struct dwarf2_locexpr_baton *baton;
14038 const char *name;
60f7655a 14039
55426c9d
JB
14040 if (child_die->tag != DW_TAG_enumerator)
14041 continue;
14042
14043 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14044 if (attr == NULL)
14045 continue;
14046
14047 name = dwarf2_name (child_die, cu);
14048 if (name == NULL)
14049 name = "<anonymous enumerator>";
14050
14051 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14052 &value, &bytes, &baton);
14053 if (value < 0)
14054 {
14055 unsigned_enum = 0;
14056 flag_enum = 0;
14057 }
14058 else if ((mask & value) != 0)
14059 flag_enum = 0;
14060 else
14061 mask |= value;
14062
14063 /* If we already know that the enum type is neither unsigned, nor
14064 a flag type, no need to look at the rest of the enumerates. */
14065 if (!unsigned_enum && !flag_enum)
14066 break;
55426c9d
JB
14067 }
14068
14069 if (unsigned_enum)
14070 TYPE_UNSIGNED (type) = 1;
14071 if (flag_enum)
14072 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14073}
14074
134d01f1
DJ
14075/* Given a DW_AT_enumeration_type die, set its type. We do not
14076 complete the type's fields yet, or create any symbols. */
c906108c 14077
f792889a 14078static struct type *
134d01f1 14079read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14080{
e7c27a73 14081 struct objfile *objfile = cu->objfile;
c906108c 14082 struct type *type;
c906108c 14083 struct attribute *attr;
0114d602 14084 const char *name;
134d01f1 14085
348e048f
DE
14086 /* If the definition of this type lives in .debug_types, read that type.
14087 Don't follow DW_AT_specification though, that will take us back up
14088 the chain and we want to go down. */
45e58e77 14089 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14090 if (attr)
14091 {
ac9ec31b 14092 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14093
ac9ec31b 14094 /* The type's CU may not be the same as CU.
02142a6c 14095 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14096 return set_die_type (die, type, cu);
14097 }
14098
c906108c
SS
14099 type = alloc_type (objfile);
14100
14101 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14102 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14103 if (name != NULL)
7d455152 14104 TYPE_TAG_NAME (type) = name;
c906108c 14105
0626fc76
TT
14106 attr = dwarf2_attr (die, DW_AT_type, cu);
14107 if (attr != NULL)
14108 {
14109 struct type *underlying_type = die_type (die, cu);
14110
14111 TYPE_TARGET_TYPE (type) = underlying_type;
14112 }
14113
e142c38c 14114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14115 if (attr)
14116 {
14117 TYPE_LENGTH (type) = DW_UNSND (attr);
14118 }
14119 else
14120 {
14121 TYPE_LENGTH (type) = 0;
14122 }
14123
137033e9
JB
14124 /* The enumeration DIE can be incomplete. In Ada, any type can be
14125 declared as private in the package spec, and then defined only
14126 inside the package body. Such types are known as Taft Amendment
14127 Types. When another package uses such a type, an incomplete DIE
14128 may be generated by the compiler. */
02eb380e 14129 if (die_is_declaration (die, cu))
876cecd0 14130 TYPE_STUB (type) = 1;
02eb380e 14131
0626fc76
TT
14132 /* Finish the creation of this type by using the enum's children.
14133 We must call this even when the underlying type has been provided
14134 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14135 update_enumeration_type_from_children (die, type, cu);
14136
0626fc76
TT
14137 /* If this type has an underlying type that is not a stub, then we
14138 may use its attributes. We always use the "unsigned" attribute
14139 in this situation, because ordinarily we guess whether the type
14140 is unsigned -- but the guess can be wrong and the underlying type
14141 can tell us the reality. However, we defer to a local size
14142 attribute if one exists, because this lets the compiler override
14143 the underlying type if needed. */
14144 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14145 {
14146 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14147 if (TYPE_LENGTH (type) == 0)
14148 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14149 }
14150
3d567982
TT
14151 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14152
f792889a 14153 return set_die_type (die, type, cu);
134d01f1
DJ
14154}
14155
14156/* Given a pointer to a die which begins an enumeration, process all
14157 the dies that define the members of the enumeration, and create the
14158 symbol for the enumeration type.
14159
14160 NOTE: We reverse the order of the element list. */
14161
14162static void
14163process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14164{
f792889a 14165 struct type *this_type;
134d01f1 14166
f792889a
DJ
14167 this_type = get_die_type (die, cu);
14168 if (this_type == NULL)
14169 this_type = read_enumeration_type (die, cu);
9dc481d3 14170
639d11d3 14171 if (die->child != NULL)
c906108c 14172 {
9dc481d3
DE
14173 struct die_info *child_die;
14174 struct symbol *sym;
14175 struct field *fields = NULL;
14176 int num_fields = 0;
15d034d0 14177 const char *name;
9dc481d3 14178
639d11d3 14179 child_die = die->child;
c906108c
SS
14180 while (child_die && child_die->tag)
14181 {
14182 if (child_die->tag != DW_TAG_enumerator)
14183 {
e7c27a73 14184 process_die (child_die, cu);
c906108c
SS
14185 }
14186 else
14187 {
39cbfefa
DJ
14188 name = dwarf2_name (child_die, cu);
14189 if (name)
c906108c 14190 {
f792889a 14191 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14192
14193 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14194 {
14195 fields = (struct field *)
14196 xrealloc (fields,
14197 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14198 * sizeof (struct field));
c906108c
SS
14199 }
14200
3567439c 14201 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14202 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14203 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14204 FIELD_BITSIZE (fields[num_fields]) = 0;
14205
14206 num_fields++;
14207 }
14208 }
14209
14210 child_die = sibling_die (child_die);
14211 }
14212
14213 if (num_fields)
14214 {
f792889a
DJ
14215 TYPE_NFIELDS (this_type) = num_fields;
14216 TYPE_FIELDS (this_type) = (struct field *)
14217 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14218 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14219 sizeof (struct field) * num_fields);
b8c9b27d 14220 xfree (fields);
c906108c 14221 }
c906108c 14222 }
134d01f1 14223
6c83ed52
TT
14224 /* If we are reading an enum from a .debug_types unit, and the enum
14225 is a declaration, and the enum is not the signatured type in the
14226 unit, then we do not want to add a symbol for it. Adding a
14227 symbol would in some cases obscure the true definition of the
14228 enum, giving users an incomplete type when the definition is
14229 actually available. Note that we do not want to do this for all
14230 enums which are just declarations, because C++0x allows forward
14231 enum declarations. */
3019eac3 14232 if (cu->per_cu->is_debug_types
6c83ed52
TT
14233 && die_is_declaration (die, cu))
14234 {
52dc124a 14235 struct signatured_type *sig_type;
6c83ed52 14236
c0f78cd4 14237 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14238 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14239 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14240 return;
14241 }
14242
f792889a 14243 new_symbol (die, this_type, cu);
c906108c
SS
14244}
14245
14246/* Extract all information from a DW_TAG_array_type DIE and put it in
14247 the DIE's type field. For now, this only handles one dimensional
14248 arrays. */
14249
f792889a 14250static struct type *
e7c27a73 14251read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14252{
e7c27a73 14253 struct objfile *objfile = cu->objfile;
c906108c 14254 struct die_info *child_die;
7e314c57 14255 struct type *type;
c906108c 14256 struct type *element_type, *range_type, *index_type;
c906108c 14257 struct attribute *attr;
15d034d0 14258 const char *name;
dc53a7ad 14259 unsigned int bit_stride = 0;
c906108c 14260
e7c27a73 14261 element_type = die_type (die, cu);
c906108c 14262
7e314c57
JK
14263 /* The die_type call above may have already set the type for this DIE. */
14264 type = get_die_type (die, cu);
14265 if (type)
14266 return type;
14267
dc53a7ad
JB
14268 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14269 if (attr != NULL)
14270 bit_stride = DW_UNSND (attr) * 8;
14271
14272 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14273 if (attr != NULL)
14274 bit_stride = DW_UNSND (attr);
14275
c906108c
SS
14276 /* Irix 6.2 native cc creates array types without children for
14277 arrays with unspecified length. */
639d11d3 14278 if (die->child == NULL)
c906108c 14279 {
46bf5051 14280 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14281 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14282 type = create_array_type_with_stride (NULL, element_type, range_type,
14283 bit_stride);
f792889a 14284 return set_die_type (die, type, cu);
c906108c
SS
14285 }
14286
791afaa2 14287 std::vector<struct type *> range_types;
639d11d3 14288 child_die = die->child;
c906108c
SS
14289 while (child_die && child_die->tag)
14290 {
14291 if (child_die->tag == DW_TAG_subrange_type)
14292 {
f792889a 14293 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14294
f792889a 14295 if (child_type != NULL)
a02abb62 14296 {
0963b4bd
MS
14297 /* The range type was succesfully read. Save it for the
14298 array type creation. */
791afaa2 14299 range_types.push_back (child_type);
a02abb62 14300 }
c906108c
SS
14301 }
14302 child_die = sibling_die (child_die);
14303 }
14304
14305 /* Dwarf2 dimensions are output from left to right, create the
14306 necessary array types in backwards order. */
7ca2d3a3 14307
c906108c 14308 type = element_type;
7ca2d3a3
DL
14309
14310 if (read_array_order (die, cu) == DW_ORD_col_major)
14311 {
14312 int i = 0;
9a619af0 14313
791afaa2 14314 while (i < range_types.size ())
dc53a7ad
JB
14315 type = create_array_type_with_stride (NULL, type, range_types[i++],
14316 bit_stride);
7ca2d3a3
DL
14317 }
14318 else
14319 {
791afaa2 14320 size_t ndim = range_types.size ();
7ca2d3a3 14321 while (ndim-- > 0)
dc53a7ad
JB
14322 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14323 bit_stride);
7ca2d3a3 14324 }
c906108c 14325
f5f8a009
EZ
14326 /* Understand Dwarf2 support for vector types (like they occur on
14327 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14328 array type. This is not part of the Dwarf2/3 standard yet, but a
14329 custom vendor extension. The main difference between a regular
14330 array and the vector variant is that vectors are passed by value
14331 to functions. */
e142c38c 14332 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14333 if (attr)
ea37ba09 14334 make_vector_type (type);
f5f8a009 14335
dbc98a8b
KW
14336 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14337 implementation may choose to implement triple vectors using this
14338 attribute. */
14339 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14340 if (attr)
14341 {
14342 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14343 TYPE_LENGTH (type) = DW_UNSND (attr);
14344 else
3e43a32a
MS
14345 complaint (&symfile_complaints,
14346 _("DW_AT_byte_size for array type smaller "
14347 "than the total size of elements"));
dbc98a8b
KW
14348 }
14349
39cbfefa
DJ
14350 name = dwarf2_name (die, cu);
14351 if (name)
14352 TYPE_NAME (type) = name;
6e70227d 14353
0963b4bd 14354 /* Install the type in the die. */
7e314c57
JK
14355 set_die_type (die, type, cu);
14356
14357 /* set_die_type should be already done. */
b4ba55a1
JB
14358 set_descriptive_type (type, die, cu);
14359
7e314c57 14360 return type;
c906108c
SS
14361}
14362
7ca2d3a3 14363static enum dwarf_array_dim_ordering
6e70227d 14364read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14365{
14366 struct attribute *attr;
14367
14368 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14369
aead7601
SM
14370 if (attr)
14371 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14372
0963b4bd
MS
14373 /* GNU F77 is a special case, as at 08/2004 array type info is the
14374 opposite order to the dwarf2 specification, but data is still
14375 laid out as per normal fortran.
7ca2d3a3 14376
0963b4bd
MS
14377 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14378 version checking. */
7ca2d3a3 14379
905e0470
PM
14380 if (cu->language == language_fortran
14381 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14382 {
14383 return DW_ORD_row_major;
14384 }
14385
6e70227d 14386 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14387 {
14388 case array_column_major:
14389 return DW_ORD_col_major;
14390 case array_row_major:
14391 default:
14392 return DW_ORD_row_major;
14393 };
14394}
14395
72019c9c 14396/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14397 the DIE's type field. */
72019c9c 14398
f792889a 14399static struct type *
72019c9c
GM
14400read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14401{
7e314c57
JK
14402 struct type *domain_type, *set_type;
14403 struct attribute *attr;
f792889a 14404
7e314c57
JK
14405 domain_type = die_type (die, cu);
14406
14407 /* The die_type call above may have already set the type for this DIE. */
14408 set_type = get_die_type (die, cu);
14409 if (set_type)
14410 return set_type;
14411
14412 set_type = create_set_type (NULL, domain_type);
14413
14414 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14415 if (attr)
14416 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14417
f792889a 14418 return set_die_type (die, set_type, cu);
72019c9c 14419}
7ca2d3a3 14420
0971de02
TT
14421/* A helper for read_common_block that creates a locexpr baton.
14422 SYM is the symbol which we are marking as computed.
14423 COMMON_DIE is the DIE for the common block.
14424 COMMON_LOC is the location expression attribute for the common
14425 block itself.
14426 MEMBER_LOC is the location expression attribute for the particular
14427 member of the common block that we are processing.
14428 CU is the CU from which the above come. */
14429
14430static void
14431mark_common_block_symbol_computed (struct symbol *sym,
14432 struct die_info *common_die,
14433 struct attribute *common_loc,
14434 struct attribute *member_loc,
14435 struct dwarf2_cu *cu)
14436{
14437 struct objfile *objfile = dwarf2_per_objfile->objfile;
14438 struct dwarf2_locexpr_baton *baton;
14439 gdb_byte *ptr;
14440 unsigned int cu_off;
14441 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14442 LONGEST offset = 0;
14443
14444 gdb_assert (common_loc && member_loc);
14445 gdb_assert (attr_form_is_block (common_loc));
14446 gdb_assert (attr_form_is_block (member_loc)
14447 || attr_form_is_constant (member_loc));
14448
8d749320 14449 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14450 baton->per_cu = cu->per_cu;
14451 gdb_assert (baton->per_cu);
14452
14453 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14454
14455 if (attr_form_is_constant (member_loc))
14456 {
14457 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14458 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14459 }
14460 else
14461 baton->size += DW_BLOCK (member_loc)->size;
14462
224c3ddb 14463 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14464 baton->data = ptr;
14465
14466 *ptr++ = DW_OP_call4;
9c541725 14467 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14468 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14469 ptr += 4;
14470
14471 if (attr_form_is_constant (member_loc))
14472 {
14473 *ptr++ = DW_OP_addr;
14474 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14475 ptr += cu->header.addr_size;
14476 }
14477 else
14478 {
14479 /* We have to copy the data here, because DW_OP_call4 will only
14480 use a DW_AT_location attribute. */
14481 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14482 ptr += DW_BLOCK (member_loc)->size;
14483 }
14484
14485 *ptr++ = DW_OP_plus;
14486 gdb_assert (ptr - baton->data == baton->size);
14487
0971de02 14488 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14489 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14490}
14491
4357ac6c
TT
14492/* Create appropriate locally-scoped variables for all the
14493 DW_TAG_common_block entries. Also create a struct common_block
14494 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14495 is used to sepate the common blocks name namespace from regular
14496 variable names. */
c906108c
SS
14497
14498static void
e7c27a73 14499read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14500{
0971de02
TT
14501 struct attribute *attr;
14502
14503 attr = dwarf2_attr (die, DW_AT_location, cu);
14504 if (attr)
14505 {
14506 /* Support the .debug_loc offsets. */
14507 if (attr_form_is_block (attr))
14508 {
14509 /* Ok. */
14510 }
14511 else if (attr_form_is_section_offset (attr))
14512 {
14513 dwarf2_complex_location_expr_complaint ();
14514 attr = NULL;
14515 }
14516 else
14517 {
14518 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14519 "common block member");
14520 attr = NULL;
14521 }
14522 }
14523
639d11d3 14524 if (die->child != NULL)
c906108c 14525 {
4357ac6c
TT
14526 struct objfile *objfile = cu->objfile;
14527 struct die_info *child_die;
14528 size_t n_entries = 0, size;
14529 struct common_block *common_block;
14530 struct symbol *sym;
74ac6d43 14531
4357ac6c
TT
14532 for (child_die = die->child;
14533 child_die && child_die->tag;
14534 child_die = sibling_die (child_die))
14535 ++n_entries;
14536
14537 size = (sizeof (struct common_block)
14538 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14539 common_block
14540 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14541 size);
4357ac6c
TT
14542 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14543 common_block->n_entries = 0;
14544
14545 for (child_die = die->child;
14546 child_die && child_die->tag;
14547 child_die = sibling_die (child_die))
14548 {
14549 /* Create the symbol in the DW_TAG_common_block block in the current
14550 symbol scope. */
e7c27a73 14551 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14552 if (sym != NULL)
14553 {
14554 struct attribute *member_loc;
14555
14556 common_block->contents[common_block->n_entries++] = sym;
14557
14558 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14559 cu);
14560 if (member_loc)
14561 {
14562 /* GDB has handled this for a long time, but it is
14563 not specified by DWARF. It seems to have been
14564 emitted by gfortran at least as recently as:
14565 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14566 complaint (&symfile_complaints,
14567 _("Variable in common block has "
14568 "DW_AT_data_member_location "
14569 "- DIE at 0x%x [in module %s]"),
9c541725 14570 to_underlying (child_die->sect_off),
4262abfb 14571 objfile_name (cu->objfile));
0971de02
TT
14572
14573 if (attr_form_is_section_offset (member_loc))
14574 dwarf2_complex_location_expr_complaint ();
14575 else if (attr_form_is_constant (member_loc)
14576 || attr_form_is_block (member_loc))
14577 {
14578 if (attr)
14579 mark_common_block_symbol_computed (sym, die, attr,
14580 member_loc, cu);
14581 }
14582 else
14583 dwarf2_complex_location_expr_complaint ();
14584 }
14585 }
c906108c 14586 }
4357ac6c
TT
14587
14588 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14589 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14590 }
14591}
14592
0114d602 14593/* Create a type for a C++ namespace. */
d9fa45fe 14594
0114d602
DJ
14595static struct type *
14596read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14597{
e7c27a73 14598 struct objfile *objfile = cu->objfile;
0114d602 14599 const char *previous_prefix, *name;
9219021c 14600 int is_anonymous;
0114d602
DJ
14601 struct type *type;
14602
14603 /* For extensions, reuse the type of the original namespace. */
14604 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14605 {
14606 struct die_info *ext_die;
14607 struct dwarf2_cu *ext_cu = cu;
9a619af0 14608
0114d602
DJ
14609 ext_die = dwarf2_extension (die, &ext_cu);
14610 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14611
14612 /* EXT_CU may not be the same as CU.
02142a6c 14613 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14614 return set_die_type (die, type, cu);
14615 }
9219021c 14616
e142c38c 14617 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14618
14619 /* Now build the name of the current namespace. */
14620
0114d602
DJ
14621 previous_prefix = determine_prefix (die, cu);
14622 if (previous_prefix[0] != '\0')
14623 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14624 previous_prefix, name, 0, cu);
0114d602
DJ
14625
14626 /* Create the type. */
19f392bc 14627 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14628 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14629
60531b24 14630 return set_die_type (die, type, cu);
0114d602
DJ
14631}
14632
22cee43f 14633/* Read a namespace scope. */
0114d602
DJ
14634
14635static void
14636read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14637{
14638 struct objfile *objfile = cu->objfile;
0114d602 14639 int is_anonymous;
9219021c 14640
5c4e30ca
DC
14641 /* Add a symbol associated to this if we haven't seen the namespace
14642 before. Also, add a using directive if it's an anonymous
14643 namespace. */
9219021c 14644
f2f0e013 14645 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14646 {
14647 struct type *type;
14648
0114d602 14649 type = read_type_die (die, cu);
e7c27a73 14650 new_symbol (die, type, cu);
5c4e30ca 14651
e8e80198 14652 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14653 if (is_anonymous)
0114d602
DJ
14654 {
14655 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14656
eb1e02fd 14657 std::vector<const char *> excludes;
22cee43f
PMR
14658 add_using_directive (using_directives (cu->language),
14659 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 14660 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 14661 }
5c4e30ca 14662 }
9219021c 14663
639d11d3 14664 if (die->child != NULL)
d9fa45fe 14665 {
639d11d3 14666 struct die_info *child_die = die->child;
6e70227d 14667
d9fa45fe
DC
14668 while (child_die && child_die->tag)
14669 {
e7c27a73 14670 process_die (child_die, cu);
d9fa45fe
DC
14671 child_die = sibling_die (child_die);
14672 }
14673 }
38d518c9
EZ
14674}
14675
f55ee35c
JK
14676/* Read a Fortran module as type. This DIE can be only a declaration used for
14677 imported module. Still we need that type as local Fortran "use ... only"
14678 declaration imports depend on the created type in determine_prefix. */
14679
14680static struct type *
14681read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14682{
14683 struct objfile *objfile = cu->objfile;
15d034d0 14684 const char *module_name;
f55ee35c
JK
14685 struct type *type;
14686
14687 module_name = dwarf2_name (die, cu);
14688 if (!module_name)
3e43a32a
MS
14689 complaint (&symfile_complaints,
14690 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14691 to_underlying (die->sect_off));
19f392bc 14692 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14693
14694 /* determine_prefix uses TYPE_TAG_NAME. */
14695 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14696
14697 return set_die_type (die, type, cu);
14698}
14699
5d7cb8df
JK
14700/* Read a Fortran module. */
14701
14702static void
14703read_module (struct die_info *die, struct dwarf2_cu *cu)
14704{
14705 struct die_info *child_die = die->child;
530e8392
KB
14706 struct type *type;
14707
14708 type = read_type_die (die, cu);
14709 new_symbol (die, type, cu);
5d7cb8df 14710
5d7cb8df
JK
14711 while (child_die && child_die->tag)
14712 {
14713 process_die (child_die, cu);
14714 child_die = sibling_die (child_die);
14715 }
14716}
14717
38d518c9
EZ
14718/* Return the name of the namespace represented by DIE. Set
14719 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14720 namespace. */
14721
14722static const char *
e142c38c 14723namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14724{
14725 struct die_info *current_die;
14726 const char *name = NULL;
14727
14728 /* Loop through the extensions until we find a name. */
14729
14730 for (current_die = die;
14731 current_die != NULL;
f2f0e013 14732 current_die = dwarf2_extension (die, &cu))
38d518c9 14733 {
96553a0c
DE
14734 /* We don't use dwarf2_name here so that we can detect the absence
14735 of a name -> anonymous namespace. */
7d45c7c3 14736 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14737
38d518c9
EZ
14738 if (name != NULL)
14739 break;
14740 }
14741
14742 /* Is it an anonymous namespace? */
14743
14744 *is_anonymous = (name == NULL);
14745 if (*is_anonymous)
2b1dbab0 14746 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14747
14748 return name;
d9fa45fe
DC
14749}
14750
c906108c
SS
14751/* Extract all information from a DW_TAG_pointer_type DIE and add to
14752 the user defined type vector. */
14753
f792889a 14754static struct type *
e7c27a73 14755read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14756{
5e2b427d 14757 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14758 struct comp_unit_head *cu_header = &cu->header;
c906108c 14759 struct type *type;
8b2dbe47
KB
14760 struct attribute *attr_byte_size;
14761 struct attribute *attr_address_class;
14762 int byte_size, addr_class;
7e314c57
JK
14763 struct type *target_type;
14764
14765 target_type = die_type (die, cu);
c906108c 14766
7e314c57
JK
14767 /* The die_type call above may have already set the type for this DIE. */
14768 type = get_die_type (die, cu);
14769 if (type)
14770 return type;
14771
14772 type = lookup_pointer_type (target_type);
8b2dbe47 14773
e142c38c 14774 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14775 if (attr_byte_size)
14776 byte_size = DW_UNSND (attr_byte_size);
c906108c 14777 else
8b2dbe47
KB
14778 byte_size = cu_header->addr_size;
14779
e142c38c 14780 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14781 if (attr_address_class)
14782 addr_class = DW_UNSND (attr_address_class);
14783 else
14784 addr_class = DW_ADDR_none;
14785
14786 /* If the pointer size or address class is different than the
14787 default, create a type variant marked as such and set the
14788 length accordingly. */
14789 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14790 {
5e2b427d 14791 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14792 {
14793 int type_flags;
14794
849957d9 14795 type_flags = gdbarch_address_class_type_flags
5e2b427d 14796 (gdbarch, byte_size, addr_class);
876cecd0
TT
14797 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14798 == 0);
8b2dbe47
KB
14799 type = make_type_with_address_space (type, type_flags);
14800 }
14801 else if (TYPE_LENGTH (type) != byte_size)
14802 {
3e43a32a
MS
14803 complaint (&symfile_complaints,
14804 _("invalid pointer size %d"), byte_size);
8b2dbe47 14805 }
6e70227d 14806 else
9a619af0
MS
14807 {
14808 /* Should we also complain about unhandled address classes? */
14809 }
c906108c 14810 }
8b2dbe47
KB
14811
14812 TYPE_LENGTH (type) = byte_size;
f792889a 14813 return set_die_type (die, type, cu);
c906108c
SS
14814}
14815
14816/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14817 the user defined type vector. */
14818
f792889a 14819static struct type *
e7c27a73 14820read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14821{
14822 struct type *type;
14823 struct type *to_type;
14824 struct type *domain;
14825
e7c27a73
DJ
14826 to_type = die_type (die, cu);
14827 domain = die_containing_type (die, cu);
0d5de010 14828
7e314c57
JK
14829 /* The calls above may have already set the type for this DIE. */
14830 type = get_die_type (die, cu);
14831 if (type)
14832 return type;
14833
0d5de010
DJ
14834 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14835 type = lookup_methodptr_type (to_type);
7078baeb
TT
14836 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14837 {
14838 struct type *new_type = alloc_type (cu->objfile);
14839
14840 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14841 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14842 TYPE_VARARGS (to_type));
14843 type = lookup_methodptr_type (new_type);
14844 }
0d5de010
DJ
14845 else
14846 type = lookup_memberptr_type (to_type, domain);
c906108c 14847
f792889a 14848 return set_die_type (die, type, cu);
c906108c
SS
14849}
14850
4297a3f0 14851/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14852 the user defined type vector. */
14853
f792889a 14854static struct type *
4297a3f0
AV
14855read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14856 enum type_code refcode)
c906108c 14857{
e7c27a73 14858 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14859 struct type *type, *target_type;
c906108c
SS
14860 struct attribute *attr;
14861
4297a3f0
AV
14862 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14863
7e314c57
JK
14864 target_type = die_type (die, cu);
14865
14866 /* The die_type call above may have already set the type for this DIE. */
14867 type = get_die_type (die, cu);
14868 if (type)
14869 return type;
14870
4297a3f0 14871 type = lookup_reference_type (target_type, refcode);
e142c38c 14872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14873 if (attr)
14874 {
14875 TYPE_LENGTH (type) = DW_UNSND (attr);
14876 }
14877 else
14878 {
107d2387 14879 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14880 }
f792889a 14881 return set_die_type (die, type, cu);
c906108c
SS
14882}
14883
cf363f18
MW
14884/* Add the given cv-qualifiers to the element type of the array. GCC
14885 outputs DWARF type qualifiers that apply to an array, not the
14886 element type. But GDB relies on the array element type to carry
14887 the cv-qualifiers. This mimics section 6.7.3 of the C99
14888 specification. */
14889
14890static struct type *
14891add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14892 struct type *base_type, int cnst, int voltl)
14893{
14894 struct type *el_type, *inner_array;
14895
14896 base_type = copy_type (base_type);
14897 inner_array = base_type;
14898
14899 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14900 {
14901 TYPE_TARGET_TYPE (inner_array) =
14902 copy_type (TYPE_TARGET_TYPE (inner_array));
14903 inner_array = TYPE_TARGET_TYPE (inner_array);
14904 }
14905
14906 el_type = TYPE_TARGET_TYPE (inner_array);
14907 cnst |= TYPE_CONST (el_type);
14908 voltl |= TYPE_VOLATILE (el_type);
14909 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14910
14911 return set_die_type (die, base_type, cu);
14912}
14913
f792889a 14914static struct type *
e7c27a73 14915read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14916{
f792889a 14917 struct type *base_type, *cv_type;
c906108c 14918
e7c27a73 14919 base_type = die_type (die, cu);
7e314c57
JK
14920
14921 /* The die_type call above may have already set the type for this DIE. */
14922 cv_type = get_die_type (die, cu);
14923 if (cv_type)
14924 return cv_type;
14925
2f608a3a
KW
14926 /* In case the const qualifier is applied to an array type, the element type
14927 is so qualified, not the array type (section 6.7.3 of C99). */
14928 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14929 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14930
f792889a
DJ
14931 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14932 return set_die_type (die, cv_type, cu);
c906108c
SS
14933}
14934
f792889a 14935static struct type *
e7c27a73 14936read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14937{
f792889a 14938 struct type *base_type, *cv_type;
c906108c 14939
e7c27a73 14940 base_type = die_type (die, cu);
7e314c57
JK
14941
14942 /* The die_type call above may have already set the type for this DIE. */
14943 cv_type = get_die_type (die, cu);
14944 if (cv_type)
14945 return cv_type;
14946
cf363f18
MW
14947 /* In case the volatile qualifier is applied to an array type, the
14948 element type is so qualified, not the array type (section 6.7.3
14949 of C99). */
14950 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14951 return add_array_cv_type (die, cu, base_type, 0, 1);
14952
f792889a
DJ
14953 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14954 return set_die_type (die, cv_type, cu);
c906108c
SS
14955}
14956
06d66ee9
TT
14957/* Handle DW_TAG_restrict_type. */
14958
14959static struct type *
14960read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14961{
14962 struct type *base_type, *cv_type;
14963
14964 base_type = die_type (die, cu);
14965
14966 /* The die_type call above may have already set the type for this DIE. */
14967 cv_type = get_die_type (die, cu);
14968 if (cv_type)
14969 return cv_type;
14970
14971 cv_type = make_restrict_type (base_type);
14972 return set_die_type (die, cv_type, cu);
14973}
14974
a2c2acaf
MW
14975/* Handle DW_TAG_atomic_type. */
14976
14977static struct type *
14978read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14979{
14980 struct type *base_type, *cv_type;
14981
14982 base_type = die_type (die, cu);
14983
14984 /* The die_type call above may have already set the type for this DIE. */
14985 cv_type = get_die_type (die, cu);
14986 if (cv_type)
14987 return cv_type;
14988
14989 cv_type = make_atomic_type (base_type);
14990 return set_die_type (die, cv_type, cu);
14991}
14992
c906108c
SS
14993/* Extract all information from a DW_TAG_string_type DIE and add to
14994 the user defined type vector. It isn't really a user defined type,
14995 but it behaves like one, with other DIE's using an AT_user_def_type
14996 attribute to reference it. */
14997
f792889a 14998static struct type *
e7c27a73 14999read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15000{
e7c27a73 15001 struct objfile *objfile = cu->objfile;
3b7538c0 15002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15003 struct type *type, *range_type, *index_type, *char_type;
15004 struct attribute *attr;
15005 unsigned int length;
15006
e142c38c 15007 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15008 if (attr)
15009 {
15010 length = DW_UNSND (attr);
15011 }
15012 else
15013 {
0963b4bd 15014 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15015 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15016 if (attr)
15017 {
15018 length = DW_UNSND (attr);
15019 }
15020 else
15021 {
15022 length = 1;
15023 }
c906108c 15024 }
6ccb9162 15025
46bf5051 15026 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15027 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15028 char_type = language_string_char_type (cu->language_defn, gdbarch);
15029 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15030
f792889a 15031 return set_die_type (die, type, cu);
c906108c
SS
15032}
15033
4d804846
JB
15034/* Assuming that DIE corresponds to a function, returns nonzero
15035 if the function is prototyped. */
15036
15037static int
15038prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15039{
15040 struct attribute *attr;
15041
15042 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15043 if (attr && (DW_UNSND (attr) != 0))
15044 return 1;
15045
15046 /* The DWARF standard implies that the DW_AT_prototyped attribute
15047 is only meaninful for C, but the concept also extends to other
15048 languages that allow unprototyped functions (Eg: Objective C).
15049 For all other languages, assume that functions are always
15050 prototyped. */
15051 if (cu->language != language_c
15052 && cu->language != language_objc
15053 && cu->language != language_opencl)
15054 return 1;
15055
15056 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15057 prototyped and unprototyped functions; default to prototyped,
15058 since that is more common in modern code (and RealView warns
15059 about unprototyped functions). */
15060 if (producer_is_realview (cu->producer))
15061 return 1;
15062
15063 return 0;
15064}
15065
c906108c
SS
15066/* Handle DIES due to C code like:
15067
15068 struct foo
c5aa993b
JM
15069 {
15070 int (*funcp)(int a, long l);
15071 int b;
15072 };
c906108c 15073
0963b4bd 15074 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15075
f792889a 15076static struct type *
e7c27a73 15077read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15078{
bb5ed363 15079 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15080 struct type *type; /* Type that this function returns. */
15081 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15082 struct attribute *attr;
15083
e7c27a73 15084 type = die_type (die, cu);
7e314c57
JK
15085
15086 /* The die_type call above may have already set the type for this DIE. */
15087 ftype = get_die_type (die, cu);
15088 if (ftype)
15089 return ftype;
15090
0c8b41f1 15091 ftype = lookup_function_type (type);
c906108c 15092
4d804846 15093 if (prototyped_function_p (die, cu))
a6c727b2 15094 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15095
c055b101
CV
15096 /* Store the calling convention in the type if it's available in
15097 the subroutine die. Otherwise set the calling convention to
15098 the default value DW_CC_normal. */
15099 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15100 if (attr)
15101 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15102 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15103 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15104 else
15105 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15106
743649fd
MW
15107 /* Record whether the function returns normally to its caller or not
15108 if the DWARF producer set that information. */
15109 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15110 if (attr && (DW_UNSND (attr) != 0))
15111 TYPE_NO_RETURN (ftype) = 1;
15112
76c10ea2
GM
15113 /* We need to add the subroutine type to the die immediately so
15114 we don't infinitely recurse when dealing with parameters
0963b4bd 15115 declared as the same subroutine type. */
76c10ea2 15116 set_die_type (die, ftype, cu);
6e70227d 15117
639d11d3 15118 if (die->child != NULL)
c906108c 15119 {
bb5ed363 15120 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15121 struct die_info *child_die;
8072405b 15122 int nparams, iparams;
c906108c
SS
15123
15124 /* Count the number of parameters.
15125 FIXME: GDB currently ignores vararg functions, but knows about
15126 vararg member functions. */
8072405b 15127 nparams = 0;
639d11d3 15128 child_die = die->child;
c906108c
SS
15129 while (child_die && child_die->tag)
15130 {
15131 if (child_die->tag == DW_TAG_formal_parameter)
15132 nparams++;
15133 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15134 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15135 child_die = sibling_die (child_die);
15136 }
15137
15138 /* Allocate storage for parameters and fill them in. */
15139 TYPE_NFIELDS (ftype) = nparams;
15140 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15141 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15142
8072405b
JK
15143 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15144 even if we error out during the parameters reading below. */
15145 for (iparams = 0; iparams < nparams; iparams++)
15146 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15147
15148 iparams = 0;
639d11d3 15149 child_die = die->child;
c906108c
SS
15150 while (child_die && child_die->tag)
15151 {
15152 if (child_die->tag == DW_TAG_formal_parameter)
15153 {
3ce3b1ba
PA
15154 struct type *arg_type;
15155
15156 /* DWARF version 2 has no clean way to discern C++
15157 static and non-static member functions. G++ helps
15158 GDB by marking the first parameter for non-static
15159 member functions (which is the this pointer) as
15160 artificial. We pass this information to
15161 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15162
15163 DWARF version 3 added DW_AT_object_pointer, which GCC
15164 4.5 does not yet generate. */
e142c38c 15165 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15166 if (attr)
15167 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15168 else
9c37b5ae 15169 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15170 arg_type = die_type (child_die, cu);
15171
15172 /* RealView does not mark THIS as const, which the testsuite
15173 expects. GCC marks THIS as const in method definitions,
15174 but not in the class specifications (GCC PR 43053). */
15175 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15176 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15177 {
15178 int is_this = 0;
15179 struct dwarf2_cu *arg_cu = cu;
15180 const char *name = dwarf2_name (child_die, cu);
15181
15182 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15183 if (attr)
15184 {
15185 /* If the compiler emits this, use it. */
15186 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15187 is_this = 1;
15188 }
15189 else if (name && strcmp (name, "this") == 0)
15190 /* Function definitions will have the argument names. */
15191 is_this = 1;
15192 else if (name == NULL && iparams == 0)
15193 /* Declarations may not have the names, so like
15194 elsewhere in GDB, assume an artificial first
15195 argument is "this". */
15196 is_this = 1;
15197
15198 if (is_this)
15199 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15200 arg_type, 0);
15201 }
15202
15203 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15204 iparams++;
15205 }
15206 child_die = sibling_die (child_die);
15207 }
15208 }
15209
76c10ea2 15210 return ftype;
c906108c
SS
15211}
15212
f792889a 15213static struct type *
e7c27a73 15214read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15215{
e7c27a73 15216 struct objfile *objfile = cu->objfile;
0114d602 15217 const char *name = NULL;
3c8e0968 15218 struct type *this_type, *target_type;
c906108c 15219
94af9270 15220 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15221 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15222 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15223 set_die_type (die, this_type, cu);
3c8e0968
DE
15224 target_type = die_type (die, cu);
15225 if (target_type != this_type)
15226 TYPE_TARGET_TYPE (this_type) = target_type;
15227 else
15228 {
15229 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15230 spec and cause infinite loops in GDB. */
15231 complaint (&symfile_complaints,
15232 _("Self-referential DW_TAG_typedef "
15233 "- DIE at 0x%x [in module %s]"),
9c541725 15234 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15235 TYPE_TARGET_TYPE (this_type) = NULL;
15236 }
f792889a 15237 return this_type;
c906108c
SS
15238}
15239
9b790ce7
UW
15240/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15241 (which may be different from NAME) to the architecture back-end to allow
15242 it to guess the correct format if necessary. */
15243
15244static struct type *
15245dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15246 const char *name_hint)
15247{
15248 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15249 const struct floatformat **format;
15250 struct type *type;
15251
15252 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15253 if (format)
15254 type = init_float_type (objfile, bits, name, format);
15255 else
77b7c781 15256 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
15257
15258 return type;
15259}
15260
c906108c
SS
15261/* Find a representation of a given base type and install
15262 it in the TYPE field of the die. */
15263
f792889a 15264static struct type *
e7c27a73 15265read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15266{
e7c27a73 15267 struct objfile *objfile = cu->objfile;
c906108c
SS
15268 struct type *type;
15269 struct attribute *attr;
19f392bc 15270 int encoding = 0, bits = 0;
15d034d0 15271 const char *name;
c906108c 15272
e142c38c 15273 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15274 if (attr)
15275 {
15276 encoding = DW_UNSND (attr);
15277 }
e142c38c 15278 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15279 if (attr)
15280 {
19f392bc 15281 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15282 }
39cbfefa 15283 name = dwarf2_name (die, cu);
6ccb9162 15284 if (!name)
c906108c 15285 {
6ccb9162
UW
15286 complaint (&symfile_complaints,
15287 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15288 }
6ccb9162
UW
15289
15290 switch (encoding)
c906108c 15291 {
6ccb9162
UW
15292 case DW_ATE_address:
15293 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 15294 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 15295 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15296 break;
15297 case DW_ATE_boolean:
19f392bc 15298 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15299 break;
15300 case DW_ATE_complex_float:
9b790ce7 15301 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15302 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15303 break;
15304 case DW_ATE_decimal_float:
19f392bc 15305 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15306 break;
15307 case DW_ATE_float:
9b790ce7 15308 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15309 break;
15310 case DW_ATE_signed:
19f392bc 15311 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15312 break;
15313 case DW_ATE_unsigned:
3b2b8fea
TT
15314 if (cu->language == language_fortran
15315 && name
61012eef 15316 && startswith (name, "character("))
19f392bc
UW
15317 type = init_character_type (objfile, bits, 1, name);
15318 else
15319 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15320 break;
15321 case DW_ATE_signed_char:
6e70227d 15322 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15323 || cu->language == language_pascal
15324 || cu->language == language_fortran)
19f392bc
UW
15325 type = init_character_type (objfile, bits, 0, name);
15326 else
15327 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15328 break;
15329 case DW_ATE_unsigned_char:
868a0084 15330 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15331 || cu->language == language_pascal
c44af4eb
TT
15332 || cu->language == language_fortran
15333 || cu->language == language_rust)
19f392bc
UW
15334 type = init_character_type (objfile, bits, 1, name);
15335 else
15336 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15337 break;
75079b2b 15338 case DW_ATE_UTF:
53e710ac
PA
15339 {
15340 gdbarch *arch = get_objfile_arch (objfile);
15341
15342 if (bits == 16)
15343 type = builtin_type (arch)->builtin_char16;
15344 else if (bits == 32)
15345 type = builtin_type (arch)->builtin_char32;
15346 else
15347 {
15348 complaint (&symfile_complaints,
15349 _("unsupported DW_ATE_UTF bit size: '%d'"),
15350 bits);
15351 type = init_integer_type (objfile, bits, 1, name);
15352 }
15353 return set_die_type (die, type, cu);
15354 }
75079b2b
TT
15355 break;
15356
6ccb9162
UW
15357 default:
15358 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15359 dwarf_type_encoding_name (encoding));
77b7c781 15360 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 15361 break;
c906108c 15362 }
6ccb9162 15363
0114d602 15364 if (name && strcmp (name, "char") == 0)
876cecd0 15365 TYPE_NOSIGN (type) = 1;
0114d602 15366
f792889a 15367 return set_die_type (die, type, cu);
c906108c
SS
15368}
15369
80180f79
SA
15370/* Parse dwarf attribute if it's a block, reference or constant and put the
15371 resulting value of the attribute into struct bound_prop.
15372 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15373
15374static int
15375attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15376 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15377{
15378 struct dwarf2_property_baton *baton;
15379 struct obstack *obstack = &cu->objfile->objfile_obstack;
15380
15381 if (attr == NULL || prop == NULL)
15382 return 0;
15383
15384 if (attr_form_is_block (attr))
15385 {
8d749320 15386 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15387 baton->referenced_type = NULL;
15388 baton->locexpr.per_cu = cu->per_cu;
15389 baton->locexpr.size = DW_BLOCK (attr)->size;
15390 baton->locexpr.data = DW_BLOCK (attr)->data;
15391 prop->data.baton = baton;
15392 prop->kind = PROP_LOCEXPR;
15393 gdb_assert (prop->data.baton != NULL);
15394 }
15395 else if (attr_form_is_ref (attr))
15396 {
15397 struct dwarf2_cu *target_cu = cu;
15398 struct die_info *target_die;
15399 struct attribute *target_attr;
15400
15401 target_die = follow_die_ref (die, attr, &target_cu);
15402 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15403 if (target_attr == NULL)
15404 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15405 target_cu);
80180f79
SA
15406 if (target_attr == NULL)
15407 return 0;
15408
df25ebbd 15409 switch (target_attr->name)
80180f79 15410 {
df25ebbd
JB
15411 case DW_AT_location:
15412 if (attr_form_is_section_offset (target_attr))
15413 {
8d749320 15414 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15415 baton->referenced_type = die_type (target_die, target_cu);
15416 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15417 prop->data.baton = baton;
15418 prop->kind = PROP_LOCLIST;
15419 gdb_assert (prop->data.baton != NULL);
15420 }
15421 else if (attr_form_is_block (target_attr))
15422 {
8d749320 15423 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15424 baton->referenced_type = die_type (target_die, target_cu);
15425 baton->locexpr.per_cu = cu->per_cu;
15426 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15427 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15428 prop->data.baton = baton;
15429 prop->kind = PROP_LOCEXPR;
15430 gdb_assert (prop->data.baton != NULL);
15431 }
15432 else
15433 {
15434 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15435 "dynamic property");
15436 return 0;
15437 }
15438 break;
15439 case DW_AT_data_member_location:
15440 {
15441 LONGEST offset;
15442
15443 if (!handle_data_member_location (target_die, target_cu,
15444 &offset))
15445 return 0;
15446
8d749320 15447 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15448 baton->referenced_type = read_type_die (target_die->parent,
15449 target_cu);
df25ebbd
JB
15450 baton->offset_info.offset = offset;
15451 baton->offset_info.type = die_type (target_die, target_cu);
15452 prop->data.baton = baton;
15453 prop->kind = PROP_ADDR_OFFSET;
15454 break;
15455 }
80180f79
SA
15456 }
15457 }
15458 else if (attr_form_is_constant (attr))
15459 {
15460 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15461 prop->kind = PROP_CONST;
15462 }
15463 else
15464 {
15465 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15466 dwarf2_name (die, cu));
15467 return 0;
15468 }
15469
15470 return 1;
15471}
15472
a02abb62
JB
15473/* Read the given DW_AT_subrange DIE. */
15474
f792889a 15475static struct type *
a02abb62
JB
15476read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15477{
4c9ad8c2 15478 struct type *base_type, *orig_base_type;
a02abb62
JB
15479 struct type *range_type;
15480 struct attribute *attr;
729efb13 15481 struct dynamic_prop low, high;
4fae6e18 15482 int low_default_is_valid;
c451ebe5 15483 int high_bound_is_count = 0;
15d034d0 15484 const char *name;
43bbcdc2 15485 LONGEST negative_mask;
e77813c8 15486
4c9ad8c2
TT
15487 orig_base_type = die_type (die, cu);
15488 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15489 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15490 creating the range type, but we use the result of check_typedef
15491 when examining properties of the type. */
15492 base_type = check_typedef (orig_base_type);
a02abb62 15493
7e314c57
JK
15494 /* The die_type call above may have already set the type for this DIE. */
15495 range_type = get_die_type (die, cu);
15496 if (range_type)
15497 return range_type;
15498
729efb13
SA
15499 low.kind = PROP_CONST;
15500 high.kind = PROP_CONST;
15501 high.data.const_val = 0;
15502
4fae6e18
JK
15503 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15504 omitting DW_AT_lower_bound. */
15505 switch (cu->language)
6e70227d 15506 {
4fae6e18
JK
15507 case language_c:
15508 case language_cplus:
729efb13 15509 low.data.const_val = 0;
4fae6e18
JK
15510 low_default_is_valid = 1;
15511 break;
15512 case language_fortran:
729efb13 15513 low.data.const_val = 1;
4fae6e18
JK
15514 low_default_is_valid = 1;
15515 break;
15516 case language_d:
4fae6e18 15517 case language_objc:
c44af4eb 15518 case language_rust:
729efb13 15519 low.data.const_val = 0;
4fae6e18
JK
15520 low_default_is_valid = (cu->header.version >= 4);
15521 break;
15522 case language_ada:
15523 case language_m2:
15524 case language_pascal:
729efb13 15525 low.data.const_val = 1;
4fae6e18
JK
15526 low_default_is_valid = (cu->header.version >= 4);
15527 break;
15528 default:
729efb13 15529 low.data.const_val = 0;
4fae6e18
JK
15530 low_default_is_valid = 0;
15531 break;
a02abb62
JB
15532 }
15533
e142c38c 15534 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15535 if (attr)
11c1ba78 15536 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15537 else if (!low_default_is_valid)
15538 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15539 "- DIE at 0x%x [in module %s]"),
9c541725 15540 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15541
e142c38c 15542 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15543 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15544 {
15545 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15546 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15547 {
c451ebe5
SA
15548 /* If bounds are constant do the final calculation here. */
15549 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15550 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15551 else
15552 high_bound_is_count = 1;
c2ff108b 15553 }
e77813c8
PM
15554 }
15555
15556 /* Dwarf-2 specifications explicitly allows to create subrange types
15557 without specifying a base type.
15558 In that case, the base type must be set to the type of
15559 the lower bound, upper bound or count, in that order, if any of these
15560 three attributes references an object that has a type.
15561 If no base type is found, the Dwarf-2 specifications say that
15562 a signed integer type of size equal to the size of an address should
15563 be used.
15564 For the following C code: `extern char gdb_int [];'
15565 GCC produces an empty range DIE.
15566 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15567 high bound or count are not yet handled by this code. */
e77813c8
PM
15568 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15569 {
15570 struct objfile *objfile = cu->objfile;
15571 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15572 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15573 struct type *int_type = objfile_type (objfile)->builtin_int;
15574
15575 /* Test "int", "long int", and "long long int" objfile types,
15576 and select the first one having a size above or equal to the
15577 architecture address size. */
15578 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15579 base_type = int_type;
15580 else
15581 {
15582 int_type = objfile_type (objfile)->builtin_long;
15583 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15584 base_type = int_type;
15585 else
15586 {
15587 int_type = objfile_type (objfile)->builtin_long_long;
15588 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15589 base_type = int_type;
15590 }
15591 }
15592 }
a02abb62 15593
dbb9c2b1
JB
15594 /* Normally, the DWARF producers are expected to use a signed
15595 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15596 But this is unfortunately not always the case, as witnessed
15597 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15598 is used instead. To work around that ambiguity, we treat
15599 the bounds as signed, and thus sign-extend their values, when
15600 the base type is signed. */
6e70227d 15601 negative_mask =
66c6502d 15602 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15603 if (low.kind == PROP_CONST
15604 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15605 low.data.const_val |= negative_mask;
15606 if (high.kind == PROP_CONST
15607 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15608 high.data.const_val |= negative_mask;
43bbcdc2 15609
729efb13 15610 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15611
c451ebe5
SA
15612 if (high_bound_is_count)
15613 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15614
c2ff108b
JK
15615 /* Ada expects an empty array on no boundary attributes. */
15616 if (attr == NULL && cu->language != language_ada)
729efb13 15617 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15618
39cbfefa
DJ
15619 name = dwarf2_name (die, cu);
15620 if (name)
15621 TYPE_NAME (range_type) = name;
6e70227d 15622
e142c38c 15623 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15624 if (attr)
15625 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15626
7e314c57
JK
15627 set_die_type (die, range_type, cu);
15628
15629 /* set_die_type should be already done. */
b4ba55a1
JB
15630 set_descriptive_type (range_type, die, cu);
15631
7e314c57 15632 return range_type;
a02abb62 15633}
6e70227d 15634
f792889a 15635static struct type *
81a17f79
JB
15636read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15637{
15638 struct type *type;
81a17f79 15639
81a17f79
JB
15640 /* For now, we only support the C meaning of an unspecified type: void. */
15641
19f392bc 15642 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15643 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15644
f792889a 15645 return set_die_type (die, type, cu);
81a17f79 15646}
a02abb62 15647
639d11d3
DC
15648/* Read a single die and all its descendents. Set the die's sibling
15649 field to NULL; set other fields in the die correctly, and set all
15650 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15651 location of the info_ptr after reading all of those dies. PARENT
15652 is the parent of the die in question. */
15653
15654static struct die_info *
dee91e82 15655read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15656 const gdb_byte *info_ptr,
15657 const gdb_byte **new_info_ptr,
dee91e82 15658 struct die_info *parent)
639d11d3
DC
15659{
15660 struct die_info *die;
d521ce57 15661 const gdb_byte *cur_ptr;
639d11d3
DC
15662 int has_children;
15663
bf6af496 15664 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15665 if (die == NULL)
15666 {
15667 *new_info_ptr = cur_ptr;
15668 return NULL;
15669 }
93311388 15670 store_in_ref_table (die, reader->cu);
639d11d3
DC
15671
15672 if (has_children)
bf6af496 15673 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15674 else
15675 {
15676 die->child = NULL;
15677 *new_info_ptr = cur_ptr;
15678 }
15679
15680 die->sibling = NULL;
15681 die->parent = parent;
15682 return die;
15683}
15684
15685/* Read a die, all of its descendents, and all of its siblings; set
15686 all of the fields of all of the dies correctly. Arguments are as
15687 in read_die_and_children. */
15688
15689static struct die_info *
bf6af496 15690read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15691 const gdb_byte *info_ptr,
15692 const gdb_byte **new_info_ptr,
bf6af496 15693 struct die_info *parent)
639d11d3
DC
15694{
15695 struct die_info *first_die, *last_sibling;
d521ce57 15696 const gdb_byte *cur_ptr;
639d11d3 15697
c906108c 15698 cur_ptr = info_ptr;
639d11d3
DC
15699 first_die = last_sibling = NULL;
15700
15701 while (1)
c906108c 15702 {
639d11d3 15703 struct die_info *die
dee91e82 15704 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15705
1d325ec1 15706 if (die == NULL)
c906108c 15707 {
639d11d3
DC
15708 *new_info_ptr = cur_ptr;
15709 return first_die;
c906108c 15710 }
1d325ec1
DJ
15711
15712 if (!first_die)
15713 first_die = die;
c906108c 15714 else
1d325ec1
DJ
15715 last_sibling->sibling = die;
15716
15717 last_sibling = die;
c906108c 15718 }
c906108c
SS
15719}
15720
bf6af496
DE
15721/* Read a die, all of its descendents, and all of its siblings; set
15722 all of the fields of all of the dies correctly. Arguments are as
15723 in read_die_and_children.
15724 This the main entry point for reading a DIE and all its children. */
15725
15726static struct die_info *
15727read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15728 const gdb_byte *info_ptr,
15729 const gdb_byte **new_info_ptr,
bf6af496
DE
15730 struct die_info *parent)
15731{
15732 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15733 new_info_ptr, parent);
15734
b4f54984 15735 if (dwarf_die_debug)
bf6af496
DE
15736 {
15737 fprintf_unfiltered (gdb_stdlog,
15738 "Read die from %s@0x%x of %s:\n",
a32a8923 15739 get_section_name (reader->die_section),
bf6af496
DE
15740 (unsigned) (info_ptr - reader->die_section->buffer),
15741 bfd_get_filename (reader->abfd));
b4f54984 15742 dump_die (die, dwarf_die_debug);
bf6af496
DE
15743 }
15744
15745 return die;
15746}
15747
3019eac3
DE
15748/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15749 attributes.
15750 The caller is responsible for filling in the extra attributes
15751 and updating (*DIEP)->num_attrs.
15752 Set DIEP to point to a newly allocated die with its information,
15753 except for its child, sibling, and parent fields.
15754 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15755
d521ce57 15756static const gdb_byte *
3019eac3 15757read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15758 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15759 int *has_children, int num_extra_attrs)
93311388 15760{
b64f50a1 15761 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15762 struct abbrev_info *abbrev;
15763 struct die_info *die;
15764 struct dwarf2_cu *cu = reader->cu;
15765 bfd *abfd = reader->abfd;
15766
9c541725 15767 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15768 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15769 info_ptr += bytes_read;
15770 if (!abbrev_number)
15771 {
15772 *diep = NULL;
15773 *has_children = 0;
15774 return info_ptr;
15775 }
15776
433df2d4 15777 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15778 if (!abbrev)
348e048f
DE
15779 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15780 abbrev_number,
15781 bfd_get_filename (abfd));
15782
3019eac3 15783 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15784 die->sect_off = sect_off;
93311388
DE
15785 die->tag = abbrev->tag;
15786 die->abbrev = abbrev_number;
15787
3019eac3
DE
15788 /* Make the result usable.
15789 The caller needs to update num_attrs after adding the extra
15790 attributes. */
93311388
DE
15791 die->num_attrs = abbrev->num_attrs;
15792
15793 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15794 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15795 info_ptr);
93311388
DE
15796
15797 *diep = die;
15798 *has_children = abbrev->has_children;
15799 return info_ptr;
15800}
15801
3019eac3
DE
15802/* Read a die and all its attributes.
15803 Set DIEP to point to a newly allocated die with its information,
15804 except for its child, sibling, and parent fields.
15805 Set HAS_CHILDREN to tell whether the die has children or not. */
15806
d521ce57 15807static const gdb_byte *
3019eac3 15808read_full_die (const struct die_reader_specs *reader,
d521ce57 15809 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15810 int *has_children)
15811{
d521ce57 15812 const gdb_byte *result;
bf6af496
DE
15813
15814 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15815
b4f54984 15816 if (dwarf_die_debug)
bf6af496
DE
15817 {
15818 fprintf_unfiltered (gdb_stdlog,
15819 "Read die from %s@0x%x of %s:\n",
a32a8923 15820 get_section_name (reader->die_section),
bf6af496
DE
15821 (unsigned) (info_ptr - reader->die_section->buffer),
15822 bfd_get_filename (reader->abfd));
b4f54984 15823 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15824 }
15825
15826 return result;
3019eac3 15827}
433df2d4
DE
15828\f
15829/* Abbreviation tables.
3019eac3 15830
433df2d4 15831 In DWARF version 2, the description of the debugging information is
c906108c
SS
15832 stored in a separate .debug_abbrev section. Before we read any
15833 dies from a section we read in all abbreviations and install them
433df2d4
DE
15834 in a hash table. */
15835
15836/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15837
15838static struct abbrev_info *
15839abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15840{
15841 struct abbrev_info *abbrev;
15842
8d749320 15843 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15844 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15845
433df2d4
DE
15846 return abbrev;
15847}
15848
15849/* Add an abbreviation to the table. */
c906108c
SS
15850
15851static void
433df2d4
DE
15852abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15853 unsigned int abbrev_number,
15854 struct abbrev_info *abbrev)
15855{
15856 unsigned int hash_number;
15857
15858 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15859 abbrev->next = abbrev_table->abbrevs[hash_number];
15860 abbrev_table->abbrevs[hash_number] = abbrev;
15861}
dee91e82 15862
433df2d4
DE
15863/* Look up an abbrev in the table.
15864 Returns NULL if the abbrev is not found. */
15865
15866static struct abbrev_info *
15867abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15868 unsigned int abbrev_number)
c906108c 15869{
433df2d4
DE
15870 unsigned int hash_number;
15871 struct abbrev_info *abbrev;
15872
15873 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15874 abbrev = abbrev_table->abbrevs[hash_number];
15875
15876 while (abbrev)
15877 {
15878 if (abbrev->number == abbrev_number)
15879 return abbrev;
15880 abbrev = abbrev->next;
15881 }
15882 return NULL;
15883}
15884
15885/* Read in an abbrev table. */
15886
15887static struct abbrev_table *
15888abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15889 sect_offset sect_off)
433df2d4
DE
15890{
15891 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15892 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15893 struct abbrev_table *abbrev_table;
d521ce57 15894 const gdb_byte *abbrev_ptr;
c906108c
SS
15895 struct abbrev_info *cur_abbrev;
15896 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15897 unsigned int abbrev_form;
f3dd6933
DJ
15898 struct attr_abbrev *cur_attrs;
15899 unsigned int allocated_attrs;
c906108c 15900
70ba0933 15901 abbrev_table = XNEW (struct abbrev_table);
9c541725 15902 abbrev_table->sect_off = sect_off;
433df2d4 15903 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15904 abbrev_table->abbrevs =
15905 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15906 ABBREV_HASH_SIZE);
433df2d4
DE
15907 memset (abbrev_table->abbrevs, 0,
15908 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15909
433df2d4 15910 dwarf2_read_section (objfile, section);
9c541725 15911 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15912 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15913 abbrev_ptr += bytes_read;
15914
f3dd6933 15915 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15916 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15917
0963b4bd 15918 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15919 while (abbrev_number)
15920 {
433df2d4 15921 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15922
15923 /* read in abbrev header */
15924 cur_abbrev->number = abbrev_number;
aead7601
SM
15925 cur_abbrev->tag
15926 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15927 abbrev_ptr += bytes_read;
15928 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15929 abbrev_ptr += 1;
15930
15931 /* now read in declarations */
22d2f3ab 15932 for (;;)
c906108c 15933 {
43988095
JK
15934 LONGEST implicit_const;
15935
22d2f3ab
JK
15936 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15937 abbrev_ptr += bytes_read;
15938 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15939 abbrev_ptr += bytes_read;
43988095
JK
15940 if (abbrev_form == DW_FORM_implicit_const)
15941 {
15942 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15943 &bytes_read);
15944 abbrev_ptr += bytes_read;
15945 }
15946 else
15947 {
15948 /* Initialize it due to a false compiler warning. */
15949 implicit_const = -1;
15950 }
22d2f3ab
JK
15951
15952 if (abbrev_name == 0)
15953 break;
15954
f3dd6933 15955 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15956 {
f3dd6933
DJ
15957 allocated_attrs += ATTR_ALLOC_CHUNK;
15958 cur_attrs
224c3ddb 15959 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15960 }
ae038cb0 15961
aead7601
SM
15962 cur_attrs[cur_abbrev->num_attrs].name
15963 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15964 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15965 = (enum dwarf_form) abbrev_form;
43988095 15966 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15967 ++cur_abbrev->num_attrs;
c906108c
SS
15968 }
15969
8d749320
SM
15970 cur_abbrev->attrs =
15971 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15972 cur_abbrev->num_attrs);
f3dd6933
DJ
15973 memcpy (cur_abbrev->attrs, cur_attrs,
15974 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15975
433df2d4 15976 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15977
15978 /* Get next abbreviation.
15979 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15980 always properly terminated with an abbrev number of 0.
15981 Exit loop if we encounter an abbreviation which we have
15982 already read (which means we are about to read the abbreviations
15983 for the next compile unit) or if the end of the abbreviation
15984 table is reached. */
433df2d4 15985 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15986 break;
15987 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15988 abbrev_ptr += bytes_read;
433df2d4 15989 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15990 break;
15991 }
f3dd6933
DJ
15992
15993 xfree (cur_attrs);
433df2d4 15994 return abbrev_table;
c906108c
SS
15995}
15996
433df2d4 15997/* Free the resources held by ABBREV_TABLE. */
c906108c 15998
c906108c 15999static void
433df2d4 16000abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16001{
433df2d4
DE
16002 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16003 xfree (abbrev_table);
c906108c
SS
16004}
16005
f4dc4d17
DE
16006/* Same as abbrev_table_free but as a cleanup.
16007 We pass in a pointer to the pointer to the table so that we can
16008 set the pointer to NULL when we're done. It also simplifies
73051182 16009 build_type_psymtabs_1. */
f4dc4d17
DE
16010
16011static void
16012abbrev_table_free_cleanup (void *table_ptr)
16013{
9a3c8263 16014 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16015
16016 if (*abbrev_table_ptr != NULL)
16017 abbrev_table_free (*abbrev_table_ptr);
16018 *abbrev_table_ptr = NULL;
16019}
16020
433df2d4
DE
16021/* Read the abbrev table for CU from ABBREV_SECTION. */
16022
16023static void
16024dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16025 struct dwarf2_section_info *abbrev_section)
c906108c 16026{
433df2d4 16027 cu->abbrev_table =
9c541725 16028 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16029}
c906108c 16030
433df2d4 16031/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16032
433df2d4
DE
16033static void
16034dwarf2_free_abbrev_table (void *ptr_to_cu)
16035{
9a3c8263 16036 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16037
a2ce51a0
DE
16038 if (cu->abbrev_table != NULL)
16039 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16040 /* Set this to NULL so that we SEGV if we try to read it later,
16041 and also because free_comp_unit verifies this is NULL. */
16042 cu->abbrev_table = NULL;
16043}
16044\f
72bf9492
DJ
16045/* Returns nonzero if TAG represents a type that we might generate a partial
16046 symbol for. */
16047
16048static int
16049is_type_tag_for_partial (int tag)
16050{
16051 switch (tag)
16052 {
16053#if 0
16054 /* Some types that would be reasonable to generate partial symbols for,
16055 that we don't at present. */
16056 case DW_TAG_array_type:
16057 case DW_TAG_file_type:
16058 case DW_TAG_ptr_to_member_type:
16059 case DW_TAG_set_type:
16060 case DW_TAG_string_type:
16061 case DW_TAG_subroutine_type:
16062#endif
16063 case DW_TAG_base_type:
16064 case DW_TAG_class_type:
680b30c7 16065 case DW_TAG_interface_type:
72bf9492
DJ
16066 case DW_TAG_enumeration_type:
16067 case DW_TAG_structure_type:
16068 case DW_TAG_subrange_type:
16069 case DW_TAG_typedef:
16070 case DW_TAG_union_type:
16071 return 1;
16072 default:
16073 return 0;
16074 }
16075}
16076
16077/* Load all DIEs that are interesting for partial symbols into memory. */
16078
16079static struct partial_die_info *
dee91e82 16080load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16081 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16082{
dee91e82 16083 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16084 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16085 struct partial_die_info *part_die;
16086 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16087 struct abbrev_info *abbrev;
16088 unsigned int bytes_read;
5afb4e99 16089 unsigned int load_all = 0;
72bf9492
DJ
16090 int nesting_level = 1;
16091
16092 parent_die = NULL;
16093 last_die = NULL;
16094
7adf1e79
DE
16095 gdb_assert (cu->per_cu != NULL);
16096 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16097 load_all = 1;
16098
72bf9492
DJ
16099 cu->partial_dies
16100 = htab_create_alloc_ex (cu->header.length / 12,
16101 partial_die_hash,
16102 partial_die_eq,
16103 NULL,
16104 &cu->comp_unit_obstack,
16105 hashtab_obstack_allocate,
16106 dummy_obstack_deallocate);
16107
8d749320 16108 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16109
16110 while (1)
16111 {
16112 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16113
16114 /* A NULL abbrev means the end of a series of children. */
16115 if (abbrev == NULL)
16116 {
16117 if (--nesting_level == 0)
16118 {
16119 /* PART_DIE was probably the last thing allocated on the
16120 comp_unit_obstack, so we could call obstack_free
16121 here. We don't do that because the waste is small,
16122 and will be cleaned up when we're done with this
16123 compilation unit. This way, we're also more robust
16124 against other users of the comp_unit_obstack. */
16125 return first_die;
16126 }
16127 info_ptr += bytes_read;
16128 last_die = parent_die;
16129 parent_die = parent_die->die_parent;
16130 continue;
16131 }
16132
98bfdba5
PA
16133 /* Check for template arguments. We never save these; if
16134 they're seen, we just mark the parent, and go on our way. */
16135 if (parent_die != NULL
16136 && cu->language == language_cplus
16137 && (abbrev->tag == DW_TAG_template_type_param
16138 || abbrev->tag == DW_TAG_template_value_param))
16139 {
16140 parent_die->has_template_arguments = 1;
16141
16142 if (!load_all)
16143 {
16144 /* We don't need a partial DIE for the template argument. */
dee91e82 16145 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16146 continue;
16147 }
16148 }
16149
0d99eb77 16150 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16151 Skip their other children. */
16152 if (!load_all
16153 && cu->language == language_cplus
16154 && parent_die != NULL
16155 && parent_die->tag == DW_TAG_subprogram)
16156 {
dee91e82 16157 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16158 continue;
16159 }
16160
5afb4e99
DJ
16161 /* Check whether this DIE is interesting enough to save. Normally
16162 we would not be interested in members here, but there may be
16163 later variables referencing them via DW_AT_specification (for
16164 static members). */
16165 if (!load_all
16166 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16167 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16168 && abbrev->tag != DW_TAG_enumerator
16169 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16170 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16171 && abbrev->tag != DW_TAG_variable
5afb4e99 16172 && abbrev->tag != DW_TAG_namespace
f55ee35c 16173 && abbrev->tag != DW_TAG_module
95554aad 16174 && abbrev->tag != DW_TAG_member
74921315
KS
16175 && abbrev->tag != DW_TAG_imported_unit
16176 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16177 {
16178 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16179 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16180 continue;
16181 }
16182
dee91e82
DE
16183 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16184 info_ptr);
72bf9492
DJ
16185
16186 /* This two-pass algorithm for processing partial symbols has a
16187 high cost in cache pressure. Thus, handle some simple cases
16188 here which cover the majority of C partial symbols. DIEs
16189 which neither have specification tags in them, nor could have
16190 specification tags elsewhere pointing at them, can simply be
16191 processed and discarded.
16192
16193 This segment is also optional; scan_partial_symbols and
16194 add_partial_symbol will handle these DIEs if we chain
16195 them in normally. When compilers which do not emit large
16196 quantities of duplicate debug information are more common,
16197 this code can probably be removed. */
16198
16199 /* Any complete simple types at the top level (pretty much all
16200 of them, for a language without namespaces), can be processed
16201 directly. */
16202 if (parent_die == NULL
16203 && part_die->has_specification == 0
16204 && part_die->is_declaration == 0
d8228535 16205 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16206 || part_die->tag == DW_TAG_base_type
16207 || part_die->tag == DW_TAG_subrange_type))
16208 {
16209 if (building_psymtab && part_die->name != NULL)
04a679b8 16210 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16211 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16212 &objfile->static_psymbols,
1762568f 16213 0, cu->language, objfile);
dee91e82 16214 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16215 continue;
16216 }
16217
d8228535
JK
16218 /* The exception for DW_TAG_typedef with has_children above is
16219 a workaround of GCC PR debug/47510. In the case of this complaint
16220 type_name_no_tag_or_error will error on such types later.
16221
16222 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16223 it could not find the child DIEs referenced later, this is checked
16224 above. In correct DWARF DW_TAG_typedef should have no children. */
16225
16226 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16227 complaint (&symfile_complaints,
16228 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16229 "- DIE at 0x%x [in module %s]"),
9c541725 16230 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16231
72bf9492
DJ
16232 /* If we're at the second level, and we're an enumerator, and
16233 our parent has no specification (meaning possibly lives in a
16234 namespace elsewhere), then we can add the partial symbol now
16235 instead of queueing it. */
16236 if (part_die->tag == DW_TAG_enumerator
16237 && parent_die != NULL
16238 && parent_die->die_parent == NULL
16239 && parent_die->tag == DW_TAG_enumeration_type
16240 && parent_die->has_specification == 0)
16241 {
16242 if (part_die->name == NULL)
3e43a32a
MS
16243 complaint (&symfile_complaints,
16244 _("malformed enumerator DIE ignored"));
72bf9492 16245 else if (building_psymtab)
04a679b8 16246 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16247 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16248 cu->language == language_cplus
bb5ed363
DE
16249 ? &objfile->global_psymbols
16250 : &objfile->static_psymbols,
1762568f 16251 0, cu->language, objfile);
72bf9492 16252
dee91e82 16253 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16254 continue;
16255 }
16256
16257 /* We'll save this DIE so link it in. */
16258 part_die->die_parent = parent_die;
16259 part_die->die_sibling = NULL;
16260 part_die->die_child = NULL;
16261
16262 if (last_die && last_die == parent_die)
16263 last_die->die_child = part_die;
16264 else if (last_die)
16265 last_die->die_sibling = part_die;
16266
16267 last_die = part_die;
16268
16269 if (first_die == NULL)
16270 first_die = part_die;
16271
16272 /* Maybe add the DIE to the hash table. Not all DIEs that we
16273 find interesting need to be in the hash table, because we
16274 also have the parent/sibling/child chains; only those that we
16275 might refer to by offset later during partial symbol reading.
16276
16277 For now this means things that might have be the target of a
16278 DW_AT_specification, DW_AT_abstract_origin, or
16279 DW_AT_extension. DW_AT_extension will refer only to
16280 namespaces; DW_AT_abstract_origin refers to functions (and
16281 many things under the function DIE, but we do not recurse
16282 into function DIEs during partial symbol reading) and
16283 possibly variables as well; DW_AT_specification refers to
16284 declarations. Declarations ought to have the DW_AT_declaration
16285 flag. It happens that GCC forgets to put it in sometimes, but
16286 only for functions, not for types.
16287
16288 Adding more things than necessary to the hash table is harmless
16289 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16290 wasted time in find_partial_die, when we reread the compilation
16291 unit with load_all_dies set. */
72bf9492 16292
5afb4e99 16293 if (load_all
72929c62 16294 || abbrev->tag == DW_TAG_constant
5afb4e99 16295 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16296 || abbrev->tag == DW_TAG_variable
16297 || abbrev->tag == DW_TAG_namespace
16298 || part_die->is_declaration)
16299 {
16300 void **slot;
16301
16302 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16303 to_underlying (part_die->sect_off),
16304 INSERT);
72bf9492
DJ
16305 *slot = part_die;
16306 }
16307
8d749320 16308 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16309
16310 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16311 we have no reason to follow the children of structures; for other
98bfdba5
PA
16312 languages we have to, so that we can get at method physnames
16313 to infer fully qualified class names, for DW_AT_specification,
16314 and for C++ template arguments. For C++, we also look one level
16315 inside functions to find template arguments (if the name of the
16316 function does not already contain the template arguments).
bc30ff58
JB
16317
16318 For Ada, we need to scan the children of subprograms and lexical
16319 blocks as well because Ada allows the definition of nested
16320 entities that could be interesting for the debugger, such as
16321 nested subprograms for instance. */
72bf9492 16322 if (last_die->has_children
5afb4e99
DJ
16323 && (load_all
16324 || last_die->tag == DW_TAG_namespace
f55ee35c 16325 || last_die->tag == DW_TAG_module
72bf9492 16326 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16327 || (cu->language == language_cplus
16328 && last_die->tag == DW_TAG_subprogram
16329 && (last_die->name == NULL
16330 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16331 || (cu->language != language_c
16332 && (last_die->tag == DW_TAG_class_type
680b30c7 16333 || last_die->tag == DW_TAG_interface_type
72bf9492 16334 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16335 || last_die->tag == DW_TAG_union_type))
16336 || (cu->language == language_ada
16337 && (last_die->tag == DW_TAG_subprogram
16338 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16339 {
16340 nesting_level++;
16341 parent_die = last_die;
16342 continue;
16343 }
16344
16345 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16346 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16347
16348 /* Back to the top, do it again. */
16349 }
16350}
16351
c906108c
SS
16352/* Read a minimal amount of information into the minimal die structure. */
16353
d521ce57 16354static const gdb_byte *
dee91e82
DE
16355read_partial_die (const struct die_reader_specs *reader,
16356 struct partial_die_info *part_die,
16357 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16358 const gdb_byte *info_ptr)
c906108c 16359{
dee91e82 16360 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16361 struct objfile *objfile = cu->objfile;
d521ce57 16362 const gdb_byte *buffer = reader->buffer;
fa238c03 16363 unsigned int i;
c906108c 16364 struct attribute attr;
c5aa993b 16365 int has_low_pc_attr = 0;
c906108c 16366 int has_high_pc_attr = 0;
91da1414 16367 int high_pc_relative = 0;
c906108c 16368
72bf9492 16369 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16370
9c541725 16371 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16372
16373 info_ptr += abbrev_len;
16374
16375 if (abbrev == NULL)
16376 return info_ptr;
16377
c906108c
SS
16378 part_die->tag = abbrev->tag;
16379 part_die->has_children = abbrev->has_children;
c906108c
SS
16380
16381 for (i = 0; i < abbrev->num_attrs; ++i)
16382 {
dee91e82 16383 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16384
16385 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16386 partial symbol table. */
c906108c
SS
16387 switch (attr.name)
16388 {
16389 case DW_AT_name:
71c25dea
TT
16390 switch (part_die->tag)
16391 {
16392 case DW_TAG_compile_unit:
95554aad 16393 case DW_TAG_partial_unit:
348e048f 16394 case DW_TAG_type_unit:
71c25dea
TT
16395 /* Compilation units have a DW_AT_name that is a filename, not
16396 a source language identifier. */
16397 case DW_TAG_enumeration_type:
16398 case DW_TAG_enumerator:
16399 /* These tags always have simple identifiers already; no need
16400 to canonicalize them. */
16401 part_die->name = DW_STRING (&attr);
16402 break;
16403 default:
16404 part_die->name
16405 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16406 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16407 break;
16408 }
c906108c 16409 break;
31ef98ae 16410 case DW_AT_linkage_name:
c906108c 16411 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16412 /* Note that both forms of linkage name might appear. We
16413 assume they will be the same, and we only store the last
16414 one we see. */
94af9270
KS
16415 if (cu->language == language_ada)
16416 part_die->name = DW_STRING (&attr);
abc72ce4 16417 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16418 break;
16419 case DW_AT_low_pc:
16420 has_low_pc_attr = 1;
31aa7e4e 16421 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16422 break;
16423 case DW_AT_high_pc:
16424 has_high_pc_attr = 1;
31aa7e4e
JB
16425 part_die->highpc = attr_value_as_address (&attr);
16426 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16427 high_pc_relative = 1;
c906108c
SS
16428 break;
16429 case DW_AT_location:
0963b4bd 16430 /* Support the .debug_loc offsets. */
8e19ed76
PS
16431 if (attr_form_is_block (&attr))
16432 {
95554aad 16433 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16434 }
3690dd37 16435 else if (attr_form_is_section_offset (&attr))
8e19ed76 16436 {
4d3c2250 16437 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16438 }
16439 else
16440 {
4d3c2250
KB
16441 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16442 "partial symbol information");
8e19ed76 16443 }
c906108c 16444 break;
c906108c
SS
16445 case DW_AT_external:
16446 part_die->is_external = DW_UNSND (&attr);
16447 break;
16448 case DW_AT_declaration:
16449 part_die->is_declaration = DW_UNSND (&attr);
16450 break;
16451 case DW_AT_type:
16452 part_die->has_type = 1;
16453 break;
16454 case DW_AT_abstract_origin:
16455 case DW_AT_specification:
72bf9492
DJ
16456 case DW_AT_extension:
16457 part_die->has_specification = 1;
c764a876 16458 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16459 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16460 || cu->per_cu->is_dwz);
c906108c
SS
16461 break;
16462 case DW_AT_sibling:
16463 /* Ignore absolute siblings, they might point outside of
16464 the current compile unit. */
16465 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16466 complaint (&symfile_complaints,
16467 _("ignoring absolute DW_AT_sibling"));
c906108c 16468 else
b9502d3f 16469 {
9c541725
PA
16470 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16471 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16472
16473 if (sibling_ptr < info_ptr)
16474 complaint (&symfile_complaints,
16475 _("DW_AT_sibling points backwards"));
22869d73
KS
16476 else if (sibling_ptr > reader->buffer_end)
16477 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16478 else
16479 part_die->sibling = sibling_ptr;
16480 }
c906108c 16481 break;
fa4028e9
JB
16482 case DW_AT_byte_size:
16483 part_die->has_byte_size = 1;
16484 break;
ff908ebf
AW
16485 case DW_AT_const_value:
16486 part_die->has_const_value = 1;
16487 break;
68511cec
CES
16488 case DW_AT_calling_convention:
16489 /* DWARF doesn't provide a way to identify a program's source-level
16490 entry point. DW_AT_calling_convention attributes are only meant
16491 to describe functions' calling conventions.
16492
16493 However, because it's a necessary piece of information in
0c1b455e
TT
16494 Fortran, and before DWARF 4 DW_CC_program was the only
16495 piece of debugging information whose definition refers to
16496 a 'main program' at all, several compilers marked Fortran
16497 main programs with DW_CC_program --- even when those
16498 functions use the standard calling conventions.
16499
16500 Although DWARF now specifies a way to provide this
16501 information, we support this practice for backward
16502 compatibility. */
68511cec 16503 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16504 && cu->language == language_fortran)
16505 part_die->main_subprogram = 1;
68511cec 16506 break;
481860b3
GB
16507 case DW_AT_inline:
16508 if (DW_UNSND (&attr) == DW_INL_inlined
16509 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16510 part_die->may_be_inlined = 1;
16511 break;
95554aad
TT
16512
16513 case DW_AT_import:
16514 if (part_die->tag == DW_TAG_imported_unit)
36586728 16515 {
9c541725 16516 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16517 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16518 || cu->per_cu->is_dwz);
16519 }
95554aad
TT
16520 break;
16521
0c1b455e
TT
16522 case DW_AT_main_subprogram:
16523 part_die->main_subprogram = DW_UNSND (&attr);
16524 break;
16525
c906108c
SS
16526 default:
16527 break;
16528 }
16529 }
16530
91da1414
MW
16531 if (high_pc_relative)
16532 part_die->highpc += part_die->lowpc;
16533
9373cf26
JK
16534 if (has_low_pc_attr && has_high_pc_attr)
16535 {
16536 /* When using the GNU linker, .gnu.linkonce. sections are used to
16537 eliminate duplicate copies of functions and vtables and such.
16538 The linker will arbitrarily choose one and discard the others.
16539 The AT_*_pc values for such functions refer to local labels in
16540 these sections. If the section from that file was discarded, the
16541 labels are not in the output, so the relocs get a value of 0.
16542 If this is a discarded function, mark the pc bounds as invalid,
16543 so that GDB will ignore it. */
16544 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16545 {
bb5ed363 16546 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16547
16548 complaint (&symfile_complaints,
16549 _("DW_AT_low_pc %s is zero "
16550 "for DIE at 0x%x [in module %s]"),
16551 paddress (gdbarch, part_die->lowpc),
9c541725 16552 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16553 }
16554 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16555 else if (part_die->lowpc >= part_die->highpc)
16556 {
bb5ed363 16557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16558
16559 complaint (&symfile_complaints,
16560 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16561 "for DIE at 0x%x [in module %s]"),
16562 paddress (gdbarch, part_die->lowpc),
16563 paddress (gdbarch, part_die->highpc),
9c541725
PA
16564 to_underlying (part_die->sect_off),
16565 objfile_name (objfile));
9373cf26
JK
16566 }
16567 else
16568 part_die->has_pc_info = 1;
16569 }
85cbf3d3 16570
c906108c
SS
16571 return info_ptr;
16572}
16573
72bf9492
DJ
16574/* Find a cached partial DIE at OFFSET in CU. */
16575
16576static struct partial_die_info *
9c541725 16577find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16578{
16579 struct partial_die_info *lookup_die = NULL;
16580 struct partial_die_info part_die;
16581
9c541725 16582 part_die.sect_off = sect_off;
9a3c8263
SM
16583 lookup_die = ((struct partial_die_info *)
16584 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16585 to_underlying (sect_off)));
72bf9492 16586
72bf9492
DJ
16587 return lookup_die;
16588}
16589
348e048f
DE
16590/* Find a partial DIE at OFFSET, which may or may not be in CU,
16591 except in the case of .debug_types DIEs which do not reference
16592 outside their CU (they do however referencing other types via
55f1336d 16593 DW_FORM_ref_sig8). */
72bf9492
DJ
16594
16595static struct partial_die_info *
9c541725 16596find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16597{
bb5ed363 16598 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16599 struct dwarf2_per_cu_data *per_cu = NULL;
16600 struct partial_die_info *pd = NULL;
72bf9492 16601
36586728 16602 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16603 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16604 {
9c541725 16605 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16606 if (pd != NULL)
16607 return pd;
0d99eb77
DE
16608 /* We missed recording what we needed.
16609 Load all dies and try again. */
16610 per_cu = cu->per_cu;
5afb4e99 16611 }
0d99eb77
DE
16612 else
16613 {
16614 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16615 if (cu->per_cu->is_debug_types)
0d99eb77 16616 {
9c541725
PA
16617 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16618 " external reference to offset 0x%x [in module %s].\n"),
16619 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16620 bfd_get_filename (objfile->obfd));
16621 }
9c541725 16622 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16623 objfile);
72bf9492 16624
0d99eb77
DE
16625 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16626 load_partial_comp_unit (per_cu);
ae038cb0 16627
0d99eb77 16628 per_cu->cu->last_used = 0;
9c541725 16629 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16630 }
5afb4e99 16631
dee91e82
DE
16632 /* If we didn't find it, and not all dies have been loaded,
16633 load them all and try again. */
16634
5afb4e99
DJ
16635 if (pd == NULL && per_cu->load_all_dies == 0)
16636 {
5afb4e99 16637 per_cu->load_all_dies = 1;
fd820528
DE
16638
16639 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16640 THIS_CU->cu may already be in use. So we can't just free it and
16641 replace its DIEs with the ones we read in. Instead, we leave those
16642 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16643 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16644 set. */
dee91e82 16645 load_partial_comp_unit (per_cu);
5afb4e99 16646
9c541725 16647 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16648 }
16649
16650 if (pd == NULL)
16651 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16652 _("could not find partial DIE 0x%x "
16653 "in cache [from module %s]\n"),
9c541725 16654 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16655 return pd;
72bf9492
DJ
16656}
16657
abc72ce4
DE
16658/* See if we can figure out if the class lives in a namespace. We do
16659 this by looking for a member function; its demangled name will
16660 contain namespace info, if there is any. */
16661
16662static void
16663guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16664 struct dwarf2_cu *cu)
16665{
16666 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16667 what template types look like, because the demangler
16668 frequently doesn't give the same name as the debug info. We
16669 could fix this by only using the demangled name to get the
16670 prefix (but see comment in read_structure_type). */
16671
16672 struct partial_die_info *real_pdi;
16673 struct partial_die_info *child_pdi;
16674
16675 /* If this DIE (this DIE's specification, if any) has a parent, then
16676 we should not do this. We'll prepend the parent's fully qualified
16677 name when we create the partial symbol. */
16678
16679 real_pdi = struct_pdi;
16680 while (real_pdi->has_specification)
36586728
TT
16681 real_pdi = find_partial_die (real_pdi->spec_offset,
16682 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16683
16684 if (real_pdi->die_parent != NULL)
16685 return;
16686
16687 for (child_pdi = struct_pdi->die_child;
16688 child_pdi != NULL;
16689 child_pdi = child_pdi->die_sibling)
16690 {
16691 if (child_pdi->tag == DW_TAG_subprogram
16692 && child_pdi->linkage_name != NULL)
16693 {
16694 char *actual_class_name
16695 = language_class_name_from_physname (cu->language_defn,
16696 child_pdi->linkage_name);
16697 if (actual_class_name != NULL)
16698 {
16699 struct_pdi->name
224c3ddb
SM
16700 = ((const char *)
16701 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16702 actual_class_name,
16703 strlen (actual_class_name)));
abc72ce4
DE
16704 xfree (actual_class_name);
16705 }
16706 break;
16707 }
16708 }
16709}
16710
72bf9492
DJ
16711/* Adjust PART_DIE before generating a symbol for it. This function
16712 may set the is_external flag or change the DIE's name. */
16713
16714static void
16715fixup_partial_die (struct partial_die_info *part_die,
16716 struct dwarf2_cu *cu)
16717{
abc72ce4
DE
16718 /* Once we've fixed up a die, there's no point in doing so again.
16719 This also avoids a memory leak if we were to call
16720 guess_partial_die_structure_name multiple times. */
16721 if (part_die->fixup_called)
16722 return;
16723
72bf9492
DJ
16724 /* If we found a reference attribute and the DIE has no name, try
16725 to find a name in the referred to DIE. */
16726
16727 if (part_die->name == NULL && part_die->has_specification)
16728 {
16729 struct partial_die_info *spec_die;
72bf9492 16730
36586728
TT
16731 spec_die = find_partial_die (part_die->spec_offset,
16732 part_die->spec_is_dwz, cu);
72bf9492 16733
10b3939b 16734 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16735
16736 if (spec_die->name)
16737 {
16738 part_die->name = spec_die->name;
16739
16740 /* Copy DW_AT_external attribute if it is set. */
16741 if (spec_die->is_external)
16742 part_die->is_external = spec_die->is_external;
16743 }
16744 }
16745
16746 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16747
16748 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16749 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16750
abc72ce4
DE
16751 /* If there is no parent die to provide a namespace, and there are
16752 children, see if we can determine the namespace from their linkage
122d1940 16753 name. */
abc72ce4 16754 if (cu->language == language_cplus
8b70b953 16755 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16756 && part_die->die_parent == NULL
16757 && part_die->has_children
16758 && (part_die->tag == DW_TAG_class_type
16759 || part_die->tag == DW_TAG_structure_type
16760 || part_die->tag == DW_TAG_union_type))
16761 guess_partial_die_structure_name (part_die, cu);
16762
53832f31
TT
16763 /* GCC might emit a nameless struct or union that has a linkage
16764 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16765 if (part_die->name == NULL
96408a79
SA
16766 && (part_die->tag == DW_TAG_class_type
16767 || part_die->tag == DW_TAG_interface_type
16768 || part_die->tag == DW_TAG_structure_type
16769 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16770 && part_die->linkage_name != NULL)
16771 {
16772 char *demangled;
16773
8de20a37 16774 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16775 if (demangled)
16776 {
96408a79
SA
16777 const char *base;
16778
16779 /* Strip any leading namespaces/classes, keep only the base name.
16780 DW_AT_name for named DIEs does not contain the prefixes. */
16781 base = strrchr (demangled, ':');
16782 if (base && base > demangled && base[-1] == ':')
16783 base++;
16784 else
16785 base = demangled;
16786
34a68019 16787 part_die->name
224c3ddb
SM
16788 = ((const char *)
16789 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16790 base, strlen (base)));
53832f31
TT
16791 xfree (demangled);
16792 }
16793 }
16794
abc72ce4 16795 part_die->fixup_called = 1;
72bf9492
DJ
16796}
16797
a8329558 16798/* Read an attribute value described by an attribute form. */
c906108c 16799
d521ce57 16800static const gdb_byte *
dee91e82
DE
16801read_attribute_value (const struct die_reader_specs *reader,
16802 struct attribute *attr, unsigned form,
43988095 16803 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16804{
dee91e82 16805 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16806 struct objfile *objfile = cu->objfile;
16807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16808 bfd *abfd = reader->abfd;
e7c27a73 16809 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16810 unsigned int bytes_read;
16811 struct dwarf_block *blk;
16812
aead7601 16813 attr->form = (enum dwarf_form) form;
a8329558 16814 switch (form)
c906108c 16815 {
c906108c 16816 case DW_FORM_ref_addr:
ae411497 16817 if (cu->header.version == 2)
4568ecf9 16818 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16819 else
4568ecf9
DE
16820 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16821 &cu->header, &bytes_read);
ae411497
TT
16822 info_ptr += bytes_read;
16823 break;
36586728
TT
16824 case DW_FORM_GNU_ref_alt:
16825 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16826 info_ptr += bytes_read;
16827 break;
ae411497 16828 case DW_FORM_addr:
e7c27a73 16829 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16830 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16831 info_ptr += bytes_read;
c906108c
SS
16832 break;
16833 case DW_FORM_block2:
7b5a2f43 16834 blk = dwarf_alloc_block (cu);
c906108c
SS
16835 blk->size = read_2_bytes (abfd, info_ptr);
16836 info_ptr += 2;
16837 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16838 info_ptr += blk->size;
16839 DW_BLOCK (attr) = blk;
16840 break;
16841 case DW_FORM_block4:
7b5a2f43 16842 blk = dwarf_alloc_block (cu);
c906108c
SS
16843 blk->size = read_4_bytes (abfd, info_ptr);
16844 info_ptr += 4;
16845 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16846 info_ptr += blk->size;
16847 DW_BLOCK (attr) = blk;
16848 break;
16849 case DW_FORM_data2:
16850 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16851 info_ptr += 2;
16852 break;
16853 case DW_FORM_data4:
16854 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16855 info_ptr += 4;
16856 break;
16857 case DW_FORM_data8:
16858 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16859 info_ptr += 8;
16860 break;
0224619f
JK
16861 case DW_FORM_data16:
16862 blk = dwarf_alloc_block (cu);
16863 blk->size = 16;
16864 blk->data = read_n_bytes (abfd, info_ptr, 16);
16865 info_ptr += 16;
16866 DW_BLOCK (attr) = blk;
16867 break;
2dc7f7b3
TT
16868 case DW_FORM_sec_offset:
16869 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16870 info_ptr += bytes_read;
16871 break;
c906108c 16872 case DW_FORM_string:
9b1c24c8 16873 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16874 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16875 info_ptr += bytes_read;
16876 break;
4bdf3d34 16877 case DW_FORM_strp:
36586728
TT
16878 if (!cu->per_cu->is_dwz)
16879 {
16880 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16881 &bytes_read);
16882 DW_STRING_IS_CANONICAL (attr) = 0;
16883 info_ptr += bytes_read;
16884 break;
16885 }
16886 /* FALLTHROUGH */
43988095
JK
16887 case DW_FORM_line_strp:
16888 if (!cu->per_cu->is_dwz)
16889 {
16890 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16891 cu_header, &bytes_read);
16892 DW_STRING_IS_CANONICAL (attr) = 0;
16893 info_ptr += bytes_read;
16894 break;
16895 }
16896 /* FALLTHROUGH */
36586728
TT
16897 case DW_FORM_GNU_strp_alt:
16898 {
16899 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16900 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16901 &bytes_read);
16902
16903 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16904 DW_STRING_IS_CANONICAL (attr) = 0;
16905 info_ptr += bytes_read;
16906 }
4bdf3d34 16907 break;
2dc7f7b3 16908 case DW_FORM_exprloc:
c906108c 16909 case DW_FORM_block:
7b5a2f43 16910 blk = dwarf_alloc_block (cu);
c906108c
SS
16911 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16912 info_ptr += bytes_read;
16913 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16914 info_ptr += blk->size;
16915 DW_BLOCK (attr) = blk;
16916 break;
16917 case DW_FORM_block1:
7b5a2f43 16918 blk = dwarf_alloc_block (cu);
c906108c
SS
16919 blk->size = read_1_byte (abfd, info_ptr);
16920 info_ptr += 1;
16921 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16922 info_ptr += blk->size;
16923 DW_BLOCK (attr) = blk;
16924 break;
16925 case DW_FORM_data1:
16926 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16927 info_ptr += 1;
16928 break;
16929 case DW_FORM_flag:
16930 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16931 info_ptr += 1;
16932 break;
2dc7f7b3
TT
16933 case DW_FORM_flag_present:
16934 DW_UNSND (attr) = 1;
16935 break;
c906108c
SS
16936 case DW_FORM_sdata:
16937 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16938 info_ptr += bytes_read;
16939 break;
16940 case DW_FORM_udata:
16941 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16942 info_ptr += bytes_read;
16943 break;
16944 case DW_FORM_ref1:
9c541725 16945 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16946 + read_1_byte (abfd, info_ptr));
c906108c
SS
16947 info_ptr += 1;
16948 break;
16949 case DW_FORM_ref2:
9c541725 16950 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16951 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16952 info_ptr += 2;
16953 break;
16954 case DW_FORM_ref4:
9c541725 16955 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16956 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16957 info_ptr += 4;
16958 break;
613e1657 16959 case DW_FORM_ref8:
9c541725 16960 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16961 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16962 info_ptr += 8;
16963 break;
55f1336d 16964 case DW_FORM_ref_sig8:
ac9ec31b 16965 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16966 info_ptr += 8;
16967 break;
c906108c 16968 case DW_FORM_ref_udata:
9c541725 16969 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16970 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16971 info_ptr += bytes_read;
16972 break;
c906108c 16973 case DW_FORM_indirect:
a8329558
KW
16974 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16975 info_ptr += bytes_read;
43988095
JK
16976 if (form == DW_FORM_implicit_const)
16977 {
16978 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16979 info_ptr += bytes_read;
16980 }
16981 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16982 info_ptr);
16983 break;
16984 case DW_FORM_implicit_const:
16985 DW_SND (attr) = implicit_const;
a8329558 16986 break;
3019eac3
DE
16987 case DW_FORM_GNU_addr_index:
16988 if (reader->dwo_file == NULL)
16989 {
16990 /* For now flag a hard error.
16991 Later we can turn this into a complaint. */
16992 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16993 dwarf_form_name (form),
16994 bfd_get_filename (abfd));
16995 }
16996 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16997 info_ptr += bytes_read;
16998 break;
16999 case DW_FORM_GNU_str_index:
17000 if (reader->dwo_file == NULL)
17001 {
17002 /* For now flag a hard error.
17003 Later we can turn this into a complaint if warranted. */
17004 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17005 dwarf_form_name (form),
17006 bfd_get_filename (abfd));
17007 }
17008 {
17009 ULONGEST str_index =
17010 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17011
342587c4 17012 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17013 DW_STRING_IS_CANONICAL (attr) = 0;
17014 info_ptr += bytes_read;
17015 }
17016 break;
c906108c 17017 default:
8a3fe4f8 17018 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17019 dwarf_form_name (form),
17020 bfd_get_filename (abfd));
c906108c 17021 }
28e94949 17022
36586728 17023 /* Super hack. */
7771576e 17024 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17025 attr->form = DW_FORM_GNU_ref_alt;
17026
28e94949
JB
17027 /* We have seen instances where the compiler tried to emit a byte
17028 size attribute of -1 which ended up being encoded as an unsigned
17029 0xffffffff. Although 0xffffffff is technically a valid size value,
17030 an object of this size seems pretty unlikely so we can relatively
17031 safely treat these cases as if the size attribute was invalid and
17032 treat them as zero by default. */
17033 if (attr->name == DW_AT_byte_size
17034 && form == DW_FORM_data4
17035 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17036 {
17037 complaint
17038 (&symfile_complaints,
43bbcdc2
PH
17039 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17040 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17041 DW_UNSND (attr) = 0;
17042 }
28e94949 17043
c906108c
SS
17044 return info_ptr;
17045}
17046
a8329558
KW
17047/* Read an attribute described by an abbreviated attribute. */
17048
d521ce57 17049static const gdb_byte *
dee91e82
DE
17050read_attribute (const struct die_reader_specs *reader,
17051 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17052 const gdb_byte *info_ptr)
a8329558
KW
17053{
17054 attr->name = abbrev->name;
43988095
JK
17055 return read_attribute_value (reader, attr, abbrev->form,
17056 abbrev->implicit_const, info_ptr);
a8329558
KW
17057}
17058
0963b4bd 17059/* Read dwarf information from a buffer. */
c906108c
SS
17060
17061static unsigned int
a1855c1d 17062read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17063{
fe1b8b76 17064 return bfd_get_8 (abfd, buf);
c906108c
SS
17065}
17066
17067static int
a1855c1d 17068read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17069{
fe1b8b76 17070 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17071}
17072
17073static unsigned int
a1855c1d 17074read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17075{
fe1b8b76 17076 return bfd_get_16 (abfd, buf);
c906108c
SS
17077}
17078
21ae7a4d 17079static int
a1855c1d 17080read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17081{
17082 return bfd_get_signed_16 (abfd, buf);
17083}
17084
c906108c 17085static unsigned int
a1855c1d 17086read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17087{
fe1b8b76 17088 return bfd_get_32 (abfd, buf);
c906108c
SS
17089}
17090
21ae7a4d 17091static int
a1855c1d 17092read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17093{
17094 return bfd_get_signed_32 (abfd, buf);
17095}
17096
93311388 17097static ULONGEST
a1855c1d 17098read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17099{
fe1b8b76 17100 return bfd_get_64 (abfd, buf);
c906108c
SS
17101}
17102
17103static CORE_ADDR
d521ce57 17104read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17105 unsigned int *bytes_read)
c906108c 17106{
e7c27a73 17107 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17108 CORE_ADDR retval = 0;
17109
107d2387 17110 if (cu_header->signed_addr_p)
c906108c 17111 {
107d2387
AC
17112 switch (cu_header->addr_size)
17113 {
17114 case 2:
fe1b8b76 17115 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17116 break;
17117 case 4:
fe1b8b76 17118 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17119 break;
17120 case 8:
fe1b8b76 17121 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17122 break;
17123 default:
8e65ff28 17124 internal_error (__FILE__, __LINE__,
e2e0b3e5 17125 _("read_address: bad switch, signed [in module %s]"),
659b0389 17126 bfd_get_filename (abfd));
107d2387
AC
17127 }
17128 }
17129 else
17130 {
17131 switch (cu_header->addr_size)
17132 {
17133 case 2:
fe1b8b76 17134 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17135 break;
17136 case 4:
fe1b8b76 17137 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17138 break;
17139 case 8:
fe1b8b76 17140 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17141 break;
17142 default:
8e65ff28 17143 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17144 _("read_address: bad switch, "
17145 "unsigned [in module %s]"),
659b0389 17146 bfd_get_filename (abfd));
107d2387 17147 }
c906108c 17148 }
64367e0a 17149
107d2387
AC
17150 *bytes_read = cu_header->addr_size;
17151 return retval;
c906108c
SS
17152}
17153
f7ef9339 17154/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17155 specification allows the initial length to take up either 4 bytes
17156 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17157 bytes describe the length and all offsets will be 8 bytes in length
17158 instead of 4.
17159
f7ef9339
KB
17160 An older, non-standard 64-bit format is also handled by this
17161 function. The older format in question stores the initial length
17162 as an 8-byte quantity without an escape value. Lengths greater
17163 than 2^32 aren't very common which means that the initial 4 bytes
17164 is almost always zero. Since a length value of zero doesn't make
17165 sense for the 32-bit format, this initial zero can be considered to
17166 be an escape value which indicates the presence of the older 64-bit
17167 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17168 greater than 4GB. If it becomes necessary to handle lengths
17169 somewhat larger than 4GB, we could allow other small values (such
17170 as the non-sensical values of 1, 2, and 3) to also be used as
17171 escape values indicating the presence of the old format.
f7ef9339 17172
917c78fc
MK
17173 The value returned via bytes_read should be used to increment the
17174 relevant pointer after calling read_initial_length().
c764a876 17175
613e1657
KB
17176 [ Note: read_initial_length() and read_offset() are based on the
17177 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17178 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17179 from:
17180
f7ef9339 17181 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17182
613e1657
KB
17183 This document is only a draft and is subject to change. (So beware.)
17184
f7ef9339 17185 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17186 determined empirically by examining 64-bit ELF files produced by
17187 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17188
17189 - Kevin, July 16, 2002
613e1657
KB
17190 ] */
17191
17192static LONGEST
d521ce57 17193read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17194{
fe1b8b76 17195 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17196
dd373385 17197 if (length == 0xffffffff)
613e1657 17198 {
fe1b8b76 17199 length = bfd_get_64 (abfd, buf + 4);
613e1657 17200 *bytes_read = 12;
613e1657 17201 }
dd373385 17202 else if (length == 0)
f7ef9339 17203 {
dd373385 17204 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17205 length = bfd_get_64 (abfd, buf);
f7ef9339 17206 *bytes_read = 8;
f7ef9339 17207 }
613e1657
KB
17208 else
17209 {
17210 *bytes_read = 4;
613e1657
KB
17211 }
17212
c764a876
DE
17213 return length;
17214}
dd373385 17215
c764a876
DE
17216/* Cover function for read_initial_length.
17217 Returns the length of the object at BUF, and stores the size of the
17218 initial length in *BYTES_READ and stores the size that offsets will be in
17219 *OFFSET_SIZE.
17220 If the initial length size is not equivalent to that specified in
17221 CU_HEADER then issue a complaint.
17222 This is useful when reading non-comp-unit headers. */
dd373385 17223
c764a876 17224static LONGEST
d521ce57 17225read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17226 const struct comp_unit_head *cu_header,
17227 unsigned int *bytes_read,
17228 unsigned int *offset_size)
17229{
17230 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17231
17232 gdb_assert (cu_header->initial_length_size == 4
17233 || cu_header->initial_length_size == 8
17234 || cu_header->initial_length_size == 12);
17235
17236 if (cu_header->initial_length_size != *bytes_read)
17237 complaint (&symfile_complaints,
17238 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17239
c764a876 17240 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17241 return length;
613e1657
KB
17242}
17243
17244/* Read an offset from the data stream. The size of the offset is
917c78fc 17245 given by cu_header->offset_size. */
613e1657
KB
17246
17247static LONGEST
d521ce57
TT
17248read_offset (bfd *abfd, const gdb_byte *buf,
17249 const struct comp_unit_head *cu_header,
891d2f0b 17250 unsigned int *bytes_read)
c764a876
DE
17251{
17252 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17253
c764a876
DE
17254 *bytes_read = cu_header->offset_size;
17255 return offset;
17256}
17257
17258/* Read an offset from the data stream. */
17259
17260static LONGEST
d521ce57 17261read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17262{
17263 LONGEST retval = 0;
17264
c764a876 17265 switch (offset_size)
613e1657
KB
17266 {
17267 case 4:
fe1b8b76 17268 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17269 break;
17270 case 8:
fe1b8b76 17271 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17272 break;
17273 default:
8e65ff28 17274 internal_error (__FILE__, __LINE__,
c764a876 17275 _("read_offset_1: bad switch [in module %s]"),
659b0389 17276 bfd_get_filename (abfd));
613e1657
KB
17277 }
17278
917c78fc 17279 return retval;
613e1657
KB
17280}
17281
d521ce57
TT
17282static const gdb_byte *
17283read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17284{
17285 /* If the size of a host char is 8 bits, we can return a pointer
17286 to the buffer, otherwise we have to copy the data to a buffer
17287 allocated on the temporary obstack. */
4bdf3d34 17288 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17289 return buf;
c906108c
SS
17290}
17291
d521ce57
TT
17292static const char *
17293read_direct_string (bfd *abfd, const gdb_byte *buf,
17294 unsigned int *bytes_read_ptr)
c906108c
SS
17295{
17296 /* If the size of a host char is 8 bits, we can return a pointer
17297 to the string, otherwise we have to copy the string to a buffer
17298 allocated on the temporary obstack. */
4bdf3d34 17299 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17300 if (*buf == '\0')
17301 {
17302 *bytes_read_ptr = 1;
17303 return NULL;
17304 }
d521ce57
TT
17305 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17306 return (const char *) buf;
4bdf3d34
JJ
17307}
17308
43988095
JK
17309/* Return pointer to string at section SECT offset STR_OFFSET with error
17310 reporting strings FORM_NAME and SECT_NAME. */
17311
d521ce57 17312static const char *
43988095
JK
17313read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17314 struct dwarf2_section_info *sect,
17315 const char *form_name,
17316 const char *sect_name)
17317{
17318 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17319 if (sect->buffer == NULL)
17320 error (_("%s used without %s section [in module %s]"),
17321 form_name, sect_name, bfd_get_filename (abfd));
17322 if (str_offset >= sect->size)
17323 error (_("%s pointing outside of %s section [in module %s]"),
17324 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17325 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17326 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17327 return NULL;
43988095
JK
17328 return (const char *) (sect->buffer + str_offset);
17329}
17330
17331/* Return pointer to string at .debug_str offset STR_OFFSET. */
17332
17333static const char *
17334read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17335{
17336 return read_indirect_string_at_offset_from (abfd, str_offset,
17337 &dwarf2_per_objfile->str,
17338 "DW_FORM_strp", ".debug_str");
17339}
17340
17341/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17342
17343static const char *
17344read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17345{
17346 return read_indirect_string_at_offset_from (abfd, str_offset,
17347 &dwarf2_per_objfile->line_str,
17348 "DW_FORM_line_strp",
17349 ".debug_line_str");
c906108c
SS
17350}
17351
36586728
TT
17352/* Read a string at offset STR_OFFSET in the .debug_str section from
17353 the .dwz file DWZ. Throw an error if the offset is too large. If
17354 the string consists of a single NUL byte, return NULL; otherwise
17355 return a pointer to the string. */
17356
d521ce57 17357static const char *
36586728
TT
17358read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17359{
17360 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17361
17362 if (dwz->str.buffer == NULL)
17363 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17364 "section [in module %s]"),
17365 bfd_get_filename (dwz->dwz_bfd));
17366 if (str_offset >= dwz->str.size)
17367 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17368 ".debug_str section [in module %s]"),
17369 bfd_get_filename (dwz->dwz_bfd));
17370 gdb_assert (HOST_CHAR_BIT == 8);
17371 if (dwz->str.buffer[str_offset] == '\0')
17372 return NULL;
d521ce57 17373 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17374}
17375
43988095
JK
17376/* Return pointer to string at .debug_str offset as read from BUF.
17377 BUF is assumed to be in a compilation unit described by CU_HEADER.
17378 Return *BYTES_READ_PTR count of bytes read from BUF. */
17379
d521ce57
TT
17380static const char *
17381read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17382 const struct comp_unit_head *cu_header,
17383 unsigned int *bytes_read_ptr)
17384{
17385 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17386
17387 return read_indirect_string_at_offset (abfd, str_offset);
17388}
17389
43988095
JK
17390/* Return pointer to string at .debug_line_str offset as read from BUF.
17391 BUF is assumed to be in a compilation unit described by CU_HEADER.
17392 Return *BYTES_READ_PTR count of bytes read from BUF. */
17393
17394static const char *
17395read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17396 const struct comp_unit_head *cu_header,
17397 unsigned int *bytes_read_ptr)
17398{
17399 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17400
17401 return read_indirect_line_string_at_offset (abfd, str_offset);
17402}
17403
17404ULONGEST
d521ce57 17405read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17406 unsigned int *bytes_read_ptr)
c906108c 17407{
12df843f 17408 ULONGEST result;
ce5d95e1 17409 unsigned int num_read;
870f88f7 17410 int shift;
c906108c
SS
17411 unsigned char byte;
17412
17413 result = 0;
17414 shift = 0;
17415 num_read = 0;
c906108c
SS
17416 while (1)
17417 {
fe1b8b76 17418 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17419 buf++;
17420 num_read++;
12df843f 17421 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17422 if ((byte & 128) == 0)
17423 {
17424 break;
17425 }
17426 shift += 7;
17427 }
17428 *bytes_read_ptr = num_read;
17429 return result;
17430}
17431
12df843f 17432static LONGEST
d521ce57
TT
17433read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17434 unsigned int *bytes_read_ptr)
c906108c 17435{
12df843f 17436 LONGEST result;
870f88f7 17437 int shift, num_read;
c906108c
SS
17438 unsigned char byte;
17439
17440 result = 0;
17441 shift = 0;
c906108c 17442 num_read = 0;
c906108c
SS
17443 while (1)
17444 {
fe1b8b76 17445 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17446 buf++;
17447 num_read++;
12df843f 17448 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17449 shift += 7;
17450 if ((byte & 128) == 0)
17451 {
17452 break;
17453 }
17454 }
77e0b926 17455 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17456 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17457 *bytes_read_ptr = num_read;
17458 return result;
17459}
17460
3019eac3
DE
17461/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17462 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17463 ADDR_SIZE is the size of addresses from the CU header. */
17464
17465static CORE_ADDR
17466read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17467{
17468 struct objfile *objfile = dwarf2_per_objfile->objfile;
17469 bfd *abfd = objfile->obfd;
17470 const gdb_byte *info_ptr;
17471
17472 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17473 if (dwarf2_per_objfile->addr.buffer == NULL)
17474 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17475 objfile_name (objfile));
3019eac3
DE
17476 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17477 error (_("DW_FORM_addr_index pointing outside of "
17478 ".debug_addr section [in module %s]"),
4262abfb 17479 objfile_name (objfile));
3019eac3
DE
17480 info_ptr = (dwarf2_per_objfile->addr.buffer
17481 + addr_base + addr_index * addr_size);
17482 if (addr_size == 4)
17483 return bfd_get_32 (abfd, info_ptr);
17484 else
17485 return bfd_get_64 (abfd, info_ptr);
17486}
17487
17488/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17489
17490static CORE_ADDR
17491read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17492{
17493 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17494}
17495
17496/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17497
17498static CORE_ADDR
d521ce57 17499read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17500 unsigned int *bytes_read)
17501{
17502 bfd *abfd = cu->objfile->obfd;
17503 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17504
17505 return read_addr_index (cu, addr_index);
17506}
17507
17508/* Data structure to pass results from dwarf2_read_addr_index_reader
17509 back to dwarf2_read_addr_index. */
17510
17511struct dwarf2_read_addr_index_data
17512{
17513 ULONGEST addr_base;
17514 int addr_size;
17515};
17516
17517/* die_reader_func for dwarf2_read_addr_index. */
17518
17519static void
17520dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17521 const gdb_byte *info_ptr,
3019eac3
DE
17522 struct die_info *comp_unit_die,
17523 int has_children,
17524 void *data)
17525{
17526 struct dwarf2_cu *cu = reader->cu;
17527 struct dwarf2_read_addr_index_data *aidata =
17528 (struct dwarf2_read_addr_index_data *) data;
17529
17530 aidata->addr_base = cu->addr_base;
17531 aidata->addr_size = cu->header.addr_size;
17532}
17533
17534/* Given an index in .debug_addr, fetch the value.
17535 NOTE: This can be called during dwarf expression evaluation,
17536 long after the debug information has been read, and thus per_cu->cu
17537 may no longer exist. */
17538
17539CORE_ADDR
17540dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17541 unsigned int addr_index)
17542{
17543 struct objfile *objfile = per_cu->objfile;
17544 struct dwarf2_cu *cu = per_cu->cu;
17545 ULONGEST addr_base;
17546 int addr_size;
17547
17548 /* This is intended to be called from outside this file. */
17549 dw2_setup (objfile);
17550
17551 /* We need addr_base and addr_size.
17552 If we don't have PER_CU->cu, we have to get it.
17553 Nasty, but the alternative is storing the needed info in PER_CU,
17554 which at this point doesn't seem justified: it's not clear how frequently
17555 it would get used and it would increase the size of every PER_CU.
17556 Entry points like dwarf2_per_cu_addr_size do a similar thing
17557 so we're not in uncharted territory here.
17558 Alas we need to be a bit more complicated as addr_base is contained
17559 in the DIE.
17560
17561 We don't need to read the entire CU(/TU).
17562 We just need the header and top level die.
a1b64ce1 17563
3019eac3 17564 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17565 For now we skip this optimization. */
3019eac3
DE
17566
17567 if (cu != NULL)
17568 {
17569 addr_base = cu->addr_base;
17570 addr_size = cu->header.addr_size;
17571 }
17572 else
17573 {
17574 struct dwarf2_read_addr_index_data aidata;
17575
a1b64ce1
DE
17576 /* Note: We can't use init_cutu_and_read_dies_simple here,
17577 we need addr_base. */
17578 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17579 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17580 addr_base = aidata.addr_base;
17581 addr_size = aidata.addr_size;
17582 }
17583
17584 return read_addr_index_1 (addr_index, addr_base, addr_size);
17585}
17586
57d63ce2
DE
17587/* Given a DW_FORM_GNU_str_index, fetch the string.
17588 This is only used by the Fission support. */
3019eac3 17589
d521ce57 17590static const char *
342587c4 17591read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17592{
17593 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17594 const char *objf_name = objfile_name (objfile);
3019eac3 17595 bfd *abfd = objfile->obfd;
342587c4 17596 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17597 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17598 struct dwarf2_section_info *str_offsets_section =
17599 &reader->dwo_file->sections.str_offsets;
d521ce57 17600 const gdb_byte *info_ptr;
3019eac3 17601 ULONGEST str_offset;
57d63ce2 17602 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17603
73869dc2
DE
17604 dwarf2_read_section (objfile, str_section);
17605 dwarf2_read_section (objfile, str_offsets_section);
17606 if (str_section->buffer == NULL)
57d63ce2 17607 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17608 " in CU at offset 0x%x [in module %s]"),
17609 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17610 if (str_offsets_section->buffer == NULL)
57d63ce2 17611 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17612 " in CU at offset 0x%x [in module %s]"),
17613 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17614 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17615 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17616 " section in CU at offset 0x%x [in module %s]"),
17617 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17618 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17619 + str_index * cu->header.offset_size);
17620 if (cu->header.offset_size == 4)
17621 str_offset = bfd_get_32 (abfd, info_ptr);
17622 else
17623 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17624 if (str_offset >= str_section->size)
57d63ce2 17625 error (_("Offset from %s pointing outside of"
9c541725
PA
17626 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17627 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17628 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17629}
17630
3019eac3
DE
17631/* Return the length of an LEB128 number in BUF. */
17632
17633static int
17634leb128_size (const gdb_byte *buf)
17635{
17636 const gdb_byte *begin = buf;
17637 gdb_byte byte;
17638
17639 while (1)
17640 {
17641 byte = *buf++;
17642 if ((byte & 128) == 0)
17643 return buf - begin;
17644 }
17645}
17646
c906108c 17647static void
e142c38c 17648set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17649{
17650 switch (lang)
17651 {
17652 case DW_LANG_C89:
76bee0cc 17653 case DW_LANG_C99:
0cfd832f 17654 case DW_LANG_C11:
c906108c 17655 case DW_LANG_C:
d1be3247 17656 case DW_LANG_UPC:
e142c38c 17657 cu->language = language_c;
c906108c 17658 break;
9c37b5ae 17659 case DW_LANG_Java:
c906108c 17660 case DW_LANG_C_plus_plus:
0cfd832f
MW
17661 case DW_LANG_C_plus_plus_11:
17662 case DW_LANG_C_plus_plus_14:
e142c38c 17663 cu->language = language_cplus;
c906108c 17664 break;
6aecb9c2
JB
17665 case DW_LANG_D:
17666 cu->language = language_d;
17667 break;
c906108c
SS
17668 case DW_LANG_Fortran77:
17669 case DW_LANG_Fortran90:
b21b22e0 17670 case DW_LANG_Fortran95:
f7de9aab
MW
17671 case DW_LANG_Fortran03:
17672 case DW_LANG_Fortran08:
e142c38c 17673 cu->language = language_fortran;
c906108c 17674 break;
a766d390
DE
17675 case DW_LANG_Go:
17676 cu->language = language_go;
17677 break;
c906108c 17678 case DW_LANG_Mips_Assembler:
e142c38c 17679 cu->language = language_asm;
c906108c
SS
17680 break;
17681 case DW_LANG_Ada83:
8aaf0b47 17682 case DW_LANG_Ada95:
bc5f45f8
JB
17683 cu->language = language_ada;
17684 break;
72019c9c
GM
17685 case DW_LANG_Modula2:
17686 cu->language = language_m2;
17687 break;
fe8e67fd
PM
17688 case DW_LANG_Pascal83:
17689 cu->language = language_pascal;
17690 break;
22566fbd
DJ
17691 case DW_LANG_ObjC:
17692 cu->language = language_objc;
17693 break;
c44af4eb
TT
17694 case DW_LANG_Rust:
17695 case DW_LANG_Rust_old:
17696 cu->language = language_rust;
17697 break;
c906108c
SS
17698 case DW_LANG_Cobol74:
17699 case DW_LANG_Cobol85:
c906108c 17700 default:
e142c38c 17701 cu->language = language_minimal;
c906108c
SS
17702 break;
17703 }
e142c38c 17704 cu->language_defn = language_def (cu->language);
c906108c
SS
17705}
17706
17707/* Return the named attribute or NULL if not there. */
17708
17709static struct attribute *
e142c38c 17710dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17711{
a48e046c 17712 for (;;)
c906108c 17713 {
a48e046c
TT
17714 unsigned int i;
17715 struct attribute *spec = NULL;
17716
17717 for (i = 0; i < die->num_attrs; ++i)
17718 {
17719 if (die->attrs[i].name == name)
17720 return &die->attrs[i];
17721 if (die->attrs[i].name == DW_AT_specification
17722 || die->attrs[i].name == DW_AT_abstract_origin)
17723 spec = &die->attrs[i];
17724 }
17725
17726 if (!spec)
17727 break;
c906108c 17728
f2f0e013 17729 die = follow_die_ref (die, spec, &cu);
f2f0e013 17730 }
c5aa993b 17731
c906108c
SS
17732 return NULL;
17733}
17734
348e048f
DE
17735/* Return the named attribute or NULL if not there,
17736 but do not follow DW_AT_specification, etc.
17737 This is for use in contexts where we're reading .debug_types dies.
17738 Following DW_AT_specification, DW_AT_abstract_origin will take us
17739 back up the chain, and we want to go down. */
17740
17741static struct attribute *
45e58e77 17742dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17743{
17744 unsigned int i;
17745
17746 for (i = 0; i < die->num_attrs; ++i)
17747 if (die->attrs[i].name == name)
17748 return &die->attrs[i];
17749
17750 return NULL;
17751}
17752
7d45c7c3
KB
17753/* Return the string associated with a string-typed attribute, or NULL if it
17754 is either not found or is of an incorrect type. */
17755
17756static const char *
17757dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17758{
17759 struct attribute *attr;
17760 const char *str = NULL;
17761
17762 attr = dwarf2_attr (die, name, cu);
17763
17764 if (attr != NULL)
17765 {
43988095 17766 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
17767 || attr->form == DW_FORM_string
17768 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 17769 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17770 str = DW_STRING (attr);
17771 else
17772 complaint (&symfile_complaints,
17773 _("string type expected for attribute %s for "
17774 "DIE at 0x%x in module %s"),
9c541725 17775 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17776 objfile_name (cu->objfile));
17777 }
17778
17779 return str;
17780}
17781
05cf31d1
JB
17782/* Return non-zero iff the attribute NAME is defined for the given DIE,
17783 and holds a non-zero value. This function should only be used for
2dc7f7b3 17784 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17785
17786static int
17787dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17788{
17789 struct attribute *attr = dwarf2_attr (die, name, cu);
17790
17791 return (attr && DW_UNSND (attr));
17792}
17793
3ca72b44 17794static int
e142c38c 17795die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17796{
05cf31d1
JB
17797 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17798 which value is non-zero. However, we have to be careful with
17799 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17800 (via dwarf2_flag_true_p) follows this attribute. So we may
17801 end up accidently finding a declaration attribute that belongs
17802 to a different DIE referenced by the specification attribute,
17803 even though the given DIE does not have a declaration attribute. */
17804 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17805 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17806}
17807
63d06c5c 17808/* Return the die giving the specification for DIE, if there is
f2f0e013 17809 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17810 containing the return value on output. If there is no
17811 specification, but there is an abstract origin, that is
17812 returned. */
63d06c5c
DC
17813
17814static struct die_info *
f2f0e013 17815die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17816{
f2f0e013
DJ
17817 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17818 *spec_cu);
63d06c5c 17819
edb3359d
DJ
17820 if (spec_attr == NULL)
17821 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17822
63d06c5c
DC
17823 if (spec_attr == NULL)
17824 return NULL;
17825 else
f2f0e013 17826 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17827}
c906108c 17828
527f3840
JK
17829/* Stub for free_line_header to match void * callback types. */
17830
17831static void
17832free_line_header_voidp (void *arg)
17833{
9a3c8263 17834 struct line_header *lh = (struct line_header *) arg;
527f3840 17835
fff8551c 17836 delete lh;
527f3840
JK
17837}
17838
fff8551c
PA
17839void
17840line_header::add_include_dir (const char *include_dir)
c906108c 17841{
27e0867f 17842 if (dwarf_line_debug >= 2)
fff8551c
PA
17843 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17844 include_dirs.size () + 1, include_dir);
27e0867f 17845
fff8551c 17846 include_dirs.push_back (include_dir);
debd256d 17847}
6e70227d 17848
fff8551c
PA
17849void
17850line_header::add_file_name (const char *name,
ecfb656c 17851 dir_index d_index,
fff8551c
PA
17852 unsigned int mod_time,
17853 unsigned int length)
debd256d 17854{
27e0867f
DE
17855 if (dwarf_line_debug >= 2)
17856 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17857 (unsigned) file_names.size () + 1, name);
27e0867f 17858
ecfb656c 17859 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17860}
6e70227d 17861
83769d0b 17862/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17863
17864static struct dwarf2_section_info *
17865get_debug_line_section (struct dwarf2_cu *cu)
17866{
17867 struct dwarf2_section_info *section;
17868
17869 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17870 DWO file. */
17871 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17872 section = &cu->dwo_unit->dwo_file->sections.line;
17873 else if (cu->per_cu->is_dwz)
17874 {
17875 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17876
17877 section = &dwz->line;
17878 }
17879 else
17880 section = &dwarf2_per_objfile->line;
17881
17882 return section;
17883}
17884
43988095
JK
17885/* Read directory or file name entry format, starting with byte of
17886 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17887 entries count and the entries themselves in the described entry
17888 format. */
17889
17890static void
17891read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17892 struct line_header *lh,
17893 const struct comp_unit_head *cu_header,
17894 void (*callback) (struct line_header *lh,
17895 const char *name,
ecfb656c 17896 dir_index d_index,
43988095
JK
17897 unsigned int mod_time,
17898 unsigned int length))
17899{
17900 gdb_byte format_count, formati;
17901 ULONGEST data_count, datai;
17902 const gdb_byte *buf = *bufp;
17903 const gdb_byte *format_header_data;
17904 int i;
17905 unsigned int bytes_read;
17906
17907 format_count = read_1_byte (abfd, buf);
17908 buf += 1;
17909 format_header_data = buf;
17910 for (formati = 0; formati < format_count; formati++)
17911 {
17912 read_unsigned_leb128 (abfd, buf, &bytes_read);
17913 buf += bytes_read;
17914 read_unsigned_leb128 (abfd, buf, &bytes_read);
17915 buf += bytes_read;
17916 }
17917
17918 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17919 buf += bytes_read;
17920 for (datai = 0; datai < data_count; datai++)
17921 {
17922 const gdb_byte *format = format_header_data;
17923 struct file_entry fe;
17924
43988095
JK
17925 for (formati = 0; formati < format_count; formati++)
17926 {
ecfb656c 17927 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17928 format += bytes_read;
43988095 17929
ecfb656c 17930 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17931 format += bytes_read;
ecfb656c
PA
17932
17933 gdb::optional<const char *> string;
17934 gdb::optional<unsigned int> uint;
17935
43988095
JK
17936 switch (form)
17937 {
17938 case DW_FORM_string:
ecfb656c 17939 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17940 buf += bytes_read;
17941 break;
17942
17943 case DW_FORM_line_strp:
ecfb656c
PA
17944 string.emplace (read_indirect_line_string (abfd, buf,
17945 cu_header,
17946 &bytes_read));
43988095
JK
17947 buf += bytes_read;
17948 break;
17949
17950 case DW_FORM_data1:
ecfb656c 17951 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17952 buf += 1;
17953 break;
17954
17955 case DW_FORM_data2:
ecfb656c 17956 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17957 buf += 2;
17958 break;
17959
17960 case DW_FORM_data4:
ecfb656c 17961 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17962 buf += 4;
17963 break;
17964
17965 case DW_FORM_data8:
ecfb656c 17966 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17967 buf += 8;
17968 break;
17969
17970 case DW_FORM_udata:
ecfb656c 17971 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17972 buf += bytes_read;
17973 break;
17974
17975 case DW_FORM_block:
17976 /* It is valid only for DW_LNCT_timestamp which is ignored by
17977 current GDB. */
17978 break;
17979 }
ecfb656c
PA
17980
17981 switch (content_type)
17982 {
17983 case DW_LNCT_path:
17984 if (string.has_value ())
17985 fe.name = *string;
17986 break;
17987 case DW_LNCT_directory_index:
17988 if (uint.has_value ())
17989 fe.d_index = (dir_index) *uint;
17990 break;
17991 case DW_LNCT_timestamp:
17992 if (uint.has_value ())
17993 fe.mod_time = *uint;
17994 break;
17995 case DW_LNCT_size:
17996 if (uint.has_value ())
17997 fe.length = *uint;
17998 break;
17999 case DW_LNCT_MD5:
18000 break;
18001 default:
18002 complaint (&symfile_complaints,
18003 _("Unknown format content type %s"),
18004 pulongest (content_type));
18005 }
43988095
JK
18006 }
18007
ecfb656c 18008 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18009 }
18010
18011 *bufp = buf;
18012}
18013
debd256d 18014/* Read the statement program header starting at OFFSET in
3019eac3 18015 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18016 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18017 Returns NULL if there is a problem reading the header, e.g., if it
18018 has a version we don't understand.
debd256d
JB
18019
18020 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18021 the returned object point into the dwarf line section buffer,
18022 and must not be freed. */
ae2de4f8 18023
fff8551c 18024static line_header_up
9c541725 18025dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18026{
d521ce57 18027 const gdb_byte *line_ptr;
c764a876 18028 unsigned int bytes_read, offset_size;
debd256d 18029 int i;
d521ce57 18030 const char *cur_dir, *cur_file;
3019eac3
DE
18031 struct dwarf2_section_info *section;
18032 bfd *abfd;
18033
36586728 18034 section = get_debug_line_section (cu);
3019eac3
DE
18035 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18036 if (section->buffer == NULL)
debd256d 18037 {
3019eac3
DE
18038 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18039 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18040 else
18041 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18042 return 0;
18043 }
18044
fceca515
DE
18045 /* We can't do this until we know the section is non-empty.
18046 Only then do we know we have such a section. */
a32a8923 18047 abfd = get_section_bfd_owner (section);
fceca515 18048
a738430d
MK
18049 /* Make sure that at least there's room for the total_length field.
18050 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18051 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18052 {
4d3c2250 18053 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18054 return 0;
18055 }
18056
fff8551c 18057 line_header_up lh (new line_header ());
debd256d 18058
9c541725 18059 lh->sect_off = sect_off;
527f3840
JK
18060 lh->offset_in_dwz = cu->per_cu->is_dwz;
18061
9c541725 18062 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18063
a738430d 18064 /* Read in the header. */
6e70227d 18065 lh->total_length =
c764a876
DE
18066 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18067 &bytes_read, &offset_size);
debd256d 18068 line_ptr += bytes_read;
3019eac3 18069 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18070 {
4d3c2250 18071 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18072 return 0;
18073 }
18074 lh->statement_program_end = line_ptr + lh->total_length;
18075 lh->version = read_2_bytes (abfd, line_ptr);
18076 line_ptr += 2;
43988095 18077 if (lh->version > 5)
cd366ee8
DE
18078 {
18079 /* This is a version we don't understand. The format could have
18080 changed in ways we don't handle properly so just punt. */
18081 complaint (&symfile_complaints,
18082 _("unsupported version in .debug_line section"));
18083 return NULL;
18084 }
43988095
JK
18085 if (lh->version >= 5)
18086 {
18087 gdb_byte segment_selector_size;
18088
18089 /* Skip address size. */
18090 read_1_byte (abfd, line_ptr);
18091 line_ptr += 1;
18092
18093 segment_selector_size = read_1_byte (abfd, line_ptr);
18094 line_ptr += 1;
18095 if (segment_selector_size != 0)
18096 {
18097 complaint (&symfile_complaints,
18098 _("unsupported segment selector size %u "
18099 "in .debug_line section"),
18100 segment_selector_size);
18101 return NULL;
18102 }
18103 }
c764a876
DE
18104 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18105 line_ptr += offset_size;
debd256d
JB
18106 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18107 line_ptr += 1;
2dc7f7b3
TT
18108 if (lh->version >= 4)
18109 {
18110 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18111 line_ptr += 1;
18112 }
18113 else
18114 lh->maximum_ops_per_instruction = 1;
18115
18116 if (lh->maximum_ops_per_instruction == 0)
18117 {
18118 lh->maximum_ops_per_instruction = 1;
18119 complaint (&symfile_complaints,
3e43a32a
MS
18120 _("invalid maximum_ops_per_instruction "
18121 "in `.debug_line' section"));
2dc7f7b3
TT
18122 }
18123
debd256d
JB
18124 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18125 line_ptr += 1;
18126 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18127 line_ptr += 1;
18128 lh->line_range = read_1_byte (abfd, line_ptr);
18129 line_ptr += 1;
18130 lh->opcode_base = read_1_byte (abfd, line_ptr);
18131 line_ptr += 1;
fff8551c 18132 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18133
18134 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18135 for (i = 1; i < lh->opcode_base; ++i)
18136 {
18137 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18138 line_ptr += 1;
18139 }
18140
43988095 18141 if (lh->version >= 5)
debd256d 18142 {
43988095 18143 /* Read directory table. */
fff8551c
PA
18144 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18145 [] (struct line_header *lh, const char *name,
ecfb656c 18146 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18147 unsigned int length)
18148 {
18149 lh->add_include_dir (name);
18150 });
debd256d 18151
43988095 18152 /* Read file name table. */
fff8551c
PA
18153 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18154 [] (struct line_header *lh, const char *name,
ecfb656c 18155 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18156 unsigned int length)
18157 {
ecfb656c 18158 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18159 });
43988095
JK
18160 }
18161 else
debd256d 18162 {
43988095
JK
18163 /* Read directory table. */
18164 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18165 {
18166 line_ptr += bytes_read;
fff8551c 18167 lh->add_include_dir (cur_dir);
43988095 18168 }
debd256d
JB
18169 line_ptr += bytes_read;
18170
43988095
JK
18171 /* Read file name table. */
18172 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18173 {
ecfb656c
PA
18174 unsigned int mod_time, length;
18175 dir_index d_index;
43988095
JK
18176
18177 line_ptr += bytes_read;
ecfb656c 18178 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18179 line_ptr += bytes_read;
18180 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18181 line_ptr += bytes_read;
18182 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18183 line_ptr += bytes_read;
18184
ecfb656c 18185 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18186 }
18187 line_ptr += bytes_read;
debd256d 18188 }
6e70227d 18189 lh->statement_program_start = line_ptr;
debd256d 18190
3019eac3 18191 if (line_ptr > (section->buffer + section->size))
4d3c2250 18192 complaint (&symfile_complaints,
3e43a32a
MS
18193 _("line number info header doesn't "
18194 "fit in `.debug_line' section"));
debd256d 18195
debd256d
JB
18196 return lh;
18197}
c906108c 18198
c6da4cef
DE
18199/* Subroutine of dwarf_decode_lines to simplify it.
18200 Return the file name of the psymtab for included file FILE_INDEX
18201 in line header LH of PST.
18202 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18203 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18204 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18205
18206 The function creates dangling cleanup registration. */
c6da4cef 18207
d521ce57 18208static const char *
c6da4cef
DE
18209psymtab_include_file_name (const struct line_header *lh, int file_index,
18210 const struct partial_symtab *pst,
18211 const char *comp_dir)
18212{
8c43009f 18213 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18214 const char *include_name = fe.name;
18215 const char *include_name_to_compare = include_name;
72b9f47f
TT
18216 const char *pst_filename;
18217 char *copied_name = NULL;
c6da4cef
DE
18218 int file_is_pst;
18219
8c43009f 18220 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18221
18222 if (!IS_ABSOLUTE_PATH (include_name)
18223 && (dir_name != NULL || comp_dir != NULL))
18224 {
18225 /* Avoid creating a duplicate psymtab for PST.
18226 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18227 Before we do the comparison, however, we need to account
18228 for DIR_NAME and COMP_DIR.
18229 First prepend dir_name (if non-NULL). If we still don't
18230 have an absolute path prepend comp_dir (if non-NULL).
18231 However, the directory we record in the include-file's
18232 psymtab does not contain COMP_DIR (to match the
18233 corresponding symtab(s)).
18234
18235 Example:
18236
18237 bash$ cd /tmp
18238 bash$ gcc -g ./hello.c
18239 include_name = "hello.c"
18240 dir_name = "."
18241 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18242 DW_AT_name = "./hello.c"
18243
18244 */
c6da4cef
DE
18245
18246 if (dir_name != NULL)
18247 {
d521ce57
TT
18248 char *tem = concat (dir_name, SLASH_STRING,
18249 include_name, (char *)NULL);
18250
18251 make_cleanup (xfree, tem);
18252 include_name = tem;
c6da4cef 18253 include_name_to_compare = include_name;
c6da4cef
DE
18254 }
18255 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18256 {
d521ce57
TT
18257 char *tem = concat (comp_dir, SLASH_STRING,
18258 include_name, (char *)NULL);
18259
18260 make_cleanup (xfree, tem);
18261 include_name_to_compare = tem;
c6da4cef
DE
18262 }
18263 }
18264
18265 pst_filename = pst->filename;
18266 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18267 {
72b9f47f
TT
18268 copied_name = concat (pst->dirname, SLASH_STRING,
18269 pst_filename, (char *)NULL);
18270 pst_filename = copied_name;
c6da4cef
DE
18271 }
18272
1e3fad37 18273 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18274
72b9f47f
TT
18275 if (copied_name != NULL)
18276 xfree (copied_name);
c6da4cef
DE
18277
18278 if (file_is_pst)
18279 return NULL;
18280 return include_name;
18281}
18282
d9b3de22
DE
18283/* State machine to track the state of the line number program. */
18284
6f77053d 18285class lnp_state_machine
d9b3de22 18286{
6f77053d
PA
18287public:
18288 /* Initialize a machine state for the start of a line number
18289 program. */
18290 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18291
8c43009f
PA
18292 file_entry *current_file ()
18293 {
18294 /* lh->file_names is 0-based, but the file name numbers in the
18295 statement program are 1-based. */
6f77053d
PA
18296 return m_line_header->file_name_at (m_file);
18297 }
18298
18299 /* Record the line in the state machine. END_SEQUENCE is true if
18300 we're processing the end of a sequence. */
18301 void record_line (bool end_sequence);
18302
18303 /* Check address and if invalid nop-out the rest of the lines in this
18304 sequence. */
18305 void check_line_address (struct dwarf2_cu *cu,
18306 const gdb_byte *line_ptr,
18307 CORE_ADDR lowpc, CORE_ADDR address);
18308
18309 void handle_set_discriminator (unsigned int discriminator)
18310 {
18311 m_discriminator = discriminator;
18312 m_line_has_non_zero_discriminator |= discriminator != 0;
18313 }
18314
18315 /* Handle DW_LNE_set_address. */
18316 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18317 {
18318 m_op_index = 0;
18319 address += baseaddr;
18320 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18321 }
18322
18323 /* Handle DW_LNS_advance_pc. */
18324 void handle_advance_pc (CORE_ADDR adjust);
18325
18326 /* Handle a special opcode. */
18327 void handle_special_opcode (unsigned char op_code);
18328
18329 /* Handle DW_LNS_advance_line. */
18330 void handle_advance_line (int line_delta)
18331 {
18332 advance_line (line_delta);
18333 }
18334
18335 /* Handle DW_LNS_set_file. */
18336 void handle_set_file (file_name_index file);
18337
18338 /* Handle DW_LNS_negate_stmt. */
18339 void handle_negate_stmt ()
18340 {
18341 m_is_stmt = !m_is_stmt;
18342 }
18343
18344 /* Handle DW_LNS_const_add_pc. */
18345 void handle_const_add_pc ();
18346
18347 /* Handle DW_LNS_fixed_advance_pc. */
18348 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18349 {
18350 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18351 m_op_index = 0;
18352 }
18353
18354 /* Handle DW_LNS_copy. */
18355 void handle_copy ()
18356 {
18357 record_line (false);
18358 m_discriminator = 0;
18359 }
18360
18361 /* Handle DW_LNE_end_sequence. */
18362 void handle_end_sequence ()
18363 {
18364 m_record_line_callback = ::record_line;
18365 }
18366
18367private:
18368 /* Advance the line by LINE_DELTA. */
18369 void advance_line (int line_delta)
18370 {
18371 m_line += line_delta;
18372
18373 if (line_delta != 0)
18374 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18375 }
18376
6f77053d
PA
18377 gdbarch *m_gdbarch;
18378
18379 /* True if we're recording lines.
18380 Otherwise we're building partial symtabs and are just interested in
18381 finding include files mentioned by the line number program. */
18382 bool m_record_lines_p;
18383
8c43009f 18384 /* The line number header. */
6f77053d 18385 line_header *m_line_header;
8c43009f 18386
6f77053d
PA
18387 /* These are part of the standard DWARF line number state machine,
18388 and initialized according to the DWARF spec. */
d9b3de22 18389
6f77053d 18390 unsigned char m_op_index = 0;
8c43009f 18391 /* The line table index (1-based) of the current file. */
6f77053d
PA
18392 file_name_index m_file = (file_name_index) 1;
18393 unsigned int m_line = 1;
18394
18395 /* These are initialized in the constructor. */
18396
18397 CORE_ADDR m_address;
18398 bool m_is_stmt;
18399 unsigned int m_discriminator;
d9b3de22
DE
18400
18401 /* Additional bits of state we need to track. */
18402
18403 /* The last file that we called dwarf2_start_subfile for.
18404 This is only used for TLLs. */
6f77053d 18405 unsigned int m_last_file = 0;
d9b3de22 18406 /* The last file a line number was recorded for. */
6f77053d 18407 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18408
18409 /* The function to call to record a line. */
6f77053d 18410 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18411
18412 /* The last line number that was recorded, used to coalesce
18413 consecutive entries for the same line. This can happen, for
18414 example, when discriminators are present. PR 17276. */
6f77053d
PA
18415 unsigned int m_last_line = 0;
18416 bool m_line_has_non_zero_discriminator = false;
8c43009f 18417};
d9b3de22 18418
6f77053d
PA
18419void
18420lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18421{
18422 CORE_ADDR addr_adj = (((m_op_index + adjust)
18423 / m_line_header->maximum_ops_per_instruction)
18424 * m_line_header->minimum_instruction_length);
18425 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18426 m_op_index = ((m_op_index + adjust)
18427 % m_line_header->maximum_ops_per_instruction);
18428}
d9b3de22 18429
6f77053d
PA
18430void
18431lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18432{
6f77053d
PA
18433 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18434 CORE_ADDR addr_adj = (((m_op_index
18435 + (adj_opcode / m_line_header->line_range))
18436 / m_line_header->maximum_ops_per_instruction)
18437 * m_line_header->minimum_instruction_length);
18438 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18439 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18440 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18441
6f77053d
PA
18442 int line_delta = (m_line_header->line_base
18443 + (adj_opcode % m_line_header->line_range));
18444 advance_line (line_delta);
18445 record_line (false);
18446 m_discriminator = 0;
18447}
d9b3de22 18448
6f77053d
PA
18449void
18450lnp_state_machine::handle_set_file (file_name_index file)
18451{
18452 m_file = file;
18453
18454 const file_entry *fe = current_file ();
18455 if (fe == NULL)
18456 dwarf2_debug_line_missing_file_complaint ();
18457 else if (m_record_lines_p)
18458 {
18459 const char *dir = fe->include_dir (m_line_header);
18460
18461 m_last_subfile = current_subfile;
18462 m_line_has_non_zero_discriminator = m_discriminator != 0;
18463 dwarf2_start_subfile (fe->name, dir);
18464 }
18465}
18466
18467void
18468lnp_state_machine::handle_const_add_pc ()
18469{
18470 CORE_ADDR adjust
18471 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18472
18473 CORE_ADDR addr_adj
18474 = (((m_op_index + adjust)
18475 / m_line_header->maximum_ops_per_instruction)
18476 * m_line_header->minimum_instruction_length);
18477
18478 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18479 m_op_index = ((m_op_index + adjust)
18480 % m_line_header->maximum_ops_per_instruction);
18481}
d9b3de22 18482
c91513d8
PP
18483/* Ignore this record_line request. */
18484
18485static void
18486noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18487{
18488 return;
18489}
18490
a05a36a5
DE
18491/* Return non-zero if we should add LINE to the line number table.
18492 LINE is the line to add, LAST_LINE is the last line that was added,
18493 LAST_SUBFILE is the subfile for LAST_LINE.
18494 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18495 had a non-zero discriminator.
18496
18497 We have to be careful in the presence of discriminators.
18498 E.g., for this line:
18499
18500 for (i = 0; i < 100000; i++);
18501
18502 clang can emit four line number entries for that one line,
18503 each with a different discriminator.
18504 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18505
18506 However, we want gdb to coalesce all four entries into one.
18507 Otherwise the user could stepi into the middle of the line and
18508 gdb would get confused about whether the pc really was in the
18509 middle of the line.
18510
18511 Things are further complicated by the fact that two consecutive
18512 line number entries for the same line is a heuristic used by gcc
18513 to denote the end of the prologue. So we can't just discard duplicate
18514 entries, we have to be selective about it. The heuristic we use is
18515 that we only collapse consecutive entries for the same line if at least
18516 one of those entries has a non-zero discriminator. PR 17276.
18517
18518 Note: Addresses in the line number state machine can never go backwards
18519 within one sequence, thus this coalescing is ok. */
18520
18521static int
18522dwarf_record_line_p (unsigned int line, unsigned int last_line,
18523 int line_has_non_zero_discriminator,
18524 struct subfile *last_subfile)
18525{
18526 if (current_subfile != last_subfile)
18527 return 1;
18528 if (line != last_line)
18529 return 1;
18530 /* Same line for the same file that we've seen already.
18531 As a last check, for pr 17276, only record the line if the line
18532 has never had a non-zero discriminator. */
18533 if (!line_has_non_zero_discriminator)
18534 return 1;
18535 return 0;
18536}
18537
252a6764
DE
18538/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18539 in the line table of subfile SUBFILE. */
18540
18541static void
d9b3de22
DE
18542dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18543 unsigned int line, CORE_ADDR address,
18544 record_line_ftype p_record_line)
252a6764
DE
18545{
18546 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18547
27e0867f
DE
18548 if (dwarf_line_debug)
18549 {
18550 fprintf_unfiltered (gdb_stdlog,
18551 "Recording line %u, file %s, address %s\n",
18552 line, lbasename (subfile->name),
18553 paddress (gdbarch, address));
18554 }
18555
d5962de5 18556 (*p_record_line) (subfile, line, addr);
252a6764
DE
18557}
18558
18559/* Subroutine of dwarf_decode_lines_1 to simplify it.
18560 Mark the end of a set of line number records.
d9b3de22 18561 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18562 If SUBFILE is NULL the request is ignored. */
18563
18564static void
18565dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18566 CORE_ADDR address, record_line_ftype p_record_line)
18567{
27e0867f
DE
18568 if (subfile == NULL)
18569 return;
18570
18571 if (dwarf_line_debug)
18572 {
18573 fprintf_unfiltered (gdb_stdlog,
18574 "Finishing current line, file %s, address %s\n",
18575 lbasename (subfile->name),
18576 paddress (gdbarch, address));
18577 }
18578
d9b3de22
DE
18579 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18580}
18581
6f77053d
PA
18582void
18583lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18584{
d9b3de22
DE
18585 if (dwarf_line_debug)
18586 {
18587 fprintf_unfiltered (gdb_stdlog,
18588 "Processing actual line %u: file %u,"
18589 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18590 m_line, to_underlying (m_file),
18591 paddress (m_gdbarch, m_address),
18592 m_is_stmt, m_discriminator);
d9b3de22
DE
18593 }
18594
6f77053d 18595 file_entry *fe = current_file ();
8c43009f
PA
18596
18597 if (fe == NULL)
d9b3de22
DE
18598 dwarf2_debug_line_missing_file_complaint ();
18599 /* For now we ignore lines not starting on an instruction boundary.
18600 But not when processing end_sequence for compatibility with the
18601 previous version of the code. */
6f77053d 18602 else if (m_op_index == 0 || end_sequence)
d9b3de22 18603 {
8c43009f 18604 fe->included_p = 1;
6f77053d 18605 if (m_record_lines_p && m_is_stmt)
d9b3de22 18606 {
6f77053d 18607 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18608 {
6f77053d
PA
18609 dwarf_finish_line (m_gdbarch, m_last_subfile,
18610 m_address, m_record_line_callback);
d9b3de22
DE
18611 }
18612
18613 if (!end_sequence)
18614 {
6f77053d
PA
18615 if (dwarf_record_line_p (m_line, m_last_line,
18616 m_line_has_non_zero_discriminator,
18617 m_last_subfile))
d9b3de22 18618 {
6f77053d
PA
18619 dwarf_record_line_1 (m_gdbarch, current_subfile,
18620 m_line, m_address,
18621 m_record_line_callback);
d9b3de22 18622 }
6f77053d
PA
18623 m_last_subfile = current_subfile;
18624 m_last_line = m_line;
d9b3de22
DE
18625 }
18626 }
18627 }
18628}
18629
6f77053d
PA
18630lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18631 bool record_lines_p)
d9b3de22 18632{
6f77053d
PA
18633 m_gdbarch = arch;
18634 m_record_lines_p = record_lines_p;
18635 m_line_header = lh;
d9b3de22 18636
6f77053d 18637 m_record_line_callback = ::record_line;
d9b3de22 18638
d9b3de22
DE
18639 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18640 was a line entry for it so that the backend has a chance to adjust it
18641 and also record it in case it needs it. This is currently used by MIPS
18642 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18643 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18644 m_is_stmt = lh->default_is_stmt;
18645 m_discriminator = 0;
252a6764
DE
18646}
18647
6f77053d
PA
18648void
18649lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18650 const gdb_byte *line_ptr,
18651 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18652{
18653 /* If address < lowpc then it's not a usable value, it's outside the
18654 pc range of the CU. However, we restrict the test to only address
18655 values of zero to preserve GDB's previous behaviour which is to
18656 handle the specific case of a function being GC'd by the linker. */
18657
18658 if (address == 0 && address < lowpc)
18659 {
18660 /* This line table is for a function which has been
18661 GCd by the linker. Ignore it. PR gdb/12528 */
18662
18663 struct objfile *objfile = cu->objfile;
18664 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18665
18666 complaint (&symfile_complaints,
18667 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18668 line_offset, objfile_name (objfile));
6f77053d
PA
18669 m_record_line_callback = noop_record_line;
18670 /* Note: record_line_callback is left as noop_record_line until
18671 we see DW_LNE_end_sequence. */
924c2928
DE
18672 }
18673}
18674
f3f5162e 18675/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18676 Process the line number information in LH.
18677 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18678 program in order to set included_p for every referenced header. */
debd256d 18679
c906108c 18680static void
43f3e411
DE
18681dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18682 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18683{
d521ce57
TT
18684 const gdb_byte *line_ptr, *extended_end;
18685 const gdb_byte *line_end;
a8c50c1f 18686 unsigned int bytes_read, extended_len;
699ca60a 18687 unsigned char op_code, extended_op;
e142c38c
DJ
18688 CORE_ADDR baseaddr;
18689 struct objfile *objfile = cu->objfile;
f3f5162e 18690 bfd *abfd = objfile->obfd;
fbf65064 18691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18692 /* True if we're recording line info (as opposed to building partial
18693 symtabs and just interested in finding include files mentioned by
18694 the line number program). */
18695 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18696
18697 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18698
debd256d
JB
18699 line_ptr = lh->statement_program_start;
18700 line_end = lh->statement_program_end;
c906108c
SS
18701
18702 /* Read the statement sequences until there's nothing left. */
18703 while (line_ptr < line_end)
18704 {
6f77053d
PA
18705 /* The DWARF line number program state machine. Reset the state
18706 machine at the start of each sequence. */
18707 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18708 bool end_sequence = false;
d9b3de22 18709
8c43009f 18710 if (record_lines_p)
c906108c 18711 {
8c43009f
PA
18712 /* Start a subfile for the current file of the state
18713 machine. */
18714 const file_entry *fe = state_machine.current_file ();
18715
18716 if (fe != NULL)
18717 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18718 }
18719
a738430d 18720 /* Decode the table. */
d9b3de22 18721 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18722 {
18723 op_code = read_1_byte (abfd, line_ptr);
18724 line_ptr += 1;
9aa1fe7e 18725
debd256d 18726 if (op_code >= lh->opcode_base)
6e70227d 18727 {
8e07a239 18728 /* Special opcode. */
6f77053d 18729 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18730 }
18731 else switch (op_code)
c906108c
SS
18732 {
18733 case DW_LNS_extended_op:
3e43a32a
MS
18734 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18735 &bytes_read);
473b7be6 18736 line_ptr += bytes_read;
a8c50c1f 18737 extended_end = line_ptr + extended_len;
c906108c
SS
18738 extended_op = read_1_byte (abfd, line_ptr);
18739 line_ptr += 1;
18740 switch (extended_op)
18741 {
18742 case DW_LNE_end_sequence:
6f77053d
PA
18743 state_machine.handle_end_sequence ();
18744 end_sequence = true;
c906108c
SS
18745 break;
18746 case DW_LNE_set_address:
d9b3de22
DE
18747 {
18748 CORE_ADDR address
18749 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18750 line_ptr += bytes_read;
6f77053d
PA
18751
18752 state_machine.check_line_address (cu, line_ptr,
18753 lowpc, address);
18754 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18755 }
c906108c
SS
18756 break;
18757 case DW_LNE_define_file:
debd256d 18758 {
d521ce57 18759 const char *cur_file;
ecfb656c
PA
18760 unsigned int mod_time, length;
18761 dir_index dindex;
6e70227d 18762
3e43a32a
MS
18763 cur_file = read_direct_string (abfd, line_ptr,
18764 &bytes_read);
debd256d 18765 line_ptr += bytes_read;
ecfb656c 18766 dindex = (dir_index)
debd256d
JB
18767 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18768 line_ptr += bytes_read;
18769 mod_time =
18770 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18771 line_ptr += bytes_read;
18772 length =
18773 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18774 line_ptr += bytes_read;
ecfb656c 18775 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18776 }
c906108c 18777 break;
d0c6ba3d 18778 case DW_LNE_set_discriminator:
6f77053d
PA
18779 {
18780 /* The discriminator is not interesting to the
18781 debugger; just ignore it. We still need to
18782 check its value though:
18783 if there are consecutive entries for the same
18784 (non-prologue) line we want to coalesce them.
18785 PR 17276. */
18786 unsigned int discr
18787 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18788 line_ptr += bytes_read;
18789
18790 state_machine.handle_set_discriminator (discr);
18791 }
d0c6ba3d 18792 break;
c906108c 18793 default:
4d3c2250 18794 complaint (&symfile_complaints,
e2e0b3e5 18795 _("mangled .debug_line section"));
debd256d 18796 return;
c906108c 18797 }
a8c50c1f
DJ
18798 /* Make sure that we parsed the extended op correctly. If e.g.
18799 we expected a different address size than the producer used,
18800 we may have read the wrong number of bytes. */
18801 if (line_ptr != extended_end)
18802 {
18803 complaint (&symfile_complaints,
18804 _("mangled .debug_line section"));
18805 return;
18806 }
c906108c
SS
18807 break;
18808 case DW_LNS_copy:
6f77053d 18809 state_machine.handle_copy ();
c906108c
SS
18810 break;
18811 case DW_LNS_advance_pc:
2dc7f7b3
TT
18812 {
18813 CORE_ADDR adjust
18814 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18815 line_ptr += bytes_read;
6f77053d
PA
18816
18817 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18818 }
c906108c
SS
18819 break;
18820 case DW_LNS_advance_line:
a05a36a5
DE
18821 {
18822 int line_delta
18823 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18824 line_ptr += bytes_read;
6f77053d
PA
18825
18826 state_machine.handle_advance_line (line_delta);
a05a36a5 18827 }
c906108c
SS
18828 break;
18829 case DW_LNS_set_file:
d9b3de22 18830 {
6f77053d 18831 file_name_index file
ecfb656c
PA
18832 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18833 &bytes_read);
d9b3de22 18834 line_ptr += bytes_read;
8c43009f 18835
6f77053d 18836 state_machine.handle_set_file (file);
d9b3de22 18837 }
c906108c
SS
18838 break;
18839 case DW_LNS_set_column:
0ad93d4f 18840 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18841 line_ptr += bytes_read;
18842 break;
18843 case DW_LNS_negate_stmt:
6f77053d 18844 state_machine.handle_negate_stmt ();
c906108c
SS
18845 break;
18846 case DW_LNS_set_basic_block:
c906108c 18847 break;
c2c6d25f
JM
18848 /* Add to the address register of the state machine the
18849 address increment value corresponding to special opcode
a738430d
MK
18850 255. I.e., this value is scaled by the minimum
18851 instruction length since special opcode 255 would have
b021a221 18852 scaled the increment. */
c906108c 18853 case DW_LNS_const_add_pc:
6f77053d 18854 state_machine.handle_const_add_pc ();
c906108c
SS
18855 break;
18856 case DW_LNS_fixed_advance_pc:
3e29f34a 18857 {
6f77053d 18858 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18859 line_ptr += 2;
6f77053d
PA
18860
18861 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18862 }
c906108c 18863 break;
9aa1fe7e 18864 default:
a738430d
MK
18865 {
18866 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18867 int i;
a738430d 18868
debd256d 18869 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18870 {
18871 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18872 line_ptr += bytes_read;
18873 }
18874 }
c906108c
SS
18875 }
18876 }
d9b3de22
DE
18877
18878 if (!end_sequence)
18879 dwarf2_debug_line_missing_end_sequence_complaint ();
18880
18881 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18882 in which case we still finish recording the last line). */
6f77053d 18883 state_machine.record_line (true);
c906108c 18884 }
f3f5162e
DE
18885}
18886
18887/* Decode the Line Number Program (LNP) for the given line_header
18888 structure and CU. The actual information extracted and the type
18889 of structures created from the LNP depends on the value of PST.
18890
18891 1. If PST is NULL, then this procedure uses the data from the program
18892 to create all necessary symbol tables, and their linetables.
18893
18894 2. If PST is not NULL, this procedure reads the program to determine
18895 the list of files included by the unit represented by PST, and
18896 builds all the associated partial symbol tables.
18897
18898 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18899 It is used for relative paths in the line table.
18900 NOTE: When processing partial symtabs (pst != NULL),
18901 comp_dir == pst->dirname.
18902
18903 NOTE: It is important that psymtabs have the same file name (via strcmp)
18904 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18905 symtab we don't use it in the name of the psymtabs we create.
18906 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18907 A good testcase for this is mb-inline.exp.
18908
527f3840
JK
18909 LOWPC is the lowest address in CU (or 0 if not known).
18910
18911 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18912 for its PC<->lines mapping information. Otherwise only the filename
18913 table is read in. */
f3f5162e
DE
18914
18915static void
18916dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18917 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18918 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18919{
18920 struct objfile *objfile = cu->objfile;
18921 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18922
527f3840
JK
18923 if (decode_mapping)
18924 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18925
18926 if (decode_for_pst_p)
18927 {
18928 int file_index;
18929
18930 /* Now that we're done scanning the Line Header Program, we can
18931 create the psymtab of each included file. */
fff8551c 18932 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18933 if (lh->file_names[file_index].included_p == 1)
18934 {
d521ce57 18935 const char *include_name =
c6da4cef
DE
18936 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18937 if (include_name != NULL)
aaa75496
JB
18938 dwarf2_create_include_psymtab (include_name, pst, objfile);
18939 }
18940 }
cb1df416
DJ
18941 else
18942 {
18943 /* Make sure a symtab is created for every file, even files
18944 which contain only variables (i.e. no code with associated
18945 line numbers). */
43f3e411 18946 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18947 int i;
cb1df416 18948
fff8551c 18949 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18950 {
8c43009f 18951 file_entry &fe = lh->file_names[i];
9a619af0 18952
8c43009f 18953 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18954
cb1df416 18955 if (current_subfile->symtab == NULL)
43f3e411
DE
18956 {
18957 current_subfile->symtab
18958 = allocate_symtab (cust, current_subfile->name);
18959 }
8c43009f 18960 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18961 }
18962 }
c906108c
SS
18963}
18964
18965/* Start a subfile for DWARF. FILENAME is the name of the file and
18966 DIRNAME the name of the source directory which contains FILENAME
4d663531 18967 or NULL if not known.
c906108c
SS
18968 This routine tries to keep line numbers from identical absolute and
18969 relative file names in a common subfile.
18970
18971 Using the `list' example from the GDB testsuite, which resides in
18972 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18973 of /srcdir/list0.c yields the following debugging information for list0.c:
18974
c5aa993b 18975 DW_AT_name: /srcdir/list0.c
4d663531 18976 DW_AT_comp_dir: /compdir
357e46e7 18977 files.files[0].name: list0.h
c5aa993b 18978 files.files[0].dir: /srcdir
357e46e7 18979 files.files[1].name: list0.c
c5aa993b 18980 files.files[1].dir: /srcdir
c906108c
SS
18981
18982 The line number information for list0.c has to end up in a single
4f1520fb
FR
18983 subfile, so that `break /srcdir/list0.c:1' works as expected.
18984 start_subfile will ensure that this happens provided that we pass the
18985 concatenation of files.files[1].dir and files.files[1].name as the
18986 subfile's name. */
c906108c
SS
18987
18988static void
4d663531 18989dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18990{
d521ce57 18991 char *copy = NULL;
4f1520fb 18992
4d663531 18993 /* In order not to lose the line information directory,
4f1520fb
FR
18994 we concatenate it to the filename when it makes sense.
18995 Note that the Dwarf3 standard says (speaking of filenames in line
18996 information): ``The directory index is ignored for file names
18997 that represent full path names''. Thus ignoring dirname in the
18998 `else' branch below isn't an issue. */
c906108c 18999
d5166ae1 19000 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19001 {
19002 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19003 filename = copy;
19004 }
c906108c 19005
4d663531 19006 start_subfile (filename);
4f1520fb 19007
d521ce57
TT
19008 if (copy != NULL)
19009 xfree (copy);
c906108c
SS
19010}
19011
f4dc4d17
DE
19012/* Start a symtab for DWARF.
19013 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19014
43f3e411 19015static struct compunit_symtab *
f4dc4d17 19016dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19017 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19018{
43f3e411 19019 struct compunit_symtab *cust
5ffa0793 19020 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19021
f4dc4d17
DE
19022 record_debugformat ("DWARF 2");
19023 record_producer (cu->producer);
19024
19025 /* We assume that we're processing GCC output. */
19026 processing_gcc_compilation = 2;
19027
4d4ec4e5 19028 cu->processing_has_namespace_info = 0;
43f3e411
DE
19029
19030 return cust;
f4dc4d17
DE
19031}
19032
4c2df51b
DJ
19033static void
19034var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19035 struct dwarf2_cu *cu)
4c2df51b 19036{
e7c27a73
DJ
19037 struct objfile *objfile = cu->objfile;
19038 struct comp_unit_head *cu_header = &cu->header;
19039
4c2df51b
DJ
19040 /* NOTE drow/2003-01-30: There used to be a comment and some special
19041 code here to turn a symbol with DW_AT_external and a
19042 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19043 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19044 with some versions of binutils) where shared libraries could have
19045 relocations against symbols in their debug information - the
19046 minimal symbol would have the right address, but the debug info
19047 would not. It's no longer necessary, because we will explicitly
19048 apply relocations when we read in the debug information now. */
19049
19050 /* A DW_AT_location attribute with no contents indicates that a
19051 variable has been optimized away. */
19052 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19053 {
f1e6e072 19054 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19055 return;
19056 }
19057
19058 /* Handle one degenerate form of location expression specially, to
19059 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19060 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19061 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19062
19063 if (attr_form_is_block (attr)
3019eac3
DE
19064 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19065 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19066 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19067 && (DW_BLOCK (attr)->size
19068 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19069 {
891d2f0b 19070 unsigned int dummy;
4c2df51b 19071
3019eac3
DE
19072 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19073 SYMBOL_VALUE_ADDRESS (sym) =
19074 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19075 else
19076 SYMBOL_VALUE_ADDRESS (sym) =
19077 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19078 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19079 fixup_symbol_section (sym, objfile);
19080 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19081 SYMBOL_SECTION (sym));
4c2df51b
DJ
19082 return;
19083 }
19084
19085 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19086 expression evaluator, and use LOC_COMPUTED only when necessary
19087 (i.e. when the value of a register or memory location is
19088 referenced, or a thread-local block, etc.). Then again, it might
19089 not be worthwhile. I'm assuming that it isn't unless performance
19090 or memory numbers show me otherwise. */
19091
f1e6e072 19092 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19093
f1e6e072 19094 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19095 cu->has_loclist = 1;
4c2df51b
DJ
19096}
19097
c906108c
SS
19098/* Given a pointer to a DWARF information entry, figure out if we need
19099 to make a symbol table entry for it, and if so, create a new entry
19100 and return a pointer to it.
19101 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19102 used the passed type.
19103 If SPACE is not NULL, use it to hold the new symbol. If it is
19104 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19105
19106static struct symbol *
34eaf542
TT
19107new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19108 struct symbol *space)
c906108c 19109{
e7c27a73 19110 struct objfile *objfile = cu->objfile;
3e29f34a 19111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19112 struct symbol *sym = NULL;
15d034d0 19113 const char *name;
c906108c
SS
19114 struct attribute *attr = NULL;
19115 struct attribute *attr2 = NULL;
e142c38c 19116 CORE_ADDR baseaddr;
e37fd15a
SW
19117 struct pending **list_to_add = NULL;
19118
edb3359d 19119 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19120
19121 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19122
94af9270 19123 name = dwarf2_name (die, cu);
c906108c
SS
19124 if (name)
19125 {
94af9270 19126 const char *linkagename;
34eaf542 19127 int suppress_add = 0;
94af9270 19128
34eaf542
TT
19129 if (space)
19130 sym = space;
19131 else
e623cf5d 19132 sym = allocate_symbol (objfile);
c906108c 19133 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19134
19135 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19136 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19137 linkagename = dwarf2_physname (name, die, cu);
19138 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19139
f55ee35c
JK
19140 /* Fortran does not have mangling standard and the mangling does differ
19141 between gfortran, iFort etc. */
19142 if (cu->language == language_fortran
b250c185 19143 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19144 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19145 dwarf2_full_name (name, die, cu),
29df156d 19146 NULL);
f55ee35c 19147
c906108c 19148 /* Default assumptions.
c5aa993b 19149 Use the passed type or decode it from the die. */
176620f1 19150 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19151 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19152 if (type != NULL)
19153 SYMBOL_TYPE (sym) = type;
19154 else
e7c27a73 19155 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19156 attr = dwarf2_attr (die,
19157 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19158 cu);
c906108c
SS
19159 if (attr)
19160 {
19161 SYMBOL_LINE (sym) = DW_UNSND (attr);
19162 }
cb1df416 19163
edb3359d
DJ
19164 attr = dwarf2_attr (die,
19165 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19166 cu);
cb1df416
DJ
19167 if (attr)
19168 {
ecfb656c 19169 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19170 struct file_entry *fe;
9a619af0 19171
ecfb656c
PA
19172 if (cu->line_header != NULL)
19173 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19174 else
19175 fe = NULL;
19176
19177 if (fe == NULL)
cb1df416
DJ
19178 complaint (&symfile_complaints,
19179 _("file index out of range"));
8c43009f
PA
19180 else
19181 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19182 }
19183
c906108c
SS
19184 switch (die->tag)
19185 {
19186 case DW_TAG_label:
e142c38c 19187 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19188 if (attr)
3e29f34a
MR
19189 {
19190 CORE_ADDR addr;
19191
19192 addr = attr_value_as_address (attr);
19193 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19194 SYMBOL_VALUE_ADDRESS (sym) = addr;
19195 }
0f5238ed
TT
19196 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19197 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19198 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19199 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19200 break;
19201 case DW_TAG_subprogram:
19202 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19203 finish_block. */
f1e6e072 19204 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19205 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19206 if ((attr2 && (DW_UNSND (attr2) != 0))
19207 || cu->language == language_ada)
c906108c 19208 {
2cfa0c8d
JB
19209 /* Subprograms marked external are stored as a global symbol.
19210 Ada subprograms, whether marked external or not, are always
19211 stored as a global symbol, because we want to be able to
19212 access them globally. For instance, we want to be able
19213 to break on a nested subprogram without having to
19214 specify the context. */
e37fd15a 19215 list_to_add = &global_symbols;
c906108c
SS
19216 }
19217 else
19218 {
e37fd15a 19219 list_to_add = cu->list_in_scope;
c906108c
SS
19220 }
19221 break;
edb3359d
DJ
19222 case DW_TAG_inlined_subroutine:
19223 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19224 finish_block. */
f1e6e072 19225 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19226 SYMBOL_INLINED (sym) = 1;
481860b3 19227 list_to_add = cu->list_in_scope;
edb3359d 19228 break;
34eaf542
TT
19229 case DW_TAG_template_value_param:
19230 suppress_add = 1;
19231 /* Fall through. */
72929c62 19232 case DW_TAG_constant:
c906108c 19233 case DW_TAG_variable:
254e6b9e 19234 case DW_TAG_member:
0963b4bd
MS
19235 /* Compilation with minimal debug info may result in
19236 variables with missing type entries. Change the
19237 misleading `void' type to something sensible. */
c906108c 19238 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19239 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19240
e142c38c 19241 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19242 /* In the case of DW_TAG_member, we should only be called for
19243 static const members. */
19244 if (die->tag == DW_TAG_member)
19245 {
3863f96c
DE
19246 /* dwarf2_add_field uses die_is_declaration,
19247 so we do the same. */
254e6b9e
DE
19248 gdb_assert (die_is_declaration (die, cu));
19249 gdb_assert (attr);
19250 }
c906108c
SS
19251 if (attr)
19252 {
e7c27a73 19253 dwarf2_const_value (attr, sym, cu);
e142c38c 19254 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19255 if (!suppress_add)
34eaf542
TT
19256 {
19257 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19258 list_to_add = &global_symbols;
34eaf542 19259 else
e37fd15a 19260 list_to_add = cu->list_in_scope;
34eaf542 19261 }
c906108c
SS
19262 break;
19263 }
e142c38c 19264 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19265 if (attr)
19266 {
e7c27a73 19267 var_decode_location (attr, sym, cu);
e142c38c 19268 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19269
19270 /* Fortran explicitly imports any global symbols to the local
19271 scope by DW_TAG_common_block. */
19272 if (cu->language == language_fortran && die->parent
19273 && die->parent->tag == DW_TAG_common_block)
19274 attr2 = NULL;
19275
caac4577
JG
19276 if (SYMBOL_CLASS (sym) == LOC_STATIC
19277 && SYMBOL_VALUE_ADDRESS (sym) == 0
19278 && !dwarf2_per_objfile->has_section_at_zero)
19279 {
19280 /* When a static variable is eliminated by the linker,
19281 the corresponding debug information is not stripped
19282 out, but the variable address is set to null;
19283 do not add such variables into symbol table. */
19284 }
19285 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19286 {
f55ee35c
JK
19287 /* Workaround gfortran PR debug/40040 - it uses
19288 DW_AT_location for variables in -fPIC libraries which may
19289 get overriden by other libraries/executable and get
19290 a different address. Resolve it by the minimal symbol
19291 which may come from inferior's executable using copy
19292 relocation. Make this workaround only for gfortran as for
19293 other compilers GDB cannot guess the minimal symbol
19294 Fortran mangling kind. */
19295 if (cu->language == language_fortran && die->parent
19296 && die->parent->tag == DW_TAG_module
19297 && cu->producer
28586665 19298 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19299 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19300
1c809c68
TT
19301 /* A variable with DW_AT_external is never static,
19302 but it may be block-scoped. */
19303 list_to_add = (cu->list_in_scope == &file_symbols
19304 ? &global_symbols : cu->list_in_scope);
1c809c68 19305 }
c906108c 19306 else
e37fd15a 19307 list_to_add = cu->list_in_scope;
c906108c
SS
19308 }
19309 else
19310 {
19311 /* We do not know the address of this symbol.
c5aa993b
JM
19312 If it is an external symbol and we have type information
19313 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19314 The address of the variable will then be determined from
19315 the minimal symbol table whenever the variable is
19316 referenced. */
e142c38c 19317 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19318
19319 /* Fortran explicitly imports any global symbols to the local
19320 scope by DW_TAG_common_block. */
19321 if (cu->language == language_fortran && die->parent
19322 && die->parent->tag == DW_TAG_common_block)
19323 {
19324 /* SYMBOL_CLASS doesn't matter here because
19325 read_common_block is going to reset it. */
19326 if (!suppress_add)
19327 list_to_add = cu->list_in_scope;
19328 }
19329 else if (attr2 && (DW_UNSND (attr2) != 0)
19330 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19331 {
0fe7935b
DJ
19332 /* A variable with DW_AT_external is never static, but it
19333 may be block-scoped. */
19334 list_to_add = (cu->list_in_scope == &file_symbols
19335 ? &global_symbols : cu->list_in_scope);
19336
f1e6e072 19337 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19338 }
442ddf59
JK
19339 else if (!die_is_declaration (die, cu))
19340 {
19341 /* Use the default LOC_OPTIMIZED_OUT class. */
19342 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19343 if (!suppress_add)
19344 list_to_add = cu->list_in_scope;
442ddf59 19345 }
c906108c
SS
19346 }
19347 break;
19348 case DW_TAG_formal_parameter:
edb3359d
DJ
19349 /* If we are inside a function, mark this as an argument. If
19350 not, we might be looking at an argument to an inlined function
19351 when we do not have enough information to show inlined frames;
19352 pretend it's a local variable in that case so that the user can
19353 still see it. */
19354 if (context_stack_depth > 0
19355 && context_stack[context_stack_depth - 1].name != NULL)
19356 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19357 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19358 if (attr)
19359 {
e7c27a73 19360 var_decode_location (attr, sym, cu);
c906108c 19361 }
e142c38c 19362 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19363 if (attr)
19364 {
e7c27a73 19365 dwarf2_const_value (attr, sym, cu);
c906108c 19366 }
f346a30d 19367
e37fd15a 19368 list_to_add = cu->list_in_scope;
c906108c
SS
19369 break;
19370 case DW_TAG_unspecified_parameters:
19371 /* From varargs functions; gdb doesn't seem to have any
19372 interest in this information, so just ignore it for now.
19373 (FIXME?) */
19374 break;
34eaf542
TT
19375 case DW_TAG_template_type_param:
19376 suppress_add = 1;
19377 /* Fall through. */
c906108c 19378 case DW_TAG_class_type:
680b30c7 19379 case DW_TAG_interface_type:
c906108c
SS
19380 case DW_TAG_structure_type:
19381 case DW_TAG_union_type:
72019c9c 19382 case DW_TAG_set_type:
c906108c 19383 case DW_TAG_enumeration_type:
f1e6e072 19384 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19385 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19386
63d06c5c 19387 {
9c37b5ae 19388 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19389 really ever be static objects: otherwise, if you try
19390 to, say, break of a class's method and you're in a file
19391 which doesn't mention that class, it won't work unless
19392 the check for all static symbols in lookup_symbol_aux
19393 saves you. See the OtherFileClass tests in
19394 gdb.c++/namespace.exp. */
19395
e37fd15a 19396 if (!suppress_add)
34eaf542 19397 {
34eaf542 19398 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19399 && cu->language == language_cplus
34eaf542 19400 ? &global_symbols : cu->list_in_scope);
63d06c5c 19401
64382290 19402 /* The semantics of C++ state that "struct foo {
9c37b5ae 19403 ... }" also defines a typedef for "foo". */
64382290 19404 if (cu->language == language_cplus
45280282 19405 || cu->language == language_ada
c44af4eb
TT
19406 || cu->language == language_d
19407 || cu->language == language_rust)
64382290
TT
19408 {
19409 /* The symbol's name is already allocated along
19410 with this objfile, so we don't need to
19411 duplicate it for the type. */
19412 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19413 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19414 }
63d06c5c
DC
19415 }
19416 }
c906108c
SS
19417 break;
19418 case DW_TAG_typedef:
f1e6e072 19419 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19420 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19421 list_to_add = cu->list_in_scope;
63d06c5c 19422 break;
c906108c 19423 case DW_TAG_base_type:
a02abb62 19424 case DW_TAG_subrange_type:
f1e6e072 19425 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19426 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19427 list_to_add = cu->list_in_scope;
c906108c
SS
19428 break;
19429 case DW_TAG_enumerator:
e142c38c 19430 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19431 if (attr)
19432 {
e7c27a73 19433 dwarf2_const_value (attr, sym, cu);
c906108c 19434 }
63d06c5c
DC
19435 {
19436 /* NOTE: carlton/2003-11-10: See comment above in the
19437 DW_TAG_class_type, etc. block. */
19438
e142c38c 19439 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19440 && cu->language == language_cplus
e142c38c 19441 ? &global_symbols : cu->list_in_scope);
63d06c5c 19442 }
c906108c 19443 break;
74921315 19444 case DW_TAG_imported_declaration:
5c4e30ca 19445 case DW_TAG_namespace:
f1e6e072 19446 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19447 list_to_add = &global_symbols;
5c4e30ca 19448 break;
530e8392
KB
19449 case DW_TAG_module:
19450 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19451 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19452 list_to_add = &global_symbols;
19453 break;
4357ac6c 19454 case DW_TAG_common_block:
f1e6e072 19455 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19456 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19457 add_symbol_to_list (sym, cu->list_in_scope);
19458 break;
c906108c
SS
19459 default:
19460 /* Not a tag we recognize. Hopefully we aren't processing
19461 trash data, but since we must specifically ignore things
19462 we don't recognize, there is nothing else we should do at
0963b4bd 19463 this point. */
e2e0b3e5 19464 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19465 dwarf_tag_name (die->tag));
c906108c
SS
19466 break;
19467 }
df8a16a1 19468
e37fd15a
SW
19469 if (suppress_add)
19470 {
19471 sym->hash_next = objfile->template_symbols;
19472 objfile->template_symbols = sym;
19473 list_to_add = NULL;
19474 }
19475
19476 if (list_to_add != NULL)
19477 add_symbol_to_list (sym, list_to_add);
19478
df8a16a1
DJ
19479 /* For the benefit of old versions of GCC, check for anonymous
19480 namespaces based on the demangled name. */
4d4ec4e5 19481 if (!cu->processing_has_namespace_info
94af9270 19482 && cu->language == language_cplus)
a10964d1 19483 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19484 }
19485 return (sym);
19486}
19487
34eaf542
TT
19488/* A wrapper for new_symbol_full that always allocates a new symbol. */
19489
19490static struct symbol *
19491new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19492{
19493 return new_symbol_full (die, type, cu, NULL);
19494}
19495
98bfdba5
PA
19496/* Given an attr with a DW_FORM_dataN value in host byte order,
19497 zero-extend it as appropriate for the symbol's type. The DWARF
19498 standard (v4) is not entirely clear about the meaning of using
19499 DW_FORM_dataN for a constant with a signed type, where the type is
19500 wider than the data. The conclusion of a discussion on the DWARF
19501 list was that this is unspecified. We choose to always zero-extend
19502 because that is the interpretation long in use by GCC. */
c906108c 19503
98bfdba5 19504static gdb_byte *
ff39bb5e 19505dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19506 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19507{
e7c27a73 19508 struct objfile *objfile = cu->objfile;
e17a4113
UW
19509 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19510 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19511 LONGEST l = DW_UNSND (attr);
19512
19513 if (bits < sizeof (*value) * 8)
19514 {
19515 l &= ((LONGEST) 1 << bits) - 1;
19516 *value = l;
19517 }
19518 else if (bits == sizeof (*value) * 8)
19519 *value = l;
19520 else
19521 {
224c3ddb 19522 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19523 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19524 return bytes;
19525 }
19526
19527 return NULL;
19528}
19529
19530/* Read a constant value from an attribute. Either set *VALUE, or if
19531 the value does not fit in *VALUE, set *BYTES - either already
19532 allocated on the objfile obstack, or newly allocated on OBSTACK,
19533 or, set *BATON, if we translated the constant to a location
19534 expression. */
19535
19536static void
ff39bb5e 19537dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19538 const char *name, struct obstack *obstack,
19539 struct dwarf2_cu *cu,
d521ce57 19540 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19541 struct dwarf2_locexpr_baton **baton)
19542{
19543 struct objfile *objfile = cu->objfile;
19544 struct comp_unit_head *cu_header = &cu->header;
c906108c 19545 struct dwarf_block *blk;
98bfdba5
PA
19546 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19547 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19548
19549 *value = 0;
19550 *bytes = NULL;
19551 *baton = NULL;
c906108c
SS
19552
19553 switch (attr->form)
19554 {
19555 case DW_FORM_addr:
3019eac3 19556 case DW_FORM_GNU_addr_index:
ac56253d 19557 {
ac56253d
TT
19558 gdb_byte *data;
19559
98bfdba5
PA
19560 if (TYPE_LENGTH (type) != cu_header->addr_size)
19561 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19562 cu_header->addr_size,
98bfdba5 19563 TYPE_LENGTH (type));
ac56253d
TT
19564 /* Symbols of this form are reasonably rare, so we just
19565 piggyback on the existing location code rather than writing
19566 a new implementation of symbol_computed_ops. */
8d749320 19567 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19568 (*baton)->per_cu = cu->per_cu;
19569 gdb_assert ((*baton)->per_cu);
ac56253d 19570
98bfdba5 19571 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19572 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19573 (*baton)->data = data;
ac56253d
TT
19574
19575 data[0] = DW_OP_addr;
19576 store_unsigned_integer (&data[1], cu_header->addr_size,
19577 byte_order, DW_ADDR (attr));
19578 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19579 }
c906108c 19580 break;
4ac36638 19581 case DW_FORM_string:
93b5768b 19582 case DW_FORM_strp:
3019eac3 19583 case DW_FORM_GNU_str_index:
36586728 19584 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19585 /* DW_STRING is already allocated on the objfile obstack, point
19586 directly to it. */
d521ce57 19587 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19588 break;
c906108c
SS
19589 case DW_FORM_block1:
19590 case DW_FORM_block2:
19591 case DW_FORM_block4:
19592 case DW_FORM_block:
2dc7f7b3 19593 case DW_FORM_exprloc:
0224619f 19594 case DW_FORM_data16:
c906108c 19595 blk = DW_BLOCK (attr);
98bfdba5
PA
19596 if (TYPE_LENGTH (type) != blk->size)
19597 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19598 TYPE_LENGTH (type));
19599 *bytes = blk->data;
c906108c 19600 break;
2df3850c
JM
19601
19602 /* The DW_AT_const_value attributes are supposed to carry the
19603 symbol's value "represented as it would be on the target
19604 architecture." By the time we get here, it's already been
19605 converted to host endianness, so we just need to sign- or
19606 zero-extend it as appropriate. */
19607 case DW_FORM_data1:
3aef2284 19608 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19609 break;
c906108c 19610 case DW_FORM_data2:
3aef2284 19611 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19612 break;
c906108c 19613 case DW_FORM_data4:
3aef2284 19614 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19615 break;
c906108c 19616 case DW_FORM_data8:
3aef2284 19617 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19618 break;
19619
c906108c 19620 case DW_FORM_sdata:
663c44ac 19621 case DW_FORM_implicit_const:
98bfdba5 19622 *value = DW_SND (attr);
2df3850c
JM
19623 break;
19624
c906108c 19625 case DW_FORM_udata:
98bfdba5 19626 *value = DW_UNSND (attr);
c906108c 19627 break;
2df3850c 19628
c906108c 19629 default:
4d3c2250 19630 complaint (&symfile_complaints,
e2e0b3e5 19631 _("unsupported const value attribute form: '%s'"),
4d3c2250 19632 dwarf_form_name (attr->form));
98bfdba5 19633 *value = 0;
c906108c
SS
19634 break;
19635 }
19636}
19637
2df3850c 19638
98bfdba5
PA
19639/* Copy constant value from an attribute to a symbol. */
19640
2df3850c 19641static void
ff39bb5e 19642dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19643 struct dwarf2_cu *cu)
2df3850c 19644{
98bfdba5 19645 struct objfile *objfile = cu->objfile;
12df843f 19646 LONGEST value;
d521ce57 19647 const gdb_byte *bytes;
98bfdba5 19648 struct dwarf2_locexpr_baton *baton;
2df3850c 19649
98bfdba5
PA
19650 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19651 SYMBOL_PRINT_NAME (sym),
19652 &objfile->objfile_obstack, cu,
19653 &value, &bytes, &baton);
2df3850c 19654
98bfdba5
PA
19655 if (baton != NULL)
19656 {
98bfdba5 19657 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19658 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19659 }
19660 else if (bytes != NULL)
19661 {
19662 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19663 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19664 }
19665 else
19666 {
19667 SYMBOL_VALUE (sym) = value;
f1e6e072 19668 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19669 }
2df3850c
JM
19670}
19671
c906108c
SS
19672/* Return the type of the die in question using its DW_AT_type attribute. */
19673
19674static struct type *
e7c27a73 19675die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19676{
c906108c 19677 struct attribute *type_attr;
c906108c 19678
e142c38c 19679 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19680 if (!type_attr)
19681 {
19682 /* A missing DW_AT_type represents a void type. */
46bf5051 19683 return objfile_type (cu->objfile)->builtin_void;
c906108c 19684 }
348e048f 19685
673bfd45 19686 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19687}
19688
b4ba55a1
JB
19689/* True iff CU's producer generates GNAT Ada auxiliary information
19690 that allows to find parallel types through that information instead
19691 of having to do expensive parallel lookups by type name. */
19692
19693static int
19694need_gnat_info (struct dwarf2_cu *cu)
19695{
19696 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19697 of GNAT produces this auxiliary information, without any indication
19698 that it is produced. Part of enhancing the FSF version of GNAT
19699 to produce that information will be to put in place an indicator
19700 that we can use in order to determine whether the descriptive type
19701 info is available or not. One suggestion that has been made is
19702 to use a new attribute, attached to the CU die. For now, assume
19703 that the descriptive type info is not available. */
19704 return 0;
19705}
19706
b4ba55a1
JB
19707/* Return the auxiliary type of the die in question using its
19708 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19709 attribute is not present. */
19710
19711static struct type *
19712die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19713{
b4ba55a1 19714 struct attribute *type_attr;
b4ba55a1
JB
19715
19716 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19717 if (!type_attr)
19718 return NULL;
19719
673bfd45 19720 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19721}
19722
19723/* If DIE has a descriptive_type attribute, then set the TYPE's
19724 descriptive type accordingly. */
19725
19726static void
19727set_descriptive_type (struct type *type, struct die_info *die,
19728 struct dwarf2_cu *cu)
19729{
19730 struct type *descriptive_type = die_descriptive_type (die, cu);
19731
19732 if (descriptive_type)
19733 {
19734 ALLOCATE_GNAT_AUX_TYPE (type);
19735 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19736 }
19737}
19738
c906108c
SS
19739/* Return the containing type of the die in question using its
19740 DW_AT_containing_type attribute. */
19741
19742static struct type *
e7c27a73 19743die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19744{
c906108c 19745 struct attribute *type_attr;
c906108c 19746
e142c38c 19747 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19748 if (!type_attr)
19749 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19750 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19751
673bfd45 19752 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19753}
19754
ac9ec31b
DE
19755/* Return an error marker type to use for the ill formed type in DIE/CU. */
19756
19757static struct type *
19758build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19759{
19760 struct objfile *objfile = dwarf2_per_objfile->objfile;
19761 char *message, *saved;
19762
19763 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19764 objfile_name (objfile),
9c541725
PA
19765 to_underlying (cu->header.sect_off),
19766 to_underlying (die->sect_off));
224c3ddb
SM
19767 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19768 message, strlen (message));
ac9ec31b
DE
19769 xfree (message);
19770
19f392bc 19771 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19772}
19773
673bfd45 19774/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19775 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19776 DW_AT_containing_type.
673bfd45
DE
19777 If there is no type substitute an error marker. */
19778
c906108c 19779static struct type *
ff39bb5e 19780lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19781 struct dwarf2_cu *cu)
c906108c 19782{
bb5ed363 19783 struct objfile *objfile = cu->objfile;
f792889a
DJ
19784 struct type *this_type;
19785
ac9ec31b
DE
19786 gdb_assert (attr->name == DW_AT_type
19787 || attr->name == DW_AT_GNAT_descriptive_type
19788 || attr->name == DW_AT_containing_type);
19789
673bfd45
DE
19790 /* First see if we have it cached. */
19791
36586728
TT
19792 if (attr->form == DW_FORM_GNU_ref_alt)
19793 {
19794 struct dwarf2_per_cu_data *per_cu;
9c541725 19795 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19796
9c541725
PA
19797 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19798 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19799 }
7771576e 19800 else if (attr_form_is_ref (attr))
673bfd45 19801 {
9c541725 19802 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19803
9c541725 19804 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19805 }
55f1336d 19806 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19807 {
ac9ec31b 19808 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19809
ac9ec31b 19810 return get_signatured_type (die, signature, cu);
673bfd45
DE
19811 }
19812 else
19813 {
ac9ec31b
DE
19814 complaint (&symfile_complaints,
19815 _("Dwarf Error: Bad type attribute %s in DIE"
19816 " at 0x%x [in module %s]"),
9c541725 19817 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19818 objfile_name (objfile));
ac9ec31b 19819 return build_error_marker_type (cu, die);
673bfd45
DE
19820 }
19821
19822 /* If not cached we need to read it in. */
19823
19824 if (this_type == NULL)
19825 {
ac9ec31b 19826 struct die_info *type_die = NULL;
673bfd45
DE
19827 struct dwarf2_cu *type_cu = cu;
19828
7771576e 19829 if (attr_form_is_ref (attr))
ac9ec31b
DE
19830 type_die = follow_die_ref (die, attr, &type_cu);
19831 if (type_die == NULL)
19832 return build_error_marker_type (cu, die);
19833 /* If we find the type now, it's probably because the type came
3019eac3
DE
19834 from an inter-CU reference and the type's CU got expanded before
19835 ours. */
ac9ec31b 19836 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19837 }
19838
19839 /* If we still don't have a type use an error marker. */
19840
19841 if (this_type == NULL)
ac9ec31b 19842 return build_error_marker_type (cu, die);
673bfd45 19843
f792889a 19844 return this_type;
c906108c
SS
19845}
19846
673bfd45
DE
19847/* Return the type in DIE, CU.
19848 Returns NULL for invalid types.
19849
02142a6c 19850 This first does a lookup in die_type_hash,
673bfd45
DE
19851 and only reads the die in if necessary.
19852
19853 NOTE: This can be called when reading in partial or full symbols. */
19854
f792889a 19855static struct type *
e7c27a73 19856read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19857{
f792889a
DJ
19858 struct type *this_type;
19859
19860 this_type = get_die_type (die, cu);
19861 if (this_type)
19862 return this_type;
19863
673bfd45
DE
19864 return read_type_die_1 (die, cu);
19865}
19866
19867/* Read the type in DIE, CU.
19868 Returns NULL for invalid types. */
19869
19870static struct type *
19871read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19872{
19873 struct type *this_type = NULL;
19874
c906108c
SS
19875 switch (die->tag)
19876 {
19877 case DW_TAG_class_type:
680b30c7 19878 case DW_TAG_interface_type:
c906108c
SS
19879 case DW_TAG_structure_type:
19880 case DW_TAG_union_type:
f792889a 19881 this_type = read_structure_type (die, cu);
c906108c
SS
19882 break;
19883 case DW_TAG_enumeration_type:
f792889a 19884 this_type = read_enumeration_type (die, cu);
c906108c
SS
19885 break;
19886 case DW_TAG_subprogram:
19887 case DW_TAG_subroutine_type:
edb3359d 19888 case DW_TAG_inlined_subroutine:
f792889a 19889 this_type = read_subroutine_type (die, cu);
c906108c
SS
19890 break;
19891 case DW_TAG_array_type:
f792889a 19892 this_type = read_array_type (die, cu);
c906108c 19893 break;
72019c9c 19894 case DW_TAG_set_type:
f792889a 19895 this_type = read_set_type (die, cu);
72019c9c 19896 break;
c906108c 19897 case DW_TAG_pointer_type:
f792889a 19898 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19899 break;
19900 case DW_TAG_ptr_to_member_type:
f792889a 19901 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19902 break;
19903 case DW_TAG_reference_type:
4297a3f0
AV
19904 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19905 break;
19906 case DW_TAG_rvalue_reference_type:
19907 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19908 break;
19909 case DW_TAG_const_type:
f792889a 19910 this_type = read_tag_const_type (die, cu);
c906108c
SS
19911 break;
19912 case DW_TAG_volatile_type:
f792889a 19913 this_type = read_tag_volatile_type (die, cu);
c906108c 19914 break;
06d66ee9
TT
19915 case DW_TAG_restrict_type:
19916 this_type = read_tag_restrict_type (die, cu);
19917 break;
c906108c 19918 case DW_TAG_string_type:
f792889a 19919 this_type = read_tag_string_type (die, cu);
c906108c
SS
19920 break;
19921 case DW_TAG_typedef:
f792889a 19922 this_type = read_typedef (die, cu);
c906108c 19923 break;
a02abb62 19924 case DW_TAG_subrange_type:
f792889a 19925 this_type = read_subrange_type (die, cu);
a02abb62 19926 break;
c906108c 19927 case DW_TAG_base_type:
f792889a 19928 this_type = read_base_type (die, cu);
c906108c 19929 break;
81a17f79 19930 case DW_TAG_unspecified_type:
f792889a 19931 this_type = read_unspecified_type (die, cu);
81a17f79 19932 break;
0114d602
DJ
19933 case DW_TAG_namespace:
19934 this_type = read_namespace_type (die, cu);
19935 break;
f55ee35c
JK
19936 case DW_TAG_module:
19937 this_type = read_module_type (die, cu);
19938 break;
a2c2acaf
MW
19939 case DW_TAG_atomic_type:
19940 this_type = read_tag_atomic_type (die, cu);
19941 break;
c906108c 19942 default:
3e43a32a
MS
19943 complaint (&symfile_complaints,
19944 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19945 dwarf_tag_name (die->tag));
c906108c
SS
19946 break;
19947 }
63d06c5c 19948
f792889a 19949 return this_type;
63d06c5c
DC
19950}
19951
abc72ce4
DE
19952/* See if we can figure out if the class lives in a namespace. We do
19953 this by looking for a member function; its demangled name will
19954 contain namespace info, if there is any.
19955 Return the computed name or NULL.
19956 Space for the result is allocated on the objfile's obstack.
19957 This is the full-die version of guess_partial_die_structure_name.
19958 In this case we know DIE has no useful parent. */
19959
19960static char *
19961guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19962{
19963 struct die_info *spec_die;
19964 struct dwarf2_cu *spec_cu;
19965 struct die_info *child;
19966
19967 spec_cu = cu;
19968 spec_die = die_specification (die, &spec_cu);
19969 if (spec_die != NULL)
19970 {
19971 die = spec_die;
19972 cu = spec_cu;
19973 }
19974
19975 for (child = die->child;
19976 child != NULL;
19977 child = child->sibling)
19978 {
19979 if (child->tag == DW_TAG_subprogram)
19980 {
73b9be8b 19981 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 19982
7d45c7c3 19983 if (linkage_name != NULL)
abc72ce4
DE
19984 {
19985 char *actual_name
19986 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19987 linkage_name);
abc72ce4
DE
19988 char *name = NULL;
19989
19990 if (actual_name != NULL)
19991 {
15d034d0 19992 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19993
19994 if (die_name != NULL
19995 && strcmp (die_name, actual_name) != 0)
19996 {
19997 /* Strip off the class name from the full name.
19998 We want the prefix. */
19999 int die_name_len = strlen (die_name);
20000 int actual_name_len = strlen (actual_name);
20001
20002 /* Test for '::' as a sanity check. */
20003 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20004 && actual_name[actual_name_len
20005 - die_name_len - 1] == ':')
224c3ddb
SM
20006 name = (char *) obstack_copy0 (
20007 &cu->objfile->per_bfd->storage_obstack,
20008 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20009 }
20010 }
20011 xfree (actual_name);
20012 return name;
20013 }
20014 }
20015 }
20016
20017 return NULL;
20018}
20019
96408a79
SA
20020/* GCC might emit a nameless typedef that has a linkage name. Determine the
20021 prefix part in such case. See
20022 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20023
a121b7c1 20024static const char *
96408a79
SA
20025anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20026{
20027 struct attribute *attr;
e6a959d6 20028 const char *base;
96408a79
SA
20029
20030 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20031 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20032 return NULL;
20033
7d45c7c3 20034 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20035 return NULL;
20036
73b9be8b 20037 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20038 if (attr == NULL || DW_STRING (attr) == NULL)
20039 return NULL;
20040
20041 /* dwarf2_name had to be already called. */
20042 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20043
20044 /* Strip the base name, keep any leading namespaces/classes. */
20045 base = strrchr (DW_STRING (attr), ':');
20046 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20047 return "";
20048
224c3ddb
SM
20049 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20050 DW_STRING (attr),
20051 &base[-1] - DW_STRING (attr));
96408a79
SA
20052}
20053
fdde2d81 20054/* Return the name of the namespace/class that DIE is defined within,
0114d602 20055 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20056
0114d602
DJ
20057 For example, if we're within the method foo() in the following
20058 code:
20059
20060 namespace N {
20061 class C {
20062 void foo () {
20063 }
20064 };
20065 }
20066
20067 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20068
0d5cff50 20069static const char *
e142c38c 20070determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20071{
0114d602
DJ
20072 struct die_info *parent, *spec_die;
20073 struct dwarf2_cu *spec_cu;
20074 struct type *parent_type;
a121b7c1 20075 const char *retval;
63d06c5c 20076
9c37b5ae 20077 if (cu->language != language_cplus
c44af4eb
TT
20078 && cu->language != language_fortran && cu->language != language_d
20079 && cu->language != language_rust)
0114d602
DJ
20080 return "";
20081
96408a79
SA
20082 retval = anonymous_struct_prefix (die, cu);
20083 if (retval)
20084 return retval;
20085
0114d602
DJ
20086 /* We have to be careful in the presence of DW_AT_specification.
20087 For example, with GCC 3.4, given the code
20088
20089 namespace N {
20090 void foo() {
20091 // Definition of N::foo.
20092 }
20093 }
20094
20095 then we'll have a tree of DIEs like this:
20096
20097 1: DW_TAG_compile_unit
20098 2: DW_TAG_namespace // N
20099 3: DW_TAG_subprogram // declaration of N::foo
20100 4: DW_TAG_subprogram // definition of N::foo
20101 DW_AT_specification // refers to die #3
20102
20103 Thus, when processing die #4, we have to pretend that we're in
20104 the context of its DW_AT_specification, namely the contex of die
20105 #3. */
20106 spec_cu = cu;
20107 spec_die = die_specification (die, &spec_cu);
20108 if (spec_die == NULL)
20109 parent = die->parent;
20110 else
63d06c5c 20111 {
0114d602
DJ
20112 parent = spec_die->parent;
20113 cu = spec_cu;
63d06c5c 20114 }
0114d602
DJ
20115
20116 if (parent == NULL)
20117 return "";
98bfdba5
PA
20118 else if (parent->building_fullname)
20119 {
20120 const char *name;
20121 const char *parent_name;
20122
20123 /* It has been seen on RealView 2.2 built binaries,
20124 DW_TAG_template_type_param types actually _defined_ as
20125 children of the parent class:
20126
20127 enum E {};
20128 template class <class Enum> Class{};
20129 Class<enum E> class_e;
20130
20131 1: DW_TAG_class_type (Class)
20132 2: DW_TAG_enumeration_type (E)
20133 3: DW_TAG_enumerator (enum1:0)
20134 3: DW_TAG_enumerator (enum2:1)
20135 ...
20136 2: DW_TAG_template_type_param
20137 DW_AT_type DW_FORM_ref_udata (E)
20138
20139 Besides being broken debug info, it can put GDB into an
20140 infinite loop. Consider:
20141
20142 When we're building the full name for Class<E>, we'll start
20143 at Class, and go look over its template type parameters,
20144 finding E. We'll then try to build the full name of E, and
20145 reach here. We're now trying to build the full name of E,
20146 and look over the parent DIE for containing scope. In the
20147 broken case, if we followed the parent DIE of E, we'd again
20148 find Class, and once again go look at its template type
20149 arguments, etc., etc. Simply don't consider such parent die
20150 as source-level parent of this die (it can't be, the language
20151 doesn't allow it), and break the loop here. */
20152 name = dwarf2_name (die, cu);
20153 parent_name = dwarf2_name (parent, cu);
20154 complaint (&symfile_complaints,
20155 _("template param type '%s' defined within parent '%s'"),
20156 name ? name : "<unknown>",
20157 parent_name ? parent_name : "<unknown>");
20158 return "";
20159 }
63d06c5c 20160 else
0114d602
DJ
20161 switch (parent->tag)
20162 {
63d06c5c 20163 case DW_TAG_namespace:
0114d602 20164 parent_type = read_type_die (parent, cu);
acebe513
UW
20165 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20166 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20167 Work around this problem here. */
20168 if (cu->language == language_cplus
20169 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20170 return "";
0114d602
DJ
20171 /* We give a name to even anonymous namespaces. */
20172 return TYPE_TAG_NAME (parent_type);
63d06c5c 20173 case DW_TAG_class_type:
680b30c7 20174 case DW_TAG_interface_type:
63d06c5c 20175 case DW_TAG_structure_type:
0114d602 20176 case DW_TAG_union_type:
f55ee35c 20177 case DW_TAG_module:
0114d602
DJ
20178 parent_type = read_type_die (parent, cu);
20179 if (TYPE_TAG_NAME (parent_type) != NULL)
20180 return TYPE_TAG_NAME (parent_type);
20181 else
20182 /* An anonymous structure is only allowed non-static data
20183 members; no typedefs, no member functions, et cetera.
20184 So it does not need a prefix. */
20185 return "";
abc72ce4 20186 case DW_TAG_compile_unit:
95554aad 20187 case DW_TAG_partial_unit:
abc72ce4
DE
20188 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20189 if (cu->language == language_cplus
8b70b953 20190 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20191 && die->child != NULL
20192 && (die->tag == DW_TAG_class_type
20193 || die->tag == DW_TAG_structure_type
20194 || die->tag == DW_TAG_union_type))
20195 {
20196 char *name = guess_full_die_structure_name (die, cu);
20197 if (name != NULL)
20198 return name;
20199 }
20200 return "";
3d567982
TT
20201 case DW_TAG_enumeration_type:
20202 parent_type = read_type_die (parent, cu);
20203 if (TYPE_DECLARED_CLASS (parent_type))
20204 {
20205 if (TYPE_TAG_NAME (parent_type) != NULL)
20206 return TYPE_TAG_NAME (parent_type);
20207 return "";
20208 }
20209 /* Fall through. */
63d06c5c 20210 default:
8176b9b8 20211 return determine_prefix (parent, cu);
63d06c5c 20212 }
63d06c5c
DC
20213}
20214
3e43a32a
MS
20215/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20216 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20217 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20218 an obconcat, otherwise allocate storage for the result. The CU argument is
20219 used to determine the language and hence, the appropriate separator. */
987504bb 20220
f55ee35c 20221#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20222
20223static char *
f55ee35c
JK
20224typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20225 int physname, struct dwarf2_cu *cu)
63d06c5c 20226{
f55ee35c 20227 const char *lead = "";
5c315b68 20228 const char *sep;
63d06c5c 20229
3e43a32a
MS
20230 if (suffix == NULL || suffix[0] == '\0'
20231 || prefix == NULL || prefix[0] == '\0')
987504bb 20232 sep = "";
45280282
IB
20233 else if (cu->language == language_d)
20234 {
20235 /* For D, the 'main' function could be defined in any module, but it
20236 should never be prefixed. */
20237 if (strcmp (suffix, "D main") == 0)
20238 {
20239 prefix = "";
20240 sep = "";
20241 }
20242 else
20243 sep = ".";
20244 }
f55ee35c
JK
20245 else if (cu->language == language_fortran && physname)
20246 {
20247 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20248 DW_AT_MIPS_linkage_name is preferred and used instead. */
20249
20250 lead = "__";
20251 sep = "_MOD_";
20252 }
987504bb
JJ
20253 else
20254 sep = "::";
63d06c5c 20255
6dd47d34
DE
20256 if (prefix == NULL)
20257 prefix = "";
20258 if (suffix == NULL)
20259 suffix = "";
20260
987504bb
JJ
20261 if (obs == NULL)
20262 {
3e43a32a 20263 char *retval
224c3ddb
SM
20264 = ((char *)
20265 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20266
f55ee35c
JK
20267 strcpy (retval, lead);
20268 strcat (retval, prefix);
6dd47d34
DE
20269 strcat (retval, sep);
20270 strcat (retval, suffix);
63d06c5c
DC
20271 return retval;
20272 }
987504bb
JJ
20273 else
20274 {
20275 /* We have an obstack. */
f55ee35c 20276 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20277 }
63d06c5c
DC
20278}
20279
c906108c
SS
20280/* Return sibling of die, NULL if no sibling. */
20281
f9aca02d 20282static struct die_info *
fba45db2 20283sibling_die (struct die_info *die)
c906108c 20284{
639d11d3 20285 return die->sibling;
c906108c
SS
20286}
20287
71c25dea
TT
20288/* Get name of a die, return NULL if not found. */
20289
15d034d0
TT
20290static const char *
20291dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20292 struct obstack *obstack)
20293{
20294 if (name && cu->language == language_cplus)
20295 {
2f408ecb 20296 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20297
2f408ecb 20298 if (!canon_name.empty ())
71c25dea 20299 {
2f408ecb
PA
20300 if (canon_name != name)
20301 name = (const char *) obstack_copy0 (obstack,
20302 canon_name.c_str (),
20303 canon_name.length ());
71c25dea
TT
20304 }
20305 }
20306
20307 return name;
c906108c
SS
20308}
20309
96553a0c
DE
20310/* Get name of a die, return NULL if not found.
20311 Anonymous namespaces are converted to their magic string. */
9219021c 20312
15d034d0 20313static const char *
e142c38c 20314dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20315{
20316 struct attribute *attr;
20317
e142c38c 20318 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20319 if ((!attr || !DW_STRING (attr))
96553a0c 20320 && die->tag != DW_TAG_namespace
53832f31
TT
20321 && die->tag != DW_TAG_class_type
20322 && die->tag != DW_TAG_interface_type
20323 && die->tag != DW_TAG_structure_type
20324 && die->tag != DW_TAG_union_type)
71c25dea
TT
20325 return NULL;
20326
20327 switch (die->tag)
20328 {
20329 case DW_TAG_compile_unit:
95554aad 20330 case DW_TAG_partial_unit:
71c25dea
TT
20331 /* Compilation units have a DW_AT_name that is a filename, not
20332 a source language identifier. */
20333 case DW_TAG_enumeration_type:
20334 case DW_TAG_enumerator:
20335 /* These tags always have simple identifiers already; no need
20336 to canonicalize them. */
20337 return DW_STRING (attr);
907af001 20338
96553a0c
DE
20339 case DW_TAG_namespace:
20340 if (attr != NULL && DW_STRING (attr) != NULL)
20341 return DW_STRING (attr);
20342 return CP_ANONYMOUS_NAMESPACE_STR;
20343
907af001
UW
20344 case DW_TAG_class_type:
20345 case DW_TAG_interface_type:
20346 case DW_TAG_structure_type:
20347 case DW_TAG_union_type:
20348 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20349 structures or unions. These were of the form "._%d" in GCC 4.1,
20350 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20351 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20352 if (attr && DW_STRING (attr)
61012eef
GB
20353 && (startswith (DW_STRING (attr), "._")
20354 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20355 return NULL;
53832f31
TT
20356
20357 /* GCC might emit a nameless typedef that has a linkage name. See
20358 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20359 if (!attr || DW_STRING (attr) == NULL)
20360 {
df5c6c50 20361 char *demangled = NULL;
53832f31 20362
73b9be8b 20363 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20364 if (attr == NULL || DW_STRING (attr) == NULL)
20365 return NULL;
20366
df5c6c50
JK
20367 /* Avoid demangling DW_STRING (attr) the second time on a second
20368 call for the same DIE. */
20369 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20370 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20371
20372 if (demangled)
20373 {
e6a959d6 20374 const char *base;
96408a79 20375
53832f31 20376 /* FIXME: we already did this for the partial symbol... */
34a68019 20377 DW_STRING (attr)
224c3ddb
SM
20378 = ((const char *)
20379 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20380 demangled, strlen (demangled)));
53832f31
TT
20381 DW_STRING_IS_CANONICAL (attr) = 1;
20382 xfree (demangled);
96408a79
SA
20383
20384 /* Strip any leading namespaces/classes, keep only the base name.
20385 DW_AT_name for named DIEs does not contain the prefixes. */
20386 base = strrchr (DW_STRING (attr), ':');
20387 if (base && base > DW_STRING (attr) && base[-1] == ':')
20388 return &base[1];
20389 else
20390 return DW_STRING (attr);
53832f31
TT
20391 }
20392 }
907af001
UW
20393 break;
20394
71c25dea 20395 default:
907af001
UW
20396 break;
20397 }
20398
20399 if (!DW_STRING_IS_CANONICAL (attr))
20400 {
20401 DW_STRING (attr)
20402 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20403 &cu->objfile->per_bfd->storage_obstack);
907af001 20404 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20405 }
907af001 20406 return DW_STRING (attr);
9219021c
DC
20407}
20408
20409/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20410 is none. *EXT_CU is the CU containing DIE on input, and the CU
20411 containing the return value on output. */
9219021c
DC
20412
20413static struct die_info *
f2f0e013 20414dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20415{
20416 struct attribute *attr;
9219021c 20417
f2f0e013 20418 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20419 if (attr == NULL)
20420 return NULL;
20421
f2f0e013 20422 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20423}
20424
c906108c
SS
20425/* Convert a DIE tag into its string name. */
20426
f39c6ffd 20427static const char *
aa1ee363 20428dwarf_tag_name (unsigned tag)
c906108c 20429{
f39c6ffd
TT
20430 const char *name = get_DW_TAG_name (tag);
20431
20432 if (name == NULL)
20433 return "DW_TAG_<unknown>";
20434
20435 return name;
c906108c
SS
20436}
20437
20438/* Convert a DWARF attribute code into its string name. */
20439
f39c6ffd 20440static const char *
aa1ee363 20441dwarf_attr_name (unsigned attr)
c906108c 20442{
f39c6ffd
TT
20443 const char *name;
20444
c764a876 20445#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20446 if (attr == DW_AT_MIPS_fde)
20447 return "DW_AT_MIPS_fde";
20448#else
20449 if (attr == DW_AT_HP_block_index)
20450 return "DW_AT_HP_block_index";
c764a876 20451#endif
f39c6ffd
TT
20452
20453 name = get_DW_AT_name (attr);
20454
20455 if (name == NULL)
20456 return "DW_AT_<unknown>";
20457
20458 return name;
c906108c
SS
20459}
20460
20461/* Convert a DWARF value form code into its string name. */
20462
f39c6ffd 20463static const char *
aa1ee363 20464dwarf_form_name (unsigned form)
c906108c 20465{
f39c6ffd
TT
20466 const char *name = get_DW_FORM_name (form);
20467
20468 if (name == NULL)
20469 return "DW_FORM_<unknown>";
20470
20471 return name;
c906108c
SS
20472}
20473
a121b7c1 20474static const char *
fba45db2 20475dwarf_bool_name (unsigned mybool)
c906108c
SS
20476{
20477 if (mybool)
20478 return "TRUE";
20479 else
20480 return "FALSE";
20481}
20482
20483/* Convert a DWARF type code into its string name. */
20484
f39c6ffd 20485static const char *
aa1ee363 20486dwarf_type_encoding_name (unsigned enc)
c906108c 20487{
f39c6ffd 20488 const char *name = get_DW_ATE_name (enc);
c906108c 20489
f39c6ffd
TT
20490 if (name == NULL)
20491 return "DW_ATE_<unknown>";
c906108c 20492
f39c6ffd 20493 return name;
c906108c 20494}
c906108c 20495
f9aca02d 20496static void
d97bc12b 20497dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20498{
20499 unsigned int i;
20500
d97bc12b
DE
20501 print_spaces (indent, f);
20502 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20503 dwarf_tag_name (die->tag), die->abbrev,
20504 to_underlying (die->sect_off));
d97bc12b
DE
20505
20506 if (die->parent != NULL)
20507 {
20508 print_spaces (indent, f);
20509 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20510 to_underlying (die->parent->sect_off));
d97bc12b
DE
20511 }
20512
20513 print_spaces (indent, f);
20514 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20515 dwarf_bool_name (die->child != NULL));
c906108c 20516
d97bc12b
DE
20517 print_spaces (indent, f);
20518 fprintf_unfiltered (f, " attributes:\n");
20519
c906108c
SS
20520 for (i = 0; i < die->num_attrs; ++i)
20521 {
d97bc12b
DE
20522 print_spaces (indent, f);
20523 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20524 dwarf_attr_name (die->attrs[i].name),
20525 dwarf_form_name (die->attrs[i].form));
d97bc12b 20526
c906108c
SS
20527 switch (die->attrs[i].form)
20528 {
c906108c 20529 case DW_FORM_addr:
3019eac3 20530 case DW_FORM_GNU_addr_index:
d97bc12b 20531 fprintf_unfiltered (f, "address: ");
5af949e3 20532 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20533 break;
20534 case DW_FORM_block2:
20535 case DW_FORM_block4:
20536 case DW_FORM_block:
20537 case DW_FORM_block1:
56eb65bd
SP
20538 fprintf_unfiltered (f, "block: size %s",
20539 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20540 break;
2dc7f7b3 20541 case DW_FORM_exprloc:
56eb65bd
SP
20542 fprintf_unfiltered (f, "expression: size %s",
20543 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20544 break;
0224619f
JK
20545 case DW_FORM_data16:
20546 fprintf_unfiltered (f, "constant of 16 bytes");
20547 break;
4568ecf9
DE
20548 case DW_FORM_ref_addr:
20549 fprintf_unfiltered (f, "ref address: ");
20550 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20551 break;
36586728
TT
20552 case DW_FORM_GNU_ref_alt:
20553 fprintf_unfiltered (f, "alt ref address: ");
20554 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20555 break;
10b3939b
DJ
20556 case DW_FORM_ref1:
20557 case DW_FORM_ref2:
20558 case DW_FORM_ref4:
4568ecf9
DE
20559 case DW_FORM_ref8:
20560 case DW_FORM_ref_udata:
d97bc12b 20561 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20562 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20563 break;
c906108c
SS
20564 case DW_FORM_data1:
20565 case DW_FORM_data2:
20566 case DW_FORM_data4:
ce5d95e1 20567 case DW_FORM_data8:
c906108c
SS
20568 case DW_FORM_udata:
20569 case DW_FORM_sdata:
43bbcdc2
PH
20570 fprintf_unfiltered (f, "constant: %s",
20571 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20572 break;
2dc7f7b3
TT
20573 case DW_FORM_sec_offset:
20574 fprintf_unfiltered (f, "section offset: %s",
20575 pulongest (DW_UNSND (&die->attrs[i])));
20576 break;
55f1336d 20577 case DW_FORM_ref_sig8:
ac9ec31b
DE
20578 fprintf_unfiltered (f, "signature: %s",
20579 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20580 break;
c906108c 20581 case DW_FORM_string:
4bdf3d34 20582 case DW_FORM_strp:
43988095 20583 case DW_FORM_line_strp:
3019eac3 20584 case DW_FORM_GNU_str_index:
36586728 20585 case DW_FORM_GNU_strp_alt:
8285870a 20586 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20587 DW_STRING (&die->attrs[i])
8285870a
JK
20588 ? DW_STRING (&die->attrs[i]) : "",
20589 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20590 break;
20591 case DW_FORM_flag:
20592 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20593 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20594 else
d97bc12b 20595 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20596 break;
2dc7f7b3
TT
20597 case DW_FORM_flag_present:
20598 fprintf_unfiltered (f, "flag: TRUE");
20599 break;
a8329558 20600 case DW_FORM_indirect:
0963b4bd
MS
20601 /* The reader will have reduced the indirect form to
20602 the "base form" so this form should not occur. */
3e43a32a
MS
20603 fprintf_unfiltered (f,
20604 "unexpected attribute form: DW_FORM_indirect");
a8329558 20605 break;
663c44ac
JK
20606 case DW_FORM_implicit_const:
20607 fprintf_unfiltered (f, "constant: %s",
20608 plongest (DW_SND (&die->attrs[i])));
20609 break;
c906108c 20610 default:
d97bc12b 20611 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20612 die->attrs[i].form);
d97bc12b 20613 break;
c906108c 20614 }
d97bc12b 20615 fprintf_unfiltered (f, "\n");
c906108c
SS
20616 }
20617}
20618
f9aca02d 20619static void
d97bc12b 20620dump_die_for_error (struct die_info *die)
c906108c 20621{
d97bc12b
DE
20622 dump_die_shallow (gdb_stderr, 0, die);
20623}
20624
20625static void
20626dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20627{
20628 int indent = level * 4;
20629
20630 gdb_assert (die != NULL);
20631
20632 if (level >= max_level)
20633 return;
20634
20635 dump_die_shallow (f, indent, die);
20636
20637 if (die->child != NULL)
c906108c 20638 {
d97bc12b
DE
20639 print_spaces (indent, f);
20640 fprintf_unfiltered (f, " Children:");
20641 if (level + 1 < max_level)
20642 {
20643 fprintf_unfiltered (f, "\n");
20644 dump_die_1 (f, level + 1, max_level, die->child);
20645 }
20646 else
20647 {
3e43a32a
MS
20648 fprintf_unfiltered (f,
20649 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20650 }
20651 }
20652
20653 if (die->sibling != NULL && level > 0)
20654 {
20655 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20656 }
20657}
20658
d97bc12b
DE
20659/* This is called from the pdie macro in gdbinit.in.
20660 It's not static so gcc will keep a copy callable from gdb. */
20661
20662void
20663dump_die (struct die_info *die, int max_level)
20664{
20665 dump_die_1 (gdb_stdlog, 0, max_level, die);
20666}
20667
f9aca02d 20668static void
51545339 20669store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20670{
51545339 20671 void **slot;
c906108c 20672
9c541725
PA
20673 slot = htab_find_slot_with_hash (cu->die_hash, die,
20674 to_underlying (die->sect_off),
b64f50a1 20675 INSERT);
51545339
DJ
20676
20677 *slot = die;
c906108c
SS
20678}
20679
b64f50a1
JK
20680/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20681 required kind. */
20682
20683static sect_offset
ff39bb5e 20684dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20685{
7771576e 20686 if (attr_form_is_ref (attr))
9c541725 20687 return (sect_offset) DW_UNSND (attr);
93311388
DE
20688
20689 complaint (&symfile_complaints,
20690 _("unsupported die ref attribute form: '%s'"),
20691 dwarf_form_name (attr->form));
9c541725 20692 return {};
c906108c
SS
20693}
20694
43bbcdc2
PH
20695/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20696 * the value held by the attribute is not constant. */
a02abb62 20697
43bbcdc2 20698static LONGEST
ff39bb5e 20699dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 20700{
663c44ac 20701 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
20702 return DW_SND (attr);
20703 else if (attr->form == DW_FORM_udata
20704 || attr->form == DW_FORM_data1
20705 || attr->form == DW_FORM_data2
20706 || attr->form == DW_FORM_data4
20707 || attr->form == DW_FORM_data8)
20708 return DW_UNSND (attr);
20709 else
20710 {
0224619f 20711 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20712 complaint (&symfile_complaints,
20713 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20714 dwarf_form_name (attr->form));
20715 return default_value;
20716 }
20717}
20718
348e048f
DE
20719/* Follow reference or signature attribute ATTR of SRC_DIE.
20720 On entry *REF_CU is the CU of SRC_DIE.
20721 On exit *REF_CU is the CU of the result. */
20722
20723static struct die_info *
ff39bb5e 20724follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20725 struct dwarf2_cu **ref_cu)
20726{
20727 struct die_info *die;
20728
7771576e 20729 if (attr_form_is_ref (attr))
348e048f 20730 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20731 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20732 die = follow_die_sig (src_die, attr, ref_cu);
20733 else
20734 {
20735 dump_die_for_error (src_die);
20736 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20737 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20738 }
20739
20740 return die;
03dd20cc
DJ
20741}
20742
5c631832 20743/* Follow reference OFFSET.
673bfd45
DE
20744 On entry *REF_CU is the CU of the source die referencing OFFSET.
20745 On exit *REF_CU is the CU of the result.
20746 Returns NULL if OFFSET is invalid. */
f504f079 20747
f9aca02d 20748static struct die_info *
9c541725 20749follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20750 struct dwarf2_cu **ref_cu)
c906108c 20751{
10b3939b 20752 struct die_info temp_die;
f2f0e013 20753 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20754
348e048f
DE
20755 gdb_assert (cu->per_cu != NULL);
20756
98bfdba5
PA
20757 target_cu = cu;
20758
3019eac3 20759 if (cu->per_cu->is_debug_types)
348e048f
DE
20760 {
20761 /* .debug_types CUs cannot reference anything outside their CU.
20762 If they need to, they have to reference a signatured type via
55f1336d 20763 DW_FORM_ref_sig8. */
9c541725 20764 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20765 return NULL;
348e048f 20766 }
36586728 20767 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20768 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20769 {
20770 struct dwarf2_per_cu_data *per_cu;
9a619af0 20771
9c541725 20772 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20773 cu->objfile);
03dd20cc
DJ
20774
20775 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20776 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20777 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20778
10b3939b
DJ
20779 target_cu = per_cu->cu;
20780 }
98bfdba5
PA
20781 else if (cu->dies == NULL)
20782 {
20783 /* We're loading full DIEs during partial symbol reading. */
20784 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20785 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20786 }
c906108c 20787
f2f0e013 20788 *ref_cu = target_cu;
9c541725 20789 temp_die.sect_off = sect_off;
9a3c8263 20790 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20791 &temp_die,
20792 to_underlying (sect_off));
5c631832 20793}
10b3939b 20794
5c631832
JK
20795/* Follow reference attribute ATTR of SRC_DIE.
20796 On entry *REF_CU is the CU of SRC_DIE.
20797 On exit *REF_CU is the CU of the result. */
20798
20799static struct die_info *
ff39bb5e 20800follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20801 struct dwarf2_cu **ref_cu)
20802{
9c541725 20803 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20804 struct dwarf2_cu *cu = *ref_cu;
20805 struct die_info *die;
20806
9c541725 20807 die = follow_die_offset (sect_off,
36586728
TT
20808 (attr->form == DW_FORM_GNU_ref_alt
20809 || cu->per_cu->is_dwz),
20810 ref_cu);
5c631832
JK
20811 if (!die)
20812 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20813 "at 0x%x [in module %s]"),
9c541725 20814 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20815 objfile_name (cu->objfile));
348e048f 20816
5c631832
JK
20817 return die;
20818}
20819
9c541725 20820/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20821 Returned value is intended for DW_OP_call*. Returned
20822 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20823
20824struct dwarf2_locexpr_baton
9c541725 20825dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20826 struct dwarf2_per_cu_data *per_cu,
20827 CORE_ADDR (*get_frame_pc) (void *baton),
20828 void *baton)
5c631832 20829{
918dd910 20830 struct dwarf2_cu *cu;
5c631832
JK
20831 struct die_info *die;
20832 struct attribute *attr;
20833 struct dwarf2_locexpr_baton retval;
20834
8cf6f0b1
TT
20835 dw2_setup (per_cu->objfile);
20836
918dd910
JK
20837 if (per_cu->cu == NULL)
20838 load_cu (per_cu);
20839 cu = per_cu->cu;
cc12ce38
DE
20840 if (cu == NULL)
20841 {
20842 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20843 Instead just throw an error, not much else we can do. */
20844 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20845 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20846 }
918dd910 20847
9c541725 20848 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20849 if (!die)
20850 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20851 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20852
20853 attr = dwarf2_attr (die, DW_AT_location, cu);
20854 if (!attr)
20855 {
e103e986
JK
20856 /* DWARF: "If there is no such attribute, then there is no effect.".
20857 DATA is ignored if SIZE is 0. */
5c631832 20858
e103e986 20859 retval.data = NULL;
5c631832
JK
20860 retval.size = 0;
20861 }
8cf6f0b1
TT
20862 else if (attr_form_is_section_offset (attr))
20863 {
20864 struct dwarf2_loclist_baton loclist_baton;
20865 CORE_ADDR pc = (*get_frame_pc) (baton);
20866 size_t size;
20867
20868 fill_in_loclist_baton (cu, &loclist_baton, attr);
20869
20870 retval.data = dwarf2_find_location_expression (&loclist_baton,
20871 &size, pc);
20872 retval.size = size;
20873 }
5c631832
JK
20874 else
20875 {
20876 if (!attr_form_is_block (attr))
20877 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20878 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20879 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20880
20881 retval.data = DW_BLOCK (attr)->data;
20882 retval.size = DW_BLOCK (attr)->size;
20883 }
20884 retval.per_cu = cu->per_cu;
918dd910 20885
918dd910
JK
20886 age_cached_comp_units ();
20887
5c631832 20888 return retval;
348e048f
DE
20889}
20890
8b9737bf
TT
20891/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20892 offset. */
20893
20894struct dwarf2_locexpr_baton
20895dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20896 struct dwarf2_per_cu_data *per_cu,
20897 CORE_ADDR (*get_frame_pc) (void *baton),
20898 void *baton)
20899{
9c541725 20900 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20901
9c541725 20902 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20903}
20904
b6807d98
TT
20905/* Write a constant of a given type as target-ordered bytes into
20906 OBSTACK. */
20907
20908static const gdb_byte *
20909write_constant_as_bytes (struct obstack *obstack,
20910 enum bfd_endian byte_order,
20911 struct type *type,
20912 ULONGEST value,
20913 LONGEST *len)
20914{
20915 gdb_byte *result;
20916
20917 *len = TYPE_LENGTH (type);
224c3ddb 20918 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20919 store_unsigned_integer (result, *len, byte_order, value);
20920
20921 return result;
20922}
20923
20924/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20925 pointer to the constant bytes and set LEN to the length of the
20926 data. If memory is needed, allocate it on OBSTACK. If the DIE
20927 does not have a DW_AT_const_value, return NULL. */
20928
20929const gdb_byte *
9c541725 20930dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20931 struct dwarf2_per_cu_data *per_cu,
20932 struct obstack *obstack,
20933 LONGEST *len)
20934{
20935 struct dwarf2_cu *cu;
20936 struct die_info *die;
20937 struct attribute *attr;
20938 const gdb_byte *result = NULL;
20939 struct type *type;
20940 LONGEST value;
20941 enum bfd_endian byte_order;
20942
20943 dw2_setup (per_cu->objfile);
20944
20945 if (per_cu->cu == NULL)
20946 load_cu (per_cu);
20947 cu = per_cu->cu;
cc12ce38
DE
20948 if (cu == NULL)
20949 {
20950 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20951 Instead just throw an error, not much else we can do. */
20952 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20953 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20954 }
b6807d98 20955
9c541725 20956 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20957 if (!die)
20958 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20959 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20960
20961
20962 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20963 if (attr == NULL)
20964 return NULL;
20965
20966 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20967 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20968
20969 switch (attr->form)
20970 {
20971 case DW_FORM_addr:
20972 case DW_FORM_GNU_addr_index:
20973 {
20974 gdb_byte *tem;
20975
20976 *len = cu->header.addr_size;
224c3ddb 20977 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20978 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20979 result = tem;
20980 }
20981 break;
20982 case DW_FORM_string:
20983 case DW_FORM_strp:
20984 case DW_FORM_GNU_str_index:
20985 case DW_FORM_GNU_strp_alt:
20986 /* DW_STRING is already allocated on the objfile obstack, point
20987 directly to it. */
20988 result = (const gdb_byte *) DW_STRING (attr);
20989 *len = strlen (DW_STRING (attr));
20990 break;
20991 case DW_FORM_block1:
20992 case DW_FORM_block2:
20993 case DW_FORM_block4:
20994 case DW_FORM_block:
20995 case DW_FORM_exprloc:
0224619f 20996 case DW_FORM_data16:
b6807d98
TT
20997 result = DW_BLOCK (attr)->data;
20998 *len = DW_BLOCK (attr)->size;
20999 break;
21000
21001 /* The DW_AT_const_value attributes are supposed to carry the
21002 symbol's value "represented as it would be on the target
21003 architecture." By the time we get here, it's already been
21004 converted to host endianness, so we just need to sign- or
21005 zero-extend it as appropriate. */
21006 case DW_FORM_data1:
21007 type = die_type (die, cu);
21008 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21009 if (result == NULL)
21010 result = write_constant_as_bytes (obstack, byte_order,
21011 type, value, len);
21012 break;
21013 case DW_FORM_data2:
21014 type = die_type (die, cu);
21015 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21016 if (result == NULL)
21017 result = write_constant_as_bytes (obstack, byte_order,
21018 type, value, len);
21019 break;
21020 case DW_FORM_data4:
21021 type = die_type (die, cu);
21022 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21023 if (result == NULL)
21024 result = write_constant_as_bytes (obstack, byte_order,
21025 type, value, len);
21026 break;
21027 case DW_FORM_data8:
21028 type = die_type (die, cu);
21029 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21030 if (result == NULL)
21031 result = write_constant_as_bytes (obstack, byte_order,
21032 type, value, len);
21033 break;
21034
21035 case DW_FORM_sdata:
663c44ac 21036 case DW_FORM_implicit_const:
b6807d98
TT
21037 type = die_type (die, cu);
21038 result = write_constant_as_bytes (obstack, byte_order,
21039 type, DW_SND (attr), len);
21040 break;
21041
21042 case DW_FORM_udata:
21043 type = die_type (die, cu);
21044 result = write_constant_as_bytes (obstack, byte_order,
21045 type, DW_UNSND (attr), len);
21046 break;
21047
21048 default:
21049 complaint (&symfile_complaints,
21050 _("unsupported const value attribute form: '%s'"),
21051 dwarf_form_name (attr->form));
21052 break;
21053 }
21054
21055 return result;
21056}
21057
7942e96e
AA
21058/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21059 valid type for this die is found. */
21060
21061struct type *
9c541725 21062dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21063 struct dwarf2_per_cu_data *per_cu)
21064{
21065 struct dwarf2_cu *cu;
21066 struct die_info *die;
21067
21068 dw2_setup (per_cu->objfile);
21069
21070 if (per_cu->cu == NULL)
21071 load_cu (per_cu);
21072 cu = per_cu->cu;
21073 if (!cu)
21074 return NULL;
21075
9c541725 21076 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21077 if (!die)
21078 return NULL;
21079
21080 return die_type (die, cu);
21081}
21082
8a9b8146
TT
21083/* Return the type of the DIE at DIE_OFFSET in the CU named by
21084 PER_CU. */
21085
21086struct type *
b64f50a1 21087dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21088 struct dwarf2_per_cu_data *per_cu)
21089{
8a9b8146 21090 dw2_setup (per_cu->objfile);
b64f50a1 21091
9c541725 21092 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21093 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21094}
21095
ac9ec31b 21096/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21097 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21098 On exit *REF_CU is the CU of the result.
21099 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21100
21101static struct die_info *
ac9ec31b
DE
21102follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21103 struct dwarf2_cu **ref_cu)
348e048f 21104{
348e048f 21105 struct die_info temp_die;
348e048f
DE
21106 struct dwarf2_cu *sig_cu;
21107 struct die_info *die;
21108
ac9ec31b
DE
21109 /* While it might be nice to assert sig_type->type == NULL here,
21110 we can get here for DW_AT_imported_declaration where we need
21111 the DIE not the type. */
348e048f
DE
21112
21113 /* If necessary, add it to the queue and load its DIEs. */
21114
95554aad 21115 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21116 read_signatured_type (sig_type);
348e048f 21117
348e048f 21118 sig_cu = sig_type->per_cu.cu;
69d751e3 21119 gdb_assert (sig_cu != NULL);
9c541725
PA
21120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21121 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21122 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21123 to_underlying (temp_die.sect_off));
348e048f
DE
21124 if (die)
21125 {
796a7ff8
DE
21126 /* For .gdb_index version 7 keep track of included TUs.
21127 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21128 if (dwarf2_per_objfile->index_table != NULL
21129 && dwarf2_per_objfile->index_table->version <= 7)
21130 {
21131 VEC_safe_push (dwarf2_per_cu_ptr,
21132 (*ref_cu)->per_cu->imported_symtabs,
21133 sig_cu->per_cu);
21134 }
21135
348e048f
DE
21136 *ref_cu = sig_cu;
21137 return die;
21138 }
21139
ac9ec31b
DE
21140 return NULL;
21141}
21142
21143/* Follow signatured type referenced by ATTR in SRC_DIE.
21144 On entry *REF_CU is the CU of SRC_DIE.
21145 On exit *REF_CU is the CU of the result.
21146 The result is the DIE of the type.
21147 If the referenced type cannot be found an error is thrown. */
21148
21149static struct die_info *
ff39bb5e 21150follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21151 struct dwarf2_cu **ref_cu)
21152{
21153 ULONGEST signature = DW_SIGNATURE (attr);
21154 struct signatured_type *sig_type;
21155 struct die_info *die;
21156
21157 gdb_assert (attr->form == DW_FORM_ref_sig8);
21158
a2ce51a0 21159 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21160 /* sig_type will be NULL if the signatured type is missing from
21161 the debug info. */
21162 if (sig_type == NULL)
21163 {
21164 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21165 " from DIE at 0x%x [in module %s]"),
9c541725 21166 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21167 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21168 }
21169
21170 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21171 if (die == NULL)
21172 {
21173 dump_die_for_error (src_die);
21174 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21175 " from DIE at 0x%x [in module %s]"),
9c541725 21176 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21177 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21178 }
21179
21180 return die;
21181}
21182
21183/* Get the type specified by SIGNATURE referenced in DIE/CU,
21184 reading in and processing the type unit if necessary. */
21185
21186static struct type *
21187get_signatured_type (struct die_info *die, ULONGEST signature,
21188 struct dwarf2_cu *cu)
21189{
21190 struct signatured_type *sig_type;
21191 struct dwarf2_cu *type_cu;
21192 struct die_info *type_die;
21193 struct type *type;
21194
a2ce51a0 21195 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21196 /* sig_type will be NULL if the signatured type is missing from
21197 the debug info. */
21198 if (sig_type == NULL)
21199 {
21200 complaint (&symfile_complaints,
21201 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21202 " from DIE at 0x%x [in module %s]"),
9c541725 21203 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21204 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21205 return build_error_marker_type (cu, die);
21206 }
21207
21208 /* If we already know the type we're done. */
21209 if (sig_type->type != NULL)
21210 return sig_type->type;
21211
21212 type_cu = cu;
21213 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21214 if (type_die != NULL)
21215 {
21216 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21217 is created. This is important, for example, because for c++ classes
21218 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21219 type = read_type_die (type_die, type_cu);
21220 if (type == NULL)
21221 {
21222 complaint (&symfile_complaints,
21223 _("Dwarf Error: Cannot build signatured type %s"
21224 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21225 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21226 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21227 type = build_error_marker_type (cu, die);
21228 }
21229 }
21230 else
21231 {
21232 complaint (&symfile_complaints,
21233 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21234 " from DIE at 0x%x [in module %s]"),
9c541725 21235 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21236 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21237 type = build_error_marker_type (cu, die);
21238 }
21239 sig_type->type = type;
21240
21241 return type;
21242}
21243
21244/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21245 reading in and processing the type unit if necessary. */
21246
21247static struct type *
ff39bb5e 21248get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21249 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21250{
21251 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21252 if (attr_form_is_ref (attr))
ac9ec31b
DE
21253 {
21254 struct dwarf2_cu *type_cu = cu;
21255 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21256
21257 return read_type_die (type_die, type_cu);
21258 }
21259 else if (attr->form == DW_FORM_ref_sig8)
21260 {
21261 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21262 }
21263 else
21264 {
21265 complaint (&symfile_complaints,
21266 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21267 " at 0x%x [in module %s]"),
9c541725 21268 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21269 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21270 return build_error_marker_type (cu, die);
21271 }
348e048f
DE
21272}
21273
e5fe5e75 21274/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21275
21276static void
e5fe5e75 21277load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21278{
52dc124a 21279 struct signatured_type *sig_type;
348e048f 21280
f4dc4d17
DE
21281 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21282 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21283
6721b2ec
DE
21284 /* We have the per_cu, but we need the signatured_type.
21285 Fortunately this is an easy translation. */
21286 gdb_assert (per_cu->is_debug_types);
21287 sig_type = (struct signatured_type *) per_cu;
348e048f 21288
6721b2ec 21289 gdb_assert (per_cu->cu == NULL);
348e048f 21290
52dc124a 21291 read_signatured_type (sig_type);
348e048f 21292
6721b2ec 21293 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21294}
21295
dee91e82
DE
21296/* die_reader_func for read_signatured_type.
21297 This is identical to load_full_comp_unit_reader,
21298 but is kept separate for now. */
348e048f
DE
21299
21300static void
dee91e82 21301read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21302 const gdb_byte *info_ptr,
dee91e82
DE
21303 struct die_info *comp_unit_die,
21304 int has_children,
21305 void *data)
348e048f 21306{
dee91e82 21307 struct dwarf2_cu *cu = reader->cu;
348e048f 21308
dee91e82
DE
21309 gdb_assert (cu->die_hash == NULL);
21310 cu->die_hash =
21311 htab_create_alloc_ex (cu->header.length / 12,
21312 die_hash,
21313 die_eq,
21314 NULL,
21315 &cu->comp_unit_obstack,
21316 hashtab_obstack_allocate,
21317 dummy_obstack_deallocate);
348e048f 21318
dee91e82
DE
21319 if (has_children)
21320 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21321 &info_ptr, comp_unit_die);
21322 cu->dies = comp_unit_die;
21323 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21324
21325 /* We try not to read any attributes in this function, because not
9cdd5dbd 21326 all CUs needed for references have been loaded yet, and symbol
348e048f 21327 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21328 or we won't be able to build types correctly.
21329 Similarly, if we do not read the producer, we can not apply
21330 producer-specific interpretation. */
95554aad 21331 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21332}
348e048f 21333
3019eac3
DE
21334/* Read in a signatured type and build its CU and DIEs.
21335 If the type is a stub for the real type in a DWO file,
21336 read in the real type from the DWO file as well. */
dee91e82
DE
21337
21338static void
21339read_signatured_type (struct signatured_type *sig_type)
21340{
21341 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21342
3019eac3 21343 gdb_assert (per_cu->is_debug_types);
dee91e82 21344 gdb_assert (per_cu->cu == NULL);
348e048f 21345
f4dc4d17
DE
21346 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21347 read_signatured_type_reader, NULL);
7ee85ab1 21348 sig_type->per_cu.tu_read = 1;
c906108c
SS
21349}
21350
c906108c
SS
21351/* Decode simple location descriptions.
21352 Given a pointer to a dwarf block that defines a location, compute
21353 the location and return the value.
21354
4cecd739
DJ
21355 NOTE drow/2003-11-18: This function is called in two situations
21356 now: for the address of static or global variables (partial symbols
21357 only) and for offsets into structures which are expected to be
21358 (more or less) constant. The partial symbol case should go away,
21359 and only the constant case should remain. That will let this
21360 function complain more accurately. A few special modes are allowed
21361 without complaint for global variables (for instance, global
21362 register values and thread-local values).
c906108c
SS
21363
21364 A location description containing no operations indicates that the
4cecd739 21365 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21366 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21367 callers will only want a very basic result and this can become a
21ae7a4d
JK
21368 complaint.
21369
21370 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21371
21372static CORE_ADDR
e7c27a73 21373decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21374{
e7c27a73 21375 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21376 size_t i;
21377 size_t size = blk->size;
d521ce57 21378 const gdb_byte *data = blk->data;
21ae7a4d
JK
21379 CORE_ADDR stack[64];
21380 int stacki;
21381 unsigned int bytes_read, unsnd;
21382 gdb_byte op;
c906108c 21383
21ae7a4d
JK
21384 i = 0;
21385 stacki = 0;
21386 stack[stacki] = 0;
21387 stack[++stacki] = 0;
21388
21389 while (i < size)
21390 {
21391 op = data[i++];
21392 switch (op)
21393 {
21394 case DW_OP_lit0:
21395 case DW_OP_lit1:
21396 case DW_OP_lit2:
21397 case DW_OP_lit3:
21398 case DW_OP_lit4:
21399 case DW_OP_lit5:
21400 case DW_OP_lit6:
21401 case DW_OP_lit7:
21402 case DW_OP_lit8:
21403 case DW_OP_lit9:
21404 case DW_OP_lit10:
21405 case DW_OP_lit11:
21406 case DW_OP_lit12:
21407 case DW_OP_lit13:
21408 case DW_OP_lit14:
21409 case DW_OP_lit15:
21410 case DW_OP_lit16:
21411 case DW_OP_lit17:
21412 case DW_OP_lit18:
21413 case DW_OP_lit19:
21414 case DW_OP_lit20:
21415 case DW_OP_lit21:
21416 case DW_OP_lit22:
21417 case DW_OP_lit23:
21418 case DW_OP_lit24:
21419 case DW_OP_lit25:
21420 case DW_OP_lit26:
21421 case DW_OP_lit27:
21422 case DW_OP_lit28:
21423 case DW_OP_lit29:
21424 case DW_OP_lit30:
21425 case DW_OP_lit31:
21426 stack[++stacki] = op - DW_OP_lit0;
21427 break;
f1bea926 21428
21ae7a4d
JK
21429 case DW_OP_reg0:
21430 case DW_OP_reg1:
21431 case DW_OP_reg2:
21432 case DW_OP_reg3:
21433 case DW_OP_reg4:
21434 case DW_OP_reg5:
21435 case DW_OP_reg6:
21436 case DW_OP_reg7:
21437 case DW_OP_reg8:
21438 case DW_OP_reg9:
21439 case DW_OP_reg10:
21440 case DW_OP_reg11:
21441 case DW_OP_reg12:
21442 case DW_OP_reg13:
21443 case DW_OP_reg14:
21444 case DW_OP_reg15:
21445 case DW_OP_reg16:
21446 case DW_OP_reg17:
21447 case DW_OP_reg18:
21448 case DW_OP_reg19:
21449 case DW_OP_reg20:
21450 case DW_OP_reg21:
21451 case DW_OP_reg22:
21452 case DW_OP_reg23:
21453 case DW_OP_reg24:
21454 case DW_OP_reg25:
21455 case DW_OP_reg26:
21456 case DW_OP_reg27:
21457 case DW_OP_reg28:
21458 case DW_OP_reg29:
21459 case DW_OP_reg30:
21460 case DW_OP_reg31:
21461 stack[++stacki] = op - DW_OP_reg0;
21462 if (i < size)
21463 dwarf2_complex_location_expr_complaint ();
21464 break;
c906108c 21465
21ae7a4d
JK
21466 case DW_OP_regx:
21467 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21468 i += bytes_read;
21469 stack[++stacki] = unsnd;
21470 if (i < size)
21471 dwarf2_complex_location_expr_complaint ();
21472 break;
c906108c 21473
21ae7a4d
JK
21474 case DW_OP_addr:
21475 stack[++stacki] = read_address (objfile->obfd, &data[i],
21476 cu, &bytes_read);
21477 i += bytes_read;
21478 break;
d53d4ac5 21479
21ae7a4d
JK
21480 case DW_OP_const1u:
21481 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21482 i += 1;
21483 break;
21484
21485 case DW_OP_const1s:
21486 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21487 i += 1;
21488 break;
21489
21490 case DW_OP_const2u:
21491 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21492 i += 2;
21493 break;
21494
21495 case DW_OP_const2s:
21496 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21497 i += 2;
21498 break;
d53d4ac5 21499
21ae7a4d
JK
21500 case DW_OP_const4u:
21501 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21502 i += 4;
21503 break;
21504
21505 case DW_OP_const4s:
21506 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21507 i += 4;
21508 break;
21509
585861ea
JK
21510 case DW_OP_const8u:
21511 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21512 i += 8;
21513 break;
21514
21ae7a4d
JK
21515 case DW_OP_constu:
21516 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21517 &bytes_read);
21518 i += bytes_read;
21519 break;
21520
21521 case DW_OP_consts:
21522 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21523 i += bytes_read;
21524 break;
21525
21526 case DW_OP_dup:
21527 stack[stacki + 1] = stack[stacki];
21528 stacki++;
21529 break;
21530
21531 case DW_OP_plus:
21532 stack[stacki - 1] += stack[stacki];
21533 stacki--;
21534 break;
21535
21536 case DW_OP_plus_uconst:
21537 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21538 &bytes_read);
21539 i += bytes_read;
21540 break;
21541
21542 case DW_OP_minus:
21543 stack[stacki - 1] -= stack[stacki];
21544 stacki--;
21545 break;
21546
21547 case DW_OP_deref:
21548 /* If we're not the last op, then we definitely can't encode
21549 this using GDB's address_class enum. This is valid for partial
21550 global symbols, although the variable's address will be bogus
21551 in the psymtab. */
21552 if (i < size)
21553 dwarf2_complex_location_expr_complaint ();
21554 break;
21555
21556 case DW_OP_GNU_push_tls_address:
4aa4e28b 21557 case DW_OP_form_tls_address:
21ae7a4d
JK
21558 /* The top of the stack has the offset from the beginning
21559 of the thread control block at which the variable is located. */
21560 /* Nothing should follow this operator, so the top of stack would
21561 be returned. */
21562 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21563 address will be bogus in the psymtab. Make it always at least
21564 non-zero to not look as a variable garbage collected by linker
21565 which have DW_OP_addr 0. */
21ae7a4d
JK
21566 if (i < size)
21567 dwarf2_complex_location_expr_complaint ();
585861ea 21568 stack[stacki]++;
21ae7a4d
JK
21569 break;
21570
21571 case DW_OP_GNU_uninit:
21572 break;
21573
3019eac3 21574 case DW_OP_GNU_addr_index:
49f6c839 21575 case DW_OP_GNU_const_index:
3019eac3
DE
21576 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21577 &bytes_read);
21578 i += bytes_read;
21579 break;
21580
21ae7a4d
JK
21581 default:
21582 {
f39c6ffd 21583 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21584
21585 if (name)
21586 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21587 name);
21588 else
21589 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21590 op);
21591 }
21592
21593 return (stack[stacki]);
d53d4ac5 21594 }
3c6e0cb3 21595
21ae7a4d
JK
21596 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21597 outside of the allocated space. Also enforce minimum>0. */
21598 if (stacki >= ARRAY_SIZE (stack) - 1)
21599 {
21600 complaint (&symfile_complaints,
21601 _("location description stack overflow"));
21602 return 0;
21603 }
21604
21605 if (stacki <= 0)
21606 {
21607 complaint (&symfile_complaints,
21608 _("location description stack underflow"));
21609 return 0;
21610 }
21611 }
21612 return (stack[stacki]);
c906108c
SS
21613}
21614
21615/* memory allocation interface */
21616
c906108c 21617static struct dwarf_block *
7b5a2f43 21618dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21619{
8d749320 21620 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21621}
21622
c906108c 21623static struct die_info *
b60c80d6 21624dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21625{
21626 struct die_info *die;
b60c80d6
DJ
21627 size_t size = sizeof (struct die_info);
21628
21629 if (num_attrs > 1)
21630 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21631
b60c80d6 21632 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21633 memset (die, 0, sizeof (struct die_info));
21634 return (die);
21635}
2e276125
JB
21636
21637\f
21638/* Macro support. */
21639
233d95b5
JK
21640/* Return file name relative to the compilation directory of file number I in
21641 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21642 responsible for freeing it. */
233d95b5 21643
2e276125 21644static char *
233d95b5 21645file_file_name (int file, struct line_header *lh)
2e276125 21646{
6a83a1e6
EZ
21647 /* Is the file number a valid index into the line header's file name
21648 table? Remember that file numbers start with one, not zero. */
fff8551c 21649 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21650 {
8c43009f 21651 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21652
8c43009f
PA
21653 if (!IS_ABSOLUTE_PATH (fe.name))
21654 {
21655 const char *dir = fe.include_dir (lh);
21656 if (dir != NULL)
21657 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21658 }
21659 return xstrdup (fe.name);
6a83a1e6 21660 }
2e276125
JB
21661 else
21662 {
6a83a1e6
EZ
21663 /* The compiler produced a bogus file number. We can at least
21664 record the macro definitions made in the file, even if we
21665 won't be able to find the file by name. */
21666 char fake_name[80];
9a619af0 21667
8c042590
PM
21668 xsnprintf (fake_name, sizeof (fake_name),
21669 "<bad macro file number %d>", file);
2e276125 21670
6e70227d 21671 complaint (&symfile_complaints,
6a83a1e6
EZ
21672 _("bad file number in macro information (%d)"),
21673 file);
2e276125 21674
6a83a1e6 21675 return xstrdup (fake_name);
2e276125
JB
21676 }
21677}
21678
233d95b5
JK
21679/* Return the full name of file number I in *LH's file name table.
21680 Use COMP_DIR as the name of the current directory of the
21681 compilation. The result is allocated using xmalloc; the caller is
21682 responsible for freeing it. */
21683static char *
21684file_full_name (int file, struct line_header *lh, const char *comp_dir)
21685{
21686 /* Is the file number a valid index into the line header's file name
21687 table? Remember that file numbers start with one, not zero. */
fff8551c 21688 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21689 {
21690 char *relative = file_file_name (file, lh);
21691
21692 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21693 return relative;
b36cec19
PA
21694 return reconcat (relative, comp_dir, SLASH_STRING,
21695 relative, (char *) NULL);
233d95b5
JK
21696 }
21697 else
21698 return file_file_name (file, lh);
21699}
21700
2e276125
JB
21701
21702static struct macro_source_file *
21703macro_start_file (int file, int line,
21704 struct macro_source_file *current_file,
43f3e411 21705 struct line_header *lh)
2e276125 21706{
233d95b5
JK
21707 /* File name relative to the compilation directory of this source file. */
21708 char *file_name = file_file_name (file, lh);
2e276125 21709
2e276125 21710 if (! current_file)
abc9d0dc 21711 {
fc474241
DE
21712 /* Note: We don't create a macro table for this compilation unit
21713 at all until we actually get a filename. */
43f3e411 21714 struct macro_table *macro_table = get_macro_table ();
fc474241 21715
abc9d0dc
TT
21716 /* If we have no current file, then this must be the start_file
21717 directive for the compilation unit's main source file. */
fc474241
DE
21718 current_file = macro_set_main (macro_table, file_name);
21719 macro_define_special (macro_table);
abc9d0dc 21720 }
2e276125 21721 else
233d95b5 21722 current_file = macro_include (current_file, line, file_name);
2e276125 21723
233d95b5 21724 xfree (file_name);
6e70227d 21725
2e276125
JB
21726 return current_file;
21727}
21728
2e276125
JB
21729static const char *
21730consume_improper_spaces (const char *p, const char *body)
21731{
21732 if (*p == ' ')
21733 {
4d3c2250 21734 complaint (&symfile_complaints,
3e43a32a
MS
21735 _("macro definition contains spaces "
21736 "in formal argument list:\n`%s'"),
4d3c2250 21737 body);
2e276125
JB
21738
21739 while (*p == ' ')
21740 p++;
21741 }
21742
21743 return p;
21744}
21745
21746
21747static void
21748parse_macro_definition (struct macro_source_file *file, int line,
21749 const char *body)
21750{
21751 const char *p;
21752
21753 /* The body string takes one of two forms. For object-like macro
21754 definitions, it should be:
21755
21756 <macro name> " " <definition>
21757
21758 For function-like macro definitions, it should be:
21759
21760 <macro name> "() " <definition>
21761 or
21762 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21763
21764 Spaces may appear only where explicitly indicated, and in the
21765 <definition>.
21766
21767 The Dwarf 2 spec says that an object-like macro's name is always
21768 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21769 the space when the macro's definition is the empty string.
2e276125
JB
21770
21771 The Dwarf 2 spec says that there should be no spaces between the
21772 formal arguments in a function-like macro's formal argument list,
21773 but versions of GCC around March 2002 include spaces after the
21774 commas. */
21775
21776
21777 /* Find the extent of the macro name. The macro name is terminated
21778 by either a space or null character (for an object-like macro) or
21779 an opening paren (for a function-like macro). */
21780 for (p = body; *p; p++)
21781 if (*p == ' ' || *p == '(')
21782 break;
21783
21784 if (*p == ' ' || *p == '\0')
21785 {
21786 /* It's an object-like macro. */
21787 int name_len = p - body;
3f8a7804 21788 char *name = savestring (body, name_len);
2e276125
JB
21789 const char *replacement;
21790
21791 if (*p == ' ')
21792 replacement = body + name_len + 1;
21793 else
21794 {
4d3c2250 21795 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21796 replacement = body + name_len;
21797 }
6e70227d 21798
2e276125
JB
21799 macro_define_object (file, line, name, replacement);
21800
21801 xfree (name);
21802 }
21803 else if (*p == '(')
21804 {
21805 /* It's a function-like macro. */
3f8a7804 21806 char *name = savestring (body, p - body);
2e276125
JB
21807 int argc = 0;
21808 int argv_size = 1;
8d749320 21809 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21810
21811 p++;
21812
21813 p = consume_improper_spaces (p, body);
21814
21815 /* Parse the formal argument list. */
21816 while (*p && *p != ')')
21817 {
21818 /* Find the extent of the current argument name. */
21819 const char *arg_start = p;
21820
21821 while (*p && *p != ',' && *p != ')' && *p != ' ')
21822 p++;
21823
21824 if (! *p || p == arg_start)
4d3c2250 21825 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21826 else
21827 {
21828 /* Make sure argv has room for the new argument. */
21829 if (argc >= argv_size)
21830 {
21831 argv_size *= 2;
224c3ddb 21832 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21833 }
21834
3f8a7804 21835 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
21836 }
21837
21838 p = consume_improper_spaces (p, body);
21839
21840 /* Consume the comma, if present. */
21841 if (*p == ',')
21842 {
21843 p++;
21844
21845 p = consume_improper_spaces (p, body);
21846 }
21847 }
21848
21849 if (*p == ')')
21850 {
21851 p++;
21852
21853 if (*p == ' ')
21854 /* Perfectly formed definition, no complaints. */
21855 macro_define_function (file, line, name,
6e70227d 21856 argc, (const char **) argv,
2e276125
JB
21857 p + 1);
21858 else if (*p == '\0')
21859 {
21860 /* Complain, but do define it. */
4d3c2250 21861 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21862 macro_define_function (file, line, name,
6e70227d 21863 argc, (const char **) argv,
2e276125
JB
21864 p);
21865 }
21866 else
21867 /* Just complain. */
4d3c2250 21868 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21869 }
21870 else
21871 /* Just complain. */
4d3c2250 21872 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21873
21874 xfree (name);
21875 {
21876 int i;
21877
21878 for (i = 0; i < argc; i++)
21879 xfree (argv[i]);
21880 }
21881 xfree (argv);
21882 }
21883 else
4d3c2250 21884 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21885}
21886
cf2c3c16
TT
21887/* Skip some bytes from BYTES according to the form given in FORM.
21888 Returns the new pointer. */
2e276125 21889
d521ce57
TT
21890static const gdb_byte *
21891skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21892 enum dwarf_form form,
21893 unsigned int offset_size,
21894 struct dwarf2_section_info *section)
2e276125 21895{
cf2c3c16 21896 unsigned int bytes_read;
2e276125 21897
cf2c3c16 21898 switch (form)
2e276125 21899 {
cf2c3c16
TT
21900 case DW_FORM_data1:
21901 case DW_FORM_flag:
21902 ++bytes;
21903 break;
21904
21905 case DW_FORM_data2:
21906 bytes += 2;
21907 break;
21908
21909 case DW_FORM_data4:
21910 bytes += 4;
21911 break;
21912
21913 case DW_FORM_data8:
21914 bytes += 8;
21915 break;
21916
0224619f
JK
21917 case DW_FORM_data16:
21918 bytes += 16;
21919 break;
21920
cf2c3c16
TT
21921 case DW_FORM_string:
21922 read_direct_string (abfd, bytes, &bytes_read);
21923 bytes += bytes_read;
21924 break;
21925
21926 case DW_FORM_sec_offset:
21927 case DW_FORM_strp:
36586728 21928 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21929 bytes += offset_size;
21930 break;
21931
21932 case DW_FORM_block:
21933 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21934 bytes += bytes_read;
21935 break;
21936
21937 case DW_FORM_block1:
21938 bytes += 1 + read_1_byte (abfd, bytes);
21939 break;
21940 case DW_FORM_block2:
21941 bytes += 2 + read_2_bytes (abfd, bytes);
21942 break;
21943 case DW_FORM_block4:
21944 bytes += 4 + read_4_bytes (abfd, bytes);
21945 break;
21946
21947 case DW_FORM_sdata:
21948 case DW_FORM_udata:
3019eac3
DE
21949 case DW_FORM_GNU_addr_index:
21950 case DW_FORM_GNU_str_index:
d521ce57 21951 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21952 if (bytes == NULL)
21953 {
21954 dwarf2_section_buffer_overflow_complaint (section);
21955 return NULL;
21956 }
cf2c3c16
TT
21957 break;
21958
663c44ac
JK
21959 case DW_FORM_implicit_const:
21960 break;
21961
cf2c3c16
TT
21962 default:
21963 {
21964 complain:
21965 complaint (&symfile_complaints,
21966 _("invalid form 0x%x in `%s'"),
a32a8923 21967 form, get_section_name (section));
cf2c3c16
TT
21968 return NULL;
21969 }
2e276125
JB
21970 }
21971
cf2c3c16
TT
21972 return bytes;
21973}
757a13d0 21974
cf2c3c16
TT
21975/* A helper for dwarf_decode_macros that handles skipping an unknown
21976 opcode. Returns an updated pointer to the macro data buffer; or,
21977 on error, issues a complaint and returns NULL. */
757a13d0 21978
d521ce57 21979static const gdb_byte *
cf2c3c16 21980skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21981 const gdb_byte **opcode_definitions,
21982 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21983 bfd *abfd,
21984 unsigned int offset_size,
21985 struct dwarf2_section_info *section)
21986{
21987 unsigned int bytes_read, i;
21988 unsigned long arg;
d521ce57 21989 const gdb_byte *defn;
2e276125 21990
cf2c3c16 21991 if (opcode_definitions[opcode] == NULL)
2e276125 21992 {
cf2c3c16
TT
21993 complaint (&symfile_complaints,
21994 _("unrecognized DW_MACFINO opcode 0x%x"),
21995 opcode);
21996 return NULL;
21997 }
2e276125 21998
cf2c3c16
TT
21999 defn = opcode_definitions[opcode];
22000 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22001 defn += bytes_read;
2e276125 22002
cf2c3c16
TT
22003 for (i = 0; i < arg; ++i)
22004 {
aead7601
SM
22005 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22006 (enum dwarf_form) defn[i], offset_size,
f664829e 22007 section);
cf2c3c16
TT
22008 if (mac_ptr == NULL)
22009 {
22010 /* skip_form_bytes already issued the complaint. */
22011 return NULL;
22012 }
22013 }
757a13d0 22014
cf2c3c16
TT
22015 return mac_ptr;
22016}
757a13d0 22017
cf2c3c16
TT
22018/* A helper function which parses the header of a macro section.
22019 If the macro section is the extended (for now called "GNU") type,
22020 then this updates *OFFSET_SIZE. Returns a pointer to just after
22021 the header, or issues a complaint and returns NULL on error. */
757a13d0 22022
d521ce57
TT
22023static const gdb_byte *
22024dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22025 bfd *abfd,
d521ce57 22026 const gdb_byte *mac_ptr,
cf2c3c16
TT
22027 unsigned int *offset_size,
22028 int section_is_gnu)
22029{
22030 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22031
cf2c3c16
TT
22032 if (section_is_gnu)
22033 {
22034 unsigned int version, flags;
757a13d0 22035
cf2c3c16 22036 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22037 if (version != 4 && version != 5)
cf2c3c16
TT
22038 {
22039 complaint (&symfile_complaints,
22040 _("unrecognized version `%d' in .debug_macro section"),
22041 version);
22042 return NULL;
22043 }
22044 mac_ptr += 2;
757a13d0 22045
cf2c3c16
TT
22046 flags = read_1_byte (abfd, mac_ptr);
22047 ++mac_ptr;
22048 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22049
cf2c3c16
TT
22050 if ((flags & 2) != 0)
22051 /* We don't need the line table offset. */
22052 mac_ptr += *offset_size;
757a13d0 22053
cf2c3c16
TT
22054 /* Vendor opcode descriptions. */
22055 if ((flags & 4) != 0)
22056 {
22057 unsigned int i, count;
757a13d0 22058
cf2c3c16
TT
22059 count = read_1_byte (abfd, mac_ptr);
22060 ++mac_ptr;
22061 for (i = 0; i < count; ++i)
22062 {
22063 unsigned int opcode, bytes_read;
22064 unsigned long arg;
22065
22066 opcode = read_1_byte (abfd, mac_ptr);
22067 ++mac_ptr;
22068 opcode_definitions[opcode] = mac_ptr;
22069 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22070 mac_ptr += bytes_read;
22071 mac_ptr += arg;
22072 }
757a13d0 22073 }
cf2c3c16 22074 }
757a13d0 22075
cf2c3c16
TT
22076 return mac_ptr;
22077}
757a13d0 22078
cf2c3c16 22079/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22080 including DW_MACRO_import. */
cf2c3c16
TT
22081
22082static void
d521ce57
TT
22083dwarf_decode_macro_bytes (bfd *abfd,
22084 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22085 struct macro_source_file *current_file,
43f3e411 22086 struct line_header *lh,
cf2c3c16 22087 struct dwarf2_section_info *section,
36586728 22088 int section_is_gnu, int section_is_dwz,
cf2c3c16 22089 unsigned int offset_size,
8fc3fc34 22090 htab_t include_hash)
cf2c3c16 22091{
4d663531 22092 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22093 enum dwarf_macro_record_type macinfo_type;
22094 int at_commandline;
d521ce57 22095 const gdb_byte *opcode_definitions[256];
757a13d0 22096
cf2c3c16
TT
22097 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22098 &offset_size, section_is_gnu);
22099 if (mac_ptr == NULL)
22100 {
22101 /* We already issued a complaint. */
22102 return;
22103 }
757a13d0
JK
22104
22105 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22106 GDB is still reading the definitions from command line. First
22107 DW_MACINFO_start_file will need to be ignored as it was already executed
22108 to create CURRENT_FILE for the main source holding also the command line
22109 definitions. On first met DW_MACINFO_start_file this flag is reset to
22110 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22111
22112 at_commandline = 1;
22113
22114 do
22115 {
22116 /* Do we at least have room for a macinfo type byte? */
22117 if (mac_ptr >= mac_end)
22118 {
f664829e 22119 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22120 break;
22121 }
22122
aead7601 22123 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22124 mac_ptr++;
22125
cf2c3c16
TT
22126 /* Note that we rely on the fact that the corresponding GNU and
22127 DWARF constants are the same. */
757a13d0
JK
22128 switch (macinfo_type)
22129 {
22130 /* A zero macinfo type indicates the end of the macro
22131 information. */
22132 case 0:
22133 break;
2e276125 22134
0af92d60
JK
22135 case DW_MACRO_define:
22136 case DW_MACRO_undef:
22137 case DW_MACRO_define_strp:
22138 case DW_MACRO_undef_strp:
22139 case DW_MACRO_define_sup:
22140 case DW_MACRO_undef_sup:
2e276125 22141 {
891d2f0b 22142 unsigned int bytes_read;
2e276125 22143 int line;
d521ce57 22144 const char *body;
cf2c3c16 22145 int is_define;
2e276125 22146
cf2c3c16
TT
22147 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22148 mac_ptr += bytes_read;
22149
0af92d60
JK
22150 if (macinfo_type == DW_MACRO_define
22151 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22152 {
22153 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22154 mac_ptr += bytes_read;
22155 }
22156 else
22157 {
22158 LONGEST str_offset;
22159
22160 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22161 mac_ptr += offset_size;
2e276125 22162
0af92d60
JK
22163 if (macinfo_type == DW_MACRO_define_sup
22164 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22165 || section_is_dwz)
36586728
TT
22166 {
22167 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22168
22169 body = read_indirect_string_from_dwz (dwz, str_offset);
22170 }
22171 else
22172 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22173 }
22174
0af92d60
JK
22175 is_define = (macinfo_type == DW_MACRO_define
22176 || macinfo_type == DW_MACRO_define_strp
22177 || macinfo_type == DW_MACRO_define_sup);
2e276125 22178 if (! current_file)
757a13d0
JK
22179 {
22180 /* DWARF violation as no main source is present. */
22181 complaint (&symfile_complaints,
22182 _("debug info with no main source gives macro %s "
22183 "on line %d: %s"),
cf2c3c16
TT
22184 is_define ? _("definition") : _("undefinition"),
22185 line, body);
757a13d0
JK
22186 break;
22187 }
3e43a32a
MS
22188 if ((line == 0 && !at_commandline)
22189 || (line != 0 && at_commandline))
4d3c2250 22190 complaint (&symfile_complaints,
757a13d0
JK
22191 _("debug info gives %s macro %s with %s line %d: %s"),
22192 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22193 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22194 line == 0 ? _("zero") : _("non-zero"), line, body);
22195
cf2c3c16 22196 if (is_define)
757a13d0 22197 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22198 else
22199 {
0af92d60
JK
22200 gdb_assert (macinfo_type == DW_MACRO_undef
22201 || macinfo_type == DW_MACRO_undef_strp
22202 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22203 macro_undef (current_file, line, body);
22204 }
2e276125
JB
22205 }
22206 break;
22207
0af92d60 22208 case DW_MACRO_start_file:
2e276125 22209 {
891d2f0b 22210 unsigned int bytes_read;
2e276125
JB
22211 int line, file;
22212
22213 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22214 mac_ptr += bytes_read;
22215 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22216 mac_ptr += bytes_read;
22217
3e43a32a
MS
22218 if ((line == 0 && !at_commandline)
22219 || (line != 0 && at_commandline))
757a13d0
JK
22220 complaint (&symfile_complaints,
22221 _("debug info gives source %d included "
22222 "from %s at %s line %d"),
22223 file, at_commandline ? _("command-line") : _("file"),
22224 line == 0 ? _("zero") : _("non-zero"), line);
22225
22226 if (at_commandline)
22227 {
0af92d60 22228 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22229 pass one. */
757a13d0
JK
22230 at_commandline = 0;
22231 }
22232 else
43f3e411 22233 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22234 }
22235 break;
22236
0af92d60 22237 case DW_MACRO_end_file:
2e276125 22238 if (! current_file)
4d3c2250 22239 complaint (&symfile_complaints,
3e43a32a
MS
22240 _("macro debug info has an unmatched "
22241 "`close_file' directive"));
2e276125
JB
22242 else
22243 {
22244 current_file = current_file->included_by;
22245 if (! current_file)
22246 {
cf2c3c16 22247 enum dwarf_macro_record_type next_type;
2e276125
JB
22248
22249 /* GCC circa March 2002 doesn't produce the zero
22250 type byte marking the end of the compilation
22251 unit. Complain if it's not there, but exit no
22252 matter what. */
22253
22254 /* Do we at least have room for a macinfo type byte? */
22255 if (mac_ptr >= mac_end)
22256 {
f664829e 22257 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22258 return;
22259 }
22260
22261 /* We don't increment mac_ptr here, so this is just
22262 a look-ahead. */
aead7601
SM
22263 next_type
22264 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22265 mac_ptr);
2e276125 22266 if (next_type != 0)
4d3c2250 22267 complaint (&symfile_complaints,
3e43a32a
MS
22268 _("no terminating 0-type entry for "
22269 "macros in `.debug_macinfo' section"));
2e276125
JB
22270
22271 return;
22272 }
22273 }
22274 break;
22275
0af92d60
JK
22276 case DW_MACRO_import:
22277 case DW_MACRO_import_sup:
cf2c3c16
TT
22278 {
22279 LONGEST offset;
8fc3fc34 22280 void **slot;
a036ba48
TT
22281 bfd *include_bfd = abfd;
22282 struct dwarf2_section_info *include_section = section;
d521ce57 22283 const gdb_byte *include_mac_end = mac_end;
a036ba48 22284 int is_dwz = section_is_dwz;
d521ce57 22285 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22286
22287 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22288 mac_ptr += offset_size;
22289
0af92d60 22290 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22291 {
22292 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22293
4d663531 22294 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22295
a036ba48 22296 include_section = &dwz->macro;
a32a8923 22297 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22298 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22299 is_dwz = 1;
22300 }
22301
22302 new_mac_ptr = include_section->buffer + offset;
22303 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22304
8fc3fc34
TT
22305 if (*slot != NULL)
22306 {
22307 /* This has actually happened; see
22308 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22309 complaint (&symfile_complaints,
0af92d60 22310 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22311 ".debug_macro section"));
22312 }
22313 else
22314 {
d521ce57 22315 *slot = (void *) new_mac_ptr;
36586728 22316
a036ba48 22317 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22318 include_mac_end, current_file, lh,
36586728 22319 section, section_is_gnu, is_dwz,
4d663531 22320 offset_size, include_hash);
8fc3fc34 22321
d521ce57 22322 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22323 }
cf2c3c16
TT
22324 }
22325 break;
22326
2e276125 22327 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22328 if (!section_is_gnu)
22329 {
22330 unsigned int bytes_read;
2e276125 22331
ac298888
TT
22332 /* This reads the constant, but since we don't recognize
22333 any vendor extensions, we ignore it. */
22334 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22335 mac_ptr += bytes_read;
22336 read_direct_string (abfd, mac_ptr, &bytes_read);
22337 mac_ptr += bytes_read;
2e276125 22338
cf2c3c16
TT
22339 /* We don't recognize any vendor extensions. */
22340 break;
22341 }
22342 /* FALLTHROUGH */
22343
22344 default:
22345 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22346 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22347 section);
22348 if (mac_ptr == NULL)
22349 return;
22350 break;
2e276125 22351 }
757a13d0 22352 } while (macinfo_type != 0);
2e276125 22353}
8e19ed76 22354
cf2c3c16 22355static void
09262596 22356dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22357 int section_is_gnu)
cf2c3c16 22358{
bb5ed363 22359 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22360 struct line_header *lh = cu->line_header;
22361 bfd *abfd;
d521ce57 22362 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22363 struct macro_source_file *current_file = 0;
22364 enum dwarf_macro_record_type macinfo_type;
22365 unsigned int offset_size = cu->header.offset_size;
d521ce57 22366 const gdb_byte *opcode_definitions[256];
8fc3fc34 22367 void **slot;
09262596
DE
22368 struct dwarf2_section_info *section;
22369 const char *section_name;
22370
22371 if (cu->dwo_unit != NULL)
22372 {
22373 if (section_is_gnu)
22374 {
22375 section = &cu->dwo_unit->dwo_file->sections.macro;
22376 section_name = ".debug_macro.dwo";
22377 }
22378 else
22379 {
22380 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22381 section_name = ".debug_macinfo.dwo";
22382 }
22383 }
22384 else
22385 {
22386 if (section_is_gnu)
22387 {
22388 section = &dwarf2_per_objfile->macro;
22389 section_name = ".debug_macro";
22390 }
22391 else
22392 {
22393 section = &dwarf2_per_objfile->macinfo;
22394 section_name = ".debug_macinfo";
22395 }
22396 }
cf2c3c16 22397
bb5ed363 22398 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22399 if (section->buffer == NULL)
22400 {
fceca515 22401 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22402 return;
22403 }
a32a8923 22404 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22405
22406 /* First pass: Find the name of the base filename.
22407 This filename is needed in order to process all macros whose definition
22408 (or undefinition) comes from the command line. These macros are defined
22409 before the first DW_MACINFO_start_file entry, and yet still need to be
22410 associated to the base file.
22411
22412 To determine the base file name, we scan the macro definitions until we
22413 reach the first DW_MACINFO_start_file entry. We then initialize
22414 CURRENT_FILE accordingly so that any macro definition found before the
22415 first DW_MACINFO_start_file can still be associated to the base file. */
22416
22417 mac_ptr = section->buffer + offset;
22418 mac_end = section->buffer + section->size;
22419
22420 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22421 &offset_size, section_is_gnu);
22422 if (mac_ptr == NULL)
22423 {
22424 /* We already issued a complaint. */
22425 return;
22426 }
22427
22428 do
22429 {
22430 /* Do we at least have room for a macinfo type byte? */
22431 if (mac_ptr >= mac_end)
22432 {
22433 /* Complaint is printed during the second pass as GDB will probably
22434 stop the first pass earlier upon finding
22435 DW_MACINFO_start_file. */
22436 break;
22437 }
22438
aead7601 22439 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22440 mac_ptr++;
22441
22442 /* Note that we rely on the fact that the corresponding GNU and
22443 DWARF constants are the same. */
22444 switch (macinfo_type)
22445 {
22446 /* A zero macinfo type indicates the end of the macro
22447 information. */
22448 case 0:
22449 break;
22450
0af92d60
JK
22451 case DW_MACRO_define:
22452 case DW_MACRO_undef:
cf2c3c16
TT
22453 /* Only skip the data by MAC_PTR. */
22454 {
22455 unsigned int bytes_read;
22456
22457 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22458 mac_ptr += bytes_read;
22459 read_direct_string (abfd, mac_ptr, &bytes_read);
22460 mac_ptr += bytes_read;
22461 }
22462 break;
22463
0af92d60 22464 case DW_MACRO_start_file:
cf2c3c16
TT
22465 {
22466 unsigned int bytes_read;
22467 int line, file;
22468
22469 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22470 mac_ptr += bytes_read;
22471 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22472 mac_ptr += bytes_read;
22473
43f3e411 22474 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22475 }
22476 break;
22477
0af92d60 22478 case DW_MACRO_end_file:
cf2c3c16
TT
22479 /* No data to skip by MAC_PTR. */
22480 break;
22481
0af92d60
JK
22482 case DW_MACRO_define_strp:
22483 case DW_MACRO_undef_strp:
22484 case DW_MACRO_define_sup:
22485 case DW_MACRO_undef_sup:
cf2c3c16
TT
22486 {
22487 unsigned int bytes_read;
22488
22489 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22490 mac_ptr += bytes_read;
22491 mac_ptr += offset_size;
22492 }
22493 break;
22494
0af92d60
JK
22495 case DW_MACRO_import:
22496 case DW_MACRO_import_sup:
cf2c3c16 22497 /* Note that, according to the spec, a transparent include
0af92d60 22498 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22499 skip this opcode. */
22500 mac_ptr += offset_size;
22501 break;
22502
22503 case DW_MACINFO_vendor_ext:
22504 /* Only skip the data by MAC_PTR. */
22505 if (!section_is_gnu)
22506 {
22507 unsigned int bytes_read;
22508
22509 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22510 mac_ptr += bytes_read;
22511 read_direct_string (abfd, mac_ptr, &bytes_read);
22512 mac_ptr += bytes_read;
22513 }
22514 /* FALLTHROUGH */
22515
22516 default:
22517 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22518 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22519 section);
22520 if (mac_ptr == NULL)
22521 return;
22522 break;
22523 }
22524 } while (macinfo_type != 0 && current_file == NULL);
22525
22526 /* Second pass: Process all entries.
22527
22528 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22529 command-line macro definitions/undefinitions. This flag is unset when we
22530 reach the first DW_MACINFO_start_file entry. */
22531
fc4007c9
TT
22532 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22533 htab_eq_pointer,
22534 NULL, xcalloc, xfree));
8fc3fc34 22535 mac_ptr = section->buffer + offset;
fc4007c9 22536 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22537 *slot = (void *) mac_ptr;
8fc3fc34 22538 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22539 current_file, lh, section,
fc4007c9
TT
22540 section_is_gnu, 0, offset_size,
22541 include_hash.get ());
cf2c3c16
TT
22542}
22543
8e19ed76 22544/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22545 if so return true else false. */
380bca97 22546
8e19ed76 22547static int
6e5a29e1 22548attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22549{
22550 return (attr == NULL ? 0 :
22551 attr->form == DW_FORM_block1
22552 || attr->form == DW_FORM_block2
22553 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22554 || attr->form == DW_FORM_block
22555 || attr->form == DW_FORM_exprloc);
8e19ed76 22556}
4c2df51b 22557
c6a0999f
JB
22558/* Return non-zero if ATTR's value is a section offset --- classes
22559 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22560 You may use DW_UNSND (attr) to retrieve such offsets.
22561
22562 Section 7.5.4, "Attribute Encodings", explains that no attribute
22563 may have a value that belongs to more than one of these classes; it
22564 would be ambiguous if we did, because we use the same forms for all
22565 of them. */
380bca97 22566
3690dd37 22567static int
6e5a29e1 22568attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22569{
22570 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22571 || attr->form == DW_FORM_data8
22572 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22573}
22574
3690dd37
JB
22575/* Return non-zero if ATTR's value falls in the 'constant' class, or
22576 zero otherwise. When this function returns true, you can apply
22577 dwarf2_get_attr_constant_value to it.
22578
22579 However, note that for some attributes you must check
22580 attr_form_is_section_offset before using this test. DW_FORM_data4
22581 and DW_FORM_data8 are members of both the constant class, and of
22582 the classes that contain offsets into other debug sections
22583 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22584 that, if an attribute's can be either a constant or one of the
22585 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22586 taken as section offsets, not constants.
22587
22588 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22589 cannot handle that. */
380bca97 22590
3690dd37 22591static int
6e5a29e1 22592attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22593{
22594 switch (attr->form)
22595 {
22596 case DW_FORM_sdata:
22597 case DW_FORM_udata:
22598 case DW_FORM_data1:
22599 case DW_FORM_data2:
22600 case DW_FORM_data4:
22601 case DW_FORM_data8:
663c44ac 22602 case DW_FORM_implicit_const:
3690dd37
JB
22603 return 1;
22604 default:
22605 return 0;
22606 }
22607}
22608
7771576e
SA
22609
22610/* DW_ADDR is always stored already as sect_offset; despite for the forms
22611 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22612
22613static int
6e5a29e1 22614attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22615{
22616 switch (attr->form)
22617 {
22618 case DW_FORM_ref_addr:
22619 case DW_FORM_ref1:
22620 case DW_FORM_ref2:
22621 case DW_FORM_ref4:
22622 case DW_FORM_ref8:
22623 case DW_FORM_ref_udata:
22624 case DW_FORM_GNU_ref_alt:
22625 return 1;
22626 default:
22627 return 0;
22628 }
22629}
22630
3019eac3
DE
22631/* Return the .debug_loc section to use for CU.
22632 For DWO files use .debug_loc.dwo. */
22633
22634static struct dwarf2_section_info *
22635cu_debug_loc_section (struct dwarf2_cu *cu)
22636{
22637 if (cu->dwo_unit)
43988095
JK
22638 {
22639 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22640
22641 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22642 }
22643 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22644 : &dwarf2_per_objfile->loc);
3019eac3
DE
22645}
22646
8cf6f0b1
TT
22647/* A helper function that fills in a dwarf2_loclist_baton. */
22648
22649static void
22650fill_in_loclist_baton (struct dwarf2_cu *cu,
22651 struct dwarf2_loclist_baton *baton,
ff39bb5e 22652 const struct attribute *attr)
8cf6f0b1 22653{
3019eac3
DE
22654 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22655
22656 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22657
22658 baton->per_cu = cu->per_cu;
22659 gdb_assert (baton->per_cu);
22660 /* We don't know how long the location list is, but make sure we
22661 don't run off the edge of the section. */
3019eac3
DE
22662 baton->size = section->size - DW_UNSND (attr);
22663 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22664 baton->base_address = cu->base_address;
f664829e 22665 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22666}
22667
4c2df51b 22668static void
ff39bb5e 22669dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22670 struct dwarf2_cu *cu, int is_block)
4c2df51b 22671{
bb5ed363 22672 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22673 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22674
3690dd37 22675 if (attr_form_is_section_offset (attr)
3019eac3 22676 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22677 the section. If so, fall through to the complaint in the
22678 other branch. */
3019eac3 22679 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22680 {
0d53c4c4 22681 struct dwarf2_loclist_baton *baton;
4c2df51b 22682
8d749320 22683 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22684
8cf6f0b1 22685 fill_in_loclist_baton (cu, baton, attr);
be391dca 22686
d00adf39 22687 if (cu->base_known == 0)
0d53c4c4 22688 complaint (&symfile_complaints,
3e43a32a
MS
22689 _("Location list used without "
22690 "specifying the CU base address."));
4c2df51b 22691
f1e6e072
TT
22692 SYMBOL_ACLASS_INDEX (sym) = (is_block
22693 ? dwarf2_loclist_block_index
22694 : dwarf2_loclist_index);
0d53c4c4
DJ
22695 SYMBOL_LOCATION_BATON (sym) = baton;
22696 }
22697 else
22698 {
22699 struct dwarf2_locexpr_baton *baton;
22700
8d749320 22701 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22702 baton->per_cu = cu->per_cu;
22703 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22704
22705 if (attr_form_is_block (attr))
22706 {
22707 /* Note that we're just copying the block's data pointer
22708 here, not the actual data. We're still pointing into the
6502dd73
DJ
22709 info_buffer for SYM's objfile; right now we never release
22710 that buffer, but when we do clean up properly this may
22711 need to change. */
0d53c4c4
DJ
22712 baton->size = DW_BLOCK (attr)->size;
22713 baton->data = DW_BLOCK (attr)->data;
22714 }
22715 else
22716 {
22717 dwarf2_invalid_attrib_class_complaint ("location description",
22718 SYMBOL_NATURAL_NAME (sym));
22719 baton->size = 0;
0d53c4c4 22720 }
6e70227d 22721
f1e6e072
TT
22722 SYMBOL_ACLASS_INDEX (sym) = (is_block
22723 ? dwarf2_locexpr_block_index
22724 : dwarf2_locexpr_index);
0d53c4c4
DJ
22725 SYMBOL_LOCATION_BATON (sym) = baton;
22726 }
4c2df51b 22727}
6502dd73 22728
9aa1f1e3
TT
22729/* Return the OBJFILE associated with the compilation unit CU. If CU
22730 came from a separate debuginfo file, then the master objfile is
22731 returned. */
ae0d2f24
UW
22732
22733struct objfile *
22734dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22735{
9291a0cd 22736 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22737
22738 /* Return the master objfile, so that we can report and look up the
22739 correct file containing this variable. */
22740 if (objfile->separate_debug_objfile_backlink)
22741 objfile = objfile->separate_debug_objfile_backlink;
22742
22743 return objfile;
22744}
22745
96408a79
SA
22746/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22747 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22748 CU_HEADERP first. */
22749
22750static const struct comp_unit_head *
22751per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22752 struct dwarf2_per_cu_data *per_cu)
22753{
d521ce57 22754 const gdb_byte *info_ptr;
96408a79
SA
22755
22756 if (per_cu->cu)
22757 return &per_cu->cu->header;
22758
9c541725 22759 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22760
22761 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22762 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22763 rcuh_kind::COMPILE);
96408a79
SA
22764
22765 return cu_headerp;
22766}
22767
ae0d2f24
UW
22768/* Return the address size given in the compilation unit header for CU. */
22769
98714339 22770int
ae0d2f24
UW
22771dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22772{
96408a79
SA
22773 struct comp_unit_head cu_header_local;
22774 const struct comp_unit_head *cu_headerp;
c471e790 22775
96408a79
SA
22776 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22777
22778 return cu_headerp->addr_size;
ae0d2f24
UW
22779}
22780
9eae7c52
TT
22781/* Return the offset size given in the compilation unit header for CU. */
22782
22783int
22784dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22785{
96408a79
SA
22786 struct comp_unit_head cu_header_local;
22787 const struct comp_unit_head *cu_headerp;
9c6c53f7 22788
96408a79
SA
22789 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22790
22791 return cu_headerp->offset_size;
22792}
22793
22794/* See its dwarf2loc.h declaration. */
22795
22796int
22797dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22798{
22799 struct comp_unit_head cu_header_local;
22800 const struct comp_unit_head *cu_headerp;
22801
22802 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22803
22804 if (cu_headerp->version == 2)
22805 return cu_headerp->addr_size;
22806 else
22807 return cu_headerp->offset_size;
181cebd4
JK
22808}
22809
9aa1f1e3
TT
22810/* Return the text offset of the CU. The returned offset comes from
22811 this CU's objfile. If this objfile came from a separate debuginfo
22812 file, then the offset may be different from the corresponding
22813 offset in the parent objfile. */
22814
22815CORE_ADDR
22816dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22817{
bb3fa9d0 22818 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22819
22820 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22821}
22822
43988095
JK
22823/* Return DWARF version number of PER_CU. */
22824
22825short
22826dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22827{
22828 return per_cu->dwarf_version;
22829}
22830
348e048f
DE
22831/* Locate the .debug_info compilation unit from CU's objfile which contains
22832 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22833
22834static struct dwarf2_per_cu_data *
9c541725 22835dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22836 unsigned int offset_in_dwz,
ae038cb0
DJ
22837 struct objfile *objfile)
22838{
22839 struct dwarf2_per_cu_data *this_cu;
22840 int low, high;
36586728 22841 const sect_offset *cu_off;
ae038cb0 22842
ae038cb0
DJ
22843 low = 0;
22844 high = dwarf2_per_objfile->n_comp_units - 1;
22845 while (high > low)
22846 {
36586728 22847 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22848 int mid = low + (high - low) / 2;
9a619af0 22849
36586728 22850 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22851 cu_off = &mid_cu->sect_off;
36586728 22852 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22853 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22854 high = mid;
22855 else
22856 low = mid + 1;
22857 }
22858 gdb_assert (low == high);
36586728 22859 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22860 cu_off = &this_cu->sect_off;
22861 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22862 {
36586728 22863 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22864 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22865 "offset 0x%x [in module %s]"),
22866 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22867
9c541725
PA
22868 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22869 <= sect_off);
ae038cb0
DJ
22870 return dwarf2_per_objfile->all_comp_units[low-1];
22871 }
22872 else
22873 {
22874 this_cu = dwarf2_per_objfile->all_comp_units[low];
22875 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22876 && sect_off >= this_cu->sect_off + this_cu->length)
22877 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22878 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22879 return this_cu;
22880 }
22881}
22882
23745b47 22883/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22884
9816fde3 22885static void
23745b47 22886init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22887{
9816fde3 22888 memset (cu, 0, sizeof (*cu));
23745b47
DE
22889 per_cu->cu = cu;
22890 cu->per_cu = per_cu;
22891 cu->objfile = per_cu->objfile;
93311388 22892 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22893}
22894
22895/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22896
22897static void
95554aad
TT
22898prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22899 enum language pretend_language)
9816fde3
JK
22900{
22901 struct attribute *attr;
22902
22903 /* Set the language we're debugging. */
22904 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22905 if (attr)
22906 set_cu_language (DW_UNSND (attr), cu);
22907 else
9cded63f 22908 {
95554aad 22909 cu->language = pretend_language;
9cded63f
TT
22910 cu->language_defn = language_def (cu->language);
22911 }
dee91e82 22912
7d45c7c3 22913 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22914}
22915
ae038cb0
DJ
22916/* Release one cached compilation unit, CU. We unlink it from the tree
22917 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22918 the caller is responsible for that.
22919 NOTE: DATA is a void * because this function is also used as a
22920 cleanup routine. */
ae038cb0
DJ
22921
22922static void
68dc6402 22923free_heap_comp_unit (void *data)
ae038cb0 22924{
9a3c8263 22925 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22926
23745b47
DE
22927 gdb_assert (cu->per_cu != NULL);
22928 cu->per_cu->cu = NULL;
ae038cb0
DJ
22929 cu->per_cu = NULL;
22930
22931 obstack_free (&cu->comp_unit_obstack, NULL);
22932
22933 xfree (cu);
22934}
22935
72bf9492 22936/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22937 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22938 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22939
22940static void
22941free_stack_comp_unit (void *data)
22942{
9a3c8263 22943 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22944
23745b47
DE
22945 gdb_assert (cu->per_cu != NULL);
22946 cu->per_cu->cu = NULL;
22947 cu->per_cu = NULL;
22948
72bf9492
DJ
22949 obstack_free (&cu->comp_unit_obstack, NULL);
22950 cu->partial_dies = NULL;
ae038cb0
DJ
22951}
22952
22953/* Free all cached compilation units. */
22954
22955static void
22956free_cached_comp_units (void *data)
22957{
330cdd98 22958 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
22959}
22960
22961/* Increase the age counter on each cached compilation unit, and free
22962 any that are too old. */
22963
22964static void
22965age_cached_comp_units (void)
22966{
22967 struct dwarf2_per_cu_data *per_cu, **last_chain;
22968
22969 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22970 per_cu = dwarf2_per_objfile->read_in_chain;
22971 while (per_cu != NULL)
22972 {
22973 per_cu->cu->last_used ++;
b4f54984 22974 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22975 dwarf2_mark (per_cu->cu);
22976 per_cu = per_cu->cu->read_in_chain;
22977 }
22978
22979 per_cu = dwarf2_per_objfile->read_in_chain;
22980 last_chain = &dwarf2_per_objfile->read_in_chain;
22981 while (per_cu != NULL)
22982 {
22983 struct dwarf2_per_cu_data *next_cu;
22984
22985 next_cu = per_cu->cu->read_in_chain;
22986
22987 if (!per_cu->cu->mark)
22988 {
68dc6402 22989 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22990 *last_chain = next_cu;
22991 }
22992 else
22993 last_chain = &per_cu->cu->read_in_chain;
22994
22995 per_cu = next_cu;
22996 }
22997}
22998
22999/* Remove a single compilation unit from the cache. */
23000
23001static void
dee91e82 23002free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23003{
23004 struct dwarf2_per_cu_data *per_cu, **last_chain;
23005
23006 per_cu = dwarf2_per_objfile->read_in_chain;
23007 last_chain = &dwarf2_per_objfile->read_in_chain;
23008 while (per_cu != NULL)
23009 {
23010 struct dwarf2_per_cu_data *next_cu;
23011
23012 next_cu = per_cu->cu->read_in_chain;
23013
dee91e82 23014 if (per_cu == target_per_cu)
ae038cb0 23015 {
68dc6402 23016 free_heap_comp_unit (per_cu->cu);
dee91e82 23017 per_cu->cu = NULL;
ae038cb0
DJ
23018 *last_chain = next_cu;
23019 break;
23020 }
23021 else
23022 last_chain = &per_cu->cu->read_in_chain;
23023
23024 per_cu = next_cu;
23025 }
23026}
23027
fe3e1990
DJ
23028/* Release all extra memory associated with OBJFILE. */
23029
23030void
23031dwarf2_free_objfile (struct objfile *objfile)
23032{
9a3c8263
SM
23033 dwarf2_per_objfile
23034 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23035 dwarf2_objfile_data_key);
fe3e1990
DJ
23036
23037 if (dwarf2_per_objfile == NULL)
23038 return;
23039
330cdd98 23040 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23041}
23042
dee91e82
DE
23043/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23044 We store these in a hash table separate from the DIEs, and preserve them
23045 when the DIEs are flushed out of cache.
23046
23047 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23048 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23049 or the type may come from a DWO file. Furthermore, while it's more logical
23050 to use per_cu->section+offset, with Fission the section with the data is in
23051 the DWO file but we don't know that section at the point we need it.
23052 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23053 because we can enter the lookup routine, get_die_type_at_offset, from
23054 outside this file, and thus won't necessarily have PER_CU->cu.
23055 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23056
dee91e82 23057struct dwarf2_per_cu_offset_and_type
1c379e20 23058{
dee91e82 23059 const struct dwarf2_per_cu_data *per_cu;
9c541725 23060 sect_offset sect_off;
1c379e20
DJ
23061 struct type *type;
23062};
23063
dee91e82 23064/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23065
23066static hashval_t
dee91e82 23067per_cu_offset_and_type_hash (const void *item)
1c379e20 23068{
9a3c8263
SM
23069 const struct dwarf2_per_cu_offset_and_type *ofs
23070 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23071
9c541725 23072 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23073}
23074
dee91e82 23075/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23076
23077static int
dee91e82 23078per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23079{
9a3c8263
SM
23080 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23081 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23082 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23083 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23084
dee91e82 23085 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23086 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23087}
23088
23089/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23090 table if necessary. For convenience, return TYPE.
23091
23092 The DIEs reading must have careful ordering to:
23093 * Not cause infite loops trying to read in DIEs as a prerequisite for
23094 reading current DIE.
23095 * Not trying to dereference contents of still incompletely read in types
23096 while reading in other DIEs.
23097 * Enable referencing still incompletely read in types just by a pointer to
23098 the type without accessing its fields.
23099
23100 Therefore caller should follow these rules:
23101 * Try to fetch any prerequisite types we may need to build this DIE type
23102 before building the type and calling set_die_type.
e71ec853 23103 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23104 possible before fetching more types to complete the current type.
23105 * Make the type as complete as possible before fetching more types. */
1c379e20 23106
f792889a 23107static struct type *
1c379e20
DJ
23108set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23109{
dee91e82 23110 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23111 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23112 struct attribute *attr;
23113 struct dynamic_prop prop;
1c379e20 23114
b4ba55a1
JB
23115 /* For Ada types, make sure that the gnat-specific data is always
23116 initialized (if not already set). There are a few types where
23117 we should not be doing so, because the type-specific area is
23118 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23119 where the type-specific area is used to store the floatformat).
23120 But this is not a problem, because the gnat-specific information
23121 is actually not needed for these types. */
23122 if (need_gnat_info (cu)
23123 && TYPE_CODE (type) != TYPE_CODE_FUNC
23124 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23125 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23126 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23127 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23128 && !HAVE_GNAT_AUX_INFO (type))
23129 INIT_GNAT_SPECIFIC (type);
23130
3f2f83dd
KB
23131 /* Read DW_AT_allocated and set in type. */
23132 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23133 if (attr_form_is_block (attr))
23134 {
23135 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23136 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23137 }
23138 else if (attr != NULL)
23139 {
23140 complaint (&symfile_complaints,
9c541725
PA
23141 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23142 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23143 to_underlying (die->sect_off));
3f2f83dd
KB
23144 }
23145
23146 /* Read DW_AT_associated and set in type. */
23147 attr = dwarf2_attr (die, DW_AT_associated, cu);
23148 if (attr_form_is_block (attr))
23149 {
23150 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23151 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23152 }
23153 else if (attr != NULL)
23154 {
23155 complaint (&symfile_complaints,
9c541725
PA
23156 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23157 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23158 to_underlying (die->sect_off));
3f2f83dd
KB
23159 }
23160
3cdcd0ce
JB
23161 /* Read DW_AT_data_location and set in type. */
23162 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23163 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23164 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23165
dee91e82 23166 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23167 {
dee91e82
DE
23168 dwarf2_per_objfile->die_type_hash =
23169 htab_create_alloc_ex (127,
23170 per_cu_offset_and_type_hash,
23171 per_cu_offset_and_type_eq,
23172 NULL,
23173 &objfile->objfile_obstack,
23174 hashtab_obstack_allocate,
23175 dummy_obstack_deallocate);
f792889a 23176 }
1c379e20 23177
dee91e82 23178 ofs.per_cu = cu->per_cu;
9c541725 23179 ofs.sect_off = die->sect_off;
1c379e20 23180 ofs.type = type;
dee91e82
DE
23181 slot = (struct dwarf2_per_cu_offset_and_type **)
23182 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23183 if (*slot)
23184 complaint (&symfile_complaints,
23185 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23186 to_underlying (die->sect_off));
8d749320
SM
23187 *slot = XOBNEW (&objfile->objfile_obstack,
23188 struct dwarf2_per_cu_offset_and_type);
1c379e20 23189 **slot = ofs;
f792889a 23190 return type;
1c379e20
DJ
23191}
23192
9c541725 23193/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23194 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23195
23196static struct type *
9c541725 23197get_die_type_at_offset (sect_offset sect_off,
673bfd45 23198 struct dwarf2_per_cu_data *per_cu)
1c379e20 23199{
dee91e82 23200 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23201
dee91e82 23202 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23203 return NULL;
1c379e20 23204
dee91e82 23205 ofs.per_cu = per_cu;
9c541725 23206 ofs.sect_off = sect_off;
9a3c8263
SM
23207 slot = ((struct dwarf2_per_cu_offset_and_type *)
23208 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23209 if (slot)
23210 return slot->type;
23211 else
23212 return NULL;
23213}
23214
02142a6c 23215/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23216 or return NULL if DIE does not have a saved type. */
23217
23218static struct type *
23219get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23220{
9c541725 23221 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23222}
23223
10b3939b
DJ
23224/* Add a dependence relationship from CU to REF_PER_CU. */
23225
23226static void
23227dwarf2_add_dependence (struct dwarf2_cu *cu,
23228 struct dwarf2_per_cu_data *ref_per_cu)
23229{
23230 void **slot;
23231
23232 if (cu->dependencies == NULL)
23233 cu->dependencies
23234 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23235 NULL, &cu->comp_unit_obstack,
23236 hashtab_obstack_allocate,
23237 dummy_obstack_deallocate);
23238
23239 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23240 if (*slot == NULL)
23241 *slot = ref_per_cu;
23242}
1c379e20 23243
f504f079
DE
23244/* Subroutine of dwarf2_mark to pass to htab_traverse.
23245 Set the mark field in every compilation unit in the
ae038cb0
DJ
23246 cache that we must keep because we are keeping CU. */
23247
10b3939b
DJ
23248static int
23249dwarf2_mark_helper (void **slot, void *data)
23250{
23251 struct dwarf2_per_cu_data *per_cu;
23252
23253 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23254
23255 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23256 reading of the chain. As such dependencies remain valid it is not much
23257 useful to track and undo them during QUIT cleanups. */
23258 if (per_cu->cu == NULL)
23259 return 1;
23260
10b3939b
DJ
23261 if (per_cu->cu->mark)
23262 return 1;
23263 per_cu->cu->mark = 1;
23264
23265 if (per_cu->cu->dependencies != NULL)
23266 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23267
23268 return 1;
23269}
23270
f504f079
DE
23271/* Set the mark field in CU and in every other compilation unit in the
23272 cache that we must keep because we are keeping CU. */
23273
ae038cb0
DJ
23274static void
23275dwarf2_mark (struct dwarf2_cu *cu)
23276{
23277 if (cu->mark)
23278 return;
23279 cu->mark = 1;
10b3939b
DJ
23280 if (cu->dependencies != NULL)
23281 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23282}
23283
23284static void
23285dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23286{
23287 while (per_cu)
23288 {
23289 per_cu->cu->mark = 0;
23290 per_cu = per_cu->cu->read_in_chain;
23291 }
72bf9492
DJ
23292}
23293
72bf9492
DJ
23294/* Trivial hash function for partial_die_info: the hash value of a DIE
23295 is its offset in .debug_info for this objfile. */
23296
23297static hashval_t
23298partial_die_hash (const void *item)
23299{
9a3c8263
SM
23300 const struct partial_die_info *part_die
23301 = (const struct partial_die_info *) item;
9a619af0 23302
9c541725 23303 return to_underlying (part_die->sect_off);
72bf9492
DJ
23304}
23305
23306/* Trivial comparison function for partial_die_info structures: two DIEs
23307 are equal if they have the same offset. */
23308
23309static int
23310partial_die_eq (const void *item_lhs, const void *item_rhs)
23311{
9a3c8263
SM
23312 const struct partial_die_info *part_die_lhs
23313 = (const struct partial_die_info *) item_lhs;
23314 const struct partial_die_info *part_die_rhs
23315 = (const struct partial_die_info *) item_rhs;
9a619af0 23316
9c541725 23317 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23318}
23319
b4f54984
DE
23320static struct cmd_list_element *set_dwarf_cmdlist;
23321static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23322
23323static void
981a3fb3 23324set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23325{
b4f54984 23326 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23327 gdb_stdout);
ae038cb0
DJ
23328}
23329
23330static void
981a3fb3 23331show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23332{
b4f54984 23333 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23334}
23335
4bf44c1c 23336/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23337
23338static void
c1bd65d0 23339dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23340{
9a3c8263 23341 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23342 int ix;
8b70b953 23343
626f2d1c
TT
23344 /* Make sure we don't accidentally use dwarf2_per_objfile while
23345 cleaning up. */
23346 dwarf2_per_objfile = NULL;
23347
59b0c7c1
JB
23348 for (ix = 0; ix < data->n_comp_units; ++ix)
23349 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23350
59b0c7c1 23351 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23352 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23353 data->all_type_units[ix]->per_cu.imported_symtabs);
23354 xfree (data->all_type_units);
95554aad 23355
8b70b953 23356 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23357
23358 if (data->dwo_files)
23359 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23360 if (data->dwp_file)
23361 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23362
23363 if (data->dwz_file && data->dwz_file->dwz_bfd)
23364 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23365}
23366
23367\f
ae2de4f8 23368/* The "save gdb-index" command. */
9291a0cd 23369
bc8f2430
JK
23370/* In-memory buffer to prepare data to be written later to a file. */
23371class data_buf
9291a0cd 23372{
bc8f2430 23373public:
bc8f2430
JK
23374 /* Copy DATA to the end of the buffer. */
23375 template<typename T>
23376 void append_data (const T &data)
23377 {
23378 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23379 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23380 grow (sizeof (data)));
bc8f2430 23381 }
b89be57b 23382
c2f134ac
PA
23383 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23384 terminating zero is appended too. */
bc8f2430
JK
23385 void append_cstr0 (const char *cstr)
23386 {
23387 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23388 std::copy (cstr, cstr + size, grow (size));
23389 }
23390
23391 /* Accept a host-format integer in VAL and append it to the buffer
23392 as a target-format integer which is LEN bytes long. */
23393 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23394 {
23395 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23396 }
9291a0cd 23397
bc8f2430
JK
23398 /* Return the size of the buffer. */
23399 size_t size () const
23400 {
23401 return m_vec.size ();
23402 }
23403
23404 /* Write the buffer to FILE. */
23405 void file_write (FILE *file) const
23406 {
a81e6d4d
PA
23407 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23408 error (_("couldn't write data to file"));
bc8f2430
JK
23409 }
23410
23411private:
c2f134ac
PA
23412 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23413 the start of the new block. */
23414 gdb_byte *grow (size_t size)
23415 {
23416 m_vec.resize (m_vec.size () + size);
23417 return &*m_vec.end () - size;
23418 }
23419
d5722aa2 23420 gdb::byte_vector m_vec;
bc8f2430 23421};
9291a0cd
TT
23422
23423/* An entry in the symbol table. */
23424struct symtab_index_entry
23425{
23426 /* The name of the symbol. */
23427 const char *name;
23428 /* The offset of the name in the constant pool. */
23429 offset_type index_offset;
23430 /* A sorted vector of the indices of all the CUs that hold an object
23431 of this name. */
bc8f2430 23432 std::vector<offset_type> cu_indices;
9291a0cd
TT
23433};
23434
23435/* The symbol table. This is a power-of-2-sized hash table. */
23436struct mapped_symtab
23437{
bc8f2430
JK
23438 mapped_symtab ()
23439 {
23440 data.resize (1024);
23441 }
b89be57b 23442
bc8f2430 23443 offset_type n_elements = 0;
4b76cda9 23444 std::vector<symtab_index_entry> data;
bc8f2430 23445};
9291a0cd 23446
bc8f2430 23447/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23448 the slot.
23449
23450 Function is used only during write_hash_table so no index format backward
23451 compatibility is needed. */
b89be57b 23452
4b76cda9 23453static symtab_index_entry &
9291a0cd
TT
23454find_slot (struct mapped_symtab *symtab, const char *name)
23455{
559a7a62 23456 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23457
bc8f2430
JK
23458 index = hash & (symtab->data.size () - 1);
23459 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23460
23461 for (;;)
23462 {
4b76cda9
PA
23463 if (symtab->data[index].name == NULL
23464 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23465 return symtab->data[index];
23466 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23467 }
23468}
23469
23470/* Expand SYMTAB's hash table. */
b89be57b 23471
9291a0cd
TT
23472static void
23473hash_expand (struct mapped_symtab *symtab)
23474{
bc8f2430 23475 auto old_entries = std::move (symtab->data);
9291a0cd 23476
bc8f2430
JK
23477 symtab->data.clear ();
23478 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23479
bc8f2430 23480 for (auto &it : old_entries)
4b76cda9 23481 if (it.name != NULL)
bc8f2430 23482 {
4b76cda9 23483 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23484 ref = std::move (it);
23485 }
9291a0cd
TT
23486}
23487
156942c7
DE
23488/* Add an entry to SYMTAB. NAME is the name of the symbol.
23489 CU_INDEX is the index of the CU in which the symbol appears.
23490 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23491
9291a0cd
TT
23492static void
23493add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23494 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23495 offset_type cu_index)
23496{
156942c7 23497 offset_type cu_index_and_attrs;
9291a0cd
TT
23498
23499 ++symtab->n_elements;
bc8f2430 23500 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23501 hash_expand (symtab);
23502
4b76cda9
PA
23503 symtab_index_entry &slot = find_slot (symtab, name);
23504 if (slot.name == NULL)
9291a0cd 23505 {
4b76cda9 23506 slot.name = name;
156942c7 23507 /* index_offset is set later. */
9291a0cd 23508 }
156942c7
DE
23509
23510 cu_index_and_attrs = 0;
23511 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23512 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23513 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23514
23515 /* We don't want to record an index value twice as we want to avoid the
23516 duplication.
23517 We process all global symbols and then all static symbols
23518 (which would allow us to avoid the duplication by only having to check
23519 the last entry pushed), but a symbol could have multiple kinds in one CU.
23520 To keep things simple we don't worry about the duplication here and
23521 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23522 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23523}
23524
23525/* Sort and remove duplicates of all symbols' cu_indices lists. */
23526
23527static void
23528uniquify_cu_indices (struct mapped_symtab *symtab)
23529{
4b76cda9 23530 for (auto &entry : symtab->data)
156942c7 23531 {
4b76cda9 23532 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23533 {
4b76cda9 23534 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23535 std::sort (cu_indices.begin (), cu_indices.end ());
23536 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23537 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23538 }
23539 }
9291a0cd
TT
23540}
23541
bc8f2430
JK
23542/* A form of 'const char *' suitable for container keys. Only the
23543 pointer is stored. The strings themselves are compared, not the
23544 pointers. */
23545class c_str_view
9291a0cd 23546{
bc8f2430
JK
23547public:
23548 c_str_view (const char *cstr)
23549 : m_cstr (cstr)
23550 {}
9291a0cd 23551
bc8f2430
JK
23552 bool operator== (const c_str_view &other) const
23553 {
23554 return strcmp (m_cstr, other.m_cstr) == 0;
23555 }
9291a0cd 23556
bc8f2430
JK
23557private:
23558 friend class c_str_view_hasher;
23559 const char *const m_cstr;
23560};
9291a0cd 23561
bc8f2430
JK
23562/* A std::unordered_map::hasher for c_str_view that uses the right
23563 hash function for strings in a mapped index. */
23564class c_str_view_hasher
23565{
23566public:
23567 size_t operator () (const c_str_view &x) const
23568 {
23569 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23570 }
23571};
b89be57b 23572
bc8f2430
JK
23573/* A std::unordered_map::hasher for std::vector<>. */
23574template<typename T>
23575class vector_hasher
9291a0cd 23576{
bc8f2430
JK
23577public:
23578 size_t operator () (const std::vector<T> &key) const
23579 {
23580 return iterative_hash (key.data (),
23581 sizeof (key.front ()) * key.size (), 0);
23582 }
23583};
9291a0cd 23584
bc8f2430
JK
23585/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23586 constant pool entries going into the data buffer CPOOL. */
3876f04e 23587
bc8f2430
JK
23588static void
23589write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23590{
23591 {
23592 /* Elements are sorted vectors of the indices of all the CUs that
23593 hold an object of this name. */
23594 std::unordered_map<std::vector<offset_type>, offset_type,
23595 vector_hasher<offset_type>>
23596 symbol_hash_table;
23597
23598 /* We add all the index vectors to the constant pool first, to
23599 ensure alignment is ok. */
4b76cda9 23600 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23601 {
4b76cda9 23602 if (entry.name == NULL)
bc8f2430 23603 continue;
4b76cda9 23604 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23605
23606 /* Finding before inserting is faster than always trying to
23607 insert, because inserting always allocates a node, does the
23608 lookup, and then destroys the new node if another node
23609 already had the same key. C++17 try_emplace will avoid
23610 this. */
23611 const auto found
4b76cda9 23612 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23613 if (found != symbol_hash_table.end ())
23614 {
4b76cda9 23615 entry.index_offset = found->second;
70a1152b
PA
23616 continue;
23617 }
23618
4b76cda9
PA
23619 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23620 entry.index_offset = cpool.size ();
23621 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23622 for (const auto index : entry.cu_indices)
23623 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23624 }
23625 }
9291a0cd
TT
23626
23627 /* Now write out the hash table. */
bc8f2430 23628 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23629 for (const auto &entry : symtab->data)
9291a0cd
TT
23630 {
23631 offset_type str_off, vec_off;
23632
4b76cda9 23633 if (entry.name != NULL)
9291a0cd 23634 {
4b76cda9 23635 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23636 if (insertpair.second)
4b76cda9 23637 cpool.append_cstr0 (entry.name);
bc8f2430 23638 str_off = insertpair.first->second;
4b76cda9 23639 vec_off = entry.index_offset;
9291a0cd
TT
23640 }
23641 else
23642 {
23643 /* While 0 is a valid constant pool index, it is not valid
23644 to have 0 for both offsets. */
23645 str_off = 0;
23646 vec_off = 0;
23647 }
23648
bc8f2430
JK
23649 output.append_data (MAYBE_SWAP (str_off));
23650 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23651 }
9291a0cd
TT
23652}
23653
bc8f2430 23654typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23655
23656/* Helper struct for building the address table. */
23657struct addrmap_index_data
23658{
bc8f2430
JK
23659 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23660 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23661 {}
23662
0a5429f6 23663 struct objfile *objfile;
bc8f2430
JK
23664 data_buf &addr_vec;
23665 psym_index_map &cu_index_htab;
0a5429f6
DE
23666
23667 /* Non-zero if the previous_* fields are valid.
23668 We can't write an entry until we see the next entry (since it is only then
23669 that we know the end of the entry). */
23670 int previous_valid;
23671 /* Index of the CU in the table of all CUs in the index file. */
23672 unsigned int previous_cu_index;
0963b4bd 23673 /* Start address of the CU. */
0a5429f6
DE
23674 CORE_ADDR previous_cu_start;
23675};
23676
bc8f2430 23677/* Write an address entry to ADDR_VEC. */
b89be57b 23678
9291a0cd 23679static void
bc8f2430 23680add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23681 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23682{
9291a0cd
TT
23683 CORE_ADDR baseaddr;
23684
23685 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23686
c2f134ac
PA
23687 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23688 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23689 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23690}
23691
23692/* Worker function for traversing an addrmap to build the address table. */
23693
23694static int
23695add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23696{
9a3c8263
SM
23697 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23698 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23699
23700 if (data->previous_valid)
bc8f2430 23701 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23702 data->previous_cu_start, start_addr,
23703 data->previous_cu_index);
23704
23705 data->previous_cu_start = start_addr;
23706 if (pst != NULL)
23707 {
bc8f2430
JK
23708 const auto it = data->cu_index_htab.find (pst);
23709 gdb_assert (it != data->cu_index_htab.cend ());
23710 data->previous_cu_index = it->second;
0a5429f6
DE
23711 data->previous_valid = 1;
23712 }
23713 else
bc8f2430 23714 data->previous_valid = 0;
0a5429f6
DE
23715
23716 return 0;
23717}
23718
bc8f2430 23719/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23720 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23721 in the index file. */
23722
23723static void
bc8f2430
JK
23724write_address_map (struct objfile *objfile, data_buf &addr_vec,
23725 psym_index_map &cu_index_htab)
0a5429f6 23726{
bc8f2430 23727 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23728
23729 /* When writing the address table, we have to cope with the fact that
23730 the addrmap iterator only provides the start of a region; we have to
23731 wait until the next invocation to get the start of the next region. */
23732
23733 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23734 addrmap_index_data.previous_valid = 0;
23735
23736 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23737 &addrmap_index_data);
23738
23739 /* It's highly unlikely the last entry (end address = 0xff...ff)
23740 is valid, but we should still handle it.
23741 The end address is recorded as the start of the next region, but that
23742 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23743 anyway. */
23744 if (addrmap_index_data.previous_valid)
bc8f2430 23745 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23746 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23747 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23748}
23749
156942c7
DE
23750/* Return the symbol kind of PSYM. */
23751
23752static gdb_index_symbol_kind
23753symbol_kind (struct partial_symbol *psym)
23754{
23755 domain_enum domain = PSYMBOL_DOMAIN (psym);
23756 enum address_class aclass = PSYMBOL_CLASS (psym);
23757
23758 switch (domain)
23759 {
23760 case VAR_DOMAIN:
23761 switch (aclass)
23762 {
23763 case LOC_BLOCK:
23764 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23765 case LOC_TYPEDEF:
23766 return GDB_INDEX_SYMBOL_KIND_TYPE;
23767 case LOC_COMPUTED:
23768 case LOC_CONST_BYTES:
23769 case LOC_OPTIMIZED_OUT:
23770 case LOC_STATIC:
23771 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23772 case LOC_CONST:
23773 /* Note: It's currently impossible to recognize psyms as enum values
23774 short of reading the type info. For now punt. */
23775 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23776 default:
23777 /* There are other LOC_FOO values that one might want to classify
23778 as variables, but dwarf2read.c doesn't currently use them. */
23779 return GDB_INDEX_SYMBOL_KIND_OTHER;
23780 }
23781 case STRUCT_DOMAIN:
23782 return GDB_INDEX_SYMBOL_KIND_TYPE;
23783 default:
23784 return GDB_INDEX_SYMBOL_KIND_OTHER;
23785 }
23786}
23787
9291a0cd 23788/* Add a list of partial symbols to SYMTAB. */
b89be57b 23789
9291a0cd
TT
23790static void
23791write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23792 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23793 struct partial_symbol **psymp,
23794 int count,
987d643c
TT
23795 offset_type cu_index,
23796 int is_static)
9291a0cd
TT
23797{
23798 for (; count-- > 0; ++psymp)
23799 {
156942c7 23800 struct partial_symbol *psym = *psymp;
987d643c 23801
156942c7 23802 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23803 error (_("Ada is not currently supported by the index"));
987d643c 23804
987d643c 23805 /* Only add a given psymbol once. */
bc8f2430 23806 if (psyms_seen.insert (psym).second)
987d643c 23807 {
156942c7
DE
23808 gdb_index_symbol_kind kind = symbol_kind (psym);
23809
156942c7
DE
23810 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23811 is_static, kind, cu_index);
987d643c 23812 }
9291a0cd
TT
23813 }
23814}
23815
1fd400ff
TT
23816/* A helper struct used when iterating over debug_types. */
23817struct signatured_type_index_data
23818{
bc8f2430
JK
23819 signatured_type_index_data (data_buf &types_list_,
23820 std::unordered_set<partial_symbol *> &psyms_seen_)
23821 : types_list (types_list_), psyms_seen (psyms_seen_)
23822 {}
23823
1fd400ff
TT
23824 struct objfile *objfile;
23825 struct mapped_symtab *symtab;
bc8f2430
JK
23826 data_buf &types_list;
23827 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23828 int cu_index;
23829};
23830
23831/* A helper function that writes a single signatured_type to an
23832 obstack. */
b89be57b 23833
1fd400ff
TT
23834static int
23835write_one_signatured_type (void **slot, void *d)
23836{
9a3c8263
SM
23837 struct signatured_type_index_data *info
23838 = (struct signatured_type_index_data *) d;
1fd400ff 23839 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23840 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23841
23842 write_psymbols (info->symtab,
987d643c 23843 info->psyms_seen,
af5bf4ad 23844 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
23845 psymtab->n_global_syms, info->cu_index,
23846 0);
1fd400ff 23847 write_psymbols (info->symtab,
987d643c 23848 info->psyms_seen,
af5bf4ad 23849 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
23850 psymtab->n_static_syms, info->cu_index,
23851 1);
1fd400ff 23852
c2f134ac
PA
23853 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23854 to_underlying (entry->per_cu.sect_off));
23855 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23856 to_underlying (entry->type_offset_in_tu));
23857 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23858
23859 ++info->cu_index;
23860
23861 return 1;
23862}
23863
e8f8bcb3
PA
23864/* Recurse into all "included" dependencies and count their symbols as
23865 if they appeared in this psymtab. */
23866
23867static void
23868recursively_count_psymbols (struct partial_symtab *psymtab,
23869 size_t &psyms_seen)
23870{
23871 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23872 if (psymtab->dependencies[i]->user != NULL)
23873 recursively_count_psymbols (psymtab->dependencies[i],
23874 psyms_seen);
23875
23876 psyms_seen += psymtab->n_global_syms;
23877 psyms_seen += psymtab->n_static_syms;
23878}
23879
95554aad
TT
23880/* Recurse into all "included" dependencies and write their symbols as
23881 if they appeared in this psymtab. */
23882
23883static void
23884recursively_write_psymbols (struct objfile *objfile,
23885 struct partial_symtab *psymtab,
23886 struct mapped_symtab *symtab,
bc8f2430 23887 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23888 offset_type cu_index)
23889{
23890 int i;
23891
23892 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23893 if (psymtab->dependencies[i]->user != NULL)
23894 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23895 symtab, psyms_seen, cu_index);
23896
23897 write_psymbols (symtab,
23898 psyms_seen,
af5bf4ad 23899 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
23900 psymtab->n_global_syms, cu_index,
23901 0);
23902 write_psymbols (symtab,
23903 psyms_seen,
af5bf4ad 23904 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
23905 psymtab->n_static_syms, cu_index,
23906 1);
23907}
23908
9291a0cd 23909/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23910
9291a0cd
TT
23911static void
23912write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23913{
9291a0cd
TT
23914 if (dwarf2_per_objfile->using_index)
23915 error (_("Cannot use an index to create the index"));
23916
8b70b953
TT
23917 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23918 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23919
260b681b
DE
23920 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23921 return;
23922
bc8f2430 23923 struct stat st;
4262abfb
JK
23924 if (stat (objfile_name (objfile), &st) < 0)
23925 perror_with_name (objfile_name (objfile));
9291a0cd 23926
bc8f2430
JK
23927 std::string filename (std::string (dir) + SLASH_STRING
23928 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23929
d419f42d 23930 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 23931 if (!out_file)
bc8f2430 23932 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23933
16b7a719
PA
23934 /* Order matters here; we want FILE to be closed before FILENAME is
23935 unlinked, because on MS-Windows one cannot delete a file that is
23936 still open. (Don't call anything here that might throw until
23937 file_closer is created.) */
bc8f2430 23938 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 23939 gdb_file_up close_out_file (out_file);
9291a0cd 23940
bc8f2430
JK
23941 mapped_symtab symtab;
23942 data_buf cu_list;
987d643c 23943
0a5429f6
DE
23944 /* While we're scanning CU's create a table that maps a psymtab pointer
23945 (which is what addrmap records) to its index (which is what is recorded
23946 in the index file). This will later be needed to write the address
23947 table. */
bc8f2430
JK
23948 psym_index_map cu_index_htab;
23949 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23950
23951 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23952 work here. Also, the debug_types entries do not appear in
23953 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23954
23955 /* The psyms_seen set is potentially going to be largish (~40k
23956 elements when indexing a -g3 build of GDB itself). Estimate the
23957 number of elements in order to avoid too many rehashes, which
23958 require rebuilding buckets and thus many trips to
23959 malloc/free. */
23960 size_t psyms_count = 0;
23961 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23962 {
23963 struct dwarf2_per_cu_data *per_cu
23964 = dwarf2_per_objfile->all_comp_units[i];
23965 struct partial_symtab *psymtab = per_cu->v.psymtab;
23966
23967 if (psymtab != NULL && psymtab->user == NULL)
23968 recursively_count_psymbols (psymtab, psyms_count);
23969 }
23970 /* Generating an index for gdb itself shows a ratio of
23971 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23972 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23973 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23974 {
3e43a32a
MS
23975 struct dwarf2_per_cu_data *per_cu
23976 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23977 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23978
92fac807
JK
23979 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23980 It may be referenced from a local scope but in such case it does not
23981 need to be present in .gdb_index. */
23982 if (psymtab == NULL)
23983 continue;
23984
95554aad 23985 if (psymtab->user == NULL)
bc8f2430
JK
23986 recursively_write_psymbols (objfile, psymtab, &symtab,
23987 psyms_seen, i);
9291a0cd 23988
bc8f2430
JK
23989 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23990 gdb_assert (insertpair.second);
9291a0cd 23991
c2f134ac
PA
23992 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23993 to_underlying (per_cu->sect_off));
23994 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23995 }
23996
0a5429f6 23997 /* Dump the address map. */
bc8f2430
JK
23998 data_buf addr_vec;
23999 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24000
1fd400ff 24001 /* Write out the .debug_type entries, if any. */
bc8f2430 24002 data_buf types_cu_list;
1fd400ff
TT
24003 if (dwarf2_per_objfile->signatured_types)
24004 {
bc8f2430
JK
24005 signatured_type_index_data sig_data (types_cu_list,
24006 psyms_seen);
1fd400ff
TT
24007
24008 sig_data.objfile = objfile;
bc8f2430 24009 sig_data.symtab = &symtab;
1fd400ff
TT
24010 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24011 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24012 write_one_signatured_type, &sig_data);
24013 }
24014
156942c7
DE
24015 /* Now that we've processed all symbols we can shrink their cu_indices
24016 lists. */
bc8f2430 24017 uniquify_cu_indices (&symtab);
156942c7 24018
bc8f2430
JK
24019 data_buf symtab_vec, constant_pool;
24020 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24021
bc8f2430
JK
24022 data_buf contents;
24023 const offset_type size_of_contents = 6 * sizeof (offset_type);
24024 offset_type total_len = size_of_contents;
9291a0cd
TT
24025
24026 /* The version number. */
bc8f2430 24027 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24028
24029 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24030 contents.append_data (MAYBE_SWAP (total_len));
24031 total_len += cu_list.size ();
9291a0cd 24032
1fd400ff 24033 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24034 contents.append_data (MAYBE_SWAP (total_len));
24035 total_len += types_cu_list.size ();
1fd400ff 24036
9291a0cd 24037 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24038 contents.append_data (MAYBE_SWAP (total_len));
24039 total_len += addr_vec.size ();
9291a0cd
TT
24040
24041 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24042 contents.append_data (MAYBE_SWAP (total_len));
24043 total_len += symtab_vec.size ();
9291a0cd
TT
24044
24045 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24046 contents.append_data (MAYBE_SWAP (total_len));
24047 total_len += constant_pool.size ();
9291a0cd 24048
bc8f2430 24049 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24050
bc8f2430
JK
24051 contents.file_write (out_file);
24052 cu_list.file_write (out_file);
24053 types_cu_list.file_write (out_file);
24054 addr_vec.file_write (out_file);
24055 symtab_vec.file_write (out_file);
24056 constant_pool.file_write (out_file);
9291a0cd 24057
bef155c3
TT
24058 /* We want to keep the file. */
24059 unlink_file.keep ();
9291a0cd
TT
24060}
24061
90476074
TT
24062/* Implementation of the `save gdb-index' command.
24063
24064 Note that the file format used by this command is documented in the
24065 GDB manual. Any changes here must be documented there. */
11570e71 24066
9291a0cd 24067static void
8384c356 24068save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24069{
24070 struct objfile *objfile;
24071
24072 if (!arg || !*arg)
96d19272 24073 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24074
24075 ALL_OBJFILES (objfile)
24076 {
24077 struct stat st;
24078
24079 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24080 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24081 continue;
24082
9a3c8263
SM
24083 dwarf2_per_objfile
24084 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24085 dwarf2_objfile_data_key);
9291a0cd
TT
24086 if (dwarf2_per_objfile)
24087 {
9291a0cd 24088
492d29ea 24089 TRY
9291a0cd
TT
24090 {
24091 write_psymtabs_to_index (objfile, arg);
24092 }
492d29ea
PA
24093 CATCH (except, RETURN_MASK_ERROR)
24094 {
24095 exception_fprintf (gdb_stderr, except,
24096 _("Error while writing index for `%s': "),
24097 objfile_name (objfile));
24098 }
24099 END_CATCH
9291a0cd
TT
24100 }
24101 }
dce234bc
PP
24102}
24103
9291a0cd
TT
24104\f
24105
b4f54984 24106int dwarf_always_disassemble;
9eae7c52
TT
24107
24108static void
b4f54984
DE
24109show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24110 struct cmd_list_element *c, const char *value)
9eae7c52 24111{
3e43a32a
MS
24112 fprintf_filtered (file,
24113 _("Whether to always disassemble "
24114 "DWARF expressions is %s.\n"),
9eae7c52
TT
24115 value);
24116}
24117
900e11f9
JK
24118static void
24119show_check_physname (struct ui_file *file, int from_tty,
24120 struct cmd_list_element *c, const char *value)
24121{
24122 fprintf_filtered (file,
24123 _("Whether to check \"physname\" is %s.\n"),
24124 value);
24125}
24126
6502dd73
DJ
24127void
24128_initialize_dwarf2_read (void)
24129{
96d19272
JK
24130 struct cmd_list_element *c;
24131
dce234bc 24132 dwarf2_objfile_data_key
c1bd65d0 24133 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24134
b4f54984
DE
24135 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24136Set DWARF specific variables.\n\
24137Configure DWARF variables such as the cache size"),
24138 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24139 0/*allow-unknown*/, &maintenance_set_cmdlist);
24140
b4f54984
DE
24141 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24142Show DWARF specific variables\n\
24143Show DWARF variables such as the cache size"),
24144 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24145 0/*allow-unknown*/, &maintenance_show_cmdlist);
24146
24147 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24148 &dwarf_max_cache_age, _("\
24149Set the upper bound on the age of cached DWARF compilation units."), _("\
24150Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24151A higher limit means that cached compilation units will be stored\n\
24152in memory longer, and more total memory will be used. Zero disables\n\
24153caching, which can slow down startup."),
2c5b56ce 24154 NULL,
b4f54984
DE
24155 show_dwarf_max_cache_age,
24156 &set_dwarf_cmdlist,
24157 &show_dwarf_cmdlist);
d97bc12b 24158
9eae7c52 24159 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24160 &dwarf_always_disassemble, _("\
9eae7c52
TT
24161Set whether `info address' always disassembles DWARF expressions."), _("\
24162Show whether `info address' always disassembles DWARF expressions."), _("\
24163When enabled, DWARF expressions are always printed in an assembly-like\n\
24164syntax. When disabled, expressions will be printed in a more\n\
24165conversational style, when possible."),
24166 NULL,
b4f54984
DE
24167 show_dwarf_always_disassemble,
24168 &set_dwarf_cmdlist,
24169 &show_dwarf_cmdlist);
24170
24171 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24172Set debugging of the DWARF reader."), _("\
24173Show debugging of the DWARF reader."), _("\
24174When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24175reading and symtab expansion. A value of 1 (one) provides basic\n\
24176information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24177 NULL,
24178 NULL,
24179 &setdebuglist, &showdebuglist);
24180
b4f54984
DE
24181 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24182Set debugging of the DWARF DIE reader."), _("\
24183Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24184When enabled (non-zero), DIEs are dumped after they are read in.\n\
24185The value is the maximum depth to print."),
ccce17b0
YQ
24186 NULL,
24187 NULL,
24188 &setdebuglist, &showdebuglist);
9291a0cd 24189
27e0867f
DE
24190 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24191Set debugging of the dwarf line reader."), _("\
24192Show debugging of the dwarf line reader."), _("\
24193When enabled (non-zero), line number entries are dumped as they are read in.\n\
24194A value of 1 (one) provides basic information.\n\
24195A value greater than 1 provides more verbose information."),
24196 NULL,
24197 NULL,
24198 &setdebuglist, &showdebuglist);
24199
900e11f9
JK
24200 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24201Set cross-checking of \"physname\" code against demangler."), _("\
24202Show cross-checking of \"physname\" code against demangler."), _("\
24203When enabled, GDB's internal \"physname\" code is checked against\n\
24204the demangler."),
24205 NULL, show_check_physname,
24206 &setdebuglist, &showdebuglist);
24207
e615022a
DE
24208 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24209 no_class, &use_deprecated_index_sections, _("\
24210Set whether to use deprecated gdb_index sections."), _("\
24211Show whether to use deprecated gdb_index sections."), _("\
24212When enabled, deprecated .gdb_index sections are used anyway.\n\
24213Normally they are ignored either because of a missing feature or\n\
24214performance issue.\n\
24215Warning: This option must be enabled before gdb reads the file."),
24216 NULL,
24217 NULL,
24218 &setlist, &showlist);
24219
96d19272 24220 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24221 _("\
fc1a9d6e 24222Save a gdb-index file.\n\
11570e71 24223Usage: save gdb-index DIRECTORY"),
96d19272
JK
24224 &save_cmdlist);
24225 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24226
24227 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24228 &dwarf2_locexpr_funcs);
24229 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24230 &dwarf2_loclist_funcs);
24231
24232 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24233 &dwarf2_block_frame_base_locexpr_funcs);
24234 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24235 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24236}