dwarf2read.c: C++fy lnp_state_machine
[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
a14ed312 1828static 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);
2888 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
TT
15127 case DW_ATE_UTF:
15128 /* We just treat this as an integer and then recognize the
15129 type by name elsewhere. */
19f392bc 15130 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
15131 break;
15132
6ccb9162
UW
15133 default:
15134 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15135 dwarf_type_encoding_name (encoding));
19f392bc
UW
15136 type = init_type (objfile, TYPE_CODE_ERROR,
15137 bits / TARGET_CHAR_BIT, name);
6ccb9162 15138 break;
c906108c 15139 }
6ccb9162 15140
0114d602 15141 if (name && strcmp (name, "char") == 0)
876cecd0 15142 TYPE_NOSIGN (type) = 1;
0114d602 15143
f792889a 15144 return set_die_type (die, type, cu);
c906108c
SS
15145}
15146
80180f79
SA
15147/* Parse dwarf attribute if it's a block, reference or constant and put the
15148 resulting value of the attribute into struct bound_prop.
15149 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15150
15151static int
15152attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15153 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15154{
15155 struct dwarf2_property_baton *baton;
15156 struct obstack *obstack = &cu->objfile->objfile_obstack;
15157
15158 if (attr == NULL || prop == NULL)
15159 return 0;
15160
15161 if (attr_form_is_block (attr))
15162 {
8d749320 15163 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15164 baton->referenced_type = NULL;
15165 baton->locexpr.per_cu = cu->per_cu;
15166 baton->locexpr.size = DW_BLOCK (attr)->size;
15167 baton->locexpr.data = DW_BLOCK (attr)->data;
15168 prop->data.baton = baton;
15169 prop->kind = PROP_LOCEXPR;
15170 gdb_assert (prop->data.baton != NULL);
15171 }
15172 else if (attr_form_is_ref (attr))
15173 {
15174 struct dwarf2_cu *target_cu = cu;
15175 struct die_info *target_die;
15176 struct attribute *target_attr;
15177
15178 target_die = follow_die_ref (die, attr, &target_cu);
15179 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15180 if (target_attr == NULL)
15181 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15182 target_cu);
80180f79
SA
15183 if (target_attr == NULL)
15184 return 0;
15185
df25ebbd 15186 switch (target_attr->name)
80180f79 15187 {
df25ebbd
JB
15188 case DW_AT_location:
15189 if (attr_form_is_section_offset (target_attr))
15190 {
8d749320 15191 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15192 baton->referenced_type = die_type (target_die, target_cu);
15193 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15194 prop->data.baton = baton;
15195 prop->kind = PROP_LOCLIST;
15196 gdb_assert (prop->data.baton != NULL);
15197 }
15198 else if (attr_form_is_block (target_attr))
15199 {
8d749320 15200 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15201 baton->referenced_type = die_type (target_die, target_cu);
15202 baton->locexpr.per_cu = cu->per_cu;
15203 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15204 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15205 prop->data.baton = baton;
15206 prop->kind = PROP_LOCEXPR;
15207 gdb_assert (prop->data.baton != NULL);
15208 }
15209 else
15210 {
15211 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15212 "dynamic property");
15213 return 0;
15214 }
15215 break;
15216 case DW_AT_data_member_location:
15217 {
15218 LONGEST offset;
15219
15220 if (!handle_data_member_location (target_die, target_cu,
15221 &offset))
15222 return 0;
15223
8d749320 15224 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15225 baton->referenced_type = read_type_die (target_die->parent,
15226 target_cu);
df25ebbd
JB
15227 baton->offset_info.offset = offset;
15228 baton->offset_info.type = die_type (target_die, target_cu);
15229 prop->data.baton = baton;
15230 prop->kind = PROP_ADDR_OFFSET;
15231 break;
15232 }
80180f79
SA
15233 }
15234 }
15235 else if (attr_form_is_constant (attr))
15236 {
15237 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15238 prop->kind = PROP_CONST;
15239 }
15240 else
15241 {
15242 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15243 dwarf2_name (die, cu));
15244 return 0;
15245 }
15246
15247 return 1;
15248}
15249
a02abb62
JB
15250/* Read the given DW_AT_subrange DIE. */
15251
f792889a 15252static struct type *
a02abb62
JB
15253read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15254{
4c9ad8c2 15255 struct type *base_type, *orig_base_type;
a02abb62
JB
15256 struct type *range_type;
15257 struct attribute *attr;
729efb13 15258 struct dynamic_prop low, high;
4fae6e18 15259 int low_default_is_valid;
c451ebe5 15260 int high_bound_is_count = 0;
15d034d0 15261 const char *name;
43bbcdc2 15262 LONGEST negative_mask;
e77813c8 15263
4c9ad8c2
TT
15264 orig_base_type = die_type (die, cu);
15265 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15266 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15267 creating the range type, but we use the result of check_typedef
15268 when examining properties of the type. */
15269 base_type = check_typedef (orig_base_type);
a02abb62 15270
7e314c57
JK
15271 /* The die_type call above may have already set the type for this DIE. */
15272 range_type = get_die_type (die, cu);
15273 if (range_type)
15274 return range_type;
15275
729efb13
SA
15276 low.kind = PROP_CONST;
15277 high.kind = PROP_CONST;
15278 high.data.const_val = 0;
15279
4fae6e18
JK
15280 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15281 omitting DW_AT_lower_bound. */
15282 switch (cu->language)
6e70227d 15283 {
4fae6e18
JK
15284 case language_c:
15285 case language_cplus:
729efb13 15286 low.data.const_val = 0;
4fae6e18
JK
15287 low_default_is_valid = 1;
15288 break;
15289 case language_fortran:
729efb13 15290 low.data.const_val = 1;
4fae6e18
JK
15291 low_default_is_valid = 1;
15292 break;
15293 case language_d:
4fae6e18 15294 case language_objc:
c44af4eb 15295 case language_rust:
729efb13 15296 low.data.const_val = 0;
4fae6e18
JK
15297 low_default_is_valid = (cu->header.version >= 4);
15298 break;
15299 case language_ada:
15300 case language_m2:
15301 case language_pascal:
729efb13 15302 low.data.const_val = 1;
4fae6e18
JK
15303 low_default_is_valid = (cu->header.version >= 4);
15304 break;
15305 default:
729efb13 15306 low.data.const_val = 0;
4fae6e18
JK
15307 low_default_is_valid = 0;
15308 break;
a02abb62
JB
15309 }
15310
e142c38c 15311 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15312 if (attr)
11c1ba78 15313 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15314 else if (!low_default_is_valid)
15315 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15316 "- DIE at 0x%x [in module %s]"),
9c541725 15317 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15318
e142c38c 15319 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15320 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15321 {
15322 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15323 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15324 {
c451ebe5
SA
15325 /* If bounds are constant do the final calculation here. */
15326 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15327 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15328 else
15329 high_bound_is_count = 1;
c2ff108b 15330 }
e77813c8
PM
15331 }
15332
15333 /* Dwarf-2 specifications explicitly allows to create subrange types
15334 without specifying a base type.
15335 In that case, the base type must be set to the type of
15336 the lower bound, upper bound or count, in that order, if any of these
15337 three attributes references an object that has a type.
15338 If no base type is found, the Dwarf-2 specifications say that
15339 a signed integer type of size equal to the size of an address should
15340 be used.
15341 For the following C code: `extern char gdb_int [];'
15342 GCC produces an empty range DIE.
15343 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15344 high bound or count are not yet handled by this code. */
e77813c8
PM
15345 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15346 {
15347 struct objfile *objfile = cu->objfile;
15348 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15349 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15350 struct type *int_type = objfile_type (objfile)->builtin_int;
15351
15352 /* Test "int", "long int", and "long long int" objfile types,
15353 and select the first one having a size above or equal to the
15354 architecture address size. */
15355 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15356 base_type = int_type;
15357 else
15358 {
15359 int_type = objfile_type (objfile)->builtin_long;
15360 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15361 base_type = int_type;
15362 else
15363 {
15364 int_type = objfile_type (objfile)->builtin_long_long;
15365 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15366 base_type = int_type;
15367 }
15368 }
15369 }
a02abb62 15370
dbb9c2b1
JB
15371 /* Normally, the DWARF producers are expected to use a signed
15372 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15373 But this is unfortunately not always the case, as witnessed
15374 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15375 is used instead. To work around that ambiguity, we treat
15376 the bounds as signed, and thus sign-extend their values, when
15377 the base type is signed. */
6e70227d 15378 negative_mask =
66c6502d 15379 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15380 if (low.kind == PROP_CONST
15381 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15382 low.data.const_val |= negative_mask;
15383 if (high.kind == PROP_CONST
15384 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15385 high.data.const_val |= negative_mask;
43bbcdc2 15386
729efb13 15387 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15388
c451ebe5
SA
15389 if (high_bound_is_count)
15390 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15391
c2ff108b
JK
15392 /* Ada expects an empty array on no boundary attributes. */
15393 if (attr == NULL && cu->language != language_ada)
729efb13 15394 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15395
39cbfefa
DJ
15396 name = dwarf2_name (die, cu);
15397 if (name)
15398 TYPE_NAME (range_type) = name;
6e70227d 15399
e142c38c 15400 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15401 if (attr)
15402 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15403
7e314c57
JK
15404 set_die_type (die, range_type, cu);
15405
15406 /* set_die_type should be already done. */
b4ba55a1
JB
15407 set_descriptive_type (range_type, die, cu);
15408
7e314c57 15409 return range_type;
a02abb62 15410}
6e70227d 15411
f792889a 15412static struct type *
81a17f79
JB
15413read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15414{
15415 struct type *type;
81a17f79 15416
81a17f79
JB
15417 /* For now, we only support the C meaning of an unspecified type: void. */
15418
19f392bc 15419 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15420 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15421
f792889a 15422 return set_die_type (die, type, cu);
81a17f79 15423}
a02abb62 15424
639d11d3
DC
15425/* Read a single die and all its descendents. Set the die's sibling
15426 field to NULL; set other fields in the die correctly, and set all
15427 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15428 location of the info_ptr after reading all of those dies. PARENT
15429 is the parent of the die in question. */
15430
15431static struct die_info *
dee91e82 15432read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15433 const gdb_byte *info_ptr,
15434 const gdb_byte **new_info_ptr,
dee91e82 15435 struct die_info *parent)
639d11d3
DC
15436{
15437 struct die_info *die;
d521ce57 15438 const gdb_byte *cur_ptr;
639d11d3
DC
15439 int has_children;
15440
bf6af496 15441 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15442 if (die == NULL)
15443 {
15444 *new_info_ptr = cur_ptr;
15445 return NULL;
15446 }
93311388 15447 store_in_ref_table (die, reader->cu);
639d11d3
DC
15448
15449 if (has_children)
bf6af496 15450 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15451 else
15452 {
15453 die->child = NULL;
15454 *new_info_ptr = cur_ptr;
15455 }
15456
15457 die->sibling = NULL;
15458 die->parent = parent;
15459 return die;
15460}
15461
15462/* Read a die, all of its descendents, and all of its siblings; set
15463 all of the fields of all of the dies correctly. Arguments are as
15464 in read_die_and_children. */
15465
15466static struct die_info *
bf6af496 15467read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15468 const gdb_byte *info_ptr,
15469 const gdb_byte **new_info_ptr,
bf6af496 15470 struct die_info *parent)
639d11d3
DC
15471{
15472 struct die_info *first_die, *last_sibling;
d521ce57 15473 const gdb_byte *cur_ptr;
639d11d3 15474
c906108c 15475 cur_ptr = info_ptr;
639d11d3
DC
15476 first_die = last_sibling = NULL;
15477
15478 while (1)
c906108c 15479 {
639d11d3 15480 struct die_info *die
dee91e82 15481 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15482
1d325ec1 15483 if (die == NULL)
c906108c 15484 {
639d11d3
DC
15485 *new_info_ptr = cur_ptr;
15486 return first_die;
c906108c 15487 }
1d325ec1
DJ
15488
15489 if (!first_die)
15490 first_die = die;
c906108c 15491 else
1d325ec1
DJ
15492 last_sibling->sibling = die;
15493
15494 last_sibling = die;
c906108c 15495 }
c906108c
SS
15496}
15497
bf6af496
DE
15498/* Read a die, all of its descendents, and all of its siblings; set
15499 all of the fields of all of the dies correctly. Arguments are as
15500 in read_die_and_children.
15501 This the main entry point for reading a DIE and all its children. */
15502
15503static struct die_info *
15504read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15505 const gdb_byte *info_ptr,
15506 const gdb_byte **new_info_ptr,
bf6af496
DE
15507 struct die_info *parent)
15508{
15509 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15510 new_info_ptr, parent);
15511
b4f54984 15512 if (dwarf_die_debug)
bf6af496
DE
15513 {
15514 fprintf_unfiltered (gdb_stdlog,
15515 "Read die from %s@0x%x of %s:\n",
a32a8923 15516 get_section_name (reader->die_section),
bf6af496
DE
15517 (unsigned) (info_ptr - reader->die_section->buffer),
15518 bfd_get_filename (reader->abfd));
b4f54984 15519 dump_die (die, dwarf_die_debug);
bf6af496
DE
15520 }
15521
15522 return die;
15523}
15524
3019eac3
DE
15525/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15526 attributes.
15527 The caller is responsible for filling in the extra attributes
15528 and updating (*DIEP)->num_attrs.
15529 Set DIEP to point to a newly allocated die with its information,
15530 except for its child, sibling, and parent fields.
15531 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15532
d521ce57 15533static const gdb_byte *
3019eac3 15534read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15535 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15536 int *has_children, int num_extra_attrs)
93311388 15537{
b64f50a1 15538 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15539 struct abbrev_info *abbrev;
15540 struct die_info *die;
15541 struct dwarf2_cu *cu = reader->cu;
15542 bfd *abfd = reader->abfd;
15543
9c541725 15544 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15545 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15546 info_ptr += bytes_read;
15547 if (!abbrev_number)
15548 {
15549 *diep = NULL;
15550 *has_children = 0;
15551 return info_ptr;
15552 }
15553
433df2d4 15554 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15555 if (!abbrev)
348e048f
DE
15556 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15557 abbrev_number,
15558 bfd_get_filename (abfd));
15559
3019eac3 15560 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15561 die->sect_off = sect_off;
93311388
DE
15562 die->tag = abbrev->tag;
15563 die->abbrev = abbrev_number;
15564
3019eac3
DE
15565 /* Make the result usable.
15566 The caller needs to update num_attrs after adding the extra
15567 attributes. */
93311388
DE
15568 die->num_attrs = abbrev->num_attrs;
15569
15570 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15571 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15572 info_ptr);
93311388
DE
15573
15574 *diep = die;
15575 *has_children = abbrev->has_children;
15576 return info_ptr;
15577}
15578
3019eac3
DE
15579/* Read a die and all its attributes.
15580 Set DIEP to point to a newly allocated die with its information,
15581 except for its child, sibling, and parent fields.
15582 Set HAS_CHILDREN to tell whether the die has children or not. */
15583
d521ce57 15584static const gdb_byte *
3019eac3 15585read_full_die (const struct die_reader_specs *reader,
d521ce57 15586 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15587 int *has_children)
15588{
d521ce57 15589 const gdb_byte *result;
bf6af496
DE
15590
15591 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15592
b4f54984 15593 if (dwarf_die_debug)
bf6af496
DE
15594 {
15595 fprintf_unfiltered (gdb_stdlog,
15596 "Read die from %s@0x%x of %s:\n",
a32a8923 15597 get_section_name (reader->die_section),
bf6af496
DE
15598 (unsigned) (info_ptr - reader->die_section->buffer),
15599 bfd_get_filename (reader->abfd));
b4f54984 15600 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15601 }
15602
15603 return result;
3019eac3 15604}
433df2d4
DE
15605\f
15606/* Abbreviation tables.
3019eac3 15607
433df2d4 15608 In DWARF version 2, the description of the debugging information is
c906108c
SS
15609 stored in a separate .debug_abbrev section. Before we read any
15610 dies from a section we read in all abbreviations and install them
433df2d4
DE
15611 in a hash table. */
15612
15613/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15614
15615static struct abbrev_info *
15616abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15617{
15618 struct abbrev_info *abbrev;
15619
8d749320 15620 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15621 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15622
433df2d4
DE
15623 return abbrev;
15624}
15625
15626/* Add an abbreviation to the table. */
c906108c
SS
15627
15628static void
433df2d4
DE
15629abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15630 unsigned int abbrev_number,
15631 struct abbrev_info *abbrev)
15632{
15633 unsigned int hash_number;
15634
15635 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15636 abbrev->next = abbrev_table->abbrevs[hash_number];
15637 abbrev_table->abbrevs[hash_number] = abbrev;
15638}
dee91e82 15639
433df2d4
DE
15640/* Look up an abbrev in the table.
15641 Returns NULL if the abbrev is not found. */
15642
15643static struct abbrev_info *
15644abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15645 unsigned int abbrev_number)
c906108c 15646{
433df2d4
DE
15647 unsigned int hash_number;
15648 struct abbrev_info *abbrev;
15649
15650 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15651 abbrev = abbrev_table->abbrevs[hash_number];
15652
15653 while (abbrev)
15654 {
15655 if (abbrev->number == abbrev_number)
15656 return abbrev;
15657 abbrev = abbrev->next;
15658 }
15659 return NULL;
15660}
15661
15662/* Read in an abbrev table. */
15663
15664static struct abbrev_table *
15665abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15666 sect_offset sect_off)
433df2d4
DE
15667{
15668 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15669 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15670 struct abbrev_table *abbrev_table;
d521ce57 15671 const gdb_byte *abbrev_ptr;
c906108c
SS
15672 struct abbrev_info *cur_abbrev;
15673 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15674 unsigned int abbrev_form;
f3dd6933
DJ
15675 struct attr_abbrev *cur_attrs;
15676 unsigned int allocated_attrs;
c906108c 15677
70ba0933 15678 abbrev_table = XNEW (struct abbrev_table);
9c541725 15679 abbrev_table->sect_off = sect_off;
433df2d4 15680 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15681 abbrev_table->abbrevs =
15682 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15683 ABBREV_HASH_SIZE);
433df2d4
DE
15684 memset (abbrev_table->abbrevs, 0,
15685 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15686
433df2d4 15687 dwarf2_read_section (objfile, section);
9c541725 15688 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15689 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15690 abbrev_ptr += bytes_read;
15691
f3dd6933 15692 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15693 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15694
0963b4bd 15695 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15696 while (abbrev_number)
15697 {
433df2d4 15698 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15699
15700 /* read in abbrev header */
15701 cur_abbrev->number = abbrev_number;
aead7601
SM
15702 cur_abbrev->tag
15703 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15704 abbrev_ptr += bytes_read;
15705 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15706 abbrev_ptr += 1;
15707
15708 /* now read in declarations */
22d2f3ab 15709 for (;;)
c906108c 15710 {
43988095
JK
15711 LONGEST implicit_const;
15712
22d2f3ab
JK
15713 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15714 abbrev_ptr += bytes_read;
15715 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15716 abbrev_ptr += bytes_read;
43988095
JK
15717 if (abbrev_form == DW_FORM_implicit_const)
15718 {
15719 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15720 &bytes_read);
15721 abbrev_ptr += bytes_read;
15722 }
15723 else
15724 {
15725 /* Initialize it due to a false compiler warning. */
15726 implicit_const = -1;
15727 }
22d2f3ab
JK
15728
15729 if (abbrev_name == 0)
15730 break;
15731
f3dd6933 15732 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15733 {
f3dd6933
DJ
15734 allocated_attrs += ATTR_ALLOC_CHUNK;
15735 cur_attrs
224c3ddb 15736 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15737 }
ae038cb0 15738
aead7601
SM
15739 cur_attrs[cur_abbrev->num_attrs].name
15740 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15741 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15742 = (enum dwarf_form) abbrev_form;
43988095 15743 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15744 ++cur_abbrev->num_attrs;
c906108c
SS
15745 }
15746
8d749320
SM
15747 cur_abbrev->attrs =
15748 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15749 cur_abbrev->num_attrs);
f3dd6933
DJ
15750 memcpy (cur_abbrev->attrs, cur_attrs,
15751 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15752
433df2d4 15753 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15754
15755 /* Get next abbreviation.
15756 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15757 always properly terminated with an abbrev number of 0.
15758 Exit loop if we encounter an abbreviation which we have
15759 already read (which means we are about to read the abbreviations
15760 for the next compile unit) or if the end of the abbreviation
15761 table is reached. */
433df2d4 15762 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15763 break;
15764 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15765 abbrev_ptr += bytes_read;
433df2d4 15766 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15767 break;
15768 }
f3dd6933
DJ
15769
15770 xfree (cur_attrs);
433df2d4 15771 return abbrev_table;
c906108c
SS
15772}
15773
433df2d4 15774/* Free the resources held by ABBREV_TABLE. */
c906108c 15775
c906108c 15776static void
433df2d4 15777abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15778{
433df2d4
DE
15779 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15780 xfree (abbrev_table);
c906108c
SS
15781}
15782
f4dc4d17
DE
15783/* Same as abbrev_table_free but as a cleanup.
15784 We pass in a pointer to the pointer to the table so that we can
15785 set the pointer to NULL when we're done. It also simplifies
73051182 15786 build_type_psymtabs_1. */
f4dc4d17
DE
15787
15788static void
15789abbrev_table_free_cleanup (void *table_ptr)
15790{
9a3c8263 15791 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15792
15793 if (*abbrev_table_ptr != NULL)
15794 abbrev_table_free (*abbrev_table_ptr);
15795 *abbrev_table_ptr = NULL;
15796}
15797
433df2d4
DE
15798/* Read the abbrev table for CU from ABBREV_SECTION. */
15799
15800static void
15801dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15802 struct dwarf2_section_info *abbrev_section)
c906108c 15803{
433df2d4 15804 cu->abbrev_table =
9c541725 15805 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15806}
c906108c 15807
433df2d4 15808/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15809
433df2d4
DE
15810static void
15811dwarf2_free_abbrev_table (void *ptr_to_cu)
15812{
9a3c8263 15813 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15814
a2ce51a0
DE
15815 if (cu->abbrev_table != NULL)
15816 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15817 /* Set this to NULL so that we SEGV if we try to read it later,
15818 and also because free_comp_unit verifies this is NULL. */
15819 cu->abbrev_table = NULL;
15820}
15821\f
72bf9492
DJ
15822/* Returns nonzero if TAG represents a type that we might generate a partial
15823 symbol for. */
15824
15825static int
15826is_type_tag_for_partial (int tag)
15827{
15828 switch (tag)
15829 {
15830#if 0
15831 /* Some types that would be reasonable to generate partial symbols for,
15832 that we don't at present. */
15833 case DW_TAG_array_type:
15834 case DW_TAG_file_type:
15835 case DW_TAG_ptr_to_member_type:
15836 case DW_TAG_set_type:
15837 case DW_TAG_string_type:
15838 case DW_TAG_subroutine_type:
15839#endif
15840 case DW_TAG_base_type:
15841 case DW_TAG_class_type:
680b30c7 15842 case DW_TAG_interface_type:
72bf9492
DJ
15843 case DW_TAG_enumeration_type:
15844 case DW_TAG_structure_type:
15845 case DW_TAG_subrange_type:
15846 case DW_TAG_typedef:
15847 case DW_TAG_union_type:
15848 return 1;
15849 default:
15850 return 0;
15851 }
15852}
15853
15854/* Load all DIEs that are interesting for partial symbols into memory. */
15855
15856static struct partial_die_info *
dee91e82 15857load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15858 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15859{
dee91e82 15860 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15861 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15862 struct partial_die_info *part_die;
15863 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15864 struct abbrev_info *abbrev;
15865 unsigned int bytes_read;
5afb4e99 15866 unsigned int load_all = 0;
72bf9492
DJ
15867 int nesting_level = 1;
15868
15869 parent_die = NULL;
15870 last_die = NULL;
15871
7adf1e79
DE
15872 gdb_assert (cu->per_cu != NULL);
15873 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15874 load_all = 1;
15875
72bf9492
DJ
15876 cu->partial_dies
15877 = htab_create_alloc_ex (cu->header.length / 12,
15878 partial_die_hash,
15879 partial_die_eq,
15880 NULL,
15881 &cu->comp_unit_obstack,
15882 hashtab_obstack_allocate,
15883 dummy_obstack_deallocate);
15884
8d749320 15885 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15886
15887 while (1)
15888 {
15889 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15890
15891 /* A NULL abbrev means the end of a series of children. */
15892 if (abbrev == NULL)
15893 {
15894 if (--nesting_level == 0)
15895 {
15896 /* PART_DIE was probably the last thing allocated on the
15897 comp_unit_obstack, so we could call obstack_free
15898 here. We don't do that because the waste is small,
15899 and will be cleaned up when we're done with this
15900 compilation unit. This way, we're also more robust
15901 against other users of the comp_unit_obstack. */
15902 return first_die;
15903 }
15904 info_ptr += bytes_read;
15905 last_die = parent_die;
15906 parent_die = parent_die->die_parent;
15907 continue;
15908 }
15909
98bfdba5
PA
15910 /* Check for template arguments. We never save these; if
15911 they're seen, we just mark the parent, and go on our way. */
15912 if (parent_die != NULL
15913 && cu->language == language_cplus
15914 && (abbrev->tag == DW_TAG_template_type_param
15915 || abbrev->tag == DW_TAG_template_value_param))
15916 {
15917 parent_die->has_template_arguments = 1;
15918
15919 if (!load_all)
15920 {
15921 /* We don't need a partial DIE for the template argument. */
dee91e82 15922 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15923 continue;
15924 }
15925 }
15926
0d99eb77 15927 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15928 Skip their other children. */
15929 if (!load_all
15930 && cu->language == language_cplus
15931 && parent_die != NULL
15932 && parent_die->tag == DW_TAG_subprogram)
15933 {
dee91e82 15934 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15935 continue;
15936 }
15937
5afb4e99
DJ
15938 /* Check whether this DIE is interesting enough to save. Normally
15939 we would not be interested in members here, but there may be
15940 later variables referencing them via DW_AT_specification (for
15941 static members). */
15942 if (!load_all
15943 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15944 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15945 && abbrev->tag != DW_TAG_enumerator
15946 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15947 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15948 && abbrev->tag != DW_TAG_variable
5afb4e99 15949 && abbrev->tag != DW_TAG_namespace
f55ee35c 15950 && abbrev->tag != DW_TAG_module
95554aad 15951 && abbrev->tag != DW_TAG_member
74921315
KS
15952 && abbrev->tag != DW_TAG_imported_unit
15953 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15954 {
15955 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15956 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15957 continue;
15958 }
15959
dee91e82
DE
15960 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15961 info_ptr);
72bf9492
DJ
15962
15963 /* This two-pass algorithm for processing partial symbols has a
15964 high cost in cache pressure. Thus, handle some simple cases
15965 here which cover the majority of C partial symbols. DIEs
15966 which neither have specification tags in them, nor could have
15967 specification tags elsewhere pointing at them, can simply be
15968 processed and discarded.
15969
15970 This segment is also optional; scan_partial_symbols and
15971 add_partial_symbol will handle these DIEs if we chain
15972 them in normally. When compilers which do not emit large
15973 quantities of duplicate debug information are more common,
15974 this code can probably be removed. */
15975
15976 /* Any complete simple types at the top level (pretty much all
15977 of them, for a language without namespaces), can be processed
15978 directly. */
15979 if (parent_die == NULL
15980 && part_die->has_specification == 0
15981 && part_die->is_declaration == 0
d8228535 15982 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15983 || part_die->tag == DW_TAG_base_type
15984 || part_die->tag == DW_TAG_subrange_type))
15985 {
15986 if (building_psymtab && part_die->name != NULL)
04a679b8 15987 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15988 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15989 &objfile->static_psymbols,
1762568f 15990 0, cu->language, objfile);
dee91e82 15991 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15992 continue;
15993 }
15994
d8228535
JK
15995 /* The exception for DW_TAG_typedef with has_children above is
15996 a workaround of GCC PR debug/47510. In the case of this complaint
15997 type_name_no_tag_or_error will error on such types later.
15998
15999 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16000 it could not find the child DIEs referenced later, this is checked
16001 above. In correct DWARF DW_TAG_typedef should have no children. */
16002
16003 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16004 complaint (&symfile_complaints,
16005 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16006 "- DIE at 0x%x [in module %s]"),
9c541725 16007 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16008
72bf9492
DJ
16009 /* If we're at the second level, and we're an enumerator, and
16010 our parent has no specification (meaning possibly lives in a
16011 namespace elsewhere), then we can add the partial symbol now
16012 instead of queueing it. */
16013 if (part_die->tag == DW_TAG_enumerator
16014 && parent_die != NULL
16015 && parent_die->die_parent == NULL
16016 && parent_die->tag == DW_TAG_enumeration_type
16017 && parent_die->has_specification == 0)
16018 {
16019 if (part_die->name == NULL)
3e43a32a
MS
16020 complaint (&symfile_complaints,
16021 _("malformed enumerator DIE ignored"));
72bf9492 16022 else if (building_psymtab)
04a679b8 16023 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16024 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16025 cu->language == language_cplus
bb5ed363
DE
16026 ? &objfile->global_psymbols
16027 : &objfile->static_psymbols,
1762568f 16028 0, cu->language, objfile);
72bf9492 16029
dee91e82 16030 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16031 continue;
16032 }
16033
16034 /* We'll save this DIE so link it in. */
16035 part_die->die_parent = parent_die;
16036 part_die->die_sibling = NULL;
16037 part_die->die_child = NULL;
16038
16039 if (last_die && last_die == parent_die)
16040 last_die->die_child = part_die;
16041 else if (last_die)
16042 last_die->die_sibling = part_die;
16043
16044 last_die = part_die;
16045
16046 if (first_die == NULL)
16047 first_die = part_die;
16048
16049 /* Maybe add the DIE to the hash table. Not all DIEs that we
16050 find interesting need to be in the hash table, because we
16051 also have the parent/sibling/child chains; only those that we
16052 might refer to by offset later during partial symbol reading.
16053
16054 For now this means things that might have be the target of a
16055 DW_AT_specification, DW_AT_abstract_origin, or
16056 DW_AT_extension. DW_AT_extension will refer only to
16057 namespaces; DW_AT_abstract_origin refers to functions (and
16058 many things under the function DIE, but we do not recurse
16059 into function DIEs during partial symbol reading) and
16060 possibly variables as well; DW_AT_specification refers to
16061 declarations. Declarations ought to have the DW_AT_declaration
16062 flag. It happens that GCC forgets to put it in sometimes, but
16063 only for functions, not for types.
16064
16065 Adding more things than necessary to the hash table is harmless
16066 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16067 wasted time in find_partial_die, when we reread the compilation
16068 unit with load_all_dies set. */
72bf9492 16069
5afb4e99 16070 if (load_all
72929c62 16071 || abbrev->tag == DW_TAG_constant
5afb4e99 16072 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16073 || abbrev->tag == DW_TAG_variable
16074 || abbrev->tag == DW_TAG_namespace
16075 || part_die->is_declaration)
16076 {
16077 void **slot;
16078
16079 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16080 to_underlying (part_die->sect_off),
16081 INSERT);
72bf9492
DJ
16082 *slot = part_die;
16083 }
16084
8d749320 16085 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16086
16087 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16088 we have no reason to follow the children of structures; for other
98bfdba5
PA
16089 languages we have to, so that we can get at method physnames
16090 to infer fully qualified class names, for DW_AT_specification,
16091 and for C++ template arguments. For C++, we also look one level
16092 inside functions to find template arguments (if the name of the
16093 function does not already contain the template arguments).
bc30ff58
JB
16094
16095 For Ada, we need to scan the children of subprograms and lexical
16096 blocks as well because Ada allows the definition of nested
16097 entities that could be interesting for the debugger, such as
16098 nested subprograms for instance. */
72bf9492 16099 if (last_die->has_children
5afb4e99
DJ
16100 && (load_all
16101 || last_die->tag == DW_TAG_namespace
f55ee35c 16102 || last_die->tag == DW_TAG_module
72bf9492 16103 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16104 || (cu->language == language_cplus
16105 && last_die->tag == DW_TAG_subprogram
16106 && (last_die->name == NULL
16107 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16108 || (cu->language != language_c
16109 && (last_die->tag == DW_TAG_class_type
680b30c7 16110 || last_die->tag == DW_TAG_interface_type
72bf9492 16111 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16112 || last_die->tag == DW_TAG_union_type))
16113 || (cu->language == language_ada
16114 && (last_die->tag == DW_TAG_subprogram
16115 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16116 {
16117 nesting_level++;
16118 parent_die = last_die;
16119 continue;
16120 }
16121
16122 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16123 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16124
16125 /* Back to the top, do it again. */
16126 }
16127}
16128
c906108c
SS
16129/* Read a minimal amount of information into the minimal die structure. */
16130
d521ce57 16131static const gdb_byte *
dee91e82
DE
16132read_partial_die (const struct die_reader_specs *reader,
16133 struct partial_die_info *part_die,
16134 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16135 const gdb_byte *info_ptr)
c906108c 16136{
dee91e82 16137 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16138 struct objfile *objfile = cu->objfile;
d521ce57 16139 const gdb_byte *buffer = reader->buffer;
fa238c03 16140 unsigned int i;
c906108c 16141 struct attribute attr;
c5aa993b 16142 int has_low_pc_attr = 0;
c906108c 16143 int has_high_pc_attr = 0;
91da1414 16144 int high_pc_relative = 0;
c906108c 16145
72bf9492 16146 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16147
9c541725 16148 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16149
16150 info_ptr += abbrev_len;
16151
16152 if (abbrev == NULL)
16153 return info_ptr;
16154
c906108c
SS
16155 part_die->tag = abbrev->tag;
16156 part_die->has_children = abbrev->has_children;
c906108c
SS
16157
16158 for (i = 0; i < abbrev->num_attrs; ++i)
16159 {
dee91e82 16160 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16161
16162 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16163 partial symbol table. */
c906108c
SS
16164 switch (attr.name)
16165 {
16166 case DW_AT_name:
71c25dea
TT
16167 switch (part_die->tag)
16168 {
16169 case DW_TAG_compile_unit:
95554aad 16170 case DW_TAG_partial_unit:
348e048f 16171 case DW_TAG_type_unit:
71c25dea
TT
16172 /* Compilation units have a DW_AT_name that is a filename, not
16173 a source language identifier. */
16174 case DW_TAG_enumeration_type:
16175 case DW_TAG_enumerator:
16176 /* These tags always have simple identifiers already; no need
16177 to canonicalize them. */
16178 part_die->name = DW_STRING (&attr);
16179 break;
16180 default:
16181 part_die->name
16182 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16183 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16184 break;
16185 }
c906108c 16186 break;
31ef98ae 16187 case DW_AT_linkage_name:
c906108c 16188 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16189 /* Note that both forms of linkage name might appear. We
16190 assume they will be the same, and we only store the last
16191 one we see. */
94af9270
KS
16192 if (cu->language == language_ada)
16193 part_die->name = DW_STRING (&attr);
abc72ce4 16194 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16195 break;
16196 case DW_AT_low_pc:
16197 has_low_pc_attr = 1;
31aa7e4e 16198 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16199 break;
16200 case DW_AT_high_pc:
16201 has_high_pc_attr = 1;
31aa7e4e
JB
16202 part_die->highpc = attr_value_as_address (&attr);
16203 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16204 high_pc_relative = 1;
c906108c
SS
16205 break;
16206 case DW_AT_location:
0963b4bd 16207 /* Support the .debug_loc offsets. */
8e19ed76
PS
16208 if (attr_form_is_block (&attr))
16209 {
95554aad 16210 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16211 }
3690dd37 16212 else if (attr_form_is_section_offset (&attr))
8e19ed76 16213 {
4d3c2250 16214 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16215 }
16216 else
16217 {
4d3c2250
KB
16218 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16219 "partial symbol information");
8e19ed76 16220 }
c906108c 16221 break;
c906108c
SS
16222 case DW_AT_external:
16223 part_die->is_external = DW_UNSND (&attr);
16224 break;
16225 case DW_AT_declaration:
16226 part_die->is_declaration = DW_UNSND (&attr);
16227 break;
16228 case DW_AT_type:
16229 part_die->has_type = 1;
16230 break;
16231 case DW_AT_abstract_origin:
16232 case DW_AT_specification:
72bf9492
DJ
16233 case DW_AT_extension:
16234 part_die->has_specification = 1;
c764a876 16235 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16236 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16237 || cu->per_cu->is_dwz);
c906108c
SS
16238 break;
16239 case DW_AT_sibling:
16240 /* Ignore absolute siblings, they might point outside of
16241 the current compile unit. */
16242 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16243 complaint (&symfile_complaints,
16244 _("ignoring absolute DW_AT_sibling"));
c906108c 16245 else
b9502d3f 16246 {
9c541725
PA
16247 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16248 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16249
16250 if (sibling_ptr < info_ptr)
16251 complaint (&symfile_complaints,
16252 _("DW_AT_sibling points backwards"));
22869d73
KS
16253 else if (sibling_ptr > reader->buffer_end)
16254 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16255 else
16256 part_die->sibling = sibling_ptr;
16257 }
c906108c 16258 break;
fa4028e9
JB
16259 case DW_AT_byte_size:
16260 part_die->has_byte_size = 1;
16261 break;
ff908ebf
AW
16262 case DW_AT_const_value:
16263 part_die->has_const_value = 1;
16264 break;
68511cec
CES
16265 case DW_AT_calling_convention:
16266 /* DWARF doesn't provide a way to identify a program's source-level
16267 entry point. DW_AT_calling_convention attributes are only meant
16268 to describe functions' calling conventions.
16269
16270 However, because it's a necessary piece of information in
0c1b455e
TT
16271 Fortran, and before DWARF 4 DW_CC_program was the only
16272 piece of debugging information whose definition refers to
16273 a 'main program' at all, several compilers marked Fortran
16274 main programs with DW_CC_program --- even when those
16275 functions use the standard calling conventions.
16276
16277 Although DWARF now specifies a way to provide this
16278 information, we support this practice for backward
16279 compatibility. */
68511cec 16280 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16281 && cu->language == language_fortran)
16282 part_die->main_subprogram = 1;
68511cec 16283 break;
481860b3
GB
16284 case DW_AT_inline:
16285 if (DW_UNSND (&attr) == DW_INL_inlined
16286 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16287 part_die->may_be_inlined = 1;
16288 break;
95554aad
TT
16289
16290 case DW_AT_import:
16291 if (part_die->tag == DW_TAG_imported_unit)
36586728 16292 {
9c541725 16293 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16294 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16295 || cu->per_cu->is_dwz);
16296 }
95554aad
TT
16297 break;
16298
0c1b455e
TT
16299 case DW_AT_main_subprogram:
16300 part_die->main_subprogram = DW_UNSND (&attr);
16301 break;
16302
c906108c
SS
16303 default:
16304 break;
16305 }
16306 }
16307
91da1414
MW
16308 if (high_pc_relative)
16309 part_die->highpc += part_die->lowpc;
16310
9373cf26
JK
16311 if (has_low_pc_attr && has_high_pc_attr)
16312 {
16313 /* When using the GNU linker, .gnu.linkonce. sections are used to
16314 eliminate duplicate copies of functions and vtables and such.
16315 The linker will arbitrarily choose one and discard the others.
16316 The AT_*_pc values for such functions refer to local labels in
16317 these sections. If the section from that file was discarded, the
16318 labels are not in the output, so the relocs get a value of 0.
16319 If this is a discarded function, mark the pc bounds as invalid,
16320 so that GDB will ignore it. */
16321 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16322 {
bb5ed363 16323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16324
16325 complaint (&symfile_complaints,
16326 _("DW_AT_low_pc %s is zero "
16327 "for DIE at 0x%x [in module %s]"),
16328 paddress (gdbarch, part_die->lowpc),
9c541725 16329 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16330 }
16331 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16332 else if (part_die->lowpc >= part_die->highpc)
16333 {
bb5ed363 16334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16335
16336 complaint (&symfile_complaints,
16337 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16338 "for DIE at 0x%x [in module %s]"),
16339 paddress (gdbarch, part_die->lowpc),
16340 paddress (gdbarch, part_die->highpc),
9c541725
PA
16341 to_underlying (part_die->sect_off),
16342 objfile_name (objfile));
9373cf26
JK
16343 }
16344 else
16345 part_die->has_pc_info = 1;
16346 }
85cbf3d3 16347
c906108c
SS
16348 return info_ptr;
16349}
16350
72bf9492
DJ
16351/* Find a cached partial DIE at OFFSET in CU. */
16352
16353static struct partial_die_info *
9c541725 16354find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16355{
16356 struct partial_die_info *lookup_die = NULL;
16357 struct partial_die_info part_die;
16358
9c541725 16359 part_die.sect_off = sect_off;
9a3c8263
SM
16360 lookup_die = ((struct partial_die_info *)
16361 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16362 to_underlying (sect_off)));
72bf9492 16363
72bf9492
DJ
16364 return lookup_die;
16365}
16366
348e048f
DE
16367/* Find a partial DIE at OFFSET, which may or may not be in CU,
16368 except in the case of .debug_types DIEs which do not reference
16369 outside their CU (they do however referencing other types via
55f1336d 16370 DW_FORM_ref_sig8). */
72bf9492
DJ
16371
16372static struct partial_die_info *
9c541725 16373find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16374{
bb5ed363 16375 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16376 struct dwarf2_per_cu_data *per_cu = NULL;
16377 struct partial_die_info *pd = NULL;
72bf9492 16378
36586728 16379 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16380 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16381 {
9c541725 16382 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16383 if (pd != NULL)
16384 return pd;
0d99eb77
DE
16385 /* We missed recording what we needed.
16386 Load all dies and try again. */
16387 per_cu = cu->per_cu;
5afb4e99 16388 }
0d99eb77
DE
16389 else
16390 {
16391 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16392 if (cu->per_cu->is_debug_types)
0d99eb77 16393 {
9c541725
PA
16394 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16395 " external reference to offset 0x%x [in module %s].\n"),
16396 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16397 bfd_get_filename (objfile->obfd));
16398 }
9c541725 16399 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16400 objfile);
72bf9492 16401
0d99eb77
DE
16402 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16403 load_partial_comp_unit (per_cu);
ae038cb0 16404
0d99eb77 16405 per_cu->cu->last_used = 0;
9c541725 16406 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16407 }
5afb4e99 16408
dee91e82
DE
16409 /* If we didn't find it, and not all dies have been loaded,
16410 load them all and try again. */
16411
5afb4e99
DJ
16412 if (pd == NULL && per_cu->load_all_dies == 0)
16413 {
5afb4e99 16414 per_cu->load_all_dies = 1;
fd820528
DE
16415
16416 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16417 THIS_CU->cu may already be in use. So we can't just free it and
16418 replace its DIEs with the ones we read in. Instead, we leave those
16419 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16420 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16421 set. */
dee91e82 16422 load_partial_comp_unit (per_cu);
5afb4e99 16423
9c541725 16424 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16425 }
16426
16427 if (pd == NULL)
16428 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16429 _("could not find partial DIE 0x%x "
16430 "in cache [from module %s]\n"),
9c541725 16431 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16432 return pd;
72bf9492
DJ
16433}
16434
abc72ce4
DE
16435/* See if we can figure out if the class lives in a namespace. We do
16436 this by looking for a member function; its demangled name will
16437 contain namespace info, if there is any. */
16438
16439static void
16440guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16441 struct dwarf2_cu *cu)
16442{
16443 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16444 what template types look like, because the demangler
16445 frequently doesn't give the same name as the debug info. We
16446 could fix this by only using the demangled name to get the
16447 prefix (but see comment in read_structure_type). */
16448
16449 struct partial_die_info *real_pdi;
16450 struct partial_die_info *child_pdi;
16451
16452 /* If this DIE (this DIE's specification, if any) has a parent, then
16453 we should not do this. We'll prepend the parent's fully qualified
16454 name when we create the partial symbol. */
16455
16456 real_pdi = struct_pdi;
16457 while (real_pdi->has_specification)
36586728
TT
16458 real_pdi = find_partial_die (real_pdi->spec_offset,
16459 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16460
16461 if (real_pdi->die_parent != NULL)
16462 return;
16463
16464 for (child_pdi = struct_pdi->die_child;
16465 child_pdi != NULL;
16466 child_pdi = child_pdi->die_sibling)
16467 {
16468 if (child_pdi->tag == DW_TAG_subprogram
16469 && child_pdi->linkage_name != NULL)
16470 {
16471 char *actual_class_name
16472 = language_class_name_from_physname (cu->language_defn,
16473 child_pdi->linkage_name);
16474 if (actual_class_name != NULL)
16475 {
16476 struct_pdi->name
224c3ddb
SM
16477 = ((const char *)
16478 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16479 actual_class_name,
16480 strlen (actual_class_name)));
abc72ce4
DE
16481 xfree (actual_class_name);
16482 }
16483 break;
16484 }
16485 }
16486}
16487
72bf9492
DJ
16488/* Adjust PART_DIE before generating a symbol for it. This function
16489 may set the is_external flag or change the DIE's name. */
16490
16491static void
16492fixup_partial_die (struct partial_die_info *part_die,
16493 struct dwarf2_cu *cu)
16494{
abc72ce4
DE
16495 /* Once we've fixed up a die, there's no point in doing so again.
16496 This also avoids a memory leak if we were to call
16497 guess_partial_die_structure_name multiple times. */
16498 if (part_die->fixup_called)
16499 return;
16500
72bf9492
DJ
16501 /* If we found a reference attribute and the DIE has no name, try
16502 to find a name in the referred to DIE. */
16503
16504 if (part_die->name == NULL && part_die->has_specification)
16505 {
16506 struct partial_die_info *spec_die;
72bf9492 16507
36586728
TT
16508 spec_die = find_partial_die (part_die->spec_offset,
16509 part_die->spec_is_dwz, cu);
72bf9492 16510
10b3939b 16511 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16512
16513 if (spec_die->name)
16514 {
16515 part_die->name = spec_die->name;
16516
16517 /* Copy DW_AT_external attribute if it is set. */
16518 if (spec_die->is_external)
16519 part_die->is_external = spec_die->is_external;
16520 }
16521 }
16522
16523 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16524
16525 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16526 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16527
abc72ce4
DE
16528 /* If there is no parent die to provide a namespace, and there are
16529 children, see if we can determine the namespace from their linkage
122d1940 16530 name. */
abc72ce4 16531 if (cu->language == language_cplus
8b70b953 16532 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16533 && part_die->die_parent == NULL
16534 && part_die->has_children
16535 && (part_die->tag == DW_TAG_class_type
16536 || part_die->tag == DW_TAG_structure_type
16537 || part_die->tag == DW_TAG_union_type))
16538 guess_partial_die_structure_name (part_die, cu);
16539
53832f31
TT
16540 /* GCC might emit a nameless struct or union that has a linkage
16541 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16542 if (part_die->name == NULL
96408a79
SA
16543 && (part_die->tag == DW_TAG_class_type
16544 || part_die->tag == DW_TAG_interface_type
16545 || part_die->tag == DW_TAG_structure_type
16546 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16547 && part_die->linkage_name != NULL)
16548 {
16549 char *demangled;
16550
8de20a37 16551 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16552 if (demangled)
16553 {
96408a79
SA
16554 const char *base;
16555
16556 /* Strip any leading namespaces/classes, keep only the base name.
16557 DW_AT_name for named DIEs does not contain the prefixes. */
16558 base = strrchr (demangled, ':');
16559 if (base && base > demangled && base[-1] == ':')
16560 base++;
16561 else
16562 base = demangled;
16563
34a68019 16564 part_die->name
224c3ddb
SM
16565 = ((const char *)
16566 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16567 base, strlen (base)));
53832f31
TT
16568 xfree (demangled);
16569 }
16570 }
16571
abc72ce4 16572 part_die->fixup_called = 1;
72bf9492
DJ
16573}
16574
a8329558 16575/* Read an attribute value described by an attribute form. */
c906108c 16576
d521ce57 16577static const gdb_byte *
dee91e82
DE
16578read_attribute_value (const struct die_reader_specs *reader,
16579 struct attribute *attr, unsigned form,
43988095 16580 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16581{
dee91e82 16582 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16583 struct objfile *objfile = cu->objfile;
16584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16585 bfd *abfd = reader->abfd;
e7c27a73 16586 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16587 unsigned int bytes_read;
16588 struct dwarf_block *blk;
16589
aead7601 16590 attr->form = (enum dwarf_form) form;
a8329558 16591 switch (form)
c906108c 16592 {
c906108c 16593 case DW_FORM_ref_addr:
ae411497 16594 if (cu->header.version == 2)
4568ecf9 16595 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16596 else
4568ecf9
DE
16597 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16598 &cu->header, &bytes_read);
ae411497
TT
16599 info_ptr += bytes_read;
16600 break;
36586728
TT
16601 case DW_FORM_GNU_ref_alt:
16602 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16603 info_ptr += bytes_read;
16604 break;
ae411497 16605 case DW_FORM_addr:
e7c27a73 16606 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16607 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16608 info_ptr += bytes_read;
c906108c
SS
16609 break;
16610 case DW_FORM_block2:
7b5a2f43 16611 blk = dwarf_alloc_block (cu);
c906108c
SS
16612 blk->size = read_2_bytes (abfd, info_ptr);
16613 info_ptr += 2;
16614 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16615 info_ptr += blk->size;
16616 DW_BLOCK (attr) = blk;
16617 break;
16618 case DW_FORM_block4:
7b5a2f43 16619 blk = dwarf_alloc_block (cu);
c906108c
SS
16620 blk->size = read_4_bytes (abfd, info_ptr);
16621 info_ptr += 4;
16622 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16623 info_ptr += blk->size;
16624 DW_BLOCK (attr) = blk;
16625 break;
16626 case DW_FORM_data2:
16627 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16628 info_ptr += 2;
16629 break;
16630 case DW_FORM_data4:
16631 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16632 info_ptr += 4;
16633 break;
16634 case DW_FORM_data8:
16635 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16636 info_ptr += 8;
16637 break;
0224619f
JK
16638 case DW_FORM_data16:
16639 blk = dwarf_alloc_block (cu);
16640 blk->size = 16;
16641 blk->data = read_n_bytes (abfd, info_ptr, 16);
16642 info_ptr += 16;
16643 DW_BLOCK (attr) = blk;
16644 break;
2dc7f7b3
TT
16645 case DW_FORM_sec_offset:
16646 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16647 info_ptr += bytes_read;
16648 break;
c906108c 16649 case DW_FORM_string:
9b1c24c8 16650 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16651 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16652 info_ptr += bytes_read;
16653 break;
4bdf3d34 16654 case DW_FORM_strp:
36586728
TT
16655 if (!cu->per_cu->is_dwz)
16656 {
16657 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16658 &bytes_read);
16659 DW_STRING_IS_CANONICAL (attr) = 0;
16660 info_ptr += bytes_read;
16661 break;
16662 }
16663 /* FALLTHROUGH */
43988095
JK
16664 case DW_FORM_line_strp:
16665 if (!cu->per_cu->is_dwz)
16666 {
16667 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16668 cu_header, &bytes_read);
16669 DW_STRING_IS_CANONICAL (attr) = 0;
16670 info_ptr += bytes_read;
16671 break;
16672 }
16673 /* FALLTHROUGH */
36586728
TT
16674 case DW_FORM_GNU_strp_alt:
16675 {
16676 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16677 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16678 &bytes_read);
16679
16680 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16681 DW_STRING_IS_CANONICAL (attr) = 0;
16682 info_ptr += bytes_read;
16683 }
4bdf3d34 16684 break;
2dc7f7b3 16685 case DW_FORM_exprloc:
c906108c 16686 case DW_FORM_block:
7b5a2f43 16687 blk = dwarf_alloc_block (cu);
c906108c
SS
16688 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16689 info_ptr += bytes_read;
16690 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16691 info_ptr += blk->size;
16692 DW_BLOCK (attr) = blk;
16693 break;
16694 case DW_FORM_block1:
7b5a2f43 16695 blk = dwarf_alloc_block (cu);
c906108c
SS
16696 blk->size = read_1_byte (abfd, info_ptr);
16697 info_ptr += 1;
16698 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16699 info_ptr += blk->size;
16700 DW_BLOCK (attr) = blk;
16701 break;
16702 case DW_FORM_data1:
16703 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16704 info_ptr += 1;
16705 break;
16706 case DW_FORM_flag:
16707 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16708 info_ptr += 1;
16709 break;
2dc7f7b3
TT
16710 case DW_FORM_flag_present:
16711 DW_UNSND (attr) = 1;
16712 break;
c906108c
SS
16713 case DW_FORM_sdata:
16714 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16715 info_ptr += bytes_read;
16716 break;
16717 case DW_FORM_udata:
16718 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16719 info_ptr += bytes_read;
16720 break;
16721 case DW_FORM_ref1:
9c541725 16722 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16723 + read_1_byte (abfd, info_ptr));
c906108c
SS
16724 info_ptr += 1;
16725 break;
16726 case DW_FORM_ref2:
9c541725 16727 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16728 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16729 info_ptr += 2;
16730 break;
16731 case DW_FORM_ref4:
9c541725 16732 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16733 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16734 info_ptr += 4;
16735 break;
613e1657 16736 case DW_FORM_ref8:
9c541725 16737 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16738 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16739 info_ptr += 8;
16740 break;
55f1336d 16741 case DW_FORM_ref_sig8:
ac9ec31b 16742 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16743 info_ptr += 8;
16744 break;
c906108c 16745 case DW_FORM_ref_udata:
9c541725 16746 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16747 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16748 info_ptr += bytes_read;
16749 break;
c906108c 16750 case DW_FORM_indirect:
a8329558
KW
16751 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16752 info_ptr += bytes_read;
43988095
JK
16753 if (form == DW_FORM_implicit_const)
16754 {
16755 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16756 info_ptr += bytes_read;
16757 }
16758 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16759 info_ptr);
16760 break;
16761 case DW_FORM_implicit_const:
16762 DW_SND (attr) = implicit_const;
a8329558 16763 break;
3019eac3
DE
16764 case DW_FORM_GNU_addr_index:
16765 if (reader->dwo_file == NULL)
16766 {
16767 /* For now flag a hard error.
16768 Later we can turn this into a complaint. */
16769 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16770 dwarf_form_name (form),
16771 bfd_get_filename (abfd));
16772 }
16773 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16774 info_ptr += bytes_read;
16775 break;
16776 case DW_FORM_GNU_str_index:
16777 if (reader->dwo_file == NULL)
16778 {
16779 /* For now flag a hard error.
16780 Later we can turn this into a complaint if warranted. */
16781 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16782 dwarf_form_name (form),
16783 bfd_get_filename (abfd));
16784 }
16785 {
16786 ULONGEST str_index =
16787 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16788
342587c4 16789 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16790 DW_STRING_IS_CANONICAL (attr) = 0;
16791 info_ptr += bytes_read;
16792 }
16793 break;
c906108c 16794 default:
8a3fe4f8 16795 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16796 dwarf_form_name (form),
16797 bfd_get_filename (abfd));
c906108c 16798 }
28e94949 16799
36586728 16800 /* Super hack. */
7771576e 16801 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16802 attr->form = DW_FORM_GNU_ref_alt;
16803
28e94949
JB
16804 /* We have seen instances where the compiler tried to emit a byte
16805 size attribute of -1 which ended up being encoded as an unsigned
16806 0xffffffff. Although 0xffffffff is technically a valid size value,
16807 an object of this size seems pretty unlikely so we can relatively
16808 safely treat these cases as if the size attribute was invalid and
16809 treat them as zero by default. */
16810 if (attr->name == DW_AT_byte_size
16811 && form == DW_FORM_data4
16812 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16813 {
16814 complaint
16815 (&symfile_complaints,
43bbcdc2
PH
16816 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16817 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16818 DW_UNSND (attr) = 0;
16819 }
28e94949 16820
c906108c
SS
16821 return info_ptr;
16822}
16823
a8329558
KW
16824/* Read an attribute described by an abbreviated attribute. */
16825
d521ce57 16826static const gdb_byte *
dee91e82
DE
16827read_attribute (const struct die_reader_specs *reader,
16828 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16829 const gdb_byte *info_ptr)
a8329558
KW
16830{
16831 attr->name = abbrev->name;
43988095
JK
16832 return read_attribute_value (reader, attr, abbrev->form,
16833 abbrev->implicit_const, info_ptr);
a8329558
KW
16834}
16835
0963b4bd 16836/* Read dwarf information from a buffer. */
c906108c
SS
16837
16838static unsigned int
a1855c1d 16839read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16840{
fe1b8b76 16841 return bfd_get_8 (abfd, buf);
c906108c
SS
16842}
16843
16844static int
a1855c1d 16845read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16846{
fe1b8b76 16847 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16848}
16849
16850static unsigned int
a1855c1d 16851read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16852{
fe1b8b76 16853 return bfd_get_16 (abfd, buf);
c906108c
SS
16854}
16855
21ae7a4d 16856static int
a1855c1d 16857read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16858{
16859 return bfd_get_signed_16 (abfd, buf);
16860}
16861
c906108c 16862static unsigned int
a1855c1d 16863read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16864{
fe1b8b76 16865 return bfd_get_32 (abfd, buf);
c906108c
SS
16866}
16867
21ae7a4d 16868static int
a1855c1d 16869read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16870{
16871 return bfd_get_signed_32 (abfd, buf);
16872}
16873
93311388 16874static ULONGEST
a1855c1d 16875read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16876{
fe1b8b76 16877 return bfd_get_64 (abfd, buf);
c906108c
SS
16878}
16879
16880static CORE_ADDR
d521ce57 16881read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16882 unsigned int *bytes_read)
c906108c 16883{
e7c27a73 16884 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16885 CORE_ADDR retval = 0;
16886
107d2387 16887 if (cu_header->signed_addr_p)
c906108c 16888 {
107d2387
AC
16889 switch (cu_header->addr_size)
16890 {
16891 case 2:
fe1b8b76 16892 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16893 break;
16894 case 4:
fe1b8b76 16895 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16896 break;
16897 case 8:
fe1b8b76 16898 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16899 break;
16900 default:
8e65ff28 16901 internal_error (__FILE__, __LINE__,
e2e0b3e5 16902 _("read_address: bad switch, signed [in module %s]"),
659b0389 16903 bfd_get_filename (abfd));
107d2387
AC
16904 }
16905 }
16906 else
16907 {
16908 switch (cu_header->addr_size)
16909 {
16910 case 2:
fe1b8b76 16911 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16912 break;
16913 case 4:
fe1b8b76 16914 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16915 break;
16916 case 8:
fe1b8b76 16917 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16918 break;
16919 default:
8e65ff28 16920 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16921 _("read_address: bad switch, "
16922 "unsigned [in module %s]"),
659b0389 16923 bfd_get_filename (abfd));
107d2387 16924 }
c906108c 16925 }
64367e0a 16926
107d2387
AC
16927 *bytes_read = cu_header->addr_size;
16928 return retval;
c906108c
SS
16929}
16930
f7ef9339 16931/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16932 specification allows the initial length to take up either 4 bytes
16933 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16934 bytes describe the length and all offsets will be 8 bytes in length
16935 instead of 4.
16936
f7ef9339
KB
16937 An older, non-standard 64-bit format is also handled by this
16938 function. The older format in question stores the initial length
16939 as an 8-byte quantity without an escape value. Lengths greater
16940 than 2^32 aren't very common which means that the initial 4 bytes
16941 is almost always zero. Since a length value of zero doesn't make
16942 sense for the 32-bit format, this initial zero can be considered to
16943 be an escape value which indicates the presence of the older 64-bit
16944 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16945 greater than 4GB. If it becomes necessary to handle lengths
16946 somewhat larger than 4GB, we could allow other small values (such
16947 as the non-sensical values of 1, 2, and 3) to also be used as
16948 escape values indicating the presence of the old format.
f7ef9339 16949
917c78fc
MK
16950 The value returned via bytes_read should be used to increment the
16951 relevant pointer after calling read_initial_length().
c764a876 16952
613e1657
KB
16953 [ Note: read_initial_length() and read_offset() are based on the
16954 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16955 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16956 from:
16957
f7ef9339 16958 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16959
613e1657
KB
16960 This document is only a draft and is subject to change. (So beware.)
16961
f7ef9339 16962 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16963 determined empirically by examining 64-bit ELF files produced by
16964 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16965
16966 - Kevin, July 16, 2002
613e1657
KB
16967 ] */
16968
16969static LONGEST
d521ce57 16970read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16971{
fe1b8b76 16972 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16973
dd373385 16974 if (length == 0xffffffff)
613e1657 16975 {
fe1b8b76 16976 length = bfd_get_64 (abfd, buf + 4);
613e1657 16977 *bytes_read = 12;
613e1657 16978 }
dd373385 16979 else if (length == 0)
f7ef9339 16980 {
dd373385 16981 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16982 length = bfd_get_64 (abfd, buf);
f7ef9339 16983 *bytes_read = 8;
f7ef9339 16984 }
613e1657
KB
16985 else
16986 {
16987 *bytes_read = 4;
613e1657
KB
16988 }
16989
c764a876
DE
16990 return length;
16991}
dd373385 16992
c764a876
DE
16993/* Cover function for read_initial_length.
16994 Returns the length of the object at BUF, and stores the size of the
16995 initial length in *BYTES_READ and stores the size that offsets will be in
16996 *OFFSET_SIZE.
16997 If the initial length size is not equivalent to that specified in
16998 CU_HEADER then issue a complaint.
16999 This is useful when reading non-comp-unit headers. */
dd373385 17000
c764a876 17001static LONGEST
d521ce57 17002read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17003 const struct comp_unit_head *cu_header,
17004 unsigned int *bytes_read,
17005 unsigned int *offset_size)
17006{
17007 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17008
17009 gdb_assert (cu_header->initial_length_size == 4
17010 || cu_header->initial_length_size == 8
17011 || cu_header->initial_length_size == 12);
17012
17013 if (cu_header->initial_length_size != *bytes_read)
17014 complaint (&symfile_complaints,
17015 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17016
c764a876 17017 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17018 return length;
613e1657
KB
17019}
17020
17021/* Read an offset from the data stream. The size of the offset is
917c78fc 17022 given by cu_header->offset_size. */
613e1657
KB
17023
17024static LONGEST
d521ce57
TT
17025read_offset (bfd *abfd, const gdb_byte *buf,
17026 const struct comp_unit_head *cu_header,
891d2f0b 17027 unsigned int *bytes_read)
c764a876
DE
17028{
17029 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17030
c764a876
DE
17031 *bytes_read = cu_header->offset_size;
17032 return offset;
17033}
17034
17035/* Read an offset from the data stream. */
17036
17037static LONGEST
d521ce57 17038read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17039{
17040 LONGEST retval = 0;
17041
c764a876 17042 switch (offset_size)
613e1657
KB
17043 {
17044 case 4:
fe1b8b76 17045 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17046 break;
17047 case 8:
fe1b8b76 17048 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17049 break;
17050 default:
8e65ff28 17051 internal_error (__FILE__, __LINE__,
c764a876 17052 _("read_offset_1: bad switch [in module %s]"),
659b0389 17053 bfd_get_filename (abfd));
613e1657
KB
17054 }
17055
917c78fc 17056 return retval;
613e1657
KB
17057}
17058
d521ce57
TT
17059static const gdb_byte *
17060read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17061{
17062 /* If the size of a host char is 8 bits, we can return a pointer
17063 to the buffer, otherwise we have to copy the data to a buffer
17064 allocated on the temporary obstack. */
4bdf3d34 17065 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17066 return buf;
c906108c
SS
17067}
17068
d521ce57
TT
17069static const char *
17070read_direct_string (bfd *abfd, const gdb_byte *buf,
17071 unsigned int *bytes_read_ptr)
c906108c
SS
17072{
17073 /* If the size of a host char is 8 bits, we can return a pointer
17074 to the string, otherwise we have to copy the string to a buffer
17075 allocated on the temporary obstack. */
4bdf3d34 17076 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17077 if (*buf == '\0')
17078 {
17079 *bytes_read_ptr = 1;
17080 return NULL;
17081 }
d521ce57
TT
17082 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17083 return (const char *) buf;
4bdf3d34
JJ
17084}
17085
43988095
JK
17086/* Return pointer to string at section SECT offset STR_OFFSET with error
17087 reporting strings FORM_NAME and SECT_NAME. */
17088
d521ce57 17089static const char *
43988095
JK
17090read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17091 struct dwarf2_section_info *sect,
17092 const char *form_name,
17093 const char *sect_name)
17094{
17095 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17096 if (sect->buffer == NULL)
17097 error (_("%s used without %s section [in module %s]"),
17098 form_name, sect_name, bfd_get_filename (abfd));
17099 if (str_offset >= sect->size)
17100 error (_("%s pointing outside of %s section [in module %s]"),
17101 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17102 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17103 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17104 return NULL;
43988095
JK
17105 return (const char *) (sect->buffer + str_offset);
17106}
17107
17108/* Return pointer to string at .debug_str offset STR_OFFSET. */
17109
17110static const char *
17111read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17112{
17113 return read_indirect_string_at_offset_from (abfd, str_offset,
17114 &dwarf2_per_objfile->str,
17115 "DW_FORM_strp", ".debug_str");
17116}
17117
17118/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17119
17120static const char *
17121read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17122{
17123 return read_indirect_string_at_offset_from (abfd, str_offset,
17124 &dwarf2_per_objfile->line_str,
17125 "DW_FORM_line_strp",
17126 ".debug_line_str");
c906108c
SS
17127}
17128
36586728
TT
17129/* Read a string at offset STR_OFFSET in the .debug_str section from
17130 the .dwz file DWZ. Throw an error if the offset is too large. If
17131 the string consists of a single NUL byte, return NULL; otherwise
17132 return a pointer to the string. */
17133
d521ce57 17134static const char *
36586728
TT
17135read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17136{
17137 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17138
17139 if (dwz->str.buffer == NULL)
17140 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17141 "section [in module %s]"),
17142 bfd_get_filename (dwz->dwz_bfd));
17143 if (str_offset >= dwz->str.size)
17144 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17145 ".debug_str section [in module %s]"),
17146 bfd_get_filename (dwz->dwz_bfd));
17147 gdb_assert (HOST_CHAR_BIT == 8);
17148 if (dwz->str.buffer[str_offset] == '\0')
17149 return NULL;
d521ce57 17150 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17151}
17152
43988095
JK
17153/* Return pointer to string at .debug_str offset as read from BUF.
17154 BUF is assumed to be in a compilation unit described by CU_HEADER.
17155 Return *BYTES_READ_PTR count of bytes read from BUF. */
17156
d521ce57
TT
17157static const char *
17158read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17159 const struct comp_unit_head *cu_header,
17160 unsigned int *bytes_read_ptr)
17161{
17162 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17163
17164 return read_indirect_string_at_offset (abfd, str_offset);
17165}
17166
43988095
JK
17167/* Return pointer to string at .debug_line_str offset as read from BUF.
17168 BUF is assumed to be in a compilation unit described by CU_HEADER.
17169 Return *BYTES_READ_PTR count of bytes read from BUF. */
17170
17171static const char *
17172read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17173 const struct comp_unit_head *cu_header,
17174 unsigned int *bytes_read_ptr)
17175{
17176 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17177
17178 return read_indirect_line_string_at_offset (abfd, str_offset);
17179}
17180
17181ULONGEST
d521ce57 17182read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17183 unsigned int *bytes_read_ptr)
c906108c 17184{
12df843f 17185 ULONGEST result;
ce5d95e1 17186 unsigned int num_read;
870f88f7 17187 int shift;
c906108c
SS
17188 unsigned char byte;
17189
17190 result = 0;
17191 shift = 0;
17192 num_read = 0;
c906108c
SS
17193 while (1)
17194 {
fe1b8b76 17195 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17196 buf++;
17197 num_read++;
12df843f 17198 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17199 if ((byte & 128) == 0)
17200 {
17201 break;
17202 }
17203 shift += 7;
17204 }
17205 *bytes_read_ptr = num_read;
17206 return result;
17207}
17208
12df843f 17209static LONGEST
d521ce57
TT
17210read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17211 unsigned int *bytes_read_ptr)
c906108c 17212{
12df843f 17213 LONGEST result;
870f88f7 17214 int shift, num_read;
c906108c
SS
17215 unsigned char byte;
17216
17217 result = 0;
17218 shift = 0;
c906108c 17219 num_read = 0;
c906108c
SS
17220 while (1)
17221 {
fe1b8b76 17222 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17223 buf++;
17224 num_read++;
12df843f 17225 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17226 shift += 7;
17227 if ((byte & 128) == 0)
17228 {
17229 break;
17230 }
17231 }
77e0b926 17232 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17233 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17234 *bytes_read_ptr = num_read;
17235 return result;
17236}
17237
3019eac3
DE
17238/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17239 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17240 ADDR_SIZE is the size of addresses from the CU header. */
17241
17242static CORE_ADDR
17243read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17244{
17245 struct objfile *objfile = dwarf2_per_objfile->objfile;
17246 bfd *abfd = objfile->obfd;
17247 const gdb_byte *info_ptr;
17248
17249 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17250 if (dwarf2_per_objfile->addr.buffer == NULL)
17251 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17252 objfile_name (objfile));
3019eac3
DE
17253 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17254 error (_("DW_FORM_addr_index pointing outside of "
17255 ".debug_addr section [in module %s]"),
4262abfb 17256 objfile_name (objfile));
3019eac3
DE
17257 info_ptr = (dwarf2_per_objfile->addr.buffer
17258 + addr_base + addr_index * addr_size);
17259 if (addr_size == 4)
17260 return bfd_get_32 (abfd, info_ptr);
17261 else
17262 return bfd_get_64 (abfd, info_ptr);
17263}
17264
17265/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17266
17267static CORE_ADDR
17268read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17269{
17270 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17271}
17272
17273/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17274
17275static CORE_ADDR
d521ce57 17276read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17277 unsigned int *bytes_read)
17278{
17279 bfd *abfd = cu->objfile->obfd;
17280 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17281
17282 return read_addr_index (cu, addr_index);
17283}
17284
17285/* Data structure to pass results from dwarf2_read_addr_index_reader
17286 back to dwarf2_read_addr_index. */
17287
17288struct dwarf2_read_addr_index_data
17289{
17290 ULONGEST addr_base;
17291 int addr_size;
17292};
17293
17294/* die_reader_func for dwarf2_read_addr_index. */
17295
17296static void
17297dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17298 const gdb_byte *info_ptr,
3019eac3
DE
17299 struct die_info *comp_unit_die,
17300 int has_children,
17301 void *data)
17302{
17303 struct dwarf2_cu *cu = reader->cu;
17304 struct dwarf2_read_addr_index_data *aidata =
17305 (struct dwarf2_read_addr_index_data *) data;
17306
17307 aidata->addr_base = cu->addr_base;
17308 aidata->addr_size = cu->header.addr_size;
17309}
17310
17311/* Given an index in .debug_addr, fetch the value.
17312 NOTE: This can be called during dwarf expression evaluation,
17313 long after the debug information has been read, and thus per_cu->cu
17314 may no longer exist. */
17315
17316CORE_ADDR
17317dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17318 unsigned int addr_index)
17319{
17320 struct objfile *objfile = per_cu->objfile;
17321 struct dwarf2_cu *cu = per_cu->cu;
17322 ULONGEST addr_base;
17323 int addr_size;
17324
17325 /* This is intended to be called from outside this file. */
17326 dw2_setup (objfile);
17327
17328 /* We need addr_base and addr_size.
17329 If we don't have PER_CU->cu, we have to get it.
17330 Nasty, but the alternative is storing the needed info in PER_CU,
17331 which at this point doesn't seem justified: it's not clear how frequently
17332 it would get used and it would increase the size of every PER_CU.
17333 Entry points like dwarf2_per_cu_addr_size do a similar thing
17334 so we're not in uncharted territory here.
17335 Alas we need to be a bit more complicated as addr_base is contained
17336 in the DIE.
17337
17338 We don't need to read the entire CU(/TU).
17339 We just need the header and top level die.
a1b64ce1 17340
3019eac3 17341 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17342 For now we skip this optimization. */
3019eac3
DE
17343
17344 if (cu != NULL)
17345 {
17346 addr_base = cu->addr_base;
17347 addr_size = cu->header.addr_size;
17348 }
17349 else
17350 {
17351 struct dwarf2_read_addr_index_data aidata;
17352
a1b64ce1
DE
17353 /* Note: We can't use init_cutu_and_read_dies_simple here,
17354 we need addr_base. */
17355 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17356 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17357 addr_base = aidata.addr_base;
17358 addr_size = aidata.addr_size;
17359 }
17360
17361 return read_addr_index_1 (addr_index, addr_base, addr_size);
17362}
17363
57d63ce2
DE
17364/* Given a DW_FORM_GNU_str_index, fetch the string.
17365 This is only used by the Fission support. */
3019eac3 17366
d521ce57 17367static const char *
342587c4 17368read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17369{
17370 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17371 const char *objf_name = objfile_name (objfile);
3019eac3 17372 bfd *abfd = objfile->obfd;
342587c4 17373 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17374 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17375 struct dwarf2_section_info *str_offsets_section =
17376 &reader->dwo_file->sections.str_offsets;
d521ce57 17377 const gdb_byte *info_ptr;
3019eac3 17378 ULONGEST str_offset;
57d63ce2 17379 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17380
73869dc2
DE
17381 dwarf2_read_section (objfile, str_section);
17382 dwarf2_read_section (objfile, str_offsets_section);
17383 if (str_section->buffer == NULL)
57d63ce2 17384 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17385 " in CU at offset 0x%x [in module %s]"),
17386 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17387 if (str_offsets_section->buffer == NULL)
57d63ce2 17388 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17389 " in CU at offset 0x%x [in module %s]"),
17390 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17391 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17392 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17393 " section in CU at offset 0x%x [in module %s]"),
17394 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17395 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17396 + str_index * cu->header.offset_size);
17397 if (cu->header.offset_size == 4)
17398 str_offset = bfd_get_32 (abfd, info_ptr);
17399 else
17400 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17401 if (str_offset >= str_section->size)
57d63ce2 17402 error (_("Offset from %s pointing outside of"
9c541725
PA
17403 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17404 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17405 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17406}
17407
3019eac3
DE
17408/* Return the length of an LEB128 number in BUF. */
17409
17410static int
17411leb128_size (const gdb_byte *buf)
17412{
17413 const gdb_byte *begin = buf;
17414 gdb_byte byte;
17415
17416 while (1)
17417 {
17418 byte = *buf++;
17419 if ((byte & 128) == 0)
17420 return buf - begin;
17421 }
17422}
17423
c906108c 17424static void
e142c38c 17425set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17426{
17427 switch (lang)
17428 {
17429 case DW_LANG_C89:
76bee0cc 17430 case DW_LANG_C99:
0cfd832f 17431 case DW_LANG_C11:
c906108c 17432 case DW_LANG_C:
d1be3247 17433 case DW_LANG_UPC:
e142c38c 17434 cu->language = language_c;
c906108c 17435 break;
9c37b5ae 17436 case DW_LANG_Java:
c906108c 17437 case DW_LANG_C_plus_plus:
0cfd832f
MW
17438 case DW_LANG_C_plus_plus_11:
17439 case DW_LANG_C_plus_plus_14:
e142c38c 17440 cu->language = language_cplus;
c906108c 17441 break;
6aecb9c2
JB
17442 case DW_LANG_D:
17443 cu->language = language_d;
17444 break;
c906108c
SS
17445 case DW_LANG_Fortran77:
17446 case DW_LANG_Fortran90:
b21b22e0 17447 case DW_LANG_Fortran95:
f7de9aab
MW
17448 case DW_LANG_Fortran03:
17449 case DW_LANG_Fortran08:
e142c38c 17450 cu->language = language_fortran;
c906108c 17451 break;
a766d390
DE
17452 case DW_LANG_Go:
17453 cu->language = language_go;
17454 break;
c906108c 17455 case DW_LANG_Mips_Assembler:
e142c38c 17456 cu->language = language_asm;
c906108c
SS
17457 break;
17458 case DW_LANG_Ada83:
8aaf0b47 17459 case DW_LANG_Ada95:
bc5f45f8
JB
17460 cu->language = language_ada;
17461 break;
72019c9c
GM
17462 case DW_LANG_Modula2:
17463 cu->language = language_m2;
17464 break;
fe8e67fd
PM
17465 case DW_LANG_Pascal83:
17466 cu->language = language_pascal;
17467 break;
22566fbd
DJ
17468 case DW_LANG_ObjC:
17469 cu->language = language_objc;
17470 break;
c44af4eb
TT
17471 case DW_LANG_Rust:
17472 case DW_LANG_Rust_old:
17473 cu->language = language_rust;
17474 break;
c906108c
SS
17475 case DW_LANG_Cobol74:
17476 case DW_LANG_Cobol85:
c906108c 17477 default:
e142c38c 17478 cu->language = language_minimal;
c906108c
SS
17479 break;
17480 }
e142c38c 17481 cu->language_defn = language_def (cu->language);
c906108c
SS
17482}
17483
17484/* Return the named attribute or NULL if not there. */
17485
17486static struct attribute *
e142c38c 17487dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17488{
a48e046c 17489 for (;;)
c906108c 17490 {
a48e046c
TT
17491 unsigned int i;
17492 struct attribute *spec = NULL;
17493
17494 for (i = 0; i < die->num_attrs; ++i)
17495 {
17496 if (die->attrs[i].name == name)
17497 return &die->attrs[i];
17498 if (die->attrs[i].name == DW_AT_specification
17499 || die->attrs[i].name == DW_AT_abstract_origin)
17500 spec = &die->attrs[i];
17501 }
17502
17503 if (!spec)
17504 break;
c906108c 17505
f2f0e013 17506 die = follow_die_ref (die, spec, &cu);
f2f0e013 17507 }
c5aa993b 17508
c906108c
SS
17509 return NULL;
17510}
17511
348e048f
DE
17512/* Return the named attribute or NULL if not there,
17513 but do not follow DW_AT_specification, etc.
17514 This is for use in contexts where we're reading .debug_types dies.
17515 Following DW_AT_specification, DW_AT_abstract_origin will take us
17516 back up the chain, and we want to go down. */
17517
17518static struct attribute *
45e58e77 17519dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17520{
17521 unsigned int i;
17522
17523 for (i = 0; i < die->num_attrs; ++i)
17524 if (die->attrs[i].name == name)
17525 return &die->attrs[i];
17526
17527 return NULL;
17528}
17529
7d45c7c3
KB
17530/* Return the string associated with a string-typed attribute, or NULL if it
17531 is either not found or is of an incorrect type. */
17532
17533static const char *
17534dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17535{
17536 struct attribute *attr;
17537 const char *str = NULL;
17538
17539 attr = dwarf2_attr (die, name, cu);
17540
17541 if (attr != NULL)
17542 {
43988095
JK
17543 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17544 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17545 str = DW_STRING (attr);
17546 else
17547 complaint (&symfile_complaints,
17548 _("string type expected for attribute %s for "
17549 "DIE at 0x%x in module %s"),
9c541725 17550 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17551 objfile_name (cu->objfile));
17552 }
17553
17554 return str;
17555}
17556
05cf31d1
JB
17557/* Return non-zero iff the attribute NAME is defined for the given DIE,
17558 and holds a non-zero value. This function should only be used for
2dc7f7b3 17559 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17560
17561static int
17562dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17563{
17564 struct attribute *attr = dwarf2_attr (die, name, cu);
17565
17566 return (attr && DW_UNSND (attr));
17567}
17568
3ca72b44 17569static int
e142c38c 17570die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17571{
05cf31d1
JB
17572 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17573 which value is non-zero. However, we have to be careful with
17574 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17575 (via dwarf2_flag_true_p) follows this attribute. So we may
17576 end up accidently finding a declaration attribute that belongs
17577 to a different DIE referenced by the specification attribute,
17578 even though the given DIE does not have a declaration attribute. */
17579 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17580 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17581}
17582
63d06c5c 17583/* Return the die giving the specification for DIE, if there is
f2f0e013 17584 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17585 containing the return value on output. If there is no
17586 specification, but there is an abstract origin, that is
17587 returned. */
63d06c5c
DC
17588
17589static struct die_info *
f2f0e013 17590die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17591{
f2f0e013
DJ
17592 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17593 *spec_cu);
63d06c5c 17594
edb3359d
DJ
17595 if (spec_attr == NULL)
17596 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17597
63d06c5c
DC
17598 if (spec_attr == NULL)
17599 return NULL;
17600 else
f2f0e013 17601 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17602}
c906108c 17603
527f3840
JK
17604/* Stub for free_line_header to match void * callback types. */
17605
17606static void
17607free_line_header_voidp (void *arg)
17608{
9a3c8263 17609 struct line_header *lh = (struct line_header *) arg;
527f3840 17610
fff8551c 17611 delete lh;
527f3840
JK
17612}
17613
fff8551c
PA
17614void
17615line_header::add_include_dir (const char *include_dir)
c906108c 17616{
27e0867f 17617 if (dwarf_line_debug >= 2)
fff8551c
PA
17618 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17619 include_dirs.size () + 1, include_dir);
27e0867f 17620
fff8551c 17621 include_dirs.push_back (include_dir);
debd256d 17622}
6e70227d 17623
fff8551c
PA
17624void
17625line_header::add_file_name (const char *name,
ecfb656c 17626 dir_index d_index,
fff8551c
PA
17627 unsigned int mod_time,
17628 unsigned int length)
debd256d 17629{
27e0867f
DE
17630 if (dwarf_line_debug >= 2)
17631 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17632 (unsigned) file_names.size () + 1, name);
27e0867f 17633
ecfb656c 17634 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17635}
6e70227d 17636
83769d0b 17637/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17638
17639static struct dwarf2_section_info *
17640get_debug_line_section (struct dwarf2_cu *cu)
17641{
17642 struct dwarf2_section_info *section;
17643
17644 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17645 DWO file. */
17646 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17647 section = &cu->dwo_unit->dwo_file->sections.line;
17648 else if (cu->per_cu->is_dwz)
17649 {
17650 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17651
17652 section = &dwz->line;
17653 }
17654 else
17655 section = &dwarf2_per_objfile->line;
17656
17657 return section;
17658}
17659
43988095
JK
17660/* Read directory or file name entry format, starting with byte of
17661 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17662 entries count and the entries themselves in the described entry
17663 format. */
17664
17665static void
17666read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17667 struct line_header *lh,
17668 const struct comp_unit_head *cu_header,
17669 void (*callback) (struct line_header *lh,
17670 const char *name,
ecfb656c 17671 dir_index d_index,
43988095
JK
17672 unsigned int mod_time,
17673 unsigned int length))
17674{
17675 gdb_byte format_count, formati;
17676 ULONGEST data_count, datai;
17677 const gdb_byte *buf = *bufp;
17678 const gdb_byte *format_header_data;
17679 int i;
17680 unsigned int bytes_read;
17681
17682 format_count = read_1_byte (abfd, buf);
17683 buf += 1;
17684 format_header_data = buf;
17685 for (formati = 0; formati < format_count; formati++)
17686 {
17687 read_unsigned_leb128 (abfd, buf, &bytes_read);
17688 buf += bytes_read;
17689 read_unsigned_leb128 (abfd, buf, &bytes_read);
17690 buf += bytes_read;
17691 }
17692
17693 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17694 buf += bytes_read;
17695 for (datai = 0; datai < data_count; datai++)
17696 {
17697 const gdb_byte *format = format_header_data;
17698 struct file_entry fe;
17699
43988095
JK
17700 for (formati = 0; formati < format_count; formati++)
17701 {
ecfb656c 17702 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17703 format += bytes_read;
43988095 17704
ecfb656c 17705 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17706 format += bytes_read;
ecfb656c
PA
17707
17708 gdb::optional<const char *> string;
17709 gdb::optional<unsigned int> uint;
17710
43988095
JK
17711 switch (form)
17712 {
17713 case DW_FORM_string:
ecfb656c 17714 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17715 buf += bytes_read;
17716 break;
17717
17718 case DW_FORM_line_strp:
ecfb656c
PA
17719 string.emplace (read_indirect_line_string (abfd, buf,
17720 cu_header,
17721 &bytes_read));
43988095
JK
17722 buf += bytes_read;
17723 break;
17724
17725 case DW_FORM_data1:
ecfb656c 17726 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17727 buf += 1;
17728 break;
17729
17730 case DW_FORM_data2:
ecfb656c 17731 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17732 buf += 2;
17733 break;
17734
17735 case DW_FORM_data4:
ecfb656c 17736 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17737 buf += 4;
17738 break;
17739
17740 case DW_FORM_data8:
ecfb656c 17741 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17742 buf += 8;
17743 break;
17744
17745 case DW_FORM_udata:
ecfb656c 17746 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17747 buf += bytes_read;
17748 break;
17749
17750 case DW_FORM_block:
17751 /* It is valid only for DW_LNCT_timestamp which is ignored by
17752 current GDB. */
17753 break;
17754 }
ecfb656c
PA
17755
17756 switch (content_type)
17757 {
17758 case DW_LNCT_path:
17759 if (string.has_value ())
17760 fe.name = *string;
17761 break;
17762 case DW_LNCT_directory_index:
17763 if (uint.has_value ())
17764 fe.d_index = (dir_index) *uint;
17765 break;
17766 case DW_LNCT_timestamp:
17767 if (uint.has_value ())
17768 fe.mod_time = *uint;
17769 break;
17770 case DW_LNCT_size:
17771 if (uint.has_value ())
17772 fe.length = *uint;
17773 break;
17774 case DW_LNCT_MD5:
17775 break;
17776 default:
17777 complaint (&symfile_complaints,
17778 _("Unknown format content type %s"),
17779 pulongest (content_type));
17780 }
43988095
JK
17781 }
17782
ecfb656c 17783 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17784 }
17785
17786 *bufp = buf;
17787}
17788
debd256d 17789/* Read the statement program header starting at OFFSET in
3019eac3 17790 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17791 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17792 Returns NULL if there is a problem reading the header, e.g., if it
17793 has a version we don't understand.
debd256d
JB
17794
17795 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17796 the returned object point into the dwarf line section buffer,
17797 and must not be freed. */
ae2de4f8 17798
fff8551c 17799static line_header_up
9c541725 17800dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17801{
d521ce57 17802 const gdb_byte *line_ptr;
c764a876 17803 unsigned int bytes_read, offset_size;
debd256d 17804 int i;
d521ce57 17805 const char *cur_dir, *cur_file;
3019eac3
DE
17806 struct dwarf2_section_info *section;
17807 bfd *abfd;
17808
36586728 17809 section = get_debug_line_section (cu);
3019eac3
DE
17810 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17811 if (section->buffer == NULL)
debd256d 17812 {
3019eac3
DE
17813 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17814 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17815 else
17816 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17817 return 0;
17818 }
17819
fceca515
DE
17820 /* We can't do this until we know the section is non-empty.
17821 Only then do we know we have such a section. */
a32a8923 17822 abfd = get_section_bfd_owner (section);
fceca515 17823
a738430d
MK
17824 /* Make sure that at least there's room for the total_length field.
17825 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17826 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17827 {
4d3c2250 17828 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17829 return 0;
17830 }
17831
fff8551c 17832 line_header_up lh (new line_header ());
debd256d 17833
9c541725 17834 lh->sect_off = sect_off;
527f3840
JK
17835 lh->offset_in_dwz = cu->per_cu->is_dwz;
17836
9c541725 17837 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 17838
a738430d 17839 /* Read in the header. */
6e70227d 17840 lh->total_length =
c764a876
DE
17841 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17842 &bytes_read, &offset_size);
debd256d 17843 line_ptr += bytes_read;
3019eac3 17844 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17845 {
4d3c2250 17846 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17847 return 0;
17848 }
17849 lh->statement_program_end = line_ptr + lh->total_length;
17850 lh->version = read_2_bytes (abfd, line_ptr);
17851 line_ptr += 2;
43988095 17852 if (lh->version > 5)
cd366ee8
DE
17853 {
17854 /* This is a version we don't understand. The format could have
17855 changed in ways we don't handle properly so just punt. */
17856 complaint (&symfile_complaints,
17857 _("unsupported version in .debug_line section"));
17858 return NULL;
17859 }
43988095
JK
17860 if (lh->version >= 5)
17861 {
17862 gdb_byte segment_selector_size;
17863
17864 /* Skip address size. */
17865 read_1_byte (abfd, line_ptr);
17866 line_ptr += 1;
17867
17868 segment_selector_size = read_1_byte (abfd, line_ptr);
17869 line_ptr += 1;
17870 if (segment_selector_size != 0)
17871 {
17872 complaint (&symfile_complaints,
17873 _("unsupported segment selector size %u "
17874 "in .debug_line section"),
17875 segment_selector_size);
17876 return NULL;
17877 }
17878 }
c764a876
DE
17879 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17880 line_ptr += offset_size;
debd256d
JB
17881 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17882 line_ptr += 1;
2dc7f7b3
TT
17883 if (lh->version >= 4)
17884 {
17885 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17886 line_ptr += 1;
17887 }
17888 else
17889 lh->maximum_ops_per_instruction = 1;
17890
17891 if (lh->maximum_ops_per_instruction == 0)
17892 {
17893 lh->maximum_ops_per_instruction = 1;
17894 complaint (&symfile_complaints,
3e43a32a
MS
17895 _("invalid maximum_ops_per_instruction "
17896 "in `.debug_line' section"));
2dc7f7b3
TT
17897 }
17898
debd256d
JB
17899 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17900 line_ptr += 1;
17901 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17902 line_ptr += 1;
17903 lh->line_range = read_1_byte (abfd, line_ptr);
17904 line_ptr += 1;
17905 lh->opcode_base = read_1_byte (abfd, line_ptr);
17906 line_ptr += 1;
fff8551c 17907 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
17908
17909 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17910 for (i = 1; i < lh->opcode_base; ++i)
17911 {
17912 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17913 line_ptr += 1;
17914 }
17915
43988095 17916 if (lh->version >= 5)
debd256d 17917 {
43988095 17918 /* Read directory table. */
fff8551c
PA
17919 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17920 [] (struct line_header *lh, const char *name,
ecfb656c 17921 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17922 unsigned int length)
17923 {
17924 lh->add_include_dir (name);
17925 });
debd256d 17926
43988095 17927 /* Read file name table. */
fff8551c
PA
17928 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17929 [] (struct line_header *lh, const char *name,
ecfb656c 17930 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17931 unsigned int length)
17932 {
ecfb656c 17933 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 17934 });
43988095
JK
17935 }
17936 else
debd256d 17937 {
43988095
JK
17938 /* Read directory table. */
17939 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17940 {
17941 line_ptr += bytes_read;
fff8551c 17942 lh->add_include_dir (cur_dir);
43988095 17943 }
debd256d
JB
17944 line_ptr += bytes_read;
17945
43988095
JK
17946 /* Read file name table. */
17947 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17948 {
ecfb656c
PA
17949 unsigned int mod_time, length;
17950 dir_index d_index;
43988095
JK
17951
17952 line_ptr += bytes_read;
ecfb656c 17953 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
17954 line_ptr += bytes_read;
17955 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17956 line_ptr += bytes_read;
17957 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17958 line_ptr += bytes_read;
17959
ecfb656c 17960 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
17961 }
17962 line_ptr += bytes_read;
debd256d 17963 }
6e70227d 17964 lh->statement_program_start = line_ptr;
debd256d 17965
3019eac3 17966 if (line_ptr > (section->buffer + section->size))
4d3c2250 17967 complaint (&symfile_complaints,
3e43a32a
MS
17968 _("line number info header doesn't "
17969 "fit in `.debug_line' section"));
debd256d 17970
debd256d
JB
17971 return lh;
17972}
c906108c 17973
c6da4cef
DE
17974/* Subroutine of dwarf_decode_lines to simplify it.
17975 Return the file name of the psymtab for included file FILE_INDEX
17976 in line header LH of PST.
17977 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17978 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17979 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17980
17981 The function creates dangling cleanup registration. */
c6da4cef 17982
d521ce57 17983static const char *
c6da4cef
DE
17984psymtab_include_file_name (const struct line_header *lh, int file_index,
17985 const struct partial_symtab *pst,
17986 const char *comp_dir)
17987{
8c43009f 17988 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
17989 const char *include_name = fe.name;
17990 const char *include_name_to_compare = include_name;
72b9f47f
TT
17991 const char *pst_filename;
17992 char *copied_name = NULL;
c6da4cef
DE
17993 int file_is_pst;
17994
8c43009f 17995 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
17996
17997 if (!IS_ABSOLUTE_PATH (include_name)
17998 && (dir_name != NULL || comp_dir != NULL))
17999 {
18000 /* Avoid creating a duplicate psymtab for PST.
18001 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18002 Before we do the comparison, however, we need to account
18003 for DIR_NAME and COMP_DIR.
18004 First prepend dir_name (if non-NULL). If we still don't
18005 have an absolute path prepend comp_dir (if non-NULL).
18006 However, the directory we record in the include-file's
18007 psymtab does not contain COMP_DIR (to match the
18008 corresponding symtab(s)).
18009
18010 Example:
18011
18012 bash$ cd /tmp
18013 bash$ gcc -g ./hello.c
18014 include_name = "hello.c"
18015 dir_name = "."
18016 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18017 DW_AT_name = "./hello.c"
18018
18019 */
c6da4cef
DE
18020
18021 if (dir_name != NULL)
18022 {
d521ce57
TT
18023 char *tem = concat (dir_name, SLASH_STRING,
18024 include_name, (char *)NULL);
18025
18026 make_cleanup (xfree, tem);
18027 include_name = tem;
c6da4cef 18028 include_name_to_compare = include_name;
c6da4cef
DE
18029 }
18030 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18031 {
d521ce57
TT
18032 char *tem = concat (comp_dir, SLASH_STRING,
18033 include_name, (char *)NULL);
18034
18035 make_cleanup (xfree, tem);
18036 include_name_to_compare = tem;
c6da4cef
DE
18037 }
18038 }
18039
18040 pst_filename = pst->filename;
18041 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18042 {
72b9f47f
TT
18043 copied_name = concat (pst->dirname, SLASH_STRING,
18044 pst_filename, (char *)NULL);
18045 pst_filename = copied_name;
c6da4cef
DE
18046 }
18047
1e3fad37 18048 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18049
72b9f47f
TT
18050 if (copied_name != NULL)
18051 xfree (copied_name);
c6da4cef
DE
18052
18053 if (file_is_pst)
18054 return NULL;
18055 return include_name;
18056}
18057
d9b3de22
DE
18058/* State machine to track the state of the line number program. */
18059
6f77053d 18060class lnp_state_machine
d9b3de22 18061{
6f77053d
PA
18062public:
18063 /* Initialize a machine state for the start of a line number
18064 program. */
18065 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18066
8c43009f
PA
18067 file_entry *current_file ()
18068 {
18069 /* lh->file_names is 0-based, but the file name numbers in the
18070 statement program are 1-based. */
6f77053d
PA
18071 return m_line_header->file_name_at (m_file);
18072 }
18073
18074 /* Record the line in the state machine. END_SEQUENCE is true if
18075 we're processing the end of a sequence. */
18076 void record_line (bool end_sequence);
18077
18078 /* Check address and if invalid nop-out the rest of the lines in this
18079 sequence. */
18080 void check_line_address (struct dwarf2_cu *cu,
18081 const gdb_byte *line_ptr,
18082 CORE_ADDR lowpc, CORE_ADDR address);
18083
18084 void handle_set_discriminator (unsigned int discriminator)
18085 {
18086 m_discriminator = discriminator;
18087 m_line_has_non_zero_discriminator |= discriminator != 0;
18088 }
18089
18090 /* Handle DW_LNE_set_address. */
18091 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18092 {
18093 m_op_index = 0;
18094 address += baseaddr;
18095 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18096 }
18097
18098 /* Handle DW_LNS_advance_pc. */
18099 void handle_advance_pc (CORE_ADDR adjust);
18100
18101 /* Handle a special opcode. */
18102 void handle_special_opcode (unsigned char op_code);
18103
18104 /* Handle DW_LNS_advance_line. */
18105 void handle_advance_line (int line_delta)
18106 {
18107 advance_line (line_delta);
18108 }
18109
18110 /* Handle DW_LNS_set_file. */
18111 void handle_set_file (file_name_index file);
18112
18113 /* Handle DW_LNS_negate_stmt. */
18114 void handle_negate_stmt ()
18115 {
18116 m_is_stmt = !m_is_stmt;
18117 }
18118
18119 /* Handle DW_LNS_const_add_pc. */
18120 void handle_const_add_pc ();
18121
18122 /* Handle DW_LNS_fixed_advance_pc. */
18123 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18124 {
18125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18126 m_op_index = 0;
18127 }
18128
18129 /* Handle DW_LNS_copy. */
18130 void handle_copy ()
18131 {
18132 record_line (false);
18133 m_discriminator = 0;
18134 }
18135
18136 /* Handle DW_LNE_end_sequence. */
18137 void handle_end_sequence ()
18138 {
18139 m_record_line_callback = ::record_line;
18140 }
18141
18142private:
18143 /* Advance the line by LINE_DELTA. */
18144 void advance_line (int line_delta)
18145 {
18146 m_line += line_delta;
18147
18148 if (line_delta != 0)
18149 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18150 }
18151
6f77053d
PA
18152 gdbarch *m_gdbarch;
18153
18154 /* True if we're recording lines.
18155 Otherwise we're building partial symtabs and are just interested in
18156 finding include files mentioned by the line number program. */
18157 bool m_record_lines_p;
18158
8c43009f 18159 /* The line number header. */
6f77053d 18160 line_header *m_line_header;
8c43009f 18161
6f77053d
PA
18162 /* These are part of the standard DWARF line number state machine,
18163 and initialized according to the DWARF spec. */
d9b3de22 18164
6f77053d 18165 unsigned char m_op_index = 0;
8c43009f 18166 /* The line table index (1-based) of the current file. */
6f77053d
PA
18167 file_name_index m_file = (file_name_index) 1;
18168 unsigned int m_line = 1;
18169
18170 /* These are initialized in the constructor. */
18171
18172 CORE_ADDR m_address;
18173 bool m_is_stmt;
18174 unsigned int m_discriminator;
d9b3de22
DE
18175
18176 /* Additional bits of state we need to track. */
18177
18178 /* The last file that we called dwarf2_start_subfile for.
18179 This is only used for TLLs. */
6f77053d 18180 unsigned int m_last_file = 0;
d9b3de22 18181 /* The last file a line number was recorded for. */
6f77053d 18182 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18183
18184 /* The function to call to record a line. */
6f77053d 18185 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18186
18187 /* The last line number that was recorded, used to coalesce
18188 consecutive entries for the same line. This can happen, for
18189 example, when discriminators are present. PR 17276. */
6f77053d
PA
18190 unsigned int m_last_line = 0;
18191 bool m_line_has_non_zero_discriminator = false;
8c43009f 18192};
d9b3de22 18193
6f77053d
PA
18194void
18195lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18196{
18197 CORE_ADDR addr_adj = (((m_op_index + adjust)
18198 / m_line_header->maximum_ops_per_instruction)
18199 * m_line_header->minimum_instruction_length);
18200 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18201 m_op_index = ((m_op_index + adjust)
18202 % m_line_header->maximum_ops_per_instruction);
18203}
d9b3de22 18204
6f77053d
PA
18205void
18206lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18207{
6f77053d
PA
18208 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18209 CORE_ADDR addr_adj = (((m_op_index
18210 + (adj_opcode / m_line_header->line_range))
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 + (adj_opcode / m_line_header->line_range))
18215 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18216
6f77053d
PA
18217 int line_delta = (m_line_header->line_base
18218 + (adj_opcode % m_line_header->line_range));
18219 advance_line (line_delta);
18220 record_line (false);
18221 m_discriminator = 0;
18222}
d9b3de22 18223
6f77053d
PA
18224void
18225lnp_state_machine::handle_set_file (file_name_index file)
18226{
18227 m_file = file;
18228
18229 const file_entry *fe = current_file ();
18230 if (fe == NULL)
18231 dwarf2_debug_line_missing_file_complaint ();
18232 else if (m_record_lines_p)
18233 {
18234 const char *dir = fe->include_dir (m_line_header);
18235
18236 m_last_subfile = current_subfile;
18237 m_line_has_non_zero_discriminator = m_discriminator != 0;
18238 dwarf2_start_subfile (fe->name, dir);
18239 }
18240}
18241
18242void
18243lnp_state_machine::handle_const_add_pc ()
18244{
18245 CORE_ADDR adjust
18246 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18247
18248 CORE_ADDR addr_adj
18249 = (((m_op_index + adjust)
18250 / m_line_header->maximum_ops_per_instruction)
18251 * m_line_header->minimum_instruction_length);
18252
18253 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18254 m_op_index = ((m_op_index + adjust)
18255 % m_line_header->maximum_ops_per_instruction);
18256}
d9b3de22 18257
c91513d8
PP
18258/* Ignore this record_line request. */
18259
18260static void
18261noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18262{
18263 return;
18264}
18265
a05a36a5
DE
18266/* Return non-zero if we should add LINE to the line number table.
18267 LINE is the line to add, LAST_LINE is the last line that was added,
18268 LAST_SUBFILE is the subfile for LAST_LINE.
18269 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18270 had a non-zero discriminator.
18271
18272 We have to be careful in the presence of discriminators.
18273 E.g., for this line:
18274
18275 for (i = 0; i < 100000; i++);
18276
18277 clang can emit four line number entries for that one line,
18278 each with a different discriminator.
18279 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18280
18281 However, we want gdb to coalesce all four entries into one.
18282 Otherwise the user could stepi into the middle of the line and
18283 gdb would get confused about whether the pc really was in the
18284 middle of the line.
18285
18286 Things are further complicated by the fact that two consecutive
18287 line number entries for the same line is a heuristic used by gcc
18288 to denote the end of the prologue. So we can't just discard duplicate
18289 entries, we have to be selective about it. The heuristic we use is
18290 that we only collapse consecutive entries for the same line if at least
18291 one of those entries has a non-zero discriminator. PR 17276.
18292
18293 Note: Addresses in the line number state machine can never go backwards
18294 within one sequence, thus this coalescing is ok. */
18295
18296static int
18297dwarf_record_line_p (unsigned int line, unsigned int last_line,
18298 int line_has_non_zero_discriminator,
18299 struct subfile *last_subfile)
18300{
18301 if (current_subfile != last_subfile)
18302 return 1;
18303 if (line != last_line)
18304 return 1;
18305 /* Same line for the same file that we've seen already.
18306 As a last check, for pr 17276, only record the line if the line
18307 has never had a non-zero discriminator. */
18308 if (!line_has_non_zero_discriminator)
18309 return 1;
18310 return 0;
18311}
18312
252a6764
DE
18313/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18314 in the line table of subfile SUBFILE. */
18315
18316static void
d9b3de22
DE
18317dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18318 unsigned int line, CORE_ADDR address,
18319 record_line_ftype p_record_line)
252a6764
DE
18320{
18321 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18322
27e0867f
DE
18323 if (dwarf_line_debug)
18324 {
18325 fprintf_unfiltered (gdb_stdlog,
18326 "Recording line %u, file %s, address %s\n",
18327 line, lbasename (subfile->name),
18328 paddress (gdbarch, address));
18329 }
18330
d5962de5 18331 (*p_record_line) (subfile, line, addr);
252a6764
DE
18332}
18333
18334/* Subroutine of dwarf_decode_lines_1 to simplify it.
18335 Mark the end of a set of line number records.
d9b3de22 18336 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18337 If SUBFILE is NULL the request is ignored. */
18338
18339static void
18340dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18341 CORE_ADDR address, record_line_ftype p_record_line)
18342{
27e0867f
DE
18343 if (subfile == NULL)
18344 return;
18345
18346 if (dwarf_line_debug)
18347 {
18348 fprintf_unfiltered (gdb_stdlog,
18349 "Finishing current line, file %s, address %s\n",
18350 lbasename (subfile->name),
18351 paddress (gdbarch, address));
18352 }
18353
d9b3de22
DE
18354 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18355}
18356
6f77053d
PA
18357void
18358lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18359{
d9b3de22
DE
18360 if (dwarf_line_debug)
18361 {
18362 fprintf_unfiltered (gdb_stdlog,
18363 "Processing actual line %u: file %u,"
18364 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18365 m_line, to_underlying (m_file),
18366 paddress (m_gdbarch, m_address),
18367 m_is_stmt, m_discriminator);
d9b3de22
DE
18368 }
18369
6f77053d 18370 file_entry *fe = current_file ();
8c43009f
PA
18371
18372 if (fe == NULL)
d9b3de22
DE
18373 dwarf2_debug_line_missing_file_complaint ();
18374 /* For now we ignore lines not starting on an instruction boundary.
18375 But not when processing end_sequence for compatibility with the
18376 previous version of the code. */
6f77053d 18377 else if (m_op_index == 0 || end_sequence)
d9b3de22 18378 {
8c43009f 18379 fe->included_p = 1;
6f77053d 18380 if (m_record_lines_p && m_is_stmt)
d9b3de22 18381 {
6f77053d 18382 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18383 {
6f77053d
PA
18384 dwarf_finish_line (m_gdbarch, m_last_subfile,
18385 m_address, m_record_line_callback);
d9b3de22
DE
18386 }
18387
18388 if (!end_sequence)
18389 {
6f77053d
PA
18390 if (dwarf_record_line_p (m_line, m_last_line,
18391 m_line_has_non_zero_discriminator,
18392 m_last_subfile))
d9b3de22 18393 {
6f77053d
PA
18394 dwarf_record_line_1 (m_gdbarch, current_subfile,
18395 m_line, m_address,
18396 m_record_line_callback);
d9b3de22 18397 }
6f77053d
PA
18398 m_last_subfile = current_subfile;
18399 m_last_line = m_line;
d9b3de22
DE
18400 }
18401 }
18402 }
18403}
18404
6f77053d
PA
18405lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18406 bool record_lines_p)
d9b3de22 18407{
6f77053d
PA
18408 m_gdbarch = arch;
18409 m_record_lines_p = record_lines_p;
18410 m_line_header = lh;
d9b3de22 18411
6f77053d 18412 m_record_line_callback = ::record_line;
d9b3de22 18413
d9b3de22
DE
18414 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18415 was a line entry for it so that the backend has a chance to adjust it
18416 and also record it in case it needs it. This is currently used by MIPS
18417 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18418 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18419 m_is_stmt = lh->default_is_stmt;
18420 m_discriminator = 0;
252a6764
DE
18421}
18422
6f77053d
PA
18423void
18424lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18425 const gdb_byte *line_ptr,
18426 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18427{
18428 /* If address < lowpc then it's not a usable value, it's outside the
18429 pc range of the CU. However, we restrict the test to only address
18430 values of zero to preserve GDB's previous behaviour which is to
18431 handle the specific case of a function being GC'd by the linker. */
18432
18433 if (address == 0 && address < lowpc)
18434 {
18435 /* This line table is for a function which has been
18436 GCd by the linker. Ignore it. PR gdb/12528 */
18437
18438 struct objfile *objfile = cu->objfile;
18439 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18440
18441 complaint (&symfile_complaints,
18442 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18443 line_offset, objfile_name (objfile));
6f77053d
PA
18444 m_record_line_callback = noop_record_line;
18445 /* Note: record_line_callback is left as noop_record_line until
18446 we see DW_LNE_end_sequence. */
924c2928
DE
18447 }
18448}
18449
f3f5162e 18450/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18451 Process the line number information in LH.
18452 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18453 program in order to set included_p for every referenced header. */
debd256d 18454
c906108c 18455static void
43f3e411
DE
18456dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18457 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18458{
d521ce57
TT
18459 const gdb_byte *line_ptr, *extended_end;
18460 const gdb_byte *line_end;
a8c50c1f 18461 unsigned int bytes_read, extended_len;
699ca60a 18462 unsigned char op_code, extended_op;
e142c38c
DJ
18463 CORE_ADDR baseaddr;
18464 struct objfile *objfile = cu->objfile;
f3f5162e 18465 bfd *abfd = objfile->obfd;
fbf65064 18466 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18467 /* True if we're recording line info (as opposed to building partial
18468 symtabs and just interested in finding include files mentioned by
18469 the line number program). */
18470 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18471
18472 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18473
debd256d
JB
18474 line_ptr = lh->statement_program_start;
18475 line_end = lh->statement_program_end;
c906108c
SS
18476
18477 /* Read the statement sequences until there's nothing left. */
18478 while (line_ptr < line_end)
18479 {
6f77053d
PA
18480 /* The DWARF line number program state machine. Reset the state
18481 machine at the start of each sequence. */
18482 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18483 bool end_sequence = false;
d9b3de22 18484
8c43009f 18485 if (record_lines_p)
c906108c 18486 {
8c43009f
PA
18487 /* Start a subfile for the current file of the state
18488 machine. */
18489 const file_entry *fe = state_machine.current_file ();
18490
18491 if (fe != NULL)
18492 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18493 }
18494
a738430d 18495 /* Decode the table. */
d9b3de22 18496 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18497 {
18498 op_code = read_1_byte (abfd, line_ptr);
18499 line_ptr += 1;
9aa1fe7e 18500
debd256d 18501 if (op_code >= lh->opcode_base)
6e70227d 18502 {
8e07a239 18503 /* Special opcode. */
6f77053d 18504 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18505 }
18506 else switch (op_code)
c906108c
SS
18507 {
18508 case DW_LNS_extended_op:
3e43a32a
MS
18509 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18510 &bytes_read);
473b7be6 18511 line_ptr += bytes_read;
a8c50c1f 18512 extended_end = line_ptr + extended_len;
c906108c
SS
18513 extended_op = read_1_byte (abfd, line_ptr);
18514 line_ptr += 1;
18515 switch (extended_op)
18516 {
18517 case DW_LNE_end_sequence:
6f77053d
PA
18518 state_machine.handle_end_sequence ();
18519 end_sequence = true;
c906108c
SS
18520 break;
18521 case DW_LNE_set_address:
d9b3de22
DE
18522 {
18523 CORE_ADDR address
18524 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18525 line_ptr += bytes_read;
6f77053d
PA
18526
18527 state_machine.check_line_address (cu, line_ptr,
18528 lowpc, address);
18529 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18530 }
c906108c
SS
18531 break;
18532 case DW_LNE_define_file:
debd256d 18533 {
d521ce57 18534 const char *cur_file;
ecfb656c
PA
18535 unsigned int mod_time, length;
18536 dir_index dindex;
6e70227d 18537
3e43a32a
MS
18538 cur_file = read_direct_string (abfd, line_ptr,
18539 &bytes_read);
debd256d 18540 line_ptr += bytes_read;
ecfb656c 18541 dindex = (dir_index)
debd256d
JB
18542 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18543 line_ptr += bytes_read;
18544 mod_time =
18545 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18546 line_ptr += bytes_read;
18547 length =
18548 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18549 line_ptr += bytes_read;
ecfb656c 18550 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18551 }
c906108c 18552 break;
d0c6ba3d 18553 case DW_LNE_set_discriminator:
6f77053d
PA
18554 {
18555 /* The discriminator is not interesting to the
18556 debugger; just ignore it. We still need to
18557 check its value though:
18558 if there are consecutive entries for the same
18559 (non-prologue) line we want to coalesce them.
18560 PR 17276. */
18561 unsigned int discr
18562 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18563 line_ptr += bytes_read;
18564
18565 state_machine.handle_set_discriminator (discr);
18566 }
d0c6ba3d 18567 break;
c906108c 18568 default:
4d3c2250 18569 complaint (&symfile_complaints,
e2e0b3e5 18570 _("mangled .debug_line section"));
debd256d 18571 return;
c906108c 18572 }
a8c50c1f
DJ
18573 /* Make sure that we parsed the extended op correctly. If e.g.
18574 we expected a different address size than the producer used,
18575 we may have read the wrong number of bytes. */
18576 if (line_ptr != extended_end)
18577 {
18578 complaint (&symfile_complaints,
18579 _("mangled .debug_line section"));
18580 return;
18581 }
c906108c
SS
18582 break;
18583 case DW_LNS_copy:
6f77053d 18584 state_machine.handle_copy ();
c906108c
SS
18585 break;
18586 case DW_LNS_advance_pc:
2dc7f7b3
TT
18587 {
18588 CORE_ADDR adjust
18589 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18590 line_ptr += bytes_read;
6f77053d
PA
18591
18592 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18593 }
c906108c
SS
18594 break;
18595 case DW_LNS_advance_line:
a05a36a5
DE
18596 {
18597 int line_delta
18598 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18599 line_ptr += bytes_read;
6f77053d
PA
18600
18601 state_machine.handle_advance_line (line_delta);
a05a36a5 18602 }
c906108c
SS
18603 break;
18604 case DW_LNS_set_file:
d9b3de22 18605 {
6f77053d 18606 file_name_index file
ecfb656c
PA
18607 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18608 &bytes_read);
d9b3de22 18609 line_ptr += bytes_read;
8c43009f 18610
6f77053d 18611 state_machine.handle_set_file (file);
d9b3de22 18612 }
c906108c
SS
18613 break;
18614 case DW_LNS_set_column:
0ad93d4f 18615 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18616 line_ptr += bytes_read;
18617 break;
18618 case DW_LNS_negate_stmt:
6f77053d 18619 state_machine.handle_negate_stmt ();
c906108c
SS
18620 break;
18621 case DW_LNS_set_basic_block:
c906108c 18622 break;
c2c6d25f
JM
18623 /* Add to the address register of the state machine the
18624 address increment value corresponding to special opcode
a738430d
MK
18625 255. I.e., this value is scaled by the minimum
18626 instruction length since special opcode 255 would have
b021a221 18627 scaled the increment. */
c906108c 18628 case DW_LNS_const_add_pc:
6f77053d 18629 state_machine.handle_const_add_pc ();
c906108c
SS
18630 break;
18631 case DW_LNS_fixed_advance_pc:
3e29f34a 18632 {
6f77053d 18633 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18634 line_ptr += 2;
6f77053d
PA
18635
18636 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18637 }
c906108c 18638 break;
9aa1fe7e 18639 default:
a738430d
MK
18640 {
18641 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18642 int i;
a738430d 18643
debd256d 18644 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18645 {
18646 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18647 line_ptr += bytes_read;
18648 }
18649 }
c906108c
SS
18650 }
18651 }
d9b3de22
DE
18652
18653 if (!end_sequence)
18654 dwarf2_debug_line_missing_end_sequence_complaint ();
18655
18656 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18657 in which case we still finish recording the last line). */
6f77053d 18658 state_machine.record_line (true);
c906108c 18659 }
f3f5162e
DE
18660}
18661
18662/* Decode the Line Number Program (LNP) for the given line_header
18663 structure and CU. The actual information extracted and the type
18664 of structures created from the LNP depends on the value of PST.
18665
18666 1. If PST is NULL, then this procedure uses the data from the program
18667 to create all necessary symbol tables, and their linetables.
18668
18669 2. If PST is not NULL, this procedure reads the program to determine
18670 the list of files included by the unit represented by PST, and
18671 builds all the associated partial symbol tables.
18672
18673 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18674 It is used for relative paths in the line table.
18675 NOTE: When processing partial symtabs (pst != NULL),
18676 comp_dir == pst->dirname.
18677
18678 NOTE: It is important that psymtabs have the same file name (via strcmp)
18679 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18680 symtab we don't use it in the name of the psymtabs we create.
18681 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18682 A good testcase for this is mb-inline.exp.
18683
527f3840
JK
18684 LOWPC is the lowest address in CU (or 0 if not known).
18685
18686 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18687 for its PC<->lines mapping information. Otherwise only the filename
18688 table is read in. */
f3f5162e
DE
18689
18690static void
18691dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18692 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18693 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18694{
18695 struct objfile *objfile = cu->objfile;
18696 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18697
527f3840
JK
18698 if (decode_mapping)
18699 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18700
18701 if (decode_for_pst_p)
18702 {
18703 int file_index;
18704
18705 /* Now that we're done scanning the Line Header Program, we can
18706 create the psymtab of each included file. */
fff8551c 18707 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18708 if (lh->file_names[file_index].included_p == 1)
18709 {
d521ce57 18710 const char *include_name =
c6da4cef
DE
18711 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18712 if (include_name != NULL)
aaa75496
JB
18713 dwarf2_create_include_psymtab (include_name, pst, objfile);
18714 }
18715 }
cb1df416
DJ
18716 else
18717 {
18718 /* Make sure a symtab is created for every file, even files
18719 which contain only variables (i.e. no code with associated
18720 line numbers). */
43f3e411 18721 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18722 int i;
cb1df416 18723
fff8551c 18724 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18725 {
8c43009f 18726 file_entry &fe = lh->file_names[i];
9a619af0 18727
8c43009f 18728 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18729
cb1df416 18730 if (current_subfile->symtab == NULL)
43f3e411
DE
18731 {
18732 current_subfile->symtab
18733 = allocate_symtab (cust, current_subfile->name);
18734 }
8c43009f 18735 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18736 }
18737 }
c906108c
SS
18738}
18739
18740/* Start a subfile for DWARF. FILENAME is the name of the file and
18741 DIRNAME the name of the source directory which contains FILENAME
4d663531 18742 or NULL if not known.
c906108c
SS
18743 This routine tries to keep line numbers from identical absolute and
18744 relative file names in a common subfile.
18745
18746 Using the `list' example from the GDB testsuite, which resides in
18747 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18748 of /srcdir/list0.c yields the following debugging information for list0.c:
18749
c5aa993b 18750 DW_AT_name: /srcdir/list0.c
4d663531 18751 DW_AT_comp_dir: /compdir
357e46e7 18752 files.files[0].name: list0.h
c5aa993b 18753 files.files[0].dir: /srcdir
357e46e7 18754 files.files[1].name: list0.c
c5aa993b 18755 files.files[1].dir: /srcdir
c906108c
SS
18756
18757 The line number information for list0.c has to end up in a single
4f1520fb
FR
18758 subfile, so that `break /srcdir/list0.c:1' works as expected.
18759 start_subfile will ensure that this happens provided that we pass the
18760 concatenation of files.files[1].dir and files.files[1].name as the
18761 subfile's name. */
c906108c
SS
18762
18763static void
4d663531 18764dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18765{
d521ce57 18766 char *copy = NULL;
4f1520fb 18767
4d663531 18768 /* In order not to lose the line information directory,
4f1520fb
FR
18769 we concatenate it to the filename when it makes sense.
18770 Note that the Dwarf3 standard says (speaking of filenames in line
18771 information): ``The directory index is ignored for file names
18772 that represent full path names''. Thus ignoring dirname in the
18773 `else' branch below isn't an issue. */
c906108c 18774
d5166ae1 18775 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18776 {
18777 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18778 filename = copy;
18779 }
c906108c 18780
4d663531 18781 start_subfile (filename);
4f1520fb 18782
d521ce57
TT
18783 if (copy != NULL)
18784 xfree (copy);
c906108c
SS
18785}
18786
f4dc4d17
DE
18787/* Start a symtab for DWARF.
18788 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18789
43f3e411 18790static struct compunit_symtab *
f4dc4d17 18791dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18792 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18793{
43f3e411
DE
18794 struct compunit_symtab *cust
18795 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18796
f4dc4d17
DE
18797 record_debugformat ("DWARF 2");
18798 record_producer (cu->producer);
18799
18800 /* We assume that we're processing GCC output. */
18801 processing_gcc_compilation = 2;
18802
4d4ec4e5 18803 cu->processing_has_namespace_info = 0;
43f3e411
DE
18804
18805 return cust;
f4dc4d17
DE
18806}
18807
4c2df51b
DJ
18808static void
18809var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18810 struct dwarf2_cu *cu)
4c2df51b 18811{
e7c27a73
DJ
18812 struct objfile *objfile = cu->objfile;
18813 struct comp_unit_head *cu_header = &cu->header;
18814
4c2df51b
DJ
18815 /* NOTE drow/2003-01-30: There used to be a comment and some special
18816 code here to turn a symbol with DW_AT_external and a
18817 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18818 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18819 with some versions of binutils) where shared libraries could have
18820 relocations against symbols in their debug information - the
18821 minimal symbol would have the right address, but the debug info
18822 would not. It's no longer necessary, because we will explicitly
18823 apply relocations when we read in the debug information now. */
18824
18825 /* A DW_AT_location attribute with no contents indicates that a
18826 variable has been optimized away. */
18827 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18828 {
f1e6e072 18829 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18830 return;
18831 }
18832
18833 /* Handle one degenerate form of location expression specially, to
18834 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18835 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18836 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18837
18838 if (attr_form_is_block (attr)
3019eac3
DE
18839 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18840 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18841 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18842 && (DW_BLOCK (attr)->size
18843 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18844 {
891d2f0b 18845 unsigned int dummy;
4c2df51b 18846
3019eac3
DE
18847 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18848 SYMBOL_VALUE_ADDRESS (sym) =
18849 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18850 else
18851 SYMBOL_VALUE_ADDRESS (sym) =
18852 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18853 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18854 fixup_symbol_section (sym, objfile);
18855 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18856 SYMBOL_SECTION (sym));
4c2df51b
DJ
18857 return;
18858 }
18859
18860 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18861 expression evaluator, and use LOC_COMPUTED only when necessary
18862 (i.e. when the value of a register or memory location is
18863 referenced, or a thread-local block, etc.). Then again, it might
18864 not be worthwhile. I'm assuming that it isn't unless performance
18865 or memory numbers show me otherwise. */
18866
f1e6e072 18867 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18868
f1e6e072 18869 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18870 cu->has_loclist = 1;
4c2df51b
DJ
18871}
18872
c906108c
SS
18873/* Given a pointer to a DWARF information entry, figure out if we need
18874 to make a symbol table entry for it, and if so, create a new entry
18875 and return a pointer to it.
18876 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18877 used the passed type.
18878 If SPACE is not NULL, use it to hold the new symbol. If it is
18879 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18880
18881static struct symbol *
34eaf542
TT
18882new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18883 struct symbol *space)
c906108c 18884{
e7c27a73 18885 struct objfile *objfile = cu->objfile;
3e29f34a 18886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18887 struct symbol *sym = NULL;
15d034d0 18888 const char *name;
c906108c
SS
18889 struct attribute *attr = NULL;
18890 struct attribute *attr2 = NULL;
e142c38c 18891 CORE_ADDR baseaddr;
e37fd15a
SW
18892 struct pending **list_to_add = NULL;
18893
edb3359d 18894 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18895
18896 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18897
94af9270 18898 name = dwarf2_name (die, cu);
c906108c
SS
18899 if (name)
18900 {
94af9270 18901 const char *linkagename;
34eaf542 18902 int suppress_add = 0;
94af9270 18903
34eaf542
TT
18904 if (space)
18905 sym = space;
18906 else
e623cf5d 18907 sym = allocate_symbol (objfile);
c906108c 18908 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18909
18910 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18911 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18912 linkagename = dwarf2_physname (name, die, cu);
18913 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18914
f55ee35c
JK
18915 /* Fortran does not have mangling standard and the mangling does differ
18916 between gfortran, iFort etc. */
18917 if (cu->language == language_fortran
b250c185 18918 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18919 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18920 dwarf2_full_name (name, die, cu),
29df156d 18921 NULL);
f55ee35c 18922
c906108c 18923 /* Default assumptions.
c5aa993b 18924 Use the passed type or decode it from the die. */
176620f1 18925 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18926 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18927 if (type != NULL)
18928 SYMBOL_TYPE (sym) = type;
18929 else
e7c27a73 18930 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18931 attr = dwarf2_attr (die,
18932 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18933 cu);
c906108c
SS
18934 if (attr)
18935 {
18936 SYMBOL_LINE (sym) = DW_UNSND (attr);
18937 }
cb1df416 18938
edb3359d
DJ
18939 attr = dwarf2_attr (die,
18940 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18941 cu);
cb1df416
DJ
18942 if (attr)
18943 {
ecfb656c 18944 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 18945 struct file_entry *fe;
9a619af0 18946
ecfb656c
PA
18947 if (cu->line_header != NULL)
18948 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
18949 else
18950 fe = NULL;
18951
18952 if (fe == NULL)
cb1df416
DJ
18953 complaint (&symfile_complaints,
18954 _("file index out of range"));
8c43009f
PA
18955 else
18956 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18957 }
18958
c906108c
SS
18959 switch (die->tag)
18960 {
18961 case DW_TAG_label:
e142c38c 18962 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18963 if (attr)
3e29f34a
MR
18964 {
18965 CORE_ADDR addr;
18966
18967 addr = attr_value_as_address (attr);
18968 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18969 SYMBOL_VALUE_ADDRESS (sym) = addr;
18970 }
0f5238ed
TT
18971 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18972 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18973 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18974 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18975 break;
18976 case DW_TAG_subprogram:
18977 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18978 finish_block. */
f1e6e072 18979 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18981 if ((attr2 && (DW_UNSND (attr2) != 0))
18982 || cu->language == language_ada)
c906108c 18983 {
2cfa0c8d
JB
18984 /* Subprograms marked external are stored as a global symbol.
18985 Ada subprograms, whether marked external or not, are always
18986 stored as a global symbol, because we want to be able to
18987 access them globally. For instance, we want to be able
18988 to break on a nested subprogram without having to
18989 specify the context. */
e37fd15a 18990 list_to_add = &global_symbols;
c906108c
SS
18991 }
18992 else
18993 {
e37fd15a 18994 list_to_add = cu->list_in_scope;
c906108c
SS
18995 }
18996 break;
edb3359d
DJ
18997 case DW_TAG_inlined_subroutine:
18998 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18999 finish_block. */
f1e6e072 19000 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19001 SYMBOL_INLINED (sym) = 1;
481860b3 19002 list_to_add = cu->list_in_scope;
edb3359d 19003 break;
34eaf542
TT
19004 case DW_TAG_template_value_param:
19005 suppress_add = 1;
19006 /* Fall through. */
72929c62 19007 case DW_TAG_constant:
c906108c 19008 case DW_TAG_variable:
254e6b9e 19009 case DW_TAG_member:
0963b4bd
MS
19010 /* Compilation with minimal debug info may result in
19011 variables with missing type entries. Change the
19012 misleading `void' type to something sensible. */
c906108c 19013 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 19014 SYMBOL_TYPE (sym)
46bf5051 19015 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 19016
e142c38c 19017 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19018 /* In the case of DW_TAG_member, we should only be called for
19019 static const members. */
19020 if (die->tag == DW_TAG_member)
19021 {
3863f96c
DE
19022 /* dwarf2_add_field uses die_is_declaration,
19023 so we do the same. */
254e6b9e
DE
19024 gdb_assert (die_is_declaration (die, cu));
19025 gdb_assert (attr);
19026 }
c906108c
SS
19027 if (attr)
19028 {
e7c27a73 19029 dwarf2_const_value (attr, sym, cu);
e142c38c 19030 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19031 if (!suppress_add)
34eaf542
TT
19032 {
19033 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19034 list_to_add = &global_symbols;
34eaf542 19035 else
e37fd15a 19036 list_to_add = cu->list_in_scope;
34eaf542 19037 }
c906108c
SS
19038 break;
19039 }
e142c38c 19040 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19041 if (attr)
19042 {
e7c27a73 19043 var_decode_location (attr, sym, cu);
e142c38c 19044 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19045
19046 /* Fortran explicitly imports any global symbols to the local
19047 scope by DW_TAG_common_block. */
19048 if (cu->language == language_fortran && die->parent
19049 && die->parent->tag == DW_TAG_common_block)
19050 attr2 = NULL;
19051
caac4577
JG
19052 if (SYMBOL_CLASS (sym) == LOC_STATIC
19053 && SYMBOL_VALUE_ADDRESS (sym) == 0
19054 && !dwarf2_per_objfile->has_section_at_zero)
19055 {
19056 /* When a static variable is eliminated by the linker,
19057 the corresponding debug information is not stripped
19058 out, but the variable address is set to null;
19059 do not add such variables into symbol table. */
19060 }
19061 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19062 {
f55ee35c
JK
19063 /* Workaround gfortran PR debug/40040 - it uses
19064 DW_AT_location for variables in -fPIC libraries which may
19065 get overriden by other libraries/executable and get
19066 a different address. Resolve it by the minimal symbol
19067 which may come from inferior's executable using copy
19068 relocation. Make this workaround only for gfortran as for
19069 other compilers GDB cannot guess the minimal symbol
19070 Fortran mangling kind. */
19071 if (cu->language == language_fortran && die->parent
19072 && die->parent->tag == DW_TAG_module
19073 && cu->producer
28586665 19074 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19075 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19076
1c809c68
TT
19077 /* A variable with DW_AT_external is never static,
19078 but it may be block-scoped. */
19079 list_to_add = (cu->list_in_scope == &file_symbols
19080 ? &global_symbols : cu->list_in_scope);
1c809c68 19081 }
c906108c 19082 else
e37fd15a 19083 list_to_add = cu->list_in_scope;
c906108c
SS
19084 }
19085 else
19086 {
19087 /* We do not know the address of this symbol.
c5aa993b
JM
19088 If it is an external symbol and we have type information
19089 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19090 The address of the variable will then be determined from
19091 the minimal symbol table whenever the variable is
19092 referenced. */
e142c38c 19093 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19094
19095 /* Fortran explicitly imports any global symbols to the local
19096 scope by DW_TAG_common_block. */
19097 if (cu->language == language_fortran && die->parent
19098 && die->parent->tag == DW_TAG_common_block)
19099 {
19100 /* SYMBOL_CLASS doesn't matter here because
19101 read_common_block is going to reset it. */
19102 if (!suppress_add)
19103 list_to_add = cu->list_in_scope;
19104 }
19105 else if (attr2 && (DW_UNSND (attr2) != 0)
19106 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19107 {
0fe7935b
DJ
19108 /* A variable with DW_AT_external is never static, but it
19109 may be block-scoped. */
19110 list_to_add = (cu->list_in_scope == &file_symbols
19111 ? &global_symbols : cu->list_in_scope);
19112
f1e6e072 19113 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19114 }
442ddf59
JK
19115 else if (!die_is_declaration (die, cu))
19116 {
19117 /* Use the default LOC_OPTIMIZED_OUT class. */
19118 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19119 if (!suppress_add)
19120 list_to_add = cu->list_in_scope;
442ddf59 19121 }
c906108c
SS
19122 }
19123 break;
19124 case DW_TAG_formal_parameter:
edb3359d
DJ
19125 /* If we are inside a function, mark this as an argument. If
19126 not, we might be looking at an argument to an inlined function
19127 when we do not have enough information to show inlined frames;
19128 pretend it's a local variable in that case so that the user can
19129 still see it. */
19130 if (context_stack_depth > 0
19131 && context_stack[context_stack_depth - 1].name != NULL)
19132 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19133 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19134 if (attr)
19135 {
e7c27a73 19136 var_decode_location (attr, sym, cu);
c906108c 19137 }
e142c38c 19138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19139 if (attr)
19140 {
e7c27a73 19141 dwarf2_const_value (attr, sym, cu);
c906108c 19142 }
f346a30d 19143
e37fd15a 19144 list_to_add = cu->list_in_scope;
c906108c
SS
19145 break;
19146 case DW_TAG_unspecified_parameters:
19147 /* From varargs functions; gdb doesn't seem to have any
19148 interest in this information, so just ignore it for now.
19149 (FIXME?) */
19150 break;
34eaf542
TT
19151 case DW_TAG_template_type_param:
19152 suppress_add = 1;
19153 /* Fall through. */
c906108c 19154 case DW_TAG_class_type:
680b30c7 19155 case DW_TAG_interface_type:
c906108c
SS
19156 case DW_TAG_structure_type:
19157 case DW_TAG_union_type:
72019c9c 19158 case DW_TAG_set_type:
c906108c 19159 case DW_TAG_enumeration_type:
f1e6e072 19160 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19161 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19162
63d06c5c 19163 {
9c37b5ae 19164 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19165 really ever be static objects: otherwise, if you try
19166 to, say, break of a class's method and you're in a file
19167 which doesn't mention that class, it won't work unless
19168 the check for all static symbols in lookup_symbol_aux
19169 saves you. See the OtherFileClass tests in
19170 gdb.c++/namespace.exp. */
19171
e37fd15a 19172 if (!suppress_add)
34eaf542 19173 {
34eaf542 19174 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19175 && cu->language == language_cplus
34eaf542 19176 ? &global_symbols : cu->list_in_scope);
63d06c5c 19177
64382290 19178 /* The semantics of C++ state that "struct foo {
9c37b5ae 19179 ... }" also defines a typedef for "foo". */
64382290 19180 if (cu->language == language_cplus
45280282 19181 || cu->language == language_ada
c44af4eb
TT
19182 || cu->language == language_d
19183 || cu->language == language_rust)
64382290
TT
19184 {
19185 /* The symbol's name is already allocated along
19186 with this objfile, so we don't need to
19187 duplicate it for the type. */
19188 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19189 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19190 }
63d06c5c
DC
19191 }
19192 }
c906108c
SS
19193 break;
19194 case DW_TAG_typedef:
f1e6e072 19195 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19196 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19197 list_to_add = cu->list_in_scope;
63d06c5c 19198 break;
c906108c 19199 case DW_TAG_base_type:
a02abb62 19200 case DW_TAG_subrange_type:
f1e6e072 19201 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19202 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19203 list_to_add = cu->list_in_scope;
c906108c
SS
19204 break;
19205 case DW_TAG_enumerator:
e142c38c 19206 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19207 if (attr)
19208 {
e7c27a73 19209 dwarf2_const_value (attr, sym, cu);
c906108c 19210 }
63d06c5c
DC
19211 {
19212 /* NOTE: carlton/2003-11-10: See comment above in the
19213 DW_TAG_class_type, etc. block. */
19214
e142c38c 19215 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19216 && cu->language == language_cplus
e142c38c 19217 ? &global_symbols : cu->list_in_scope);
63d06c5c 19218 }
c906108c 19219 break;
74921315 19220 case DW_TAG_imported_declaration:
5c4e30ca 19221 case DW_TAG_namespace:
f1e6e072 19222 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19223 list_to_add = &global_symbols;
5c4e30ca 19224 break;
530e8392
KB
19225 case DW_TAG_module:
19226 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19227 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19228 list_to_add = &global_symbols;
19229 break;
4357ac6c 19230 case DW_TAG_common_block:
f1e6e072 19231 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19232 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19233 add_symbol_to_list (sym, cu->list_in_scope);
19234 break;
c906108c
SS
19235 default:
19236 /* Not a tag we recognize. Hopefully we aren't processing
19237 trash data, but since we must specifically ignore things
19238 we don't recognize, there is nothing else we should do at
0963b4bd 19239 this point. */
e2e0b3e5 19240 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19241 dwarf_tag_name (die->tag));
c906108c
SS
19242 break;
19243 }
df8a16a1 19244
e37fd15a
SW
19245 if (suppress_add)
19246 {
19247 sym->hash_next = objfile->template_symbols;
19248 objfile->template_symbols = sym;
19249 list_to_add = NULL;
19250 }
19251
19252 if (list_to_add != NULL)
19253 add_symbol_to_list (sym, list_to_add);
19254
df8a16a1
DJ
19255 /* For the benefit of old versions of GCC, check for anonymous
19256 namespaces based on the demangled name. */
4d4ec4e5 19257 if (!cu->processing_has_namespace_info
94af9270 19258 && cu->language == language_cplus)
a10964d1 19259 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19260 }
19261 return (sym);
19262}
19263
34eaf542
TT
19264/* A wrapper for new_symbol_full that always allocates a new symbol. */
19265
19266static struct symbol *
19267new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19268{
19269 return new_symbol_full (die, type, cu, NULL);
19270}
19271
98bfdba5
PA
19272/* Given an attr with a DW_FORM_dataN value in host byte order,
19273 zero-extend it as appropriate for the symbol's type. The DWARF
19274 standard (v4) is not entirely clear about the meaning of using
19275 DW_FORM_dataN for a constant with a signed type, where the type is
19276 wider than the data. The conclusion of a discussion on the DWARF
19277 list was that this is unspecified. We choose to always zero-extend
19278 because that is the interpretation long in use by GCC. */
c906108c 19279
98bfdba5 19280static gdb_byte *
ff39bb5e 19281dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19282 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19283{
e7c27a73 19284 struct objfile *objfile = cu->objfile;
e17a4113
UW
19285 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19286 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19287 LONGEST l = DW_UNSND (attr);
19288
19289 if (bits < sizeof (*value) * 8)
19290 {
19291 l &= ((LONGEST) 1 << bits) - 1;
19292 *value = l;
19293 }
19294 else if (bits == sizeof (*value) * 8)
19295 *value = l;
19296 else
19297 {
224c3ddb 19298 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19299 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19300 return bytes;
19301 }
19302
19303 return NULL;
19304}
19305
19306/* Read a constant value from an attribute. Either set *VALUE, or if
19307 the value does not fit in *VALUE, set *BYTES - either already
19308 allocated on the objfile obstack, or newly allocated on OBSTACK,
19309 or, set *BATON, if we translated the constant to a location
19310 expression. */
19311
19312static void
ff39bb5e 19313dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19314 const char *name, struct obstack *obstack,
19315 struct dwarf2_cu *cu,
d521ce57 19316 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19317 struct dwarf2_locexpr_baton **baton)
19318{
19319 struct objfile *objfile = cu->objfile;
19320 struct comp_unit_head *cu_header = &cu->header;
c906108c 19321 struct dwarf_block *blk;
98bfdba5
PA
19322 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19323 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19324
19325 *value = 0;
19326 *bytes = NULL;
19327 *baton = NULL;
c906108c
SS
19328
19329 switch (attr->form)
19330 {
19331 case DW_FORM_addr:
3019eac3 19332 case DW_FORM_GNU_addr_index:
ac56253d 19333 {
ac56253d
TT
19334 gdb_byte *data;
19335
98bfdba5
PA
19336 if (TYPE_LENGTH (type) != cu_header->addr_size)
19337 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19338 cu_header->addr_size,
98bfdba5 19339 TYPE_LENGTH (type));
ac56253d
TT
19340 /* Symbols of this form are reasonably rare, so we just
19341 piggyback on the existing location code rather than writing
19342 a new implementation of symbol_computed_ops. */
8d749320 19343 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19344 (*baton)->per_cu = cu->per_cu;
19345 gdb_assert ((*baton)->per_cu);
ac56253d 19346
98bfdba5 19347 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19348 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19349 (*baton)->data = data;
ac56253d
TT
19350
19351 data[0] = DW_OP_addr;
19352 store_unsigned_integer (&data[1], cu_header->addr_size,
19353 byte_order, DW_ADDR (attr));
19354 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19355 }
c906108c 19356 break;
4ac36638 19357 case DW_FORM_string:
93b5768b 19358 case DW_FORM_strp:
3019eac3 19359 case DW_FORM_GNU_str_index:
36586728 19360 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19361 /* DW_STRING is already allocated on the objfile obstack, point
19362 directly to it. */
d521ce57 19363 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19364 break;
c906108c
SS
19365 case DW_FORM_block1:
19366 case DW_FORM_block2:
19367 case DW_FORM_block4:
19368 case DW_FORM_block:
2dc7f7b3 19369 case DW_FORM_exprloc:
0224619f 19370 case DW_FORM_data16:
c906108c 19371 blk = DW_BLOCK (attr);
98bfdba5
PA
19372 if (TYPE_LENGTH (type) != blk->size)
19373 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19374 TYPE_LENGTH (type));
19375 *bytes = blk->data;
c906108c 19376 break;
2df3850c
JM
19377
19378 /* The DW_AT_const_value attributes are supposed to carry the
19379 symbol's value "represented as it would be on the target
19380 architecture." By the time we get here, it's already been
19381 converted to host endianness, so we just need to sign- or
19382 zero-extend it as appropriate. */
19383 case DW_FORM_data1:
3aef2284 19384 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19385 break;
c906108c 19386 case DW_FORM_data2:
3aef2284 19387 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19388 break;
c906108c 19389 case DW_FORM_data4:
3aef2284 19390 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19391 break;
c906108c 19392 case DW_FORM_data8:
3aef2284 19393 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19394 break;
19395
c906108c 19396 case DW_FORM_sdata:
98bfdba5 19397 *value = DW_SND (attr);
2df3850c
JM
19398 break;
19399
c906108c 19400 case DW_FORM_udata:
98bfdba5 19401 *value = DW_UNSND (attr);
c906108c 19402 break;
2df3850c 19403
c906108c 19404 default:
4d3c2250 19405 complaint (&symfile_complaints,
e2e0b3e5 19406 _("unsupported const value attribute form: '%s'"),
4d3c2250 19407 dwarf_form_name (attr->form));
98bfdba5 19408 *value = 0;
c906108c
SS
19409 break;
19410 }
19411}
19412
2df3850c 19413
98bfdba5
PA
19414/* Copy constant value from an attribute to a symbol. */
19415
2df3850c 19416static void
ff39bb5e 19417dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19418 struct dwarf2_cu *cu)
2df3850c 19419{
98bfdba5 19420 struct objfile *objfile = cu->objfile;
12df843f 19421 LONGEST value;
d521ce57 19422 const gdb_byte *bytes;
98bfdba5 19423 struct dwarf2_locexpr_baton *baton;
2df3850c 19424
98bfdba5
PA
19425 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19426 SYMBOL_PRINT_NAME (sym),
19427 &objfile->objfile_obstack, cu,
19428 &value, &bytes, &baton);
2df3850c 19429
98bfdba5
PA
19430 if (baton != NULL)
19431 {
98bfdba5 19432 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19433 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19434 }
19435 else if (bytes != NULL)
19436 {
19437 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19438 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19439 }
19440 else
19441 {
19442 SYMBOL_VALUE (sym) = value;
f1e6e072 19443 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19444 }
2df3850c
JM
19445}
19446
c906108c
SS
19447/* Return the type of the die in question using its DW_AT_type attribute. */
19448
19449static struct type *
e7c27a73 19450die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19451{
c906108c 19452 struct attribute *type_attr;
c906108c 19453
e142c38c 19454 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19455 if (!type_attr)
19456 {
19457 /* A missing DW_AT_type represents a void type. */
46bf5051 19458 return objfile_type (cu->objfile)->builtin_void;
c906108c 19459 }
348e048f 19460
673bfd45 19461 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19462}
19463
b4ba55a1
JB
19464/* True iff CU's producer generates GNAT Ada auxiliary information
19465 that allows to find parallel types through that information instead
19466 of having to do expensive parallel lookups by type name. */
19467
19468static int
19469need_gnat_info (struct dwarf2_cu *cu)
19470{
19471 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19472 of GNAT produces this auxiliary information, without any indication
19473 that it is produced. Part of enhancing the FSF version of GNAT
19474 to produce that information will be to put in place an indicator
19475 that we can use in order to determine whether the descriptive type
19476 info is available or not. One suggestion that has been made is
19477 to use a new attribute, attached to the CU die. For now, assume
19478 that the descriptive type info is not available. */
19479 return 0;
19480}
19481
b4ba55a1
JB
19482/* Return the auxiliary type of the die in question using its
19483 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19484 attribute is not present. */
19485
19486static struct type *
19487die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19488{
b4ba55a1 19489 struct attribute *type_attr;
b4ba55a1
JB
19490
19491 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19492 if (!type_attr)
19493 return NULL;
19494
673bfd45 19495 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19496}
19497
19498/* If DIE has a descriptive_type attribute, then set the TYPE's
19499 descriptive type accordingly. */
19500
19501static void
19502set_descriptive_type (struct type *type, struct die_info *die,
19503 struct dwarf2_cu *cu)
19504{
19505 struct type *descriptive_type = die_descriptive_type (die, cu);
19506
19507 if (descriptive_type)
19508 {
19509 ALLOCATE_GNAT_AUX_TYPE (type);
19510 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19511 }
19512}
19513
c906108c
SS
19514/* Return the containing type of the die in question using its
19515 DW_AT_containing_type attribute. */
19516
19517static struct type *
e7c27a73 19518die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19519{
c906108c 19520 struct attribute *type_attr;
c906108c 19521
e142c38c 19522 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19523 if (!type_attr)
19524 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19525 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19526
673bfd45 19527 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19528}
19529
ac9ec31b
DE
19530/* Return an error marker type to use for the ill formed type in DIE/CU. */
19531
19532static struct type *
19533build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19534{
19535 struct objfile *objfile = dwarf2_per_objfile->objfile;
19536 char *message, *saved;
19537
19538 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19539 objfile_name (objfile),
9c541725
PA
19540 to_underlying (cu->header.sect_off),
19541 to_underlying (die->sect_off));
224c3ddb
SM
19542 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19543 message, strlen (message));
ac9ec31b
DE
19544 xfree (message);
19545
19f392bc 19546 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19547}
19548
673bfd45 19549/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19550 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19551 DW_AT_containing_type.
673bfd45
DE
19552 If there is no type substitute an error marker. */
19553
c906108c 19554static struct type *
ff39bb5e 19555lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19556 struct dwarf2_cu *cu)
c906108c 19557{
bb5ed363 19558 struct objfile *objfile = cu->objfile;
f792889a
DJ
19559 struct type *this_type;
19560
ac9ec31b
DE
19561 gdb_assert (attr->name == DW_AT_type
19562 || attr->name == DW_AT_GNAT_descriptive_type
19563 || attr->name == DW_AT_containing_type);
19564
673bfd45
DE
19565 /* First see if we have it cached. */
19566
36586728
TT
19567 if (attr->form == DW_FORM_GNU_ref_alt)
19568 {
19569 struct dwarf2_per_cu_data *per_cu;
9c541725 19570 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19571
9c541725
PA
19572 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19573 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19574 }
7771576e 19575 else if (attr_form_is_ref (attr))
673bfd45 19576 {
9c541725 19577 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19578
9c541725 19579 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19580 }
55f1336d 19581 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19582 {
ac9ec31b 19583 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19584
ac9ec31b 19585 return get_signatured_type (die, signature, cu);
673bfd45
DE
19586 }
19587 else
19588 {
ac9ec31b
DE
19589 complaint (&symfile_complaints,
19590 _("Dwarf Error: Bad type attribute %s in DIE"
19591 " at 0x%x [in module %s]"),
9c541725 19592 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19593 objfile_name (objfile));
ac9ec31b 19594 return build_error_marker_type (cu, die);
673bfd45
DE
19595 }
19596
19597 /* If not cached we need to read it in. */
19598
19599 if (this_type == NULL)
19600 {
ac9ec31b 19601 struct die_info *type_die = NULL;
673bfd45
DE
19602 struct dwarf2_cu *type_cu = cu;
19603
7771576e 19604 if (attr_form_is_ref (attr))
ac9ec31b
DE
19605 type_die = follow_die_ref (die, attr, &type_cu);
19606 if (type_die == NULL)
19607 return build_error_marker_type (cu, die);
19608 /* If we find the type now, it's probably because the type came
3019eac3
DE
19609 from an inter-CU reference and the type's CU got expanded before
19610 ours. */
ac9ec31b 19611 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19612 }
19613
19614 /* If we still don't have a type use an error marker. */
19615
19616 if (this_type == NULL)
ac9ec31b 19617 return build_error_marker_type (cu, die);
673bfd45 19618
f792889a 19619 return this_type;
c906108c
SS
19620}
19621
673bfd45
DE
19622/* Return the type in DIE, CU.
19623 Returns NULL for invalid types.
19624
02142a6c 19625 This first does a lookup in die_type_hash,
673bfd45
DE
19626 and only reads the die in if necessary.
19627
19628 NOTE: This can be called when reading in partial or full symbols. */
19629
f792889a 19630static struct type *
e7c27a73 19631read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19632{
f792889a
DJ
19633 struct type *this_type;
19634
19635 this_type = get_die_type (die, cu);
19636 if (this_type)
19637 return this_type;
19638
673bfd45
DE
19639 return read_type_die_1 (die, cu);
19640}
19641
19642/* Read the type in DIE, CU.
19643 Returns NULL for invalid types. */
19644
19645static struct type *
19646read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19647{
19648 struct type *this_type = NULL;
19649
c906108c
SS
19650 switch (die->tag)
19651 {
19652 case DW_TAG_class_type:
680b30c7 19653 case DW_TAG_interface_type:
c906108c
SS
19654 case DW_TAG_structure_type:
19655 case DW_TAG_union_type:
f792889a 19656 this_type = read_structure_type (die, cu);
c906108c
SS
19657 break;
19658 case DW_TAG_enumeration_type:
f792889a 19659 this_type = read_enumeration_type (die, cu);
c906108c
SS
19660 break;
19661 case DW_TAG_subprogram:
19662 case DW_TAG_subroutine_type:
edb3359d 19663 case DW_TAG_inlined_subroutine:
f792889a 19664 this_type = read_subroutine_type (die, cu);
c906108c
SS
19665 break;
19666 case DW_TAG_array_type:
f792889a 19667 this_type = read_array_type (die, cu);
c906108c 19668 break;
72019c9c 19669 case DW_TAG_set_type:
f792889a 19670 this_type = read_set_type (die, cu);
72019c9c 19671 break;
c906108c 19672 case DW_TAG_pointer_type:
f792889a 19673 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19674 break;
19675 case DW_TAG_ptr_to_member_type:
f792889a 19676 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19677 break;
19678 case DW_TAG_reference_type:
4297a3f0
AV
19679 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19680 break;
19681 case DW_TAG_rvalue_reference_type:
19682 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19683 break;
19684 case DW_TAG_const_type:
f792889a 19685 this_type = read_tag_const_type (die, cu);
c906108c
SS
19686 break;
19687 case DW_TAG_volatile_type:
f792889a 19688 this_type = read_tag_volatile_type (die, cu);
c906108c 19689 break;
06d66ee9
TT
19690 case DW_TAG_restrict_type:
19691 this_type = read_tag_restrict_type (die, cu);
19692 break;
c906108c 19693 case DW_TAG_string_type:
f792889a 19694 this_type = read_tag_string_type (die, cu);
c906108c
SS
19695 break;
19696 case DW_TAG_typedef:
f792889a 19697 this_type = read_typedef (die, cu);
c906108c 19698 break;
a02abb62 19699 case DW_TAG_subrange_type:
f792889a 19700 this_type = read_subrange_type (die, cu);
a02abb62 19701 break;
c906108c 19702 case DW_TAG_base_type:
f792889a 19703 this_type = read_base_type (die, cu);
c906108c 19704 break;
81a17f79 19705 case DW_TAG_unspecified_type:
f792889a 19706 this_type = read_unspecified_type (die, cu);
81a17f79 19707 break;
0114d602
DJ
19708 case DW_TAG_namespace:
19709 this_type = read_namespace_type (die, cu);
19710 break;
f55ee35c
JK
19711 case DW_TAG_module:
19712 this_type = read_module_type (die, cu);
19713 break;
a2c2acaf
MW
19714 case DW_TAG_atomic_type:
19715 this_type = read_tag_atomic_type (die, cu);
19716 break;
c906108c 19717 default:
3e43a32a
MS
19718 complaint (&symfile_complaints,
19719 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19720 dwarf_tag_name (die->tag));
c906108c
SS
19721 break;
19722 }
63d06c5c 19723
f792889a 19724 return this_type;
63d06c5c
DC
19725}
19726
abc72ce4
DE
19727/* See if we can figure out if the class lives in a namespace. We do
19728 this by looking for a member function; its demangled name will
19729 contain namespace info, if there is any.
19730 Return the computed name or NULL.
19731 Space for the result is allocated on the objfile's obstack.
19732 This is the full-die version of guess_partial_die_structure_name.
19733 In this case we know DIE has no useful parent. */
19734
19735static char *
19736guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19737{
19738 struct die_info *spec_die;
19739 struct dwarf2_cu *spec_cu;
19740 struct die_info *child;
19741
19742 spec_cu = cu;
19743 spec_die = die_specification (die, &spec_cu);
19744 if (spec_die != NULL)
19745 {
19746 die = spec_die;
19747 cu = spec_cu;
19748 }
19749
19750 for (child = die->child;
19751 child != NULL;
19752 child = child->sibling)
19753 {
19754 if (child->tag == DW_TAG_subprogram)
19755 {
7d45c7c3 19756 const char *linkage_name;
abc72ce4 19757
7d45c7c3
KB
19758 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19759 if (linkage_name == NULL)
19760 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19761 cu);
19762 if (linkage_name != NULL)
abc72ce4
DE
19763 {
19764 char *actual_name
19765 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19766 linkage_name);
abc72ce4
DE
19767 char *name = NULL;
19768
19769 if (actual_name != NULL)
19770 {
15d034d0 19771 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19772
19773 if (die_name != NULL
19774 && strcmp (die_name, actual_name) != 0)
19775 {
19776 /* Strip off the class name from the full name.
19777 We want the prefix. */
19778 int die_name_len = strlen (die_name);
19779 int actual_name_len = strlen (actual_name);
19780
19781 /* Test for '::' as a sanity check. */
19782 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19783 && actual_name[actual_name_len
19784 - die_name_len - 1] == ':')
224c3ddb
SM
19785 name = (char *) obstack_copy0 (
19786 &cu->objfile->per_bfd->storage_obstack,
19787 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19788 }
19789 }
19790 xfree (actual_name);
19791 return name;
19792 }
19793 }
19794 }
19795
19796 return NULL;
19797}
19798
96408a79
SA
19799/* GCC might emit a nameless typedef that has a linkage name. Determine the
19800 prefix part in such case. See
19801 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19802
19803static char *
19804anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19805{
19806 struct attribute *attr;
e6a959d6 19807 const char *base;
96408a79
SA
19808
19809 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19810 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19811 return NULL;
19812
7d45c7c3 19813 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19814 return NULL;
19815
19816 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19817 if (attr == NULL)
19818 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19819 if (attr == NULL || DW_STRING (attr) == NULL)
19820 return NULL;
19821
19822 /* dwarf2_name had to be already called. */
19823 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19824
19825 /* Strip the base name, keep any leading namespaces/classes. */
19826 base = strrchr (DW_STRING (attr), ':');
19827 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19828 return "";
19829
224c3ddb
SM
19830 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19831 DW_STRING (attr),
19832 &base[-1] - DW_STRING (attr));
96408a79
SA
19833}
19834
fdde2d81 19835/* Return the name of the namespace/class that DIE is defined within,
0114d602 19836 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19837
0114d602
DJ
19838 For example, if we're within the method foo() in the following
19839 code:
19840
19841 namespace N {
19842 class C {
19843 void foo () {
19844 }
19845 };
19846 }
19847
19848 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19849
0d5cff50 19850static const char *
e142c38c 19851determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19852{
0114d602
DJ
19853 struct die_info *parent, *spec_die;
19854 struct dwarf2_cu *spec_cu;
19855 struct type *parent_type;
96408a79 19856 char *retval;
63d06c5c 19857
9c37b5ae 19858 if (cu->language != language_cplus
c44af4eb
TT
19859 && cu->language != language_fortran && cu->language != language_d
19860 && cu->language != language_rust)
0114d602
DJ
19861 return "";
19862
96408a79
SA
19863 retval = anonymous_struct_prefix (die, cu);
19864 if (retval)
19865 return retval;
19866
0114d602
DJ
19867 /* We have to be careful in the presence of DW_AT_specification.
19868 For example, with GCC 3.4, given the code
19869
19870 namespace N {
19871 void foo() {
19872 // Definition of N::foo.
19873 }
19874 }
19875
19876 then we'll have a tree of DIEs like this:
19877
19878 1: DW_TAG_compile_unit
19879 2: DW_TAG_namespace // N
19880 3: DW_TAG_subprogram // declaration of N::foo
19881 4: DW_TAG_subprogram // definition of N::foo
19882 DW_AT_specification // refers to die #3
19883
19884 Thus, when processing die #4, we have to pretend that we're in
19885 the context of its DW_AT_specification, namely the contex of die
19886 #3. */
19887 spec_cu = cu;
19888 spec_die = die_specification (die, &spec_cu);
19889 if (spec_die == NULL)
19890 parent = die->parent;
19891 else
63d06c5c 19892 {
0114d602
DJ
19893 parent = spec_die->parent;
19894 cu = spec_cu;
63d06c5c 19895 }
0114d602
DJ
19896
19897 if (parent == NULL)
19898 return "";
98bfdba5
PA
19899 else if (parent->building_fullname)
19900 {
19901 const char *name;
19902 const char *parent_name;
19903
19904 /* It has been seen on RealView 2.2 built binaries,
19905 DW_TAG_template_type_param types actually _defined_ as
19906 children of the parent class:
19907
19908 enum E {};
19909 template class <class Enum> Class{};
19910 Class<enum E> class_e;
19911
19912 1: DW_TAG_class_type (Class)
19913 2: DW_TAG_enumeration_type (E)
19914 3: DW_TAG_enumerator (enum1:0)
19915 3: DW_TAG_enumerator (enum2:1)
19916 ...
19917 2: DW_TAG_template_type_param
19918 DW_AT_type DW_FORM_ref_udata (E)
19919
19920 Besides being broken debug info, it can put GDB into an
19921 infinite loop. Consider:
19922
19923 When we're building the full name for Class<E>, we'll start
19924 at Class, and go look over its template type parameters,
19925 finding E. We'll then try to build the full name of E, and
19926 reach here. We're now trying to build the full name of E,
19927 and look over the parent DIE for containing scope. In the
19928 broken case, if we followed the parent DIE of E, we'd again
19929 find Class, and once again go look at its template type
19930 arguments, etc., etc. Simply don't consider such parent die
19931 as source-level parent of this die (it can't be, the language
19932 doesn't allow it), and break the loop here. */
19933 name = dwarf2_name (die, cu);
19934 parent_name = dwarf2_name (parent, cu);
19935 complaint (&symfile_complaints,
19936 _("template param type '%s' defined within parent '%s'"),
19937 name ? name : "<unknown>",
19938 parent_name ? parent_name : "<unknown>");
19939 return "";
19940 }
63d06c5c 19941 else
0114d602
DJ
19942 switch (parent->tag)
19943 {
63d06c5c 19944 case DW_TAG_namespace:
0114d602 19945 parent_type = read_type_die (parent, cu);
acebe513
UW
19946 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19947 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19948 Work around this problem here. */
19949 if (cu->language == language_cplus
19950 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19951 return "";
0114d602
DJ
19952 /* We give a name to even anonymous namespaces. */
19953 return TYPE_TAG_NAME (parent_type);
63d06c5c 19954 case DW_TAG_class_type:
680b30c7 19955 case DW_TAG_interface_type:
63d06c5c 19956 case DW_TAG_structure_type:
0114d602 19957 case DW_TAG_union_type:
f55ee35c 19958 case DW_TAG_module:
0114d602
DJ
19959 parent_type = read_type_die (parent, cu);
19960 if (TYPE_TAG_NAME (parent_type) != NULL)
19961 return TYPE_TAG_NAME (parent_type);
19962 else
19963 /* An anonymous structure is only allowed non-static data
19964 members; no typedefs, no member functions, et cetera.
19965 So it does not need a prefix. */
19966 return "";
abc72ce4 19967 case DW_TAG_compile_unit:
95554aad 19968 case DW_TAG_partial_unit:
abc72ce4
DE
19969 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19970 if (cu->language == language_cplus
8b70b953 19971 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19972 && die->child != NULL
19973 && (die->tag == DW_TAG_class_type
19974 || die->tag == DW_TAG_structure_type
19975 || die->tag == DW_TAG_union_type))
19976 {
19977 char *name = guess_full_die_structure_name (die, cu);
19978 if (name != NULL)
19979 return name;
19980 }
19981 return "";
3d567982
TT
19982 case DW_TAG_enumeration_type:
19983 parent_type = read_type_die (parent, cu);
19984 if (TYPE_DECLARED_CLASS (parent_type))
19985 {
19986 if (TYPE_TAG_NAME (parent_type) != NULL)
19987 return TYPE_TAG_NAME (parent_type);
19988 return "";
19989 }
19990 /* Fall through. */
63d06c5c 19991 default:
8176b9b8 19992 return determine_prefix (parent, cu);
63d06c5c 19993 }
63d06c5c
DC
19994}
19995
3e43a32a
MS
19996/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19997 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19998 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19999 an obconcat, otherwise allocate storage for the result. The CU argument is
20000 used to determine the language and hence, the appropriate separator. */
987504bb 20001
f55ee35c 20002#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20003
20004static char *
f55ee35c
JK
20005typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20006 int physname, struct dwarf2_cu *cu)
63d06c5c 20007{
f55ee35c 20008 const char *lead = "";
5c315b68 20009 const char *sep;
63d06c5c 20010
3e43a32a
MS
20011 if (suffix == NULL || suffix[0] == '\0'
20012 || prefix == NULL || prefix[0] == '\0')
987504bb 20013 sep = "";
45280282
IB
20014 else if (cu->language == language_d)
20015 {
20016 /* For D, the 'main' function could be defined in any module, but it
20017 should never be prefixed. */
20018 if (strcmp (suffix, "D main") == 0)
20019 {
20020 prefix = "";
20021 sep = "";
20022 }
20023 else
20024 sep = ".";
20025 }
f55ee35c
JK
20026 else if (cu->language == language_fortran && physname)
20027 {
20028 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20029 DW_AT_MIPS_linkage_name is preferred and used instead. */
20030
20031 lead = "__";
20032 sep = "_MOD_";
20033 }
987504bb
JJ
20034 else
20035 sep = "::";
63d06c5c 20036
6dd47d34
DE
20037 if (prefix == NULL)
20038 prefix = "";
20039 if (suffix == NULL)
20040 suffix = "";
20041
987504bb
JJ
20042 if (obs == NULL)
20043 {
3e43a32a 20044 char *retval
224c3ddb
SM
20045 = ((char *)
20046 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20047
f55ee35c
JK
20048 strcpy (retval, lead);
20049 strcat (retval, prefix);
6dd47d34
DE
20050 strcat (retval, sep);
20051 strcat (retval, suffix);
63d06c5c
DC
20052 return retval;
20053 }
987504bb
JJ
20054 else
20055 {
20056 /* We have an obstack. */
f55ee35c 20057 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20058 }
63d06c5c
DC
20059}
20060
c906108c
SS
20061/* Return sibling of die, NULL if no sibling. */
20062
f9aca02d 20063static struct die_info *
fba45db2 20064sibling_die (struct die_info *die)
c906108c 20065{
639d11d3 20066 return die->sibling;
c906108c
SS
20067}
20068
71c25dea
TT
20069/* Get name of a die, return NULL if not found. */
20070
15d034d0
TT
20071static const char *
20072dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20073 struct obstack *obstack)
20074{
20075 if (name && cu->language == language_cplus)
20076 {
2f408ecb 20077 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20078
2f408ecb 20079 if (!canon_name.empty ())
71c25dea 20080 {
2f408ecb
PA
20081 if (canon_name != name)
20082 name = (const char *) obstack_copy0 (obstack,
20083 canon_name.c_str (),
20084 canon_name.length ());
71c25dea
TT
20085 }
20086 }
20087
20088 return name;
c906108c
SS
20089}
20090
96553a0c
DE
20091/* Get name of a die, return NULL if not found.
20092 Anonymous namespaces are converted to their magic string. */
9219021c 20093
15d034d0 20094static const char *
e142c38c 20095dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20096{
20097 struct attribute *attr;
20098
e142c38c 20099 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20100 if ((!attr || !DW_STRING (attr))
96553a0c 20101 && die->tag != DW_TAG_namespace
53832f31
TT
20102 && die->tag != DW_TAG_class_type
20103 && die->tag != DW_TAG_interface_type
20104 && die->tag != DW_TAG_structure_type
20105 && die->tag != DW_TAG_union_type)
71c25dea
TT
20106 return NULL;
20107
20108 switch (die->tag)
20109 {
20110 case DW_TAG_compile_unit:
95554aad 20111 case DW_TAG_partial_unit:
71c25dea
TT
20112 /* Compilation units have a DW_AT_name that is a filename, not
20113 a source language identifier. */
20114 case DW_TAG_enumeration_type:
20115 case DW_TAG_enumerator:
20116 /* These tags always have simple identifiers already; no need
20117 to canonicalize them. */
20118 return DW_STRING (attr);
907af001 20119
96553a0c
DE
20120 case DW_TAG_namespace:
20121 if (attr != NULL && DW_STRING (attr) != NULL)
20122 return DW_STRING (attr);
20123 return CP_ANONYMOUS_NAMESPACE_STR;
20124
907af001
UW
20125 case DW_TAG_class_type:
20126 case DW_TAG_interface_type:
20127 case DW_TAG_structure_type:
20128 case DW_TAG_union_type:
20129 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20130 structures or unions. These were of the form "._%d" in GCC 4.1,
20131 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20132 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20133 if (attr && DW_STRING (attr)
61012eef
GB
20134 && (startswith (DW_STRING (attr), "._")
20135 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20136 return NULL;
53832f31
TT
20137
20138 /* GCC might emit a nameless typedef that has a linkage name. See
20139 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20140 if (!attr || DW_STRING (attr) == NULL)
20141 {
df5c6c50 20142 char *demangled = NULL;
53832f31
TT
20143
20144 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20145 if (attr == NULL)
20146 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20147
20148 if (attr == NULL || DW_STRING (attr) == NULL)
20149 return NULL;
20150
df5c6c50
JK
20151 /* Avoid demangling DW_STRING (attr) the second time on a second
20152 call for the same DIE. */
20153 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20154 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20155
20156 if (demangled)
20157 {
e6a959d6 20158 const char *base;
96408a79 20159
53832f31 20160 /* FIXME: we already did this for the partial symbol... */
34a68019 20161 DW_STRING (attr)
224c3ddb
SM
20162 = ((const char *)
20163 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20164 demangled, strlen (demangled)));
53832f31
TT
20165 DW_STRING_IS_CANONICAL (attr) = 1;
20166 xfree (demangled);
96408a79
SA
20167
20168 /* Strip any leading namespaces/classes, keep only the base name.
20169 DW_AT_name for named DIEs does not contain the prefixes. */
20170 base = strrchr (DW_STRING (attr), ':');
20171 if (base && base > DW_STRING (attr) && base[-1] == ':')
20172 return &base[1];
20173 else
20174 return DW_STRING (attr);
53832f31
TT
20175 }
20176 }
907af001
UW
20177 break;
20178
71c25dea 20179 default:
907af001
UW
20180 break;
20181 }
20182
20183 if (!DW_STRING_IS_CANONICAL (attr))
20184 {
20185 DW_STRING (attr)
20186 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20187 &cu->objfile->per_bfd->storage_obstack);
907af001 20188 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20189 }
907af001 20190 return DW_STRING (attr);
9219021c
DC
20191}
20192
20193/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20194 is none. *EXT_CU is the CU containing DIE on input, and the CU
20195 containing the return value on output. */
9219021c
DC
20196
20197static struct die_info *
f2f0e013 20198dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20199{
20200 struct attribute *attr;
9219021c 20201
f2f0e013 20202 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20203 if (attr == NULL)
20204 return NULL;
20205
f2f0e013 20206 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20207}
20208
c906108c
SS
20209/* Convert a DIE tag into its string name. */
20210
f39c6ffd 20211static const char *
aa1ee363 20212dwarf_tag_name (unsigned tag)
c906108c 20213{
f39c6ffd
TT
20214 const char *name = get_DW_TAG_name (tag);
20215
20216 if (name == NULL)
20217 return "DW_TAG_<unknown>";
20218
20219 return name;
c906108c
SS
20220}
20221
20222/* Convert a DWARF attribute code into its string name. */
20223
f39c6ffd 20224static const char *
aa1ee363 20225dwarf_attr_name (unsigned attr)
c906108c 20226{
f39c6ffd
TT
20227 const char *name;
20228
c764a876 20229#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20230 if (attr == DW_AT_MIPS_fde)
20231 return "DW_AT_MIPS_fde";
20232#else
20233 if (attr == DW_AT_HP_block_index)
20234 return "DW_AT_HP_block_index";
c764a876 20235#endif
f39c6ffd
TT
20236
20237 name = get_DW_AT_name (attr);
20238
20239 if (name == NULL)
20240 return "DW_AT_<unknown>";
20241
20242 return name;
c906108c
SS
20243}
20244
20245/* Convert a DWARF value form code into its string name. */
20246
f39c6ffd 20247static const char *
aa1ee363 20248dwarf_form_name (unsigned form)
c906108c 20249{
f39c6ffd
TT
20250 const char *name = get_DW_FORM_name (form);
20251
20252 if (name == NULL)
20253 return "DW_FORM_<unknown>";
20254
20255 return name;
c906108c
SS
20256}
20257
20258static char *
fba45db2 20259dwarf_bool_name (unsigned mybool)
c906108c
SS
20260{
20261 if (mybool)
20262 return "TRUE";
20263 else
20264 return "FALSE";
20265}
20266
20267/* Convert a DWARF type code into its string name. */
20268
f39c6ffd 20269static const char *
aa1ee363 20270dwarf_type_encoding_name (unsigned enc)
c906108c 20271{
f39c6ffd 20272 const char *name = get_DW_ATE_name (enc);
c906108c 20273
f39c6ffd
TT
20274 if (name == NULL)
20275 return "DW_ATE_<unknown>";
c906108c 20276
f39c6ffd 20277 return name;
c906108c 20278}
c906108c 20279
f9aca02d 20280static void
d97bc12b 20281dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20282{
20283 unsigned int i;
20284
d97bc12b
DE
20285 print_spaces (indent, f);
20286 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20287 dwarf_tag_name (die->tag), die->abbrev,
20288 to_underlying (die->sect_off));
d97bc12b
DE
20289
20290 if (die->parent != NULL)
20291 {
20292 print_spaces (indent, f);
20293 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20294 to_underlying (die->parent->sect_off));
d97bc12b
DE
20295 }
20296
20297 print_spaces (indent, f);
20298 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20299 dwarf_bool_name (die->child != NULL));
c906108c 20300
d97bc12b
DE
20301 print_spaces (indent, f);
20302 fprintf_unfiltered (f, " attributes:\n");
20303
c906108c
SS
20304 for (i = 0; i < die->num_attrs; ++i)
20305 {
d97bc12b
DE
20306 print_spaces (indent, f);
20307 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20308 dwarf_attr_name (die->attrs[i].name),
20309 dwarf_form_name (die->attrs[i].form));
d97bc12b 20310
c906108c
SS
20311 switch (die->attrs[i].form)
20312 {
c906108c 20313 case DW_FORM_addr:
3019eac3 20314 case DW_FORM_GNU_addr_index:
d97bc12b 20315 fprintf_unfiltered (f, "address: ");
5af949e3 20316 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20317 break;
20318 case DW_FORM_block2:
20319 case DW_FORM_block4:
20320 case DW_FORM_block:
20321 case DW_FORM_block1:
56eb65bd
SP
20322 fprintf_unfiltered (f, "block: size %s",
20323 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20324 break;
2dc7f7b3 20325 case DW_FORM_exprloc:
56eb65bd
SP
20326 fprintf_unfiltered (f, "expression: size %s",
20327 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20328 break;
0224619f
JK
20329 case DW_FORM_data16:
20330 fprintf_unfiltered (f, "constant of 16 bytes");
20331 break;
4568ecf9
DE
20332 case DW_FORM_ref_addr:
20333 fprintf_unfiltered (f, "ref address: ");
20334 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20335 break;
36586728
TT
20336 case DW_FORM_GNU_ref_alt:
20337 fprintf_unfiltered (f, "alt ref address: ");
20338 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20339 break;
10b3939b
DJ
20340 case DW_FORM_ref1:
20341 case DW_FORM_ref2:
20342 case DW_FORM_ref4:
4568ecf9
DE
20343 case DW_FORM_ref8:
20344 case DW_FORM_ref_udata:
d97bc12b 20345 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20346 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20347 break;
c906108c
SS
20348 case DW_FORM_data1:
20349 case DW_FORM_data2:
20350 case DW_FORM_data4:
ce5d95e1 20351 case DW_FORM_data8:
c906108c
SS
20352 case DW_FORM_udata:
20353 case DW_FORM_sdata:
43bbcdc2
PH
20354 fprintf_unfiltered (f, "constant: %s",
20355 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20356 break;
2dc7f7b3
TT
20357 case DW_FORM_sec_offset:
20358 fprintf_unfiltered (f, "section offset: %s",
20359 pulongest (DW_UNSND (&die->attrs[i])));
20360 break;
55f1336d 20361 case DW_FORM_ref_sig8:
ac9ec31b
DE
20362 fprintf_unfiltered (f, "signature: %s",
20363 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20364 break;
c906108c 20365 case DW_FORM_string:
4bdf3d34 20366 case DW_FORM_strp:
43988095 20367 case DW_FORM_line_strp:
3019eac3 20368 case DW_FORM_GNU_str_index:
36586728 20369 case DW_FORM_GNU_strp_alt:
8285870a 20370 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20371 DW_STRING (&die->attrs[i])
8285870a
JK
20372 ? DW_STRING (&die->attrs[i]) : "",
20373 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20374 break;
20375 case DW_FORM_flag:
20376 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20377 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20378 else
d97bc12b 20379 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20380 break;
2dc7f7b3
TT
20381 case DW_FORM_flag_present:
20382 fprintf_unfiltered (f, "flag: TRUE");
20383 break;
a8329558 20384 case DW_FORM_indirect:
0963b4bd
MS
20385 /* The reader will have reduced the indirect form to
20386 the "base form" so this form should not occur. */
3e43a32a
MS
20387 fprintf_unfiltered (f,
20388 "unexpected attribute form: DW_FORM_indirect");
a8329558 20389 break;
c906108c 20390 default:
d97bc12b 20391 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20392 die->attrs[i].form);
d97bc12b 20393 break;
c906108c 20394 }
d97bc12b 20395 fprintf_unfiltered (f, "\n");
c906108c
SS
20396 }
20397}
20398
f9aca02d 20399static void
d97bc12b 20400dump_die_for_error (struct die_info *die)
c906108c 20401{
d97bc12b
DE
20402 dump_die_shallow (gdb_stderr, 0, die);
20403}
20404
20405static void
20406dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20407{
20408 int indent = level * 4;
20409
20410 gdb_assert (die != NULL);
20411
20412 if (level >= max_level)
20413 return;
20414
20415 dump_die_shallow (f, indent, die);
20416
20417 if (die->child != NULL)
c906108c 20418 {
d97bc12b
DE
20419 print_spaces (indent, f);
20420 fprintf_unfiltered (f, " Children:");
20421 if (level + 1 < max_level)
20422 {
20423 fprintf_unfiltered (f, "\n");
20424 dump_die_1 (f, level + 1, max_level, die->child);
20425 }
20426 else
20427 {
3e43a32a
MS
20428 fprintf_unfiltered (f,
20429 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20430 }
20431 }
20432
20433 if (die->sibling != NULL && level > 0)
20434 {
20435 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20436 }
20437}
20438
d97bc12b
DE
20439/* This is called from the pdie macro in gdbinit.in.
20440 It's not static so gcc will keep a copy callable from gdb. */
20441
20442void
20443dump_die (struct die_info *die, int max_level)
20444{
20445 dump_die_1 (gdb_stdlog, 0, max_level, die);
20446}
20447
f9aca02d 20448static void
51545339 20449store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20450{
51545339 20451 void **slot;
c906108c 20452
9c541725
PA
20453 slot = htab_find_slot_with_hash (cu->die_hash, die,
20454 to_underlying (die->sect_off),
b64f50a1 20455 INSERT);
51545339
DJ
20456
20457 *slot = die;
c906108c
SS
20458}
20459
b64f50a1
JK
20460/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20461 required kind. */
20462
20463static sect_offset
ff39bb5e 20464dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20465{
7771576e 20466 if (attr_form_is_ref (attr))
9c541725 20467 return (sect_offset) DW_UNSND (attr);
93311388
DE
20468
20469 complaint (&symfile_complaints,
20470 _("unsupported die ref attribute form: '%s'"),
20471 dwarf_form_name (attr->form));
9c541725 20472 return {};
c906108c
SS
20473}
20474
43bbcdc2
PH
20475/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20476 * the value held by the attribute is not constant. */
a02abb62 20477
43bbcdc2 20478static LONGEST
ff39bb5e 20479dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20480{
20481 if (attr->form == DW_FORM_sdata)
20482 return DW_SND (attr);
20483 else if (attr->form == DW_FORM_udata
20484 || attr->form == DW_FORM_data1
20485 || attr->form == DW_FORM_data2
20486 || attr->form == DW_FORM_data4
20487 || attr->form == DW_FORM_data8)
20488 return DW_UNSND (attr);
20489 else
20490 {
0224619f 20491 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20492 complaint (&symfile_complaints,
20493 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20494 dwarf_form_name (attr->form));
20495 return default_value;
20496 }
20497}
20498
348e048f
DE
20499/* Follow reference or signature attribute ATTR of SRC_DIE.
20500 On entry *REF_CU is the CU of SRC_DIE.
20501 On exit *REF_CU is the CU of the result. */
20502
20503static struct die_info *
ff39bb5e 20504follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20505 struct dwarf2_cu **ref_cu)
20506{
20507 struct die_info *die;
20508
7771576e 20509 if (attr_form_is_ref (attr))
348e048f 20510 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20511 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20512 die = follow_die_sig (src_die, attr, ref_cu);
20513 else
20514 {
20515 dump_die_for_error (src_die);
20516 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20517 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20518 }
20519
20520 return die;
03dd20cc
DJ
20521}
20522
5c631832 20523/* Follow reference OFFSET.
673bfd45
DE
20524 On entry *REF_CU is the CU of the source die referencing OFFSET.
20525 On exit *REF_CU is the CU of the result.
20526 Returns NULL if OFFSET is invalid. */
f504f079 20527
f9aca02d 20528static struct die_info *
9c541725 20529follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20530 struct dwarf2_cu **ref_cu)
c906108c 20531{
10b3939b 20532 struct die_info temp_die;
f2f0e013 20533 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20534
348e048f
DE
20535 gdb_assert (cu->per_cu != NULL);
20536
98bfdba5
PA
20537 target_cu = cu;
20538
3019eac3 20539 if (cu->per_cu->is_debug_types)
348e048f
DE
20540 {
20541 /* .debug_types CUs cannot reference anything outside their CU.
20542 If they need to, they have to reference a signatured type via
55f1336d 20543 DW_FORM_ref_sig8. */
9c541725 20544 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20545 return NULL;
348e048f 20546 }
36586728 20547 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20548 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20549 {
20550 struct dwarf2_per_cu_data *per_cu;
9a619af0 20551
9c541725 20552 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20553 cu->objfile);
03dd20cc
DJ
20554
20555 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20556 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20557 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20558
10b3939b
DJ
20559 target_cu = per_cu->cu;
20560 }
98bfdba5
PA
20561 else if (cu->dies == NULL)
20562 {
20563 /* We're loading full DIEs during partial symbol reading. */
20564 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20565 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20566 }
c906108c 20567
f2f0e013 20568 *ref_cu = target_cu;
9c541725 20569 temp_die.sect_off = sect_off;
9a3c8263 20570 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20571 &temp_die,
20572 to_underlying (sect_off));
5c631832 20573}
10b3939b 20574
5c631832
JK
20575/* Follow reference attribute ATTR of SRC_DIE.
20576 On entry *REF_CU is the CU of SRC_DIE.
20577 On exit *REF_CU is the CU of the result. */
20578
20579static struct die_info *
ff39bb5e 20580follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20581 struct dwarf2_cu **ref_cu)
20582{
9c541725 20583 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20584 struct dwarf2_cu *cu = *ref_cu;
20585 struct die_info *die;
20586
9c541725 20587 die = follow_die_offset (sect_off,
36586728
TT
20588 (attr->form == DW_FORM_GNU_ref_alt
20589 || cu->per_cu->is_dwz),
20590 ref_cu);
5c631832
JK
20591 if (!die)
20592 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20593 "at 0x%x [in module %s]"),
9c541725 20594 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20595 objfile_name (cu->objfile));
348e048f 20596
5c631832
JK
20597 return die;
20598}
20599
9c541725 20600/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20601 Returned value is intended for DW_OP_call*. Returned
20602 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20603
20604struct dwarf2_locexpr_baton
9c541725 20605dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20606 struct dwarf2_per_cu_data *per_cu,
20607 CORE_ADDR (*get_frame_pc) (void *baton),
20608 void *baton)
5c631832 20609{
918dd910 20610 struct dwarf2_cu *cu;
5c631832
JK
20611 struct die_info *die;
20612 struct attribute *attr;
20613 struct dwarf2_locexpr_baton retval;
20614
8cf6f0b1
TT
20615 dw2_setup (per_cu->objfile);
20616
918dd910
JK
20617 if (per_cu->cu == NULL)
20618 load_cu (per_cu);
20619 cu = per_cu->cu;
cc12ce38
DE
20620 if (cu == NULL)
20621 {
20622 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20623 Instead just throw an error, not much else we can do. */
20624 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20625 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20626 }
918dd910 20627
9c541725 20628 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20629 if (!die)
20630 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20631 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20632
20633 attr = dwarf2_attr (die, DW_AT_location, cu);
20634 if (!attr)
20635 {
e103e986
JK
20636 /* DWARF: "If there is no such attribute, then there is no effect.".
20637 DATA is ignored if SIZE is 0. */
5c631832 20638
e103e986 20639 retval.data = NULL;
5c631832
JK
20640 retval.size = 0;
20641 }
8cf6f0b1
TT
20642 else if (attr_form_is_section_offset (attr))
20643 {
20644 struct dwarf2_loclist_baton loclist_baton;
20645 CORE_ADDR pc = (*get_frame_pc) (baton);
20646 size_t size;
20647
20648 fill_in_loclist_baton (cu, &loclist_baton, attr);
20649
20650 retval.data = dwarf2_find_location_expression (&loclist_baton,
20651 &size, pc);
20652 retval.size = size;
20653 }
5c631832
JK
20654 else
20655 {
20656 if (!attr_form_is_block (attr))
20657 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20658 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20659 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20660
20661 retval.data = DW_BLOCK (attr)->data;
20662 retval.size = DW_BLOCK (attr)->size;
20663 }
20664 retval.per_cu = cu->per_cu;
918dd910 20665
918dd910
JK
20666 age_cached_comp_units ();
20667
5c631832 20668 return retval;
348e048f
DE
20669}
20670
8b9737bf
TT
20671/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20672 offset. */
20673
20674struct dwarf2_locexpr_baton
20675dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20676 struct dwarf2_per_cu_data *per_cu,
20677 CORE_ADDR (*get_frame_pc) (void *baton),
20678 void *baton)
20679{
9c541725 20680 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20681
9c541725 20682 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20683}
20684
b6807d98
TT
20685/* Write a constant of a given type as target-ordered bytes into
20686 OBSTACK. */
20687
20688static const gdb_byte *
20689write_constant_as_bytes (struct obstack *obstack,
20690 enum bfd_endian byte_order,
20691 struct type *type,
20692 ULONGEST value,
20693 LONGEST *len)
20694{
20695 gdb_byte *result;
20696
20697 *len = TYPE_LENGTH (type);
224c3ddb 20698 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20699 store_unsigned_integer (result, *len, byte_order, value);
20700
20701 return result;
20702}
20703
20704/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20705 pointer to the constant bytes and set LEN to the length of the
20706 data. If memory is needed, allocate it on OBSTACK. If the DIE
20707 does not have a DW_AT_const_value, return NULL. */
20708
20709const gdb_byte *
9c541725 20710dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20711 struct dwarf2_per_cu_data *per_cu,
20712 struct obstack *obstack,
20713 LONGEST *len)
20714{
20715 struct dwarf2_cu *cu;
20716 struct die_info *die;
20717 struct attribute *attr;
20718 const gdb_byte *result = NULL;
20719 struct type *type;
20720 LONGEST value;
20721 enum bfd_endian byte_order;
20722
20723 dw2_setup (per_cu->objfile);
20724
20725 if (per_cu->cu == NULL)
20726 load_cu (per_cu);
20727 cu = per_cu->cu;
cc12ce38
DE
20728 if (cu == NULL)
20729 {
20730 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20731 Instead just throw an error, not much else we can do. */
20732 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20733 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20734 }
b6807d98 20735
9c541725 20736 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20737 if (!die)
20738 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20739 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20740
20741
20742 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20743 if (attr == NULL)
20744 return NULL;
20745
20746 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20747 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20748
20749 switch (attr->form)
20750 {
20751 case DW_FORM_addr:
20752 case DW_FORM_GNU_addr_index:
20753 {
20754 gdb_byte *tem;
20755
20756 *len = cu->header.addr_size;
224c3ddb 20757 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20758 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20759 result = tem;
20760 }
20761 break;
20762 case DW_FORM_string:
20763 case DW_FORM_strp:
20764 case DW_FORM_GNU_str_index:
20765 case DW_FORM_GNU_strp_alt:
20766 /* DW_STRING is already allocated on the objfile obstack, point
20767 directly to it. */
20768 result = (const gdb_byte *) DW_STRING (attr);
20769 *len = strlen (DW_STRING (attr));
20770 break;
20771 case DW_FORM_block1:
20772 case DW_FORM_block2:
20773 case DW_FORM_block4:
20774 case DW_FORM_block:
20775 case DW_FORM_exprloc:
0224619f 20776 case DW_FORM_data16:
b6807d98
TT
20777 result = DW_BLOCK (attr)->data;
20778 *len = DW_BLOCK (attr)->size;
20779 break;
20780
20781 /* The DW_AT_const_value attributes are supposed to carry the
20782 symbol's value "represented as it would be on the target
20783 architecture." By the time we get here, it's already been
20784 converted to host endianness, so we just need to sign- or
20785 zero-extend it as appropriate. */
20786 case DW_FORM_data1:
20787 type = die_type (die, cu);
20788 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20789 if (result == NULL)
20790 result = write_constant_as_bytes (obstack, byte_order,
20791 type, value, len);
20792 break;
20793 case DW_FORM_data2:
20794 type = die_type (die, cu);
20795 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20796 if (result == NULL)
20797 result = write_constant_as_bytes (obstack, byte_order,
20798 type, value, len);
20799 break;
20800 case DW_FORM_data4:
20801 type = die_type (die, cu);
20802 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20803 if (result == NULL)
20804 result = write_constant_as_bytes (obstack, byte_order,
20805 type, value, len);
20806 break;
20807 case DW_FORM_data8:
20808 type = die_type (die, cu);
20809 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20810 if (result == NULL)
20811 result = write_constant_as_bytes (obstack, byte_order,
20812 type, value, len);
20813 break;
20814
20815 case DW_FORM_sdata:
20816 type = die_type (die, cu);
20817 result = write_constant_as_bytes (obstack, byte_order,
20818 type, DW_SND (attr), len);
20819 break;
20820
20821 case DW_FORM_udata:
20822 type = die_type (die, cu);
20823 result = write_constant_as_bytes (obstack, byte_order,
20824 type, DW_UNSND (attr), len);
20825 break;
20826
20827 default:
20828 complaint (&symfile_complaints,
20829 _("unsupported const value attribute form: '%s'"),
20830 dwarf_form_name (attr->form));
20831 break;
20832 }
20833
20834 return result;
20835}
20836
7942e96e
AA
20837/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20838 valid type for this die is found. */
20839
20840struct type *
9c541725 20841dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
20842 struct dwarf2_per_cu_data *per_cu)
20843{
20844 struct dwarf2_cu *cu;
20845 struct die_info *die;
20846
20847 dw2_setup (per_cu->objfile);
20848
20849 if (per_cu->cu == NULL)
20850 load_cu (per_cu);
20851 cu = per_cu->cu;
20852 if (!cu)
20853 return NULL;
20854
9c541725 20855 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
20856 if (!die)
20857 return NULL;
20858
20859 return die_type (die, cu);
20860}
20861
8a9b8146
TT
20862/* Return the type of the DIE at DIE_OFFSET in the CU named by
20863 PER_CU. */
20864
20865struct type *
b64f50a1 20866dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20867 struct dwarf2_per_cu_data *per_cu)
20868{
8a9b8146 20869 dw2_setup (per_cu->objfile);
b64f50a1 20870
9c541725 20871 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 20872 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20873}
20874
ac9ec31b 20875/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20876 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20877 On exit *REF_CU is the CU of the result.
20878 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20879
20880static struct die_info *
ac9ec31b
DE
20881follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20882 struct dwarf2_cu **ref_cu)
348e048f 20883{
348e048f 20884 struct die_info temp_die;
348e048f
DE
20885 struct dwarf2_cu *sig_cu;
20886 struct die_info *die;
20887
ac9ec31b
DE
20888 /* While it might be nice to assert sig_type->type == NULL here,
20889 we can get here for DW_AT_imported_declaration where we need
20890 the DIE not the type. */
348e048f
DE
20891
20892 /* If necessary, add it to the queue and load its DIEs. */
20893
95554aad 20894 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20895 read_signatured_type (sig_type);
348e048f 20896
348e048f 20897 sig_cu = sig_type->per_cu.cu;
69d751e3 20898 gdb_assert (sig_cu != NULL);
9c541725
PA
20899 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20900 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 20901 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 20902 to_underlying (temp_die.sect_off));
348e048f
DE
20903 if (die)
20904 {
796a7ff8
DE
20905 /* For .gdb_index version 7 keep track of included TUs.
20906 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20907 if (dwarf2_per_objfile->index_table != NULL
20908 && dwarf2_per_objfile->index_table->version <= 7)
20909 {
20910 VEC_safe_push (dwarf2_per_cu_ptr,
20911 (*ref_cu)->per_cu->imported_symtabs,
20912 sig_cu->per_cu);
20913 }
20914
348e048f
DE
20915 *ref_cu = sig_cu;
20916 return die;
20917 }
20918
ac9ec31b
DE
20919 return NULL;
20920}
20921
20922/* Follow signatured type referenced by ATTR in SRC_DIE.
20923 On entry *REF_CU is the CU of SRC_DIE.
20924 On exit *REF_CU is the CU of the result.
20925 The result is the DIE of the type.
20926 If the referenced type cannot be found an error is thrown. */
20927
20928static struct die_info *
ff39bb5e 20929follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20930 struct dwarf2_cu **ref_cu)
20931{
20932 ULONGEST signature = DW_SIGNATURE (attr);
20933 struct signatured_type *sig_type;
20934 struct die_info *die;
20935
20936 gdb_assert (attr->form == DW_FORM_ref_sig8);
20937
a2ce51a0 20938 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20939 /* sig_type will be NULL if the signatured type is missing from
20940 the debug info. */
20941 if (sig_type == NULL)
20942 {
20943 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20944 " from DIE at 0x%x [in module %s]"),
9c541725 20945 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20946 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20947 }
20948
20949 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20950 if (die == NULL)
20951 {
20952 dump_die_for_error (src_die);
20953 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20954 " from DIE at 0x%x [in module %s]"),
9c541725 20955 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20956 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20957 }
20958
20959 return die;
20960}
20961
20962/* Get the type specified by SIGNATURE referenced in DIE/CU,
20963 reading in and processing the type unit if necessary. */
20964
20965static struct type *
20966get_signatured_type (struct die_info *die, ULONGEST signature,
20967 struct dwarf2_cu *cu)
20968{
20969 struct signatured_type *sig_type;
20970 struct dwarf2_cu *type_cu;
20971 struct die_info *type_die;
20972 struct type *type;
20973
a2ce51a0 20974 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20975 /* sig_type will be NULL if the signatured type is missing from
20976 the debug info. */
20977 if (sig_type == NULL)
20978 {
20979 complaint (&symfile_complaints,
20980 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20981 " from DIE at 0x%x [in module %s]"),
9c541725 20982 hex_string (signature), to_underlying (die->sect_off),
4262abfb 20983 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20984 return build_error_marker_type (cu, die);
20985 }
20986
20987 /* If we already know the type we're done. */
20988 if (sig_type->type != NULL)
20989 return sig_type->type;
20990
20991 type_cu = cu;
20992 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20993 if (type_die != NULL)
20994 {
20995 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20996 is created. This is important, for example, because for c++ classes
20997 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20998 type = read_type_die (type_die, type_cu);
20999 if (type == NULL)
21000 {
21001 complaint (&symfile_complaints,
21002 _("Dwarf Error: Cannot build signatured type %s"
21003 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21004 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21005 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21006 type = build_error_marker_type (cu, die);
21007 }
21008 }
21009 else
21010 {
21011 complaint (&symfile_complaints,
21012 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21013 " from DIE at 0x%x [in module %s]"),
9c541725 21014 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21015 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21016 type = build_error_marker_type (cu, die);
21017 }
21018 sig_type->type = type;
21019
21020 return type;
21021}
21022
21023/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21024 reading in and processing the type unit if necessary. */
21025
21026static struct type *
ff39bb5e 21027get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21028 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21029{
21030 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21031 if (attr_form_is_ref (attr))
ac9ec31b
DE
21032 {
21033 struct dwarf2_cu *type_cu = cu;
21034 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21035
21036 return read_type_die (type_die, type_cu);
21037 }
21038 else if (attr->form == DW_FORM_ref_sig8)
21039 {
21040 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21041 }
21042 else
21043 {
21044 complaint (&symfile_complaints,
21045 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21046 " at 0x%x [in module %s]"),
9c541725 21047 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21048 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21049 return build_error_marker_type (cu, die);
21050 }
348e048f
DE
21051}
21052
e5fe5e75 21053/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21054
21055static void
e5fe5e75 21056load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21057{
52dc124a 21058 struct signatured_type *sig_type;
348e048f 21059
f4dc4d17
DE
21060 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21061 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21062
6721b2ec
DE
21063 /* We have the per_cu, but we need the signatured_type.
21064 Fortunately this is an easy translation. */
21065 gdb_assert (per_cu->is_debug_types);
21066 sig_type = (struct signatured_type *) per_cu;
348e048f 21067
6721b2ec 21068 gdb_assert (per_cu->cu == NULL);
348e048f 21069
52dc124a 21070 read_signatured_type (sig_type);
348e048f 21071
6721b2ec 21072 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21073}
21074
dee91e82
DE
21075/* die_reader_func for read_signatured_type.
21076 This is identical to load_full_comp_unit_reader,
21077 but is kept separate for now. */
348e048f
DE
21078
21079static void
dee91e82 21080read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21081 const gdb_byte *info_ptr,
dee91e82
DE
21082 struct die_info *comp_unit_die,
21083 int has_children,
21084 void *data)
348e048f 21085{
dee91e82 21086 struct dwarf2_cu *cu = reader->cu;
348e048f 21087
dee91e82
DE
21088 gdb_assert (cu->die_hash == NULL);
21089 cu->die_hash =
21090 htab_create_alloc_ex (cu->header.length / 12,
21091 die_hash,
21092 die_eq,
21093 NULL,
21094 &cu->comp_unit_obstack,
21095 hashtab_obstack_allocate,
21096 dummy_obstack_deallocate);
348e048f 21097
dee91e82
DE
21098 if (has_children)
21099 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21100 &info_ptr, comp_unit_die);
21101 cu->dies = comp_unit_die;
21102 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21103
21104 /* We try not to read any attributes in this function, because not
9cdd5dbd 21105 all CUs needed for references have been loaded yet, and symbol
348e048f 21106 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21107 or we won't be able to build types correctly.
21108 Similarly, if we do not read the producer, we can not apply
21109 producer-specific interpretation. */
95554aad 21110 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21111}
348e048f 21112
3019eac3
DE
21113/* Read in a signatured type and build its CU and DIEs.
21114 If the type is a stub for the real type in a DWO file,
21115 read in the real type from the DWO file as well. */
dee91e82
DE
21116
21117static void
21118read_signatured_type (struct signatured_type *sig_type)
21119{
21120 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21121
3019eac3 21122 gdb_assert (per_cu->is_debug_types);
dee91e82 21123 gdb_assert (per_cu->cu == NULL);
348e048f 21124
f4dc4d17
DE
21125 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21126 read_signatured_type_reader, NULL);
7ee85ab1 21127 sig_type->per_cu.tu_read = 1;
c906108c
SS
21128}
21129
c906108c
SS
21130/* Decode simple location descriptions.
21131 Given a pointer to a dwarf block that defines a location, compute
21132 the location and return the value.
21133
4cecd739
DJ
21134 NOTE drow/2003-11-18: This function is called in two situations
21135 now: for the address of static or global variables (partial symbols
21136 only) and for offsets into structures which are expected to be
21137 (more or less) constant. The partial symbol case should go away,
21138 and only the constant case should remain. That will let this
21139 function complain more accurately. A few special modes are allowed
21140 without complaint for global variables (for instance, global
21141 register values and thread-local values).
c906108c
SS
21142
21143 A location description containing no operations indicates that the
4cecd739 21144 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21145 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21146 callers will only want a very basic result and this can become a
21ae7a4d
JK
21147 complaint.
21148
21149 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21150
21151static CORE_ADDR
e7c27a73 21152decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21153{
e7c27a73 21154 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21155 size_t i;
21156 size_t size = blk->size;
d521ce57 21157 const gdb_byte *data = blk->data;
21ae7a4d
JK
21158 CORE_ADDR stack[64];
21159 int stacki;
21160 unsigned int bytes_read, unsnd;
21161 gdb_byte op;
c906108c 21162
21ae7a4d
JK
21163 i = 0;
21164 stacki = 0;
21165 stack[stacki] = 0;
21166 stack[++stacki] = 0;
21167
21168 while (i < size)
21169 {
21170 op = data[i++];
21171 switch (op)
21172 {
21173 case DW_OP_lit0:
21174 case DW_OP_lit1:
21175 case DW_OP_lit2:
21176 case DW_OP_lit3:
21177 case DW_OP_lit4:
21178 case DW_OP_lit5:
21179 case DW_OP_lit6:
21180 case DW_OP_lit7:
21181 case DW_OP_lit8:
21182 case DW_OP_lit9:
21183 case DW_OP_lit10:
21184 case DW_OP_lit11:
21185 case DW_OP_lit12:
21186 case DW_OP_lit13:
21187 case DW_OP_lit14:
21188 case DW_OP_lit15:
21189 case DW_OP_lit16:
21190 case DW_OP_lit17:
21191 case DW_OP_lit18:
21192 case DW_OP_lit19:
21193 case DW_OP_lit20:
21194 case DW_OP_lit21:
21195 case DW_OP_lit22:
21196 case DW_OP_lit23:
21197 case DW_OP_lit24:
21198 case DW_OP_lit25:
21199 case DW_OP_lit26:
21200 case DW_OP_lit27:
21201 case DW_OP_lit28:
21202 case DW_OP_lit29:
21203 case DW_OP_lit30:
21204 case DW_OP_lit31:
21205 stack[++stacki] = op - DW_OP_lit0;
21206 break;
f1bea926 21207
21ae7a4d
JK
21208 case DW_OP_reg0:
21209 case DW_OP_reg1:
21210 case DW_OP_reg2:
21211 case DW_OP_reg3:
21212 case DW_OP_reg4:
21213 case DW_OP_reg5:
21214 case DW_OP_reg6:
21215 case DW_OP_reg7:
21216 case DW_OP_reg8:
21217 case DW_OP_reg9:
21218 case DW_OP_reg10:
21219 case DW_OP_reg11:
21220 case DW_OP_reg12:
21221 case DW_OP_reg13:
21222 case DW_OP_reg14:
21223 case DW_OP_reg15:
21224 case DW_OP_reg16:
21225 case DW_OP_reg17:
21226 case DW_OP_reg18:
21227 case DW_OP_reg19:
21228 case DW_OP_reg20:
21229 case DW_OP_reg21:
21230 case DW_OP_reg22:
21231 case DW_OP_reg23:
21232 case DW_OP_reg24:
21233 case DW_OP_reg25:
21234 case DW_OP_reg26:
21235 case DW_OP_reg27:
21236 case DW_OP_reg28:
21237 case DW_OP_reg29:
21238 case DW_OP_reg30:
21239 case DW_OP_reg31:
21240 stack[++stacki] = op - DW_OP_reg0;
21241 if (i < size)
21242 dwarf2_complex_location_expr_complaint ();
21243 break;
c906108c 21244
21ae7a4d
JK
21245 case DW_OP_regx:
21246 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21247 i += bytes_read;
21248 stack[++stacki] = unsnd;
21249 if (i < size)
21250 dwarf2_complex_location_expr_complaint ();
21251 break;
c906108c 21252
21ae7a4d
JK
21253 case DW_OP_addr:
21254 stack[++stacki] = read_address (objfile->obfd, &data[i],
21255 cu, &bytes_read);
21256 i += bytes_read;
21257 break;
d53d4ac5 21258
21ae7a4d
JK
21259 case DW_OP_const1u:
21260 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21261 i += 1;
21262 break;
21263
21264 case DW_OP_const1s:
21265 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21266 i += 1;
21267 break;
21268
21269 case DW_OP_const2u:
21270 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21271 i += 2;
21272 break;
21273
21274 case DW_OP_const2s:
21275 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21276 i += 2;
21277 break;
d53d4ac5 21278
21ae7a4d
JK
21279 case DW_OP_const4u:
21280 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21281 i += 4;
21282 break;
21283
21284 case DW_OP_const4s:
21285 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21286 i += 4;
21287 break;
21288
585861ea
JK
21289 case DW_OP_const8u:
21290 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21291 i += 8;
21292 break;
21293
21ae7a4d
JK
21294 case DW_OP_constu:
21295 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21296 &bytes_read);
21297 i += bytes_read;
21298 break;
21299
21300 case DW_OP_consts:
21301 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21302 i += bytes_read;
21303 break;
21304
21305 case DW_OP_dup:
21306 stack[stacki + 1] = stack[stacki];
21307 stacki++;
21308 break;
21309
21310 case DW_OP_plus:
21311 stack[stacki - 1] += stack[stacki];
21312 stacki--;
21313 break;
21314
21315 case DW_OP_plus_uconst:
21316 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21317 &bytes_read);
21318 i += bytes_read;
21319 break;
21320
21321 case DW_OP_minus:
21322 stack[stacki - 1] -= stack[stacki];
21323 stacki--;
21324 break;
21325
21326 case DW_OP_deref:
21327 /* If we're not the last op, then we definitely can't encode
21328 this using GDB's address_class enum. This is valid for partial
21329 global symbols, although the variable's address will be bogus
21330 in the psymtab. */
21331 if (i < size)
21332 dwarf2_complex_location_expr_complaint ();
21333 break;
21334
21335 case DW_OP_GNU_push_tls_address:
4aa4e28b 21336 case DW_OP_form_tls_address:
21ae7a4d
JK
21337 /* The top of the stack has the offset from the beginning
21338 of the thread control block at which the variable is located. */
21339 /* Nothing should follow this operator, so the top of stack would
21340 be returned. */
21341 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21342 address will be bogus in the psymtab. Make it always at least
21343 non-zero to not look as a variable garbage collected by linker
21344 which have DW_OP_addr 0. */
21ae7a4d
JK
21345 if (i < size)
21346 dwarf2_complex_location_expr_complaint ();
585861ea 21347 stack[stacki]++;
21ae7a4d
JK
21348 break;
21349
21350 case DW_OP_GNU_uninit:
21351 break;
21352
3019eac3 21353 case DW_OP_GNU_addr_index:
49f6c839 21354 case DW_OP_GNU_const_index:
3019eac3
DE
21355 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21356 &bytes_read);
21357 i += bytes_read;
21358 break;
21359
21ae7a4d
JK
21360 default:
21361 {
f39c6ffd 21362 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21363
21364 if (name)
21365 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21366 name);
21367 else
21368 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21369 op);
21370 }
21371
21372 return (stack[stacki]);
d53d4ac5 21373 }
3c6e0cb3 21374
21ae7a4d
JK
21375 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21376 outside of the allocated space. Also enforce minimum>0. */
21377 if (stacki >= ARRAY_SIZE (stack) - 1)
21378 {
21379 complaint (&symfile_complaints,
21380 _("location description stack overflow"));
21381 return 0;
21382 }
21383
21384 if (stacki <= 0)
21385 {
21386 complaint (&symfile_complaints,
21387 _("location description stack underflow"));
21388 return 0;
21389 }
21390 }
21391 return (stack[stacki]);
c906108c
SS
21392}
21393
21394/* memory allocation interface */
21395
c906108c 21396static struct dwarf_block *
7b5a2f43 21397dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21398{
8d749320 21399 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21400}
21401
c906108c 21402static struct die_info *
b60c80d6 21403dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21404{
21405 struct die_info *die;
b60c80d6
DJ
21406 size_t size = sizeof (struct die_info);
21407
21408 if (num_attrs > 1)
21409 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21410
b60c80d6 21411 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21412 memset (die, 0, sizeof (struct die_info));
21413 return (die);
21414}
2e276125
JB
21415
21416\f
21417/* Macro support. */
21418
233d95b5
JK
21419/* Return file name relative to the compilation directory of file number I in
21420 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21421 responsible for freeing it. */
233d95b5 21422
2e276125 21423static char *
233d95b5 21424file_file_name (int file, struct line_header *lh)
2e276125 21425{
6a83a1e6
EZ
21426 /* Is the file number a valid index into the line header's file name
21427 table? Remember that file numbers start with one, not zero. */
fff8551c 21428 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21429 {
8c43009f 21430 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21431
8c43009f
PA
21432 if (!IS_ABSOLUTE_PATH (fe.name))
21433 {
21434 const char *dir = fe.include_dir (lh);
21435 if (dir != NULL)
21436 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21437 }
21438 return xstrdup (fe.name);
6a83a1e6 21439 }
2e276125
JB
21440 else
21441 {
6a83a1e6
EZ
21442 /* The compiler produced a bogus file number. We can at least
21443 record the macro definitions made in the file, even if we
21444 won't be able to find the file by name. */
21445 char fake_name[80];
9a619af0 21446
8c042590
PM
21447 xsnprintf (fake_name, sizeof (fake_name),
21448 "<bad macro file number %d>", file);
2e276125 21449
6e70227d 21450 complaint (&symfile_complaints,
6a83a1e6
EZ
21451 _("bad file number in macro information (%d)"),
21452 file);
2e276125 21453
6a83a1e6 21454 return xstrdup (fake_name);
2e276125
JB
21455 }
21456}
21457
233d95b5
JK
21458/* Return the full name of file number I in *LH's file name table.
21459 Use COMP_DIR as the name of the current directory of the
21460 compilation. The result is allocated using xmalloc; the caller is
21461 responsible for freeing it. */
21462static char *
21463file_full_name (int file, struct line_header *lh, const char *comp_dir)
21464{
21465 /* Is the file number a valid index into the line header's file name
21466 table? Remember that file numbers start with one, not zero. */
fff8551c 21467 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21468 {
21469 char *relative = file_file_name (file, lh);
21470
21471 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21472 return relative;
b36cec19
PA
21473 return reconcat (relative, comp_dir, SLASH_STRING,
21474 relative, (char *) NULL);
233d95b5
JK
21475 }
21476 else
21477 return file_file_name (file, lh);
21478}
21479
2e276125
JB
21480
21481static struct macro_source_file *
21482macro_start_file (int file, int line,
21483 struct macro_source_file *current_file,
43f3e411 21484 struct line_header *lh)
2e276125 21485{
233d95b5
JK
21486 /* File name relative to the compilation directory of this source file. */
21487 char *file_name = file_file_name (file, lh);
2e276125 21488
2e276125 21489 if (! current_file)
abc9d0dc 21490 {
fc474241
DE
21491 /* Note: We don't create a macro table for this compilation unit
21492 at all until we actually get a filename. */
43f3e411 21493 struct macro_table *macro_table = get_macro_table ();
fc474241 21494
abc9d0dc
TT
21495 /* If we have no current file, then this must be the start_file
21496 directive for the compilation unit's main source file. */
fc474241
DE
21497 current_file = macro_set_main (macro_table, file_name);
21498 macro_define_special (macro_table);
abc9d0dc 21499 }
2e276125 21500 else
233d95b5 21501 current_file = macro_include (current_file, line, file_name);
2e276125 21502
233d95b5 21503 xfree (file_name);
6e70227d 21504
2e276125
JB
21505 return current_file;
21506}
21507
21508
21509/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21510 followed by a null byte. */
21511static char *
21512copy_string (const char *buf, int len)
21513{
224c3ddb 21514 char *s = (char *) xmalloc (len + 1);
9a619af0 21515
2e276125
JB
21516 memcpy (s, buf, len);
21517 s[len] = '\0';
2e276125
JB
21518 return s;
21519}
21520
21521
21522static const char *
21523consume_improper_spaces (const char *p, const char *body)
21524{
21525 if (*p == ' ')
21526 {
4d3c2250 21527 complaint (&symfile_complaints,
3e43a32a
MS
21528 _("macro definition contains spaces "
21529 "in formal argument list:\n`%s'"),
4d3c2250 21530 body);
2e276125
JB
21531
21532 while (*p == ' ')
21533 p++;
21534 }
21535
21536 return p;
21537}
21538
21539
21540static void
21541parse_macro_definition (struct macro_source_file *file, int line,
21542 const char *body)
21543{
21544 const char *p;
21545
21546 /* The body string takes one of two forms. For object-like macro
21547 definitions, it should be:
21548
21549 <macro name> " " <definition>
21550
21551 For function-like macro definitions, it should be:
21552
21553 <macro name> "() " <definition>
21554 or
21555 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21556
21557 Spaces may appear only where explicitly indicated, and in the
21558 <definition>.
21559
21560 The Dwarf 2 spec says that an object-like macro's name is always
21561 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21562 the space when the macro's definition is the empty string.
2e276125
JB
21563
21564 The Dwarf 2 spec says that there should be no spaces between the
21565 formal arguments in a function-like macro's formal argument list,
21566 but versions of GCC around March 2002 include spaces after the
21567 commas. */
21568
21569
21570 /* Find the extent of the macro name. The macro name is terminated
21571 by either a space or null character (for an object-like macro) or
21572 an opening paren (for a function-like macro). */
21573 for (p = body; *p; p++)
21574 if (*p == ' ' || *p == '(')
21575 break;
21576
21577 if (*p == ' ' || *p == '\0')
21578 {
21579 /* It's an object-like macro. */
21580 int name_len = p - body;
21581 char *name = copy_string (body, name_len);
21582 const char *replacement;
21583
21584 if (*p == ' ')
21585 replacement = body + name_len + 1;
21586 else
21587 {
4d3c2250 21588 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21589 replacement = body + name_len;
21590 }
6e70227d 21591
2e276125
JB
21592 macro_define_object (file, line, name, replacement);
21593
21594 xfree (name);
21595 }
21596 else if (*p == '(')
21597 {
21598 /* It's a function-like macro. */
21599 char *name = copy_string (body, p - body);
21600 int argc = 0;
21601 int argv_size = 1;
8d749320 21602 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21603
21604 p++;
21605
21606 p = consume_improper_spaces (p, body);
21607
21608 /* Parse the formal argument list. */
21609 while (*p && *p != ')')
21610 {
21611 /* Find the extent of the current argument name. */
21612 const char *arg_start = p;
21613
21614 while (*p && *p != ',' && *p != ')' && *p != ' ')
21615 p++;
21616
21617 if (! *p || p == arg_start)
4d3c2250 21618 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21619 else
21620 {
21621 /* Make sure argv has room for the new argument. */
21622 if (argc >= argv_size)
21623 {
21624 argv_size *= 2;
224c3ddb 21625 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21626 }
21627
21628 argv[argc++] = copy_string (arg_start, p - arg_start);
21629 }
21630
21631 p = consume_improper_spaces (p, body);
21632
21633 /* Consume the comma, if present. */
21634 if (*p == ',')
21635 {
21636 p++;
21637
21638 p = consume_improper_spaces (p, body);
21639 }
21640 }
21641
21642 if (*p == ')')
21643 {
21644 p++;
21645
21646 if (*p == ' ')
21647 /* Perfectly formed definition, no complaints. */
21648 macro_define_function (file, line, name,
6e70227d 21649 argc, (const char **) argv,
2e276125
JB
21650 p + 1);
21651 else if (*p == '\0')
21652 {
21653 /* Complain, but do define it. */
4d3c2250 21654 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21655 macro_define_function (file, line, name,
6e70227d 21656 argc, (const char **) argv,
2e276125
JB
21657 p);
21658 }
21659 else
21660 /* Just complain. */
4d3c2250 21661 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21662 }
21663 else
21664 /* Just complain. */
4d3c2250 21665 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21666
21667 xfree (name);
21668 {
21669 int i;
21670
21671 for (i = 0; i < argc; i++)
21672 xfree (argv[i]);
21673 }
21674 xfree (argv);
21675 }
21676 else
4d3c2250 21677 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21678}
21679
cf2c3c16
TT
21680/* Skip some bytes from BYTES according to the form given in FORM.
21681 Returns the new pointer. */
2e276125 21682
d521ce57
TT
21683static const gdb_byte *
21684skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21685 enum dwarf_form form,
21686 unsigned int offset_size,
21687 struct dwarf2_section_info *section)
2e276125 21688{
cf2c3c16 21689 unsigned int bytes_read;
2e276125 21690
cf2c3c16 21691 switch (form)
2e276125 21692 {
cf2c3c16
TT
21693 case DW_FORM_data1:
21694 case DW_FORM_flag:
21695 ++bytes;
21696 break;
21697
21698 case DW_FORM_data2:
21699 bytes += 2;
21700 break;
21701
21702 case DW_FORM_data4:
21703 bytes += 4;
21704 break;
21705
21706 case DW_FORM_data8:
21707 bytes += 8;
21708 break;
21709
0224619f
JK
21710 case DW_FORM_data16:
21711 bytes += 16;
21712 break;
21713
cf2c3c16
TT
21714 case DW_FORM_string:
21715 read_direct_string (abfd, bytes, &bytes_read);
21716 bytes += bytes_read;
21717 break;
21718
21719 case DW_FORM_sec_offset:
21720 case DW_FORM_strp:
36586728 21721 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21722 bytes += offset_size;
21723 break;
21724
21725 case DW_FORM_block:
21726 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21727 bytes += bytes_read;
21728 break;
21729
21730 case DW_FORM_block1:
21731 bytes += 1 + read_1_byte (abfd, bytes);
21732 break;
21733 case DW_FORM_block2:
21734 bytes += 2 + read_2_bytes (abfd, bytes);
21735 break;
21736 case DW_FORM_block4:
21737 bytes += 4 + read_4_bytes (abfd, bytes);
21738 break;
21739
21740 case DW_FORM_sdata:
21741 case DW_FORM_udata:
3019eac3
DE
21742 case DW_FORM_GNU_addr_index:
21743 case DW_FORM_GNU_str_index:
d521ce57 21744 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21745 if (bytes == NULL)
21746 {
21747 dwarf2_section_buffer_overflow_complaint (section);
21748 return NULL;
21749 }
cf2c3c16
TT
21750 break;
21751
21752 default:
21753 {
21754 complain:
21755 complaint (&symfile_complaints,
21756 _("invalid form 0x%x in `%s'"),
a32a8923 21757 form, get_section_name (section));
cf2c3c16
TT
21758 return NULL;
21759 }
2e276125
JB
21760 }
21761
cf2c3c16
TT
21762 return bytes;
21763}
757a13d0 21764
cf2c3c16
TT
21765/* A helper for dwarf_decode_macros that handles skipping an unknown
21766 opcode. Returns an updated pointer to the macro data buffer; or,
21767 on error, issues a complaint and returns NULL. */
757a13d0 21768
d521ce57 21769static const gdb_byte *
cf2c3c16 21770skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21771 const gdb_byte **opcode_definitions,
21772 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21773 bfd *abfd,
21774 unsigned int offset_size,
21775 struct dwarf2_section_info *section)
21776{
21777 unsigned int bytes_read, i;
21778 unsigned long arg;
d521ce57 21779 const gdb_byte *defn;
2e276125 21780
cf2c3c16 21781 if (opcode_definitions[opcode] == NULL)
2e276125 21782 {
cf2c3c16
TT
21783 complaint (&symfile_complaints,
21784 _("unrecognized DW_MACFINO opcode 0x%x"),
21785 opcode);
21786 return NULL;
21787 }
2e276125 21788
cf2c3c16
TT
21789 defn = opcode_definitions[opcode];
21790 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21791 defn += bytes_read;
2e276125 21792
cf2c3c16
TT
21793 for (i = 0; i < arg; ++i)
21794 {
aead7601
SM
21795 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21796 (enum dwarf_form) defn[i], offset_size,
f664829e 21797 section);
cf2c3c16
TT
21798 if (mac_ptr == NULL)
21799 {
21800 /* skip_form_bytes already issued the complaint. */
21801 return NULL;
21802 }
21803 }
757a13d0 21804
cf2c3c16
TT
21805 return mac_ptr;
21806}
757a13d0 21807
cf2c3c16
TT
21808/* A helper function which parses the header of a macro section.
21809 If the macro section is the extended (for now called "GNU") type,
21810 then this updates *OFFSET_SIZE. Returns a pointer to just after
21811 the header, or issues a complaint and returns NULL on error. */
757a13d0 21812
d521ce57
TT
21813static const gdb_byte *
21814dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21815 bfd *abfd,
d521ce57 21816 const gdb_byte *mac_ptr,
cf2c3c16
TT
21817 unsigned int *offset_size,
21818 int section_is_gnu)
21819{
21820 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21821
cf2c3c16
TT
21822 if (section_is_gnu)
21823 {
21824 unsigned int version, flags;
757a13d0 21825
cf2c3c16 21826 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21827 if (version != 4 && version != 5)
cf2c3c16
TT
21828 {
21829 complaint (&symfile_complaints,
21830 _("unrecognized version `%d' in .debug_macro section"),
21831 version);
21832 return NULL;
21833 }
21834 mac_ptr += 2;
757a13d0 21835
cf2c3c16
TT
21836 flags = read_1_byte (abfd, mac_ptr);
21837 ++mac_ptr;
21838 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21839
cf2c3c16
TT
21840 if ((flags & 2) != 0)
21841 /* We don't need the line table offset. */
21842 mac_ptr += *offset_size;
757a13d0 21843
cf2c3c16
TT
21844 /* Vendor opcode descriptions. */
21845 if ((flags & 4) != 0)
21846 {
21847 unsigned int i, count;
757a13d0 21848
cf2c3c16
TT
21849 count = read_1_byte (abfd, mac_ptr);
21850 ++mac_ptr;
21851 for (i = 0; i < count; ++i)
21852 {
21853 unsigned int opcode, bytes_read;
21854 unsigned long arg;
21855
21856 opcode = read_1_byte (abfd, mac_ptr);
21857 ++mac_ptr;
21858 opcode_definitions[opcode] = mac_ptr;
21859 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21860 mac_ptr += bytes_read;
21861 mac_ptr += arg;
21862 }
757a13d0 21863 }
cf2c3c16 21864 }
757a13d0 21865
cf2c3c16
TT
21866 return mac_ptr;
21867}
757a13d0 21868
cf2c3c16 21869/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21870 including DW_MACRO_import. */
cf2c3c16
TT
21871
21872static void
d521ce57
TT
21873dwarf_decode_macro_bytes (bfd *abfd,
21874 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21875 struct macro_source_file *current_file,
43f3e411 21876 struct line_header *lh,
cf2c3c16 21877 struct dwarf2_section_info *section,
36586728 21878 int section_is_gnu, int section_is_dwz,
cf2c3c16 21879 unsigned int offset_size,
8fc3fc34 21880 htab_t include_hash)
cf2c3c16 21881{
4d663531 21882 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21883 enum dwarf_macro_record_type macinfo_type;
21884 int at_commandline;
d521ce57 21885 const gdb_byte *opcode_definitions[256];
757a13d0 21886
cf2c3c16
TT
21887 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21888 &offset_size, section_is_gnu);
21889 if (mac_ptr == NULL)
21890 {
21891 /* We already issued a complaint. */
21892 return;
21893 }
757a13d0
JK
21894
21895 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21896 GDB is still reading the definitions from command line. First
21897 DW_MACINFO_start_file will need to be ignored as it was already executed
21898 to create CURRENT_FILE for the main source holding also the command line
21899 definitions. On first met DW_MACINFO_start_file this flag is reset to
21900 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21901
21902 at_commandline = 1;
21903
21904 do
21905 {
21906 /* Do we at least have room for a macinfo type byte? */
21907 if (mac_ptr >= mac_end)
21908 {
f664829e 21909 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21910 break;
21911 }
21912
aead7601 21913 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21914 mac_ptr++;
21915
cf2c3c16
TT
21916 /* Note that we rely on the fact that the corresponding GNU and
21917 DWARF constants are the same. */
757a13d0
JK
21918 switch (macinfo_type)
21919 {
21920 /* A zero macinfo type indicates the end of the macro
21921 information. */
21922 case 0:
21923 break;
2e276125 21924
0af92d60
JK
21925 case DW_MACRO_define:
21926 case DW_MACRO_undef:
21927 case DW_MACRO_define_strp:
21928 case DW_MACRO_undef_strp:
21929 case DW_MACRO_define_sup:
21930 case DW_MACRO_undef_sup:
2e276125 21931 {
891d2f0b 21932 unsigned int bytes_read;
2e276125 21933 int line;
d521ce57 21934 const char *body;
cf2c3c16 21935 int is_define;
2e276125 21936
cf2c3c16
TT
21937 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21938 mac_ptr += bytes_read;
21939
0af92d60
JK
21940 if (macinfo_type == DW_MACRO_define
21941 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21942 {
21943 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21944 mac_ptr += bytes_read;
21945 }
21946 else
21947 {
21948 LONGEST str_offset;
21949
21950 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21951 mac_ptr += offset_size;
2e276125 21952
0af92d60
JK
21953 if (macinfo_type == DW_MACRO_define_sup
21954 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21955 || section_is_dwz)
36586728
TT
21956 {
21957 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21958
21959 body = read_indirect_string_from_dwz (dwz, str_offset);
21960 }
21961 else
21962 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21963 }
21964
0af92d60
JK
21965 is_define = (macinfo_type == DW_MACRO_define
21966 || macinfo_type == DW_MACRO_define_strp
21967 || macinfo_type == DW_MACRO_define_sup);
2e276125 21968 if (! current_file)
757a13d0
JK
21969 {
21970 /* DWARF violation as no main source is present. */
21971 complaint (&symfile_complaints,
21972 _("debug info with no main source gives macro %s "
21973 "on line %d: %s"),
cf2c3c16
TT
21974 is_define ? _("definition") : _("undefinition"),
21975 line, body);
757a13d0
JK
21976 break;
21977 }
3e43a32a
MS
21978 if ((line == 0 && !at_commandline)
21979 || (line != 0 && at_commandline))
4d3c2250 21980 complaint (&symfile_complaints,
757a13d0
JK
21981 _("debug info gives %s macro %s with %s line %d: %s"),
21982 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21983 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21984 line == 0 ? _("zero") : _("non-zero"), line, body);
21985
cf2c3c16 21986 if (is_define)
757a13d0 21987 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21988 else
21989 {
0af92d60
JK
21990 gdb_assert (macinfo_type == DW_MACRO_undef
21991 || macinfo_type == DW_MACRO_undef_strp
21992 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
21993 macro_undef (current_file, line, body);
21994 }
2e276125
JB
21995 }
21996 break;
21997
0af92d60 21998 case DW_MACRO_start_file:
2e276125 21999 {
891d2f0b 22000 unsigned int bytes_read;
2e276125
JB
22001 int line, file;
22002
22003 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22004 mac_ptr += bytes_read;
22005 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22006 mac_ptr += bytes_read;
22007
3e43a32a
MS
22008 if ((line == 0 && !at_commandline)
22009 || (line != 0 && at_commandline))
757a13d0
JK
22010 complaint (&symfile_complaints,
22011 _("debug info gives source %d included "
22012 "from %s at %s line %d"),
22013 file, at_commandline ? _("command-line") : _("file"),
22014 line == 0 ? _("zero") : _("non-zero"), line);
22015
22016 if (at_commandline)
22017 {
0af92d60 22018 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22019 pass one. */
757a13d0
JK
22020 at_commandline = 0;
22021 }
22022 else
43f3e411 22023 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22024 }
22025 break;
22026
0af92d60 22027 case DW_MACRO_end_file:
2e276125 22028 if (! current_file)
4d3c2250 22029 complaint (&symfile_complaints,
3e43a32a
MS
22030 _("macro debug info has an unmatched "
22031 "`close_file' directive"));
2e276125
JB
22032 else
22033 {
22034 current_file = current_file->included_by;
22035 if (! current_file)
22036 {
cf2c3c16 22037 enum dwarf_macro_record_type next_type;
2e276125
JB
22038
22039 /* GCC circa March 2002 doesn't produce the zero
22040 type byte marking the end of the compilation
22041 unit. Complain if it's not there, but exit no
22042 matter what. */
22043
22044 /* Do we at least have room for a macinfo type byte? */
22045 if (mac_ptr >= mac_end)
22046 {
f664829e 22047 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22048 return;
22049 }
22050
22051 /* We don't increment mac_ptr here, so this is just
22052 a look-ahead. */
aead7601
SM
22053 next_type
22054 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22055 mac_ptr);
2e276125 22056 if (next_type != 0)
4d3c2250 22057 complaint (&symfile_complaints,
3e43a32a
MS
22058 _("no terminating 0-type entry for "
22059 "macros in `.debug_macinfo' section"));
2e276125
JB
22060
22061 return;
22062 }
22063 }
22064 break;
22065
0af92d60
JK
22066 case DW_MACRO_import:
22067 case DW_MACRO_import_sup:
cf2c3c16
TT
22068 {
22069 LONGEST offset;
8fc3fc34 22070 void **slot;
a036ba48
TT
22071 bfd *include_bfd = abfd;
22072 struct dwarf2_section_info *include_section = section;
d521ce57 22073 const gdb_byte *include_mac_end = mac_end;
a036ba48 22074 int is_dwz = section_is_dwz;
d521ce57 22075 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22076
22077 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22078 mac_ptr += offset_size;
22079
0af92d60 22080 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22081 {
22082 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22083
4d663531 22084 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22085
a036ba48 22086 include_section = &dwz->macro;
a32a8923 22087 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22088 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22089 is_dwz = 1;
22090 }
22091
22092 new_mac_ptr = include_section->buffer + offset;
22093 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22094
8fc3fc34
TT
22095 if (*slot != NULL)
22096 {
22097 /* This has actually happened; see
22098 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22099 complaint (&symfile_complaints,
0af92d60 22100 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22101 ".debug_macro section"));
22102 }
22103 else
22104 {
d521ce57 22105 *slot = (void *) new_mac_ptr;
36586728 22106
a036ba48 22107 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22108 include_mac_end, current_file, lh,
36586728 22109 section, section_is_gnu, is_dwz,
4d663531 22110 offset_size, include_hash);
8fc3fc34 22111
d521ce57 22112 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22113 }
cf2c3c16
TT
22114 }
22115 break;
22116
2e276125 22117 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22118 if (!section_is_gnu)
22119 {
22120 unsigned int bytes_read;
2e276125 22121
ac298888
TT
22122 /* This reads the constant, but since we don't recognize
22123 any vendor extensions, we ignore it. */
22124 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22125 mac_ptr += bytes_read;
22126 read_direct_string (abfd, mac_ptr, &bytes_read);
22127 mac_ptr += bytes_read;
2e276125 22128
cf2c3c16
TT
22129 /* We don't recognize any vendor extensions. */
22130 break;
22131 }
22132 /* FALLTHROUGH */
22133
22134 default:
22135 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22136 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22137 section);
22138 if (mac_ptr == NULL)
22139 return;
22140 break;
2e276125 22141 }
757a13d0 22142 } while (macinfo_type != 0);
2e276125 22143}
8e19ed76 22144
cf2c3c16 22145static void
09262596 22146dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22147 int section_is_gnu)
cf2c3c16 22148{
bb5ed363 22149 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22150 struct line_header *lh = cu->line_header;
22151 bfd *abfd;
d521ce57 22152 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22153 struct macro_source_file *current_file = 0;
22154 enum dwarf_macro_record_type macinfo_type;
22155 unsigned int offset_size = cu->header.offset_size;
d521ce57 22156 const gdb_byte *opcode_definitions[256];
8fc3fc34 22157 struct cleanup *cleanup;
8fc3fc34 22158 void **slot;
09262596
DE
22159 struct dwarf2_section_info *section;
22160 const char *section_name;
22161
22162 if (cu->dwo_unit != NULL)
22163 {
22164 if (section_is_gnu)
22165 {
22166 section = &cu->dwo_unit->dwo_file->sections.macro;
22167 section_name = ".debug_macro.dwo";
22168 }
22169 else
22170 {
22171 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22172 section_name = ".debug_macinfo.dwo";
22173 }
22174 }
22175 else
22176 {
22177 if (section_is_gnu)
22178 {
22179 section = &dwarf2_per_objfile->macro;
22180 section_name = ".debug_macro";
22181 }
22182 else
22183 {
22184 section = &dwarf2_per_objfile->macinfo;
22185 section_name = ".debug_macinfo";
22186 }
22187 }
cf2c3c16 22188
bb5ed363 22189 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22190 if (section->buffer == NULL)
22191 {
fceca515 22192 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22193 return;
22194 }
a32a8923 22195 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22196
22197 /* First pass: Find the name of the base filename.
22198 This filename is needed in order to process all macros whose definition
22199 (or undefinition) comes from the command line. These macros are defined
22200 before the first DW_MACINFO_start_file entry, and yet still need to be
22201 associated to the base file.
22202
22203 To determine the base file name, we scan the macro definitions until we
22204 reach the first DW_MACINFO_start_file entry. We then initialize
22205 CURRENT_FILE accordingly so that any macro definition found before the
22206 first DW_MACINFO_start_file can still be associated to the base file. */
22207
22208 mac_ptr = section->buffer + offset;
22209 mac_end = section->buffer + section->size;
22210
22211 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22212 &offset_size, section_is_gnu);
22213 if (mac_ptr == NULL)
22214 {
22215 /* We already issued a complaint. */
22216 return;
22217 }
22218
22219 do
22220 {
22221 /* Do we at least have room for a macinfo type byte? */
22222 if (mac_ptr >= mac_end)
22223 {
22224 /* Complaint is printed during the second pass as GDB will probably
22225 stop the first pass earlier upon finding
22226 DW_MACINFO_start_file. */
22227 break;
22228 }
22229
aead7601 22230 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22231 mac_ptr++;
22232
22233 /* Note that we rely on the fact that the corresponding GNU and
22234 DWARF constants are the same. */
22235 switch (macinfo_type)
22236 {
22237 /* A zero macinfo type indicates the end of the macro
22238 information. */
22239 case 0:
22240 break;
22241
0af92d60
JK
22242 case DW_MACRO_define:
22243 case DW_MACRO_undef:
cf2c3c16
TT
22244 /* Only skip the data by MAC_PTR. */
22245 {
22246 unsigned int bytes_read;
22247
22248 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22249 mac_ptr += bytes_read;
22250 read_direct_string (abfd, mac_ptr, &bytes_read);
22251 mac_ptr += bytes_read;
22252 }
22253 break;
22254
0af92d60 22255 case DW_MACRO_start_file:
cf2c3c16
TT
22256 {
22257 unsigned int bytes_read;
22258 int line, file;
22259
22260 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22261 mac_ptr += bytes_read;
22262 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22263 mac_ptr += bytes_read;
22264
43f3e411 22265 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22266 }
22267 break;
22268
0af92d60 22269 case DW_MACRO_end_file:
cf2c3c16
TT
22270 /* No data to skip by MAC_PTR. */
22271 break;
22272
0af92d60
JK
22273 case DW_MACRO_define_strp:
22274 case DW_MACRO_undef_strp:
22275 case DW_MACRO_define_sup:
22276 case DW_MACRO_undef_sup:
cf2c3c16
TT
22277 {
22278 unsigned int bytes_read;
22279
22280 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22281 mac_ptr += bytes_read;
22282 mac_ptr += offset_size;
22283 }
22284 break;
22285
0af92d60
JK
22286 case DW_MACRO_import:
22287 case DW_MACRO_import_sup:
cf2c3c16 22288 /* Note that, according to the spec, a transparent include
0af92d60 22289 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22290 skip this opcode. */
22291 mac_ptr += offset_size;
22292 break;
22293
22294 case DW_MACINFO_vendor_ext:
22295 /* Only skip the data by MAC_PTR. */
22296 if (!section_is_gnu)
22297 {
22298 unsigned int bytes_read;
22299
22300 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22301 mac_ptr += bytes_read;
22302 read_direct_string (abfd, mac_ptr, &bytes_read);
22303 mac_ptr += bytes_read;
22304 }
22305 /* FALLTHROUGH */
22306
22307 default:
22308 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22309 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22310 section);
22311 if (mac_ptr == NULL)
22312 return;
22313 break;
22314 }
22315 } while (macinfo_type != 0 && current_file == NULL);
22316
22317 /* Second pass: Process all entries.
22318
22319 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22320 command-line macro definitions/undefinitions. This flag is unset when we
22321 reach the first DW_MACINFO_start_file entry. */
22322
fc4007c9
TT
22323 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22324 htab_eq_pointer,
22325 NULL, xcalloc, xfree));
8fc3fc34 22326 mac_ptr = section->buffer + offset;
fc4007c9 22327 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22328 *slot = (void *) mac_ptr;
8fc3fc34 22329 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22330 current_file, lh, section,
fc4007c9
TT
22331 section_is_gnu, 0, offset_size,
22332 include_hash.get ());
cf2c3c16
TT
22333}
22334
8e19ed76 22335/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22336 if so return true else false. */
380bca97 22337
8e19ed76 22338static int
6e5a29e1 22339attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22340{
22341 return (attr == NULL ? 0 :
22342 attr->form == DW_FORM_block1
22343 || attr->form == DW_FORM_block2
22344 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22345 || attr->form == DW_FORM_block
22346 || attr->form == DW_FORM_exprloc);
8e19ed76 22347}
4c2df51b 22348
c6a0999f
JB
22349/* Return non-zero if ATTR's value is a section offset --- classes
22350 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22351 You may use DW_UNSND (attr) to retrieve such offsets.
22352
22353 Section 7.5.4, "Attribute Encodings", explains that no attribute
22354 may have a value that belongs to more than one of these classes; it
22355 would be ambiguous if we did, because we use the same forms for all
22356 of them. */
380bca97 22357
3690dd37 22358static int
6e5a29e1 22359attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22360{
22361 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22362 || attr->form == DW_FORM_data8
22363 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22364}
22365
3690dd37
JB
22366/* Return non-zero if ATTR's value falls in the 'constant' class, or
22367 zero otherwise. When this function returns true, you can apply
22368 dwarf2_get_attr_constant_value to it.
22369
22370 However, note that for some attributes you must check
22371 attr_form_is_section_offset before using this test. DW_FORM_data4
22372 and DW_FORM_data8 are members of both the constant class, and of
22373 the classes that contain offsets into other debug sections
22374 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22375 that, if an attribute's can be either a constant or one of the
22376 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22377 taken as section offsets, not constants.
22378
22379 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22380 cannot handle that. */
380bca97 22381
3690dd37 22382static int
6e5a29e1 22383attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22384{
22385 switch (attr->form)
22386 {
22387 case DW_FORM_sdata:
22388 case DW_FORM_udata:
22389 case DW_FORM_data1:
22390 case DW_FORM_data2:
22391 case DW_FORM_data4:
22392 case DW_FORM_data8:
22393 return 1;
22394 default:
22395 return 0;
22396 }
22397}
22398
7771576e
SA
22399
22400/* DW_ADDR is always stored already as sect_offset; despite for the forms
22401 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22402
22403static int
6e5a29e1 22404attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22405{
22406 switch (attr->form)
22407 {
22408 case DW_FORM_ref_addr:
22409 case DW_FORM_ref1:
22410 case DW_FORM_ref2:
22411 case DW_FORM_ref4:
22412 case DW_FORM_ref8:
22413 case DW_FORM_ref_udata:
22414 case DW_FORM_GNU_ref_alt:
22415 return 1;
22416 default:
22417 return 0;
22418 }
22419}
22420
3019eac3
DE
22421/* Return the .debug_loc section to use for CU.
22422 For DWO files use .debug_loc.dwo. */
22423
22424static struct dwarf2_section_info *
22425cu_debug_loc_section (struct dwarf2_cu *cu)
22426{
22427 if (cu->dwo_unit)
43988095
JK
22428 {
22429 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22430
22431 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22432 }
22433 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22434 : &dwarf2_per_objfile->loc);
3019eac3
DE
22435}
22436
8cf6f0b1
TT
22437/* A helper function that fills in a dwarf2_loclist_baton. */
22438
22439static void
22440fill_in_loclist_baton (struct dwarf2_cu *cu,
22441 struct dwarf2_loclist_baton *baton,
ff39bb5e 22442 const struct attribute *attr)
8cf6f0b1 22443{
3019eac3
DE
22444 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22445
22446 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22447
22448 baton->per_cu = cu->per_cu;
22449 gdb_assert (baton->per_cu);
22450 /* We don't know how long the location list is, but make sure we
22451 don't run off the edge of the section. */
3019eac3
DE
22452 baton->size = section->size - DW_UNSND (attr);
22453 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22454 baton->base_address = cu->base_address;
f664829e 22455 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22456}
22457
4c2df51b 22458static void
ff39bb5e 22459dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22460 struct dwarf2_cu *cu, int is_block)
4c2df51b 22461{
bb5ed363 22462 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22463 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22464
3690dd37 22465 if (attr_form_is_section_offset (attr)
3019eac3 22466 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22467 the section. If so, fall through to the complaint in the
22468 other branch. */
3019eac3 22469 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22470 {
0d53c4c4 22471 struct dwarf2_loclist_baton *baton;
4c2df51b 22472
8d749320 22473 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22474
8cf6f0b1 22475 fill_in_loclist_baton (cu, baton, attr);
be391dca 22476
d00adf39 22477 if (cu->base_known == 0)
0d53c4c4 22478 complaint (&symfile_complaints,
3e43a32a
MS
22479 _("Location list used without "
22480 "specifying the CU base address."));
4c2df51b 22481
f1e6e072
TT
22482 SYMBOL_ACLASS_INDEX (sym) = (is_block
22483 ? dwarf2_loclist_block_index
22484 : dwarf2_loclist_index);
0d53c4c4
DJ
22485 SYMBOL_LOCATION_BATON (sym) = baton;
22486 }
22487 else
22488 {
22489 struct dwarf2_locexpr_baton *baton;
22490
8d749320 22491 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22492 baton->per_cu = cu->per_cu;
22493 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22494
22495 if (attr_form_is_block (attr))
22496 {
22497 /* Note that we're just copying the block's data pointer
22498 here, not the actual data. We're still pointing into the
6502dd73
DJ
22499 info_buffer for SYM's objfile; right now we never release
22500 that buffer, but when we do clean up properly this may
22501 need to change. */
0d53c4c4
DJ
22502 baton->size = DW_BLOCK (attr)->size;
22503 baton->data = DW_BLOCK (attr)->data;
22504 }
22505 else
22506 {
22507 dwarf2_invalid_attrib_class_complaint ("location description",
22508 SYMBOL_NATURAL_NAME (sym));
22509 baton->size = 0;
0d53c4c4 22510 }
6e70227d 22511
f1e6e072
TT
22512 SYMBOL_ACLASS_INDEX (sym) = (is_block
22513 ? dwarf2_locexpr_block_index
22514 : dwarf2_locexpr_index);
0d53c4c4
DJ
22515 SYMBOL_LOCATION_BATON (sym) = baton;
22516 }
4c2df51b 22517}
6502dd73 22518
9aa1f1e3
TT
22519/* Return the OBJFILE associated with the compilation unit CU. If CU
22520 came from a separate debuginfo file, then the master objfile is
22521 returned. */
ae0d2f24
UW
22522
22523struct objfile *
22524dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22525{
9291a0cd 22526 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22527
22528 /* Return the master objfile, so that we can report and look up the
22529 correct file containing this variable. */
22530 if (objfile->separate_debug_objfile_backlink)
22531 objfile = objfile->separate_debug_objfile_backlink;
22532
22533 return objfile;
22534}
22535
96408a79
SA
22536/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22537 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22538 CU_HEADERP first. */
22539
22540static const struct comp_unit_head *
22541per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22542 struct dwarf2_per_cu_data *per_cu)
22543{
d521ce57 22544 const gdb_byte *info_ptr;
96408a79
SA
22545
22546 if (per_cu->cu)
22547 return &per_cu->cu->header;
22548
9c541725 22549 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22550
22551 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22552 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22553 rcuh_kind::COMPILE);
96408a79
SA
22554
22555 return cu_headerp;
22556}
22557
ae0d2f24
UW
22558/* Return the address size given in the compilation unit header for CU. */
22559
98714339 22560int
ae0d2f24
UW
22561dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22562{
96408a79
SA
22563 struct comp_unit_head cu_header_local;
22564 const struct comp_unit_head *cu_headerp;
c471e790 22565
96408a79
SA
22566 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22567
22568 return cu_headerp->addr_size;
ae0d2f24
UW
22569}
22570
9eae7c52
TT
22571/* Return the offset size given in the compilation unit header for CU. */
22572
22573int
22574dwarf2_per_cu_offset_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;
9c6c53f7 22578
96408a79
SA
22579 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22580
22581 return cu_headerp->offset_size;
22582}
22583
22584/* See its dwarf2loc.h declaration. */
22585
22586int
22587dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22588{
22589 struct comp_unit_head cu_header_local;
22590 const struct comp_unit_head *cu_headerp;
22591
22592 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22593
22594 if (cu_headerp->version == 2)
22595 return cu_headerp->addr_size;
22596 else
22597 return cu_headerp->offset_size;
181cebd4
JK
22598}
22599
9aa1f1e3
TT
22600/* Return the text offset of the CU. The returned offset comes from
22601 this CU's objfile. If this objfile came from a separate debuginfo
22602 file, then the offset may be different from the corresponding
22603 offset in the parent objfile. */
22604
22605CORE_ADDR
22606dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22607{
bb3fa9d0 22608 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22609
22610 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22611}
22612
43988095
JK
22613/* Return DWARF version number of PER_CU. */
22614
22615short
22616dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22617{
22618 return per_cu->dwarf_version;
22619}
22620
348e048f
DE
22621/* Locate the .debug_info compilation unit from CU's objfile which contains
22622 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22623
22624static struct dwarf2_per_cu_data *
9c541725 22625dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22626 unsigned int offset_in_dwz,
ae038cb0
DJ
22627 struct objfile *objfile)
22628{
22629 struct dwarf2_per_cu_data *this_cu;
22630 int low, high;
36586728 22631 const sect_offset *cu_off;
ae038cb0 22632
ae038cb0
DJ
22633 low = 0;
22634 high = dwarf2_per_objfile->n_comp_units - 1;
22635 while (high > low)
22636 {
36586728 22637 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22638 int mid = low + (high - low) / 2;
9a619af0 22639
36586728 22640 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22641 cu_off = &mid_cu->sect_off;
36586728 22642 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22643 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22644 high = mid;
22645 else
22646 low = mid + 1;
22647 }
22648 gdb_assert (low == high);
36586728 22649 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22650 cu_off = &this_cu->sect_off;
22651 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22652 {
36586728 22653 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22654 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22655 "offset 0x%x [in module %s]"),
22656 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22657
9c541725
PA
22658 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22659 <= sect_off);
ae038cb0
DJ
22660 return dwarf2_per_objfile->all_comp_units[low-1];
22661 }
22662 else
22663 {
22664 this_cu = dwarf2_per_objfile->all_comp_units[low];
22665 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22666 && sect_off >= this_cu->sect_off + this_cu->length)
22667 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22668 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22669 return this_cu;
22670 }
22671}
22672
23745b47 22673/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22674
9816fde3 22675static void
23745b47 22676init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22677{
9816fde3 22678 memset (cu, 0, sizeof (*cu));
23745b47
DE
22679 per_cu->cu = cu;
22680 cu->per_cu = per_cu;
22681 cu->objfile = per_cu->objfile;
93311388 22682 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22683}
22684
22685/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22686
22687static void
95554aad
TT
22688prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22689 enum language pretend_language)
9816fde3
JK
22690{
22691 struct attribute *attr;
22692
22693 /* Set the language we're debugging. */
22694 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22695 if (attr)
22696 set_cu_language (DW_UNSND (attr), cu);
22697 else
9cded63f 22698 {
95554aad 22699 cu->language = pretend_language;
9cded63f
TT
22700 cu->language_defn = language_def (cu->language);
22701 }
dee91e82 22702
7d45c7c3 22703 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22704}
22705
ae038cb0
DJ
22706/* Release one cached compilation unit, CU. We unlink it from the tree
22707 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22708 the caller is responsible for that.
22709 NOTE: DATA is a void * because this function is also used as a
22710 cleanup routine. */
ae038cb0
DJ
22711
22712static void
68dc6402 22713free_heap_comp_unit (void *data)
ae038cb0 22714{
9a3c8263 22715 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22716
23745b47
DE
22717 gdb_assert (cu->per_cu != NULL);
22718 cu->per_cu->cu = NULL;
ae038cb0
DJ
22719 cu->per_cu = NULL;
22720
22721 obstack_free (&cu->comp_unit_obstack, NULL);
22722
22723 xfree (cu);
22724}
22725
72bf9492 22726/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22727 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22728 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22729
22730static void
22731free_stack_comp_unit (void *data)
22732{
9a3c8263 22733 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22734
23745b47
DE
22735 gdb_assert (cu->per_cu != NULL);
22736 cu->per_cu->cu = NULL;
22737 cu->per_cu = NULL;
22738
72bf9492
DJ
22739 obstack_free (&cu->comp_unit_obstack, NULL);
22740 cu->partial_dies = NULL;
ae038cb0
DJ
22741}
22742
22743/* Free all cached compilation units. */
22744
22745static void
22746free_cached_comp_units (void *data)
22747{
22748 struct dwarf2_per_cu_data *per_cu, **last_chain;
22749
22750 per_cu = dwarf2_per_objfile->read_in_chain;
22751 last_chain = &dwarf2_per_objfile->read_in_chain;
22752 while (per_cu != NULL)
22753 {
22754 struct dwarf2_per_cu_data *next_cu;
22755
22756 next_cu = per_cu->cu->read_in_chain;
22757
68dc6402 22758 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22759 *last_chain = next_cu;
22760
22761 per_cu = next_cu;
22762 }
22763}
22764
22765/* Increase the age counter on each cached compilation unit, and free
22766 any that are too old. */
22767
22768static void
22769age_cached_comp_units (void)
22770{
22771 struct dwarf2_per_cu_data *per_cu, **last_chain;
22772
22773 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22774 per_cu = dwarf2_per_objfile->read_in_chain;
22775 while (per_cu != NULL)
22776 {
22777 per_cu->cu->last_used ++;
b4f54984 22778 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22779 dwarf2_mark (per_cu->cu);
22780 per_cu = per_cu->cu->read_in_chain;
22781 }
22782
22783 per_cu = dwarf2_per_objfile->read_in_chain;
22784 last_chain = &dwarf2_per_objfile->read_in_chain;
22785 while (per_cu != NULL)
22786 {
22787 struct dwarf2_per_cu_data *next_cu;
22788
22789 next_cu = per_cu->cu->read_in_chain;
22790
22791 if (!per_cu->cu->mark)
22792 {
68dc6402 22793 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22794 *last_chain = next_cu;
22795 }
22796 else
22797 last_chain = &per_cu->cu->read_in_chain;
22798
22799 per_cu = next_cu;
22800 }
22801}
22802
22803/* Remove a single compilation unit from the cache. */
22804
22805static void
dee91e82 22806free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22807{
22808 struct dwarf2_per_cu_data *per_cu, **last_chain;
22809
22810 per_cu = dwarf2_per_objfile->read_in_chain;
22811 last_chain = &dwarf2_per_objfile->read_in_chain;
22812 while (per_cu != NULL)
22813 {
22814 struct dwarf2_per_cu_data *next_cu;
22815
22816 next_cu = per_cu->cu->read_in_chain;
22817
dee91e82 22818 if (per_cu == target_per_cu)
ae038cb0 22819 {
68dc6402 22820 free_heap_comp_unit (per_cu->cu);
dee91e82 22821 per_cu->cu = NULL;
ae038cb0
DJ
22822 *last_chain = next_cu;
22823 break;
22824 }
22825 else
22826 last_chain = &per_cu->cu->read_in_chain;
22827
22828 per_cu = next_cu;
22829 }
22830}
22831
fe3e1990
DJ
22832/* Release all extra memory associated with OBJFILE. */
22833
22834void
22835dwarf2_free_objfile (struct objfile *objfile)
22836{
9a3c8263
SM
22837 dwarf2_per_objfile
22838 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22839 dwarf2_objfile_data_key);
fe3e1990
DJ
22840
22841 if (dwarf2_per_objfile == NULL)
22842 return;
22843
22844 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22845 free_cached_comp_units (NULL);
22846
7b9f3c50
DE
22847 if (dwarf2_per_objfile->quick_file_names_table)
22848 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22849
527f3840
JK
22850 if (dwarf2_per_objfile->line_header_hash)
22851 htab_delete (dwarf2_per_objfile->line_header_hash);
22852
fe3e1990
DJ
22853 /* Everything else should be on the objfile obstack. */
22854}
22855
dee91e82
DE
22856/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22857 We store these in a hash table separate from the DIEs, and preserve them
22858 when the DIEs are flushed out of cache.
22859
22860 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22861 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22862 or the type may come from a DWO file. Furthermore, while it's more logical
22863 to use per_cu->section+offset, with Fission the section with the data is in
22864 the DWO file but we don't know that section at the point we need it.
22865 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22866 because we can enter the lookup routine, get_die_type_at_offset, from
22867 outside this file, and thus won't necessarily have PER_CU->cu.
22868 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22869
dee91e82 22870struct dwarf2_per_cu_offset_and_type
1c379e20 22871{
dee91e82 22872 const struct dwarf2_per_cu_data *per_cu;
9c541725 22873 sect_offset sect_off;
1c379e20
DJ
22874 struct type *type;
22875};
22876
dee91e82 22877/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22878
22879static hashval_t
dee91e82 22880per_cu_offset_and_type_hash (const void *item)
1c379e20 22881{
9a3c8263
SM
22882 const struct dwarf2_per_cu_offset_and_type *ofs
22883 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22884
9c541725 22885 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22886}
22887
dee91e82 22888/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22889
22890static int
dee91e82 22891per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22892{
9a3c8263
SM
22893 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22894 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22895 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22896 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22897
dee91e82 22898 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 22899 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
22900}
22901
22902/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22903 table if necessary. For convenience, return TYPE.
22904
22905 The DIEs reading must have careful ordering to:
22906 * Not cause infite loops trying to read in DIEs as a prerequisite for
22907 reading current DIE.
22908 * Not trying to dereference contents of still incompletely read in types
22909 while reading in other DIEs.
22910 * Enable referencing still incompletely read in types just by a pointer to
22911 the type without accessing its fields.
22912
22913 Therefore caller should follow these rules:
22914 * Try to fetch any prerequisite types we may need to build this DIE type
22915 before building the type and calling set_die_type.
e71ec853 22916 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22917 possible before fetching more types to complete the current type.
22918 * Make the type as complete as possible before fetching more types. */
1c379e20 22919
f792889a 22920static struct type *
1c379e20
DJ
22921set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22922{
dee91e82 22923 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22924 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22925 struct attribute *attr;
22926 struct dynamic_prop prop;
1c379e20 22927
b4ba55a1
JB
22928 /* For Ada types, make sure that the gnat-specific data is always
22929 initialized (if not already set). There are a few types where
22930 we should not be doing so, because the type-specific area is
22931 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22932 where the type-specific area is used to store the floatformat).
22933 But this is not a problem, because the gnat-specific information
22934 is actually not needed for these types. */
22935 if (need_gnat_info (cu)
22936 && TYPE_CODE (type) != TYPE_CODE_FUNC
22937 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22938 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22939 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22940 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22941 && !HAVE_GNAT_AUX_INFO (type))
22942 INIT_GNAT_SPECIFIC (type);
22943
3f2f83dd
KB
22944 /* Read DW_AT_allocated and set in type. */
22945 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22946 if (attr_form_is_block (attr))
22947 {
22948 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22949 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22950 }
22951 else if (attr != NULL)
22952 {
22953 complaint (&symfile_complaints,
9c541725
PA
22954 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22955 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22956 to_underlying (die->sect_off));
3f2f83dd
KB
22957 }
22958
22959 /* Read DW_AT_associated and set in type. */
22960 attr = dwarf2_attr (die, DW_AT_associated, cu);
22961 if (attr_form_is_block (attr))
22962 {
22963 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22964 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22965 }
22966 else if (attr != NULL)
22967 {
22968 complaint (&symfile_complaints,
9c541725
PA
22969 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22970 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22971 to_underlying (die->sect_off));
3f2f83dd
KB
22972 }
22973
3cdcd0ce
JB
22974 /* Read DW_AT_data_location and set in type. */
22975 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22976 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22977 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22978
dee91e82 22979 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22980 {
dee91e82
DE
22981 dwarf2_per_objfile->die_type_hash =
22982 htab_create_alloc_ex (127,
22983 per_cu_offset_and_type_hash,
22984 per_cu_offset_and_type_eq,
22985 NULL,
22986 &objfile->objfile_obstack,
22987 hashtab_obstack_allocate,
22988 dummy_obstack_deallocate);
f792889a 22989 }
1c379e20 22990
dee91e82 22991 ofs.per_cu = cu->per_cu;
9c541725 22992 ofs.sect_off = die->sect_off;
1c379e20 22993 ofs.type = type;
dee91e82
DE
22994 slot = (struct dwarf2_per_cu_offset_and_type **)
22995 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22996 if (*slot)
22997 complaint (&symfile_complaints,
22998 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 22999 to_underlying (die->sect_off));
8d749320
SM
23000 *slot = XOBNEW (&objfile->objfile_obstack,
23001 struct dwarf2_per_cu_offset_and_type);
1c379e20 23002 **slot = ofs;
f792889a 23003 return type;
1c379e20
DJ
23004}
23005
9c541725 23006/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23007 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23008
23009static struct type *
9c541725 23010get_die_type_at_offset (sect_offset sect_off,
673bfd45 23011 struct dwarf2_per_cu_data *per_cu)
1c379e20 23012{
dee91e82 23013 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23014
dee91e82 23015 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23016 return NULL;
1c379e20 23017
dee91e82 23018 ofs.per_cu = per_cu;
9c541725 23019 ofs.sect_off = sect_off;
9a3c8263
SM
23020 slot = ((struct dwarf2_per_cu_offset_and_type *)
23021 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23022 if (slot)
23023 return slot->type;
23024 else
23025 return NULL;
23026}
23027
02142a6c 23028/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23029 or return NULL if DIE does not have a saved type. */
23030
23031static struct type *
23032get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23033{
9c541725 23034 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23035}
23036
10b3939b
DJ
23037/* Add a dependence relationship from CU to REF_PER_CU. */
23038
23039static void
23040dwarf2_add_dependence (struct dwarf2_cu *cu,
23041 struct dwarf2_per_cu_data *ref_per_cu)
23042{
23043 void **slot;
23044
23045 if (cu->dependencies == NULL)
23046 cu->dependencies
23047 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23048 NULL, &cu->comp_unit_obstack,
23049 hashtab_obstack_allocate,
23050 dummy_obstack_deallocate);
23051
23052 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23053 if (*slot == NULL)
23054 *slot = ref_per_cu;
23055}
1c379e20 23056
f504f079
DE
23057/* Subroutine of dwarf2_mark to pass to htab_traverse.
23058 Set the mark field in every compilation unit in the
ae038cb0
DJ
23059 cache that we must keep because we are keeping CU. */
23060
10b3939b
DJ
23061static int
23062dwarf2_mark_helper (void **slot, void *data)
23063{
23064 struct dwarf2_per_cu_data *per_cu;
23065
23066 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23067
23068 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23069 reading of the chain. As such dependencies remain valid it is not much
23070 useful to track and undo them during QUIT cleanups. */
23071 if (per_cu->cu == NULL)
23072 return 1;
23073
10b3939b
DJ
23074 if (per_cu->cu->mark)
23075 return 1;
23076 per_cu->cu->mark = 1;
23077
23078 if (per_cu->cu->dependencies != NULL)
23079 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23080
23081 return 1;
23082}
23083
f504f079
DE
23084/* Set the mark field in CU and in every other compilation unit in the
23085 cache that we must keep because we are keeping CU. */
23086
ae038cb0
DJ
23087static void
23088dwarf2_mark (struct dwarf2_cu *cu)
23089{
23090 if (cu->mark)
23091 return;
23092 cu->mark = 1;
10b3939b
DJ
23093 if (cu->dependencies != NULL)
23094 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23095}
23096
23097static void
23098dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23099{
23100 while (per_cu)
23101 {
23102 per_cu->cu->mark = 0;
23103 per_cu = per_cu->cu->read_in_chain;
23104 }
72bf9492
DJ
23105}
23106
72bf9492
DJ
23107/* Trivial hash function for partial_die_info: the hash value of a DIE
23108 is its offset in .debug_info for this objfile. */
23109
23110static hashval_t
23111partial_die_hash (const void *item)
23112{
9a3c8263
SM
23113 const struct partial_die_info *part_die
23114 = (const struct partial_die_info *) item;
9a619af0 23115
9c541725 23116 return to_underlying (part_die->sect_off);
72bf9492
DJ
23117}
23118
23119/* Trivial comparison function for partial_die_info structures: two DIEs
23120 are equal if they have the same offset. */
23121
23122static int
23123partial_die_eq (const void *item_lhs, const void *item_rhs)
23124{
9a3c8263
SM
23125 const struct partial_die_info *part_die_lhs
23126 = (const struct partial_die_info *) item_lhs;
23127 const struct partial_die_info *part_die_rhs
23128 = (const struct partial_die_info *) item_rhs;
9a619af0 23129
9c541725 23130 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23131}
23132
b4f54984
DE
23133static struct cmd_list_element *set_dwarf_cmdlist;
23134static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23135
23136static void
b4f54984 23137set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23138{
b4f54984 23139 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23140 gdb_stdout);
ae038cb0
DJ
23141}
23142
23143static void
b4f54984 23144show_dwarf_cmd (char *args, int from_tty)
6e70227d 23145{
b4f54984 23146 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23147}
23148
4bf44c1c 23149/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23150
23151static void
c1bd65d0 23152dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23153{
9a3c8263 23154 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23155 int ix;
8b70b953 23156
626f2d1c
TT
23157 /* Make sure we don't accidentally use dwarf2_per_objfile while
23158 cleaning up. */
23159 dwarf2_per_objfile = NULL;
23160
59b0c7c1
JB
23161 for (ix = 0; ix < data->n_comp_units; ++ix)
23162 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23163
59b0c7c1 23164 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23165 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23166 data->all_type_units[ix]->per_cu.imported_symtabs);
23167 xfree (data->all_type_units);
95554aad 23168
8b70b953 23169 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23170
23171 if (data->dwo_files)
23172 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23173 if (data->dwp_file)
23174 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23175
23176 if (data->dwz_file && data->dwz_file->dwz_bfd)
23177 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23178}
23179
23180\f
ae2de4f8 23181/* The "save gdb-index" command. */
9291a0cd
TT
23182
23183/* The contents of the hash table we create when building the string
23184 table. */
23185struct strtab_entry
23186{
23187 offset_type offset;
23188 const char *str;
23189};
23190
559a7a62
JK
23191/* Hash function for a strtab_entry.
23192
23193 Function is used only during write_hash_table so no index format backward
23194 compatibility is needed. */
b89be57b 23195
9291a0cd
TT
23196static hashval_t
23197hash_strtab_entry (const void *e)
23198{
9a3c8263 23199 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 23200 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
23201}
23202
23203/* Equality function for a strtab_entry. */
b89be57b 23204
9291a0cd
TT
23205static int
23206eq_strtab_entry (const void *a, const void *b)
23207{
9a3c8263
SM
23208 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23209 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
23210 return !strcmp (ea->str, eb->str);
23211}
23212
23213/* Create a strtab_entry hash table. */
b89be57b 23214
9291a0cd
TT
23215static htab_t
23216create_strtab (void)
23217{
23218 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23219 xfree, xcalloc, xfree);
23220}
23221
23222/* Add a string to the constant pool. Return the string's offset in
23223 host order. */
b89be57b 23224
9291a0cd
TT
23225static offset_type
23226add_string (htab_t table, struct obstack *cpool, const char *str)
23227{
23228 void **slot;
23229 struct strtab_entry entry;
23230 struct strtab_entry *result;
23231
23232 entry.str = str;
23233 slot = htab_find_slot (table, &entry, INSERT);
23234 if (*slot)
9a3c8263 23235 result = (struct strtab_entry *) *slot;
9291a0cd
TT
23236 else
23237 {
23238 result = XNEW (struct strtab_entry);
23239 result->offset = obstack_object_size (cpool);
23240 result->str = str;
23241 obstack_grow_str0 (cpool, str);
23242 *slot = result;
23243 }
23244 return result->offset;
23245}
23246
23247/* An entry in the symbol table. */
23248struct symtab_index_entry
23249{
23250 /* The name of the symbol. */
23251 const char *name;
23252 /* The offset of the name in the constant pool. */
23253 offset_type index_offset;
23254 /* A sorted vector of the indices of all the CUs that hold an object
23255 of this name. */
23256 VEC (offset_type) *cu_indices;
23257};
23258
23259/* The symbol table. This is a power-of-2-sized hash table. */
23260struct mapped_symtab
23261{
23262 offset_type n_elements;
23263 offset_type size;
23264 struct symtab_index_entry **data;
23265};
23266
23267/* Hash function for a symtab_index_entry. */
b89be57b 23268
9291a0cd
TT
23269static hashval_t
23270hash_symtab_entry (const void *e)
23271{
9a3c8263
SM
23272 const struct symtab_index_entry *entry
23273 = (const struct symtab_index_entry *) e;
9291a0cd
TT
23274 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23275 sizeof (offset_type) * VEC_length (offset_type,
23276 entry->cu_indices),
23277 0);
23278}
23279
23280/* Equality function for a symtab_index_entry. */
b89be57b 23281
9291a0cd
TT
23282static int
23283eq_symtab_entry (const void *a, const void *b)
23284{
9a3c8263
SM
23285 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23286 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
23287 int len = VEC_length (offset_type, ea->cu_indices);
23288 if (len != VEC_length (offset_type, eb->cu_indices))
23289 return 0;
23290 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23291 VEC_address (offset_type, eb->cu_indices),
23292 sizeof (offset_type) * len);
23293}
23294
23295/* Destroy a symtab_index_entry. */
b89be57b 23296
9291a0cd
TT
23297static void
23298delete_symtab_entry (void *p)
23299{
9a3c8263 23300 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
23301 VEC_free (offset_type, entry->cu_indices);
23302 xfree (entry);
23303}
23304
23305/* Create a hash table holding symtab_index_entry objects. */
b89be57b 23306
9291a0cd 23307static htab_t
3876f04e 23308create_symbol_hash_table (void)
9291a0cd
TT
23309{
23310 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23311 delete_symtab_entry, xcalloc, xfree);
23312}
23313
23314/* Create a new mapped symtab object. */
b89be57b 23315
9291a0cd
TT
23316static struct mapped_symtab *
23317create_mapped_symtab (void)
23318{
23319 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23320 symtab->n_elements = 0;
23321 symtab->size = 1024;
23322 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23323 return symtab;
23324}
23325
23326/* Destroy a mapped_symtab. */
b89be57b 23327
9291a0cd
TT
23328static void
23329cleanup_mapped_symtab (void *p)
23330{
9a3c8263 23331 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
23332 /* The contents of the array are freed when the other hash table is
23333 destroyed. */
23334 xfree (symtab->data);
23335 xfree (symtab);
23336}
23337
23338/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
23339 the slot.
23340
23341 Function is used only during write_hash_table so no index format backward
23342 compatibility is needed. */
b89be57b 23343
9291a0cd
TT
23344static struct symtab_index_entry **
23345find_slot (struct mapped_symtab *symtab, const char *name)
23346{
559a7a62 23347 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
23348
23349 index = hash & (symtab->size - 1);
23350 step = ((hash * 17) & (symtab->size - 1)) | 1;
23351
23352 for (;;)
23353 {
23354 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23355 return &symtab->data[index];
23356 index = (index + step) & (symtab->size - 1);
23357 }
23358}
23359
23360/* Expand SYMTAB's hash table. */
b89be57b 23361
9291a0cd
TT
23362static void
23363hash_expand (struct mapped_symtab *symtab)
23364{
23365 offset_type old_size = symtab->size;
23366 offset_type i;
23367 struct symtab_index_entry **old_entries = symtab->data;
23368
23369 symtab->size *= 2;
23370 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23371
23372 for (i = 0; i < old_size; ++i)
23373 {
23374 if (old_entries[i])
23375 {
23376 struct symtab_index_entry **slot = find_slot (symtab,
23377 old_entries[i]->name);
23378 *slot = old_entries[i];
23379 }
23380 }
23381
23382 xfree (old_entries);
23383}
23384
156942c7
DE
23385/* Add an entry to SYMTAB. NAME is the name of the symbol.
23386 CU_INDEX is the index of the CU in which the symbol appears.
23387 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23388
9291a0cd
TT
23389static void
23390add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23391 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23392 offset_type cu_index)
23393{
23394 struct symtab_index_entry **slot;
156942c7 23395 offset_type cu_index_and_attrs;
9291a0cd
TT
23396
23397 ++symtab->n_elements;
23398 if (4 * symtab->n_elements / 3 >= symtab->size)
23399 hash_expand (symtab);
23400
23401 slot = find_slot (symtab, name);
23402 if (!*slot)
23403 {
23404 *slot = XNEW (struct symtab_index_entry);
23405 (*slot)->name = name;
156942c7 23406 /* index_offset is set later. */
9291a0cd
TT
23407 (*slot)->cu_indices = NULL;
23408 }
156942c7
DE
23409
23410 cu_index_and_attrs = 0;
23411 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23412 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23413 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23414
23415 /* We don't want to record an index value twice as we want to avoid the
23416 duplication.
23417 We process all global symbols and then all static symbols
23418 (which would allow us to avoid the duplication by only having to check
23419 the last entry pushed), but a symbol could have multiple kinds in one CU.
23420 To keep things simple we don't worry about the duplication here and
23421 sort and uniqufy the list after we've processed all symbols. */
23422 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23423}
23424
23425/* qsort helper routine for uniquify_cu_indices. */
23426
23427static int
23428offset_type_compare (const void *ap, const void *bp)
23429{
23430 offset_type a = *(offset_type *) ap;
23431 offset_type b = *(offset_type *) bp;
23432
23433 return (a > b) - (b > a);
23434}
23435
23436/* Sort and remove duplicates of all symbols' cu_indices lists. */
23437
23438static void
23439uniquify_cu_indices (struct mapped_symtab *symtab)
23440{
23441 int i;
23442
23443 for (i = 0; i < symtab->size; ++i)
23444 {
23445 struct symtab_index_entry *entry = symtab->data[i];
23446
23447 if (entry
23448 && entry->cu_indices != NULL)
23449 {
23450 unsigned int next_to_insert, next_to_check;
23451 offset_type last_value;
23452
23453 qsort (VEC_address (offset_type, entry->cu_indices),
23454 VEC_length (offset_type, entry->cu_indices),
23455 sizeof (offset_type), offset_type_compare);
23456
23457 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23458 next_to_insert = 1;
23459 for (next_to_check = 1;
23460 next_to_check < VEC_length (offset_type, entry->cu_indices);
23461 ++next_to_check)
23462 {
23463 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23464 != last_value)
23465 {
23466 last_value = VEC_index (offset_type, entry->cu_indices,
23467 next_to_check);
23468 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23469 last_value);
23470 ++next_to_insert;
23471 }
23472 }
23473 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23474 }
23475 }
9291a0cd
TT
23476}
23477
23478/* Add a vector of indices to the constant pool. */
b89be57b 23479
9291a0cd 23480static offset_type
3876f04e 23481add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
23482 struct symtab_index_entry *entry)
23483{
23484 void **slot;
23485
3876f04e 23486 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
23487 if (!*slot)
23488 {
23489 offset_type len = VEC_length (offset_type, entry->cu_indices);
23490 offset_type val = MAYBE_SWAP (len);
23491 offset_type iter;
23492 int i;
23493
23494 *slot = entry;
23495 entry->index_offset = obstack_object_size (cpool);
23496
23497 obstack_grow (cpool, &val, sizeof (val));
23498 for (i = 0;
23499 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23500 ++i)
23501 {
23502 val = MAYBE_SWAP (iter);
23503 obstack_grow (cpool, &val, sizeof (val));
23504 }
23505 }
23506 else
23507 {
9a3c8263
SM
23508 struct symtab_index_entry *old_entry
23509 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
23510 entry->index_offset = old_entry->index_offset;
23511 entry = old_entry;
23512 }
23513 return entry->index_offset;
23514}
23515
23516/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23517 constant pool entries going into the obstack CPOOL. */
b89be57b 23518
9291a0cd
TT
23519static void
23520write_hash_table (struct mapped_symtab *symtab,
23521 struct obstack *output, struct obstack *cpool)
23522{
23523 offset_type i;
3876f04e 23524 htab_t symbol_hash_table;
9291a0cd
TT
23525 htab_t str_table;
23526
3876f04e 23527 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23528 str_table = create_strtab ();
3876f04e 23529
9291a0cd
TT
23530 /* We add all the index vectors to the constant pool first, to
23531 ensure alignment is ok. */
23532 for (i = 0; i < symtab->size; ++i)
23533 {
23534 if (symtab->data[i])
3876f04e 23535 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23536 }
23537
23538 /* Now write out the hash table. */
23539 for (i = 0; i < symtab->size; ++i)
23540 {
23541 offset_type str_off, vec_off;
23542
23543 if (symtab->data[i])
23544 {
23545 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23546 vec_off = symtab->data[i]->index_offset;
23547 }
23548 else
23549 {
23550 /* While 0 is a valid constant pool index, it is not valid
23551 to have 0 for both offsets. */
23552 str_off = 0;
23553 vec_off = 0;
23554 }
23555
23556 str_off = MAYBE_SWAP (str_off);
23557 vec_off = MAYBE_SWAP (vec_off);
23558
23559 obstack_grow (output, &str_off, sizeof (str_off));
23560 obstack_grow (output, &vec_off, sizeof (vec_off));
23561 }
23562
23563 htab_delete (str_table);
3876f04e 23564 htab_delete (symbol_hash_table);
9291a0cd
TT
23565}
23566
0a5429f6
DE
23567/* Struct to map psymtab to CU index in the index file. */
23568struct psymtab_cu_index_map
23569{
23570 struct partial_symtab *psymtab;
23571 unsigned int cu_index;
23572};
23573
23574static hashval_t
23575hash_psymtab_cu_index (const void *item)
23576{
9a3c8263
SM
23577 const struct psymtab_cu_index_map *map
23578 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23579
23580 return htab_hash_pointer (map->psymtab);
23581}
23582
23583static int
23584eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23585{
9a3c8263
SM
23586 const struct psymtab_cu_index_map *lhs
23587 = (const struct psymtab_cu_index_map *) item_lhs;
23588 const struct psymtab_cu_index_map *rhs
23589 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23590
23591 return lhs->psymtab == rhs->psymtab;
23592}
23593
23594/* Helper struct for building the address table. */
23595struct addrmap_index_data
23596{
23597 struct objfile *objfile;
23598 struct obstack *addr_obstack;
23599 htab_t cu_index_htab;
23600
23601 /* Non-zero if the previous_* fields are valid.
23602 We can't write an entry until we see the next entry (since it is only then
23603 that we know the end of the entry). */
23604 int previous_valid;
23605 /* Index of the CU in the table of all CUs in the index file. */
23606 unsigned int previous_cu_index;
0963b4bd 23607 /* Start address of the CU. */
0a5429f6
DE
23608 CORE_ADDR previous_cu_start;
23609};
23610
23611/* Write an address entry to OBSTACK. */
b89be57b 23612
9291a0cd 23613static void
0a5429f6
DE
23614add_address_entry (struct objfile *objfile, struct obstack *obstack,
23615 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23616{
0a5429f6 23617 offset_type cu_index_to_write;
948f8e3d 23618 gdb_byte addr[8];
9291a0cd
TT
23619 CORE_ADDR baseaddr;
23620
23621 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23622
0a5429f6
DE
23623 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23624 obstack_grow (obstack, addr, 8);
23625 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23626 obstack_grow (obstack, addr, 8);
23627 cu_index_to_write = MAYBE_SWAP (cu_index);
23628 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23629}
23630
23631/* Worker function for traversing an addrmap to build the address table. */
23632
23633static int
23634add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23635{
9a3c8263
SM
23636 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23637 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23638
23639 if (data->previous_valid)
23640 add_address_entry (data->objfile, data->addr_obstack,
23641 data->previous_cu_start, start_addr,
23642 data->previous_cu_index);
23643
23644 data->previous_cu_start = start_addr;
23645 if (pst != NULL)
23646 {
23647 struct psymtab_cu_index_map find_map, *map;
23648 find_map.psymtab = pst;
9a3c8263
SM
23649 map = ((struct psymtab_cu_index_map *)
23650 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23651 gdb_assert (map != NULL);
23652 data->previous_cu_index = map->cu_index;
23653 data->previous_valid = 1;
23654 }
23655 else
23656 data->previous_valid = 0;
23657
23658 return 0;
23659}
23660
23661/* Write OBJFILE's address map to OBSTACK.
23662 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23663 in the index file. */
23664
23665static void
23666write_address_map (struct objfile *objfile, struct obstack *obstack,
23667 htab_t cu_index_htab)
23668{
23669 struct addrmap_index_data addrmap_index_data;
23670
23671 /* When writing the address table, we have to cope with the fact that
23672 the addrmap iterator only provides the start of a region; we have to
23673 wait until the next invocation to get the start of the next region. */
23674
23675 addrmap_index_data.objfile = objfile;
23676 addrmap_index_data.addr_obstack = obstack;
23677 addrmap_index_data.cu_index_htab = cu_index_htab;
23678 addrmap_index_data.previous_valid = 0;
23679
23680 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23681 &addrmap_index_data);
23682
23683 /* It's highly unlikely the last entry (end address = 0xff...ff)
23684 is valid, but we should still handle it.
23685 The end address is recorded as the start of the next region, but that
23686 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23687 anyway. */
23688 if (addrmap_index_data.previous_valid)
23689 add_address_entry (objfile, obstack,
23690 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23691 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23692}
23693
156942c7
DE
23694/* Return the symbol kind of PSYM. */
23695
23696static gdb_index_symbol_kind
23697symbol_kind (struct partial_symbol *psym)
23698{
23699 domain_enum domain = PSYMBOL_DOMAIN (psym);
23700 enum address_class aclass = PSYMBOL_CLASS (psym);
23701
23702 switch (domain)
23703 {
23704 case VAR_DOMAIN:
23705 switch (aclass)
23706 {
23707 case LOC_BLOCK:
23708 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23709 case LOC_TYPEDEF:
23710 return GDB_INDEX_SYMBOL_KIND_TYPE;
23711 case LOC_COMPUTED:
23712 case LOC_CONST_BYTES:
23713 case LOC_OPTIMIZED_OUT:
23714 case LOC_STATIC:
23715 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23716 case LOC_CONST:
23717 /* Note: It's currently impossible to recognize psyms as enum values
23718 short of reading the type info. For now punt. */
23719 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23720 default:
23721 /* There are other LOC_FOO values that one might want to classify
23722 as variables, but dwarf2read.c doesn't currently use them. */
23723 return GDB_INDEX_SYMBOL_KIND_OTHER;
23724 }
23725 case STRUCT_DOMAIN:
23726 return GDB_INDEX_SYMBOL_KIND_TYPE;
23727 default:
23728 return GDB_INDEX_SYMBOL_KIND_OTHER;
23729 }
23730}
23731
9291a0cd 23732/* Add a list of partial symbols to SYMTAB. */
b89be57b 23733
9291a0cd
TT
23734static void
23735write_psymbols (struct mapped_symtab *symtab,
987d643c 23736 htab_t psyms_seen,
9291a0cd
TT
23737 struct partial_symbol **psymp,
23738 int count,
987d643c
TT
23739 offset_type cu_index,
23740 int is_static)
9291a0cd
TT
23741{
23742 for (; count-- > 0; ++psymp)
23743 {
156942c7
DE
23744 struct partial_symbol *psym = *psymp;
23745 void **slot;
987d643c 23746
156942c7 23747 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23748 error (_("Ada is not currently supported by the index"));
987d643c 23749
987d643c 23750 /* Only add a given psymbol once. */
156942c7 23751 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23752 if (!*slot)
23753 {
156942c7
DE
23754 gdb_index_symbol_kind kind = symbol_kind (psym);
23755
23756 *slot = psym;
23757 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23758 is_static, kind, cu_index);
987d643c 23759 }
9291a0cd
TT
23760 }
23761}
23762
23763/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23764 exception if there is an error. */
b89be57b 23765
9291a0cd
TT
23766static void
23767write_obstack (FILE *file, struct obstack *obstack)
23768{
23769 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23770 file)
23771 != obstack_object_size (obstack))
23772 error (_("couldn't data write to file"));
23773}
23774
1fd400ff
TT
23775/* A helper struct used when iterating over debug_types. */
23776struct signatured_type_index_data
23777{
23778 struct objfile *objfile;
23779 struct mapped_symtab *symtab;
23780 struct obstack *types_list;
987d643c 23781 htab_t psyms_seen;
1fd400ff
TT
23782 int cu_index;
23783};
23784
23785/* A helper function that writes a single signatured_type to an
23786 obstack. */
b89be57b 23787
1fd400ff
TT
23788static int
23789write_one_signatured_type (void **slot, void *d)
23790{
9a3c8263
SM
23791 struct signatured_type_index_data *info
23792 = (struct signatured_type_index_data *) d;
1fd400ff 23793 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23794 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23795 gdb_byte val[8];
23796
23797 write_psymbols (info->symtab,
987d643c 23798 info->psyms_seen,
3e43a32a
MS
23799 info->objfile->global_psymbols.list
23800 + psymtab->globals_offset,
987d643c
TT
23801 psymtab->n_global_syms, info->cu_index,
23802 0);
1fd400ff 23803 write_psymbols (info->symtab,
987d643c 23804 info->psyms_seen,
3e43a32a
MS
23805 info->objfile->static_psymbols.list
23806 + psymtab->statics_offset,
987d643c
TT
23807 psymtab->n_static_syms, info->cu_index,
23808 1);
1fd400ff 23809
b64f50a1 23810 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23811 to_underlying (entry->per_cu.sect_off));
1fd400ff 23812 obstack_grow (info->types_list, val, 8);
3019eac3 23813 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23814 to_underlying (entry->type_offset_in_tu));
1fd400ff
TT
23815 obstack_grow (info->types_list, val, 8);
23816 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23817 obstack_grow (info->types_list, val, 8);
23818
23819 ++info->cu_index;
23820
23821 return 1;
23822}
23823
95554aad
TT
23824/* Recurse into all "included" dependencies and write their symbols as
23825 if they appeared in this psymtab. */
23826
23827static void
23828recursively_write_psymbols (struct objfile *objfile,
23829 struct partial_symtab *psymtab,
23830 struct mapped_symtab *symtab,
23831 htab_t psyms_seen,
23832 offset_type cu_index)
23833{
23834 int i;
23835
23836 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23837 if (psymtab->dependencies[i]->user != NULL)
23838 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23839 symtab, psyms_seen, cu_index);
23840
23841 write_psymbols (symtab,
23842 psyms_seen,
23843 objfile->global_psymbols.list + psymtab->globals_offset,
23844 psymtab->n_global_syms, cu_index,
23845 0);
23846 write_psymbols (symtab,
23847 psyms_seen,
23848 objfile->static_psymbols.list + psymtab->statics_offset,
23849 psymtab->n_static_syms, cu_index,
23850 1);
23851}
23852
9291a0cd 23853/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23854
9291a0cd
TT
23855static void
23856write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23857{
23858 struct cleanup *cleanup;
bef155c3 23859 char *filename;
1fd400ff
TT
23860 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23861 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23862 int i;
23863 FILE *out_file;
23864 struct mapped_symtab *symtab;
23865 offset_type val, size_of_contents, total_len;
23866 struct stat st;
0a5429f6 23867 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23868
9291a0cd
TT
23869 if (dwarf2_per_objfile->using_index)
23870 error (_("Cannot use an index to create the index"));
23871
8b70b953
TT
23872 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23873 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23874
260b681b
DE
23875 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23876 return;
23877
4262abfb
JK
23878 if (stat (objfile_name (objfile), &st) < 0)
23879 perror_with_name (objfile_name (objfile));
9291a0cd 23880
4262abfb 23881 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23882 INDEX_SUFFIX, (char *) NULL);
23883 cleanup = make_cleanup (xfree, filename);
23884
614c279d 23885 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23886 if (!out_file)
23887 error (_("Can't open `%s' for writing"), filename);
23888
bef155c3 23889 gdb::unlinker unlink_file (filename);
9291a0cd
TT
23890
23891 symtab = create_mapped_symtab ();
23892 make_cleanup (cleanup_mapped_symtab, symtab);
23893
23894 obstack_init (&addr_obstack);
23895 make_cleanup_obstack_free (&addr_obstack);
23896
23897 obstack_init (&cu_list);
23898 make_cleanup_obstack_free (&cu_list);
23899
1fd400ff
TT
23900 obstack_init (&types_cu_list);
23901 make_cleanup_obstack_free (&types_cu_list);
23902
fc4007c9
TT
23903 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23904 htab_eq_pointer,
23905 NULL, xcalloc, xfree));
987d643c 23906
0a5429f6
DE
23907 /* While we're scanning CU's create a table that maps a psymtab pointer
23908 (which is what addrmap records) to its index (which is what is recorded
23909 in the index file). This will later be needed to write the address
23910 table. */
fc4007c9
TT
23911 htab_up cu_index_htab (htab_create_alloc (100,
23912 hash_psymtab_cu_index,
23913 eq_psymtab_cu_index,
23914 NULL, xcalloc, xfree));
8d749320
SM
23915 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23916 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23917 make_cleanup (xfree, psymtab_cu_index_map);
23918
23919 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23920 work here. Also, the debug_types entries do not appear in
23921 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23922 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23923 {
3e43a32a
MS
23924 struct dwarf2_per_cu_data *per_cu
23925 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23926 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23927 gdb_byte val[8];
0a5429f6
DE
23928 struct psymtab_cu_index_map *map;
23929 void **slot;
9291a0cd 23930
92fac807
JK
23931 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23932 It may be referenced from a local scope but in such case it does not
23933 need to be present in .gdb_index. */
23934 if (psymtab == NULL)
23935 continue;
23936
95554aad 23937 if (psymtab->user == NULL)
fc4007c9
TT
23938 recursively_write_psymbols (objfile, psymtab, symtab,
23939 psyms_seen.get (), i);
9291a0cd 23940
0a5429f6
DE
23941 map = &psymtab_cu_index_map[i];
23942 map->psymtab = psymtab;
23943 map->cu_index = i;
fc4007c9 23944 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23945 gdb_assert (slot != NULL);
23946 gdb_assert (*slot == NULL);
23947 *slot = map;
9291a0cd 23948
b64f50a1 23949 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
9c541725 23950 to_underlying (per_cu->sect_off));
9291a0cd 23951 obstack_grow (&cu_list, val, 8);
e254ef6a 23952 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23953 obstack_grow (&cu_list, val, 8);
23954 }
23955
0a5429f6 23956 /* Dump the address map. */
fc4007c9 23957 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23958
1fd400ff
TT
23959 /* Write out the .debug_type entries, if any. */
23960 if (dwarf2_per_objfile->signatured_types)
23961 {
23962 struct signatured_type_index_data sig_data;
23963
23964 sig_data.objfile = objfile;
23965 sig_data.symtab = symtab;
23966 sig_data.types_list = &types_cu_list;
fc4007c9 23967 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
23968 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23969 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23970 write_one_signatured_type, &sig_data);
23971 }
23972
156942c7
DE
23973 /* Now that we've processed all symbols we can shrink their cu_indices
23974 lists. */
23975 uniquify_cu_indices (symtab);
23976
9291a0cd
TT
23977 obstack_init (&constant_pool);
23978 make_cleanup_obstack_free (&constant_pool);
23979 obstack_init (&symtab_obstack);
23980 make_cleanup_obstack_free (&symtab_obstack);
23981 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23982
23983 obstack_init (&contents);
23984 make_cleanup_obstack_free (&contents);
1fd400ff 23985 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23986 total_len = size_of_contents;
23987
23988 /* The version number. */
796a7ff8 23989 val = MAYBE_SWAP (8);
9291a0cd
TT
23990 obstack_grow (&contents, &val, sizeof (val));
23991
23992 /* The offset of the CU list from the start of the file. */
23993 val = MAYBE_SWAP (total_len);
23994 obstack_grow (&contents, &val, sizeof (val));
23995 total_len += obstack_object_size (&cu_list);
23996
1fd400ff
TT
23997 /* The offset of the types CU list from the start of the file. */
23998 val = MAYBE_SWAP (total_len);
23999 obstack_grow (&contents, &val, sizeof (val));
24000 total_len += obstack_object_size (&types_cu_list);
24001
9291a0cd
TT
24002 /* The offset of the address table from the start of the file. */
24003 val = MAYBE_SWAP (total_len);
24004 obstack_grow (&contents, &val, sizeof (val));
24005 total_len += obstack_object_size (&addr_obstack);
24006
24007 /* The offset of the symbol table from the start of the file. */
24008 val = MAYBE_SWAP (total_len);
24009 obstack_grow (&contents, &val, sizeof (val));
24010 total_len += obstack_object_size (&symtab_obstack);
24011
24012 /* The offset of the constant pool from the start of the file. */
24013 val = MAYBE_SWAP (total_len);
24014 obstack_grow (&contents, &val, sizeof (val));
24015 total_len += obstack_object_size (&constant_pool);
24016
24017 gdb_assert (obstack_object_size (&contents) == size_of_contents);
24018
24019 write_obstack (out_file, &contents);
24020 write_obstack (out_file, &cu_list);
1fd400ff 24021 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
24022 write_obstack (out_file, &addr_obstack);
24023 write_obstack (out_file, &symtab_obstack);
24024 write_obstack (out_file, &constant_pool);
24025
24026 fclose (out_file);
24027
bef155c3
TT
24028 /* We want to keep the file. */
24029 unlink_file.keep ();
9291a0cd
TT
24030
24031 do_cleanups (cleanup);
24032}
24033
90476074
TT
24034/* Implementation of the `save gdb-index' command.
24035
24036 Note that the file format used by this command is documented in the
24037 GDB manual. Any changes here must be documented there. */
11570e71 24038
9291a0cd
TT
24039static void
24040save_gdb_index_command (char *arg, int from_tty)
24041{
24042 struct objfile *objfile;
24043
24044 if (!arg || !*arg)
96d19272 24045 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24046
24047 ALL_OBJFILES (objfile)
24048 {
24049 struct stat st;
24050
24051 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24052 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24053 continue;
24054
9a3c8263
SM
24055 dwarf2_per_objfile
24056 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24057 dwarf2_objfile_data_key);
9291a0cd
TT
24058 if (dwarf2_per_objfile)
24059 {
9291a0cd 24060
492d29ea 24061 TRY
9291a0cd
TT
24062 {
24063 write_psymtabs_to_index (objfile, arg);
24064 }
492d29ea
PA
24065 CATCH (except, RETURN_MASK_ERROR)
24066 {
24067 exception_fprintf (gdb_stderr, except,
24068 _("Error while writing index for `%s': "),
24069 objfile_name (objfile));
24070 }
24071 END_CATCH
9291a0cd
TT
24072 }
24073 }
dce234bc
PP
24074}
24075
9291a0cd
TT
24076\f
24077
b4f54984 24078int dwarf_always_disassemble;
9eae7c52
TT
24079
24080static void
b4f54984
DE
24081show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24082 struct cmd_list_element *c, const char *value)
9eae7c52 24083{
3e43a32a
MS
24084 fprintf_filtered (file,
24085 _("Whether to always disassemble "
24086 "DWARF expressions is %s.\n"),
9eae7c52
TT
24087 value);
24088}
24089
900e11f9
JK
24090static void
24091show_check_physname (struct ui_file *file, int from_tty,
24092 struct cmd_list_element *c, const char *value)
24093{
24094 fprintf_filtered (file,
24095 _("Whether to check \"physname\" is %s.\n"),
24096 value);
24097}
24098
6502dd73
DJ
24099void _initialize_dwarf2_read (void);
24100
24101void
24102_initialize_dwarf2_read (void)
24103{
96d19272
JK
24104 struct cmd_list_element *c;
24105
dce234bc 24106 dwarf2_objfile_data_key
c1bd65d0 24107 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24108
b4f54984
DE
24109 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24110Set DWARF specific variables.\n\
24111Configure DWARF variables such as the cache size"),
24112 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24113 0/*allow-unknown*/, &maintenance_set_cmdlist);
24114
b4f54984
DE
24115 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24116Show DWARF specific variables\n\
24117Show DWARF variables such as the cache size"),
24118 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24119 0/*allow-unknown*/, &maintenance_show_cmdlist);
24120
24121 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24122 &dwarf_max_cache_age, _("\
24123Set the upper bound on the age of cached DWARF compilation units."), _("\
24124Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24125A higher limit means that cached compilation units will be stored\n\
24126in memory longer, and more total memory will be used. Zero disables\n\
24127caching, which can slow down startup."),
2c5b56ce 24128 NULL,
b4f54984
DE
24129 show_dwarf_max_cache_age,
24130 &set_dwarf_cmdlist,
24131 &show_dwarf_cmdlist);
d97bc12b 24132
9eae7c52 24133 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24134 &dwarf_always_disassemble, _("\
9eae7c52
TT
24135Set whether `info address' always disassembles DWARF expressions."), _("\
24136Show whether `info address' always disassembles DWARF expressions."), _("\
24137When enabled, DWARF expressions are always printed in an assembly-like\n\
24138syntax. When disabled, expressions will be printed in a more\n\
24139conversational style, when possible."),
24140 NULL,
b4f54984
DE
24141 show_dwarf_always_disassemble,
24142 &set_dwarf_cmdlist,
24143 &show_dwarf_cmdlist);
24144
24145 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24146Set debugging of the DWARF reader."), _("\
24147Show debugging of the DWARF reader."), _("\
24148When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24149reading and symtab expansion. A value of 1 (one) provides basic\n\
24150information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24151 NULL,
24152 NULL,
24153 &setdebuglist, &showdebuglist);
24154
b4f54984
DE
24155 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24156Set debugging of the DWARF DIE reader."), _("\
24157Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24158When enabled (non-zero), DIEs are dumped after they are read in.\n\
24159The value is the maximum depth to print."),
ccce17b0
YQ
24160 NULL,
24161 NULL,
24162 &setdebuglist, &showdebuglist);
9291a0cd 24163
27e0867f
DE
24164 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24165Set debugging of the dwarf line reader."), _("\
24166Show debugging of the dwarf line reader."), _("\
24167When enabled (non-zero), line number entries are dumped as they are read in.\n\
24168A value of 1 (one) provides basic information.\n\
24169A value greater than 1 provides more verbose information."),
24170 NULL,
24171 NULL,
24172 &setdebuglist, &showdebuglist);
24173
900e11f9
JK
24174 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24175Set cross-checking of \"physname\" code against demangler."), _("\
24176Show cross-checking of \"physname\" code against demangler."), _("\
24177When enabled, GDB's internal \"physname\" code is checked against\n\
24178the demangler."),
24179 NULL, show_check_physname,
24180 &setdebuglist, &showdebuglist);
24181
e615022a
DE
24182 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24183 no_class, &use_deprecated_index_sections, _("\
24184Set whether to use deprecated gdb_index sections."), _("\
24185Show whether to use deprecated gdb_index sections."), _("\
24186When enabled, deprecated .gdb_index sections are used anyway.\n\
24187Normally they are ignored either because of a missing feature or\n\
24188performance issue.\n\
24189Warning: This option must be enabled before gdb reads the file."),
24190 NULL,
24191 NULL,
24192 &setlist, &showlist);
24193
96d19272 24194 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24195 _("\
fc1a9d6e 24196Save a gdb-index file.\n\
11570e71 24197Usage: save gdb-index DIRECTORY"),
96d19272
JK
24198 &save_cmdlist);
24199 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24200
24201 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24202 &dwarf2_locexpr_funcs);
24203 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24204 &dwarf2_loclist_funcs);
24205
24206 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24207 &dwarf2_block_frame_base_locexpr_funcs);
24208 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24209 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24210}
This page took 3.969339 seconds and 4 git commands to generate.