Change increment_reading_symtab to return a scoped_restore
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
4c2df51b 76
c906108c 77#include <fcntl.h>
c906108c 78#include <sys/types.h>
325fac50 79#include <algorithm>
d8151005 80
34eaf542
TT
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
73be47f5
DE
84/* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
b4f54984
DE
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87static unsigned int dwarf_read_debug = 0;
45cfd468 88
d97bc12b 89/* When non-zero, dump DIEs after they are read in. */
b4f54984 90static unsigned int dwarf_die_debug = 0;
d97bc12b 91
27e0867f
DE
92/* When non-zero, dump line number entries as they are read in. */
93static unsigned int dwarf_line_debug = 0;
94
900e11f9
JK
95/* When non-zero, cross-check physname against demangler. */
96static int check_physname = 0;
97
481860b3 98/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 99static int use_deprecated_index_sections = 0;
481860b3 100
6502dd73
DJ
101static const struct objfile_data *dwarf2_objfile_data_key;
102
f1e6e072
TT
103/* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105static int dwarf2_locexpr_index;
106static int dwarf2_loclist_index;
107static int dwarf2_locexpr_block_index;
108static int dwarf2_loclist_block_index;
109
73869dc2
DE
110/* A descriptor for dwarf sections.
111
112 S.ASECTION, SIZE are typically initialized when the objfile is first
113 scanned. BUFFER, READIN are filled in later when the section is read.
114 If the section contained compressed data then SIZE is updated to record
115 the uncompressed size of the section.
116
117 DWP file format V2 introduces a wrinkle that is easiest to handle by
118 creating the concept of virtual sections contained within a real section.
119 In DWP V2 the sections of the input DWO files are concatenated together
120 into one section, but section offsets are kept relative to the original
121 input section.
122 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
123 the real section this "virtual" section is contained in, and BUFFER,SIZE
124 describe the virtual section. */
125
dce234bc
PP
126struct dwarf2_section_info
127{
73869dc2
DE
128 union
129 {
e5aa3347 130 /* If this is a real section, the bfd section. */
049412e3 131 asection *section;
73869dc2 132 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 133 section. */
73869dc2
DE
134 struct dwarf2_section_info *containing_section;
135 } s;
19ac8c2e 136 /* Pointer to section data, only valid if readin. */
d521ce57 137 const gdb_byte *buffer;
73869dc2 138 /* The size of the section, real or virtual. */
dce234bc 139 bfd_size_type size;
73869dc2
DE
140 /* If this is a virtual section, the offset in the real section.
141 Only valid if is_virtual. */
142 bfd_size_type virtual_offset;
be391dca 143 /* True if we have tried to read this section. */
73869dc2
DE
144 char readin;
145 /* True if this is a virtual section, False otherwise.
049412e3 146 This specifies which of s.section and s.containing_section to use. */
73869dc2 147 char is_virtual;
dce234bc
PP
148};
149
8b70b953
TT
150typedef struct dwarf2_section_info dwarf2_section_info_def;
151DEF_VEC_O (dwarf2_section_info_def);
152
9291a0cd
TT
153/* All offsets in the index are of this type. It must be
154 architecture-independent. */
155typedef uint32_t offset_type;
156
157DEF_VEC_I (offset_type);
158
156942c7
DE
159/* Ensure only legit values are used. */
160#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
161 do { \
162 gdb_assert ((unsigned int) (value) <= 1); \
163 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
164 } while (0)
165
166/* Ensure only legit values are used. */
167#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
168 do { \
169 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
170 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
171 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174/* Ensure we don't use more than the alloted nuber of bits for the CU. */
175#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
178 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
179 } while (0)
180
9291a0cd
TT
181/* A description of the mapped index. The file format is described in
182 a comment by the code that writes the index. */
183struct mapped_index
184{
559a7a62
JK
185 /* Index data format version. */
186 int version;
187
9291a0cd
TT
188 /* The total length of the buffer. */
189 off_t total_size;
b11b1f88 190
9291a0cd
TT
191 /* A pointer to the address table data. */
192 const gdb_byte *address_table;
b11b1f88 193
9291a0cd
TT
194 /* Size of the address table data in bytes. */
195 offset_type address_table_size;
b11b1f88 196
3876f04e
DE
197 /* The symbol table, implemented as a hash table. */
198 const offset_type *symbol_table;
b11b1f88 199
9291a0cd 200 /* Size in slots, each slot is 2 offset_types. */
3876f04e 201 offset_type symbol_table_slots;
b11b1f88 202
9291a0cd
TT
203 /* A pointer to the constant pool. */
204 const char *constant_pool;
205};
206
95554aad
TT
207typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
208DEF_VEC_P (dwarf2_per_cu_ptr);
209
52059ffd
TT
210struct tu_stats
211{
212 int nr_uniq_abbrev_tables;
213 int nr_symtabs;
214 int nr_symtab_sharers;
215 int nr_stmt_less_type_units;
216 int nr_all_type_units_reallocs;
217};
218
9cdd5dbd
DE
219/* Collection of data recorded per objfile.
220 This hangs off of dwarf2_objfile_data_key. */
221
6502dd73
DJ
222struct dwarf2_per_objfile
223{
dce234bc
PP
224 struct dwarf2_section_info info;
225 struct dwarf2_section_info abbrev;
226 struct dwarf2_section_info line;
dce234bc 227 struct dwarf2_section_info loc;
43988095 228 struct dwarf2_section_info loclists;
dce234bc 229 struct dwarf2_section_info macinfo;
cf2c3c16 230 struct dwarf2_section_info macro;
dce234bc 231 struct dwarf2_section_info str;
43988095 232 struct dwarf2_section_info line_str;
dce234bc 233 struct dwarf2_section_info ranges;
43988095 234 struct dwarf2_section_info rnglists;
3019eac3 235 struct dwarf2_section_info addr;
dce234bc
PP
236 struct dwarf2_section_info frame;
237 struct dwarf2_section_info eh_frame;
9291a0cd 238 struct dwarf2_section_info gdb_index;
ae038cb0 239
8b70b953
TT
240 VEC (dwarf2_section_info_def) *types;
241
be391dca
TT
242 /* Back link. */
243 struct objfile *objfile;
244
d467dd73 245 /* Table of all the compilation units. This is used to locate
10b3939b 246 the target compilation unit of a particular reference. */
ae038cb0
DJ
247 struct dwarf2_per_cu_data **all_comp_units;
248
249 /* The number of compilation units in ALL_COMP_UNITS. */
250 int n_comp_units;
251
1fd400ff 252 /* The number of .debug_types-related CUs. */
d467dd73 253 int n_type_units;
1fd400ff 254
6aa5f3a6
DE
255 /* The number of elements allocated in all_type_units.
256 If there are skeleton-less TUs, we add them to all_type_units lazily. */
257 int n_allocated_type_units;
258
a2ce51a0
DE
259 /* The .debug_types-related CUs (TUs).
260 This is stored in malloc space because we may realloc it. */
b4dd5633 261 struct signatured_type **all_type_units;
1fd400ff 262
f4dc4d17
DE
263 /* Table of struct type_unit_group objects.
264 The hash key is the DW_AT_stmt_list value. */
265 htab_t type_unit_groups;
72dca2f5 266
348e048f
DE
267 /* A table mapping .debug_types signatures to its signatured_type entry.
268 This is NULL if the .debug_types section hasn't been read in yet. */
269 htab_t signatured_types;
270
f4dc4d17
DE
271 /* Type unit statistics, to see how well the scaling improvements
272 are doing. */
52059ffd 273 struct tu_stats tu_stats;
f4dc4d17
DE
274
275 /* A chain of compilation units that are currently read in, so that
276 they can be freed later. */
277 struct dwarf2_per_cu_data *read_in_chain;
278
3019eac3
DE
279 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
280 This is NULL if the table hasn't been allocated yet. */
281 htab_t dwo_files;
282
80626a55
DE
283 /* Non-zero if we've check for whether there is a DWP file. */
284 int dwp_checked;
285
286 /* The DWP file if there is one, or NULL. */
287 struct dwp_file *dwp_file;
288
36586728
TT
289 /* The shared '.dwz' file, if one exists. This is used when the
290 original data was compressed using 'dwz -m'. */
291 struct dwz_file *dwz_file;
292
72dca2f5
FR
293 /* A flag indicating wether this objfile has a section loaded at a
294 VMA of 0. */
295 int has_section_at_zero;
9291a0cd 296
ae2de4f8
DE
297 /* True if we are using the mapped index,
298 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
299 unsigned char using_index;
300
ae2de4f8 301 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 302 struct mapped_index *index_table;
98bfdba5 303
7b9f3c50 304 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
305 TUs typically share line table entries with a CU, so we maintain a
306 separate table of all line table entries to support the sharing.
307 Note that while there can be way more TUs than CUs, we've already
308 sorted all the TUs into "type unit groups", grouped by their
309 DW_AT_stmt_list value. Therefore the only sharing done here is with a
310 CU and its associated TU group if there is one. */
7b9f3c50
DE
311 htab_t quick_file_names_table;
312
98bfdba5
PA
313 /* Set during partial symbol reading, to prevent queueing of full
314 symbols. */
315 int reading_partial_symbols;
673bfd45 316
dee91e82 317 /* Table mapping type DIEs to their struct type *.
673bfd45 318 This is NULL if not allocated yet.
02142a6c 319 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 320 htab_t die_type_hash;
95554aad
TT
321
322 /* The CUs we recently read. */
323 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
324
325 /* Table containing line_header indexed by offset and offset_in_dwz. */
326 htab_t line_header_hash;
6502dd73
DJ
327};
328
329static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 330
251d32d9 331/* Default names of the debugging sections. */
c906108c 332
233a11ab
CS
333/* Note that if the debugging section has been compressed, it might
334 have a name like .zdebug_info. */
335
9cdd5dbd
DE
336static const struct dwarf2_debug_sections dwarf2_elf_names =
337{
251d32d9
TG
338 { ".debug_info", ".zdebug_info" },
339 { ".debug_abbrev", ".zdebug_abbrev" },
340 { ".debug_line", ".zdebug_line" },
341 { ".debug_loc", ".zdebug_loc" },
43988095 342 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 343 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 344 { ".debug_macro", ".zdebug_macro" },
251d32d9 345 { ".debug_str", ".zdebug_str" },
43988095 346 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 347 { ".debug_ranges", ".zdebug_ranges" },
43988095 348 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 349 { ".debug_types", ".zdebug_types" },
3019eac3 350 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
351 { ".debug_frame", ".zdebug_frame" },
352 { ".eh_frame", NULL },
24d3216f
TT
353 { ".gdb_index", ".zgdb_index" },
354 23
251d32d9 355};
c906108c 356
80626a55 357/* List of DWO/DWP sections. */
3019eac3 358
80626a55 359static const struct dwop_section_names
3019eac3
DE
360{
361 struct dwarf2_section_names abbrev_dwo;
362 struct dwarf2_section_names info_dwo;
363 struct dwarf2_section_names line_dwo;
364 struct dwarf2_section_names loc_dwo;
43988095 365 struct dwarf2_section_names loclists_dwo;
09262596
DE
366 struct dwarf2_section_names macinfo_dwo;
367 struct dwarf2_section_names macro_dwo;
3019eac3
DE
368 struct dwarf2_section_names str_dwo;
369 struct dwarf2_section_names str_offsets_dwo;
370 struct dwarf2_section_names types_dwo;
80626a55
DE
371 struct dwarf2_section_names cu_index;
372 struct dwarf2_section_names tu_index;
3019eac3 373}
80626a55 374dwop_section_names =
3019eac3
DE
375{
376 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
377 { ".debug_info.dwo", ".zdebug_info.dwo" },
378 { ".debug_line.dwo", ".zdebug_line.dwo" },
379 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 380 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
381 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
382 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
383 { ".debug_str.dwo", ".zdebug_str.dwo" },
384 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
385 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
386 { ".debug_cu_index", ".zdebug_cu_index" },
387 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
388};
389
c906108c
SS
390/* local data types */
391
107d2387
AC
392/* The data in a compilation unit header, after target2host
393 translation, looks like this. */
c906108c 394struct comp_unit_head
a738430d 395{
c764a876 396 unsigned int length;
a738430d 397 short version;
a738430d
MK
398 unsigned char addr_size;
399 unsigned char signed_addr_p;
9c541725 400 sect_offset abbrev_sect_off;
57349743 401
a738430d
MK
402 /* Size of file offsets; either 4 or 8. */
403 unsigned int offset_size;
57349743 404
a738430d
MK
405 /* Size of the length field; either 4 or 12. */
406 unsigned int initial_length_size;
57349743 407
43988095
JK
408 enum dwarf_unit_type unit_type;
409
a738430d
MK
410 /* Offset to the first byte of this compilation unit header in the
411 .debug_info section, for resolving relative reference dies. */
9c541725 412 sect_offset sect_off;
57349743 413
d00adf39
DE
414 /* Offset to first die in this cu from the start of the cu.
415 This will be the first byte following the compilation unit header. */
9c541725 416 cu_offset first_die_cu_offset;
43988095
JK
417
418 /* 64-bit signature of this type unit - it is valid only for
419 UNIT_TYPE DW_UT_type. */
420 ULONGEST signature;
421
422 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 423 cu_offset type_cu_offset_in_tu;
a738430d 424};
c906108c 425
3da10d80
KS
426/* Type used for delaying computation of method physnames.
427 See comments for compute_delayed_physnames. */
428struct delayed_method_info
429{
430 /* The type to which the method is attached, i.e., its parent class. */
431 struct type *type;
432
433 /* The index of the method in the type's function fieldlists. */
434 int fnfield_index;
435
436 /* The index of the method in the fieldlist. */
437 int index;
438
439 /* The name of the DIE. */
440 const char *name;
441
442 /* The DIE associated with this method. */
443 struct die_info *die;
444};
445
446typedef struct delayed_method_info delayed_method_info;
447DEF_VEC_O (delayed_method_info);
448
e7c27a73
DJ
449/* Internal state when decoding a particular compilation unit. */
450struct dwarf2_cu
451{
452 /* The objfile containing this compilation unit. */
453 struct objfile *objfile;
454
d00adf39 455 /* The header of the compilation unit. */
e7c27a73 456 struct comp_unit_head header;
e142c38c 457
d00adf39
DE
458 /* Base address of this compilation unit. */
459 CORE_ADDR base_address;
460
461 /* Non-zero if base_address has been set. */
462 int base_known;
463
e142c38c
DJ
464 /* The language we are debugging. */
465 enum language language;
466 const struct language_defn *language_defn;
467
b0f35d58
DL
468 const char *producer;
469
e142c38c
DJ
470 /* The generic symbol table building routines have separate lists for
471 file scope symbols and all all other scopes (local scopes). So
472 we need to select the right one to pass to add_symbol_to_list().
473 We do it by keeping a pointer to the correct list in list_in_scope.
474
475 FIXME: The original dwarf code just treated the file scope as the
476 first local scope, and all other local scopes as nested local
477 scopes, and worked fine. Check to see if we really need to
478 distinguish these in buildsym.c. */
479 struct pending **list_in_scope;
480
433df2d4
DE
481 /* The abbrev table for this CU.
482 Normally this points to the abbrev table in the objfile.
483 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
484 struct abbrev_table *abbrev_table;
72bf9492 485
b64f50a1
JK
486 /* Hash table holding all the loaded partial DIEs
487 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
488 htab_t partial_dies;
489
490 /* Storage for things with the same lifetime as this read-in compilation
491 unit, including partial DIEs. */
492 struct obstack comp_unit_obstack;
493
ae038cb0
DJ
494 /* When multiple dwarf2_cu structures are living in memory, this field
495 chains them all together, so that they can be released efficiently.
496 We will probably also want a generation counter so that most-recently-used
497 compilation units are cached... */
498 struct dwarf2_per_cu_data *read_in_chain;
499
69d751e3 500 /* Backlink to our per_cu entry. */
ae038cb0
DJ
501 struct dwarf2_per_cu_data *per_cu;
502
503 /* How many compilation units ago was this CU last referenced? */
504 int last_used;
505
b64f50a1
JK
506 /* A hash table of DIE cu_offset for following references with
507 die_info->offset.sect_off as hash. */
51545339 508 htab_t die_hash;
10b3939b
DJ
509
510 /* Full DIEs if read in. */
511 struct die_info *dies;
512
513 /* A set of pointers to dwarf2_per_cu_data objects for compilation
514 units referenced by this one. Only set during full symbol processing;
515 partial symbol tables do not have dependencies. */
516 htab_t dependencies;
517
cb1df416
DJ
518 /* Header data from the line table, during full symbol processing. */
519 struct line_header *line_header;
520
3da10d80
KS
521 /* A list of methods which need to have physnames computed
522 after all type information has been read. */
523 VEC (delayed_method_info) *method_list;
524
96408a79
SA
525 /* To be copied to symtab->call_site_htab. */
526 htab_t call_site_htab;
527
034e5797
DE
528 /* Non-NULL if this CU came from a DWO file.
529 There is an invariant here that is important to remember:
530 Except for attributes copied from the top level DIE in the "main"
531 (or "stub") file in preparation for reading the DWO file
532 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
533 Either there isn't a DWO file (in which case this is NULL and the point
534 is moot), or there is and either we're not going to read it (in which
535 case this is NULL) or there is and we are reading it (in which case this
536 is non-NULL). */
3019eac3
DE
537 struct dwo_unit *dwo_unit;
538
539 /* The DW_AT_addr_base attribute if present, zero otherwise
540 (zero is a valid value though).
1dbab08b 541 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
542 ULONGEST addr_base;
543
2e3cf129
DE
544 /* The DW_AT_ranges_base attribute if present, zero otherwise
545 (zero is a valid value though).
1dbab08b 546 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 547 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
548 be used without needing to know whether DWO files are in use or not.
549 N.B. This does not apply to DW_AT_ranges appearing in
550 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
551 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
552 DW_AT_ranges_base *would* have to be applied, and we'd have to care
553 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
554 ULONGEST ranges_base;
555
ae038cb0
DJ
556 /* Mark used when releasing cached dies. */
557 unsigned int mark : 1;
558
8be455d7
JK
559 /* This CU references .debug_loc. See the symtab->locations_valid field.
560 This test is imperfect as there may exist optimized debug code not using
561 any location list and still facing inlining issues if handled as
562 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 563 unsigned int has_loclist : 1;
ba919b58 564
1b80a9fa
JK
565 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
566 if all the producer_is_* fields are valid. This information is cached
567 because profiling CU expansion showed excessive time spent in
568 producer_is_gxx_lt_4_6. */
ba919b58
TT
569 unsigned int checked_producer : 1;
570 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 571 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 572 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
573
574 /* When set, the file that we're processing is known to have
575 debugging info for C++ namespaces. GCC 3.3.x did not produce
576 this information, but later versions do. */
577
578 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
579};
580
10b3939b
DJ
581/* Persistent data held for a compilation unit, even when not
582 processing it. We put a pointer to this structure in the
28dee7f5 583 read_symtab_private field of the psymtab. */
10b3939b 584
ae038cb0
DJ
585struct dwarf2_per_cu_data
586{
36586728 587 /* The start offset and length of this compilation unit.
45452591 588 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
589 initial_length_size.
590 If the DIE refers to a DWO file, this is always of the original die,
591 not the DWO file. */
9c541725 592 sect_offset sect_off;
36586728 593 unsigned int length;
ae038cb0 594
43988095
JK
595 /* DWARF standard version this data has been read from (such as 4 or 5). */
596 short dwarf_version;
597
ae038cb0
DJ
598 /* Flag indicating this compilation unit will be read in before
599 any of the current compilation units are processed. */
c764a876 600 unsigned int queued : 1;
ae038cb0 601
0d99eb77
DE
602 /* This flag will be set when reading partial DIEs if we need to load
603 absolutely all DIEs for this compilation unit, instead of just the ones
604 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
605 hash table and don't find it. */
606 unsigned int load_all_dies : 1;
607
0186c6a7
DE
608 /* Non-zero if this CU is from .debug_types.
609 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
610 this is non-zero. */
3019eac3
DE
611 unsigned int is_debug_types : 1;
612
36586728
TT
613 /* Non-zero if this CU is from the .dwz file. */
614 unsigned int is_dwz : 1;
615
a2ce51a0
DE
616 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
617 This flag is only valid if is_debug_types is true.
618 We can't read a CU directly from a DWO file: There are required
619 attributes in the stub. */
620 unsigned int reading_dwo_directly : 1;
621
7ee85ab1
DE
622 /* Non-zero if the TU has been read.
623 This is used to assist the "Stay in DWO Optimization" for Fission:
624 When reading a DWO, it's faster to read TUs from the DWO instead of
625 fetching them from random other DWOs (due to comdat folding).
626 If the TU has already been read, the optimization is unnecessary
627 (and unwise - we don't want to change where gdb thinks the TU lives
628 "midflight").
629 This flag is only valid if is_debug_types is true. */
630 unsigned int tu_read : 1;
631
3019eac3
DE
632 /* The section this CU/TU lives in.
633 If the DIE refers to a DWO file, this is always the original die,
634 not the DWO file. */
8a0459fd 635 struct dwarf2_section_info *section;
348e048f 636
17ea53c3 637 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
638 of the CU cache it gets reset to NULL again. This is left as NULL for
639 dummy CUs (a CU header, but nothing else). */
ae038cb0 640 struct dwarf2_cu *cu;
1c379e20 641
9cdd5dbd
DE
642 /* The corresponding objfile.
643 Normally we can get the objfile from dwarf2_per_objfile.
644 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
645 struct objfile *objfile;
646
fffbe6a8
YQ
647 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
648 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
649 union
650 {
651 /* The partial symbol table associated with this compilation unit,
95554aad 652 or NULL for unread partial units. */
9291a0cd
TT
653 struct partial_symtab *psymtab;
654
655 /* Data needed by the "quick" functions. */
656 struct dwarf2_per_cu_quick_data *quick;
657 } v;
95554aad 658
796a7ff8
DE
659 /* The CUs we import using DW_TAG_imported_unit. This is filled in
660 while reading psymtabs, used to compute the psymtab dependencies,
661 and then cleared. Then it is filled in again while reading full
662 symbols, and only deleted when the objfile is destroyed.
663
664 This is also used to work around a difference between the way gold
665 generates .gdb_index version <=7 and the way gdb does. Arguably this
666 is a gold bug. For symbols coming from TUs, gold records in the index
667 the CU that includes the TU instead of the TU itself. This breaks
668 dw2_lookup_symbol: It assumes that if the index says symbol X lives
669 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
670 will find X. Alas TUs live in their own symtab, so after expanding CU Y
671 we need to look in TU Z to find X. Fortunately, this is akin to
672 DW_TAG_imported_unit, so we just use the same mechanism: For
673 .gdb_index version <=7 this also records the TUs that the CU referred
674 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
675 indices so we only pay a price for gold generated indices.
676 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 677 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
678};
679
348e048f
DE
680/* Entry in the signatured_types hash table. */
681
682struct signatured_type
683{
42e7ad6c 684 /* The "per_cu" object of this type.
ac9ec31b 685 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
686 N.B.: This is the first member so that it's easy to convert pointers
687 between them. */
688 struct dwarf2_per_cu_data per_cu;
689
3019eac3 690 /* The type's signature. */
348e048f
DE
691 ULONGEST signature;
692
3019eac3 693 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
694 If this TU is a DWO stub and the definition lives in a DWO file
695 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
696 cu_offset type_offset_in_tu;
697
698 /* Offset in the section of the type's DIE.
699 If the definition lives in a DWO file, this is the offset in the
700 .debug_types.dwo section.
701 The value is zero until the actual value is known.
702 Zero is otherwise not a valid section offset. */
703 sect_offset type_offset_in_section;
0186c6a7
DE
704
705 /* Type units are grouped by their DW_AT_stmt_list entry so that they
706 can share them. This points to the containing symtab. */
707 struct type_unit_group *type_unit_group;
ac9ec31b
DE
708
709 /* The type.
710 The first time we encounter this type we fully read it in and install it
711 in the symbol tables. Subsequent times we only need the type. */
712 struct type *type;
a2ce51a0
DE
713
714 /* Containing DWO unit.
715 This field is valid iff per_cu.reading_dwo_directly. */
716 struct dwo_unit *dwo_unit;
348e048f
DE
717};
718
0186c6a7
DE
719typedef struct signatured_type *sig_type_ptr;
720DEF_VEC_P (sig_type_ptr);
721
094b34ac
DE
722/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
723 This includes type_unit_group and quick_file_names. */
724
725struct stmt_list_hash
726{
727 /* The DWO unit this table is from or NULL if there is none. */
728 struct dwo_unit *dwo_unit;
729
730 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 731 sect_offset line_sect_off;
094b34ac
DE
732};
733
f4dc4d17
DE
734/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
735 an object of this type. */
736
737struct type_unit_group
738{
0186c6a7 739 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
740 To simplify things we create an artificial CU that "includes" all the
741 type units using this stmt_list so that the rest of the code still has
742 a "per_cu" handle on the symtab.
743 This PER_CU is recognized by having no section. */
8a0459fd 744#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
745 struct dwarf2_per_cu_data per_cu;
746
0186c6a7
DE
747 /* The TUs that share this DW_AT_stmt_list entry.
748 This is added to while parsing type units to build partial symtabs,
749 and is deleted afterwards and not used again. */
750 VEC (sig_type_ptr) *tus;
f4dc4d17 751
43f3e411 752 /* The compunit symtab.
094b34ac 753 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
754 so we create an essentially anonymous symtab as the compunit symtab. */
755 struct compunit_symtab *compunit_symtab;
f4dc4d17 756
094b34ac
DE
757 /* The data used to construct the hash key. */
758 struct stmt_list_hash hash;
f4dc4d17
DE
759
760 /* The number of symtabs from the line header.
761 The value here must match line_header.num_file_names. */
762 unsigned int num_symtabs;
763
764 /* The symbol tables for this TU (obtained from the files listed in
765 DW_AT_stmt_list).
766 WARNING: The order of entries here must match the order of entries
767 in the line header. After the first TU using this type_unit_group, the
768 line header for the subsequent TUs is recreated from this. This is done
769 because we need to use the same symtabs for each TU using the same
770 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
771 there's no guarantee the line header doesn't have duplicate entries. */
772 struct symtab **symtabs;
773};
774
73869dc2 775/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
776
777struct dwo_sections
778{
779 struct dwarf2_section_info abbrev;
3019eac3
DE
780 struct dwarf2_section_info line;
781 struct dwarf2_section_info loc;
43988095 782 struct dwarf2_section_info loclists;
09262596
DE
783 struct dwarf2_section_info macinfo;
784 struct dwarf2_section_info macro;
3019eac3
DE
785 struct dwarf2_section_info str;
786 struct dwarf2_section_info str_offsets;
80626a55
DE
787 /* In the case of a virtual DWO file, these two are unused. */
788 struct dwarf2_section_info info;
3019eac3
DE
789 VEC (dwarf2_section_info_def) *types;
790};
791
c88ee1f0 792/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
793
794struct dwo_unit
795{
796 /* Backlink to the containing struct dwo_file. */
797 struct dwo_file *dwo_file;
798
799 /* The "id" that distinguishes this CU/TU.
800 .debug_info calls this "dwo_id", .debug_types calls this "signature".
801 Since signatures came first, we stick with it for consistency. */
802 ULONGEST signature;
803
804 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 805 struct dwarf2_section_info *section;
3019eac3 806
9c541725
PA
807 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
808 sect_offset sect_off;
3019eac3
DE
809 unsigned int length;
810
811 /* For types, offset in the type's DIE of the type defined by this TU. */
812 cu_offset type_offset_in_tu;
813};
814
73869dc2
DE
815/* include/dwarf2.h defines the DWP section codes.
816 It defines a max value but it doesn't define a min value, which we
817 use for error checking, so provide one. */
818
819enum dwp_v2_section_ids
820{
821 DW_SECT_MIN = 1
822};
823
80626a55 824/* Data for one DWO file.
57d63ce2
DE
825
826 This includes virtual DWO files (a virtual DWO file is a DWO file as it
827 appears in a DWP file). DWP files don't really have DWO files per se -
828 comdat folding of types "loses" the DWO file they came from, and from
829 a high level view DWP files appear to contain a mass of random types.
830 However, to maintain consistency with the non-DWP case we pretend DWP
831 files contain virtual DWO files, and we assign each TU with one virtual
832 DWO file (generally based on the line and abbrev section offsets -
833 a heuristic that seems to work in practice). */
3019eac3
DE
834
835struct dwo_file
836{
0ac5b59e 837 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
838 For virtual DWO files the name is constructed from the section offsets
839 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
840 from related CU+TUs. */
0ac5b59e
DE
841 const char *dwo_name;
842
843 /* The DW_AT_comp_dir attribute. */
844 const char *comp_dir;
3019eac3 845
80626a55
DE
846 /* The bfd, when the file is open. Otherwise this is NULL.
847 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
848 bfd *dbfd;
3019eac3 849
73869dc2
DE
850 /* The sections that make up this DWO file.
851 Remember that for virtual DWO files in DWP V2, these are virtual
852 sections (for lack of a better name). */
3019eac3
DE
853 struct dwo_sections sections;
854
19c3d4c9
DE
855 /* The CU in the file.
856 We only support one because having more than one requires hacking the
857 dwo_name of each to match, which is highly unlikely to happen.
858 Doing this means all TUs can share comp_dir: We also assume that
859 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
860 struct dwo_unit *cu;
3019eac3
DE
861
862 /* Table of TUs in the file.
863 Each element is a struct dwo_unit. */
864 htab_t tus;
865};
866
80626a55
DE
867/* These sections are what may appear in a DWP file. */
868
869struct dwp_sections
870{
73869dc2 871 /* These are used by both DWP version 1 and 2. */
80626a55
DE
872 struct dwarf2_section_info str;
873 struct dwarf2_section_info cu_index;
874 struct dwarf2_section_info tu_index;
73869dc2
DE
875
876 /* These are only used by DWP version 2 files.
877 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
878 sections are referenced by section number, and are not recorded here.
879 In DWP version 2 there is at most one copy of all these sections, each
880 section being (effectively) comprised of the concatenation of all of the
881 individual sections that exist in the version 1 format.
882 To keep the code simple we treat each of these concatenated pieces as a
883 section itself (a virtual section?). */
884 struct dwarf2_section_info abbrev;
885 struct dwarf2_section_info info;
886 struct dwarf2_section_info line;
887 struct dwarf2_section_info loc;
888 struct dwarf2_section_info macinfo;
889 struct dwarf2_section_info macro;
890 struct dwarf2_section_info str_offsets;
891 struct dwarf2_section_info types;
80626a55
DE
892};
893
73869dc2
DE
894/* These sections are what may appear in a virtual DWO file in DWP version 1.
895 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 896
73869dc2 897struct virtual_v1_dwo_sections
80626a55
DE
898{
899 struct dwarf2_section_info abbrev;
900 struct dwarf2_section_info line;
901 struct dwarf2_section_info loc;
902 struct dwarf2_section_info macinfo;
903 struct dwarf2_section_info macro;
904 struct dwarf2_section_info str_offsets;
905 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 906 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
907 struct dwarf2_section_info info_or_types;
908};
909
73869dc2
DE
910/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
911 In version 2, the sections of the DWO files are concatenated together
912 and stored in one section of that name. Thus each ELF section contains
913 several "virtual" sections. */
914
915struct virtual_v2_dwo_sections
916{
917 bfd_size_type abbrev_offset;
918 bfd_size_type abbrev_size;
919
920 bfd_size_type line_offset;
921 bfd_size_type line_size;
922
923 bfd_size_type loc_offset;
924 bfd_size_type loc_size;
925
926 bfd_size_type macinfo_offset;
927 bfd_size_type macinfo_size;
928
929 bfd_size_type macro_offset;
930 bfd_size_type macro_size;
931
932 bfd_size_type str_offsets_offset;
933 bfd_size_type str_offsets_size;
934
935 /* Each DWP hash table entry records one CU or one TU.
936 That is recorded here, and copied to dwo_unit.section. */
937 bfd_size_type info_or_types_offset;
938 bfd_size_type info_or_types_size;
939};
940
80626a55
DE
941/* Contents of DWP hash tables. */
942
943struct dwp_hash_table
944{
73869dc2 945 uint32_t version, nr_columns;
80626a55 946 uint32_t nr_units, nr_slots;
73869dc2
DE
947 const gdb_byte *hash_table, *unit_table;
948 union
949 {
950 struct
951 {
952 const gdb_byte *indices;
953 } v1;
954 struct
955 {
956 /* This is indexed by column number and gives the id of the section
957 in that column. */
958#define MAX_NR_V2_DWO_SECTIONS \
959 (1 /* .debug_info or .debug_types */ \
960 + 1 /* .debug_abbrev */ \
961 + 1 /* .debug_line */ \
962 + 1 /* .debug_loc */ \
963 + 1 /* .debug_str_offsets */ \
964 + 1 /* .debug_macro or .debug_macinfo */)
965 int section_ids[MAX_NR_V2_DWO_SECTIONS];
966 const gdb_byte *offsets;
967 const gdb_byte *sizes;
968 } v2;
969 } section_pool;
80626a55
DE
970};
971
972/* Data for one DWP file. */
973
974struct dwp_file
975{
976 /* Name of the file. */
977 const char *name;
978
73869dc2
DE
979 /* File format version. */
980 int version;
981
93417882 982 /* The bfd. */
80626a55
DE
983 bfd *dbfd;
984
985 /* Section info for this file. */
986 struct dwp_sections sections;
987
57d63ce2 988 /* Table of CUs in the file. */
80626a55
DE
989 const struct dwp_hash_table *cus;
990
991 /* Table of TUs in the file. */
992 const struct dwp_hash_table *tus;
993
19ac8c2e
DE
994 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
995 htab_t loaded_cus;
996 htab_t loaded_tus;
80626a55 997
73869dc2
DE
998 /* Table to map ELF section numbers to their sections.
999 This is only needed for the DWP V1 file format. */
80626a55
DE
1000 unsigned int num_sections;
1001 asection **elf_sections;
1002};
1003
36586728
TT
1004/* This represents a '.dwz' file. */
1005
1006struct dwz_file
1007{
1008 /* A dwz file can only contain a few sections. */
1009 struct dwarf2_section_info abbrev;
1010 struct dwarf2_section_info info;
1011 struct dwarf2_section_info str;
1012 struct dwarf2_section_info line;
1013 struct dwarf2_section_info macro;
2ec9a5e0 1014 struct dwarf2_section_info gdb_index;
36586728
TT
1015
1016 /* The dwz's BFD. */
1017 bfd *dwz_bfd;
1018};
1019
0963b4bd
MS
1020/* Struct used to pass misc. parameters to read_die_and_children, et
1021 al. which are used for both .debug_info and .debug_types dies.
1022 All parameters here are unchanging for the life of the call. This
dee91e82 1023 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1024
1025struct die_reader_specs
1026{
a32a8923 1027 /* The bfd of die_section. */
93311388
DE
1028 bfd* abfd;
1029
1030 /* The CU of the DIE we are parsing. */
1031 struct dwarf2_cu *cu;
1032
80626a55 1033 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1034 struct dwo_file *dwo_file;
1035
dee91e82 1036 /* The section the die comes from.
3019eac3 1037 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1038 struct dwarf2_section_info *die_section;
1039
1040 /* die_section->buffer. */
d521ce57 1041 const gdb_byte *buffer;
f664829e
DE
1042
1043 /* The end of the buffer. */
1044 const gdb_byte *buffer_end;
a2ce51a0
DE
1045
1046 /* The value of the DW_AT_comp_dir attribute. */
1047 const char *comp_dir;
93311388
DE
1048};
1049
fd820528 1050/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1051typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1052 const gdb_byte *info_ptr,
dee91e82
DE
1053 struct die_info *comp_unit_die,
1054 int has_children,
1055 void *data);
1056
ecfb656c
PA
1057/* A 1-based directory index. This is a strong typedef to prevent
1058 accidentally using a directory index as a 0-based index into an
1059 array/vector. */
1060enum class dir_index : unsigned int {};
1061
1062/* Likewise, a 1-based file name index. */
1063enum class file_name_index : unsigned int {};
1064
52059ffd
TT
1065struct file_entry
1066{
fff8551c
PA
1067 file_entry () = default;
1068
ecfb656c 1069 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1070 unsigned int mod_time_, unsigned int length_)
1071 : name (name_),
ecfb656c 1072 d_index (d_index_),
fff8551c
PA
1073 mod_time (mod_time_),
1074 length (length_)
1075 {}
1076
ecfb656c
PA
1077 /* Return the include directory at D_INDEX stored in LH. Returns
1078 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1079 const char *include_dir (const line_header *lh) const;
1080
fff8551c
PA
1081 /* The file name. Note this is an observing pointer. The memory is
1082 owned by debug_line_buffer. */
1083 const char *name {};
1084
8c43009f 1085 /* The directory index (1-based). */
ecfb656c 1086 dir_index d_index {};
fff8551c
PA
1087
1088 unsigned int mod_time {};
1089
1090 unsigned int length {};
1091
1092 /* True if referenced by the Line Number Program. */
1093 bool included_p {};
1094
83769d0b 1095 /* The associated symbol table, if any. */
fff8551c 1096 struct symtab *symtab {};
52059ffd
TT
1097};
1098
debd256d
JB
1099/* The line number information for a compilation unit (found in the
1100 .debug_line section) begins with a "statement program header",
1101 which contains the following information. */
1102struct line_header
1103{
fff8551c
PA
1104 line_header ()
1105 : offset_in_dwz {}
1106 {}
1107
1108 /* Add an entry to the include directory table. */
1109 void add_include_dir (const char *include_dir);
1110
1111 /* Add an entry to the file name table. */
ecfb656c 1112 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1113 unsigned int mod_time, unsigned int length);
1114
ecfb656c 1115 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1116 is out of bounds. */
ecfb656c 1117 const char *include_dir_at (dir_index index) const
8c43009f 1118 {
ecfb656c
PA
1119 /* Convert directory index number (1-based) to vector index
1120 (0-based). */
1121 size_t vec_index = to_underlying (index) - 1;
1122
1123 if (vec_index >= include_dirs.size ())
8c43009f 1124 return NULL;
ecfb656c 1125 return include_dirs[vec_index];
8c43009f
PA
1126 }
1127
ecfb656c 1128 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1129 is out of bounds. */
ecfb656c 1130 file_entry *file_name_at (file_name_index index)
8c43009f 1131 {
ecfb656c
PA
1132 /* Convert file name index number (1-based) to vector index
1133 (0-based). */
1134 size_t vec_index = to_underlying (index) - 1;
1135
1136 if (vec_index >= file_names.size ())
fff8551c 1137 return NULL;
ecfb656c 1138 return &file_names[vec_index];
fff8551c
PA
1139 }
1140
1141 /* Const version of the above. */
1142 const file_entry *file_name_at (unsigned int index) const
1143 {
1144 if (index >= file_names.size ())
8c43009f
PA
1145 return NULL;
1146 return &file_names[index];
1147 }
1148
527f3840 1149 /* Offset of line number information in .debug_line section. */
9c541725 1150 sect_offset sect_off {};
527f3840
JK
1151
1152 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1153 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1154
1155 unsigned int total_length {};
1156 unsigned short version {};
1157 unsigned int header_length {};
1158 unsigned char minimum_instruction_length {};
1159 unsigned char maximum_ops_per_instruction {};
1160 unsigned char default_is_stmt {};
1161 int line_base {};
1162 unsigned char line_range {};
1163 unsigned char opcode_base {};
debd256d
JB
1164
1165 /* standard_opcode_lengths[i] is the number of operands for the
1166 standard opcode whose value is i. This means that
1167 standard_opcode_lengths[0] is unused, and the last meaningful
1168 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1169 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1170
fff8551c
PA
1171 /* The include_directories table. Note these are observing
1172 pointers. The memory is owned by debug_line_buffer. */
1173 std::vector<const char *> include_dirs;
debd256d 1174
fff8551c
PA
1175 /* The file_names table. */
1176 std::vector<file_entry> file_names;
debd256d
JB
1177
1178 /* The start and end of the statement program following this
6502dd73 1179 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1180 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1181};
c906108c 1182
fff8551c
PA
1183typedef std::unique_ptr<line_header> line_header_up;
1184
8c43009f
PA
1185const char *
1186file_entry::include_dir (const line_header *lh) const
1187{
ecfb656c 1188 return lh->include_dir_at (d_index);
8c43009f
PA
1189}
1190
c906108c 1191/* When we construct a partial symbol table entry we only
0963b4bd 1192 need this much information. */
c906108c
SS
1193struct partial_die_info
1194 {
72bf9492 1195 /* Offset of this DIE. */
9c541725 1196 sect_offset sect_off;
72bf9492
DJ
1197
1198 /* DWARF-2 tag for this DIE. */
1199 ENUM_BITFIELD(dwarf_tag) tag : 16;
1200
72bf9492
DJ
1201 /* Assorted flags describing the data found in this DIE. */
1202 unsigned int has_children : 1;
1203 unsigned int is_external : 1;
1204 unsigned int is_declaration : 1;
1205 unsigned int has_type : 1;
1206 unsigned int has_specification : 1;
1207 unsigned int has_pc_info : 1;
481860b3 1208 unsigned int may_be_inlined : 1;
72bf9492 1209
0c1b455e
TT
1210 /* This DIE has been marked DW_AT_main_subprogram. */
1211 unsigned int main_subprogram : 1;
1212
72bf9492
DJ
1213 /* Flag set if the SCOPE field of this structure has been
1214 computed. */
1215 unsigned int scope_set : 1;
1216
fa4028e9
JB
1217 /* Flag set if the DIE has a byte_size attribute. */
1218 unsigned int has_byte_size : 1;
1219
ff908ebf
AW
1220 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1221 unsigned int has_const_value : 1;
1222
98bfdba5
PA
1223 /* Flag set if any of the DIE's children are template arguments. */
1224 unsigned int has_template_arguments : 1;
1225
abc72ce4
DE
1226 /* Flag set if fixup_partial_die has been called on this die. */
1227 unsigned int fixup_called : 1;
1228
36586728
TT
1229 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1230 unsigned int is_dwz : 1;
1231
1232 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1233 unsigned int spec_is_dwz : 1;
1234
72bf9492 1235 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1236 sometimes a default name for unnamed DIEs. */
15d034d0 1237 const char *name;
72bf9492 1238
abc72ce4
DE
1239 /* The linkage name, if present. */
1240 const char *linkage_name;
1241
72bf9492
DJ
1242 /* The scope to prepend to our children. This is generally
1243 allocated on the comp_unit_obstack, so will disappear
1244 when this compilation unit leaves the cache. */
15d034d0 1245 const char *scope;
72bf9492 1246
95554aad
TT
1247 /* Some data associated with the partial DIE. The tag determines
1248 which field is live. */
1249 union
1250 {
1251 /* The location description associated with this DIE, if any. */
1252 struct dwarf_block *locdesc;
1253 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1254 sect_offset sect_off;
95554aad 1255 } d;
72bf9492
DJ
1256
1257 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1258 CORE_ADDR lowpc;
1259 CORE_ADDR highpc;
72bf9492 1260
93311388 1261 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1262 DW_AT_sibling, if any. */
abc72ce4
DE
1263 /* NOTE: This member isn't strictly necessary, read_partial_die could
1264 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1265 const gdb_byte *sibling;
72bf9492
DJ
1266
1267 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1268 DW_AT_specification (or DW_AT_abstract_origin or
1269 DW_AT_extension). */
b64f50a1 1270 sect_offset spec_offset;
72bf9492
DJ
1271
1272 /* Pointers to this DIE's parent, first child, and next sibling,
1273 if any. */
1274 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1275 };
1276
0963b4bd 1277/* This data structure holds the information of an abbrev. */
c906108c
SS
1278struct abbrev_info
1279 {
1280 unsigned int number; /* number identifying abbrev */
1281 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1282 unsigned short has_children; /* boolean */
1283 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1284 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1285 struct abbrev_info *next; /* next in chain */
1286 };
1287
1288struct attr_abbrev
1289 {
9d25dd43
DE
1290 ENUM_BITFIELD(dwarf_attribute) name : 16;
1291 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1292
1293 /* It is valid only if FORM is DW_FORM_implicit_const. */
1294 LONGEST implicit_const;
c906108c
SS
1295 };
1296
433df2d4
DE
1297/* Size of abbrev_table.abbrev_hash_table. */
1298#define ABBREV_HASH_SIZE 121
1299
1300/* Top level data structure to contain an abbreviation table. */
1301
1302struct abbrev_table
1303{
f4dc4d17
DE
1304 /* Where the abbrev table came from.
1305 This is used as a sanity check when the table is used. */
9c541725 1306 sect_offset sect_off;
433df2d4
DE
1307
1308 /* Storage for the abbrev table. */
1309 struct obstack abbrev_obstack;
1310
1311 /* Hash table of abbrevs.
1312 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1313 It could be statically allocated, but the previous code didn't so we
1314 don't either. */
1315 struct abbrev_info **abbrevs;
1316};
1317
0963b4bd 1318/* Attributes have a name and a value. */
b60c80d6
DJ
1319struct attribute
1320 {
9d25dd43 1321 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1322 ENUM_BITFIELD(dwarf_form) form : 15;
1323
1324 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1325 field should be in u.str (existing only for DW_STRING) but it is kept
1326 here for better struct attribute alignment. */
1327 unsigned int string_is_canonical : 1;
1328
b60c80d6
DJ
1329 union
1330 {
15d034d0 1331 const char *str;
b60c80d6 1332 struct dwarf_block *blk;
43bbcdc2
PH
1333 ULONGEST unsnd;
1334 LONGEST snd;
b60c80d6 1335 CORE_ADDR addr;
ac9ec31b 1336 ULONGEST signature;
b60c80d6
DJ
1337 }
1338 u;
1339 };
1340
0963b4bd 1341/* This data structure holds a complete die structure. */
c906108c
SS
1342struct die_info
1343 {
76815b17
DE
1344 /* DWARF-2 tag for this DIE. */
1345 ENUM_BITFIELD(dwarf_tag) tag : 16;
1346
1347 /* Number of attributes */
98bfdba5
PA
1348 unsigned char num_attrs;
1349
1350 /* True if we're presently building the full type name for the
1351 type derived from this DIE. */
1352 unsigned char building_fullname : 1;
76815b17 1353
adde2bff
DE
1354 /* True if this die is in process. PR 16581. */
1355 unsigned char in_process : 1;
1356
76815b17
DE
1357 /* Abbrev number */
1358 unsigned int abbrev;
1359
93311388 1360 /* Offset in .debug_info or .debug_types section. */
9c541725 1361 sect_offset sect_off;
78ba4af6
JB
1362
1363 /* The dies in a compilation unit form an n-ary tree. PARENT
1364 points to this die's parent; CHILD points to the first child of
1365 this node; and all the children of a given node are chained
4950bc1c 1366 together via their SIBLING fields. */
639d11d3
DC
1367 struct die_info *child; /* Its first child, if any. */
1368 struct die_info *sibling; /* Its next sibling, if any. */
1369 struct die_info *parent; /* Its parent, if any. */
c906108c 1370
b60c80d6
DJ
1371 /* An array of attributes, with NUM_ATTRS elements. There may be
1372 zero, but it's not common and zero-sized arrays are not
1373 sufficiently portable C. */
1374 struct attribute attrs[1];
c906108c
SS
1375 };
1376
0963b4bd 1377/* Get at parts of an attribute structure. */
c906108c
SS
1378
1379#define DW_STRING(attr) ((attr)->u.str)
8285870a 1380#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1381#define DW_UNSND(attr) ((attr)->u.unsnd)
1382#define DW_BLOCK(attr) ((attr)->u.blk)
1383#define DW_SND(attr) ((attr)->u.snd)
1384#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1385#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1386
0963b4bd 1387/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1388struct dwarf_block
1389 {
56eb65bd 1390 size_t size;
1d6edc3c
JK
1391
1392 /* Valid only if SIZE is not zero. */
d521ce57 1393 const gdb_byte *data;
c906108c
SS
1394 };
1395
c906108c
SS
1396#ifndef ATTR_ALLOC_CHUNK
1397#define ATTR_ALLOC_CHUNK 4
1398#endif
1399
c906108c
SS
1400/* Allocate fields for structs, unions and enums in this size. */
1401#ifndef DW_FIELD_ALLOC_CHUNK
1402#define DW_FIELD_ALLOC_CHUNK 4
1403#endif
1404
c906108c
SS
1405/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1406 but this would require a corresponding change in unpack_field_as_long
1407 and friends. */
1408static int bits_per_byte = 8;
1409
52059ffd
TT
1410struct nextfield
1411{
1412 struct nextfield *next;
1413 int accessibility;
1414 int virtuality;
1415 struct field field;
1416};
1417
1418struct nextfnfield
1419{
1420 struct nextfnfield *next;
1421 struct fn_field fnfield;
1422};
1423
1424struct fnfieldlist
1425{
1426 const char *name;
1427 int length;
1428 struct nextfnfield *head;
1429};
1430
1431struct typedef_field_list
1432{
1433 struct typedef_field field;
1434 struct typedef_field_list *next;
1435};
1436
c906108c
SS
1437/* The routines that read and process dies for a C struct or C++ class
1438 pass lists of data member fields and lists of member function fields
1439 in an instance of a field_info structure, as defined below. */
1440struct field_info
c5aa993b 1441 {
0963b4bd 1442 /* List of data member and baseclasses fields. */
52059ffd 1443 struct nextfield *fields, *baseclasses;
c906108c 1444
7d0ccb61 1445 /* Number of fields (including baseclasses). */
c5aa993b 1446 int nfields;
c906108c 1447
c5aa993b
JM
1448 /* Number of baseclasses. */
1449 int nbaseclasses;
c906108c 1450
c5aa993b
JM
1451 /* Set if the accesibility of one of the fields is not public. */
1452 int non_public_fields;
c906108c 1453
c5aa993b
JM
1454 /* Member function fields array, entries are allocated in the order they
1455 are encountered in the object file. */
52059ffd 1456 struct nextfnfield *fnfields;
c906108c 1457
c5aa993b
JM
1458 /* Member function fieldlist array, contains name of possibly overloaded
1459 member function, number of overloaded member functions and a pointer
1460 to the head of the member function field chain. */
52059ffd 1461 struct fnfieldlist *fnfieldlists;
c906108c 1462
c5aa993b
JM
1463 /* Number of entries in the fnfieldlists array. */
1464 int nfnfields;
98751a41
JK
1465
1466 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1467 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1468 struct typedef_field_list *typedef_field_list;
98751a41 1469 unsigned typedef_field_list_count;
c5aa993b 1470 };
c906108c 1471
10b3939b
DJ
1472/* One item on the queue of compilation units to read in full symbols
1473 for. */
1474struct dwarf2_queue_item
1475{
1476 struct dwarf2_per_cu_data *per_cu;
95554aad 1477 enum language pretend_language;
10b3939b
DJ
1478 struct dwarf2_queue_item *next;
1479};
1480
1481/* The current queue. */
1482static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1483
ae038cb0
DJ
1484/* Loaded secondary compilation units are kept in memory until they
1485 have not been referenced for the processing of this many
1486 compilation units. Set this to zero to disable caching. Cache
1487 sizes of up to at least twenty will improve startup time for
1488 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1489static int dwarf_max_cache_age = 5;
920d2a44 1490static void
b4f54984
DE
1491show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1492 struct cmd_list_element *c, const char *value)
920d2a44 1493{
3e43a32a 1494 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1495 "DWARF compilation units is %s.\n"),
920d2a44
AC
1496 value);
1497}
4390d890 1498\f
c906108c
SS
1499/* local function prototypes */
1500
a32a8923
DE
1501static const char *get_section_name (const struct dwarf2_section_info *);
1502
1503static const char *get_section_file_name (const struct dwarf2_section_info *);
1504
4efb68b1 1505static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1506
918dd910
JK
1507static void dwarf2_find_base_address (struct die_info *die,
1508 struct dwarf2_cu *cu);
1509
0018ea6f
DE
1510static struct partial_symtab *create_partial_symtab
1511 (struct dwarf2_per_cu_data *per_cu, const char *name);
1512
c67a9c90 1513static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1514
72bf9492
DJ
1515static void scan_partial_symbols (struct partial_die_info *,
1516 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1517 int, struct dwarf2_cu *);
c906108c 1518
72bf9492
DJ
1519static void add_partial_symbol (struct partial_die_info *,
1520 struct dwarf2_cu *);
63d06c5c 1521
72bf9492
DJ
1522static void add_partial_namespace (struct partial_die_info *pdi,
1523 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1524 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1525
5d7cb8df 1526static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1527 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1528 struct dwarf2_cu *cu);
1529
72bf9492
DJ
1530static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1531 struct dwarf2_cu *cu);
91c24f0a 1532
bc30ff58
JB
1533static void add_partial_subprogram (struct partial_die_info *pdi,
1534 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1535 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1536
257e7a09
YQ
1537static void dwarf2_read_symtab (struct partial_symtab *,
1538 struct objfile *);
c906108c 1539
a14ed312 1540static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1541
433df2d4
DE
1542static struct abbrev_info *abbrev_table_lookup_abbrev
1543 (const struct abbrev_table *, unsigned int);
1544
1545static struct abbrev_table *abbrev_table_read_table
1546 (struct dwarf2_section_info *, sect_offset);
1547
1548static void abbrev_table_free (struct abbrev_table *);
1549
f4dc4d17
DE
1550static void abbrev_table_free_cleanup (void *);
1551
dee91e82
DE
1552static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1553 struct dwarf2_section_info *);
c906108c 1554
f3dd6933 1555static void dwarf2_free_abbrev_table (void *);
c906108c 1556
d521ce57 1557static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1558
dee91e82 1559static struct partial_die_info *load_partial_dies
d521ce57 1560 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1561
d521ce57
TT
1562static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1563 struct partial_die_info *,
1564 struct abbrev_info *,
1565 unsigned int,
1566 const gdb_byte *);
c906108c 1567
36586728 1568static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1569 struct dwarf2_cu *);
72bf9492
DJ
1570
1571static void fixup_partial_die (struct partial_die_info *,
1572 struct dwarf2_cu *);
1573
d521ce57
TT
1574static const gdb_byte *read_attribute (const struct die_reader_specs *,
1575 struct attribute *, struct attr_abbrev *,
1576 const gdb_byte *);
a8329558 1577
a1855c1d 1578static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1579
a1855c1d 1580static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1581
a1855c1d 1582static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1583
a1855c1d 1584static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1585
a1855c1d 1586static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1587
d521ce57 1588static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1589 unsigned int *);
c906108c 1590
d521ce57 1591static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1592
1593static LONGEST read_checked_initial_length_and_offset
d521ce57 1594 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1595 unsigned int *, unsigned int *);
613e1657 1596
d521ce57
TT
1597static LONGEST read_offset (bfd *, const gdb_byte *,
1598 const struct comp_unit_head *,
c764a876
DE
1599 unsigned int *);
1600
d521ce57 1601static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1602
f4dc4d17
DE
1603static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1604 sect_offset);
1605
d521ce57 1606static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1607
d521ce57 1608static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1609
d521ce57
TT
1610static const char *read_indirect_string (bfd *, const gdb_byte *,
1611 const struct comp_unit_head *,
1612 unsigned int *);
4bdf3d34 1613
43988095
JK
1614static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1615 const struct comp_unit_head *,
1616 unsigned int *);
36586728 1617
43988095 1618static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1619
d521ce57 1620static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1621
d521ce57
TT
1622static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1623 const gdb_byte *,
3019eac3
DE
1624 unsigned int *);
1625
d521ce57 1626static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1627 ULONGEST str_index);
3019eac3 1628
e142c38c 1629static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1630
e142c38c
DJ
1631static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1632 struct dwarf2_cu *);
c906108c 1633
348e048f 1634static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1635 unsigned int);
348e048f 1636
7d45c7c3
KB
1637static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1638 struct dwarf2_cu *cu);
1639
05cf31d1
JB
1640static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1641 struct dwarf2_cu *cu);
1642
e142c38c 1643static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1644
e142c38c 1645static struct die_info *die_specification (struct die_info *die,
f2f0e013 1646 struct dwarf2_cu **);
63d06c5c 1647
9c541725 1648static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1649 struct dwarf2_cu *cu);
debd256d 1650
f3f5162e 1651static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1652 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1653 CORE_ADDR, int decode_mapping);
c906108c 1654
4d663531 1655static void dwarf2_start_subfile (const char *, const char *);
c906108c 1656
43f3e411
DE
1657static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1658 const char *, const char *,
1659 CORE_ADDR);
f4dc4d17 1660
a14ed312 1661static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1662 struct dwarf2_cu *);
c906108c 1663
34eaf542
TT
1664static struct symbol *new_symbol_full (struct die_info *, struct type *,
1665 struct dwarf2_cu *, struct symbol *);
1666
ff39bb5e 1667static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1668 struct dwarf2_cu *);
c906108c 1669
ff39bb5e 1670static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1671 struct type *type,
1672 const char *name,
1673 struct obstack *obstack,
12df843f 1674 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1675 const gdb_byte **bytes,
98bfdba5 1676 struct dwarf2_locexpr_baton **baton);
2df3850c 1677
e7c27a73 1678static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1679
b4ba55a1
JB
1680static int need_gnat_info (struct dwarf2_cu *);
1681
3e43a32a
MS
1682static struct type *die_descriptive_type (struct die_info *,
1683 struct dwarf2_cu *);
b4ba55a1
JB
1684
1685static void set_descriptive_type (struct type *, struct die_info *,
1686 struct dwarf2_cu *);
1687
e7c27a73
DJ
1688static struct type *die_containing_type (struct die_info *,
1689 struct dwarf2_cu *);
c906108c 1690
ff39bb5e 1691static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1692 struct dwarf2_cu *);
c906108c 1693
f792889a 1694static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1695
673bfd45
DE
1696static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1697
0d5cff50 1698static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1699
6e70227d 1700static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1701 const char *suffix, int physname,
1702 struct dwarf2_cu *cu);
63d06c5c 1703
e7c27a73 1704static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1705
348e048f
DE
1706static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1707
e7c27a73 1708static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1709
e7c27a73 1710static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1711
96408a79
SA
1712static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1713
ff013f42
JK
1714static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1715 struct dwarf2_cu *, struct partial_symtab *);
1716
3a2b436a 1717/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1718 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1719enum pc_bounds_kind
1720{
e385593e 1721 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1722 PC_BOUNDS_NOT_PRESENT,
1723
e385593e
JK
1724 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1725 were present but they do not form a valid range of PC addresses. */
1726 PC_BOUNDS_INVALID,
1727
3a2b436a
JK
1728 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1729 PC_BOUNDS_RANGES,
1730
1731 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1732 PC_BOUNDS_HIGH_LOW,
1733};
1734
1735static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1736 CORE_ADDR *, CORE_ADDR *,
1737 struct dwarf2_cu *,
1738 struct partial_symtab *);
c906108c 1739
fae299cd
DC
1740static void get_scope_pc_bounds (struct die_info *,
1741 CORE_ADDR *, CORE_ADDR *,
1742 struct dwarf2_cu *);
1743
801e3a5b
JB
1744static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1745 CORE_ADDR, struct dwarf2_cu *);
1746
a14ed312 1747static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1748 struct dwarf2_cu *);
c906108c 1749
a14ed312 1750static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1751 struct type *, struct dwarf2_cu *);
c906108c 1752
a14ed312 1753static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1754 struct die_info *, struct type *,
e7c27a73 1755 struct dwarf2_cu *);
c906108c 1756
a14ed312 1757static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1758 struct type *,
1759 struct dwarf2_cu *);
c906108c 1760
134d01f1 1761static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1762
e7c27a73 1763static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1764
e7c27a73 1765static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1766
5d7cb8df
JK
1767static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1768
22cee43f
PMR
1769static struct using_direct **using_directives (enum language);
1770
27aa8d6a
SW
1771static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1772
74921315
KS
1773static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1774
f55ee35c
JK
1775static struct type *read_module_type (struct die_info *die,
1776 struct dwarf2_cu *cu);
1777
38d518c9 1778static const char *namespace_name (struct die_info *die,
e142c38c 1779 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1780
134d01f1 1781static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1782
e7c27a73 1783static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1784
6e70227d 1785static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1786 struct dwarf2_cu *);
1787
bf6af496 1788static struct die_info *read_die_and_siblings_1
d521ce57 1789 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1790 struct die_info *);
639d11d3 1791
dee91e82 1792static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1793 const gdb_byte *info_ptr,
1794 const gdb_byte **new_info_ptr,
639d11d3
DC
1795 struct die_info *parent);
1796
d521ce57
TT
1797static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1798 struct die_info **, const gdb_byte *,
1799 int *, int);
3019eac3 1800
d521ce57
TT
1801static const gdb_byte *read_full_die (const struct die_reader_specs *,
1802 struct die_info **, const gdb_byte *,
1803 int *);
93311388 1804
e7c27a73 1805static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1806
15d034d0
TT
1807static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1808 struct obstack *);
71c25dea 1809
15d034d0 1810static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1811
15d034d0 1812static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1813 struct die_info *die,
1814 struct dwarf2_cu *cu);
1815
ca69b9e6
DE
1816static const char *dwarf2_physname (const char *name, struct die_info *die,
1817 struct dwarf2_cu *cu);
1818
e142c38c 1819static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1820 struct dwarf2_cu **);
9219021c 1821
f39c6ffd 1822static const char *dwarf_tag_name (unsigned int);
c906108c 1823
f39c6ffd 1824static const char *dwarf_attr_name (unsigned int);
c906108c 1825
f39c6ffd 1826static const char *dwarf_form_name (unsigned int);
c906108c 1827
a121b7c1 1828static const char *dwarf_bool_name (unsigned int);
c906108c 1829
f39c6ffd 1830static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1831
f9aca02d 1832static struct die_info *sibling_die (struct die_info *);
c906108c 1833
d97bc12b
DE
1834static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1835
1836static void dump_die_for_error (struct die_info *);
1837
1838static void dump_die_1 (struct ui_file *, int level, int max_level,
1839 struct die_info *);
c906108c 1840
d97bc12b 1841/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1842
51545339 1843static void store_in_ref_table (struct die_info *,
10b3939b 1844 struct dwarf2_cu *);
c906108c 1845
ff39bb5e 1846static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1847
ff39bb5e 1848static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1849
348e048f 1850static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1851 const struct attribute *,
348e048f
DE
1852 struct dwarf2_cu **);
1853
10b3939b 1854static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1855 const struct attribute *,
f2f0e013 1856 struct dwarf2_cu **);
c906108c 1857
348e048f 1858static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1859 const struct attribute *,
348e048f
DE
1860 struct dwarf2_cu **);
1861
ac9ec31b
DE
1862static struct type *get_signatured_type (struct die_info *, ULONGEST,
1863 struct dwarf2_cu *);
1864
1865static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1866 const struct attribute *,
ac9ec31b
DE
1867 struct dwarf2_cu *);
1868
e5fe5e75 1869static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1870
52dc124a 1871static void read_signatured_type (struct signatured_type *);
348e048f 1872
63e43d3a
PMR
1873static int attr_to_dynamic_prop (const struct attribute *attr,
1874 struct die_info *die, struct dwarf2_cu *cu,
1875 struct dynamic_prop *prop);
1876
c906108c
SS
1877/* memory allocation interface */
1878
7b5a2f43 1879static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1880
b60c80d6 1881static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1882
43f3e411 1883static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1884
6e5a29e1 1885static int attr_form_is_block (const struct attribute *);
8e19ed76 1886
6e5a29e1 1887static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1888
6e5a29e1 1889static int attr_form_is_constant (const struct attribute *);
3690dd37 1890
6e5a29e1 1891static int attr_form_is_ref (const struct attribute *);
7771576e 1892
8cf6f0b1
TT
1893static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1894 struct dwarf2_loclist_baton *baton,
ff39bb5e 1895 const struct attribute *attr);
8cf6f0b1 1896
ff39bb5e 1897static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1898 struct symbol *sym,
f1e6e072
TT
1899 struct dwarf2_cu *cu,
1900 int is_block);
4c2df51b 1901
d521ce57
TT
1902static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1903 const gdb_byte *info_ptr,
1904 struct abbrev_info *abbrev);
4bb7a0a7 1905
72bf9492
DJ
1906static void free_stack_comp_unit (void *);
1907
72bf9492
DJ
1908static hashval_t partial_die_hash (const void *item);
1909
1910static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1911
ae038cb0 1912static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1913 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1914
9816fde3 1915static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1916 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1917
1918static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1919 struct die_info *comp_unit_die,
1920 enum language pretend_language);
93311388 1921
68dc6402 1922static void free_heap_comp_unit (void *);
ae038cb0
DJ
1923
1924static void free_cached_comp_units (void *);
1925
1926static void age_cached_comp_units (void);
1927
dee91e82 1928static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1929
f792889a
DJ
1930static struct type *set_die_type (struct die_info *, struct type *,
1931 struct dwarf2_cu *);
1c379e20 1932
ae038cb0
DJ
1933static void create_all_comp_units (struct objfile *);
1934
0e50663e 1935static int create_all_type_units (struct objfile *);
1fd400ff 1936
95554aad
TT
1937static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1938 enum language);
10b3939b 1939
95554aad
TT
1940static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1941 enum language);
10b3939b 1942
f4dc4d17
DE
1943static void process_full_type_unit (struct dwarf2_per_cu_data *,
1944 enum language);
1945
10b3939b
DJ
1946static void dwarf2_add_dependence (struct dwarf2_cu *,
1947 struct dwarf2_per_cu_data *);
1948
ae038cb0
DJ
1949static void dwarf2_mark (struct dwarf2_cu *);
1950
1951static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1952
b64f50a1 1953static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1954 struct dwarf2_per_cu_data *);
673bfd45 1955
f792889a 1956static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1957
9291a0cd
TT
1958static void dwarf2_release_queue (void *dummy);
1959
95554aad
TT
1960static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1961 enum language pretend_language);
1962
a0f42c21 1963static void process_queue (void);
9291a0cd 1964
d721ba37
PA
1965/* The return type of find_file_and_directory. Note, the enclosed
1966 string pointers are only valid while this object is valid. */
1967
1968struct file_and_directory
1969{
1970 /* The filename. This is never NULL. */
1971 const char *name;
1972
1973 /* The compilation directory. NULL if not known. If we needed to
1974 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1975 points directly to the DW_AT_comp_dir string attribute owned by
1976 the obstack that owns the DIE. */
1977 const char *comp_dir;
1978
1979 /* If we needed to build a new string for comp_dir, this is what
1980 owns the storage. */
1981 std::string comp_dir_storage;
1982};
1983
1984static file_and_directory find_file_and_directory (struct die_info *die,
1985 struct dwarf2_cu *cu);
9291a0cd
TT
1986
1987static char *file_full_name (int file, struct line_header *lh,
1988 const char *comp_dir);
1989
43988095
JK
1990/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1991enum class rcuh_kind { COMPILE, TYPE };
1992
d521ce57 1993static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1994 (struct comp_unit_head *header,
1995 struct dwarf2_section_info *section,
d521ce57 1996 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1997 rcuh_kind section_kind);
36586728 1998
fd820528 1999static void init_cutu_and_read_dies
f4dc4d17
DE
2000 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2001 int use_existing_cu, int keep,
3019eac3
DE
2002 die_reader_func_ftype *die_reader_func, void *data);
2003
dee91e82
DE
2004static void init_cutu_and_read_dies_simple
2005 (struct dwarf2_per_cu_data *this_cu,
2006 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2007
673bfd45 2008static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2009
3019eac3
DE
2010static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2011
57d63ce2
DE
2012static struct dwo_unit *lookup_dwo_unit_in_dwp
2013 (struct dwp_file *dwp_file, const char *comp_dir,
2014 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2015
2016static struct dwp_file *get_dwp_file (void);
2017
3019eac3 2018static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2019 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2020
2021static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2022 (struct signatured_type *, const char *, const char *);
3019eac3 2023
89e63ee4
DE
2024static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2025
3019eac3
DE
2026static void free_dwo_file_cleanup (void *);
2027
95554aad
TT
2028static void process_cu_includes (void);
2029
1b80a9fa 2030static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2031
2032static void free_line_header_voidp (void *arg);
4390d890
DE
2033\f
2034/* Various complaints about symbol reading that don't abort the process. */
2035
2036static void
2037dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2038{
2039 complaint (&symfile_complaints,
2040 _("statement list doesn't fit in .debug_line section"));
2041}
2042
2043static void
2044dwarf2_debug_line_missing_file_complaint (void)
2045{
2046 complaint (&symfile_complaints,
2047 _(".debug_line section has line data without a file"));
2048}
2049
2050static void
2051dwarf2_debug_line_missing_end_sequence_complaint (void)
2052{
2053 complaint (&symfile_complaints,
2054 _(".debug_line section has line "
2055 "program sequence without an end"));
2056}
2057
2058static void
2059dwarf2_complex_location_expr_complaint (void)
2060{
2061 complaint (&symfile_complaints, _("location expression too complex"));
2062}
2063
2064static void
2065dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2066 int arg3)
2067{
2068 complaint (&symfile_complaints,
2069 _("const value length mismatch for '%s', got %d, expected %d"),
2070 arg1, arg2, arg3);
2071}
2072
2073static void
2074dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2075{
2076 complaint (&symfile_complaints,
2077 _("debug info runs off end of %s section"
2078 " [in module %s]"),
a32a8923
DE
2079 get_section_name (section),
2080 get_section_file_name (section));
4390d890 2081}
1b80a9fa 2082
4390d890
DE
2083static void
2084dwarf2_macro_malformed_definition_complaint (const char *arg1)
2085{
2086 complaint (&symfile_complaints,
2087 _("macro debug info contains a "
2088 "malformed macro definition:\n`%s'"),
2089 arg1);
2090}
2091
2092static void
2093dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2094{
2095 complaint (&symfile_complaints,
2096 _("invalid attribute class or form for '%s' in '%s'"),
2097 arg1, arg2);
2098}
527f3840
JK
2099
2100/* Hash function for line_header_hash. */
2101
2102static hashval_t
2103line_header_hash (const struct line_header *ofs)
2104{
9c541725 2105 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2106}
2107
2108/* Hash function for htab_create_alloc_ex for line_header_hash. */
2109
2110static hashval_t
2111line_header_hash_voidp (const void *item)
2112{
9a3c8263 2113 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2114
2115 return line_header_hash (ofs);
2116}
2117
2118/* Equality function for line_header_hash. */
2119
2120static int
2121line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2122{
9a3c8263
SM
2123 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2124 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2125
9c541725 2126 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2127 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2128}
2129
4390d890 2130\f
9291a0cd
TT
2131#if WORDS_BIGENDIAN
2132
2133/* Convert VALUE between big- and little-endian. */
2134static offset_type
2135byte_swap (offset_type value)
2136{
2137 offset_type result;
2138
2139 result = (value & 0xff) << 24;
2140 result |= (value & 0xff00) << 8;
2141 result |= (value & 0xff0000) >> 8;
2142 result |= (value & 0xff000000) >> 24;
2143 return result;
2144}
2145
2146#define MAYBE_SWAP(V) byte_swap (V)
2147
2148#else
2149#define MAYBE_SWAP(V) (V)
2150#endif /* WORDS_BIGENDIAN */
2151
31aa7e4e
JB
2152/* Read the given attribute value as an address, taking the attribute's
2153 form into account. */
2154
2155static CORE_ADDR
2156attr_value_as_address (struct attribute *attr)
2157{
2158 CORE_ADDR addr;
2159
2160 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2161 {
2162 /* Aside from a few clearly defined exceptions, attributes that
2163 contain an address must always be in DW_FORM_addr form.
2164 Unfortunately, some compilers happen to be violating this
2165 requirement by encoding addresses using other forms, such
2166 as DW_FORM_data4 for example. For those broken compilers,
2167 we try to do our best, without any guarantee of success,
2168 to interpret the address correctly. It would also be nice
2169 to generate a complaint, but that would require us to maintain
2170 a list of legitimate cases where a non-address form is allowed,
2171 as well as update callers to pass in at least the CU's DWARF
2172 version. This is more overhead than what we're willing to
2173 expand for a pretty rare case. */
2174 addr = DW_UNSND (attr);
2175 }
2176 else
2177 addr = DW_ADDR (attr);
2178
2179 return addr;
2180}
2181
9291a0cd
TT
2182/* The suffix for an index file. */
2183#define INDEX_SUFFIX ".gdb-index"
2184
c906108c 2185/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2186 information and return true if we have enough to do something.
2187 NAMES points to the dwarf2 section names, or is NULL if the standard
2188 ELF names are used. */
c906108c
SS
2189
2190int
251d32d9
TG
2191dwarf2_has_info (struct objfile *objfile,
2192 const struct dwarf2_debug_sections *names)
c906108c 2193{
9a3c8263
SM
2194 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2195 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2196 if (!dwarf2_per_objfile)
2197 {
2198 /* Initialize per-objfile state. */
2199 struct dwarf2_per_objfile *data
8d749320 2200 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2201
be391dca
TT
2202 memset (data, 0, sizeof (*data));
2203 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2204 dwarf2_per_objfile = data;
6502dd73 2205
251d32d9
TG
2206 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2207 (void *) names);
be391dca
TT
2208 dwarf2_per_objfile->objfile = objfile;
2209 }
73869dc2 2210 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2211 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2212 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2213 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2214}
2215
2216/* Return the containing section of virtual section SECTION. */
2217
2218static struct dwarf2_section_info *
2219get_containing_section (const struct dwarf2_section_info *section)
2220{
2221 gdb_assert (section->is_virtual);
2222 return section->s.containing_section;
c906108c
SS
2223}
2224
a32a8923
DE
2225/* Return the bfd owner of SECTION. */
2226
2227static struct bfd *
2228get_section_bfd_owner (const struct dwarf2_section_info *section)
2229{
73869dc2
DE
2230 if (section->is_virtual)
2231 {
2232 section = get_containing_section (section);
2233 gdb_assert (!section->is_virtual);
2234 }
049412e3 2235 return section->s.section->owner;
a32a8923
DE
2236}
2237
2238/* Return the bfd section of SECTION.
2239 Returns NULL if the section is not present. */
2240
2241static asection *
2242get_section_bfd_section (const struct dwarf2_section_info *section)
2243{
73869dc2
DE
2244 if (section->is_virtual)
2245 {
2246 section = get_containing_section (section);
2247 gdb_assert (!section->is_virtual);
2248 }
049412e3 2249 return section->s.section;
a32a8923
DE
2250}
2251
2252/* Return the name of SECTION. */
2253
2254static const char *
2255get_section_name (const struct dwarf2_section_info *section)
2256{
2257 asection *sectp = get_section_bfd_section (section);
2258
2259 gdb_assert (sectp != NULL);
2260 return bfd_section_name (get_section_bfd_owner (section), sectp);
2261}
2262
2263/* Return the name of the file SECTION is in. */
2264
2265static const char *
2266get_section_file_name (const struct dwarf2_section_info *section)
2267{
2268 bfd *abfd = get_section_bfd_owner (section);
2269
2270 return bfd_get_filename (abfd);
2271}
2272
2273/* Return the id of SECTION.
2274 Returns 0 if SECTION doesn't exist. */
2275
2276static int
2277get_section_id (const struct dwarf2_section_info *section)
2278{
2279 asection *sectp = get_section_bfd_section (section);
2280
2281 if (sectp == NULL)
2282 return 0;
2283 return sectp->id;
2284}
2285
2286/* Return the flags of SECTION.
73869dc2 2287 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2288
2289static int
2290get_section_flags (const struct dwarf2_section_info *section)
2291{
2292 asection *sectp = get_section_bfd_section (section);
2293
2294 gdb_assert (sectp != NULL);
2295 return bfd_get_section_flags (sectp->owner, sectp);
2296}
2297
251d32d9
TG
2298/* When loading sections, we look either for uncompressed section or for
2299 compressed section names. */
233a11ab
CS
2300
2301static int
251d32d9
TG
2302section_is_p (const char *section_name,
2303 const struct dwarf2_section_names *names)
233a11ab 2304{
251d32d9
TG
2305 if (names->normal != NULL
2306 && strcmp (section_name, names->normal) == 0)
2307 return 1;
2308 if (names->compressed != NULL
2309 && strcmp (section_name, names->compressed) == 0)
2310 return 1;
2311 return 0;
233a11ab
CS
2312}
2313
c906108c
SS
2314/* This function is mapped across the sections and remembers the
2315 offset and size of each of the debugging sections we are interested
2316 in. */
2317
2318static void
251d32d9 2319dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2320{
251d32d9 2321 const struct dwarf2_debug_sections *names;
dc7650b8 2322 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2323
2324 if (vnames == NULL)
2325 names = &dwarf2_elf_names;
2326 else
2327 names = (const struct dwarf2_debug_sections *) vnames;
2328
dc7650b8
JK
2329 if ((aflag & SEC_HAS_CONTENTS) == 0)
2330 {
2331 }
2332 else if (section_is_p (sectp->name, &names->info))
c906108c 2333 {
049412e3 2334 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2335 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2336 }
251d32d9 2337 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2338 {
049412e3 2339 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2340 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2341 }
251d32d9 2342 else if (section_is_p (sectp->name, &names->line))
c906108c 2343 {
049412e3 2344 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2345 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2346 }
251d32d9 2347 else if (section_is_p (sectp->name, &names->loc))
c906108c 2348 {
049412e3 2349 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2350 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2351 }
43988095
JK
2352 else if (section_is_p (sectp->name, &names->loclists))
2353 {
2354 dwarf2_per_objfile->loclists.s.section = sectp;
2355 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2356 }
251d32d9 2357 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2358 {
049412e3 2359 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2360 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2361 }
cf2c3c16
TT
2362 else if (section_is_p (sectp->name, &names->macro))
2363 {
049412e3 2364 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2365 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2366 }
251d32d9 2367 else if (section_is_p (sectp->name, &names->str))
c906108c 2368 {
049412e3 2369 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2370 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2371 }
43988095
JK
2372 else if (section_is_p (sectp->name, &names->line_str))
2373 {
2374 dwarf2_per_objfile->line_str.s.section = sectp;
2375 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2376 }
3019eac3
DE
2377 else if (section_is_p (sectp->name, &names->addr))
2378 {
049412e3 2379 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2380 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2381 }
251d32d9 2382 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2383 {
049412e3 2384 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2385 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2386 }
251d32d9 2387 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2388 {
049412e3 2389 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2390 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2391 }
251d32d9 2392 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2393 {
049412e3 2394 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2395 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2396 }
43988095
JK
2397 else if (section_is_p (sectp->name, &names->rnglists))
2398 {
2399 dwarf2_per_objfile->rnglists.s.section = sectp;
2400 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2401 }
251d32d9 2402 else if (section_is_p (sectp->name, &names->types))
348e048f 2403 {
8b70b953
TT
2404 struct dwarf2_section_info type_section;
2405
2406 memset (&type_section, 0, sizeof (type_section));
049412e3 2407 type_section.s.section = sectp;
8b70b953
TT
2408 type_section.size = bfd_get_section_size (sectp);
2409
2410 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2411 &type_section);
348e048f 2412 }
251d32d9 2413 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2414 {
049412e3 2415 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2416 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2417 }
dce234bc 2418
b4e1fd61 2419 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2420 && bfd_section_vma (abfd, sectp) == 0)
2421 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2422}
2423
fceca515
DE
2424/* A helper function that decides whether a section is empty,
2425 or not present. */
9e0ac564
TT
2426
2427static int
19ac8c2e 2428dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2429{
73869dc2
DE
2430 if (section->is_virtual)
2431 return section->size == 0;
049412e3 2432 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2433}
2434
3019eac3
DE
2435/* Read the contents of the section INFO.
2436 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2437 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2438 of the DWO file.
dce234bc 2439 If the section is compressed, uncompress it before returning. */
c906108c 2440
dce234bc
PP
2441static void
2442dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2443{
a32a8923 2444 asection *sectp;
3019eac3 2445 bfd *abfd;
dce234bc 2446 gdb_byte *buf, *retbuf;
c906108c 2447
be391dca
TT
2448 if (info->readin)
2449 return;
dce234bc 2450 info->buffer = NULL;
be391dca 2451 info->readin = 1;
188dd5d6 2452
9e0ac564 2453 if (dwarf2_section_empty_p (info))
dce234bc 2454 return;
c906108c 2455
a32a8923 2456 sectp = get_section_bfd_section (info);
3019eac3 2457
73869dc2
DE
2458 /* If this is a virtual section we need to read in the real one first. */
2459 if (info->is_virtual)
2460 {
2461 struct dwarf2_section_info *containing_section =
2462 get_containing_section (info);
2463
2464 gdb_assert (sectp != NULL);
2465 if ((sectp->flags & SEC_RELOC) != 0)
2466 {
2467 error (_("Dwarf Error: DWP format V2 with relocations is not"
2468 " supported in section %s [in module %s]"),
2469 get_section_name (info), get_section_file_name (info));
2470 }
2471 dwarf2_read_section (objfile, containing_section);
2472 /* Other code should have already caught virtual sections that don't
2473 fit. */
2474 gdb_assert (info->virtual_offset + info->size
2475 <= containing_section->size);
2476 /* If the real section is empty or there was a problem reading the
2477 section we shouldn't get here. */
2478 gdb_assert (containing_section->buffer != NULL);
2479 info->buffer = containing_section->buffer + info->virtual_offset;
2480 return;
2481 }
2482
4bf44c1c
TT
2483 /* If the section has relocations, we must read it ourselves.
2484 Otherwise we attach it to the BFD. */
2485 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2486 {
d521ce57 2487 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2488 return;
dce234bc 2489 }
dce234bc 2490
224c3ddb 2491 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2492 info->buffer = buf;
dce234bc
PP
2493
2494 /* When debugging .o files, we may need to apply relocations; see
2495 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2496 We never compress sections in .o files, so we only need to
2497 try this when the section is not compressed. */
ac8035ab 2498 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2499 if (retbuf != NULL)
2500 {
2501 info->buffer = retbuf;
2502 return;
2503 }
2504
a32a8923
DE
2505 abfd = get_section_bfd_owner (info);
2506 gdb_assert (abfd != NULL);
2507
dce234bc
PP
2508 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2509 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2510 {
2511 error (_("Dwarf Error: Can't read DWARF data"
2512 " in section %s [in module %s]"),
2513 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2514 }
dce234bc
PP
2515}
2516
9e0ac564
TT
2517/* A helper function that returns the size of a section in a safe way.
2518 If you are positive that the section has been read before using the
2519 size, then it is safe to refer to the dwarf2_section_info object's
2520 "size" field directly. In other cases, you must call this
2521 function, because for compressed sections the size field is not set
2522 correctly until the section has been read. */
2523
2524static bfd_size_type
2525dwarf2_section_size (struct objfile *objfile,
2526 struct dwarf2_section_info *info)
2527{
2528 if (!info->readin)
2529 dwarf2_read_section (objfile, info);
2530 return info->size;
2531}
2532
dce234bc 2533/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2534 SECTION_NAME. */
af34e669 2535
dce234bc 2536void
3017a003
TG
2537dwarf2_get_section_info (struct objfile *objfile,
2538 enum dwarf2_section_enum sect,
d521ce57 2539 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2540 bfd_size_type *sizep)
2541{
2542 struct dwarf2_per_objfile *data
9a3c8263
SM
2543 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2544 dwarf2_objfile_data_key);
dce234bc 2545 struct dwarf2_section_info *info;
a3b2a86b
TT
2546
2547 /* We may see an objfile without any DWARF, in which case we just
2548 return nothing. */
2549 if (data == NULL)
2550 {
2551 *sectp = NULL;
2552 *bufp = NULL;
2553 *sizep = 0;
2554 return;
2555 }
3017a003
TG
2556 switch (sect)
2557 {
2558 case DWARF2_DEBUG_FRAME:
2559 info = &data->frame;
2560 break;
2561 case DWARF2_EH_FRAME:
2562 info = &data->eh_frame;
2563 break;
2564 default:
2565 gdb_assert_not_reached ("unexpected section");
2566 }
dce234bc 2567
9e0ac564 2568 dwarf2_read_section (objfile, info);
dce234bc 2569
a32a8923 2570 *sectp = get_section_bfd_section (info);
dce234bc
PP
2571 *bufp = info->buffer;
2572 *sizep = info->size;
2573}
2574
36586728
TT
2575/* A helper function to find the sections for a .dwz file. */
2576
2577static void
2578locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2579{
9a3c8263 2580 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2581
2582 /* Note that we only support the standard ELF names, because .dwz
2583 is ELF-only (at the time of writing). */
2584 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2585 {
049412e3 2586 dwz_file->abbrev.s.section = sectp;
36586728
TT
2587 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2588 }
2589 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2590 {
049412e3 2591 dwz_file->info.s.section = sectp;
36586728
TT
2592 dwz_file->info.size = bfd_get_section_size (sectp);
2593 }
2594 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2595 {
049412e3 2596 dwz_file->str.s.section = sectp;
36586728
TT
2597 dwz_file->str.size = bfd_get_section_size (sectp);
2598 }
2599 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2600 {
049412e3 2601 dwz_file->line.s.section = sectp;
36586728
TT
2602 dwz_file->line.size = bfd_get_section_size (sectp);
2603 }
2604 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2605 {
049412e3 2606 dwz_file->macro.s.section = sectp;
36586728
TT
2607 dwz_file->macro.size = bfd_get_section_size (sectp);
2608 }
2ec9a5e0
TT
2609 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2610 {
049412e3 2611 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2612 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2613 }
36586728
TT
2614}
2615
4db1a1dc
TT
2616/* Open the separate '.dwz' debug file, if needed. Return NULL if
2617 there is no .gnu_debugaltlink section in the file. Error if there
2618 is such a section but the file cannot be found. */
36586728
TT
2619
2620static struct dwz_file *
2621dwarf2_get_dwz_file (void)
2622{
4db1a1dc 2623 char *data;
36586728
TT
2624 struct cleanup *cleanup;
2625 const char *filename;
2626 struct dwz_file *result;
acd13123 2627 bfd_size_type buildid_len_arg;
dc294be5
TT
2628 size_t buildid_len;
2629 bfd_byte *buildid;
36586728
TT
2630
2631 if (dwarf2_per_objfile->dwz_file != NULL)
2632 return dwarf2_per_objfile->dwz_file;
2633
4db1a1dc
TT
2634 bfd_set_error (bfd_error_no_error);
2635 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2636 &buildid_len_arg, &buildid);
4db1a1dc
TT
2637 if (data == NULL)
2638 {
2639 if (bfd_get_error () == bfd_error_no_error)
2640 return NULL;
2641 error (_("could not read '.gnu_debugaltlink' section: %s"),
2642 bfd_errmsg (bfd_get_error ()));
2643 }
36586728 2644 cleanup = make_cleanup (xfree, data);
dc294be5 2645 make_cleanup (xfree, buildid);
36586728 2646
acd13123
TT
2647 buildid_len = (size_t) buildid_len_arg;
2648
f9d83a0b 2649 filename = (const char *) data;
d721ba37
PA
2650
2651 std::string abs_storage;
36586728
TT
2652 if (!IS_ABSOLUTE_PATH (filename))
2653 {
4262abfb 2654 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2655
2656 make_cleanup (xfree, abs);
d721ba37
PA
2657 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2658 filename = abs_storage.c_str ();
36586728
TT
2659 }
2660
dc294be5
TT
2661 /* First try the file name given in the section. If that doesn't
2662 work, try to use the build-id instead. */
192b62ce 2663 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2664 if (dwz_bfd != NULL)
36586728 2665 {
192b62ce
TT
2666 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2667 dwz_bfd.release ();
36586728
TT
2668 }
2669
dc294be5
TT
2670 if (dwz_bfd == NULL)
2671 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2672
2673 if (dwz_bfd == NULL)
2674 error (_("could not find '.gnu_debugaltlink' file for %s"),
2675 objfile_name (dwarf2_per_objfile->objfile));
2676
36586728
TT
2677 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2678 struct dwz_file);
192b62ce 2679 result->dwz_bfd = dwz_bfd.release ();
36586728 2680
192b62ce 2681 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2682
2683 do_cleanups (cleanup);
2684
192b62ce 2685 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2686 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2687 return result;
2688}
9291a0cd 2689\f
7b9f3c50
DE
2690/* DWARF quick_symbols_functions support. */
2691
2692/* TUs can share .debug_line entries, and there can be a lot more TUs than
2693 unique line tables, so we maintain a separate table of all .debug_line
2694 derived entries to support the sharing.
2695 All the quick functions need is the list of file names. We discard the
2696 line_header when we're done and don't need to record it here. */
2697struct quick_file_names
2698{
094b34ac
DE
2699 /* The data used to construct the hash key. */
2700 struct stmt_list_hash hash;
7b9f3c50
DE
2701
2702 /* The number of entries in file_names, real_names. */
2703 unsigned int num_file_names;
2704
2705 /* The file names from the line table, after being run through
2706 file_full_name. */
2707 const char **file_names;
2708
2709 /* The file names from the line table after being run through
2710 gdb_realpath. These are computed lazily. */
2711 const char **real_names;
2712};
2713
2714/* When using the index (and thus not using psymtabs), each CU has an
2715 object of this type. This is used to hold information needed by
2716 the various "quick" methods. */
2717struct dwarf2_per_cu_quick_data
2718{
2719 /* The file table. This can be NULL if there was no file table
2720 or it's currently not read in.
2721 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2722 struct quick_file_names *file_names;
2723
2724 /* The corresponding symbol table. This is NULL if symbols for this
2725 CU have not yet been read. */
43f3e411 2726 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2727
2728 /* A temporary mark bit used when iterating over all CUs in
2729 expand_symtabs_matching. */
2730 unsigned int mark : 1;
2731
2732 /* True if we've tried to read the file table and found there isn't one.
2733 There will be no point in trying to read it again next time. */
2734 unsigned int no_file_data : 1;
2735};
2736
094b34ac
DE
2737/* Utility hash function for a stmt_list_hash. */
2738
2739static hashval_t
2740hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2741{
2742 hashval_t v = 0;
2743
2744 if (stmt_list_hash->dwo_unit != NULL)
2745 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2746 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2747 return v;
2748}
2749
2750/* Utility equality function for a stmt_list_hash. */
2751
2752static int
2753eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2754 const struct stmt_list_hash *rhs)
2755{
2756 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2757 return 0;
2758 if (lhs->dwo_unit != NULL
2759 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2760 return 0;
2761
9c541725 2762 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2763}
2764
7b9f3c50
DE
2765/* Hash function for a quick_file_names. */
2766
2767static hashval_t
2768hash_file_name_entry (const void *e)
2769{
9a3c8263
SM
2770 const struct quick_file_names *file_data
2771 = (const struct quick_file_names *) e;
7b9f3c50 2772
094b34ac 2773 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2774}
2775
2776/* Equality function for a quick_file_names. */
2777
2778static int
2779eq_file_name_entry (const void *a, const void *b)
2780{
9a3c8263
SM
2781 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2782 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2783
094b34ac 2784 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2785}
2786
2787/* Delete function for a quick_file_names. */
2788
2789static void
2790delete_file_name_entry (void *e)
2791{
9a3c8263 2792 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2793 int i;
2794
2795 for (i = 0; i < file_data->num_file_names; ++i)
2796 {
2797 xfree ((void*) file_data->file_names[i]);
2798 if (file_data->real_names)
2799 xfree ((void*) file_data->real_names[i]);
2800 }
2801
2802 /* The space for the struct itself lives on objfile_obstack,
2803 so we don't free it here. */
2804}
2805
2806/* Create a quick_file_names hash table. */
2807
2808static htab_t
2809create_quick_file_names_table (unsigned int nr_initial_entries)
2810{
2811 return htab_create_alloc (nr_initial_entries,
2812 hash_file_name_entry, eq_file_name_entry,
2813 delete_file_name_entry, xcalloc, xfree);
2814}
9291a0cd 2815
918dd910
JK
2816/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2817 have to be created afterwards. You should call age_cached_comp_units after
2818 processing PER_CU->CU. dw2_setup must have been already called. */
2819
2820static void
2821load_cu (struct dwarf2_per_cu_data *per_cu)
2822{
3019eac3 2823 if (per_cu->is_debug_types)
e5fe5e75 2824 load_full_type_unit (per_cu);
918dd910 2825 else
95554aad 2826 load_full_comp_unit (per_cu, language_minimal);
918dd910 2827
cc12ce38
DE
2828 if (per_cu->cu == NULL)
2829 return; /* Dummy CU. */
2dc860c0
DE
2830
2831 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2832}
2833
a0f42c21 2834/* Read in the symbols for PER_CU. */
2fdf6df6 2835
9291a0cd 2836static void
a0f42c21 2837dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2838{
2839 struct cleanup *back_to;
2840
f4dc4d17
DE
2841 /* Skip type_unit_groups, reading the type units they contain
2842 is handled elsewhere. */
2843 if (IS_TYPE_UNIT_GROUP (per_cu))
2844 return;
2845
9291a0cd
TT
2846 back_to = make_cleanup (dwarf2_release_queue, NULL);
2847
95554aad 2848 if (dwarf2_per_objfile->using_index
43f3e411 2849 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2850 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2851 {
2852 queue_comp_unit (per_cu, language_minimal);
2853 load_cu (per_cu);
89e63ee4
DE
2854
2855 /* If we just loaded a CU from a DWO, and we're working with an index
2856 that may badly handle TUs, load all the TUs in that DWO as well.
2857 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2858 if (!per_cu->is_debug_types
cc12ce38 2859 && per_cu->cu != NULL
89e63ee4
DE
2860 && per_cu->cu->dwo_unit != NULL
2861 && dwarf2_per_objfile->index_table != NULL
2862 && dwarf2_per_objfile->index_table->version <= 7
2863 /* DWP files aren't supported yet. */
2864 && get_dwp_file () == NULL)
2865 queue_and_load_all_dwo_tus (per_cu);
95554aad 2866 }
9291a0cd 2867
a0f42c21 2868 process_queue ();
9291a0cd
TT
2869
2870 /* Age the cache, releasing compilation units that have not
2871 been used recently. */
2872 age_cached_comp_units ();
2873
2874 do_cleanups (back_to);
2875}
2876
2877/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2878 the objfile from which this CU came. Returns the resulting symbol
2879 table. */
2fdf6df6 2880
43f3e411 2881static struct compunit_symtab *
a0f42c21 2882dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2883{
95554aad 2884 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2885 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2886 {
2887 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2888 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2889 dw2_do_instantiate_symtab (per_cu);
95554aad 2890 process_cu_includes ();
9291a0cd
TT
2891 do_cleanups (back_to);
2892 }
f194fefb 2893
43f3e411 2894 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2895}
2896
8832e7e3 2897/* Return the CU/TU given its index.
f4dc4d17
DE
2898
2899 This is intended for loops like:
2900
2901 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2902 + dwarf2_per_objfile->n_type_units); ++i)
2903 {
8832e7e3 2904 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2905
2906 ...;
2907 }
2908*/
2fdf6df6 2909
1fd400ff 2910static struct dwarf2_per_cu_data *
8832e7e3 2911dw2_get_cutu (int index)
1fd400ff
TT
2912{
2913 if (index >= dwarf2_per_objfile->n_comp_units)
2914 {
f4dc4d17 2915 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2916 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2917 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2918 }
2919
2920 return dwarf2_per_objfile->all_comp_units[index];
2921}
2922
8832e7e3
DE
2923/* Return the CU given its index.
2924 This differs from dw2_get_cutu in that it's for when you know INDEX
2925 refers to a CU. */
f4dc4d17
DE
2926
2927static struct dwarf2_per_cu_data *
8832e7e3 2928dw2_get_cu (int index)
f4dc4d17 2929{
8832e7e3 2930 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2931
1fd400ff
TT
2932 return dwarf2_per_objfile->all_comp_units[index];
2933}
2934
2ec9a5e0
TT
2935/* A helper for create_cus_from_index that handles a given list of
2936 CUs. */
2fdf6df6 2937
74a0d9f6 2938static void
2ec9a5e0
TT
2939create_cus_from_index_list (struct objfile *objfile,
2940 const gdb_byte *cu_list, offset_type n_elements,
2941 struct dwarf2_section_info *section,
2942 int is_dwz,
2943 int base_offset)
9291a0cd
TT
2944{
2945 offset_type i;
9291a0cd 2946
2ec9a5e0 2947 for (i = 0; i < n_elements; i += 2)
9291a0cd 2948 {
74a0d9f6 2949 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2950
2951 sect_offset sect_off
2952 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2953 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2954 cu_list += 2 * 8;
2955
9c541725
PA
2956 dwarf2_per_cu_data *the_cu
2957 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2958 struct dwarf2_per_cu_data);
2959 the_cu->sect_off = sect_off;
9291a0cd
TT
2960 the_cu->length = length;
2961 the_cu->objfile = objfile;
8a0459fd 2962 the_cu->section = section;
9291a0cd
TT
2963 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2965 the_cu->is_dwz = is_dwz;
2966 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2967 }
9291a0cd
TT
2968}
2969
2ec9a5e0 2970/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2971 the CU objects for this objfile. */
2ec9a5e0 2972
74a0d9f6 2973static void
2ec9a5e0
TT
2974create_cus_from_index (struct objfile *objfile,
2975 const gdb_byte *cu_list, offset_type cu_list_elements,
2976 const gdb_byte *dwz_list, offset_type dwz_elements)
2977{
2978 struct dwz_file *dwz;
2979
2980 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2981 dwarf2_per_objfile->all_comp_units =
2982 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2983 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2984
74a0d9f6
JK
2985 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2986 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2987
2988 if (dwz_elements == 0)
74a0d9f6 2989 return;
2ec9a5e0
TT
2990
2991 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2992 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2993 cu_list_elements / 2);
2ec9a5e0
TT
2994}
2995
1fd400ff 2996/* Create the signatured type hash table from the index. */
673bfd45 2997
74a0d9f6 2998static void
673bfd45 2999create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3000 struct dwarf2_section_info *section,
673bfd45
DE
3001 const gdb_byte *bytes,
3002 offset_type elements)
1fd400ff
TT
3003{
3004 offset_type i;
673bfd45 3005 htab_t sig_types_hash;
1fd400ff 3006
6aa5f3a6
DE
3007 dwarf2_per_objfile->n_type_units
3008 = dwarf2_per_objfile->n_allocated_type_units
3009 = elements / 3;
8d749320
SM
3010 dwarf2_per_objfile->all_type_units =
3011 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3012
673bfd45 3013 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3014
3015 for (i = 0; i < elements; i += 3)
3016 {
52dc124a 3017 struct signatured_type *sig_type;
9c541725 3018 ULONGEST signature;
1fd400ff 3019 void **slot;
9c541725 3020 cu_offset type_offset_in_tu;
1fd400ff 3021
74a0d9f6 3022 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3023 sect_offset sect_off
3024 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3025 type_offset_in_tu
3026 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3027 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3028 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3029 bytes += 3 * 8;
3030
52dc124a 3031 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3032 struct signatured_type);
52dc124a 3033 sig_type->signature = signature;
9c541725 3034 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3035 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3036 sig_type->per_cu.section = section;
9c541725 3037 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3038 sig_type->per_cu.objfile = objfile;
3039 sig_type->per_cu.v.quick
1fd400ff
TT
3040 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3041 struct dwarf2_per_cu_quick_data);
3042
52dc124a
DE
3043 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3044 *slot = sig_type;
1fd400ff 3045
b4dd5633 3046 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3047 }
3048
673bfd45 3049 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3050}
3051
9291a0cd
TT
3052/* Read the address map data from the mapped index, and use it to
3053 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3054
9291a0cd
TT
3055static void
3056create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3057{
3e29f34a 3058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
3059 const gdb_byte *iter, *end;
3060 struct obstack temp_obstack;
3061 struct addrmap *mutable_map;
3062 struct cleanup *cleanup;
3063 CORE_ADDR baseaddr;
3064
3065 obstack_init (&temp_obstack);
3066 cleanup = make_cleanup_obstack_free (&temp_obstack);
3067 mutable_map = addrmap_create_mutable (&temp_obstack);
3068
3069 iter = index->address_table;
3070 end = iter + index->address_table_size;
3071
3072 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3073
3074 while (iter < end)
3075 {
3076 ULONGEST hi, lo, cu_index;
3077 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3078 iter += 8;
3079 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3080 iter += 8;
3081 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3082 iter += 4;
f652bce2 3083
24a55014 3084 if (lo > hi)
f652bce2 3085 {
24a55014
DE
3086 complaint (&symfile_complaints,
3087 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3088 hex_string (lo), hex_string (hi));
24a55014 3089 continue;
f652bce2 3090 }
24a55014
DE
3091
3092 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3093 {
3094 complaint (&symfile_complaints,
3095 _(".gdb_index address table has invalid CU number %u"),
3096 (unsigned) cu_index);
24a55014 3097 continue;
f652bce2 3098 }
24a55014 3099
3e29f34a
MR
3100 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3101 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3102 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3103 }
3104
3105 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3106 &objfile->objfile_obstack);
3107 do_cleanups (cleanup);
3108}
3109
59d7bcaf
JK
3110/* The hash function for strings in the mapped index. This is the same as
3111 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3112 implementation. This is necessary because the hash function is tied to the
3113 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3114 SYMBOL_HASH_NEXT.
3115
3116 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3117
9291a0cd 3118static hashval_t
559a7a62 3119mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3120{
3121 const unsigned char *str = (const unsigned char *) p;
3122 hashval_t r = 0;
3123 unsigned char c;
3124
3125 while ((c = *str++) != 0)
559a7a62
JK
3126 {
3127 if (index_version >= 5)
3128 c = tolower (c);
3129 r = r * 67 + c - 113;
3130 }
9291a0cd
TT
3131
3132 return r;
3133}
3134
3135/* Find a slot in the mapped index INDEX for the object named NAME.
3136 If NAME is found, set *VEC_OUT to point to the CU vector in the
3137 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3138
9291a0cd
TT
3139static int
3140find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3141 offset_type **vec_out)
3142{
0cf03b49
JK
3143 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3144 offset_type hash;
9291a0cd 3145 offset_type slot, step;
559a7a62 3146 int (*cmp) (const char *, const char *);
9291a0cd 3147
0cf03b49 3148 if (current_language->la_language == language_cplus
45280282
IB
3149 || current_language->la_language == language_fortran
3150 || current_language->la_language == language_d)
0cf03b49
JK
3151 {
3152 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3153 not contain any. */
a8719064 3154
72998fb3 3155 if (strchr (name, '(') != NULL)
0cf03b49 3156 {
72998fb3 3157 char *without_params = cp_remove_params (name);
0cf03b49 3158
72998fb3
DE
3159 if (without_params != NULL)
3160 {
3161 make_cleanup (xfree, without_params);
3162 name = without_params;
3163 }
0cf03b49
JK
3164 }
3165 }
3166
559a7a62 3167 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3168 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3169 simulate our NAME being searched is also lowercased. */
3170 hash = mapped_index_string_hash ((index->version == 4
3171 && case_sensitivity == case_sensitive_off
3172 ? 5 : index->version),
3173 name);
3174
3876f04e
DE
3175 slot = hash & (index->symbol_table_slots - 1);
3176 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3177 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3178
3179 for (;;)
3180 {
3181 /* Convert a slot number to an offset into the table. */
3182 offset_type i = 2 * slot;
3183 const char *str;
3876f04e 3184 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3185 {
3186 do_cleanups (back_to);
3187 return 0;
3188 }
9291a0cd 3189
3876f04e 3190 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3191 if (!cmp (name, str))
9291a0cd
TT
3192 {
3193 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3194 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3195 do_cleanups (back_to);
9291a0cd
TT
3196 return 1;
3197 }
3198
3876f04e 3199 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3200 }
3201}
3202
2ec9a5e0
TT
3203/* A helper function that reads the .gdb_index from SECTION and fills
3204 in MAP. FILENAME is the name of the file containing the section;
3205 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3206 ok to use deprecated sections.
3207
3208 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3209 out parameters that are filled in with information about the CU and
3210 TU lists in the section.
3211
3212 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3213
9291a0cd 3214static int
2ec9a5e0
TT
3215read_index_from_section (struct objfile *objfile,
3216 const char *filename,
3217 int deprecated_ok,
3218 struct dwarf2_section_info *section,
3219 struct mapped_index *map,
3220 const gdb_byte **cu_list,
3221 offset_type *cu_list_elements,
3222 const gdb_byte **types_list,
3223 offset_type *types_list_elements)
9291a0cd 3224{
948f8e3d 3225 const gdb_byte *addr;
2ec9a5e0 3226 offset_type version;
b3b272e1 3227 offset_type *metadata;
1fd400ff 3228 int i;
9291a0cd 3229
2ec9a5e0 3230 if (dwarf2_section_empty_p (section))
9291a0cd 3231 return 0;
82430852
JK
3232
3233 /* Older elfutils strip versions could keep the section in the main
3234 executable while splitting it for the separate debug info file. */
a32a8923 3235 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3236 return 0;
3237
2ec9a5e0 3238 dwarf2_read_section (objfile, section);
9291a0cd 3239
2ec9a5e0 3240 addr = section->buffer;
9291a0cd 3241 /* Version check. */
1fd400ff 3242 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3243 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3244 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3245 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3246 indices. */
831adc1f 3247 if (version < 4)
481860b3
GB
3248 {
3249 static int warning_printed = 0;
3250 if (!warning_printed)
3251 {
3252 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3253 filename);
481860b3
GB
3254 warning_printed = 1;
3255 }
3256 return 0;
3257 }
3258 /* Index version 4 uses a different hash function than index version
3259 5 and later.
3260
3261 Versions earlier than 6 did not emit psymbols for inlined
3262 functions. Using these files will cause GDB not to be able to
3263 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3264 indices unless the user has done
3265 "set use-deprecated-index-sections on". */
2ec9a5e0 3266 if (version < 6 && !deprecated_ok)
481860b3
GB
3267 {
3268 static int warning_printed = 0;
3269 if (!warning_printed)
3270 {
e615022a
DE
3271 warning (_("\
3272Skipping deprecated .gdb_index section in %s.\n\
3273Do \"set use-deprecated-index-sections on\" before the file is read\n\
3274to use the section anyway."),
2ec9a5e0 3275 filename);
481860b3
GB
3276 warning_printed = 1;
3277 }
3278 return 0;
3279 }
796a7ff8 3280 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3281 of the TU (for symbols coming from TUs),
3282 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3283 Plus gold-generated indices can have duplicate entries for global symbols,
3284 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3285 These are just performance bugs, and we can't distinguish gdb-generated
3286 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3287
481860b3 3288 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3289 longer backward compatible. */
796a7ff8 3290 if (version > 8)
594e8718 3291 return 0;
9291a0cd 3292
559a7a62 3293 map->version = version;
2ec9a5e0 3294 map->total_size = section->size;
9291a0cd
TT
3295
3296 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3297
3298 i = 0;
2ec9a5e0
TT
3299 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3300 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3301 / 8);
1fd400ff
TT
3302 ++i;
3303
2ec9a5e0
TT
3304 *types_list = addr + MAYBE_SWAP (metadata[i]);
3305 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3306 - MAYBE_SWAP (metadata[i]))
3307 / 8);
987d643c 3308 ++i;
1fd400ff
TT
3309
3310 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3311 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3312 - MAYBE_SWAP (metadata[i]));
3313 ++i;
3314
3876f04e
DE
3315 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3316 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3317 - MAYBE_SWAP (metadata[i]))
3318 / (2 * sizeof (offset_type)));
1fd400ff 3319 ++i;
9291a0cd 3320
f9d83a0b 3321 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3322
2ec9a5e0
TT
3323 return 1;
3324}
3325
3326
3327/* Read the index file. If everything went ok, initialize the "quick"
3328 elements of all the CUs and return 1. Otherwise, return 0. */
3329
3330static int
3331dwarf2_read_index (struct objfile *objfile)
3332{
3333 struct mapped_index local_map, *map;
3334 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3335 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3336 struct dwz_file *dwz;
2ec9a5e0 3337
4262abfb 3338 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3339 use_deprecated_index_sections,
3340 &dwarf2_per_objfile->gdb_index, &local_map,
3341 &cu_list, &cu_list_elements,
3342 &types_list, &types_list_elements))
3343 return 0;
3344
0fefef59 3345 /* Don't use the index if it's empty. */
2ec9a5e0 3346 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3347 return 0;
3348
2ec9a5e0
TT
3349 /* If there is a .dwz file, read it so we can get its CU list as
3350 well. */
4db1a1dc
TT
3351 dwz = dwarf2_get_dwz_file ();
3352 if (dwz != NULL)
2ec9a5e0 3353 {
2ec9a5e0
TT
3354 struct mapped_index dwz_map;
3355 const gdb_byte *dwz_types_ignore;
3356 offset_type dwz_types_elements_ignore;
3357
3358 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3359 1,
3360 &dwz->gdb_index, &dwz_map,
3361 &dwz_list, &dwz_list_elements,
3362 &dwz_types_ignore,
3363 &dwz_types_elements_ignore))
3364 {
3365 warning (_("could not read '.gdb_index' section from %s; skipping"),
3366 bfd_get_filename (dwz->dwz_bfd));
3367 return 0;
3368 }
3369 }
3370
74a0d9f6
JK
3371 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3372 dwz_list_elements);
1fd400ff 3373
8b70b953
TT
3374 if (types_list_elements)
3375 {
3376 struct dwarf2_section_info *section;
3377
3378 /* We can only handle a single .debug_types when we have an
3379 index. */
3380 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3381 return 0;
3382
3383 section = VEC_index (dwarf2_section_info_def,
3384 dwarf2_per_objfile->types, 0);
3385
74a0d9f6
JK
3386 create_signatured_type_table_from_index (objfile, section, types_list,
3387 types_list_elements);
8b70b953 3388 }
9291a0cd 3389
2ec9a5e0
TT
3390 create_addrmap_from_index (objfile, &local_map);
3391
8d749320 3392 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3393 *map = local_map;
9291a0cd
TT
3394
3395 dwarf2_per_objfile->index_table = map;
3396 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3397 dwarf2_per_objfile->quick_file_names_table =
3398 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3399
3400 return 1;
3401}
3402
3403/* A helper for the "quick" functions which sets the global
3404 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3405
9291a0cd
TT
3406static void
3407dw2_setup (struct objfile *objfile)
3408{
9a3c8263
SM
3409 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3410 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3411 gdb_assert (dwarf2_per_objfile);
3412}
3413
dee91e82 3414/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3415
dee91e82
DE
3416static void
3417dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3418 const gdb_byte *info_ptr,
dee91e82
DE
3419 struct die_info *comp_unit_die,
3420 int has_children,
3421 void *data)
9291a0cd 3422{
dee91e82
DE
3423 struct dwarf2_cu *cu = reader->cu;
3424 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3425 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3426 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3427 struct attribute *attr;
dee91e82 3428 int i;
7b9f3c50
DE
3429 void **slot;
3430 struct quick_file_names *qfn;
9291a0cd 3431
0186c6a7
DE
3432 gdb_assert (! this_cu->is_debug_types);
3433
07261596
TT
3434 /* Our callers never want to match partial units -- instead they
3435 will match the enclosing full CU. */
3436 if (comp_unit_die->tag == DW_TAG_partial_unit)
3437 {
3438 this_cu->v.quick->no_file_data = 1;
3439 return;
3440 }
3441
0186c6a7 3442 lh_cu = this_cu;
7b9f3c50 3443 slot = NULL;
dee91e82 3444
fff8551c 3445 line_header_up lh;
9c541725 3446 sect_offset line_offset {};
fff8551c 3447
dee91e82 3448 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3449 if (attr)
3450 {
7b9f3c50
DE
3451 struct quick_file_names find_entry;
3452
9c541725 3453 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3454
3455 /* We may have already read in this line header (TU line header sharing).
3456 If we have we're done. */
094b34ac 3457 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3458 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3459 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3460 &find_entry, INSERT);
3461 if (*slot != NULL)
3462 {
9a3c8263 3463 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3464 return;
7b9f3c50
DE
3465 }
3466
3019eac3 3467 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3468 }
3469 if (lh == NULL)
3470 {
094b34ac 3471 lh_cu->v.quick->no_file_data = 1;
dee91e82 3472 return;
9291a0cd
TT
3473 }
3474
8d749320 3475 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3476 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3477 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3478 gdb_assert (slot != NULL);
3479 *slot = qfn;
9291a0cd 3480
d721ba37 3481 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3482
fff8551c 3483 qfn->num_file_names = lh->file_names.size ();
8d749320 3484 qfn->file_names =
fff8551c
PA
3485 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3486 for (i = 0; i < lh->file_names.size (); ++i)
3487 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3488 qfn->real_names = NULL;
9291a0cd 3489
094b34ac 3490 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3491}
3492
3493/* A helper for the "quick" functions which attempts to read the line
3494 table for THIS_CU. */
3495
3496static struct quick_file_names *
e4a48d9d 3497dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3498{
0186c6a7
DE
3499 /* This should never be called for TUs. */
3500 gdb_assert (! this_cu->is_debug_types);
3501 /* Nor type unit groups. */
3502 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3503
dee91e82
DE
3504 if (this_cu->v.quick->file_names != NULL)
3505 return this_cu->v.quick->file_names;
3506 /* If we know there is no line data, no point in looking again. */
3507 if (this_cu->v.quick->no_file_data)
3508 return NULL;
3509
0186c6a7 3510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3511
3512 if (this_cu->v.quick->no_file_data)
3513 return NULL;
3514 return this_cu->v.quick->file_names;
9291a0cd
TT
3515}
3516
3517/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3518 real path for a given file name from the line table. */
2fdf6df6 3519
9291a0cd 3520static const char *
7b9f3c50
DE
3521dw2_get_real_path (struct objfile *objfile,
3522 struct quick_file_names *qfn, int index)
9291a0cd 3523{
7b9f3c50
DE
3524 if (qfn->real_names == NULL)
3525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3526 qfn->num_file_names, const char *);
9291a0cd 3527
7b9f3c50
DE
3528 if (qfn->real_names[index] == NULL)
3529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3530
7b9f3c50 3531 return qfn->real_names[index];
9291a0cd
TT
3532}
3533
3534static struct symtab *
3535dw2_find_last_source_symtab (struct objfile *objfile)
3536{
43f3e411 3537 struct compunit_symtab *cust;
9291a0cd 3538 int index;
ae2de4f8 3539
9291a0cd
TT
3540 dw2_setup (objfile);
3541 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3542 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3543 if (cust == NULL)
3544 return NULL;
3545 return compunit_primary_filetab (cust);
9291a0cd
TT
3546}
3547
7b9f3c50
DE
3548/* Traversal function for dw2_forget_cached_source_info. */
3549
3550static int
3551dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3552{
7b9f3c50 3553 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3554
7b9f3c50 3555 if (file_data->real_names)
9291a0cd 3556 {
7b9f3c50 3557 int i;
9291a0cd 3558
7b9f3c50 3559 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3560 {
7b9f3c50
DE
3561 xfree ((void*) file_data->real_names[i]);
3562 file_data->real_names[i] = NULL;
9291a0cd
TT
3563 }
3564 }
7b9f3c50
DE
3565
3566 return 1;
3567}
3568
3569static void
3570dw2_forget_cached_source_info (struct objfile *objfile)
3571{
3572 dw2_setup (objfile);
3573
3574 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3575 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3576}
3577
f8eba3c6
TT
3578/* Helper function for dw2_map_symtabs_matching_filename that expands
3579 the symtabs and calls the iterator. */
3580
3581static int
3582dw2_map_expand_apply (struct objfile *objfile,
3583 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3584 const char *name, const char *real_path,
14bc53a8 3585 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3586{
43f3e411 3587 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3588
3589 /* Don't visit already-expanded CUs. */
43f3e411 3590 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3591 return 0;
3592
3593 /* This may expand more than one symtab, and we want to iterate over
3594 all of them. */
a0f42c21 3595 dw2_instantiate_symtab (per_cu);
f8eba3c6 3596
14bc53a8
PA
3597 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3598 last_made, callback);
f8eba3c6
TT
3599}
3600
3601/* Implementation of the map_symtabs_matching_filename method. */
3602
14bc53a8
PA
3603static bool
3604dw2_map_symtabs_matching_filename
3605 (struct objfile *objfile, const char *name, const char *real_path,
3606 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3607{
3608 int i;
c011a4f4 3609 const char *name_basename = lbasename (name);
9291a0cd
TT
3610
3611 dw2_setup (objfile);
ae2de4f8 3612
848e3e78
DE
3613 /* The rule is CUs specify all the files, including those used by
3614 any TU, so there's no need to scan TUs here. */
f4dc4d17 3615
848e3e78 3616 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3617 {
3618 int j;
8832e7e3 3619 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3620 struct quick_file_names *file_data;
9291a0cd 3621
3d7bb9d9 3622 /* We only need to look at symtabs not already expanded. */
43f3e411 3623 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3624 continue;
3625
e4a48d9d 3626 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3627 if (file_data == NULL)
9291a0cd
TT
3628 continue;
3629
7b9f3c50 3630 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3631 {
7b9f3c50 3632 const char *this_name = file_data->file_names[j];
da235a7c 3633 const char *this_real_name;
9291a0cd 3634
af529f8f 3635 if (compare_filenames_for_search (this_name, name))
9291a0cd 3636 {
f5b95b50 3637 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3638 callback))
3639 return true;
288e77a7 3640 continue;
4aac40c8 3641 }
9291a0cd 3642
c011a4f4
DE
3643 /* Before we invoke realpath, which can get expensive when many
3644 files are involved, do a quick comparison of the basenames. */
3645 if (! basenames_may_differ
3646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3647 continue;
3648
da235a7c
JK
3649 this_real_name = dw2_get_real_path (objfile, file_data, j);
3650 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3651 {
da235a7c 3652 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3653 callback))
3654 return true;
288e77a7 3655 continue;
da235a7c 3656 }
9291a0cd 3657
da235a7c
JK
3658 if (real_path != NULL)
3659 {
af529f8f
JK
3660 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3661 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3662 if (this_real_name != NULL
af529f8f 3663 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3664 {
f5b95b50 3665 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3666 callback))
3667 return true;
288e77a7 3668 continue;
9291a0cd
TT
3669 }
3670 }
3671 }
3672 }
3673
14bc53a8 3674 return false;
9291a0cd
TT
3675}
3676
da51c347
DE
3677/* Struct used to manage iterating over all CUs looking for a symbol. */
3678
3679struct dw2_symtab_iterator
9291a0cd 3680{
da51c347
DE
3681 /* The internalized form of .gdb_index. */
3682 struct mapped_index *index;
3683 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3684 int want_specific_block;
3685 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3686 Unused if !WANT_SPECIFIC_BLOCK. */
3687 int block_index;
3688 /* The kind of symbol we're looking for. */
3689 domain_enum domain;
3690 /* The list of CUs from the index entry of the symbol,
3691 or NULL if not found. */
3692 offset_type *vec;
3693 /* The next element in VEC to look at. */
3694 int next;
3695 /* The number of elements in VEC, or zero if there is no match. */
3696 int length;
8943b874
DE
3697 /* Have we seen a global version of the symbol?
3698 If so we can ignore all further global instances.
3699 This is to work around gold/15646, inefficient gold-generated
3700 indices. */
3701 int global_seen;
da51c347 3702};
9291a0cd 3703
da51c347
DE
3704/* Initialize the index symtab iterator ITER.
3705 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3706 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3707
9291a0cd 3708static void
da51c347
DE
3709dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3710 struct mapped_index *index,
3711 int want_specific_block,
3712 int block_index,
3713 domain_enum domain,
3714 const char *name)
3715{
3716 iter->index = index;
3717 iter->want_specific_block = want_specific_block;
3718 iter->block_index = block_index;
3719 iter->domain = domain;
3720 iter->next = 0;
8943b874 3721 iter->global_seen = 0;
da51c347
DE
3722
3723 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3724 iter->length = MAYBE_SWAP (*iter->vec);
3725 else
3726 {
3727 iter->vec = NULL;
3728 iter->length = 0;
3729 }
3730}
3731
3732/* Return the next matching CU or NULL if there are no more. */
3733
3734static struct dwarf2_per_cu_data *
3735dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3736{
3737 for ( ; iter->next < iter->length; ++iter->next)
3738 {
3739 offset_type cu_index_and_attrs =
3740 MAYBE_SWAP (iter->vec[iter->next + 1]);
3741 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3742 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3743 int want_static = iter->block_index != GLOBAL_BLOCK;
3744 /* This value is only valid for index versions >= 7. */
3745 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3746 gdb_index_symbol_kind symbol_kind =
3747 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3748 /* Only check the symbol attributes if they're present.
3749 Indices prior to version 7 don't record them,
3750 and indices >= 7 may elide them for certain symbols
3751 (gold does this). */
3752 int attrs_valid =
3753 (iter->index->version >= 7
3754 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3755
3190f0c6
DE
3756 /* Don't crash on bad data. */
3757 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3758 + dwarf2_per_objfile->n_type_units))
3759 {
3760 complaint (&symfile_complaints,
3761 _(".gdb_index entry has bad CU index"
4262abfb
JK
3762 " [in module %s]"),
3763 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3764 continue;
3765 }
3766
8832e7e3 3767 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3768
da51c347 3769 /* Skip if already read in. */
43f3e411 3770 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3771 continue;
3772
8943b874
DE
3773 /* Check static vs global. */
3774 if (attrs_valid)
3775 {
3776 if (iter->want_specific_block
3777 && want_static != is_static)
3778 continue;
3779 /* Work around gold/15646. */
3780 if (!is_static && iter->global_seen)
3781 continue;
3782 if (!is_static)
3783 iter->global_seen = 1;
3784 }
da51c347
DE
3785
3786 /* Only check the symbol's kind if it has one. */
3787 if (attrs_valid)
3788 {
3789 switch (iter->domain)
3790 {
3791 case VAR_DOMAIN:
3792 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3793 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3794 /* Some types are also in VAR_DOMAIN. */
3795 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3796 continue;
3797 break;
3798 case STRUCT_DOMAIN:
3799 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3800 continue;
3801 break;
3802 case LABEL_DOMAIN:
3803 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3804 continue;
3805 break;
3806 default:
3807 break;
3808 }
3809 }
3810
3811 ++iter->next;
3812 return per_cu;
3813 }
3814
3815 return NULL;
3816}
3817
43f3e411 3818static struct compunit_symtab *
da51c347
DE
3819dw2_lookup_symbol (struct objfile *objfile, int block_index,
3820 const char *name, domain_enum domain)
9291a0cd 3821{
43f3e411 3822 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3823 struct mapped_index *index;
3824
9291a0cd
TT
3825 dw2_setup (objfile);
3826
156942c7
DE
3827 index = dwarf2_per_objfile->index_table;
3828
da51c347 3829 /* index is NULL if OBJF_READNOW. */
156942c7 3830 if (index)
9291a0cd 3831 {
da51c347
DE
3832 struct dw2_symtab_iterator iter;
3833 struct dwarf2_per_cu_data *per_cu;
3834
3835 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3836
da51c347 3837 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3838 {
b2e2f908 3839 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3840 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3841 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3842 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3843
b2e2f908
DE
3844 sym = block_find_symbol (block, name, domain,
3845 block_find_non_opaque_type_preferred,
3846 &with_opaque);
3847
da51c347
DE
3848 /* Some caution must be observed with overloaded functions
3849 and methods, since the index will not contain any overload
3850 information (but NAME might contain it). */
da51c347 3851
b2e2f908
DE
3852 if (sym != NULL
3853 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3854 return stab;
3855 if (with_opaque != NULL
3856 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3857 stab_best = stab;
da51c347
DE
3858
3859 /* Keep looking through other CUs. */
9291a0cd
TT
3860 }
3861 }
9291a0cd 3862
da51c347 3863 return stab_best;
9291a0cd
TT
3864}
3865
3866static void
3867dw2_print_stats (struct objfile *objfile)
3868{
e4a48d9d 3869 int i, total, count;
9291a0cd
TT
3870
3871 dw2_setup (objfile);
e4a48d9d 3872 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3873 count = 0;
e4a48d9d 3874 for (i = 0; i < total; ++i)
9291a0cd 3875 {
8832e7e3 3876 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3877
43f3e411 3878 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3879 ++count;
3880 }
e4a48d9d 3881 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3882 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3883}
3884
779bd270
DE
3885/* This dumps minimal information about the index.
3886 It is called via "mt print objfiles".
3887 One use is to verify .gdb_index has been loaded by the
3888 gdb.dwarf2/gdb-index.exp testcase. */
3889
9291a0cd
TT
3890static void
3891dw2_dump (struct objfile *objfile)
3892{
779bd270
DE
3893 dw2_setup (objfile);
3894 gdb_assert (dwarf2_per_objfile->using_index);
3895 printf_filtered (".gdb_index:");
3896 if (dwarf2_per_objfile->index_table != NULL)
3897 {
3898 printf_filtered (" version %d\n",
3899 dwarf2_per_objfile->index_table->version);
3900 }
3901 else
3902 printf_filtered (" faked for \"readnow\"\n");
3903 printf_filtered ("\n");
9291a0cd
TT
3904}
3905
3906static void
3189cb12
DE
3907dw2_relocate (struct objfile *objfile,
3908 const struct section_offsets *new_offsets,
3909 const struct section_offsets *delta)
9291a0cd
TT
3910{
3911 /* There's nothing to relocate here. */
3912}
3913
3914static void
3915dw2_expand_symtabs_for_function (struct objfile *objfile,
3916 const char *func_name)
3917{
da51c347
DE
3918 struct mapped_index *index;
3919
3920 dw2_setup (objfile);
3921
3922 index = dwarf2_per_objfile->index_table;
3923
3924 /* index is NULL if OBJF_READNOW. */
3925 if (index)
3926 {
3927 struct dw2_symtab_iterator iter;
3928 struct dwarf2_per_cu_data *per_cu;
3929
3930 /* Note: It doesn't matter what we pass for block_index here. */
3931 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3932 func_name);
3933
3934 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3935 dw2_instantiate_symtab (per_cu);
3936 }
9291a0cd
TT
3937}
3938
3939static void
3940dw2_expand_all_symtabs (struct objfile *objfile)
3941{
3942 int i;
3943
3944 dw2_setup (objfile);
1fd400ff
TT
3945
3946 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3947 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3948 {
8832e7e3 3949 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3950
a0f42c21 3951 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3952 }
3953}
3954
3955static void
652a8996
JK
3956dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3957 const char *fullname)
9291a0cd
TT
3958{
3959 int i;
3960
3961 dw2_setup (objfile);
d4637a04
DE
3962
3963 /* We don't need to consider type units here.
3964 This is only called for examining code, e.g. expand_line_sal.
3965 There can be an order of magnitude (or more) more type units
3966 than comp units, and we avoid them if we can. */
3967
3968 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3969 {
3970 int j;
8832e7e3 3971 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3972 struct quick_file_names *file_data;
9291a0cd 3973
3d7bb9d9 3974 /* We only need to look at symtabs not already expanded. */
43f3e411 3975 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3976 continue;
3977
e4a48d9d 3978 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3979 if (file_data == NULL)
9291a0cd
TT
3980 continue;
3981
7b9f3c50 3982 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3983 {
652a8996
JK
3984 const char *this_fullname = file_data->file_names[j];
3985
3986 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3987 {
a0f42c21 3988 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3989 break;
3990 }
3991 }
3992 }
3993}
3994
9291a0cd 3995static void
ade7ed9e 3996dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3997 const char * name, domain_enum domain,
ade7ed9e 3998 int global,
40658b94
PH
3999 int (*callback) (struct block *,
4000 struct symbol *, void *),
2edb89d3
JK
4001 void *data, symbol_compare_ftype *match,
4002 symbol_compare_ftype *ordered_compare)
9291a0cd 4003{
40658b94 4004 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4005 current language is Ada for a non-Ada objfile using GNU index. As Ada
4006 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4007}
4008
4009static void
f8eba3c6
TT
4010dw2_expand_symtabs_matching
4011 (struct objfile *objfile,
14bc53a8
PA
4012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4013 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4014 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4015 enum search_domain kind)
9291a0cd
TT
4016{
4017 int i;
4018 offset_type iter;
4b5246aa 4019 struct mapped_index *index;
9291a0cd
TT
4020
4021 dw2_setup (objfile);
ae2de4f8
DE
4022
4023 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4024 if (!dwarf2_per_objfile->index_table)
4025 return;
4b5246aa 4026 index = dwarf2_per_objfile->index_table;
9291a0cd 4027
7b08b9eb 4028 if (file_matcher != NULL)
24c79950 4029 {
fc4007c9
TT
4030 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4031 htab_eq_pointer,
4032 NULL, xcalloc, xfree));
4033 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4034 htab_eq_pointer,
4035 NULL, xcalloc, xfree));
24c79950 4036
848e3e78
DE
4037 /* The rule is CUs specify all the files, including those used by
4038 any TU, so there's no need to scan TUs here. */
4039
4040 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4041 {
4042 int j;
8832e7e3 4043 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4044 struct quick_file_names *file_data;
4045 void **slot;
7b08b9eb 4046
61d96d7e
DE
4047 QUIT;
4048
24c79950 4049 per_cu->v.quick->mark = 0;
3d7bb9d9 4050
24c79950 4051 /* We only need to look at symtabs not already expanded. */
43f3e411 4052 if (per_cu->v.quick->compunit_symtab)
24c79950 4053 continue;
7b08b9eb 4054
e4a48d9d 4055 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4056 if (file_data == NULL)
4057 continue;
7b08b9eb 4058
fc4007c9 4059 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4060 continue;
fc4007c9 4061 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4062 {
4063 per_cu->v.quick->mark = 1;
4064 continue;
4065 }
4066
4067 for (j = 0; j < file_data->num_file_names; ++j)
4068 {
da235a7c
JK
4069 const char *this_real_name;
4070
14bc53a8 4071 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4072 {
4073 per_cu->v.quick->mark = 1;
4074 break;
4075 }
da235a7c
JK
4076
4077 /* Before we invoke realpath, which can get expensive when many
4078 files are involved, do a quick comparison of the basenames. */
4079 if (!basenames_may_differ
4080 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4081 true))
da235a7c
JK
4082 continue;
4083
4084 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4085 if (file_matcher (this_real_name, false))
da235a7c
JK
4086 {
4087 per_cu->v.quick->mark = 1;
4088 break;
4089 }
24c79950
TT
4090 }
4091
4092 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4093 ? visited_found.get ()
4094 : visited_not_found.get (),
24c79950
TT
4095 file_data, INSERT);
4096 *slot = file_data;
4097 }
24c79950 4098 }
9291a0cd 4099
3876f04e 4100 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4101 {
4102 offset_type idx = 2 * iter;
4103 const char *name;
4104 offset_type *vec, vec_len, vec_idx;
8943b874 4105 int global_seen = 0;
9291a0cd 4106
61d96d7e
DE
4107 QUIT;
4108
3876f04e 4109 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4110 continue;
4111
3876f04e 4112 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4113
14bc53a8 4114 if (!symbol_matcher (name))
9291a0cd
TT
4115 continue;
4116
4117 /* The name was matched, now expand corresponding CUs that were
4118 marked. */
4b5246aa 4119 vec = (offset_type *) (index->constant_pool
3876f04e 4120 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4121 vec_len = MAYBE_SWAP (vec[0]);
4122 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4123 {
e254ef6a 4124 struct dwarf2_per_cu_data *per_cu;
156942c7 4125 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4126 /* This value is only valid for index versions >= 7. */
4127 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4128 gdb_index_symbol_kind symbol_kind =
4129 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4130 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4131 /* Only check the symbol attributes if they're present.
4132 Indices prior to version 7 don't record them,
4133 and indices >= 7 may elide them for certain symbols
4134 (gold does this). */
4135 int attrs_valid =
4136 (index->version >= 7
4137 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4138
8943b874
DE
4139 /* Work around gold/15646. */
4140 if (attrs_valid)
4141 {
4142 if (!is_static && global_seen)
4143 continue;
4144 if (!is_static)
4145 global_seen = 1;
4146 }
4147
3190f0c6
DE
4148 /* Only check the symbol's kind if it has one. */
4149 if (attrs_valid)
156942c7
DE
4150 {
4151 switch (kind)
4152 {
4153 case VARIABLES_DOMAIN:
4154 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4155 continue;
4156 break;
4157 case FUNCTIONS_DOMAIN:
4158 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4159 continue;
4160 break;
4161 case TYPES_DOMAIN:
4162 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4163 continue;
4164 break;
4165 default:
4166 break;
4167 }
4168 }
4169
3190f0c6
DE
4170 /* Don't crash on bad data. */
4171 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4172 + dwarf2_per_objfile->n_type_units))
4173 {
4174 complaint (&symfile_complaints,
4175 _(".gdb_index entry has bad CU index"
4262abfb 4176 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4177 continue;
4178 }
4179
8832e7e3 4180 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4181 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4182 {
4183 int symtab_was_null =
4184 (per_cu->v.quick->compunit_symtab == NULL);
4185
4186 dw2_instantiate_symtab (per_cu);
4187
4188 if (expansion_notify != NULL
4189 && symtab_was_null
4190 && per_cu->v.quick->compunit_symtab != NULL)
4191 {
14bc53a8 4192 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4193 }
4194 }
9291a0cd
TT
4195 }
4196 }
4197}
4198
43f3e411 4199/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4200 symtab. */
4201
43f3e411
DE
4202static struct compunit_symtab *
4203recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4204 CORE_ADDR pc)
9703b513
TT
4205{
4206 int i;
4207
43f3e411
DE
4208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4210 return cust;
9703b513 4211
43f3e411 4212 if (cust->includes == NULL)
a3ec0bb1
DE
4213 return NULL;
4214
43f3e411 4215 for (i = 0; cust->includes[i]; ++i)
9703b513 4216 {
43f3e411 4217 struct compunit_symtab *s = cust->includes[i];
9703b513 4218
43f3e411 4219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4220 if (s != NULL)
4221 return s;
4222 }
4223
4224 return NULL;
4225}
4226
43f3e411
DE
4227static struct compunit_symtab *
4228dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4229 struct bound_minimal_symbol msymbol,
4230 CORE_ADDR pc,
4231 struct obj_section *section,
4232 int warn_if_readin)
9291a0cd
TT
4233{
4234 struct dwarf2_per_cu_data *data;
43f3e411 4235 struct compunit_symtab *result;
9291a0cd
TT
4236
4237 dw2_setup (objfile);
4238
4239 if (!objfile->psymtabs_addrmap)
4240 return NULL;
4241
9a3c8263
SM
4242 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4243 pc);
9291a0cd
TT
4244 if (!data)
4245 return NULL;
4246
43f3e411 4247 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4248 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4249 paddress (get_objfile_arch (objfile), pc));
4250
43f3e411
DE
4251 result
4252 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4253 pc);
9703b513
TT
4254 gdb_assert (result != NULL);
4255 return result;
9291a0cd
TT
4256}
4257
9291a0cd 4258static void
44b13c5a 4259dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4260 void *data, int need_fullname)
9291a0cd
TT
4261{
4262 int i;
fc4007c9
TT
4263 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4264 NULL, xcalloc, xfree));
9291a0cd
TT
4265
4266 dw2_setup (objfile);
ae2de4f8 4267
848e3e78
DE
4268 /* The rule is CUs specify all the files, including those used by
4269 any TU, so there's no need to scan TUs here.
4270 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4271
848e3e78 4272 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4273 {
8832e7e3 4274 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4275
43f3e411 4276 if (per_cu->v.quick->compunit_symtab)
24c79950 4277 {
fc4007c9
TT
4278 void **slot = htab_find_slot (visited.get (),
4279 per_cu->v.quick->file_names,
24c79950
TT
4280 INSERT);
4281
4282 *slot = per_cu->v.quick->file_names;
4283 }
4284 }
4285
848e3e78 4286 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4287 {
4288 int j;
8832e7e3 4289 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4290 struct quick_file_names *file_data;
24c79950 4291 void **slot;
9291a0cd 4292
3d7bb9d9 4293 /* We only need to look at symtabs not already expanded. */
43f3e411 4294 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4295 continue;
4296
e4a48d9d 4297 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4298 if (file_data == NULL)
9291a0cd
TT
4299 continue;
4300
fc4007c9 4301 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4302 if (*slot)
4303 {
4304 /* Already visited. */
4305 continue;
4306 }
4307 *slot = file_data;
4308
7b9f3c50 4309 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4310 {
74e2f255
DE
4311 const char *this_real_name;
4312
4313 if (need_fullname)
4314 this_real_name = dw2_get_real_path (objfile, file_data, j);
4315 else
4316 this_real_name = NULL;
7b9f3c50 4317 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4318 }
4319 }
4320}
4321
4322static int
4323dw2_has_symbols (struct objfile *objfile)
4324{
4325 return 1;
4326}
4327
4328const struct quick_symbol_functions dwarf2_gdb_index_functions =
4329{
4330 dw2_has_symbols,
4331 dw2_find_last_source_symtab,
4332 dw2_forget_cached_source_info,
f8eba3c6 4333 dw2_map_symtabs_matching_filename,
9291a0cd 4334 dw2_lookup_symbol,
9291a0cd
TT
4335 dw2_print_stats,
4336 dw2_dump,
4337 dw2_relocate,
4338 dw2_expand_symtabs_for_function,
4339 dw2_expand_all_symtabs,
652a8996 4340 dw2_expand_symtabs_with_fullname,
40658b94 4341 dw2_map_matching_symbols,
9291a0cd 4342 dw2_expand_symtabs_matching,
43f3e411 4343 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4344 dw2_map_symbol_filenames
4345};
4346
4347/* Initialize for reading DWARF for this objfile. Return 0 if this
4348 file will use psymtabs, or 1 if using the GNU index. */
4349
4350int
4351dwarf2_initialize_objfile (struct objfile *objfile)
4352{
4353 /* If we're about to read full symbols, don't bother with the
4354 indices. In this case we also don't care if some other debug
4355 format is making psymtabs, because they are all about to be
4356 expanded anyway. */
4357 if ((objfile->flags & OBJF_READNOW))
4358 {
4359 int i;
4360
4361 dwarf2_per_objfile->using_index = 1;
4362 create_all_comp_units (objfile);
0e50663e 4363 create_all_type_units (objfile);
7b9f3c50
DE
4364 dwarf2_per_objfile->quick_file_names_table =
4365 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4366
1fd400ff 4367 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4368 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4369 {
8832e7e3 4370 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4371
e254ef6a
DE
4372 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4373 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4374 }
4375
4376 /* Return 1 so that gdb sees the "quick" functions. However,
4377 these functions will be no-ops because we will have expanded
4378 all symtabs. */
4379 return 1;
4380 }
4381
4382 if (dwarf2_read_index (objfile))
4383 return 1;
4384
9291a0cd
TT
4385 return 0;
4386}
4387
4388\f
4389
dce234bc
PP
4390/* Build a partial symbol table. */
4391
4392void
f29dff0a 4393dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4394{
c9bf0622 4395
f29dff0a 4396 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4397 {
4398 init_psymbol_list (objfile, 1024);
4399 }
4400
492d29ea 4401 TRY
c9bf0622
TT
4402 {
4403 /* This isn't really ideal: all the data we allocate on the
4404 objfile's obstack is still uselessly kept around. However,
4405 freeing it seems unsafe. */
906768f9 4406 psymtab_discarder psymtabs (objfile);
c9bf0622 4407 dwarf2_build_psymtabs_hard (objfile);
906768f9 4408 psymtabs.keep ();
c9bf0622 4409 }
492d29ea
PA
4410 CATCH (except, RETURN_MASK_ERROR)
4411 {
4412 exception_print (gdb_stderr, except);
4413 }
4414 END_CATCH
c906108c 4415}
c906108c 4416
1ce1cefd
DE
4417/* Return the total length of the CU described by HEADER. */
4418
4419static unsigned int
4420get_cu_length (const struct comp_unit_head *header)
4421{
4422 return header->initial_length_size + header->length;
4423}
4424
9c541725 4425/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4426
9c541725
PA
4427static inline bool
4428offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4429{
9c541725
PA
4430 sect_offset bottom = cu_header->sect_off;
4431 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4432
9c541725 4433 return sect_off >= bottom && sect_off < top;
45452591
DE
4434}
4435
3b80fe9b
DE
4436/* Find the base address of the compilation unit for range lists and
4437 location lists. It will normally be specified by DW_AT_low_pc.
4438 In DWARF-3 draft 4, the base address could be overridden by
4439 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4440 compilation units with discontinuous ranges. */
4441
4442static void
4443dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4444{
4445 struct attribute *attr;
4446
4447 cu->base_known = 0;
4448 cu->base_address = 0;
4449
4450 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4451 if (attr)
4452 {
31aa7e4e 4453 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4454 cu->base_known = 1;
4455 }
4456 else
4457 {
4458 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4459 if (attr)
4460 {
31aa7e4e 4461 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4462 cu->base_known = 1;
4463 }
4464 }
4465}
4466
93311388 4467/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4468 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4469 NOTE: This leaves members offset, first_die_offset to be filled in
4470 by the caller. */
107d2387 4471
d521ce57 4472static const gdb_byte *
107d2387 4473read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4474 const gdb_byte *info_ptr,
4475 struct dwarf2_section_info *section,
4476 rcuh_kind section_kind)
107d2387
AC
4477{
4478 int signed_addr;
891d2f0b 4479 unsigned int bytes_read;
43988095
JK
4480 const char *filename = get_section_file_name (section);
4481 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4482
4483 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4484 cu_header->initial_length_size = bytes_read;
4485 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4486 info_ptr += bytes_read;
107d2387
AC
4487 cu_header->version = read_2_bytes (abfd, info_ptr);
4488 info_ptr += 2;
43988095
JK
4489 if (cu_header->version < 5)
4490 switch (section_kind)
4491 {
4492 case rcuh_kind::COMPILE:
4493 cu_header->unit_type = DW_UT_compile;
4494 break;
4495 case rcuh_kind::TYPE:
4496 cu_header->unit_type = DW_UT_type;
4497 break;
4498 default:
4499 internal_error (__FILE__, __LINE__,
4500 _("read_comp_unit_head: invalid section_kind"));
4501 }
4502 else
4503 {
4504 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4505 (read_1_byte (abfd, info_ptr));
4506 info_ptr += 1;
4507 switch (cu_header->unit_type)
4508 {
4509 case DW_UT_compile:
4510 if (section_kind != rcuh_kind::COMPILE)
4511 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4512 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4513 filename);
4514 break;
4515 case DW_UT_type:
4516 section_kind = rcuh_kind::TYPE;
4517 break;
4518 default:
4519 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4520 "(is %d, should be %d or %d) [in module %s]"),
4521 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4522 }
4523
4524 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4525 info_ptr += 1;
4526 }
9c541725
PA
4527 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4528 cu_header,
4529 &bytes_read);
613e1657 4530 info_ptr += bytes_read;
43988095
JK
4531 if (cu_header->version < 5)
4532 {
4533 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4534 info_ptr += 1;
4535 }
107d2387
AC
4536 signed_addr = bfd_get_sign_extend_vma (abfd);
4537 if (signed_addr < 0)
8e65ff28 4538 internal_error (__FILE__, __LINE__,
e2e0b3e5 4539 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4540 cu_header->signed_addr_p = signed_addr;
c764a876 4541
43988095
JK
4542 if (section_kind == rcuh_kind::TYPE)
4543 {
4544 LONGEST type_offset;
4545
4546 cu_header->signature = read_8_bytes (abfd, info_ptr);
4547 info_ptr += 8;
4548
4549 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4550 info_ptr += bytes_read;
9c541725
PA
4551 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4552 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4553 error (_("Dwarf Error: Too big type_offset in compilation unit "
4554 "header (is %s) [in module %s]"), plongest (type_offset),
4555 filename);
4556 }
4557
107d2387
AC
4558 return info_ptr;
4559}
4560
36586728
TT
4561/* Helper function that returns the proper abbrev section for
4562 THIS_CU. */
4563
4564static struct dwarf2_section_info *
4565get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4566{
4567 struct dwarf2_section_info *abbrev;
4568
4569 if (this_cu->is_dwz)
4570 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4571 else
4572 abbrev = &dwarf2_per_objfile->abbrev;
4573
4574 return abbrev;
4575}
4576
9ff913ba
DE
4577/* Subroutine of read_and_check_comp_unit_head and
4578 read_and_check_type_unit_head to simplify them.
4579 Perform various error checking on the header. */
4580
4581static void
4582error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4583 struct dwarf2_section_info *section,
4584 struct dwarf2_section_info *abbrev_section)
9ff913ba 4585{
a32a8923 4586 const char *filename = get_section_file_name (section);
9ff913ba 4587
43988095 4588 if (header->version < 2 || header->version > 5)
9ff913ba 4589 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4590 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4591 filename);
4592
9c541725 4593 if (to_underlying (header->abbrev_sect_off)
36586728 4594 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4595 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4596 "(offset 0x%x + 6) [in module %s]"),
4597 to_underlying (header->abbrev_sect_off),
4598 to_underlying (header->sect_off),
9ff913ba
DE
4599 filename);
4600
9c541725 4601 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4602 avoid potential 32-bit overflow. */
9c541725 4603 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4604 > section->size)
9c541725
PA
4605 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4606 "(offset 0x%x + 0) [in module %s]"),
4607 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4608 filename);
4609}
4610
4611/* Read in a CU/TU header and perform some basic error checking.
4612 The contents of the header are stored in HEADER.
4613 The result is a pointer to the start of the first DIE. */
adabb602 4614
d521ce57 4615static const gdb_byte *
9ff913ba
DE
4616read_and_check_comp_unit_head (struct comp_unit_head *header,
4617 struct dwarf2_section_info *section,
4bdcc0c1 4618 struct dwarf2_section_info *abbrev_section,
d521ce57 4619 const gdb_byte *info_ptr,
43988095 4620 rcuh_kind section_kind)
72bf9492 4621{
d521ce57 4622 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4623 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4624
9c541725 4625 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4626
43988095 4627 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4628
9c541725 4629 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4630
4bdcc0c1 4631 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4632
4633 return info_ptr;
348e048f
DE
4634}
4635
f4dc4d17
DE
4636/* Fetch the abbreviation table offset from a comp or type unit header. */
4637
4638static sect_offset
4639read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4640 sect_offset sect_off)
f4dc4d17 4641{
a32a8923 4642 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4643 const gdb_byte *info_ptr;
ac298888 4644 unsigned int initial_length_size, offset_size;
43988095 4645 uint16_t version;
f4dc4d17
DE
4646
4647 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4648 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4649 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4650 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4651 info_ptr += initial_length_size;
4652
4653 version = read_2_bytes (abfd, info_ptr);
4654 info_ptr += 2;
4655 if (version >= 5)
4656 {
4657 /* Skip unit type and address size. */
4658 info_ptr += 2;
4659 }
4660
9c541725 4661 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4662}
4663
aaa75496
JB
4664/* Allocate a new partial symtab for file named NAME and mark this new
4665 partial symtab as being an include of PST. */
4666
4667static void
d521ce57 4668dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4669 struct objfile *objfile)
4670{
4671 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4672
fbd9ab74
JK
4673 if (!IS_ABSOLUTE_PATH (subpst->filename))
4674 {
4675 /* It shares objfile->objfile_obstack. */
4676 subpst->dirname = pst->dirname;
4677 }
4678
aaa75496
JB
4679 subpst->textlow = 0;
4680 subpst->texthigh = 0;
4681
8d749320
SM
4682 subpst->dependencies
4683 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4684 subpst->dependencies[0] = pst;
4685 subpst->number_of_dependencies = 1;
4686
4687 subpst->globals_offset = 0;
4688 subpst->n_global_syms = 0;
4689 subpst->statics_offset = 0;
4690 subpst->n_static_syms = 0;
43f3e411 4691 subpst->compunit_symtab = NULL;
aaa75496
JB
4692 subpst->read_symtab = pst->read_symtab;
4693 subpst->readin = 0;
4694
4695 /* No private part is necessary for include psymtabs. This property
4696 can be used to differentiate between such include psymtabs and
10b3939b 4697 the regular ones. */
58a9656e 4698 subpst->read_symtab_private = NULL;
aaa75496
JB
4699}
4700
4701/* Read the Line Number Program data and extract the list of files
4702 included by the source file represented by PST. Build an include
d85a05f0 4703 partial symtab for each of these included files. */
aaa75496
JB
4704
4705static void
4706dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4707 struct die_info *die,
4708 struct partial_symtab *pst)
aaa75496 4709{
fff8551c 4710 line_header_up lh;
d85a05f0 4711 struct attribute *attr;
aaa75496 4712
d85a05f0
DJ
4713 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4714 if (attr)
9c541725 4715 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4716 if (lh == NULL)
4717 return; /* No linetable, so no includes. */
4718
c6da4cef 4719 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4720 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4721}
4722
348e048f 4723static hashval_t
52dc124a 4724hash_signatured_type (const void *item)
348e048f 4725{
9a3c8263
SM
4726 const struct signatured_type *sig_type
4727 = (const struct signatured_type *) item;
9a619af0 4728
348e048f 4729 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4730 return sig_type->signature;
348e048f
DE
4731}
4732
4733static int
52dc124a 4734eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4735{
9a3c8263
SM
4736 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4737 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4738
348e048f
DE
4739 return lhs->signature == rhs->signature;
4740}
4741
1fd400ff
TT
4742/* Allocate a hash table for signatured types. */
4743
4744static htab_t
673bfd45 4745allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4746{
4747 return htab_create_alloc_ex (41,
52dc124a
DE
4748 hash_signatured_type,
4749 eq_signatured_type,
1fd400ff
TT
4750 NULL,
4751 &objfile->objfile_obstack,
4752 hashtab_obstack_allocate,
4753 dummy_obstack_deallocate);
4754}
4755
d467dd73 4756/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4757
4758static int
d467dd73 4759add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4760{
9a3c8263
SM
4761 struct signatured_type *sigt = (struct signatured_type *) *slot;
4762 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4763
b4dd5633 4764 **datap = sigt;
1fd400ff
TT
4765 ++*datap;
4766
4767 return 1;
4768}
4769
78d4d2c5 4770/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4771 and fill them into TYPES_HTAB. It will process only type units,
4772 therefore DW_UT_type. */
c88ee1f0 4773
78d4d2c5
JK
4774static void
4775create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4776 dwarf2_section_info *section, htab_t &types_htab,
4777 rcuh_kind section_kind)
348e048f 4778{
3019eac3 4779 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4780 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4781 bfd *abfd;
4782 const gdb_byte *info_ptr, *end_ptr;
348e048f 4783
4bdcc0c1
DE
4784 abbrev_section = (dwo_file != NULL
4785 ? &dwo_file->sections.abbrev
4786 : &dwarf2_per_objfile->abbrev);
4787
b4f54984 4788 if (dwarf_read_debug)
43988095
JK
4789 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4790 get_section_name (section),
a32a8923 4791 get_section_file_name (abbrev_section));
09406207 4792
78d4d2c5
JK
4793 dwarf2_read_section (objfile, section);
4794 info_ptr = section->buffer;
348e048f 4795
78d4d2c5
JK
4796 if (info_ptr == NULL)
4797 return;
348e048f 4798
78d4d2c5
JK
4799 /* We can't set abfd until now because the section may be empty or
4800 not present, in which case the bfd is unknown. */
4801 abfd = get_section_bfd_owner (section);
348e048f 4802
78d4d2c5
JK
4803 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4804 because we don't need to read any dies: the signature is in the
4805 header. */
3019eac3 4806
78d4d2c5
JK
4807 end_ptr = info_ptr + section->size;
4808 while (info_ptr < end_ptr)
4809 {
78d4d2c5
JK
4810 struct signatured_type *sig_type;
4811 struct dwo_unit *dwo_tu;
4812 void **slot;
4813 const gdb_byte *ptr = info_ptr;
4814 struct comp_unit_head header;
4815 unsigned int length;
8b70b953 4816
9c541725 4817 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4818
a49dd8dd
JK
4819 /* Initialize it due to a false compiler warning. */
4820 header.signature = -1;
9c541725 4821 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4822
78d4d2c5
JK
4823 /* We need to read the type's signature in order to build the hash
4824 table, but we don't need anything else just yet. */
348e048f 4825
43988095
JK
4826 ptr = read_and_check_comp_unit_head (&header, section,
4827 abbrev_section, ptr, section_kind);
348e048f 4828
78d4d2c5 4829 length = get_cu_length (&header);
6caca83c 4830
78d4d2c5
JK
4831 /* Skip dummy type units. */
4832 if (ptr >= info_ptr + length
43988095
JK
4833 || peek_abbrev_code (abfd, ptr) == 0
4834 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4835 {
4836 info_ptr += length;
4837 continue;
4838 }
dee91e82 4839
78d4d2c5
JK
4840 if (types_htab == NULL)
4841 {
4842 if (dwo_file)
4843 types_htab = allocate_dwo_unit_table (objfile);
4844 else
4845 types_htab = allocate_signatured_type_table (objfile);
4846 }
8b70b953 4847
78d4d2c5
JK
4848 if (dwo_file)
4849 {
4850 sig_type = NULL;
4851 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4852 struct dwo_unit);
4853 dwo_tu->dwo_file = dwo_file;
43988095 4854 dwo_tu->signature = header.signature;
9c541725 4855 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4856 dwo_tu->section = section;
9c541725 4857 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4858 dwo_tu->length = length;
4859 }
4860 else
4861 {
4862 /* N.B.: type_offset is not usable if this type uses a DWO file.
4863 The real type_offset is in the DWO file. */
4864 dwo_tu = NULL;
4865 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4866 struct signatured_type);
43988095 4867 sig_type->signature = header.signature;
9c541725 4868 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4869 sig_type->per_cu.objfile = objfile;
4870 sig_type->per_cu.is_debug_types = 1;
4871 sig_type->per_cu.section = section;
9c541725 4872 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4873 sig_type->per_cu.length = length;
4874 }
4875
4876 slot = htab_find_slot (types_htab,
4877 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4878 INSERT);
4879 gdb_assert (slot != NULL);
4880 if (*slot != NULL)
4881 {
9c541725 4882 sect_offset dup_sect_off;
0349ea22 4883
3019eac3
DE
4884 if (dwo_file)
4885 {
78d4d2c5
JK
4886 const struct dwo_unit *dup_tu
4887 = (const struct dwo_unit *) *slot;
4888
9c541725 4889 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4890 }
4891 else
4892 {
78d4d2c5
JK
4893 const struct signatured_type *dup_tu
4894 = (const struct signatured_type *) *slot;
4895
9c541725 4896 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4897 }
8b70b953 4898
78d4d2c5
JK
4899 complaint (&symfile_complaints,
4900 _("debug type entry at offset 0x%x is duplicate to"
4901 " the entry at offset 0x%x, signature %s"),
9c541725 4902 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4903 hex_string (header.signature));
78d4d2c5
JK
4904 }
4905 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4906
78d4d2c5
JK
4907 if (dwarf_read_debug > 1)
4908 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4909 to_underlying (sect_off),
43988095 4910 hex_string (header.signature));
3019eac3 4911
78d4d2c5
JK
4912 info_ptr += length;
4913 }
4914}
3019eac3 4915
78d4d2c5
JK
4916/* Create the hash table of all entries in the .debug_types
4917 (or .debug_types.dwo) section(s).
4918 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4919 otherwise it is NULL.
b3c8eb43 4920
78d4d2c5 4921 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4922
78d4d2c5 4923 Note: This function processes DWO files only, not DWP files. */
348e048f 4924
78d4d2c5
JK
4925static void
4926create_debug_types_hash_table (struct dwo_file *dwo_file,
4927 VEC (dwarf2_section_info_def) *types,
4928 htab_t &types_htab)
4929{
4930 int ix;
4931 struct dwarf2_section_info *section;
4932
4933 if (VEC_empty (dwarf2_section_info_def, types))
4934 return;
348e048f 4935
78d4d2c5
JK
4936 for (ix = 0;
4937 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4938 ++ix)
43988095
JK
4939 create_debug_type_hash_table (dwo_file, section, types_htab,
4940 rcuh_kind::TYPE);
3019eac3
DE
4941}
4942
4943/* Create the hash table of all entries in the .debug_types section,
4944 and initialize all_type_units.
4945 The result is zero if there is an error (e.g. missing .debug_types section),
4946 otherwise non-zero. */
4947
4948static int
4949create_all_type_units (struct objfile *objfile)
4950{
78d4d2c5 4951 htab_t types_htab = NULL;
b4dd5633 4952 struct signatured_type **iter;
3019eac3 4953
43988095
JK
4954 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4955 rcuh_kind::COMPILE);
78d4d2c5 4956 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
4957 if (types_htab == NULL)
4958 {
4959 dwarf2_per_objfile->signatured_types = NULL;
4960 return 0;
4961 }
4962
348e048f
DE
4963 dwarf2_per_objfile->signatured_types = types_htab;
4964
6aa5f3a6
DE
4965 dwarf2_per_objfile->n_type_units
4966 = dwarf2_per_objfile->n_allocated_type_units
4967 = htab_elements (types_htab);
8d749320
SM
4968 dwarf2_per_objfile->all_type_units =
4969 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4970 iter = &dwarf2_per_objfile->all_type_units[0];
4971 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4972 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4973 == dwarf2_per_objfile->n_type_units);
1fd400ff 4974
348e048f
DE
4975 return 1;
4976}
4977
6aa5f3a6
DE
4978/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4979 If SLOT is non-NULL, it is the entry to use in the hash table.
4980 Otherwise we find one. */
4981
4982static struct signatured_type *
4983add_type_unit (ULONGEST sig, void **slot)
4984{
4985 struct objfile *objfile = dwarf2_per_objfile->objfile;
4986 int n_type_units = dwarf2_per_objfile->n_type_units;
4987 struct signatured_type *sig_type;
4988
4989 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4990 ++n_type_units;
4991 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4992 {
4993 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4994 dwarf2_per_objfile->n_allocated_type_units = 1;
4995 dwarf2_per_objfile->n_allocated_type_units *= 2;
4996 dwarf2_per_objfile->all_type_units
224c3ddb
SM
4997 = XRESIZEVEC (struct signatured_type *,
4998 dwarf2_per_objfile->all_type_units,
4999 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5000 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5001 }
5002 dwarf2_per_objfile->n_type_units = n_type_units;
5003
5004 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5005 struct signatured_type);
5006 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5007 sig_type->signature = sig;
5008 sig_type->per_cu.is_debug_types = 1;
5009 if (dwarf2_per_objfile->using_index)
5010 {
5011 sig_type->per_cu.v.quick =
5012 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5013 struct dwarf2_per_cu_quick_data);
5014 }
5015
5016 if (slot == NULL)
5017 {
5018 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5019 sig_type, INSERT);
5020 }
5021 gdb_assert (*slot == NULL);
5022 *slot = sig_type;
5023 /* The rest of sig_type must be filled in by the caller. */
5024 return sig_type;
5025}
5026
a2ce51a0
DE
5027/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5028 Fill in SIG_ENTRY with DWO_ENTRY. */
5029
5030static void
5031fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5032 struct signatured_type *sig_entry,
5033 struct dwo_unit *dwo_entry)
5034{
7ee85ab1 5035 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5036 gdb_assert (! sig_entry->per_cu.queued);
5037 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5038 if (dwarf2_per_objfile->using_index)
5039 {
5040 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5041 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5042 }
5043 else
5044 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5045 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5046 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5047 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5048 gdb_assert (sig_entry->dwo_unit == NULL);
5049
5050 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5051 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5052 sig_entry->per_cu.length = dwo_entry->length;
5053 sig_entry->per_cu.reading_dwo_directly = 1;
5054 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5055 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5056 sig_entry->dwo_unit = dwo_entry;
5057}
5058
5059/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5060 If we haven't read the TU yet, create the signatured_type data structure
5061 for a TU to be read in directly from a DWO file, bypassing the stub.
5062 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5063 using .gdb_index, then when reading a CU we want to stay in the DWO file
5064 containing that CU. Otherwise we could end up reading several other DWO
5065 files (due to comdat folding) to process the transitive closure of all the
5066 mentioned TUs, and that can be slow. The current DWO file will have every
5067 type signature that it needs.
a2ce51a0
DE
5068 We only do this for .gdb_index because in the psymtab case we already have
5069 to read all the DWOs to build the type unit groups. */
5070
5071static struct signatured_type *
5072lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5073{
5074 struct objfile *objfile = dwarf2_per_objfile->objfile;
5075 struct dwo_file *dwo_file;
5076 struct dwo_unit find_dwo_entry, *dwo_entry;
5077 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5078 void **slot;
a2ce51a0
DE
5079
5080 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5081
6aa5f3a6
DE
5082 /* If TU skeletons have been removed then we may not have read in any
5083 TUs yet. */
5084 if (dwarf2_per_objfile->signatured_types == NULL)
5085 {
5086 dwarf2_per_objfile->signatured_types
5087 = allocate_signatured_type_table (objfile);
5088 }
a2ce51a0
DE
5089
5090 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5091 Use the global signatured_types array to do our own comdat-folding
5092 of types. If this is the first time we're reading this TU, and
5093 the TU has an entry in .gdb_index, replace the recorded data from
5094 .gdb_index with this TU. */
a2ce51a0 5095
a2ce51a0 5096 find_sig_entry.signature = sig;
6aa5f3a6
DE
5097 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5098 &find_sig_entry, INSERT);
9a3c8263 5099 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5100
5101 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5102 read. Don't reassign the global entry to point to this DWO if that's
5103 the case. Also note that if the TU is already being read, it may not
5104 have come from a DWO, the program may be a mix of Fission-compiled
5105 code and non-Fission-compiled code. */
5106
5107 /* Have we already tried to read this TU?
5108 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5109 needn't exist in the global table yet). */
5110 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5111 return sig_entry;
5112
6aa5f3a6
DE
5113 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5114 dwo_unit of the TU itself. */
5115 dwo_file = cu->dwo_unit->dwo_file;
5116
a2ce51a0
DE
5117 /* Ok, this is the first time we're reading this TU. */
5118 if (dwo_file->tus == NULL)
5119 return NULL;
5120 find_dwo_entry.signature = sig;
9a3c8263 5121 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5122 if (dwo_entry == NULL)
5123 return NULL;
5124
6aa5f3a6
DE
5125 /* If the global table doesn't have an entry for this TU, add one. */
5126 if (sig_entry == NULL)
5127 sig_entry = add_type_unit (sig, slot);
5128
a2ce51a0 5129 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5130 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5131 return sig_entry;
5132}
5133
a2ce51a0
DE
5134/* Subroutine of lookup_signatured_type.
5135 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5136 then try the DWP file. If the TU stub (skeleton) has been removed then
5137 it won't be in .gdb_index. */
a2ce51a0
DE
5138
5139static struct signatured_type *
5140lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5141{
5142 struct objfile *objfile = dwarf2_per_objfile->objfile;
5143 struct dwp_file *dwp_file = get_dwp_file ();
5144 struct dwo_unit *dwo_entry;
5145 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5146 void **slot;
a2ce51a0
DE
5147
5148 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5149 gdb_assert (dwp_file != NULL);
5150
6aa5f3a6
DE
5151 /* If TU skeletons have been removed then we may not have read in any
5152 TUs yet. */
5153 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5154 {
6aa5f3a6
DE
5155 dwarf2_per_objfile->signatured_types
5156 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5157 }
5158
6aa5f3a6
DE
5159 find_sig_entry.signature = sig;
5160 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5161 &find_sig_entry, INSERT);
9a3c8263 5162 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5163
5164 /* Have we already tried to read this TU?
5165 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5166 needn't exist in the global table yet). */
5167 if (sig_entry != NULL)
5168 return sig_entry;
5169
a2ce51a0
DE
5170 if (dwp_file->tus == NULL)
5171 return NULL;
57d63ce2
DE
5172 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5173 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5174 if (dwo_entry == NULL)
5175 return NULL;
5176
6aa5f3a6 5177 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5178 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5179
a2ce51a0
DE
5180 return sig_entry;
5181}
5182
380bca97 5183/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5184 Returns NULL if signature SIG is not present in the table.
5185 It is up to the caller to complain about this. */
348e048f
DE
5186
5187static struct signatured_type *
a2ce51a0 5188lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5189{
a2ce51a0
DE
5190 if (cu->dwo_unit
5191 && dwarf2_per_objfile->using_index)
5192 {
5193 /* We're in a DWO/DWP file, and we're using .gdb_index.
5194 These cases require special processing. */
5195 if (get_dwp_file () == NULL)
5196 return lookup_dwo_signatured_type (cu, sig);
5197 else
5198 return lookup_dwp_signatured_type (cu, sig);
5199 }
5200 else
5201 {
5202 struct signatured_type find_entry, *entry;
348e048f 5203
a2ce51a0
DE
5204 if (dwarf2_per_objfile->signatured_types == NULL)
5205 return NULL;
5206 find_entry.signature = sig;
9a3c8263
SM
5207 entry = ((struct signatured_type *)
5208 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5209 return entry;
5210 }
348e048f 5211}
42e7ad6c
DE
5212\f
5213/* Low level DIE reading support. */
348e048f 5214
d85a05f0
DJ
5215/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5216
5217static void
5218init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5219 struct dwarf2_cu *cu,
3019eac3
DE
5220 struct dwarf2_section_info *section,
5221 struct dwo_file *dwo_file)
d85a05f0 5222{
fceca515 5223 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5224 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5225 reader->cu = cu;
3019eac3 5226 reader->dwo_file = dwo_file;
dee91e82
DE
5227 reader->die_section = section;
5228 reader->buffer = section->buffer;
f664829e 5229 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5230 reader->comp_dir = NULL;
d85a05f0
DJ
5231}
5232
b0c7bfa9
DE
5233/* Subroutine of init_cutu_and_read_dies to simplify it.
5234 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5235 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5236 already.
5237
5238 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5239 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5240 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5241 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5242 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5243 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5244 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5245 are filled in with the info of the DIE from the DWO file.
5246 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5247 provided an abbrev table to use.
5248 The result is non-zero if a valid (non-dummy) DIE was found. */
5249
5250static int
5251read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5252 struct dwo_unit *dwo_unit,
5253 int abbrev_table_provided,
5254 struct die_info *stub_comp_unit_die,
a2ce51a0 5255 const char *stub_comp_dir,
b0c7bfa9 5256 struct die_reader_specs *result_reader,
d521ce57 5257 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5258 struct die_info **result_comp_unit_die,
5259 int *result_has_children)
5260{
5261 struct objfile *objfile = dwarf2_per_objfile->objfile;
5262 struct dwarf2_cu *cu = this_cu->cu;
5263 struct dwarf2_section_info *section;
5264 bfd *abfd;
d521ce57 5265 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5266 ULONGEST signature; /* Or dwo_id. */
5267 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5268 int i,num_extra_attrs;
5269 struct dwarf2_section_info *dwo_abbrev_section;
5270 struct attribute *attr;
5271 struct die_info *comp_unit_die;
5272
b0aeadb3
DE
5273 /* At most one of these may be provided. */
5274 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5275
b0c7bfa9
DE
5276 /* These attributes aren't processed until later:
5277 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5278 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5279 referenced later. However, these attributes are found in the stub
5280 which we won't have later. In order to not impose this complication
5281 on the rest of the code, we read them here and copy them to the
5282 DWO CU/TU die. */
b0c7bfa9
DE
5283
5284 stmt_list = NULL;
5285 low_pc = NULL;
5286 high_pc = NULL;
5287 ranges = NULL;
5288 comp_dir = NULL;
5289
5290 if (stub_comp_unit_die != NULL)
5291 {
5292 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5293 DWO file. */
5294 if (! this_cu->is_debug_types)
5295 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5296 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5297 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5298 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5299 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5300
5301 /* There should be a DW_AT_addr_base attribute here (if needed).
5302 We need the value before we can process DW_FORM_GNU_addr_index. */
5303 cu->addr_base = 0;
5304 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5305 if (attr)
5306 cu->addr_base = DW_UNSND (attr);
5307
5308 /* There should be a DW_AT_ranges_base attribute here (if needed).
5309 We need the value before we can process DW_AT_ranges. */
5310 cu->ranges_base = 0;
5311 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5312 if (attr)
5313 cu->ranges_base = DW_UNSND (attr);
5314 }
a2ce51a0
DE
5315 else if (stub_comp_dir != NULL)
5316 {
5317 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5318 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5319 comp_dir->name = DW_AT_comp_dir;
5320 comp_dir->form = DW_FORM_string;
5321 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5322 DW_STRING (comp_dir) = stub_comp_dir;
5323 }
b0c7bfa9
DE
5324
5325 /* Set up for reading the DWO CU/TU. */
5326 cu->dwo_unit = dwo_unit;
5327 section = dwo_unit->section;
5328 dwarf2_read_section (objfile, section);
a32a8923 5329 abfd = get_section_bfd_owner (section);
9c541725
PA
5330 begin_info_ptr = info_ptr = (section->buffer
5331 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5332 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5333 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5334
5335 if (this_cu->is_debug_types)
5336 {
b0c7bfa9
DE
5337 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5338
43988095 5339 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5340 dwo_abbrev_section,
43988095 5341 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5342 /* This is not an assert because it can be caused by bad debug info. */
43988095 5343 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5344 {
5345 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5346 " TU at offset 0x%x [in module %s]"),
5347 hex_string (sig_type->signature),
43988095 5348 hex_string (cu->header.signature),
9c541725 5349 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5350 bfd_get_filename (abfd));
5351 }
9c541725 5352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5353 /* For DWOs coming from DWP files, we don't know the CU length
5354 nor the type's offset in the TU until now. */
5355 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5356 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5357
5358 /* Establish the type offset that can be used to lookup the type.
5359 For DWO files, we don't know it until now. */
9c541725
PA
5360 sig_type->type_offset_in_section
5361 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5362 }
5363 else
5364 {
5365 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5366 dwo_abbrev_section,
43988095 5367 info_ptr, rcuh_kind::COMPILE);
9c541725 5368 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5369 /* For DWOs coming from DWP files, we don't know the CU length
5370 until now. */
5371 dwo_unit->length = get_cu_length (&cu->header);
5372 }
5373
02142a6c
DE
5374 /* Replace the CU's original abbrev table with the DWO's.
5375 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5376 if (abbrev_table_provided)
5377 {
5378 /* Don't free the provided abbrev table, the caller of
5379 init_cutu_and_read_dies owns it. */
5380 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5381 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5382 make_cleanup (dwarf2_free_abbrev_table, cu);
5383 }
5384 else
5385 {
5386 dwarf2_free_abbrev_table (cu);
5387 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5388 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5389 }
5390
5391 /* Read in the die, but leave space to copy over the attributes
5392 from the stub. This has the benefit of simplifying the rest of
5393 the code - all the work to maintain the illusion of a single
5394 DW_TAG_{compile,type}_unit DIE is done here. */
5395 num_extra_attrs = ((stmt_list != NULL)
5396 + (low_pc != NULL)
5397 + (high_pc != NULL)
5398 + (ranges != NULL)
5399 + (comp_dir != NULL));
5400 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5401 result_has_children, num_extra_attrs);
5402
5403 /* Copy over the attributes from the stub to the DIE we just read in. */
5404 comp_unit_die = *result_comp_unit_die;
5405 i = comp_unit_die->num_attrs;
5406 if (stmt_list != NULL)
5407 comp_unit_die->attrs[i++] = *stmt_list;
5408 if (low_pc != NULL)
5409 comp_unit_die->attrs[i++] = *low_pc;
5410 if (high_pc != NULL)
5411 comp_unit_die->attrs[i++] = *high_pc;
5412 if (ranges != NULL)
5413 comp_unit_die->attrs[i++] = *ranges;
5414 if (comp_dir != NULL)
5415 comp_unit_die->attrs[i++] = *comp_dir;
5416 comp_unit_die->num_attrs += num_extra_attrs;
5417
b4f54984 5418 if (dwarf_die_debug)
bf6af496
DE
5419 {
5420 fprintf_unfiltered (gdb_stdlog,
5421 "Read die from %s@0x%x of %s:\n",
a32a8923 5422 get_section_name (section),
bf6af496
DE
5423 (unsigned) (begin_info_ptr - section->buffer),
5424 bfd_get_filename (abfd));
b4f54984 5425 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5426 }
5427
a2ce51a0
DE
5428 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5429 TUs by skipping the stub and going directly to the entry in the DWO file.
5430 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5431 to get it via circuitous means. Blech. */
5432 if (comp_dir != NULL)
5433 result_reader->comp_dir = DW_STRING (comp_dir);
5434
b0c7bfa9
DE
5435 /* Skip dummy compilation units. */
5436 if (info_ptr >= begin_info_ptr + dwo_unit->length
5437 || peek_abbrev_code (abfd, info_ptr) == 0)
5438 return 0;
5439
5440 *result_info_ptr = info_ptr;
5441 return 1;
5442}
5443
5444/* Subroutine of init_cutu_and_read_dies to simplify it.
5445 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5446 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5447
5448static struct dwo_unit *
5449lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5450 struct die_info *comp_unit_die)
5451{
5452 struct dwarf2_cu *cu = this_cu->cu;
5453 struct attribute *attr;
5454 ULONGEST signature;
5455 struct dwo_unit *dwo_unit;
5456 const char *comp_dir, *dwo_name;
5457
a2ce51a0
DE
5458 gdb_assert (cu != NULL);
5459
b0c7bfa9 5460 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5461 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5462 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5463
5464 if (this_cu->is_debug_types)
5465 {
5466 struct signatured_type *sig_type;
5467
5468 /* Since this_cu is the first member of struct signatured_type,
5469 we can go from a pointer to one to a pointer to the other. */
5470 sig_type = (struct signatured_type *) this_cu;
5471 signature = sig_type->signature;
5472 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5473 }
5474 else
5475 {
5476 struct attribute *attr;
5477
5478 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5479 if (! attr)
5480 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5481 " [in module %s]"),
4262abfb 5482 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5483 signature = DW_UNSND (attr);
5484 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5485 signature);
5486 }
5487
b0c7bfa9
DE
5488 return dwo_unit;
5489}
5490
a2ce51a0 5491/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5492 See it for a description of the parameters.
5493 Read a TU directly from a DWO file, bypassing the stub.
5494
5495 Note: This function could be a little bit simpler if we shared cleanups
5496 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5497 to do, so we keep this function self-contained. Or we could move this
5498 into our caller, but it's complex enough already. */
a2ce51a0
DE
5499
5500static void
6aa5f3a6
DE
5501init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5502 int use_existing_cu, int keep,
a2ce51a0
DE
5503 die_reader_func_ftype *die_reader_func,
5504 void *data)
5505{
5506 struct dwarf2_cu *cu;
5507 struct signatured_type *sig_type;
6aa5f3a6 5508 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5509 struct die_reader_specs reader;
5510 const gdb_byte *info_ptr;
5511 struct die_info *comp_unit_die;
5512 int has_children;
5513
5514 /* Verify we can do the following downcast, and that we have the
5515 data we need. */
5516 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5517 sig_type = (struct signatured_type *) this_cu;
5518 gdb_assert (sig_type->dwo_unit != NULL);
5519
5520 cleanups = make_cleanup (null_cleanup, NULL);
5521
6aa5f3a6
DE
5522 if (use_existing_cu && this_cu->cu != NULL)
5523 {
5524 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5525 cu = this_cu->cu;
5526 /* There's no need to do the rereading_dwo_cu handling that
5527 init_cutu_and_read_dies does since we don't read the stub. */
5528 }
5529 else
5530 {
5531 /* If !use_existing_cu, this_cu->cu must be NULL. */
5532 gdb_assert (this_cu->cu == NULL);
8d749320 5533 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5534 init_one_comp_unit (cu, this_cu);
5535 /* If an error occurs while loading, release our storage. */
5536 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5537 }
5538
5539 /* A future optimization, if needed, would be to use an existing
5540 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5541 could share abbrev tables. */
a2ce51a0
DE
5542
5543 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5544 0 /* abbrev_table_provided */,
5545 NULL /* stub_comp_unit_die */,
5546 sig_type->dwo_unit->dwo_file->comp_dir,
5547 &reader, &info_ptr,
5548 &comp_unit_die, &has_children) == 0)
5549 {
5550 /* Dummy die. */
5551 do_cleanups (cleanups);
5552 return;
5553 }
5554
5555 /* All the "real" work is done here. */
5556 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5557
6aa5f3a6 5558 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5559 but the alternative is making the latter more complex.
5560 This function is only for the special case of using DWO files directly:
5561 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5562 if (free_cu_cleanup != NULL)
a2ce51a0 5563 {
6aa5f3a6
DE
5564 if (keep)
5565 {
5566 /* We've successfully allocated this compilation unit. Let our
5567 caller clean it up when finished with it. */
5568 discard_cleanups (free_cu_cleanup);
a2ce51a0 5569
6aa5f3a6
DE
5570 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5571 So we have to manually free the abbrev table. */
5572 dwarf2_free_abbrev_table (cu);
a2ce51a0 5573
6aa5f3a6
DE
5574 /* Link this CU into read_in_chain. */
5575 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5576 dwarf2_per_objfile->read_in_chain = this_cu;
5577 }
5578 else
5579 do_cleanups (free_cu_cleanup);
a2ce51a0 5580 }
a2ce51a0
DE
5581
5582 do_cleanups (cleanups);
5583}
5584
fd820528 5585/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5586 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5587
f4dc4d17
DE
5588 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5589 Otherwise the table specified in the comp unit header is read in and used.
5590 This is an optimization for when we already have the abbrev table.
5591
dee91e82
DE
5592 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5593 Otherwise, a new CU is allocated with xmalloc.
5594
5595 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5596 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5597
5598 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5599 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5600
70221824 5601static void
fd820528 5602init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5603 struct abbrev_table *abbrev_table,
fd820528
DE
5604 int use_existing_cu, int keep,
5605 die_reader_func_ftype *die_reader_func,
5606 void *data)
c906108c 5607{
dee91e82 5608 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5609 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5610 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5611 struct dwarf2_cu *cu;
d521ce57 5612 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5613 struct die_reader_specs reader;
d85a05f0 5614 struct die_info *comp_unit_die;
dee91e82 5615 int has_children;
d85a05f0 5616 struct attribute *attr;
365156ad 5617 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5618 struct signatured_type *sig_type = NULL;
4bdcc0c1 5619 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5620 /* Non-zero if CU currently points to a DWO file and we need to
5621 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5622 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5623 int rereading_dwo_cu = 0;
c906108c 5624
b4f54984 5625 if (dwarf_die_debug)
09406207
DE
5626 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5627 this_cu->is_debug_types ? "type" : "comp",
9c541725 5628 to_underlying (this_cu->sect_off));
09406207 5629
dee91e82
DE
5630 if (use_existing_cu)
5631 gdb_assert (keep);
23745b47 5632
a2ce51a0
DE
5633 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5634 file (instead of going through the stub), short-circuit all of this. */
5635 if (this_cu->reading_dwo_directly)
5636 {
5637 /* Narrow down the scope of possibilities to have to understand. */
5638 gdb_assert (this_cu->is_debug_types);
5639 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5640 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5641 die_reader_func, data);
a2ce51a0
DE
5642 return;
5643 }
5644
dee91e82
DE
5645 cleanups = make_cleanup (null_cleanup, NULL);
5646
5647 /* This is cheap if the section is already read in. */
5648 dwarf2_read_section (objfile, section);
5649
9c541725 5650 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5651
5652 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5653
5654 if (use_existing_cu && this_cu->cu != NULL)
5655 {
5656 cu = this_cu->cu;
42e7ad6c
DE
5657 /* If this CU is from a DWO file we need to start over, we need to
5658 refetch the attributes from the skeleton CU.
5659 This could be optimized by retrieving those attributes from when we
5660 were here the first time: the previous comp_unit_die was stored in
5661 comp_unit_obstack. But there's no data yet that we need this
5662 optimization. */
5663 if (cu->dwo_unit != NULL)
5664 rereading_dwo_cu = 1;
dee91e82
DE
5665 }
5666 else
5667 {
5668 /* If !use_existing_cu, this_cu->cu must be NULL. */
5669 gdb_assert (this_cu->cu == NULL);
8d749320 5670 cu = XNEW (struct dwarf2_cu);
dee91e82 5671 init_one_comp_unit (cu, this_cu);
dee91e82 5672 /* If an error occurs while loading, release our storage. */
365156ad 5673 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5674 }
dee91e82 5675
b0c7bfa9 5676 /* Get the header. */
9c541725 5677 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5678 {
5679 /* We already have the header, there's no need to read it in again. */
9c541725 5680 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5681 }
5682 else
5683 {
3019eac3 5684 if (this_cu->is_debug_types)
dee91e82 5685 {
43988095 5686 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5687 abbrev_section, info_ptr,
43988095 5688 rcuh_kind::TYPE);
dee91e82 5689
42e7ad6c
DE
5690 /* Since per_cu is the first member of struct signatured_type,
5691 we can go from a pointer to one to a pointer to the other. */
5692 sig_type = (struct signatured_type *) this_cu;
43988095 5693 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5694 gdb_assert (sig_type->type_offset_in_tu
5695 == cu->header.type_cu_offset_in_tu);
5696 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5697
42e7ad6c
DE
5698 /* LENGTH has not been set yet for type units if we're
5699 using .gdb_index. */
1ce1cefd 5700 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5701
5702 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5703 sig_type->type_offset_in_section =
5704 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5705
5706 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5707 }
5708 else
5709 {
4bdcc0c1
DE
5710 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5711 abbrev_section,
43988095
JK
5712 info_ptr,
5713 rcuh_kind::COMPILE);
dee91e82 5714
9c541725 5715 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5716 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5717 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5718 }
5719 }
10b3939b 5720
6caca83c 5721 /* Skip dummy compilation units. */
dee91e82 5722 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5723 || peek_abbrev_code (abfd, info_ptr) == 0)
5724 {
dee91e82 5725 do_cleanups (cleanups);
21b2bd31 5726 return;
6caca83c
CC
5727 }
5728
433df2d4
DE
5729 /* If we don't have them yet, read the abbrevs for this compilation unit.
5730 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5731 done. Note that it's important that if the CU had an abbrev table
5732 on entry we don't free it when we're done: Somewhere up the call stack
5733 it may be in use. */
f4dc4d17
DE
5734 if (abbrev_table != NULL)
5735 {
5736 gdb_assert (cu->abbrev_table == NULL);
9c541725 5737 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5738 cu->abbrev_table = abbrev_table;
5739 }
5740 else if (cu->abbrev_table == NULL)
dee91e82 5741 {
4bdcc0c1 5742 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5743 make_cleanup (dwarf2_free_abbrev_table, cu);
5744 }
42e7ad6c
DE
5745 else if (rereading_dwo_cu)
5746 {
5747 dwarf2_free_abbrev_table (cu);
5748 dwarf2_read_abbrevs (cu, abbrev_section);
5749 }
af703f96 5750
dee91e82 5751 /* Read the top level CU/TU die. */
3019eac3 5752 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5753 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5754
b0c7bfa9
DE
5755 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5756 from the DWO file.
5757 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5758 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5759 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5760 if (attr)
5761 {
3019eac3 5762 struct dwo_unit *dwo_unit;
b0c7bfa9 5763 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5764
5765 if (has_children)
6a506a2d
DE
5766 {
5767 complaint (&symfile_complaints,
5768 _("compilation unit with DW_AT_GNU_dwo_name"
5769 " has children (offset 0x%x) [in module %s]"),
9c541725 5770 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5771 }
b0c7bfa9 5772 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5773 if (dwo_unit != NULL)
3019eac3 5774 {
6a506a2d
DE
5775 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5776 abbrev_table != NULL,
a2ce51a0 5777 comp_unit_die, NULL,
6a506a2d
DE
5778 &reader, &info_ptr,
5779 &dwo_comp_unit_die, &has_children) == 0)
5780 {
5781 /* Dummy die. */
5782 do_cleanups (cleanups);
5783 return;
5784 }
5785 comp_unit_die = dwo_comp_unit_die;
5786 }
5787 else
5788 {
5789 /* Yikes, we couldn't find the rest of the DIE, we only have
5790 the stub. A complaint has already been logged. There's
5791 not much more we can do except pass on the stub DIE to
5792 die_reader_func. We don't want to throw an error on bad
5793 debug info. */
3019eac3
DE
5794 }
5795 }
5796
b0c7bfa9 5797 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5798 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5799
b0c7bfa9 5800 /* Done, clean up. */
365156ad 5801 if (free_cu_cleanup != NULL)
348e048f 5802 {
365156ad
TT
5803 if (keep)
5804 {
5805 /* We've successfully allocated this compilation unit. Let our
5806 caller clean it up when finished with it. */
5807 discard_cleanups (free_cu_cleanup);
dee91e82 5808
365156ad
TT
5809 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5810 So we have to manually free the abbrev table. */
5811 dwarf2_free_abbrev_table (cu);
dee91e82 5812
365156ad
TT
5813 /* Link this CU into read_in_chain. */
5814 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5815 dwarf2_per_objfile->read_in_chain = this_cu;
5816 }
5817 else
5818 do_cleanups (free_cu_cleanup);
348e048f 5819 }
365156ad
TT
5820
5821 do_cleanups (cleanups);
dee91e82
DE
5822}
5823
33e80786
DE
5824/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5825 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5826 to have already done the lookup to find the DWO file).
dee91e82
DE
5827
5828 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5829 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5830
5831 We fill in THIS_CU->length.
5832
5833 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5834 linker) then DIE_READER_FUNC will not get called.
5835
5836 THIS_CU->cu is always freed when done.
3019eac3
DE
5837 This is done in order to not leave THIS_CU->cu in a state where we have
5838 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5839
5840static void
5841init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5842 struct dwo_file *dwo_file,
dee91e82
DE
5843 die_reader_func_ftype *die_reader_func,
5844 void *data)
5845{
5846 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5847 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5848 bfd *abfd = get_section_bfd_owner (section);
33e80786 5849 struct dwarf2_section_info *abbrev_section;
dee91e82 5850 struct dwarf2_cu cu;
d521ce57 5851 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5852 struct die_reader_specs reader;
5853 struct cleanup *cleanups;
5854 struct die_info *comp_unit_die;
5855 int has_children;
5856
b4f54984 5857 if (dwarf_die_debug)
09406207
DE
5858 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5859 this_cu->is_debug_types ? "type" : "comp",
9c541725 5860 to_underlying (this_cu->sect_off));
09406207 5861
dee91e82
DE
5862 gdb_assert (this_cu->cu == NULL);
5863
33e80786
DE
5864 abbrev_section = (dwo_file != NULL
5865 ? &dwo_file->sections.abbrev
5866 : get_abbrev_section_for_cu (this_cu));
5867
dee91e82
DE
5868 /* This is cheap if the section is already read in. */
5869 dwarf2_read_section (objfile, section);
5870
5871 init_one_comp_unit (&cu, this_cu);
5872
5873 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5874
9c541725 5875 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5876 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5877 abbrev_section, info_ptr,
43988095
JK
5878 (this_cu->is_debug_types
5879 ? rcuh_kind::TYPE
5880 : rcuh_kind::COMPILE));
dee91e82 5881
1ce1cefd 5882 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5883
5884 /* Skip dummy compilation units. */
5885 if (info_ptr >= begin_info_ptr + this_cu->length
5886 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5887 {
dee91e82 5888 do_cleanups (cleanups);
21b2bd31 5889 return;
93311388 5890 }
72bf9492 5891
dee91e82
DE
5892 dwarf2_read_abbrevs (&cu, abbrev_section);
5893 make_cleanup (dwarf2_free_abbrev_table, &cu);
5894
3019eac3 5895 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5896 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5897
5898 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5899
5900 do_cleanups (cleanups);
5901}
5902
3019eac3
DE
5903/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5904 does not lookup the specified DWO file.
5905 This cannot be used to read DWO files.
dee91e82
DE
5906
5907 THIS_CU->cu is always freed when done.
3019eac3
DE
5908 This is done in order to not leave THIS_CU->cu in a state where we have
5909 to care whether it refers to the "main" CU or the DWO CU.
5910 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5911
5912static void
5913init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5914 die_reader_func_ftype *die_reader_func,
5915 void *data)
5916{
33e80786 5917 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5918}
0018ea6f
DE
5919\f
5920/* Type Unit Groups.
dee91e82 5921
0018ea6f
DE
5922 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5923 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5924 so that all types coming from the same compilation (.o file) are grouped
5925 together. A future step could be to put the types in the same symtab as
5926 the CU the types ultimately came from. */
ff013f42 5927
f4dc4d17
DE
5928static hashval_t
5929hash_type_unit_group (const void *item)
5930{
9a3c8263
SM
5931 const struct type_unit_group *tu_group
5932 = (const struct type_unit_group *) item;
f4dc4d17 5933
094b34ac 5934 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5935}
348e048f
DE
5936
5937static int
f4dc4d17 5938eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5939{
9a3c8263
SM
5940 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5941 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5942
094b34ac 5943 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5944}
348e048f 5945
f4dc4d17
DE
5946/* Allocate a hash table for type unit groups. */
5947
5948static htab_t
5949allocate_type_unit_groups_table (void)
5950{
5951 return htab_create_alloc_ex (3,
5952 hash_type_unit_group,
5953 eq_type_unit_group,
5954 NULL,
5955 &dwarf2_per_objfile->objfile->objfile_obstack,
5956 hashtab_obstack_allocate,
5957 dummy_obstack_deallocate);
5958}
dee91e82 5959
f4dc4d17
DE
5960/* Type units that don't have DW_AT_stmt_list are grouped into their own
5961 partial symtabs. We combine several TUs per psymtab to not let the size
5962 of any one psymtab grow too big. */
5963#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5964#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5965
094b34ac 5966/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5967 Create the type_unit_group object used to hold one or more TUs. */
5968
5969static struct type_unit_group *
094b34ac 5970create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5971{
5972 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5973 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5974 struct type_unit_group *tu_group;
f4dc4d17
DE
5975
5976 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5977 struct type_unit_group);
094b34ac 5978 per_cu = &tu_group->per_cu;
f4dc4d17 5979 per_cu->objfile = objfile;
f4dc4d17 5980
094b34ac
DE
5981 if (dwarf2_per_objfile->using_index)
5982 {
5983 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5984 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5985 }
5986 else
5987 {
9c541725 5988 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
5989 struct partial_symtab *pst;
5990 char *name;
5991
5992 /* Give the symtab a useful name for debug purposes. */
5993 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5994 name = xstrprintf ("<type_units_%d>",
5995 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5996 else
5997 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5998
5999 pst = create_partial_symtab (per_cu, name);
6000 pst->anonymous = 1;
f4dc4d17 6001
094b34ac
DE
6002 xfree (name);
6003 }
f4dc4d17 6004
094b34ac 6005 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6006 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6007
6008 return tu_group;
6009}
6010
094b34ac
DE
6011/* Look up the type_unit_group for type unit CU, and create it if necessary.
6012 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6013
6014static struct type_unit_group *
ff39bb5e 6015get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6016{
6017 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6018 struct type_unit_group *tu_group;
6019 void **slot;
6020 unsigned int line_offset;
6021 struct type_unit_group type_unit_group_for_lookup;
6022
6023 if (dwarf2_per_objfile->type_unit_groups == NULL)
6024 {
6025 dwarf2_per_objfile->type_unit_groups =
6026 allocate_type_unit_groups_table ();
6027 }
6028
6029 /* Do we need to create a new group, or can we use an existing one? */
6030
6031 if (stmt_list)
6032 {
6033 line_offset = DW_UNSND (stmt_list);
6034 ++tu_stats->nr_symtab_sharers;
6035 }
6036 else
6037 {
6038 /* Ugh, no stmt_list. Rare, but we have to handle it.
6039 We can do various things here like create one group per TU or
6040 spread them over multiple groups to split up the expansion work.
6041 To avoid worst case scenarios (too many groups or too large groups)
6042 we, umm, group them in bunches. */
6043 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6044 | (tu_stats->nr_stmt_less_type_units
6045 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6046 ++tu_stats->nr_stmt_less_type_units;
6047 }
6048
094b34ac 6049 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6050 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6051 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6052 &type_unit_group_for_lookup, INSERT);
6053 if (*slot != NULL)
6054 {
9a3c8263 6055 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6056 gdb_assert (tu_group != NULL);
6057 }
6058 else
6059 {
9c541725 6060 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6061 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6062 *slot = tu_group;
6063 ++tu_stats->nr_symtabs;
6064 }
6065
6066 return tu_group;
6067}
0018ea6f
DE
6068\f
6069/* Partial symbol tables. */
6070
6071/* Create a psymtab named NAME and assign it to PER_CU.
6072
6073 The caller must fill in the following details:
6074 dirname, textlow, texthigh. */
6075
6076static struct partial_symtab *
6077create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6078{
6079 struct objfile *objfile = per_cu->objfile;
6080 struct partial_symtab *pst;
6081
18a94d75 6082 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6083 objfile->global_psymbols.next,
6084 objfile->static_psymbols.next);
6085
6086 pst->psymtabs_addrmap_supported = 1;
6087
6088 /* This is the glue that links PST into GDB's symbol API. */
6089 pst->read_symtab_private = per_cu;
6090 pst->read_symtab = dwarf2_read_symtab;
6091 per_cu->v.psymtab = pst;
6092
6093 return pst;
6094}
6095
b93601f3
TT
6096/* The DATA object passed to process_psymtab_comp_unit_reader has this
6097 type. */
6098
6099struct process_psymtab_comp_unit_data
6100{
6101 /* True if we are reading a DW_TAG_partial_unit. */
6102
6103 int want_partial_unit;
6104
6105 /* The "pretend" language that is used if the CU doesn't declare a
6106 language. */
6107
6108 enum language pretend_language;
6109};
6110
0018ea6f
DE
6111/* die_reader_func for process_psymtab_comp_unit. */
6112
6113static void
6114process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6115 const gdb_byte *info_ptr,
0018ea6f
DE
6116 struct die_info *comp_unit_die,
6117 int has_children,
6118 void *data)
6119{
6120 struct dwarf2_cu *cu = reader->cu;
6121 struct objfile *objfile = cu->objfile;
3e29f34a 6122 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6123 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6124 CORE_ADDR baseaddr;
6125 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6126 struct partial_symtab *pst;
3a2b436a 6127 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6128 const char *filename;
9a3c8263
SM
6129 struct process_psymtab_comp_unit_data *info
6130 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6131
b93601f3 6132 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6133 return;
6134
6135 gdb_assert (! per_cu->is_debug_types);
6136
b93601f3 6137 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6138
6139 cu->list_in_scope = &file_symbols;
6140
6141 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6142 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6143 if (filename == NULL)
0018ea6f 6144 filename = "";
0018ea6f
DE
6145
6146 pst = create_partial_symtab (per_cu, filename);
6147
6148 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6149 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6150
6151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6152
6153 dwarf2_find_base_address (comp_unit_die, cu);
6154
6155 /* Possibly set the default values of LOWPC and HIGHPC from
6156 `DW_AT_ranges'. */
3a2b436a
JK
6157 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6158 &best_highpc, cu, pst);
6159 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6160 /* Store the contiguous range if it is not empty; it can be empty for
6161 CUs with no code. */
6162 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6163 gdbarch_adjust_dwarf2_addr (gdbarch,
6164 best_lowpc + baseaddr),
6165 gdbarch_adjust_dwarf2_addr (gdbarch,
6166 best_highpc + baseaddr) - 1,
6167 pst);
0018ea6f
DE
6168
6169 /* Check if comp unit has_children.
6170 If so, read the rest of the partial symbols from this comp unit.
6171 If not, there's no more debug_info for this comp unit. */
6172 if (has_children)
6173 {
6174 struct partial_die_info *first_die;
6175 CORE_ADDR lowpc, highpc;
6176
6177 lowpc = ((CORE_ADDR) -1);
6178 highpc = ((CORE_ADDR) 0);
6179
6180 first_die = load_partial_dies (reader, info_ptr, 1);
6181
6182 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6183 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6184
6185 /* If we didn't find a lowpc, set it to highpc to avoid
6186 complaints from `maint check'. */
6187 if (lowpc == ((CORE_ADDR) -1))
6188 lowpc = highpc;
6189
6190 /* If the compilation unit didn't have an explicit address range,
6191 then use the information extracted from its child dies. */
e385593e 6192 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6193 {
6194 best_lowpc = lowpc;
6195 best_highpc = highpc;
6196 }
6197 }
3e29f34a
MR
6198 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6199 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6200
8763cede 6201 end_psymtab_common (objfile, pst);
0018ea6f
DE
6202
6203 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6204 {
6205 int i;
6206 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6207 struct dwarf2_per_cu_data *iter;
6208
6209 /* Fill in 'dependencies' here; we fill in 'users' in a
6210 post-pass. */
6211 pst->number_of_dependencies = len;
8d749320
SM
6212 pst->dependencies =
6213 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6214 for (i = 0;
6215 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6216 i, iter);
6217 ++i)
6218 pst->dependencies[i] = iter->v.psymtab;
6219
6220 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6221 }
6222
6223 /* Get the list of files included in the current compilation unit,
6224 and build a psymtab for each of them. */
6225 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6226
b4f54984 6227 if (dwarf_read_debug)
0018ea6f
DE
6228 {
6229 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6230
6231 fprintf_unfiltered (gdb_stdlog,
6232 "Psymtab for %s unit @0x%x: %s - %s"
6233 ", %d global, %d static syms\n",
6234 per_cu->is_debug_types ? "type" : "comp",
9c541725 6235 to_underlying (per_cu->sect_off),
0018ea6f
DE
6236 paddress (gdbarch, pst->textlow),
6237 paddress (gdbarch, pst->texthigh),
6238 pst->n_global_syms, pst->n_static_syms);
6239 }
6240}
6241
6242/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6243 Process compilation unit THIS_CU for a psymtab. */
6244
6245static void
6246process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6247 int want_partial_unit,
6248 enum language pretend_language)
0018ea6f 6249{
b93601f3
TT
6250 struct process_psymtab_comp_unit_data info;
6251
0018ea6f
DE
6252 /* If this compilation unit was already read in, free the
6253 cached copy in order to read it in again. This is
6254 necessary because we skipped some symbols when we first
6255 read in the compilation unit (see load_partial_dies).
6256 This problem could be avoided, but the benefit is unclear. */
6257 if (this_cu->cu != NULL)
6258 free_one_cached_comp_unit (this_cu);
6259
6260 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6261 info.want_partial_unit = want_partial_unit;
6262 info.pretend_language = pretend_language;
0018ea6f
DE
6263 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6264 process_psymtab_comp_unit_reader,
b93601f3 6265 &info);
0018ea6f
DE
6266
6267 /* Age out any secondary CUs. */
6268 age_cached_comp_units ();
6269}
f4dc4d17
DE
6270
6271/* Reader function for build_type_psymtabs. */
6272
6273static void
6274build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6275 const gdb_byte *info_ptr,
f4dc4d17
DE
6276 struct die_info *type_unit_die,
6277 int has_children,
6278 void *data)
6279{
6280 struct objfile *objfile = dwarf2_per_objfile->objfile;
6281 struct dwarf2_cu *cu = reader->cu;
6282 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6283 struct signatured_type *sig_type;
f4dc4d17
DE
6284 struct type_unit_group *tu_group;
6285 struct attribute *attr;
6286 struct partial_die_info *first_die;
6287 CORE_ADDR lowpc, highpc;
6288 struct partial_symtab *pst;
6289
6290 gdb_assert (data == NULL);
0186c6a7
DE
6291 gdb_assert (per_cu->is_debug_types);
6292 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6293
6294 if (! has_children)
6295 return;
6296
6297 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6298 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6299
0186c6a7 6300 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6301
6302 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6303 cu->list_in_scope = &file_symbols;
6304 pst = create_partial_symtab (per_cu, "");
6305 pst->anonymous = 1;
6306
6307 first_die = load_partial_dies (reader, info_ptr, 1);
6308
6309 lowpc = (CORE_ADDR) -1;
6310 highpc = (CORE_ADDR) 0;
6311 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6312
8763cede 6313 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6314}
6315
73051182
DE
6316/* Struct used to sort TUs by their abbreviation table offset. */
6317
6318struct tu_abbrev_offset
6319{
6320 struct signatured_type *sig_type;
6321 sect_offset abbrev_offset;
6322};
6323
6324/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6325
6326static int
6327sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6328{
9a3c8263
SM
6329 const struct tu_abbrev_offset * const *a
6330 = (const struct tu_abbrev_offset * const*) ap;
6331 const struct tu_abbrev_offset * const *b
6332 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6333 sect_offset aoff = (*a)->abbrev_offset;
6334 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6335
6336 return (aoff > boff) - (aoff < boff);
6337}
6338
6339/* Efficiently read all the type units.
6340 This does the bulk of the work for build_type_psymtabs.
6341
6342 The efficiency is because we sort TUs by the abbrev table they use and
6343 only read each abbrev table once. In one program there are 200K TUs
6344 sharing 8K abbrev tables.
6345
6346 The main purpose of this function is to support building the
6347 dwarf2_per_objfile->type_unit_groups table.
6348 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6349 can collapse the search space by grouping them by stmt_list.
6350 The savings can be significant, in the same program from above the 200K TUs
6351 share 8K stmt_list tables.
6352
6353 FUNC is expected to call get_type_unit_group, which will create the
6354 struct type_unit_group if necessary and add it to
6355 dwarf2_per_objfile->type_unit_groups. */
6356
6357static void
6358build_type_psymtabs_1 (void)
6359{
73051182
DE
6360 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6361 struct cleanup *cleanups;
6362 struct abbrev_table *abbrev_table;
6363 sect_offset abbrev_offset;
6364 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6365 int i;
6366
6367 /* It's up to the caller to not call us multiple times. */
6368 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6369
6370 if (dwarf2_per_objfile->n_type_units == 0)
6371 return;
6372
6373 /* TUs typically share abbrev tables, and there can be way more TUs than
6374 abbrev tables. Sort by abbrev table to reduce the number of times we
6375 read each abbrev table in.
6376 Alternatives are to punt or to maintain a cache of abbrev tables.
6377 This is simpler and efficient enough for now.
6378
6379 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6380 symtab to use). Typically TUs with the same abbrev offset have the same
6381 stmt_list value too so in practice this should work well.
6382
6383 The basic algorithm here is:
6384
6385 sort TUs by abbrev table
6386 for each TU with same abbrev table:
6387 read abbrev table if first user
6388 read TU top level DIE
6389 [IWBN if DWO skeletons had DW_AT_stmt_list]
6390 call FUNC */
6391
b4f54984 6392 if (dwarf_read_debug)
73051182
DE
6393 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6394
6395 /* Sort in a separate table to maintain the order of all_type_units
6396 for .gdb_index: TU indices directly index all_type_units. */
6397 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6398 dwarf2_per_objfile->n_type_units);
6399 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6400 {
6401 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6402
6403 sorted_by_abbrev[i].sig_type = sig_type;
6404 sorted_by_abbrev[i].abbrev_offset =
6405 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6406 sig_type->per_cu.sect_off);
73051182
DE
6407 }
6408 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6409 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6410 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6411
9c541725 6412 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6413 abbrev_table = NULL;
6414 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6415
6416 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6417 {
6418 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6419
6420 /* Switch to the next abbrev table if necessary. */
6421 if (abbrev_table == NULL
9c541725 6422 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6423 {
6424 if (abbrev_table != NULL)
6425 {
6426 abbrev_table_free (abbrev_table);
6427 /* Reset to NULL in case abbrev_table_read_table throws
6428 an error: abbrev_table_free_cleanup will get called. */
6429 abbrev_table = NULL;
6430 }
6431 abbrev_offset = tu->abbrev_offset;
6432 abbrev_table =
6433 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6434 abbrev_offset);
6435 ++tu_stats->nr_uniq_abbrev_tables;
6436 }
6437
6438 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6439 build_type_psymtabs_reader, NULL);
6440 }
6441
73051182 6442 do_cleanups (cleanups);
6aa5f3a6 6443}
73051182 6444
6aa5f3a6
DE
6445/* Print collected type unit statistics. */
6446
6447static void
6448print_tu_stats (void)
6449{
6450 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6451
6452 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6453 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6454 dwarf2_per_objfile->n_type_units);
6455 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6456 tu_stats->nr_uniq_abbrev_tables);
6457 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6458 tu_stats->nr_symtabs);
6459 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6460 tu_stats->nr_symtab_sharers);
6461 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6462 tu_stats->nr_stmt_less_type_units);
6463 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6464 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6465}
6466
f4dc4d17
DE
6467/* Traversal function for build_type_psymtabs. */
6468
6469static int
6470build_type_psymtab_dependencies (void **slot, void *info)
6471{
6472 struct objfile *objfile = dwarf2_per_objfile->objfile;
6473 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6474 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6475 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6476 int len = VEC_length (sig_type_ptr, tu_group->tus);
6477 struct signatured_type *iter;
f4dc4d17
DE
6478 int i;
6479
6480 gdb_assert (len > 0);
0186c6a7 6481 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6482
6483 pst->number_of_dependencies = len;
8d749320
SM
6484 pst->dependencies =
6485 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6486 for (i = 0;
0186c6a7 6487 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6488 ++i)
6489 {
0186c6a7
DE
6490 gdb_assert (iter->per_cu.is_debug_types);
6491 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6492 iter->type_unit_group = tu_group;
f4dc4d17
DE
6493 }
6494
0186c6a7 6495 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6496
6497 return 1;
6498}
6499
6500/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6501 Build partial symbol tables for the .debug_types comp-units. */
6502
6503static void
6504build_type_psymtabs (struct objfile *objfile)
6505{
0e50663e 6506 if (! create_all_type_units (objfile))
348e048f
DE
6507 return;
6508
73051182 6509 build_type_psymtabs_1 ();
6aa5f3a6 6510}
f4dc4d17 6511
6aa5f3a6
DE
6512/* Traversal function for process_skeletonless_type_unit.
6513 Read a TU in a DWO file and build partial symbols for it. */
6514
6515static int
6516process_skeletonless_type_unit (void **slot, void *info)
6517{
6518 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6519 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6520 struct signatured_type find_entry, *entry;
6521
6522 /* If this TU doesn't exist in the global table, add it and read it in. */
6523
6524 if (dwarf2_per_objfile->signatured_types == NULL)
6525 {
6526 dwarf2_per_objfile->signatured_types
6527 = allocate_signatured_type_table (objfile);
6528 }
6529
6530 find_entry.signature = dwo_unit->signature;
6531 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6532 INSERT);
6533 /* If we've already seen this type there's nothing to do. What's happening
6534 is we're doing our own version of comdat-folding here. */
6535 if (*slot != NULL)
6536 return 1;
6537
6538 /* This does the job that create_all_type_units would have done for
6539 this TU. */
6540 entry = add_type_unit (dwo_unit->signature, slot);
6541 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6542 *slot = entry;
6543
6544 /* This does the job that build_type_psymtabs_1 would have done. */
6545 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6546 build_type_psymtabs_reader, NULL);
6547
6548 return 1;
6549}
6550
6551/* Traversal function for process_skeletonless_type_units. */
6552
6553static int
6554process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6555{
6556 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6557
6558 if (dwo_file->tus != NULL)
6559 {
6560 htab_traverse_noresize (dwo_file->tus,
6561 process_skeletonless_type_unit, info);
6562 }
6563
6564 return 1;
6565}
6566
6567/* Scan all TUs of DWO files, verifying we've processed them.
6568 This is needed in case a TU was emitted without its skeleton.
6569 Note: This can't be done until we know what all the DWO files are. */
6570
6571static void
6572process_skeletonless_type_units (struct objfile *objfile)
6573{
6574 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6575 if (get_dwp_file () == NULL
6576 && dwarf2_per_objfile->dwo_files != NULL)
6577 {
6578 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6579 process_dwo_file_for_skeletonless_type_units,
6580 objfile);
6581 }
348e048f
DE
6582}
6583
60606b2c
TT
6584/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6585
6586static void
6587psymtabs_addrmap_cleanup (void *o)
6588{
9a3c8263 6589 struct objfile *objfile = (struct objfile *) o;
ec61707d 6590
60606b2c
TT
6591 objfile->psymtabs_addrmap = NULL;
6592}
6593
95554aad
TT
6594/* Compute the 'user' field for each psymtab in OBJFILE. */
6595
6596static void
6597set_partial_user (struct objfile *objfile)
6598{
6599 int i;
6600
6601 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6602 {
8832e7e3 6603 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6604 struct partial_symtab *pst = per_cu->v.psymtab;
6605 int j;
6606
36586728
TT
6607 if (pst == NULL)
6608 continue;
6609
95554aad
TT
6610 for (j = 0; j < pst->number_of_dependencies; ++j)
6611 {
6612 /* Set the 'user' field only if it is not already set. */
6613 if (pst->dependencies[j]->user == NULL)
6614 pst->dependencies[j]->user = pst;
6615 }
6616 }
6617}
6618
93311388
DE
6619/* Build the partial symbol table by doing a quick pass through the
6620 .debug_info and .debug_abbrev sections. */
72bf9492 6621
93311388 6622static void
c67a9c90 6623dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6624{
60606b2c
TT
6625 struct cleanup *back_to, *addrmap_cleanup;
6626 struct obstack temp_obstack;
21b2bd31 6627 int i;
93311388 6628
b4f54984 6629 if (dwarf_read_debug)
45cfd468
DE
6630 {
6631 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6632 objfile_name (objfile));
45cfd468
DE
6633 }
6634
98bfdba5
PA
6635 dwarf2_per_objfile->reading_partial_symbols = 1;
6636
be391dca 6637 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6638
93311388
DE
6639 /* Any cached compilation units will be linked by the per-objfile
6640 read_in_chain. Make sure to free them when we're done. */
6641 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6642
348e048f
DE
6643 build_type_psymtabs (objfile);
6644
93311388 6645 create_all_comp_units (objfile);
c906108c 6646
60606b2c
TT
6647 /* Create a temporary address map on a temporary obstack. We later
6648 copy this to the final obstack. */
6649 obstack_init (&temp_obstack);
6650 make_cleanup_obstack_free (&temp_obstack);
6651 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6652 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6653
21b2bd31 6654 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6655 {
8832e7e3 6656 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6657
b93601f3 6658 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6659 }
ff013f42 6660
6aa5f3a6
DE
6661 /* This has to wait until we read the CUs, we need the list of DWOs. */
6662 process_skeletonless_type_units (objfile);
6663
6664 /* Now that all TUs have been processed we can fill in the dependencies. */
6665 if (dwarf2_per_objfile->type_unit_groups != NULL)
6666 {
6667 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6668 build_type_psymtab_dependencies, NULL);
6669 }
6670
b4f54984 6671 if (dwarf_read_debug)
6aa5f3a6
DE
6672 print_tu_stats ();
6673
95554aad
TT
6674 set_partial_user (objfile);
6675
ff013f42
JK
6676 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6677 &objfile->objfile_obstack);
60606b2c 6678 discard_cleanups (addrmap_cleanup);
ff013f42 6679
ae038cb0 6680 do_cleanups (back_to);
45cfd468 6681
b4f54984 6682 if (dwarf_read_debug)
45cfd468 6683 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6684 objfile_name (objfile));
ae038cb0
DJ
6685}
6686
3019eac3 6687/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6688
6689static void
dee91e82 6690load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6691 const gdb_byte *info_ptr,
dee91e82
DE
6692 struct die_info *comp_unit_die,
6693 int has_children,
6694 void *data)
ae038cb0 6695{
dee91e82 6696 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6697
95554aad 6698 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6699
ae038cb0
DJ
6700 /* Check if comp unit has_children.
6701 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6702 If not, there's no more debug_info for this comp unit. */
d85a05f0 6703 if (has_children)
dee91e82
DE
6704 load_partial_dies (reader, info_ptr, 0);
6705}
98bfdba5 6706
dee91e82
DE
6707/* Load the partial DIEs for a secondary CU into memory.
6708 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6709
dee91e82
DE
6710static void
6711load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6712{
f4dc4d17
DE
6713 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6714 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6715}
6716
ae038cb0 6717static void
36586728
TT
6718read_comp_units_from_section (struct objfile *objfile,
6719 struct dwarf2_section_info *section,
6720 unsigned int is_dwz,
6721 int *n_allocated,
6722 int *n_comp_units,
6723 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6724{
d521ce57 6725 const gdb_byte *info_ptr;
a32a8923 6726 bfd *abfd = get_section_bfd_owner (section);
be391dca 6727
b4f54984 6728 if (dwarf_read_debug)
bf6af496 6729 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6730 get_section_name (section),
6731 get_section_file_name (section));
bf6af496 6732
36586728 6733 dwarf2_read_section (objfile, section);
ae038cb0 6734
36586728 6735 info_ptr = section->buffer;
6e70227d 6736
36586728 6737 while (info_ptr < section->buffer + section->size)
ae038cb0 6738 {
c764a876 6739 unsigned int length, initial_length_size;
ae038cb0 6740 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6741
9c541725 6742 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0
DJ
6743
6744 /* Read just enough information to find out where the next
6745 compilation unit is. */
36586728 6746 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6747
6748 /* Save the compilation unit for later lookup. */
8d749320 6749 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0 6750 memset (this_cu, 0, sizeof (*this_cu));
9c541725 6751 this_cu->sect_off = sect_off;
c764a876 6752 this_cu->length = length + initial_length_size;
36586728 6753 this_cu->is_dwz = is_dwz;
9291a0cd 6754 this_cu->objfile = objfile;
8a0459fd 6755 this_cu->section = section;
ae038cb0 6756
36586728 6757 if (*n_comp_units == *n_allocated)
ae038cb0 6758 {
36586728 6759 *n_allocated *= 2;
224c3ddb
SM
6760 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6761 *all_comp_units, *n_allocated);
ae038cb0 6762 }
36586728
TT
6763 (*all_comp_units)[*n_comp_units] = this_cu;
6764 ++*n_comp_units;
ae038cb0
DJ
6765
6766 info_ptr = info_ptr + this_cu->length;
6767 }
36586728
TT
6768}
6769
6770/* Create a list of all compilation units in OBJFILE.
6771 This is only done for -readnow and building partial symtabs. */
6772
6773static void
6774create_all_comp_units (struct objfile *objfile)
6775{
6776 int n_allocated;
6777 int n_comp_units;
6778 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6779 struct dwz_file *dwz;
36586728
TT
6780
6781 n_comp_units = 0;
6782 n_allocated = 10;
8d749320 6783 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6784
6785 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6786 &n_allocated, &n_comp_units, &all_comp_units);
6787
4db1a1dc
TT
6788 dwz = dwarf2_get_dwz_file ();
6789 if (dwz != NULL)
6790 read_comp_units_from_section (objfile, &dwz->info, 1,
6791 &n_allocated, &n_comp_units,
6792 &all_comp_units);
ae038cb0 6793
8d749320
SM
6794 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6795 struct dwarf2_per_cu_data *,
6796 n_comp_units);
ae038cb0
DJ
6797 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6798 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6799 xfree (all_comp_units);
6800 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6801}
6802
5734ee8b 6803/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6804 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6805 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6806 DW_AT_ranges). See the comments of add_partial_subprogram on how
6807 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6808
72bf9492
DJ
6809static void
6810scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6811 CORE_ADDR *highpc, int set_addrmap,
6812 struct dwarf2_cu *cu)
c906108c 6813{
72bf9492 6814 struct partial_die_info *pdi;
c906108c 6815
91c24f0a
DC
6816 /* Now, march along the PDI's, descending into ones which have
6817 interesting children but skipping the children of the other ones,
6818 until we reach the end of the compilation unit. */
c906108c 6819
72bf9492 6820 pdi = first_die;
91c24f0a 6821
72bf9492
DJ
6822 while (pdi != NULL)
6823 {
6824 fixup_partial_die (pdi, cu);
c906108c 6825
f55ee35c 6826 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6827 children, so we need to look at them. Ditto for anonymous
6828 enums. */
933c6fe4 6829
72bf9492 6830 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6831 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6832 || pdi->tag == DW_TAG_imported_unit)
c906108c 6833 {
72bf9492 6834 switch (pdi->tag)
c906108c
SS
6835 {
6836 case DW_TAG_subprogram:
cdc07690 6837 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6838 break;
72929c62 6839 case DW_TAG_constant:
c906108c
SS
6840 case DW_TAG_variable:
6841 case DW_TAG_typedef:
91c24f0a 6842 case DW_TAG_union_type:
72bf9492 6843 if (!pdi->is_declaration)
63d06c5c 6844 {
72bf9492 6845 add_partial_symbol (pdi, cu);
63d06c5c
DC
6846 }
6847 break;
c906108c 6848 case DW_TAG_class_type:
680b30c7 6849 case DW_TAG_interface_type:
c906108c 6850 case DW_TAG_structure_type:
72bf9492 6851 if (!pdi->is_declaration)
c906108c 6852 {
72bf9492 6853 add_partial_symbol (pdi, cu);
c906108c 6854 }
e98c9e7c
TT
6855 if (cu->language == language_rust && pdi->has_children)
6856 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6857 set_addrmap, cu);
c906108c 6858 break;
91c24f0a 6859 case DW_TAG_enumeration_type:
72bf9492
DJ
6860 if (!pdi->is_declaration)
6861 add_partial_enumeration (pdi, cu);
c906108c
SS
6862 break;
6863 case DW_TAG_base_type:
a02abb62 6864 case DW_TAG_subrange_type:
c906108c 6865 /* File scope base type definitions are added to the partial
c5aa993b 6866 symbol table. */
72bf9492 6867 add_partial_symbol (pdi, cu);
c906108c 6868 break;
d9fa45fe 6869 case DW_TAG_namespace:
cdc07690 6870 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6871 break;
5d7cb8df 6872 case DW_TAG_module:
cdc07690 6873 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6874 break;
95554aad
TT
6875 case DW_TAG_imported_unit:
6876 {
6877 struct dwarf2_per_cu_data *per_cu;
6878
f4dc4d17
DE
6879 /* For now we don't handle imported units in type units. */
6880 if (cu->per_cu->is_debug_types)
6881 {
6882 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6883 " supported in type units [in module %s]"),
4262abfb 6884 objfile_name (cu->objfile));
f4dc4d17
DE
6885 }
6886
9c541725 6887 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6888 pdi->is_dwz,
95554aad
TT
6889 cu->objfile);
6890
6891 /* Go read the partial unit, if needed. */
6892 if (per_cu->v.psymtab == NULL)
b93601f3 6893 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6894
f4dc4d17 6895 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6896 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6897 }
6898 break;
74921315
KS
6899 case DW_TAG_imported_declaration:
6900 add_partial_symbol (pdi, cu);
6901 break;
c906108c
SS
6902 default:
6903 break;
6904 }
6905 }
6906
72bf9492
DJ
6907 /* If the die has a sibling, skip to the sibling. */
6908
6909 pdi = pdi->die_sibling;
6910 }
6911}
6912
6913/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6914
72bf9492 6915 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6916 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6917 Enumerators are an exception; they use the scope of their parent
6918 enumeration type, i.e. the name of the enumeration type is not
6919 prepended to the enumerator.
91c24f0a 6920
72bf9492
DJ
6921 There are two complexities. One is DW_AT_specification; in this
6922 case "parent" means the parent of the target of the specification,
6923 instead of the direct parent of the DIE. The other is compilers
6924 which do not emit DW_TAG_namespace; in this case we try to guess
6925 the fully qualified name of structure types from their members'
6926 linkage names. This must be done using the DIE's children rather
6927 than the children of any DW_AT_specification target. We only need
6928 to do this for structures at the top level, i.e. if the target of
6929 any DW_AT_specification (if any; otherwise the DIE itself) does not
6930 have a parent. */
6931
6932/* Compute the scope prefix associated with PDI's parent, in
6933 compilation unit CU. The result will be allocated on CU's
6934 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6935 field. NULL is returned if no prefix is necessary. */
15d034d0 6936static const char *
72bf9492
DJ
6937partial_die_parent_scope (struct partial_die_info *pdi,
6938 struct dwarf2_cu *cu)
6939{
15d034d0 6940 const char *grandparent_scope;
72bf9492 6941 struct partial_die_info *parent, *real_pdi;
91c24f0a 6942
72bf9492
DJ
6943 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6944 then this means the parent of the specification DIE. */
6945
6946 real_pdi = pdi;
72bf9492 6947 while (real_pdi->has_specification)
36586728
TT
6948 real_pdi = find_partial_die (real_pdi->spec_offset,
6949 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6950
6951 parent = real_pdi->die_parent;
6952 if (parent == NULL)
6953 return NULL;
6954
6955 if (parent->scope_set)
6956 return parent->scope;
6957
6958 fixup_partial_die (parent, cu);
6959
10b3939b 6960 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6961
acebe513
UW
6962 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6963 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6964 Work around this problem here. */
6965 if (cu->language == language_cplus
6e70227d 6966 && parent->tag == DW_TAG_namespace
acebe513
UW
6967 && strcmp (parent->name, "::") == 0
6968 && grandparent_scope == NULL)
6969 {
6970 parent->scope = NULL;
6971 parent->scope_set = 1;
6972 return NULL;
6973 }
6974
9c6c53f7
SA
6975 if (pdi->tag == DW_TAG_enumerator)
6976 /* Enumerators should not get the name of the enumeration as a prefix. */
6977 parent->scope = grandparent_scope;
6978 else if (parent->tag == DW_TAG_namespace
f55ee35c 6979 || parent->tag == DW_TAG_module
72bf9492
DJ
6980 || parent->tag == DW_TAG_structure_type
6981 || parent->tag == DW_TAG_class_type
680b30c7 6982 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6983 || parent->tag == DW_TAG_union_type
6984 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6985 {
6986 if (grandparent_scope == NULL)
6987 parent->scope = parent->name;
6988 else
3e43a32a
MS
6989 parent->scope = typename_concat (&cu->comp_unit_obstack,
6990 grandparent_scope,
f55ee35c 6991 parent->name, 0, cu);
72bf9492 6992 }
72bf9492
DJ
6993 else
6994 {
6995 /* FIXME drow/2004-04-01: What should we be doing with
6996 function-local names? For partial symbols, we should probably be
6997 ignoring them. */
6998 complaint (&symfile_complaints,
e2e0b3e5 6999 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7000 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7001 parent->scope = grandparent_scope;
c906108c
SS
7002 }
7003
72bf9492
DJ
7004 parent->scope_set = 1;
7005 return parent->scope;
7006}
7007
7008/* Return the fully scoped name associated with PDI, from compilation unit
7009 CU. The result will be allocated with malloc. */
4568ecf9 7010
72bf9492
DJ
7011static char *
7012partial_die_full_name (struct partial_die_info *pdi,
7013 struct dwarf2_cu *cu)
7014{
15d034d0 7015 const char *parent_scope;
72bf9492 7016
98bfdba5
PA
7017 /* If this is a template instantiation, we can not work out the
7018 template arguments from partial DIEs. So, unfortunately, we have
7019 to go through the full DIEs. At least any work we do building
7020 types here will be reused if full symbols are loaded later. */
7021 if (pdi->has_template_arguments)
7022 {
7023 fixup_partial_die (pdi, cu);
7024
7025 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7026 {
7027 struct die_info *die;
7028 struct attribute attr;
7029 struct dwarf2_cu *ref_cu = cu;
7030
b64f50a1 7031 /* DW_FORM_ref_addr is using section offset. */
b4069958 7032 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7033 attr.form = DW_FORM_ref_addr;
9c541725 7034 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7035 die = follow_die_ref (NULL, &attr, &ref_cu);
7036
7037 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7038 }
7039 }
7040
72bf9492
DJ
7041 parent_scope = partial_die_parent_scope (pdi, cu);
7042 if (parent_scope == NULL)
7043 return NULL;
7044 else
f55ee35c 7045 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7046}
7047
7048static void
72bf9492 7049add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7050{
e7c27a73 7051 struct objfile *objfile = cu->objfile;
3e29f34a 7052 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7053 CORE_ADDR addr = 0;
15d034d0 7054 const char *actual_name = NULL;
e142c38c 7055 CORE_ADDR baseaddr;
15d034d0 7056 char *built_actual_name;
e142c38c
DJ
7057
7058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7059
15d034d0
TT
7060 built_actual_name = partial_die_full_name (pdi, cu);
7061 if (built_actual_name != NULL)
7062 actual_name = built_actual_name;
63d06c5c 7063
72bf9492
DJ
7064 if (actual_name == NULL)
7065 actual_name = pdi->name;
7066
c906108c
SS
7067 switch (pdi->tag)
7068 {
7069 case DW_TAG_subprogram:
3e29f34a 7070 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7071 if (pdi->is_external || cu->language == language_ada)
c906108c 7072 {
2cfa0c8d
JB
7073 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7074 of the global scope. But in Ada, we want to be able to access
7075 nested procedures globally. So all Ada subprograms are stored
7076 in the global scope. */
f47fb265 7077 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7078 built_actual_name != NULL,
f47fb265
MS
7079 VAR_DOMAIN, LOC_BLOCK,
7080 &objfile->global_psymbols,
1762568f 7081 addr, cu->language, objfile);
c906108c
SS
7082 }
7083 else
7084 {
f47fb265 7085 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7086 built_actual_name != NULL,
f47fb265
MS
7087 VAR_DOMAIN, LOC_BLOCK,
7088 &objfile->static_psymbols,
1762568f 7089 addr, cu->language, objfile);
c906108c 7090 }
0c1b455e
TT
7091
7092 if (pdi->main_subprogram && actual_name != NULL)
7093 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7094 break;
72929c62
JB
7095 case DW_TAG_constant:
7096 {
7097 struct psymbol_allocation_list *list;
7098
7099 if (pdi->is_external)
7100 list = &objfile->global_psymbols;
7101 else
7102 list = &objfile->static_psymbols;
f47fb265 7103 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7104 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7105 list, 0, cu->language, objfile);
72929c62
JB
7106 }
7107 break;
c906108c 7108 case DW_TAG_variable:
95554aad
TT
7109 if (pdi->d.locdesc)
7110 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7111
95554aad 7112 if (pdi->d.locdesc
caac4577
JG
7113 && addr == 0
7114 && !dwarf2_per_objfile->has_section_at_zero)
7115 {
7116 /* A global or static variable may also have been stripped
7117 out by the linker if unused, in which case its address
7118 will be nullified; do not add such variables into partial
7119 symbol table then. */
7120 }
7121 else if (pdi->is_external)
c906108c
SS
7122 {
7123 /* Global Variable.
7124 Don't enter into the minimal symbol tables as there is
7125 a minimal symbol table entry from the ELF symbols already.
7126 Enter into partial symbol table if it has a location
7127 descriptor or a type.
7128 If the location descriptor is missing, new_symbol will create
7129 a LOC_UNRESOLVED symbol, the address of the variable will then
7130 be determined from the minimal symbol table whenever the variable
7131 is referenced.
7132 The address for the partial symbol table entry is not
7133 used by GDB, but it comes in handy for debugging partial symbol
7134 table building. */
7135
95554aad 7136 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7137 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7138 built_actual_name != NULL,
f47fb265
MS
7139 VAR_DOMAIN, LOC_STATIC,
7140 &objfile->global_psymbols,
1762568f 7141 addr + baseaddr,
f47fb265 7142 cu->language, objfile);
c906108c
SS
7143 }
7144 else
7145 {
ff908ebf
AW
7146 int has_loc = pdi->d.locdesc != NULL;
7147
7148 /* Static Variable. Skip symbols whose value we cannot know (those
7149 without location descriptors or constant values). */
7150 if (!has_loc && !pdi->has_const_value)
decbce07 7151 {
15d034d0 7152 xfree (built_actual_name);
decbce07
MS
7153 return;
7154 }
ff908ebf 7155
f47fb265 7156 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7157 built_actual_name != NULL,
f47fb265
MS
7158 VAR_DOMAIN, LOC_STATIC,
7159 &objfile->static_psymbols,
ff908ebf 7160 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7161 cu->language, objfile);
c906108c
SS
7162 }
7163 break;
7164 case DW_TAG_typedef:
7165 case DW_TAG_base_type:
a02abb62 7166 case DW_TAG_subrange_type:
38d518c9 7167 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7168 built_actual_name != NULL,
176620f1 7169 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7170 &objfile->static_psymbols,
1762568f 7171 0, cu->language, objfile);
c906108c 7172 break;
74921315 7173 case DW_TAG_imported_declaration:
72bf9492
DJ
7174 case DW_TAG_namespace:
7175 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7176 built_actual_name != NULL,
72bf9492
DJ
7177 VAR_DOMAIN, LOC_TYPEDEF,
7178 &objfile->global_psymbols,
1762568f 7179 0, cu->language, objfile);
72bf9492 7180 break;
530e8392
KB
7181 case DW_TAG_module:
7182 add_psymbol_to_list (actual_name, strlen (actual_name),
7183 built_actual_name != NULL,
7184 MODULE_DOMAIN, LOC_TYPEDEF,
7185 &objfile->global_psymbols,
1762568f 7186 0, cu->language, objfile);
530e8392 7187 break;
c906108c 7188 case DW_TAG_class_type:
680b30c7 7189 case DW_TAG_interface_type:
c906108c
SS
7190 case DW_TAG_structure_type:
7191 case DW_TAG_union_type:
7192 case DW_TAG_enumeration_type:
fa4028e9
JB
7193 /* Skip external references. The DWARF standard says in the section
7194 about "Structure, Union, and Class Type Entries": "An incomplete
7195 structure, union or class type is represented by a structure,
7196 union or class entry that does not have a byte size attribute
7197 and that has a DW_AT_declaration attribute." */
7198 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7199 {
15d034d0 7200 xfree (built_actual_name);
decbce07
MS
7201 return;
7202 }
fa4028e9 7203
63d06c5c
DC
7204 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7205 static vs. global. */
38d518c9 7206 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7207 built_actual_name != NULL,
176620f1 7208 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7209 cu->language == language_cplus
63d06c5c
DC
7210 ? &objfile->global_psymbols
7211 : &objfile->static_psymbols,
1762568f 7212 0, cu->language, objfile);
c906108c 7213
c906108c
SS
7214 break;
7215 case DW_TAG_enumerator:
38d518c9 7216 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7217 built_actual_name != NULL,
176620f1 7218 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7219 cu->language == language_cplus
f6fe98ef
DJ
7220 ? &objfile->global_psymbols
7221 : &objfile->static_psymbols,
1762568f 7222 0, cu->language, objfile);
c906108c
SS
7223 break;
7224 default:
7225 break;
7226 }
5c4e30ca 7227
15d034d0 7228 xfree (built_actual_name);
c906108c
SS
7229}
7230
5c4e30ca
DC
7231/* Read a partial die corresponding to a namespace; also, add a symbol
7232 corresponding to that namespace to the symbol table. NAMESPACE is
7233 the name of the enclosing namespace. */
91c24f0a 7234
72bf9492
DJ
7235static void
7236add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7237 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7238 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7239{
72bf9492 7240 /* Add a symbol for the namespace. */
e7c27a73 7241
72bf9492 7242 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7243
7244 /* Now scan partial symbols in that namespace. */
7245
91c24f0a 7246 if (pdi->has_children)
cdc07690 7247 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7248}
7249
5d7cb8df
JK
7250/* Read a partial die corresponding to a Fortran module. */
7251
7252static void
7253add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7254 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7255{
530e8392
KB
7256 /* Add a symbol for the namespace. */
7257
7258 add_partial_symbol (pdi, cu);
7259
f55ee35c 7260 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7261
7262 if (pdi->has_children)
cdc07690 7263 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7264}
7265
bc30ff58
JB
7266/* Read a partial die corresponding to a subprogram and create a partial
7267 symbol for that subprogram. When the CU language allows it, this
7268 routine also defines a partial symbol for each nested subprogram
cdc07690 7269 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7270 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7271 and highest PC values found in PDI.
6e70227d 7272
cdc07690
YQ
7273 PDI may also be a lexical block, in which case we simply search
7274 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7275 Again, this is only performed when the CU language allows this
7276 type of definitions. */
7277
7278static void
7279add_partial_subprogram (struct partial_die_info *pdi,
7280 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7281 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7282{
7283 if (pdi->tag == DW_TAG_subprogram)
7284 {
7285 if (pdi->has_pc_info)
7286 {
7287 if (pdi->lowpc < *lowpc)
7288 *lowpc = pdi->lowpc;
7289 if (pdi->highpc > *highpc)
7290 *highpc = pdi->highpc;
cdc07690 7291 if (set_addrmap)
5734ee8b 7292 {
5734ee8b 7293 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7295 CORE_ADDR baseaddr;
7296 CORE_ADDR highpc;
7297 CORE_ADDR lowpc;
5734ee8b
DJ
7298
7299 baseaddr = ANOFFSET (objfile->section_offsets,
7300 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7301 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7302 pdi->lowpc + baseaddr);
7303 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7304 pdi->highpc + baseaddr);
7305 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7306 cu->per_cu->v.psymtab);
5734ee8b 7307 }
481860b3
GB
7308 }
7309
7310 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7311 {
bc30ff58 7312 if (!pdi->is_declaration)
e8d05480
JB
7313 /* Ignore subprogram DIEs that do not have a name, they are
7314 illegal. Do not emit a complaint at this point, we will
7315 do so when we convert this psymtab into a symtab. */
7316 if (pdi->name)
7317 add_partial_symbol (pdi, cu);
bc30ff58
JB
7318 }
7319 }
6e70227d 7320
bc30ff58
JB
7321 if (! pdi->has_children)
7322 return;
7323
7324 if (cu->language == language_ada)
7325 {
7326 pdi = pdi->die_child;
7327 while (pdi != NULL)
7328 {
7329 fixup_partial_die (pdi, cu);
7330 if (pdi->tag == DW_TAG_subprogram
7331 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7332 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7333 pdi = pdi->die_sibling;
7334 }
7335 }
7336}
7337
91c24f0a
DC
7338/* Read a partial die corresponding to an enumeration type. */
7339
72bf9492
DJ
7340static void
7341add_partial_enumeration (struct partial_die_info *enum_pdi,
7342 struct dwarf2_cu *cu)
91c24f0a 7343{
72bf9492 7344 struct partial_die_info *pdi;
91c24f0a
DC
7345
7346 if (enum_pdi->name != NULL)
72bf9492
DJ
7347 add_partial_symbol (enum_pdi, cu);
7348
7349 pdi = enum_pdi->die_child;
7350 while (pdi)
91c24f0a 7351 {
72bf9492 7352 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7353 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7354 else
72bf9492
DJ
7355 add_partial_symbol (pdi, cu);
7356 pdi = pdi->die_sibling;
91c24f0a 7357 }
91c24f0a
DC
7358}
7359
6caca83c
CC
7360/* Return the initial uleb128 in the die at INFO_PTR. */
7361
7362static unsigned int
d521ce57 7363peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7364{
7365 unsigned int bytes_read;
7366
7367 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7368}
7369
4bb7a0a7
DJ
7370/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7371 Return the corresponding abbrev, or NULL if the number is zero (indicating
7372 an empty DIE). In either case *BYTES_READ will be set to the length of
7373 the initial number. */
7374
7375static struct abbrev_info *
d521ce57 7376peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7377 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7378{
7379 bfd *abfd = cu->objfile->obfd;
7380 unsigned int abbrev_number;
7381 struct abbrev_info *abbrev;
7382
7383 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7384
7385 if (abbrev_number == 0)
7386 return NULL;
7387
433df2d4 7388 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7389 if (!abbrev)
7390 {
422b9917
DE
7391 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7392 " at offset 0x%x [in module %s]"),
7393 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7394 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7395 }
7396
7397 return abbrev;
7398}
7399
93311388
DE
7400/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7401 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7402 DIE. Any children of the skipped DIEs will also be skipped. */
7403
d521ce57
TT
7404static const gdb_byte *
7405skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7406{
dee91e82 7407 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7408 struct abbrev_info *abbrev;
7409 unsigned int bytes_read;
7410
7411 while (1)
7412 {
7413 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7414 if (abbrev == NULL)
7415 return info_ptr + bytes_read;
7416 else
dee91e82 7417 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7418 }
7419}
7420
93311388
DE
7421/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7422 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7423 abbrev corresponding to that skipped uleb128 should be passed in
7424 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7425 children. */
7426
d521ce57
TT
7427static const gdb_byte *
7428skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7429 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7430{
7431 unsigned int bytes_read;
7432 struct attribute attr;
dee91e82
DE
7433 bfd *abfd = reader->abfd;
7434 struct dwarf2_cu *cu = reader->cu;
d521ce57 7435 const gdb_byte *buffer = reader->buffer;
f664829e 7436 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7437 unsigned int form, i;
7438
7439 for (i = 0; i < abbrev->num_attrs; i++)
7440 {
7441 /* The only abbrev we care about is DW_AT_sibling. */
7442 if (abbrev->attrs[i].name == DW_AT_sibling)
7443 {
dee91e82 7444 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7445 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7446 complaint (&symfile_complaints,
7447 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7448 else
b9502d3f 7449 {
9c541725
PA
7450 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7451 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7452
7453 if (sibling_ptr < info_ptr)
7454 complaint (&symfile_complaints,
7455 _("DW_AT_sibling points backwards"));
22869d73
KS
7456 else if (sibling_ptr > reader->buffer_end)
7457 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7458 else
7459 return sibling_ptr;
7460 }
4bb7a0a7
DJ
7461 }
7462
7463 /* If it isn't DW_AT_sibling, skip this attribute. */
7464 form = abbrev->attrs[i].form;
7465 skip_attribute:
7466 switch (form)
7467 {
4bb7a0a7 7468 case DW_FORM_ref_addr:
ae411497
TT
7469 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7470 and later it is offset sized. */
7471 if (cu->header.version == 2)
7472 info_ptr += cu->header.addr_size;
7473 else
7474 info_ptr += cu->header.offset_size;
7475 break;
36586728
TT
7476 case DW_FORM_GNU_ref_alt:
7477 info_ptr += cu->header.offset_size;
7478 break;
ae411497 7479 case DW_FORM_addr:
4bb7a0a7
DJ
7480 info_ptr += cu->header.addr_size;
7481 break;
7482 case DW_FORM_data1:
7483 case DW_FORM_ref1:
7484 case DW_FORM_flag:
7485 info_ptr += 1;
7486 break;
2dc7f7b3 7487 case DW_FORM_flag_present:
43988095 7488 case DW_FORM_implicit_const:
2dc7f7b3 7489 break;
4bb7a0a7
DJ
7490 case DW_FORM_data2:
7491 case DW_FORM_ref2:
7492 info_ptr += 2;
7493 break;
7494 case DW_FORM_data4:
7495 case DW_FORM_ref4:
7496 info_ptr += 4;
7497 break;
7498 case DW_FORM_data8:
7499 case DW_FORM_ref8:
55f1336d 7500 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7501 info_ptr += 8;
7502 break;
0224619f
JK
7503 case DW_FORM_data16:
7504 info_ptr += 16;
7505 break;
4bb7a0a7 7506 case DW_FORM_string:
9b1c24c8 7507 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7508 info_ptr += bytes_read;
7509 break;
2dc7f7b3 7510 case DW_FORM_sec_offset:
4bb7a0a7 7511 case DW_FORM_strp:
36586728 7512 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7513 info_ptr += cu->header.offset_size;
7514 break;
2dc7f7b3 7515 case DW_FORM_exprloc:
4bb7a0a7
DJ
7516 case DW_FORM_block:
7517 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7518 info_ptr += bytes_read;
7519 break;
7520 case DW_FORM_block1:
7521 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7522 break;
7523 case DW_FORM_block2:
7524 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7525 break;
7526 case DW_FORM_block4:
7527 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7528 break;
7529 case DW_FORM_sdata:
7530 case DW_FORM_udata:
7531 case DW_FORM_ref_udata:
3019eac3
DE
7532 case DW_FORM_GNU_addr_index:
7533 case DW_FORM_GNU_str_index:
d521ce57 7534 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7535 break;
7536 case DW_FORM_indirect:
7537 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7538 info_ptr += bytes_read;
7539 /* We need to continue parsing from here, so just go back to
7540 the top. */
7541 goto skip_attribute;
7542
7543 default:
3e43a32a
MS
7544 error (_("Dwarf Error: Cannot handle %s "
7545 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7546 dwarf_form_name (form),
7547 bfd_get_filename (abfd));
7548 }
7549 }
7550
7551 if (abbrev->has_children)
dee91e82 7552 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7553 else
7554 return info_ptr;
7555}
7556
93311388 7557/* Locate ORIG_PDI's sibling.
dee91e82 7558 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7559
d521ce57 7560static const gdb_byte *
dee91e82
DE
7561locate_pdi_sibling (const struct die_reader_specs *reader,
7562 struct partial_die_info *orig_pdi,
d521ce57 7563 const gdb_byte *info_ptr)
91c24f0a
DC
7564{
7565 /* Do we know the sibling already? */
72bf9492 7566
91c24f0a
DC
7567 if (orig_pdi->sibling)
7568 return orig_pdi->sibling;
7569
7570 /* Are there any children to deal with? */
7571
7572 if (!orig_pdi->has_children)
7573 return info_ptr;
7574
4bb7a0a7 7575 /* Skip the children the long way. */
91c24f0a 7576
dee91e82 7577 return skip_children (reader, info_ptr);
91c24f0a
DC
7578}
7579
257e7a09 7580/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7581 not NULL. */
c906108c
SS
7582
7583static void
257e7a09
YQ
7584dwarf2_read_symtab (struct partial_symtab *self,
7585 struct objfile *objfile)
c906108c 7586{
257e7a09 7587 if (self->readin)
c906108c 7588 {
442e4d9c 7589 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7590 self->filename);
442e4d9c
YQ
7591 }
7592 else
7593 {
7594 if (info_verbose)
c906108c 7595 {
442e4d9c 7596 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7597 self->filename);
442e4d9c 7598 gdb_flush (gdb_stdout);
c906108c 7599 }
c906108c 7600
442e4d9c 7601 /* Restore our global data. */
9a3c8263
SM
7602 dwarf2_per_objfile
7603 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7604 dwarf2_objfile_data_key);
10b3939b 7605
442e4d9c
YQ
7606 /* If this psymtab is constructed from a debug-only objfile, the
7607 has_section_at_zero flag will not necessarily be correct. We
7608 can get the correct value for this flag by looking at the data
7609 associated with the (presumably stripped) associated objfile. */
7610 if (objfile->separate_debug_objfile_backlink)
7611 {
7612 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7613 = ((struct dwarf2_per_objfile *)
7614 objfile_data (objfile->separate_debug_objfile_backlink,
7615 dwarf2_objfile_data_key));
9a619af0 7616
442e4d9c
YQ
7617 dwarf2_per_objfile->has_section_at_zero
7618 = dpo_backlink->has_section_at_zero;
7619 }
b2ab525c 7620
442e4d9c 7621 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7622
257e7a09 7623 psymtab_to_symtab_1 (self);
c906108c 7624
442e4d9c
YQ
7625 /* Finish up the debug error message. */
7626 if (info_verbose)
7627 printf_filtered (_("done.\n"));
c906108c 7628 }
95554aad
TT
7629
7630 process_cu_includes ();
c906108c 7631}
9cdd5dbd
DE
7632\f
7633/* Reading in full CUs. */
c906108c 7634
10b3939b
DJ
7635/* Add PER_CU to the queue. */
7636
7637static void
95554aad
TT
7638queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7639 enum language pretend_language)
10b3939b
DJ
7640{
7641 struct dwarf2_queue_item *item;
7642
7643 per_cu->queued = 1;
8d749320 7644 item = XNEW (struct dwarf2_queue_item);
10b3939b 7645 item->per_cu = per_cu;
95554aad 7646 item->pretend_language = pretend_language;
10b3939b
DJ
7647 item->next = NULL;
7648
7649 if (dwarf2_queue == NULL)
7650 dwarf2_queue = item;
7651 else
7652 dwarf2_queue_tail->next = item;
7653
7654 dwarf2_queue_tail = item;
7655}
7656
89e63ee4
DE
7657/* If PER_CU is not yet queued, add it to the queue.
7658 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7659 dependency.
0907af0c 7660 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7661 meaning either PER_CU is already queued or it is already loaded.
7662
7663 N.B. There is an invariant here that if a CU is queued then it is loaded.
7664 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7665
7666static int
89e63ee4 7667maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7668 struct dwarf2_per_cu_data *per_cu,
7669 enum language pretend_language)
7670{
7671 /* We may arrive here during partial symbol reading, if we need full
7672 DIEs to process an unusual case (e.g. template arguments). Do
7673 not queue PER_CU, just tell our caller to load its DIEs. */
7674 if (dwarf2_per_objfile->reading_partial_symbols)
7675 {
7676 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7677 return 1;
7678 return 0;
7679 }
7680
7681 /* Mark the dependence relation so that we don't flush PER_CU
7682 too early. */
89e63ee4
DE
7683 if (dependent_cu != NULL)
7684 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7685
7686 /* If it's already on the queue, we have nothing to do. */
7687 if (per_cu->queued)
7688 return 0;
7689
7690 /* If the compilation unit is already loaded, just mark it as
7691 used. */
7692 if (per_cu->cu != NULL)
7693 {
7694 per_cu->cu->last_used = 0;
7695 return 0;
7696 }
7697
7698 /* Add it to the queue. */
7699 queue_comp_unit (per_cu, pretend_language);
7700
7701 return 1;
7702}
7703
10b3939b
DJ
7704/* Process the queue. */
7705
7706static void
a0f42c21 7707process_queue (void)
10b3939b
DJ
7708{
7709 struct dwarf2_queue_item *item, *next_item;
7710
b4f54984 7711 if (dwarf_read_debug)
45cfd468
DE
7712 {
7713 fprintf_unfiltered (gdb_stdlog,
7714 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7715 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7716 }
7717
03dd20cc
DJ
7718 /* The queue starts out with one item, but following a DIE reference
7719 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7720 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7721 {
cc12ce38
DE
7722 if ((dwarf2_per_objfile->using_index
7723 ? !item->per_cu->v.quick->compunit_symtab
7724 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7725 /* Skip dummy CUs. */
7726 && item->per_cu->cu != NULL)
f4dc4d17
DE
7727 {
7728 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7729 unsigned int debug_print_threshold;
247f5c4f 7730 char buf[100];
f4dc4d17 7731
247f5c4f 7732 if (per_cu->is_debug_types)
f4dc4d17 7733 {
247f5c4f
DE
7734 struct signatured_type *sig_type =
7735 (struct signatured_type *) per_cu;
7736
7737 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7738 hex_string (sig_type->signature),
9c541725 7739 to_underlying (per_cu->sect_off));
73be47f5
DE
7740 /* There can be 100s of TUs.
7741 Only print them in verbose mode. */
7742 debug_print_threshold = 2;
f4dc4d17 7743 }
247f5c4f 7744 else
73be47f5 7745 {
9c541725
PA
7746 sprintf (buf, "CU at offset 0x%x",
7747 to_underlying (per_cu->sect_off));
73be47f5
DE
7748 debug_print_threshold = 1;
7749 }
247f5c4f 7750
b4f54984 7751 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7752 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7753
7754 if (per_cu->is_debug_types)
7755 process_full_type_unit (per_cu, item->pretend_language);
7756 else
7757 process_full_comp_unit (per_cu, item->pretend_language);
7758
b4f54984 7759 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7760 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7761 }
10b3939b
DJ
7762
7763 item->per_cu->queued = 0;
7764 next_item = item->next;
7765 xfree (item);
7766 }
7767
7768 dwarf2_queue_tail = NULL;
45cfd468 7769
b4f54984 7770 if (dwarf_read_debug)
45cfd468
DE
7771 {
7772 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7773 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7774 }
10b3939b
DJ
7775}
7776
7777/* Free all allocated queue entries. This function only releases anything if
7778 an error was thrown; if the queue was processed then it would have been
7779 freed as we went along. */
7780
7781static void
7782dwarf2_release_queue (void *dummy)
7783{
7784 struct dwarf2_queue_item *item, *last;
7785
7786 item = dwarf2_queue;
7787 while (item)
7788 {
7789 /* Anything still marked queued is likely to be in an
7790 inconsistent state, so discard it. */
7791 if (item->per_cu->queued)
7792 {
7793 if (item->per_cu->cu != NULL)
dee91e82 7794 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7795 item->per_cu->queued = 0;
7796 }
7797
7798 last = item;
7799 item = item->next;
7800 xfree (last);
7801 }
7802
7803 dwarf2_queue = dwarf2_queue_tail = NULL;
7804}
7805
7806/* Read in full symbols for PST, and anything it depends on. */
7807
c906108c 7808static void
fba45db2 7809psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7810{
10b3939b 7811 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7812 int i;
7813
95554aad
TT
7814 if (pst->readin)
7815 return;
7816
aaa75496 7817 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7818 if (!pst->dependencies[i]->readin
7819 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7820 {
7821 /* Inform about additional files that need to be read in. */
7822 if (info_verbose)
7823 {
a3f17187 7824 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7825 fputs_filtered (" ", gdb_stdout);
7826 wrap_here ("");
7827 fputs_filtered ("and ", gdb_stdout);
7828 wrap_here ("");
7829 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7830 wrap_here (""); /* Flush output. */
aaa75496
JB
7831 gdb_flush (gdb_stdout);
7832 }
7833 psymtab_to_symtab_1 (pst->dependencies[i]);
7834 }
7835
9a3c8263 7836 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7837
7838 if (per_cu == NULL)
aaa75496
JB
7839 {
7840 /* It's an include file, no symbols to read for it.
7841 Everything is in the parent symtab. */
7842 pst->readin = 1;
7843 return;
7844 }
c906108c 7845
a0f42c21 7846 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7847}
7848
dee91e82
DE
7849/* Trivial hash function for die_info: the hash value of a DIE
7850 is its offset in .debug_info for this objfile. */
10b3939b 7851
dee91e82
DE
7852static hashval_t
7853die_hash (const void *item)
10b3939b 7854{
9a3c8263 7855 const struct die_info *die = (const struct die_info *) item;
6502dd73 7856
9c541725 7857 return to_underlying (die->sect_off);
dee91e82 7858}
63d06c5c 7859
dee91e82
DE
7860/* Trivial comparison function for die_info structures: two DIEs
7861 are equal if they have the same offset. */
98bfdba5 7862
dee91e82
DE
7863static int
7864die_eq (const void *item_lhs, const void *item_rhs)
7865{
9a3c8263
SM
7866 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7867 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7868
9c541725 7869 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7870}
c906108c 7871
dee91e82
DE
7872/* die_reader_func for load_full_comp_unit.
7873 This is identical to read_signatured_type_reader,
7874 but is kept separate for now. */
c906108c 7875
dee91e82
DE
7876static void
7877load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7878 const gdb_byte *info_ptr,
dee91e82
DE
7879 struct die_info *comp_unit_die,
7880 int has_children,
7881 void *data)
7882{
7883 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7884 enum language *language_ptr = (enum language *) data;
6caca83c 7885
dee91e82
DE
7886 gdb_assert (cu->die_hash == NULL);
7887 cu->die_hash =
7888 htab_create_alloc_ex (cu->header.length / 12,
7889 die_hash,
7890 die_eq,
7891 NULL,
7892 &cu->comp_unit_obstack,
7893 hashtab_obstack_allocate,
7894 dummy_obstack_deallocate);
e142c38c 7895
dee91e82
DE
7896 if (has_children)
7897 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7898 &info_ptr, comp_unit_die);
7899 cu->dies = comp_unit_die;
7900 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7901
7902 /* We try not to read any attributes in this function, because not
9cdd5dbd 7903 all CUs needed for references have been loaded yet, and symbol
10b3939b 7904 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7905 or we won't be able to build types correctly.
7906 Similarly, if we do not read the producer, we can not apply
7907 producer-specific interpretation. */
95554aad 7908 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7909}
10b3939b 7910
dee91e82 7911/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7912
dee91e82 7913static void
95554aad
TT
7914load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7915 enum language pretend_language)
dee91e82 7916{
3019eac3 7917 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7918
f4dc4d17
DE
7919 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7920 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7921}
7922
3da10d80
KS
7923/* Add a DIE to the delayed physname list. */
7924
7925static void
7926add_to_method_list (struct type *type, int fnfield_index, int index,
7927 const char *name, struct die_info *die,
7928 struct dwarf2_cu *cu)
7929{
7930 struct delayed_method_info mi;
7931 mi.type = type;
7932 mi.fnfield_index = fnfield_index;
7933 mi.index = index;
7934 mi.name = name;
7935 mi.die = die;
7936 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7937}
7938
7939/* A cleanup for freeing the delayed method list. */
7940
7941static void
7942free_delayed_list (void *ptr)
7943{
7944 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7945 if (cu->method_list != NULL)
7946 {
7947 VEC_free (delayed_method_info, cu->method_list);
7948 cu->method_list = NULL;
7949 }
7950}
7951
7952/* Compute the physnames of any methods on the CU's method list.
7953
7954 The computation of method physnames is delayed in order to avoid the
7955 (bad) condition that one of the method's formal parameters is of an as yet
7956 incomplete type. */
7957
7958static void
7959compute_delayed_physnames (struct dwarf2_cu *cu)
7960{
7961 int i;
7962 struct delayed_method_info *mi;
7963 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7964 {
1d06ead6 7965 const char *physname;
3da10d80
KS
7966 struct fn_fieldlist *fn_flp
7967 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7968 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7969 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7970 = physname ? physname : "";
3da10d80
KS
7971 }
7972}
7973
a766d390
DE
7974/* Go objects should be embedded in a DW_TAG_module DIE,
7975 and it's not clear if/how imported objects will appear.
7976 To keep Go support simple until that's worked out,
7977 go back through what we've read and create something usable.
7978 We could do this while processing each DIE, and feels kinda cleaner,
7979 but that way is more invasive.
7980 This is to, for example, allow the user to type "p var" or "b main"
7981 without having to specify the package name, and allow lookups
7982 of module.object to work in contexts that use the expression
7983 parser. */
7984
7985static void
7986fixup_go_packaging (struct dwarf2_cu *cu)
7987{
7988 char *package_name = NULL;
7989 struct pending *list;
7990 int i;
7991
7992 for (list = global_symbols; list != NULL; list = list->next)
7993 {
7994 for (i = 0; i < list->nsyms; ++i)
7995 {
7996 struct symbol *sym = list->symbol[i];
7997
7998 if (SYMBOL_LANGUAGE (sym) == language_go
7999 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8000 {
8001 char *this_package_name = go_symbol_package_name (sym);
8002
8003 if (this_package_name == NULL)
8004 continue;
8005 if (package_name == NULL)
8006 package_name = this_package_name;
8007 else
8008 {
8009 if (strcmp (package_name, this_package_name) != 0)
8010 complaint (&symfile_complaints,
8011 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8012 (symbol_symtab (sym) != NULL
8013 ? symtab_to_filename_for_display
8014 (symbol_symtab (sym))
4262abfb 8015 : objfile_name (cu->objfile)),
a766d390
DE
8016 this_package_name, package_name);
8017 xfree (this_package_name);
8018 }
8019 }
8020 }
8021 }
8022
8023 if (package_name != NULL)
8024 {
8025 struct objfile *objfile = cu->objfile;
34a68019 8026 const char *saved_package_name
224c3ddb
SM
8027 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8028 package_name,
8029 strlen (package_name));
19f392bc
UW
8030 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8031 saved_package_name);
a766d390
DE
8032 struct symbol *sym;
8033
8034 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8035
e623cf5d 8036 sym = allocate_symbol (objfile);
f85f34ed 8037 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8038 SYMBOL_SET_NAMES (sym, saved_package_name,
8039 strlen (saved_package_name), 0, objfile);
a766d390
DE
8040 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8041 e.g., "main" finds the "main" module and not C's main(). */
8042 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8044 SYMBOL_TYPE (sym) = type;
8045
8046 add_symbol_to_list (sym, &global_symbols);
8047
8048 xfree (package_name);
8049 }
8050}
8051
95554aad
TT
8052/* Return the symtab for PER_CU. This works properly regardless of
8053 whether we're using the index or psymtabs. */
8054
43f3e411
DE
8055static struct compunit_symtab *
8056get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8057{
8058 return (dwarf2_per_objfile->using_index
43f3e411
DE
8059 ? per_cu->v.quick->compunit_symtab
8060 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8061}
8062
8063/* A helper function for computing the list of all symbol tables
8064 included by PER_CU. */
8065
8066static void
43f3e411 8067recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8068 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8069 struct dwarf2_per_cu_data *per_cu,
43f3e411 8070 struct compunit_symtab *immediate_parent)
95554aad
TT
8071{
8072 void **slot;
8073 int ix;
43f3e411 8074 struct compunit_symtab *cust;
95554aad
TT
8075 struct dwarf2_per_cu_data *iter;
8076
8077 slot = htab_find_slot (all_children, per_cu, INSERT);
8078 if (*slot != NULL)
8079 {
8080 /* This inclusion and its children have been processed. */
8081 return;
8082 }
8083
8084 *slot = per_cu;
8085 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8086 cust = get_compunit_symtab (per_cu);
8087 if (cust != NULL)
ec94af83
DE
8088 {
8089 /* If this is a type unit only add its symbol table if we haven't
8090 seen it yet (type unit per_cu's can share symtabs). */
8091 if (per_cu->is_debug_types)
8092 {
43f3e411 8093 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8094 if (*slot == NULL)
8095 {
43f3e411
DE
8096 *slot = cust;
8097 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8098 if (cust->user == NULL)
8099 cust->user = immediate_parent;
ec94af83
DE
8100 }
8101 }
8102 else
f9125b6c 8103 {
43f3e411
DE
8104 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8105 if (cust->user == NULL)
8106 cust->user = immediate_parent;
f9125b6c 8107 }
ec94af83 8108 }
95554aad
TT
8109
8110 for (ix = 0;
796a7ff8 8111 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8112 ++ix)
ec94af83
DE
8113 {
8114 recursively_compute_inclusions (result, all_children,
43f3e411 8115 all_type_symtabs, iter, cust);
ec94af83 8116 }
95554aad
TT
8117}
8118
43f3e411 8119/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8120 PER_CU. */
8121
8122static void
43f3e411 8123compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8124{
f4dc4d17
DE
8125 gdb_assert (! per_cu->is_debug_types);
8126
796a7ff8 8127 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8128 {
8129 int ix, len;
ec94af83 8130 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8131 struct compunit_symtab *compunit_symtab_iter;
8132 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8133 htab_t all_children, all_type_symtabs;
43f3e411 8134 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8135
8136 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8137 if (cust == NULL)
95554aad
TT
8138 return;
8139
8140 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8141 NULL, xcalloc, xfree);
ec94af83
DE
8142 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8143 NULL, xcalloc, xfree);
95554aad
TT
8144
8145 for (ix = 0;
796a7ff8 8146 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8147 ix, per_cu_iter);
95554aad 8148 ++ix)
ec94af83
DE
8149 {
8150 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8151 all_type_symtabs, per_cu_iter,
43f3e411 8152 cust);
ec94af83 8153 }
95554aad 8154
ec94af83 8155 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8156 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8157 cust->includes
8d749320
SM
8158 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8159 struct compunit_symtab *, len + 1);
95554aad 8160 for (ix = 0;
43f3e411
DE
8161 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8162 compunit_symtab_iter);
95554aad 8163 ++ix)
43f3e411
DE
8164 cust->includes[ix] = compunit_symtab_iter;
8165 cust->includes[len] = NULL;
95554aad 8166
43f3e411 8167 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8168 htab_delete (all_children);
ec94af83 8169 htab_delete (all_type_symtabs);
95554aad
TT
8170 }
8171}
8172
8173/* Compute the 'includes' field for the symtabs of all the CUs we just
8174 read. */
8175
8176static void
8177process_cu_includes (void)
8178{
8179 int ix;
8180 struct dwarf2_per_cu_data *iter;
8181
8182 for (ix = 0;
8183 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8184 ix, iter);
8185 ++ix)
f4dc4d17
DE
8186 {
8187 if (! iter->is_debug_types)
43f3e411 8188 compute_compunit_symtab_includes (iter);
f4dc4d17 8189 }
95554aad
TT
8190
8191 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8192}
8193
9cdd5dbd 8194/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8195 already been loaded into memory. */
8196
8197static void
95554aad
TT
8198process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8199 enum language pretend_language)
10b3939b 8200{
10b3939b 8201 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8202 struct objfile *objfile = per_cu->objfile;
3e29f34a 8203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8204 CORE_ADDR lowpc, highpc;
43f3e411 8205 struct compunit_symtab *cust;
3da10d80 8206 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8207 CORE_ADDR baseaddr;
4359dff1 8208 struct block *static_block;
3e29f34a 8209 CORE_ADDR addr;
10b3939b
DJ
8210
8211 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8212
10b3939b
DJ
8213 buildsym_init ();
8214 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8215 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8216
8217 cu->list_in_scope = &file_symbols;
c906108c 8218
95554aad
TT
8219 cu->language = pretend_language;
8220 cu->language_defn = language_def (cu->language);
8221
c906108c 8222 /* Do line number decoding in read_file_scope () */
10b3939b 8223 process_die (cu->dies, cu);
c906108c 8224
a766d390
DE
8225 /* For now fudge the Go package. */
8226 if (cu->language == language_go)
8227 fixup_go_packaging (cu);
8228
3da10d80
KS
8229 /* Now that we have processed all the DIEs in the CU, all the types
8230 should be complete, and it should now be safe to compute all of the
8231 physnames. */
8232 compute_delayed_physnames (cu);
8233 do_cleanups (delayed_list_cleanup);
8234
fae299cd
DC
8235 /* Some compilers don't define a DW_AT_high_pc attribute for the
8236 compilation unit. If the DW_AT_high_pc is missing, synthesize
8237 it, by scanning the DIE's below the compilation unit. */
10b3939b 8238 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8239
3e29f34a
MR
8240 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8241 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8242
8243 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8244 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8245 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8246 addrmap to help ensure it has an accurate map of pc values belonging to
8247 this comp unit. */
8248 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8249
43f3e411
DE
8250 cust = end_symtab_from_static_block (static_block,
8251 SECT_OFF_TEXT (objfile), 0);
c906108c 8252
43f3e411 8253 if (cust != NULL)
c906108c 8254 {
df15bd07 8255 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8256
8be455d7
JK
8257 /* Set symtab language to language from DW_AT_language. If the
8258 compilation is from a C file generated by language preprocessors, do
8259 not set the language if it was already deduced by start_subfile. */
43f3e411 8260 if (!(cu->language == language_c
40e3ad0e 8261 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8262 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8263
8264 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8265 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8266 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8267 there were bugs in prologue debug info, fixed later in GCC-4.5
8268 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8269
8270 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8271 needed, it would be wrong due to missing DW_AT_producer there.
8272
8273 Still one can confuse GDB by using non-standard GCC compilation
8274 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8275 */
ab260dad 8276 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8277 cust->locations_valid = 1;
e0d00bc7
JK
8278
8279 if (gcc_4_minor >= 5)
43f3e411 8280 cust->epilogue_unwind_valid = 1;
96408a79 8281
43f3e411 8282 cust->call_site_htab = cu->call_site_htab;
c906108c 8283 }
9291a0cd
TT
8284
8285 if (dwarf2_per_objfile->using_index)
43f3e411 8286 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8287 else
8288 {
8289 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8290 pst->compunit_symtab = cust;
9291a0cd
TT
8291 pst->readin = 1;
8292 }
c906108c 8293
95554aad
TT
8294 /* Push it for inclusion processing later. */
8295 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8296
c906108c 8297 do_cleanups (back_to);
f4dc4d17 8298}
45cfd468 8299
f4dc4d17
DE
8300/* Generate full symbol information for type unit PER_CU, whose DIEs have
8301 already been loaded into memory. */
8302
8303static void
8304process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8305 enum language pretend_language)
8306{
8307 struct dwarf2_cu *cu = per_cu->cu;
8308 struct objfile *objfile = per_cu->objfile;
43f3e411 8309 struct compunit_symtab *cust;
f4dc4d17 8310 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8311 struct signatured_type *sig_type;
8312
8313 gdb_assert (per_cu->is_debug_types);
8314 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8315
8316 buildsym_init ();
8317 back_to = make_cleanup (really_free_pendings, NULL);
8318 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8319
8320 cu->list_in_scope = &file_symbols;
8321
8322 cu->language = pretend_language;
8323 cu->language_defn = language_def (cu->language);
8324
8325 /* The symbol tables are set up in read_type_unit_scope. */
8326 process_die (cu->dies, cu);
8327
8328 /* For now fudge the Go package. */
8329 if (cu->language == language_go)
8330 fixup_go_packaging (cu);
8331
8332 /* Now that we have processed all the DIEs in the CU, all the types
8333 should be complete, and it should now be safe to compute all of the
8334 physnames. */
8335 compute_delayed_physnames (cu);
8336 do_cleanups (delayed_list_cleanup);
8337
8338 /* TUs share symbol tables.
8339 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8340 of it with end_expandable_symtab. Otherwise, complete the addition of
8341 this TU's symbols to the existing symtab. */
43f3e411 8342 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8343 {
43f3e411
DE
8344 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8345 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8346
43f3e411 8347 if (cust != NULL)
f4dc4d17
DE
8348 {
8349 /* Set symtab language to language from DW_AT_language. If the
8350 compilation is from a C file generated by language preprocessors,
8351 do not set the language if it was already deduced by
8352 start_subfile. */
43f3e411
DE
8353 if (!(cu->language == language_c
8354 && COMPUNIT_FILETABS (cust)->language != language_c))
8355 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8356 }
8357 }
8358 else
8359 {
0ab9ce85 8360 augment_type_symtab ();
43f3e411 8361 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8362 }
8363
8364 if (dwarf2_per_objfile->using_index)
43f3e411 8365 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8366 else
8367 {
8368 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8369 pst->compunit_symtab = cust;
f4dc4d17 8370 pst->readin = 1;
45cfd468 8371 }
f4dc4d17
DE
8372
8373 do_cleanups (back_to);
c906108c
SS
8374}
8375
95554aad
TT
8376/* Process an imported unit DIE. */
8377
8378static void
8379process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8380{
8381 struct attribute *attr;
8382
f4dc4d17
DE
8383 /* For now we don't handle imported units in type units. */
8384 if (cu->per_cu->is_debug_types)
8385 {
8386 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8387 " supported in type units [in module %s]"),
4262abfb 8388 objfile_name (cu->objfile));
f4dc4d17
DE
8389 }
8390
95554aad
TT
8391 attr = dwarf2_attr (die, DW_AT_import, cu);
8392 if (attr != NULL)
8393 {
9c541725
PA
8394 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8395 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8396 dwarf2_per_cu_data *per_cu
8397 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8398
69d751e3 8399 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8400 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8401 load_full_comp_unit (per_cu, cu->language);
8402
796a7ff8 8403 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8404 per_cu);
8405 }
8406}
8407
adde2bff
DE
8408/* Reset the in_process bit of a die. */
8409
8410static void
8411reset_die_in_process (void *arg)
8412{
9a3c8263 8413 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8414
adde2bff
DE
8415 die->in_process = 0;
8416}
8417
c906108c
SS
8418/* Process a die and its children. */
8419
8420static void
e7c27a73 8421process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8422{
adde2bff
DE
8423 struct cleanup *in_process;
8424
8425 /* We should only be processing those not already in process. */
8426 gdb_assert (!die->in_process);
8427
8428 die->in_process = 1;
8429 in_process = make_cleanup (reset_die_in_process,die);
8430
c906108c
SS
8431 switch (die->tag)
8432 {
8433 case DW_TAG_padding:
8434 break;
8435 case DW_TAG_compile_unit:
95554aad 8436 case DW_TAG_partial_unit:
e7c27a73 8437 read_file_scope (die, cu);
c906108c 8438 break;
348e048f
DE
8439 case DW_TAG_type_unit:
8440 read_type_unit_scope (die, cu);
8441 break;
c906108c 8442 case DW_TAG_subprogram:
c906108c 8443 case DW_TAG_inlined_subroutine:
edb3359d 8444 read_func_scope (die, cu);
c906108c
SS
8445 break;
8446 case DW_TAG_lexical_block:
14898363
L
8447 case DW_TAG_try_block:
8448 case DW_TAG_catch_block:
e7c27a73 8449 read_lexical_block_scope (die, cu);
c906108c 8450 break;
216f72a1 8451 case DW_TAG_call_site:
96408a79
SA
8452 case DW_TAG_GNU_call_site:
8453 read_call_site_scope (die, cu);
8454 break;
c906108c 8455 case DW_TAG_class_type:
680b30c7 8456 case DW_TAG_interface_type:
c906108c
SS
8457 case DW_TAG_structure_type:
8458 case DW_TAG_union_type:
134d01f1 8459 process_structure_scope (die, cu);
c906108c
SS
8460 break;
8461 case DW_TAG_enumeration_type:
134d01f1 8462 process_enumeration_scope (die, cu);
c906108c 8463 break;
134d01f1 8464
f792889a
DJ
8465 /* These dies have a type, but processing them does not create
8466 a symbol or recurse to process the children. Therefore we can
8467 read them on-demand through read_type_die. */
c906108c 8468 case DW_TAG_subroutine_type:
72019c9c 8469 case DW_TAG_set_type:
c906108c 8470 case DW_TAG_array_type:
c906108c 8471 case DW_TAG_pointer_type:
c906108c 8472 case DW_TAG_ptr_to_member_type:
c906108c 8473 case DW_TAG_reference_type:
4297a3f0 8474 case DW_TAG_rvalue_reference_type:
c906108c 8475 case DW_TAG_string_type:
c906108c 8476 break;
134d01f1 8477
c906108c 8478 case DW_TAG_base_type:
a02abb62 8479 case DW_TAG_subrange_type:
cb249c71 8480 case DW_TAG_typedef:
134d01f1
DJ
8481 /* Add a typedef symbol for the type definition, if it has a
8482 DW_AT_name. */
f792889a 8483 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8484 break;
c906108c 8485 case DW_TAG_common_block:
e7c27a73 8486 read_common_block (die, cu);
c906108c
SS
8487 break;
8488 case DW_TAG_common_inclusion:
8489 break;
d9fa45fe 8490 case DW_TAG_namespace:
4d4ec4e5 8491 cu->processing_has_namespace_info = 1;
e7c27a73 8492 read_namespace (die, cu);
d9fa45fe 8493 break;
5d7cb8df 8494 case DW_TAG_module:
4d4ec4e5 8495 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8496 read_module (die, cu);
8497 break;
d9fa45fe 8498 case DW_TAG_imported_declaration:
74921315
KS
8499 cu->processing_has_namespace_info = 1;
8500 if (read_namespace_alias (die, cu))
8501 break;
8502 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8503 case DW_TAG_imported_module:
4d4ec4e5 8504 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8505 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8506 || cu->language != language_fortran))
8507 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8508 dwarf_tag_name (die->tag));
8509 read_import_statement (die, cu);
d9fa45fe 8510 break;
95554aad
TT
8511
8512 case DW_TAG_imported_unit:
8513 process_imported_unit_die (die, cu);
8514 break;
8515
c906108c 8516 default:
e7c27a73 8517 new_symbol (die, NULL, cu);
c906108c
SS
8518 break;
8519 }
adde2bff
DE
8520
8521 do_cleanups (in_process);
c906108c 8522}
ca69b9e6
DE
8523\f
8524/* DWARF name computation. */
c906108c 8525
94af9270
KS
8526/* A helper function for dwarf2_compute_name which determines whether DIE
8527 needs to have the name of the scope prepended to the name listed in the
8528 die. */
8529
8530static int
8531die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8532{
1c809c68
TT
8533 struct attribute *attr;
8534
94af9270
KS
8535 switch (die->tag)
8536 {
8537 case DW_TAG_namespace:
8538 case DW_TAG_typedef:
8539 case DW_TAG_class_type:
8540 case DW_TAG_interface_type:
8541 case DW_TAG_structure_type:
8542 case DW_TAG_union_type:
8543 case DW_TAG_enumeration_type:
8544 case DW_TAG_enumerator:
8545 case DW_TAG_subprogram:
08a76f8a 8546 case DW_TAG_inlined_subroutine:
94af9270 8547 case DW_TAG_member:
74921315 8548 case DW_TAG_imported_declaration:
94af9270
KS
8549 return 1;
8550
8551 case DW_TAG_variable:
c2b0a229 8552 case DW_TAG_constant:
94af9270
KS
8553 /* We only need to prefix "globally" visible variables. These include
8554 any variable marked with DW_AT_external or any variable that
8555 lives in a namespace. [Variables in anonymous namespaces
8556 require prefixing, but they are not DW_AT_external.] */
8557
8558 if (dwarf2_attr (die, DW_AT_specification, cu))
8559 {
8560 struct dwarf2_cu *spec_cu = cu;
9a619af0 8561
94af9270
KS
8562 return die_needs_namespace (die_specification (die, &spec_cu),
8563 spec_cu);
8564 }
8565
1c809c68 8566 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8567 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8568 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8569 return 0;
8570 /* A variable in a lexical block of some kind does not need a
8571 namespace, even though in C++ such variables may be external
8572 and have a mangled name. */
8573 if (die->parent->tag == DW_TAG_lexical_block
8574 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8575 || die->parent->tag == DW_TAG_catch_block
8576 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8577 return 0;
8578 return 1;
94af9270
KS
8579
8580 default:
8581 return 0;
8582 }
8583}
8584
8585/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8586 compute the physname for the object, which include a method's:
9c37b5ae 8587 - formal parameters (C++),
a766d390 8588 - receiver type (Go),
a766d390
DE
8589
8590 The term "physname" is a bit confusing.
8591 For C++, for example, it is the demangled name.
8592 For Go, for example, it's the mangled name.
94af9270 8593
af6b7be1
JB
8594 For Ada, return the DIE's linkage name rather than the fully qualified
8595 name. PHYSNAME is ignored..
8596
94af9270
KS
8597 The result is allocated on the objfile_obstack and canonicalized. */
8598
8599static const char *
15d034d0
TT
8600dwarf2_compute_name (const char *name,
8601 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8602 int physname)
8603{
bb5ed363
DE
8604 struct objfile *objfile = cu->objfile;
8605
94af9270
KS
8606 if (name == NULL)
8607 name = dwarf2_name (die, cu);
8608
2ee7123e
DE
8609 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8610 but otherwise compute it by typename_concat inside GDB.
8611 FIXME: Actually this is not really true, or at least not always true.
8612 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8613 Fortran names because there is no mangling standard. So new_symbol_full
8614 will set the demangled name to the result of dwarf2_full_name, and it is
8615 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8616 if (cu->language == language_ada
8617 || (cu->language == language_fortran && physname))
8618 {
8619 /* For Ada unit, we prefer the linkage name over the name, as
8620 the former contains the exported name, which the user expects
8621 to be able to reference. Ideally, we want the user to be able
8622 to reference this entity using either natural or linkage name,
8623 but we haven't started looking at this enhancement yet. */
2ee7123e 8624 const char *linkage_name;
f55ee35c 8625
2ee7123e
DE
8626 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8627 if (linkage_name == NULL)
8628 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8629 if (linkage_name != NULL)
8630 return linkage_name;
f55ee35c
JK
8631 }
8632
94af9270
KS
8633 /* These are the only languages we know how to qualify names in. */
8634 if (name != NULL
9c37b5ae 8635 && (cu->language == language_cplus
c44af4eb
TT
8636 || cu->language == language_fortran || cu->language == language_d
8637 || cu->language == language_rust))
94af9270
KS
8638 {
8639 if (die_needs_namespace (die, cu))
8640 {
8641 long length;
0d5cff50 8642 const char *prefix;
34a68019 8643 const char *canonical_name = NULL;
94af9270 8644
d7e74731
PA
8645 string_file buf;
8646
94af9270 8647 prefix = determine_prefix (die, cu);
94af9270
KS
8648 if (*prefix != '\0')
8649 {
f55ee35c
JK
8650 char *prefixed_name = typename_concat (NULL, prefix, name,
8651 physname, cu);
9a619af0 8652
d7e74731 8653 buf.puts (prefixed_name);
94af9270
KS
8654 xfree (prefixed_name);
8655 }
8656 else
d7e74731 8657 buf.puts (name);
94af9270 8658
98bfdba5
PA
8659 /* Template parameters may be specified in the DIE's DW_AT_name, or
8660 as children with DW_TAG_template_type_param or
8661 DW_TAG_value_type_param. If the latter, add them to the name
8662 here. If the name already has template parameters, then
8663 skip this step; some versions of GCC emit both, and
8664 it is more efficient to use the pre-computed name.
8665
8666 Something to keep in mind about this process: it is very
8667 unlikely, or in some cases downright impossible, to produce
8668 something that will match the mangled name of a function.
8669 If the definition of the function has the same debug info,
8670 we should be able to match up with it anyway. But fallbacks
8671 using the minimal symbol, for instance to find a method
8672 implemented in a stripped copy of libstdc++, will not work.
8673 If we do not have debug info for the definition, we will have to
8674 match them up some other way.
8675
8676 When we do name matching there is a related problem with function
8677 templates; two instantiated function templates are allowed to
8678 differ only by their return types, which we do not add here. */
8679
8680 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8681 {
8682 struct attribute *attr;
8683 struct die_info *child;
8684 int first = 1;
8685
8686 die->building_fullname = 1;
8687
8688 for (child = die->child; child != NULL; child = child->sibling)
8689 {
8690 struct type *type;
12df843f 8691 LONGEST value;
d521ce57 8692 const gdb_byte *bytes;
98bfdba5
PA
8693 struct dwarf2_locexpr_baton *baton;
8694 struct value *v;
8695
8696 if (child->tag != DW_TAG_template_type_param
8697 && child->tag != DW_TAG_template_value_param)
8698 continue;
8699
8700 if (first)
8701 {
d7e74731 8702 buf.puts ("<");
98bfdba5
PA
8703 first = 0;
8704 }
8705 else
d7e74731 8706 buf.puts (", ");
98bfdba5
PA
8707
8708 attr = dwarf2_attr (child, DW_AT_type, cu);
8709 if (attr == NULL)
8710 {
8711 complaint (&symfile_complaints,
8712 _("template parameter missing DW_AT_type"));
d7e74731 8713 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8714 continue;
8715 }
8716 type = die_type (child, cu);
8717
8718 if (child->tag == DW_TAG_template_type_param)
8719 {
d7e74731 8720 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8721 continue;
8722 }
8723
8724 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8725 if (attr == NULL)
8726 {
8727 complaint (&symfile_complaints,
3e43a32a
MS
8728 _("template parameter missing "
8729 "DW_AT_const_value"));
d7e74731 8730 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8731 continue;
8732 }
8733
8734 dwarf2_const_value_attr (attr, type, name,
8735 &cu->comp_unit_obstack, cu,
8736 &value, &bytes, &baton);
8737
8738 if (TYPE_NOSIGN (type))
8739 /* GDB prints characters as NUMBER 'CHAR'. If that's
8740 changed, this can use value_print instead. */
d7e74731 8741 c_printchar (value, type, &buf);
98bfdba5
PA
8742 else
8743 {
8744 struct value_print_options opts;
8745
8746 if (baton != NULL)
8747 v = dwarf2_evaluate_loc_desc (type, NULL,
8748 baton->data,
8749 baton->size,
8750 baton->per_cu);
8751 else if (bytes != NULL)
8752 {
8753 v = allocate_value (type);
8754 memcpy (value_contents_writeable (v), bytes,
8755 TYPE_LENGTH (type));
8756 }
8757 else
8758 v = value_from_longest (type, value);
8759
3e43a32a
MS
8760 /* Specify decimal so that we do not depend on
8761 the radix. */
98bfdba5
PA
8762 get_formatted_print_options (&opts, 'd');
8763 opts.raw = 1;
d7e74731 8764 value_print (v, &buf, &opts);
98bfdba5
PA
8765 release_value (v);
8766 value_free (v);
8767 }
8768 }
8769
8770 die->building_fullname = 0;
8771
8772 if (!first)
8773 {
8774 /* Close the argument list, with a space if necessary
8775 (nested templates). */
d7e74731
PA
8776 if (!buf.empty () && buf.string ().back () == '>')
8777 buf.puts (" >");
98bfdba5 8778 else
d7e74731 8779 buf.puts (">");
98bfdba5
PA
8780 }
8781 }
8782
9c37b5ae 8783 /* For C++ methods, append formal parameter type
94af9270 8784 information, if PHYSNAME. */
6e70227d 8785
94af9270 8786 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8787 && cu->language == language_cplus)
94af9270
KS
8788 {
8789 struct type *type = read_type_die (die, cu);
8790
d7e74731 8791 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8792 &type_print_raw_options);
94af9270 8793
9c37b5ae 8794 if (cu->language == language_cplus)
94af9270 8795 {
60430eff
DJ
8796 /* Assume that an artificial first parameter is
8797 "this", but do not crash if it is not. RealView
8798 marks unnamed (and thus unused) parameters as
8799 artificial; there is no way to differentiate
8800 the two cases. */
94af9270
KS
8801 if (TYPE_NFIELDS (type) > 0
8802 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8803 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8804 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8805 0))))
d7e74731 8806 buf.puts (" const");
94af9270
KS
8807 }
8808 }
8809
d7e74731 8810 const std::string &intermediate_name = buf.string ();
94af9270
KS
8811
8812 if (cu->language == language_cplus)
34a68019 8813 canonical_name
322a8516 8814 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8815 &objfile->per_bfd->storage_obstack);
8816
8817 /* If we only computed INTERMEDIATE_NAME, or if
8818 INTERMEDIATE_NAME is already canonical, then we need to
8819 copy it to the appropriate obstack. */
322a8516 8820 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8821 name = ((const char *)
8822 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8823 intermediate_name.c_str (),
8824 intermediate_name.length ()));
34a68019
TT
8825 else
8826 name = canonical_name;
94af9270
KS
8827 }
8828 }
8829
8830 return name;
8831}
8832
0114d602
DJ
8833/* Return the fully qualified name of DIE, based on its DW_AT_name.
8834 If scope qualifiers are appropriate they will be added. The result
34a68019 8835 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8836 not have a name. NAME may either be from a previous call to
8837 dwarf2_name or NULL.
8838
9c37b5ae 8839 The output string will be canonicalized (if C++). */
0114d602
DJ
8840
8841static const char *
15d034d0 8842dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8843{
94af9270
KS
8844 return dwarf2_compute_name (name, die, cu, 0);
8845}
0114d602 8846
94af9270
KS
8847/* Construct a physname for the given DIE in CU. NAME may either be
8848 from a previous call to dwarf2_name or NULL. The result will be
8849 allocated on the objfile_objstack or NULL if the DIE does not have a
8850 name.
0114d602 8851
9c37b5ae 8852 The output string will be canonicalized (if C++). */
0114d602 8853
94af9270 8854static const char *
15d034d0 8855dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8856{
bb5ed363 8857 struct objfile *objfile = cu->objfile;
900e11f9
JK
8858 const char *retval, *mangled = NULL, *canon = NULL;
8859 struct cleanup *back_to;
8860 int need_copy = 1;
8861
8862 /* In this case dwarf2_compute_name is just a shortcut not building anything
8863 on its own. */
8864 if (!die_needs_namespace (die, cu))
8865 return dwarf2_compute_name (name, die, cu, 1);
8866
8867 back_to = make_cleanup (null_cleanup, NULL);
8868
7d45c7c3
KB
8869 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8870 if (mangled == NULL)
8871 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8872
e98c9e7c
TT
8873 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8874 See https://github.com/rust-lang/rust/issues/32925. */
8875 if (cu->language == language_rust && mangled != NULL
8876 && strchr (mangled, '{') != NULL)
8877 mangled = NULL;
8878
900e11f9
JK
8879 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8880 has computed. */
7d45c7c3 8881 if (mangled != NULL)
900e11f9
JK
8882 {
8883 char *demangled;
8884
900e11f9
JK
8885 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8886 type. It is easier for GDB users to search for such functions as
8887 `name(params)' than `long name(params)'. In such case the minimal
8888 symbol names do not match the full symbol names but for template
8889 functions there is never a need to look up their definition from their
8890 declaration so the only disadvantage remains the minimal symbol
8891 variant `long name(params)' does not have the proper inferior type.
8892 */
8893
a766d390
DE
8894 if (cu->language == language_go)
8895 {
8896 /* This is a lie, but we already lie to the caller new_symbol_full.
8897 new_symbol_full assumes we return the mangled name.
8898 This just undoes that lie until things are cleaned up. */
8899 demangled = NULL;
8900 }
8901 else
8902 {
8de20a37 8903 demangled = gdb_demangle (mangled,
9c37b5ae 8904 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8905 }
900e11f9
JK
8906 if (demangled)
8907 {
8908 make_cleanup (xfree, demangled);
8909 canon = demangled;
8910 }
8911 else
8912 {
8913 canon = mangled;
8914 need_copy = 0;
8915 }
8916 }
8917
8918 if (canon == NULL || check_physname)
8919 {
8920 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8921
8922 if (canon != NULL && strcmp (physname, canon) != 0)
8923 {
8924 /* It may not mean a bug in GDB. The compiler could also
8925 compute DW_AT_linkage_name incorrectly. But in such case
8926 GDB would need to be bug-to-bug compatible. */
8927
8928 complaint (&symfile_complaints,
8929 _("Computed physname <%s> does not match demangled <%s> "
8930 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 8931 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 8932 objfile_name (objfile));
900e11f9
JK
8933
8934 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8935 is available here - over computed PHYSNAME. It is safer
8936 against both buggy GDB and buggy compilers. */
8937
8938 retval = canon;
8939 }
8940 else
8941 {
8942 retval = physname;
8943 need_copy = 0;
8944 }
8945 }
8946 else
8947 retval = canon;
8948
8949 if (need_copy)
224c3ddb
SM
8950 retval = ((const char *)
8951 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8952 retval, strlen (retval)));
900e11f9
JK
8953
8954 do_cleanups (back_to);
8955 return retval;
0114d602
DJ
8956}
8957
74921315
KS
8958/* Inspect DIE in CU for a namespace alias. If one exists, record
8959 a new symbol for it.
8960
8961 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8962
8963static int
8964read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8965{
8966 struct attribute *attr;
8967
8968 /* If the die does not have a name, this is not a namespace
8969 alias. */
8970 attr = dwarf2_attr (die, DW_AT_name, cu);
8971 if (attr != NULL)
8972 {
8973 int num;
8974 struct die_info *d = die;
8975 struct dwarf2_cu *imported_cu = cu;
8976
8977 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8978 keep inspecting DIEs until we hit the underlying import. */
8979#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8980 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8981 {
8982 attr = dwarf2_attr (d, DW_AT_import, cu);
8983 if (attr == NULL)
8984 break;
8985
8986 d = follow_die_ref (d, attr, &imported_cu);
8987 if (d->tag != DW_TAG_imported_declaration)
8988 break;
8989 }
8990
8991 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8992 {
8993 complaint (&symfile_complaints,
8994 _("DIE at 0x%x has too many recursively imported "
9c541725 8995 "declarations"), to_underlying (d->sect_off));
74921315
KS
8996 return 0;
8997 }
8998
8999 if (attr != NULL)
9000 {
9001 struct type *type;
9c541725 9002 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9003
9c541725 9004 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9005 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9006 {
9007 /* This declaration is a global namespace alias. Add
9008 a symbol for it whose type is the aliased namespace. */
9009 new_symbol (die, type, cu);
9010 return 1;
9011 }
9012 }
9013 }
9014
9015 return 0;
9016}
9017
22cee43f
PMR
9018/* Return the using directives repository (global or local?) to use in the
9019 current context for LANGUAGE.
9020
9021 For Ada, imported declarations can materialize renamings, which *may* be
9022 global. However it is impossible (for now?) in DWARF to distinguish
9023 "external" imported declarations and "static" ones. As all imported
9024 declarations seem to be static in all other languages, make them all CU-wide
9025 global only in Ada. */
9026
9027static struct using_direct **
9028using_directives (enum language language)
9029{
9030 if (language == language_ada && context_stack_depth == 0)
9031 return &global_using_directives;
9032 else
9033 return &local_using_directives;
9034}
9035
27aa8d6a
SW
9036/* Read the import statement specified by the given die and record it. */
9037
9038static void
9039read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9040{
bb5ed363 9041 struct objfile *objfile = cu->objfile;
27aa8d6a 9042 struct attribute *import_attr;
32019081 9043 struct die_info *imported_die, *child_die;
de4affc9 9044 struct dwarf2_cu *imported_cu;
27aa8d6a 9045 const char *imported_name;
794684b6 9046 const char *imported_name_prefix;
13387711
SW
9047 const char *canonical_name;
9048 const char *import_alias;
9049 const char *imported_declaration = NULL;
794684b6 9050 const char *import_prefix;
32019081
JK
9051 VEC (const_char_ptr) *excludes = NULL;
9052 struct cleanup *cleanups;
13387711 9053
27aa8d6a
SW
9054 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9055 if (import_attr == NULL)
9056 {
9057 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9058 dwarf_tag_name (die->tag));
9059 return;
9060 }
9061
de4affc9
CC
9062 imported_cu = cu;
9063 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9064 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9065 if (imported_name == NULL)
9066 {
9067 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9068
9069 The import in the following code:
9070 namespace A
9071 {
9072 typedef int B;
9073 }
9074
9075 int main ()
9076 {
9077 using A::B;
9078 B b;
9079 return b;
9080 }
9081
9082 ...
9083 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9084 <52> DW_AT_decl_file : 1
9085 <53> DW_AT_decl_line : 6
9086 <54> DW_AT_import : <0x75>
9087 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9088 <59> DW_AT_name : B
9089 <5b> DW_AT_decl_file : 1
9090 <5c> DW_AT_decl_line : 2
9091 <5d> DW_AT_type : <0x6e>
9092 ...
9093 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9094 <76> DW_AT_byte_size : 4
9095 <77> DW_AT_encoding : 5 (signed)
9096
9097 imports the wrong die ( 0x75 instead of 0x58 ).
9098 This case will be ignored until the gcc bug is fixed. */
9099 return;
9100 }
9101
82856980
SW
9102 /* Figure out the local name after import. */
9103 import_alias = dwarf2_name (die, cu);
27aa8d6a 9104
794684b6
SW
9105 /* Figure out where the statement is being imported to. */
9106 import_prefix = determine_prefix (die, cu);
9107
9108 /* Figure out what the scope of the imported die is and prepend it
9109 to the name of the imported die. */
de4affc9 9110 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9111
f55ee35c
JK
9112 if (imported_die->tag != DW_TAG_namespace
9113 && imported_die->tag != DW_TAG_module)
794684b6 9114 {
13387711
SW
9115 imported_declaration = imported_name;
9116 canonical_name = imported_name_prefix;
794684b6 9117 }
13387711 9118 else if (strlen (imported_name_prefix) > 0)
12aaed36 9119 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9120 imported_name_prefix,
9121 (cu->language == language_d ? "." : "::"),
9122 imported_name, (char *) NULL);
13387711
SW
9123 else
9124 canonical_name = imported_name;
794684b6 9125
32019081
JK
9126 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9127
9128 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9129 for (child_die = die->child; child_die && child_die->tag;
9130 child_die = sibling_die (child_die))
9131 {
9132 /* DWARF-4: A Fortran use statement with a “rename list” may be
9133 represented by an imported module entry with an import attribute
9134 referring to the module and owned entries corresponding to those
9135 entities that are renamed as part of being imported. */
9136
9137 if (child_die->tag != DW_TAG_imported_declaration)
9138 {
9139 complaint (&symfile_complaints,
9140 _("child DW_TAG_imported_declaration expected "
9141 "- DIE at 0x%x [in module %s]"),
9c541725 9142 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9143 continue;
9144 }
9145
9146 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9147 if (import_attr == NULL)
9148 {
9149 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9150 dwarf_tag_name (child_die->tag));
9151 continue;
9152 }
9153
9154 imported_cu = cu;
9155 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9156 &imported_cu);
9157 imported_name = dwarf2_name (imported_die, imported_cu);
9158 if (imported_name == NULL)
9159 {
9160 complaint (&symfile_complaints,
9161 _("child DW_TAG_imported_declaration has unknown "
9162 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9163 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9164 continue;
9165 }
9166
9167 VEC_safe_push (const_char_ptr, excludes, imported_name);
9168
9169 process_die (child_die, cu);
9170 }
9171
22cee43f
PMR
9172 add_using_directive (using_directives (cu->language),
9173 import_prefix,
9174 canonical_name,
9175 import_alias,
9176 imported_declaration,
9177 excludes,
9178 0,
9179 &objfile->objfile_obstack);
32019081
JK
9180
9181 do_cleanups (cleanups);
27aa8d6a
SW
9182}
9183
1b80a9fa
JK
9184/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9185 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9186 this, it was first present in GCC release 4.3.0. */
9187
9188static int
9189producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9190{
9191 if (!cu->checked_producer)
9192 check_producer (cu);
9193
9194 return cu->producer_is_gcc_lt_4_3;
9195}
9196
d721ba37
PA
9197static file_and_directory
9198find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9199{
d721ba37
PA
9200 file_and_directory res;
9201
9291a0cd
TT
9202 /* Find the filename. Do not use dwarf2_name here, since the filename
9203 is not a source language identifier. */
d721ba37
PA
9204 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9205 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9206
d721ba37
PA
9207 if (res.comp_dir == NULL
9208 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9209 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9210 {
d721ba37
PA
9211 res.comp_dir_storage = ldirname (res.name);
9212 if (!res.comp_dir_storage.empty ())
9213 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9214 }
d721ba37 9215 if (res.comp_dir != NULL)
9291a0cd
TT
9216 {
9217 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9218 directory, get rid of it. */
d721ba37 9219 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9220
d721ba37
PA
9221 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9222 res.comp_dir = cp + 1;
9291a0cd
TT
9223 }
9224
d721ba37
PA
9225 if (res.name == NULL)
9226 res.name = "<unknown>";
9227
9228 return res;
9291a0cd
TT
9229}
9230
f4dc4d17
DE
9231/* Handle DW_AT_stmt_list for a compilation unit.
9232 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9233 COMP_DIR is the compilation directory. LOWPC is passed to
9234 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9235
9236static void
9237handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9238 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9239{
527f3840 9240 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9241 struct attribute *attr;
527f3840
JK
9242 struct line_header line_header_local;
9243 hashval_t line_header_local_hash;
9244 unsigned u;
9245 void **slot;
9246 int decode_mapping;
2ab95328 9247
f4dc4d17
DE
9248 gdb_assert (! cu->per_cu->is_debug_types);
9249
2ab95328 9250 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9251 if (attr == NULL)
9252 return;
9253
9c541725 9254 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9255
9256 /* The line header hash table is only created if needed (it exists to
9257 prevent redundant reading of the line table for partial_units).
9258 If we're given a partial_unit, we'll need it. If we're given a
9259 compile_unit, then use the line header hash table if it's already
9260 created, but don't create one just yet. */
9261
9262 if (dwarf2_per_objfile->line_header_hash == NULL
9263 && die->tag == DW_TAG_partial_unit)
2ab95328 9264 {
527f3840
JK
9265 dwarf2_per_objfile->line_header_hash
9266 = htab_create_alloc_ex (127, line_header_hash_voidp,
9267 line_header_eq_voidp,
9268 free_line_header_voidp,
9269 &objfile->objfile_obstack,
9270 hashtab_obstack_allocate,
9271 dummy_obstack_deallocate);
9272 }
2ab95328 9273
9c541725 9274 line_header_local.sect_off = line_offset;
527f3840
JK
9275 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9276 line_header_local_hash = line_header_hash (&line_header_local);
9277 if (dwarf2_per_objfile->line_header_hash != NULL)
9278 {
9279 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9280 &line_header_local,
9281 line_header_local_hash, NO_INSERT);
9282
9283 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9284 is not present in *SLOT (since if there is something in *SLOT then
9285 it will be for a partial_unit). */
9286 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9287 {
527f3840 9288 gdb_assert (*slot != NULL);
9a3c8263 9289 cu->line_header = (struct line_header *) *slot;
527f3840 9290 return;
dee91e82 9291 }
2ab95328 9292 }
527f3840
JK
9293
9294 /* dwarf_decode_line_header does not yet provide sufficient information.
9295 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9296 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9297 if (lh == NULL)
527f3840 9298 return;
fff8551c 9299 cu->line_header = lh.get ();
527f3840
JK
9300
9301 if (dwarf2_per_objfile->line_header_hash == NULL)
9302 slot = NULL;
9303 else
9304 {
9305 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9306 &line_header_local,
9307 line_header_local_hash, INSERT);
9308 gdb_assert (slot != NULL);
9309 }
9310 if (slot != NULL && *slot == NULL)
9311 {
9312 /* This newly decoded line number information unit will be owned
9313 by line_header_hash hash table. */
9314 *slot = cu->line_header;
9315 }
9316 else
9317 {
9318 /* We cannot free any current entry in (*slot) as that struct line_header
9319 may be already used by multiple CUs. Create only temporary decoded
9320 line_header for this CU - it may happen at most once for each line
9321 number information unit. And if we're not using line_header_hash
9322 then this is what we want as well. */
9323 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9324 }
9325 decode_mapping = (die->tag != DW_TAG_partial_unit);
9326 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9327 decode_mapping);
fff8551c
PA
9328
9329 lh.release ();
2ab95328
TT
9330}
9331
95554aad 9332/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9333
c906108c 9334static void
e7c27a73 9335read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9336{
dee91e82 9337 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9339 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9340 CORE_ADDR highpc = ((CORE_ADDR) 0);
9341 struct attribute *attr;
c906108c 9342 struct die_info *child_die;
e142c38c 9343 CORE_ADDR baseaddr;
6e70227d 9344
e142c38c 9345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9346
fae299cd 9347 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9348
9349 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9350 from finish_block. */
2acceee2 9351 if (lowpc == ((CORE_ADDR) -1))
c906108c 9352 lowpc = highpc;
3e29f34a 9353 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9354
d721ba37 9355 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9356
95554aad 9357 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9358
f4b8a18d
KW
9359 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9360 standardised yet. As a workaround for the language detection we fall
9361 back to the DW_AT_producer string. */
9362 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9363 cu->language = language_opencl;
9364
3019eac3
DE
9365 /* Similar hack for Go. */
9366 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9367 set_cu_language (DW_LANG_Go, cu);
9368
d721ba37 9369 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9370
9371 /* Decode line number information if present. We do this before
9372 processing child DIEs, so that the line header table is available
9373 for DW_AT_decl_file. */
d721ba37 9374 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9375
9376 /* Process all dies in compilation unit. */
9377 if (die->child != NULL)
9378 {
9379 child_die = die->child;
9380 while (child_die && child_die->tag)
9381 {
9382 process_die (child_die, cu);
9383 child_die = sibling_die (child_die);
9384 }
9385 }
9386
9387 /* Decode macro information, if present. Dwarf 2 macro information
9388 refers to information in the line number info statement program
9389 header, so we can only read it if we've read the header
9390 successfully. */
0af92d60
JK
9391 attr = dwarf2_attr (die, DW_AT_macros, cu);
9392 if (attr == NULL)
9393 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9394 if (attr && cu->line_header)
9395 {
9396 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9397 complaint (&symfile_complaints,
0af92d60 9398 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9399
43f3e411 9400 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9401 }
9402 else
9403 {
9404 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9405 if (attr && cu->line_header)
9406 {
9407 unsigned int macro_offset = DW_UNSND (attr);
9408
43f3e411 9409 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9410 }
9411 }
3019eac3
DE
9412}
9413
f4dc4d17
DE
9414/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9415 Create the set of symtabs used by this TU, or if this TU is sharing
9416 symtabs with another TU and the symtabs have already been created
9417 then restore those symtabs in the line header.
9418 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9419
9420static void
f4dc4d17 9421setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9422{
f4dc4d17
DE
9423 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9424 struct type_unit_group *tu_group;
9425 int first_time;
3019eac3 9426 struct attribute *attr;
9c541725 9427 unsigned int i;
0186c6a7 9428 struct signatured_type *sig_type;
3019eac3 9429
f4dc4d17 9430 gdb_assert (per_cu->is_debug_types);
0186c6a7 9431 sig_type = (struct signatured_type *) per_cu;
3019eac3 9432
f4dc4d17 9433 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9434
f4dc4d17 9435 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9436 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9437 if (sig_type->type_unit_group == NULL)
9438 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9439 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9440
9441 /* If we've already processed this stmt_list there's no real need to
9442 do it again, we could fake it and just recreate the part we need
9443 (file name,index -> symtab mapping). If data shows this optimization
9444 is useful we can do it then. */
43f3e411 9445 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9446
9447 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9448 debug info. */
fff8551c 9449 line_header_up lh;
f4dc4d17 9450 if (attr != NULL)
3019eac3 9451 {
9c541725 9452 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9453 lh = dwarf_decode_line_header (line_offset, cu);
9454 }
9455 if (lh == NULL)
9456 {
9457 if (first_time)
9458 dwarf2_start_symtab (cu, "", NULL, 0);
9459 else
9460 {
9461 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9462 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9463 }
f4dc4d17 9464 return;
3019eac3
DE
9465 }
9466
fff8551c 9467 cu->line_header = lh.get ();
3019eac3 9468
f4dc4d17
DE
9469 if (first_time)
9470 {
43f3e411 9471 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9472
1fd60fc0
DE
9473 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9474 still initializing it, and our caller (a few levels up)
9475 process_full_type_unit still needs to know if this is the first
9476 time. */
9477
fff8551c
PA
9478 tu_group->num_symtabs = lh->file_names.size ();
9479 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
3019eac3 9480
fff8551c 9481 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17 9482 {
8c43009f 9483 file_entry &fe = lh->file_names[i];
3019eac3 9484
fff8551c 9485 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
3019eac3 9486
f4dc4d17
DE
9487 if (current_subfile->symtab == NULL)
9488 {
9489 /* NOTE: start_subfile will recognize when it's been passed
9490 a file it has already seen. So we can't assume there's a
43f3e411 9491 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9492 lh->file_names may contain dups. */
43f3e411
DE
9493 current_subfile->symtab
9494 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9495 }
9496
8c43009f
PA
9497 fe.symtab = current_subfile->symtab;
9498 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9499 }
9500 }
9501 else
3019eac3 9502 {
0ab9ce85 9503 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9504
fff8551c 9505 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17
DE
9506 {
9507 struct file_entry *fe = &lh->file_names[i];
9508
9509 fe->symtab = tu_group->symtabs[i];
9510 }
3019eac3
DE
9511 }
9512
fff8551c
PA
9513 lh.release ();
9514
f4dc4d17
DE
9515 /* The main symtab is allocated last. Type units don't have DW_AT_name
9516 so they don't have a "real" (so to speak) symtab anyway.
9517 There is later code that will assign the main symtab to all symbols
9518 that don't have one. We need to handle the case of a symbol with a
9519 missing symtab (DW_AT_decl_file) anyway. */
9520}
3019eac3 9521
f4dc4d17
DE
9522/* Process DW_TAG_type_unit.
9523 For TUs we want to skip the first top level sibling if it's not the
9524 actual type being defined by this TU. In this case the first top
9525 level sibling is there to provide context only. */
3019eac3 9526
f4dc4d17
DE
9527static void
9528read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9529{
9530 struct die_info *child_die;
3019eac3 9531
f4dc4d17
DE
9532 prepare_one_comp_unit (cu, die, language_minimal);
9533
9534 /* Initialize (or reinitialize) the machinery for building symtabs.
9535 We do this before processing child DIEs, so that the line header table
9536 is available for DW_AT_decl_file. */
9537 setup_type_unit_groups (die, cu);
9538
9539 if (die->child != NULL)
9540 {
9541 child_die = die->child;
9542 while (child_die && child_die->tag)
9543 {
9544 process_die (child_die, cu);
9545 child_die = sibling_die (child_die);
9546 }
9547 }
3019eac3
DE
9548}
9549\f
80626a55
DE
9550/* DWO/DWP files.
9551
9552 http://gcc.gnu.org/wiki/DebugFission
9553 http://gcc.gnu.org/wiki/DebugFissionDWP
9554
9555 To simplify handling of both DWO files ("object" files with the DWARF info)
9556 and DWP files (a file with the DWOs packaged up into one file), we treat
9557 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9558
9559static hashval_t
9560hash_dwo_file (const void *item)
9561{
9a3c8263 9562 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9563 hashval_t hash;
3019eac3 9564
a2ce51a0
DE
9565 hash = htab_hash_string (dwo_file->dwo_name);
9566 if (dwo_file->comp_dir != NULL)
9567 hash += htab_hash_string (dwo_file->comp_dir);
9568 return hash;
3019eac3
DE
9569}
9570
9571static int
9572eq_dwo_file (const void *item_lhs, const void *item_rhs)
9573{
9a3c8263
SM
9574 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9575 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9576
a2ce51a0
DE
9577 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9578 return 0;
9579 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9580 return lhs->comp_dir == rhs->comp_dir;
9581 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9582}
9583
9584/* Allocate a hash table for DWO files. */
9585
9586static htab_t
9587allocate_dwo_file_hash_table (void)
9588{
9589 struct objfile *objfile = dwarf2_per_objfile->objfile;
9590
9591 return htab_create_alloc_ex (41,
9592 hash_dwo_file,
9593 eq_dwo_file,
9594 NULL,
9595 &objfile->objfile_obstack,
9596 hashtab_obstack_allocate,
9597 dummy_obstack_deallocate);
9598}
9599
80626a55
DE
9600/* Lookup DWO file DWO_NAME. */
9601
9602static void **
0ac5b59e 9603lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9604{
9605 struct dwo_file find_entry;
9606 void **slot;
9607
9608 if (dwarf2_per_objfile->dwo_files == NULL)
9609 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9610
9611 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9612 find_entry.dwo_name = dwo_name;
9613 find_entry.comp_dir = comp_dir;
80626a55
DE
9614 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9615
9616 return slot;
9617}
9618
3019eac3
DE
9619static hashval_t
9620hash_dwo_unit (const void *item)
9621{
9a3c8263 9622 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9623
9624 /* This drops the top 32 bits of the id, but is ok for a hash. */
9625 return dwo_unit->signature;
9626}
9627
9628static int
9629eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9630{
9a3c8263
SM
9631 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9632 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9633
9634 /* The signature is assumed to be unique within the DWO file.
9635 So while object file CU dwo_id's always have the value zero,
9636 that's OK, assuming each object file DWO file has only one CU,
9637 and that's the rule for now. */
9638 return lhs->signature == rhs->signature;
9639}
9640
9641/* Allocate a hash table for DWO CUs,TUs.
9642 There is one of these tables for each of CUs,TUs for each DWO file. */
9643
9644static htab_t
9645allocate_dwo_unit_table (struct objfile *objfile)
9646{
9647 /* Start out with a pretty small number.
9648 Generally DWO files contain only one CU and maybe some TUs. */
9649 return htab_create_alloc_ex (3,
9650 hash_dwo_unit,
9651 eq_dwo_unit,
9652 NULL,
9653 &objfile->objfile_obstack,
9654 hashtab_obstack_allocate,
9655 dummy_obstack_deallocate);
9656}
9657
80626a55 9658/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9659
19c3d4c9 9660struct create_dwo_cu_data
3019eac3
DE
9661{
9662 struct dwo_file *dwo_file;
19c3d4c9 9663 struct dwo_unit dwo_unit;
3019eac3
DE
9664};
9665
19c3d4c9 9666/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9667
9668static void
19c3d4c9
DE
9669create_dwo_cu_reader (const struct die_reader_specs *reader,
9670 const gdb_byte *info_ptr,
9671 struct die_info *comp_unit_die,
9672 int has_children,
9673 void *datap)
3019eac3
DE
9674{
9675 struct dwarf2_cu *cu = reader->cu;
9c541725 9676 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9677 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9678 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9679 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9680 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9681 struct attribute *attr;
3019eac3
DE
9682
9683 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9684 if (attr == NULL)
9685 {
19c3d4c9
DE
9686 complaint (&symfile_complaints,
9687 _("Dwarf Error: debug entry at offset 0x%x is missing"
9688 " its dwo_id [in module %s]"),
9c541725 9689 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9690 return;
9691 }
9692
3019eac3
DE
9693 dwo_unit->dwo_file = dwo_file;
9694 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9695 dwo_unit->section = section;
9c541725 9696 dwo_unit->sect_off = sect_off;
3019eac3
DE
9697 dwo_unit->length = cu->per_cu->length;
9698
b4f54984 9699 if (dwarf_read_debug)
4031ecc5 9700 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9701 to_underlying (sect_off),
9702 hex_string (dwo_unit->signature));
3019eac3
DE
9703}
9704
19c3d4c9
DE
9705/* Create the dwo_unit for the lone CU in DWO_FILE.
9706 Note: This function processes DWO files only, not DWP files. */
3019eac3 9707
19c3d4c9
DE
9708static struct dwo_unit *
9709create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9710{
9711 struct objfile *objfile = dwarf2_per_objfile->objfile;
9712 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9713 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9714 struct create_dwo_cu_data create_dwo_cu_data;
9715 struct dwo_unit *dwo_unit;
3019eac3
DE
9716
9717 dwarf2_read_section (objfile, section);
9718 info_ptr = section->buffer;
9719
9720 if (info_ptr == NULL)
9721 return NULL;
9722
b4f54984 9723 if (dwarf_read_debug)
19c3d4c9
DE
9724 {
9725 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9726 get_section_name (section),
9727 get_section_file_name (section));
19c3d4c9 9728 }
3019eac3 9729
19c3d4c9
DE
9730 create_dwo_cu_data.dwo_file = dwo_file;
9731 dwo_unit = NULL;
3019eac3
DE
9732
9733 end_ptr = info_ptr + section->size;
9734 while (info_ptr < end_ptr)
9735 {
9736 struct dwarf2_per_cu_data per_cu;
9737
19c3d4c9
DE
9738 memset (&create_dwo_cu_data.dwo_unit, 0,
9739 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9740 memset (&per_cu, 0, sizeof (per_cu));
9741 per_cu.objfile = objfile;
9742 per_cu.is_debug_types = 0;
9c541725 9743 per_cu.sect_off = sect_offset (info_ptr - section->buffer);
8a0459fd 9744 per_cu.section = section;
3019eac3 9745
33e80786 9746 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9747 create_dwo_cu_reader,
9748 &create_dwo_cu_data);
9749
9750 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9751 {
9752 /* If we've already found one, complain. We only support one
9753 because having more than one requires hacking the dwo_name of
9754 each to match, which is highly unlikely to happen. */
9755 if (dwo_unit != NULL)
9756 {
9757 complaint (&symfile_complaints,
9758 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9759 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9760 break;
9761 }
9762
9763 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9764 *dwo_unit = create_dwo_cu_data.dwo_unit;
9765 }
3019eac3
DE
9766
9767 info_ptr += per_cu.length;
9768 }
9769
19c3d4c9 9770 return dwo_unit;
3019eac3
DE
9771}
9772
80626a55
DE
9773/* DWP file .debug_{cu,tu}_index section format:
9774 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9775
d2415c6c
DE
9776 DWP Version 1:
9777
80626a55
DE
9778 Both index sections have the same format, and serve to map a 64-bit
9779 signature to a set of section numbers. Each section begins with a header,
9780 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9781 indexes, and a pool of 32-bit section numbers. The index sections will be
9782 aligned at 8-byte boundaries in the file.
9783
d2415c6c
DE
9784 The index section header consists of:
9785
9786 V, 32 bit version number
9787 -, 32 bits unused
9788 N, 32 bit number of compilation units or type units in the index
9789 M, 32 bit number of slots in the hash table
80626a55 9790
d2415c6c 9791 Numbers are recorded using the byte order of the application binary.
80626a55 9792
d2415c6c
DE
9793 The hash table begins at offset 16 in the section, and consists of an array
9794 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9795 order of the application binary). Unused slots in the hash table are 0.
9796 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9797
d2415c6c
DE
9798 The parallel table begins immediately after the hash table
9799 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9800 array of 32-bit indexes (using the byte order of the application binary),
9801 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9802 table contains a 32-bit index into the pool of section numbers. For unused
9803 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9804
73869dc2
DE
9805 The pool of section numbers begins immediately following the hash table
9806 (at offset 16 + 12 * M from the beginning of the section). The pool of
9807 section numbers consists of an array of 32-bit words (using the byte order
9808 of the application binary). Each item in the array is indexed starting
9809 from 0. The hash table entry provides the index of the first section
9810 number in the set. Additional section numbers in the set follow, and the
9811 set is terminated by a 0 entry (section number 0 is not used in ELF).
9812
9813 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9814 section must be the first entry in the set, and the .debug_abbrev.dwo must
9815 be the second entry. Other members of the set may follow in any order.
9816
9817 ---
9818
9819 DWP Version 2:
9820
9821 DWP Version 2 combines all the .debug_info, etc. sections into one,
9822 and the entries in the index tables are now offsets into these sections.
9823 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9824 section.
9825
9826 Index Section Contents:
9827 Header
9828 Hash Table of Signatures dwp_hash_table.hash_table
9829 Parallel Table of Indices dwp_hash_table.unit_table
9830 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9831 Table of Section Sizes dwp_hash_table.v2.sizes
9832
9833 The index section header consists of:
9834
9835 V, 32 bit version number
9836 L, 32 bit number of columns in the table of section offsets
9837 N, 32 bit number of compilation units or type units in the index
9838 M, 32 bit number of slots in the hash table
9839
9840 Numbers are recorded using the byte order of the application binary.
9841
9842 The hash table has the same format as version 1.
9843 The parallel table of indices has the same format as version 1,
9844 except that the entries are origin-1 indices into the table of sections
9845 offsets and the table of section sizes.
9846
9847 The table of offsets begins immediately following the parallel table
9848 (at offset 16 + 12 * M from the beginning of the section). The table is
9849 a two-dimensional array of 32-bit words (using the byte order of the
9850 application binary), with L columns and N+1 rows, in row-major order.
9851 Each row in the array is indexed starting from 0. The first row provides
9852 a key to the remaining rows: each column in this row provides an identifier
9853 for a debug section, and the offsets in the same column of subsequent rows
9854 refer to that section. The section identifiers are:
9855
9856 DW_SECT_INFO 1 .debug_info.dwo
9857 DW_SECT_TYPES 2 .debug_types.dwo
9858 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9859 DW_SECT_LINE 4 .debug_line.dwo
9860 DW_SECT_LOC 5 .debug_loc.dwo
9861 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9862 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9863 DW_SECT_MACRO 8 .debug_macro.dwo
9864
9865 The offsets provided by the CU and TU index sections are the base offsets
9866 for the contributions made by each CU or TU to the corresponding section
9867 in the package file. Each CU and TU header contains an abbrev_offset
9868 field, used to find the abbreviations table for that CU or TU within the
9869 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9870 be interpreted as relative to the base offset given in the index section.
9871 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9872 should be interpreted as relative to the base offset for .debug_line.dwo,
9873 and offsets into other debug sections obtained from DWARF attributes should
9874 also be interpreted as relative to the corresponding base offset.
9875
9876 The table of sizes begins immediately following the table of offsets.
9877 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9878 with L columns and N rows, in row-major order. Each row in the array is
9879 indexed starting from 1 (row 0 is shared by the two tables).
9880
9881 ---
9882
9883 Hash table lookup is handled the same in version 1 and 2:
9884
9885 We assume that N and M will not exceed 2^32 - 1.
9886 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9887
d2415c6c
DE
9888 Given a 64-bit compilation unit signature or a type signature S, an entry
9889 in the hash table is located as follows:
80626a55 9890
d2415c6c
DE
9891 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9892 the low-order k bits all set to 1.
80626a55 9893
d2415c6c 9894 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9895
d2415c6c
DE
9896 3) If the hash table entry at index H matches the signature, use that
9897 entry. If the hash table entry at index H is unused (all zeroes),
9898 terminate the search: the signature is not present in the table.
80626a55 9899
d2415c6c 9900 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9901
d2415c6c 9902 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9903 to stop at an unused slot or find the match. */
80626a55
DE
9904
9905/* Create a hash table to map DWO IDs to their CU/TU entry in
9906 .debug_{info,types}.dwo in DWP_FILE.
9907 Returns NULL if there isn't one.
9908 Note: This function processes DWP files only, not DWO files. */
9909
9910static struct dwp_hash_table *
9911create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9912{
9913 struct objfile *objfile = dwarf2_per_objfile->objfile;
9914 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9915 const gdb_byte *index_ptr, *index_end;
80626a55 9916 struct dwarf2_section_info *index;
73869dc2 9917 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9918 struct dwp_hash_table *htab;
9919
9920 if (is_debug_types)
9921 index = &dwp_file->sections.tu_index;
9922 else
9923 index = &dwp_file->sections.cu_index;
9924
9925 if (dwarf2_section_empty_p (index))
9926 return NULL;
9927 dwarf2_read_section (objfile, index);
9928
9929 index_ptr = index->buffer;
9930 index_end = index_ptr + index->size;
9931
9932 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9933 index_ptr += 4;
9934 if (version == 2)
9935 nr_columns = read_4_bytes (dbfd, index_ptr);
9936 else
9937 nr_columns = 0;
9938 index_ptr += 4;
80626a55
DE
9939 nr_units = read_4_bytes (dbfd, index_ptr);
9940 index_ptr += 4;
9941 nr_slots = read_4_bytes (dbfd, index_ptr);
9942 index_ptr += 4;
9943
73869dc2 9944 if (version != 1 && version != 2)
80626a55 9945 {
21aa081e 9946 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9947 " [in module %s]"),
21aa081e 9948 pulongest (version), dwp_file->name);
80626a55
DE
9949 }
9950 if (nr_slots != (nr_slots & -nr_slots))
9951 {
21aa081e 9952 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9953 " is not power of 2 [in module %s]"),
21aa081e 9954 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9955 }
9956
9957 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9958 htab->version = version;
9959 htab->nr_columns = nr_columns;
80626a55
DE
9960 htab->nr_units = nr_units;
9961 htab->nr_slots = nr_slots;
9962 htab->hash_table = index_ptr;
9963 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9964
9965 /* Exit early if the table is empty. */
9966 if (nr_slots == 0 || nr_units == 0
9967 || (version == 2 && nr_columns == 0))
9968 {
9969 /* All must be zero. */
9970 if (nr_slots != 0 || nr_units != 0
9971 || (version == 2 && nr_columns != 0))
9972 {
9973 complaint (&symfile_complaints,
9974 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9975 " all zero [in modules %s]"),
9976 dwp_file->name);
9977 }
9978 return htab;
9979 }
9980
9981 if (version == 1)
9982 {
9983 htab->section_pool.v1.indices =
9984 htab->unit_table + sizeof (uint32_t) * nr_slots;
9985 /* It's harder to decide whether the section is too small in v1.
9986 V1 is deprecated anyway so we punt. */
9987 }
9988 else
9989 {
9990 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9991 int *ids = htab->section_pool.v2.section_ids;
9992 /* Reverse map for error checking. */
9993 int ids_seen[DW_SECT_MAX + 1];
9994 int i;
9995
9996 if (nr_columns < 2)
9997 {
9998 error (_("Dwarf Error: bad DWP hash table, too few columns"
9999 " in section table [in module %s]"),
10000 dwp_file->name);
10001 }
10002 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10003 {
10004 error (_("Dwarf Error: bad DWP hash table, too many columns"
10005 " in section table [in module %s]"),
10006 dwp_file->name);
10007 }
10008 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10009 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10010 for (i = 0; i < nr_columns; ++i)
10011 {
10012 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10013
10014 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10015 {
10016 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10017 " in section table [in module %s]"),
10018 id, dwp_file->name);
10019 }
10020 if (ids_seen[id] != -1)
10021 {
10022 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10023 " id %d in section table [in module %s]"),
10024 id, dwp_file->name);
10025 }
10026 ids_seen[id] = i;
10027 ids[i] = id;
10028 }
10029 /* Must have exactly one info or types section. */
10030 if (((ids_seen[DW_SECT_INFO] != -1)
10031 + (ids_seen[DW_SECT_TYPES] != -1))
10032 != 1)
10033 {
10034 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10035 " DWO info/types section [in module %s]"),
10036 dwp_file->name);
10037 }
10038 /* Must have an abbrev section. */
10039 if (ids_seen[DW_SECT_ABBREV] == -1)
10040 {
10041 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10042 " section [in module %s]"),
10043 dwp_file->name);
10044 }
10045 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10046 htab->section_pool.v2.sizes =
10047 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10048 * nr_units * nr_columns);
10049 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10050 * nr_units * nr_columns))
10051 > index_end)
10052 {
10053 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10054 " [in module %s]"),
10055 dwp_file->name);
10056 }
10057 }
80626a55
DE
10058
10059 return htab;
10060}
10061
10062/* Update SECTIONS with the data from SECTP.
10063
10064 This function is like the other "locate" section routines that are
10065 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10066 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10067
10068 The result is non-zero for success, or zero if an error was found. */
10069
10070static int
73869dc2
DE
10071locate_v1_virtual_dwo_sections (asection *sectp,
10072 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10073{
10074 const struct dwop_section_names *names = &dwop_section_names;
10075
10076 if (section_is_p (sectp->name, &names->abbrev_dwo))
10077 {
10078 /* There can be only one. */
049412e3 10079 if (sections->abbrev.s.section != NULL)
80626a55 10080 return 0;
049412e3 10081 sections->abbrev.s.section = sectp;
80626a55
DE
10082 sections->abbrev.size = bfd_get_section_size (sectp);
10083 }
10084 else if (section_is_p (sectp->name, &names->info_dwo)
10085 || section_is_p (sectp->name, &names->types_dwo))
10086 {
10087 /* There can be only one. */
049412e3 10088 if (sections->info_or_types.s.section != NULL)
80626a55 10089 return 0;
049412e3 10090 sections->info_or_types.s.section = sectp;
80626a55
DE
10091 sections->info_or_types.size = bfd_get_section_size (sectp);
10092 }
10093 else if (section_is_p (sectp->name, &names->line_dwo))
10094 {
10095 /* There can be only one. */
049412e3 10096 if (sections->line.s.section != NULL)
80626a55 10097 return 0;
049412e3 10098 sections->line.s.section = sectp;
80626a55
DE
10099 sections->line.size = bfd_get_section_size (sectp);
10100 }
10101 else if (section_is_p (sectp->name, &names->loc_dwo))
10102 {
10103 /* There can be only one. */
049412e3 10104 if (sections->loc.s.section != NULL)
80626a55 10105 return 0;
049412e3 10106 sections->loc.s.section = sectp;
80626a55
DE
10107 sections->loc.size = bfd_get_section_size (sectp);
10108 }
10109 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10110 {
10111 /* There can be only one. */
049412e3 10112 if (sections->macinfo.s.section != NULL)
80626a55 10113 return 0;
049412e3 10114 sections->macinfo.s.section = sectp;
80626a55
DE
10115 sections->macinfo.size = bfd_get_section_size (sectp);
10116 }
10117 else if (section_is_p (sectp->name, &names->macro_dwo))
10118 {
10119 /* There can be only one. */
049412e3 10120 if (sections->macro.s.section != NULL)
80626a55 10121 return 0;
049412e3 10122 sections->macro.s.section = sectp;
80626a55
DE
10123 sections->macro.size = bfd_get_section_size (sectp);
10124 }
10125 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10126 {
10127 /* There can be only one. */
049412e3 10128 if (sections->str_offsets.s.section != NULL)
80626a55 10129 return 0;
049412e3 10130 sections->str_offsets.s.section = sectp;
80626a55
DE
10131 sections->str_offsets.size = bfd_get_section_size (sectp);
10132 }
10133 else
10134 {
10135 /* No other kind of section is valid. */
10136 return 0;
10137 }
10138
10139 return 1;
10140}
10141
73869dc2
DE
10142/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10143 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10144 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10145 This is for DWP version 1 files. */
80626a55
DE
10146
10147static struct dwo_unit *
73869dc2
DE
10148create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10149 uint32_t unit_index,
10150 const char *comp_dir,
10151 ULONGEST signature, int is_debug_types)
80626a55
DE
10152{
10153 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10154 const struct dwp_hash_table *dwp_htab =
10155 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10156 bfd *dbfd = dwp_file->dbfd;
10157 const char *kind = is_debug_types ? "TU" : "CU";
10158 struct dwo_file *dwo_file;
10159 struct dwo_unit *dwo_unit;
73869dc2 10160 struct virtual_v1_dwo_sections sections;
80626a55
DE
10161 void **dwo_file_slot;
10162 char *virtual_dwo_name;
80626a55
DE
10163 struct cleanup *cleanups;
10164 int i;
10165
73869dc2
DE
10166 gdb_assert (dwp_file->version == 1);
10167
b4f54984 10168 if (dwarf_read_debug)
80626a55 10169 {
73869dc2 10170 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10171 kind,
73869dc2 10172 pulongest (unit_index), hex_string (signature),
80626a55
DE
10173 dwp_file->name);
10174 }
10175
19ac8c2e 10176 /* Fetch the sections of this DWO unit.
80626a55
DE
10177 Put a limit on the number of sections we look for so that bad data
10178 doesn't cause us to loop forever. */
10179
73869dc2 10180#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10181 (1 /* .debug_info or .debug_types */ \
10182 + 1 /* .debug_abbrev */ \
10183 + 1 /* .debug_line */ \
10184 + 1 /* .debug_loc */ \
10185 + 1 /* .debug_str_offsets */ \
19ac8c2e 10186 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10187 + 1 /* trailing zero */)
10188
10189 memset (&sections, 0, sizeof (sections));
10190 cleanups = make_cleanup (null_cleanup, 0);
10191
73869dc2 10192 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10193 {
10194 asection *sectp;
10195 uint32_t section_nr =
10196 read_4_bytes (dbfd,
73869dc2
DE
10197 dwp_htab->section_pool.v1.indices
10198 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10199
10200 if (section_nr == 0)
10201 break;
10202 if (section_nr >= dwp_file->num_sections)
10203 {
10204 error (_("Dwarf Error: bad DWP hash table, section number too large"
10205 " [in module %s]"),
10206 dwp_file->name);
10207 }
10208
10209 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10210 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10211 {
10212 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10213 " [in module %s]"),
10214 dwp_file->name);
10215 }
10216 }
10217
10218 if (i < 2
a32a8923
DE
10219 || dwarf2_section_empty_p (&sections.info_or_types)
10220 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10221 {
10222 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10223 " [in module %s]"),
10224 dwp_file->name);
10225 }
73869dc2 10226 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10227 {
10228 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10229 " [in module %s]"),
10230 dwp_file->name);
10231 }
10232
10233 /* It's easier for the rest of the code if we fake a struct dwo_file and
10234 have dwo_unit "live" in that. At least for now.
10235
10236 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10237 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10238 file, we can combine them back into a virtual DWO file to save space
10239 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10240 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10241
2792b94d
PM
10242 virtual_dwo_name =
10243 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10244 get_section_id (&sections.abbrev),
10245 get_section_id (&sections.line),
10246 get_section_id (&sections.loc),
10247 get_section_id (&sections.str_offsets));
80626a55
DE
10248 make_cleanup (xfree, virtual_dwo_name);
10249 /* Can we use an existing virtual DWO file? */
0ac5b59e 10250 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10251 /* Create one if necessary. */
10252 if (*dwo_file_slot == NULL)
10253 {
b4f54984 10254 if (dwarf_read_debug)
80626a55
DE
10255 {
10256 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10257 virtual_dwo_name);
10258 }
10259 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10260 dwo_file->dwo_name
10261 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10262 virtual_dwo_name,
10263 strlen (virtual_dwo_name));
0ac5b59e 10264 dwo_file->comp_dir = comp_dir;
80626a55
DE
10265 dwo_file->sections.abbrev = sections.abbrev;
10266 dwo_file->sections.line = sections.line;
10267 dwo_file->sections.loc = sections.loc;
10268 dwo_file->sections.macinfo = sections.macinfo;
10269 dwo_file->sections.macro = sections.macro;
10270 dwo_file->sections.str_offsets = sections.str_offsets;
10271 /* The "str" section is global to the entire DWP file. */
10272 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10273 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10274 there's no need to record it in dwo_file.
10275 Also, we can't simply record type sections in dwo_file because
10276 we record a pointer into the vector in dwo_unit. As we collect more
10277 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10278 for it, invalidating all copies of pointers into the previous
10279 contents. */
80626a55
DE
10280 *dwo_file_slot = dwo_file;
10281 }
10282 else
10283 {
b4f54984 10284 if (dwarf_read_debug)
80626a55
DE
10285 {
10286 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10287 virtual_dwo_name);
10288 }
9a3c8263 10289 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10290 }
10291 do_cleanups (cleanups);
10292
10293 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10294 dwo_unit->dwo_file = dwo_file;
10295 dwo_unit->signature = signature;
8d749320
SM
10296 dwo_unit->section =
10297 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10298 *dwo_unit->section = sections.info_or_types;
57d63ce2 10299 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10300
10301 return dwo_unit;
10302}
10303
73869dc2
DE
10304/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10305 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10306 piece within that section used by a TU/CU, return a virtual section
10307 of just that piece. */
10308
10309static struct dwarf2_section_info
10310create_dwp_v2_section (struct dwarf2_section_info *section,
10311 bfd_size_type offset, bfd_size_type size)
10312{
10313 struct dwarf2_section_info result;
10314 asection *sectp;
10315
10316 gdb_assert (section != NULL);
10317 gdb_assert (!section->is_virtual);
10318
10319 memset (&result, 0, sizeof (result));
10320 result.s.containing_section = section;
10321 result.is_virtual = 1;
10322
10323 if (size == 0)
10324 return result;
10325
10326 sectp = get_section_bfd_section (section);
10327
10328 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10329 bounds of the real section. This is a pretty-rare event, so just
10330 flag an error (easier) instead of a warning and trying to cope. */
10331 if (sectp == NULL
10332 || offset + size > bfd_get_section_size (sectp))
10333 {
10334 bfd *abfd = sectp->owner;
10335
10336 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10337 " in section %s [in module %s]"),
10338 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10339 objfile_name (dwarf2_per_objfile->objfile));
10340 }
10341
10342 result.virtual_offset = offset;
10343 result.size = size;
10344 return result;
10345}
10346
10347/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10348 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10349 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10350 This is for DWP version 2 files. */
10351
10352static struct dwo_unit *
10353create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10354 uint32_t unit_index,
10355 const char *comp_dir,
10356 ULONGEST signature, int is_debug_types)
10357{
10358 struct objfile *objfile = dwarf2_per_objfile->objfile;
10359 const struct dwp_hash_table *dwp_htab =
10360 is_debug_types ? dwp_file->tus : dwp_file->cus;
10361 bfd *dbfd = dwp_file->dbfd;
10362 const char *kind = is_debug_types ? "TU" : "CU";
10363 struct dwo_file *dwo_file;
10364 struct dwo_unit *dwo_unit;
10365 struct virtual_v2_dwo_sections sections;
10366 void **dwo_file_slot;
10367 char *virtual_dwo_name;
73869dc2
DE
10368 struct cleanup *cleanups;
10369 int i;
10370
10371 gdb_assert (dwp_file->version == 2);
10372
b4f54984 10373 if (dwarf_read_debug)
73869dc2
DE
10374 {
10375 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10376 kind,
10377 pulongest (unit_index), hex_string (signature),
10378 dwp_file->name);
10379 }
10380
10381 /* Fetch the section offsets of this DWO unit. */
10382
10383 memset (&sections, 0, sizeof (sections));
10384 cleanups = make_cleanup (null_cleanup, 0);
10385
10386 for (i = 0; i < dwp_htab->nr_columns; ++i)
10387 {
10388 uint32_t offset = read_4_bytes (dbfd,
10389 dwp_htab->section_pool.v2.offsets
10390 + (((unit_index - 1) * dwp_htab->nr_columns
10391 + i)
10392 * sizeof (uint32_t)));
10393 uint32_t size = read_4_bytes (dbfd,
10394 dwp_htab->section_pool.v2.sizes
10395 + (((unit_index - 1) * dwp_htab->nr_columns
10396 + i)
10397 * sizeof (uint32_t)));
10398
10399 switch (dwp_htab->section_pool.v2.section_ids[i])
10400 {
10401 case DW_SECT_INFO:
10402 case DW_SECT_TYPES:
10403 sections.info_or_types_offset = offset;
10404 sections.info_or_types_size = size;
10405 break;
10406 case DW_SECT_ABBREV:
10407 sections.abbrev_offset = offset;
10408 sections.abbrev_size = size;
10409 break;
10410 case DW_SECT_LINE:
10411 sections.line_offset = offset;
10412 sections.line_size = size;
10413 break;
10414 case DW_SECT_LOC:
10415 sections.loc_offset = offset;
10416 sections.loc_size = size;
10417 break;
10418 case DW_SECT_STR_OFFSETS:
10419 sections.str_offsets_offset = offset;
10420 sections.str_offsets_size = size;
10421 break;
10422 case DW_SECT_MACINFO:
10423 sections.macinfo_offset = offset;
10424 sections.macinfo_size = size;
10425 break;
10426 case DW_SECT_MACRO:
10427 sections.macro_offset = offset;
10428 sections.macro_size = size;
10429 break;
10430 }
10431 }
10432
10433 /* It's easier for the rest of the code if we fake a struct dwo_file and
10434 have dwo_unit "live" in that. At least for now.
10435
10436 The DWP file can be made up of a random collection of CUs and TUs.
10437 However, for each CU + set of TUs that came from the same original DWO
10438 file, we can combine them back into a virtual DWO file to save space
10439 (fewer struct dwo_file objects to allocate). Remember that for really
10440 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10441
10442 virtual_dwo_name =
10443 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10444 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10445 (long) (sections.line_size ? sections.line_offset : 0),
10446 (long) (sections.loc_size ? sections.loc_offset : 0),
10447 (long) (sections.str_offsets_size
10448 ? sections.str_offsets_offset : 0));
10449 make_cleanup (xfree, virtual_dwo_name);
10450 /* Can we use an existing virtual DWO file? */
10451 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10452 /* Create one if necessary. */
10453 if (*dwo_file_slot == NULL)
10454 {
b4f54984 10455 if (dwarf_read_debug)
73869dc2
DE
10456 {
10457 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10458 virtual_dwo_name);
10459 }
10460 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10461 dwo_file->dwo_name
10462 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10463 virtual_dwo_name,
10464 strlen (virtual_dwo_name));
73869dc2
DE
10465 dwo_file->comp_dir = comp_dir;
10466 dwo_file->sections.abbrev =
10467 create_dwp_v2_section (&dwp_file->sections.abbrev,
10468 sections.abbrev_offset, sections.abbrev_size);
10469 dwo_file->sections.line =
10470 create_dwp_v2_section (&dwp_file->sections.line,
10471 sections.line_offset, sections.line_size);
10472 dwo_file->sections.loc =
10473 create_dwp_v2_section (&dwp_file->sections.loc,
10474 sections.loc_offset, sections.loc_size);
10475 dwo_file->sections.macinfo =
10476 create_dwp_v2_section (&dwp_file->sections.macinfo,
10477 sections.macinfo_offset, sections.macinfo_size);
10478 dwo_file->sections.macro =
10479 create_dwp_v2_section (&dwp_file->sections.macro,
10480 sections.macro_offset, sections.macro_size);
10481 dwo_file->sections.str_offsets =
10482 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10483 sections.str_offsets_offset,
10484 sections.str_offsets_size);
10485 /* The "str" section is global to the entire DWP file. */
10486 dwo_file->sections.str = dwp_file->sections.str;
10487 /* The info or types section is assigned below to dwo_unit,
10488 there's no need to record it in dwo_file.
10489 Also, we can't simply record type sections in dwo_file because
10490 we record a pointer into the vector in dwo_unit. As we collect more
10491 types we'll grow the vector and eventually have to reallocate space
10492 for it, invalidating all copies of pointers into the previous
10493 contents. */
10494 *dwo_file_slot = dwo_file;
10495 }
10496 else
10497 {
b4f54984 10498 if (dwarf_read_debug)
73869dc2
DE
10499 {
10500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10501 virtual_dwo_name);
10502 }
9a3c8263 10503 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10504 }
10505 do_cleanups (cleanups);
10506
10507 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10508 dwo_unit->dwo_file = dwo_file;
10509 dwo_unit->signature = signature;
8d749320
SM
10510 dwo_unit->section =
10511 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10512 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10513 ? &dwp_file->sections.types
10514 : &dwp_file->sections.info,
10515 sections.info_or_types_offset,
10516 sections.info_or_types_size);
10517 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10518
10519 return dwo_unit;
10520}
10521
57d63ce2
DE
10522/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10523 Returns NULL if the signature isn't found. */
80626a55
DE
10524
10525static struct dwo_unit *
57d63ce2
DE
10526lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10527 ULONGEST signature, int is_debug_types)
80626a55 10528{
57d63ce2
DE
10529 const struct dwp_hash_table *dwp_htab =
10530 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10531 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10532 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10533 uint32_t hash = signature & mask;
10534 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10535 unsigned int i;
10536 void **slot;
870f88f7 10537 struct dwo_unit find_dwo_cu;
80626a55
DE
10538
10539 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10540 find_dwo_cu.signature = signature;
19ac8c2e
DE
10541 slot = htab_find_slot (is_debug_types
10542 ? dwp_file->loaded_tus
10543 : dwp_file->loaded_cus,
10544 &find_dwo_cu, INSERT);
80626a55
DE
10545
10546 if (*slot != NULL)
9a3c8263 10547 return (struct dwo_unit *) *slot;
80626a55
DE
10548
10549 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10550 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10551 {
10552 ULONGEST signature_in_table;
10553
10554 signature_in_table =
57d63ce2 10555 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10556 if (signature_in_table == signature)
10557 {
57d63ce2
DE
10558 uint32_t unit_index =
10559 read_4_bytes (dbfd,
10560 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10561
73869dc2
DE
10562 if (dwp_file->version == 1)
10563 {
10564 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10565 comp_dir, signature,
10566 is_debug_types);
10567 }
10568 else
10569 {
10570 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10571 comp_dir, signature,
10572 is_debug_types);
10573 }
9a3c8263 10574 return (struct dwo_unit *) *slot;
80626a55
DE
10575 }
10576 if (signature_in_table == 0)
10577 return NULL;
10578 hash = (hash + hash2) & mask;
10579 }
10580
10581 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10582 " [in module %s]"),
10583 dwp_file->name);
10584}
10585
ab5088bf 10586/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10587 Open the file specified by FILE_NAME and hand it off to BFD for
10588 preliminary analysis. Return a newly initialized bfd *, which
10589 includes a canonicalized copy of FILE_NAME.
80626a55 10590 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10591 SEARCH_CWD is true if the current directory is to be searched.
10592 It will be searched before debug-file-directory.
13aaf454
DE
10593 If successful, the file is added to the bfd include table of the
10594 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10595 If unable to find/open the file, return NULL.
3019eac3
DE
10596 NOTE: This function is derived from symfile_bfd_open. */
10597
192b62ce 10598static gdb_bfd_ref_ptr
6ac97d4c 10599try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10600{
80626a55 10601 int desc, flags;
3019eac3 10602 char *absolute_name;
9c02c129
DE
10603 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10604 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10605 to debug_file_directory. */
10606 char *search_path;
10607 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10608
6ac97d4c
DE
10609 if (search_cwd)
10610 {
10611 if (*debug_file_directory != '\0')
10612 search_path = concat (".", dirname_separator_string,
b36cec19 10613 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10614 else
10615 search_path = xstrdup (".");
10616 }
9c02c129 10617 else
6ac97d4c 10618 search_path = xstrdup (debug_file_directory);
3019eac3 10619
492c0ab7 10620 flags = OPF_RETURN_REALPATH;
80626a55
DE
10621 if (is_dwp)
10622 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10623 desc = openp (search_path, flags, file_name,
3019eac3 10624 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10625 xfree (search_path);
3019eac3
DE
10626 if (desc < 0)
10627 return NULL;
10628
192b62ce 10629 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10630 xfree (absolute_name);
9c02c129
DE
10631 if (sym_bfd == NULL)
10632 return NULL;
192b62ce 10633 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10634
192b62ce
TT
10635 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10636 return NULL;
3019eac3 10637
13aaf454
DE
10638 /* Success. Record the bfd as having been included by the objfile's bfd.
10639 This is important because things like demangled_names_hash lives in the
10640 objfile's per_bfd space and may have references to things like symbol
10641 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10642 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10643
3019eac3
DE
10644 return sym_bfd;
10645}
10646
ab5088bf 10647/* Try to open DWO file FILE_NAME.
3019eac3
DE
10648 COMP_DIR is the DW_AT_comp_dir attribute.
10649 The result is the bfd handle of the file.
10650 If there is a problem finding or opening the file, return NULL.
10651 Upon success, the canonicalized path of the file is stored in the bfd,
10652 same as symfile_bfd_open. */
10653
192b62ce 10654static gdb_bfd_ref_ptr
ab5088bf 10655open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10656{
80626a55 10657 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10658 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10659
10660 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10661
10662 if (comp_dir != NULL)
10663 {
b36cec19
PA
10664 char *path_to_try = concat (comp_dir, SLASH_STRING,
10665 file_name, (char *) NULL);
3019eac3
DE
10666
10667 /* NOTE: If comp_dir is a relative path, this will also try the
10668 search path, which seems useful. */
192b62ce
TT
10669 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10670 1 /*search_cwd*/));
3019eac3
DE
10671 xfree (path_to_try);
10672 if (abfd != NULL)
10673 return abfd;
10674 }
10675
10676 /* That didn't work, try debug-file-directory, which, despite its name,
10677 is a list of paths. */
10678
10679 if (*debug_file_directory == '\0')
10680 return NULL;
10681
6ac97d4c 10682 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10683}
10684
80626a55
DE
10685/* This function is mapped across the sections and remembers the offset and
10686 size of each of the DWO debugging sections we are interested in. */
10687
10688static void
10689dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10690{
9a3c8263 10691 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10692 const struct dwop_section_names *names = &dwop_section_names;
10693
10694 if (section_is_p (sectp->name, &names->abbrev_dwo))
10695 {
049412e3 10696 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10697 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10698 }
10699 else if (section_is_p (sectp->name, &names->info_dwo))
10700 {
049412e3 10701 dwo_sections->info.s.section = sectp;
80626a55
DE
10702 dwo_sections->info.size = bfd_get_section_size (sectp);
10703 }
10704 else if (section_is_p (sectp->name, &names->line_dwo))
10705 {
049412e3 10706 dwo_sections->line.s.section = sectp;
80626a55
DE
10707 dwo_sections->line.size = bfd_get_section_size (sectp);
10708 }
10709 else if (section_is_p (sectp->name, &names->loc_dwo))
10710 {
049412e3 10711 dwo_sections->loc.s.section = sectp;
80626a55
DE
10712 dwo_sections->loc.size = bfd_get_section_size (sectp);
10713 }
10714 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10715 {
049412e3 10716 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10717 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10718 }
10719 else if (section_is_p (sectp->name, &names->macro_dwo))
10720 {
049412e3 10721 dwo_sections->macro.s.section = sectp;
80626a55
DE
10722 dwo_sections->macro.size = bfd_get_section_size (sectp);
10723 }
10724 else if (section_is_p (sectp->name, &names->str_dwo))
10725 {
049412e3 10726 dwo_sections->str.s.section = sectp;
80626a55
DE
10727 dwo_sections->str.size = bfd_get_section_size (sectp);
10728 }
10729 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10730 {
049412e3 10731 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10732 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10733 }
10734 else if (section_is_p (sectp->name, &names->types_dwo))
10735 {
10736 struct dwarf2_section_info type_section;
10737
10738 memset (&type_section, 0, sizeof (type_section));
049412e3 10739 type_section.s.section = sectp;
80626a55
DE
10740 type_section.size = bfd_get_section_size (sectp);
10741 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10742 &type_section);
10743 }
10744}
10745
ab5088bf 10746/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10747 by PER_CU. This is for the non-DWP case.
80626a55 10748 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10749
10750static struct dwo_file *
0ac5b59e
DE
10751open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10752 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10753{
10754 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10755 struct dwo_file *dwo_file;
3019eac3
DE
10756 struct cleanup *cleanups;
10757
192b62ce 10758 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10759 if (dbfd == NULL)
10760 {
b4f54984 10761 if (dwarf_read_debug)
80626a55
DE
10762 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10763 return NULL;
10764 }
10765 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10766 dwo_file->dwo_name = dwo_name;
10767 dwo_file->comp_dir = comp_dir;
192b62ce 10768 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10769
10770 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10771
192b62ce
TT
10772 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10773 &dwo_file->sections);
3019eac3 10774
19c3d4c9 10775 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10776
78d4d2c5
JK
10777 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10778 dwo_file->tus);
3019eac3
DE
10779
10780 discard_cleanups (cleanups);
10781
b4f54984 10782 if (dwarf_read_debug)
80626a55
DE
10783 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10784
3019eac3
DE
10785 return dwo_file;
10786}
10787
80626a55 10788/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10789 size of each of the DWP debugging sections common to version 1 and 2 that
10790 we are interested in. */
3019eac3 10791
80626a55 10792static void
73869dc2
DE
10793dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10794 void *dwp_file_ptr)
3019eac3 10795{
9a3c8263 10796 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10797 const struct dwop_section_names *names = &dwop_section_names;
10798 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10799
80626a55 10800 /* Record the ELF section number for later lookup: this is what the
73869dc2 10801 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10802 gdb_assert (elf_section_nr < dwp_file->num_sections);
10803 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10804
80626a55
DE
10805 /* Look for specific sections that we need. */
10806 if (section_is_p (sectp->name, &names->str_dwo))
10807 {
049412e3 10808 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10809 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10810 }
10811 else if (section_is_p (sectp->name, &names->cu_index))
10812 {
049412e3 10813 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10814 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10815 }
10816 else if (section_is_p (sectp->name, &names->tu_index))
10817 {
049412e3 10818 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10819 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10820 }
10821}
3019eac3 10822
73869dc2
DE
10823/* This function is mapped across the sections and remembers the offset and
10824 size of each of the DWP version 2 debugging sections that we are interested
10825 in. This is split into a separate function because we don't know if we
10826 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10827
10828static void
10829dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10830{
9a3c8263 10831 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10832 const struct dwop_section_names *names = &dwop_section_names;
10833 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10834
10835 /* Record the ELF section number for later lookup: this is what the
10836 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10837 gdb_assert (elf_section_nr < dwp_file->num_sections);
10838 dwp_file->elf_sections[elf_section_nr] = sectp;
10839
10840 /* Look for specific sections that we need. */
10841 if (section_is_p (sectp->name, &names->abbrev_dwo))
10842 {
049412e3 10843 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10844 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10845 }
10846 else if (section_is_p (sectp->name, &names->info_dwo))
10847 {
049412e3 10848 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10849 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10850 }
10851 else if (section_is_p (sectp->name, &names->line_dwo))
10852 {
049412e3 10853 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10854 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10855 }
10856 else if (section_is_p (sectp->name, &names->loc_dwo))
10857 {
049412e3 10858 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10859 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10860 }
10861 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10862 {
049412e3 10863 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10864 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10865 }
10866 else if (section_is_p (sectp->name, &names->macro_dwo))
10867 {
049412e3 10868 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10869 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10870 }
10871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10872 {
049412e3 10873 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10874 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10875 }
10876 else if (section_is_p (sectp->name, &names->types_dwo))
10877 {
049412e3 10878 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10879 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10880 }
10881}
10882
80626a55 10883/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10884
80626a55
DE
10885static hashval_t
10886hash_dwp_loaded_cutus (const void *item)
10887{
9a3c8263 10888 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10889
80626a55
DE
10890 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10891 return dwo_unit->signature;
3019eac3
DE
10892}
10893
80626a55 10894/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10895
80626a55
DE
10896static int
10897eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10898{
9a3c8263
SM
10899 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10900 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10901
80626a55
DE
10902 return dua->signature == dub->signature;
10903}
3019eac3 10904
80626a55 10905/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10906
80626a55
DE
10907static htab_t
10908allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10909{
10910 return htab_create_alloc_ex (3,
10911 hash_dwp_loaded_cutus,
10912 eq_dwp_loaded_cutus,
10913 NULL,
10914 &objfile->objfile_obstack,
10915 hashtab_obstack_allocate,
10916 dummy_obstack_deallocate);
10917}
3019eac3 10918
ab5088bf
DE
10919/* Try to open DWP file FILE_NAME.
10920 The result is the bfd handle of the file.
10921 If there is a problem finding or opening the file, return NULL.
10922 Upon success, the canonicalized path of the file is stored in the bfd,
10923 same as symfile_bfd_open. */
10924
192b62ce 10925static gdb_bfd_ref_ptr
ab5088bf
DE
10926open_dwp_file (const char *file_name)
10927{
192b62ce
TT
10928 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10929 1 /*search_cwd*/));
6ac97d4c
DE
10930 if (abfd != NULL)
10931 return abfd;
10932
10933 /* Work around upstream bug 15652.
10934 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10935 [Whether that's a "bug" is debatable, but it is getting in our way.]
10936 We have no real idea where the dwp file is, because gdb's realpath-ing
10937 of the executable's path may have discarded the needed info.
10938 [IWBN if the dwp file name was recorded in the executable, akin to
10939 .gnu_debuglink, but that doesn't exist yet.]
10940 Strip the directory from FILE_NAME and search again. */
10941 if (*debug_file_directory != '\0')
10942 {
10943 /* Don't implicitly search the current directory here.
10944 If the user wants to search "." to handle this case,
10945 it must be added to debug-file-directory. */
10946 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10947 0 /*search_cwd*/);
10948 }
10949
10950 return NULL;
ab5088bf
DE
10951}
10952
80626a55
DE
10953/* Initialize the use of the DWP file for the current objfile.
10954 By convention the name of the DWP file is ${objfile}.dwp.
10955 The result is NULL if it can't be found. */
a766d390 10956
80626a55 10957static struct dwp_file *
ab5088bf 10958open_and_init_dwp_file (void)
80626a55
DE
10959{
10960 struct objfile *objfile = dwarf2_per_objfile->objfile;
10961 struct dwp_file *dwp_file;
80626a55 10962
82bf32bc
JK
10963 /* Try to find first .dwp for the binary file before any symbolic links
10964 resolving. */
6c447423
DE
10965
10966 /* If the objfile is a debug file, find the name of the real binary
10967 file and get the name of dwp file from there. */
d721ba37 10968 std::string dwp_name;
6c447423
DE
10969 if (objfile->separate_debug_objfile_backlink != NULL)
10970 {
10971 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10972 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 10973
d721ba37 10974 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
10975 }
10976 else
d721ba37
PA
10977 dwp_name = objfile->original_name;
10978
10979 dwp_name += ".dwp";
80626a55 10980
d721ba37 10981 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
10982 if (dbfd == NULL
10983 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10984 {
10985 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
10986 dwp_name = objfile_name (objfile);
10987 dwp_name += ".dwp";
10988 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
10989 }
10990
80626a55
DE
10991 if (dbfd == NULL)
10992 {
b4f54984 10993 if (dwarf_read_debug)
d721ba37 10994 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 10995 return NULL;
3019eac3 10996 }
80626a55 10997 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10998 dwp_file->name = bfd_get_filename (dbfd.get ());
10999 dwp_file->dbfd = dbfd.release ();
c906108c 11000
80626a55 11001 /* +1: section 0 is unused */
192b62ce 11002 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11003 dwp_file->elf_sections =
11004 OBSTACK_CALLOC (&objfile->objfile_obstack,
11005 dwp_file->num_sections, asection *);
11006
192b62ce
TT
11007 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11008 dwp_file);
80626a55
DE
11009
11010 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11011
11012 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11013
73869dc2
DE
11014 /* The DWP file version is stored in the hash table. Oh well. */
11015 if (dwp_file->cus->version != dwp_file->tus->version)
11016 {
11017 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11018 pretty bizarre. We use pulongest here because that's the established
4d65956b 11019 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11020 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11021 " TU version %s [in DWP file %s]"),
11022 pulongest (dwp_file->cus->version),
d721ba37 11023 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11024 }
11025 dwp_file->version = dwp_file->cus->version;
11026
11027 if (dwp_file->version == 2)
192b62ce
TT
11028 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11029 dwp_file);
73869dc2 11030
19ac8c2e
DE
11031 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11032 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11033
b4f54984 11034 if (dwarf_read_debug)
80626a55
DE
11035 {
11036 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11037 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11038 " %s CUs, %s TUs\n",
11039 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11040 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11041 }
11042
11043 return dwp_file;
3019eac3 11044}
c906108c 11045
ab5088bf
DE
11046/* Wrapper around open_and_init_dwp_file, only open it once. */
11047
11048static struct dwp_file *
11049get_dwp_file (void)
11050{
11051 if (! dwarf2_per_objfile->dwp_checked)
11052 {
11053 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11054 dwarf2_per_objfile->dwp_checked = 1;
11055 }
11056 return dwarf2_per_objfile->dwp_file;
11057}
11058
80626a55
DE
11059/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11060 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11061 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11062 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11063 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11064
11065 This is called, for example, when wanting to read a variable with a
11066 complex location. Therefore we don't want to do file i/o for every call.
11067 Therefore we don't want to look for a DWO file on every call.
11068 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11069 then we check if we've already seen DWO_NAME, and only THEN do we check
11070 for a DWO file.
11071
1c658ad5 11072 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11073 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11074
3019eac3 11075static struct dwo_unit *
80626a55
DE
11076lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11077 const char *dwo_name, const char *comp_dir,
11078 ULONGEST signature, int is_debug_types)
3019eac3
DE
11079{
11080 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11081 const char *kind = is_debug_types ? "TU" : "CU";
11082 void **dwo_file_slot;
3019eac3 11083 struct dwo_file *dwo_file;
80626a55 11084 struct dwp_file *dwp_file;
cb1df416 11085
6a506a2d
DE
11086 /* First see if there's a DWP file.
11087 If we have a DWP file but didn't find the DWO inside it, don't
11088 look for the original DWO file. It makes gdb behave differently
11089 depending on whether one is debugging in the build tree. */
cf2c3c16 11090
ab5088bf 11091 dwp_file = get_dwp_file ();
80626a55 11092 if (dwp_file != NULL)
cf2c3c16 11093 {
80626a55
DE
11094 const struct dwp_hash_table *dwp_htab =
11095 is_debug_types ? dwp_file->tus : dwp_file->cus;
11096
11097 if (dwp_htab != NULL)
11098 {
11099 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11100 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11101 signature, is_debug_types);
80626a55
DE
11102
11103 if (dwo_cutu != NULL)
11104 {
b4f54984 11105 if (dwarf_read_debug)
80626a55
DE
11106 {
11107 fprintf_unfiltered (gdb_stdlog,
11108 "Virtual DWO %s %s found: @%s\n",
11109 kind, hex_string (signature),
11110 host_address_to_string (dwo_cutu));
11111 }
11112 return dwo_cutu;
11113 }
11114 }
11115 }
6a506a2d 11116 else
80626a55 11117 {
6a506a2d 11118 /* No DWP file, look for the DWO file. */
80626a55 11119
6a506a2d
DE
11120 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11121 if (*dwo_file_slot == NULL)
80626a55 11122 {
6a506a2d
DE
11123 /* Read in the file and build a table of the CUs/TUs it contains. */
11124 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11125 }
6a506a2d 11126 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11127 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11128
6a506a2d 11129 if (dwo_file != NULL)
19c3d4c9 11130 {
6a506a2d
DE
11131 struct dwo_unit *dwo_cutu = NULL;
11132
11133 if (is_debug_types && dwo_file->tus)
11134 {
11135 struct dwo_unit find_dwo_cutu;
11136
11137 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11138 find_dwo_cutu.signature = signature;
9a3c8263
SM
11139 dwo_cutu
11140 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11141 }
11142 else if (!is_debug_types && dwo_file->cu)
80626a55 11143 {
6a506a2d
DE
11144 if (signature == dwo_file->cu->signature)
11145 dwo_cutu = dwo_file->cu;
11146 }
11147
11148 if (dwo_cutu != NULL)
11149 {
b4f54984 11150 if (dwarf_read_debug)
6a506a2d
DE
11151 {
11152 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11153 kind, dwo_name, hex_string (signature),
11154 host_address_to_string (dwo_cutu));
11155 }
11156 return dwo_cutu;
80626a55
DE
11157 }
11158 }
2e276125 11159 }
9cdd5dbd 11160
80626a55
DE
11161 /* We didn't find it. This could mean a dwo_id mismatch, or
11162 someone deleted the DWO/DWP file, or the search path isn't set up
11163 correctly to find the file. */
11164
b4f54984 11165 if (dwarf_read_debug)
80626a55
DE
11166 {
11167 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11168 kind, dwo_name, hex_string (signature));
11169 }
3019eac3 11170
6656a72d
DE
11171 /* This is a warning and not a complaint because it can be caused by
11172 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11173 {
11174 /* Print the name of the DWP file if we looked there, helps the user
11175 better diagnose the problem. */
11176 char *dwp_text = NULL;
11177 struct cleanup *cleanups;
11178
11179 if (dwp_file != NULL)
11180 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11181 cleanups = make_cleanup (xfree, dwp_text);
11182
11183 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11184 " [in module %s]"),
11185 kind, dwo_name, hex_string (signature),
11186 dwp_text != NULL ? dwp_text : "",
11187 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11188 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11189
11190 do_cleanups (cleanups);
11191 }
3019eac3 11192 return NULL;
5fb290d7
DJ
11193}
11194
80626a55
DE
11195/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11196 See lookup_dwo_cutu_unit for details. */
11197
11198static struct dwo_unit *
11199lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11200 const char *dwo_name, const char *comp_dir,
11201 ULONGEST signature)
11202{
11203 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11204}
11205
11206/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11207 See lookup_dwo_cutu_unit for details. */
11208
11209static struct dwo_unit *
11210lookup_dwo_type_unit (struct signatured_type *this_tu,
11211 const char *dwo_name, const char *comp_dir)
11212{
11213 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11214}
11215
89e63ee4
DE
11216/* Traversal function for queue_and_load_all_dwo_tus. */
11217
11218static int
11219queue_and_load_dwo_tu (void **slot, void *info)
11220{
11221 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11222 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11223 ULONGEST signature = dwo_unit->signature;
11224 struct signatured_type *sig_type =
11225 lookup_dwo_signatured_type (per_cu->cu, signature);
11226
11227 if (sig_type != NULL)
11228 {
11229 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11230
11231 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11232 a real dependency of PER_CU on SIG_TYPE. That is detected later
11233 while processing PER_CU. */
11234 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11235 load_full_type_unit (sig_cu);
11236 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11237 }
11238
11239 return 1;
11240}
11241
11242/* Queue all TUs contained in the DWO of PER_CU to be read in.
11243 The DWO may have the only definition of the type, though it may not be
11244 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11245 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11246
11247static void
11248queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11249{
11250 struct dwo_unit *dwo_unit;
11251 struct dwo_file *dwo_file;
11252
11253 gdb_assert (!per_cu->is_debug_types);
11254 gdb_assert (get_dwp_file () == NULL);
11255 gdb_assert (per_cu->cu != NULL);
11256
11257 dwo_unit = per_cu->cu->dwo_unit;
11258 gdb_assert (dwo_unit != NULL);
11259
11260 dwo_file = dwo_unit->dwo_file;
11261 if (dwo_file->tus != NULL)
11262 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11263}
11264
3019eac3
DE
11265/* Free all resources associated with DWO_FILE.
11266 Close the DWO file and munmap the sections.
11267 All memory should be on the objfile obstack. */
348e048f
DE
11268
11269static void
3019eac3 11270free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11271{
348e048f 11272
5c6fa7ab 11273 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11274 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11275
3019eac3
DE
11276 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11277}
348e048f 11278
3019eac3 11279/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11280
3019eac3
DE
11281static void
11282free_dwo_file_cleanup (void *arg)
11283{
11284 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11285 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11286
3019eac3
DE
11287 free_dwo_file (dwo_file, objfile);
11288}
348e048f 11289
3019eac3 11290/* Traversal function for free_dwo_files. */
2ab95328 11291
3019eac3
DE
11292static int
11293free_dwo_file_from_slot (void **slot, void *info)
11294{
11295 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11296 struct objfile *objfile = (struct objfile *) info;
348e048f 11297
3019eac3 11298 free_dwo_file (dwo_file, objfile);
348e048f 11299
3019eac3
DE
11300 return 1;
11301}
348e048f 11302
3019eac3 11303/* Free all resources associated with DWO_FILES. */
348e048f 11304
3019eac3
DE
11305static void
11306free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11307{
11308 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11309}
3019eac3
DE
11310\f
11311/* Read in various DIEs. */
348e048f 11312
d389af10
JK
11313/* qsort helper for inherit_abstract_dies. */
11314
11315static int
11316unsigned_int_compar (const void *ap, const void *bp)
11317{
11318 unsigned int a = *(unsigned int *) ap;
11319 unsigned int b = *(unsigned int *) bp;
11320
11321 return (a > b) - (b > a);
11322}
11323
11324/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11325 Inherit only the children of the DW_AT_abstract_origin DIE not being
11326 already referenced by DW_AT_abstract_origin from the children of the
11327 current DIE. */
d389af10
JK
11328
11329static void
11330inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11331{
11332 struct die_info *child_die;
11333 unsigned die_children_count;
11334 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11335 sect_offset *offsets;
11336 sect_offset *offsets_end, *offsetp;
d389af10
JK
11337 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11338 struct die_info *origin_die;
11339 /* Iterator of the ORIGIN_DIE children. */
11340 struct die_info *origin_child_die;
11341 struct cleanup *cleanups;
11342 struct attribute *attr;
cd02d79d
PA
11343 struct dwarf2_cu *origin_cu;
11344 struct pending **origin_previous_list_in_scope;
d389af10
JK
11345
11346 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11347 if (!attr)
11348 return;
11349
cd02d79d
PA
11350 /* Note that following die references may follow to a die in a
11351 different cu. */
11352
11353 origin_cu = cu;
11354 origin_die = follow_die_ref (die, attr, &origin_cu);
11355
11356 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11357 symbols in. */
11358 origin_previous_list_in_scope = origin_cu->list_in_scope;
11359 origin_cu->list_in_scope = cu->list_in_scope;
11360
edb3359d
DJ
11361 if (die->tag != origin_die->tag
11362 && !(die->tag == DW_TAG_inlined_subroutine
11363 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11364 complaint (&symfile_complaints,
11365 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11366 to_underlying (die->sect_off),
11367 to_underlying (origin_die->sect_off));
d389af10
JK
11368
11369 child_die = die->child;
11370 die_children_count = 0;
11371 while (child_die && child_die->tag)
11372 {
11373 child_die = sibling_die (child_die);
11374 die_children_count++;
11375 }
8d749320 11376 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11377 cleanups = make_cleanup (xfree, offsets);
11378
11379 offsets_end = offsets;
3ea89b92
PMR
11380 for (child_die = die->child;
11381 child_die && child_die->tag;
11382 child_die = sibling_die (child_die))
11383 {
11384 struct die_info *child_origin_die;
11385 struct dwarf2_cu *child_origin_cu;
11386
11387 /* We are trying to process concrete instance entries:
216f72a1 11388 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11389 it's not relevant to our analysis here. i.e. detecting DIEs that are
11390 present in the abstract instance but not referenced in the concrete
11391 one. */
216f72a1
JK
11392 if (child_die->tag == DW_TAG_call_site
11393 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11394 continue;
11395
c38f313d
DJ
11396 /* For each CHILD_DIE, find the corresponding child of
11397 ORIGIN_DIE. If there is more than one layer of
11398 DW_AT_abstract_origin, follow them all; there shouldn't be,
11399 but GCC versions at least through 4.4 generate this (GCC PR
11400 40573). */
3ea89b92
PMR
11401 child_origin_die = child_die;
11402 child_origin_cu = cu;
c38f313d
DJ
11403 while (1)
11404 {
cd02d79d
PA
11405 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11406 child_origin_cu);
c38f313d
DJ
11407 if (attr == NULL)
11408 break;
cd02d79d
PA
11409 child_origin_die = follow_die_ref (child_origin_die, attr,
11410 &child_origin_cu);
c38f313d
DJ
11411 }
11412
d389af10
JK
11413 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11414 counterpart may exist. */
c38f313d 11415 if (child_origin_die != child_die)
d389af10 11416 {
edb3359d
DJ
11417 if (child_die->tag != child_origin_die->tag
11418 && !(child_die->tag == DW_TAG_inlined_subroutine
11419 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11420 complaint (&symfile_complaints,
11421 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11422 "different tags"),
11423 to_underlying (child_die->sect_off),
11424 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11425 if (child_origin_die->parent != origin_die)
11426 complaint (&symfile_complaints,
11427 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11428 "different parents"),
11429 to_underlying (child_die->sect_off),
11430 to_underlying (child_origin_die->sect_off));
c38f313d 11431 else
9c541725 11432 *offsets_end++ = child_origin_die->sect_off;
d389af10 11433 }
d389af10
JK
11434 }
11435 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11436 unsigned_int_compar);
11437 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11438 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11439 complaint (&symfile_complaints,
11440 _("Multiple children of DIE 0x%x refer "
11441 "to DIE 0x%x as their abstract origin"),
9c541725 11442 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11443
11444 offsetp = offsets;
11445 origin_child_die = origin_die->child;
11446 while (origin_child_die && origin_child_die->tag)
11447 {
11448 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11449 while (offsetp < offsets_end
9c541725 11450 && *offsetp < origin_child_die->sect_off)
d389af10 11451 offsetp++;
b64f50a1 11452 if (offsetp >= offsets_end
9c541725 11453 || *offsetp > origin_child_die->sect_off)
d389af10 11454 {
adde2bff
DE
11455 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11456 Check whether we're already processing ORIGIN_CHILD_DIE.
11457 This can happen with mutually referenced abstract_origins.
11458 PR 16581. */
11459 if (!origin_child_die->in_process)
11460 process_die (origin_child_die, origin_cu);
d389af10
JK
11461 }
11462 origin_child_die = sibling_die (origin_child_die);
11463 }
cd02d79d 11464 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11465
11466 do_cleanups (cleanups);
11467}
11468
c906108c 11469static void
e7c27a73 11470read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11471{
e7c27a73 11472 struct objfile *objfile = cu->objfile;
3e29f34a 11473 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11474 struct context_stack *newobj;
c906108c
SS
11475 CORE_ADDR lowpc;
11476 CORE_ADDR highpc;
11477 struct die_info *child_die;
edb3359d 11478 struct attribute *attr, *call_line, *call_file;
15d034d0 11479 const char *name;
e142c38c 11480 CORE_ADDR baseaddr;
801e3a5b 11481 struct block *block;
edb3359d 11482 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11483 VEC (symbolp) *template_args = NULL;
11484 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11485
11486 if (inlined_func)
11487 {
11488 /* If we do not have call site information, we can't show the
11489 caller of this inlined function. That's too confusing, so
11490 only use the scope for local variables. */
11491 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11492 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11493 if (call_line == NULL || call_file == NULL)
11494 {
11495 read_lexical_block_scope (die, cu);
11496 return;
11497 }
11498 }
c906108c 11499
e142c38c
DJ
11500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11501
94af9270 11502 name = dwarf2_name (die, cu);
c906108c 11503
e8d05480
JB
11504 /* Ignore functions with missing or empty names. These are actually
11505 illegal according to the DWARF standard. */
11506 if (name == NULL)
11507 {
11508 complaint (&symfile_complaints,
b64f50a1 11509 _("missing name for subprogram DIE at %d"),
9c541725 11510 to_underlying (die->sect_off));
e8d05480
JB
11511 return;
11512 }
11513
11514 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11515 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11516 <= PC_BOUNDS_INVALID)
e8d05480 11517 {
ae4d0c03
PM
11518 attr = dwarf2_attr (die, DW_AT_external, cu);
11519 if (!attr || !DW_UNSND (attr))
11520 complaint (&symfile_complaints,
3e43a32a
MS
11521 _("cannot get low and high bounds "
11522 "for subprogram DIE at %d"),
9c541725 11523 to_underlying (die->sect_off));
e8d05480
JB
11524 return;
11525 }
c906108c 11526
3e29f34a
MR
11527 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11528 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11529
34eaf542
TT
11530 /* If we have any template arguments, then we must allocate a
11531 different sort of symbol. */
11532 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11533 {
11534 if (child_die->tag == DW_TAG_template_type_param
11535 || child_die->tag == DW_TAG_template_value_param)
11536 {
e623cf5d 11537 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11538 templ_func->base.is_cplus_template_function = 1;
11539 break;
11540 }
11541 }
11542
fe978cb0
PA
11543 newobj = push_context (0, lowpc);
11544 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11545 (struct symbol *) templ_func);
4c2df51b 11546
4cecd739
DJ
11547 /* If there is a location expression for DW_AT_frame_base, record
11548 it. */
e142c38c 11549 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11550 if (attr)
fe978cb0 11551 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11552
63e43d3a
PMR
11553 /* If there is a location for the static link, record it. */
11554 newobj->static_link = NULL;
11555 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11556 if (attr)
11557 {
224c3ddb
SM
11558 newobj->static_link
11559 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11560 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11561 }
11562
e142c38c 11563 cu->list_in_scope = &local_symbols;
c906108c 11564
639d11d3 11565 if (die->child != NULL)
c906108c 11566 {
639d11d3 11567 child_die = die->child;
c906108c
SS
11568 while (child_die && child_die->tag)
11569 {
34eaf542
TT
11570 if (child_die->tag == DW_TAG_template_type_param
11571 || child_die->tag == DW_TAG_template_value_param)
11572 {
11573 struct symbol *arg = new_symbol (child_die, NULL, cu);
11574
f1078f66
DJ
11575 if (arg != NULL)
11576 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11577 }
11578 else
11579 process_die (child_die, cu);
c906108c
SS
11580 child_die = sibling_die (child_die);
11581 }
11582 }
11583
d389af10
JK
11584 inherit_abstract_dies (die, cu);
11585
4a811a97
UW
11586 /* If we have a DW_AT_specification, we might need to import using
11587 directives from the context of the specification DIE. See the
11588 comment in determine_prefix. */
11589 if (cu->language == language_cplus
11590 && dwarf2_attr (die, DW_AT_specification, cu))
11591 {
11592 struct dwarf2_cu *spec_cu = cu;
11593 struct die_info *spec_die = die_specification (die, &spec_cu);
11594
11595 while (spec_die)
11596 {
11597 child_die = spec_die->child;
11598 while (child_die && child_die->tag)
11599 {
11600 if (child_die->tag == DW_TAG_imported_module)
11601 process_die (child_die, spec_cu);
11602 child_die = sibling_die (child_die);
11603 }
11604
11605 /* In some cases, GCC generates specification DIEs that
11606 themselves contain DW_AT_specification attributes. */
11607 spec_die = die_specification (spec_die, &spec_cu);
11608 }
11609 }
11610
fe978cb0 11611 newobj = pop_context ();
c906108c 11612 /* Make a block for the local symbols within. */
fe978cb0 11613 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11614 newobj->static_link, lowpc, highpc);
801e3a5b 11615
df8a16a1 11616 /* For C++, set the block's scope. */
45280282
IB
11617 if ((cu->language == language_cplus
11618 || cu->language == language_fortran
c44af4eb
TT
11619 || cu->language == language_d
11620 || cu->language == language_rust)
4d4ec4e5 11621 && cu->processing_has_namespace_info)
195a3f6c
TT
11622 block_set_scope (block, determine_prefix (die, cu),
11623 &objfile->objfile_obstack);
df8a16a1 11624
801e3a5b
JB
11625 /* If we have address ranges, record them. */
11626 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11627
fe978cb0 11628 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11629
34eaf542
TT
11630 /* Attach template arguments to function. */
11631 if (! VEC_empty (symbolp, template_args))
11632 {
11633 gdb_assert (templ_func != NULL);
11634
11635 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11636 templ_func->template_arguments
8d749320
SM
11637 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11638 templ_func->n_template_arguments);
34eaf542
TT
11639 memcpy (templ_func->template_arguments,
11640 VEC_address (symbolp, template_args),
11641 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11642 VEC_free (symbolp, template_args);
11643 }
11644
208d8187
JB
11645 /* In C++, we can have functions nested inside functions (e.g., when
11646 a function declares a class that has methods). This means that
11647 when we finish processing a function scope, we may need to go
11648 back to building a containing block's symbol lists. */
fe978cb0 11649 local_symbols = newobj->locals;
22cee43f 11650 local_using_directives = newobj->local_using_directives;
208d8187 11651
921e78cf
JB
11652 /* If we've finished processing a top-level function, subsequent
11653 symbols go in the file symbol list. */
11654 if (outermost_context_p ())
e142c38c 11655 cu->list_in_scope = &file_symbols;
c906108c
SS
11656}
11657
11658/* Process all the DIES contained within a lexical block scope. Start
11659 a new scope, process the dies, and then close the scope. */
11660
11661static void
e7c27a73 11662read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11663{
e7c27a73 11664 struct objfile *objfile = cu->objfile;
3e29f34a 11665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11666 struct context_stack *newobj;
c906108c
SS
11667 CORE_ADDR lowpc, highpc;
11668 struct die_info *child_die;
e142c38c
DJ
11669 CORE_ADDR baseaddr;
11670
11671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11672
11673 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11674 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11675 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11676 be nasty. Might be easier to properly extend generic blocks to
af34e669 11677 describe ranges. */
e385593e
JK
11678 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11679 {
11680 case PC_BOUNDS_NOT_PRESENT:
11681 /* DW_TAG_lexical_block has no attributes, process its children as if
11682 there was no wrapping by that DW_TAG_lexical_block.
11683 GCC does no longer produces such DWARF since GCC r224161. */
11684 for (child_die = die->child;
11685 child_die != NULL && child_die->tag;
11686 child_die = sibling_die (child_die))
11687 process_die (child_die, cu);
11688 return;
11689 case PC_BOUNDS_INVALID:
11690 return;
11691 }
3e29f34a
MR
11692 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11693 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11694
11695 push_context (0, lowpc);
639d11d3 11696 if (die->child != NULL)
c906108c 11697 {
639d11d3 11698 child_die = die->child;
c906108c
SS
11699 while (child_die && child_die->tag)
11700 {
e7c27a73 11701 process_die (child_die, cu);
c906108c
SS
11702 child_die = sibling_die (child_die);
11703 }
11704 }
3ea89b92 11705 inherit_abstract_dies (die, cu);
fe978cb0 11706 newobj = pop_context ();
c906108c 11707
22cee43f 11708 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11709 {
801e3a5b 11710 struct block *block
63e43d3a 11711 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11712 newobj->start_addr, highpc);
801e3a5b
JB
11713
11714 /* Note that recording ranges after traversing children, as we
11715 do here, means that recording a parent's ranges entails
11716 walking across all its children's ranges as they appear in
11717 the address map, which is quadratic behavior.
11718
11719 It would be nicer to record the parent's ranges before
11720 traversing its children, simply overriding whatever you find
11721 there. But since we don't even decide whether to create a
11722 block until after we've traversed its children, that's hard
11723 to do. */
11724 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11725 }
fe978cb0 11726 local_symbols = newobj->locals;
22cee43f 11727 local_using_directives = newobj->local_using_directives;
c906108c
SS
11728}
11729
216f72a1 11730/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11731
11732static void
11733read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11734{
11735 struct objfile *objfile = cu->objfile;
11736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11737 CORE_ADDR pc, baseaddr;
11738 struct attribute *attr;
11739 struct call_site *call_site, call_site_local;
11740 void **slot;
11741 int nparams;
11742 struct die_info *child_die;
11743
11744 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11745
216f72a1
JK
11746 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11747 if (attr == NULL)
11748 {
11749 /* This was a pre-DWARF-5 GNU extension alias
11750 for DW_AT_call_return_pc. */
11751 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11752 }
96408a79
SA
11753 if (!attr)
11754 {
11755 complaint (&symfile_complaints,
216f72a1 11756 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11757 "DIE 0x%x [in module %s]"),
9c541725 11758 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11759 return;
11760 }
31aa7e4e 11761 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11762 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11763
11764 if (cu->call_site_htab == NULL)
11765 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11766 NULL, &objfile->objfile_obstack,
11767 hashtab_obstack_allocate, NULL);
11768 call_site_local.pc = pc;
11769 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11770 if (*slot != NULL)
11771 {
11772 complaint (&symfile_complaints,
216f72a1 11773 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11774 "DIE 0x%x [in module %s]"),
9c541725 11775 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11776 objfile_name (objfile));
96408a79
SA
11777 return;
11778 }
11779
11780 /* Count parameters at the caller. */
11781
11782 nparams = 0;
11783 for (child_die = die->child; child_die && child_die->tag;
11784 child_die = sibling_die (child_die))
11785 {
216f72a1
JK
11786 if (child_die->tag != DW_TAG_call_site_parameter
11787 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11788 {
11789 complaint (&symfile_complaints,
216f72a1
JK
11790 _("Tag %d is not DW_TAG_call_site_parameter in "
11791 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11792 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11793 objfile_name (objfile));
96408a79
SA
11794 continue;
11795 }
11796
11797 nparams++;
11798 }
11799
224c3ddb
SM
11800 call_site
11801 = ((struct call_site *)
11802 obstack_alloc (&objfile->objfile_obstack,
11803 sizeof (*call_site)
11804 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11805 *slot = call_site;
11806 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11807 call_site->pc = pc;
11808
216f72a1
JK
11809 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11810 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11811 {
11812 struct die_info *func_die;
11813
11814 /* Skip also over DW_TAG_inlined_subroutine. */
11815 for (func_die = die->parent;
11816 func_die && func_die->tag != DW_TAG_subprogram
11817 && func_die->tag != DW_TAG_subroutine_type;
11818 func_die = func_die->parent);
11819
216f72a1
JK
11820 /* DW_AT_call_all_calls is a superset
11821 of DW_AT_call_all_tail_calls. */
96408a79 11822 if (func_die
216f72a1 11823 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11824 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11825 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11826 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11827 {
11828 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11829 not complete. But keep CALL_SITE for look ups via call_site_htab,
11830 both the initial caller containing the real return address PC and
11831 the final callee containing the current PC of a chain of tail
11832 calls do not need to have the tail call list complete. But any
11833 function candidate for a virtual tail call frame searched via
11834 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11835 determined unambiguously. */
11836 }
11837 else
11838 {
11839 struct type *func_type = NULL;
11840
11841 if (func_die)
11842 func_type = get_die_type (func_die, cu);
11843 if (func_type != NULL)
11844 {
11845 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11846
11847 /* Enlist this call site to the function. */
11848 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11849 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11850 }
11851 else
11852 complaint (&symfile_complaints,
216f72a1 11853 _("Cannot find function owning DW_TAG_call_site "
96408a79 11854 "DIE 0x%x [in module %s]"),
9c541725 11855 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11856 }
11857 }
11858
216f72a1
JK
11859 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11860 if (attr == NULL)
11861 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11862 if (attr == NULL)
11863 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11864 if (attr == NULL)
216f72a1
JK
11865 {
11866 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11867 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11868 }
96408a79
SA
11869 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11870 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11871 /* Keep NULL DWARF_BLOCK. */;
11872 else if (attr_form_is_block (attr))
11873 {
11874 struct dwarf2_locexpr_baton *dlbaton;
11875
8d749320 11876 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11877 dlbaton->data = DW_BLOCK (attr)->data;
11878 dlbaton->size = DW_BLOCK (attr)->size;
11879 dlbaton->per_cu = cu->per_cu;
11880
11881 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11882 }
7771576e 11883 else if (attr_form_is_ref (attr))
96408a79 11884 {
96408a79
SA
11885 struct dwarf2_cu *target_cu = cu;
11886 struct die_info *target_die;
11887
ac9ec31b 11888 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11889 gdb_assert (target_cu->objfile == objfile);
11890 if (die_is_declaration (target_die, target_cu))
11891 {
7d45c7c3 11892 const char *target_physname;
9112db09
JK
11893
11894 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11895 target_physname = dwarf2_string_attr (target_die,
11896 DW_AT_linkage_name,
11897 target_cu);
11898 if (target_physname == NULL)
11899 target_physname = dwarf2_string_attr (target_die,
11900 DW_AT_MIPS_linkage_name,
11901 target_cu);
11902 if (target_physname == NULL)
9112db09 11903 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11904 if (target_physname == NULL)
11905 complaint (&symfile_complaints,
216f72a1 11906 _("DW_AT_call_target target DIE has invalid "
96408a79 11907 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 11908 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11909 else
7d455152 11910 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11911 }
11912 else
11913 {
11914 CORE_ADDR lowpc;
11915
11916 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11917 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11918 <= PC_BOUNDS_INVALID)
96408a79 11919 complaint (&symfile_complaints,
216f72a1 11920 _("DW_AT_call_target target DIE has invalid "
96408a79 11921 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 11922 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11923 else
3e29f34a
MR
11924 {
11925 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11926 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11927 }
96408a79
SA
11928 }
11929 }
11930 else
11931 complaint (&symfile_complaints,
216f72a1 11932 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11933 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 11934 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11935
11936 call_site->per_cu = cu->per_cu;
11937
11938 for (child_die = die->child;
11939 child_die && child_die->tag;
11940 child_die = sibling_die (child_die))
11941 {
96408a79 11942 struct call_site_parameter *parameter;
1788b2d3 11943 struct attribute *loc, *origin;
96408a79 11944
216f72a1
JK
11945 if (child_die->tag != DW_TAG_call_site_parameter
11946 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11947 {
11948 /* Already printed the complaint above. */
11949 continue;
11950 }
11951
11952 gdb_assert (call_site->parameter_count < nparams);
11953 parameter = &call_site->parameter[call_site->parameter_count];
11954
1788b2d3
JK
11955 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11956 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11957 register is contained in DW_AT_call_value. */
96408a79 11958
24c5c679 11959 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11960 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11961 if (origin == NULL)
11962 {
11963 /* This was a pre-DWARF-5 GNU extension alias
11964 for DW_AT_call_parameter. */
11965 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11966 }
7771576e 11967 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 11968 {
1788b2d3 11969 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
11970
11971 sect_offset sect_off
11972 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11973 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
11974 {
11975 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11976 binding can be done only inside one CU. Such referenced DIE
11977 therefore cannot be even moved to DW_TAG_partial_unit. */
11978 complaint (&symfile_complaints,
216f72a1
JK
11979 _("DW_AT_call_parameter offset is not in CU for "
11980 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
11981 to_underlying (child_die->sect_off),
11982 objfile_name (objfile));
d76b7dbc
JK
11983 continue;
11984 }
9c541725
PA
11985 parameter->u.param_cu_off
11986 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
11987 }
11988 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11989 {
11990 complaint (&symfile_complaints,
11991 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11992 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11993 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
11994 continue;
11995 }
24c5c679 11996 else
96408a79 11997 {
24c5c679
JK
11998 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11999 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12000 if (parameter->u.dwarf_reg != -1)
12001 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12002 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12003 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12004 &parameter->u.fb_offset))
12005 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12006 else
12007 {
12008 complaint (&symfile_complaints,
12009 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12010 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12011 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12012 "[in module %s]"),
9c541725
PA
12013 to_underlying (child_die->sect_off),
12014 objfile_name (objfile));
24c5c679
JK
12015 continue;
12016 }
96408a79
SA
12017 }
12018
216f72a1
JK
12019 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12020 if (attr == NULL)
12021 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12022 if (!attr_form_is_block (attr))
12023 {
12024 complaint (&symfile_complaints,
216f72a1
JK
12025 _("No DW_FORM_block* DW_AT_call_value for "
12026 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12027 to_underlying (child_die->sect_off),
12028 objfile_name (objfile));
96408a79
SA
12029 continue;
12030 }
12031 parameter->value = DW_BLOCK (attr)->data;
12032 parameter->value_size = DW_BLOCK (attr)->size;
12033
12034 /* Parameters are not pre-cleared by memset above. */
12035 parameter->data_value = NULL;
12036 parameter->data_value_size = 0;
12037 call_site->parameter_count++;
12038
216f72a1
JK
12039 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12040 if (attr == NULL)
12041 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12042 if (attr)
12043 {
12044 if (!attr_form_is_block (attr))
12045 complaint (&symfile_complaints,
216f72a1
JK
12046 _("No DW_FORM_block* DW_AT_call_data_value for "
12047 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12048 to_underlying (child_die->sect_off),
12049 objfile_name (objfile));
96408a79
SA
12050 else
12051 {
12052 parameter->data_value = DW_BLOCK (attr)->data;
12053 parameter->data_value_size = DW_BLOCK (attr)->size;
12054 }
12055 }
12056 }
12057}
12058
43988095
JK
12059/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12060 reading .debug_rnglists.
12061 Callback's type should be:
12062 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12063 Return true if the attributes are present and valid, otherwise,
12064 return false. */
12065
12066template <typename Callback>
12067static bool
12068dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12069 Callback &&callback)
12070{
12071 struct objfile *objfile = cu->objfile;
12072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12073 struct comp_unit_head *cu_header = &cu->header;
12074 bfd *obfd = objfile->obfd;
12075 unsigned int addr_size = cu_header->addr_size;
12076 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12077 /* Base address selection entry. */
12078 CORE_ADDR base;
12079 int found_base;
12080 unsigned int dummy;
12081 const gdb_byte *buffer;
12082 CORE_ADDR low = 0;
12083 CORE_ADDR high = 0;
12084 CORE_ADDR baseaddr;
12085 bool overflow = false;
12086
12087 found_base = cu->base_known;
12088 base = cu->base_address;
12089
12090 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12091 if (offset >= dwarf2_per_objfile->rnglists.size)
12092 {
12093 complaint (&symfile_complaints,
12094 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12095 offset);
12096 return false;
12097 }
12098 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12099
12100 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12101
12102 while (1)
12103 {
7814882a
JK
12104 /* Initialize it due to a false compiler warning. */
12105 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12106 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12107 + dwarf2_per_objfile->rnglists.size);
12108 unsigned int bytes_read;
12109
12110 if (buffer == buf_end)
12111 {
12112 overflow = true;
12113 break;
12114 }
12115 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12116 switch (rlet)
12117 {
12118 case DW_RLE_end_of_list:
12119 break;
12120 case DW_RLE_base_address:
12121 if (buffer + cu->header.addr_size > buf_end)
12122 {
12123 overflow = true;
12124 break;
12125 }
12126 base = read_address (obfd, buffer, cu, &bytes_read);
12127 found_base = 1;
12128 buffer += bytes_read;
12129 break;
12130 case DW_RLE_start_length:
12131 if (buffer + cu->header.addr_size > buf_end)
12132 {
12133 overflow = true;
12134 break;
12135 }
12136 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12137 buffer += bytes_read;
12138 range_end = (range_beginning
12139 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12140 buffer += bytes_read;
12141 if (buffer > buf_end)
12142 {
12143 overflow = true;
12144 break;
12145 }
12146 break;
12147 case DW_RLE_offset_pair:
12148 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12149 buffer += bytes_read;
12150 if (buffer > buf_end)
12151 {
12152 overflow = true;
12153 break;
12154 }
12155 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12156 buffer += bytes_read;
12157 if (buffer > buf_end)
12158 {
12159 overflow = true;
12160 break;
12161 }
12162 break;
12163 case DW_RLE_start_end:
12164 if (buffer + 2 * cu->header.addr_size > buf_end)
12165 {
12166 overflow = true;
12167 break;
12168 }
12169 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12170 buffer += bytes_read;
12171 range_end = read_address (obfd, buffer, cu, &bytes_read);
12172 buffer += bytes_read;
12173 break;
12174 default:
12175 complaint (&symfile_complaints,
12176 _("Invalid .debug_rnglists data (no base address)"));
12177 return false;
12178 }
12179 if (rlet == DW_RLE_end_of_list || overflow)
12180 break;
12181 if (rlet == DW_RLE_base_address)
12182 continue;
12183
12184 if (!found_base)
12185 {
12186 /* We have no valid base address for the ranges
12187 data. */
12188 complaint (&symfile_complaints,
12189 _("Invalid .debug_rnglists data (no base address)"));
12190 return false;
12191 }
12192
12193 if (range_beginning > range_end)
12194 {
12195 /* Inverted range entries are invalid. */
12196 complaint (&symfile_complaints,
12197 _("Invalid .debug_rnglists data (inverted range)"));
12198 return false;
12199 }
12200
12201 /* Empty range entries have no effect. */
12202 if (range_beginning == range_end)
12203 continue;
12204
12205 range_beginning += base;
12206 range_end += base;
12207
12208 /* A not-uncommon case of bad debug info.
12209 Don't pollute the addrmap with bad data. */
12210 if (range_beginning + baseaddr == 0
12211 && !dwarf2_per_objfile->has_section_at_zero)
12212 {
12213 complaint (&symfile_complaints,
12214 _(".debug_rnglists entry has start address of zero"
12215 " [in module %s]"), objfile_name (objfile));
12216 continue;
12217 }
12218
12219 callback (range_beginning, range_end);
12220 }
12221
12222 if (overflow)
12223 {
12224 complaint (&symfile_complaints,
12225 _("Offset %d is not terminated "
12226 "for DW_AT_ranges attribute"),
12227 offset);
12228 return false;
12229 }
12230
12231 return true;
12232}
12233
12234/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12235 Callback's type should be:
12236 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12237 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12238
43988095 12239template <typename Callback>
43039443 12240static int
5f46c5a5 12241dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12242 Callback &&callback)
43039443
JK
12243{
12244 struct objfile *objfile = cu->objfile;
3e29f34a 12245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12246 struct comp_unit_head *cu_header = &cu->header;
12247 bfd *obfd = objfile->obfd;
12248 unsigned int addr_size = cu_header->addr_size;
12249 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12250 /* Base address selection entry. */
12251 CORE_ADDR base;
12252 int found_base;
12253 unsigned int dummy;
d521ce57 12254 const gdb_byte *buffer;
ff013f42 12255 CORE_ADDR baseaddr;
43039443 12256
43988095
JK
12257 if (cu_header->version >= 5)
12258 return dwarf2_rnglists_process (offset, cu, callback);
12259
d00adf39
DE
12260 found_base = cu->base_known;
12261 base = cu->base_address;
43039443 12262
be391dca 12263 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12264 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12265 {
12266 complaint (&symfile_complaints,
12267 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12268 offset);
12269 return 0;
12270 }
dce234bc 12271 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12272
e7030f15 12273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12274
43039443
JK
12275 while (1)
12276 {
12277 CORE_ADDR range_beginning, range_end;
12278
12279 range_beginning = read_address (obfd, buffer, cu, &dummy);
12280 buffer += addr_size;
12281 range_end = read_address (obfd, buffer, cu, &dummy);
12282 buffer += addr_size;
12283 offset += 2 * addr_size;
12284
12285 /* An end of list marker is a pair of zero addresses. */
12286 if (range_beginning == 0 && range_end == 0)
12287 /* Found the end of list entry. */
12288 break;
12289
12290 /* Each base address selection entry is a pair of 2 values.
12291 The first is the largest possible address, the second is
12292 the base address. Check for a base address here. */
12293 if ((range_beginning & mask) == mask)
12294 {
28d2bfb9
AB
12295 /* If we found the largest possible address, then we already
12296 have the base address in range_end. */
12297 base = range_end;
43039443
JK
12298 found_base = 1;
12299 continue;
12300 }
12301
12302 if (!found_base)
12303 {
12304 /* We have no valid base address for the ranges
12305 data. */
12306 complaint (&symfile_complaints,
12307 _("Invalid .debug_ranges data (no base address)"));
12308 return 0;
12309 }
12310
9277c30c
UW
12311 if (range_beginning > range_end)
12312 {
12313 /* Inverted range entries are invalid. */
12314 complaint (&symfile_complaints,
12315 _("Invalid .debug_ranges data (inverted range)"));
12316 return 0;
12317 }
12318
12319 /* Empty range entries have no effect. */
12320 if (range_beginning == range_end)
12321 continue;
12322
43039443
JK
12323 range_beginning += base;
12324 range_end += base;
12325
01093045
DE
12326 /* A not-uncommon case of bad debug info.
12327 Don't pollute the addrmap with bad data. */
12328 if (range_beginning + baseaddr == 0
12329 && !dwarf2_per_objfile->has_section_at_zero)
12330 {
12331 complaint (&symfile_complaints,
12332 _(".debug_ranges entry has start address of zero"
4262abfb 12333 " [in module %s]"), objfile_name (objfile));
01093045
DE
12334 continue;
12335 }
12336
5f46c5a5
JK
12337 callback (range_beginning, range_end);
12338 }
12339
12340 return 1;
12341}
12342
12343/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12344 Return 1 if the attributes are present and valid, otherwise, return 0.
12345 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12346
12347static int
12348dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12349 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12350 struct partial_symtab *ranges_pst)
12351{
12352 struct objfile *objfile = cu->objfile;
12353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12354 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12355 SECT_OFF_TEXT (objfile));
12356 int low_set = 0;
12357 CORE_ADDR low = 0;
12358 CORE_ADDR high = 0;
12359 int retval;
12360
12361 retval = dwarf2_ranges_process (offset, cu,
12362 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12363 {
9277c30c 12364 if (ranges_pst != NULL)
3e29f34a
MR
12365 {
12366 CORE_ADDR lowpc;
12367 CORE_ADDR highpc;
12368
12369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12370 range_beginning + baseaddr);
12371 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12372 range_end + baseaddr);
12373 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12374 ranges_pst);
12375 }
ff013f42 12376
43039443
JK
12377 /* FIXME: This is recording everything as a low-high
12378 segment of consecutive addresses. We should have a
12379 data structure for discontiguous block ranges
12380 instead. */
12381 if (! low_set)
12382 {
12383 low = range_beginning;
12384 high = range_end;
12385 low_set = 1;
12386 }
12387 else
12388 {
12389 if (range_beginning < low)
12390 low = range_beginning;
12391 if (range_end > high)
12392 high = range_end;
12393 }
5f46c5a5
JK
12394 });
12395 if (!retval)
12396 return 0;
43039443
JK
12397
12398 if (! low_set)
12399 /* If the first entry is an end-of-list marker, the range
12400 describes an empty scope, i.e. no instructions. */
12401 return 0;
12402
12403 if (low_return)
12404 *low_return = low;
12405 if (high_return)
12406 *high_return = high;
12407 return 1;
12408}
12409
3a2b436a
JK
12410/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12411 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12412 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12413
3a2b436a 12414static enum pc_bounds_kind
af34e669 12415dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12416 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12417 struct partial_symtab *pst)
c906108c
SS
12418{
12419 struct attribute *attr;
91da1414 12420 struct attribute *attr_high;
af34e669
DJ
12421 CORE_ADDR low = 0;
12422 CORE_ADDR high = 0;
e385593e 12423 enum pc_bounds_kind ret;
c906108c 12424
91da1414
MW
12425 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12426 if (attr_high)
af34e669 12427 {
e142c38c 12428 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12429 if (attr)
91da1414 12430 {
31aa7e4e
JB
12431 low = attr_value_as_address (attr);
12432 high = attr_value_as_address (attr_high);
12433 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12434 high += low;
91da1414 12435 }
af34e669
DJ
12436 else
12437 /* Found high w/o low attribute. */
e385593e 12438 return PC_BOUNDS_INVALID;
af34e669
DJ
12439
12440 /* Found consecutive range of addresses. */
3a2b436a 12441 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12442 }
c906108c 12443 else
af34e669 12444 {
e142c38c 12445 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12446 if (attr != NULL)
12447 {
ab435259
DE
12448 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12449 We take advantage of the fact that DW_AT_ranges does not appear
12450 in DW_TAG_compile_unit of DWO files. */
12451 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12452 unsigned int ranges_offset = (DW_UNSND (attr)
12453 + (need_ranges_base
12454 ? cu->ranges_base
12455 : 0));
2e3cf129 12456
af34e669 12457 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12458 .debug_ranges section. */
2e3cf129 12459 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12460 return PC_BOUNDS_INVALID;
43039443 12461 /* Found discontinuous range of addresses. */
3a2b436a 12462 ret = PC_BOUNDS_RANGES;
af34e669 12463 }
e385593e
JK
12464 else
12465 return PC_BOUNDS_NOT_PRESENT;
af34e669 12466 }
c906108c 12467
9373cf26
JK
12468 /* read_partial_die has also the strict LOW < HIGH requirement. */
12469 if (high <= low)
e385593e 12470 return PC_BOUNDS_INVALID;
c906108c
SS
12471
12472 /* When using the GNU linker, .gnu.linkonce. sections are used to
12473 eliminate duplicate copies of functions and vtables and such.
12474 The linker will arbitrarily choose one and discard the others.
12475 The AT_*_pc values for such functions refer to local labels in
12476 these sections. If the section from that file was discarded, the
12477 labels are not in the output, so the relocs get a value of 0.
12478 If this is a discarded function, mark the pc bounds as invalid,
12479 so that GDB will ignore it. */
72dca2f5 12480 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12481 return PC_BOUNDS_INVALID;
c906108c
SS
12482
12483 *lowpc = low;
96408a79
SA
12484 if (highpc)
12485 *highpc = high;
af34e669 12486 return ret;
c906108c
SS
12487}
12488
b084d499
JB
12489/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12490 its low and high PC addresses. Do nothing if these addresses could not
12491 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12492 and HIGHPC to the high address if greater than HIGHPC. */
12493
12494static void
12495dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12496 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12497 struct dwarf2_cu *cu)
12498{
12499 CORE_ADDR low, high;
12500 struct die_info *child = die->child;
12501
e385593e 12502 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12503 {
325fac50
PA
12504 *lowpc = std::min (*lowpc, low);
12505 *highpc = std::max (*highpc, high);
b084d499
JB
12506 }
12507
12508 /* If the language does not allow nested subprograms (either inside
12509 subprograms or lexical blocks), we're done. */
12510 if (cu->language != language_ada)
12511 return;
6e70227d 12512
b084d499
JB
12513 /* Check all the children of the given DIE. If it contains nested
12514 subprograms, then check their pc bounds. Likewise, we need to
12515 check lexical blocks as well, as they may also contain subprogram
12516 definitions. */
12517 while (child && child->tag)
12518 {
12519 if (child->tag == DW_TAG_subprogram
12520 || child->tag == DW_TAG_lexical_block)
12521 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12522 child = sibling_die (child);
12523 }
12524}
12525
fae299cd
DC
12526/* Get the low and high pc's represented by the scope DIE, and store
12527 them in *LOWPC and *HIGHPC. If the correct values can't be
12528 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12529
12530static void
12531get_scope_pc_bounds (struct die_info *die,
12532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12533 struct dwarf2_cu *cu)
12534{
12535 CORE_ADDR best_low = (CORE_ADDR) -1;
12536 CORE_ADDR best_high = (CORE_ADDR) 0;
12537 CORE_ADDR current_low, current_high;
12538
3a2b436a 12539 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12540 >= PC_BOUNDS_RANGES)
fae299cd
DC
12541 {
12542 best_low = current_low;
12543 best_high = current_high;
12544 }
12545 else
12546 {
12547 struct die_info *child = die->child;
12548
12549 while (child && child->tag)
12550 {
12551 switch (child->tag) {
12552 case DW_TAG_subprogram:
b084d499 12553 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12554 break;
12555 case DW_TAG_namespace:
f55ee35c 12556 case DW_TAG_module:
fae299cd
DC
12557 /* FIXME: carlton/2004-01-16: Should we do this for
12558 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12559 that current GCC's always emit the DIEs corresponding
12560 to definitions of methods of classes as children of a
12561 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12562 the DIEs giving the declarations, which could be
12563 anywhere). But I don't see any reason why the
12564 standards says that they have to be there. */
12565 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12566
12567 if (current_low != ((CORE_ADDR) -1))
12568 {
325fac50
PA
12569 best_low = std::min (best_low, current_low);
12570 best_high = std::max (best_high, current_high);
fae299cd
DC
12571 }
12572 break;
12573 default:
0963b4bd 12574 /* Ignore. */
fae299cd
DC
12575 break;
12576 }
12577
12578 child = sibling_die (child);
12579 }
12580 }
12581
12582 *lowpc = best_low;
12583 *highpc = best_high;
12584}
12585
801e3a5b
JB
12586/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12587 in DIE. */
380bca97 12588
801e3a5b
JB
12589static void
12590dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12591 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12592{
bb5ed363 12593 struct objfile *objfile = cu->objfile;
3e29f34a 12594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12595 struct attribute *attr;
91da1414 12596 struct attribute *attr_high;
801e3a5b 12597
91da1414
MW
12598 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12599 if (attr_high)
801e3a5b 12600 {
801e3a5b
JB
12601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12602 if (attr)
12603 {
31aa7e4e
JB
12604 CORE_ADDR low = attr_value_as_address (attr);
12605 CORE_ADDR high = attr_value_as_address (attr_high);
12606
12607 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12608 high += low;
9a619af0 12609
3e29f34a
MR
12610 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12611 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12612 record_block_range (block, low, high - 1);
801e3a5b
JB
12613 }
12614 }
12615
12616 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12617 if (attr)
12618 {
bb5ed363 12619 bfd *obfd = objfile->obfd;
ab435259
DE
12620 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12621 We take advantage of the fact that DW_AT_ranges does not appear
12622 in DW_TAG_compile_unit of DWO files. */
12623 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12624
12625 /* The value of the DW_AT_ranges attribute is the offset of the
12626 address range list in the .debug_ranges section. */
ab435259
DE
12627 unsigned long offset = (DW_UNSND (attr)
12628 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12629 const gdb_byte *buffer;
801e3a5b
JB
12630
12631 /* For some target architectures, but not others, the
12632 read_address function sign-extends the addresses it returns.
12633 To recognize base address selection entries, we need a
12634 mask. */
12635 unsigned int addr_size = cu->header.addr_size;
12636 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12637
12638 /* The base address, to which the next pair is relative. Note
12639 that this 'base' is a DWARF concept: most entries in a range
12640 list are relative, to reduce the number of relocs against the
12641 debugging information. This is separate from this function's
12642 'baseaddr' argument, which GDB uses to relocate debugging
12643 information from a shared library based on the address at
12644 which the library was loaded. */
d00adf39
DE
12645 CORE_ADDR base = cu->base_address;
12646 int base_known = cu->base_known;
801e3a5b 12647
5f46c5a5
JK
12648 dwarf2_ranges_process (offset, cu,
12649 [&] (CORE_ADDR start, CORE_ADDR end)
12650 {
58fdfd2c
JK
12651 start += baseaddr;
12652 end += baseaddr;
5f46c5a5
JK
12653 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12654 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12655 record_block_range (block, start, end - 1);
12656 });
801e3a5b
JB
12657 }
12658}
12659
685b1105
JK
12660/* Check whether the producer field indicates either of GCC < 4.6, or the
12661 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12662
685b1105
JK
12663static void
12664check_producer (struct dwarf2_cu *cu)
60d5a603 12665{
38360086 12666 int major, minor;
60d5a603
JK
12667
12668 if (cu->producer == NULL)
12669 {
12670 /* For unknown compilers expect their behavior is DWARF version
12671 compliant.
12672
12673 GCC started to support .debug_types sections by -gdwarf-4 since
12674 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12675 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12676 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12677 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12678 }
b1ffba5a 12679 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12680 {
38360086
MW
12681 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12682 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12683 }
61012eef 12684 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12685 cu->producer_is_icc = 1;
12686 else
12687 {
12688 /* For other non-GCC compilers, expect their behavior is DWARF version
12689 compliant. */
60d5a603
JK
12690 }
12691
ba919b58 12692 cu->checked_producer = 1;
685b1105 12693}
ba919b58 12694
685b1105
JK
12695/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12696 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12697 during 4.6.0 experimental. */
12698
12699static int
12700producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12701{
12702 if (!cu->checked_producer)
12703 check_producer (cu);
12704
12705 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12706}
12707
12708/* Return the default accessibility type if it is not overriden by
12709 DW_AT_accessibility. */
12710
12711static enum dwarf_access_attribute
12712dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12713{
12714 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12715 {
12716 /* The default DWARF 2 accessibility for members is public, the default
12717 accessibility for inheritance is private. */
12718
12719 if (die->tag != DW_TAG_inheritance)
12720 return DW_ACCESS_public;
12721 else
12722 return DW_ACCESS_private;
12723 }
12724 else
12725 {
12726 /* DWARF 3+ defines the default accessibility a different way. The same
12727 rules apply now for DW_TAG_inheritance as for the members and it only
12728 depends on the container kind. */
12729
12730 if (die->parent->tag == DW_TAG_class_type)
12731 return DW_ACCESS_private;
12732 else
12733 return DW_ACCESS_public;
12734 }
12735}
12736
74ac6d43
TT
12737/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12738 offset. If the attribute was not found return 0, otherwise return
12739 1. If it was found but could not properly be handled, set *OFFSET
12740 to 0. */
12741
12742static int
12743handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12744 LONGEST *offset)
12745{
12746 struct attribute *attr;
12747
12748 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12749 if (attr != NULL)
12750 {
12751 *offset = 0;
12752
12753 /* Note that we do not check for a section offset first here.
12754 This is because DW_AT_data_member_location is new in DWARF 4,
12755 so if we see it, we can assume that a constant form is really
12756 a constant and not a section offset. */
12757 if (attr_form_is_constant (attr))
12758 *offset = dwarf2_get_attr_constant_value (attr, 0);
12759 else if (attr_form_is_section_offset (attr))
12760 dwarf2_complex_location_expr_complaint ();
12761 else if (attr_form_is_block (attr))
12762 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12763 else
12764 dwarf2_complex_location_expr_complaint ();
12765
12766 return 1;
12767 }
12768
12769 return 0;
12770}
12771
c906108c
SS
12772/* Add an aggregate field to the field list. */
12773
12774static void
107d2387 12775dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12776 struct dwarf2_cu *cu)
6e70227d 12777{
e7c27a73 12778 struct objfile *objfile = cu->objfile;
5e2b427d 12779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12780 struct nextfield *new_field;
12781 struct attribute *attr;
12782 struct field *fp;
15d034d0 12783 const char *fieldname = "";
c906108c
SS
12784
12785 /* Allocate a new field list entry and link it in. */
8d749320 12786 new_field = XNEW (struct nextfield);
b8c9b27d 12787 make_cleanup (xfree, new_field);
c906108c 12788 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12789
12790 if (die->tag == DW_TAG_inheritance)
12791 {
12792 new_field->next = fip->baseclasses;
12793 fip->baseclasses = new_field;
12794 }
12795 else
12796 {
12797 new_field->next = fip->fields;
12798 fip->fields = new_field;
12799 }
c906108c
SS
12800 fip->nfields++;
12801
e142c38c 12802 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12803 if (attr)
12804 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12805 else
12806 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12807 if (new_field->accessibility != DW_ACCESS_public)
12808 fip->non_public_fields = 1;
60d5a603 12809
e142c38c 12810 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12811 if (attr)
12812 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12813 else
12814 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12815
12816 fp = &new_field->field;
a9a9bd0f 12817
e142c38c 12818 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12819 {
74ac6d43
TT
12820 LONGEST offset;
12821
a9a9bd0f 12822 /* Data member other than a C++ static data member. */
6e70227d 12823
c906108c 12824 /* Get type of field. */
e7c27a73 12825 fp->type = die_type (die, cu);
c906108c 12826
d6a843b5 12827 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12828
c906108c 12829 /* Get bit size of field (zero if none). */
e142c38c 12830 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12831 if (attr)
12832 {
12833 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12834 }
12835 else
12836 {
12837 FIELD_BITSIZE (*fp) = 0;
12838 }
12839
12840 /* Get bit offset of field. */
74ac6d43
TT
12841 if (handle_data_member_location (die, cu, &offset))
12842 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12843 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12844 if (attr)
12845 {
5e2b427d 12846 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12847 {
12848 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12849 additional bit offset from the MSB of the containing
12850 anonymous object to the MSB of the field. We don't
12851 have to do anything special since we don't need to
12852 know the size of the anonymous object. */
f41f5e61 12853 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12854 }
12855 else
12856 {
12857 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12858 MSB of the anonymous object, subtract off the number of
12859 bits from the MSB of the field to the MSB of the
12860 object, and then subtract off the number of bits of
12861 the field itself. The result is the bit offset of
12862 the LSB of the field. */
c906108c
SS
12863 int anonymous_size;
12864 int bit_offset = DW_UNSND (attr);
12865
e142c38c 12866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12867 if (attr)
12868 {
12869 /* The size of the anonymous object containing
12870 the bit field is explicit, so use the
12871 indicated size (in bytes). */
12872 anonymous_size = DW_UNSND (attr);
12873 }
12874 else
12875 {
12876 /* The size of the anonymous object containing
12877 the bit field must be inferred from the type
12878 attribute of the data member containing the
12879 bit field. */
12880 anonymous_size = TYPE_LENGTH (fp->type);
12881 }
f41f5e61
PA
12882 SET_FIELD_BITPOS (*fp,
12883 (FIELD_BITPOS (*fp)
12884 + anonymous_size * bits_per_byte
12885 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12886 }
12887 }
da5b30da
AA
12888 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12889 if (attr != NULL)
12890 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12891 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12892
12893 /* Get name of field. */
39cbfefa
DJ
12894 fieldname = dwarf2_name (die, cu);
12895 if (fieldname == NULL)
12896 fieldname = "";
d8151005
DJ
12897
12898 /* The name is already allocated along with this objfile, so we don't
12899 need to duplicate it for the type. */
12900 fp->name = fieldname;
c906108c
SS
12901
12902 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12903 pointer or virtual base class pointer) to private. */
e142c38c 12904 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12905 {
d48cc9dd 12906 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12907 new_field->accessibility = DW_ACCESS_private;
12908 fip->non_public_fields = 1;
12909 }
12910 }
a9a9bd0f 12911 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12912 {
a9a9bd0f
DC
12913 /* C++ static member. */
12914
12915 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12916 is a declaration, but all versions of G++ as of this writing
12917 (so through at least 3.2.1) incorrectly generate
12918 DW_TAG_variable tags. */
6e70227d 12919
ff355380 12920 const char *physname;
c906108c 12921
a9a9bd0f 12922 /* Get name of field. */
39cbfefa
DJ
12923 fieldname = dwarf2_name (die, cu);
12924 if (fieldname == NULL)
c906108c
SS
12925 return;
12926
254e6b9e 12927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12928 if (attr
12929 /* Only create a symbol if this is an external value.
12930 new_symbol checks this and puts the value in the global symbol
12931 table, which we want. If it is not external, new_symbol
12932 will try to put the value in cu->list_in_scope which is wrong. */
12933 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12934 {
12935 /* A static const member, not much different than an enum as far as
12936 we're concerned, except that we can support more types. */
12937 new_symbol (die, NULL, cu);
12938 }
12939
2df3850c 12940 /* Get physical name. */
ff355380 12941 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12942
d8151005
DJ
12943 /* The name is already allocated along with this objfile, so we don't
12944 need to duplicate it for the type. */
12945 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12946 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12947 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12948 }
12949 else if (die->tag == DW_TAG_inheritance)
12950 {
74ac6d43 12951 LONGEST offset;
d4b96c9a 12952
74ac6d43
TT
12953 /* C++ base class field. */
12954 if (handle_data_member_location (die, cu, &offset))
12955 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12956 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12957 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12958 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12959 fip->nbaseclasses++;
12960 }
12961}
12962
98751a41
JK
12963/* Add a typedef defined in the scope of the FIP's class. */
12964
12965static void
12966dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12967 struct dwarf2_cu *cu)
6e70227d 12968{
98751a41 12969 struct typedef_field_list *new_field;
98751a41 12970 struct typedef_field *fp;
98751a41
JK
12971
12972 /* Allocate a new field list entry and link it in. */
8d749320 12973 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12974 make_cleanup (xfree, new_field);
12975
12976 gdb_assert (die->tag == DW_TAG_typedef);
12977
12978 fp = &new_field->field;
12979
12980 /* Get name of field. */
12981 fp->name = dwarf2_name (die, cu);
12982 if (fp->name == NULL)
12983 return;
12984
12985 fp->type = read_type_die (die, cu);
12986
12987 new_field->next = fip->typedef_field_list;
12988 fip->typedef_field_list = new_field;
12989 fip->typedef_field_list_count++;
12990}
12991
c906108c
SS
12992/* Create the vector of fields, and attach it to the type. */
12993
12994static void
fba45db2 12995dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12996 struct dwarf2_cu *cu)
c906108c
SS
12997{
12998 int nfields = fip->nfields;
12999
13000 /* Record the field count, allocate space for the array of fields,
13001 and create blank accessibility bitfields if necessary. */
13002 TYPE_NFIELDS (type) = nfields;
13003 TYPE_FIELDS (type) = (struct field *)
13004 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13005 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13006
b4ba55a1 13007 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13008 {
13009 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13010
13011 TYPE_FIELD_PRIVATE_BITS (type) =
13012 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13013 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13014
13015 TYPE_FIELD_PROTECTED_BITS (type) =
13016 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13017 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13018
774b6a14
TT
13019 TYPE_FIELD_IGNORE_BITS (type) =
13020 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13021 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13022 }
13023
13024 /* If the type has baseclasses, allocate and clear a bit vector for
13025 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13026 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13027 {
13028 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13029 unsigned char *pointer;
c906108c
SS
13030
13031 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13032 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13033 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13034 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13035 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13036 }
13037
3e43a32a
MS
13038 /* Copy the saved-up fields into the field vector. Start from the head of
13039 the list, adding to the tail of the field array, so that they end up in
13040 the same order in the array in which they were added to the list. */
c906108c
SS
13041 while (nfields-- > 0)
13042 {
7d0ccb61
DJ
13043 struct nextfield *fieldp;
13044
13045 if (fip->fields)
13046 {
13047 fieldp = fip->fields;
13048 fip->fields = fieldp->next;
13049 }
13050 else
13051 {
13052 fieldp = fip->baseclasses;
13053 fip->baseclasses = fieldp->next;
13054 }
13055
13056 TYPE_FIELD (type, nfields) = fieldp->field;
13057 switch (fieldp->accessibility)
c906108c 13058 {
c5aa993b 13059 case DW_ACCESS_private:
b4ba55a1
JB
13060 if (cu->language != language_ada)
13061 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13062 break;
c906108c 13063
c5aa993b 13064 case DW_ACCESS_protected:
b4ba55a1
JB
13065 if (cu->language != language_ada)
13066 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13067 break;
c906108c 13068
c5aa993b
JM
13069 case DW_ACCESS_public:
13070 break;
c906108c 13071
c5aa993b
JM
13072 default:
13073 /* Unknown accessibility. Complain and treat it as public. */
13074 {
e2e0b3e5 13075 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13076 fieldp->accessibility);
c5aa993b
JM
13077 }
13078 break;
c906108c
SS
13079 }
13080 if (nfields < fip->nbaseclasses)
13081 {
7d0ccb61 13082 switch (fieldp->virtuality)
c906108c 13083 {
c5aa993b
JM
13084 case DW_VIRTUALITY_virtual:
13085 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13086 if (cu->language == language_ada)
a73c6dcd 13087 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13088 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13089 break;
c906108c
SS
13090 }
13091 }
c906108c
SS
13092 }
13093}
13094
7d27a96d
TT
13095/* Return true if this member function is a constructor, false
13096 otherwise. */
13097
13098static int
13099dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13100{
13101 const char *fieldname;
fe978cb0 13102 const char *type_name;
7d27a96d
TT
13103 int len;
13104
13105 if (die->parent == NULL)
13106 return 0;
13107
13108 if (die->parent->tag != DW_TAG_structure_type
13109 && die->parent->tag != DW_TAG_union_type
13110 && die->parent->tag != DW_TAG_class_type)
13111 return 0;
13112
13113 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13114 type_name = dwarf2_name (die->parent, cu);
13115 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13116 return 0;
13117
13118 len = strlen (fieldname);
fe978cb0
PA
13119 return (strncmp (fieldname, type_name, len) == 0
13120 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13121}
13122
c906108c
SS
13123/* Add a member function to the proper fieldlist. */
13124
13125static void
107d2387 13126dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13127 struct type *type, struct dwarf2_cu *cu)
c906108c 13128{
e7c27a73 13129 struct objfile *objfile = cu->objfile;
c906108c
SS
13130 struct attribute *attr;
13131 struct fnfieldlist *flp;
13132 int i;
13133 struct fn_field *fnp;
15d034d0 13134 const char *fieldname;
c906108c 13135 struct nextfnfield *new_fnfield;
f792889a 13136 struct type *this_type;
60d5a603 13137 enum dwarf_access_attribute accessibility;
c906108c 13138
b4ba55a1 13139 if (cu->language == language_ada)
a73c6dcd 13140 error (_("unexpected member function in Ada type"));
b4ba55a1 13141
2df3850c 13142 /* Get name of member function. */
39cbfefa
DJ
13143 fieldname = dwarf2_name (die, cu);
13144 if (fieldname == NULL)
2df3850c 13145 return;
c906108c 13146
c906108c
SS
13147 /* Look up member function name in fieldlist. */
13148 for (i = 0; i < fip->nfnfields; i++)
13149 {
27bfe10e 13150 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13151 break;
13152 }
13153
13154 /* Create new list element if necessary. */
13155 if (i < fip->nfnfields)
13156 flp = &fip->fnfieldlists[i];
13157 else
13158 {
13159 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13160 {
13161 fip->fnfieldlists = (struct fnfieldlist *)
13162 xrealloc (fip->fnfieldlists,
13163 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13164 * sizeof (struct fnfieldlist));
c906108c 13165 if (fip->nfnfields == 0)
c13c43fd 13166 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13167 }
13168 flp = &fip->fnfieldlists[fip->nfnfields];
13169 flp->name = fieldname;
13170 flp->length = 0;
13171 flp->head = NULL;
3da10d80 13172 i = fip->nfnfields++;
c906108c
SS
13173 }
13174
13175 /* Create a new member function field and chain it to the field list
0963b4bd 13176 entry. */
8d749320 13177 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13178 make_cleanup (xfree, new_fnfield);
c906108c
SS
13179 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13180 new_fnfield->next = flp->head;
13181 flp->head = new_fnfield;
13182 flp->length++;
13183
13184 /* Fill in the member function field info. */
13185 fnp = &new_fnfield->fnfield;
3da10d80
KS
13186
13187 /* Delay processing of the physname until later. */
9c37b5ae 13188 if (cu->language == language_cplus)
3da10d80
KS
13189 {
13190 add_to_method_list (type, i, flp->length - 1, fieldname,
13191 die, cu);
13192 }
13193 else
13194 {
1d06ead6 13195 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13196 fnp->physname = physname ? physname : "";
13197 }
13198
c906108c 13199 fnp->type = alloc_type (objfile);
f792889a
DJ
13200 this_type = read_type_die (die, cu);
13201 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13202 {
f792889a 13203 int nparams = TYPE_NFIELDS (this_type);
c906108c 13204
f792889a 13205 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13206 of the method itself (TYPE_CODE_METHOD). */
13207 smash_to_method_type (fnp->type, type,
f792889a
DJ
13208 TYPE_TARGET_TYPE (this_type),
13209 TYPE_FIELDS (this_type),
13210 TYPE_NFIELDS (this_type),
13211 TYPE_VARARGS (this_type));
c906108c
SS
13212
13213 /* Handle static member functions.
c5aa993b 13214 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13215 member functions. G++ helps GDB by marking the first
13216 parameter for non-static member functions (which is the this
13217 pointer) as artificial. We obtain this information from
13218 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13219 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13220 fnp->voffset = VOFFSET_STATIC;
13221 }
13222 else
e2e0b3e5 13223 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13224 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13225
13226 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13227 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13228 fnp->fcontext = die_containing_type (die, cu);
c906108c 13229
3e43a32a
MS
13230 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13231 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13232
13233 /* Get accessibility. */
e142c38c 13234 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13235 if (attr)
aead7601 13236 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13237 else
13238 accessibility = dwarf2_default_access_attribute (die, cu);
13239 switch (accessibility)
c906108c 13240 {
60d5a603
JK
13241 case DW_ACCESS_private:
13242 fnp->is_private = 1;
13243 break;
13244 case DW_ACCESS_protected:
13245 fnp->is_protected = 1;
13246 break;
c906108c
SS
13247 }
13248
b02dede2 13249 /* Check for artificial methods. */
e142c38c 13250 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13251 if (attr && DW_UNSND (attr) != 0)
13252 fnp->is_artificial = 1;
13253
7d27a96d
TT
13254 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13255
0d564a31 13256 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13257 function. For older versions of GCC, this is an offset in the
13258 appropriate virtual table, as specified by DW_AT_containing_type.
13259 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13260 to the object address. */
13261
e142c38c 13262 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13263 if (attr)
8e19ed76 13264 {
aec5aa8b 13265 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13266 {
aec5aa8b
TT
13267 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13268 {
13269 /* Old-style GCC. */
13270 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13271 }
13272 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13273 || (DW_BLOCK (attr)->size > 1
13274 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13275 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13276 {
aec5aa8b
TT
13277 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13278 if ((fnp->voffset % cu->header.addr_size) != 0)
13279 dwarf2_complex_location_expr_complaint ();
13280 else
13281 fnp->voffset /= cu->header.addr_size;
13282 fnp->voffset += 2;
13283 }
13284 else
13285 dwarf2_complex_location_expr_complaint ();
13286
13287 if (!fnp->fcontext)
7e993ebf
KS
13288 {
13289 /* If there is no `this' field and no DW_AT_containing_type,
13290 we cannot actually find a base class context for the
13291 vtable! */
13292 if (TYPE_NFIELDS (this_type) == 0
13293 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13294 {
13295 complaint (&symfile_complaints,
13296 _("cannot determine context for virtual member "
13297 "function \"%s\" (offset %d)"),
9c541725 13298 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13299 }
13300 else
13301 {
13302 fnp->fcontext
13303 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13304 }
13305 }
aec5aa8b 13306 }
3690dd37 13307 else if (attr_form_is_section_offset (attr))
8e19ed76 13308 {
4d3c2250 13309 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13310 }
13311 else
13312 {
4d3c2250
KB
13313 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13314 fieldname);
8e19ed76 13315 }
0d564a31 13316 }
d48cc9dd
DJ
13317 else
13318 {
13319 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13320 if (attr && DW_UNSND (attr))
13321 {
13322 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13323 complaint (&symfile_complaints,
3e43a32a
MS
13324 _("Member function \"%s\" (offset %d) is virtual "
13325 "but the vtable offset is not specified"),
9c541725 13326 fieldname, to_underlying (die->sect_off));
9655fd1a 13327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13328 TYPE_CPLUS_DYNAMIC (type) = 1;
13329 }
13330 }
c906108c
SS
13331}
13332
13333/* Create the vector of member function fields, and attach it to the type. */
13334
13335static void
fba45db2 13336dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13337 struct dwarf2_cu *cu)
c906108c
SS
13338{
13339 struct fnfieldlist *flp;
c906108c
SS
13340 int i;
13341
b4ba55a1 13342 if (cu->language == language_ada)
a73c6dcd 13343 error (_("unexpected member functions in Ada type"));
b4ba55a1 13344
c906108c
SS
13345 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13346 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13347 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13348
13349 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13350 {
13351 struct nextfnfield *nfp = flp->head;
13352 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13353 int k;
13354
13355 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13356 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13357 fn_flp->fn_fields = (struct fn_field *)
13358 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13359 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13360 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13361 }
13362
13363 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13364}
13365
1168df01
JB
13366/* Returns non-zero if NAME is the name of a vtable member in CU's
13367 language, zero otherwise. */
13368static int
13369is_vtable_name (const char *name, struct dwarf2_cu *cu)
13370{
13371 static const char vptr[] = "_vptr";
987504bb 13372 static const char vtable[] = "vtable";
1168df01 13373
9c37b5ae
TT
13374 /* Look for the C++ form of the vtable. */
13375 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13376 return 1;
13377
13378 return 0;
13379}
13380
c0dd20ea 13381/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13382 functions, with the ABI-specified layout. If TYPE describes
13383 such a structure, smash it into a member function type.
61049d3b
DJ
13384
13385 GCC shouldn't do this; it should just output pointer to member DIEs.
13386 This is GCC PR debug/28767. */
c0dd20ea 13387
0b92b5bb
TT
13388static void
13389quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13390{
09e2d7c7 13391 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13392
13393 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13394 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13395 return;
c0dd20ea
DJ
13396
13397 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13398 if (TYPE_FIELD_NAME (type, 0) == NULL
13399 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13400 || TYPE_FIELD_NAME (type, 1) == NULL
13401 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13402 return;
c0dd20ea
DJ
13403
13404 /* Find the type of the method. */
0b92b5bb 13405 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13406 if (pfn_type == NULL
13407 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13408 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13409 return;
c0dd20ea
DJ
13410
13411 /* Look for the "this" argument. */
13412 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13413 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13414 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13415 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13416 return;
c0dd20ea 13417
09e2d7c7 13418 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13419 new_type = alloc_type (objfile);
09e2d7c7 13420 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13421 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13422 TYPE_VARARGS (pfn_type));
0b92b5bb 13423 smash_to_methodptr_type (type, new_type);
c0dd20ea 13424}
1168df01 13425
685b1105
JK
13426/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13427 (icc). */
13428
13429static int
13430producer_is_icc (struct dwarf2_cu *cu)
13431{
13432 if (!cu->checked_producer)
13433 check_producer (cu);
13434
13435 return cu->producer_is_icc;
13436}
13437
c906108c 13438/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13439 (definition) to create a type for the structure or union. Fill in
13440 the type's name and general properties; the members will not be
83655187
DE
13441 processed until process_structure_scope. A symbol table entry for
13442 the type will also not be done until process_structure_scope (assuming
13443 the type has a name).
c906108c 13444
c767944b
DJ
13445 NOTE: we need to call these functions regardless of whether or not the
13446 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13447 structure or union. This gets the type entered into our set of
83655187 13448 user defined types. */
c906108c 13449
f792889a 13450static struct type *
134d01f1 13451read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13452{
e7c27a73 13453 struct objfile *objfile = cu->objfile;
c906108c
SS
13454 struct type *type;
13455 struct attribute *attr;
15d034d0 13456 const char *name;
c906108c 13457
348e048f
DE
13458 /* If the definition of this type lives in .debug_types, read that type.
13459 Don't follow DW_AT_specification though, that will take us back up
13460 the chain and we want to go down. */
45e58e77 13461 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13462 if (attr)
13463 {
ac9ec31b 13464 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13465
ac9ec31b 13466 /* The type's CU may not be the same as CU.
02142a6c 13467 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13468 return set_die_type (die, type, cu);
13469 }
13470
c0dd20ea 13471 type = alloc_type (objfile);
c906108c 13472 INIT_CPLUS_SPECIFIC (type);
93311388 13473
39cbfefa
DJ
13474 name = dwarf2_name (die, cu);
13475 if (name != NULL)
c906108c 13476 {
987504bb 13477 if (cu->language == language_cplus
c44af4eb
TT
13478 || cu->language == language_d
13479 || cu->language == language_rust)
63d06c5c 13480 {
15d034d0 13481 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13482
13483 /* dwarf2_full_name might have already finished building the DIE's
13484 type. If so, there is no need to continue. */
13485 if (get_die_type (die, cu) != NULL)
13486 return get_die_type (die, cu);
13487
13488 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13489 if (die->tag == DW_TAG_structure_type
13490 || die->tag == DW_TAG_class_type)
13491 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13492 }
13493 else
13494 {
d8151005
DJ
13495 /* The name is already allocated along with this objfile, so
13496 we don't need to duplicate it for the type. */
7d455152 13497 TYPE_TAG_NAME (type) = name;
94af9270
KS
13498 if (die->tag == DW_TAG_class_type)
13499 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13500 }
c906108c
SS
13501 }
13502
13503 if (die->tag == DW_TAG_structure_type)
13504 {
13505 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13506 }
13507 else if (die->tag == DW_TAG_union_type)
13508 {
13509 TYPE_CODE (type) = TYPE_CODE_UNION;
13510 }
13511 else
13512 {
4753d33b 13513 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13514 }
13515
0cc2414c
TT
13516 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13517 TYPE_DECLARED_CLASS (type) = 1;
13518
e142c38c 13519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13520 if (attr)
13521 {
155bfbd3
JB
13522 if (attr_form_is_constant (attr))
13523 TYPE_LENGTH (type) = DW_UNSND (attr);
13524 else
13525 {
13526 /* For the moment, dynamic type sizes are not supported
13527 by GDB's struct type. The actual size is determined
13528 on-demand when resolving the type of a given object,
13529 so set the type's length to zero for now. Otherwise,
13530 we record an expression as the length, and that expression
13531 could lead to a very large value, which could eventually
13532 lead to us trying to allocate that much memory when creating
13533 a value of that type. */
13534 TYPE_LENGTH (type) = 0;
13535 }
c906108c
SS
13536 }
13537 else
13538 {
13539 TYPE_LENGTH (type) = 0;
13540 }
13541
422b1cb0 13542 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13543 {
13544 /* ICC does not output the required DW_AT_declaration
13545 on incomplete types, but gives them a size of zero. */
422b1cb0 13546 TYPE_STUB (type) = 1;
685b1105
JK
13547 }
13548 else
13549 TYPE_STUB_SUPPORTED (type) = 1;
13550
dc718098 13551 if (die_is_declaration (die, cu))
876cecd0 13552 TYPE_STUB (type) = 1;
a6c727b2
DJ
13553 else if (attr == NULL && die->child == NULL
13554 && producer_is_realview (cu->producer))
13555 /* RealView does not output the required DW_AT_declaration
13556 on incomplete types. */
13557 TYPE_STUB (type) = 1;
dc718098 13558
c906108c
SS
13559 /* We need to add the type field to the die immediately so we don't
13560 infinitely recurse when dealing with pointers to the structure
0963b4bd 13561 type within the structure itself. */
1c379e20 13562 set_die_type (die, type, cu);
c906108c 13563
7e314c57
JK
13564 /* set_die_type should be already done. */
13565 set_descriptive_type (type, die, cu);
13566
c767944b
DJ
13567 return type;
13568}
13569
13570/* Finish creating a structure or union type, including filling in
13571 its members and creating a symbol for it. */
13572
13573static void
13574process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13575{
13576 struct objfile *objfile = cu->objfile;
ca040673 13577 struct die_info *child_die;
c767944b
DJ
13578 struct type *type;
13579
13580 type = get_die_type (die, cu);
13581 if (type == NULL)
13582 type = read_structure_type (die, cu);
13583
e142c38c 13584 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13585 {
13586 struct field_info fi;
34eaf542 13587 VEC (symbolp) *template_args = NULL;
c767944b 13588 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13589
13590 memset (&fi, 0, sizeof (struct field_info));
13591
639d11d3 13592 child_die = die->child;
c906108c
SS
13593
13594 while (child_die && child_die->tag)
13595 {
a9a9bd0f
DC
13596 if (child_die->tag == DW_TAG_member
13597 || child_die->tag == DW_TAG_variable)
c906108c 13598 {
a9a9bd0f
DC
13599 /* NOTE: carlton/2002-11-05: A C++ static data member
13600 should be a DW_TAG_member that is a declaration, but
13601 all versions of G++ as of this writing (so through at
13602 least 3.2.1) incorrectly generate DW_TAG_variable
13603 tags for them instead. */
e7c27a73 13604 dwarf2_add_field (&fi, child_die, cu);
c906108c 13605 }
8713b1b1 13606 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13607 {
e98c9e7c
TT
13608 /* Rust doesn't have member functions in the C++ sense.
13609 However, it does emit ordinary functions as children
13610 of a struct DIE. */
13611 if (cu->language == language_rust)
13612 read_func_scope (child_die, cu);
13613 else
13614 {
13615 /* C++ member function. */
13616 dwarf2_add_member_fn (&fi, child_die, type, cu);
13617 }
c906108c
SS
13618 }
13619 else if (child_die->tag == DW_TAG_inheritance)
13620 {
13621 /* C++ base class field. */
e7c27a73 13622 dwarf2_add_field (&fi, child_die, cu);
c906108c 13623 }
98751a41
JK
13624 else if (child_die->tag == DW_TAG_typedef)
13625 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13626 else if (child_die->tag == DW_TAG_template_type_param
13627 || child_die->tag == DW_TAG_template_value_param)
13628 {
13629 struct symbol *arg = new_symbol (child_die, NULL, cu);
13630
f1078f66
DJ
13631 if (arg != NULL)
13632 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13633 }
13634
c906108c
SS
13635 child_die = sibling_die (child_die);
13636 }
13637
34eaf542
TT
13638 /* Attach template arguments to type. */
13639 if (! VEC_empty (symbolp, template_args))
13640 {
13641 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13642 TYPE_N_TEMPLATE_ARGUMENTS (type)
13643 = VEC_length (symbolp, template_args);
13644 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13645 = XOBNEWVEC (&objfile->objfile_obstack,
13646 struct symbol *,
13647 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13648 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13649 VEC_address (symbolp, template_args),
13650 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13651 * sizeof (struct symbol *)));
13652 VEC_free (symbolp, template_args);
13653 }
13654
c906108c
SS
13655 /* Attach fields and member functions to the type. */
13656 if (fi.nfields)
e7c27a73 13657 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13658 if (fi.nfnfields)
13659 {
e7c27a73 13660 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13661
c5aa993b 13662 /* Get the type which refers to the base class (possibly this
c906108c 13663 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13664 class from the DW_AT_containing_type attribute. This use of
13665 DW_AT_containing_type is a GNU extension. */
c906108c 13666
e142c38c 13667 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13668 {
e7c27a73 13669 struct type *t = die_containing_type (die, cu);
c906108c 13670
ae6ae975 13671 set_type_vptr_basetype (type, t);
c906108c
SS
13672 if (type == t)
13673 {
c906108c
SS
13674 int i;
13675
13676 /* Our own class provides vtbl ptr. */
13677 for (i = TYPE_NFIELDS (t) - 1;
13678 i >= TYPE_N_BASECLASSES (t);
13679 --i)
13680 {
0d5cff50 13681 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13682
1168df01 13683 if (is_vtable_name (fieldname, cu))
c906108c 13684 {
ae6ae975 13685 set_type_vptr_fieldno (type, i);
c906108c
SS
13686 break;
13687 }
13688 }
13689
13690 /* Complain if virtual function table field not found. */
13691 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13692 complaint (&symfile_complaints,
3e43a32a
MS
13693 _("virtual function table pointer "
13694 "not found when defining class '%s'"),
4d3c2250
KB
13695 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13696 "");
c906108c
SS
13697 }
13698 else
13699 {
ae6ae975 13700 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13701 }
13702 }
f6235d4c 13703 else if (cu->producer
61012eef 13704 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13705 {
13706 /* The IBM XLC compiler does not provide direct indication
13707 of the containing type, but the vtable pointer is
13708 always named __vfp. */
13709
13710 int i;
13711
13712 for (i = TYPE_NFIELDS (type) - 1;
13713 i >= TYPE_N_BASECLASSES (type);
13714 --i)
13715 {
13716 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13717 {
ae6ae975
DE
13718 set_type_vptr_fieldno (type, i);
13719 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13720 break;
13721 }
13722 }
13723 }
c906108c 13724 }
98751a41
JK
13725
13726 /* Copy fi.typedef_field_list linked list elements content into the
13727 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13728 if (fi.typedef_field_list)
13729 {
13730 int i = fi.typedef_field_list_count;
13731
a0d7a4ff 13732 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13733 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13734 = ((struct typedef_field *)
13735 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13736 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13737
13738 /* Reverse the list order to keep the debug info elements order. */
13739 while (--i >= 0)
13740 {
13741 struct typedef_field *dest, *src;
6e70227d 13742
98751a41
JK
13743 dest = &TYPE_TYPEDEF_FIELD (type, i);
13744 src = &fi.typedef_field_list->field;
13745 fi.typedef_field_list = fi.typedef_field_list->next;
13746 *dest = *src;
13747 }
13748 }
c767944b
DJ
13749
13750 do_cleanups (back_to);
c906108c 13751 }
63d06c5c 13752
bb5ed363 13753 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13754
90aeadfc
DC
13755 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13756 snapshots) has been known to create a die giving a declaration
13757 for a class that has, as a child, a die giving a definition for a
13758 nested class. So we have to process our children even if the
13759 current die is a declaration. Normally, of course, a declaration
13760 won't have any children at all. */
134d01f1 13761
ca040673
DE
13762 child_die = die->child;
13763
90aeadfc
DC
13764 while (child_die != NULL && child_die->tag)
13765 {
13766 if (child_die->tag == DW_TAG_member
13767 || child_die->tag == DW_TAG_variable
34eaf542
TT
13768 || child_die->tag == DW_TAG_inheritance
13769 || child_die->tag == DW_TAG_template_value_param
13770 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13771 {
90aeadfc 13772 /* Do nothing. */
134d01f1 13773 }
90aeadfc
DC
13774 else
13775 process_die (child_die, cu);
134d01f1 13776
90aeadfc 13777 child_die = sibling_die (child_die);
134d01f1
DJ
13778 }
13779
fa4028e9
JB
13780 /* Do not consider external references. According to the DWARF standard,
13781 these DIEs are identified by the fact that they have no byte_size
13782 attribute, and a declaration attribute. */
13783 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13784 || !die_is_declaration (die, cu))
c767944b 13785 new_symbol (die, type, cu);
134d01f1
DJ
13786}
13787
55426c9d
JB
13788/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13789 update TYPE using some information only available in DIE's children. */
13790
13791static void
13792update_enumeration_type_from_children (struct die_info *die,
13793 struct type *type,
13794 struct dwarf2_cu *cu)
13795{
13796 struct obstack obstack;
60f7655a 13797 struct die_info *child_die;
55426c9d
JB
13798 int unsigned_enum = 1;
13799 int flag_enum = 1;
13800 ULONGEST mask = 0;
13801 struct cleanup *old_chain;
13802
13803 obstack_init (&obstack);
13804 old_chain = make_cleanup_obstack_free (&obstack);
13805
60f7655a
DE
13806 for (child_die = die->child;
13807 child_die != NULL && child_die->tag;
13808 child_die = sibling_die (child_die))
55426c9d
JB
13809 {
13810 struct attribute *attr;
13811 LONGEST value;
13812 const gdb_byte *bytes;
13813 struct dwarf2_locexpr_baton *baton;
13814 const char *name;
60f7655a 13815
55426c9d
JB
13816 if (child_die->tag != DW_TAG_enumerator)
13817 continue;
13818
13819 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13820 if (attr == NULL)
13821 continue;
13822
13823 name = dwarf2_name (child_die, cu);
13824 if (name == NULL)
13825 name = "<anonymous enumerator>";
13826
13827 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13828 &value, &bytes, &baton);
13829 if (value < 0)
13830 {
13831 unsigned_enum = 0;
13832 flag_enum = 0;
13833 }
13834 else if ((mask & value) != 0)
13835 flag_enum = 0;
13836 else
13837 mask |= value;
13838
13839 /* If we already know that the enum type is neither unsigned, nor
13840 a flag type, no need to look at the rest of the enumerates. */
13841 if (!unsigned_enum && !flag_enum)
13842 break;
55426c9d
JB
13843 }
13844
13845 if (unsigned_enum)
13846 TYPE_UNSIGNED (type) = 1;
13847 if (flag_enum)
13848 TYPE_FLAG_ENUM (type) = 1;
13849
13850 do_cleanups (old_chain);
13851}
13852
134d01f1
DJ
13853/* Given a DW_AT_enumeration_type die, set its type. We do not
13854 complete the type's fields yet, or create any symbols. */
c906108c 13855
f792889a 13856static struct type *
134d01f1 13857read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13858{
e7c27a73 13859 struct objfile *objfile = cu->objfile;
c906108c 13860 struct type *type;
c906108c 13861 struct attribute *attr;
0114d602 13862 const char *name;
134d01f1 13863
348e048f
DE
13864 /* If the definition of this type lives in .debug_types, read that type.
13865 Don't follow DW_AT_specification though, that will take us back up
13866 the chain and we want to go down. */
45e58e77 13867 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13868 if (attr)
13869 {
ac9ec31b 13870 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13871
ac9ec31b 13872 /* The type's CU may not be the same as CU.
02142a6c 13873 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13874 return set_die_type (die, type, cu);
13875 }
13876
c906108c
SS
13877 type = alloc_type (objfile);
13878
13879 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13880 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13881 if (name != NULL)
7d455152 13882 TYPE_TAG_NAME (type) = name;
c906108c 13883
0626fc76
TT
13884 attr = dwarf2_attr (die, DW_AT_type, cu);
13885 if (attr != NULL)
13886 {
13887 struct type *underlying_type = die_type (die, cu);
13888
13889 TYPE_TARGET_TYPE (type) = underlying_type;
13890 }
13891
e142c38c 13892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13893 if (attr)
13894 {
13895 TYPE_LENGTH (type) = DW_UNSND (attr);
13896 }
13897 else
13898 {
13899 TYPE_LENGTH (type) = 0;
13900 }
13901
137033e9
JB
13902 /* The enumeration DIE can be incomplete. In Ada, any type can be
13903 declared as private in the package spec, and then defined only
13904 inside the package body. Such types are known as Taft Amendment
13905 Types. When another package uses such a type, an incomplete DIE
13906 may be generated by the compiler. */
02eb380e 13907 if (die_is_declaration (die, cu))
876cecd0 13908 TYPE_STUB (type) = 1;
02eb380e 13909
0626fc76
TT
13910 /* Finish the creation of this type by using the enum's children.
13911 We must call this even when the underlying type has been provided
13912 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13913 update_enumeration_type_from_children (die, type, cu);
13914
0626fc76
TT
13915 /* If this type has an underlying type that is not a stub, then we
13916 may use its attributes. We always use the "unsigned" attribute
13917 in this situation, because ordinarily we guess whether the type
13918 is unsigned -- but the guess can be wrong and the underlying type
13919 can tell us the reality. However, we defer to a local size
13920 attribute if one exists, because this lets the compiler override
13921 the underlying type if needed. */
13922 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13923 {
13924 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13925 if (TYPE_LENGTH (type) == 0)
13926 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13927 }
13928
3d567982
TT
13929 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13930
f792889a 13931 return set_die_type (die, type, cu);
134d01f1
DJ
13932}
13933
13934/* Given a pointer to a die which begins an enumeration, process all
13935 the dies that define the members of the enumeration, and create the
13936 symbol for the enumeration type.
13937
13938 NOTE: We reverse the order of the element list. */
13939
13940static void
13941process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13942{
f792889a 13943 struct type *this_type;
134d01f1 13944
f792889a
DJ
13945 this_type = get_die_type (die, cu);
13946 if (this_type == NULL)
13947 this_type = read_enumeration_type (die, cu);
9dc481d3 13948
639d11d3 13949 if (die->child != NULL)
c906108c 13950 {
9dc481d3
DE
13951 struct die_info *child_die;
13952 struct symbol *sym;
13953 struct field *fields = NULL;
13954 int num_fields = 0;
15d034d0 13955 const char *name;
9dc481d3 13956
639d11d3 13957 child_die = die->child;
c906108c
SS
13958 while (child_die && child_die->tag)
13959 {
13960 if (child_die->tag != DW_TAG_enumerator)
13961 {
e7c27a73 13962 process_die (child_die, cu);
c906108c
SS
13963 }
13964 else
13965 {
39cbfefa
DJ
13966 name = dwarf2_name (child_die, cu);
13967 if (name)
c906108c 13968 {
f792889a 13969 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13970
13971 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13972 {
13973 fields = (struct field *)
13974 xrealloc (fields,
13975 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13976 * sizeof (struct field));
c906108c
SS
13977 }
13978
3567439c 13979 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13980 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13981 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13982 FIELD_BITSIZE (fields[num_fields]) = 0;
13983
13984 num_fields++;
13985 }
13986 }
13987
13988 child_die = sibling_die (child_die);
13989 }
13990
13991 if (num_fields)
13992 {
f792889a
DJ
13993 TYPE_NFIELDS (this_type) = num_fields;
13994 TYPE_FIELDS (this_type) = (struct field *)
13995 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13996 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13997 sizeof (struct field) * num_fields);
b8c9b27d 13998 xfree (fields);
c906108c 13999 }
c906108c 14000 }
134d01f1 14001
6c83ed52
TT
14002 /* If we are reading an enum from a .debug_types unit, and the enum
14003 is a declaration, and the enum is not the signatured type in the
14004 unit, then we do not want to add a symbol for it. Adding a
14005 symbol would in some cases obscure the true definition of the
14006 enum, giving users an incomplete type when the definition is
14007 actually available. Note that we do not want to do this for all
14008 enums which are just declarations, because C++0x allows forward
14009 enum declarations. */
3019eac3 14010 if (cu->per_cu->is_debug_types
6c83ed52
TT
14011 && die_is_declaration (die, cu))
14012 {
52dc124a 14013 struct signatured_type *sig_type;
6c83ed52 14014
c0f78cd4 14015 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14017 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14018 return;
14019 }
14020
f792889a 14021 new_symbol (die, this_type, cu);
c906108c
SS
14022}
14023
14024/* Extract all information from a DW_TAG_array_type DIE and put it in
14025 the DIE's type field. For now, this only handles one dimensional
14026 arrays. */
14027
f792889a 14028static struct type *
e7c27a73 14029read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14030{
e7c27a73 14031 struct objfile *objfile = cu->objfile;
c906108c 14032 struct die_info *child_die;
7e314c57 14033 struct type *type;
c906108c
SS
14034 struct type *element_type, *range_type, *index_type;
14035 struct type **range_types = NULL;
14036 struct attribute *attr;
14037 int ndim = 0;
14038 struct cleanup *back_to;
15d034d0 14039 const char *name;
dc53a7ad 14040 unsigned int bit_stride = 0;
c906108c 14041
e7c27a73 14042 element_type = die_type (die, cu);
c906108c 14043
7e314c57
JK
14044 /* The die_type call above may have already set the type for this DIE. */
14045 type = get_die_type (die, cu);
14046 if (type)
14047 return type;
14048
dc53a7ad
JB
14049 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14050 if (attr != NULL)
14051 bit_stride = DW_UNSND (attr) * 8;
14052
14053 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14054 if (attr != NULL)
14055 bit_stride = DW_UNSND (attr);
14056
c906108c
SS
14057 /* Irix 6.2 native cc creates array types without children for
14058 arrays with unspecified length. */
639d11d3 14059 if (die->child == NULL)
c906108c 14060 {
46bf5051 14061 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14062 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14063 type = create_array_type_with_stride (NULL, element_type, range_type,
14064 bit_stride);
f792889a 14065 return set_die_type (die, type, cu);
c906108c
SS
14066 }
14067
14068 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14069 child_die = die->child;
c906108c
SS
14070 while (child_die && child_die->tag)
14071 {
14072 if (child_die->tag == DW_TAG_subrange_type)
14073 {
f792889a 14074 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14075
f792889a 14076 if (child_type != NULL)
a02abb62 14077 {
0963b4bd
MS
14078 /* The range type was succesfully read. Save it for the
14079 array type creation. */
a02abb62
JB
14080 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14081 {
14082 range_types = (struct type **)
14083 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14084 * sizeof (struct type *));
14085 if (ndim == 0)
14086 make_cleanup (free_current_contents, &range_types);
14087 }
f792889a 14088 range_types[ndim++] = child_type;
a02abb62 14089 }
c906108c
SS
14090 }
14091 child_die = sibling_die (child_die);
14092 }
14093
14094 /* Dwarf2 dimensions are output from left to right, create the
14095 necessary array types in backwards order. */
7ca2d3a3 14096
c906108c 14097 type = element_type;
7ca2d3a3
DL
14098
14099 if (read_array_order (die, cu) == DW_ORD_col_major)
14100 {
14101 int i = 0;
9a619af0 14102
7ca2d3a3 14103 while (i < ndim)
dc53a7ad
JB
14104 type = create_array_type_with_stride (NULL, type, range_types[i++],
14105 bit_stride);
7ca2d3a3
DL
14106 }
14107 else
14108 {
14109 while (ndim-- > 0)
dc53a7ad
JB
14110 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14111 bit_stride);
7ca2d3a3 14112 }
c906108c 14113
f5f8a009
EZ
14114 /* Understand Dwarf2 support for vector types (like they occur on
14115 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14116 array type. This is not part of the Dwarf2/3 standard yet, but a
14117 custom vendor extension. The main difference between a regular
14118 array and the vector variant is that vectors are passed by value
14119 to functions. */
e142c38c 14120 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14121 if (attr)
ea37ba09 14122 make_vector_type (type);
f5f8a009 14123
dbc98a8b
KW
14124 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14125 implementation may choose to implement triple vectors using this
14126 attribute. */
14127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14128 if (attr)
14129 {
14130 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14131 TYPE_LENGTH (type) = DW_UNSND (attr);
14132 else
3e43a32a
MS
14133 complaint (&symfile_complaints,
14134 _("DW_AT_byte_size for array type smaller "
14135 "than the total size of elements"));
dbc98a8b
KW
14136 }
14137
39cbfefa
DJ
14138 name = dwarf2_name (die, cu);
14139 if (name)
14140 TYPE_NAME (type) = name;
6e70227d 14141
0963b4bd 14142 /* Install the type in the die. */
7e314c57
JK
14143 set_die_type (die, type, cu);
14144
14145 /* set_die_type should be already done. */
b4ba55a1
JB
14146 set_descriptive_type (type, die, cu);
14147
c906108c
SS
14148 do_cleanups (back_to);
14149
7e314c57 14150 return type;
c906108c
SS
14151}
14152
7ca2d3a3 14153static enum dwarf_array_dim_ordering
6e70227d 14154read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14155{
14156 struct attribute *attr;
14157
14158 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14159
aead7601
SM
14160 if (attr)
14161 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14162
0963b4bd
MS
14163 /* GNU F77 is a special case, as at 08/2004 array type info is the
14164 opposite order to the dwarf2 specification, but data is still
14165 laid out as per normal fortran.
7ca2d3a3 14166
0963b4bd
MS
14167 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14168 version checking. */
7ca2d3a3 14169
905e0470
PM
14170 if (cu->language == language_fortran
14171 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14172 {
14173 return DW_ORD_row_major;
14174 }
14175
6e70227d 14176 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14177 {
14178 case array_column_major:
14179 return DW_ORD_col_major;
14180 case array_row_major:
14181 default:
14182 return DW_ORD_row_major;
14183 };
14184}
14185
72019c9c 14186/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14187 the DIE's type field. */
72019c9c 14188
f792889a 14189static struct type *
72019c9c
GM
14190read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14191{
7e314c57
JK
14192 struct type *domain_type, *set_type;
14193 struct attribute *attr;
f792889a 14194
7e314c57
JK
14195 domain_type = die_type (die, cu);
14196
14197 /* The die_type call above may have already set the type for this DIE. */
14198 set_type = get_die_type (die, cu);
14199 if (set_type)
14200 return set_type;
14201
14202 set_type = create_set_type (NULL, domain_type);
14203
14204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14205 if (attr)
14206 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14207
f792889a 14208 return set_die_type (die, set_type, cu);
72019c9c 14209}
7ca2d3a3 14210
0971de02
TT
14211/* A helper for read_common_block that creates a locexpr baton.
14212 SYM is the symbol which we are marking as computed.
14213 COMMON_DIE is the DIE for the common block.
14214 COMMON_LOC is the location expression attribute for the common
14215 block itself.
14216 MEMBER_LOC is the location expression attribute for the particular
14217 member of the common block that we are processing.
14218 CU is the CU from which the above come. */
14219
14220static void
14221mark_common_block_symbol_computed (struct symbol *sym,
14222 struct die_info *common_die,
14223 struct attribute *common_loc,
14224 struct attribute *member_loc,
14225 struct dwarf2_cu *cu)
14226{
14227 struct objfile *objfile = dwarf2_per_objfile->objfile;
14228 struct dwarf2_locexpr_baton *baton;
14229 gdb_byte *ptr;
14230 unsigned int cu_off;
14231 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14232 LONGEST offset = 0;
14233
14234 gdb_assert (common_loc && member_loc);
14235 gdb_assert (attr_form_is_block (common_loc));
14236 gdb_assert (attr_form_is_block (member_loc)
14237 || attr_form_is_constant (member_loc));
14238
8d749320 14239 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14240 baton->per_cu = cu->per_cu;
14241 gdb_assert (baton->per_cu);
14242
14243 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14244
14245 if (attr_form_is_constant (member_loc))
14246 {
14247 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14248 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14249 }
14250 else
14251 baton->size += DW_BLOCK (member_loc)->size;
14252
224c3ddb 14253 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14254 baton->data = ptr;
14255
14256 *ptr++ = DW_OP_call4;
9c541725 14257 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14258 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14259 ptr += 4;
14260
14261 if (attr_form_is_constant (member_loc))
14262 {
14263 *ptr++ = DW_OP_addr;
14264 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14265 ptr += cu->header.addr_size;
14266 }
14267 else
14268 {
14269 /* We have to copy the data here, because DW_OP_call4 will only
14270 use a DW_AT_location attribute. */
14271 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14272 ptr += DW_BLOCK (member_loc)->size;
14273 }
14274
14275 *ptr++ = DW_OP_plus;
14276 gdb_assert (ptr - baton->data == baton->size);
14277
0971de02 14278 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14279 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14280}
14281
4357ac6c
TT
14282/* Create appropriate locally-scoped variables for all the
14283 DW_TAG_common_block entries. Also create a struct common_block
14284 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14285 is used to sepate the common blocks name namespace from regular
14286 variable names. */
c906108c
SS
14287
14288static void
e7c27a73 14289read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14290{
0971de02
TT
14291 struct attribute *attr;
14292
14293 attr = dwarf2_attr (die, DW_AT_location, cu);
14294 if (attr)
14295 {
14296 /* Support the .debug_loc offsets. */
14297 if (attr_form_is_block (attr))
14298 {
14299 /* Ok. */
14300 }
14301 else if (attr_form_is_section_offset (attr))
14302 {
14303 dwarf2_complex_location_expr_complaint ();
14304 attr = NULL;
14305 }
14306 else
14307 {
14308 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14309 "common block member");
14310 attr = NULL;
14311 }
14312 }
14313
639d11d3 14314 if (die->child != NULL)
c906108c 14315 {
4357ac6c
TT
14316 struct objfile *objfile = cu->objfile;
14317 struct die_info *child_die;
14318 size_t n_entries = 0, size;
14319 struct common_block *common_block;
14320 struct symbol *sym;
74ac6d43 14321
4357ac6c
TT
14322 for (child_die = die->child;
14323 child_die && child_die->tag;
14324 child_die = sibling_die (child_die))
14325 ++n_entries;
14326
14327 size = (sizeof (struct common_block)
14328 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14329 common_block
14330 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14331 size);
4357ac6c
TT
14332 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14333 common_block->n_entries = 0;
14334
14335 for (child_die = die->child;
14336 child_die && child_die->tag;
14337 child_die = sibling_die (child_die))
14338 {
14339 /* Create the symbol in the DW_TAG_common_block block in the current
14340 symbol scope. */
e7c27a73 14341 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14342 if (sym != NULL)
14343 {
14344 struct attribute *member_loc;
14345
14346 common_block->contents[common_block->n_entries++] = sym;
14347
14348 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14349 cu);
14350 if (member_loc)
14351 {
14352 /* GDB has handled this for a long time, but it is
14353 not specified by DWARF. It seems to have been
14354 emitted by gfortran at least as recently as:
14355 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14356 complaint (&symfile_complaints,
14357 _("Variable in common block has "
14358 "DW_AT_data_member_location "
14359 "- DIE at 0x%x [in module %s]"),
9c541725 14360 to_underlying (child_die->sect_off),
4262abfb 14361 objfile_name (cu->objfile));
0971de02
TT
14362
14363 if (attr_form_is_section_offset (member_loc))
14364 dwarf2_complex_location_expr_complaint ();
14365 else if (attr_form_is_constant (member_loc)
14366 || attr_form_is_block (member_loc))
14367 {
14368 if (attr)
14369 mark_common_block_symbol_computed (sym, die, attr,
14370 member_loc, cu);
14371 }
14372 else
14373 dwarf2_complex_location_expr_complaint ();
14374 }
14375 }
c906108c 14376 }
4357ac6c
TT
14377
14378 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14379 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14380 }
14381}
14382
0114d602 14383/* Create a type for a C++ namespace. */
d9fa45fe 14384
0114d602
DJ
14385static struct type *
14386read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14387{
e7c27a73 14388 struct objfile *objfile = cu->objfile;
0114d602 14389 const char *previous_prefix, *name;
9219021c 14390 int is_anonymous;
0114d602
DJ
14391 struct type *type;
14392
14393 /* For extensions, reuse the type of the original namespace. */
14394 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14395 {
14396 struct die_info *ext_die;
14397 struct dwarf2_cu *ext_cu = cu;
9a619af0 14398
0114d602
DJ
14399 ext_die = dwarf2_extension (die, &ext_cu);
14400 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14401
14402 /* EXT_CU may not be the same as CU.
02142a6c 14403 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14404 return set_die_type (die, type, cu);
14405 }
9219021c 14406
e142c38c 14407 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14408
14409 /* Now build the name of the current namespace. */
14410
0114d602
DJ
14411 previous_prefix = determine_prefix (die, cu);
14412 if (previous_prefix[0] != '\0')
14413 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14414 previous_prefix, name, 0, cu);
0114d602
DJ
14415
14416 /* Create the type. */
19f392bc 14417 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14418 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14419
60531b24 14420 return set_die_type (die, type, cu);
0114d602
DJ
14421}
14422
22cee43f 14423/* Read a namespace scope. */
0114d602
DJ
14424
14425static void
14426read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14427{
14428 struct objfile *objfile = cu->objfile;
0114d602 14429 int is_anonymous;
9219021c 14430
5c4e30ca
DC
14431 /* Add a symbol associated to this if we haven't seen the namespace
14432 before. Also, add a using directive if it's an anonymous
14433 namespace. */
9219021c 14434
f2f0e013 14435 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14436 {
14437 struct type *type;
14438
0114d602 14439 type = read_type_die (die, cu);
e7c27a73 14440 new_symbol (die, type, cu);
5c4e30ca 14441
e8e80198 14442 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14443 if (is_anonymous)
0114d602
DJ
14444 {
14445 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14446
22cee43f
PMR
14447 add_using_directive (using_directives (cu->language),
14448 previous_prefix, TYPE_NAME (type), NULL,
14449 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14450 }
5c4e30ca 14451 }
9219021c 14452
639d11d3 14453 if (die->child != NULL)
d9fa45fe 14454 {
639d11d3 14455 struct die_info *child_die = die->child;
6e70227d 14456
d9fa45fe
DC
14457 while (child_die && child_die->tag)
14458 {
e7c27a73 14459 process_die (child_die, cu);
d9fa45fe
DC
14460 child_die = sibling_die (child_die);
14461 }
14462 }
38d518c9
EZ
14463}
14464
f55ee35c
JK
14465/* Read a Fortran module as type. This DIE can be only a declaration used for
14466 imported module. Still we need that type as local Fortran "use ... only"
14467 declaration imports depend on the created type in determine_prefix. */
14468
14469static struct type *
14470read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14471{
14472 struct objfile *objfile = cu->objfile;
15d034d0 14473 const char *module_name;
f55ee35c
JK
14474 struct type *type;
14475
14476 module_name = dwarf2_name (die, cu);
14477 if (!module_name)
3e43a32a
MS
14478 complaint (&symfile_complaints,
14479 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14480 to_underlying (die->sect_off));
19f392bc 14481 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14482
14483 /* determine_prefix uses TYPE_TAG_NAME. */
14484 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14485
14486 return set_die_type (die, type, cu);
14487}
14488
5d7cb8df
JK
14489/* Read a Fortran module. */
14490
14491static void
14492read_module (struct die_info *die, struct dwarf2_cu *cu)
14493{
14494 struct die_info *child_die = die->child;
530e8392
KB
14495 struct type *type;
14496
14497 type = read_type_die (die, cu);
14498 new_symbol (die, type, cu);
5d7cb8df 14499
5d7cb8df
JK
14500 while (child_die && child_die->tag)
14501 {
14502 process_die (child_die, cu);
14503 child_die = sibling_die (child_die);
14504 }
14505}
14506
38d518c9
EZ
14507/* Return the name of the namespace represented by DIE. Set
14508 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14509 namespace. */
14510
14511static const char *
e142c38c 14512namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14513{
14514 struct die_info *current_die;
14515 const char *name = NULL;
14516
14517 /* Loop through the extensions until we find a name. */
14518
14519 for (current_die = die;
14520 current_die != NULL;
f2f0e013 14521 current_die = dwarf2_extension (die, &cu))
38d518c9 14522 {
96553a0c
DE
14523 /* We don't use dwarf2_name here so that we can detect the absence
14524 of a name -> anonymous namespace. */
7d45c7c3 14525 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14526
38d518c9
EZ
14527 if (name != NULL)
14528 break;
14529 }
14530
14531 /* Is it an anonymous namespace? */
14532
14533 *is_anonymous = (name == NULL);
14534 if (*is_anonymous)
2b1dbab0 14535 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14536
14537 return name;
d9fa45fe
DC
14538}
14539
c906108c
SS
14540/* Extract all information from a DW_TAG_pointer_type DIE and add to
14541 the user defined type vector. */
14542
f792889a 14543static struct type *
e7c27a73 14544read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14545{
5e2b427d 14546 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14547 struct comp_unit_head *cu_header = &cu->header;
c906108c 14548 struct type *type;
8b2dbe47
KB
14549 struct attribute *attr_byte_size;
14550 struct attribute *attr_address_class;
14551 int byte_size, addr_class;
7e314c57
JK
14552 struct type *target_type;
14553
14554 target_type = die_type (die, cu);
c906108c 14555
7e314c57
JK
14556 /* The die_type call above may have already set the type for this DIE. */
14557 type = get_die_type (die, cu);
14558 if (type)
14559 return type;
14560
14561 type = lookup_pointer_type (target_type);
8b2dbe47 14562
e142c38c 14563 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14564 if (attr_byte_size)
14565 byte_size = DW_UNSND (attr_byte_size);
c906108c 14566 else
8b2dbe47
KB
14567 byte_size = cu_header->addr_size;
14568
e142c38c 14569 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14570 if (attr_address_class)
14571 addr_class = DW_UNSND (attr_address_class);
14572 else
14573 addr_class = DW_ADDR_none;
14574
14575 /* If the pointer size or address class is different than the
14576 default, create a type variant marked as such and set the
14577 length accordingly. */
14578 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14579 {
5e2b427d 14580 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14581 {
14582 int type_flags;
14583
849957d9 14584 type_flags = gdbarch_address_class_type_flags
5e2b427d 14585 (gdbarch, byte_size, addr_class);
876cecd0
TT
14586 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14587 == 0);
8b2dbe47
KB
14588 type = make_type_with_address_space (type, type_flags);
14589 }
14590 else if (TYPE_LENGTH (type) != byte_size)
14591 {
3e43a32a
MS
14592 complaint (&symfile_complaints,
14593 _("invalid pointer size %d"), byte_size);
8b2dbe47 14594 }
6e70227d 14595 else
9a619af0
MS
14596 {
14597 /* Should we also complain about unhandled address classes? */
14598 }
c906108c 14599 }
8b2dbe47
KB
14600
14601 TYPE_LENGTH (type) = byte_size;
f792889a 14602 return set_die_type (die, type, cu);
c906108c
SS
14603}
14604
14605/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14606 the user defined type vector. */
14607
f792889a 14608static struct type *
e7c27a73 14609read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14610{
14611 struct type *type;
14612 struct type *to_type;
14613 struct type *domain;
14614
e7c27a73
DJ
14615 to_type = die_type (die, cu);
14616 domain = die_containing_type (die, cu);
0d5de010 14617
7e314c57
JK
14618 /* The calls above may have already set the type for this DIE. */
14619 type = get_die_type (die, cu);
14620 if (type)
14621 return type;
14622
0d5de010
DJ
14623 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14624 type = lookup_methodptr_type (to_type);
7078baeb
TT
14625 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14626 {
14627 struct type *new_type = alloc_type (cu->objfile);
14628
14629 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14630 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14631 TYPE_VARARGS (to_type));
14632 type = lookup_methodptr_type (new_type);
14633 }
0d5de010
DJ
14634 else
14635 type = lookup_memberptr_type (to_type, domain);
c906108c 14636
f792889a 14637 return set_die_type (die, type, cu);
c906108c
SS
14638}
14639
4297a3f0 14640/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14641 the user defined type vector. */
14642
f792889a 14643static struct type *
4297a3f0
AV
14644read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14645 enum type_code refcode)
c906108c 14646{
e7c27a73 14647 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14648 struct type *type, *target_type;
c906108c
SS
14649 struct attribute *attr;
14650
4297a3f0
AV
14651 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14652
7e314c57
JK
14653 target_type = die_type (die, cu);
14654
14655 /* The die_type call above may have already set the type for this DIE. */
14656 type = get_die_type (die, cu);
14657 if (type)
14658 return type;
14659
4297a3f0 14660 type = lookup_reference_type (target_type, refcode);
e142c38c 14661 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14662 if (attr)
14663 {
14664 TYPE_LENGTH (type) = DW_UNSND (attr);
14665 }
14666 else
14667 {
107d2387 14668 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14669 }
f792889a 14670 return set_die_type (die, type, cu);
c906108c
SS
14671}
14672
cf363f18
MW
14673/* Add the given cv-qualifiers to the element type of the array. GCC
14674 outputs DWARF type qualifiers that apply to an array, not the
14675 element type. But GDB relies on the array element type to carry
14676 the cv-qualifiers. This mimics section 6.7.3 of the C99
14677 specification. */
14678
14679static struct type *
14680add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14681 struct type *base_type, int cnst, int voltl)
14682{
14683 struct type *el_type, *inner_array;
14684
14685 base_type = copy_type (base_type);
14686 inner_array = base_type;
14687
14688 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14689 {
14690 TYPE_TARGET_TYPE (inner_array) =
14691 copy_type (TYPE_TARGET_TYPE (inner_array));
14692 inner_array = TYPE_TARGET_TYPE (inner_array);
14693 }
14694
14695 el_type = TYPE_TARGET_TYPE (inner_array);
14696 cnst |= TYPE_CONST (el_type);
14697 voltl |= TYPE_VOLATILE (el_type);
14698 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14699
14700 return set_die_type (die, base_type, cu);
14701}
14702
f792889a 14703static struct type *
e7c27a73 14704read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14705{
f792889a 14706 struct type *base_type, *cv_type;
c906108c 14707
e7c27a73 14708 base_type = die_type (die, cu);
7e314c57
JK
14709
14710 /* The die_type call above may have already set the type for this DIE. */
14711 cv_type = get_die_type (die, cu);
14712 if (cv_type)
14713 return cv_type;
14714
2f608a3a
KW
14715 /* In case the const qualifier is applied to an array type, the element type
14716 is so qualified, not the array type (section 6.7.3 of C99). */
14717 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14718 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14719
f792889a
DJ
14720 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14721 return set_die_type (die, cv_type, cu);
c906108c
SS
14722}
14723
f792889a 14724static struct type *
e7c27a73 14725read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14726{
f792889a 14727 struct type *base_type, *cv_type;
c906108c 14728
e7c27a73 14729 base_type = die_type (die, cu);
7e314c57
JK
14730
14731 /* The die_type call above may have already set the type for this DIE. */
14732 cv_type = get_die_type (die, cu);
14733 if (cv_type)
14734 return cv_type;
14735
cf363f18
MW
14736 /* In case the volatile qualifier is applied to an array type, the
14737 element type is so qualified, not the array type (section 6.7.3
14738 of C99). */
14739 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14740 return add_array_cv_type (die, cu, base_type, 0, 1);
14741
f792889a
DJ
14742 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14743 return set_die_type (die, cv_type, cu);
c906108c
SS
14744}
14745
06d66ee9
TT
14746/* Handle DW_TAG_restrict_type. */
14747
14748static struct type *
14749read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14750{
14751 struct type *base_type, *cv_type;
14752
14753 base_type = die_type (die, cu);
14754
14755 /* The die_type call above may have already set the type for this DIE. */
14756 cv_type = get_die_type (die, cu);
14757 if (cv_type)
14758 return cv_type;
14759
14760 cv_type = make_restrict_type (base_type);
14761 return set_die_type (die, cv_type, cu);
14762}
14763
a2c2acaf
MW
14764/* Handle DW_TAG_atomic_type. */
14765
14766static struct type *
14767read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14768{
14769 struct type *base_type, *cv_type;
14770
14771 base_type = die_type (die, cu);
14772
14773 /* The die_type call above may have already set the type for this DIE. */
14774 cv_type = get_die_type (die, cu);
14775 if (cv_type)
14776 return cv_type;
14777
14778 cv_type = make_atomic_type (base_type);
14779 return set_die_type (die, cv_type, cu);
14780}
14781
c906108c
SS
14782/* Extract all information from a DW_TAG_string_type DIE and add to
14783 the user defined type vector. It isn't really a user defined type,
14784 but it behaves like one, with other DIE's using an AT_user_def_type
14785 attribute to reference it. */
14786
f792889a 14787static struct type *
e7c27a73 14788read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14789{
e7c27a73 14790 struct objfile *objfile = cu->objfile;
3b7538c0 14791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14792 struct type *type, *range_type, *index_type, *char_type;
14793 struct attribute *attr;
14794 unsigned int length;
14795
e142c38c 14796 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14797 if (attr)
14798 {
14799 length = DW_UNSND (attr);
14800 }
14801 else
14802 {
0963b4bd 14803 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14805 if (attr)
14806 {
14807 length = DW_UNSND (attr);
14808 }
14809 else
14810 {
14811 length = 1;
14812 }
c906108c 14813 }
6ccb9162 14814
46bf5051 14815 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14816 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14817 char_type = language_string_char_type (cu->language_defn, gdbarch);
14818 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14819
f792889a 14820 return set_die_type (die, type, cu);
c906108c
SS
14821}
14822
4d804846
JB
14823/* Assuming that DIE corresponds to a function, returns nonzero
14824 if the function is prototyped. */
14825
14826static int
14827prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14828{
14829 struct attribute *attr;
14830
14831 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14832 if (attr && (DW_UNSND (attr) != 0))
14833 return 1;
14834
14835 /* The DWARF standard implies that the DW_AT_prototyped attribute
14836 is only meaninful for C, but the concept also extends to other
14837 languages that allow unprototyped functions (Eg: Objective C).
14838 For all other languages, assume that functions are always
14839 prototyped. */
14840 if (cu->language != language_c
14841 && cu->language != language_objc
14842 && cu->language != language_opencl)
14843 return 1;
14844
14845 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14846 prototyped and unprototyped functions; default to prototyped,
14847 since that is more common in modern code (and RealView warns
14848 about unprototyped functions). */
14849 if (producer_is_realview (cu->producer))
14850 return 1;
14851
14852 return 0;
14853}
14854
c906108c
SS
14855/* Handle DIES due to C code like:
14856
14857 struct foo
c5aa993b
JM
14858 {
14859 int (*funcp)(int a, long l);
14860 int b;
14861 };
c906108c 14862
0963b4bd 14863 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14864
f792889a 14865static struct type *
e7c27a73 14866read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14867{
bb5ed363 14868 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14869 struct type *type; /* Type that this function returns. */
14870 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14871 struct attribute *attr;
14872
e7c27a73 14873 type = die_type (die, cu);
7e314c57
JK
14874
14875 /* The die_type call above may have already set the type for this DIE. */
14876 ftype = get_die_type (die, cu);
14877 if (ftype)
14878 return ftype;
14879
0c8b41f1 14880 ftype = lookup_function_type (type);
c906108c 14881
4d804846 14882 if (prototyped_function_p (die, cu))
a6c727b2 14883 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14884
c055b101
CV
14885 /* Store the calling convention in the type if it's available in
14886 the subroutine die. Otherwise set the calling convention to
14887 the default value DW_CC_normal. */
14888 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14889 if (attr)
14890 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14891 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14892 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14893 else
14894 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14895
743649fd
MW
14896 /* Record whether the function returns normally to its caller or not
14897 if the DWARF producer set that information. */
14898 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14899 if (attr && (DW_UNSND (attr) != 0))
14900 TYPE_NO_RETURN (ftype) = 1;
14901
76c10ea2
GM
14902 /* We need to add the subroutine type to the die immediately so
14903 we don't infinitely recurse when dealing with parameters
0963b4bd 14904 declared as the same subroutine type. */
76c10ea2 14905 set_die_type (die, ftype, cu);
6e70227d 14906
639d11d3 14907 if (die->child != NULL)
c906108c 14908 {
bb5ed363 14909 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14910 struct die_info *child_die;
8072405b 14911 int nparams, iparams;
c906108c
SS
14912
14913 /* Count the number of parameters.
14914 FIXME: GDB currently ignores vararg functions, but knows about
14915 vararg member functions. */
8072405b 14916 nparams = 0;
639d11d3 14917 child_die = die->child;
c906108c
SS
14918 while (child_die && child_die->tag)
14919 {
14920 if (child_die->tag == DW_TAG_formal_parameter)
14921 nparams++;
14922 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14923 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14924 child_die = sibling_die (child_die);
14925 }
14926
14927 /* Allocate storage for parameters and fill them in. */
14928 TYPE_NFIELDS (ftype) = nparams;
14929 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14930 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14931
8072405b
JK
14932 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14933 even if we error out during the parameters reading below. */
14934 for (iparams = 0; iparams < nparams; iparams++)
14935 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14936
14937 iparams = 0;
639d11d3 14938 child_die = die->child;
c906108c
SS
14939 while (child_die && child_die->tag)
14940 {
14941 if (child_die->tag == DW_TAG_formal_parameter)
14942 {
3ce3b1ba
PA
14943 struct type *arg_type;
14944
14945 /* DWARF version 2 has no clean way to discern C++
14946 static and non-static member functions. G++ helps
14947 GDB by marking the first parameter for non-static
14948 member functions (which is the this pointer) as
14949 artificial. We pass this information to
14950 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14951
14952 DWARF version 3 added DW_AT_object_pointer, which GCC
14953 4.5 does not yet generate. */
e142c38c 14954 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14955 if (attr)
14956 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14957 else
9c37b5ae 14958 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14959 arg_type = die_type (child_die, cu);
14960
14961 /* RealView does not mark THIS as const, which the testsuite
14962 expects. GCC marks THIS as const in method definitions,
14963 but not in the class specifications (GCC PR 43053). */
14964 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14965 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14966 {
14967 int is_this = 0;
14968 struct dwarf2_cu *arg_cu = cu;
14969 const char *name = dwarf2_name (child_die, cu);
14970
14971 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14972 if (attr)
14973 {
14974 /* If the compiler emits this, use it. */
14975 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14976 is_this = 1;
14977 }
14978 else if (name && strcmp (name, "this") == 0)
14979 /* Function definitions will have the argument names. */
14980 is_this = 1;
14981 else if (name == NULL && iparams == 0)
14982 /* Declarations may not have the names, so like
14983 elsewhere in GDB, assume an artificial first
14984 argument is "this". */
14985 is_this = 1;
14986
14987 if (is_this)
14988 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14989 arg_type, 0);
14990 }
14991
14992 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14993 iparams++;
14994 }
14995 child_die = sibling_die (child_die);
14996 }
14997 }
14998
76c10ea2 14999 return ftype;
c906108c
SS
15000}
15001
f792889a 15002static struct type *
e7c27a73 15003read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15004{
e7c27a73 15005 struct objfile *objfile = cu->objfile;
0114d602 15006 const char *name = NULL;
3c8e0968 15007 struct type *this_type, *target_type;
c906108c 15008
94af9270 15009 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15010 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15011 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15012 set_die_type (die, this_type, cu);
3c8e0968
DE
15013 target_type = die_type (die, cu);
15014 if (target_type != this_type)
15015 TYPE_TARGET_TYPE (this_type) = target_type;
15016 else
15017 {
15018 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15019 spec and cause infinite loops in GDB. */
15020 complaint (&symfile_complaints,
15021 _("Self-referential DW_TAG_typedef "
15022 "- DIE at 0x%x [in module %s]"),
9c541725 15023 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15024 TYPE_TARGET_TYPE (this_type) = NULL;
15025 }
f792889a 15026 return this_type;
c906108c
SS
15027}
15028
9b790ce7
UW
15029/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15030 (which may be different from NAME) to the architecture back-end to allow
15031 it to guess the correct format if necessary. */
15032
15033static struct type *
15034dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15035 const char *name_hint)
15036{
15037 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15038 const struct floatformat **format;
15039 struct type *type;
15040
15041 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15042 if (format)
15043 type = init_float_type (objfile, bits, name, format);
15044 else
15045 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15046
15047 return type;
15048}
15049
c906108c
SS
15050/* Find a representation of a given base type and install
15051 it in the TYPE field of the die. */
15052
f792889a 15053static struct type *
e7c27a73 15054read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15055{
e7c27a73 15056 struct objfile *objfile = cu->objfile;
c906108c
SS
15057 struct type *type;
15058 struct attribute *attr;
19f392bc 15059 int encoding = 0, bits = 0;
15d034d0 15060 const char *name;
c906108c 15061
e142c38c 15062 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15063 if (attr)
15064 {
15065 encoding = DW_UNSND (attr);
15066 }
e142c38c 15067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15068 if (attr)
15069 {
19f392bc 15070 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15071 }
39cbfefa 15072 name = dwarf2_name (die, cu);
6ccb9162 15073 if (!name)
c906108c 15074 {
6ccb9162
UW
15075 complaint (&symfile_complaints,
15076 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15077 }
6ccb9162
UW
15078
15079 switch (encoding)
c906108c 15080 {
6ccb9162
UW
15081 case DW_ATE_address:
15082 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15083 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15084 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15085 break;
15086 case DW_ATE_boolean:
19f392bc 15087 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15088 break;
15089 case DW_ATE_complex_float:
9b790ce7 15090 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15091 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15092 break;
15093 case DW_ATE_decimal_float:
19f392bc 15094 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15095 break;
15096 case DW_ATE_float:
9b790ce7 15097 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15098 break;
15099 case DW_ATE_signed:
19f392bc 15100 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15101 break;
15102 case DW_ATE_unsigned:
3b2b8fea
TT
15103 if (cu->language == language_fortran
15104 && name
61012eef 15105 && startswith (name, "character("))
19f392bc
UW
15106 type = init_character_type (objfile, bits, 1, name);
15107 else
15108 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15109 break;
15110 case DW_ATE_signed_char:
6e70227d 15111 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15112 || cu->language == language_pascal
15113 || cu->language == language_fortran)
19f392bc
UW
15114 type = init_character_type (objfile, bits, 0, name);
15115 else
15116 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15117 break;
15118 case DW_ATE_unsigned_char:
868a0084 15119 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15120 || cu->language == language_pascal
c44af4eb
TT
15121 || cu->language == language_fortran
15122 || cu->language == language_rust)
19f392bc
UW
15123 type = init_character_type (objfile, bits, 1, name);
15124 else
15125 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15126 break;
75079b2b 15127 case DW_ATE_UTF:
53e710ac
PA
15128 {
15129 gdbarch *arch = get_objfile_arch (objfile);
15130
15131 if (bits == 16)
15132 type = builtin_type (arch)->builtin_char16;
15133 else if (bits == 32)
15134 type = builtin_type (arch)->builtin_char32;
15135 else
15136 {
15137 complaint (&symfile_complaints,
15138 _("unsupported DW_ATE_UTF bit size: '%d'"),
15139 bits);
15140 type = init_integer_type (objfile, bits, 1, name);
15141 }
15142 return set_die_type (die, type, cu);
15143 }
75079b2b
TT
15144 break;
15145
6ccb9162
UW
15146 default:
15147 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15148 dwarf_type_encoding_name (encoding));
19f392bc
UW
15149 type = init_type (objfile, TYPE_CODE_ERROR,
15150 bits / TARGET_CHAR_BIT, name);
6ccb9162 15151 break;
c906108c 15152 }
6ccb9162 15153
0114d602 15154 if (name && strcmp (name, "char") == 0)
876cecd0 15155 TYPE_NOSIGN (type) = 1;
0114d602 15156
f792889a 15157 return set_die_type (die, type, cu);
c906108c
SS
15158}
15159
80180f79
SA
15160/* Parse dwarf attribute if it's a block, reference or constant and put the
15161 resulting value of the attribute into struct bound_prop.
15162 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15163
15164static int
15165attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15166 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15167{
15168 struct dwarf2_property_baton *baton;
15169 struct obstack *obstack = &cu->objfile->objfile_obstack;
15170
15171 if (attr == NULL || prop == NULL)
15172 return 0;
15173
15174 if (attr_form_is_block (attr))
15175 {
8d749320 15176 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15177 baton->referenced_type = NULL;
15178 baton->locexpr.per_cu = cu->per_cu;
15179 baton->locexpr.size = DW_BLOCK (attr)->size;
15180 baton->locexpr.data = DW_BLOCK (attr)->data;
15181 prop->data.baton = baton;
15182 prop->kind = PROP_LOCEXPR;
15183 gdb_assert (prop->data.baton != NULL);
15184 }
15185 else if (attr_form_is_ref (attr))
15186 {
15187 struct dwarf2_cu *target_cu = cu;
15188 struct die_info *target_die;
15189 struct attribute *target_attr;
15190
15191 target_die = follow_die_ref (die, attr, &target_cu);
15192 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15193 if (target_attr == NULL)
15194 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15195 target_cu);
80180f79
SA
15196 if (target_attr == NULL)
15197 return 0;
15198
df25ebbd 15199 switch (target_attr->name)
80180f79 15200 {
df25ebbd
JB
15201 case DW_AT_location:
15202 if (attr_form_is_section_offset (target_attr))
15203 {
8d749320 15204 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15205 baton->referenced_type = die_type (target_die, target_cu);
15206 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15207 prop->data.baton = baton;
15208 prop->kind = PROP_LOCLIST;
15209 gdb_assert (prop->data.baton != NULL);
15210 }
15211 else if (attr_form_is_block (target_attr))
15212 {
8d749320 15213 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15214 baton->referenced_type = die_type (target_die, target_cu);
15215 baton->locexpr.per_cu = cu->per_cu;
15216 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15217 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15218 prop->data.baton = baton;
15219 prop->kind = PROP_LOCEXPR;
15220 gdb_assert (prop->data.baton != NULL);
15221 }
15222 else
15223 {
15224 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15225 "dynamic property");
15226 return 0;
15227 }
15228 break;
15229 case DW_AT_data_member_location:
15230 {
15231 LONGEST offset;
15232
15233 if (!handle_data_member_location (target_die, target_cu,
15234 &offset))
15235 return 0;
15236
8d749320 15237 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15238 baton->referenced_type = read_type_die (target_die->parent,
15239 target_cu);
df25ebbd
JB
15240 baton->offset_info.offset = offset;
15241 baton->offset_info.type = die_type (target_die, target_cu);
15242 prop->data.baton = baton;
15243 prop->kind = PROP_ADDR_OFFSET;
15244 break;
15245 }
80180f79
SA
15246 }
15247 }
15248 else if (attr_form_is_constant (attr))
15249 {
15250 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15251 prop->kind = PROP_CONST;
15252 }
15253 else
15254 {
15255 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15256 dwarf2_name (die, cu));
15257 return 0;
15258 }
15259
15260 return 1;
15261}
15262
a02abb62
JB
15263/* Read the given DW_AT_subrange DIE. */
15264
f792889a 15265static struct type *
a02abb62
JB
15266read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15267{
4c9ad8c2 15268 struct type *base_type, *orig_base_type;
a02abb62
JB
15269 struct type *range_type;
15270 struct attribute *attr;
729efb13 15271 struct dynamic_prop low, high;
4fae6e18 15272 int low_default_is_valid;
c451ebe5 15273 int high_bound_is_count = 0;
15d034d0 15274 const char *name;
43bbcdc2 15275 LONGEST negative_mask;
e77813c8 15276
4c9ad8c2
TT
15277 orig_base_type = die_type (die, cu);
15278 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15279 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15280 creating the range type, but we use the result of check_typedef
15281 when examining properties of the type. */
15282 base_type = check_typedef (orig_base_type);
a02abb62 15283
7e314c57
JK
15284 /* The die_type call above may have already set the type for this DIE. */
15285 range_type = get_die_type (die, cu);
15286 if (range_type)
15287 return range_type;
15288
729efb13
SA
15289 low.kind = PROP_CONST;
15290 high.kind = PROP_CONST;
15291 high.data.const_val = 0;
15292
4fae6e18
JK
15293 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15294 omitting DW_AT_lower_bound. */
15295 switch (cu->language)
6e70227d 15296 {
4fae6e18
JK
15297 case language_c:
15298 case language_cplus:
729efb13 15299 low.data.const_val = 0;
4fae6e18
JK
15300 low_default_is_valid = 1;
15301 break;
15302 case language_fortran:
729efb13 15303 low.data.const_val = 1;
4fae6e18
JK
15304 low_default_is_valid = 1;
15305 break;
15306 case language_d:
4fae6e18 15307 case language_objc:
c44af4eb 15308 case language_rust:
729efb13 15309 low.data.const_val = 0;
4fae6e18
JK
15310 low_default_is_valid = (cu->header.version >= 4);
15311 break;
15312 case language_ada:
15313 case language_m2:
15314 case language_pascal:
729efb13 15315 low.data.const_val = 1;
4fae6e18
JK
15316 low_default_is_valid = (cu->header.version >= 4);
15317 break;
15318 default:
729efb13 15319 low.data.const_val = 0;
4fae6e18
JK
15320 low_default_is_valid = 0;
15321 break;
a02abb62
JB
15322 }
15323
e142c38c 15324 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15325 if (attr)
11c1ba78 15326 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15327 else if (!low_default_is_valid)
15328 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15329 "- DIE at 0x%x [in module %s]"),
9c541725 15330 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15331
e142c38c 15332 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15333 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15334 {
15335 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15336 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15337 {
c451ebe5
SA
15338 /* If bounds are constant do the final calculation here. */
15339 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15340 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15341 else
15342 high_bound_is_count = 1;
c2ff108b 15343 }
e77813c8
PM
15344 }
15345
15346 /* Dwarf-2 specifications explicitly allows to create subrange types
15347 without specifying a base type.
15348 In that case, the base type must be set to the type of
15349 the lower bound, upper bound or count, in that order, if any of these
15350 three attributes references an object that has a type.
15351 If no base type is found, the Dwarf-2 specifications say that
15352 a signed integer type of size equal to the size of an address should
15353 be used.
15354 For the following C code: `extern char gdb_int [];'
15355 GCC produces an empty range DIE.
15356 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15357 high bound or count are not yet handled by this code. */
e77813c8
PM
15358 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15359 {
15360 struct objfile *objfile = cu->objfile;
15361 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15362 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15363 struct type *int_type = objfile_type (objfile)->builtin_int;
15364
15365 /* Test "int", "long int", and "long long int" objfile types,
15366 and select the first one having a size above or equal to the
15367 architecture address size. */
15368 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15369 base_type = int_type;
15370 else
15371 {
15372 int_type = objfile_type (objfile)->builtin_long;
15373 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15374 base_type = int_type;
15375 else
15376 {
15377 int_type = objfile_type (objfile)->builtin_long_long;
15378 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15379 base_type = int_type;
15380 }
15381 }
15382 }
a02abb62 15383
dbb9c2b1
JB
15384 /* Normally, the DWARF producers are expected to use a signed
15385 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15386 But this is unfortunately not always the case, as witnessed
15387 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15388 is used instead. To work around that ambiguity, we treat
15389 the bounds as signed, and thus sign-extend their values, when
15390 the base type is signed. */
6e70227d 15391 negative_mask =
66c6502d 15392 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15393 if (low.kind == PROP_CONST
15394 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15395 low.data.const_val |= negative_mask;
15396 if (high.kind == PROP_CONST
15397 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15398 high.data.const_val |= negative_mask;
43bbcdc2 15399
729efb13 15400 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15401
c451ebe5
SA
15402 if (high_bound_is_count)
15403 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15404
c2ff108b
JK
15405 /* Ada expects an empty array on no boundary attributes. */
15406 if (attr == NULL && cu->language != language_ada)
729efb13 15407 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15408
39cbfefa
DJ
15409 name = dwarf2_name (die, cu);
15410 if (name)
15411 TYPE_NAME (range_type) = name;
6e70227d 15412
e142c38c 15413 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15414 if (attr)
15415 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15416
7e314c57
JK
15417 set_die_type (die, range_type, cu);
15418
15419 /* set_die_type should be already done. */
b4ba55a1
JB
15420 set_descriptive_type (range_type, die, cu);
15421
7e314c57 15422 return range_type;
a02abb62 15423}
6e70227d 15424
f792889a 15425static struct type *
81a17f79
JB
15426read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15427{
15428 struct type *type;
81a17f79 15429
81a17f79
JB
15430 /* For now, we only support the C meaning of an unspecified type: void. */
15431
19f392bc 15432 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15433 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15434
f792889a 15435 return set_die_type (die, type, cu);
81a17f79 15436}
a02abb62 15437
639d11d3
DC
15438/* Read a single die and all its descendents. Set the die's sibling
15439 field to NULL; set other fields in the die correctly, and set all
15440 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15441 location of the info_ptr after reading all of those dies. PARENT
15442 is the parent of the die in question. */
15443
15444static struct die_info *
dee91e82 15445read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15446 const gdb_byte *info_ptr,
15447 const gdb_byte **new_info_ptr,
dee91e82 15448 struct die_info *parent)
639d11d3
DC
15449{
15450 struct die_info *die;
d521ce57 15451 const gdb_byte *cur_ptr;
639d11d3
DC
15452 int has_children;
15453
bf6af496 15454 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15455 if (die == NULL)
15456 {
15457 *new_info_ptr = cur_ptr;
15458 return NULL;
15459 }
93311388 15460 store_in_ref_table (die, reader->cu);
639d11d3
DC
15461
15462 if (has_children)
bf6af496 15463 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15464 else
15465 {
15466 die->child = NULL;
15467 *new_info_ptr = cur_ptr;
15468 }
15469
15470 die->sibling = NULL;
15471 die->parent = parent;
15472 return die;
15473}
15474
15475/* Read a die, all of its descendents, and all of its siblings; set
15476 all of the fields of all of the dies correctly. Arguments are as
15477 in read_die_and_children. */
15478
15479static struct die_info *
bf6af496 15480read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15481 const gdb_byte *info_ptr,
15482 const gdb_byte **new_info_ptr,
bf6af496 15483 struct die_info *parent)
639d11d3
DC
15484{
15485 struct die_info *first_die, *last_sibling;
d521ce57 15486 const gdb_byte *cur_ptr;
639d11d3 15487
c906108c 15488 cur_ptr = info_ptr;
639d11d3
DC
15489 first_die = last_sibling = NULL;
15490
15491 while (1)
c906108c 15492 {
639d11d3 15493 struct die_info *die
dee91e82 15494 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15495
1d325ec1 15496 if (die == NULL)
c906108c 15497 {
639d11d3
DC
15498 *new_info_ptr = cur_ptr;
15499 return first_die;
c906108c 15500 }
1d325ec1
DJ
15501
15502 if (!first_die)
15503 first_die = die;
c906108c 15504 else
1d325ec1
DJ
15505 last_sibling->sibling = die;
15506
15507 last_sibling = die;
c906108c 15508 }
c906108c
SS
15509}
15510
bf6af496
DE
15511/* Read a die, all of its descendents, and all of its siblings; set
15512 all of the fields of all of the dies correctly. Arguments are as
15513 in read_die_and_children.
15514 This the main entry point for reading a DIE and all its children. */
15515
15516static struct die_info *
15517read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15518 const gdb_byte *info_ptr,
15519 const gdb_byte **new_info_ptr,
bf6af496
DE
15520 struct die_info *parent)
15521{
15522 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15523 new_info_ptr, parent);
15524
b4f54984 15525 if (dwarf_die_debug)
bf6af496
DE
15526 {
15527 fprintf_unfiltered (gdb_stdlog,
15528 "Read die from %s@0x%x of %s:\n",
a32a8923 15529 get_section_name (reader->die_section),
bf6af496
DE
15530 (unsigned) (info_ptr - reader->die_section->buffer),
15531 bfd_get_filename (reader->abfd));
b4f54984 15532 dump_die (die, dwarf_die_debug);
bf6af496
DE
15533 }
15534
15535 return die;
15536}
15537
3019eac3
DE
15538/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15539 attributes.
15540 The caller is responsible for filling in the extra attributes
15541 and updating (*DIEP)->num_attrs.
15542 Set DIEP to point to a newly allocated die with its information,
15543 except for its child, sibling, and parent fields.
15544 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15545
d521ce57 15546static const gdb_byte *
3019eac3 15547read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15548 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15549 int *has_children, int num_extra_attrs)
93311388 15550{
b64f50a1 15551 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15552 struct abbrev_info *abbrev;
15553 struct die_info *die;
15554 struct dwarf2_cu *cu = reader->cu;
15555 bfd *abfd = reader->abfd;
15556
9c541725 15557 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15558 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15559 info_ptr += bytes_read;
15560 if (!abbrev_number)
15561 {
15562 *diep = NULL;
15563 *has_children = 0;
15564 return info_ptr;
15565 }
15566
433df2d4 15567 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15568 if (!abbrev)
348e048f
DE
15569 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15570 abbrev_number,
15571 bfd_get_filename (abfd));
15572
3019eac3 15573 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15574 die->sect_off = sect_off;
93311388
DE
15575 die->tag = abbrev->tag;
15576 die->abbrev = abbrev_number;
15577
3019eac3
DE
15578 /* Make the result usable.
15579 The caller needs to update num_attrs after adding the extra
15580 attributes. */
93311388
DE
15581 die->num_attrs = abbrev->num_attrs;
15582
15583 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15584 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15585 info_ptr);
93311388
DE
15586
15587 *diep = die;
15588 *has_children = abbrev->has_children;
15589 return info_ptr;
15590}
15591
3019eac3
DE
15592/* Read a die and all its attributes.
15593 Set DIEP to point to a newly allocated die with its information,
15594 except for its child, sibling, and parent fields.
15595 Set HAS_CHILDREN to tell whether the die has children or not. */
15596
d521ce57 15597static const gdb_byte *
3019eac3 15598read_full_die (const struct die_reader_specs *reader,
d521ce57 15599 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15600 int *has_children)
15601{
d521ce57 15602 const gdb_byte *result;
bf6af496
DE
15603
15604 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15605
b4f54984 15606 if (dwarf_die_debug)
bf6af496
DE
15607 {
15608 fprintf_unfiltered (gdb_stdlog,
15609 "Read die from %s@0x%x of %s:\n",
a32a8923 15610 get_section_name (reader->die_section),
bf6af496
DE
15611 (unsigned) (info_ptr - reader->die_section->buffer),
15612 bfd_get_filename (reader->abfd));
b4f54984 15613 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15614 }
15615
15616 return result;
3019eac3 15617}
433df2d4
DE
15618\f
15619/* Abbreviation tables.
3019eac3 15620
433df2d4 15621 In DWARF version 2, the description of the debugging information is
c906108c
SS
15622 stored in a separate .debug_abbrev section. Before we read any
15623 dies from a section we read in all abbreviations and install them
433df2d4
DE
15624 in a hash table. */
15625
15626/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15627
15628static struct abbrev_info *
15629abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15630{
15631 struct abbrev_info *abbrev;
15632
8d749320 15633 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15634 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15635
433df2d4
DE
15636 return abbrev;
15637}
15638
15639/* Add an abbreviation to the table. */
c906108c
SS
15640
15641static void
433df2d4
DE
15642abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15643 unsigned int abbrev_number,
15644 struct abbrev_info *abbrev)
15645{
15646 unsigned int hash_number;
15647
15648 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15649 abbrev->next = abbrev_table->abbrevs[hash_number];
15650 abbrev_table->abbrevs[hash_number] = abbrev;
15651}
dee91e82 15652
433df2d4
DE
15653/* Look up an abbrev in the table.
15654 Returns NULL if the abbrev is not found. */
15655
15656static struct abbrev_info *
15657abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15658 unsigned int abbrev_number)
c906108c 15659{
433df2d4
DE
15660 unsigned int hash_number;
15661 struct abbrev_info *abbrev;
15662
15663 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15664 abbrev = abbrev_table->abbrevs[hash_number];
15665
15666 while (abbrev)
15667 {
15668 if (abbrev->number == abbrev_number)
15669 return abbrev;
15670 abbrev = abbrev->next;
15671 }
15672 return NULL;
15673}
15674
15675/* Read in an abbrev table. */
15676
15677static struct abbrev_table *
15678abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15679 sect_offset sect_off)
433df2d4
DE
15680{
15681 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15682 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15683 struct abbrev_table *abbrev_table;
d521ce57 15684 const gdb_byte *abbrev_ptr;
c906108c
SS
15685 struct abbrev_info *cur_abbrev;
15686 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15687 unsigned int abbrev_form;
f3dd6933
DJ
15688 struct attr_abbrev *cur_attrs;
15689 unsigned int allocated_attrs;
c906108c 15690
70ba0933 15691 abbrev_table = XNEW (struct abbrev_table);
9c541725 15692 abbrev_table->sect_off = sect_off;
433df2d4 15693 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15694 abbrev_table->abbrevs =
15695 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15696 ABBREV_HASH_SIZE);
433df2d4
DE
15697 memset (abbrev_table->abbrevs, 0,
15698 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15699
433df2d4 15700 dwarf2_read_section (objfile, section);
9c541725 15701 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15702 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15703 abbrev_ptr += bytes_read;
15704
f3dd6933 15705 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15706 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15707
0963b4bd 15708 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15709 while (abbrev_number)
15710 {
433df2d4 15711 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15712
15713 /* read in abbrev header */
15714 cur_abbrev->number = abbrev_number;
aead7601
SM
15715 cur_abbrev->tag
15716 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15717 abbrev_ptr += bytes_read;
15718 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15719 abbrev_ptr += 1;
15720
15721 /* now read in declarations */
22d2f3ab 15722 for (;;)
c906108c 15723 {
43988095
JK
15724 LONGEST implicit_const;
15725
22d2f3ab
JK
15726 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15727 abbrev_ptr += bytes_read;
15728 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15729 abbrev_ptr += bytes_read;
43988095
JK
15730 if (abbrev_form == DW_FORM_implicit_const)
15731 {
15732 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15733 &bytes_read);
15734 abbrev_ptr += bytes_read;
15735 }
15736 else
15737 {
15738 /* Initialize it due to a false compiler warning. */
15739 implicit_const = -1;
15740 }
22d2f3ab
JK
15741
15742 if (abbrev_name == 0)
15743 break;
15744
f3dd6933 15745 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15746 {
f3dd6933
DJ
15747 allocated_attrs += ATTR_ALLOC_CHUNK;
15748 cur_attrs
224c3ddb 15749 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15750 }
ae038cb0 15751
aead7601
SM
15752 cur_attrs[cur_abbrev->num_attrs].name
15753 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15754 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15755 = (enum dwarf_form) abbrev_form;
43988095 15756 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15757 ++cur_abbrev->num_attrs;
c906108c
SS
15758 }
15759
8d749320
SM
15760 cur_abbrev->attrs =
15761 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15762 cur_abbrev->num_attrs);
f3dd6933
DJ
15763 memcpy (cur_abbrev->attrs, cur_attrs,
15764 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15765
433df2d4 15766 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15767
15768 /* Get next abbreviation.
15769 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15770 always properly terminated with an abbrev number of 0.
15771 Exit loop if we encounter an abbreviation which we have
15772 already read (which means we are about to read the abbreviations
15773 for the next compile unit) or if the end of the abbreviation
15774 table is reached. */
433df2d4 15775 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15776 break;
15777 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15778 abbrev_ptr += bytes_read;
433df2d4 15779 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15780 break;
15781 }
f3dd6933
DJ
15782
15783 xfree (cur_attrs);
433df2d4 15784 return abbrev_table;
c906108c
SS
15785}
15786
433df2d4 15787/* Free the resources held by ABBREV_TABLE. */
c906108c 15788
c906108c 15789static void
433df2d4 15790abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15791{
433df2d4
DE
15792 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15793 xfree (abbrev_table);
c906108c
SS
15794}
15795
f4dc4d17
DE
15796/* Same as abbrev_table_free but as a cleanup.
15797 We pass in a pointer to the pointer to the table so that we can
15798 set the pointer to NULL when we're done. It also simplifies
73051182 15799 build_type_psymtabs_1. */
f4dc4d17
DE
15800
15801static void
15802abbrev_table_free_cleanup (void *table_ptr)
15803{
9a3c8263 15804 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15805
15806 if (*abbrev_table_ptr != NULL)
15807 abbrev_table_free (*abbrev_table_ptr);
15808 *abbrev_table_ptr = NULL;
15809}
15810
433df2d4
DE
15811/* Read the abbrev table for CU from ABBREV_SECTION. */
15812
15813static void
15814dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15815 struct dwarf2_section_info *abbrev_section)
c906108c 15816{
433df2d4 15817 cu->abbrev_table =
9c541725 15818 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15819}
c906108c 15820
433df2d4 15821/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15822
433df2d4
DE
15823static void
15824dwarf2_free_abbrev_table (void *ptr_to_cu)
15825{
9a3c8263 15826 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15827
a2ce51a0
DE
15828 if (cu->abbrev_table != NULL)
15829 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15830 /* Set this to NULL so that we SEGV if we try to read it later,
15831 and also because free_comp_unit verifies this is NULL. */
15832 cu->abbrev_table = NULL;
15833}
15834\f
72bf9492
DJ
15835/* Returns nonzero if TAG represents a type that we might generate a partial
15836 symbol for. */
15837
15838static int
15839is_type_tag_for_partial (int tag)
15840{
15841 switch (tag)
15842 {
15843#if 0
15844 /* Some types that would be reasonable to generate partial symbols for,
15845 that we don't at present. */
15846 case DW_TAG_array_type:
15847 case DW_TAG_file_type:
15848 case DW_TAG_ptr_to_member_type:
15849 case DW_TAG_set_type:
15850 case DW_TAG_string_type:
15851 case DW_TAG_subroutine_type:
15852#endif
15853 case DW_TAG_base_type:
15854 case DW_TAG_class_type:
680b30c7 15855 case DW_TAG_interface_type:
72bf9492
DJ
15856 case DW_TAG_enumeration_type:
15857 case DW_TAG_structure_type:
15858 case DW_TAG_subrange_type:
15859 case DW_TAG_typedef:
15860 case DW_TAG_union_type:
15861 return 1;
15862 default:
15863 return 0;
15864 }
15865}
15866
15867/* Load all DIEs that are interesting for partial symbols into memory. */
15868
15869static struct partial_die_info *
dee91e82 15870load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15871 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15872{
dee91e82 15873 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15874 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15875 struct partial_die_info *part_die;
15876 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15877 struct abbrev_info *abbrev;
15878 unsigned int bytes_read;
5afb4e99 15879 unsigned int load_all = 0;
72bf9492
DJ
15880 int nesting_level = 1;
15881
15882 parent_die = NULL;
15883 last_die = NULL;
15884
7adf1e79
DE
15885 gdb_assert (cu->per_cu != NULL);
15886 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15887 load_all = 1;
15888
72bf9492
DJ
15889 cu->partial_dies
15890 = htab_create_alloc_ex (cu->header.length / 12,
15891 partial_die_hash,
15892 partial_die_eq,
15893 NULL,
15894 &cu->comp_unit_obstack,
15895 hashtab_obstack_allocate,
15896 dummy_obstack_deallocate);
15897
8d749320 15898 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15899
15900 while (1)
15901 {
15902 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15903
15904 /* A NULL abbrev means the end of a series of children. */
15905 if (abbrev == NULL)
15906 {
15907 if (--nesting_level == 0)
15908 {
15909 /* PART_DIE was probably the last thing allocated on the
15910 comp_unit_obstack, so we could call obstack_free
15911 here. We don't do that because the waste is small,
15912 and will be cleaned up when we're done with this
15913 compilation unit. This way, we're also more robust
15914 against other users of the comp_unit_obstack. */
15915 return first_die;
15916 }
15917 info_ptr += bytes_read;
15918 last_die = parent_die;
15919 parent_die = parent_die->die_parent;
15920 continue;
15921 }
15922
98bfdba5
PA
15923 /* Check for template arguments. We never save these; if
15924 they're seen, we just mark the parent, and go on our way. */
15925 if (parent_die != NULL
15926 && cu->language == language_cplus
15927 && (abbrev->tag == DW_TAG_template_type_param
15928 || abbrev->tag == DW_TAG_template_value_param))
15929 {
15930 parent_die->has_template_arguments = 1;
15931
15932 if (!load_all)
15933 {
15934 /* We don't need a partial DIE for the template argument. */
dee91e82 15935 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15936 continue;
15937 }
15938 }
15939
0d99eb77 15940 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15941 Skip their other children. */
15942 if (!load_all
15943 && cu->language == language_cplus
15944 && parent_die != NULL
15945 && parent_die->tag == DW_TAG_subprogram)
15946 {
dee91e82 15947 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15948 continue;
15949 }
15950
5afb4e99
DJ
15951 /* Check whether this DIE is interesting enough to save. Normally
15952 we would not be interested in members here, but there may be
15953 later variables referencing them via DW_AT_specification (for
15954 static members). */
15955 if (!load_all
15956 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15957 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15958 && abbrev->tag != DW_TAG_enumerator
15959 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15960 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15961 && abbrev->tag != DW_TAG_variable
5afb4e99 15962 && abbrev->tag != DW_TAG_namespace
f55ee35c 15963 && abbrev->tag != DW_TAG_module
95554aad 15964 && abbrev->tag != DW_TAG_member
74921315
KS
15965 && abbrev->tag != DW_TAG_imported_unit
15966 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15967 {
15968 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15969 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15970 continue;
15971 }
15972
dee91e82
DE
15973 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15974 info_ptr);
72bf9492
DJ
15975
15976 /* This two-pass algorithm for processing partial symbols has a
15977 high cost in cache pressure. Thus, handle some simple cases
15978 here which cover the majority of C partial symbols. DIEs
15979 which neither have specification tags in them, nor could have
15980 specification tags elsewhere pointing at them, can simply be
15981 processed and discarded.
15982
15983 This segment is also optional; scan_partial_symbols and
15984 add_partial_symbol will handle these DIEs if we chain
15985 them in normally. When compilers which do not emit large
15986 quantities of duplicate debug information are more common,
15987 this code can probably be removed. */
15988
15989 /* Any complete simple types at the top level (pretty much all
15990 of them, for a language without namespaces), can be processed
15991 directly. */
15992 if (parent_die == NULL
15993 && part_die->has_specification == 0
15994 && part_die->is_declaration == 0
d8228535 15995 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15996 || part_die->tag == DW_TAG_base_type
15997 || part_die->tag == DW_TAG_subrange_type))
15998 {
15999 if (building_psymtab && part_die->name != NULL)
04a679b8 16000 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16001 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16002 &objfile->static_psymbols,
1762568f 16003 0, cu->language, objfile);
dee91e82 16004 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16005 continue;
16006 }
16007
d8228535
JK
16008 /* The exception for DW_TAG_typedef with has_children above is
16009 a workaround of GCC PR debug/47510. In the case of this complaint
16010 type_name_no_tag_or_error will error on such types later.
16011
16012 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16013 it could not find the child DIEs referenced later, this is checked
16014 above. In correct DWARF DW_TAG_typedef should have no children. */
16015
16016 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16017 complaint (&symfile_complaints,
16018 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16019 "- DIE at 0x%x [in module %s]"),
9c541725 16020 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16021
72bf9492
DJ
16022 /* If we're at the second level, and we're an enumerator, and
16023 our parent has no specification (meaning possibly lives in a
16024 namespace elsewhere), then we can add the partial symbol now
16025 instead of queueing it. */
16026 if (part_die->tag == DW_TAG_enumerator
16027 && parent_die != NULL
16028 && parent_die->die_parent == NULL
16029 && parent_die->tag == DW_TAG_enumeration_type
16030 && parent_die->has_specification == 0)
16031 {
16032 if (part_die->name == NULL)
3e43a32a
MS
16033 complaint (&symfile_complaints,
16034 _("malformed enumerator DIE ignored"));
72bf9492 16035 else if (building_psymtab)
04a679b8 16036 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16037 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16038 cu->language == language_cplus
bb5ed363
DE
16039 ? &objfile->global_psymbols
16040 : &objfile->static_psymbols,
1762568f 16041 0, cu->language, objfile);
72bf9492 16042
dee91e82 16043 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16044 continue;
16045 }
16046
16047 /* We'll save this DIE so link it in. */
16048 part_die->die_parent = parent_die;
16049 part_die->die_sibling = NULL;
16050 part_die->die_child = NULL;
16051
16052 if (last_die && last_die == parent_die)
16053 last_die->die_child = part_die;
16054 else if (last_die)
16055 last_die->die_sibling = part_die;
16056
16057 last_die = part_die;
16058
16059 if (first_die == NULL)
16060 first_die = part_die;
16061
16062 /* Maybe add the DIE to the hash table. Not all DIEs that we
16063 find interesting need to be in the hash table, because we
16064 also have the parent/sibling/child chains; only those that we
16065 might refer to by offset later during partial symbol reading.
16066
16067 For now this means things that might have be the target of a
16068 DW_AT_specification, DW_AT_abstract_origin, or
16069 DW_AT_extension. DW_AT_extension will refer only to
16070 namespaces; DW_AT_abstract_origin refers to functions (and
16071 many things under the function DIE, but we do not recurse
16072 into function DIEs during partial symbol reading) and
16073 possibly variables as well; DW_AT_specification refers to
16074 declarations. Declarations ought to have the DW_AT_declaration
16075 flag. It happens that GCC forgets to put it in sometimes, but
16076 only for functions, not for types.
16077
16078 Adding more things than necessary to the hash table is harmless
16079 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16080 wasted time in find_partial_die, when we reread the compilation
16081 unit with load_all_dies set. */
72bf9492 16082
5afb4e99 16083 if (load_all
72929c62 16084 || abbrev->tag == DW_TAG_constant
5afb4e99 16085 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16086 || abbrev->tag == DW_TAG_variable
16087 || abbrev->tag == DW_TAG_namespace
16088 || part_die->is_declaration)
16089 {
16090 void **slot;
16091
16092 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16093 to_underlying (part_die->sect_off),
16094 INSERT);
72bf9492
DJ
16095 *slot = part_die;
16096 }
16097
8d749320 16098 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16099
16100 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16101 we have no reason to follow the children of structures; for other
98bfdba5
PA
16102 languages we have to, so that we can get at method physnames
16103 to infer fully qualified class names, for DW_AT_specification,
16104 and for C++ template arguments. For C++, we also look one level
16105 inside functions to find template arguments (if the name of the
16106 function does not already contain the template arguments).
bc30ff58
JB
16107
16108 For Ada, we need to scan the children of subprograms and lexical
16109 blocks as well because Ada allows the definition of nested
16110 entities that could be interesting for the debugger, such as
16111 nested subprograms for instance. */
72bf9492 16112 if (last_die->has_children
5afb4e99
DJ
16113 && (load_all
16114 || last_die->tag == DW_TAG_namespace
f55ee35c 16115 || last_die->tag == DW_TAG_module
72bf9492 16116 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16117 || (cu->language == language_cplus
16118 && last_die->tag == DW_TAG_subprogram
16119 && (last_die->name == NULL
16120 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16121 || (cu->language != language_c
16122 && (last_die->tag == DW_TAG_class_type
680b30c7 16123 || last_die->tag == DW_TAG_interface_type
72bf9492 16124 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16125 || last_die->tag == DW_TAG_union_type))
16126 || (cu->language == language_ada
16127 && (last_die->tag == DW_TAG_subprogram
16128 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16129 {
16130 nesting_level++;
16131 parent_die = last_die;
16132 continue;
16133 }
16134
16135 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16136 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16137
16138 /* Back to the top, do it again. */
16139 }
16140}
16141
c906108c
SS
16142/* Read a minimal amount of information into the minimal die structure. */
16143
d521ce57 16144static const gdb_byte *
dee91e82
DE
16145read_partial_die (const struct die_reader_specs *reader,
16146 struct partial_die_info *part_die,
16147 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16148 const gdb_byte *info_ptr)
c906108c 16149{
dee91e82 16150 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16151 struct objfile *objfile = cu->objfile;
d521ce57 16152 const gdb_byte *buffer = reader->buffer;
fa238c03 16153 unsigned int i;
c906108c 16154 struct attribute attr;
c5aa993b 16155 int has_low_pc_attr = 0;
c906108c 16156 int has_high_pc_attr = 0;
91da1414 16157 int high_pc_relative = 0;
c906108c 16158
72bf9492 16159 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16160
9c541725 16161 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16162
16163 info_ptr += abbrev_len;
16164
16165 if (abbrev == NULL)
16166 return info_ptr;
16167
c906108c
SS
16168 part_die->tag = abbrev->tag;
16169 part_die->has_children = abbrev->has_children;
c906108c
SS
16170
16171 for (i = 0; i < abbrev->num_attrs; ++i)
16172 {
dee91e82 16173 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16174
16175 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16176 partial symbol table. */
c906108c
SS
16177 switch (attr.name)
16178 {
16179 case DW_AT_name:
71c25dea
TT
16180 switch (part_die->tag)
16181 {
16182 case DW_TAG_compile_unit:
95554aad 16183 case DW_TAG_partial_unit:
348e048f 16184 case DW_TAG_type_unit:
71c25dea
TT
16185 /* Compilation units have a DW_AT_name that is a filename, not
16186 a source language identifier. */
16187 case DW_TAG_enumeration_type:
16188 case DW_TAG_enumerator:
16189 /* These tags always have simple identifiers already; no need
16190 to canonicalize them. */
16191 part_die->name = DW_STRING (&attr);
16192 break;
16193 default:
16194 part_die->name
16195 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16196 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16197 break;
16198 }
c906108c 16199 break;
31ef98ae 16200 case DW_AT_linkage_name:
c906108c 16201 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16202 /* Note that both forms of linkage name might appear. We
16203 assume they will be the same, and we only store the last
16204 one we see. */
94af9270
KS
16205 if (cu->language == language_ada)
16206 part_die->name = DW_STRING (&attr);
abc72ce4 16207 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16208 break;
16209 case DW_AT_low_pc:
16210 has_low_pc_attr = 1;
31aa7e4e 16211 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16212 break;
16213 case DW_AT_high_pc:
16214 has_high_pc_attr = 1;
31aa7e4e
JB
16215 part_die->highpc = attr_value_as_address (&attr);
16216 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16217 high_pc_relative = 1;
c906108c
SS
16218 break;
16219 case DW_AT_location:
0963b4bd 16220 /* Support the .debug_loc offsets. */
8e19ed76
PS
16221 if (attr_form_is_block (&attr))
16222 {
95554aad 16223 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16224 }
3690dd37 16225 else if (attr_form_is_section_offset (&attr))
8e19ed76 16226 {
4d3c2250 16227 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16228 }
16229 else
16230 {
4d3c2250
KB
16231 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16232 "partial symbol information");
8e19ed76 16233 }
c906108c 16234 break;
c906108c
SS
16235 case DW_AT_external:
16236 part_die->is_external = DW_UNSND (&attr);
16237 break;
16238 case DW_AT_declaration:
16239 part_die->is_declaration = DW_UNSND (&attr);
16240 break;
16241 case DW_AT_type:
16242 part_die->has_type = 1;
16243 break;
16244 case DW_AT_abstract_origin:
16245 case DW_AT_specification:
72bf9492
DJ
16246 case DW_AT_extension:
16247 part_die->has_specification = 1;
c764a876 16248 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16249 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16250 || cu->per_cu->is_dwz);
c906108c
SS
16251 break;
16252 case DW_AT_sibling:
16253 /* Ignore absolute siblings, they might point outside of
16254 the current compile unit. */
16255 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16256 complaint (&symfile_complaints,
16257 _("ignoring absolute DW_AT_sibling"));
c906108c 16258 else
b9502d3f 16259 {
9c541725
PA
16260 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16261 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16262
16263 if (sibling_ptr < info_ptr)
16264 complaint (&symfile_complaints,
16265 _("DW_AT_sibling points backwards"));
22869d73
KS
16266 else if (sibling_ptr > reader->buffer_end)
16267 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16268 else
16269 part_die->sibling = sibling_ptr;
16270 }
c906108c 16271 break;
fa4028e9
JB
16272 case DW_AT_byte_size:
16273 part_die->has_byte_size = 1;
16274 break;
ff908ebf
AW
16275 case DW_AT_const_value:
16276 part_die->has_const_value = 1;
16277 break;
68511cec
CES
16278 case DW_AT_calling_convention:
16279 /* DWARF doesn't provide a way to identify a program's source-level
16280 entry point. DW_AT_calling_convention attributes are only meant
16281 to describe functions' calling conventions.
16282
16283 However, because it's a necessary piece of information in
0c1b455e
TT
16284 Fortran, and before DWARF 4 DW_CC_program was the only
16285 piece of debugging information whose definition refers to
16286 a 'main program' at all, several compilers marked Fortran
16287 main programs with DW_CC_program --- even when those
16288 functions use the standard calling conventions.
16289
16290 Although DWARF now specifies a way to provide this
16291 information, we support this practice for backward
16292 compatibility. */
68511cec 16293 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16294 && cu->language == language_fortran)
16295 part_die->main_subprogram = 1;
68511cec 16296 break;
481860b3
GB
16297 case DW_AT_inline:
16298 if (DW_UNSND (&attr) == DW_INL_inlined
16299 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16300 part_die->may_be_inlined = 1;
16301 break;
95554aad
TT
16302
16303 case DW_AT_import:
16304 if (part_die->tag == DW_TAG_imported_unit)
36586728 16305 {
9c541725 16306 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16307 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16308 || cu->per_cu->is_dwz);
16309 }
95554aad
TT
16310 break;
16311
0c1b455e
TT
16312 case DW_AT_main_subprogram:
16313 part_die->main_subprogram = DW_UNSND (&attr);
16314 break;
16315
c906108c
SS
16316 default:
16317 break;
16318 }
16319 }
16320
91da1414
MW
16321 if (high_pc_relative)
16322 part_die->highpc += part_die->lowpc;
16323
9373cf26
JK
16324 if (has_low_pc_attr && has_high_pc_attr)
16325 {
16326 /* When using the GNU linker, .gnu.linkonce. sections are used to
16327 eliminate duplicate copies of functions and vtables and such.
16328 The linker will arbitrarily choose one and discard the others.
16329 The AT_*_pc values for such functions refer to local labels in
16330 these sections. If the section from that file was discarded, the
16331 labels are not in the output, so the relocs get a value of 0.
16332 If this is a discarded function, mark the pc bounds as invalid,
16333 so that GDB will ignore it. */
16334 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16335 {
bb5ed363 16336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16337
16338 complaint (&symfile_complaints,
16339 _("DW_AT_low_pc %s is zero "
16340 "for DIE at 0x%x [in module %s]"),
16341 paddress (gdbarch, part_die->lowpc),
9c541725 16342 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16343 }
16344 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16345 else if (part_die->lowpc >= part_die->highpc)
16346 {
bb5ed363 16347 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16348
16349 complaint (&symfile_complaints,
16350 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16351 "for DIE at 0x%x [in module %s]"),
16352 paddress (gdbarch, part_die->lowpc),
16353 paddress (gdbarch, part_die->highpc),
9c541725
PA
16354 to_underlying (part_die->sect_off),
16355 objfile_name (objfile));
9373cf26
JK
16356 }
16357 else
16358 part_die->has_pc_info = 1;
16359 }
85cbf3d3 16360
c906108c
SS
16361 return info_ptr;
16362}
16363
72bf9492
DJ
16364/* Find a cached partial DIE at OFFSET in CU. */
16365
16366static struct partial_die_info *
9c541725 16367find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16368{
16369 struct partial_die_info *lookup_die = NULL;
16370 struct partial_die_info part_die;
16371
9c541725 16372 part_die.sect_off = sect_off;
9a3c8263
SM
16373 lookup_die = ((struct partial_die_info *)
16374 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16375 to_underlying (sect_off)));
72bf9492 16376
72bf9492
DJ
16377 return lookup_die;
16378}
16379
348e048f
DE
16380/* Find a partial DIE at OFFSET, which may or may not be in CU,
16381 except in the case of .debug_types DIEs which do not reference
16382 outside their CU (they do however referencing other types via
55f1336d 16383 DW_FORM_ref_sig8). */
72bf9492
DJ
16384
16385static struct partial_die_info *
9c541725 16386find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16387{
bb5ed363 16388 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16389 struct dwarf2_per_cu_data *per_cu = NULL;
16390 struct partial_die_info *pd = NULL;
72bf9492 16391
36586728 16392 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16393 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16394 {
9c541725 16395 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16396 if (pd != NULL)
16397 return pd;
0d99eb77
DE
16398 /* We missed recording what we needed.
16399 Load all dies and try again. */
16400 per_cu = cu->per_cu;
5afb4e99 16401 }
0d99eb77
DE
16402 else
16403 {
16404 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16405 if (cu->per_cu->is_debug_types)
0d99eb77 16406 {
9c541725
PA
16407 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16408 " external reference to offset 0x%x [in module %s].\n"),
16409 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16410 bfd_get_filename (objfile->obfd));
16411 }
9c541725 16412 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16413 objfile);
72bf9492 16414
0d99eb77
DE
16415 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16416 load_partial_comp_unit (per_cu);
ae038cb0 16417
0d99eb77 16418 per_cu->cu->last_used = 0;
9c541725 16419 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16420 }
5afb4e99 16421
dee91e82
DE
16422 /* If we didn't find it, and not all dies have been loaded,
16423 load them all and try again. */
16424
5afb4e99
DJ
16425 if (pd == NULL && per_cu->load_all_dies == 0)
16426 {
5afb4e99 16427 per_cu->load_all_dies = 1;
fd820528
DE
16428
16429 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16430 THIS_CU->cu may already be in use. So we can't just free it and
16431 replace its DIEs with the ones we read in. Instead, we leave those
16432 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16433 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16434 set. */
dee91e82 16435 load_partial_comp_unit (per_cu);
5afb4e99 16436
9c541725 16437 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16438 }
16439
16440 if (pd == NULL)
16441 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16442 _("could not find partial DIE 0x%x "
16443 "in cache [from module %s]\n"),
9c541725 16444 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16445 return pd;
72bf9492
DJ
16446}
16447
abc72ce4
DE
16448/* See if we can figure out if the class lives in a namespace. We do
16449 this by looking for a member function; its demangled name will
16450 contain namespace info, if there is any. */
16451
16452static void
16453guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16454 struct dwarf2_cu *cu)
16455{
16456 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16457 what template types look like, because the demangler
16458 frequently doesn't give the same name as the debug info. We
16459 could fix this by only using the demangled name to get the
16460 prefix (but see comment in read_structure_type). */
16461
16462 struct partial_die_info *real_pdi;
16463 struct partial_die_info *child_pdi;
16464
16465 /* If this DIE (this DIE's specification, if any) has a parent, then
16466 we should not do this. We'll prepend the parent's fully qualified
16467 name when we create the partial symbol. */
16468
16469 real_pdi = struct_pdi;
16470 while (real_pdi->has_specification)
36586728
TT
16471 real_pdi = find_partial_die (real_pdi->spec_offset,
16472 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16473
16474 if (real_pdi->die_parent != NULL)
16475 return;
16476
16477 for (child_pdi = struct_pdi->die_child;
16478 child_pdi != NULL;
16479 child_pdi = child_pdi->die_sibling)
16480 {
16481 if (child_pdi->tag == DW_TAG_subprogram
16482 && child_pdi->linkage_name != NULL)
16483 {
16484 char *actual_class_name
16485 = language_class_name_from_physname (cu->language_defn,
16486 child_pdi->linkage_name);
16487 if (actual_class_name != NULL)
16488 {
16489 struct_pdi->name
224c3ddb
SM
16490 = ((const char *)
16491 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16492 actual_class_name,
16493 strlen (actual_class_name)));
abc72ce4
DE
16494 xfree (actual_class_name);
16495 }
16496 break;
16497 }
16498 }
16499}
16500
72bf9492
DJ
16501/* Adjust PART_DIE before generating a symbol for it. This function
16502 may set the is_external flag or change the DIE's name. */
16503
16504static void
16505fixup_partial_die (struct partial_die_info *part_die,
16506 struct dwarf2_cu *cu)
16507{
abc72ce4
DE
16508 /* Once we've fixed up a die, there's no point in doing so again.
16509 This also avoids a memory leak if we were to call
16510 guess_partial_die_structure_name multiple times. */
16511 if (part_die->fixup_called)
16512 return;
16513
72bf9492
DJ
16514 /* If we found a reference attribute and the DIE has no name, try
16515 to find a name in the referred to DIE. */
16516
16517 if (part_die->name == NULL && part_die->has_specification)
16518 {
16519 struct partial_die_info *spec_die;
72bf9492 16520
36586728
TT
16521 spec_die = find_partial_die (part_die->spec_offset,
16522 part_die->spec_is_dwz, cu);
72bf9492 16523
10b3939b 16524 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16525
16526 if (spec_die->name)
16527 {
16528 part_die->name = spec_die->name;
16529
16530 /* Copy DW_AT_external attribute if it is set. */
16531 if (spec_die->is_external)
16532 part_die->is_external = spec_die->is_external;
16533 }
16534 }
16535
16536 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16537
16538 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16539 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16540
abc72ce4
DE
16541 /* If there is no parent die to provide a namespace, and there are
16542 children, see if we can determine the namespace from their linkage
122d1940 16543 name. */
abc72ce4 16544 if (cu->language == language_cplus
8b70b953 16545 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16546 && part_die->die_parent == NULL
16547 && part_die->has_children
16548 && (part_die->tag == DW_TAG_class_type
16549 || part_die->tag == DW_TAG_structure_type
16550 || part_die->tag == DW_TAG_union_type))
16551 guess_partial_die_structure_name (part_die, cu);
16552
53832f31
TT
16553 /* GCC might emit a nameless struct or union that has a linkage
16554 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16555 if (part_die->name == NULL
96408a79
SA
16556 && (part_die->tag == DW_TAG_class_type
16557 || part_die->tag == DW_TAG_interface_type
16558 || part_die->tag == DW_TAG_structure_type
16559 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16560 && part_die->linkage_name != NULL)
16561 {
16562 char *demangled;
16563
8de20a37 16564 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16565 if (demangled)
16566 {
96408a79
SA
16567 const char *base;
16568
16569 /* Strip any leading namespaces/classes, keep only the base name.
16570 DW_AT_name for named DIEs does not contain the prefixes. */
16571 base = strrchr (demangled, ':');
16572 if (base && base > demangled && base[-1] == ':')
16573 base++;
16574 else
16575 base = demangled;
16576
34a68019 16577 part_die->name
224c3ddb
SM
16578 = ((const char *)
16579 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16580 base, strlen (base)));
53832f31
TT
16581 xfree (demangled);
16582 }
16583 }
16584
abc72ce4 16585 part_die->fixup_called = 1;
72bf9492
DJ
16586}
16587
a8329558 16588/* Read an attribute value described by an attribute form. */
c906108c 16589
d521ce57 16590static const gdb_byte *
dee91e82
DE
16591read_attribute_value (const struct die_reader_specs *reader,
16592 struct attribute *attr, unsigned form,
43988095 16593 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16594{
dee91e82 16595 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16596 struct objfile *objfile = cu->objfile;
16597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16598 bfd *abfd = reader->abfd;
e7c27a73 16599 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16600 unsigned int bytes_read;
16601 struct dwarf_block *blk;
16602
aead7601 16603 attr->form = (enum dwarf_form) form;
a8329558 16604 switch (form)
c906108c 16605 {
c906108c 16606 case DW_FORM_ref_addr:
ae411497 16607 if (cu->header.version == 2)
4568ecf9 16608 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16609 else
4568ecf9
DE
16610 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16611 &cu->header, &bytes_read);
ae411497
TT
16612 info_ptr += bytes_read;
16613 break;
36586728
TT
16614 case DW_FORM_GNU_ref_alt:
16615 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16616 info_ptr += bytes_read;
16617 break;
ae411497 16618 case DW_FORM_addr:
e7c27a73 16619 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16620 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16621 info_ptr += bytes_read;
c906108c
SS
16622 break;
16623 case DW_FORM_block2:
7b5a2f43 16624 blk = dwarf_alloc_block (cu);
c906108c
SS
16625 blk->size = read_2_bytes (abfd, info_ptr);
16626 info_ptr += 2;
16627 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16628 info_ptr += blk->size;
16629 DW_BLOCK (attr) = blk;
16630 break;
16631 case DW_FORM_block4:
7b5a2f43 16632 blk = dwarf_alloc_block (cu);
c906108c
SS
16633 blk->size = read_4_bytes (abfd, info_ptr);
16634 info_ptr += 4;
16635 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16636 info_ptr += blk->size;
16637 DW_BLOCK (attr) = blk;
16638 break;
16639 case DW_FORM_data2:
16640 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16641 info_ptr += 2;
16642 break;
16643 case DW_FORM_data4:
16644 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16645 info_ptr += 4;
16646 break;
16647 case DW_FORM_data8:
16648 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16649 info_ptr += 8;
16650 break;
0224619f
JK
16651 case DW_FORM_data16:
16652 blk = dwarf_alloc_block (cu);
16653 blk->size = 16;
16654 blk->data = read_n_bytes (abfd, info_ptr, 16);
16655 info_ptr += 16;
16656 DW_BLOCK (attr) = blk;
16657 break;
2dc7f7b3
TT
16658 case DW_FORM_sec_offset:
16659 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16660 info_ptr += bytes_read;
16661 break;
c906108c 16662 case DW_FORM_string:
9b1c24c8 16663 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16664 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16665 info_ptr += bytes_read;
16666 break;
4bdf3d34 16667 case DW_FORM_strp:
36586728
TT
16668 if (!cu->per_cu->is_dwz)
16669 {
16670 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16671 &bytes_read);
16672 DW_STRING_IS_CANONICAL (attr) = 0;
16673 info_ptr += bytes_read;
16674 break;
16675 }
16676 /* FALLTHROUGH */
43988095
JK
16677 case DW_FORM_line_strp:
16678 if (!cu->per_cu->is_dwz)
16679 {
16680 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16681 cu_header, &bytes_read);
16682 DW_STRING_IS_CANONICAL (attr) = 0;
16683 info_ptr += bytes_read;
16684 break;
16685 }
16686 /* FALLTHROUGH */
36586728
TT
16687 case DW_FORM_GNU_strp_alt:
16688 {
16689 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16690 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16691 &bytes_read);
16692
16693 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16694 DW_STRING_IS_CANONICAL (attr) = 0;
16695 info_ptr += bytes_read;
16696 }
4bdf3d34 16697 break;
2dc7f7b3 16698 case DW_FORM_exprloc:
c906108c 16699 case DW_FORM_block:
7b5a2f43 16700 blk = dwarf_alloc_block (cu);
c906108c
SS
16701 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16702 info_ptr += bytes_read;
16703 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16704 info_ptr += blk->size;
16705 DW_BLOCK (attr) = blk;
16706 break;
16707 case DW_FORM_block1:
7b5a2f43 16708 blk = dwarf_alloc_block (cu);
c906108c
SS
16709 blk->size = read_1_byte (abfd, info_ptr);
16710 info_ptr += 1;
16711 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16712 info_ptr += blk->size;
16713 DW_BLOCK (attr) = blk;
16714 break;
16715 case DW_FORM_data1:
16716 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16717 info_ptr += 1;
16718 break;
16719 case DW_FORM_flag:
16720 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16721 info_ptr += 1;
16722 break;
2dc7f7b3
TT
16723 case DW_FORM_flag_present:
16724 DW_UNSND (attr) = 1;
16725 break;
c906108c
SS
16726 case DW_FORM_sdata:
16727 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16728 info_ptr += bytes_read;
16729 break;
16730 case DW_FORM_udata:
16731 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16732 info_ptr += bytes_read;
16733 break;
16734 case DW_FORM_ref1:
9c541725 16735 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16736 + read_1_byte (abfd, info_ptr));
c906108c
SS
16737 info_ptr += 1;
16738 break;
16739 case DW_FORM_ref2:
9c541725 16740 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16741 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16742 info_ptr += 2;
16743 break;
16744 case DW_FORM_ref4:
9c541725 16745 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16746 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16747 info_ptr += 4;
16748 break;
613e1657 16749 case DW_FORM_ref8:
9c541725 16750 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16751 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16752 info_ptr += 8;
16753 break;
55f1336d 16754 case DW_FORM_ref_sig8:
ac9ec31b 16755 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16756 info_ptr += 8;
16757 break;
c906108c 16758 case DW_FORM_ref_udata:
9c541725 16759 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16760 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16761 info_ptr += bytes_read;
16762 break;
c906108c 16763 case DW_FORM_indirect:
a8329558
KW
16764 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16765 info_ptr += bytes_read;
43988095
JK
16766 if (form == DW_FORM_implicit_const)
16767 {
16768 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16769 info_ptr += bytes_read;
16770 }
16771 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16772 info_ptr);
16773 break;
16774 case DW_FORM_implicit_const:
16775 DW_SND (attr) = implicit_const;
a8329558 16776 break;
3019eac3
DE
16777 case DW_FORM_GNU_addr_index:
16778 if (reader->dwo_file == NULL)
16779 {
16780 /* For now flag a hard error.
16781 Later we can turn this into a complaint. */
16782 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16783 dwarf_form_name (form),
16784 bfd_get_filename (abfd));
16785 }
16786 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16787 info_ptr += bytes_read;
16788 break;
16789 case DW_FORM_GNU_str_index:
16790 if (reader->dwo_file == NULL)
16791 {
16792 /* For now flag a hard error.
16793 Later we can turn this into a complaint if warranted. */
16794 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16795 dwarf_form_name (form),
16796 bfd_get_filename (abfd));
16797 }
16798 {
16799 ULONGEST str_index =
16800 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16801
342587c4 16802 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16803 DW_STRING_IS_CANONICAL (attr) = 0;
16804 info_ptr += bytes_read;
16805 }
16806 break;
c906108c 16807 default:
8a3fe4f8 16808 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16809 dwarf_form_name (form),
16810 bfd_get_filename (abfd));
c906108c 16811 }
28e94949 16812
36586728 16813 /* Super hack. */
7771576e 16814 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16815 attr->form = DW_FORM_GNU_ref_alt;
16816
28e94949
JB
16817 /* We have seen instances where the compiler tried to emit a byte
16818 size attribute of -1 which ended up being encoded as an unsigned
16819 0xffffffff. Although 0xffffffff is technically a valid size value,
16820 an object of this size seems pretty unlikely so we can relatively
16821 safely treat these cases as if the size attribute was invalid and
16822 treat them as zero by default. */
16823 if (attr->name == DW_AT_byte_size
16824 && form == DW_FORM_data4
16825 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16826 {
16827 complaint
16828 (&symfile_complaints,
43bbcdc2
PH
16829 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16830 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16831 DW_UNSND (attr) = 0;
16832 }
28e94949 16833
c906108c
SS
16834 return info_ptr;
16835}
16836
a8329558
KW
16837/* Read an attribute described by an abbreviated attribute. */
16838
d521ce57 16839static const gdb_byte *
dee91e82
DE
16840read_attribute (const struct die_reader_specs *reader,
16841 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16842 const gdb_byte *info_ptr)
a8329558
KW
16843{
16844 attr->name = abbrev->name;
43988095
JK
16845 return read_attribute_value (reader, attr, abbrev->form,
16846 abbrev->implicit_const, info_ptr);
a8329558
KW
16847}
16848
0963b4bd 16849/* Read dwarf information from a buffer. */
c906108c
SS
16850
16851static unsigned int
a1855c1d 16852read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16853{
fe1b8b76 16854 return bfd_get_8 (abfd, buf);
c906108c
SS
16855}
16856
16857static int
a1855c1d 16858read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16859{
fe1b8b76 16860 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16861}
16862
16863static unsigned int
a1855c1d 16864read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16865{
fe1b8b76 16866 return bfd_get_16 (abfd, buf);
c906108c
SS
16867}
16868
21ae7a4d 16869static int
a1855c1d 16870read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16871{
16872 return bfd_get_signed_16 (abfd, buf);
16873}
16874
c906108c 16875static unsigned int
a1855c1d 16876read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16877{
fe1b8b76 16878 return bfd_get_32 (abfd, buf);
c906108c
SS
16879}
16880
21ae7a4d 16881static int
a1855c1d 16882read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16883{
16884 return bfd_get_signed_32 (abfd, buf);
16885}
16886
93311388 16887static ULONGEST
a1855c1d 16888read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16889{
fe1b8b76 16890 return bfd_get_64 (abfd, buf);
c906108c
SS
16891}
16892
16893static CORE_ADDR
d521ce57 16894read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16895 unsigned int *bytes_read)
c906108c 16896{
e7c27a73 16897 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16898 CORE_ADDR retval = 0;
16899
107d2387 16900 if (cu_header->signed_addr_p)
c906108c 16901 {
107d2387
AC
16902 switch (cu_header->addr_size)
16903 {
16904 case 2:
fe1b8b76 16905 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16906 break;
16907 case 4:
fe1b8b76 16908 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16909 break;
16910 case 8:
fe1b8b76 16911 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16912 break;
16913 default:
8e65ff28 16914 internal_error (__FILE__, __LINE__,
e2e0b3e5 16915 _("read_address: bad switch, signed [in module %s]"),
659b0389 16916 bfd_get_filename (abfd));
107d2387
AC
16917 }
16918 }
16919 else
16920 {
16921 switch (cu_header->addr_size)
16922 {
16923 case 2:
fe1b8b76 16924 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16925 break;
16926 case 4:
fe1b8b76 16927 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16928 break;
16929 case 8:
fe1b8b76 16930 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16931 break;
16932 default:
8e65ff28 16933 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16934 _("read_address: bad switch, "
16935 "unsigned [in module %s]"),
659b0389 16936 bfd_get_filename (abfd));
107d2387 16937 }
c906108c 16938 }
64367e0a 16939
107d2387
AC
16940 *bytes_read = cu_header->addr_size;
16941 return retval;
c906108c
SS
16942}
16943
f7ef9339 16944/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16945 specification allows the initial length to take up either 4 bytes
16946 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16947 bytes describe the length and all offsets will be 8 bytes in length
16948 instead of 4.
16949
f7ef9339
KB
16950 An older, non-standard 64-bit format is also handled by this
16951 function. The older format in question stores the initial length
16952 as an 8-byte quantity without an escape value. Lengths greater
16953 than 2^32 aren't very common which means that the initial 4 bytes
16954 is almost always zero. Since a length value of zero doesn't make
16955 sense for the 32-bit format, this initial zero can be considered to
16956 be an escape value which indicates the presence of the older 64-bit
16957 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16958 greater than 4GB. If it becomes necessary to handle lengths
16959 somewhat larger than 4GB, we could allow other small values (such
16960 as the non-sensical values of 1, 2, and 3) to also be used as
16961 escape values indicating the presence of the old format.
f7ef9339 16962
917c78fc
MK
16963 The value returned via bytes_read should be used to increment the
16964 relevant pointer after calling read_initial_length().
c764a876 16965
613e1657
KB
16966 [ Note: read_initial_length() and read_offset() are based on the
16967 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16968 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16969 from:
16970
f7ef9339 16971 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16972
613e1657
KB
16973 This document is only a draft and is subject to change. (So beware.)
16974
f7ef9339 16975 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16976 determined empirically by examining 64-bit ELF files produced by
16977 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16978
16979 - Kevin, July 16, 2002
613e1657
KB
16980 ] */
16981
16982static LONGEST
d521ce57 16983read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16984{
fe1b8b76 16985 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16986
dd373385 16987 if (length == 0xffffffff)
613e1657 16988 {
fe1b8b76 16989 length = bfd_get_64 (abfd, buf + 4);
613e1657 16990 *bytes_read = 12;
613e1657 16991 }
dd373385 16992 else if (length == 0)
f7ef9339 16993 {
dd373385 16994 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16995 length = bfd_get_64 (abfd, buf);
f7ef9339 16996 *bytes_read = 8;
f7ef9339 16997 }
613e1657
KB
16998 else
16999 {
17000 *bytes_read = 4;
613e1657
KB
17001 }
17002
c764a876
DE
17003 return length;
17004}
dd373385 17005
c764a876
DE
17006/* Cover function for read_initial_length.
17007 Returns the length of the object at BUF, and stores the size of the
17008 initial length in *BYTES_READ and stores the size that offsets will be in
17009 *OFFSET_SIZE.
17010 If the initial length size is not equivalent to that specified in
17011 CU_HEADER then issue a complaint.
17012 This is useful when reading non-comp-unit headers. */
dd373385 17013
c764a876 17014static LONGEST
d521ce57 17015read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17016 const struct comp_unit_head *cu_header,
17017 unsigned int *bytes_read,
17018 unsigned int *offset_size)
17019{
17020 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17021
17022 gdb_assert (cu_header->initial_length_size == 4
17023 || cu_header->initial_length_size == 8
17024 || cu_header->initial_length_size == 12);
17025
17026 if (cu_header->initial_length_size != *bytes_read)
17027 complaint (&symfile_complaints,
17028 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17029
c764a876 17030 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17031 return length;
613e1657
KB
17032}
17033
17034/* Read an offset from the data stream. The size of the offset is
917c78fc 17035 given by cu_header->offset_size. */
613e1657
KB
17036
17037static LONGEST
d521ce57
TT
17038read_offset (bfd *abfd, const gdb_byte *buf,
17039 const struct comp_unit_head *cu_header,
891d2f0b 17040 unsigned int *bytes_read)
c764a876
DE
17041{
17042 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17043
c764a876
DE
17044 *bytes_read = cu_header->offset_size;
17045 return offset;
17046}
17047
17048/* Read an offset from the data stream. */
17049
17050static LONGEST
d521ce57 17051read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17052{
17053 LONGEST retval = 0;
17054
c764a876 17055 switch (offset_size)
613e1657
KB
17056 {
17057 case 4:
fe1b8b76 17058 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17059 break;
17060 case 8:
fe1b8b76 17061 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17062 break;
17063 default:
8e65ff28 17064 internal_error (__FILE__, __LINE__,
c764a876 17065 _("read_offset_1: bad switch [in module %s]"),
659b0389 17066 bfd_get_filename (abfd));
613e1657
KB
17067 }
17068
917c78fc 17069 return retval;
613e1657
KB
17070}
17071
d521ce57
TT
17072static const gdb_byte *
17073read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17074{
17075 /* If the size of a host char is 8 bits, we can return a pointer
17076 to the buffer, otherwise we have to copy the data to a buffer
17077 allocated on the temporary obstack. */
4bdf3d34 17078 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17079 return buf;
c906108c
SS
17080}
17081
d521ce57
TT
17082static const char *
17083read_direct_string (bfd *abfd, const gdb_byte *buf,
17084 unsigned int *bytes_read_ptr)
c906108c
SS
17085{
17086 /* If the size of a host char is 8 bits, we can return a pointer
17087 to the string, otherwise we have to copy the string to a buffer
17088 allocated on the temporary obstack. */
4bdf3d34 17089 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17090 if (*buf == '\0')
17091 {
17092 *bytes_read_ptr = 1;
17093 return NULL;
17094 }
d521ce57
TT
17095 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17096 return (const char *) buf;
4bdf3d34
JJ
17097}
17098
43988095
JK
17099/* Return pointer to string at section SECT offset STR_OFFSET with error
17100 reporting strings FORM_NAME and SECT_NAME. */
17101
d521ce57 17102static const char *
43988095
JK
17103read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17104 struct dwarf2_section_info *sect,
17105 const char *form_name,
17106 const char *sect_name)
17107{
17108 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17109 if (sect->buffer == NULL)
17110 error (_("%s used without %s section [in module %s]"),
17111 form_name, sect_name, bfd_get_filename (abfd));
17112 if (str_offset >= sect->size)
17113 error (_("%s pointing outside of %s section [in module %s]"),
17114 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17115 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17116 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17117 return NULL;
43988095
JK
17118 return (const char *) (sect->buffer + str_offset);
17119}
17120
17121/* Return pointer to string at .debug_str offset STR_OFFSET. */
17122
17123static const char *
17124read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17125{
17126 return read_indirect_string_at_offset_from (abfd, str_offset,
17127 &dwarf2_per_objfile->str,
17128 "DW_FORM_strp", ".debug_str");
17129}
17130
17131/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17132
17133static const char *
17134read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17135{
17136 return read_indirect_string_at_offset_from (abfd, str_offset,
17137 &dwarf2_per_objfile->line_str,
17138 "DW_FORM_line_strp",
17139 ".debug_line_str");
c906108c
SS
17140}
17141
36586728
TT
17142/* Read a string at offset STR_OFFSET in the .debug_str section from
17143 the .dwz file DWZ. Throw an error if the offset is too large. If
17144 the string consists of a single NUL byte, return NULL; otherwise
17145 return a pointer to the string. */
17146
d521ce57 17147static const char *
36586728
TT
17148read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17149{
17150 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17151
17152 if (dwz->str.buffer == NULL)
17153 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17154 "section [in module %s]"),
17155 bfd_get_filename (dwz->dwz_bfd));
17156 if (str_offset >= dwz->str.size)
17157 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17158 ".debug_str section [in module %s]"),
17159 bfd_get_filename (dwz->dwz_bfd));
17160 gdb_assert (HOST_CHAR_BIT == 8);
17161 if (dwz->str.buffer[str_offset] == '\0')
17162 return NULL;
d521ce57 17163 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17164}
17165
43988095
JK
17166/* Return pointer to string at .debug_str offset as read from BUF.
17167 BUF is assumed to be in a compilation unit described by CU_HEADER.
17168 Return *BYTES_READ_PTR count of bytes read from BUF. */
17169
d521ce57
TT
17170static const char *
17171read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17172 const struct comp_unit_head *cu_header,
17173 unsigned int *bytes_read_ptr)
17174{
17175 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17176
17177 return read_indirect_string_at_offset (abfd, str_offset);
17178}
17179
43988095
JK
17180/* Return pointer to string at .debug_line_str offset as read from BUF.
17181 BUF is assumed to be in a compilation unit described by CU_HEADER.
17182 Return *BYTES_READ_PTR count of bytes read from BUF. */
17183
17184static const char *
17185read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17186 const struct comp_unit_head *cu_header,
17187 unsigned int *bytes_read_ptr)
17188{
17189 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17190
17191 return read_indirect_line_string_at_offset (abfd, str_offset);
17192}
17193
17194ULONGEST
d521ce57 17195read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17196 unsigned int *bytes_read_ptr)
c906108c 17197{
12df843f 17198 ULONGEST result;
ce5d95e1 17199 unsigned int num_read;
870f88f7 17200 int shift;
c906108c
SS
17201 unsigned char byte;
17202
17203 result = 0;
17204 shift = 0;
17205 num_read = 0;
c906108c
SS
17206 while (1)
17207 {
fe1b8b76 17208 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17209 buf++;
17210 num_read++;
12df843f 17211 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17212 if ((byte & 128) == 0)
17213 {
17214 break;
17215 }
17216 shift += 7;
17217 }
17218 *bytes_read_ptr = num_read;
17219 return result;
17220}
17221
12df843f 17222static LONGEST
d521ce57
TT
17223read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17224 unsigned int *bytes_read_ptr)
c906108c 17225{
12df843f 17226 LONGEST result;
870f88f7 17227 int shift, num_read;
c906108c
SS
17228 unsigned char byte;
17229
17230 result = 0;
17231 shift = 0;
c906108c 17232 num_read = 0;
c906108c
SS
17233 while (1)
17234 {
fe1b8b76 17235 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17236 buf++;
17237 num_read++;
12df843f 17238 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17239 shift += 7;
17240 if ((byte & 128) == 0)
17241 {
17242 break;
17243 }
17244 }
77e0b926 17245 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17246 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17247 *bytes_read_ptr = num_read;
17248 return result;
17249}
17250
3019eac3
DE
17251/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17252 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17253 ADDR_SIZE is the size of addresses from the CU header. */
17254
17255static CORE_ADDR
17256read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17257{
17258 struct objfile *objfile = dwarf2_per_objfile->objfile;
17259 bfd *abfd = objfile->obfd;
17260 const gdb_byte *info_ptr;
17261
17262 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17263 if (dwarf2_per_objfile->addr.buffer == NULL)
17264 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17265 objfile_name (objfile));
3019eac3
DE
17266 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17267 error (_("DW_FORM_addr_index pointing outside of "
17268 ".debug_addr section [in module %s]"),
4262abfb 17269 objfile_name (objfile));
3019eac3
DE
17270 info_ptr = (dwarf2_per_objfile->addr.buffer
17271 + addr_base + addr_index * addr_size);
17272 if (addr_size == 4)
17273 return bfd_get_32 (abfd, info_ptr);
17274 else
17275 return bfd_get_64 (abfd, info_ptr);
17276}
17277
17278/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17279
17280static CORE_ADDR
17281read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17282{
17283 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17284}
17285
17286/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17287
17288static CORE_ADDR
d521ce57 17289read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17290 unsigned int *bytes_read)
17291{
17292 bfd *abfd = cu->objfile->obfd;
17293 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17294
17295 return read_addr_index (cu, addr_index);
17296}
17297
17298/* Data structure to pass results from dwarf2_read_addr_index_reader
17299 back to dwarf2_read_addr_index. */
17300
17301struct dwarf2_read_addr_index_data
17302{
17303 ULONGEST addr_base;
17304 int addr_size;
17305};
17306
17307/* die_reader_func for dwarf2_read_addr_index. */
17308
17309static void
17310dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17311 const gdb_byte *info_ptr,
3019eac3
DE
17312 struct die_info *comp_unit_die,
17313 int has_children,
17314 void *data)
17315{
17316 struct dwarf2_cu *cu = reader->cu;
17317 struct dwarf2_read_addr_index_data *aidata =
17318 (struct dwarf2_read_addr_index_data *) data;
17319
17320 aidata->addr_base = cu->addr_base;
17321 aidata->addr_size = cu->header.addr_size;
17322}
17323
17324/* Given an index in .debug_addr, fetch the value.
17325 NOTE: This can be called during dwarf expression evaluation,
17326 long after the debug information has been read, and thus per_cu->cu
17327 may no longer exist. */
17328
17329CORE_ADDR
17330dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17331 unsigned int addr_index)
17332{
17333 struct objfile *objfile = per_cu->objfile;
17334 struct dwarf2_cu *cu = per_cu->cu;
17335 ULONGEST addr_base;
17336 int addr_size;
17337
17338 /* This is intended to be called from outside this file. */
17339 dw2_setup (objfile);
17340
17341 /* We need addr_base and addr_size.
17342 If we don't have PER_CU->cu, we have to get it.
17343 Nasty, but the alternative is storing the needed info in PER_CU,
17344 which at this point doesn't seem justified: it's not clear how frequently
17345 it would get used and it would increase the size of every PER_CU.
17346 Entry points like dwarf2_per_cu_addr_size do a similar thing
17347 so we're not in uncharted territory here.
17348 Alas we need to be a bit more complicated as addr_base is contained
17349 in the DIE.
17350
17351 We don't need to read the entire CU(/TU).
17352 We just need the header and top level die.
a1b64ce1 17353
3019eac3 17354 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17355 For now we skip this optimization. */
3019eac3
DE
17356
17357 if (cu != NULL)
17358 {
17359 addr_base = cu->addr_base;
17360 addr_size = cu->header.addr_size;
17361 }
17362 else
17363 {
17364 struct dwarf2_read_addr_index_data aidata;
17365
a1b64ce1
DE
17366 /* Note: We can't use init_cutu_and_read_dies_simple here,
17367 we need addr_base. */
17368 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17369 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17370 addr_base = aidata.addr_base;
17371 addr_size = aidata.addr_size;
17372 }
17373
17374 return read_addr_index_1 (addr_index, addr_base, addr_size);
17375}
17376
57d63ce2
DE
17377/* Given a DW_FORM_GNU_str_index, fetch the string.
17378 This is only used by the Fission support. */
3019eac3 17379
d521ce57 17380static const char *
342587c4 17381read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17382{
17383 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17384 const char *objf_name = objfile_name (objfile);
3019eac3 17385 bfd *abfd = objfile->obfd;
342587c4 17386 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17387 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17388 struct dwarf2_section_info *str_offsets_section =
17389 &reader->dwo_file->sections.str_offsets;
d521ce57 17390 const gdb_byte *info_ptr;
3019eac3 17391 ULONGEST str_offset;
57d63ce2 17392 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17393
73869dc2
DE
17394 dwarf2_read_section (objfile, str_section);
17395 dwarf2_read_section (objfile, str_offsets_section);
17396 if (str_section->buffer == NULL)
57d63ce2 17397 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17398 " in CU at offset 0x%x [in module %s]"),
17399 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17400 if (str_offsets_section->buffer == NULL)
57d63ce2 17401 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17402 " in CU at offset 0x%x [in module %s]"),
17403 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17404 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17405 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17406 " section in CU at offset 0x%x [in module %s]"),
17407 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17408 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17409 + str_index * cu->header.offset_size);
17410 if (cu->header.offset_size == 4)
17411 str_offset = bfd_get_32 (abfd, info_ptr);
17412 else
17413 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17414 if (str_offset >= str_section->size)
57d63ce2 17415 error (_("Offset from %s pointing outside of"
9c541725
PA
17416 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17417 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17418 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17419}
17420
3019eac3
DE
17421/* Return the length of an LEB128 number in BUF. */
17422
17423static int
17424leb128_size (const gdb_byte *buf)
17425{
17426 const gdb_byte *begin = buf;
17427 gdb_byte byte;
17428
17429 while (1)
17430 {
17431 byte = *buf++;
17432 if ((byte & 128) == 0)
17433 return buf - begin;
17434 }
17435}
17436
c906108c 17437static void
e142c38c 17438set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17439{
17440 switch (lang)
17441 {
17442 case DW_LANG_C89:
76bee0cc 17443 case DW_LANG_C99:
0cfd832f 17444 case DW_LANG_C11:
c906108c 17445 case DW_LANG_C:
d1be3247 17446 case DW_LANG_UPC:
e142c38c 17447 cu->language = language_c;
c906108c 17448 break;
9c37b5ae 17449 case DW_LANG_Java:
c906108c 17450 case DW_LANG_C_plus_plus:
0cfd832f
MW
17451 case DW_LANG_C_plus_plus_11:
17452 case DW_LANG_C_plus_plus_14:
e142c38c 17453 cu->language = language_cplus;
c906108c 17454 break;
6aecb9c2
JB
17455 case DW_LANG_D:
17456 cu->language = language_d;
17457 break;
c906108c
SS
17458 case DW_LANG_Fortran77:
17459 case DW_LANG_Fortran90:
b21b22e0 17460 case DW_LANG_Fortran95:
f7de9aab
MW
17461 case DW_LANG_Fortran03:
17462 case DW_LANG_Fortran08:
e142c38c 17463 cu->language = language_fortran;
c906108c 17464 break;
a766d390
DE
17465 case DW_LANG_Go:
17466 cu->language = language_go;
17467 break;
c906108c 17468 case DW_LANG_Mips_Assembler:
e142c38c 17469 cu->language = language_asm;
c906108c
SS
17470 break;
17471 case DW_LANG_Ada83:
8aaf0b47 17472 case DW_LANG_Ada95:
bc5f45f8
JB
17473 cu->language = language_ada;
17474 break;
72019c9c
GM
17475 case DW_LANG_Modula2:
17476 cu->language = language_m2;
17477 break;
fe8e67fd
PM
17478 case DW_LANG_Pascal83:
17479 cu->language = language_pascal;
17480 break;
22566fbd
DJ
17481 case DW_LANG_ObjC:
17482 cu->language = language_objc;
17483 break;
c44af4eb
TT
17484 case DW_LANG_Rust:
17485 case DW_LANG_Rust_old:
17486 cu->language = language_rust;
17487 break;
c906108c
SS
17488 case DW_LANG_Cobol74:
17489 case DW_LANG_Cobol85:
c906108c 17490 default:
e142c38c 17491 cu->language = language_minimal;
c906108c
SS
17492 break;
17493 }
e142c38c 17494 cu->language_defn = language_def (cu->language);
c906108c
SS
17495}
17496
17497/* Return the named attribute or NULL if not there. */
17498
17499static struct attribute *
e142c38c 17500dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17501{
a48e046c 17502 for (;;)
c906108c 17503 {
a48e046c
TT
17504 unsigned int i;
17505 struct attribute *spec = NULL;
17506
17507 for (i = 0; i < die->num_attrs; ++i)
17508 {
17509 if (die->attrs[i].name == name)
17510 return &die->attrs[i];
17511 if (die->attrs[i].name == DW_AT_specification
17512 || die->attrs[i].name == DW_AT_abstract_origin)
17513 spec = &die->attrs[i];
17514 }
17515
17516 if (!spec)
17517 break;
c906108c 17518
f2f0e013 17519 die = follow_die_ref (die, spec, &cu);
f2f0e013 17520 }
c5aa993b 17521
c906108c
SS
17522 return NULL;
17523}
17524
348e048f
DE
17525/* Return the named attribute or NULL if not there,
17526 but do not follow DW_AT_specification, etc.
17527 This is for use in contexts where we're reading .debug_types dies.
17528 Following DW_AT_specification, DW_AT_abstract_origin will take us
17529 back up the chain, and we want to go down. */
17530
17531static struct attribute *
45e58e77 17532dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17533{
17534 unsigned int i;
17535
17536 for (i = 0; i < die->num_attrs; ++i)
17537 if (die->attrs[i].name == name)
17538 return &die->attrs[i];
17539
17540 return NULL;
17541}
17542
7d45c7c3
KB
17543/* Return the string associated with a string-typed attribute, or NULL if it
17544 is either not found or is of an incorrect type. */
17545
17546static const char *
17547dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17548{
17549 struct attribute *attr;
17550 const char *str = NULL;
17551
17552 attr = dwarf2_attr (die, name, cu);
17553
17554 if (attr != NULL)
17555 {
43988095
JK
17556 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17557 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17558 str = DW_STRING (attr);
17559 else
17560 complaint (&symfile_complaints,
17561 _("string type expected for attribute %s for "
17562 "DIE at 0x%x in module %s"),
9c541725 17563 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17564 objfile_name (cu->objfile));
17565 }
17566
17567 return str;
17568}
17569
05cf31d1
JB
17570/* Return non-zero iff the attribute NAME is defined for the given DIE,
17571 and holds a non-zero value. This function should only be used for
2dc7f7b3 17572 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17573
17574static int
17575dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17576{
17577 struct attribute *attr = dwarf2_attr (die, name, cu);
17578
17579 return (attr && DW_UNSND (attr));
17580}
17581
3ca72b44 17582static int
e142c38c 17583die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17584{
05cf31d1
JB
17585 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17586 which value is non-zero. However, we have to be careful with
17587 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17588 (via dwarf2_flag_true_p) follows this attribute. So we may
17589 end up accidently finding a declaration attribute that belongs
17590 to a different DIE referenced by the specification attribute,
17591 even though the given DIE does not have a declaration attribute. */
17592 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17593 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17594}
17595
63d06c5c 17596/* Return the die giving the specification for DIE, if there is
f2f0e013 17597 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17598 containing the return value on output. If there is no
17599 specification, but there is an abstract origin, that is
17600 returned. */
63d06c5c
DC
17601
17602static struct die_info *
f2f0e013 17603die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17604{
f2f0e013
DJ
17605 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17606 *spec_cu);
63d06c5c 17607
edb3359d
DJ
17608 if (spec_attr == NULL)
17609 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17610
63d06c5c
DC
17611 if (spec_attr == NULL)
17612 return NULL;
17613 else
f2f0e013 17614 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17615}
c906108c 17616
527f3840
JK
17617/* Stub for free_line_header to match void * callback types. */
17618
17619static void
17620free_line_header_voidp (void *arg)
17621{
9a3c8263 17622 struct line_header *lh = (struct line_header *) arg;
527f3840 17623
fff8551c 17624 delete lh;
527f3840
JK
17625}
17626
fff8551c
PA
17627void
17628line_header::add_include_dir (const char *include_dir)
c906108c 17629{
27e0867f 17630 if (dwarf_line_debug >= 2)
fff8551c
PA
17631 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17632 include_dirs.size () + 1, include_dir);
27e0867f 17633
fff8551c 17634 include_dirs.push_back (include_dir);
debd256d 17635}
6e70227d 17636
fff8551c
PA
17637void
17638line_header::add_file_name (const char *name,
ecfb656c 17639 dir_index d_index,
fff8551c
PA
17640 unsigned int mod_time,
17641 unsigned int length)
debd256d 17642{
27e0867f
DE
17643 if (dwarf_line_debug >= 2)
17644 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17645 (unsigned) file_names.size () + 1, name);
27e0867f 17646
ecfb656c 17647 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17648}
6e70227d 17649
83769d0b 17650/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17651
17652static struct dwarf2_section_info *
17653get_debug_line_section (struct dwarf2_cu *cu)
17654{
17655 struct dwarf2_section_info *section;
17656
17657 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17658 DWO file. */
17659 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17660 section = &cu->dwo_unit->dwo_file->sections.line;
17661 else if (cu->per_cu->is_dwz)
17662 {
17663 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17664
17665 section = &dwz->line;
17666 }
17667 else
17668 section = &dwarf2_per_objfile->line;
17669
17670 return section;
17671}
17672
43988095
JK
17673/* Read directory or file name entry format, starting with byte of
17674 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17675 entries count and the entries themselves in the described entry
17676 format. */
17677
17678static void
17679read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17680 struct line_header *lh,
17681 const struct comp_unit_head *cu_header,
17682 void (*callback) (struct line_header *lh,
17683 const char *name,
ecfb656c 17684 dir_index d_index,
43988095
JK
17685 unsigned int mod_time,
17686 unsigned int length))
17687{
17688 gdb_byte format_count, formati;
17689 ULONGEST data_count, datai;
17690 const gdb_byte *buf = *bufp;
17691 const gdb_byte *format_header_data;
17692 int i;
17693 unsigned int bytes_read;
17694
17695 format_count = read_1_byte (abfd, buf);
17696 buf += 1;
17697 format_header_data = buf;
17698 for (formati = 0; formati < format_count; formati++)
17699 {
17700 read_unsigned_leb128 (abfd, buf, &bytes_read);
17701 buf += bytes_read;
17702 read_unsigned_leb128 (abfd, buf, &bytes_read);
17703 buf += bytes_read;
17704 }
17705
17706 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17707 buf += bytes_read;
17708 for (datai = 0; datai < data_count; datai++)
17709 {
17710 const gdb_byte *format = format_header_data;
17711 struct file_entry fe;
17712
43988095
JK
17713 for (formati = 0; formati < format_count; formati++)
17714 {
ecfb656c 17715 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17716 format += bytes_read;
43988095 17717
ecfb656c 17718 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17719 format += bytes_read;
ecfb656c
PA
17720
17721 gdb::optional<const char *> string;
17722 gdb::optional<unsigned int> uint;
17723
43988095
JK
17724 switch (form)
17725 {
17726 case DW_FORM_string:
ecfb656c 17727 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17728 buf += bytes_read;
17729 break;
17730
17731 case DW_FORM_line_strp:
ecfb656c
PA
17732 string.emplace (read_indirect_line_string (abfd, buf,
17733 cu_header,
17734 &bytes_read));
43988095
JK
17735 buf += bytes_read;
17736 break;
17737
17738 case DW_FORM_data1:
ecfb656c 17739 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17740 buf += 1;
17741 break;
17742
17743 case DW_FORM_data2:
ecfb656c 17744 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17745 buf += 2;
17746 break;
17747
17748 case DW_FORM_data4:
ecfb656c 17749 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17750 buf += 4;
17751 break;
17752
17753 case DW_FORM_data8:
ecfb656c 17754 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17755 buf += 8;
17756 break;
17757
17758 case DW_FORM_udata:
ecfb656c 17759 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17760 buf += bytes_read;
17761 break;
17762
17763 case DW_FORM_block:
17764 /* It is valid only for DW_LNCT_timestamp which is ignored by
17765 current GDB. */
17766 break;
17767 }
ecfb656c
PA
17768
17769 switch (content_type)
17770 {
17771 case DW_LNCT_path:
17772 if (string.has_value ())
17773 fe.name = *string;
17774 break;
17775 case DW_LNCT_directory_index:
17776 if (uint.has_value ())
17777 fe.d_index = (dir_index) *uint;
17778 break;
17779 case DW_LNCT_timestamp:
17780 if (uint.has_value ())
17781 fe.mod_time = *uint;
17782 break;
17783 case DW_LNCT_size:
17784 if (uint.has_value ())
17785 fe.length = *uint;
17786 break;
17787 case DW_LNCT_MD5:
17788 break;
17789 default:
17790 complaint (&symfile_complaints,
17791 _("Unknown format content type %s"),
17792 pulongest (content_type));
17793 }
43988095
JK
17794 }
17795
ecfb656c 17796 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17797 }
17798
17799 *bufp = buf;
17800}
17801
debd256d 17802/* Read the statement program header starting at OFFSET in
3019eac3 17803 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17804 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17805 Returns NULL if there is a problem reading the header, e.g., if it
17806 has a version we don't understand.
debd256d
JB
17807
17808 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17809 the returned object point into the dwarf line section buffer,
17810 and must not be freed. */
ae2de4f8 17811
fff8551c 17812static line_header_up
9c541725 17813dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17814{
d521ce57 17815 const gdb_byte *line_ptr;
c764a876 17816 unsigned int bytes_read, offset_size;
debd256d 17817 int i;
d521ce57 17818 const char *cur_dir, *cur_file;
3019eac3
DE
17819 struct dwarf2_section_info *section;
17820 bfd *abfd;
17821
36586728 17822 section = get_debug_line_section (cu);
3019eac3
DE
17823 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17824 if (section->buffer == NULL)
debd256d 17825 {
3019eac3
DE
17826 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17827 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17828 else
17829 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17830 return 0;
17831 }
17832
fceca515
DE
17833 /* We can't do this until we know the section is non-empty.
17834 Only then do we know we have such a section. */
a32a8923 17835 abfd = get_section_bfd_owner (section);
fceca515 17836
a738430d
MK
17837 /* Make sure that at least there's room for the total_length field.
17838 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17839 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17840 {
4d3c2250 17841 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17842 return 0;
17843 }
17844
fff8551c 17845 line_header_up lh (new line_header ());
debd256d 17846
9c541725 17847 lh->sect_off = sect_off;
527f3840
JK
17848 lh->offset_in_dwz = cu->per_cu->is_dwz;
17849
9c541725 17850 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 17851
a738430d 17852 /* Read in the header. */
6e70227d 17853 lh->total_length =
c764a876
DE
17854 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17855 &bytes_read, &offset_size);
debd256d 17856 line_ptr += bytes_read;
3019eac3 17857 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17858 {
4d3c2250 17859 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17860 return 0;
17861 }
17862 lh->statement_program_end = line_ptr + lh->total_length;
17863 lh->version = read_2_bytes (abfd, line_ptr);
17864 line_ptr += 2;
43988095 17865 if (lh->version > 5)
cd366ee8
DE
17866 {
17867 /* This is a version we don't understand. The format could have
17868 changed in ways we don't handle properly so just punt. */
17869 complaint (&symfile_complaints,
17870 _("unsupported version in .debug_line section"));
17871 return NULL;
17872 }
43988095
JK
17873 if (lh->version >= 5)
17874 {
17875 gdb_byte segment_selector_size;
17876
17877 /* Skip address size. */
17878 read_1_byte (abfd, line_ptr);
17879 line_ptr += 1;
17880
17881 segment_selector_size = read_1_byte (abfd, line_ptr);
17882 line_ptr += 1;
17883 if (segment_selector_size != 0)
17884 {
17885 complaint (&symfile_complaints,
17886 _("unsupported segment selector size %u "
17887 "in .debug_line section"),
17888 segment_selector_size);
17889 return NULL;
17890 }
17891 }
c764a876
DE
17892 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17893 line_ptr += offset_size;
debd256d
JB
17894 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17895 line_ptr += 1;
2dc7f7b3
TT
17896 if (lh->version >= 4)
17897 {
17898 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17899 line_ptr += 1;
17900 }
17901 else
17902 lh->maximum_ops_per_instruction = 1;
17903
17904 if (lh->maximum_ops_per_instruction == 0)
17905 {
17906 lh->maximum_ops_per_instruction = 1;
17907 complaint (&symfile_complaints,
3e43a32a
MS
17908 _("invalid maximum_ops_per_instruction "
17909 "in `.debug_line' section"));
2dc7f7b3
TT
17910 }
17911
debd256d
JB
17912 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17913 line_ptr += 1;
17914 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17915 line_ptr += 1;
17916 lh->line_range = read_1_byte (abfd, line_ptr);
17917 line_ptr += 1;
17918 lh->opcode_base = read_1_byte (abfd, line_ptr);
17919 line_ptr += 1;
fff8551c 17920 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
17921
17922 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17923 for (i = 1; i < lh->opcode_base; ++i)
17924 {
17925 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17926 line_ptr += 1;
17927 }
17928
43988095 17929 if (lh->version >= 5)
debd256d 17930 {
43988095 17931 /* Read directory table. */
fff8551c
PA
17932 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17933 [] (struct line_header *lh, const char *name,
ecfb656c 17934 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17935 unsigned int length)
17936 {
17937 lh->add_include_dir (name);
17938 });
debd256d 17939
43988095 17940 /* Read file name table. */
fff8551c
PA
17941 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17942 [] (struct line_header *lh, const char *name,
ecfb656c 17943 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17944 unsigned int length)
17945 {
ecfb656c 17946 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 17947 });
43988095
JK
17948 }
17949 else
debd256d 17950 {
43988095
JK
17951 /* Read directory table. */
17952 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17953 {
17954 line_ptr += bytes_read;
fff8551c 17955 lh->add_include_dir (cur_dir);
43988095 17956 }
debd256d
JB
17957 line_ptr += bytes_read;
17958
43988095
JK
17959 /* Read file name table. */
17960 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17961 {
ecfb656c
PA
17962 unsigned int mod_time, length;
17963 dir_index d_index;
43988095
JK
17964
17965 line_ptr += bytes_read;
ecfb656c 17966 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
17967 line_ptr += bytes_read;
17968 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17969 line_ptr += bytes_read;
17970 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17971 line_ptr += bytes_read;
17972
ecfb656c 17973 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
17974 }
17975 line_ptr += bytes_read;
debd256d 17976 }
6e70227d 17977 lh->statement_program_start = line_ptr;
debd256d 17978
3019eac3 17979 if (line_ptr > (section->buffer + section->size))
4d3c2250 17980 complaint (&symfile_complaints,
3e43a32a
MS
17981 _("line number info header doesn't "
17982 "fit in `.debug_line' section"));
debd256d 17983
debd256d
JB
17984 return lh;
17985}
c906108c 17986
c6da4cef
DE
17987/* Subroutine of dwarf_decode_lines to simplify it.
17988 Return the file name of the psymtab for included file FILE_INDEX
17989 in line header LH of PST.
17990 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17991 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17992 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17993
17994 The function creates dangling cleanup registration. */
c6da4cef 17995
d521ce57 17996static const char *
c6da4cef
DE
17997psymtab_include_file_name (const struct line_header *lh, int file_index,
17998 const struct partial_symtab *pst,
17999 const char *comp_dir)
18000{
8c43009f 18001 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18002 const char *include_name = fe.name;
18003 const char *include_name_to_compare = include_name;
72b9f47f
TT
18004 const char *pst_filename;
18005 char *copied_name = NULL;
c6da4cef
DE
18006 int file_is_pst;
18007
8c43009f 18008 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18009
18010 if (!IS_ABSOLUTE_PATH (include_name)
18011 && (dir_name != NULL || comp_dir != NULL))
18012 {
18013 /* Avoid creating a duplicate psymtab for PST.
18014 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18015 Before we do the comparison, however, we need to account
18016 for DIR_NAME and COMP_DIR.
18017 First prepend dir_name (if non-NULL). If we still don't
18018 have an absolute path prepend comp_dir (if non-NULL).
18019 However, the directory we record in the include-file's
18020 psymtab does not contain COMP_DIR (to match the
18021 corresponding symtab(s)).
18022
18023 Example:
18024
18025 bash$ cd /tmp
18026 bash$ gcc -g ./hello.c
18027 include_name = "hello.c"
18028 dir_name = "."
18029 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18030 DW_AT_name = "./hello.c"
18031
18032 */
c6da4cef
DE
18033
18034 if (dir_name != NULL)
18035 {
d521ce57
TT
18036 char *tem = concat (dir_name, SLASH_STRING,
18037 include_name, (char *)NULL);
18038
18039 make_cleanup (xfree, tem);
18040 include_name = tem;
c6da4cef 18041 include_name_to_compare = include_name;
c6da4cef
DE
18042 }
18043 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18044 {
d521ce57
TT
18045 char *tem = concat (comp_dir, SLASH_STRING,
18046 include_name, (char *)NULL);
18047
18048 make_cleanup (xfree, tem);
18049 include_name_to_compare = tem;
c6da4cef
DE
18050 }
18051 }
18052
18053 pst_filename = pst->filename;
18054 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18055 {
72b9f47f
TT
18056 copied_name = concat (pst->dirname, SLASH_STRING,
18057 pst_filename, (char *)NULL);
18058 pst_filename = copied_name;
c6da4cef
DE
18059 }
18060
1e3fad37 18061 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18062
72b9f47f
TT
18063 if (copied_name != NULL)
18064 xfree (copied_name);
c6da4cef
DE
18065
18066 if (file_is_pst)
18067 return NULL;
18068 return include_name;
18069}
18070
d9b3de22
DE
18071/* State machine to track the state of the line number program. */
18072
6f77053d 18073class lnp_state_machine
d9b3de22 18074{
6f77053d
PA
18075public:
18076 /* Initialize a machine state for the start of a line number
18077 program. */
18078 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18079
8c43009f
PA
18080 file_entry *current_file ()
18081 {
18082 /* lh->file_names is 0-based, but the file name numbers in the
18083 statement program are 1-based. */
6f77053d
PA
18084 return m_line_header->file_name_at (m_file);
18085 }
18086
18087 /* Record the line in the state machine. END_SEQUENCE is true if
18088 we're processing the end of a sequence. */
18089 void record_line (bool end_sequence);
18090
18091 /* Check address and if invalid nop-out the rest of the lines in this
18092 sequence. */
18093 void check_line_address (struct dwarf2_cu *cu,
18094 const gdb_byte *line_ptr,
18095 CORE_ADDR lowpc, CORE_ADDR address);
18096
18097 void handle_set_discriminator (unsigned int discriminator)
18098 {
18099 m_discriminator = discriminator;
18100 m_line_has_non_zero_discriminator |= discriminator != 0;
18101 }
18102
18103 /* Handle DW_LNE_set_address. */
18104 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18105 {
18106 m_op_index = 0;
18107 address += baseaddr;
18108 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18109 }
18110
18111 /* Handle DW_LNS_advance_pc. */
18112 void handle_advance_pc (CORE_ADDR adjust);
18113
18114 /* Handle a special opcode. */
18115 void handle_special_opcode (unsigned char op_code);
18116
18117 /* Handle DW_LNS_advance_line. */
18118 void handle_advance_line (int line_delta)
18119 {
18120 advance_line (line_delta);
18121 }
18122
18123 /* Handle DW_LNS_set_file. */
18124 void handle_set_file (file_name_index file);
18125
18126 /* Handle DW_LNS_negate_stmt. */
18127 void handle_negate_stmt ()
18128 {
18129 m_is_stmt = !m_is_stmt;
18130 }
18131
18132 /* Handle DW_LNS_const_add_pc. */
18133 void handle_const_add_pc ();
18134
18135 /* Handle DW_LNS_fixed_advance_pc. */
18136 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18137 {
18138 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18139 m_op_index = 0;
18140 }
18141
18142 /* Handle DW_LNS_copy. */
18143 void handle_copy ()
18144 {
18145 record_line (false);
18146 m_discriminator = 0;
18147 }
18148
18149 /* Handle DW_LNE_end_sequence. */
18150 void handle_end_sequence ()
18151 {
18152 m_record_line_callback = ::record_line;
18153 }
18154
18155private:
18156 /* Advance the line by LINE_DELTA. */
18157 void advance_line (int line_delta)
18158 {
18159 m_line += line_delta;
18160
18161 if (line_delta != 0)
18162 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18163 }
18164
6f77053d
PA
18165 gdbarch *m_gdbarch;
18166
18167 /* True if we're recording lines.
18168 Otherwise we're building partial symtabs and are just interested in
18169 finding include files mentioned by the line number program. */
18170 bool m_record_lines_p;
18171
8c43009f 18172 /* The line number header. */
6f77053d 18173 line_header *m_line_header;
8c43009f 18174
6f77053d
PA
18175 /* These are part of the standard DWARF line number state machine,
18176 and initialized according to the DWARF spec. */
d9b3de22 18177
6f77053d 18178 unsigned char m_op_index = 0;
8c43009f 18179 /* The line table index (1-based) of the current file. */
6f77053d
PA
18180 file_name_index m_file = (file_name_index) 1;
18181 unsigned int m_line = 1;
18182
18183 /* These are initialized in the constructor. */
18184
18185 CORE_ADDR m_address;
18186 bool m_is_stmt;
18187 unsigned int m_discriminator;
d9b3de22
DE
18188
18189 /* Additional bits of state we need to track. */
18190
18191 /* The last file that we called dwarf2_start_subfile for.
18192 This is only used for TLLs. */
6f77053d 18193 unsigned int m_last_file = 0;
d9b3de22 18194 /* The last file a line number was recorded for. */
6f77053d 18195 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18196
18197 /* The function to call to record a line. */
6f77053d 18198 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18199
18200 /* The last line number that was recorded, used to coalesce
18201 consecutive entries for the same line. This can happen, for
18202 example, when discriminators are present. PR 17276. */
6f77053d
PA
18203 unsigned int m_last_line = 0;
18204 bool m_line_has_non_zero_discriminator = false;
8c43009f 18205};
d9b3de22 18206
6f77053d
PA
18207void
18208lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18209{
18210 CORE_ADDR addr_adj = (((m_op_index + adjust)
18211 / m_line_header->maximum_ops_per_instruction)
18212 * m_line_header->minimum_instruction_length);
18213 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18214 m_op_index = ((m_op_index + adjust)
18215 % m_line_header->maximum_ops_per_instruction);
18216}
d9b3de22 18217
6f77053d
PA
18218void
18219lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18220{
6f77053d
PA
18221 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18222 CORE_ADDR addr_adj = (((m_op_index
18223 + (adj_opcode / m_line_header->line_range))
18224 / m_line_header->maximum_ops_per_instruction)
18225 * m_line_header->minimum_instruction_length);
18226 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18227 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18228 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18229
6f77053d
PA
18230 int line_delta = (m_line_header->line_base
18231 + (adj_opcode % m_line_header->line_range));
18232 advance_line (line_delta);
18233 record_line (false);
18234 m_discriminator = 0;
18235}
d9b3de22 18236
6f77053d
PA
18237void
18238lnp_state_machine::handle_set_file (file_name_index file)
18239{
18240 m_file = file;
18241
18242 const file_entry *fe = current_file ();
18243 if (fe == NULL)
18244 dwarf2_debug_line_missing_file_complaint ();
18245 else if (m_record_lines_p)
18246 {
18247 const char *dir = fe->include_dir (m_line_header);
18248
18249 m_last_subfile = current_subfile;
18250 m_line_has_non_zero_discriminator = m_discriminator != 0;
18251 dwarf2_start_subfile (fe->name, dir);
18252 }
18253}
18254
18255void
18256lnp_state_machine::handle_const_add_pc ()
18257{
18258 CORE_ADDR adjust
18259 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18260
18261 CORE_ADDR addr_adj
18262 = (((m_op_index + adjust)
18263 / m_line_header->maximum_ops_per_instruction)
18264 * m_line_header->minimum_instruction_length);
18265
18266 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18267 m_op_index = ((m_op_index + adjust)
18268 % m_line_header->maximum_ops_per_instruction);
18269}
d9b3de22 18270
c91513d8
PP
18271/* Ignore this record_line request. */
18272
18273static void
18274noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18275{
18276 return;
18277}
18278
a05a36a5
DE
18279/* Return non-zero if we should add LINE to the line number table.
18280 LINE is the line to add, LAST_LINE is the last line that was added,
18281 LAST_SUBFILE is the subfile for LAST_LINE.
18282 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18283 had a non-zero discriminator.
18284
18285 We have to be careful in the presence of discriminators.
18286 E.g., for this line:
18287
18288 for (i = 0; i < 100000; i++);
18289
18290 clang can emit four line number entries for that one line,
18291 each with a different discriminator.
18292 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18293
18294 However, we want gdb to coalesce all four entries into one.
18295 Otherwise the user could stepi into the middle of the line and
18296 gdb would get confused about whether the pc really was in the
18297 middle of the line.
18298
18299 Things are further complicated by the fact that two consecutive
18300 line number entries for the same line is a heuristic used by gcc
18301 to denote the end of the prologue. So we can't just discard duplicate
18302 entries, we have to be selective about it. The heuristic we use is
18303 that we only collapse consecutive entries for the same line if at least
18304 one of those entries has a non-zero discriminator. PR 17276.
18305
18306 Note: Addresses in the line number state machine can never go backwards
18307 within one sequence, thus this coalescing is ok. */
18308
18309static int
18310dwarf_record_line_p (unsigned int line, unsigned int last_line,
18311 int line_has_non_zero_discriminator,
18312 struct subfile *last_subfile)
18313{
18314 if (current_subfile != last_subfile)
18315 return 1;
18316 if (line != last_line)
18317 return 1;
18318 /* Same line for the same file that we've seen already.
18319 As a last check, for pr 17276, only record the line if the line
18320 has never had a non-zero discriminator. */
18321 if (!line_has_non_zero_discriminator)
18322 return 1;
18323 return 0;
18324}
18325
252a6764
DE
18326/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18327 in the line table of subfile SUBFILE. */
18328
18329static void
d9b3de22
DE
18330dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18331 unsigned int line, CORE_ADDR address,
18332 record_line_ftype p_record_line)
252a6764
DE
18333{
18334 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18335
27e0867f
DE
18336 if (dwarf_line_debug)
18337 {
18338 fprintf_unfiltered (gdb_stdlog,
18339 "Recording line %u, file %s, address %s\n",
18340 line, lbasename (subfile->name),
18341 paddress (gdbarch, address));
18342 }
18343
d5962de5 18344 (*p_record_line) (subfile, line, addr);
252a6764
DE
18345}
18346
18347/* Subroutine of dwarf_decode_lines_1 to simplify it.
18348 Mark the end of a set of line number records.
d9b3de22 18349 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18350 If SUBFILE is NULL the request is ignored. */
18351
18352static void
18353dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18354 CORE_ADDR address, record_line_ftype p_record_line)
18355{
27e0867f
DE
18356 if (subfile == NULL)
18357 return;
18358
18359 if (dwarf_line_debug)
18360 {
18361 fprintf_unfiltered (gdb_stdlog,
18362 "Finishing current line, file %s, address %s\n",
18363 lbasename (subfile->name),
18364 paddress (gdbarch, address));
18365 }
18366
d9b3de22
DE
18367 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18368}
18369
6f77053d
PA
18370void
18371lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18372{
d9b3de22
DE
18373 if (dwarf_line_debug)
18374 {
18375 fprintf_unfiltered (gdb_stdlog,
18376 "Processing actual line %u: file %u,"
18377 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18378 m_line, to_underlying (m_file),
18379 paddress (m_gdbarch, m_address),
18380 m_is_stmt, m_discriminator);
d9b3de22
DE
18381 }
18382
6f77053d 18383 file_entry *fe = current_file ();
8c43009f
PA
18384
18385 if (fe == NULL)
d9b3de22
DE
18386 dwarf2_debug_line_missing_file_complaint ();
18387 /* For now we ignore lines not starting on an instruction boundary.
18388 But not when processing end_sequence for compatibility with the
18389 previous version of the code. */
6f77053d 18390 else if (m_op_index == 0 || end_sequence)
d9b3de22 18391 {
8c43009f 18392 fe->included_p = 1;
6f77053d 18393 if (m_record_lines_p && m_is_stmt)
d9b3de22 18394 {
6f77053d 18395 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18396 {
6f77053d
PA
18397 dwarf_finish_line (m_gdbarch, m_last_subfile,
18398 m_address, m_record_line_callback);
d9b3de22
DE
18399 }
18400
18401 if (!end_sequence)
18402 {
6f77053d
PA
18403 if (dwarf_record_line_p (m_line, m_last_line,
18404 m_line_has_non_zero_discriminator,
18405 m_last_subfile))
d9b3de22 18406 {
6f77053d
PA
18407 dwarf_record_line_1 (m_gdbarch, current_subfile,
18408 m_line, m_address,
18409 m_record_line_callback);
d9b3de22 18410 }
6f77053d
PA
18411 m_last_subfile = current_subfile;
18412 m_last_line = m_line;
d9b3de22
DE
18413 }
18414 }
18415 }
18416}
18417
6f77053d
PA
18418lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18419 bool record_lines_p)
d9b3de22 18420{
6f77053d
PA
18421 m_gdbarch = arch;
18422 m_record_lines_p = record_lines_p;
18423 m_line_header = lh;
d9b3de22 18424
6f77053d 18425 m_record_line_callback = ::record_line;
d9b3de22 18426
d9b3de22
DE
18427 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18428 was a line entry for it so that the backend has a chance to adjust it
18429 and also record it in case it needs it. This is currently used by MIPS
18430 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18431 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18432 m_is_stmt = lh->default_is_stmt;
18433 m_discriminator = 0;
252a6764
DE
18434}
18435
6f77053d
PA
18436void
18437lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18438 const gdb_byte *line_ptr,
18439 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18440{
18441 /* If address < lowpc then it's not a usable value, it's outside the
18442 pc range of the CU. However, we restrict the test to only address
18443 values of zero to preserve GDB's previous behaviour which is to
18444 handle the specific case of a function being GC'd by the linker. */
18445
18446 if (address == 0 && address < lowpc)
18447 {
18448 /* This line table is for a function which has been
18449 GCd by the linker. Ignore it. PR gdb/12528 */
18450
18451 struct objfile *objfile = cu->objfile;
18452 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18453
18454 complaint (&symfile_complaints,
18455 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18456 line_offset, objfile_name (objfile));
6f77053d
PA
18457 m_record_line_callback = noop_record_line;
18458 /* Note: record_line_callback is left as noop_record_line until
18459 we see DW_LNE_end_sequence. */
924c2928
DE
18460 }
18461}
18462
f3f5162e 18463/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18464 Process the line number information in LH.
18465 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18466 program in order to set included_p for every referenced header. */
debd256d 18467
c906108c 18468static void
43f3e411
DE
18469dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18470 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18471{
d521ce57
TT
18472 const gdb_byte *line_ptr, *extended_end;
18473 const gdb_byte *line_end;
a8c50c1f 18474 unsigned int bytes_read, extended_len;
699ca60a 18475 unsigned char op_code, extended_op;
e142c38c
DJ
18476 CORE_ADDR baseaddr;
18477 struct objfile *objfile = cu->objfile;
f3f5162e 18478 bfd *abfd = objfile->obfd;
fbf65064 18479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18480 /* True if we're recording line info (as opposed to building partial
18481 symtabs and just interested in finding include files mentioned by
18482 the line number program). */
18483 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18484
18485 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18486
debd256d
JB
18487 line_ptr = lh->statement_program_start;
18488 line_end = lh->statement_program_end;
c906108c
SS
18489
18490 /* Read the statement sequences until there's nothing left. */
18491 while (line_ptr < line_end)
18492 {
6f77053d
PA
18493 /* The DWARF line number program state machine. Reset the state
18494 machine at the start of each sequence. */
18495 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18496 bool end_sequence = false;
d9b3de22 18497
8c43009f 18498 if (record_lines_p)
c906108c 18499 {
8c43009f
PA
18500 /* Start a subfile for the current file of the state
18501 machine. */
18502 const file_entry *fe = state_machine.current_file ();
18503
18504 if (fe != NULL)
18505 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18506 }
18507
a738430d 18508 /* Decode the table. */
d9b3de22 18509 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18510 {
18511 op_code = read_1_byte (abfd, line_ptr);
18512 line_ptr += 1;
9aa1fe7e 18513
debd256d 18514 if (op_code >= lh->opcode_base)
6e70227d 18515 {
8e07a239 18516 /* Special opcode. */
6f77053d 18517 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18518 }
18519 else switch (op_code)
c906108c
SS
18520 {
18521 case DW_LNS_extended_op:
3e43a32a
MS
18522 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18523 &bytes_read);
473b7be6 18524 line_ptr += bytes_read;
a8c50c1f 18525 extended_end = line_ptr + extended_len;
c906108c
SS
18526 extended_op = read_1_byte (abfd, line_ptr);
18527 line_ptr += 1;
18528 switch (extended_op)
18529 {
18530 case DW_LNE_end_sequence:
6f77053d
PA
18531 state_machine.handle_end_sequence ();
18532 end_sequence = true;
c906108c
SS
18533 break;
18534 case DW_LNE_set_address:
d9b3de22
DE
18535 {
18536 CORE_ADDR address
18537 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18538 line_ptr += bytes_read;
6f77053d
PA
18539
18540 state_machine.check_line_address (cu, line_ptr,
18541 lowpc, address);
18542 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18543 }
c906108c
SS
18544 break;
18545 case DW_LNE_define_file:
debd256d 18546 {
d521ce57 18547 const char *cur_file;
ecfb656c
PA
18548 unsigned int mod_time, length;
18549 dir_index dindex;
6e70227d 18550
3e43a32a
MS
18551 cur_file = read_direct_string (abfd, line_ptr,
18552 &bytes_read);
debd256d 18553 line_ptr += bytes_read;
ecfb656c 18554 dindex = (dir_index)
debd256d
JB
18555 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18556 line_ptr += bytes_read;
18557 mod_time =
18558 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18559 line_ptr += bytes_read;
18560 length =
18561 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18562 line_ptr += bytes_read;
ecfb656c 18563 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18564 }
c906108c 18565 break;
d0c6ba3d 18566 case DW_LNE_set_discriminator:
6f77053d
PA
18567 {
18568 /* The discriminator is not interesting to the
18569 debugger; just ignore it. We still need to
18570 check its value though:
18571 if there are consecutive entries for the same
18572 (non-prologue) line we want to coalesce them.
18573 PR 17276. */
18574 unsigned int discr
18575 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18576 line_ptr += bytes_read;
18577
18578 state_machine.handle_set_discriminator (discr);
18579 }
d0c6ba3d 18580 break;
c906108c 18581 default:
4d3c2250 18582 complaint (&symfile_complaints,
e2e0b3e5 18583 _("mangled .debug_line section"));
debd256d 18584 return;
c906108c 18585 }
a8c50c1f
DJ
18586 /* Make sure that we parsed the extended op correctly. If e.g.
18587 we expected a different address size than the producer used,
18588 we may have read the wrong number of bytes. */
18589 if (line_ptr != extended_end)
18590 {
18591 complaint (&symfile_complaints,
18592 _("mangled .debug_line section"));
18593 return;
18594 }
c906108c
SS
18595 break;
18596 case DW_LNS_copy:
6f77053d 18597 state_machine.handle_copy ();
c906108c
SS
18598 break;
18599 case DW_LNS_advance_pc:
2dc7f7b3
TT
18600 {
18601 CORE_ADDR adjust
18602 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18603 line_ptr += bytes_read;
6f77053d
PA
18604
18605 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18606 }
c906108c
SS
18607 break;
18608 case DW_LNS_advance_line:
a05a36a5
DE
18609 {
18610 int line_delta
18611 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18612 line_ptr += bytes_read;
6f77053d
PA
18613
18614 state_machine.handle_advance_line (line_delta);
a05a36a5 18615 }
c906108c
SS
18616 break;
18617 case DW_LNS_set_file:
d9b3de22 18618 {
6f77053d 18619 file_name_index file
ecfb656c
PA
18620 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18621 &bytes_read);
d9b3de22 18622 line_ptr += bytes_read;
8c43009f 18623
6f77053d 18624 state_machine.handle_set_file (file);
d9b3de22 18625 }
c906108c
SS
18626 break;
18627 case DW_LNS_set_column:
0ad93d4f 18628 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18629 line_ptr += bytes_read;
18630 break;
18631 case DW_LNS_negate_stmt:
6f77053d 18632 state_machine.handle_negate_stmt ();
c906108c
SS
18633 break;
18634 case DW_LNS_set_basic_block:
c906108c 18635 break;
c2c6d25f
JM
18636 /* Add to the address register of the state machine the
18637 address increment value corresponding to special opcode
a738430d
MK
18638 255. I.e., this value is scaled by the minimum
18639 instruction length since special opcode 255 would have
b021a221 18640 scaled the increment. */
c906108c 18641 case DW_LNS_const_add_pc:
6f77053d 18642 state_machine.handle_const_add_pc ();
c906108c
SS
18643 break;
18644 case DW_LNS_fixed_advance_pc:
3e29f34a 18645 {
6f77053d 18646 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18647 line_ptr += 2;
6f77053d
PA
18648
18649 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18650 }
c906108c 18651 break;
9aa1fe7e 18652 default:
a738430d
MK
18653 {
18654 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18655 int i;
a738430d 18656
debd256d 18657 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18658 {
18659 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18660 line_ptr += bytes_read;
18661 }
18662 }
c906108c
SS
18663 }
18664 }
d9b3de22
DE
18665
18666 if (!end_sequence)
18667 dwarf2_debug_line_missing_end_sequence_complaint ();
18668
18669 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18670 in which case we still finish recording the last line). */
6f77053d 18671 state_machine.record_line (true);
c906108c 18672 }
f3f5162e
DE
18673}
18674
18675/* Decode the Line Number Program (LNP) for the given line_header
18676 structure and CU. The actual information extracted and the type
18677 of structures created from the LNP depends on the value of PST.
18678
18679 1. If PST is NULL, then this procedure uses the data from the program
18680 to create all necessary symbol tables, and their linetables.
18681
18682 2. If PST is not NULL, this procedure reads the program to determine
18683 the list of files included by the unit represented by PST, and
18684 builds all the associated partial symbol tables.
18685
18686 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18687 It is used for relative paths in the line table.
18688 NOTE: When processing partial symtabs (pst != NULL),
18689 comp_dir == pst->dirname.
18690
18691 NOTE: It is important that psymtabs have the same file name (via strcmp)
18692 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18693 symtab we don't use it in the name of the psymtabs we create.
18694 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18695 A good testcase for this is mb-inline.exp.
18696
527f3840
JK
18697 LOWPC is the lowest address in CU (or 0 if not known).
18698
18699 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18700 for its PC<->lines mapping information. Otherwise only the filename
18701 table is read in. */
f3f5162e
DE
18702
18703static void
18704dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18705 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18706 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18707{
18708 struct objfile *objfile = cu->objfile;
18709 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18710
527f3840
JK
18711 if (decode_mapping)
18712 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18713
18714 if (decode_for_pst_p)
18715 {
18716 int file_index;
18717
18718 /* Now that we're done scanning the Line Header Program, we can
18719 create the psymtab of each included file. */
fff8551c 18720 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18721 if (lh->file_names[file_index].included_p == 1)
18722 {
d521ce57 18723 const char *include_name =
c6da4cef
DE
18724 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18725 if (include_name != NULL)
aaa75496
JB
18726 dwarf2_create_include_psymtab (include_name, pst, objfile);
18727 }
18728 }
cb1df416
DJ
18729 else
18730 {
18731 /* Make sure a symtab is created for every file, even files
18732 which contain only variables (i.e. no code with associated
18733 line numbers). */
43f3e411 18734 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18735 int i;
cb1df416 18736
fff8551c 18737 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18738 {
8c43009f 18739 file_entry &fe = lh->file_names[i];
9a619af0 18740
8c43009f 18741 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18742
cb1df416 18743 if (current_subfile->symtab == NULL)
43f3e411
DE
18744 {
18745 current_subfile->symtab
18746 = allocate_symtab (cust, current_subfile->name);
18747 }
8c43009f 18748 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18749 }
18750 }
c906108c
SS
18751}
18752
18753/* Start a subfile for DWARF. FILENAME is the name of the file and
18754 DIRNAME the name of the source directory which contains FILENAME
4d663531 18755 or NULL if not known.
c906108c
SS
18756 This routine tries to keep line numbers from identical absolute and
18757 relative file names in a common subfile.
18758
18759 Using the `list' example from the GDB testsuite, which resides in
18760 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18761 of /srcdir/list0.c yields the following debugging information for list0.c:
18762
c5aa993b 18763 DW_AT_name: /srcdir/list0.c
4d663531 18764 DW_AT_comp_dir: /compdir
357e46e7 18765 files.files[0].name: list0.h
c5aa993b 18766 files.files[0].dir: /srcdir
357e46e7 18767 files.files[1].name: list0.c
c5aa993b 18768 files.files[1].dir: /srcdir
c906108c
SS
18769
18770 The line number information for list0.c has to end up in a single
4f1520fb
FR
18771 subfile, so that `break /srcdir/list0.c:1' works as expected.
18772 start_subfile will ensure that this happens provided that we pass the
18773 concatenation of files.files[1].dir and files.files[1].name as the
18774 subfile's name. */
c906108c
SS
18775
18776static void
4d663531 18777dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18778{
d521ce57 18779 char *copy = NULL;
4f1520fb 18780
4d663531 18781 /* In order not to lose the line information directory,
4f1520fb
FR
18782 we concatenate it to the filename when it makes sense.
18783 Note that the Dwarf3 standard says (speaking of filenames in line
18784 information): ``The directory index is ignored for file names
18785 that represent full path names''. Thus ignoring dirname in the
18786 `else' branch below isn't an issue. */
c906108c 18787
d5166ae1 18788 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18789 {
18790 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18791 filename = copy;
18792 }
c906108c 18793
4d663531 18794 start_subfile (filename);
4f1520fb 18795
d521ce57
TT
18796 if (copy != NULL)
18797 xfree (copy);
c906108c
SS
18798}
18799
f4dc4d17
DE
18800/* Start a symtab for DWARF.
18801 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18802
43f3e411 18803static struct compunit_symtab *
f4dc4d17 18804dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18805 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18806{
43f3e411
DE
18807 struct compunit_symtab *cust
18808 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18809
f4dc4d17
DE
18810 record_debugformat ("DWARF 2");
18811 record_producer (cu->producer);
18812
18813 /* We assume that we're processing GCC output. */
18814 processing_gcc_compilation = 2;
18815
4d4ec4e5 18816 cu->processing_has_namespace_info = 0;
43f3e411
DE
18817
18818 return cust;
f4dc4d17
DE
18819}
18820
4c2df51b
DJ
18821static void
18822var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18823 struct dwarf2_cu *cu)
4c2df51b 18824{
e7c27a73
DJ
18825 struct objfile *objfile = cu->objfile;
18826 struct comp_unit_head *cu_header = &cu->header;
18827
4c2df51b
DJ
18828 /* NOTE drow/2003-01-30: There used to be a comment and some special
18829 code here to turn a symbol with DW_AT_external and a
18830 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18831 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18832 with some versions of binutils) where shared libraries could have
18833 relocations against symbols in their debug information - the
18834 minimal symbol would have the right address, but the debug info
18835 would not. It's no longer necessary, because we will explicitly
18836 apply relocations when we read in the debug information now. */
18837
18838 /* A DW_AT_location attribute with no contents indicates that a
18839 variable has been optimized away. */
18840 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18841 {
f1e6e072 18842 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18843 return;
18844 }
18845
18846 /* Handle one degenerate form of location expression specially, to
18847 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18848 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18849 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18850
18851 if (attr_form_is_block (attr)
3019eac3
DE
18852 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18853 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18854 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18855 && (DW_BLOCK (attr)->size
18856 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18857 {
891d2f0b 18858 unsigned int dummy;
4c2df51b 18859
3019eac3
DE
18860 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18861 SYMBOL_VALUE_ADDRESS (sym) =
18862 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18863 else
18864 SYMBOL_VALUE_ADDRESS (sym) =
18865 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18866 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18867 fixup_symbol_section (sym, objfile);
18868 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18869 SYMBOL_SECTION (sym));
4c2df51b
DJ
18870 return;
18871 }
18872
18873 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18874 expression evaluator, and use LOC_COMPUTED only when necessary
18875 (i.e. when the value of a register or memory location is
18876 referenced, or a thread-local block, etc.). Then again, it might
18877 not be worthwhile. I'm assuming that it isn't unless performance
18878 or memory numbers show me otherwise. */
18879
f1e6e072 18880 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18881
f1e6e072 18882 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18883 cu->has_loclist = 1;
4c2df51b
DJ
18884}
18885
c906108c
SS
18886/* Given a pointer to a DWARF information entry, figure out if we need
18887 to make a symbol table entry for it, and if so, create a new entry
18888 and return a pointer to it.
18889 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18890 used the passed type.
18891 If SPACE is not NULL, use it to hold the new symbol. If it is
18892 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18893
18894static struct symbol *
34eaf542
TT
18895new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18896 struct symbol *space)
c906108c 18897{
e7c27a73 18898 struct objfile *objfile = cu->objfile;
3e29f34a 18899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18900 struct symbol *sym = NULL;
15d034d0 18901 const char *name;
c906108c
SS
18902 struct attribute *attr = NULL;
18903 struct attribute *attr2 = NULL;
e142c38c 18904 CORE_ADDR baseaddr;
e37fd15a
SW
18905 struct pending **list_to_add = NULL;
18906
edb3359d 18907 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18908
18909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18910
94af9270 18911 name = dwarf2_name (die, cu);
c906108c
SS
18912 if (name)
18913 {
94af9270 18914 const char *linkagename;
34eaf542 18915 int suppress_add = 0;
94af9270 18916
34eaf542
TT
18917 if (space)
18918 sym = space;
18919 else
e623cf5d 18920 sym = allocate_symbol (objfile);
c906108c 18921 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18922
18923 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18924 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18925 linkagename = dwarf2_physname (name, die, cu);
18926 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18927
f55ee35c
JK
18928 /* Fortran does not have mangling standard and the mangling does differ
18929 between gfortran, iFort etc. */
18930 if (cu->language == language_fortran
b250c185 18931 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18932 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18933 dwarf2_full_name (name, die, cu),
29df156d 18934 NULL);
f55ee35c 18935
c906108c 18936 /* Default assumptions.
c5aa993b 18937 Use the passed type or decode it from the die. */
176620f1 18938 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18939 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18940 if (type != NULL)
18941 SYMBOL_TYPE (sym) = type;
18942 else
e7c27a73 18943 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18944 attr = dwarf2_attr (die,
18945 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18946 cu);
c906108c
SS
18947 if (attr)
18948 {
18949 SYMBOL_LINE (sym) = DW_UNSND (attr);
18950 }
cb1df416 18951
edb3359d
DJ
18952 attr = dwarf2_attr (die,
18953 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18954 cu);
cb1df416
DJ
18955 if (attr)
18956 {
ecfb656c 18957 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 18958 struct file_entry *fe;
9a619af0 18959
ecfb656c
PA
18960 if (cu->line_header != NULL)
18961 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
18962 else
18963 fe = NULL;
18964
18965 if (fe == NULL)
cb1df416
DJ
18966 complaint (&symfile_complaints,
18967 _("file index out of range"));
8c43009f
PA
18968 else
18969 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18970 }
18971
c906108c
SS
18972 switch (die->tag)
18973 {
18974 case DW_TAG_label:
e142c38c 18975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18976 if (attr)
3e29f34a
MR
18977 {
18978 CORE_ADDR addr;
18979
18980 addr = attr_value_as_address (attr);
18981 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18982 SYMBOL_VALUE_ADDRESS (sym) = addr;
18983 }
0f5238ed
TT
18984 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18985 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18986 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18987 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18988 break;
18989 case DW_TAG_subprogram:
18990 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18991 finish_block. */
f1e6e072 18992 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18994 if ((attr2 && (DW_UNSND (attr2) != 0))
18995 || cu->language == language_ada)
c906108c 18996 {
2cfa0c8d
JB
18997 /* Subprograms marked external are stored as a global symbol.
18998 Ada subprograms, whether marked external or not, are always
18999 stored as a global symbol, because we want to be able to
19000 access them globally. For instance, we want to be able
19001 to break on a nested subprogram without having to
19002 specify the context. */
e37fd15a 19003 list_to_add = &global_symbols;
c906108c
SS
19004 }
19005 else
19006 {
e37fd15a 19007 list_to_add = cu->list_in_scope;
c906108c
SS
19008 }
19009 break;
edb3359d
DJ
19010 case DW_TAG_inlined_subroutine:
19011 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19012 finish_block. */
f1e6e072 19013 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19014 SYMBOL_INLINED (sym) = 1;
481860b3 19015 list_to_add = cu->list_in_scope;
edb3359d 19016 break;
34eaf542
TT
19017 case DW_TAG_template_value_param:
19018 suppress_add = 1;
19019 /* Fall through. */
72929c62 19020 case DW_TAG_constant:
c906108c 19021 case DW_TAG_variable:
254e6b9e 19022 case DW_TAG_member:
0963b4bd
MS
19023 /* Compilation with minimal debug info may result in
19024 variables with missing type entries. Change the
19025 misleading `void' type to something sensible. */
c906108c 19026 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 19027 SYMBOL_TYPE (sym)
46bf5051 19028 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 19029
e142c38c 19030 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19031 /* In the case of DW_TAG_member, we should only be called for
19032 static const members. */
19033 if (die->tag == DW_TAG_member)
19034 {
3863f96c
DE
19035 /* dwarf2_add_field uses die_is_declaration,
19036 so we do the same. */
254e6b9e
DE
19037 gdb_assert (die_is_declaration (die, cu));
19038 gdb_assert (attr);
19039 }
c906108c
SS
19040 if (attr)
19041 {
e7c27a73 19042 dwarf2_const_value (attr, sym, cu);
e142c38c 19043 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19044 if (!suppress_add)
34eaf542
TT
19045 {
19046 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19047 list_to_add = &global_symbols;
34eaf542 19048 else
e37fd15a 19049 list_to_add = cu->list_in_scope;
34eaf542 19050 }
c906108c
SS
19051 break;
19052 }
e142c38c 19053 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19054 if (attr)
19055 {
e7c27a73 19056 var_decode_location (attr, sym, cu);
e142c38c 19057 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19058
19059 /* Fortran explicitly imports any global symbols to the local
19060 scope by DW_TAG_common_block. */
19061 if (cu->language == language_fortran && die->parent
19062 && die->parent->tag == DW_TAG_common_block)
19063 attr2 = NULL;
19064
caac4577
JG
19065 if (SYMBOL_CLASS (sym) == LOC_STATIC
19066 && SYMBOL_VALUE_ADDRESS (sym) == 0
19067 && !dwarf2_per_objfile->has_section_at_zero)
19068 {
19069 /* When a static variable is eliminated by the linker,
19070 the corresponding debug information is not stripped
19071 out, but the variable address is set to null;
19072 do not add such variables into symbol table. */
19073 }
19074 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19075 {
f55ee35c
JK
19076 /* Workaround gfortran PR debug/40040 - it uses
19077 DW_AT_location for variables in -fPIC libraries which may
19078 get overriden by other libraries/executable and get
19079 a different address. Resolve it by the minimal symbol
19080 which may come from inferior's executable using copy
19081 relocation. Make this workaround only for gfortran as for
19082 other compilers GDB cannot guess the minimal symbol
19083 Fortran mangling kind. */
19084 if (cu->language == language_fortran && die->parent
19085 && die->parent->tag == DW_TAG_module
19086 && cu->producer
28586665 19087 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19088 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19089
1c809c68
TT
19090 /* A variable with DW_AT_external is never static,
19091 but it may be block-scoped. */
19092 list_to_add = (cu->list_in_scope == &file_symbols
19093 ? &global_symbols : cu->list_in_scope);
1c809c68 19094 }
c906108c 19095 else
e37fd15a 19096 list_to_add = cu->list_in_scope;
c906108c
SS
19097 }
19098 else
19099 {
19100 /* We do not know the address of this symbol.
c5aa993b
JM
19101 If it is an external symbol and we have type information
19102 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19103 The address of the variable will then be determined from
19104 the minimal symbol table whenever the variable is
19105 referenced. */
e142c38c 19106 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19107
19108 /* Fortran explicitly imports any global symbols to the local
19109 scope by DW_TAG_common_block. */
19110 if (cu->language == language_fortran && die->parent
19111 && die->parent->tag == DW_TAG_common_block)
19112 {
19113 /* SYMBOL_CLASS doesn't matter here because
19114 read_common_block is going to reset it. */
19115 if (!suppress_add)
19116 list_to_add = cu->list_in_scope;
19117 }
19118 else if (attr2 && (DW_UNSND (attr2) != 0)
19119 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19120 {
0fe7935b
DJ
19121 /* A variable with DW_AT_external is never static, but it
19122 may be block-scoped. */
19123 list_to_add = (cu->list_in_scope == &file_symbols
19124 ? &global_symbols : cu->list_in_scope);
19125
f1e6e072 19126 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19127 }
442ddf59
JK
19128 else if (!die_is_declaration (die, cu))
19129 {
19130 /* Use the default LOC_OPTIMIZED_OUT class. */
19131 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19132 if (!suppress_add)
19133 list_to_add = cu->list_in_scope;
442ddf59 19134 }
c906108c
SS
19135 }
19136 break;
19137 case DW_TAG_formal_parameter:
edb3359d
DJ
19138 /* If we are inside a function, mark this as an argument. If
19139 not, we might be looking at an argument to an inlined function
19140 when we do not have enough information to show inlined frames;
19141 pretend it's a local variable in that case so that the user can
19142 still see it. */
19143 if (context_stack_depth > 0
19144 && context_stack[context_stack_depth - 1].name != NULL)
19145 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19146 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19147 if (attr)
19148 {
e7c27a73 19149 var_decode_location (attr, sym, cu);
c906108c 19150 }
e142c38c 19151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19152 if (attr)
19153 {
e7c27a73 19154 dwarf2_const_value (attr, sym, cu);
c906108c 19155 }
f346a30d 19156
e37fd15a 19157 list_to_add = cu->list_in_scope;
c906108c
SS
19158 break;
19159 case DW_TAG_unspecified_parameters:
19160 /* From varargs functions; gdb doesn't seem to have any
19161 interest in this information, so just ignore it for now.
19162 (FIXME?) */
19163 break;
34eaf542
TT
19164 case DW_TAG_template_type_param:
19165 suppress_add = 1;
19166 /* Fall through. */
c906108c 19167 case DW_TAG_class_type:
680b30c7 19168 case DW_TAG_interface_type:
c906108c
SS
19169 case DW_TAG_structure_type:
19170 case DW_TAG_union_type:
72019c9c 19171 case DW_TAG_set_type:
c906108c 19172 case DW_TAG_enumeration_type:
f1e6e072 19173 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19174 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19175
63d06c5c 19176 {
9c37b5ae 19177 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19178 really ever be static objects: otherwise, if you try
19179 to, say, break of a class's method and you're in a file
19180 which doesn't mention that class, it won't work unless
19181 the check for all static symbols in lookup_symbol_aux
19182 saves you. See the OtherFileClass tests in
19183 gdb.c++/namespace.exp. */
19184
e37fd15a 19185 if (!suppress_add)
34eaf542 19186 {
34eaf542 19187 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19188 && cu->language == language_cplus
34eaf542 19189 ? &global_symbols : cu->list_in_scope);
63d06c5c 19190
64382290 19191 /* The semantics of C++ state that "struct foo {
9c37b5ae 19192 ... }" also defines a typedef for "foo". */
64382290 19193 if (cu->language == language_cplus
45280282 19194 || cu->language == language_ada
c44af4eb
TT
19195 || cu->language == language_d
19196 || cu->language == language_rust)
64382290
TT
19197 {
19198 /* The symbol's name is already allocated along
19199 with this objfile, so we don't need to
19200 duplicate it for the type. */
19201 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19202 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19203 }
63d06c5c
DC
19204 }
19205 }
c906108c
SS
19206 break;
19207 case DW_TAG_typedef:
f1e6e072 19208 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19209 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19210 list_to_add = cu->list_in_scope;
63d06c5c 19211 break;
c906108c 19212 case DW_TAG_base_type:
a02abb62 19213 case DW_TAG_subrange_type:
f1e6e072 19214 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19216 list_to_add = cu->list_in_scope;
c906108c
SS
19217 break;
19218 case DW_TAG_enumerator:
e142c38c 19219 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19220 if (attr)
19221 {
e7c27a73 19222 dwarf2_const_value (attr, sym, cu);
c906108c 19223 }
63d06c5c
DC
19224 {
19225 /* NOTE: carlton/2003-11-10: See comment above in the
19226 DW_TAG_class_type, etc. block. */
19227
e142c38c 19228 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19229 && cu->language == language_cplus
e142c38c 19230 ? &global_symbols : cu->list_in_scope);
63d06c5c 19231 }
c906108c 19232 break;
74921315 19233 case DW_TAG_imported_declaration:
5c4e30ca 19234 case DW_TAG_namespace:
f1e6e072 19235 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19236 list_to_add = &global_symbols;
5c4e30ca 19237 break;
530e8392
KB
19238 case DW_TAG_module:
19239 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19240 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19241 list_to_add = &global_symbols;
19242 break;
4357ac6c 19243 case DW_TAG_common_block:
f1e6e072 19244 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19245 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19246 add_symbol_to_list (sym, cu->list_in_scope);
19247 break;
c906108c
SS
19248 default:
19249 /* Not a tag we recognize. Hopefully we aren't processing
19250 trash data, but since we must specifically ignore things
19251 we don't recognize, there is nothing else we should do at
0963b4bd 19252 this point. */
e2e0b3e5 19253 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19254 dwarf_tag_name (die->tag));
c906108c
SS
19255 break;
19256 }
df8a16a1 19257
e37fd15a
SW
19258 if (suppress_add)
19259 {
19260 sym->hash_next = objfile->template_symbols;
19261 objfile->template_symbols = sym;
19262 list_to_add = NULL;
19263 }
19264
19265 if (list_to_add != NULL)
19266 add_symbol_to_list (sym, list_to_add);
19267
df8a16a1
DJ
19268 /* For the benefit of old versions of GCC, check for anonymous
19269 namespaces based on the demangled name. */
4d4ec4e5 19270 if (!cu->processing_has_namespace_info
94af9270 19271 && cu->language == language_cplus)
a10964d1 19272 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19273 }
19274 return (sym);
19275}
19276
34eaf542
TT
19277/* A wrapper for new_symbol_full that always allocates a new symbol. */
19278
19279static struct symbol *
19280new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19281{
19282 return new_symbol_full (die, type, cu, NULL);
19283}
19284
98bfdba5
PA
19285/* Given an attr with a DW_FORM_dataN value in host byte order,
19286 zero-extend it as appropriate for the symbol's type. The DWARF
19287 standard (v4) is not entirely clear about the meaning of using
19288 DW_FORM_dataN for a constant with a signed type, where the type is
19289 wider than the data. The conclusion of a discussion on the DWARF
19290 list was that this is unspecified. We choose to always zero-extend
19291 because that is the interpretation long in use by GCC. */
c906108c 19292
98bfdba5 19293static gdb_byte *
ff39bb5e 19294dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19295 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19296{
e7c27a73 19297 struct objfile *objfile = cu->objfile;
e17a4113
UW
19298 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19299 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19300 LONGEST l = DW_UNSND (attr);
19301
19302 if (bits < sizeof (*value) * 8)
19303 {
19304 l &= ((LONGEST) 1 << bits) - 1;
19305 *value = l;
19306 }
19307 else if (bits == sizeof (*value) * 8)
19308 *value = l;
19309 else
19310 {
224c3ddb 19311 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19312 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19313 return bytes;
19314 }
19315
19316 return NULL;
19317}
19318
19319/* Read a constant value from an attribute. Either set *VALUE, or if
19320 the value does not fit in *VALUE, set *BYTES - either already
19321 allocated on the objfile obstack, or newly allocated on OBSTACK,
19322 or, set *BATON, if we translated the constant to a location
19323 expression. */
19324
19325static void
ff39bb5e 19326dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19327 const char *name, struct obstack *obstack,
19328 struct dwarf2_cu *cu,
d521ce57 19329 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19330 struct dwarf2_locexpr_baton **baton)
19331{
19332 struct objfile *objfile = cu->objfile;
19333 struct comp_unit_head *cu_header = &cu->header;
c906108c 19334 struct dwarf_block *blk;
98bfdba5
PA
19335 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19336 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19337
19338 *value = 0;
19339 *bytes = NULL;
19340 *baton = NULL;
c906108c
SS
19341
19342 switch (attr->form)
19343 {
19344 case DW_FORM_addr:
3019eac3 19345 case DW_FORM_GNU_addr_index:
ac56253d 19346 {
ac56253d
TT
19347 gdb_byte *data;
19348
98bfdba5
PA
19349 if (TYPE_LENGTH (type) != cu_header->addr_size)
19350 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19351 cu_header->addr_size,
98bfdba5 19352 TYPE_LENGTH (type));
ac56253d
TT
19353 /* Symbols of this form are reasonably rare, so we just
19354 piggyback on the existing location code rather than writing
19355 a new implementation of symbol_computed_ops. */
8d749320 19356 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19357 (*baton)->per_cu = cu->per_cu;
19358 gdb_assert ((*baton)->per_cu);
ac56253d 19359
98bfdba5 19360 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19361 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19362 (*baton)->data = data;
ac56253d
TT
19363
19364 data[0] = DW_OP_addr;
19365 store_unsigned_integer (&data[1], cu_header->addr_size,
19366 byte_order, DW_ADDR (attr));
19367 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19368 }
c906108c 19369 break;
4ac36638 19370 case DW_FORM_string:
93b5768b 19371 case DW_FORM_strp:
3019eac3 19372 case DW_FORM_GNU_str_index:
36586728 19373 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19374 /* DW_STRING is already allocated on the objfile obstack, point
19375 directly to it. */
d521ce57 19376 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19377 break;
c906108c
SS
19378 case DW_FORM_block1:
19379 case DW_FORM_block2:
19380 case DW_FORM_block4:
19381 case DW_FORM_block:
2dc7f7b3 19382 case DW_FORM_exprloc:
0224619f 19383 case DW_FORM_data16:
c906108c 19384 blk = DW_BLOCK (attr);
98bfdba5
PA
19385 if (TYPE_LENGTH (type) != blk->size)
19386 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19387 TYPE_LENGTH (type));
19388 *bytes = blk->data;
c906108c 19389 break;
2df3850c
JM
19390
19391 /* The DW_AT_const_value attributes are supposed to carry the
19392 symbol's value "represented as it would be on the target
19393 architecture." By the time we get here, it's already been
19394 converted to host endianness, so we just need to sign- or
19395 zero-extend it as appropriate. */
19396 case DW_FORM_data1:
3aef2284 19397 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19398 break;
c906108c 19399 case DW_FORM_data2:
3aef2284 19400 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19401 break;
c906108c 19402 case DW_FORM_data4:
3aef2284 19403 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19404 break;
c906108c 19405 case DW_FORM_data8:
3aef2284 19406 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19407 break;
19408
c906108c 19409 case DW_FORM_sdata:
98bfdba5 19410 *value = DW_SND (attr);
2df3850c
JM
19411 break;
19412
c906108c 19413 case DW_FORM_udata:
98bfdba5 19414 *value = DW_UNSND (attr);
c906108c 19415 break;
2df3850c 19416
c906108c 19417 default:
4d3c2250 19418 complaint (&symfile_complaints,
e2e0b3e5 19419 _("unsupported const value attribute form: '%s'"),
4d3c2250 19420 dwarf_form_name (attr->form));
98bfdba5 19421 *value = 0;
c906108c
SS
19422 break;
19423 }
19424}
19425
2df3850c 19426
98bfdba5
PA
19427/* Copy constant value from an attribute to a symbol. */
19428
2df3850c 19429static void
ff39bb5e 19430dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19431 struct dwarf2_cu *cu)
2df3850c 19432{
98bfdba5 19433 struct objfile *objfile = cu->objfile;
12df843f 19434 LONGEST value;
d521ce57 19435 const gdb_byte *bytes;
98bfdba5 19436 struct dwarf2_locexpr_baton *baton;
2df3850c 19437
98bfdba5
PA
19438 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19439 SYMBOL_PRINT_NAME (sym),
19440 &objfile->objfile_obstack, cu,
19441 &value, &bytes, &baton);
2df3850c 19442
98bfdba5
PA
19443 if (baton != NULL)
19444 {
98bfdba5 19445 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19446 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19447 }
19448 else if (bytes != NULL)
19449 {
19450 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19451 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19452 }
19453 else
19454 {
19455 SYMBOL_VALUE (sym) = value;
f1e6e072 19456 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19457 }
2df3850c
JM
19458}
19459
c906108c
SS
19460/* Return the type of the die in question using its DW_AT_type attribute. */
19461
19462static struct type *
e7c27a73 19463die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19464{
c906108c 19465 struct attribute *type_attr;
c906108c 19466
e142c38c 19467 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19468 if (!type_attr)
19469 {
19470 /* A missing DW_AT_type represents a void type. */
46bf5051 19471 return objfile_type (cu->objfile)->builtin_void;
c906108c 19472 }
348e048f 19473
673bfd45 19474 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19475}
19476
b4ba55a1
JB
19477/* True iff CU's producer generates GNAT Ada auxiliary information
19478 that allows to find parallel types through that information instead
19479 of having to do expensive parallel lookups by type name. */
19480
19481static int
19482need_gnat_info (struct dwarf2_cu *cu)
19483{
19484 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19485 of GNAT produces this auxiliary information, without any indication
19486 that it is produced. Part of enhancing the FSF version of GNAT
19487 to produce that information will be to put in place an indicator
19488 that we can use in order to determine whether the descriptive type
19489 info is available or not. One suggestion that has been made is
19490 to use a new attribute, attached to the CU die. For now, assume
19491 that the descriptive type info is not available. */
19492 return 0;
19493}
19494
b4ba55a1
JB
19495/* Return the auxiliary type of the die in question using its
19496 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19497 attribute is not present. */
19498
19499static struct type *
19500die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19501{
b4ba55a1 19502 struct attribute *type_attr;
b4ba55a1
JB
19503
19504 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19505 if (!type_attr)
19506 return NULL;
19507
673bfd45 19508 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19509}
19510
19511/* If DIE has a descriptive_type attribute, then set the TYPE's
19512 descriptive type accordingly. */
19513
19514static void
19515set_descriptive_type (struct type *type, struct die_info *die,
19516 struct dwarf2_cu *cu)
19517{
19518 struct type *descriptive_type = die_descriptive_type (die, cu);
19519
19520 if (descriptive_type)
19521 {
19522 ALLOCATE_GNAT_AUX_TYPE (type);
19523 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19524 }
19525}
19526
c906108c
SS
19527/* Return the containing type of the die in question using its
19528 DW_AT_containing_type attribute. */
19529
19530static struct type *
e7c27a73 19531die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19532{
c906108c 19533 struct attribute *type_attr;
c906108c 19534
e142c38c 19535 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19536 if (!type_attr)
19537 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19538 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19539
673bfd45 19540 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19541}
19542
ac9ec31b
DE
19543/* Return an error marker type to use for the ill formed type in DIE/CU. */
19544
19545static struct type *
19546build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19547{
19548 struct objfile *objfile = dwarf2_per_objfile->objfile;
19549 char *message, *saved;
19550
19551 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19552 objfile_name (objfile),
9c541725
PA
19553 to_underlying (cu->header.sect_off),
19554 to_underlying (die->sect_off));
224c3ddb
SM
19555 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19556 message, strlen (message));
ac9ec31b
DE
19557 xfree (message);
19558
19f392bc 19559 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19560}
19561
673bfd45 19562/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19563 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19564 DW_AT_containing_type.
673bfd45
DE
19565 If there is no type substitute an error marker. */
19566
c906108c 19567static struct type *
ff39bb5e 19568lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19569 struct dwarf2_cu *cu)
c906108c 19570{
bb5ed363 19571 struct objfile *objfile = cu->objfile;
f792889a
DJ
19572 struct type *this_type;
19573
ac9ec31b
DE
19574 gdb_assert (attr->name == DW_AT_type
19575 || attr->name == DW_AT_GNAT_descriptive_type
19576 || attr->name == DW_AT_containing_type);
19577
673bfd45
DE
19578 /* First see if we have it cached. */
19579
36586728
TT
19580 if (attr->form == DW_FORM_GNU_ref_alt)
19581 {
19582 struct dwarf2_per_cu_data *per_cu;
9c541725 19583 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19584
9c541725
PA
19585 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19586 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19587 }
7771576e 19588 else if (attr_form_is_ref (attr))
673bfd45 19589 {
9c541725 19590 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19591
9c541725 19592 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19593 }
55f1336d 19594 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19595 {
ac9ec31b 19596 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19597
ac9ec31b 19598 return get_signatured_type (die, signature, cu);
673bfd45
DE
19599 }
19600 else
19601 {
ac9ec31b
DE
19602 complaint (&symfile_complaints,
19603 _("Dwarf Error: Bad type attribute %s in DIE"
19604 " at 0x%x [in module %s]"),
9c541725 19605 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19606 objfile_name (objfile));
ac9ec31b 19607 return build_error_marker_type (cu, die);
673bfd45
DE
19608 }
19609
19610 /* If not cached we need to read it in. */
19611
19612 if (this_type == NULL)
19613 {
ac9ec31b 19614 struct die_info *type_die = NULL;
673bfd45
DE
19615 struct dwarf2_cu *type_cu = cu;
19616
7771576e 19617 if (attr_form_is_ref (attr))
ac9ec31b
DE
19618 type_die = follow_die_ref (die, attr, &type_cu);
19619 if (type_die == NULL)
19620 return build_error_marker_type (cu, die);
19621 /* If we find the type now, it's probably because the type came
3019eac3
DE
19622 from an inter-CU reference and the type's CU got expanded before
19623 ours. */
ac9ec31b 19624 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19625 }
19626
19627 /* If we still don't have a type use an error marker. */
19628
19629 if (this_type == NULL)
ac9ec31b 19630 return build_error_marker_type (cu, die);
673bfd45 19631
f792889a 19632 return this_type;
c906108c
SS
19633}
19634
673bfd45
DE
19635/* Return the type in DIE, CU.
19636 Returns NULL for invalid types.
19637
02142a6c 19638 This first does a lookup in die_type_hash,
673bfd45
DE
19639 and only reads the die in if necessary.
19640
19641 NOTE: This can be called when reading in partial or full symbols. */
19642
f792889a 19643static struct type *
e7c27a73 19644read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19645{
f792889a
DJ
19646 struct type *this_type;
19647
19648 this_type = get_die_type (die, cu);
19649 if (this_type)
19650 return this_type;
19651
673bfd45
DE
19652 return read_type_die_1 (die, cu);
19653}
19654
19655/* Read the type in DIE, CU.
19656 Returns NULL for invalid types. */
19657
19658static struct type *
19659read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19660{
19661 struct type *this_type = NULL;
19662
c906108c
SS
19663 switch (die->tag)
19664 {
19665 case DW_TAG_class_type:
680b30c7 19666 case DW_TAG_interface_type:
c906108c
SS
19667 case DW_TAG_structure_type:
19668 case DW_TAG_union_type:
f792889a 19669 this_type = read_structure_type (die, cu);
c906108c
SS
19670 break;
19671 case DW_TAG_enumeration_type:
f792889a 19672 this_type = read_enumeration_type (die, cu);
c906108c
SS
19673 break;
19674 case DW_TAG_subprogram:
19675 case DW_TAG_subroutine_type:
edb3359d 19676 case DW_TAG_inlined_subroutine:
f792889a 19677 this_type = read_subroutine_type (die, cu);
c906108c
SS
19678 break;
19679 case DW_TAG_array_type:
f792889a 19680 this_type = read_array_type (die, cu);
c906108c 19681 break;
72019c9c 19682 case DW_TAG_set_type:
f792889a 19683 this_type = read_set_type (die, cu);
72019c9c 19684 break;
c906108c 19685 case DW_TAG_pointer_type:
f792889a 19686 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19687 break;
19688 case DW_TAG_ptr_to_member_type:
f792889a 19689 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19690 break;
19691 case DW_TAG_reference_type:
4297a3f0
AV
19692 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19693 break;
19694 case DW_TAG_rvalue_reference_type:
19695 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19696 break;
19697 case DW_TAG_const_type:
f792889a 19698 this_type = read_tag_const_type (die, cu);
c906108c
SS
19699 break;
19700 case DW_TAG_volatile_type:
f792889a 19701 this_type = read_tag_volatile_type (die, cu);
c906108c 19702 break;
06d66ee9
TT
19703 case DW_TAG_restrict_type:
19704 this_type = read_tag_restrict_type (die, cu);
19705 break;
c906108c 19706 case DW_TAG_string_type:
f792889a 19707 this_type = read_tag_string_type (die, cu);
c906108c
SS
19708 break;
19709 case DW_TAG_typedef:
f792889a 19710 this_type = read_typedef (die, cu);
c906108c 19711 break;
a02abb62 19712 case DW_TAG_subrange_type:
f792889a 19713 this_type = read_subrange_type (die, cu);
a02abb62 19714 break;
c906108c 19715 case DW_TAG_base_type:
f792889a 19716 this_type = read_base_type (die, cu);
c906108c 19717 break;
81a17f79 19718 case DW_TAG_unspecified_type:
f792889a 19719 this_type = read_unspecified_type (die, cu);
81a17f79 19720 break;
0114d602
DJ
19721 case DW_TAG_namespace:
19722 this_type = read_namespace_type (die, cu);
19723 break;
f55ee35c
JK
19724 case DW_TAG_module:
19725 this_type = read_module_type (die, cu);
19726 break;
a2c2acaf
MW
19727 case DW_TAG_atomic_type:
19728 this_type = read_tag_atomic_type (die, cu);
19729 break;
c906108c 19730 default:
3e43a32a
MS
19731 complaint (&symfile_complaints,
19732 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19733 dwarf_tag_name (die->tag));
c906108c
SS
19734 break;
19735 }
63d06c5c 19736
f792889a 19737 return this_type;
63d06c5c
DC
19738}
19739
abc72ce4
DE
19740/* See if we can figure out if the class lives in a namespace. We do
19741 this by looking for a member function; its demangled name will
19742 contain namespace info, if there is any.
19743 Return the computed name or NULL.
19744 Space for the result is allocated on the objfile's obstack.
19745 This is the full-die version of guess_partial_die_structure_name.
19746 In this case we know DIE has no useful parent. */
19747
19748static char *
19749guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19750{
19751 struct die_info *spec_die;
19752 struct dwarf2_cu *spec_cu;
19753 struct die_info *child;
19754
19755 spec_cu = cu;
19756 spec_die = die_specification (die, &spec_cu);
19757 if (spec_die != NULL)
19758 {
19759 die = spec_die;
19760 cu = spec_cu;
19761 }
19762
19763 for (child = die->child;
19764 child != NULL;
19765 child = child->sibling)
19766 {
19767 if (child->tag == DW_TAG_subprogram)
19768 {
7d45c7c3 19769 const char *linkage_name;
abc72ce4 19770
7d45c7c3
KB
19771 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19772 if (linkage_name == NULL)
19773 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19774 cu);
19775 if (linkage_name != NULL)
abc72ce4
DE
19776 {
19777 char *actual_name
19778 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19779 linkage_name);
abc72ce4
DE
19780 char *name = NULL;
19781
19782 if (actual_name != NULL)
19783 {
15d034d0 19784 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19785
19786 if (die_name != NULL
19787 && strcmp (die_name, actual_name) != 0)
19788 {
19789 /* Strip off the class name from the full name.
19790 We want the prefix. */
19791 int die_name_len = strlen (die_name);
19792 int actual_name_len = strlen (actual_name);
19793
19794 /* Test for '::' as a sanity check. */
19795 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19796 && actual_name[actual_name_len
19797 - die_name_len - 1] == ':')
224c3ddb
SM
19798 name = (char *) obstack_copy0 (
19799 &cu->objfile->per_bfd->storage_obstack,
19800 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19801 }
19802 }
19803 xfree (actual_name);
19804 return name;
19805 }
19806 }
19807 }
19808
19809 return NULL;
19810}
19811
96408a79
SA
19812/* GCC might emit a nameless typedef that has a linkage name. Determine the
19813 prefix part in such case. See
19814 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19815
a121b7c1 19816static const char *
96408a79
SA
19817anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19818{
19819 struct attribute *attr;
e6a959d6 19820 const char *base;
96408a79
SA
19821
19822 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19823 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19824 return NULL;
19825
7d45c7c3 19826 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19827 return NULL;
19828
19829 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19830 if (attr == NULL)
19831 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19832 if (attr == NULL || DW_STRING (attr) == NULL)
19833 return NULL;
19834
19835 /* dwarf2_name had to be already called. */
19836 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19837
19838 /* Strip the base name, keep any leading namespaces/classes. */
19839 base = strrchr (DW_STRING (attr), ':');
19840 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19841 return "";
19842
224c3ddb
SM
19843 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19844 DW_STRING (attr),
19845 &base[-1] - DW_STRING (attr));
96408a79
SA
19846}
19847
fdde2d81 19848/* Return the name of the namespace/class that DIE is defined within,
0114d602 19849 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19850
0114d602
DJ
19851 For example, if we're within the method foo() in the following
19852 code:
19853
19854 namespace N {
19855 class C {
19856 void foo () {
19857 }
19858 };
19859 }
19860
19861 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19862
0d5cff50 19863static const char *
e142c38c 19864determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19865{
0114d602
DJ
19866 struct die_info *parent, *spec_die;
19867 struct dwarf2_cu *spec_cu;
19868 struct type *parent_type;
a121b7c1 19869 const char *retval;
63d06c5c 19870
9c37b5ae 19871 if (cu->language != language_cplus
c44af4eb
TT
19872 && cu->language != language_fortran && cu->language != language_d
19873 && cu->language != language_rust)
0114d602
DJ
19874 return "";
19875
96408a79
SA
19876 retval = anonymous_struct_prefix (die, cu);
19877 if (retval)
19878 return retval;
19879
0114d602
DJ
19880 /* We have to be careful in the presence of DW_AT_specification.
19881 For example, with GCC 3.4, given the code
19882
19883 namespace N {
19884 void foo() {
19885 // Definition of N::foo.
19886 }
19887 }
19888
19889 then we'll have a tree of DIEs like this:
19890
19891 1: DW_TAG_compile_unit
19892 2: DW_TAG_namespace // N
19893 3: DW_TAG_subprogram // declaration of N::foo
19894 4: DW_TAG_subprogram // definition of N::foo
19895 DW_AT_specification // refers to die #3
19896
19897 Thus, when processing die #4, we have to pretend that we're in
19898 the context of its DW_AT_specification, namely the contex of die
19899 #3. */
19900 spec_cu = cu;
19901 spec_die = die_specification (die, &spec_cu);
19902 if (spec_die == NULL)
19903 parent = die->parent;
19904 else
63d06c5c 19905 {
0114d602
DJ
19906 parent = spec_die->parent;
19907 cu = spec_cu;
63d06c5c 19908 }
0114d602
DJ
19909
19910 if (parent == NULL)
19911 return "";
98bfdba5
PA
19912 else if (parent->building_fullname)
19913 {
19914 const char *name;
19915 const char *parent_name;
19916
19917 /* It has been seen on RealView 2.2 built binaries,
19918 DW_TAG_template_type_param types actually _defined_ as
19919 children of the parent class:
19920
19921 enum E {};
19922 template class <class Enum> Class{};
19923 Class<enum E> class_e;
19924
19925 1: DW_TAG_class_type (Class)
19926 2: DW_TAG_enumeration_type (E)
19927 3: DW_TAG_enumerator (enum1:0)
19928 3: DW_TAG_enumerator (enum2:1)
19929 ...
19930 2: DW_TAG_template_type_param
19931 DW_AT_type DW_FORM_ref_udata (E)
19932
19933 Besides being broken debug info, it can put GDB into an
19934 infinite loop. Consider:
19935
19936 When we're building the full name for Class<E>, we'll start
19937 at Class, and go look over its template type parameters,
19938 finding E. We'll then try to build the full name of E, and
19939 reach here. We're now trying to build the full name of E,
19940 and look over the parent DIE for containing scope. In the
19941 broken case, if we followed the parent DIE of E, we'd again
19942 find Class, and once again go look at its template type
19943 arguments, etc., etc. Simply don't consider such parent die
19944 as source-level parent of this die (it can't be, the language
19945 doesn't allow it), and break the loop here. */
19946 name = dwarf2_name (die, cu);
19947 parent_name = dwarf2_name (parent, cu);
19948 complaint (&symfile_complaints,
19949 _("template param type '%s' defined within parent '%s'"),
19950 name ? name : "<unknown>",
19951 parent_name ? parent_name : "<unknown>");
19952 return "";
19953 }
63d06c5c 19954 else
0114d602
DJ
19955 switch (parent->tag)
19956 {
63d06c5c 19957 case DW_TAG_namespace:
0114d602 19958 parent_type = read_type_die (parent, cu);
acebe513
UW
19959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19961 Work around this problem here. */
19962 if (cu->language == language_cplus
19963 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19964 return "";
0114d602
DJ
19965 /* We give a name to even anonymous namespaces. */
19966 return TYPE_TAG_NAME (parent_type);
63d06c5c 19967 case DW_TAG_class_type:
680b30c7 19968 case DW_TAG_interface_type:
63d06c5c 19969 case DW_TAG_structure_type:
0114d602 19970 case DW_TAG_union_type:
f55ee35c 19971 case DW_TAG_module:
0114d602
DJ
19972 parent_type = read_type_die (parent, cu);
19973 if (TYPE_TAG_NAME (parent_type) != NULL)
19974 return TYPE_TAG_NAME (parent_type);
19975 else
19976 /* An anonymous structure is only allowed non-static data
19977 members; no typedefs, no member functions, et cetera.
19978 So it does not need a prefix. */
19979 return "";
abc72ce4 19980 case DW_TAG_compile_unit:
95554aad 19981 case DW_TAG_partial_unit:
abc72ce4
DE
19982 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19983 if (cu->language == language_cplus
8b70b953 19984 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19985 && die->child != NULL
19986 && (die->tag == DW_TAG_class_type
19987 || die->tag == DW_TAG_structure_type
19988 || die->tag == DW_TAG_union_type))
19989 {
19990 char *name = guess_full_die_structure_name (die, cu);
19991 if (name != NULL)
19992 return name;
19993 }
19994 return "";
3d567982
TT
19995 case DW_TAG_enumeration_type:
19996 parent_type = read_type_die (parent, cu);
19997 if (TYPE_DECLARED_CLASS (parent_type))
19998 {
19999 if (TYPE_TAG_NAME (parent_type) != NULL)
20000 return TYPE_TAG_NAME (parent_type);
20001 return "";
20002 }
20003 /* Fall through. */
63d06c5c 20004 default:
8176b9b8 20005 return determine_prefix (parent, cu);
63d06c5c 20006 }
63d06c5c
DC
20007}
20008
3e43a32a
MS
20009/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20010 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20011 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20012 an obconcat, otherwise allocate storage for the result. The CU argument is
20013 used to determine the language and hence, the appropriate separator. */
987504bb 20014
f55ee35c 20015#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20016
20017static char *
f55ee35c
JK
20018typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20019 int physname, struct dwarf2_cu *cu)
63d06c5c 20020{
f55ee35c 20021 const char *lead = "";
5c315b68 20022 const char *sep;
63d06c5c 20023
3e43a32a
MS
20024 if (suffix == NULL || suffix[0] == '\0'
20025 || prefix == NULL || prefix[0] == '\0')
987504bb 20026 sep = "";
45280282
IB
20027 else if (cu->language == language_d)
20028 {
20029 /* For D, the 'main' function could be defined in any module, but it
20030 should never be prefixed. */
20031 if (strcmp (suffix, "D main") == 0)
20032 {
20033 prefix = "";
20034 sep = "";
20035 }
20036 else
20037 sep = ".";
20038 }
f55ee35c
JK
20039 else if (cu->language == language_fortran && physname)
20040 {
20041 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20042 DW_AT_MIPS_linkage_name is preferred and used instead. */
20043
20044 lead = "__";
20045 sep = "_MOD_";
20046 }
987504bb
JJ
20047 else
20048 sep = "::";
63d06c5c 20049
6dd47d34
DE
20050 if (prefix == NULL)
20051 prefix = "";
20052 if (suffix == NULL)
20053 suffix = "";
20054
987504bb
JJ
20055 if (obs == NULL)
20056 {
3e43a32a 20057 char *retval
224c3ddb
SM
20058 = ((char *)
20059 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20060
f55ee35c
JK
20061 strcpy (retval, lead);
20062 strcat (retval, prefix);
6dd47d34
DE
20063 strcat (retval, sep);
20064 strcat (retval, suffix);
63d06c5c
DC
20065 return retval;
20066 }
987504bb
JJ
20067 else
20068 {
20069 /* We have an obstack. */
f55ee35c 20070 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20071 }
63d06c5c
DC
20072}
20073
c906108c
SS
20074/* Return sibling of die, NULL if no sibling. */
20075
f9aca02d 20076static struct die_info *
fba45db2 20077sibling_die (struct die_info *die)
c906108c 20078{
639d11d3 20079 return die->sibling;
c906108c
SS
20080}
20081
71c25dea
TT
20082/* Get name of a die, return NULL if not found. */
20083
15d034d0
TT
20084static const char *
20085dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20086 struct obstack *obstack)
20087{
20088 if (name && cu->language == language_cplus)
20089 {
2f408ecb 20090 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20091
2f408ecb 20092 if (!canon_name.empty ())
71c25dea 20093 {
2f408ecb
PA
20094 if (canon_name != name)
20095 name = (const char *) obstack_copy0 (obstack,
20096 canon_name.c_str (),
20097 canon_name.length ());
71c25dea
TT
20098 }
20099 }
20100
20101 return name;
c906108c
SS
20102}
20103
96553a0c
DE
20104/* Get name of a die, return NULL if not found.
20105 Anonymous namespaces are converted to their magic string. */
9219021c 20106
15d034d0 20107static const char *
e142c38c 20108dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20109{
20110 struct attribute *attr;
20111
e142c38c 20112 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20113 if ((!attr || !DW_STRING (attr))
96553a0c 20114 && die->tag != DW_TAG_namespace
53832f31
TT
20115 && die->tag != DW_TAG_class_type
20116 && die->tag != DW_TAG_interface_type
20117 && die->tag != DW_TAG_structure_type
20118 && die->tag != DW_TAG_union_type)
71c25dea
TT
20119 return NULL;
20120
20121 switch (die->tag)
20122 {
20123 case DW_TAG_compile_unit:
95554aad 20124 case DW_TAG_partial_unit:
71c25dea
TT
20125 /* Compilation units have a DW_AT_name that is a filename, not
20126 a source language identifier. */
20127 case DW_TAG_enumeration_type:
20128 case DW_TAG_enumerator:
20129 /* These tags always have simple identifiers already; no need
20130 to canonicalize them. */
20131 return DW_STRING (attr);
907af001 20132
96553a0c
DE
20133 case DW_TAG_namespace:
20134 if (attr != NULL && DW_STRING (attr) != NULL)
20135 return DW_STRING (attr);
20136 return CP_ANONYMOUS_NAMESPACE_STR;
20137
907af001
UW
20138 case DW_TAG_class_type:
20139 case DW_TAG_interface_type:
20140 case DW_TAG_structure_type:
20141 case DW_TAG_union_type:
20142 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20143 structures or unions. These were of the form "._%d" in GCC 4.1,
20144 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20145 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20146 if (attr && DW_STRING (attr)
61012eef
GB
20147 && (startswith (DW_STRING (attr), "._")
20148 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20149 return NULL;
53832f31
TT
20150
20151 /* GCC might emit a nameless typedef that has a linkage name. See
20152 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20153 if (!attr || DW_STRING (attr) == NULL)
20154 {
df5c6c50 20155 char *demangled = NULL;
53832f31
TT
20156
20157 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20158 if (attr == NULL)
20159 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20160
20161 if (attr == NULL || DW_STRING (attr) == NULL)
20162 return NULL;
20163
df5c6c50
JK
20164 /* Avoid demangling DW_STRING (attr) the second time on a second
20165 call for the same DIE. */
20166 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20167 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20168
20169 if (demangled)
20170 {
e6a959d6 20171 const char *base;
96408a79 20172
53832f31 20173 /* FIXME: we already did this for the partial symbol... */
34a68019 20174 DW_STRING (attr)
224c3ddb
SM
20175 = ((const char *)
20176 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20177 demangled, strlen (demangled)));
53832f31
TT
20178 DW_STRING_IS_CANONICAL (attr) = 1;
20179 xfree (demangled);
96408a79
SA
20180
20181 /* Strip any leading namespaces/classes, keep only the base name.
20182 DW_AT_name for named DIEs does not contain the prefixes. */
20183 base = strrchr (DW_STRING (attr), ':');
20184 if (base && base > DW_STRING (attr) && base[-1] == ':')
20185 return &base[1];
20186 else
20187 return DW_STRING (attr);
53832f31
TT
20188 }
20189 }
907af001
UW
20190 break;
20191
71c25dea 20192 default:
907af001
UW
20193 break;
20194 }
20195
20196 if (!DW_STRING_IS_CANONICAL (attr))
20197 {
20198 DW_STRING (attr)
20199 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20200 &cu->objfile->per_bfd->storage_obstack);
907af001 20201 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20202 }
907af001 20203 return DW_STRING (attr);
9219021c
DC
20204}
20205
20206/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20207 is none. *EXT_CU is the CU containing DIE on input, and the CU
20208 containing the return value on output. */
9219021c
DC
20209
20210static struct die_info *
f2f0e013 20211dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20212{
20213 struct attribute *attr;
9219021c 20214
f2f0e013 20215 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20216 if (attr == NULL)
20217 return NULL;
20218
f2f0e013 20219 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20220}
20221
c906108c
SS
20222/* Convert a DIE tag into its string name. */
20223
f39c6ffd 20224static const char *
aa1ee363 20225dwarf_tag_name (unsigned tag)
c906108c 20226{
f39c6ffd
TT
20227 const char *name = get_DW_TAG_name (tag);
20228
20229 if (name == NULL)
20230 return "DW_TAG_<unknown>";
20231
20232 return name;
c906108c
SS
20233}
20234
20235/* Convert a DWARF attribute code into its string name. */
20236
f39c6ffd 20237static const char *
aa1ee363 20238dwarf_attr_name (unsigned attr)
c906108c 20239{
f39c6ffd
TT
20240 const char *name;
20241
c764a876 20242#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20243 if (attr == DW_AT_MIPS_fde)
20244 return "DW_AT_MIPS_fde";
20245#else
20246 if (attr == DW_AT_HP_block_index)
20247 return "DW_AT_HP_block_index";
c764a876 20248#endif
f39c6ffd
TT
20249
20250 name = get_DW_AT_name (attr);
20251
20252 if (name == NULL)
20253 return "DW_AT_<unknown>";
20254
20255 return name;
c906108c
SS
20256}
20257
20258/* Convert a DWARF value form code into its string name. */
20259
f39c6ffd 20260static const char *
aa1ee363 20261dwarf_form_name (unsigned form)
c906108c 20262{
f39c6ffd
TT
20263 const char *name = get_DW_FORM_name (form);
20264
20265 if (name == NULL)
20266 return "DW_FORM_<unknown>";
20267
20268 return name;
c906108c
SS
20269}
20270
a121b7c1 20271static const char *
fba45db2 20272dwarf_bool_name (unsigned mybool)
c906108c
SS
20273{
20274 if (mybool)
20275 return "TRUE";
20276 else
20277 return "FALSE";
20278}
20279
20280/* Convert a DWARF type code into its string name. */
20281
f39c6ffd 20282static const char *
aa1ee363 20283dwarf_type_encoding_name (unsigned enc)
c906108c 20284{
f39c6ffd 20285 const char *name = get_DW_ATE_name (enc);
c906108c 20286
f39c6ffd
TT
20287 if (name == NULL)
20288 return "DW_ATE_<unknown>";
c906108c 20289
f39c6ffd 20290 return name;
c906108c 20291}
c906108c 20292
f9aca02d 20293static void
d97bc12b 20294dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20295{
20296 unsigned int i;
20297
d97bc12b
DE
20298 print_spaces (indent, f);
20299 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20300 dwarf_tag_name (die->tag), die->abbrev,
20301 to_underlying (die->sect_off));
d97bc12b
DE
20302
20303 if (die->parent != NULL)
20304 {
20305 print_spaces (indent, f);
20306 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20307 to_underlying (die->parent->sect_off));
d97bc12b
DE
20308 }
20309
20310 print_spaces (indent, f);
20311 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20312 dwarf_bool_name (die->child != NULL));
c906108c 20313
d97bc12b
DE
20314 print_spaces (indent, f);
20315 fprintf_unfiltered (f, " attributes:\n");
20316
c906108c
SS
20317 for (i = 0; i < die->num_attrs; ++i)
20318 {
d97bc12b
DE
20319 print_spaces (indent, f);
20320 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20321 dwarf_attr_name (die->attrs[i].name),
20322 dwarf_form_name (die->attrs[i].form));
d97bc12b 20323
c906108c
SS
20324 switch (die->attrs[i].form)
20325 {
c906108c 20326 case DW_FORM_addr:
3019eac3 20327 case DW_FORM_GNU_addr_index:
d97bc12b 20328 fprintf_unfiltered (f, "address: ");
5af949e3 20329 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20330 break;
20331 case DW_FORM_block2:
20332 case DW_FORM_block4:
20333 case DW_FORM_block:
20334 case DW_FORM_block1:
56eb65bd
SP
20335 fprintf_unfiltered (f, "block: size %s",
20336 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20337 break;
2dc7f7b3 20338 case DW_FORM_exprloc:
56eb65bd
SP
20339 fprintf_unfiltered (f, "expression: size %s",
20340 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20341 break;
0224619f
JK
20342 case DW_FORM_data16:
20343 fprintf_unfiltered (f, "constant of 16 bytes");
20344 break;
4568ecf9
DE
20345 case DW_FORM_ref_addr:
20346 fprintf_unfiltered (f, "ref address: ");
20347 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20348 break;
36586728
TT
20349 case DW_FORM_GNU_ref_alt:
20350 fprintf_unfiltered (f, "alt ref address: ");
20351 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20352 break;
10b3939b
DJ
20353 case DW_FORM_ref1:
20354 case DW_FORM_ref2:
20355 case DW_FORM_ref4:
4568ecf9
DE
20356 case DW_FORM_ref8:
20357 case DW_FORM_ref_udata:
d97bc12b 20358 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20359 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20360 break;
c906108c
SS
20361 case DW_FORM_data1:
20362 case DW_FORM_data2:
20363 case DW_FORM_data4:
ce5d95e1 20364 case DW_FORM_data8:
c906108c
SS
20365 case DW_FORM_udata:
20366 case DW_FORM_sdata:
43bbcdc2
PH
20367 fprintf_unfiltered (f, "constant: %s",
20368 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20369 break;
2dc7f7b3
TT
20370 case DW_FORM_sec_offset:
20371 fprintf_unfiltered (f, "section offset: %s",
20372 pulongest (DW_UNSND (&die->attrs[i])));
20373 break;
55f1336d 20374 case DW_FORM_ref_sig8:
ac9ec31b
DE
20375 fprintf_unfiltered (f, "signature: %s",
20376 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20377 break;
c906108c 20378 case DW_FORM_string:
4bdf3d34 20379 case DW_FORM_strp:
43988095 20380 case DW_FORM_line_strp:
3019eac3 20381 case DW_FORM_GNU_str_index:
36586728 20382 case DW_FORM_GNU_strp_alt:
8285870a 20383 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20384 DW_STRING (&die->attrs[i])
8285870a
JK
20385 ? DW_STRING (&die->attrs[i]) : "",
20386 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20387 break;
20388 case DW_FORM_flag:
20389 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20390 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20391 else
d97bc12b 20392 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20393 break;
2dc7f7b3
TT
20394 case DW_FORM_flag_present:
20395 fprintf_unfiltered (f, "flag: TRUE");
20396 break;
a8329558 20397 case DW_FORM_indirect:
0963b4bd
MS
20398 /* The reader will have reduced the indirect form to
20399 the "base form" so this form should not occur. */
3e43a32a
MS
20400 fprintf_unfiltered (f,
20401 "unexpected attribute form: DW_FORM_indirect");
a8329558 20402 break;
c906108c 20403 default:
d97bc12b 20404 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20405 die->attrs[i].form);
d97bc12b 20406 break;
c906108c 20407 }
d97bc12b 20408 fprintf_unfiltered (f, "\n");
c906108c
SS
20409 }
20410}
20411
f9aca02d 20412static void
d97bc12b 20413dump_die_for_error (struct die_info *die)
c906108c 20414{
d97bc12b
DE
20415 dump_die_shallow (gdb_stderr, 0, die);
20416}
20417
20418static void
20419dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20420{
20421 int indent = level * 4;
20422
20423 gdb_assert (die != NULL);
20424
20425 if (level >= max_level)
20426 return;
20427
20428 dump_die_shallow (f, indent, die);
20429
20430 if (die->child != NULL)
c906108c 20431 {
d97bc12b
DE
20432 print_spaces (indent, f);
20433 fprintf_unfiltered (f, " Children:");
20434 if (level + 1 < max_level)
20435 {
20436 fprintf_unfiltered (f, "\n");
20437 dump_die_1 (f, level + 1, max_level, die->child);
20438 }
20439 else
20440 {
3e43a32a
MS
20441 fprintf_unfiltered (f,
20442 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20443 }
20444 }
20445
20446 if (die->sibling != NULL && level > 0)
20447 {
20448 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20449 }
20450}
20451
d97bc12b
DE
20452/* This is called from the pdie macro in gdbinit.in.
20453 It's not static so gcc will keep a copy callable from gdb. */
20454
20455void
20456dump_die (struct die_info *die, int max_level)
20457{
20458 dump_die_1 (gdb_stdlog, 0, max_level, die);
20459}
20460
f9aca02d 20461static void
51545339 20462store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20463{
51545339 20464 void **slot;
c906108c 20465
9c541725
PA
20466 slot = htab_find_slot_with_hash (cu->die_hash, die,
20467 to_underlying (die->sect_off),
b64f50a1 20468 INSERT);
51545339
DJ
20469
20470 *slot = die;
c906108c
SS
20471}
20472
b64f50a1
JK
20473/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20474 required kind. */
20475
20476static sect_offset
ff39bb5e 20477dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20478{
7771576e 20479 if (attr_form_is_ref (attr))
9c541725 20480 return (sect_offset) DW_UNSND (attr);
93311388
DE
20481
20482 complaint (&symfile_complaints,
20483 _("unsupported die ref attribute form: '%s'"),
20484 dwarf_form_name (attr->form));
9c541725 20485 return {};
c906108c
SS
20486}
20487
43bbcdc2
PH
20488/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20489 * the value held by the attribute is not constant. */
a02abb62 20490
43bbcdc2 20491static LONGEST
ff39bb5e 20492dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20493{
20494 if (attr->form == DW_FORM_sdata)
20495 return DW_SND (attr);
20496 else if (attr->form == DW_FORM_udata
20497 || attr->form == DW_FORM_data1
20498 || attr->form == DW_FORM_data2
20499 || attr->form == DW_FORM_data4
20500 || attr->form == DW_FORM_data8)
20501 return DW_UNSND (attr);
20502 else
20503 {
0224619f 20504 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20505 complaint (&symfile_complaints,
20506 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20507 dwarf_form_name (attr->form));
20508 return default_value;
20509 }
20510}
20511
348e048f
DE
20512/* Follow reference or signature attribute ATTR of SRC_DIE.
20513 On entry *REF_CU is the CU of SRC_DIE.
20514 On exit *REF_CU is the CU of the result. */
20515
20516static struct die_info *
ff39bb5e 20517follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20518 struct dwarf2_cu **ref_cu)
20519{
20520 struct die_info *die;
20521
7771576e 20522 if (attr_form_is_ref (attr))
348e048f 20523 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20524 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20525 die = follow_die_sig (src_die, attr, ref_cu);
20526 else
20527 {
20528 dump_die_for_error (src_die);
20529 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20530 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20531 }
20532
20533 return die;
03dd20cc
DJ
20534}
20535
5c631832 20536/* Follow reference OFFSET.
673bfd45
DE
20537 On entry *REF_CU is the CU of the source die referencing OFFSET.
20538 On exit *REF_CU is the CU of the result.
20539 Returns NULL if OFFSET is invalid. */
f504f079 20540
f9aca02d 20541static struct die_info *
9c541725 20542follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20543 struct dwarf2_cu **ref_cu)
c906108c 20544{
10b3939b 20545 struct die_info temp_die;
f2f0e013 20546 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20547
348e048f
DE
20548 gdb_assert (cu->per_cu != NULL);
20549
98bfdba5
PA
20550 target_cu = cu;
20551
3019eac3 20552 if (cu->per_cu->is_debug_types)
348e048f
DE
20553 {
20554 /* .debug_types CUs cannot reference anything outside their CU.
20555 If they need to, they have to reference a signatured type via
55f1336d 20556 DW_FORM_ref_sig8. */
9c541725 20557 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20558 return NULL;
348e048f 20559 }
36586728 20560 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20561 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20562 {
20563 struct dwarf2_per_cu_data *per_cu;
9a619af0 20564
9c541725 20565 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20566 cu->objfile);
03dd20cc
DJ
20567
20568 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20569 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20570 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20571
10b3939b
DJ
20572 target_cu = per_cu->cu;
20573 }
98bfdba5
PA
20574 else if (cu->dies == NULL)
20575 {
20576 /* We're loading full DIEs during partial symbol reading. */
20577 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20578 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20579 }
c906108c 20580
f2f0e013 20581 *ref_cu = target_cu;
9c541725 20582 temp_die.sect_off = sect_off;
9a3c8263 20583 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20584 &temp_die,
20585 to_underlying (sect_off));
5c631832 20586}
10b3939b 20587
5c631832
JK
20588/* Follow reference attribute ATTR of SRC_DIE.
20589 On entry *REF_CU is the CU of SRC_DIE.
20590 On exit *REF_CU is the CU of the result. */
20591
20592static struct die_info *
ff39bb5e 20593follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20594 struct dwarf2_cu **ref_cu)
20595{
9c541725 20596 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20597 struct dwarf2_cu *cu = *ref_cu;
20598 struct die_info *die;
20599
9c541725 20600 die = follow_die_offset (sect_off,
36586728
TT
20601 (attr->form == DW_FORM_GNU_ref_alt
20602 || cu->per_cu->is_dwz),
20603 ref_cu);
5c631832
JK
20604 if (!die)
20605 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20606 "at 0x%x [in module %s]"),
9c541725 20607 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20608 objfile_name (cu->objfile));
348e048f 20609
5c631832
JK
20610 return die;
20611}
20612
9c541725 20613/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20614 Returned value is intended for DW_OP_call*. Returned
20615 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20616
20617struct dwarf2_locexpr_baton
9c541725 20618dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20619 struct dwarf2_per_cu_data *per_cu,
20620 CORE_ADDR (*get_frame_pc) (void *baton),
20621 void *baton)
5c631832 20622{
918dd910 20623 struct dwarf2_cu *cu;
5c631832
JK
20624 struct die_info *die;
20625 struct attribute *attr;
20626 struct dwarf2_locexpr_baton retval;
20627
8cf6f0b1
TT
20628 dw2_setup (per_cu->objfile);
20629
918dd910
JK
20630 if (per_cu->cu == NULL)
20631 load_cu (per_cu);
20632 cu = per_cu->cu;
cc12ce38
DE
20633 if (cu == NULL)
20634 {
20635 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20636 Instead just throw an error, not much else we can do. */
20637 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20638 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20639 }
918dd910 20640
9c541725 20641 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20642 if (!die)
20643 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20644 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20645
20646 attr = dwarf2_attr (die, DW_AT_location, cu);
20647 if (!attr)
20648 {
e103e986
JK
20649 /* DWARF: "If there is no such attribute, then there is no effect.".
20650 DATA is ignored if SIZE is 0. */
5c631832 20651
e103e986 20652 retval.data = NULL;
5c631832
JK
20653 retval.size = 0;
20654 }
8cf6f0b1
TT
20655 else if (attr_form_is_section_offset (attr))
20656 {
20657 struct dwarf2_loclist_baton loclist_baton;
20658 CORE_ADDR pc = (*get_frame_pc) (baton);
20659 size_t size;
20660
20661 fill_in_loclist_baton (cu, &loclist_baton, attr);
20662
20663 retval.data = dwarf2_find_location_expression (&loclist_baton,
20664 &size, pc);
20665 retval.size = size;
20666 }
5c631832
JK
20667 else
20668 {
20669 if (!attr_form_is_block (attr))
20670 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20671 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20672 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20673
20674 retval.data = DW_BLOCK (attr)->data;
20675 retval.size = DW_BLOCK (attr)->size;
20676 }
20677 retval.per_cu = cu->per_cu;
918dd910 20678
918dd910
JK
20679 age_cached_comp_units ();
20680
5c631832 20681 return retval;
348e048f
DE
20682}
20683
8b9737bf
TT
20684/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20685 offset. */
20686
20687struct dwarf2_locexpr_baton
20688dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20689 struct dwarf2_per_cu_data *per_cu,
20690 CORE_ADDR (*get_frame_pc) (void *baton),
20691 void *baton)
20692{
9c541725 20693 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20694
9c541725 20695 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20696}
20697
b6807d98
TT
20698/* Write a constant of a given type as target-ordered bytes into
20699 OBSTACK. */
20700
20701static const gdb_byte *
20702write_constant_as_bytes (struct obstack *obstack,
20703 enum bfd_endian byte_order,
20704 struct type *type,
20705 ULONGEST value,
20706 LONGEST *len)
20707{
20708 gdb_byte *result;
20709
20710 *len = TYPE_LENGTH (type);
224c3ddb 20711 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20712 store_unsigned_integer (result, *len, byte_order, value);
20713
20714 return result;
20715}
20716
20717/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20718 pointer to the constant bytes and set LEN to the length of the
20719 data. If memory is needed, allocate it on OBSTACK. If the DIE
20720 does not have a DW_AT_const_value, return NULL. */
20721
20722const gdb_byte *
9c541725 20723dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20724 struct dwarf2_per_cu_data *per_cu,
20725 struct obstack *obstack,
20726 LONGEST *len)
20727{
20728 struct dwarf2_cu *cu;
20729 struct die_info *die;
20730 struct attribute *attr;
20731 const gdb_byte *result = NULL;
20732 struct type *type;
20733 LONGEST value;
20734 enum bfd_endian byte_order;
20735
20736 dw2_setup (per_cu->objfile);
20737
20738 if (per_cu->cu == NULL)
20739 load_cu (per_cu);
20740 cu = per_cu->cu;
cc12ce38
DE
20741 if (cu == NULL)
20742 {
20743 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20744 Instead just throw an error, not much else we can do. */
20745 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20746 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20747 }
b6807d98 20748
9c541725 20749 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20750 if (!die)
20751 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20752 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20753
20754
20755 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20756 if (attr == NULL)
20757 return NULL;
20758
20759 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20760 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20761
20762 switch (attr->form)
20763 {
20764 case DW_FORM_addr:
20765 case DW_FORM_GNU_addr_index:
20766 {
20767 gdb_byte *tem;
20768
20769 *len = cu->header.addr_size;
224c3ddb 20770 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20771 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20772 result = tem;
20773 }
20774 break;
20775 case DW_FORM_string:
20776 case DW_FORM_strp:
20777 case DW_FORM_GNU_str_index:
20778 case DW_FORM_GNU_strp_alt:
20779 /* DW_STRING is already allocated on the objfile obstack, point
20780 directly to it. */
20781 result = (const gdb_byte *) DW_STRING (attr);
20782 *len = strlen (DW_STRING (attr));
20783 break;
20784 case DW_FORM_block1:
20785 case DW_FORM_block2:
20786 case DW_FORM_block4:
20787 case DW_FORM_block:
20788 case DW_FORM_exprloc:
0224619f 20789 case DW_FORM_data16:
b6807d98
TT
20790 result = DW_BLOCK (attr)->data;
20791 *len = DW_BLOCK (attr)->size;
20792 break;
20793
20794 /* The DW_AT_const_value attributes are supposed to carry the
20795 symbol's value "represented as it would be on the target
20796 architecture." By the time we get here, it's already been
20797 converted to host endianness, so we just need to sign- or
20798 zero-extend it as appropriate. */
20799 case DW_FORM_data1:
20800 type = die_type (die, cu);
20801 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20802 if (result == NULL)
20803 result = write_constant_as_bytes (obstack, byte_order,
20804 type, value, len);
20805 break;
20806 case DW_FORM_data2:
20807 type = die_type (die, cu);
20808 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20809 if (result == NULL)
20810 result = write_constant_as_bytes (obstack, byte_order,
20811 type, value, len);
20812 break;
20813 case DW_FORM_data4:
20814 type = die_type (die, cu);
20815 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20816 if (result == NULL)
20817 result = write_constant_as_bytes (obstack, byte_order,
20818 type, value, len);
20819 break;
20820 case DW_FORM_data8:
20821 type = die_type (die, cu);
20822 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20823 if (result == NULL)
20824 result = write_constant_as_bytes (obstack, byte_order,
20825 type, value, len);
20826 break;
20827
20828 case DW_FORM_sdata:
20829 type = die_type (die, cu);
20830 result = write_constant_as_bytes (obstack, byte_order,
20831 type, DW_SND (attr), len);
20832 break;
20833
20834 case DW_FORM_udata:
20835 type = die_type (die, cu);
20836 result = write_constant_as_bytes (obstack, byte_order,
20837 type, DW_UNSND (attr), len);
20838 break;
20839
20840 default:
20841 complaint (&symfile_complaints,
20842 _("unsupported const value attribute form: '%s'"),
20843 dwarf_form_name (attr->form));
20844 break;
20845 }
20846
20847 return result;
20848}
20849
7942e96e
AA
20850/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20851 valid type for this die is found. */
20852
20853struct type *
9c541725 20854dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
20855 struct dwarf2_per_cu_data *per_cu)
20856{
20857 struct dwarf2_cu *cu;
20858 struct die_info *die;
20859
20860 dw2_setup (per_cu->objfile);
20861
20862 if (per_cu->cu == NULL)
20863 load_cu (per_cu);
20864 cu = per_cu->cu;
20865 if (!cu)
20866 return NULL;
20867
9c541725 20868 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
20869 if (!die)
20870 return NULL;
20871
20872 return die_type (die, cu);
20873}
20874
8a9b8146
TT
20875/* Return the type of the DIE at DIE_OFFSET in the CU named by
20876 PER_CU. */
20877
20878struct type *
b64f50a1 20879dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20880 struct dwarf2_per_cu_data *per_cu)
20881{
8a9b8146 20882 dw2_setup (per_cu->objfile);
b64f50a1 20883
9c541725 20884 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 20885 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20886}
20887
ac9ec31b 20888/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20889 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20890 On exit *REF_CU is the CU of the result.
20891 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20892
20893static struct die_info *
ac9ec31b
DE
20894follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20895 struct dwarf2_cu **ref_cu)
348e048f 20896{
348e048f 20897 struct die_info temp_die;
348e048f
DE
20898 struct dwarf2_cu *sig_cu;
20899 struct die_info *die;
20900
ac9ec31b
DE
20901 /* While it might be nice to assert sig_type->type == NULL here,
20902 we can get here for DW_AT_imported_declaration where we need
20903 the DIE not the type. */
348e048f
DE
20904
20905 /* If necessary, add it to the queue and load its DIEs. */
20906
95554aad 20907 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20908 read_signatured_type (sig_type);
348e048f 20909
348e048f 20910 sig_cu = sig_type->per_cu.cu;
69d751e3 20911 gdb_assert (sig_cu != NULL);
9c541725
PA
20912 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20913 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 20914 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 20915 to_underlying (temp_die.sect_off));
348e048f
DE
20916 if (die)
20917 {
796a7ff8
DE
20918 /* For .gdb_index version 7 keep track of included TUs.
20919 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20920 if (dwarf2_per_objfile->index_table != NULL
20921 && dwarf2_per_objfile->index_table->version <= 7)
20922 {
20923 VEC_safe_push (dwarf2_per_cu_ptr,
20924 (*ref_cu)->per_cu->imported_symtabs,
20925 sig_cu->per_cu);
20926 }
20927
348e048f
DE
20928 *ref_cu = sig_cu;
20929 return die;
20930 }
20931
ac9ec31b
DE
20932 return NULL;
20933}
20934
20935/* Follow signatured type referenced by ATTR in SRC_DIE.
20936 On entry *REF_CU is the CU of SRC_DIE.
20937 On exit *REF_CU is the CU of the result.
20938 The result is the DIE of the type.
20939 If the referenced type cannot be found an error is thrown. */
20940
20941static struct die_info *
ff39bb5e 20942follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20943 struct dwarf2_cu **ref_cu)
20944{
20945 ULONGEST signature = DW_SIGNATURE (attr);
20946 struct signatured_type *sig_type;
20947 struct die_info *die;
20948
20949 gdb_assert (attr->form == DW_FORM_ref_sig8);
20950
a2ce51a0 20951 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20952 /* sig_type will be NULL if the signatured type is missing from
20953 the debug info. */
20954 if (sig_type == NULL)
20955 {
20956 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20957 " from DIE at 0x%x [in module %s]"),
9c541725 20958 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20959 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20960 }
20961
20962 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20963 if (die == NULL)
20964 {
20965 dump_die_for_error (src_die);
20966 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20967 " from DIE at 0x%x [in module %s]"),
9c541725 20968 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20969 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20970 }
20971
20972 return die;
20973}
20974
20975/* Get the type specified by SIGNATURE referenced in DIE/CU,
20976 reading in and processing the type unit if necessary. */
20977
20978static struct type *
20979get_signatured_type (struct die_info *die, ULONGEST signature,
20980 struct dwarf2_cu *cu)
20981{
20982 struct signatured_type *sig_type;
20983 struct dwarf2_cu *type_cu;
20984 struct die_info *type_die;
20985 struct type *type;
20986
a2ce51a0 20987 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20988 /* sig_type will be NULL if the signatured type is missing from
20989 the debug info. */
20990 if (sig_type == NULL)
20991 {
20992 complaint (&symfile_complaints,
20993 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20994 " from DIE at 0x%x [in module %s]"),
9c541725 20995 hex_string (signature), to_underlying (die->sect_off),
4262abfb 20996 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20997 return build_error_marker_type (cu, die);
20998 }
20999
21000 /* If we already know the type we're done. */
21001 if (sig_type->type != NULL)
21002 return sig_type->type;
21003
21004 type_cu = cu;
21005 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21006 if (type_die != NULL)
21007 {
21008 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21009 is created. This is important, for example, because for c++ classes
21010 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21011 type = read_type_die (type_die, type_cu);
21012 if (type == NULL)
21013 {
21014 complaint (&symfile_complaints,
21015 _("Dwarf Error: Cannot build signatured type %s"
21016 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21017 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21018 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21019 type = build_error_marker_type (cu, die);
21020 }
21021 }
21022 else
21023 {
21024 complaint (&symfile_complaints,
21025 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21026 " from DIE at 0x%x [in module %s]"),
9c541725 21027 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21028 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21029 type = build_error_marker_type (cu, die);
21030 }
21031 sig_type->type = type;
21032
21033 return type;
21034}
21035
21036/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21037 reading in and processing the type unit if necessary. */
21038
21039static struct type *
ff39bb5e 21040get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21041 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21042{
21043 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21044 if (attr_form_is_ref (attr))
ac9ec31b
DE
21045 {
21046 struct dwarf2_cu *type_cu = cu;
21047 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21048
21049 return read_type_die (type_die, type_cu);
21050 }
21051 else if (attr->form == DW_FORM_ref_sig8)
21052 {
21053 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21054 }
21055 else
21056 {
21057 complaint (&symfile_complaints,
21058 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21059 " at 0x%x [in module %s]"),
9c541725 21060 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21061 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21062 return build_error_marker_type (cu, die);
21063 }
348e048f
DE
21064}
21065
e5fe5e75 21066/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21067
21068static void
e5fe5e75 21069load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21070{
52dc124a 21071 struct signatured_type *sig_type;
348e048f 21072
f4dc4d17
DE
21073 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21074 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21075
6721b2ec
DE
21076 /* We have the per_cu, but we need the signatured_type.
21077 Fortunately this is an easy translation. */
21078 gdb_assert (per_cu->is_debug_types);
21079 sig_type = (struct signatured_type *) per_cu;
348e048f 21080
6721b2ec 21081 gdb_assert (per_cu->cu == NULL);
348e048f 21082
52dc124a 21083 read_signatured_type (sig_type);
348e048f 21084
6721b2ec 21085 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21086}
21087
dee91e82
DE
21088/* die_reader_func for read_signatured_type.
21089 This is identical to load_full_comp_unit_reader,
21090 but is kept separate for now. */
348e048f
DE
21091
21092static void
dee91e82 21093read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21094 const gdb_byte *info_ptr,
dee91e82
DE
21095 struct die_info *comp_unit_die,
21096 int has_children,
21097 void *data)
348e048f 21098{
dee91e82 21099 struct dwarf2_cu *cu = reader->cu;
348e048f 21100
dee91e82
DE
21101 gdb_assert (cu->die_hash == NULL);
21102 cu->die_hash =
21103 htab_create_alloc_ex (cu->header.length / 12,
21104 die_hash,
21105 die_eq,
21106 NULL,
21107 &cu->comp_unit_obstack,
21108 hashtab_obstack_allocate,
21109 dummy_obstack_deallocate);
348e048f 21110
dee91e82
DE
21111 if (has_children)
21112 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21113 &info_ptr, comp_unit_die);
21114 cu->dies = comp_unit_die;
21115 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21116
21117 /* We try not to read any attributes in this function, because not
9cdd5dbd 21118 all CUs needed for references have been loaded yet, and symbol
348e048f 21119 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21120 or we won't be able to build types correctly.
21121 Similarly, if we do not read the producer, we can not apply
21122 producer-specific interpretation. */
95554aad 21123 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21124}
348e048f 21125
3019eac3
DE
21126/* Read in a signatured type and build its CU and DIEs.
21127 If the type is a stub for the real type in a DWO file,
21128 read in the real type from the DWO file as well. */
dee91e82
DE
21129
21130static void
21131read_signatured_type (struct signatured_type *sig_type)
21132{
21133 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21134
3019eac3 21135 gdb_assert (per_cu->is_debug_types);
dee91e82 21136 gdb_assert (per_cu->cu == NULL);
348e048f 21137
f4dc4d17
DE
21138 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21139 read_signatured_type_reader, NULL);
7ee85ab1 21140 sig_type->per_cu.tu_read = 1;
c906108c
SS
21141}
21142
c906108c
SS
21143/* Decode simple location descriptions.
21144 Given a pointer to a dwarf block that defines a location, compute
21145 the location and return the value.
21146
4cecd739
DJ
21147 NOTE drow/2003-11-18: This function is called in two situations
21148 now: for the address of static or global variables (partial symbols
21149 only) and for offsets into structures which are expected to be
21150 (more or less) constant. The partial symbol case should go away,
21151 and only the constant case should remain. That will let this
21152 function complain more accurately. A few special modes are allowed
21153 without complaint for global variables (for instance, global
21154 register values and thread-local values).
c906108c
SS
21155
21156 A location description containing no operations indicates that the
4cecd739 21157 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21158 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21159 callers will only want a very basic result and this can become a
21ae7a4d
JK
21160 complaint.
21161
21162 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21163
21164static CORE_ADDR
e7c27a73 21165decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21166{
e7c27a73 21167 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21168 size_t i;
21169 size_t size = blk->size;
d521ce57 21170 const gdb_byte *data = blk->data;
21ae7a4d
JK
21171 CORE_ADDR stack[64];
21172 int stacki;
21173 unsigned int bytes_read, unsnd;
21174 gdb_byte op;
c906108c 21175
21ae7a4d
JK
21176 i = 0;
21177 stacki = 0;
21178 stack[stacki] = 0;
21179 stack[++stacki] = 0;
21180
21181 while (i < size)
21182 {
21183 op = data[i++];
21184 switch (op)
21185 {
21186 case DW_OP_lit0:
21187 case DW_OP_lit1:
21188 case DW_OP_lit2:
21189 case DW_OP_lit3:
21190 case DW_OP_lit4:
21191 case DW_OP_lit5:
21192 case DW_OP_lit6:
21193 case DW_OP_lit7:
21194 case DW_OP_lit8:
21195 case DW_OP_lit9:
21196 case DW_OP_lit10:
21197 case DW_OP_lit11:
21198 case DW_OP_lit12:
21199 case DW_OP_lit13:
21200 case DW_OP_lit14:
21201 case DW_OP_lit15:
21202 case DW_OP_lit16:
21203 case DW_OP_lit17:
21204 case DW_OP_lit18:
21205 case DW_OP_lit19:
21206 case DW_OP_lit20:
21207 case DW_OP_lit21:
21208 case DW_OP_lit22:
21209 case DW_OP_lit23:
21210 case DW_OP_lit24:
21211 case DW_OP_lit25:
21212 case DW_OP_lit26:
21213 case DW_OP_lit27:
21214 case DW_OP_lit28:
21215 case DW_OP_lit29:
21216 case DW_OP_lit30:
21217 case DW_OP_lit31:
21218 stack[++stacki] = op - DW_OP_lit0;
21219 break;
f1bea926 21220
21ae7a4d
JK
21221 case DW_OP_reg0:
21222 case DW_OP_reg1:
21223 case DW_OP_reg2:
21224 case DW_OP_reg3:
21225 case DW_OP_reg4:
21226 case DW_OP_reg5:
21227 case DW_OP_reg6:
21228 case DW_OP_reg7:
21229 case DW_OP_reg8:
21230 case DW_OP_reg9:
21231 case DW_OP_reg10:
21232 case DW_OP_reg11:
21233 case DW_OP_reg12:
21234 case DW_OP_reg13:
21235 case DW_OP_reg14:
21236 case DW_OP_reg15:
21237 case DW_OP_reg16:
21238 case DW_OP_reg17:
21239 case DW_OP_reg18:
21240 case DW_OP_reg19:
21241 case DW_OP_reg20:
21242 case DW_OP_reg21:
21243 case DW_OP_reg22:
21244 case DW_OP_reg23:
21245 case DW_OP_reg24:
21246 case DW_OP_reg25:
21247 case DW_OP_reg26:
21248 case DW_OP_reg27:
21249 case DW_OP_reg28:
21250 case DW_OP_reg29:
21251 case DW_OP_reg30:
21252 case DW_OP_reg31:
21253 stack[++stacki] = op - DW_OP_reg0;
21254 if (i < size)
21255 dwarf2_complex_location_expr_complaint ();
21256 break;
c906108c 21257
21ae7a4d
JK
21258 case DW_OP_regx:
21259 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21260 i += bytes_read;
21261 stack[++stacki] = unsnd;
21262 if (i < size)
21263 dwarf2_complex_location_expr_complaint ();
21264 break;
c906108c 21265
21ae7a4d
JK
21266 case DW_OP_addr:
21267 stack[++stacki] = read_address (objfile->obfd, &data[i],
21268 cu, &bytes_read);
21269 i += bytes_read;
21270 break;
d53d4ac5 21271
21ae7a4d
JK
21272 case DW_OP_const1u:
21273 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21274 i += 1;
21275 break;
21276
21277 case DW_OP_const1s:
21278 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21279 i += 1;
21280 break;
21281
21282 case DW_OP_const2u:
21283 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21284 i += 2;
21285 break;
21286
21287 case DW_OP_const2s:
21288 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21289 i += 2;
21290 break;
d53d4ac5 21291
21ae7a4d
JK
21292 case DW_OP_const4u:
21293 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21294 i += 4;
21295 break;
21296
21297 case DW_OP_const4s:
21298 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21299 i += 4;
21300 break;
21301
585861ea
JK
21302 case DW_OP_const8u:
21303 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21304 i += 8;
21305 break;
21306
21ae7a4d
JK
21307 case DW_OP_constu:
21308 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21309 &bytes_read);
21310 i += bytes_read;
21311 break;
21312
21313 case DW_OP_consts:
21314 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21315 i += bytes_read;
21316 break;
21317
21318 case DW_OP_dup:
21319 stack[stacki + 1] = stack[stacki];
21320 stacki++;
21321 break;
21322
21323 case DW_OP_plus:
21324 stack[stacki - 1] += stack[stacki];
21325 stacki--;
21326 break;
21327
21328 case DW_OP_plus_uconst:
21329 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21330 &bytes_read);
21331 i += bytes_read;
21332 break;
21333
21334 case DW_OP_minus:
21335 stack[stacki - 1] -= stack[stacki];
21336 stacki--;
21337 break;
21338
21339 case DW_OP_deref:
21340 /* If we're not the last op, then we definitely can't encode
21341 this using GDB's address_class enum. This is valid for partial
21342 global symbols, although the variable's address will be bogus
21343 in the psymtab. */
21344 if (i < size)
21345 dwarf2_complex_location_expr_complaint ();
21346 break;
21347
21348 case DW_OP_GNU_push_tls_address:
4aa4e28b 21349 case DW_OP_form_tls_address:
21ae7a4d
JK
21350 /* The top of the stack has the offset from the beginning
21351 of the thread control block at which the variable is located. */
21352 /* Nothing should follow this operator, so the top of stack would
21353 be returned. */
21354 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21355 address will be bogus in the psymtab. Make it always at least
21356 non-zero to not look as a variable garbage collected by linker
21357 which have DW_OP_addr 0. */
21ae7a4d
JK
21358 if (i < size)
21359 dwarf2_complex_location_expr_complaint ();
585861ea 21360 stack[stacki]++;
21ae7a4d
JK
21361 break;
21362
21363 case DW_OP_GNU_uninit:
21364 break;
21365
3019eac3 21366 case DW_OP_GNU_addr_index:
49f6c839 21367 case DW_OP_GNU_const_index:
3019eac3
DE
21368 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21369 &bytes_read);
21370 i += bytes_read;
21371 break;
21372
21ae7a4d
JK
21373 default:
21374 {
f39c6ffd 21375 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21376
21377 if (name)
21378 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21379 name);
21380 else
21381 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21382 op);
21383 }
21384
21385 return (stack[stacki]);
d53d4ac5 21386 }
3c6e0cb3 21387
21ae7a4d
JK
21388 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21389 outside of the allocated space. Also enforce minimum>0. */
21390 if (stacki >= ARRAY_SIZE (stack) - 1)
21391 {
21392 complaint (&symfile_complaints,
21393 _("location description stack overflow"));
21394 return 0;
21395 }
21396
21397 if (stacki <= 0)
21398 {
21399 complaint (&symfile_complaints,
21400 _("location description stack underflow"));
21401 return 0;
21402 }
21403 }
21404 return (stack[stacki]);
c906108c
SS
21405}
21406
21407/* memory allocation interface */
21408
c906108c 21409static struct dwarf_block *
7b5a2f43 21410dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21411{
8d749320 21412 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21413}
21414
c906108c 21415static struct die_info *
b60c80d6 21416dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21417{
21418 struct die_info *die;
b60c80d6
DJ
21419 size_t size = sizeof (struct die_info);
21420
21421 if (num_attrs > 1)
21422 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21423
b60c80d6 21424 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21425 memset (die, 0, sizeof (struct die_info));
21426 return (die);
21427}
2e276125
JB
21428
21429\f
21430/* Macro support. */
21431
233d95b5
JK
21432/* Return file name relative to the compilation directory of file number I in
21433 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21434 responsible for freeing it. */
233d95b5 21435
2e276125 21436static char *
233d95b5 21437file_file_name (int file, struct line_header *lh)
2e276125 21438{
6a83a1e6
EZ
21439 /* Is the file number a valid index into the line header's file name
21440 table? Remember that file numbers start with one, not zero. */
fff8551c 21441 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21442 {
8c43009f 21443 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21444
8c43009f
PA
21445 if (!IS_ABSOLUTE_PATH (fe.name))
21446 {
21447 const char *dir = fe.include_dir (lh);
21448 if (dir != NULL)
21449 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21450 }
21451 return xstrdup (fe.name);
6a83a1e6 21452 }
2e276125
JB
21453 else
21454 {
6a83a1e6
EZ
21455 /* The compiler produced a bogus file number. We can at least
21456 record the macro definitions made in the file, even if we
21457 won't be able to find the file by name. */
21458 char fake_name[80];
9a619af0 21459
8c042590
PM
21460 xsnprintf (fake_name, sizeof (fake_name),
21461 "<bad macro file number %d>", file);
2e276125 21462
6e70227d 21463 complaint (&symfile_complaints,
6a83a1e6
EZ
21464 _("bad file number in macro information (%d)"),
21465 file);
2e276125 21466
6a83a1e6 21467 return xstrdup (fake_name);
2e276125
JB
21468 }
21469}
21470
233d95b5
JK
21471/* Return the full name of file number I in *LH's file name table.
21472 Use COMP_DIR as the name of the current directory of the
21473 compilation. The result is allocated using xmalloc; the caller is
21474 responsible for freeing it. */
21475static char *
21476file_full_name (int file, struct line_header *lh, const char *comp_dir)
21477{
21478 /* Is the file number a valid index into the line header's file name
21479 table? Remember that file numbers start with one, not zero. */
fff8551c 21480 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21481 {
21482 char *relative = file_file_name (file, lh);
21483
21484 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21485 return relative;
b36cec19
PA
21486 return reconcat (relative, comp_dir, SLASH_STRING,
21487 relative, (char *) NULL);
233d95b5
JK
21488 }
21489 else
21490 return file_file_name (file, lh);
21491}
21492
2e276125
JB
21493
21494static struct macro_source_file *
21495macro_start_file (int file, int line,
21496 struct macro_source_file *current_file,
43f3e411 21497 struct line_header *lh)
2e276125 21498{
233d95b5
JK
21499 /* File name relative to the compilation directory of this source file. */
21500 char *file_name = file_file_name (file, lh);
2e276125 21501
2e276125 21502 if (! current_file)
abc9d0dc 21503 {
fc474241
DE
21504 /* Note: We don't create a macro table for this compilation unit
21505 at all until we actually get a filename. */
43f3e411 21506 struct macro_table *macro_table = get_macro_table ();
fc474241 21507
abc9d0dc
TT
21508 /* If we have no current file, then this must be the start_file
21509 directive for the compilation unit's main source file. */
fc474241
DE
21510 current_file = macro_set_main (macro_table, file_name);
21511 macro_define_special (macro_table);
abc9d0dc 21512 }
2e276125 21513 else
233d95b5 21514 current_file = macro_include (current_file, line, file_name);
2e276125 21515
233d95b5 21516 xfree (file_name);
6e70227d 21517
2e276125
JB
21518 return current_file;
21519}
21520
21521
21522/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21523 followed by a null byte. */
21524static char *
21525copy_string (const char *buf, int len)
21526{
224c3ddb 21527 char *s = (char *) xmalloc (len + 1);
9a619af0 21528
2e276125
JB
21529 memcpy (s, buf, len);
21530 s[len] = '\0';
2e276125
JB
21531 return s;
21532}
21533
21534
21535static const char *
21536consume_improper_spaces (const char *p, const char *body)
21537{
21538 if (*p == ' ')
21539 {
4d3c2250 21540 complaint (&symfile_complaints,
3e43a32a
MS
21541 _("macro definition contains spaces "
21542 "in formal argument list:\n`%s'"),
4d3c2250 21543 body);
2e276125
JB
21544
21545 while (*p == ' ')
21546 p++;
21547 }
21548
21549 return p;
21550}
21551
21552
21553static void
21554parse_macro_definition (struct macro_source_file *file, int line,
21555 const char *body)
21556{
21557 const char *p;
21558
21559 /* The body string takes one of two forms. For object-like macro
21560 definitions, it should be:
21561
21562 <macro name> " " <definition>
21563
21564 For function-like macro definitions, it should be:
21565
21566 <macro name> "() " <definition>
21567 or
21568 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21569
21570 Spaces may appear only where explicitly indicated, and in the
21571 <definition>.
21572
21573 The Dwarf 2 spec says that an object-like macro's name is always
21574 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21575 the space when the macro's definition is the empty string.
2e276125
JB
21576
21577 The Dwarf 2 spec says that there should be no spaces between the
21578 formal arguments in a function-like macro's formal argument list,
21579 but versions of GCC around March 2002 include spaces after the
21580 commas. */
21581
21582
21583 /* Find the extent of the macro name. The macro name is terminated
21584 by either a space or null character (for an object-like macro) or
21585 an opening paren (for a function-like macro). */
21586 for (p = body; *p; p++)
21587 if (*p == ' ' || *p == '(')
21588 break;
21589
21590 if (*p == ' ' || *p == '\0')
21591 {
21592 /* It's an object-like macro. */
21593 int name_len = p - body;
21594 char *name = copy_string (body, name_len);
21595 const char *replacement;
21596
21597 if (*p == ' ')
21598 replacement = body + name_len + 1;
21599 else
21600 {
4d3c2250 21601 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21602 replacement = body + name_len;
21603 }
6e70227d 21604
2e276125
JB
21605 macro_define_object (file, line, name, replacement);
21606
21607 xfree (name);
21608 }
21609 else if (*p == '(')
21610 {
21611 /* It's a function-like macro. */
21612 char *name = copy_string (body, p - body);
21613 int argc = 0;
21614 int argv_size = 1;
8d749320 21615 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21616
21617 p++;
21618
21619 p = consume_improper_spaces (p, body);
21620
21621 /* Parse the formal argument list. */
21622 while (*p && *p != ')')
21623 {
21624 /* Find the extent of the current argument name. */
21625 const char *arg_start = p;
21626
21627 while (*p && *p != ',' && *p != ')' && *p != ' ')
21628 p++;
21629
21630 if (! *p || p == arg_start)
4d3c2250 21631 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21632 else
21633 {
21634 /* Make sure argv has room for the new argument. */
21635 if (argc >= argv_size)
21636 {
21637 argv_size *= 2;
224c3ddb 21638 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21639 }
21640
21641 argv[argc++] = copy_string (arg_start, p - arg_start);
21642 }
21643
21644 p = consume_improper_spaces (p, body);
21645
21646 /* Consume the comma, if present. */
21647 if (*p == ',')
21648 {
21649 p++;
21650
21651 p = consume_improper_spaces (p, body);
21652 }
21653 }
21654
21655 if (*p == ')')
21656 {
21657 p++;
21658
21659 if (*p == ' ')
21660 /* Perfectly formed definition, no complaints. */
21661 macro_define_function (file, line, name,
6e70227d 21662 argc, (const char **) argv,
2e276125
JB
21663 p + 1);
21664 else if (*p == '\0')
21665 {
21666 /* Complain, but do define it. */
4d3c2250 21667 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21668 macro_define_function (file, line, name,
6e70227d 21669 argc, (const char **) argv,
2e276125
JB
21670 p);
21671 }
21672 else
21673 /* Just complain. */
4d3c2250 21674 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21675 }
21676 else
21677 /* Just complain. */
4d3c2250 21678 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21679
21680 xfree (name);
21681 {
21682 int i;
21683
21684 for (i = 0; i < argc; i++)
21685 xfree (argv[i]);
21686 }
21687 xfree (argv);
21688 }
21689 else
4d3c2250 21690 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21691}
21692
cf2c3c16
TT
21693/* Skip some bytes from BYTES according to the form given in FORM.
21694 Returns the new pointer. */
2e276125 21695
d521ce57
TT
21696static const gdb_byte *
21697skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21698 enum dwarf_form form,
21699 unsigned int offset_size,
21700 struct dwarf2_section_info *section)
2e276125 21701{
cf2c3c16 21702 unsigned int bytes_read;
2e276125 21703
cf2c3c16 21704 switch (form)
2e276125 21705 {
cf2c3c16
TT
21706 case DW_FORM_data1:
21707 case DW_FORM_flag:
21708 ++bytes;
21709 break;
21710
21711 case DW_FORM_data2:
21712 bytes += 2;
21713 break;
21714
21715 case DW_FORM_data4:
21716 bytes += 4;
21717 break;
21718
21719 case DW_FORM_data8:
21720 bytes += 8;
21721 break;
21722
0224619f
JK
21723 case DW_FORM_data16:
21724 bytes += 16;
21725 break;
21726
cf2c3c16
TT
21727 case DW_FORM_string:
21728 read_direct_string (abfd, bytes, &bytes_read);
21729 bytes += bytes_read;
21730 break;
21731
21732 case DW_FORM_sec_offset:
21733 case DW_FORM_strp:
36586728 21734 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21735 bytes += offset_size;
21736 break;
21737
21738 case DW_FORM_block:
21739 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21740 bytes += bytes_read;
21741 break;
21742
21743 case DW_FORM_block1:
21744 bytes += 1 + read_1_byte (abfd, bytes);
21745 break;
21746 case DW_FORM_block2:
21747 bytes += 2 + read_2_bytes (abfd, bytes);
21748 break;
21749 case DW_FORM_block4:
21750 bytes += 4 + read_4_bytes (abfd, bytes);
21751 break;
21752
21753 case DW_FORM_sdata:
21754 case DW_FORM_udata:
3019eac3
DE
21755 case DW_FORM_GNU_addr_index:
21756 case DW_FORM_GNU_str_index:
d521ce57 21757 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21758 if (bytes == NULL)
21759 {
21760 dwarf2_section_buffer_overflow_complaint (section);
21761 return NULL;
21762 }
cf2c3c16
TT
21763 break;
21764
21765 default:
21766 {
21767 complain:
21768 complaint (&symfile_complaints,
21769 _("invalid form 0x%x in `%s'"),
a32a8923 21770 form, get_section_name (section));
cf2c3c16
TT
21771 return NULL;
21772 }
2e276125
JB
21773 }
21774
cf2c3c16
TT
21775 return bytes;
21776}
757a13d0 21777
cf2c3c16
TT
21778/* A helper for dwarf_decode_macros that handles skipping an unknown
21779 opcode. Returns an updated pointer to the macro data buffer; or,
21780 on error, issues a complaint and returns NULL. */
757a13d0 21781
d521ce57 21782static const gdb_byte *
cf2c3c16 21783skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21784 const gdb_byte **opcode_definitions,
21785 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21786 bfd *abfd,
21787 unsigned int offset_size,
21788 struct dwarf2_section_info *section)
21789{
21790 unsigned int bytes_read, i;
21791 unsigned long arg;
d521ce57 21792 const gdb_byte *defn;
2e276125 21793
cf2c3c16 21794 if (opcode_definitions[opcode] == NULL)
2e276125 21795 {
cf2c3c16
TT
21796 complaint (&symfile_complaints,
21797 _("unrecognized DW_MACFINO opcode 0x%x"),
21798 opcode);
21799 return NULL;
21800 }
2e276125 21801
cf2c3c16
TT
21802 defn = opcode_definitions[opcode];
21803 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21804 defn += bytes_read;
2e276125 21805
cf2c3c16
TT
21806 for (i = 0; i < arg; ++i)
21807 {
aead7601
SM
21808 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21809 (enum dwarf_form) defn[i], offset_size,
f664829e 21810 section);
cf2c3c16
TT
21811 if (mac_ptr == NULL)
21812 {
21813 /* skip_form_bytes already issued the complaint. */
21814 return NULL;
21815 }
21816 }
757a13d0 21817
cf2c3c16
TT
21818 return mac_ptr;
21819}
757a13d0 21820
cf2c3c16
TT
21821/* A helper function which parses the header of a macro section.
21822 If the macro section is the extended (for now called "GNU") type,
21823 then this updates *OFFSET_SIZE. Returns a pointer to just after
21824 the header, or issues a complaint and returns NULL on error. */
757a13d0 21825
d521ce57
TT
21826static const gdb_byte *
21827dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21828 bfd *abfd,
d521ce57 21829 const gdb_byte *mac_ptr,
cf2c3c16
TT
21830 unsigned int *offset_size,
21831 int section_is_gnu)
21832{
21833 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21834
cf2c3c16
TT
21835 if (section_is_gnu)
21836 {
21837 unsigned int version, flags;
757a13d0 21838
cf2c3c16 21839 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21840 if (version != 4 && version != 5)
cf2c3c16
TT
21841 {
21842 complaint (&symfile_complaints,
21843 _("unrecognized version `%d' in .debug_macro section"),
21844 version);
21845 return NULL;
21846 }
21847 mac_ptr += 2;
757a13d0 21848
cf2c3c16
TT
21849 flags = read_1_byte (abfd, mac_ptr);
21850 ++mac_ptr;
21851 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21852
cf2c3c16
TT
21853 if ((flags & 2) != 0)
21854 /* We don't need the line table offset. */
21855 mac_ptr += *offset_size;
757a13d0 21856
cf2c3c16
TT
21857 /* Vendor opcode descriptions. */
21858 if ((flags & 4) != 0)
21859 {
21860 unsigned int i, count;
757a13d0 21861
cf2c3c16
TT
21862 count = read_1_byte (abfd, mac_ptr);
21863 ++mac_ptr;
21864 for (i = 0; i < count; ++i)
21865 {
21866 unsigned int opcode, bytes_read;
21867 unsigned long arg;
21868
21869 opcode = read_1_byte (abfd, mac_ptr);
21870 ++mac_ptr;
21871 opcode_definitions[opcode] = mac_ptr;
21872 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21873 mac_ptr += bytes_read;
21874 mac_ptr += arg;
21875 }
757a13d0 21876 }
cf2c3c16 21877 }
757a13d0 21878
cf2c3c16
TT
21879 return mac_ptr;
21880}
757a13d0 21881
cf2c3c16 21882/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21883 including DW_MACRO_import. */
cf2c3c16
TT
21884
21885static void
d521ce57
TT
21886dwarf_decode_macro_bytes (bfd *abfd,
21887 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21888 struct macro_source_file *current_file,
43f3e411 21889 struct line_header *lh,
cf2c3c16 21890 struct dwarf2_section_info *section,
36586728 21891 int section_is_gnu, int section_is_dwz,
cf2c3c16 21892 unsigned int offset_size,
8fc3fc34 21893 htab_t include_hash)
cf2c3c16 21894{
4d663531 21895 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21896 enum dwarf_macro_record_type macinfo_type;
21897 int at_commandline;
d521ce57 21898 const gdb_byte *opcode_definitions[256];
757a13d0 21899
cf2c3c16
TT
21900 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21901 &offset_size, section_is_gnu);
21902 if (mac_ptr == NULL)
21903 {
21904 /* We already issued a complaint. */
21905 return;
21906 }
757a13d0
JK
21907
21908 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21909 GDB is still reading the definitions from command line. First
21910 DW_MACINFO_start_file will need to be ignored as it was already executed
21911 to create CURRENT_FILE for the main source holding also the command line
21912 definitions. On first met DW_MACINFO_start_file this flag is reset to
21913 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21914
21915 at_commandline = 1;
21916
21917 do
21918 {
21919 /* Do we at least have room for a macinfo type byte? */
21920 if (mac_ptr >= mac_end)
21921 {
f664829e 21922 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21923 break;
21924 }
21925
aead7601 21926 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21927 mac_ptr++;
21928
cf2c3c16
TT
21929 /* Note that we rely on the fact that the corresponding GNU and
21930 DWARF constants are the same. */
757a13d0
JK
21931 switch (macinfo_type)
21932 {
21933 /* A zero macinfo type indicates the end of the macro
21934 information. */
21935 case 0:
21936 break;
2e276125 21937
0af92d60
JK
21938 case DW_MACRO_define:
21939 case DW_MACRO_undef:
21940 case DW_MACRO_define_strp:
21941 case DW_MACRO_undef_strp:
21942 case DW_MACRO_define_sup:
21943 case DW_MACRO_undef_sup:
2e276125 21944 {
891d2f0b 21945 unsigned int bytes_read;
2e276125 21946 int line;
d521ce57 21947 const char *body;
cf2c3c16 21948 int is_define;
2e276125 21949
cf2c3c16
TT
21950 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21951 mac_ptr += bytes_read;
21952
0af92d60
JK
21953 if (macinfo_type == DW_MACRO_define
21954 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21955 {
21956 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21957 mac_ptr += bytes_read;
21958 }
21959 else
21960 {
21961 LONGEST str_offset;
21962
21963 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21964 mac_ptr += offset_size;
2e276125 21965
0af92d60
JK
21966 if (macinfo_type == DW_MACRO_define_sup
21967 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21968 || section_is_dwz)
36586728
TT
21969 {
21970 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21971
21972 body = read_indirect_string_from_dwz (dwz, str_offset);
21973 }
21974 else
21975 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21976 }
21977
0af92d60
JK
21978 is_define = (macinfo_type == DW_MACRO_define
21979 || macinfo_type == DW_MACRO_define_strp
21980 || macinfo_type == DW_MACRO_define_sup);
2e276125 21981 if (! current_file)
757a13d0
JK
21982 {
21983 /* DWARF violation as no main source is present. */
21984 complaint (&symfile_complaints,
21985 _("debug info with no main source gives macro %s "
21986 "on line %d: %s"),
cf2c3c16
TT
21987 is_define ? _("definition") : _("undefinition"),
21988 line, body);
757a13d0
JK
21989 break;
21990 }
3e43a32a
MS
21991 if ((line == 0 && !at_commandline)
21992 || (line != 0 && at_commandline))
4d3c2250 21993 complaint (&symfile_complaints,
757a13d0
JK
21994 _("debug info gives %s macro %s with %s line %d: %s"),
21995 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21996 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21997 line == 0 ? _("zero") : _("non-zero"), line, body);
21998
cf2c3c16 21999 if (is_define)
757a13d0 22000 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22001 else
22002 {
0af92d60
JK
22003 gdb_assert (macinfo_type == DW_MACRO_undef
22004 || macinfo_type == DW_MACRO_undef_strp
22005 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22006 macro_undef (current_file, line, body);
22007 }
2e276125
JB
22008 }
22009 break;
22010
0af92d60 22011 case DW_MACRO_start_file:
2e276125 22012 {
891d2f0b 22013 unsigned int bytes_read;
2e276125
JB
22014 int line, file;
22015
22016 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22017 mac_ptr += bytes_read;
22018 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22019 mac_ptr += bytes_read;
22020
3e43a32a
MS
22021 if ((line == 0 && !at_commandline)
22022 || (line != 0 && at_commandline))
757a13d0
JK
22023 complaint (&symfile_complaints,
22024 _("debug info gives source %d included "
22025 "from %s at %s line %d"),
22026 file, at_commandline ? _("command-line") : _("file"),
22027 line == 0 ? _("zero") : _("non-zero"), line);
22028
22029 if (at_commandline)
22030 {
0af92d60 22031 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22032 pass one. */
757a13d0
JK
22033 at_commandline = 0;
22034 }
22035 else
43f3e411 22036 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22037 }
22038 break;
22039
0af92d60 22040 case DW_MACRO_end_file:
2e276125 22041 if (! current_file)
4d3c2250 22042 complaint (&symfile_complaints,
3e43a32a
MS
22043 _("macro debug info has an unmatched "
22044 "`close_file' directive"));
2e276125
JB
22045 else
22046 {
22047 current_file = current_file->included_by;
22048 if (! current_file)
22049 {
cf2c3c16 22050 enum dwarf_macro_record_type next_type;
2e276125
JB
22051
22052 /* GCC circa March 2002 doesn't produce the zero
22053 type byte marking the end of the compilation
22054 unit. Complain if it's not there, but exit no
22055 matter what. */
22056
22057 /* Do we at least have room for a macinfo type byte? */
22058 if (mac_ptr >= mac_end)
22059 {
f664829e 22060 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22061 return;
22062 }
22063
22064 /* We don't increment mac_ptr here, so this is just
22065 a look-ahead. */
aead7601
SM
22066 next_type
22067 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22068 mac_ptr);
2e276125 22069 if (next_type != 0)
4d3c2250 22070 complaint (&symfile_complaints,
3e43a32a
MS
22071 _("no terminating 0-type entry for "
22072 "macros in `.debug_macinfo' section"));
2e276125
JB
22073
22074 return;
22075 }
22076 }
22077 break;
22078
0af92d60
JK
22079 case DW_MACRO_import:
22080 case DW_MACRO_import_sup:
cf2c3c16
TT
22081 {
22082 LONGEST offset;
8fc3fc34 22083 void **slot;
a036ba48
TT
22084 bfd *include_bfd = abfd;
22085 struct dwarf2_section_info *include_section = section;
d521ce57 22086 const gdb_byte *include_mac_end = mac_end;
a036ba48 22087 int is_dwz = section_is_dwz;
d521ce57 22088 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22089
22090 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22091 mac_ptr += offset_size;
22092
0af92d60 22093 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22094 {
22095 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22096
4d663531 22097 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22098
a036ba48 22099 include_section = &dwz->macro;
a32a8923 22100 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22101 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22102 is_dwz = 1;
22103 }
22104
22105 new_mac_ptr = include_section->buffer + offset;
22106 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22107
8fc3fc34
TT
22108 if (*slot != NULL)
22109 {
22110 /* This has actually happened; see
22111 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22112 complaint (&symfile_complaints,
0af92d60 22113 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22114 ".debug_macro section"));
22115 }
22116 else
22117 {
d521ce57 22118 *slot = (void *) new_mac_ptr;
36586728 22119
a036ba48 22120 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22121 include_mac_end, current_file, lh,
36586728 22122 section, section_is_gnu, is_dwz,
4d663531 22123 offset_size, include_hash);
8fc3fc34 22124
d521ce57 22125 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22126 }
cf2c3c16
TT
22127 }
22128 break;
22129
2e276125 22130 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22131 if (!section_is_gnu)
22132 {
22133 unsigned int bytes_read;
2e276125 22134
ac298888
TT
22135 /* This reads the constant, but since we don't recognize
22136 any vendor extensions, we ignore it. */
22137 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22138 mac_ptr += bytes_read;
22139 read_direct_string (abfd, mac_ptr, &bytes_read);
22140 mac_ptr += bytes_read;
2e276125 22141
cf2c3c16
TT
22142 /* We don't recognize any vendor extensions. */
22143 break;
22144 }
22145 /* FALLTHROUGH */
22146
22147 default:
22148 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22149 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22150 section);
22151 if (mac_ptr == NULL)
22152 return;
22153 break;
2e276125 22154 }
757a13d0 22155 } while (macinfo_type != 0);
2e276125 22156}
8e19ed76 22157
cf2c3c16 22158static void
09262596 22159dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22160 int section_is_gnu)
cf2c3c16 22161{
bb5ed363 22162 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22163 struct line_header *lh = cu->line_header;
22164 bfd *abfd;
d521ce57 22165 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22166 struct macro_source_file *current_file = 0;
22167 enum dwarf_macro_record_type macinfo_type;
22168 unsigned int offset_size = cu->header.offset_size;
d521ce57 22169 const gdb_byte *opcode_definitions[256];
8fc3fc34 22170 struct cleanup *cleanup;
8fc3fc34 22171 void **slot;
09262596
DE
22172 struct dwarf2_section_info *section;
22173 const char *section_name;
22174
22175 if (cu->dwo_unit != NULL)
22176 {
22177 if (section_is_gnu)
22178 {
22179 section = &cu->dwo_unit->dwo_file->sections.macro;
22180 section_name = ".debug_macro.dwo";
22181 }
22182 else
22183 {
22184 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22185 section_name = ".debug_macinfo.dwo";
22186 }
22187 }
22188 else
22189 {
22190 if (section_is_gnu)
22191 {
22192 section = &dwarf2_per_objfile->macro;
22193 section_name = ".debug_macro";
22194 }
22195 else
22196 {
22197 section = &dwarf2_per_objfile->macinfo;
22198 section_name = ".debug_macinfo";
22199 }
22200 }
cf2c3c16 22201
bb5ed363 22202 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22203 if (section->buffer == NULL)
22204 {
fceca515 22205 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22206 return;
22207 }
a32a8923 22208 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22209
22210 /* First pass: Find the name of the base filename.
22211 This filename is needed in order to process all macros whose definition
22212 (or undefinition) comes from the command line. These macros are defined
22213 before the first DW_MACINFO_start_file entry, and yet still need to be
22214 associated to the base file.
22215
22216 To determine the base file name, we scan the macro definitions until we
22217 reach the first DW_MACINFO_start_file entry. We then initialize
22218 CURRENT_FILE accordingly so that any macro definition found before the
22219 first DW_MACINFO_start_file can still be associated to the base file. */
22220
22221 mac_ptr = section->buffer + offset;
22222 mac_end = section->buffer + section->size;
22223
22224 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22225 &offset_size, section_is_gnu);
22226 if (mac_ptr == NULL)
22227 {
22228 /* We already issued a complaint. */
22229 return;
22230 }
22231
22232 do
22233 {
22234 /* Do we at least have room for a macinfo type byte? */
22235 if (mac_ptr >= mac_end)
22236 {
22237 /* Complaint is printed during the second pass as GDB will probably
22238 stop the first pass earlier upon finding
22239 DW_MACINFO_start_file. */
22240 break;
22241 }
22242
aead7601 22243 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22244 mac_ptr++;
22245
22246 /* Note that we rely on the fact that the corresponding GNU and
22247 DWARF constants are the same. */
22248 switch (macinfo_type)
22249 {
22250 /* A zero macinfo type indicates the end of the macro
22251 information. */
22252 case 0:
22253 break;
22254
0af92d60
JK
22255 case DW_MACRO_define:
22256 case DW_MACRO_undef:
cf2c3c16
TT
22257 /* Only skip the data by MAC_PTR. */
22258 {
22259 unsigned int bytes_read;
22260
22261 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22262 mac_ptr += bytes_read;
22263 read_direct_string (abfd, mac_ptr, &bytes_read);
22264 mac_ptr += bytes_read;
22265 }
22266 break;
22267
0af92d60 22268 case DW_MACRO_start_file:
cf2c3c16
TT
22269 {
22270 unsigned int bytes_read;
22271 int line, file;
22272
22273 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22274 mac_ptr += bytes_read;
22275 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22276 mac_ptr += bytes_read;
22277
43f3e411 22278 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22279 }
22280 break;
22281
0af92d60 22282 case DW_MACRO_end_file:
cf2c3c16
TT
22283 /* No data to skip by MAC_PTR. */
22284 break;
22285
0af92d60
JK
22286 case DW_MACRO_define_strp:
22287 case DW_MACRO_undef_strp:
22288 case DW_MACRO_define_sup:
22289 case DW_MACRO_undef_sup:
cf2c3c16
TT
22290 {
22291 unsigned int bytes_read;
22292
22293 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22294 mac_ptr += bytes_read;
22295 mac_ptr += offset_size;
22296 }
22297 break;
22298
0af92d60
JK
22299 case DW_MACRO_import:
22300 case DW_MACRO_import_sup:
cf2c3c16 22301 /* Note that, according to the spec, a transparent include
0af92d60 22302 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22303 skip this opcode. */
22304 mac_ptr += offset_size;
22305 break;
22306
22307 case DW_MACINFO_vendor_ext:
22308 /* Only skip the data by MAC_PTR. */
22309 if (!section_is_gnu)
22310 {
22311 unsigned int bytes_read;
22312
22313 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22314 mac_ptr += bytes_read;
22315 read_direct_string (abfd, mac_ptr, &bytes_read);
22316 mac_ptr += bytes_read;
22317 }
22318 /* FALLTHROUGH */
22319
22320 default:
22321 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22322 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22323 section);
22324 if (mac_ptr == NULL)
22325 return;
22326 break;
22327 }
22328 } while (macinfo_type != 0 && current_file == NULL);
22329
22330 /* Second pass: Process all entries.
22331
22332 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22333 command-line macro definitions/undefinitions. This flag is unset when we
22334 reach the first DW_MACINFO_start_file entry. */
22335
fc4007c9
TT
22336 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22337 htab_eq_pointer,
22338 NULL, xcalloc, xfree));
8fc3fc34 22339 mac_ptr = section->buffer + offset;
fc4007c9 22340 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22341 *slot = (void *) mac_ptr;
8fc3fc34 22342 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22343 current_file, lh, section,
fc4007c9
TT
22344 section_is_gnu, 0, offset_size,
22345 include_hash.get ());
cf2c3c16
TT
22346}
22347
8e19ed76 22348/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22349 if so return true else false. */
380bca97 22350
8e19ed76 22351static int
6e5a29e1 22352attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22353{
22354 return (attr == NULL ? 0 :
22355 attr->form == DW_FORM_block1
22356 || attr->form == DW_FORM_block2
22357 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22358 || attr->form == DW_FORM_block
22359 || attr->form == DW_FORM_exprloc);
8e19ed76 22360}
4c2df51b 22361
c6a0999f
JB
22362/* Return non-zero if ATTR's value is a section offset --- classes
22363 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22364 You may use DW_UNSND (attr) to retrieve such offsets.
22365
22366 Section 7.5.4, "Attribute Encodings", explains that no attribute
22367 may have a value that belongs to more than one of these classes; it
22368 would be ambiguous if we did, because we use the same forms for all
22369 of them. */
380bca97 22370
3690dd37 22371static int
6e5a29e1 22372attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22373{
22374 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22375 || attr->form == DW_FORM_data8
22376 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22377}
22378
3690dd37
JB
22379/* Return non-zero if ATTR's value falls in the 'constant' class, or
22380 zero otherwise. When this function returns true, you can apply
22381 dwarf2_get_attr_constant_value to it.
22382
22383 However, note that for some attributes you must check
22384 attr_form_is_section_offset before using this test. DW_FORM_data4
22385 and DW_FORM_data8 are members of both the constant class, and of
22386 the classes that contain offsets into other debug sections
22387 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22388 that, if an attribute's can be either a constant or one of the
22389 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22390 taken as section offsets, not constants.
22391
22392 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22393 cannot handle that. */
380bca97 22394
3690dd37 22395static int
6e5a29e1 22396attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22397{
22398 switch (attr->form)
22399 {
22400 case DW_FORM_sdata:
22401 case DW_FORM_udata:
22402 case DW_FORM_data1:
22403 case DW_FORM_data2:
22404 case DW_FORM_data4:
22405 case DW_FORM_data8:
22406 return 1;
22407 default:
22408 return 0;
22409 }
22410}
22411
7771576e
SA
22412
22413/* DW_ADDR is always stored already as sect_offset; despite for the forms
22414 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22415
22416static int
6e5a29e1 22417attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22418{
22419 switch (attr->form)
22420 {
22421 case DW_FORM_ref_addr:
22422 case DW_FORM_ref1:
22423 case DW_FORM_ref2:
22424 case DW_FORM_ref4:
22425 case DW_FORM_ref8:
22426 case DW_FORM_ref_udata:
22427 case DW_FORM_GNU_ref_alt:
22428 return 1;
22429 default:
22430 return 0;
22431 }
22432}
22433
3019eac3
DE
22434/* Return the .debug_loc section to use for CU.
22435 For DWO files use .debug_loc.dwo. */
22436
22437static struct dwarf2_section_info *
22438cu_debug_loc_section (struct dwarf2_cu *cu)
22439{
22440 if (cu->dwo_unit)
43988095
JK
22441 {
22442 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22443
22444 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22445 }
22446 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22447 : &dwarf2_per_objfile->loc);
3019eac3
DE
22448}
22449
8cf6f0b1
TT
22450/* A helper function that fills in a dwarf2_loclist_baton. */
22451
22452static void
22453fill_in_loclist_baton (struct dwarf2_cu *cu,
22454 struct dwarf2_loclist_baton *baton,
ff39bb5e 22455 const struct attribute *attr)
8cf6f0b1 22456{
3019eac3
DE
22457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22458
22459 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22460
22461 baton->per_cu = cu->per_cu;
22462 gdb_assert (baton->per_cu);
22463 /* We don't know how long the location list is, but make sure we
22464 don't run off the edge of the section. */
3019eac3
DE
22465 baton->size = section->size - DW_UNSND (attr);
22466 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22467 baton->base_address = cu->base_address;
f664829e 22468 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22469}
22470
4c2df51b 22471static void
ff39bb5e 22472dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22473 struct dwarf2_cu *cu, int is_block)
4c2df51b 22474{
bb5ed363 22475 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22476 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22477
3690dd37 22478 if (attr_form_is_section_offset (attr)
3019eac3 22479 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22480 the section. If so, fall through to the complaint in the
22481 other branch. */
3019eac3 22482 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22483 {
0d53c4c4 22484 struct dwarf2_loclist_baton *baton;
4c2df51b 22485
8d749320 22486 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22487
8cf6f0b1 22488 fill_in_loclist_baton (cu, baton, attr);
be391dca 22489
d00adf39 22490 if (cu->base_known == 0)
0d53c4c4 22491 complaint (&symfile_complaints,
3e43a32a
MS
22492 _("Location list used without "
22493 "specifying the CU base address."));
4c2df51b 22494
f1e6e072
TT
22495 SYMBOL_ACLASS_INDEX (sym) = (is_block
22496 ? dwarf2_loclist_block_index
22497 : dwarf2_loclist_index);
0d53c4c4
DJ
22498 SYMBOL_LOCATION_BATON (sym) = baton;
22499 }
22500 else
22501 {
22502 struct dwarf2_locexpr_baton *baton;
22503
8d749320 22504 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22505 baton->per_cu = cu->per_cu;
22506 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22507
22508 if (attr_form_is_block (attr))
22509 {
22510 /* Note that we're just copying the block's data pointer
22511 here, not the actual data. We're still pointing into the
6502dd73
DJ
22512 info_buffer for SYM's objfile; right now we never release
22513 that buffer, but when we do clean up properly this may
22514 need to change. */
0d53c4c4
DJ
22515 baton->size = DW_BLOCK (attr)->size;
22516 baton->data = DW_BLOCK (attr)->data;
22517 }
22518 else
22519 {
22520 dwarf2_invalid_attrib_class_complaint ("location description",
22521 SYMBOL_NATURAL_NAME (sym));
22522 baton->size = 0;
0d53c4c4 22523 }
6e70227d 22524
f1e6e072
TT
22525 SYMBOL_ACLASS_INDEX (sym) = (is_block
22526 ? dwarf2_locexpr_block_index
22527 : dwarf2_locexpr_index);
0d53c4c4
DJ
22528 SYMBOL_LOCATION_BATON (sym) = baton;
22529 }
4c2df51b 22530}
6502dd73 22531
9aa1f1e3
TT
22532/* Return the OBJFILE associated with the compilation unit CU. If CU
22533 came from a separate debuginfo file, then the master objfile is
22534 returned. */
ae0d2f24
UW
22535
22536struct objfile *
22537dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22538{
9291a0cd 22539 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22540
22541 /* Return the master objfile, so that we can report and look up the
22542 correct file containing this variable. */
22543 if (objfile->separate_debug_objfile_backlink)
22544 objfile = objfile->separate_debug_objfile_backlink;
22545
22546 return objfile;
22547}
22548
96408a79
SA
22549/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22550 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22551 CU_HEADERP first. */
22552
22553static const struct comp_unit_head *
22554per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22555 struct dwarf2_per_cu_data *per_cu)
22556{
d521ce57 22557 const gdb_byte *info_ptr;
96408a79
SA
22558
22559 if (per_cu->cu)
22560 return &per_cu->cu->header;
22561
9c541725 22562 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22563
22564 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22565 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22566 rcuh_kind::COMPILE);
96408a79
SA
22567
22568 return cu_headerp;
22569}
22570
ae0d2f24
UW
22571/* Return the address size given in the compilation unit header for CU. */
22572
98714339 22573int
ae0d2f24
UW
22574dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22575{
96408a79
SA
22576 struct comp_unit_head cu_header_local;
22577 const struct comp_unit_head *cu_headerp;
c471e790 22578
96408a79
SA
22579 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22580
22581 return cu_headerp->addr_size;
ae0d2f24
UW
22582}
22583
9eae7c52
TT
22584/* Return the offset size given in the compilation unit header for CU. */
22585
22586int
22587dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22588{
96408a79
SA
22589 struct comp_unit_head cu_header_local;
22590 const struct comp_unit_head *cu_headerp;
9c6c53f7 22591
96408a79
SA
22592 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22593
22594 return cu_headerp->offset_size;
22595}
22596
22597/* See its dwarf2loc.h declaration. */
22598
22599int
22600dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22601{
22602 struct comp_unit_head cu_header_local;
22603 const struct comp_unit_head *cu_headerp;
22604
22605 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22606
22607 if (cu_headerp->version == 2)
22608 return cu_headerp->addr_size;
22609 else
22610 return cu_headerp->offset_size;
181cebd4
JK
22611}
22612
9aa1f1e3
TT
22613/* Return the text offset of the CU. The returned offset comes from
22614 this CU's objfile. If this objfile came from a separate debuginfo
22615 file, then the offset may be different from the corresponding
22616 offset in the parent objfile. */
22617
22618CORE_ADDR
22619dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22620{
bb3fa9d0 22621 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22622
22623 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22624}
22625
43988095
JK
22626/* Return DWARF version number of PER_CU. */
22627
22628short
22629dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22630{
22631 return per_cu->dwarf_version;
22632}
22633
348e048f
DE
22634/* Locate the .debug_info compilation unit from CU's objfile which contains
22635 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22636
22637static struct dwarf2_per_cu_data *
9c541725 22638dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22639 unsigned int offset_in_dwz,
ae038cb0
DJ
22640 struct objfile *objfile)
22641{
22642 struct dwarf2_per_cu_data *this_cu;
22643 int low, high;
36586728 22644 const sect_offset *cu_off;
ae038cb0 22645
ae038cb0
DJ
22646 low = 0;
22647 high = dwarf2_per_objfile->n_comp_units - 1;
22648 while (high > low)
22649 {
36586728 22650 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22651 int mid = low + (high - low) / 2;
9a619af0 22652
36586728 22653 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22654 cu_off = &mid_cu->sect_off;
36586728 22655 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22656 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22657 high = mid;
22658 else
22659 low = mid + 1;
22660 }
22661 gdb_assert (low == high);
36586728 22662 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22663 cu_off = &this_cu->sect_off;
22664 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22665 {
36586728 22666 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22667 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22668 "offset 0x%x [in module %s]"),
22669 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22670
9c541725
PA
22671 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22672 <= sect_off);
ae038cb0
DJ
22673 return dwarf2_per_objfile->all_comp_units[low-1];
22674 }
22675 else
22676 {
22677 this_cu = dwarf2_per_objfile->all_comp_units[low];
22678 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22679 && sect_off >= this_cu->sect_off + this_cu->length)
22680 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22681 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22682 return this_cu;
22683 }
22684}
22685
23745b47 22686/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22687
9816fde3 22688static void
23745b47 22689init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22690{
9816fde3 22691 memset (cu, 0, sizeof (*cu));
23745b47
DE
22692 per_cu->cu = cu;
22693 cu->per_cu = per_cu;
22694 cu->objfile = per_cu->objfile;
93311388 22695 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22696}
22697
22698/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22699
22700static void
95554aad
TT
22701prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22702 enum language pretend_language)
9816fde3
JK
22703{
22704 struct attribute *attr;
22705
22706 /* Set the language we're debugging. */
22707 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22708 if (attr)
22709 set_cu_language (DW_UNSND (attr), cu);
22710 else
9cded63f 22711 {
95554aad 22712 cu->language = pretend_language;
9cded63f
TT
22713 cu->language_defn = language_def (cu->language);
22714 }
dee91e82 22715
7d45c7c3 22716 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22717}
22718
ae038cb0
DJ
22719/* Release one cached compilation unit, CU. We unlink it from the tree
22720 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22721 the caller is responsible for that.
22722 NOTE: DATA is a void * because this function is also used as a
22723 cleanup routine. */
ae038cb0
DJ
22724
22725static void
68dc6402 22726free_heap_comp_unit (void *data)
ae038cb0 22727{
9a3c8263 22728 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22729
23745b47
DE
22730 gdb_assert (cu->per_cu != NULL);
22731 cu->per_cu->cu = NULL;
ae038cb0
DJ
22732 cu->per_cu = NULL;
22733
22734 obstack_free (&cu->comp_unit_obstack, NULL);
22735
22736 xfree (cu);
22737}
22738
72bf9492 22739/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22740 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22741 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22742
22743static void
22744free_stack_comp_unit (void *data)
22745{
9a3c8263 22746 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22747
23745b47
DE
22748 gdb_assert (cu->per_cu != NULL);
22749 cu->per_cu->cu = NULL;
22750 cu->per_cu = NULL;
22751
72bf9492
DJ
22752 obstack_free (&cu->comp_unit_obstack, NULL);
22753 cu->partial_dies = NULL;
ae038cb0
DJ
22754}
22755
22756/* Free all cached compilation units. */
22757
22758static void
22759free_cached_comp_units (void *data)
22760{
22761 struct dwarf2_per_cu_data *per_cu, **last_chain;
22762
22763 per_cu = dwarf2_per_objfile->read_in_chain;
22764 last_chain = &dwarf2_per_objfile->read_in_chain;
22765 while (per_cu != NULL)
22766 {
22767 struct dwarf2_per_cu_data *next_cu;
22768
22769 next_cu = per_cu->cu->read_in_chain;
22770
68dc6402 22771 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22772 *last_chain = next_cu;
22773
22774 per_cu = next_cu;
22775 }
22776}
22777
22778/* Increase the age counter on each cached compilation unit, and free
22779 any that are too old. */
22780
22781static void
22782age_cached_comp_units (void)
22783{
22784 struct dwarf2_per_cu_data *per_cu, **last_chain;
22785
22786 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22787 per_cu = dwarf2_per_objfile->read_in_chain;
22788 while (per_cu != NULL)
22789 {
22790 per_cu->cu->last_used ++;
b4f54984 22791 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22792 dwarf2_mark (per_cu->cu);
22793 per_cu = per_cu->cu->read_in_chain;
22794 }
22795
22796 per_cu = dwarf2_per_objfile->read_in_chain;
22797 last_chain = &dwarf2_per_objfile->read_in_chain;
22798 while (per_cu != NULL)
22799 {
22800 struct dwarf2_per_cu_data *next_cu;
22801
22802 next_cu = per_cu->cu->read_in_chain;
22803
22804 if (!per_cu->cu->mark)
22805 {
68dc6402 22806 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22807 *last_chain = next_cu;
22808 }
22809 else
22810 last_chain = &per_cu->cu->read_in_chain;
22811
22812 per_cu = next_cu;
22813 }
22814}
22815
22816/* Remove a single compilation unit from the cache. */
22817
22818static void
dee91e82 22819free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22820{
22821 struct dwarf2_per_cu_data *per_cu, **last_chain;
22822
22823 per_cu = dwarf2_per_objfile->read_in_chain;
22824 last_chain = &dwarf2_per_objfile->read_in_chain;
22825 while (per_cu != NULL)
22826 {
22827 struct dwarf2_per_cu_data *next_cu;
22828
22829 next_cu = per_cu->cu->read_in_chain;
22830
dee91e82 22831 if (per_cu == target_per_cu)
ae038cb0 22832 {
68dc6402 22833 free_heap_comp_unit (per_cu->cu);
dee91e82 22834 per_cu->cu = NULL;
ae038cb0
DJ
22835 *last_chain = next_cu;
22836 break;
22837 }
22838 else
22839 last_chain = &per_cu->cu->read_in_chain;
22840
22841 per_cu = next_cu;
22842 }
22843}
22844
fe3e1990
DJ
22845/* Release all extra memory associated with OBJFILE. */
22846
22847void
22848dwarf2_free_objfile (struct objfile *objfile)
22849{
9a3c8263
SM
22850 dwarf2_per_objfile
22851 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22852 dwarf2_objfile_data_key);
fe3e1990
DJ
22853
22854 if (dwarf2_per_objfile == NULL)
22855 return;
22856
22857 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22858 free_cached_comp_units (NULL);
22859
7b9f3c50
DE
22860 if (dwarf2_per_objfile->quick_file_names_table)
22861 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22862
527f3840
JK
22863 if (dwarf2_per_objfile->line_header_hash)
22864 htab_delete (dwarf2_per_objfile->line_header_hash);
22865
fe3e1990
DJ
22866 /* Everything else should be on the objfile obstack. */
22867}
22868
dee91e82
DE
22869/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22870 We store these in a hash table separate from the DIEs, and preserve them
22871 when the DIEs are flushed out of cache.
22872
22873 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22874 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22875 or the type may come from a DWO file. Furthermore, while it's more logical
22876 to use per_cu->section+offset, with Fission the section with the data is in
22877 the DWO file but we don't know that section at the point we need it.
22878 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22879 because we can enter the lookup routine, get_die_type_at_offset, from
22880 outside this file, and thus won't necessarily have PER_CU->cu.
22881 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22882
dee91e82 22883struct dwarf2_per_cu_offset_and_type
1c379e20 22884{
dee91e82 22885 const struct dwarf2_per_cu_data *per_cu;
9c541725 22886 sect_offset sect_off;
1c379e20
DJ
22887 struct type *type;
22888};
22889
dee91e82 22890/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22891
22892static hashval_t
dee91e82 22893per_cu_offset_and_type_hash (const void *item)
1c379e20 22894{
9a3c8263
SM
22895 const struct dwarf2_per_cu_offset_and_type *ofs
22896 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22897
9c541725 22898 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22899}
22900
dee91e82 22901/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22902
22903static int
dee91e82 22904per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22905{
9a3c8263
SM
22906 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22907 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22908 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22909 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22910
dee91e82 22911 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 22912 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
22913}
22914
22915/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22916 table if necessary. For convenience, return TYPE.
22917
22918 The DIEs reading must have careful ordering to:
22919 * Not cause infite loops trying to read in DIEs as a prerequisite for
22920 reading current DIE.
22921 * Not trying to dereference contents of still incompletely read in types
22922 while reading in other DIEs.
22923 * Enable referencing still incompletely read in types just by a pointer to
22924 the type without accessing its fields.
22925
22926 Therefore caller should follow these rules:
22927 * Try to fetch any prerequisite types we may need to build this DIE type
22928 before building the type and calling set_die_type.
e71ec853 22929 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22930 possible before fetching more types to complete the current type.
22931 * Make the type as complete as possible before fetching more types. */
1c379e20 22932
f792889a 22933static struct type *
1c379e20
DJ
22934set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22935{
dee91e82 22936 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22937 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22938 struct attribute *attr;
22939 struct dynamic_prop prop;
1c379e20 22940
b4ba55a1
JB
22941 /* For Ada types, make sure that the gnat-specific data is always
22942 initialized (if not already set). There are a few types where
22943 we should not be doing so, because the type-specific area is
22944 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22945 where the type-specific area is used to store the floatformat).
22946 But this is not a problem, because the gnat-specific information
22947 is actually not needed for these types. */
22948 if (need_gnat_info (cu)
22949 && TYPE_CODE (type) != TYPE_CODE_FUNC
22950 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22951 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22952 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22953 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22954 && !HAVE_GNAT_AUX_INFO (type))
22955 INIT_GNAT_SPECIFIC (type);
22956
3f2f83dd
KB
22957 /* Read DW_AT_allocated and set in type. */
22958 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22959 if (attr_form_is_block (attr))
22960 {
22961 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22962 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22963 }
22964 else if (attr != NULL)
22965 {
22966 complaint (&symfile_complaints,
9c541725
PA
22967 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22968 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22969 to_underlying (die->sect_off));
3f2f83dd
KB
22970 }
22971
22972 /* Read DW_AT_associated and set in type. */
22973 attr = dwarf2_attr (die, DW_AT_associated, cu);
22974 if (attr_form_is_block (attr))
22975 {
22976 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22977 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22978 }
22979 else if (attr != NULL)
22980 {
22981 complaint (&symfile_complaints,
9c541725
PA
22982 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22983 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22984 to_underlying (die->sect_off));
3f2f83dd
KB
22985 }
22986
3cdcd0ce
JB
22987 /* Read DW_AT_data_location and set in type. */
22988 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22989 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22990 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22991
dee91e82 22992 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22993 {
dee91e82
DE
22994 dwarf2_per_objfile->die_type_hash =
22995 htab_create_alloc_ex (127,
22996 per_cu_offset_and_type_hash,
22997 per_cu_offset_and_type_eq,
22998 NULL,
22999 &objfile->objfile_obstack,
23000 hashtab_obstack_allocate,
23001 dummy_obstack_deallocate);
f792889a 23002 }
1c379e20 23003
dee91e82 23004 ofs.per_cu = cu->per_cu;
9c541725 23005 ofs.sect_off = die->sect_off;
1c379e20 23006 ofs.type = type;
dee91e82
DE
23007 slot = (struct dwarf2_per_cu_offset_and_type **)
23008 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23009 if (*slot)
23010 complaint (&symfile_complaints,
23011 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23012 to_underlying (die->sect_off));
8d749320
SM
23013 *slot = XOBNEW (&objfile->objfile_obstack,
23014 struct dwarf2_per_cu_offset_and_type);
1c379e20 23015 **slot = ofs;
f792889a 23016 return type;
1c379e20
DJ
23017}
23018
9c541725 23019/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23020 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23021
23022static struct type *
9c541725 23023get_die_type_at_offset (sect_offset sect_off,
673bfd45 23024 struct dwarf2_per_cu_data *per_cu)
1c379e20 23025{
dee91e82 23026 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23027
dee91e82 23028 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23029 return NULL;
1c379e20 23030
dee91e82 23031 ofs.per_cu = per_cu;
9c541725 23032 ofs.sect_off = sect_off;
9a3c8263
SM
23033 slot = ((struct dwarf2_per_cu_offset_and_type *)
23034 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23035 if (slot)
23036 return slot->type;
23037 else
23038 return NULL;
23039}
23040
02142a6c 23041/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23042 or return NULL if DIE does not have a saved type. */
23043
23044static struct type *
23045get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23046{
9c541725 23047 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23048}
23049
10b3939b
DJ
23050/* Add a dependence relationship from CU to REF_PER_CU. */
23051
23052static void
23053dwarf2_add_dependence (struct dwarf2_cu *cu,
23054 struct dwarf2_per_cu_data *ref_per_cu)
23055{
23056 void **slot;
23057
23058 if (cu->dependencies == NULL)
23059 cu->dependencies
23060 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23061 NULL, &cu->comp_unit_obstack,
23062 hashtab_obstack_allocate,
23063 dummy_obstack_deallocate);
23064
23065 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23066 if (*slot == NULL)
23067 *slot = ref_per_cu;
23068}
1c379e20 23069
f504f079
DE
23070/* Subroutine of dwarf2_mark to pass to htab_traverse.
23071 Set the mark field in every compilation unit in the
ae038cb0
DJ
23072 cache that we must keep because we are keeping CU. */
23073
10b3939b
DJ
23074static int
23075dwarf2_mark_helper (void **slot, void *data)
23076{
23077 struct dwarf2_per_cu_data *per_cu;
23078
23079 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23080
23081 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23082 reading of the chain. As such dependencies remain valid it is not much
23083 useful to track and undo them during QUIT cleanups. */
23084 if (per_cu->cu == NULL)
23085 return 1;
23086
10b3939b
DJ
23087 if (per_cu->cu->mark)
23088 return 1;
23089 per_cu->cu->mark = 1;
23090
23091 if (per_cu->cu->dependencies != NULL)
23092 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23093
23094 return 1;
23095}
23096
f504f079
DE
23097/* Set the mark field in CU and in every other compilation unit in the
23098 cache that we must keep because we are keeping CU. */
23099
ae038cb0
DJ
23100static void
23101dwarf2_mark (struct dwarf2_cu *cu)
23102{
23103 if (cu->mark)
23104 return;
23105 cu->mark = 1;
10b3939b
DJ
23106 if (cu->dependencies != NULL)
23107 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23108}
23109
23110static void
23111dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23112{
23113 while (per_cu)
23114 {
23115 per_cu->cu->mark = 0;
23116 per_cu = per_cu->cu->read_in_chain;
23117 }
72bf9492
DJ
23118}
23119
72bf9492
DJ
23120/* Trivial hash function for partial_die_info: the hash value of a DIE
23121 is its offset in .debug_info for this objfile. */
23122
23123static hashval_t
23124partial_die_hash (const void *item)
23125{
9a3c8263
SM
23126 const struct partial_die_info *part_die
23127 = (const struct partial_die_info *) item;
9a619af0 23128
9c541725 23129 return to_underlying (part_die->sect_off);
72bf9492
DJ
23130}
23131
23132/* Trivial comparison function for partial_die_info structures: two DIEs
23133 are equal if they have the same offset. */
23134
23135static int
23136partial_die_eq (const void *item_lhs, const void *item_rhs)
23137{
9a3c8263
SM
23138 const struct partial_die_info *part_die_lhs
23139 = (const struct partial_die_info *) item_lhs;
23140 const struct partial_die_info *part_die_rhs
23141 = (const struct partial_die_info *) item_rhs;
9a619af0 23142
9c541725 23143 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23144}
23145
b4f54984
DE
23146static struct cmd_list_element *set_dwarf_cmdlist;
23147static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23148
23149static void
b4f54984 23150set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23151{
b4f54984 23152 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23153 gdb_stdout);
ae038cb0
DJ
23154}
23155
23156static void
b4f54984 23157show_dwarf_cmd (char *args, int from_tty)
6e70227d 23158{
b4f54984 23159 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23160}
23161
4bf44c1c 23162/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23163
23164static void
c1bd65d0 23165dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23166{
9a3c8263 23167 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23168 int ix;
8b70b953 23169
626f2d1c
TT
23170 /* Make sure we don't accidentally use dwarf2_per_objfile while
23171 cleaning up. */
23172 dwarf2_per_objfile = NULL;
23173
59b0c7c1
JB
23174 for (ix = 0; ix < data->n_comp_units; ++ix)
23175 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23176
59b0c7c1 23177 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23178 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23179 data->all_type_units[ix]->per_cu.imported_symtabs);
23180 xfree (data->all_type_units);
95554aad 23181
8b70b953 23182 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23183
23184 if (data->dwo_files)
23185 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23186 if (data->dwp_file)
23187 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23188
23189 if (data->dwz_file && data->dwz_file->dwz_bfd)
23190 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23191}
23192
23193\f
ae2de4f8 23194/* The "save gdb-index" command. */
9291a0cd
TT
23195
23196/* The contents of the hash table we create when building the string
23197 table. */
23198struct strtab_entry
23199{
23200 offset_type offset;
23201 const char *str;
23202};
23203
559a7a62
JK
23204/* Hash function for a strtab_entry.
23205
23206 Function is used only during write_hash_table so no index format backward
23207 compatibility is needed. */
b89be57b 23208
9291a0cd
TT
23209static hashval_t
23210hash_strtab_entry (const void *e)
23211{
9a3c8263 23212 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 23213 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
23214}
23215
23216/* Equality function for a strtab_entry. */
b89be57b 23217
9291a0cd
TT
23218static int
23219eq_strtab_entry (const void *a, const void *b)
23220{
9a3c8263
SM
23221 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23222 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
23223 return !strcmp (ea->str, eb->str);
23224}
23225
23226/* Create a strtab_entry hash table. */
b89be57b 23227
9291a0cd
TT
23228static htab_t
23229create_strtab (void)
23230{
23231 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23232 xfree, xcalloc, xfree);
23233}
23234
23235/* Add a string to the constant pool. Return the string's offset in
23236 host order. */
b89be57b 23237
9291a0cd
TT
23238static offset_type
23239add_string (htab_t table, struct obstack *cpool, const char *str)
23240{
23241 void **slot;
23242 struct strtab_entry entry;
23243 struct strtab_entry *result;
23244
23245 entry.str = str;
23246 slot = htab_find_slot (table, &entry, INSERT);
23247 if (*slot)
9a3c8263 23248 result = (struct strtab_entry *) *slot;
9291a0cd
TT
23249 else
23250 {
23251 result = XNEW (struct strtab_entry);
23252 result->offset = obstack_object_size (cpool);
23253 result->str = str;
23254 obstack_grow_str0 (cpool, str);
23255 *slot = result;
23256 }
23257 return result->offset;
23258}
23259
23260/* An entry in the symbol table. */
23261struct symtab_index_entry
23262{
23263 /* The name of the symbol. */
23264 const char *name;
23265 /* The offset of the name in the constant pool. */
23266 offset_type index_offset;
23267 /* A sorted vector of the indices of all the CUs that hold an object
23268 of this name. */
23269 VEC (offset_type) *cu_indices;
23270};
23271
23272/* The symbol table. This is a power-of-2-sized hash table. */
23273struct mapped_symtab
23274{
23275 offset_type n_elements;
23276 offset_type size;
23277 struct symtab_index_entry **data;
23278};
23279
23280/* Hash function for a symtab_index_entry. */
b89be57b 23281
9291a0cd
TT
23282static hashval_t
23283hash_symtab_entry (const void *e)
23284{
9a3c8263
SM
23285 const struct symtab_index_entry *entry
23286 = (const struct symtab_index_entry *) e;
9291a0cd
TT
23287 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23288 sizeof (offset_type) * VEC_length (offset_type,
23289 entry->cu_indices),
23290 0);
23291}
23292
23293/* Equality function for a symtab_index_entry. */
b89be57b 23294
9291a0cd
TT
23295static int
23296eq_symtab_entry (const void *a, const void *b)
23297{
9a3c8263
SM
23298 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23299 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
23300 int len = VEC_length (offset_type, ea->cu_indices);
23301 if (len != VEC_length (offset_type, eb->cu_indices))
23302 return 0;
23303 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23304 VEC_address (offset_type, eb->cu_indices),
23305 sizeof (offset_type) * len);
23306}
23307
23308/* Destroy a symtab_index_entry. */
b89be57b 23309
9291a0cd
TT
23310static void
23311delete_symtab_entry (void *p)
23312{
9a3c8263 23313 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
23314 VEC_free (offset_type, entry->cu_indices);
23315 xfree (entry);
23316}
23317
23318/* Create a hash table holding symtab_index_entry objects. */
b89be57b 23319
9291a0cd 23320static htab_t
3876f04e 23321create_symbol_hash_table (void)
9291a0cd
TT
23322{
23323 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23324 delete_symtab_entry, xcalloc, xfree);
23325}
23326
23327/* Create a new mapped symtab object. */
b89be57b 23328
9291a0cd
TT
23329static struct mapped_symtab *
23330create_mapped_symtab (void)
23331{
23332 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23333 symtab->n_elements = 0;
23334 symtab->size = 1024;
23335 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23336 return symtab;
23337}
23338
23339/* Destroy a mapped_symtab. */
b89be57b 23340
9291a0cd
TT
23341static void
23342cleanup_mapped_symtab (void *p)
23343{
9a3c8263 23344 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
23345 /* The contents of the array are freed when the other hash table is
23346 destroyed. */
23347 xfree (symtab->data);
23348 xfree (symtab);
23349}
23350
23351/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
23352 the slot.
23353
23354 Function is used only during write_hash_table so no index format backward
23355 compatibility is needed. */
b89be57b 23356
9291a0cd
TT
23357static struct symtab_index_entry **
23358find_slot (struct mapped_symtab *symtab, const char *name)
23359{
559a7a62 23360 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
23361
23362 index = hash & (symtab->size - 1);
23363 step = ((hash * 17) & (symtab->size - 1)) | 1;
23364
23365 for (;;)
23366 {
23367 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23368 return &symtab->data[index];
23369 index = (index + step) & (symtab->size - 1);
23370 }
23371}
23372
23373/* Expand SYMTAB's hash table. */
b89be57b 23374
9291a0cd
TT
23375static void
23376hash_expand (struct mapped_symtab *symtab)
23377{
23378 offset_type old_size = symtab->size;
23379 offset_type i;
23380 struct symtab_index_entry **old_entries = symtab->data;
23381
23382 symtab->size *= 2;
23383 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23384
23385 for (i = 0; i < old_size; ++i)
23386 {
23387 if (old_entries[i])
23388 {
23389 struct symtab_index_entry **slot = find_slot (symtab,
23390 old_entries[i]->name);
23391 *slot = old_entries[i];
23392 }
23393 }
23394
23395 xfree (old_entries);
23396}
23397
156942c7
DE
23398/* Add an entry to SYMTAB. NAME is the name of the symbol.
23399 CU_INDEX is the index of the CU in which the symbol appears.
23400 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23401
9291a0cd
TT
23402static void
23403add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23404 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23405 offset_type cu_index)
23406{
23407 struct symtab_index_entry **slot;
156942c7 23408 offset_type cu_index_and_attrs;
9291a0cd
TT
23409
23410 ++symtab->n_elements;
23411 if (4 * symtab->n_elements / 3 >= symtab->size)
23412 hash_expand (symtab);
23413
23414 slot = find_slot (symtab, name);
23415 if (!*slot)
23416 {
23417 *slot = XNEW (struct symtab_index_entry);
23418 (*slot)->name = name;
156942c7 23419 /* index_offset is set later. */
9291a0cd
TT
23420 (*slot)->cu_indices = NULL;
23421 }
156942c7
DE
23422
23423 cu_index_and_attrs = 0;
23424 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23425 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23426 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23427
23428 /* We don't want to record an index value twice as we want to avoid the
23429 duplication.
23430 We process all global symbols and then all static symbols
23431 (which would allow us to avoid the duplication by only having to check
23432 the last entry pushed), but a symbol could have multiple kinds in one CU.
23433 To keep things simple we don't worry about the duplication here and
23434 sort and uniqufy the list after we've processed all symbols. */
23435 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23436}
23437
23438/* qsort helper routine for uniquify_cu_indices. */
23439
23440static int
23441offset_type_compare (const void *ap, const void *bp)
23442{
23443 offset_type a = *(offset_type *) ap;
23444 offset_type b = *(offset_type *) bp;
23445
23446 return (a > b) - (b > a);
23447}
23448
23449/* Sort and remove duplicates of all symbols' cu_indices lists. */
23450
23451static void
23452uniquify_cu_indices (struct mapped_symtab *symtab)
23453{
23454 int i;
23455
23456 for (i = 0; i < symtab->size; ++i)
23457 {
23458 struct symtab_index_entry *entry = symtab->data[i];
23459
23460 if (entry
23461 && entry->cu_indices != NULL)
23462 {
23463 unsigned int next_to_insert, next_to_check;
23464 offset_type last_value;
23465
23466 qsort (VEC_address (offset_type, entry->cu_indices),
23467 VEC_length (offset_type, entry->cu_indices),
23468 sizeof (offset_type), offset_type_compare);
23469
23470 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23471 next_to_insert = 1;
23472 for (next_to_check = 1;
23473 next_to_check < VEC_length (offset_type, entry->cu_indices);
23474 ++next_to_check)
23475 {
23476 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23477 != last_value)
23478 {
23479 last_value = VEC_index (offset_type, entry->cu_indices,
23480 next_to_check);
23481 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23482 last_value);
23483 ++next_to_insert;
23484 }
23485 }
23486 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23487 }
23488 }
9291a0cd
TT
23489}
23490
23491/* Add a vector of indices to the constant pool. */
b89be57b 23492
9291a0cd 23493static offset_type
3876f04e 23494add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
23495 struct symtab_index_entry *entry)
23496{
23497 void **slot;
23498
3876f04e 23499 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
23500 if (!*slot)
23501 {
23502 offset_type len = VEC_length (offset_type, entry->cu_indices);
23503 offset_type val = MAYBE_SWAP (len);
23504 offset_type iter;
23505 int i;
23506
23507 *slot = entry;
23508 entry->index_offset = obstack_object_size (cpool);
23509
23510 obstack_grow (cpool, &val, sizeof (val));
23511 for (i = 0;
23512 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23513 ++i)
23514 {
23515 val = MAYBE_SWAP (iter);
23516 obstack_grow (cpool, &val, sizeof (val));
23517 }
23518 }
23519 else
23520 {
9a3c8263
SM
23521 struct symtab_index_entry *old_entry
23522 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
23523 entry->index_offset = old_entry->index_offset;
23524 entry = old_entry;
23525 }
23526 return entry->index_offset;
23527}
23528
23529/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23530 constant pool entries going into the obstack CPOOL. */
b89be57b 23531
9291a0cd
TT
23532static void
23533write_hash_table (struct mapped_symtab *symtab,
23534 struct obstack *output, struct obstack *cpool)
23535{
23536 offset_type i;
3876f04e 23537 htab_t symbol_hash_table;
9291a0cd
TT
23538 htab_t str_table;
23539
3876f04e 23540 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23541 str_table = create_strtab ();
3876f04e 23542
9291a0cd
TT
23543 /* We add all the index vectors to the constant pool first, to
23544 ensure alignment is ok. */
23545 for (i = 0; i < symtab->size; ++i)
23546 {
23547 if (symtab->data[i])
3876f04e 23548 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23549 }
23550
23551 /* Now write out the hash table. */
23552 for (i = 0; i < symtab->size; ++i)
23553 {
23554 offset_type str_off, vec_off;
23555
23556 if (symtab->data[i])
23557 {
23558 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23559 vec_off = symtab->data[i]->index_offset;
23560 }
23561 else
23562 {
23563 /* While 0 is a valid constant pool index, it is not valid
23564 to have 0 for both offsets. */
23565 str_off = 0;
23566 vec_off = 0;
23567 }
23568
23569 str_off = MAYBE_SWAP (str_off);
23570 vec_off = MAYBE_SWAP (vec_off);
23571
23572 obstack_grow (output, &str_off, sizeof (str_off));
23573 obstack_grow (output, &vec_off, sizeof (vec_off));
23574 }
23575
23576 htab_delete (str_table);
3876f04e 23577 htab_delete (symbol_hash_table);
9291a0cd
TT
23578}
23579
0a5429f6
DE
23580/* Struct to map psymtab to CU index in the index file. */
23581struct psymtab_cu_index_map
23582{
23583 struct partial_symtab *psymtab;
23584 unsigned int cu_index;
23585};
23586
23587static hashval_t
23588hash_psymtab_cu_index (const void *item)
23589{
9a3c8263
SM
23590 const struct psymtab_cu_index_map *map
23591 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23592
23593 return htab_hash_pointer (map->psymtab);
23594}
23595
23596static int
23597eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23598{
9a3c8263
SM
23599 const struct psymtab_cu_index_map *lhs
23600 = (const struct psymtab_cu_index_map *) item_lhs;
23601 const struct psymtab_cu_index_map *rhs
23602 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23603
23604 return lhs->psymtab == rhs->psymtab;
23605}
23606
23607/* Helper struct for building the address table. */
23608struct addrmap_index_data
23609{
23610 struct objfile *objfile;
23611 struct obstack *addr_obstack;
23612 htab_t cu_index_htab;
23613
23614 /* Non-zero if the previous_* fields are valid.
23615 We can't write an entry until we see the next entry (since it is only then
23616 that we know the end of the entry). */
23617 int previous_valid;
23618 /* Index of the CU in the table of all CUs in the index file. */
23619 unsigned int previous_cu_index;
0963b4bd 23620 /* Start address of the CU. */
0a5429f6
DE
23621 CORE_ADDR previous_cu_start;
23622};
23623
23624/* Write an address entry to OBSTACK. */
b89be57b 23625
9291a0cd 23626static void
0a5429f6
DE
23627add_address_entry (struct objfile *objfile, struct obstack *obstack,
23628 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23629{
0a5429f6 23630 offset_type cu_index_to_write;
948f8e3d 23631 gdb_byte addr[8];
9291a0cd
TT
23632 CORE_ADDR baseaddr;
23633
23634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23635
0a5429f6
DE
23636 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23637 obstack_grow (obstack, addr, 8);
23638 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23639 obstack_grow (obstack, addr, 8);
23640 cu_index_to_write = MAYBE_SWAP (cu_index);
23641 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23642}
23643
23644/* Worker function for traversing an addrmap to build the address table. */
23645
23646static int
23647add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23648{
9a3c8263
SM
23649 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23650 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23651
23652 if (data->previous_valid)
23653 add_address_entry (data->objfile, data->addr_obstack,
23654 data->previous_cu_start, start_addr,
23655 data->previous_cu_index);
23656
23657 data->previous_cu_start = start_addr;
23658 if (pst != NULL)
23659 {
23660 struct psymtab_cu_index_map find_map, *map;
23661 find_map.psymtab = pst;
9a3c8263
SM
23662 map = ((struct psymtab_cu_index_map *)
23663 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23664 gdb_assert (map != NULL);
23665 data->previous_cu_index = map->cu_index;
23666 data->previous_valid = 1;
23667 }
23668 else
23669 data->previous_valid = 0;
23670
23671 return 0;
23672}
23673
23674/* Write OBJFILE's address map to OBSTACK.
23675 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23676 in the index file. */
23677
23678static void
23679write_address_map (struct objfile *objfile, struct obstack *obstack,
23680 htab_t cu_index_htab)
23681{
23682 struct addrmap_index_data addrmap_index_data;
23683
23684 /* When writing the address table, we have to cope with the fact that
23685 the addrmap iterator only provides the start of a region; we have to
23686 wait until the next invocation to get the start of the next region. */
23687
23688 addrmap_index_data.objfile = objfile;
23689 addrmap_index_data.addr_obstack = obstack;
23690 addrmap_index_data.cu_index_htab = cu_index_htab;
23691 addrmap_index_data.previous_valid = 0;
23692
23693 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23694 &addrmap_index_data);
23695
23696 /* It's highly unlikely the last entry (end address = 0xff...ff)
23697 is valid, but we should still handle it.
23698 The end address is recorded as the start of the next region, but that
23699 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23700 anyway. */
23701 if (addrmap_index_data.previous_valid)
23702 add_address_entry (objfile, obstack,
23703 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23704 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23705}
23706
156942c7
DE
23707/* Return the symbol kind of PSYM. */
23708
23709static gdb_index_symbol_kind
23710symbol_kind (struct partial_symbol *psym)
23711{
23712 domain_enum domain = PSYMBOL_DOMAIN (psym);
23713 enum address_class aclass = PSYMBOL_CLASS (psym);
23714
23715 switch (domain)
23716 {
23717 case VAR_DOMAIN:
23718 switch (aclass)
23719 {
23720 case LOC_BLOCK:
23721 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23722 case LOC_TYPEDEF:
23723 return GDB_INDEX_SYMBOL_KIND_TYPE;
23724 case LOC_COMPUTED:
23725 case LOC_CONST_BYTES:
23726 case LOC_OPTIMIZED_OUT:
23727 case LOC_STATIC:
23728 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23729 case LOC_CONST:
23730 /* Note: It's currently impossible to recognize psyms as enum values
23731 short of reading the type info. For now punt. */
23732 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23733 default:
23734 /* There are other LOC_FOO values that one might want to classify
23735 as variables, but dwarf2read.c doesn't currently use them. */
23736 return GDB_INDEX_SYMBOL_KIND_OTHER;
23737 }
23738 case STRUCT_DOMAIN:
23739 return GDB_INDEX_SYMBOL_KIND_TYPE;
23740 default:
23741 return GDB_INDEX_SYMBOL_KIND_OTHER;
23742 }
23743}
23744
9291a0cd 23745/* Add a list of partial symbols to SYMTAB. */
b89be57b 23746
9291a0cd
TT
23747static void
23748write_psymbols (struct mapped_symtab *symtab,
987d643c 23749 htab_t psyms_seen,
9291a0cd
TT
23750 struct partial_symbol **psymp,
23751 int count,
987d643c
TT
23752 offset_type cu_index,
23753 int is_static)
9291a0cd
TT
23754{
23755 for (; count-- > 0; ++psymp)
23756 {
156942c7
DE
23757 struct partial_symbol *psym = *psymp;
23758 void **slot;
987d643c 23759
156942c7 23760 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23761 error (_("Ada is not currently supported by the index"));
987d643c 23762
987d643c 23763 /* Only add a given psymbol once. */
156942c7 23764 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23765 if (!*slot)
23766 {
156942c7
DE
23767 gdb_index_symbol_kind kind = symbol_kind (psym);
23768
23769 *slot = psym;
23770 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23771 is_static, kind, cu_index);
987d643c 23772 }
9291a0cd
TT
23773 }
23774}
23775
23776/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23777 exception if there is an error. */
b89be57b 23778
9291a0cd
TT
23779static void
23780write_obstack (FILE *file, struct obstack *obstack)
23781{
23782 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23783 file)
23784 != obstack_object_size (obstack))
23785 error (_("couldn't data write to file"));
23786}
23787
1fd400ff
TT
23788/* A helper struct used when iterating over debug_types. */
23789struct signatured_type_index_data
23790{
23791 struct objfile *objfile;
23792 struct mapped_symtab *symtab;
23793 struct obstack *types_list;
987d643c 23794 htab_t psyms_seen;
1fd400ff
TT
23795 int cu_index;
23796};
23797
23798/* A helper function that writes a single signatured_type to an
23799 obstack. */
b89be57b 23800
1fd400ff
TT
23801static int
23802write_one_signatured_type (void **slot, void *d)
23803{
9a3c8263
SM
23804 struct signatured_type_index_data *info
23805 = (struct signatured_type_index_data *) d;
1fd400ff 23806 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23807 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23808 gdb_byte val[8];
23809
23810 write_psymbols (info->symtab,
987d643c 23811 info->psyms_seen,
3e43a32a
MS
23812 info->objfile->global_psymbols.list
23813 + psymtab->globals_offset,
987d643c
TT
23814 psymtab->n_global_syms, info->cu_index,
23815 0);
1fd400ff 23816 write_psymbols (info->symtab,
987d643c 23817 info->psyms_seen,
3e43a32a
MS
23818 info->objfile->static_psymbols.list
23819 + psymtab->statics_offset,
987d643c
TT
23820 psymtab->n_static_syms, info->cu_index,
23821 1);
1fd400ff 23822
b64f50a1 23823 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23824 to_underlying (entry->per_cu.sect_off));
1fd400ff 23825 obstack_grow (info->types_list, val, 8);
3019eac3 23826 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23827 to_underlying (entry->type_offset_in_tu));
1fd400ff
TT
23828 obstack_grow (info->types_list, val, 8);
23829 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23830 obstack_grow (info->types_list, val, 8);
23831
23832 ++info->cu_index;
23833
23834 return 1;
23835}
23836
95554aad
TT
23837/* Recurse into all "included" dependencies and write their symbols as
23838 if they appeared in this psymtab. */
23839
23840static void
23841recursively_write_psymbols (struct objfile *objfile,
23842 struct partial_symtab *psymtab,
23843 struct mapped_symtab *symtab,
23844 htab_t psyms_seen,
23845 offset_type cu_index)
23846{
23847 int i;
23848
23849 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23850 if (psymtab->dependencies[i]->user != NULL)
23851 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23852 symtab, psyms_seen, cu_index);
23853
23854 write_psymbols (symtab,
23855 psyms_seen,
23856 objfile->global_psymbols.list + psymtab->globals_offset,
23857 psymtab->n_global_syms, cu_index,
23858 0);
23859 write_psymbols (symtab,
23860 psyms_seen,
23861 objfile->static_psymbols.list + psymtab->statics_offset,
23862 psymtab->n_static_syms, cu_index,
23863 1);
23864}
23865
9291a0cd 23866/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23867
9291a0cd
TT
23868static void
23869write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23870{
23871 struct cleanup *cleanup;
bef155c3 23872 char *filename;
1fd400ff
TT
23873 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23874 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23875 int i;
23876 FILE *out_file;
23877 struct mapped_symtab *symtab;
23878 offset_type val, size_of_contents, total_len;
23879 struct stat st;
0a5429f6 23880 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23881
9291a0cd
TT
23882 if (dwarf2_per_objfile->using_index)
23883 error (_("Cannot use an index to create the index"));
23884
8b70b953
TT
23885 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23886 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23887
260b681b
DE
23888 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23889 return;
23890
4262abfb
JK
23891 if (stat (objfile_name (objfile), &st) < 0)
23892 perror_with_name (objfile_name (objfile));
9291a0cd 23893
4262abfb 23894 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23895 INDEX_SUFFIX, (char *) NULL);
23896 cleanup = make_cleanup (xfree, filename);
23897
614c279d 23898 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23899 if (!out_file)
23900 error (_("Can't open `%s' for writing"), filename);
23901
bef155c3 23902 gdb::unlinker unlink_file (filename);
9291a0cd
TT
23903
23904 symtab = create_mapped_symtab ();
23905 make_cleanup (cleanup_mapped_symtab, symtab);
23906
23907 obstack_init (&addr_obstack);
23908 make_cleanup_obstack_free (&addr_obstack);
23909
23910 obstack_init (&cu_list);
23911 make_cleanup_obstack_free (&cu_list);
23912
1fd400ff
TT
23913 obstack_init (&types_cu_list);
23914 make_cleanup_obstack_free (&types_cu_list);
23915
fc4007c9
TT
23916 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23917 htab_eq_pointer,
23918 NULL, xcalloc, xfree));
987d643c 23919
0a5429f6
DE
23920 /* While we're scanning CU's create a table that maps a psymtab pointer
23921 (which is what addrmap records) to its index (which is what is recorded
23922 in the index file). This will later be needed to write the address
23923 table. */
fc4007c9
TT
23924 htab_up cu_index_htab (htab_create_alloc (100,
23925 hash_psymtab_cu_index,
23926 eq_psymtab_cu_index,
23927 NULL, xcalloc, xfree));
8d749320
SM
23928 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23929 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23930 make_cleanup (xfree, psymtab_cu_index_map);
23931
23932 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23933 work here. Also, the debug_types entries do not appear in
23934 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23935 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23936 {
3e43a32a
MS
23937 struct dwarf2_per_cu_data *per_cu
23938 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23939 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23940 gdb_byte val[8];
0a5429f6
DE
23941 struct psymtab_cu_index_map *map;
23942 void **slot;
9291a0cd 23943
92fac807
JK
23944 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23945 It may be referenced from a local scope but in such case it does not
23946 need to be present in .gdb_index. */
23947 if (psymtab == NULL)
23948 continue;
23949
95554aad 23950 if (psymtab->user == NULL)
fc4007c9
TT
23951 recursively_write_psymbols (objfile, psymtab, symtab,
23952 psyms_seen.get (), i);
9291a0cd 23953
0a5429f6
DE
23954 map = &psymtab_cu_index_map[i];
23955 map->psymtab = psymtab;
23956 map->cu_index = i;
fc4007c9 23957 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23958 gdb_assert (slot != NULL);
23959 gdb_assert (*slot == NULL);
23960 *slot = map;
9291a0cd 23961
b64f50a1 23962 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23963 to_underlying (per_cu->sect_off));
9291a0cd 23964 obstack_grow (&cu_list, val, 8);
e254ef6a 23965 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23966 obstack_grow (&cu_list, val, 8);
23967 }
23968
0a5429f6 23969 /* Dump the address map. */
fc4007c9 23970 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23971
1fd400ff
TT
23972 /* Write out the .debug_type entries, if any. */
23973 if (dwarf2_per_objfile->signatured_types)
23974 {
23975 struct signatured_type_index_data sig_data;
23976
23977 sig_data.objfile = objfile;
23978 sig_data.symtab = symtab;
23979 sig_data.types_list = &types_cu_list;
fc4007c9 23980 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
23981 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23982 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23983 write_one_signatured_type, &sig_data);
23984 }
23985
156942c7
DE
23986 /* Now that we've processed all symbols we can shrink their cu_indices
23987 lists. */
23988 uniquify_cu_indices (symtab);
23989
9291a0cd
TT
23990 obstack_init (&constant_pool);
23991 make_cleanup_obstack_free (&constant_pool);
23992 obstack_init (&symtab_obstack);
23993 make_cleanup_obstack_free (&symtab_obstack);
23994 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23995
23996 obstack_init (&contents);
23997 make_cleanup_obstack_free (&contents);
1fd400ff 23998 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23999 total_len = size_of_contents;
24000
24001 /* The version number. */
796a7ff8 24002 val = MAYBE_SWAP (8);
9291a0cd
TT
24003 obstack_grow (&contents, &val, sizeof (val));
24004
24005 /* The offset of the CU list from the start of the file. */
24006 val = MAYBE_SWAP (total_len);
24007 obstack_grow (&contents, &val, sizeof (val));
24008 total_len += obstack_object_size (&cu_list);
24009
1fd400ff
TT
24010 /* The offset of the types CU list from the start of the file. */
24011 val = MAYBE_SWAP (total_len);
24012 obstack_grow (&contents, &val, sizeof (val));
24013 total_len += obstack_object_size (&types_cu_list);
24014
9291a0cd
TT
24015 /* The offset of the address table from the start of the file. */
24016 val = MAYBE_SWAP (total_len);
24017 obstack_grow (&contents, &val, sizeof (val));
24018 total_len += obstack_object_size (&addr_obstack);
24019
24020 /* The offset of the symbol table from the start of the file. */
24021 val = MAYBE_SWAP (total_len);
24022 obstack_grow (&contents, &val, sizeof (val));
24023 total_len += obstack_object_size (&symtab_obstack);
24024
24025 /* The offset of the constant pool from the start of the file. */
24026 val = MAYBE_SWAP (total_len);
24027 obstack_grow (&contents, &val, sizeof (val));
24028 total_len += obstack_object_size (&constant_pool);
24029
24030 gdb_assert (obstack_object_size (&contents) == size_of_contents);
24031
24032 write_obstack (out_file, &contents);
24033 write_obstack (out_file, &cu_list);
1fd400ff 24034 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
24035 write_obstack (out_file, &addr_obstack);
24036 write_obstack (out_file, &symtab_obstack);
24037 write_obstack (out_file, &constant_pool);
24038
24039 fclose (out_file);
24040
bef155c3
TT
24041 /* We want to keep the file. */
24042 unlink_file.keep ();
9291a0cd
TT
24043
24044 do_cleanups (cleanup);
24045}
24046
90476074
TT
24047/* Implementation of the `save gdb-index' command.
24048
24049 Note that the file format used by this command is documented in the
24050 GDB manual. Any changes here must be documented there. */
11570e71 24051
9291a0cd
TT
24052static void
24053save_gdb_index_command (char *arg, int from_tty)
24054{
24055 struct objfile *objfile;
24056
24057 if (!arg || !*arg)
96d19272 24058 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24059
24060 ALL_OBJFILES (objfile)
24061 {
24062 struct stat st;
24063
24064 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24065 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24066 continue;
24067
9a3c8263
SM
24068 dwarf2_per_objfile
24069 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24070 dwarf2_objfile_data_key);
9291a0cd
TT
24071 if (dwarf2_per_objfile)
24072 {
9291a0cd 24073
492d29ea 24074 TRY
9291a0cd
TT
24075 {
24076 write_psymtabs_to_index (objfile, arg);
24077 }
492d29ea
PA
24078 CATCH (except, RETURN_MASK_ERROR)
24079 {
24080 exception_fprintf (gdb_stderr, except,
24081 _("Error while writing index for `%s': "),
24082 objfile_name (objfile));
24083 }
24084 END_CATCH
9291a0cd
TT
24085 }
24086 }
dce234bc
PP
24087}
24088
9291a0cd
TT
24089\f
24090
b4f54984 24091int dwarf_always_disassemble;
9eae7c52
TT
24092
24093static void
b4f54984
DE
24094show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24095 struct cmd_list_element *c, const char *value)
9eae7c52 24096{
3e43a32a
MS
24097 fprintf_filtered (file,
24098 _("Whether to always disassemble "
24099 "DWARF expressions is %s.\n"),
9eae7c52
TT
24100 value);
24101}
24102
900e11f9
JK
24103static void
24104show_check_physname (struct ui_file *file, int from_tty,
24105 struct cmd_list_element *c, const char *value)
24106{
24107 fprintf_filtered (file,
24108 _("Whether to check \"physname\" is %s.\n"),
24109 value);
24110}
24111
6502dd73
DJ
24112void _initialize_dwarf2_read (void);
24113
24114void
24115_initialize_dwarf2_read (void)
24116{
96d19272
JK
24117 struct cmd_list_element *c;
24118
dce234bc 24119 dwarf2_objfile_data_key
c1bd65d0 24120 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24121
b4f54984
DE
24122 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24123Set DWARF specific variables.\n\
24124Configure DWARF variables such as the cache size"),
24125 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24126 0/*allow-unknown*/, &maintenance_set_cmdlist);
24127
b4f54984
DE
24128 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24129Show DWARF specific variables\n\
24130Show DWARF variables such as the cache size"),
24131 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24132 0/*allow-unknown*/, &maintenance_show_cmdlist);
24133
24134 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24135 &dwarf_max_cache_age, _("\
24136Set the upper bound on the age of cached DWARF compilation units."), _("\
24137Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24138A higher limit means that cached compilation units will be stored\n\
24139in memory longer, and more total memory will be used. Zero disables\n\
24140caching, which can slow down startup."),
2c5b56ce 24141 NULL,
b4f54984
DE
24142 show_dwarf_max_cache_age,
24143 &set_dwarf_cmdlist,
24144 &show_dwarf_cmdlist);
d97bc12b 24145
9eae7c52 24146 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24147 &dwarf_always_disassemble, _("\
9eae7c52
TT
24148Set whether `info address' always disassembles DWARF expressions."), _("\
24149Show whether `info address' always disassembles DWARF expressions."), _("\
24150When enabled, DWARF expressions are always printed in an assembly-like\n\
24151syntax. When disabled, expressions will be printed in a more\n\
24152conversational style, when possible."),
24153 NULL,
b4f54984
DE
24154 show_dwarf_always_disassemble,
24155 &set_dwarf_cmdlist,
24156 &show_dwarf_cmdlist);
24157
24158 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24159Set debugging of the DWARF reader."), _("\
24160Show debugging of the DWARF reader."), _("\
24161When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24162reading and symtab expansion. A value of 1 (one) provides basic\n\
24163information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24164 NULL,
24165 NULL,
24166 &setdebuglist, &showdebuglist);
24167
b4f54984
DE
24168 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24169Set debugging of the DWARF DIE reader."), _("\
24170Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24171When enabled (non-zero), DIEs are dumped after they are read in.\n\
24172The value is the maximum depth to print."),
ccce17b0
YQ
24173 NULL,
24174 NULL,
24175 &setdebuglist, &showdebuglist);
9291a0cd 24176
27e0867f
DE
24177 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24178Set debugging of the dwarf line reader."), _("\
24179Show debugging of the dwarf line reader."), _("\
24180When enabled (non-zero), line number entries are dumped as they are read in.\n\
24181A value of 1 (one) provides basic information.\n\
24182A value greater than 1 provides more verbose information."),
24183 NULL,
24184 NULL,
24185 &setdebuglist, &showdebuglist);
24186
900e11f9
JK
24187 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24188Set cross-checking of \"physname\" code against demangler."), _("\
24189Show cross-checking of \"physname\" code against demangler."), _("\
24190When enabled, GDB's internal \"physname\" code is checked against\n\
24191the demangler."),
24192 NULL, show_check_physname,
24193 &setdebuglist, &showdebuglist);
24194
e615022a
DE
24195 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24196 no_class, &use_deprecated_index_sections, _("\
24197Set whether to use deprecated gdb_index sections."), _("\
24198Show whether to use deprecated gdb_index sections."), _("\
24199When enabled, deprecated .gdb_index sections are used anyway.\n\
24200Normally they are ignored either because of a missing feature or\n\
24201performance issue.\n\
24202Warning: This option must be enabled before gdb reads the file."),
24203 NULL,
24204 NULL,
24205 &setlist, &showlist);
24206
96d19272 24207 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24208 _("\
fc1a9d6e 24209Save a gdb-index file.\n\
11570e71 24210Usage: save gdb-index DIRECTORY"),
96d19272
JK
24211 &save_cmdlist);
24212 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24213
24214 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24215 &dwarf2_locexpr_funcs);
24216 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24217 &dwarf2_loclist_funcs);
24218
24219 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24220 &dwarf2_block_frame_base_locexpr_funcs);
24221 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24222 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24223}
This page took 4.832377 seconds and 4 git commands to generate.