Eliminate make_cleanup_obstack_free, introduce auto_obstack
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
4c2df51b 77
c906108c 78#include <fcntl.h>
c906108c 79#include <sys/types.h>
325fac50 80#include <algorithm>
bc8f2430
JK
81#include <unordered_set>
82#include <unordered_map>
d8151005 83
34eaf542
TT
84typedef struct symbol *symbolp;
85DEF_VEC_P (symbolp);
86
73be47f5
DE
87/* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
b4f54984
DE
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90static unsigned int dwarf_read_debug = 0;
45cfd468 91
d97bc12b 92/* When non-zero, dump DIEs after they are read in. */
b4f54984 93static unsigned int dwarf_die_debug = 0;
d97bc12b 94
27e0867f
DE
95/* When non-zero, dump line number entries as they are read in. */
96static unsigned int dwarf_line_debug = 0;
97
900e11f9
JK
98/* When non-zero, cross-check physname against demangler. */
99static int check_physname = 0;
100
481860b3 101/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 102static int use_deprecated_index_sections = 0;
481860b3 103
6502dd73
DJ
104static const struct objfile_data *dwarf2_objfile_data_key;
105
f1e6e072
TT
106/* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108static int dwarf2_locexpr_index;
109static int dwarf2_loclist_index;
110static int dwarf2_locexpr_block_index;
111static int dwarf2_loclist_block_index;
112
73869dc2
DE
113/* A descriptor for dwarf sections.
114
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
119
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
124 input section.
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
128
dce234bc
PP
129struct dwarf2_section_info
130{
73869dc2
DE
131 union
132 {
e5aa3347 133 /* If this is a real section, the bfd section. */
049412e3 134 asection *section;
73869dc2 135 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 136 section. */
73869dc2
DE
137 struct dwarf2_section_info *containing_section;
138 } s;
19ac8c2e 139 /* Pointer to section data, only valid if readin. */
d521ce57 140 const gdb_byte *buffer;
73869dc2 141 /* The size of the section, real or virtual. */
dce234bc 142 bfd_size_type size;
73869dc2
DE
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
be391dca 146 /* True if we have tried to read this section. */
73869dc2
DE
147 char readin;
148 /* True if this is a virtual section, False otherwise.
049412e3 149 This specifies which of s.section and s.containing_section to use. */
73869dc2 150 char is_virtual;
dce234bc
PP
151};
152
8b70b953
TT
153typedef struct dwarf2_section_info dwarf2_section_info_def;
154DEF_VEC_O (dwarf2_section_info_def);
155
9291a0cd
TT
156/* All offsets in the index are of this type. It must be
157 architecture-independent. */
158typedef uint32_t offset_type;
159
160DEF_VEC_I (offset_type);
161
156942c7
DE
162/* Ensure only legit values are used. */
163#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 do { \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
167 } while (0)
168
169/* Ensure only legit values are used. */
170#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 do { \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
175 } while (0)
176
177/* Ensure we don't use more than the alloted nuber of bits for the CU. */
178#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 do { \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
182 } while (0)
183
9291a0cd
TT
184/* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186struct mapped_index
187{
559a7a62
JK
188 /* Index data format version. */
189 int version;
190
9291a0cd
TT
191 /* The total length of the buffer. */
192 off_t total_size;
b11b1f88 193
9291a0cd
TT
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
b11b1f88 196
9291a0cd
TT
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
b11b1f88 199
3876f04e
DE
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
b11b1f88 202
9291a0cd 203 /* Size in slots, each slot is 2 offset_types. */
3876f04e 204 offset_type symbol_table_slots;
b11b1f88 205
9291a0cd
TT
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
208};
209
95554aad
TT
210typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211DEF_VEC_P (dwarf2_per_cu_ptr);
212
52059ffd
TT
213struct tu_stats
214{
215 int nr_uniq_abbrev_tables;
216 int nr_symtabs;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
220};
221
9cdd5dbd
DE
222/* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
224
6502dd73
DJ
225struct dwarf2_per_objfile
226{
dce234bc
PP
227 struct dwarf2_section_info info;
228 struct dwarf2_section_info abbrev;
229 struct dwarf2_section_info line;
dce234bc 230 struct dwarf2_section_info loc;
43988095 231 struct dwarf2_section_info loclists;
dce234bc 232 struct dwarf2_section_info macinfo;
cf2c3c16 233 struct dwarf2_section_info macro;
dce234bc 234 struct dwarf2_section_info str;
43988095 235 struct dwarf2_section_info line_str;
dce234bc 236 struct dwarf2_section_info ranges;
43988095 237 struct dwarf2_section_info rnglists;
3019eac3 238 struct dwarf2_section_info addr;
dce234bc
PP
239 struct dwarf2_section_info frame;
240 struct dwarf2_section_info eh_frame;
9291a0cd 241 struct dwarf2_section_info gdb_index;
ae038cb0 242
8b70b953
TT
243 VEC (dwarf2_section_info_def) *types;
244
be391dca
TT
245 /* Back link. */
246 struct objfile *objfile;
247
d467dd73 248 /* Table of all the compilation units. This is used to locate
10b3939b 249 the target compilation unit of a particular reference. */
ae038cb0
DJ
250 struct dwarf2_per_cu_data **all_comp_units;
251
252 /* The number of compilation units in ALL_COMP_UNITS. */
253 int n_comp_units;
254
1fd400ff 255 /* The number of .debug_types-related CUs. */
d467dd73 256 int n_type_units;
1fd400ff 257
6aa5f3a6
DE
258 /* The number of elements allocated in all_type_units.
259 If there are skeleton-less TUs, we add them to all_type_units lazily. */
260 int n_allocated_type_units;
261
a2ce51a0
DE
262 /* The .debug_types-related CUs (TUs).
263 This is stored in malloc space because we may realloc it. */
b4dd5633 264 struct signatured_type **all_type_units;
1fd400ff 265
f4dc4d17
DE
266 /* Table of struct type_unit_group objects.
267 The hash key is the DW_AT_stmt_list value. */
268 htab_t type_unit_groups;
72dca2f5 269
348e048f
DE
270 /* A table mapping .debug_types signatures to its signatured_type entry.
271 This is NULL if the .debug_types section hasn't been read in yet. */
272 htab_t signatured_types;
273
f4dc4d17
DE
274 /* Type unit statistics, to see how well the scaling improvements
275 are doing. */
52059ffd 276 struct tu_stats tu_stats;
f4dc4d17
DE
277
278 /* A chain of compilation units that are currently read in, so that
279 they can be freed later. */
280 struct dwarf2_per_cu_data *read_in_chain;
281
3019eac3
DE
282 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
283 This is NULL if the table hasn't been allocated yet. */
284 htab_t dwo_files;
285
80626a55
DE
286 /* Non-zero if we've check for whether there is a DWP file. */
287 int dwp_checked;
288
289 /* The DWP file if there is one, or NULL. */
290 struct dwp_file *dwp_file;
291
36586728
TT
292 /* The shared '.dwz' file, if one exists. This is used when the
293 original data was compressed using 'dwz -m'. */
294 struct dwz_file *dwz_file;
295
72dca2f5
FR
296 /* A flag indicating wether this objfile has a section loaded at a
297 VMA of 0. */
298 int has_section_at_zero;
9291a0cd 299
ae2de4f8
DE
300 /* True if we are using the mapped index,
301 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
302 unsigned char using_index;
303
ae2de4f8 304 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 305 struct mapped_index *index_table;
98bfdba5 306
7b9f3c50 307 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
308 TUs typically share line table entries with a CU, so we maintain a
309 separate table of all line table entries to support the sharing.
310 Note that while there can be way more TUs than CUs, we've already
311 sorted all the TUs into "type unit groups", grouped by their
312 DW_AT_stmt_list value. Therefore the only sharing done here is with a
313 CU and its associated TU group if there is one. */
7b9f3c50
DE
314 htab_t quick_file_names_table;
315
98bfdba5
PA
316 /* Set during partial symbol reading, to prevent queueing of full
317 symbols. */
318 int reading_partial_symbols;
673bfd45 319
dee91e82 320 /* Table mapping type DIEs to their struct type *.
673bfd45 321 This is NULL if not allocated yet.
02142a6c 322 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 323 htab_t die_type_hash;
95554aad
TT
324
325 /* The CUs we recently read. */
326 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
327
328 /* Table containing line_header indexed by offset and offset_in_dwz. */
329 htab_t line_header_hash;
6502dd73
DJ
330};
331
332static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 333
251d32d9 334/* Default names of the debugging sections. */
c906108c 335
233a11ab
CS
336/* Note that if the debugging section has been compressed, it might
337 have a name like .zdebug_info. */
338
9cdd5dbd
DE
339static const struct dwarf2_debug_sections dwarf2_elf_names =
340{
251d32d9
TG
341 { ".debug_info", ".zdebug_info" },
342 { ".debug_abbrev", ".zdebug_abbrev" },
343 { ".debug_line", ".zdebug_line" },
344 { ".debug_loc", ".zdebug_loc" },
43988095 345 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 346 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 347 { ".debug_macro", ".zdebug_macro" },
251d32d9 348 { ".debug_str", ".zdebug_str" },
43988095 349 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 350 { ".debug_ranges", ".zdebug_ranges" },
43988095 351 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 352 { ".debug_types", ".zdebug_types" },
3019eac3 353 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
354 { ".debug_frame", ".zdebug_frame" },
355 { ".eh_frame", NULL },
24d3216f
TT
356 { ".gdb_index", ".zgdb_index" },
357 23
251d32d9 358};
c906108c 359
80626a55 360/* List of DWO/DWP sections. */
3019eac3 361
80626a55 362static const struct dwop_section_names
3019eac3
DE
363{
364 struct dwarf2_section_names abbrev_dwo;
365 struct dwarf2_section_names info_dwo;
366 struct dwarf2_section_names line_dwo;
367 struct dwarf2_section_names loc_dwo;
43988095 368 struct dwarf2_section_names loclists_dwo;
09262596
DE
369 struct dwarf2_section_names macinfo_dwo;
370 struct dwarf2_section_names macro_dwo;
3019eac3
DE
371 struct dwarf2_section_names str_dwo;
372 struct dwarf2_section_names str_offsets_dwo;
373 struct dwarf2_section_names types_dwo;
80626a55
DE
374 struct dwarf2_section_names cu_index;
375 struct dwarf2_section_names tu_index;
3019eac3 376}
80626a55 377dwop_section_names =
3019eac3
DE
378{
379 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
380 { ".debug_info.dwo", ".zdebug_info.dwo" },
381 { ".debug_line.dwo", ".zdebug_line.dwo" },
382 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 383 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
384 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
385 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
386 { ".debug_str.dwo", ".zdebug_str.dwo" },
387 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
388 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
389 { ".debug_cu_index", ".zdebug_cu_index" },
390 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
391};
392
c906108c
SS
393/* local data types */
394
107d2387
AC
395/* The data in a compilation unit header, after target2host
396 translation, looks like this. */
c906108c 397struct comp_unit_head
a738430d 398{
c764a876 399 unsigned int length;
a738430d 400 short version;
a738430d
MK
401 unsigned char addr_size;
402 unsigned char signed_addr_p;
9c541725 403 sect_offset abbrev_sect_off;
57349743 404
a738430d
MK
405 /* Size of file offsets; either 4 or 8. */
406 unsigned int offset_size;
57349743 407
a738430d
MK
408 /* Size of the length field; either 4 or 12. */
409 unsigned int initial_length_size;
57349743 410
43988095
JK
411 enum dwarf_unit_type unit_type;
412
a738430d
MK
413 /* Offset to the first byte of this compilation unit header in the
414 .debug_info section, for resolving relative reference dies. */
9c541725 415 sect_offset sect_off;
57349743 416
d00adf39
DE
417 /* Offset to first die in this cu from the start of the cu.
418 This will be the first byte following the compilation unit header. */
9c541725 419 cu_offset first_die_cu_offset;
43988095
JK
420
421 /* 64-bit signature of this type unit - it is valid only for
422 UNIT_TYPE DW_UT_type. */
423 ULONGEST signature;
424
425 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 426 cu_offset type_cu_offset_in_tu;
a738430d 427};
c906108c 428
3da10d80
KS
429/* Type used for delaying computation of method physnames.
430 See comments for compute_delayed_physnames. */
431struct delayed_method_info
432{
433 /* The type to which the method is attached, i.e., its parent class. */
434 struct type *type;
435
436 /* The index of the method in the type's function fieldlists. */
437 int fnfield_index;
438
439 /* The index of the method in the fieldlist. */
440 int index;
441
442 /* The name of the DIE. */
443 const char *name;
444
445 /* The DIE associated with this method. */
446 struct die_info *die;
447};
448
449typedef struct delayed_method_info delayed_method_info;
450DEF_VEC_O (delayed_method_info);
451
e7c27a73
DJ
452/* Internal state when decoding a particular compilation unit. */
453struct dwarf2_cu
454{
455 /* The objfile containing this compilation unit. */
456 struct objfile *objfile;
457
d00adf39 458 /* The header of the compilation unit. */
e7c27a73 459 struct comp_unit_head header;
e142c38c 460
d00adf39
DE
461 /* Base address of this compilation unit. */
462 CORE_ADDR base_address;
463
464 /* Non-zero if base_address has been set. */
465 int base_known;
466
e142c38c
DJ
467 /* The language we are debugging. */
468 enum language language;
469 const struct language_defn *language_defn;
470
b0f35d58
DL
471 const char *producer;
472
e142c38c
DJ
473 /* The generic symbol table building routines have separate lists for
474 file scope symbols and all all other scopes (local scopes). So
475 we need to select the right one to pass to add_symbol_to_list().
476 We do it by keeping a pointer to the correct list in list_in_scope.
477
478 FIXME: The original dwarf code just treated the file scope as the
479 first local scope, and all other local scopes as nested local
480 scopes, and worked fine. Check to see if we really need to
481 distinguish these in buildsym.c. */
482 struct pending **list_in_scope;
483
433df2d4
DE
484 /* The abbrev table for this CU.
485 Normally this points to the abbrev table in the objfile.
486 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
487 struct abbrev_table *abbrev_table;
72bf9492 488
b64f50a1
JK
489 /* Hash table holding all the loaded partial DIEs
490 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
491 htab_t partial_dies;
492
493 /* Storage for things with the same lifetime as this read-in compilation
494 unit, including partial DIEs. */
495 struct obstack comp_unit_obstack;
496
ae038cb0
DJ
497 /* When multiple dwarf2_cu structures are living in memory, this field
498 chains them all together, so that they can be released efficiently.
499 We will probably also want a generation counter so that most-recently-used
500 compilation units are cached... */
501 struct dwarf2_per_cu_data *read_in_chain;
502
69d751e3 503 /* Backlink to our per_cu entry. */
ae038cb0
DJ
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* How many compilation units ago was this CU last referenced? */
507 int last_used;
508
b64f50a1
JK
509 /* A hash table of DIE cu_offset for following references with
510 die_info->offset.sect_off as hash. */
51545339 511 htab_t die_hash;
10b3939b
DJ
512
513 /* Full DIEs if read in. */
514 struct die_info *dies;
515
516 /* A set of pointers to dwarf2_per_cu_data objects for compilation
517 units referenced by this one. Only set during full symbol processing;
518 partial symbol tables do not have dependencies. */
519 htab_t dependencies;
520
cb1df416
DJ
521 /* Header data from the line table, during full symbol processing. */
522 struct line_header *line_header;
523
3da10d80
KS
524 /* A list of methods which need to have physnames computed
525 after all type information has been read. */
526 VEC (delayed_method_info) *method_list;
527
96408a79
SA
528 /* To be copied to symtab->call_site_htab. */
529 htab_t call_site_htab;
530
034e5797
DE
531 /* Non-NULL if this CU came from a DWO file.
532 There is an invariant here that is important to remember:
533 Except for attributes copied from the top level DIE in the "main"
534 (or "stub") file in preparation for reading the DWO file
535 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
536 Either there isn't a DWO file (in which case this is NULL and the point
537 is moot), or there is and either we're not going to read it (in which
538 case this is NULL) or there is and we are reading it (in which case this
539 is non-NULL). */
3019eac3
DE
540 struct dwo_unit *dwo_unit;
541
542 /* The DW_AT_addr_base attribute if present, zero otherwise
543 (zero is a valid value though).
1dbab08b 544 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
545 ULONGEST addr_base;
546
2e3cf129
DE
547 /* The DW_AT_ranges_base attribute if present, zero otherwise
548 (zero is a valid value though).
1dbab08b 549 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 550 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
551 be used without needing to know whether DWO files are in use or not.
552 N.B. This does not apply to DW_AT_ranges appearing in
553 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
554 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
555 DW_AT_ranges_base *would* have to be applied, and we'd have to care
556 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
557 ULONGEST ranges_base;
558
ae038cb0
DJ
559 /* Mark used when releasing cached dies. */
560 unsigned int mark : 1;
561
8be455d7
JK
562 /* This CU references .debug_loc. See the symtab->locations_valid field.
563 This test is imperfect as there may exist optimized debug code not using
564 any location list and still facing inlining issues if handled as
565 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 566 unsigned int has_loclist : 1;
ba919b58 567
1b80a9fa
JK
568 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
569 if all the producer_is_* fields are valid. This information is cached
570 because profiling CU expansion showed excessive time spent in
571 producer_is_gxx_lt_4_6. */
ba919b58
TT
572 unsigned int checked_producer : 1;
573 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 574 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 575 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
576
577 /* When set, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
582};
583
10b3939b
DJ
584/* Persistent data held for a compilation unit, even when not
585 processing it. We put a pointer to this structure in the
28dee7f5 586 read_symtab_private field of the psymtab. */
10b3939b 587
ae038cb0
DJ
588struct dwarf2_per_cu_data
589{
36586728 590 /* The start offset and length of this compilation unit.
45452591 591 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
592 initial_length_size.
593 If the DIE refers to a DWO file, this is always of the original die,
594 not the DWO file. */
9c541725 595 sect_offset sect_off;
36586728 596 unsigned int length;
ae038cb0 597
43988095
JK
598 /* DWARF standard version this data has been read from (such as 4 or 5). */
599 short dwarf_version;
600
ae038cb0
DJ
601 /* Flag indicating this compilation unit will be read in before
602 any of the current compilation units are processed. */
c764a876 603 unsigned int queued : 1;
ae038cb0 604
0d99eb77
DE
605 /* This flag will be set when reading partial DIEs if we need to load
606 absolutely all DIEs for this compilation unit, instead of just the ones
607 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
608 hash table and don't find it. */
609 unsigned int load_all_dies : 1;
610
0186c6a7
DE
611 /* Non-zero if this CU is from .debug_types.
612 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
613 this is non-zero. */
3019eac3
DE
614 unsigned int is_debug_types : 1;
615
36586728
TT
616 /* Non-zero if this CU is from the .dwz file. */
617 unsigned int is_dwz : 1;
618
a2ce51a0
DE
619 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
620 This flag is only valid if is_debug_types is true.
621 We can't read a CU directly from a DWO file: There are required
622 attributes in the stub. */
623 unsigned int reading_dwo_directly : 1;
624
7ee85ab1
DE
625 /* Non-zero if the TU has been read.
626 This is used to assist the "Stay in DWO Optimization" for Fission:
627 When reading a DWO, it's faster to read TUs from the DWO instead of
628 fetching them from random other DWOs (due to comdat folding).
629 If the TU has already been read, the optimization is unnecessary
630 (and unwise - we don't want to change where gdb thinks the TU lives
631 "midflight").
632 This flag is only valid if is_debug_types is true. */
633 unsigned int tu_read : 1;
634
3019eac3
DE
635 /* The section this CU/TU lives in.
636 If the DIE refers to a DWO file, this is always the original die,
637 not the DWO file. */
8a0459fd 638 struct dwarf2_section_info *section;
348e048f 639
17ea53c3 640 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
641 of the CU cache it gets reset to NULL again. This is left as NULL for
642 dummy CUs (a CU header, but nothing else). */
ae038cb0 643 struct dwarf2_cu *cu;
1c379e20 644
9cdd5dbd
DE
645 /* The corresponding objfile.
646 Normally we can get the objfile from dwarf2_per_objfile.
647 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
648 struct objfile *objfile;
649
fffbe6a8
YQ
650 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
651 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
652 union
653 {
654 /* The partial symbol table associated with this compilation unit,
95554aad 655 or NULL for unread partial units. */
9291a0cd
TT
656 struct partial_symtab *psymtab;
657
658 /* Data needed by the "quick" functions. */
659 struct dwarf2_per_cu_quick_data *quick;
660 } v;
95554aad 661
796a7ff8
DE
662 /* The CUs we import using DW_TAG_imported_unit. This is filled in
663 while reading psymtabs, used to compute the psymtab dependencies,
664 and then cleared. Then it is filled in again while reading full
665 symbols, and only deleted when the objfile is destroyed.
666
667 This is also used to work around a difference between the way gold
668 generates .gdb_index version <=7 and the way gdb does. Arguably this
669 is a gold bug. For symbols coming from TUs, gold records in the index
670 the CU that includes the TU instead of the TU itself. This breaks
671 dw2_lookup_symbol: It assumes that if the index says symbol X lives
672 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
673 will find X. Alas TUs live in their own symtab, so after expanding CU Y
674 we need to look in TU Z to find X. Fortunately, this is akin to
675 DW_TAG_imported_unit, so we just use the same mechanism: For
676 .gdb_index version <=7 this also records the TUs that the CU referred
677 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
678 indices so we only pay a price for gold generated indices.
679 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 680 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
681};
682
348e048f
DE
683/* Entry in the signatured_types hash table. */
684
685struct signatured_type
686{
42e7ad6c 687 /* The "per_cu" object of this type.
ac9ec31b 688 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
689 N.B.: This is the first member so that it's easy to convert pointers
690 between them. */
691 struct dwarf2_per_cu_data per_cu;
692
3019eac3 693 /* The type's signature. */
348e048f
DE
694 ULONGEST signature;
695
3019eac3 696 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
697 If this TU is a DWO stub and the definition lives in a DWO file
698 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
699 cu_offset type_offset_in_tu;
700
701 /* Offset in the section of the type's DIE.
702 If the definition lives in a DWO file, this is the offset in the
703 .debug_types.dwo section.
704 The value is zero until the actual value is known.
705 Zero is otherwise not a valid section offset. */
706 sect_offset type_offset_in_section;
0186c6a7
DE
707
708 /* Type units are grouped by their DW_AT_stmt_list entry so that they
709 can share them. This points to the containing symtab. */
710 struct type_unit_group *type_unit_group;
ac9ec31b
DE
711
712 /* The type.
713 The first time we encounter this type we fully read it in and install it
714 in the symbol tables. Subsequent times we only need the type. */
715 struct type *type;
a2ce51a0
DE
716
717 /* Containing DWO unit.
718 This field is valid iff per_cu.reading_dwo_directly. */
719 struct dwo_unit *dwo_unit;
348e048f
DE
720};
721
0186c6a7
DE
722typedef struct signatured_type *sig_type_ptr;
723DEF_VEC_P (sig_type_ptr);
724
094b34ac
DE
725/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
726 This includes type_unit_group and quick_file_names. */
727
728struct stmt_list_hash
729{
730 /* The DWO unit this table is from or NULL if there is none. */
731 struct dwo_unit *dwo_unit;
732
733 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 734 sect_offset line_sect_off;
094b34ac
DE
735};
736
f4dc4d17
DE
737/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
738 an object of this type. */
739
740struct type_unit_group
741{
0186c6a7 742 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
743 To simplify things we create an artificial CU that "includes" all the
744 type units using this stmt_list so that the rest of the code still has
745 a "per_cu" handle on the symtab.
746 This PER_CU is recognized by having no section. */
8a0459fd 747#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
748 struct dwarf2_per_cu_data per_cu;
749
0186c6a7
DE
750 /* The TUs that share this DW_AT_stmt_list entry.
751 This is added to while parsing type units to build partial symtabs,
752 and is deleted afterwards and not used again. */
753 VEC (sig_type_ptr) *tus;
f4dc4d17 754
43f3e411 755 /* The compunit symtab.
094b34ac 756 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
757 so we create an essentially anonymous symtab as the compunit symtab. */
758 struct compunit_symtab *compunit_symtab;
f4dc4d17 759
094b34ac
DE
760 /* The data used to construct the hash key. */
761 struct stmt_list_hash hash;
f4dc4d17
DE
762
763 /* The number of symtabs from the line header.
764 The value here must match line_header.num_file_names. */
765 unsigned int num_symtabs;
766
767 /* The symbol tables for this TU (obtained from the files listed in
768 DW_AT_stmt_list).
769 WARNING: The order of entries here must match the order of entries
770 in the line header. After the first TU using this type_unit_group, the
771 line header for the subsequent TUs is recreated from this. This is done
772 because we need to use the same symtabs for each TU using the same
773 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
774 there's no guarantee the line header doesn't have duplicate entries. */
775 struct symtab **symtabs;
776};
777
73869dc2 778/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
779
780struct dwo_sections
781{
782 struct dwarf2_section_info abbrev;
3019eac3
DE
783 struct dwarf2_section_info line;
784 struct dwarf2_section_info loc;
43988095 785 struct dwarf2_section_info loclists;
09262596
DE
786 struct dwarf2_section_info macinfo;
787 struct dwarf2_section_info macro;
3019eac3
DE
788 struct dwarf2_section_info str;
789 struct dwarf2_section_info str_offsets;
80626a55
DE
790 /* In the case of a virtual DWO file, these two are unused. */
791 struct dwarf2_section_info info;
3019eac3
DE
792 VEC (dwarf2_section_info_def) *types;
793};
794
c88ee1f0 795/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
796
797struct dwo_unit
798{
799 /* Backlink to the containing struct dwo_file. */
800 struct dwo_file *dwo_file;
801
802 /* The "id" that distinguishes this CU/TU.
803 .debug_info calls this "dwo_id", .debug_types calls this "signature".
804 Since signatures came first, we stick with it for consistency. */
805 ULONGEST signature;
806
807 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 808 struct dwarf2_section_info *section;
3019eac3 809
9c541725
PA
810 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
811 sect_offset sect_off;
3019eac3
DE
812 unsigned int length;
813
814 /* For types, offset in the type's DIE of the type defined by this TU. */
815 cu_offset type_offset_in_tu;
816};
817
73869dc2
DE
818/* include/dwarf2.h defines the DWP section codes.
819 It defines a max value but it doesn't define a min value, which we
820 use for error checking, so provide one. */
821
822enum dwp_v2_section_ids
823{
824 DW_SECT_MIN = 1
825};
826
80626a55 827/* Data for one DWO file.
57d63ce2
DE
828
829 This includes virtual DWO files (a virtual DWO file is a DWO file as it
830 appears in a DWP file). DWP files don't really have DWO files per se -
831 comdat folding of types "loses" the DWO file they came from, and from
832 a high level view DWP files appear to contain a mass of random types.
833 However, to maintain consistency with the non-DWP case we pretend DWP
834 files contain virtual DWO files, and we assign each TU with one virtual
835 DWO file (generally based on the line and abbrev section offsets -
836 a heuristic that seems to work in practice). */
3019eac3
DE
837
838struct dwo_file
839{
0ac5b59e 840 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
841 For virtual DWO files the name is constructed from the section offsets
842 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
843 from related CU+TUs. */
0ac5b59e
DE
844 const char *dwo_name;
845
846 /* The DW_AT_comp_dir attribute. */
847 const char *comp_dir;
3019eac3 848
80626a55
DE
849 /* The bfd, when the file is open. Otherwise this is NULL.
850 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
851 bfd *dbfd;
3019eac3 852
73869dc2
DE
853 /* The sections that make up this DWO file.
854 Remember that for virtual DWO files in DWP V2, these are virtual
855 sections (for lack of a better name). */
3019eac3
DE
856 struct dwo_sections sections;
857
19c3d4c9
DE
858 /* The CU in the file.
859 We only support one because having more than one requires hacking the
860 dwo_name of each to match, which is highly unlikely to happen.
861 Doing this means all TUs can share comp_dir: We also assume that
862 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
863 struct dwo_unit *cu;
3019eac3
DE
864
865 /* Table of TUs in the file.
866 Each element is a struct dwo_unit. */
867 htab_t tus;
868};
869
80626a55
DE
870/* These sections are what may appear in a DWP file. */
871
872struct dwp_sections
873{
73869dc2 874 /* These are used by both DWP version 1 and 2. */
80626a55
DE
875 struct dwarf2_section_info str;
876 struct dwarf2_section_info cu_index;
877 struct dwarf2_section_info tu_index;
73869dc2
DE
878
879 /* These are only used by DWP version 2 files.
880 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
881 sections are referenced by section number, and are not recorded here.
882 In DWP version 2 there is at most one copy of all these sections, each
883 section being (effectively) comprised of the concatenation of all of the
884 individual sections that exist in the version 1 format.
885 To keep the code simple we treat each of these concatenated pieces as a
886 section itself (a virtual section?). */
887 struct dwarf2_section_info abbrev;
888 struct dwarf2_section_info info;
889 struct dwarf2_section_info line;
890 struct dwarf2_section_info loc;
891 struct dwarf2_section_info macinfo;
892 struct dwarf2_section_info macro;
893 struct dwarf2_section_info str_offsets;
894 struct dwarf2_section_info types;
80626a55
DE
895};
896
73869dc2
DE
897/* These sections are what may appear in a virtual DWO file in DWP version 1.
898 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 899
73869dc2 900struct virtual_v1_dwo_sections
80626a55
DE
901{
902 struct dwarf2_section_info abbrev;
903 struct dwarf2_section_info line;
904 struct dwarf2_section_info loc;
905 struct dwarf2_section_info macinfo;
906 struct dwarf2_section_info macro;
907 struct dwarf2_section_info str_offsets;
908 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 909 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
910 struct dwarf2_section_info info_or_types;
911};
912
73869dc2
DE
913/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
914 In version 2, the sections of the DWO files are concatenated together
915 and stored in one section of that name. Thus each ELF section contains
916 several "virtual" sections. */
917
918struct virtual_v2_dwo_sections
919{
920 bfd_size_type abbrev_offset;
921 bfd_size_type abbrev_size;
922
923 bfd_size_type line_offset;
924 bfd_size_type line_size;
925
926 bfd_size_type loc_offset;
927 bfd_size_type loc_size;
928
929 bfd_size_type macinfo_offset;
930 bfd_size_type macinfo_size;
931
932 bfd_size_type macro_offset;
933 bfd_size_type macro_size;
934
935 bfd_size_type str_offsets_offset;
936 bfd_size_type str_offsets_size;
937
938 /* Each DWP hash table entry records one CU or one TU.
939 That is recorded here, and copied to dwo_unit.section. */
940 bfd_size_type info_or_types_offset;
941 bfd_size_type info_or_types_size;
942};
943
80626a55
DE
944/* Contents of DWP hash tables. */
945
946struct dwp_hash_table
947{
73869dc2 948 uint32_t version, nr_columns;
80626a55 949 uint32_t nr_units, nr_slots;
73869dc2
DE
950 const gdb_byte *hash_table, *unit_table;
951 union
952 {
953 struct
954 {
955 const gdb_byte *indices;
956 } v1;
957 struct
958 {
959 /* This is indexed by column number and gives the id of the section
960 in that column. */
961#define MAX_NR_V2_DWO_SECTIONS \
962 (1 /* .debug_info or .debug_types */ \
963 + 1 /* .debug_abbrev */ \
964 + 1 /* .debug_line */ \
965 + 1 /* .debug_loc */ \
966 + 1 /* .debug_str_offsets */ \
967 + 1 /* .debug_macro or .debug_macinfo */)
968 int section_ids[MAX_NR_V2_DWO_SECTIONS];
969 const gdb_byte *offsets;
970 const gdb_byte *sizes;
971 } v2;
972 } section_pool;
80626a55
DE
973};
974
975/* Data for one DWP file. */
976
977struct dwp_file
978{
979 /* Name of the file. */
980 const char *name;
981
73869dc2
DE
982 /* File format version. */
983 int version;
984
93417882 985 /* The bfd. */
80626a55
DE
986 bfd *dbfd;
987
988 /* Section info for this file. */
989 struct dwp_sections sections;
990
57d63ce2 991 /* Table of CUs in the file. */
80626a55
DE
992 const struct dwp_hash_table *cus;
993
994 /* Table of TUs in the file. */
995 const struct dwp_hash_table *tus;
996
19ac8c2e
DE
997 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
998 htab_t loaded_cus;
999 htab_t loaded_tus;
80626a55 1000
73869dc2
DE
1001 /* Table to map ELF section numbers to their sections.
1002 This is only needed for the DWP V1 file format. */
80626a55
DE
1003 unsigned int num_sections;
1004 asection **elf_sections;
1005};
1006
36586728
TT
1007/* This represents a '.dwz' file. */
1008
1009struct dwz_file
1010{
1011 /* A dwz file can only contain a few sections. */
1012 struct dwarf2_section_info abbrev;
1013 struct dwarf2_section_info info;
1014 struct dwarf2_section_info str;
1015 struct dwarf2_section_info line;
1016 struct dwarf2_section_info macro;
2ec9a5e0 1017 struct dwarf2_section_info gdb_index;
36586728
TT
1018
1019 /* The dwz's BFD. */
1020 bfd *dwz_bfd;
1021};
1022
0963b4bd
MS
1023/* Struct used to pass misc. parameters to read_die_and_children, et
1024 al. which are used for both .debug_info and .debug_types dies.
1025 All parameters here are unchanging for the life of the call. This
dee91e82 1026 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1027
1028struct die_reader_specs
1029{
a32a8923 1030 /* The bfd of die_section. */
93311388
DE
1031 bfd* abfd;
1032
1033 /* The CU of the DIE we are parsing. */
1034 struct dwarf2_cu *cu;
1035
80626a55 1036 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1037 struct dwo_file *dwo_file;
1038
dee91e82 1039 /* The section the die comes from.
3019eac3 1040 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1041 struct dwarf2_section_info *die_section;
1042
1043 /* die_section->buffer. */
d521ce57 1044 const gdb_byte *buffer;
f664829e
DE
1045
1046 /* The end of the buffer. */
1047 const gdb_byte *buffer_end;
a2ce51a0
DE
1048
1049 /* The value of the DW_AT_comp_dir attribute. */
1050 const char *comp_dir;
93311388
DE
1051};
1052
fd820528 1053/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1054typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1055 const gdb_byte *info_ptr,
dee91e82
DE
1056 struct die_info *comp_unit_die,
1057 int has_children,
1058 void *data);
1059
ecfb656c
PA
1060/* A 1-based directory index. This is a strong typedef to prevent
1061 accidentally using a directory index as a 0-based index into an
1062 array/vector. */
1063enum class dir_index : unsigned int {};
1064
1065/* Likewise, a 1-based file name index. */
1066enum class file_name_index : unsigned int {};
1067
52059ffd
TT
1068struct file_entry
1069{
fff8551c
PA
1070 file_entry () = default;
1071
ecfb656c 1072 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1073 unsigned int mod_time_, unsigned int length_)
1074 : name (name_),
ecfb656c 1075 d_index (d_index_),
fff8551c
PA
1076 mod_time (mod_time_),
1077 length (length_)
1078 {}
1079
ecfb656c
PA
1080 /* Return the include directory at D_INDEX stored in LH. Returns
1081 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1082 const char *include_dir (const line_header *lh) const;
1083
fff8551c
PA
1084 /* The file name. Note this is an observing pointer. The memory is
1085 owned by debug_line_buffer. */
1086 const char *name {};
1087
8c43009f 1088 /* The directory index (1-based). */
ecfb656c 1089 dir_index d_index {};
fff8551c
PA
1090
1091 unsigned int mod_time {};
1092
1093 unsigned int length {};
1094
1095 /* True if referenced by the Line Number Program. */
1096 bool included_p {};
1097
83769d0b 1098 /* The associated symbol table, if any. */
fff8551c 1099 struct symtab *symtab {};
52059ffd
TT
1100};
1101
debd256d
JB
1102/* The line number information for a compilation unit (found in the
1103 .debug_line section) begins with a "statement program header",
1104 which contains the following information. */
1105struct line_header
1106{
fff8551c
PA
1107 line_header ()
1108 : offset_in_dwz {}
1109 {}
1110
1111 /* Add an entry to the include directory table. */
1112 void add_include_dir (const char *include_dir);
1113
1114 /* Add an entry to the file name table. */
ecfb656c 1115 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1116 unsigned int mod_time, unsigned int length);
1117
ecfb656c 1118 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1119 is out of bounds. */
ecfb656c 1120 const char *include_dir_at (dir_index index) const
8c43009f 1121 {
ecfb656c
PA
1122 /* Convert directory index number (1-based) to vector index
1123 (0-based). */
1124 size_t vec_index = to_underlying (index) - 1;
1125
1126 if (vec_index >= include_dirs.size ())
8c43009f 1127 return NULL;
ecfb656c 1128 return include_dirs[vec_index];
8c43009f
PA
1129 }
1130
ecfb656c 1131 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1132 is out of bounds. */
ecfb656c 1133 file_entry *file_name_at (file_name_index index)
8c43009f 1134 {
ecfb656c
PA
1135 /* Convert file name index number (1-based) to vector index
1136 (0-based). */
1137 size_t vec_index = to_underlying (index) - 1;
1138
1139 if (vec_index >= file_names.size ())
fff8551c 1140 return NULL;
ecfb656c 1141 return &file_names[vec_index];
fff8551c
PA
1142 }
1143
1144 /* Const version of the above. */
1145 const file_entry *file_name_at (unsigned int index) const
1146 {
1147 if (index >= file_names.size ())
8c43009f
PA
1148 return NULL;
1149 return &file_names[index];
1150 }
1151
527f3840 1152 /* Offset of line number information in .debug_line section. */
9c541725 1153 sect_offset sect_off {};
527f3840
JK
1154
1155 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1156 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1157
1158 unsigned int total_length {};
1159 unsigned short version {};
1160 unsigned int header_length {};
1161 unsigned char minimum_instruction_length {};
1162 unsigned char maximum_ops_per_instruction {};
1163 unsigned char default_is_stmt {};
1164 int line_base {};
1165 unsigned char line_range {};
1166 unsigned char opcode_base {};
debd256d
JB
1167
1168 /* standard_opcode_lengths[i] is the number of operands for the
1169 standard opcode whose value is i. This means that
1170 standard_opcode_lengths[0] is unused, and the last meaningful
1171 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1172 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1173
fff8551c
PA
1174 /* The include_directories table. Note these are observing
1175 pointers. The memory is owned by debug_line_buffer. */
1176 std::vector<const char *> include_dirs;
debd256d 1177
fff8551c
PA
1178 /* The file_names table. */
1179 std::vector<file_entry> file_names;
debd256d
JB
1180
1181 /* The start and end of the statement program following this
6502dd73 1182 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1183 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1184};
c906108c 1185
fff8551c
PA
1186typedef std::unique_ptr<line_header> line_header_up;
1187
8c43009f
PA
1188const char *
1189file_entry::include_dir (const line_header *lh) const
1190{
ecfb656c 1191 return lh->include_dir_at (d_index);
8c43009f
PA
1192}
1193
c906108c 1194/* When we construct a partial symbol table entry we only
0963b4bd 1195 need this much information. */
c906108c
SS
1196struct partial_die_info
1197 {
72bf9492 1198 /* Offset of this DIE. */
9c541725 1199 sect_offset sect_off;
72bf9492
DJ
1200
1201 /* DWARF-2 tag for this DIE. */
1202 ENUM_BITFIELD(dwarf_tag) tag : 16;
1203
72bf9492
DJ
1204 /* Assorted flags describing the data found in this DIE. */
1205 unsigned int has_children : 1;
1206 unsigned int is_external : 1;
1207 unsigned int is_declaration : 1;
1208 unsigned int has_type : 1;
1209 unsigned int has_specification : 1;
1210 unsigned int has_pc_info : 1;
481860b3 1211 unsigned int may_be_inlined : 1;
72bf9492 1212
0c1b455e
TT
1213 /* This DIE has been marked DW_AT_main_subprogram. */
1214 unsigned int main_subprogram : 1;
1215
72bf9492
DJ
1216 /* Flag set if the SCOPE field of this structure has been
1217 computed. */
1218 unsigned int scope_set : 1;
1219
fa4028e9
JB
1220 /* Flag set if the DIE has a byte_size attribute. */
1221 unsigned int has_byte_size : 1;
1222
ff908ebf
AW
1223 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1224 unsigned int has_const_value : 1;
1225
98bfdba5
PA
1226 /* Flag set if any of the DIE's children are template arguments. */
1227 unsigned int has_template_arguments : 1;
1228
abc72ce4
DE
1229 /* Flag set if fixup_partial_die has been called on this die. */
1230 unsigned int fixup_called : 1;
1231
36586728
TT
1232 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1233 unsigned int is_dwz : 1;
1234
1235 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1236 unsigned int spec_is_dwz : 1;
1237
72bf9492 1238 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1239 sometimes a default name for unnamed DIEs. */
15d034d0 1240 const char *name;
72bf9492 1241
abc72ce4
DE
1242 /* The linkage name, if present. */
1243 const char *linkage_name;
1244
72bf9492
DJ
1245 /* The scope to prepend to our children. This is generally
1246 allocated on the comp_unit_obstack, so will disappear
1247 when this compilation unit leaves the cache. */
15d034d0 1248 const char *scope;
72bf9492 1249
95554aad
TT
1250 /* Some data associated with the partial DIE. The tag determines
1251 which field is live. */
1252 union
1253 {
1254 /* The location description associated with this DIE, if any. */
1255 struct dwarf_block *locdesc;
1256 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1257 sect_offset sect_off;
95554aad 1258 } d;
72bf9492
DJ
1259
1260 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1261 CORE_ADDR lowpc;
1262 CORE_ADDR highpc;
72bf9492 1263
93311388 1264 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1265 DW_AT_sibling, if any. */
abc72ce4
DE
1266 /* NOTE: This member isn't strictly necessary, read_partial_die could
1267 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1268 const gdb_byte *sibling;
72bf9492
DJ
1269
1270 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1271 DW_AT_specification (or DW_AT_abstract_origin or
1272 DW_AT_extension). */
b64f50a1 1273 sect_offset spec_offset;
72bf9492
DJ
1274
1275 /* Pointers to this DIE's parent, first child, and next sibling,
1276 if any. */
1277 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1278 };
1279
0963b4bd 1280/* This data structure holds the information of an abbrev. */
c906108c
SS
1281struct abbrev_info
1282 {
1283 unsigned int number; /* number identifying abbrev */
1284 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1285 unsigned short has_children; /* boolean */
1286 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1287 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1288 struct abbrev_info *next; /* next in chain */
1289 };
1290
1291struct attr_abbrev
1292 {
9d25dd43
DE
1293 ENUM_BITFIELD(dwarf_attribute) name : 16;
1294 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1295
1296 /* It is valid only if FORM is DW_FORM_implicit_const. */
1297 LONGEST implicit_const;
c906108c
SS
1298 };
1299
433df2d4
DE
1300/* Size of abbrev_table.abbrev_hash_table. */
1301#define ABBREV_HASH_SIZE 121
1302
1303/* Top level data structure to contain an abbreviation table. */
1304
1305struct abbrev_table
1306{
f4dc4d17
DE
1307 /* Where the abbrev table came from.
1308 This is used as a sanity check when the table is used. */
9c541725 1309 sect_offset sect_off;
433df2d4
DE
1310
1311 /* Storage for the abbrev table. */
1312 struct obstack abbrev_obstack;
1313
1314 /* Hash table of abbrevs.
1315 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1316 It could be statically allocated, but the previous code didn't so we
1317 don't either. */
1318 struct abbrev_info **abbrevs;
1319};
1320
0963b4bd 1321/* Attributes have a name and a value. */
b60c80d6
DJ
1322struct attribute
1323 {
9d25dd43 1324 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1325 ENUM_BITFIELD(dwarf_form) form : 15;
1326
1327 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1328 field should be in u.str (existing only for DW_STRING) but it is kept
1329 here for better struct attribute alignment. */
1330 unsigned int string_is_canonical : 1;
1331
b60c80d6
DJ
1332 union
1333 {
15d034d0 1334 const char *str;
b60c80d6 1335 struct dwarf_block *blk;
43bbcdc2
PH
1336 ULONGEST unsnd;
1337 LONGEST snd;
b60c80d6 1338 CORE_ADDR addr;
ac9ec31b 1339 ULONGEST signature;
b60c80d6
DJ
1340 }
1341 u;
1342 };
1343
0963b4bd 1344/* This data structure holds a complete die structure. */
c906108c
SS
1345struct die_info
1346 {
76815b17
DE
1347 /* DWARF-2 tag for this DIE. */
1348 ENUM_BITFIELD(dwarf_tag) tag : 16;
1349
1350 /* Number of attributes */
98bfdba5
PA
1351 unsigned char num_attrs;
1352
1353 /* True if we're presently building the full type name for the
1354 type derived from this DIE. */
1355 unsigned char building_fullname : 1;
76815b17 1356
adde2bff
DE
1357 /* True if this die is in process. PR 16581. */
1358 unsigned char in_process : 1;
1359
76815b17
DE
1360 /* Abbrev number */
1361 unsigned int abbrev;
1362
93311388 1363 /* Offset in .debug_info or .debug_types section. */
9c541725 1364 sect_offset sect_off;
78ba4af6
JB
1365
1366 /* The dies in a compilation unit form an n-ary tree. PARENT
1367 points to this die's parent; CHILD points to the first child of
1368 this node; and all the children of a given node are chained
4950bc1c 1369 together via their SIBLING fields. */
639d11d3
DC
1370 struct die_info *child; /* Its first child, if any. */
1371 struct die_info *sibling; /* Its next sibling, if any. */
1372 struct die_info *parent; /* Its parent, if any. */
c906108c 1373
b60c80d6
DJ
1374 /* An array of attributes, with NUM_ATTRS elements. There may be
1375 zero, but it's not common and zero-sized arrays are not
1376 sufficiently portable C. */
1377 struct attribute attrs[1];
c906108c
SS
1378 };
1379
0963b4bd 1380/* Get at parts of an attribute structure. */
c906108c
SS
1381
1382#define DW_STRING(attr) ((attr)->u.str)
8285870a 1383#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1384#define DW_UNSND(attr) ((attr)->u.unsnd)
1385#define DW_BLOCK(attr) ((attr)->u.blk)
1386#define DW_SND(attr) ((attr)->u.snd)
1387#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1388#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1389
0963b4bd 1390/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1391struct dwarf_block
1392 {
56eb65bd 1393 size_t size;
1d6edc3c
JK
1394
1395 /* Valid only if SIZE is not zero. */
d521ce57 1396 const gdb_byte *data;
c906108c
SS
1397 };
1398
c906108c
SS
1399#ifndef ATTR_ALLOC_CHUNK
1400#define ATTR_ALLOC_CHUNK 4
1401#endif
1402
c906108c
SS
1403/* Allocate fields for structs, unions and enums in this size. */
1404#ifndef DW_FIELD_ALLOC_CHUNK
1405#define DW_FIELD_ALLOC_CHUNK 4
1406#endif
1407
c906108c
SS
1408/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1409 but this would require a corresponding change in unpack_field_as_long
1410 and friends. */
1411static int bits_per_byte = 8;
1412
52059ffd
TT
1413struct nextfield
1414{
1415 struct nextfield *next;
1416 int accessibility;
1417 int virtuality;
1418 struct field field;
1419};
1420
1421struct nextfnfield
1422{
1423 struct nextfnfield *next;
1424 struct fn_field fnfield;
1425};
1426
1427struct fnfieldlist
1428{
1429 const char *name;
1430 int length;
1431 struct nextfnfield *head;
1432};
1433
1434struct typedef_field_list
1435{
1436 struct typedef_field field;
1437 struct typedef_field_list *next;
1438};
1439
c906108c
SS
1440/* The routines that read and process dies for a C struct or C++ class
1441 pass lists of data member fields and lists of member function fields
1442 in an instance of a field_info structure, as defined below. */
1443struct field_info
c5aa993b 1444 {
0963b4bd 1445 /* List of data member and baseclasses fields. */
52059ffd 1446 struct nextfield *fields, *baseclasses;
c906108c 1447
7d0ccb61 1448 /* Number of fields (including baseclasses). */
c5aa993b 1449 int nfields;
c906108c 1450
c5aa993b
JM
1451 /* Number of baseclasses. */
1452 int nbaseclasses;
c906108c 1453
c5aa993b
JM
1454 /* Set if the accesibility of one of the fields is not public. */
1455 int non_public_fields;
c906108c 1456
c5aa993b
JM
1457 /* Member function fields array, entries are allocated in the order they
1458 are encountered in the object file. */
52059ffd 1459 struct nextfnfield *fnfields;
c906108c 1460
c5aa993b
JM
1461 /* Member function fieldlist array, contains name of possibly overloaded
1462 member function, number of overloaded member functions and a pointer
1463 to the head of the member function field chain. */
52059ffd 1464 struct fnfieldlist *fnfieldlists;
c906108c 1465
c5aa993b
JM
1466 /* Number of entries in the fnfieldlists array. */
1467 int nfnfields;
98751a41
JK
1468
1469 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1470 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1471 struct typedef_field_list *typedef_field_list;
98751a41 1472 unsigned typedef_field_list_count;
c5aa993b 1473 };
c906108c 1474
10b3939b
DJ
1475/* One item on the queue of compilation units to read in full symbols
1476 for. */
1477struct dwarf2_queue_item
1478{
1479 struct dwarf2_per_cu_data *per_cu;
95554aad 1480 enum language pretend_language;
10b3939b
DJ
1481 struct dwarf2_queue_item *next;
1482};
1483
1484/* The current queue. */
1485static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1486
ae038cb0
DJ
1487/* Loaded secondary compilation units are kept in memory until they
1488 have not been referenced for the processing of this many
1489 compilation units. Set this to zero to disable caching. Cache
1490 sizes of up to at least twenty will improve startup time for
1491 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1492static int dwarf_max_cache_age = 5;
920d2a44 1493static void
b4f54984
DE
1494show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1495 struct cmd_list_element *c, const char *value)
920d2a44 1496{
3e43a32a 1497 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1498 "DWARF compilation units is %s.\n"),
920d2a44
AC
1499 value);
1500}
4390d890 1501\f
c906108c
SS
1502/* local function prototypes */
1503
a32a8923
DE
1504static const char *get_section_name (const struct dwarf2_section_info *);
1505
1506static const char *get_section_file_name (const struct dwarf2_section_info *);
1507
4efb68b1 1508static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1509
918dd910
JK
1510static void dwarf2_find_base_address (struct die_info *die,
1511 struct dwarf2_cu *cu);
1512
0018ea6f
DE
1513static struct partial_symtab *create_partial_symtab
1514 (struct dwarf2_per_cu_data *per_cu, const char *name);
1515
c67a9c90 1516static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1517
72bf9492
DJ
1518static void scan_partial_symbols (struct partial_die_info *,
1519 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1520 int, struct dwarf2_cu *);
c906108c 1521
72bf9492
DJ
1522static void add_partial_symbol (struct partial_die_info *,
1523 struct dwarf2_cu *);
63d06c5c 1524
72bf9492
DJ
1525static void add_partial_namespace (struct partial_die_info *pdi,
1526 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1527 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1528
5d7cb8df 1529static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1530 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1531 struct dwarf2_cu *cu);
1532
72bf9492
DJ
1533static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1534 struct dwarf2_cu *cu);
91c24f0a 1535
bc30ff58
JB
1536static void add_partial_subprogram (struct partial_die_info *pdi,
1537 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1538 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1539
257e7a09
YQ
1540static void dwarf2_read_symtab (struct partial_symtab *,
1541 struct objfile *);
c906108c 1542
a14ed312 1543static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1544
433df2d4
DE
1545static struct abbrev_info *abbrev_table_lookup_abbrev
1546 (const struct abbrev_table *, unsigned int);
1547
1548static struct abbrev_table *abbrev_table_read_table
1549 (struct dwarf2_section_info *, sect_offset);
1550
1551static void abbrev_table_free (struct abbrev_table *);
1552
f4dc4d17
DE
1553static void abbrev_table_free_cleanup (void *);
1554
dee91e82
DE
1555static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1556 struct dwarf2_section_info *);
c906108c 1557
f3dd6933 1558static void dwarf2_free_abbrev_table (void *);
c906108c 1559
d521ce57 1560static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1561
dee91e82 1562static struct partial_die_info *load_partial_dies
d521ce57 1563 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1564
d521ce57
TT
1565static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1566 struct partial_die_info *,
1567 struct abbrev_info *,
1568 unsigned int,
1569 const gdb_byte *);
c906108c 1570
36586728 1571static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1572 struct dwarf2_cu *);
72bf9492
DJ
1573
1574static void fixup_partial_die (struct partial_die_info *,
1575 struct dwarf2_cu *);
1576
d521ce57
TT
1577static const gdb_byte *read_attribute (const struct die_reader_specs *,
1578 struct attribute *, struct attr_abbrev *,
1579 const gdb_byte *);
a8329558 1580
a1855c1d 1581static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1582
a1855c1d 1583static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1584
a1855c1d 1585static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1586
a1855c1d 1587static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1588
a1855c1d 1589static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1590
d521ce57 1591static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1592 unsigned int *);
c906108c 1593
d521ce57 1594static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1595
1596static LONGEST read_checked_initial_length_and_offset
d521ce57 1597 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1598 unsigned int *, unsigned int *);
613e1657 1599
d521ce57
TT
1600static LONGEST read_offset (bfd *, const gdb_byte *,
1601 const struct comp_unit_head *,
c764a876
DE
1602 unsigned int *);
1603
d521ce57 1604static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1605
f4dc4d17
DE
1606static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1607 sect_offset);
1608
d521ce57 1609static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1610
d521ce57 1611static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1612
d521ce57
TT
1613static const char *read_indirect_string (bfd *, const gdb_byte *,
1614 const struct comp_unit_head *,
1615 unsigned int *);
4bdf3d34 1616
43988095
JK
1617static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1618 const struct comp_unit_head *,
1619 unsigned int *);
36586728 1620
43988095 1621static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1622
d521ce57 1623static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1624
d521ce57
TT
1625static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1626 const gdb_byte *,
3019eac3
DE
1627 unsigned int *);
1628
d521ce57 1629static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1630 ULONGEST str_index);
3019eac3 1631
e142c38c 1632static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1633
e142c38c
DJ
1634static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1635 struct dwarf2_cu *);
c906108c 1636
348e048f 1637static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1638 unsigned int);
348e048f 1639
7d45c7c3
KB
1640static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1641 struct dwarf2_cu *cu);
1642
05cf31d1
JB
1643static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1644 struct dwarf2_cu *cu);
1645
e142c38c 1646static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1647
e142c38c 1648static struct die_info *die_specification (struct die_info *die,
f2f0e013 1649 struct dwarf2_cu **);
63d06c5c 1650
9c541725 1651static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1652 struct dwarf2_cu *cu);
debd256d 1653
f3f5162e 1654static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1655 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1656 CORE_ADDR, int decode_mapping);
c906108c 1657
4d663531 1658static void dwarf2_start_subfile (const char *, const char *);
c906108c 1659
43f3e411
DE
1660static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1661 const char *, const char *,
1662 CORE_ADDR);
f4dc4d17 1663
a14ed312 1664static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1665 struct dwarf2_cu *);
c906108c 1666
34eaf542
TT
1667static struct symbol *new_symbol_full (struct die_info *, struct type *,
1668 struct dwarf2_cu *, struct symbol *);
1669
ff39bb5e 1670static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1671 struct dwarf2_cu *);
c906108c 1672
ff39bb5e 1673static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1674 struct type *type,
1675 const char *name,
1676 struct obstack *obstack,
12df843f 1677 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1678 const gdb_byte **bytes,
98bfdba5 1679 struct dwarf2_locexpr_baton **baton);
2df3850c 1680
e7c27a73 1681static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1682
b4ba55a1
JB
1683static int need_gnat_info (struct dwarf2_cu *);
1684
3e43a32a
MS
1685static struct type *die_descriptive_type (struct die_info *,
1686 struct dwarf2_cu *);
b4ba55a1
JB
1687
1688static void set_descriptive_type (struct type *, struct die_info *,
1689 struct dwarf2_cu *);
1690
e7c27a73
DJ
1691static struct type *die_containing_type (struct die_info *,
1692 struct dwarf2_cu *);
c906108c 1693
ff39bb5e 1694static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1695 struct dwarf2_cu *);
c906108c 1696
f792889a 1697static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1698
673bfd45
DE
1699static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1700
0d5cff50 1701static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1702
6e70227d 1703static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1704 const char *suffix, int physname,
1705 struct dwarf2_cu *cu);
63d06c5c 1706
e7c27a73 1707static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1708
348e048f
DE
1709static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1710
e7c27a73 1711static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1712
e7c27a73 1713static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1714
96408a79
SA
1715static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1716
ff013f42
JK
1717static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1718 struct dwarf2_cu *, struct partial_symtab *);
1719
3a2b436a 1720/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1721 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1722enum pc_bounds_kind
1723{
e385593e 1724 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1725 PC_BOUNDS_NOT_PRESENT,
1726
e385593e
JK
1727 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1728 were present but they do not form a valid range of PC addresses. */
1729 PC_BOUNDS_INVALID,
1730
3a2b436a
JK
1731 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1732 PC_BOUNDS_RANGES,
1733
1734 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1735 PC_BOUNDS_HIGH_LOW,
1736};
1737
1738static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1739 CORE_ADDR *, CORE_ADDR *,
1740 struct dwarf2_cu *,
1741 struct partial_symtab *);
c906108c 1742
fae299cd
DC
1743static void get_scope_pc_bounds (struct die_info *,
1744 CORE_ADDR *, CORE_ADDR *,
1745 struct dwarf2_cu *);
1746
801e3a5b
JB
1747static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1748 CORE_ADDR, struct dwarf2_cu *);
1749
a14ed312 1750static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1751 struct dwarf2_cu *);
c906108c 1752
a14ed312 1753static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1754 struct type *, struct dwarf2_cu *);
c906108c 1755
a14ed312 1756static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1757 struct die_info *, struct type *,
e7c27a73 1758 struct dwarf2_cu *);
c906108c 1759
a14ed312 1760static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1761 struct type *,
1762 struct dwarf2_cu *);
c906108c 1763
134d01f1 1764static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1765
e7c27a73 1766static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1767
e7c27a73 1768static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1769
5d7cb8df
JK
1770static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1771
22cee43f
PMR
1772static struct using_direct **using_directives (enum language);
1773
27aa8d6a
SW
1774static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1775
74921315
KS
1776static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1777
f55ee35c
JK
1778static struct type *read_module_type (struct die_info *die,
1779 struct dwarf2_cu *cu);
1780
38d518c9 1781static const char *namespace_name (struct die_info *die,
e142c38c 1782 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1783
134d01f1 1784static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1785
e7c27a73 1786static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1787
6e70227d 1788static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1789 struct dwarf2_cu *);
1790
bf6af496 1791static struct die_info *read_die_and_siblings_1
d521ce57 1792 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1793 struct die_info *);
639d11d3 1794
dee91e82 1795static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1796 const gdb_byte *info_ptr,
1797 const gdb_byte **new_info_ptr,
639d11d3
DC
1798 struct die_info *parent);
1799
d521ce57
TT
1800static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1801 struct die_info **, const gdb_byte *,
1802 int *, int);
3019eac3 1803
d521ce57
TT
1804static const gdb_byte *read_full_die (const struct die_reader_specs *,
1805 struct die_info **, const gdb_byte *,
1806 int *);
93311388 1807
e7c27a73 1808static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1809
15d034d0
TT
1810static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1811 struct obstack *);
71c25dea 1812
15d034d0 1813static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1814
15d034d0 1815static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1816 struct die_info *die,
1817 struct dwarf2_cu *cu);
1818
ca69b9e6
DE
1819static const char *dwarf2_physname (const char *name, struct die_info *die,
1820 struct dwarf2_cu *cu);
1821
e142c38c 1822static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1823 struct dwarf2_cu **);
9219021c 1824
f39c6ffd 1825static const char *dwarf_tag_name (unsigned int);
c906108c 1826
f39c6ffd 1827static const char *dwarf_attr_name (unsigned int);
c906108c 1828
f39c6ffd 1829static const char *dwarf_form_name (unsigned int);
c906108c 1830
a121b7c1 1831static const char *dwarf_bool_name (unsigned int);
c906108c 1832
f39c6ffd 1833static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1834
f9aca02d 1835static struct die_info *sibling_die (struct die_info *);
c906108c 1836
d97bc12b
DE
1837static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1838
1839static void dump_die_for_error (struct die_info *);
1840
1841static void dump_die_1 (struct ui_file *, int level, int max_level,
1842 struct die_info *);
c906108c 1843
d97bc12b 1844/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1845
51545339 1846static void store_in_ref_table (struct die_info *,
10b3939b 1847 struct dwarf2_cu *);
c906108c 1848
ff39bb5e 1849static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1850
ff39bb5e 1851static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1852
348e048f 1853static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1854 const struct attribute *,
348e048f
DE
1855 struct dwarf2_cu **);
1856
10b3939b 1857static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1858 const struct attribute *,
f2f0e013 1859 struct dwarf2_cu **);
c906108c 1860
348e048f 1861static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1862 const struct attribute *,
348e048f
DE
1863 struct dwarf2_cu **);
1864
ac9ec31b
DE
1865static struct type *get_signatured_type (struct die_info *, ULONGEST,
1866 struct dwarf2_cu *);
1867
1868static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1869 const struct attribute *,
ac9ec31b
DE
1870 struct dwarf2_cu *);
1871
e5fe5e75 1872static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1873
52dc124a 1874static void read_signatured_type (struct signatured_type *);
348e048f 1875
63e43d3a
PMR
1876static int attr_to_dynamic_prop (const struct attribute *attr,
1877 struct die_info *die, struct dwarf2_cu *cu,
1878 struct dynamic_prop *prop);
1879
c906108c
SS
1880/* memory allocation interface */
1881
7b5a2f43 1882static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1883
b60c80d6 1884static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1885
43f3e411 1886static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1887
6e5a29e1 1888static int attr_form_is_block (const struct attribute *);
8e19ed76 1889
6e5a29e1 1890static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1891
6e5a29e1 1892static int attr_form_is_constant (const struct attribute *);
3690dd37 1893
6e5a29e1 1894static int attr_form_is_ref (const struct attribute *);
7771576e 1895
8cf6f0b1
TT
1896static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1897 struct dwarf2_loclist_baton *baton,
ff39bb5e 1898 const struct attribute *attr);
8cf6f0b1 1899
ff39bb5e 1900static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1901 struct symbol *sym,
f1e6e072
TT
1902 struct dwarf2_cu *cu,
1903 int is_block);
4c2df51b 1904
d521ce57
TT
1905static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1906 const gdb_byte *info_ptr,
1907 struct abbrev_info *abbrev);
4bb7a0a7 1908
72bf9492
DJ
1909static void free_stack_comp_unit (void *);
1910
72bf9492
DJ
1911static hashval_t partial_die_hash (const void *item);
1912
1913static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1914
ae038cb0 1915static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 1916 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1917
9816fde3 1918static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1919 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1920
1921static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1922 struct die_info *comp_unit_die,
1923 enum language pretend_language);
93311388 1924
68dc6402 1925static void free_heap_comp_unit (void *);
ae038cb0
DJ
1926
1927static void free_cached_comp_units (void *);
1928
1929static void age_cached_comp_units (void);
1930
dee91e82 1931static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1932
f792889a
DJ
1933static struct type *set_die_type (struct die_info *, struct type *,
1934 struct dwarf2_cu *);
1c379e20 1935
ae038cb0
DJ
1936static void create_all_comp_units (struct objfile *);
1937
0e50663e 1938static int create_all_type_units (struct objfile *);
1fd400ff 1939
95554aad
TT
1940static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1941 enum language);
10b3939b 1942
95554aad
TT
1943static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1944 enum language);
10b3939b 1945
f4dc4d17
DE
1946static void process_full_type_unit (struct dwarf2_per_cu_data *,
1947 enum language);
1948
10b3939b
DJ
1949static void dwarf2_add_dependence (struct dwarf2_cu *,
1950 struct dwarf2_per_cu_data *);
1951
ae038cb0
DJ
1952static void dwarf2_mark (struct dwarf2_cu *);
1953
1954static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1955
b64f50a1 1956static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1957 struct dwarf2_per_cu_data *);
673bfd45 1958
f792889a 1959static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1960
9291a0cd
TT
1961static void dwarf2_release_queue (void *dummy);
1962
95554aad
TT
1963static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1964 enum language pretend_language);
1965
a0f42c21 1966static void process_queue (void);
9291a0cd 1967
d721ba37
PA
1968/* The return type of find_file_and_directory. Note, the enclosed
1969 string pointers are only valid while this object is valid. */
1970
1971struct file_and_directory
1972{
1973 /* The filename. This is never NULL. */
1974 const char *name;
1975
1976 /* The compilation directory. NULL if not known. If we needed to
1977 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1978 points directly to the DW_AT_comp_dir string attribute owned by
1979 the obstack that owns the DIE. */
1980 const char *comp_dir;
1981
1982 /* If we needed to build a new string for comp_dir, this is what
1983 owns the storage. */
1984 std::string comp_dir_storage;
1985};
1986
1987static file_and_directory find_file_and_directory (struct die_info *die,
1988 struct dwarf2_cu *cu);
9291a0cd
TT
1989
1990static char *file_full_name (int file, struct line_header *lh,
1991 const char *comp_dir);
1992
43988095
JK
1993/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1994enum class rcuh_kind { COMPILE, TYPE };
1995
d521ce57 1996static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1997 (struct comp_unit_head *header,
1998 struct dwarf2_section_info *section,
d521ce57 1999 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2000 rcuh_kind section_kind);
36586728 2001
fd820528 2002static void init_cutu_and_read_dies
f4dc4d17
DE
2003 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2004 int use_existing_cu, int keep,
3019eac3
DE
2005 die_reader_func_ftype *die_reader_func, void *data);
2006
dee91e82
DE
2007static void init_cutu_and_read_dies_simple
2008 (struct dwarf2_per_cu_data *this_cu,
2009 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2010
673bfd45 2011static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2012
3019eac3
DE
2013static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2014
57d63ce2
DE
2015static struct dwo_unit *lookup_dwo_unit_in_dwp
2016 (struct dwp_file *dwp_file, const char *comp_dir,
2017 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2018
2019static struct dwp_file *get_dwp_file (void);
2020
3019eac3 2021static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2022 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2023
2024static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2025 (struct signatured_type *, const char *, const char *);
3019eac3 2026
89e63ee4
DE
2027static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2028
3019eac3
DE
2029static void free_dwo_file_cleanup (void *);
2030
95554aad
TT
2031static void process_cu_includes (void);
2032
1b80a9fa 2033static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2034
2035static void free_line_header_voidp (void *arg);
4390d890
DE
2036\f
2037/* Various complaints about symbol reading that don't abort the process. */
2038
2039static void
2040dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2041{
2042 complaint (&symfile_complaints,
2043 _("statement list doesn't fit in .debug_line section"));
2044}
2045
2046static void
2047dwarf2_debug_line_missing_file_complaint (void)
2048{
2049 complaint (&symfile_complaints,
2050 _(".debug_line section has line data without a file"));
2051}
2052
2053static void
2054dwarf2_debug_line_missing_end_sequence_complaint (void)
2055{
2056 complaint (&symfile_complaints,
2057 _(".debug_line section has line "
2058 "program sequence without an end"));
2059}
2060
2061static void
2062dwarf2_complex_location_expr_complaint (void)
2063{
2064 complaint (&symfile_complaints, _("location expression too complex"));
2065}
2066
2067static void
2068dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2069 int arg3)
2070{
2071 complaint (&symfile_complaints,
2072 _("const value length mismatch for '%s', got %d, expected %d"),
2073 arg1, arg2, arg3);
2074}
2075
2076static void
2077dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2078{
2079 complaint (&symfile_complaints,
2080 _("debug info runs off end of %s section"
2081 " [in module %s]"),
a32a8923
DE
2082 get_section_name (section),
2083 get_section_file_name (section));
4390d890 2084}
1b80a9fa 2085
4390d890
DE
2086static void
2087dwarf2_macro_malformed_definition_complaint (const char *arg1)
2088{
2089 complaint (&symfile_complaints,
2090 _("macro debug info contains a "
2091 "malformed macro definition:\n`%s'"),
2092 arg1);
2093}
2094
2095static void
2096dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2097{
2098 complaint (&symfile_complaints,
2099 _("invalid attribute class or form for '%s' in '%s'"),
2100 arg1, arg2);
2101}
527f3840
JK
2102
2103/* Hash function for line_header_hash. */
2104
2105static hashval_t
2106line_header_hash (const struct line_header *ofs)
2107{
9c541725 2108 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2109}
2110
2111/* Hash function for htab_create_alloc_ex for line_header_hash. */
2112
2113static hashval_t
2114line_header_hash_voidp (const void *item)
2115{
9a3c8263 2116 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2117
2118 return line_header_hash (ofs);
2119}
2120
2121/* Equality function for line_header_hash. */
2122
2123static int
2124line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2125{
9a3c8263
SM
2126 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2127 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2128
9c541725 2129 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2130 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2131}
2132
4390d890 2133\f
9291a0cd
TT
2134#if WORDS_BIGENDIAN
2135
2136/* Convert VALUE between big- and little-endian. */
2137static offset_type
2138byte_swap (offset_type value)
2139{
2140 offset_type result;
2141
2142 result = (value & 0xff) << 24;
2143 result |= (value & 0xff00) << 8;
2144 result |= (value & 0xff0000) >> 8;
2145 result |= (value & 0xff000000) >> 24;
2146 return result;
2147}
2148
2149#define MAYBE_SWAP(V) byte_swap (V)
2150
2151#else
bc8f2430 2152#define MAYBE_SWAP(V) static_cast<offset_type> (V)
9291a0cd
TT
2153#endif /* WORDS_BIGENDIAN */
2154
31aa7e4e
JB
2155/* Read the given attribute value as an address, taking the attribute's
2156 form into account. */
2157
2158static CORE_ADDR
2159attr_value_as_address (struct attribute *attr)
2160{
2161 CORE_ADDR addr;
2162
2163 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2164 {
2165 /* Aside from a few clearly defined exceptions, attributes that
2166 contain an address must always be in DW_FORM_addr form.
2167 Unfortunately, some compilers happen to be violating this
2168 requirement by encoding addresses using other forms, such
2169 as DW_FORM_data4 for example. For those broken compilers,
2170 we try to do our best, without any guarantee of success,
2171 to interpret the address correctly. It would also be nice
2172 to generate a complaint, but that would require us to maintain
2173 a list of legitimate cases where a non-address form is allowed,
2174 as well as update callers to pass in at least the CU's DWARF
2175 version. This is more overhead than what we're willing to
2176 expand for a pretty rare case. */
2177 addr = DW_UNSND (attr);
2178 }
2179 else
2180 addr = DW_ADDR (attr);
2181
2182 return addr;
2183}
2184
9291a0cd
TT
2185/* The suffix for an index file. */
2186#define INDEX_SUFFIX ".gdb-index"
2187
c906108c 2188/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2189 information and return true if we have enough to do something.
2190 NAMES points to the dwarf2 section names, or is NULL if the standard
2191 ELF names are used. */
c906108c
SS
2192
2193int
251d32d9
TG
2194dwarf2_has_info (struct objfile *objfile,
2195 const struct dwarf2_debug_sections *names)
c906108c 2196{
9a3c8263
SM
2197 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2198 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2199 if (!dwarf2_per_objfile)
2200 {
2201 /* Initialize per-objfile state. */
2202 struct dwarf2_per_objfile *data
8d749320 2203 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2204
be391dca
TT
2205 memset (data, 0, sizeof (*data));
2206 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2207 dwarf2_per_objfile = data;
6502dd73 2208
251d32d9
TG
2209 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2210 (void *) names);
be391dca
TT
2211 dwarf2_per_objfile->objfile = objfile;
2212 }
73869dc2 2213 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2214 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2215 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2216 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2217}
2218
2219/* Return the containing section of virtual section SECTION. */
2220
2221static struct dwarf2_section_info *
2222get_containing_section (const struct dwarf2_section_info *section)
2223{
2224 gdb_assert (section->is_virtual);
2225 return section->s.containing_section;
c906108c
SS
2226}
2227
a32a8923
DE
2228/* Return the bfd owner of SECTION. */
2229
2230static struct bfd *
2231get_section_bfd_owner (const struct dwarf2_section_info *section)
2232{
73869dc2
DE
2233 if (section->is_virtual)
2234 {
2235 section = get_containing_section (section);
2236 gdb_assert (!section->is_virtual);
2237 }
049412e3 2238 return section->s.section->owner;
a32a8923
DE
2239}
2240
2241/* Return the bfd section of SECTION.
2242 Returns NULL if the section is not present. */
2243
2244static asection *
2245get_section_bfd_section (const struct dwarf2_section_info *section)
2246{
73869dc2
DE
2247 if (section->is_virtual)
2248 {
2249 section = get_containing_section (section);
2250 gdb_assert (!section->is_virtual);
2251 }
049412e3 2252 return section->s.section;
a32a8923
DE
2253}
2254
2255/* Return the name of SECTION. */
2256
2257static const char *
2258get_section_name (const struct dwarf2_section_info *section)
2259{
2260 asection *sectp = get_section_bfd_section (section);
2261
2262 gdb_assert (sectp != NULL);
2263 return bfd_section_name (get_section_bfd_owner (section), sectp);
2264}
2265
2266/* Return the name of the file SECTION is in. */
2267
2268static const char *
2269get_section_file_name (const struct dwarf2_section_info *section)
2270{
2271 bfd *abfd = get_section_bfd_owner (section);
2272
2273 return bfd_get_filename (abfd);
2274}
2275
2276/* Return the id of SECTION.
2277 Returns 0 if SECTION doesn't exist. */
2278
2279static int
2280get_section_id (const struct dwarf2_section_info *section)
2281{
2282 asection *sectp = get_section_bfd_section (section);
2283
2284 if (sectp == NULL)
2285 return 0;
2286 return sectp->id;
2287}
2288
2289/* Return the flags of SECTION.
73869dc2 2290 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2291
2292static int
2293get_section_flags (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 gdb_assert (sectp != NULL);
2298 return bfd_get_section_flags (sectp->owner, sectp);
2299}
2300
251d32d9
TG
2301/* When loading sections, we look either for uncompressed section or for
2302 compressed section names. */
233a11ab
CS
2303
2304static int
251d32d9
TG
2305section_is_p (const char *section_name,
2306 const struct dwarf2_section_names *names)
233a11ab 2307{
251d32d9
TG
2308 if (names->normal != NULL
2309 && strcmp (section_name, names->normal) == 0)
2310 return 1;
2311 if (names->compressed != NULL
2312 && strcmp (section_name, names->compressed) == 0)
2313 return 1;
2314 return 0;
233a11ab
CS
2315}
2316
c906108c
SS
2317/* This function is mapped across the sections and remembers the
2318 offset and size of each of the debugging sections we are interested
2319 in. */
2320
2321static void
251d32d9 2322dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2323{
251d32d9 2324 const struct dwarf2_debug_sections *names;
dc7650b8 2325 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2326
2327 if (vnames == NULL)
2328 names = &dwarf2_elf_names;
2329 else
2330 names = (const struct dwarf2_debug_sections *) vnames;
2331
dc7650b8
JK
2332 if ((aflag & SEC_HAS_CONTENTS) == 0)
2333 {
2334 }
2335 else if (section_is_p (sectp->name, &names->info))
c906108c 2336 {
049412e3 2337 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2338 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2339 }
251d32d9 2340 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2341 {
049412e3 2342 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2343 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2344 }
251d32d9 2345 else if (section_is_p (sectp->name, &names->line))
c906108c 2346 {
049412e3 2347 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2348 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2349 }
251d32d9 2350 else if (section_is_p (sectp->name, &names->loc))
c906108c 2351 {
049412e3 2352 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2353 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2354 }
43988095
JK
2355 else if (section_is_p (sectp->name, &names->loclists))
2356 {
2357 dwarf2_per_objfile->loclists.s.section = sectp;
2358 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2359 }
251d32d9 2360 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2361 {
049412e3 2362 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2363 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2364 }
cf2c3c16
TT
2365 else if (section_is_p (sectp->name, &names->macro))
2366 {
049412e3 2367 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2368 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2369 }
251d32d9 2370 else if (section_is_p (sectp->name, &names->str))
c906108c 2371 {
049412e3 2372 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2373 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2374 }
43988095
JK
2375 else if (section_is_p (sectp->name, &names->line_str))
2376 {
2377 dwarf2_per_objfile->line_str.s.section = sectp;
2378 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2379 }
3019eac3
DE
2380 else if (section_is_p (sectp->name, &names->addr))
2381 {
049412e3 2382 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2383 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2384 }
251d32d9 2385 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2386 {
049412e3 2387 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2388 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2389 }
251d32d9 2390 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2391 {
049412e3 2392 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2393 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2394 }
251d32d9 2395 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2396 {
049412e3 2397 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2398 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2399 }
43988095
JK
2400 else if (section_is_p (sectp->name, &names->rnglists))
2401 {
2402 dwarf2_per_objfile->rnglists.s.section = sectp;
2403 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2404 }
251d32d9 2405 else if (section_is_p (sectp->name, &names->types))
348e048f 2406 {
8b70b953
TT
2407 struct dwarf2_section_info type_section;
2408
2409 memset (&type_section, 0, sizeof (type_section));
049412e3 2410 type_section.s.section = sectp;
8b70b953
TT
2411 type_section.size = bfd_get_section_size (sectp);
2412
2413 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2414 &type_section);
348e048f 2415 }
251d32d9 2416 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2417 {
049412e3 2418 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2419 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2420 }
dce234bc 2421
b4e1fd61 2422 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2423 && bfd_section_vma (abfd, sectp) == 0)
2424 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2425}
2426
fceca515
DE
2427/* A helper function that decides whether a section is empty,
2428 or not present. */
9e0ac564
TT
2429
2430static int
19ac8c2e 2431dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2432{
73869dc2
DE
2433 if (section->is_virtual)
2434 return section->size == 0;
049412e3 2435 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2436}
2437
3019eac3
DE
2438/* Read the contents of the section INFO.
2439 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2440 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2441 of the DWO file.
dce234bc 2442 If the section is compressed, uncompress it before returning. */
c906108c 2443
dce234bc
PP
2444static void
2445dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2446{
a32a8923 2447 asection *sectp;
3019eac3 2448 bfd *abfd;
dce234bc 2449 gdb_byte *buf, *retbuf;
c906108c 2450
be391dca
TT
2451 if (info->readin)
2452 return;
dce234bc 2453 info->buffer = NULL;
be391dca 2454 info->readin = 1;
188dd5d6 2455
9e0ac564 2456 if (dwarf2_section_empty_p (info))
dce234bc 2457 return;
c906108c 2458
a32a8923 2459 sectp = get_section_bfd_section (info);
3019eac3 2460
73869dc2
DE
2461 /* If this is a virtual section we need to read in the real one first. */
2462 if (info->is_virtual)
2463 {
2464 struct dwarf2_section_info *containing_section =
2465 get_containing_section (info);
2466
2467 gdb_assert (sectp != NULL);
2468 if ((sectp->flags & SEC_RELOC) != 0)
2469 {
2470 error (_("Dwarf Error: DWP format V2 with relocations is not"
2471 " supported in section %s [in module %s]"),
2472 get_section_name (info), get_section_file_name (info));
2473 }
2474 dwarf2_read_section (objfile, containing_section);
2475 /* Other code should have already caught virtual sections that don't
2476 fit. */
2477 gdb_assert (info->virtual_offset + info->size
2478 <= containing_section->size);
2479 /* If the real section is empty or there was a problem reading the
2480 section we shouldn't get here. */
2481 gdb_assert (containing_section->buffer != NULL);
2482 info->buffer = containing_section->buffer + info->virtual_offset;
2483 return;
2484 }
2485
4bf44c1c
TT
2486 /* If the section has relocations, we must read it ourselves.
2487 Otherwise we attach it to the BFD. */
2488 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2489 {
d521ce57 2490 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2491 return;
dce234bc 2492 }
dce234bc 2493
224c3ddb 2494 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2495 info->buffer = buf;
dce234bc
PP
2496
2497 /* When debugging .o files, we may need to apply relocations; see
2498 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2499 We never compress sections in .o files, so we only need to
2500 try this when the section is not compressed. */
ac8035ab 2501 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2502 if (retbuf != NULL)
2503 {
2504 info->buffer = retbuf;
2505 return;
2506 }
2507
a32a8923
DE
2508 abfd = get_section_bfd_owner (info);
2509 gdb_assert (abfd != NULL);
2510
dce234bc
PP
2511 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2512 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2513 {
2514 error (_("Dwarf Error: Can't read DWARF data"
2515 " in section %s [in module %s]"),
2516 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2517 }
dce234bc
PP
2518}
2519
9e0ac564
TT
2520/* A helper function that returns the size of a section in a safe way.
2521 If you are positive that the section has been read before using the
2522 size, then it is safe to refer to the dwarf2_section_info object's
2523 "size" field directly. In other cases, you must call this
2524 function, because for compressed sections the size field is not set
2525 correctly until the section has been read. */
2526
2527static bfd_size_type
2528dwarf2_section_size (struct objfile *objfile,
2529 struct dwarf2_section_info *info)
2530{
2531 if (!info->readin)
2532 dwarf2_read_section (objfile, info);
2533 return info->size;
2534}
2535
dce234bc 2536/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2537 SECTION_NAME. */
af34e669 2538
dce234bc 2539void
3017a003
TG
2540dwarf2_get_section_info (struct objfile *objfile,
2541 enum dwarf2_section_enum sect,
d521ce57 2542 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2543 bfd_size_type *sizep)
2544{
2545 struct dwarf2_per_objfile *data
9a3c8263
SM
2546 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2547 dwarf2_objfile_data_key);
dce234bc 2548 struct dwarf2_section_info *info;
a3b2a86b
TT
2549
2550 /* We may see an objfile without any DWARF, in which case we just
2551 return nothing. */
2552 if (data == NULL)
2553 {
2554 *sectp = NULL;
2555 *bufp = NULL;
2556 *sizep = 0;
2557 return;
2558 }
3017a003
TG
2559 switch (sect)
2560 {
2561 case DWARF2_DEBUG_FRAME:
2562 info = &data->frame;
2563 break;
2564 case DWARF2_EH_FRAME:
2565 info = &data->eh_frame;
2566 break;
2567 default:
2568 gdb_assert_not_reached ("unexpected section");
2569 }
dce234bc 2570
9e0ac564 2571 dwarf2_read_section (objfile, info);
dce234bc 2572
a32a8923 2573 *sectp = get_section_bfd_section (info);
dce234bc
PP
2574 *bufp = info->buffer;
2575 *sizep = info->size;
2576}
2577
36586728
TT
2578/* A helper function to find the sections for a .dwz file. */
2579
2580static void
2581locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2582{
9a3c8263 2583 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2584
2585 /* Note that we only support the standard ELF names, because .dwz
2586 is ELF-only (at the time of writing). */
2587 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2588 {
049412e3 2589 dwz_file->abbrev.s.section = sectp;
36586728
TT
2590 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2591 }
2592 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2593 {
049412e3 2594 dwz_file->info.s.section = sectp;
36586728
TT
2595 dwz_file->info.size = bfd_get_section_size (sectp);
2596 }
2597 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2598 {
049412e3 2599 dwz_file->str.s.section = sectp;
36586728
TT
2600 dwz_file->str.size = bfd_get_section_size (sectp);
2601 }
2602 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2603 {
049412e3 2604 dwz_file->line.s.section = sectp;
36586728
TT
2605 dwz_file->line.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2608 {
049412e3 2609 dwz_file->macro.s.section = sectp;
36586728
TT
2610 dwz_file->macro.size = bfd_get_section_size (sectp);
2611 }
2ec9a5e0
TT
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2613 {
049412e3 2614 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2615 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2616 }
36586728
TT
2617}
2618
4db1a1dc
TT
2619/* Open the separate '.dwz' debug file, if needed. Return NULL if
2620 there is no .gnu_debugaltlink section in the file. Error if there
2621 is such a section but the file cannot be found. */
36586728
TT
2622
2623static struct dwz_file *
2624dwarf2_get_dwz_file (void)
2625{
4db1a1dc 2626 char *data;
36586728
TT
2627 struct cleanup *cleanup;
2628 const char *filename;
2629 struct dwz_file *result;
acd13123 2630 bfd_size_type buildid_len_arg;
dc294be5
TT
2631 size_t buildid_len;
2632 bfd_byte *buildid;
36586728
TT
2633
2634 if (dwarf2_per_objfile->dwz_file != NULL)
2635 return dwarf2_per_objfile->dwz_file;
2636
4db1a1dc
TT
2637 bfd_set_error (bfd_error_no_error);
2638 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2639 &buildid_len_arg, &buildid);
4db1a1dc
TT
2640 if (data == NULL)
2641 {
2642 if (bfd_get_error () == bfd_error_no_error)
2643 return NULL;
2644 error (_("could not read '.gnu_debugaltlink' section: %s"),
2645 bfd_errmsg (bfd_get_error ()));
2646 }
36586728 2647 cleanup = make_cleanup (xfree, data);
dc294be5 2648 make_cleanup (xfree, buildid);
36586728 2649
acd13123
TT
2650 buildid_len = (size_t) buildid_len_arg;
2651
f9d83a0b 2652 filename = (const char *) data;
d721ba37
PA
2653
2654 std::string abs_storage;
36586728
TT
2655 if (!IS_ABSOLUTE_PATH (filename))
2656 {
4262abfb 2657 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2658
2659 make_cleanup (xfree, abs);
d721ba37
PA
2660 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2661 filename = abs_storage.c_str ();
36586728
TT
2662 }
2663
dc294be5
TT
2664 /* First try the file name given in the section. If that doesn't
2665 work, try to use the build-id instead. */
192b62ce 2666 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2667 if (dwz_bfd != NULL)
36586728 2668 {
192b62ce
TT
2669 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2670 dwz_bfd.release ();
36586728
TT
2671 }
2672
dc294be5
TT
2673 if (dwz_bfd == NULL)
2674 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2675
2676 if (dwz_bfd == NULL)
2677 error (_("could not find '.gnu_debugaltlink' file for %s"),
2678 objfile_name (dwarf2_per_objfile->objfile));
2679
36586728
TT
2680 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2681 struct dwz_file);
192b62ce 2682 result->dwz_bfd = dwz_bfd.release ();
36586728 2683
192b62ce 2684 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2685
2686 do_cleanups (cleanup);
2687
192b62ce 2688 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2689 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2690 return result;
2691}
9291a0cd 2692\f
7b9f3c50
DE
2693/* DWARF quick_symbols_functions support. */
2694
2695/* TUs can share .debug_line entries, and there can be a lot more TUs than
2696 unique line tables, so we maintain a separate table of all .debug_line
2697 derived entries to support the sharing.
2698 All the quick functions need is the list of file names. We discard the
2699 line_header when we're done and don't need to record it here. */
2700struct quick_file_names
2701{
094b34ac
DE
2702 /* The data used to construct the hash key. */
2703 struct stmt_list_hash hash;
7b9f3c50
DE
2704
2705 /* The number of entries in file_names, real_names. */
2706 unsigned int num_file_names;
2707
2708 /* The file names from the line table, after being run through
2709 file_full_name. */
2710 const char **file_names;
2711
2712 /* The file names from the line table after being run through
2713 gdb_realpath. These are computed lazily. */
2714 const char **real_names;
2715};
2716
2717/* When using the index (and thus not using psymtabs), each CU has an
2718 object of this type. This is used to hold information needed by
2719 the various "quick" methods. */
2720struct dwarf2_per_cu_quick_data
2721{
2722 /* The file table. This can be NULL if there was no file table
2723 or it's currently not read in.
2724 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2725 struct quick_file_names *file_names;
2726
2727 /* The corresponding symbol table. This is NULL if symbols for this
2728 CU have not yet been read. */
43f3e411 2729 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2730
2731 /* A temporary mark bit used when iterating over all CUs in
2732 expand_symtabs_matching. */
2733 unsigned int mark : 1;
2734
2735 /* True if we've tried to read the file table and found there isn't one.
2736 There will be no point in trying to read it again next time. */
2737 unsigned int no_file_data : 1;
2738};
2739
094b34ac
DE
2740/* Utility hash function for a stmt_list_hash. */
2741
2742static hashval_t
2743hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2744{
2745 hashval_t v = 0;
2746
2747 if (stmt_list_hash->dwo_unit != NULL)
2748 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2749 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2750 return v;
2751}
2752
2753/* Utility equality function for a stmt_list_hash. */
2754
2755static int
2756eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2757 const struct stmt_list_hash *rhs)
2758{
2759 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2760 return 0;
2761 if (lhs->dwo_unit != NULL
2762 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2763 return 0;
2764
9c541725 2765 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2766}
2767
7b9f3c50
DE
2768/* Hash function for a quick_file_names. */
2769
2770static hashval_t
2771hash_file_name_entry (const void *e)
2772{
9a3c8263
SM
2773 const struct quick_file_names *file_data
2774 = (const struct quick_file_names *) e;
7b9f3c50 2775
094b34ac 2776 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2777}
2778
2779/* Equality function for a quick_file_names. */
2780
2781static int
2782eq_file_name_entry (const void *a, const void *b)
2783{
9a3c8263
SM
2784 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2785 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2786
094b34ac 2787 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2788}
2789
2790/* Delete function for a quick_file_names. */
2791
2792static void
2793delete_file_name_entry (void *e)
2794{
9a3c8263 2795 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2796 int i;
2797
2798 for (i = 0; i < file_data->num_file_names; ++i)
2799 {
2800 xfree ((void*) file_data->file_names[i]);
2801 if (file_data->real_names)
2802 xfree ((void*) file_data->real_names[i]);
2803 }
2804
2805 /* The space for the struct itself lives on objfile_obstack,
2806 so we don't free it here. */
2807}
2808
2809/* Create a quick_file_names hash table. */
2810
2811static htab_t
2812create_quick_file_names_table (unsigned int nr_initial_entries)
2813{
2814 return htab_create_alloc (nr_initial_entries,
2815 hash_file_name_entry, eq_file_name_entry,
2816 delete_file_name_entry, xcalloc, xfree);
2817}
9291a0cd 2818
918dd910
JK
2819/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2820 have to be created afterwards. You should call age_cached_comp_units after
2821 processing PER_CU->CU. dw2_setup must have been already called. */
2822
2823static void
2824load_cu (struct dwarf2_per_cu_data *per_cu)
2825{
3019eac3 2826 if (per_cu->is_debug_types)
e5fe5e75 2827 load_full_type_unit (per_cu);
918dd910 2828 else
95554aad 2829 load_full_comp_unit (per_cu, language_minimal);
918dd910 2830
cc12ce38
DE
2831 if (per_cu->cu == NULL)
2832 return; /* Dummy CU. */
2dc860c0
DE
2833
2834 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2835}
2836
a0f42c21 2837/* Read in the symbols for PER_CU. */
2fdf6df6 2838
9291a0cd 2839static void
a0f42c21 2840dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2841{
2842 struct cleanup *back_to;
2843
f4dc4d17
DE
2844 /* Skip type_unit_groups, reading the type units they contain
2845 is handled elsewhere. */
2846 if (IS_TYPE_UNIT_GROUP (per_cu))
2847 return;
2848
9291a0cd
TT
2849 back_to = make_cleanup (dwarf2_release_queue, NULL);
2850
95554aad 2851 if (dwarf2_per_objfile->using_index
43f3e411 2852 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2853 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2854 {
2855 queue_comp_unit (per_cu, language_minimal);
2856 load_cu (per_cu);
89e63ee4
DE
2857
2858 /* If we just loaded a CU from a DWO, and we're working with an index
2859 that may badly handle TUs, load all the TUs in that DWO as well.
2860 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2861 if (!per_cu->is_debug_types
cc12ce38 2862 && per_cu->cu != NULL
89e63ee4
DE
2863 && per_cu->cu->dwo_unit != NULL
2864 && dwarf2_per_objfile->index_table != NULL
2865 && dwarf2_per_objfile->index_table->version <= 7
2866 /* DWP files aren't supported yet. */
2867 && get_dwp_file () == NULL)
2868 queue_and_load_all_dwo_tus (per_cu);
95554aad 2869 }
9291a0cd 2870
a0f42c21 2871 process_queue ();
9291a0cd
TT
2872
2873 /* Age the cache, releasing compilation units that have not
2874 been used recently. */
2875 age_cached_comp_units ();
2876
2877 do_cleanups (back_to);
2878}
2879
2880/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2881 the objfile from which this CU came. Returns the resulting symbol
2882 table. */
2fdf6df6 2883
43f3e411 2884static struct compunit_symtab *
a0f42c21 2885dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2886{
95554aad 2887 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2888 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2889 {
2890 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2891 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2892 dw2_do_instantiate_symtab (per_cu);
95554aad 2893 process_cu_includes ();
9291a0cd
TT
2894 do_cleanups (back_to);
2895 }
f194fefb 2896
43f3e411 2897 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2898}
2899
8832e7e3 2900/* Return the CU/TU given its index.
f4dc4d17
DE
2901
2902 This is intended for loops like:
2903
2904 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2905 + dwarf2_per_objfile->n_type_units); ++i)
2906 {
8832e7e3 2907 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2908
2909 ...;
2910 }
2911*/
2fdf6df6 2912
1fd400ff 2913static struct dwarf2_per_cu_data *
8832e7e3 2914dw2_get_cutu (int index)
1fd400ff
TT
2915{
2916 if (index >= dwarf2_per_objfile->n_comp_units)
2917 {
f4dc4d17 2918 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2919 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2920 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2921 }
2922
2923 return dwarf2_per_objfile->all_comp_units[index];
2924}
2925
8832e7e3
DE
2926/* Return the CU given its index.
2927 This differs from dw2_get_cutu in that it's for when you know INDEX
2928 refers to a CU. */
f4dc4d17
DE
2929
2930static struct dwarf2_per_cu_data *
8832e7e3 2931dw2_get_cu (int index)
f4dc4d17 2932{
8832e7e3 2933 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2934
1fd400ff
TT
2935 return dwarf2_per_objfile->all_comp_units[index];
2936}
2937
2ec9a5e0
TT
2938/* A helper for create_cus_from_index that handles a given list of
2939 CUs. */
2fdf6df6 2940
74a0d9f6 2941static void
2ec9a5e0
TT
2942create_cus_from_index_list (struct objfile *objfile,
2943 const gdb_byte *cu_list, offset_type n_elements,
2944 struct dwarf2_section_info *section,
2945 int is_dwz,
2946 int base_offset)
9291a0cd
TT
2947{
2948 offset_type i;
9291a0cd 2949
2ec9a5e0 2950 for (i = 0; i < n_elements; i += 2)
9291a0cd 2951 {
74a0d9f6 2952 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2953
2954 sect_offset sect_off
2955 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2956 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2957 cu_list += 2 * 8;
2958
9c541725
PA
2959 dwarf2_per_cu_data *the_cu
2960 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2961 struct dwarf2_per_cu_data);
2962 the_cu->sect_off = sect_off;
9291a0cd
TT
2963 the_cu->length = length;
2964 the_cu->objfile = objfile;
8a0459fd 2965 the_cu->section = section;
9291a0cd
TT
2966 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2967 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2968 the_cu->is_dwz = is_dwz;
2969 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2970 }
9291a0cd
TT
2971}
2972
2ec9a5e0 2973/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2974 the CU objects for this objfile. */
2ec9a5e0 2975
74a0d9f6 2976static void
2ec9a5e0
TT
2977create_cus_from_index (struct objfile *objfile,
2978 const gdb_byte *cu_list, offset_type cu_list_elements,
2979 const gdb_byte *dwz_list, offset_type dwz_elements)
2980{
2981 struct dwz_file *dwz;
2982
2983 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2984 dwarf2_per_objfile->all_comp_units =
2985 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2986 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2987
74a0d9f6
JK
2988 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2989 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2990
2991 if (dwz_elements == 0)
74a0d9f6 2992 return;
2ec9a5e0
TT
2993
2994 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2995 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2996 cu_list_elements / 2);
2ec9a5e0
TT
2997}
2998
1fd400ff 2999/* Create the signatured type hash table from the index. */
673bfd45 3000
74a0d9f6 3001static void
673bfd45 3002create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3003 struct dwarf2_section_info *section,
673bfd45
DE
3004 const gdb_byte *bytes,
3005 offset_type elements)
1fd400ff
TT
3006{
3007 offset_type i;
673bfd45 3008 htab_t sig_types_hash;
1fd400ff 3009
6aa5f3a6
DE
3010 dwarf2_per_objfile->n_type_units
3011 = dwarf2_per_objfile->n_allocated_type_units
3012 = elements / 3;
8d749320
SM
3013 dwarf2_per_objfile->all_type_units =
3014 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3015
673bfd45 3016 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3017
3018 for (i = 0; i < elements; i += 3)
3019 {
52dc124a 3020 struct signatured_type *sig_type;
9c541725 3021 ULONGEST signature;
1fd400ff 3022 void **slot;
9c541725 3023 cu_offset type_offset_in_tu;
1fd400ff 3024
74a0d9f6 3025 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3026 sect_offset sect_off
3027 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3028 type_offset_in_tu
3029 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3030 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3031 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3032 bytes += 3 * 8;
3033
52dc124a 3034 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3035 struct signatured_type);
52dc124a 3036 sig_type->signature = signature;
9c541725 3037 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3038 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3039 sig_type->per_cu.section = section;
9c541725 3040 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3041 sig_type->per_cu.objfile = objfile;
3042 sig_type->per_cu.v.quick
1fd400ff
TT
3043 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3044 struct dwarf2_per_cu_quick_data);
3045
52dc124a
DE
3046 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3047 *slot = sig_type;
1fd400ff 3048
b4dd5633 3049 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3050 }
3051
673bfd45 3052 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3053}
3054
9291a0cd
TT
3055/* Read the address map data from the mapped index, and use it to
3056 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3057
9291a0cd
TT
3058static void
3059create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3060{
3e29f34a 3061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3062 const gdb_byte *iter, *end;
9291a0cd 3063 struct addrmap *mutable_map;
9291a0cd
TT
3064 CORE_ADDR baseaddr;
3065
8268c778
PA
3066 auto_obstack temp_obstack;
3067
9291a0cd
TT
3068 mutable_map = addrmap_create_mutable (&temp_obstack);
3069
3070 iter = index->address_table;
3071 end = iter + index->address_table_size;
3072
3073 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3074
3075 while (iter < end)
3076 {
3077 ULONGEST hi, lo, cu_index;
3078 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3079 iter += 8;
3080 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3081 iter += 8;
3082 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3083 iter += 4;
f652bce2 3084
24a55014 3085 if (lo > hi)
f652bce2 3086 {
24a55014
DE
3087 complaint (&symfile_complaints,
3088 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3089 hex_string (lo), hex_string (hi));
24a55014 3090 continue;
f652bce2 3091 }
24a55014
DE
3092
3093 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3094 {
3095 complaint (&symfile_complaints,
3096 _(".gdb_index address table has invalid CU number %u"),
3097 (unsigned) cu_index);
24a55014 3098 continue;
f652bce2 3099 }
24a55014 3100
3e29f34a
MR
3101 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3102 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3103 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3104 }
3105
3106 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3107 &objfile->objfile_obstack);
9291a0cd
TT
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 6625 struct cleanup *back_to, *addrmap_cleanup;
21b2bd31 6626 int i;
93311388 6627
b4f54984 6628 if (dwarf_read_debug)
45cfd468
DE
6629 {
6630 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6631 objfile_name (objfile));
45cfd468
DE
6632 }
6633
98bfdba5
PA
6634 dwarf2_per_objfile->reading_partial_symbols = 1;
6635
be391dca 6636 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6637
93311388
DE
6638 /* Any cached compilation units will be linked by the per-objfile
6639 read_in_chain. Make sure to free them when we're done. */
6640 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6641
348e048f
DE
6642 build_type_psymtabs (objfile);
6643
93311388 6644 create_all_comp_units (objfile);
c906108c 6645
60606b2c
TT
6646 /* Create a temporary address map on a temporary obstack. We later
6647 copy this to the final obstack. */
8268c778 6648 auto_obstack temp_obstack;
60606b2c
TT
6649 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6650 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6651
21b2bd31 6652 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6653 {
8832e7e3 6654 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6655
b93601f3 6656 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6657 }
ff013f42 6658
6aa5f3a6
DE
6659 /* This has to wait until we read the CUs, we need the list of DWOs. */
6660 process_skeletonless_type_units (objfile);
6661
6662 /* Now that all TUs have been processed we can fill in the dependencies. */
6663 if (dwarf2_per_objfile->type_unit_groups != NULL)
6664 {
6665 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6666 build_type_psymtab_dependencies, NULL);
6667 }
6668
b4f54984 6669 if (dwarf_read_debug)
6aa5f3a6
DE
6670 print_tu_stats ();
6671
95554aad
TT
6672 set_partial_user (objfile);
6673
ff013f42
JK
6674 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6675 &objfile->objfile_obstack);
60606b2c 6676 discard_cleanups (addrmap_cleanup);
ff013f42 6677
ae038cb0 6678 do_cleanups (back_to);
45cfd468 6679
b4f54984 6680 if (dwarf_read_debug)
45cfd468 6681 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6682 objfile_name (objfile));
ae038cb0
DJ
6683}
6684
3019eac3 6685/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6686
6687static void
dee91e82 6688load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6689 const gdb_byte *info_ptr,
dee91e82
DE
6690 struct die_info *comp_unit_die,
6691 int has_children,
6692 void *data)
ae038cb0 6693{
dee91e82 6694 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6695
95554aad 6696 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6697
ae038cb0
DJ
6698 /* Check if comp unit has_children.
6699 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6700 If not, there's no more debug_info for this comp unit. */
d85a05f0 6701 if (has_children)
dee91e82
DE
6702 load_partial_dies (reader, info_ptr, 0);
6703}
98bfdba5 6704
dee91e82
DE
6705/* Load the partial DIEs for a secondary CU into memory.
6706 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6707
dee91e82
DE
6708static void
6709load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6710{
f4dc4d17
DE
6711 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6712 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6713}
6714
ae038cb0 6715static void
36586728
TT
6716read_comp_units_from_section (struct objfile *objfile,
6717 struct dwarf2_section_info *section,
6718 unsigned int is_dwz,
6719 int *n_allocated,
6720 int *n_comp_units,
6721 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6722{
d521ce57 6723 const gdb_byte *info_ptr;
a32a8923 6724 bfd *abfd = get_section_bfd_owner (section);
be391dca 6725
b4f54984 6726 if (dwarf_read_debug)
bf6af496 6727 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6728 get_section_name (section),
6729 get_section_file_name (section));
bf6af496 6730
36586728 6731 dwarf2_read_section (objfile, section);
ae038cb0 6732
36586728 6733 info_ptr = section->buffer;
6e70227d 6734
36586728 6735 while (info_ptr < section->buffer + section->size)
ae038cb0 6736 {
c764a876 6737 unsigned int length, initial_length_size;
ae038cb0 6738 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6739
9c541725 6740 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0
DJ
6741
6742 /* Read just enough information to find out where the next
6743 compilation unit is. */
36586728 6744 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6745
6746 /* Save the compilation unit for later lookup. */
8d749320 6747 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0 6748 memset (this_cu, 0, sizeof (*this_cu));
9c541725 6749 this_cu->sect_off = sect_off;
c764a876 6750 this_cu->length = length + initial_length_size;
36586728 6751 this_cu->is_dwz = is_dwz;
9291a0cd 6752 this_cu->objfile = objfile;
8a0459fd 6753 this_cu->section = section;
ae038cb0 6754
36586728 6755 if (*n_comp_units == *n_allocated)
ae038cb0 6756 {
36586728 6757 *n_allocated *= 2;
224c3ddb
SM
6758 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6759 *all_comp_units, *n_allocated);
ae038cb0 6760 }
36586728
TT
6761 (*all_comp_units)[*n_comp_units] = this_cu;
6762 ++*n_comp_units;
ae038cb0
DJ
6763
6764 info_ptr = info_ptr + this_cu->length;
6765 }
36586728
TT
6766}
6767
6768/* Create a list of all compilation units in OBJFILE.
6769 This is only done for -readnow and building partial symtabs. */
6770
6771static void
6772create_all_comp_units (struct objfile *objfile)
6773{
6774 int n_allocated;
6775 int n_comp_units;
6776 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6777 struct dwz_file *dwz;
36586728
TT
6778
6779 n_comp_units = 0;
6780 n_allocated = 10;
8d749320 6781 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6782
6783 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6784 &n_allocated, &n_comp_units, &all_comp_units);
6785
4db1a1dc
TT
6786 dwz = dwarf2_get_dwz_file ();
6787 if (dwz != NULL)
6788 read_comp_units_from_section (objfile, &dwz->info, 1,
6789 &n_allocated, &n_comp_units,
6790 &all_comp_units);
ae038cb0 6791
8d749320
SM
6792 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6793 struct dwarf2_per_cu_data *,
6794 n_comp_units);
ae038cb0
DJ
6795 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6796 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6797 xfree (all_comp_units);
6798 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6799}
6800
5734ee8b 6801/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6802 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6803 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6804 DW_AT_ranges). See the comments of add_partial_subprogram on how
6805 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6806
72bf9492
DJ
6807static void
6808scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6809 CORE_ADDR *highpc, int set_addrmap,
6810 struct dwarf2_cu *cu)
c906108c 6811{
72bf9492 6812 struct partial_die_info *pdi;
c906108c 6813
91c24f0a
DC
6814 /* Now, march along the PDI's, descending into ones which have
6815 interesting children but skipping the children of the other ones,
6816 until we reach the end of the compilation unit. */
c906108c 6817
72bf9492 6818 pdi = first_die;
91c24f0a 6819
72bf9492
DJ
6820 while (pdi != NULL)
6821 {
6822 fixup_partial_die (pdi, cu);
c906108c 6823
f55ee35c 6824 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6825 children, so we need to look at them. Ditto for anonymous
6826 enums. */
933c6fe4 6827
72bf9492 6828 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6829 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6830 || pdi->tag == DW_TAG_imported_unit)
c906108c 6831 {
72bf9492 6832 switch (pdi->tag)
c906108c
SS
6833 {
6834 case DW_TAG_subprogram:
cdc07690 6835 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6836 break;
72929c62 6837 case DW_TAG_constant:
c906108c
SS
6838 case DW_TAG_variable:
6839 case DW_TAG_typedef:
91c24f0a 6840 case DW_TAG_union_type:
72bf9492 6841 if (!pdi->is_declaration)
63d06c5c 6842 {
72bf9492 6843 add_partial_symbol (pdi, cu);
63d06c5c
DC
6844 }
6845 break;
c906108c 6846 case DW_TAG_class_type:
680b30c7 6847 case DW_TAG_interface_type:
c906108c 6848 case DW_TAG_structure_type:
72bf9492 6849 if (!pdi->is_declaration)
c906108c 6850 {
72bf9492 6851 add_partial_symbol (pdi, cu);
c906108c 6852 }
e98c9e7c
TT
6853 if (cu->language == language_rust && pdi->has_children)
6854 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6855 set_addrmap, cu);
c906108c 6856 break;
91c24f0a 6857 case DW_TAG_enumeration_type:
72bf9492
DJ
6858 if (!pdi->is_declaration)
6859 add_partial_enumeration (pdi, cu);
c906108c
SS
6860 break;
6861 case DW_TAG_base_type:
a02abb62 6862 case DW_TAG_subrange_type:
c906108c 6863 /* File scope base type definitions are added to the partial
c5aa993b 6864 symbol table. */
72bf9492 6865 add_partial_symbol (pdi, cu);
c906108c 6866 break;
d9fa45fe 6867 case DW_TAG_namespace:
cdc07690 6868 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6869 break;
5d7cb8df 6870 case DW_TAG_module:
cdc07690 6871 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6872 break;
95554aad
TT
6873 case DW_TAG_imported_unit:
6874 {
6875 struct dwarf2_per_cu_data *per_cu;
6876
f4dc4d17
DE
6877 /* For now we don't handle imported units in type units. */
6878 if (cu->per_cu->is_debug_types)
6879 {
6880 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6881 " supported in type units [in module %s]"),
4262abfb 6882 objfile_name (cu->objfile));
f4dc4d17
DE
6883 }
6884
9c541725 6885 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6886 pdi->is_dwz,
95554aad
TT
6887 cu->objfile);
6888
6889 /* Go read the partial unit, if needed. */
6890 if (per_cu->v.psymtab == NULL)
b93601f3 6891 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6892
f4dc4d17 6893 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6894 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6895 }
6896 break;
74921315
KS
6897 case DW_TAG_imported_declaration:
6898 add_partial_symbol (pdi, cu);
6899 break;
c906108c
SS
6900 default:
6901 break;
6902 }
6903 }
6904
72bf9492
DJ
6905 /* If the die has a sibling, skip to the sibling. */
6906
6907 pdi = pdi->die_sibling;
6908 }
6909}
6910
6911/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6912
72bf9492 6913 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6914 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6915 Enumerators are an exception; they use the scope of their parent
6916 enumeration type, i.e. the name of the enumeration type is not
6917 prepended to the enumerator.
91c24f0a 6918
72bf9492
DJ
6919 There are two complexities. One is DW_AT_specification; in this
6920 case "parent" means the parent of the target of the specification,
6921 instead of the direct parent of the DIE. The other is compilers
6922 which do not emit DW_TAG_namespace; in this case we try to guess
6923 the fully qualified name of structure types from their members'
6924 linkage names. This must be done using the DIE's children rather
6925 than the children of any DW_AT_specification target. We only need
6926 to do this for structures at the top level, i.e. if the target of
6927 any DW_AT_specification (if any; otherwise the DIE itself) does not
6928 have a parent. */
6929
6930/* Compute the scope prefix associated with PDI's parent, in
6931 compilation unit CU. The result will be allocated on CU's
6932 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6933 field. NULL is returned if no prefix is necessary. */
15d034d0 6934static const char *
72bf9492
DJ
6935partial_die_parent_scope (struct partial_die_info *pdi,
6936 struct dwarf2_cu *cu)
6937{
15d034d0 6938 const char *grandparent_scope;
72bf9492 6939 struct partial_die_info *parent, *real_pdi;
91c24f0a 6940
72bf9492
DJ
6941 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6942 then this means the parent of the specification DIE. */
6943
6944 real_pdi = pdi;
72bf9492 6945 while (real_pdi->has_specification)
36586728
TT
6946 real_pdi = find_partial_die (real_pdi->spec_offset,
6947 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6948
6949 parent = real_pdi->die_parent;
6950 if (parent == NULL)
6951 return NULL;
6952
6953 if (parent->scope_set)
6954 return parent->scope;
6955
6956 fixup_partial_die (parent, cu);
6957
10b3939b 6958 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6959
acebe513
UW
6960 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6961 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6962 Work around this problem here. */
6963 if (cu->language == language_cplus
6e70227d 6964 && parent->tag == DW_TAG_namespace
acebe513
UW
6965 && strcmp (parent->name, "::") == 0
6966 && grandparent_scope == NULL)
6967 {
6968 parent->scope = NULL;
6969 parent->scope_set = 1;
6970 return NULL;
6971 }
6972
9c6c53f7
SA
6973 if (pdi->tag == DW_TAG_enumerator)
6974 /* Enumerators should not get the name of the enumeration as a prefix. */
6975 parent->scope = grandparent_scope;
6976 else if (parent->tag == DW_TAG_namespace
f55ee35c 6977 || parent->tag == DW_TAG_module
72bf9492
DJ
6978 || parent->tag == DW_TAG_structure_type
6979 || parent->tag == DW_TAG_class_type
680b30c7 6980 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6981 || parent->tag == DW_TAG_union_type
6982 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6983 {
6984 if (grandparent_scope == NULL)
6985 parent->scope = parent->name;
6986 else
3e43a32a
MS
6987 parent->scope = typename_concat (&cu->comp_unit_obstack,
6988 grandparent_scope,
f55ee35c 6989 parent->name, 0, cu);
72bf9492 6990 }
72bf9492
DJ
6991 else
6992 {
6993 /* FIXME drow/2004-04-01: What should we be doing with
6994 function-local names? For partial symbols, we should probably be
6995 ignoring them. */
6996 complaint (&symfile_complaints,
e2e0b3e5 6997 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 6998 parent->tag, to_underlying (pdi->sect_off));
72bf9492 6999 parent->scope = grandparent_scope;
c906108c
SS
7000 }
7001
72bf9492
DJ
7002 parent->scope_set = 1;
7003 return parent->scope;
7004}
7005
7006/* Return the fully scoped name associated with PDI, from compilation unit
7007 CU. The result will be allocated with malloc. */
4568ecf9 7008
72bf9492
DJ
7009static char *
7010partial_die_full_name (struct partial_die_info *pdi,
7011 struct dwarf2_cu *cu)
7012{
15d034d0 7013 const char *parent_scope;
72bf9492 7014
98bfdba5
PA
7015 /* If this is a template instantiation, we can not work out the
7016 template arguments from partial DIEs. So, unfortunately, we have
7017 to go through the full DIEs. At least any work we do building
7018 types here will be reused if full symbols are loaded later. */
7019 if (pdi->has_template_arguments)
7020 {
7021 fixup_partial_die (pdi, cu);
7022
7023 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7024 {
7025 struct die_info *die;
7026 struct attribute attr;
7027 struct dwarf2_cu *ref_cu = cu;
7028
b64f50a1 7029 /* DW_FORM_ref_addr is using section offset. */
b4069958 7030 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7031 attr.form = DW_FORM_ref_addr;
9c541725 7032 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7033 die = follow_die_ref (NULL, &attr, &ref_cu);
7034
7035 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7036 }
7037 }
7038
72bf9492
DJ
7039 parent_scope = partial_die_parent_scope (pdi, cu);
7040 if (parent_scope == NULL)
7041 return NULL;
7042 else
f55ee35c 7043 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7044}
7045
7046static void
72bf9492 7047add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7048{
e7c27a73 7049 struct objfile *objfile = cu->objfile;
3e29f34a 7050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7051 CORE_ADDR addr = 0;
15d034d0 7052 const char *actual_name = NULL;
e142c38c 7053 CORE_ADDR baseaddr;
15d034d0 7054 char *built_actual_name;
e142c38c
DJ
7055
7056 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7057
15d034d0
TT
7058 built_actual_name = partial_die_full_name (pdi, cu);
7059 if (built_actual_name != NULL)
7060 actual_name = built_actual_name;
63d06c5c 7061
72bf9492
DJ
7062 if (actual_name == NULL)
7063 actual_name = pdi->name;
7064
c906108c
SS
7065 switch (pdi->tag)
7066 {
7067 case DW_TAG_subprogram:
3e29f34a 7068 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7069 if (pdi->is_external || cu->language == language_ada)
c906108c 7070 {
2cfa0c8d
JB
7071 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7072 of the global scope. But in Ada, we want to be able to access
7073 nested procedures globally. So all Ada subprograms are stored
7074 in the global scope. */
f47fb265 7075 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7076 built_actual_name != NULL,
f47fb265
MS
7077 VAR_DOMAIN, LOC_BLOCK,
7078 &objfile->global_psymbols,
1762568f 7079 addr, cu->language, objfile);
c906108c
SS
7080 }
7081 else
7082 {
f47fb265 7083 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7084 built_actual_name != NULL,
f47fb265
MS
7085 VAR_DOMAIN, LOC_BLOCK,
7086 &objfile->static_psymbols,
1762568f 7087 addr, cu->language, objfile);
c906108c 7088 }
0c1b455e
TT
7089
7090 if (pdi->main_subprogram && actual_name != NULL)
7091 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7092 break;
72929c62
JB
7093 case DW_TAG_constant:
7094 {
7095 struct psymbol_allocation_list *list;
7096
7097 if (pdi->is_external)
7098 list = &objfile->global_psymbols;
7099 else
7100 list = &objfile->static_psymbols;
f47fb265 7101 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7102 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7103 list, 0, cu->language, objfile);
72929c62
JB
7104 }
7105 break;
c906108c 7106 case DW_TAG_variable:
95554aad
TT
7107 if (pdi->d.locdesc)
7108 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7109
95554aad 7110 if (pdi->d.locdesc
caac4577
JG
7111 && addr == 0
7112 && !dwarf2_per_objfile->has_section_at_zero)
7113 {
7114 /* A global or static variable may also have been stripped
7115 out by the linker if unused, in which case its address
7116 will be nullified; do not add such variables into partial
7117 symbol table then. */
7118 }
7119 else if (pdi->is_external)
c906108c
SS
7120 {
7121 /* Global Variable.
7122 Don't enter into the minimal symbol tables as there is
7123 a minimal symbol table entry from the ELF symbols already.
7124 Enter into partial symbol table if it has a location
7125 descriptor or a type.
7126 If the location descriptor is missing, new_symbol will create
7127 a LOC_UNRESOLVED symbol, the address of the variable will then
7128 be determined from the minimal symbol table whenever the variable
7129 is referenced.
7130 The address for the partial symbol table entry is not
7131 used by GDB, but it comes in handy for debugging partial symbol
7132 table building. */
7133
95554aad 7134 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7135 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7136 built_actual_name != NULL,
f47fb265
MS
7137 VAR_DOMAIN, LOC_STATIC,
7138 &objfile->global_psymbols,
1762568f 7139 addr + baseaddr,
f47fb265 7140 cu->language, objfile);
c906108c
SS
7141 }
7142 else
7143 {
ff908ebf
AW
7144 int has_loc = pdi->d.locdesc != NULL;
7145
7146 /* Static Variable. Skip symbols whose value we cannot know (those
7147 without location descriptors or constant values). */
7148 if (!has_loc && !pdi->has_const_value)
decbce07 7149 {
15d034d0 7150 xfree (built_actual_name);
decbce07
MS
7151 return;
7152 }
ff908ebf 7153
f47fb265 7154 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7155 built_actual_name != NULL,
f47fb265
MS
7156 VAR_DOMAIN, LOC_STATIC,
7157 &objfile->static_psymbols,
ff908ebf 7158 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7159 cu->language, objfile);
c906108c
SS
7160 }
7161 break;
7162 case DW_TAG_typedef:
7163 case DW_TAG_base_type:
a02abb62 7164 case DW_TAG_subrange_type:
38d518c9 7165 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7166 built_actual_name != NULL,
176620f1 7167 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7168 &objfile->static_psymbols,
1762568f 7169 0, cu->language, objfile);
c906108c 7170 break;
74921315 7171 case DW_TAG_imported_declaration:
72bf9492
DJ
7172 case DW_TAG_namespace:
7173 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7174 built_actual_name != NULL,
72bf9492
DJ
7175 VAR_DOMAIN, LOC_TYPEDEF,
7176 &objfile->global_psymbols,
1762568f 7177 0, cu->language, objfile);
72bf9492 7178 break;
530e8392
KB
7179 case DW_TAG_module:
7180 add_psymbol_to_list (actual_name, strlen (actual_name),
7181 built_actual_name != NULL,
7182 MODULE_DOMAIN, LOC_TYPEDEF,
7183 &objfile->global_psymbols,
1762568f 7184 0, cu->language, objfile);
530e8392 7185 break;
c906108c 7186 case DW_TAG_class_type:
680b30c7 7187 case DW_TAG_interface_type:
c906108c
SS
7188 case DW_TAG_structure_type:
7189 case DW_TAG_union_type:
7190 case DW_TAG_enumeration_type:
fa4028e9
JB
7191 /* Skip external references. The DWARF standard says in the section
7192 about "Structure, Union, and Class Type Entries": "An incomplete
7193 structure, union or class type is represented by a structure,
7194 union or class entry that does not have a byte size attribute
7195 and that has a DW_AT_declaration attribute." */
7196 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7197 {
15d034d0 7198 xfree (built_actual_name);
decbce07
MS
7199 return;
7200 }
fa4028e9 7201
63d06c5c
DC
7202 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7203 static vs. global. */
38d518c9 7204 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7205 built_actual_name != NULL,
176620f1 7206 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7207 cu->language == language_cplus
63d06c5c
DC
7208 ? &objfile->global_psymbols
7209 : &objfile->static_psymbols,
1762568f 7210 0, cu->language, objfile);
c906108c 7211
c906108c
SS
7212 break;
7213 case DW_TAG_enumerator:
38d518c9 7214 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7215 built_actual_name != NULL,
176620f1 7216 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7217 cu->language == language_cplus
f6fe98ef
DJ
7218 ? &objfile->global_psymbols
7219 : &objfile->static_psymbols,
1762568f 7220 0, cu->language, objfile);
c906108c
SS
7221 break;
7222 default:
7223 break;
7224 }
5c4e30ca 7225
15d034d0 7226 xfree (built_actual_name);
c906108c
SS
7227}
7228
5c4e30ca
DC
7229/* Read a partial die corresponding to a namespace; also, add a symbol
7230 corresponding to that namespace to the symbol table. NAMESPACE is
7231 the name of the enclosing namespace. */
91c24f0a 7232
72bf9492
DJ
7233static void
7234add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7236 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7237{
72bf9492 7238 /* Add a symbol for the namespace. */
e7c27a73 7239
72bf9492 7240 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7241
7242 /* Now scan partial symbols in that namespace. */
7243
91c24f0a 7244 if (pdi->has_children)
cdc07690 7245 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7246}
7247
5d7cb8df
JK
7248/* Read a partial die corresponding to a Fortran module. */
7249
7250static void
7251add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7252 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7253{
530e8392
KB
7254 /* Add a symbol for the namespace. */
7255
7256 add_partial_symbol (pdi, cu);
7257
f55ee35c 7258 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7259
7260 if (pdi->has_children)
cdc07690 7261 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7262}
7263
bc30ff58
JB
7264/* Read a partial die corresponding to a subprogram and create a partial
7265 symbol for that subprogram. When the CU language allows it, this
7266 routine also defines a partial symbol for each nested subprogram
cdc07690 7267 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7268 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7269 and highest PC values found in PDI.
6e70227d 7270
cdc07690
YQ
7271 PDI may also be a lexical block, in which case we simply search
7272 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7273 Again, this is only performed when the CU language allows this
7274 type of definitions. */
7275
7276static void
7277add_partial_subprogram (struct partial_die_info *pdi,
7278 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7279 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7280{
7281 if (pdi->tag == DW_TAG_subprogram)
7282 {
7283 if (pdi->has_pc_info)
7284 {
7285 if (pdi->lowpc < *lowpc)
7286 *lowpc = pdi->lowpc;
7287 if (pdi->highpc > *highpc)
7288 *highpc = pdi->highpc;
cdc07690 7289 if (set_addrmap)
5734ee8b 7290 {
5734ee8b 7291 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7293 CORE_ADDR baseaddr;
7294 CORE_ADDR highpc;
7295 CORE_ADDR lowpc;
5734ee8b
DJ
7296
7297 baseaddr = ANOFFSET (objfile->section_offsets,
7298 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7299 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7300 pdi->lowpc + baseaddr);
7301 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7302 pdi->highpc + baseaddr);
7303 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7304 cu->per_cu->v.psymtab);
5734ee8b 7305 }
481860b3
GB
7306 }
7307
7308 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7309 {
bc30ff58 7310 if (!pdi->is_declaration)
e8d05480
JB
7311 /* Ignore subprogram DIEs that do not have a name, they are
7312 illegal. Do not emit a complaint at this point, we will
7313 do so when we convert this psymtab into a symtab. */
7314 if (pdi->name)
7315 add_partial_symbol (pdi, cu);
bc30ff58
JB
7316 }
7317 }
6e70227d 7318
bc30ff58
JB
7319 if (! pdi->has_children)
7320 return;
7321
7322 if (cu->language == language_ada)
7323 {
7324 pdi = pdi->die_child;
7325 while (pdi != NULL)
7326 {
7327 fixup_partial_die (pdi, cu);
7328 if (pdi->tag == DW_TAG_subprogram
7329 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7330 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7331 pdi = pdi->die_sibling;
7332 }
7333 }
7334}
7335
91c24f0a
DC
7336/* Read a partial die corresponding to an enumeration type. */
7337
72bf9492
DJ
7338static void
7339add_partial_enumeration (struct partial_die_info *enum_pdi,
7340 struct dwarf2_cu *cu)
91c24f0a 7341{
72bf9492 7342 struct partial_die_info *pdi;
91c24f0a
DC
7343
7344 if (enum_pdi->name != NULL)
72bf9492
DJ
7345 add_partial_symbol (enum_pdi, cu);
7346
7347 pdi = enum_pdi->die_child;
7348 while (pdi)
91c24f0a 7349 {
72bf9492 7350 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7351 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7352 else
72bf9492
DJ
7353 add_partial_symbol (pdi, cu);
7354 pdi = pdi->die_sibling;
91c24f0a 7355 }
91c24f0a
DC
7356}
7357
6caca83c
CC
7358/* Return the initial uleb128 in the die at INFO_PTR. */
7359
7360static unsigned int
d521ce57 7361peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7362{
7363 unsigned int bytes_read;
7364
7365 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7366}
7367
4bb7a0a7
DJ
7368/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7369 Return the corresponding abbrev, or NULL if the number is zero (indicating
7370 an empty DIE). In either case *BYTES_READ will be set to the length of
7371 the initial number. */
7372
7373static struct abbrev_info *
d521ce57 7374peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7375 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7376{
7377 bfd *abfd = cu->objfile->obfd;
7378 unsigned int abbrev_number;
7379 struct abbrev_info *abbrev;
7380
7381 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7382
7383 if (abbrev_number == 0)
7384 return NULL;
7385
433df2d4 7386 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7387 if (!abbrev)
7388 {
422b9917
DE
7389 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7390 " at offset 0x%x [in module %s]"),
7391 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7392 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7393 }
7394
7395 return abbrev;
7396}
7397
93311388
DE
7398/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7399 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7400 DIE. Any children of the skipped DIEs will also be skipped. */
7401
d521ce57
TT
7402static const gdb_byte *
7403skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7404{
dee91e82 7405 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7406 struct abbrev_info *abbrev;
7407 unsigned int bytes_read;
7408
7409 while (1)
7410 {
7411 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7412 if (abbrev == NULL)
7413 return info_ptr + bytes_read;
7414 else
dee91e82 7415 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7416 }
7417}
7418
93311388
DE
7419/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7420 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7421 abbrev corresponding to that skipped uleb128 should be passed in
7422 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7423 children. */
7424
d521ce57
TT
7425static const gdb_byte *
7426skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7427 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7428{
7429 unsigned int bytes_read;
7430 struct attribute attr;
dee91e82
DE
7431 bfd *abfd = reader->abfd;
7432 struct dwarf2_cu *cu = reader->cu;
d521ce57 7433 const gdb_byte *buffer = reader->buffer;
f664829e 7434 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7435 unsigned int form, i;
7436
7437 for (i = 0; i < abbrev->num_attrs; i++)
7438 {
7439 /* The only abbrev we care about is DW_AT_sibling. */
7440 if (abbrev->attrs[i].name == DW_AT_sibling)
7441 {
dee91e82 7442 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7443 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7444 complaint (&symfile_complaints,
7445 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7446 else
b9502d3f 7447 {
9c541725
PA
7448 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7449 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7450
7451 if (sibling_ptr < info_ptr)
7452 complaint (&symfile_complaints,
7453 _("DW_AT_sibling points backwards"));
22869d73
KS
7454 else if (sibling_ptr > reader->buffer_end)
7455 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7456 else
7457 return sibling_ptr;
7458 }
4bb7a0a7
DJ
7459 }
7460
7461 /* If it isn't DW_AT_sibling, skip this attribute. */
7462 form = abbrev->attrs[i].form;
7463 skip_attribute:
7464 switch (form)
7465 {
4bb7a0a7 7466 case DW_FORM_ref_addr:
ae411497
TT
7467 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7468 and later it is offset sized. */
7469 if (cu->header.version == 2)
7470 info_ptr += cu->header.addr_size;
7471 else
7472 info_ptr += cu->header.offset_size;
7473 break;
36586728
TT
7474 case DW_FORM_GNU_ref_alt:
7475 info_ptr += cu->header.offset_size;
7476 break;
ae411497 7477 case DW_FORM_addr:
4bb7a0a7
DJ
7478 info_ptr += cu->header.addr_size;
7479 break;
7480 case DW_FORM_data1:
7481 case DW_FORM_ref1:
7482 case DW_FORM_flag:
7483 info_ptr += 1;
7484 break;
2dc7f7b3 7485 case DW_FORM_flag_present:
43988095 7486 case DW_FORM_implicit_const:
2dc7f7b3 7487 break;
4bb7a0a7
DJ
7488 case DW_FORM_data2:
7489 case DW_FORM_ref2:
7490 info_ptr += 2;
7491 break;
7492 case DW_FORM_data4:
7493 case DW_FORM_ref4:
7494 info_ptr += 4;
7495 break;
7496 case DW_FORM_data8:
7497 case DW_FORM_ref8:
55f1336d 7498 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7499 info_ptr += 8;
7500 break;
0224619f
JK
7501 case DW_FORM_data16:
7502 info_ptr += 16;
7503 break;
4bb7a0a7 7504 case DW_FORM_string:
9b1c24c8 7505 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7506 info_ptr += bytes_read;
7507 break;
2dc7f7b3 7508 case DW_FORM_sec_offset:
4bb7a0a7 7509 case DW_FORM_strp:
36586728 7510 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7511 info_ptr += cu->header.offset_size;
7512 break;
2dc7f7b3 7513 case DW_FORM_exprloc:
4bb7a0a7
DJ
7514 case DW_FORM_block:
7515 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7516 info_ptr += bytes_read;
7517 break;
7518 case DW_FORM_block1:
7519 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7520 break;
7521 case DW_FORM_block2:
7522 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7523 break;
7524 case DW_FORM_block4:
7525 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7526 break;
7527 case DW_FORM_sdata:
7528 case DW_FORM_udata:
7529 case DW_FORM_ref_udata:
3019eac3
DE
7530 case DW_FORM_GNU_addr_index:
7531 case DW_FORM_GNU_str_index:
d521ce57 7532 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7533 break;
7534 case DW_FORM_indirect:
7535 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7536 info_ptr += bytes_read;
7537 /* We need to continue parsing from here, so just go back to
7538 the top. */
7539 goto skip_attribute;
7540
7541 default:
3e43a32a
MS
7542 error (_("Dwarf Error: Cannot handle %s "
7543 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7544 dwarf_form_name (form),
7545 bfd_get_filename (abfd));
7546 }
7547 }
7548
7549 if (abbrev->has_children)
dee91e82 7550 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7551 else
7552 return info_ptr;
7553}
7554
93311388 7555/* Locate ORIG_PDI's sibling.
dee91e82 7556 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7557
d521ce57 7558static const gdb_byte *
dee91e82
DE
7559locate_pdi_sibling (const struct die_reader_specs *reader,
7560 struct partial_die_info *orig_pdi,
d521ce57 7561 const gdb_byte *info_ptr)
91c24f0a
DC
7562{
7563 /* Do we know the sibling already? */
72bf9492 7564
91c24f0a
DC
7565 if (orig_pdi->sibling)
7566 return orig_pdi->sibling;
7567
7568 /* Are there any children to deal with? */
7569
7570 if (!orig_pdi->has_children)
7571 return info_ptr;
7572
4bb7a0a7 7573 /* Skip the children the long way. */
91c24f0a 7574
dee91e82 7575 return skip_children (reader, info_ptr);
91c24f0a
DC
7576}
7577
257e7a09 7578/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7579 not NULL. */
c906108c
SS
7580
7581static void
257e7a09
YQ
7582dwarf2_read_symtab (struct partial_symtab *self,
7583 struct objfile *objfile)
c906108c 7584{
257e7a09 7585 if (self->readin)
c906108c 7586 {
442e4d9c 7587 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7588 self->filename);
442e4d9c
YQ
7589 }
7590 else
7591 {
7592 if (info_verbose)
c906108c 7593 {
442e4d9c 7594 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7595 self->filename);
442e4d9c 7596 gdb_flush (gdb_stdout);
c906108c 7597 }
c906108c 7598
442e4d9c 7599 /* Restore our global data. */
9a3c8263
SM
7600 dwarf2_per_objfile
7601 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7602 dwarf2_objfile_data_key);
10b3939b 7603
442e4d9c
YQ
7604 /* If this psymtab is constructed from a debug-only objfile, the
7605 has_section_at_zero flag will not necessarily be correct. We
7606 can get the correct value for this flag by looking at the data
7607 associated with the (presumably stripped) associated objfile. */
7608 if (objfile->separate_debug_objfile_backlink)
7609 {
7610 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7611 = ((struct dwarf2_per_objfile *)
7612 objfile_data (objfile->separate_debug_objfile_backlink,
7613 dwarf2_objfile_data_key));
9a619af0 7614
442e4d9c
YQ
7615 dwarf2_per_objfile->has_section_at_zero
7616 = dpo_backlink->has_section_at_zero;
7617 }
b2ab525c 7618
442e4d9c 7619 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7620
257e7a09 7621 psymtab_to_symtab_1 (self);
c906108c 7622
442e4d9c
YQ
7623 /* Finish up the debug error message. */
7624 if (info_verbose)
7625 printf_filtered (_("done.\n"));
c906108c 7626 }
95554aad
TT
7627
7628 process_cu_includes ();
c906108c 7629}
9cdd5dbd
DE
7630\f
7631/* Reading in full CUs. */
c906108c 7632
10b3939b
DJ
7633/* Add PER_CU to the queue. */
7634
7635static void
95554aad
TT
7636queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7637 enum language pretend_language)
10b3939b
DJ
7638{
7639 struct dwarf2_queue_item *item;
7640
7641 per_cu->queued = 1;
8d749320 7642 item = XNEW (struct dwarf2_queue_item);
10b3939b 7643 item->per_cu = per_cu;
95554aad 7644 item->pretend_language = pretend_language;
10b3939b
DJ
7645 item->next = NULL;
7646
7647 if (dwarf2_queue == NULL)
7648 dwarf2_queue = item;
7649 else
7650 dwarf2_queue_tail->next = item;
7651
7652 dwarf2_queue_tail = item;
7653}
7654
89e63ee4
DE
7655/* If PER_CU is not yet queued, add it to the queue.
7656 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7657 dependency.
0907af0c 7658 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7659 meaning either PER_CU is already queued or it is already loaded.
7660
7661 N.B. There is an invariant here that if a CU is queued then it is loaded.
7662 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7663
7664static int
89e63ee4 7665maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7666 struct dwarf2_per_cu_data *per_cu,
7667 enum language pretend_language)
7668{
7669 /* We may arrive here during partial symbol reading, if we need full
7670 DIEs to process an unusual case (e.g. template arguments). Do
7671 not queue PER_CU, just tell our caller to load its DIEs. */
7672 if (dwarf2_per_objfile->reading_partial_symbols)
7673 {
7674 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7675 return 1;
7676 return 0;
7677 }
7678
7679 /* Mark the dependence relation so that we don't flush PER_CU
7680 too early. */
89e63ee4
DE
7681 if (dependent_cu != NULL)
7682 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7683
7684 /* If it's already on the queue, we have nothing to do. */
7685 if (per_cu->queued)
7686 return 0;
7687
7688 /* If the compilation unit is already loaded, just mark it as
7689 used. */
7690 if (per_cu->cu != NULL)
7691 {
7692 per_cu->cu->last_used = 0;
7693 return 0;
7694 }
7695
7696 /* Add it to the queue. */
7697 queue_comp_unit (per_cu, pretend_language);
7698
7699 return 1;
7700}
7701
10b3939b
DJ
7702/* Process the queue. */
7703
7704static void
a0f42c21 7705process_queue (void)
10b3939b
DJ
7706{
7707 struct dwarf2_queue_item *item, *next_item;
7708
b4f54984 7709 if (dwarf_read_debug)
45cfd468
DE
7710 {
7711 fprintf_unfiltered (gdb_stdlog,
7712 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7713 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7714 }
7715
03dd20cc
DJ
7716 /* The queue starts out with one item, but following a DIE reference
7717 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7718 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7719 {
cc12ce38
DE
7720 if ((dwarf2_per_objfile->using_index
7721 ? !item->per_cu->v.quick->compunit_symtab
7722 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7723 /* Skip dummy CUs. */
7724 && item->per_cu->cu != NULL)
f4dc4d17
DE
7725 {
7726 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7727 unsigned int debug_print_threshold;
247f5c4f 7728 char buf[100];
f4dc4d17 7729
247f5c4f 7730 if (per_cu->is_debug_types)
f4dc4d17 7731 {
247f5c4f
DE
7732 struct signatured_type *sig_type =
7733 (struct signatured_type *) per_cu;
7734
7735 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7736 hex_string (sig_type->signature),
9c541725 7737 to_underlying (per_cu->sect_off));
73be47f5
DE
7738 /* There can be 100s of TUs.
7739 Only print them in verbose mode. */
7740 debug_print_threshold = 2;
f4dc4d17 7741 }
247f5c4f 7742 else
73be47f5 7743 {
9c541725
PA
7744 sprintf (buf, "CU at offset 0x%x",
7745 to_underlying (per_cu->sect_off));
73be47f5
DE
7746 debug_print_threshold = 1;
7747 }
247f5c4f 7748
b4f54984 7749 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7750 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7751
7752 if (per_cu->is_debug_types)
7753 process_full_type_unit (per_cu, item->pretend_language);
7754 else
7755 process_full_comp_unit (per_cu, item->pretend_language);
7756
b4f54984 7757 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7758 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7759 }
10b3939b
DJ
7760
7761 item->per_cu->queued = 0;
7762 next_item = item->next;
7763 xfree (item);
7764 }
7765
7766 dwarf2_queue_tail = NULL;
45cfd468 7767
b4f54984 7768 if (dwarf_read_debug)
45cfd468
DE
7769 {
7770 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7771 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7772 }
10b3939b
DJ
7773}
7774
7775/* Free all allocated queue entries. This function only releases anything if
7776 an error was thrown; if the queue was processed then it would have been
7777 freed as we went along. */
7778
7779static void
7780dwarf2_release_queue (void *dummy)
7781{
7782 struct dwarf2_queue_item *item, *last;
7783
7784 item = dwarf2_queue;
7785 while (item)
7786 {
7787 /* Anything still marked queued is likely to be in an
7788 inconsistent state, so discard it. */
7789 if (item->per_cu->queued)
7790 {
7791 if (item->per_cu->cu != NULL)
dee91e82 7792 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7793 item->per_cu->queued = 0;
7794 }
7795
7796 last = item;
7797 item = item->next;
7798 xfree (last);
7799 }
7800
7801 dwarf2_queue = dwarf2_queue_tail = NULL;
7802}
7803
7804/* Read in full symbols for PST, and anything it depends on. */
7805
c906108c 7806static void
fba45db2 7807psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7808{
10b3939b 7809 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7810 int i;
7811
95554aad
TT
7812 if (pst->readin)
7813 return;
7814
aaa75496 7815 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7816 if (!pst->dependencies[i]->readin
7817 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7818 {
7819 /* Inform about additional files that need to be read in. */
7820 if (info_verbose)
7821 {
a3f17187 7822 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7823 fputs_filtered (" ", gdb_stdout);
7824 wrap_here ("");
7825 fputs_filtered ("and ", gdb_stdout);
7826 wrap_here ("");
7827 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7828 wrap_here (""); /* Flush output. */
aaa75496
JB
7829 gdb_flush (gdb_stdout);
7830 }
7831 psymtab_to_symtab_1 (pst->dependencies[i]);
7832 }
7833
9a3c8263 7834 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7835
7836 if (per_cu == NULL)
aaa75496
JB
7837 {
7838 /* It's an include file, no symbols to read for it.
7839 Everything is in the parent symtab. */
7840 pst->readin = 1;
7841 return;
7842 }
c906108c 7843
a0f42c21 7844 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7845}
7846
dee91e82
DE
7847/* Trivial hash function for die_info: the hash value of a DIE
7848 is its offset in .debug_info for this objfile. */
10b3939b 7849
dee91e82
DE
7850static hashval_t
7851die_hash (const void *item)
10b3939b 7852{
9a3c8263 7853 const struct die_info *die = (const struct die_info *) item;
6502dd73 7854
9c541725 7855 return to_underlying (die->sect_off);
dee91e82 7856}
63d06c5c 7857
dee91e82
DE
7858/* Trivial comparison function for die_info structures: two DIEs
7859 are equal if they have the same offset. */
98bfdba5 7860
dee91e82
DE
7861static int
7862die_eq (const void *item_lhs, const void *item_rhs)
7863{
9a3c8263
SM
7864 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7865 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7866
9c541725 7867 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7868}
c906108c 7869
dee91e82
DE
7870/* die_reader_func for load_full_comp_unit.
7871 This is identical to read_signatured_type_reader,
7872 but is kept separate for now. */
c906108c 7873
dee91e82
DE
7874static void
7875load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7876 const gdb_byte *info_ptr,
dee91e82
DE
7877 struct die_info *comp_unit_die,
7878 int has_children,
7879 void *data)
7880{
7881 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7882 enum language *language_ptr = (enum language *) data;
6caca83c 7883
dee91e82
DE
7884 gdb_assert (cu->die_hash == NULL);
7885 cu->die_hash =
7886 htab_create_alloc_ex (cu->header.length / 12,
7887 die_hash,
7888 die_eq,
7889 NULL,
7890 &cu->comp_unit_obstack,
7891 hashtab_obstack_allocate,
7892 dummy_obstack_deallocate);
e142c38c 7893
dee91e82
DE
7894 if (has_children)
7895 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7896 &info_ptr, comp_unit_die);
7897 cu->dies = comp_unit_die;
7898 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7899
7900 /* We try not to read any attributes in this function, because not
9cdd5dbd 7901 all CUs needed for references have been loaded yet, and symbol
10b3939b 7902 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7903 or we won't be able to build types correctly.
7904 Similarly, if we do not read the producer, we can not apply
7905 producer-specific interpretation. */
95554aad 7906 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7907}
10b3939b 7908
dee91e82 7909/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7910
dee91e82 7911static void
95554aad
TT
7912load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7913 enum language pretend_language)
dee91e82 7914{
3019eac3 7915 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7916
f4dc4d17
DE
7917 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7918 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7919}
7920
3da10d80
KS
7921/* Add a DIE to the delayed physname list. */
7922
7923static void
7924add_to_method_list (struct type *type, int fnfield_index, int index,
7925 const char *name, struct die_info *die,
7926 struct dwarf2_cu *cu)
7927{
7928 struct delayed_method_info mi;
7929 mi.type = type;
7930 mi.fnfield_index = fnfield_index;
7931 mi.index = index;
7932 mi.name = name;
7933 mi.die = die;
7934 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7935}
7936
7937/* A cleanup for freeing the delayed method list. */
7938
7939static void
7940free_delayed_list (void *ptr)
7941{
7942 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7943 if (cu->method_list != NULL)
7944 {
7945 VEC_free (delayed_method_info, cu->method_list);
7946 cu->method_list = NULL;
7947 }
7948}
7949
7950/* Compute the physnames of any methods on the CU's method list.
7951
7952 The computation of method physnames is delayed in order to avoid the
7953 (bad) condition that one of the method's formal parameters is of an as yet
7954 incomplete type. */
7955
7956static void
7957compute_delayed_physnames (struct dwarf2_cu *cu)
7958{
7959 int i;
7960 struct delayed_method_info *mi;
7961 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7962 {
1d06ead6 7963 const char *physname;
3da10d80
KS
7964 struct fn_fieldlist *fn_flp
7965 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7966 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7967 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7968 = physname ? physname : "";
3da10d80
KS
7969 }
7970}
7971
a766d390
DE
7972/* Go objects should be embedded in a DW_TAG_module DIE,
7973 and it's not clear if/how imported objects will appear.
7974 To keep Go support simple until that's worked out,
7975 go back through what we've read and create something usable.
7976 We could do this while processing each DIE, and feels kinda cleaner,
7977 but that way is more invasive.
7978 This is to, for example, allow the user to type "p var" or "b main"
7979 without having to specify the package name, and allow lookups
7980 of module.object to work in contexts that use the expression
7981 parser. */
7982
7983static void
7984fixup_go_packaging (struct dwarf2_cu *cu)
7985{
7986 char *package_name = NULL;
7987 struct pending *list;
7988 int i;
7989
7990 for (list = global_symbols; list != NULL; list = list->next)
7991 {
7992 for (i = 0; i < list->nsyms; ++i)
7993 {
7994 struct symbol *sym = list->symbol[i];
7995
7996 if (SYMBOL_LANGUAGE (sym) == language_go
7997 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7998 {
7999 char *this_package_name = go_symbol_package_name (sym);
8000
8001 if (this_package_name == NULL)
8002 continue;
8003 if (package_name == NULL)
8004 package_name = this_package_name;
8005 else
8006 {
8007 if (strcmp (package_name, this_package_name) != 0)
8008 complaint (&symfile_complaints,
8009 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8010 (symbol_symtab (sym) != NULL
8011 ? symtab_to_filename_for_display
8012 (symbol_symtab (sym))
4262abfb 8013 : objfile_name (cu->objfile)),
a766d390
DE
8014 this_package_name, package_name);
8015 xfree (this_package_name);
8016 }
8017 }
8018 }
8019 }
8020
8021 if (package_name != NULL)
8022 {
8023 struct objfile *objfile = cu->objfile;
34a68019 8024 const char *saved_package_name
224c3ddb
SM
8025 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8026 package_name,
8027 strlen (package_name));
19f392bc
UW
8028 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8029 saved_package_name);
a766d390
DE
8030 struct symbol *sym;
8031
8032 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8033
e623cf5d 8034 sym = allocate_symbol (objfile);
f85f34ed 8035 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8036 SYMBOL_SET_NAMES (sym, saved_package_name,
8037 strlen (saved_package_name), 0, objfile);
a766d390
DE
8038 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8039 e.g., "main" finds the "main" module and not C's main(). */
8040 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8041 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8042 SYMBOL_TYPE (sym) = type;
8043
8044 add_symbol_to_list (sym, &global_symbols);
8045
8046 xfree (package_name);
8047 }
8048}
8049
95554aad
TT
8050/* Return the symtab for PER_CU. This works properly regardless of
8051 whether we're using the index or psymtabs. */
8052
43f3e411
DE
8053static struct compunit_symtab *
8054get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8055{
8056 return (dwarf2_per_objfile->using_index
43f3e411
DE
8057 ? per_cu->v.quick->compunit_symtab
8058 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8059}
8060
8061/* A helper function for computing the list of all symbol tables
8062 included by PER_CU. */
8063
8064static void
43f3e411 8065recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8066 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8067 struct dwarf2_per_cu_data *per_cu,
43f3e411 8068 struct compunit_symtab *immediate_parent)
95554aad
TT
8069{
8070 void **slot;
8071 int ix;
43f3e411 8072 struct compunit_symtab *cust;
95554aad
TT
8073 struct dwarf2_per_cu_data *iter;
8074
8075 slot = htab_find_slot (all_children, per_cu, INSERT);
8076 if (*slot != NULL)
8077 {
8078 /* This inclusion and its children have been processed. */
8079 return;
8080 }
8081
8082 *slot = per_cu;
8083 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8084 cust = get_compunit_symtab (per_cu);
8085 if (cust != NULL)
ec94af83
DE
8086 {
8087 /* If this is a type unit only add its symbol table if we haven't
8088 seen it yet (type unit per_cu's can share symtabs). */
8089 if (per_cu->is_debug_types)
8090 {
43f3e411 8091 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8092 if (*slot == NULL)
8093 {
43f3e411
DE
8094 *slot = cust;
8095 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8096 if (cust->user == NULL)
8097 cust->user = immediate_parent;
ec94af83
DE
8098 }
8099 }
8100 else
f9125b6c 8101 {
43f3e411
DE
8102 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8103 if (cust->user == NULL)
8104 cust->user = immediate_parent;
f9125b6c 8105 }
ec94af83 8106 }
95554aad
TT
8107
8108 for (ix = 0;
796a7ff8 8109 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8110 ++ix)
ec94af83
DE
8111 {
8112 recursively_compute_inclusions (result, all_children,
43f3e411 8113 all_type_symtabs, iter, cust);
ec94af83 8114 }
95554aad
TT
8115}
8116
43f3e411 8117/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8118 PER_CU. */
8119
8120static void
43f3e411 8121compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8122{
f4dc4d17
DE
8123 gdb_assert (! per_cu->is_debug_types);
8124
796a7ff8 8125 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8126 {
8127 int ix, len;
ec94af83 8128 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8129 struct compunit_symtab *compunit_symtab_iter;
8130 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8131 htab_t all_children, all_type_symtabs;
43f3e411 8132 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8133
8134 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8135 if (cust == NULL)
95554aad
TT
8136 return;
8137
8138 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8139 NULL, xcalloc, xfree);
ec94af83
DE
8140 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8141 NULL, xcalloc, xfree);
95554aad
TT
8142
8143 for (ix = 0;
796a7ff8 8144 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8145 ix, per_cu_iter);
95554aad 8146 ++ix)
ec94af83
DE
8147 {
8148 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8149 all_type_symtabs, per_cu_iter,
43f3e411 8150 cust);
ec94af83 8151 }
95554aad 8152
ec94af83 8153 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8154 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8155 cust->includes
8d749320
SM
8156 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8157 struct compunit_symtab *, len + 1);
95554aad 8158 for (ix = 0;
43f3e411
DE
8159 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8160 compunit_symtab_iter);
95554aad 8161 ++ix)
43f3e411
DE
8162 cust->includes[ix] = compunit_symtab_iter;
8163 cust->includes[len] = NULL;
95554aad 8164
43f3e411 8165 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8166 htab_delete (all_children);
ec94af83 8167 htab_delete (all_type_symtabs);
95554aad
TT
8168 }
8169}
8170
8171/* Compute the 'includes' field for the symtabs of all the CUs we just
8172 read. */
8173
8174static void
8175process_cu_includes (void)
8176{
8177 int ix;
8178 struct dwarf2_per_cu_data *iter;
8179
8180 for (ix = 0;
8181 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8182 ix, iter);
8183 ++ix)
f4dc4d17
DE
8184 {
8185 if (! iter->is_debug_types)
43f3e411 8186 compute_compunit_symtab_includes (iter);
f4dc4d17 8187 }
95554aad
TT
8188
8189 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8190}
8191
9cdd5dbd 8192/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8193 already been loaded into memory. */
8194
8195static void
95554aad
TT
8196process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8197 enum language pretend_language)
10b3939b 8198{
10b3939b 8199 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8200 struct objfile *objfile = per_cu->objfile;
3e29f34a 8201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8202 CORE_ADDR lowpc, highpc;
43f3e411 8203 struct compunit_symtab *cust;
3da10d80 8204 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8205 CORE_ADDR baseaddr;
4359dff1 8206 struct block *static_block;
3e29f34a 8207 CORE_ADDR addr;
10b3939b
DJ
8208
8209 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8210
10b3939b
DJ
8211 buildsym_init ();
8212 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8213 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8214
8215 cu->list_in_scope = &file_symbols;
c906108c 8216
95554aad
TT
8217 cu->language = pretend_language;
8218 cu->language_defn = language_def (cu->language);
8219
c906108c 8220 /* Do line number decoding in read_file_scope () */
10b3939b 8221 process_die (cu->dies, cu);
c906108c 8222
a766d390
DE
8223 /* For now fudge the Go package. */
8224 if (cu->language == language_go)
8225 fixup_go_packaging (cu);
8226
3da10d80
KS
8227 /* Now that we have processed all the DIEs in the CU, all the types
8228 should be complete, and it should now be safe to compute all of the
8229 physnames. */
8230 compute_delayed_physnames (cu);
8231 do_cleanups (delayed_list_cleanup);
8232
fae299cd
DC
8233 /* Some compilers don't define a DW_AT_high_pc attribute for the
8234 compilation unit. If the DW_AT_high_pc is missing, synthesize
8235 it, by scanning the DIE's below the compilation unit. */
10b3939b 8236 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8237
3e29f34a
MR
8238 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8239 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8240
8241 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8242 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8243 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8244 addrmap to help ensure it has an accurate map of pc values belonging to
8245 this comp unit. */
8246 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8247
43f3e411
DE
8248 cust = end_symtab_from_static_block (static_block,
8249 SECT_OFF_TEXT (objfile), 0);
c906108c 8250
43f3e411 8251 if (cust != NULL)
c906108c 8252 {
df15bd07 8253 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8254
8be455d7
JK
8255 /* Set symtab language to language from DW_AT_language. If the
8256 compilation is from a C file generated by language preprocessors, do
8257 not set the language if it was already deduced by start_subfile. */
43f3e411 8258 if (!(cu->language == language_c
40e3ad0e 8259 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8260 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8261
8262 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8263 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8264 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8265 there were bugs in prologue debug info, fixed later in GCC-4.5
8266 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8267
8268 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8269 needed, it would be wrong due to missing DW_AT_producer there.
8270
8271 Still one can confuse GDB by using non-standard GCC compilation
8272 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8273 */
ab260dad 8274 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8275 cust->locations_valid = 1;
e0d00bc7
JK
8276
8277 if (gcc_4_minor >= 5)
43f3e411 8278 cust->epilogue_unwind_valid = 1;
96408a79 8279
43f3e411 8280 cust->call_site_htab = cu->call_site_htab;
c906108c 8281 }
9291a0cd
TT
8282
8283 if (dwarf2_per_objfile->using_index)
43f3e411 8284 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8285 else
8286 {
8287 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8288 pst->compunit_symtab = cust;
9291a0cd
TT
8289 pst->readin = 1;
8290 }
c906108c 8291
95554aad
TT
8292 /* Push it for inclusion processing later. */
8293 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8294
c906108c 8295 do_cleanups (back_to);
f4dc4d17 8296}
45cfd468 8297
f4dc4d17
DE
8298/* Generate full symbol information for type unit PER_CU, whose DIEs have
8299 already been loaded into memory. */
8300
8301static void
8302process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8303 enum language pretend_language)
8304{
8305 struct dwarf2_cu *cu = per_cu->cu;
8306 struct objfile *objfile = per_cu->objfile;
43f3e411 8307 struct compunit_symtab *cust;
f4dc4d17 8308 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8309 struct signatured_type *sig_type;
8310
8311 gdb_assert (per_cu->is_debug_types);
8312 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8313
8314 buildsym_init ();
8315 back_to = make_cleanup (really_free_pendings, NULL);
8316 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8317
8318 cu->list_in_scope = &file_symbols;
8319
8320 cu->language = pretend_language;
8321 cu->language_defn = language_def (cu->language);
8322
8323 /* The symbol tables are set up in read_type_unit_scope. */
8324 process_die (cu->dies, cu);
8325
8326 /* For now fudge the Go package. */
8327 if (cu->language == language_go)
8328 fixup_go_packaging (cu);
8329
8330 /* Now that we have processed all the DIEs in the CU, all the types
8331 should be complete, and it should now be safe to compute all of the
8332 physnames. */
8333 compute_delayed_physnames (cu);
8334 do_cleanups (delayed_list_cleanup);
8335
8336 /* TUs share symbol tables.
8337 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8338 of it with end_expandable_symtab. Otherwise, complete the addition of
8339 this TU's symbols to the existing symtab. */
43f3e411 8340 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8341 {
43f3e411
DE
8342 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8343 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8344
43f3e411 8345 if (cust != NULL)
f4dc4d17
DE
8346 {
8347 /* Set symtab language to language from DW_AT_language. If the
8348 compilation is from a C file generated by language preprocessors,
8349 do not set the language if it was already deduced by
8350 start_subfile. */
43f3e411
DE
8351 if (!(cu->language == language_c
8352 && COMPUNIT_FILETABS (cust)->language != language_c))
8353 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8354 }
8355 }
8356 else
8357 {
0ab9ce85 8358 augment_type_symtab ();
43f3e411 8359 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8360 }
8361
8362 if (dwarf2_per_objfile->using_index)
43f3e411 8363 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8364 else
8365 {
8366 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8367 pst->compunit_symtab = cust;
f4dc4d17 8368 pst->readin = 1;
45cfd468 8369 }
f4dc4d17
DE
8370
8371 do_cleanups (back_to);
c906108c
SS
8372}
8373
95554aad
TT
8374/* Process an imported unit DIE. */
8375
8376static void
8377process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8378{
8379 struct attribute *attr;
8380
f4dc4d17
DE
8381 /* For now we don't handle imported units in type units. */
8382 if (cu->per_cu->is_debug_types)
8383 {
8384 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8385 " supported in type units [in module %s]"),
4262abfb 8386 objfile_name (cu->objfile));
f4dc4d17
DE
8387 }
8388
95554aad
TT
8389 attr = dwarf2_attr (die, DW_AT_import, cu);
8390 if (attr != NULL)
8391 {
9c541725
PA
8392 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8393 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8394 dwarf2_per_cu_data *per_cu
8395 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8396
69d751e3 8397 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8398 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8399 load_full_comp_unit (per_cu, cu->language);
8400
796a7ff8 8401 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8402 per_cu);
8403 }
8404}
8405
adde2bff
DE
8406/* Reset the in_process bit of a die. */
8407
8408static void
8409reset_die_in_process (void *arg)
8410{
9a3c8263 8411 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8412
adde2bff
DE
8413 die->in_process = 0;
8414}
8415
c906108c
SS
8416/* Process a die and its children. */
8417
8418static void
e7c27a73 8419process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8420{
adde2bff
DE
8421 struct cleanup *in_process;
8422
8423 /* We should only be processing those not already in process. */
8424 gdb_assert (!die->in_process);
8425
8426 die->in_process = 1;
8427 in_process = make_cleanup (reset_die_in_process,die);
8428
c906108c
SS
8429 switch (die->tag)
8430 {
8431 case DW_TAG_padding:
8432 break;
8433 case DW_TAG_compile_unit:
95554aad 8434 case DW_TAG_partial_unit:
e7c27a73 8435 read_file_scope (die, cu);
c906108c 8436 break;
348e048f
DE
8437 case DW_TAG_type_unit:
8438 read_type_unit_scope (die, cu);
8439 break;
c906108c 8440 case DW_TAG_subprogram:
c906108c 8441 case DW_TAG_inlined_subroutine:
edb3359d 8442 read_func_scope (die, cu);
c906108c
SS
8443 break;
8444 case DW_TAG_lexical_block:
14898363
L
8445 case DW_TAG_try_block:
8446 case DW_TAG_catch_block:
e7c27a73 8447 read_lexical_block_scope (die, cu);
c906108c 8448 break;
216f72a1 8449 case DW_TAG_call_site:
96408a79
SA
8450 case DW_TAG_GNU_call_site:
8451 read_call_site_scope (die, cu);
8452 break;
c906108c 8453 case DW_TAG_class_type:
680b30c7 8454 case DW_TAG_interface_type:
c906108c
SS
8455 case DW_TAG_structure_type:
8456 case DW_TAG_union_type:
134d01f1 8457 process_structure_scope (die, cu);
c906108c
SS
8458 break;
8459 case DW_TAG_enumeration_type:
134d01f1 8460 process_enumeration_scope (die, cu);
c906108c 8461 break;
134d01f1 8462
f792889a
DJ
8463 /* These dies have a type, but processing them does not create
8464 a symbol or recurse to process the children. Therefore we can
8465 read them on-demand through read_type_die. */
c906108c 8466 case DW_TAG_subroutine_type:
72019c9c 8467 case DW_TAG_set_type:
c906108c 8468 case DW_TAG_array_type:
c906108c 8469 case DW_TAG_pointer_type:
c906108c 8470 case DW_TAG_ptr_to_member_type:
c906108c 8471 case DW_TAG_reference_type:
4297a3f0 8472 case DW_TAG_rvalue_reference_type:
c906108c 8473 case DW_TAG_string_type:
c906108c 8474 break;
134d01f1 8475
c906108c 8476 case DW_TAG_base_type:
a02abb62 8477 case DW_TAG_subrange_type:
cb249c71 8478 case DW_TAG_typedef:
134d01f1
DJ
8479 /* Add a typedef symbol for the type definition, if it has a
8480 DW_AT_name. */
f792889a 8481 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8482 break;
c906108c 8483 case DW_TAG_common_block:
e7c27a73 8484 read_common_block (die, cu);
c906108c
SS
8485 break;
8486 case DW_TAG_common_inclusion:
8487 break;
d9fa45fe 8488 case DW_TAG_namespace:
4d4ec4e5 8489 cu->processing_has_namespace_info = 1;
e7c27a73 8490 read_namespace (die, cu);
d9fa45fe 8491 break;
5d7cb8df 8492 case DW_TAG_module:
4d4ec4e5 8493 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8494 read_module (die, cu);
8495 break;
d9fa45fe 8496 case DW_TAG_imported_declaration:
74921315
KS
8497 cu->processing_has_namespace_info = 1;
8498 if (read_namespace_alias (die, cu))
8499 break;
8500 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8501 case DW_TAG_imported_module:
4d4ec4e5 8502 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8503 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8504 || cu->language != language_fortran))
8505 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8506 dwarf_tag_name (die->tag));
8507 read_import_statement (die, cu);
d9fa45fe 8508 break;
95554aad
TT
8509
8510 case DW_TAG_imported_unit:
8511 process_imported_unit_die (die, cu);
8512 break;
8513
c906108c 8514 default:
e7c27a73 8515 new_symbol (die, NULL, cu);
c906108c
SS
8516 break;
8517 }
adde2bff
DE
8518
8519 do_cleanups (in_process);
c906108c 8520}
ca69b9e6
DE
8521\f
8522/* DWARF name computation. */
c906108c 8523
94af9270
KS
8524/* A helper function for dwarf2_compute_name which determines whether DIE
8525 needs to have the name of the scope prepended to the name listed in the
8526 die. */
8527
8528static int
8529die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8530{
1c809c68
TT
8531 struct attribute *attr;
8532
94af9270
KS
8533 switch (die->tag)
8534 {
8535 case DW_TAG_namespace:
8536 case DW_TAG_typedef:
8537 case DW_TAG_class_type:
8538 case DW_TAG_interface_type:
8539 case DW_TAG_structure_type:
8540 case DW_TAG_union_type:
8541 case DW_TAG_enumeration_type:
8542 case DW_TAG_enumerator:
8543 case DW_TAG_subprogram:
08a76f8a 8544 case DW_TAG_inlined_subroutine:
94af9270 8545 case DW_TAG_member:
74921315 8546 case DW_TAG_imported_declaration:
94af9270
KS
8547 return 1;
8548
8549 case DW_TAG_variable:
c2b0a229 8550 case DW_TAG_constant:
94af9270
KS
8551 /* We only need to prefix "globally" visible variables. These include
8552 any variable marked with DW_AT_external or any variable that
8553 lives in a namespace. [Variables in anonymous namespaces
8554 require prefixing, but they are not DW_AT_external.] */
8555
8556 if (dwarf2_attr (die, DW_AT_specification, cu))
8557 {
8558 struct dwarf2_cu *spec_cu = cu;
9a619af0 8559
94af9270
KS
8560 return die_needs_namespace (die_specification (die, &spec_cu),
8561 spec_cu);
8562 }
8563
1c809c68 8564 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8565 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8566 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8567 return 0;
8568 /* A variable in a lexical block of some kind does not need a
8569 namespace, even though in C++ such variables may be external
8570 and have a mangled name. */
8571 if (die->parent->tag == DW_TAG_lexical_block
8572 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8573 || die->parent->tag == DW_TAG_catch_block
8574 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8575 return 0;
8576 return 1;
94af9270
KS
8577
8578 default:
8579 return 0;
8580 }
8581}
8582
8583/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8584 compute the physname for the object, which include a method's:
9c37b5ae 8585 - formal parameters (C++),
a766d390 8586 - receiver type (Go),
a766d390
DE
8587
8588 The term "physname" is a bit confusing.
8589 For C++, for example, it is the demangled name.
8590 For Go, for example, it's the mangled name.
94af9270 8591
af6b7be1
JB
8592 For Ada, return the DIE's linkage name rather than the fully qualified
8593 name. PHYSNAME is ignored..
8594
94af9270
KS
8595 The result is allocated on the objfile_obstack and canonicalized. */
8596
8597static const char *
15d034d0
TT
8598dwarf2_compute_name (const char *name,
8599 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8600 int physname)
8601{
bb5ed363
DE
8602 struct objfile *objfile = cu->objfile;
8603
94af9270
KS
8604 if (name == NULL)
8605 name = dwarf2_name (die, cu);
8606
2ee7123e
DE
8607 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8608 but otherwise compute it by typename_concat inside GDB.
8609 FIXME: Actually this is not really true, or at least not always true.
8610 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8611 Fortran names because there is no mangling standard. So new_symbol_full
8612 will set the demangled name to the result of dwarf2_full_name, and it is
8613 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8614 if (cu->language == language_ada
8615 || (cu->language == language_fortran && physname))
8616 {
8617 /* For Ada unit, we prefer the linkage name over the name, as
8618 the former contains the exported name, which the user expects
8619 to be able to reference. Ideally, we want the user to be able
8620 to reference this entity using either natural or linkage name,
8621 but we haven't started looking at this enhancement yet. */
2ee7123e 8622 const char *linkage_name;
f55ee35c 8623
2ee7123e
DE
8624 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8625 if (linkage_name == NULL)
8626 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8627 if (linkage_name != NULL)
8628 return linkage_name;
f55ee35c
JK
8629 }
8630
94af9270
KS
8631 /* These are the only languages we know how to qualify names in. */
8632 if (name != NULL
9c37b5ae 8633 && (cu->language == language_cplus
c44af4eb
TT
8634 || cu->language == language_fortran || cu->language == language_d
8635 || cu->language == language_rust))
94af9270
KS
8636 {
8637 if (die_needs_namespace (die, cu))
8638 {
8639 long length;
0d5cff50 8640 const char *prefix;
34a68019 8641 const char *canonical_name = NULL;
94af9270 8642
d7e74731
PA
8643 string_file buf;
8644
94af9270 8645 prefix = determine_prefix (die, cu);
94af9270
KS
8646 if (*prefix != '\0')
8647 {
f55ee35c
JK
8648 char *prefixed_name = typename_concat (NULL, prefix, name,
8649 physname, cu);
9a619af0 8650
d7e74731 8651 buf.puts (prefixed_name);
94af9270
KS
8652 xfree (prefixed_name);
8653 }
8654 else
d7e74731 8655 buf.puts (name);
94af9270 8656
98bfdba5
PA
8657 /* Template parameters may be specified in the DIE's DW_AT_name, or
8658 as children with DW_TAG_template_type_param or
8659 DW_TAG_value_type_param. If the latter, add them to the name
8660 here. If the name already has template parameters, then
8661 skip this step; some versions of GCC emit both, and
8662 it is more efficient to use the pre-computed name.
8663
8664 Something to keep in mind about this process: it is very
8665 unlikely, or in some cases downright impossible, to produce
8666 something that will match the mangled name of a function.
8667 If the definition of the function has the same debug info,
8668 we should be able to match up with it anyway. But fallbacks
8669 using the minimal symbol, for instance to find a method
8670 implemented in a stripped copy of libstdc++, will not work.
8671 If we do not have debug info for the definition, we will have to
8672 match them up some other way.
8673
8674 When we do name matching there is a related problem with function
8675 templates; two instantiated function templates are allowed to
8676 differ only by their return types, which we do not add here. */
8677
8678 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8679 {
8680 struct attribute *attr;
8681 struct die_info *child;
8682 int first = 1;
8683
8684 die->building_fullname = 1;
8685
8686 for (child = die->child; child != NULL; child = child->sibling)
8687 {
8688 struct type *type;
12df843f 8689 LONGEST value;
d521ce57 8690 const gdb_byte *bytes;
98bfdba5
PA
8691 struct dwarf2_locexpr_baton *baton;
8692 struct value *v;
8693
8694 if (child->tag != DW_TAG_template_type_param
8695 && child->tag != DW_TAG_template_value_param)
8696 continue;
8697
8698 if (first)
8699 {
d7e74731 8700 buf.puts ("<");
98bfdba5
PA
8701 first = 0;
8702 }
8703 else
d7e74731 8704 buf.puts (", ");
98bfdba5
PA
8705
8706 attr = dwarf2_attr (child, DW_AT_type, cu);
8707 if (attr == NULL)
8708 {
8709 complaint (&symfile_complaints,
8710 _("template parameter missing DW_AT_type"));
d7e74731 8711 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8712 continue;
8713 }
8714 type = die_type (child, cu);
8715
8716 if (child->tag == DW_TAG_template_type_param)
8717 {
d7e74731 8718 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8719 continue;
8720 }
8721
8722 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8723 if (attr == NULL)
8724 {
8725 complaint (&symfile_complaints,
3e43a32a
MS
8726 _("template parameter missing "
8727 "DW_AT_const_value"));
d7e74731 8728 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8729 continue;
8730 }
8731
8732 dwarf2_const_value_attr (attr, type, name,
8733 &cu->comp_unit_obstack, cu,
8734 &value, &bytes, &baton);
8735
8736 if (TYPE_NOSIGN (type))
8737 /* GDB prints characters as NUMBER 'CHAR'. If that's
8738 changed, this can use value_print instead. */
d7e74731 8739 c_printchar (value, type, &buf);
98bfdba5
PA
8740 else
8741 {
8742 struct value_print_options opts;
8743
8744 if (baton != NULL)
8745 v = dwarf2_evaluate_loc_desc (type, NULL,
8746 baton->data,
8747 baton->size,
8748 baton->per_cu);
8749 else if (bytes != NULL)
8750 {
8751 v = allocate_value (type);
8752 memcpy (value_contents_writeable (v), bytes,
8753 TYPE_LENGTH (type));
8754 }
8755 else
8756 v = value_from_longest (type, value);
8757
3e43a32a
MS
8758 /* Specify decimal so that we do not depend on
8759 the radix. */
98bfdba5
PA
8760 get_formatted_print_options (&opts, 'd');
8761 opts.raw = 1;
d7e74731 8762 value_print (v, &buf, &opts);
98bfdba5
PA
8763 release_value (v);
8764 value_free (v);
8765 }
8766 }
8767
8768 die->building_fullname = 0;
8769
8770 if (!first)
8771 {
8772 /* Close the argument list, with a space if necessary
8773 (nested templates). */
d7e74731
PA
8774 if (!buf.empty () && buf.string ().back () == '>')
8775 buf.puts (" >");
98bfdba5 8776 else
d7e74731 8777 buf.puts (">");
98bfdba5
PA
8778 }
8779 }
8780
9c37b5ae 8781 /* For C++ methods, append formal parameter type
94af9270 8782 information, if PHYSNAME. */
6e70227d 8783
94af9270 8784 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8785 && cu->language == language_cplus)
94af9270
KS
8786 {
8787 struct type *type = read_type_die (die, cu);
8788
d7e74731 8789 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8790 &type_print_raw_options);
94af9270 8791
9c37b5ae 8792 if (cu->language == language_cplus)
94af9270 8793 {
60430eff
DJ
8794 /* Assume that an artificial first parameter is
8795 "this", but do not crash if it is not. RealView
8796 marks unnamed (and thus unused) parameters as
8797 artificial; there is no way to differentiate
8798 the two cases. */
94af9270
KS
8799 if (TYPE_NFIELDS (type) > 0
8800 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8801 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8802 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8803 0))))
d7e74731 8804 buf.puts (" const");
94af9270
KS
8805 }
8806 }
8807
d7e74731 8808 const std::string &intermediate_name = buf.string ();
94af9270
KS
8809
8810 if (cu->language == language_cplus)
34a68019 8811 canonical_name
322a8516 8812 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8813 &objfile->per_bfd->storage_obstack);
8814
8815 /* If we only computed INTERMEDIATE_NAME, or if
8816 INTERMEDIATE_NAME is already canonical, then we need to
8817 copy it to the appropriate obstack. */
322a8516 8818 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8819 name = ((const char *)
8820 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8821 intermediate_name.c_str (),
8822 intermediate_name.length ()));
34a68019
TT
8823 else
8824 name = canonical_name;
94af9270
KS
8825 }
8826 }
8827
8828 return name;
8829}
8830
0114d602
DJ
8831/* Return the fully qualified name of DIE, based on its DW_AT_name.
8832 If scope qualifiers are appropriate they will be added. The result
34a68019 8833 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8834 not have a name. NAME may either be from a previous call to
8835 dwarf2_name or NULL.
8836
9c37b5ae 8837 The output string will be canonicalized (if C++). */
0114d602
DJ
8838
8839static const char *
15d034d0 8840dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8841{
94af9270
KS
8842 return dwarf2_compute_name (name, die, cu, 0);
8843}
0114d602 8844
94af9270
KS
8845/* Construct a physname for the given DIE in CU. NAME may either be
8846 from a previous call to dwarf2_name or NULL. The result will be
8847 allocated on the objfile_objstack or NULL if the DIE does not have a
8848 name.
0114d602 8849
9c37b5ae 8850 The output string will be canonicalized (if C++). */
0114d602 8851
94af9270 8852static const char *
15d034d0 8853dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8854{
bb5ed363 8855 struct objfile *objfile = cu->objfile;
900e11f9
JK
8856 const char *retval, *mangled = NULL, *canon = NULL;
8857 struct cleanup *back_to;
8858 int need_copy = 1;
8859
8860 /* In this case dwarf2_compute_name is just a shortcut not building anything
8861 on its own. */
8862 if (!die_needs_namespace (die, cu))
8863 return dwarf2_compute_name (name, die, cu, 1);
8864
8865 back_to = make_cleanup (null_cleanup, NULL);
8866
7d45c7c3
KB
8867 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8868 if (mangled == NULL)
8869 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8870
e98c9e7c
TT
8871 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8872 See https://github.com/rust-lang/rust/issues/32925. */
8873 if (cu->language == language_rust && mangled != NULL
8874 && strchr (mangled, '{') != NULL)
8875 mangled = NULL;
8876
900e11f9
JK
8877 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8878 has computed. */
7d45c7c3 8879 if (mangled != NULL)
900e11f9
JK
8880 {
8881 char *demangled;
8882
900e11f9
JK
8883 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8884 type. It is easier for GDB users to search for such functions as
8885 `name(params)' than `long name(params)'. In such case the minimal
8886 symbol names do not match the full symbol names but for template
8887 functions there is never a need to look up their definition from their
8888 declaration so the only disadvantage remains the minimal symbol
8889 variant `long name(params)' does not have the proper inferior type.
8890 */
8891
a766d390
DE
8892 if (cu->language == language_go)
8893 {
8894 /* This is a lie, but we already lie to the caller new_symbol_full.
8895 new_symbol_full assumes we return the mangled name.
8896 This just undoes that lie until things are cleaned up. */
8897 demangled = NULL;
8898 }
8899 else
8900 {
8de20a37 8901 demangled = gdb_demangle (mangled,
9c37b5ae 8902 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8903 }
900e11f9
JK
8904 if (demangled)
8905 {
8906 make_cleanup (xfree, demangled);
8907 canon = demangled;
8908 }
8909 else
8910 {
8911 canon = mangled;
8912 need_copy = 0;
8913 }
8914 }
8915
8916 if (canon == NULL || check_physname)
8917 {
8918 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8919
8920 if (canon != NULL && strcmp (physname, canon) != 0)
8921 {
8922 /* It may not mean a bug in GDB. The compiler could also
8923 compute DW_AT_linkage_name incorrectly. But in such case
8924 GDB would need to be bug-to-bug compatible. */
8925
8926 complaint (&symfile_complaints,
8927 _("Computed physname <%s> does not match demangled <%s> "
8928 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 8929 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 8930 objfile_name (objfile));
900e11f9
JK
8931
8932 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8933 is available here - over computed PHYSNAME. It is safer
8934 against both buggy GDB and buggy compilers. */
8935
8936 retval = canon;
8937 }
8938 else
8939 {
8940 retval = physname;
8941 need_copy = 0;
8942 }
8943 }
8944 else
8945 retval = canon;
8946
8947 if (need_copy)
224c3ddb
SM
8948 retval = ((const char *)
8949 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8950 retval, strlen (retval)));
900e11f9
JK
8951
8952 do_cleanups (back_to);
8953 return retval;
0114d602
DJ
8954}
8955
74921315
KS
8956/* Inspect DIE in CU for a namespace alias. If one exists, record
8957 a new symbol for it.
8958
8959 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8960
8961static int
8962read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8963{
8964 struct attribute *attr;
8965
8966 /* If the die does not have a name, this is not a namespace
8967 alias. */
8968 attr = dwarf2_attr (die, DW_AT_name, cu);
8969 if (attr != NULL)
8970 {
8971 int num;
8972 struct die_info *d = die;
8973 struct dwarf2_cu *imported_cu = cu;
8974
8975 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8976 keep inspecting DIEs until we hit the underlying import. */
8977#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8978 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8979 {
8980 attr = dwarf2_attr (d, DW_AT_import, cu);
8981 if (attr == NULL)
8982 break;
8983
8984 d = follow_die_ref (d, attr, &imported_cu);
8985 if (d->tag != DW_TAG_imported_declaration)
8986 break;
8987 }
8988
8989 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8990 {
8991 complaint (&symfile_complaints,
8992 _("DIE at 0x%x has too many recursively imported "
9c541725 8993 "declarations"), to_underlying (d->sect_off));
74921315
KS
8994 return 0;
8995 }
8996
8997 if (attr != NULL)
8998 {
8999 struct type *type;
9c541725 9000 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9001
9c541725 9002 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9003 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9004 {
9005 /* This declaration is a global namespace alias. Add
9006 a symbol for it whose type is the aliased namespace. */
9007 new_symbol (die, type, cu);
9008 return 1;
9009 }
9010 }
9011 }
9012
9013 return 0;
9014}
9015
22cee43f
PMR
9016/* Return the using directives repository (global or local?) to use in the
9017 current context for LANGUAGE.
9018
9019 For Ada, imported declarations can materialize renamings, which *may* be
9020 global. However it is impossible (for now?) in DWARF to distinguish
9021 "external" imported declarations and "static" ones. As all imported
9022 declarations seem to be static in all other languages, make them all CU-wide
9023 global only in Ada. */
9024
9025static struct using_direct **
9026using_directives (enum language language)
9027{
9028 if (language == language_ada && context_stack_depth == 0)
9029 return &global_using_directives;
9030 else
9031 return &local_using_directives;
9032}
9033
27aa8d6a
SW
9034/* Read the import statement specified by the given die and record it. */
9035
9036static void
9037read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9038{
bb5ed363 9039 struct objfile *objfile = cu->objfile;
27aa8d6a 9040 struct attribute *import_attr;
32019081 9041 struct die_info *imported_die, *child_die;
de4affc9 9042 struct dwarf2_cu *imported_cu;
27aa8d6a 9043 const char *imported_name;
794684b6 9044 const char *imported_name_prefix;
13387711
SW
9045 const char *canonical_name;
9046 const char *import_alias;
9047 const char *imported_declaration = NULL;
794684b6 9048 const char *import_prefix;
32019081
JK
9049 VEC (const_char_ptr) *excludes = NULL;
9050 struct cleanup *cleanups;
13387711 9051
27aa8d6a
SW
9052 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9053 if (import_attr == NULL)
9054 {
9055 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9056 dwarf_tag_name (die->tag));
9057 return;
9058 }
9059
de4affc9
CC
9060 imported_cu = cu;
9061 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9062 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9063 if (imported_name == NULL)
9064 {
9065 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9066
9067 The import in the following code:
9068 namespace A
9069 {
9070 typedef int B;
9071 }
9072
9073 int main ()
9074 {
9075 using A::B;
9076 B b;
9077 return b;
9078 }
9079
9080 ...
9081 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9082 <52> DW_AT_decl_file : 1
9083 <53> DW_AT_decl_line : 6
9084 <54> DW_AT_import : <0x75>
9085 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9086 <59> DW_AT_name : B
9087 <5b> DW_AT_decl_file : 1
9088 <5c> DW_AT_decl_line : 2
9089 <5d> DW_AT_type : <0x6e>
9090 ...
9091 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9092 <76> DW_AT_byte_size : 4
9093 <77> DW_AT_encoding : 5 (signed)
9094
9095 imports the wrong die ( 0x75 instead of 0x58 ).
9096 This case will be ignored until the gcc bug is fixed. */
9097 return;
9098 }
9099
82856980
SW
9100 /* Figure out the local name after import. */
9101 import_alias = dwarf2_name (die, cu);
27aa8d6a 9102
794684b6
SW
9103 /* Figure out where the statement is being imported to. */
9104 import_prefix = determine_prefix (die, cu);
9105
9106 /* Figure out what the scope of the imported die is and prepend it
9107 to the name of the imported die. */
de4affc9 9108 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9109
f55ee35c
JK
9110 if (imported_die->tag != DW_TAG_namespace
9111 && imported_die->tag != DW_TAG_module)
794684b6 9112 {
13387711
SW
9113 imported_declaration = imported_name;
9114 canonical_name = imported_name_prefix;
794684b6 9115 }
13387711 9116 else if (strlen (imported_name_prefix) > 0)
12aaed36 9117 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9118 imported_name_prefix,
9119 (cu->language == language_d ? "." : "::"),
9120 imported_name, (char *) NULL);
13387711
SW
9121 else
9122 canonical_name = imported_name;
794684b6 9123
32019081
JK
9124 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9125
9126 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9127 for (child_die = die->child; child_die && child_die->tag;
9128 child_die = sibling_die (child_die))
9129 {
9130 /* DWARF-4: A Fortran use statement with a “rename list” may be
9131 represented by an imported module entry with an import attribute
9132 referring to the module and owned entries corresponding to those
9133 entities that are renamed as part of being imported. */
9134
9135 if (child_die->tag != DW_TAG_imported_declaration)
9136 {
9137 complaint (&symfile_complaints,
9138 _("child DW_TAG_imported_declaration expected "
9139 "- DIE at 0x%x [in module %s]"),
9c541725 9140 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9141 continue;
9142 }
9143
9144 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9145 if (import_attr == NULL)
9146 {
9147 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9148 dwarf_tag_name (child_die->tag));
9149 continue;
9150 }
9151
9152 imported_cu = cu;
9153 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9154 &imported_cu);
9155 imported_name = dwarf2_name (imported_die, imported_cu);
9156 if (imported_name == NULL)
9157 {
9158 complaint (&symfile_complaints,
9159 _("child DW_TAG_imported_declaration has unknown "
9160 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9161 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9162 continue;
9163 }
9164
9165 VEC_safe_push (const_char_ptr, excludes, imported_name);
9166
9167 process_die (child_die, cu);
9168 }
9169
22cee43f
PMR
9170 add_using_directive (using_directives (cu->language),
9171 import_prefix,
9172 canonical_name,
9173 import_alias,
9174 imported_declaration,
9175 excludes,
9176 0,
9177 &objfile->objfile_obstack);
32019081
JK
9178
9179 do_cleanups (cleanups);
27aa8d6a
SW
9180}
9181
1b80a9fa
JK
9182/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9183 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9184 this, it was first present in GCC release 4.3.0. */
9185
9186static int
9187producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9188{
9189 if (!cu->checked_producer)
9190 check_producer (cu);
9191
9192 return cu->producer_is_gcc_lt_4_3;
9193}
9194
d721ba37
PA
9195static file_and_directory
9196find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9197{
d721ba37
PA
9198 file_and_directory res;
9199
9291a0cd
TT
9200 /* Find the filename. Do not use dwarf2_name here, since the filename
9201 is not a source language identifier. */
d721ba37
PA
9202 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9203 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9204
d721ba37
PA
9205 if (res.comp_dir == NULL
9206 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9207 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9208 {
d721ba37
PA
9209 res.comp_dir_storage = ldirname (res.name);
9210 if (!res.comp_dir_storage.empty ())
9211 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9212 }
d721ba37 9213 if (res.comp_dir != NULL)
9291a0cd
TT
9214 {
9215 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9216 directory, get rid of it. */
d721ba37 9217 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9218
d721ba37
PA
9219 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9220 res.comp_dir = cp + 1;
9291a0cd
TT
9221 }
9222
d721ba37
PA
9223 if (res.name == NULL)
9224 res.name = "<unknown>";
9225
9226 return res;
9291a0cd
TT
9227}
9228
f4dc4d17
DE
9229/* Handle DW_AT_stmt_list for a compilation unit.
9230 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9231 COMP_DIR is the compilation directory. LOWPC is passed to
9232 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9233
9234static void
9235handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9236 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9237{
527f3840 9238 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9239 struct attribute *attr;
527f3840
JK
9240 struct line_header line_header_local;
9241 hashval_t line_header_local_hash;
9242 unsigned u;
9243 void **slot;
9244 int decode_mapping;
2ab95328 9245
f4dc4d17
DE
9246 gdb_assert (! cu->per_cu->is_debug_types);
9247
2ab95328 9248 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9249 if (attr == NULL)
9250 return;
9251
9c541725 9252 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9253
9254 /* The line header hash table is only created if needed (it exists to
9255 prevent redundant reading of the line table for partial_units).
9256 If we're given a partial_unit, we'll need it. If we're given a
9257 compile_unit, then use the line header hash table if it's already
9258 created, but don't create one just yet. */
9259
9260 if (dwarf2_per_objfile->line_header_hash == NULL
9261 && die->tag == DW_TAG_partial_unit)
2ab95328 9262 {
527f3840
JK
9263 dwarf2_per_objfile->line_header_hash
9264 = htab_create_alloc_ex (127, line_header_hash_voidp,
9265 line_header_eq_voidp,
9266 free_line_header_voidp,
9267 &objfile->objfile_obstack,
9268 hashtab_obstack_allocate,
9269 dummy_obstack_deallocate);
9270 }
2ab95328 9271
9c541725 9272 line_header_local.sect_off = line_offset;
527f3840
JK
9273 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9274 line_header_local_hash = line_header_hash (&line_header_local);
9275 if (dwarf2_per_objfile->line_header_hash != NULL)
9276 {
9277 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9278 &line_header_local,
9279 line_header_local_hash, NO_INSERT);
9280
9281 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9282 is not present in *SLOT (since if there is something in *SLOT then
9283 it will be for a partial_unit). */
9284 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9285 {
527f3840 9286 gdb_assert (*slot != NULL);
9a3c8263 9287 cu->line_header = (struct line_header *) *slot;
527f3840 9288 return;
dee91e82 9289 }
2ab95328 9290 }
527f3840
JK
9291
9292 /* dwarf_decode_line_header does not yet provide sufficient information.
9293 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9294 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9295 if (lh == NULL)
527f3840 9296 return;
fff8551c 9297 cu->line_header = lh.get ();
527f3840
JK
9298
9299 if (dwarf2_per_objfile->line_header_hash == NULL)
9300 slot = NULL;
9301 else
9302 {
9303 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9304 &line_header_local,
9305 line_header_local_hash, INSERT);
9306 gdb_assert (slot != NULL);
9307 }
9308 if (slot != NULL && *slot == NULL)
9309 {
9310 /* This newly decoded line number information unit will be owned
9311 by line_header_hash hash table. */
9312 *slot = cu->line_header;
9313 }
9314 else
9315 {
9316 /* We cannot free any current entry in (*slot) as that struct line_header
9317 may be already used by multiple CUs. Create only temporary decoded
9318 line_header for this CU - it may happen at most once for each line
9319 number information unit. And if we're not using line_header_hash
9320 then this is what we want as well. */
9321 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9322 }
9323 decode_mapping = (die->tag != DW_TAG_partial_unit);
9324 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9325 decode_mapping);
fff8551c
PA
9326
9327 lh.release ();
2ab95328
TT
9328}
9329
95554aad 9330/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9331
c906108c 9332static void
e7c27a73 9333read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9334{
dee91e82 9335 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9337 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9338 CORE_ADDR highpc = ((CORE_ADDR) 0);
9339 struct attribute *attr;
c906108c 9340 struct die_info *child_die;
e142c38c 9341 CORE_ADDR baseaddr;
6e70227d 9342
e142c38c 9343 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9344
fae299cd 9345 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9346
9347 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9348 from finish_block. */
2acceee2 9349 if (lowpc == ((CORE_ADDR) -1))
c906108c 9350 lowpc = highpc;
3e29f34a 9351 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9352
d721ba37 9353 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9354
95554aad 9355 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9356
f4b8a18d
KW
9357 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9358 standardised yet. As a workaround for the language detection we fall
9359 back to the DW_AT_producer string. */
9360 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9361 cu->language = language_opencl;
9362
3019eac3
DE
9363 /* Similar hack for Go. */
9364 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9365 set_cu_language (DW_LANG_Go, cu);
9366
d721ba37 9367 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9368
9369 /* Decode line number information if present. We do this before
9370 processing child DIEs, so that the line header table is available
9371 for DW_AT_decl_file. */
d721ba37 9372 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9373
9374 /* Process all dies in compilation unit. */
9375 if (die->child != NULL)
9376 {
9377 child_die = die->child;
9378 while (child_die && child_die->tag)
9379 {
9380 process_die (child_die, cu);
9381 child_die = sibling_die (child_die);
9382 }
9383 }
9384
9385 /* Decode macro information, if present. Dwarf 2 macro information
9386 refers to information in the line number info statement program
9387 header, so we can only read it if we've read the header
9388 successfully. */
0af92d60
JK
9389 attr = dwarf2_attr (die, DW_AT_macros, cu);
9390 if (attr == NULL)
9391 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9392 if (attr && cu->line_header)
9393 {
9394 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9395 complaint (&symfile_complaints,
0af92d60 9396 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9397
43f3e411 9398 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9399 }
9400 else
9401 {
9402 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9403 if (attr && cu->line_header)
9404 {
9405 unsigned int macro_offset = DW_UNSND (attr);
9406
43f3e411 9407 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9408 }
9409 }
3019eac3
DE
9410}
9411
f4dc4d17
DE
9412/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9413 Create the set of symtabs used by this TU, or if this TU is sharing
9414 symtabs with another TU and the symtabs have already been created
9415 then restore those symtabs in the line header.
9416 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9417
9418static void
f4dc4d17 9419setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9420{
f4dc4d17
DE
9421 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9422 struct type_unit_group *tu_group;
9423 int first_time;
3019eac3 9424 struct attribute *attr;
9c541725 9425 unsigned int i;
0186c6a7 9426 struct signatured_type *sig_type;
3019eac3 9427
f4dc4d17 9428 gdb_assert (per_cu->is_debug_types);
0186c6a7 9429 sig_type = (struct signatured_type *) per_cu;
3019eac3 9430
f4dc4d17 9431 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9432
f4dc4d17 9433 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9434 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9435 if (sig_type->type_unit_group == NULL)
9436 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9437 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9438
9439 /* If we've already processed this stmt_list there's no real need to
9440 do it again, we could fake it and just recreate the part we need
9441 (file name,index -> symtab mapping). If data shows this optimization
9442 is useful we can do it then. */
43f3e411 9443 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9444
9445 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9446 debug info. */
fff8551c 9447 line_header_up lh;
f4dc4d17 9448 if (attr != NULL)
3019eac3 9449 {
9c541725 9450 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9451 lh = dwarf_decode_line_header (line_offset, cu);
9452 }
9453 if (lh == NULL)
9454 {
9455 if (first_time)
9456 dwarf2_start_symtab (cu, "", NULL, 0);
9457 else
9458 {
9459 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9460 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9461 }
f4dc4d17 9462 return;
3019eac3
DE
9463 }
9464
fff8551c 9465 cu->line_header = lh.get ();
3019eac3 9466
f4dc4d17
DE
9467 if (first_time)
9468 {
43f3e411 9469 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9470
1fd60fc0
DE
9471 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9472 still initializing it, and our caller (a few levels up)
9473 process_full_type_unit still needs to know if this is the first
9474 time. */
9475
fff8551c
PA
9476 tu_group->num_symtabs = lh->file_names.size ();
9477 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
3019eac3 9478
fff8551c 9479 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17 9480 {
8c43009f 9481 file_entry &fe = lh->file_names[i];
3019eac3 9482
fff8551c 9483 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
3019eac3 9484
f4dc4d17
DE
9485 if (current_subfile->symtab == NULL)
9486 {
9487 /* NOTE: start_subfile will recognize when it's been passed
9488 a file it has already seen. So we can't assume there's a
43f3e411 9489 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9490 lh->file_names may contain dups. */
43f3e411
DE
9491 current_subfile->symtab
9492 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9493 }
9494
8c43009f
PA
9495 fe.symtab = current_subfile->symtab;
9496 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9497 }
9498 }
9499 else
3019eac3 9500 {
0ab9ce85 9501 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9502
fff8551c 9503 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17
DE
9504 {
9505 struct file_entry *fe = &lh->file_names[i];
9506
9507 fe->symtab = tu_group->symtabs[i];
9508 }
3019eac3
DE
9509 }
9510
fff8551c
PA
9511 lh.release ();
9512
f4dc4d17
DE
9513 /* The main symtab is allocated last. Type units don't have DW_AT_name
9514 so they don't have a "real" (so to speak) symtab anyway.
9515 There is later code that will assign the main symtab to all symbols
9516 that don't have one. We need to handle the case of a symbol with a
9517 missing symtab (DW_AT_decl_file) anyway. */
9518}
3019eac3 9519
f4dc4d17
DE
9520/* Process DW_TAG_type_unit.
9521 For TUs we want to skip the first top level sibling if it's not the
9522 actual type being defined by this TU. In this case the first top
9523 level sibling is there to provide context only. */
3019eac3 9524
f4dc4d17
DE
9525static void
9526read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9527{
9528 struct die_info *child_die;
3019eac3 9529
f4dc4d17
DE
9530 prepare_one_comp_unit (cu, die, language_minimal);
9531
9532 /* Initialize (or reinitialize) the machinery for building symtabs.
9533 We do this before processing child DIEs, so that the line header table
9534 is available for DW_AT_decl_file. */
9535 setup_type_unit_groups (die, cu);
9536
9537 if (die->child != NULL)
9538 {
9539 child_die = die->child;
9540 while (child_die && child_die->tag)
9541 {
9542 process_die (child_die, cu);
9543 child_die = sibling_die (child_die);
9544 }
9545 }
3019eac3
DE
9546}
9547\f
80626a55
DE
9548/* DWO/DWP files.
9549
9550 http://gcc.gnu.org/wiki/DebugFission
9551 http://gcc.gnu.org/wiki/DebugFissionDWP
9552
9553 To simplify handling of both DWO files ("object" files with the DWARF info)
9554 and DWP files (a file with the DWOs packaged up into one file), we treat
9555 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9556
9557static hashval_t
9558hash_dwo_file (const void *item)
9559{
9a3c8263 9560 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9561 hashval_t hash;
3019eac3 9562
a2ce51a0
DE
9563 hash = htab_hash_string (dwo_file->dwo_name);
9564 if (dwo_file->comp_dir != NULL)
9565 hash += htab_hash_string (dwo_file->comp_dir);
9566 return hash;
3019eac3
DE
9567}
9568
9569static int
9570eq_dwo_file (const void *item_lhs, const void *item_rhs)
9571{
9a3c8263
SM
9572 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9573 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9574
a2ce51a0
DE
9575 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9576 return 0;
9577 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9578 return lhs->comp_dir == rhs->comp_dir;
9579 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9580}
9581
9582/* Allocate a hash table for DWO files. */
9583
9584static htab_t
9585allocate_dwo_file_hash_table (void)
9586{
9587 struct objfile *objfile = dwarf2_per_objfile->objfile;
9588
9589 return htab_create_alloc_ex (41,
9590 hash_dwo_file,
9591 eq_dwo_file,
9592 NULL,
9593 &objfile->objfile_obstack,
9594 hashtab_obstack_allocate,
9595 dummy_obstack_deallocate);
9596}
9597
80626a55
DE
9598/* Lookup DWO file DWO_NAME. */
9599
9600static void **
0ac5b59e 9601lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9602{
9603 struct dwo_file find_entry;
9604 void **slot;
9605
9606 if (dwarf2_per_objfile->dwo_files == NULL)
9607 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9608
9609 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9610 find_entry.dwo_name = dwo_name;
9611 find_entry.comp_dir = comp_dir;
80626a55
DE
9612 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9613
9614 return slot;
9615}
9616
3019eac3
DE
9617static hashval_t
9618hash_dwo_unit (const void *item)
9619{
9a3c8263 9620 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9621
9622 /* This drops the top 32 bits of the id, but is ok for a hash. */
9623 return dwo_unit->signature;
9624}
9625
9626static int
9627eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9628{
9a3c8263
SM
9629 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9630 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9631
9632 /* The signature is assumed to be unique within the DWO file.
9633 So while object file CU dwo_id's always have the value zero,
9634 that's OK, assuming each object file DWO file has only one CU,
9635 and that's the rule for now. */
9636 return lhs->signature == rhs->signature;
9637}
9638
9639/* Allocate a hash table for DWO CUs,TUs.
9640 There is one of these tables for each of CUs,TUs for each DWO file. */
9641
9642static htab_t
9643allocate_dwo_unit_table (struct objfile *objfile)
9644{
9645 /* Start out with a pretty small number.
9646 Generally DWO files contain only one CU and maybe some TUs. */
9647 return htab_create_alloc_ex (3,
9648 hash_dwo_unit,
9649 eq_dwo_unit,
9650 NULL,
9651 &objfile->objfile_obstack,
9652 hashtab_obstack_allocate,
9653 dummy_obstack_deallocate);
9654}
9655
80626a55 9656/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9657
19c3d4c9 9658struct create_dwo_cu_data
3019eac3
DE
9659{
9660 struct dwo_file *dwo_file;
19c3d4c9 9661 struct dwo_unit dwo_unit;
3019eac3
DE
9662};
9663
19c3d4c9 9664/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9665
9666static void
19c3d4c9
DE
9667create_dwo_cu_reader (const struct die_reader_specs *reader,
9668 const gdb_byte *info_ptr,
9669 struct die_info *comp_unit_die,
9670 int has_children,
9671 void *datap)
3019eac3
DE
9672{
9673 struct dwarf2_cu *cu = reader->cu;
9c541725 9674 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9675 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9676 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9677 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9678 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9679 struct attribute *attr;
3019eac3
DE
9680
9681 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9682 if (attr == NULL)
9683 {
19c3d4c9
DE
9684 complaint (&symfile_complaints,
9685 _("Dwarf Error: debug entry at offset 0x%x is missing"
9686 " its dwo_id [in module %s]"),
9c541725 9687 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9688 return;
9689 }
9690
3019eac3
DE
9691 dwo_unit->dwo_file = dwo_file;
9692 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9693 dwo_unit->section = section;
9c541725 9694 dwo_unit->sect_off = sect_off;
3019eac3
DE
9695 dwo_unit->length = cu->per_cu->length;
9696
b4f54984 9697 if (dwarf_read_debug)
4031ecc5 9698 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9699 to_underlying (sect_off),
9700 hex_string (dwo_unit->signature));
3019eac3
DE
9701}
9702
19c3d4c9
DE
9703/* Create the dwo_unit for the lone CU in DWO_FILE.
9704 Note: This function processes DWO files only, not DWP files. */
3019eac3 9705
19c3d4c9
DE
9706static struct dwo_unit *
9707create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9708{
9709 struct objfile *objfile = dwarf2_per_objfile->objfile;
9710 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9711 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9712 struct create_dwo_cu_data create_dwo_cu_data;
9713 struct dwo_unit *dwo_unit;
3019eac3
DE
9714
9715 dwarf2_read_section (objfile, section);
9716 info_ptr = section->buffer;
9717
9718 if (info_ptr == NULL)
9719 return NULL;
9720
b4f54984 9721 if (dwarf_read_debug)
19c3d4c9
DE
9722 {
9723 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9724 get_section_name (section),
9725 get_section_file_name (section));
19c3d4c9 9726 }
3019eac3 9727
19c3d4c9
DE
9728 create_dwo_cu_data.dwo_file = dwo_file;
9729 dwo_unit = NULL;
3019eac3
DE
9730
9731 end_ptr = info_ptr + section->size;
9732 while (info_ptr < end_ptr)
9733 {
9734 struct dwarf2_per_cu_data per_cu;
9735
19c3d4c9
DE
9736 memset (&create_dwo_cu_data.dwo_unit, 0,
9737 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9738 memset (&per_cu, 0, sizeof (per_cu));
9739 per_cu.objfile = objfile;
9740 per_cu.is_debug_types = 0;
9c541725 9741 per_cu.sect_off = sect_offset (info_ptr - section->buffer);
8a0459fd 9742 per_cu.section = section;
3019eac3 9743
33e80786 9744 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9745 create_dwo_cu_reader,
9746 &create_dwo_cu_data);
9747
9748 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9749 {
9750 /* If we've already found one, complain. We only support one
9751 because having more than one requires hacking the dwo_name of
9752 each to match, which is highly unlikely to happen. */
9753 if (dwo_unit != NULL)
9754 {
9755 complaint (&symfile_complaints,
9756 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9757 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9758 break;
9759 }
9760
9761 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9762 *dwo_unit = create_dwo_cu_data.dwo_unit;
9763 }
3019eac3
DE
9764
9765 info_ptr += per_cu.length;
9766 }
9767
19c3d4c9 9768 return dwo_unit;
3019eac3
DE
9769}
9770
80626a55
DE
9771/* DWP file .debug_{cu,tu}_index section format:
9772 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9773
d2415c6c
DE
9774 DWP Version 1:
9775
80626a55
DE
9776 Both index sections have the same format, and serve to map a 64-bit
9777 signature to a set of section numbers. Each section begins with a header,
9778 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9779 indexes, and a pool of 32-bit section numbers. The index sections will be
9780 aligned at 8-byte boundaries in the file.
9781
d2415c6c
DE
9782 The index section header consists of:
9783
9784 V, 32 bit version number
9785 -, 32 bits unused
9786 N, 32 bit number of compilation units or type units in the index
9787 M, 32 bit number of slots in the hash table
80626a55 9788
d2415c6c 9789 Numbers are recorded using the byte order of the application binary.
80626a55 9790
d2415c6c
DE
9791 The hash table begins at offset 16 in the section, and consists of an array
9792 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9793 order of the application binary). Unused slots in the hash table are 0.
9794 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9795
d2415c6c
DE
9796 The parallel table begins immediately after the hash table
9797 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9798 array of 32-bit indexes (using the byte order of the application binary),
9799 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9800 table contains a 32-bit index into the pool of section numbers. For unused
9801 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9802
73869dc2
DE
9803 The pool of section numbers begins immediately following the hash table
9804 (at offset 16 + 12 * M from the beginning of the section). The pool of
9805 section numbers consists of an array of 32-bit words (using the byte order
9806 of the application binary). Each item in the array is indexed starting
9807 from 0. The hash table entry provides the index of the first section
9808 number in the set. Additional section numbers in the set follow, and the
9809 set is terminated by a 0 entry (section number 0 is not used in ELF).
9810
9811 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9812 section must be the first entry in the set, and the .debug_abbrev.dwo must
9813 be the second entry. Other members of the set may follow in any order.
9814
9815 ---
9816
9817 DWP Version 2:
9818
9819 DWP Version 2 combines all the .debug_info, etc. sections into one,
9820 and the entries in the index tables are now offsets into these sections.
9821 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9822 section.
9823
9824 Index Section Contents:
9825 Header
9826 Hash Table of Signatures dwp_hash_table.hash_table
9827 Parallel Table of Indices dwp_hash_table.unit_table
9828 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9829 Table of Section Sizes dwp_hash_table.v2.sizes
9830
9831 The index section header consists of:
9832
9833 V, 32 bit version number
9834 L, 32 bit number of columns in the table of section offsets
9835 N, 32 bit number of compilation units or type units in the index
9836 M, 32 bit number of slots in the hash table
9837
9838 Numbers are recorded using the byte order of the application binary.
9839
9840 The hash table has the same format as version 1.
9841 The parallel table of indices has the same format as version 1,
9842 except that the entries are origin-1 indices into the table of sections
9843 offsets and the table of section sizes.
9844
9845 The table of offsets begins immediately following the parallel table
9846 (at offset 16 + 12 * M from the beginning of the section). The table is
9847 a two-dimensional array of 32-bit words (using the byte order of the
9848 application binary), with L columns and N+1 rows, in row-major order.
9849 Each row in the array is indexed starting from 0. The first row provides
9850 a key to the remaining rows: each column in this row provides an identifier
9851 for a debug section, and the offsets in the same column of subsequent rows
9852 refer to that section. The section identifiers are:
9853
9854 DW_SECT_INFO 1 .debug_info.dwo
9855 DW_SECT_TYPES 2 .debug_types.dwo
9856 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9857 DW_SECT_LINE 4 .debug_line.dwo
9858 DW_SECT_LOC 5 .debug_loc.dwo
9859 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9860 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9861 DW_SECT_MACRO 8 .debug_macro.dwo
9862
9863 The offsets provided by the CU and TU index sections are the base offsets
9864 for the contributions made by each CU or TU to the corresponding section
9865 in the package file. Each CU and TU header contains an abbrev_offset
9866 field, used to find the abbreviations table for that CU or TU within the
9867 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9868 be interpreted as relative to the base offset given in the index section.
9869 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9870 should be interpreted as relative to the base offset for .debug_line.dwo,
9871 and offsets into other debug sections obtained from DWARF attributes should
9872 also be interpreted as relative to the corresponding base offset.
9873
9874 The table of sizes begins immediately following the table of offsets.
9875 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9876 with L columns and N rows, in row-major order. Each row in the array is
9877 indexed starting from 1 (row 0 is shared by the two tables).
9878
9879 ---
9880
9881 Hash table lookup is handled the same in version 1 and 2:
9882
9883 We assume that N and M will not exceed 2^32 - 1.
9884 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9885
d2415c6c
DE
9886 Given a 64-bit compilation unit signature or a type signature S, an entry
9887 in the hash table is located as follows:
80626a55 9888
d2415c6c
DE
9889 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9890 the low-order k bits all set to 1.
80626a55 9891
d2415c6c 9892 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9893
d2415c6c
DE
9894 3) If the hash table entry at index H matches the signature, use that
9895 entry. If the hash table entry at index H is unused (all zeroes),
9896 terminate the search: the signature is not present in the table.
80626a55 9897
d2415c6c 9898 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9899
d2415c6c 9900 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9901 to stop at an unused slot or find the match. */
80626a55
DE
9902
9903/* Create a hash table to map DWO IDs to their CU/TU entry in
9904 .debug_{info,types}.dwo in DWP_FILE.
9905 Returns NULL if there isn't one.
9906 Note: This function processes DWP files only, not DWO files. */
9907
9908static struct dwp_hash_table *
9909create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9910{
9911 struct objfile *objfile = dwarf2_per_objfile->objfile;
9912 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9913 const gdb_byte *index_ptr, *index_end;
80626a55 9914 struct dwarf2_section_info *index;
73869dc2 9915 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9916 struct dwp_hash_table *htab;
9917
9918 if (is_debug_types)
9919 index = &dwp_file->sections.tu_index;
9920 else
9921 index = &dwp_file->sections.cu_index;
9922
9923 if (dwarf2_section_empty_p (index))
9924 return NULL;
9925 dwarf2_read_section (objfile, index);
9926
9927 index_ptr = index->buffer;
9928 index_end = index_ptr + index->size;
9929
9930 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9931 index_ptr += 4;
9932 if (version == 2)
9933 nr_columns = read_4_bytes (dbfd, index_ptr);
9934 else
9935 nr_columns = 0;
9936 index_ptr += 4;
80626a55
DE
9937 nr_units = read_4_bytes (dbfd, index_ptr);
9938 index_ptr += 4;
9939 nr_slots = read_4_bytes (dbfd, index_ptr);
9940 index_ptr += 4;
9941
73869dc2 9942 if (version != 1 && version != 2)
80626a55 9943 {
21aa081e 9944 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9945 " [in module %s]"),
21aa081e 9946 pulongest (version), dwp_file->name);
80626a55
DE
9947 }
9948 if (nr_slots != (nr_slots & -nr_slots))
9949 {
21aa081e 9950 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9951 " is not power of 2 [in module %s]"),
21aa081e 9952 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9953 }
9954
9955 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9956 htab->version = version;
9957 htab->nr_columns = nr_columns;
80626a55
DE
9958 htab->nr_units = nr_units;
9959 htab->nr_slots = nr_slots;
9960 htab->hash_table = index_ptr;
9961 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9962
9963 /* Exit early if the table is empty. */
9964 if (nr_slots == 0 || nr_units == 0
9965 || (version == 2 && nr_columns == 0))
9966 {
9967 /* All must be zero. */
9968 if (nr_slots != 0 || nr_units != 0
9969 || (version == 2 && nr_columns != 0))
9970 {
9971 complaint (&symfile_complaints,
9972 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9973 " all zero [in modules %s]"),
9974 dwp_file->name);
9975 }
9976 return htab;
9977 }
9978
9979 if (version == 1)
9980 {
9981 htab->section_pool.v1.indices =
9982 htab->unit_table + sizeof (uint32_t) * nr_slots;
9983 /* It's harder to decide whether the section is too small in v1.
9984 V1 is deprecated anyway so we punt. */
9985 }
9986 else
9987 {
9988 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9989 int *ids = htab->section_pool.v2.section_ids;
9990 /* Reverse map for error checking. */
9991 int ids_seen[DW_SECT_MAX + 1];
9992 int i;
9993
9994 if (nr_columns < 2)
9995 {
9996 error (_("Dwarf Error: bad DWP hash table, too few columns"
9997 " in section table [in module %s]"),
9998 dwp_file->name);
9999 }
10000 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10001 {
10002 error (_("Dwarf Error: bad DWP hash table, too many columns"
10003 " in section table [in module %s]"),
10004 dwp_file->name);
10005 }
10006 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10007 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10008 for (i = 0; i < nr_columns; ++i)
10009 {
10010 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10011
10012 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10013 {
10014 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10015 " in section table [in module %s]"),
10016 id, dwp_file->name);
10017 }
10018 if (ids_seen[id] != -1)
10019 {
10020 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10021 " id %d in section table [in module %s]"),
10022 id, dwp_file->name);
10023 }
10024 ids_seen[id] = i;
10025 ids[i] = id;
10026 }
10027 /* Must have exactly one info or types section. */
10028 if (((ids_seen[DW_SECT_INFO] != -1)
10029 + (ids_seen[DW_SECT_TYPES] != -1))
10030 != 1)
10031 {
10032 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10033 " DWO info/types section [in module %s]"),
10034 dwp_file->name);
10035 }
10036 /* Must have an abbrev section. */
10037 if (ids_seen[DW_SECT_ABBREV] == -1)
10038 {
10039 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10040 " section [in module %s]"),
10041 dwp_file->name);
10042 }
10043 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10044 htab->section_pool.v2.sizes =
10045 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10046 * nr_units * nr_columns);
10047 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10048 * nr_units * nr_columns))
10049 > index_end)
10050 {
10051 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10052 " [in module %s]"),
10053 dwp_file->name);
10054 }
10055 }
80626a55
DE
10056
10057 return htab;
10058}
10059
10060/* Update SECTIONS with the data from SECTP.
10061
10062 This function is like the other "locate" section routines that are
10063 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10064 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10065
10066 The result is non-zero for success, or zero if an error was found. */
10067
10068static int
73869dc2
DE
10069locate_v1_virtual_dwo_sections (asection *sectp,
10070 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10071{
10072 const struct dwop_section_names *names = &dwop_section_names;
10073
10074 if (section_is_p (sectp->name, &names->abbrev_dwo))
10075 {
10076 /* There can be only one. */
049412e3 10077 if (sections->abbrev.s.section != NULL)
80626a55 10078 return 0;
049412e3 10079 sections->abbrev.s.section = sectp;
80626a55
DE
10080 sections->abbrev.size = bfd_get_section_size (sectp);
10081 }
10082 else if (section_is_p (sectp->name, &names->info_dwo)
10083 || section_is_p (sectp->name, &names->types_dwo))
10084 {
10085 /* There can be only one. */
049412e3 10086 if (sections->info_or_types.s.section != NULL)
80626a55 10087 return 0;
049412e3 10088 sections->info_or_types.s.section = sectp;
80626a55
DE
10089 sections->info_or_types.size = bfd_get_section_size (sectp);
10090 }
10091 else if (section_is_p (sectp->name, &names->line_dwo))
10092 {
10093 /* There can be only one. */
049412e3 10094 if (sections->line.s.section != NULL)
80626a55 10095 return 0;
049412e3 10096 sections->line.s.section = sectp;
80626a55
DE
10097 sections->line.size = bfd_get_section_size (sectp);
10098 }
10099 else if (section_is_p (sectp->name, &names->loc_dwo))
10100 {
10101 /* There can be only one. */
049412e3 10102 if (sections->loc.s.section != NULL)
80626a55 10103 return 0;
049412e3 10104 sections->loc.s.section = sectp;
80626a55
DE
10105 sections->loc.size = bfd_get_section_size (sectp);
10106 }
10107 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10108 {
10109 /* There can be only one. */
049412e3 10110 if (sections->macinfo.s.section != NULL)
80626a55 10111 return 0;
049412e3 10112 sections->macinfo.s.section = sectp;
80626a55
DE
10113 sections->macinfo.size = bfd_get_section_size (sectp);
10114 }
10115 else if (section_is_p (sectp->name, &names->macro_dwo))
10116 {
10117 /* There can be only one. */
049412e3 10118 if (sections->macro.s.section != NULL)
80626a55 10119 return 0;
049412e3 10120 sections->macro.s.section = sectp;
80626a55
DE
10121 sections->macro.size = bfd_get_section_size (sectp);
10122 }
10123 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10124 {
10125 /* There can be only one. */
049412e3 10126 if (sections->str_offsets.s.section != NULL)
80626a55 10127 return 0;
049412e3 10128 sections->str_offsets.s.section = sectp;
80626a55
DE
10129 sections->str_offsets.size = bfd_get_section_size (sectp);
10130 }
10131 else
10132 {
10133 /* No other kind of section is valid. */
10134 return 0;
10135 }
10136
10137 return 1;
10138}
10139
73869dc2
DE
10140/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10141 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10142 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10143 This is for DWP version 1 files. */
80626a55
DE
10144
10145static struct dwo_unit *
73869dc2
DE
10146create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10147 uint32_t unit_index,
10148 const char *comp_dir,
10149 ULONGEST signature, int is_debug_types)
80626a55
DE
10150{
10151 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10152 const struct dwp_hash_table *dwp_htab =
10153 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10154 bfd *dbfd = dwp_file->dbfd;
10155 const char *kind = is_debug_types ? "TU" : "CU";
10156 struct dwo_file *dwo_file;
10157 struct dwo_unit *dwo_unit;
73869dc2 10158 struct virtual_v1_dwo_sections sections;
80626a55
DE
10159 void **dwo_file_slot;
10160 char *virtual_dwo_name;
80626a55
DE
10161 struct cleanup *cleanups;
10162 int i;
10163
73869dc2
DE
10164 gdb_assert (dwp_file->version == 1);
10165
b4f54984 10166 if (dwarf_read_debug)
80626a55 10167 {
73869dc2 10168 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10169 kind,
73869dc2 10170 pulongest (unit_index), hex_string (signature),
80626a55
DE
10171 dwp_file->name);
10172 }
10173
19ac8c2e 10174 /* Fetch the sections of this DWO unit.
80626a55
DE
10175 Put a limit on the number of sections we look for so that bad data
10176 doesn't cause us to loop forever. */
10177
73869dc2 10178#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10179 (1 /* .debug_info or .debug_types */ \
10180 + 1 /* .debug_abbrev */ \
10181 + 1 /* .debug_line */ \
10182 + 1 /* .debug_loc */ \
10183 + 1 /* .debug_str_offsets */ \
19ac8c2e 10184 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10185 + 1 /* trailing zero */)
10186
10187 memset (&sections, 0, sizeof (sections));
10188 cleanups = make_cleanup (null_cleanup, 0);
10189
73869dc2 10190 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10191 {
10192 asection *sectp;
10193 uint32_t section_nr =
10194 read_4_bytes (dbfd,
73869dc2
DE
10195 dwp_htab->section_pool.v1.indices
10196 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10197
10198 if (section_nr == 0)
10199 break;
10200 if (section_nr >= dwp_file->num_sections)
10201 {
10202 error (_("Dwarf Error: bad DWP hash table, section number too large"
10203 " [in module %s]"),
10204 dwp_file->name);
10205 }
10206
10207 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10208 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10209 {
10210 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10211 " [in module %s]"),
10212 dwp_file->name);
10213 }
10214 }
10215
10216 if (i < 2
a32a8923
DE
10217 || dwarf2_section_empty_p (&sections.info_or_types)
10218 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10219 {
10220 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10221 " [in module %s]"),
10222 dwp_file->name);
10223 }
73869dc2 10224 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10225 {
10226 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10227 " [in module %s]"),
10228 dwp_file->name);
10229 }
10230
10231 /* It's easier for the rest of the code if we fake a struct dwo_file and
10232 have dwo_unit "live" in that. At least for now.
10233
10234 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10235 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10236 file, we can combine them back into a virtual DWO file to save space
10237 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10238 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10239
2792b94d
PM
10240 virtual_dwo_name =
10241 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10242 get_section_id (&sections.abbrev),
10243 get_section_id (&sections.line),
10244 get_section_id (&sections.loc),
10245 get_section_id (&sections.str_offsets));
80626a55
DE
10246 make_cleanup (xfree, virtual_dwo_name);
10247 /* Can we use an existing virtual DWO file? */
0ac5b59e 10248 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10249 /* Create one if necessary. */
10250 if (*dwo_file_slot == NULL)
10251 {
b4f54984 10252 if (dwarf_read_debug)
80626a55
DE
10253 {
10254 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10255 virtual_dwo_name);
10256 }
10257 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10258 dwo_file->dwo_name
10259 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10260 virtual_dwo_name,
10261 strlen (virtual_dwo_name));
0ac5b59e 10262 dwo_file->comp_dir = comp_dir;
80626a55
DE
10263 dwo_file->sections.abbrev = sections.abbrev;
10264 dwo_file->sections.line = sections.line;
10265 dwo_file->sections.loc = sections.loc;
10266 dwo_file->sections.macinfo = sections.macinfo;
10267 dwo_file->sections.macro = sections.macro;
10268 dwo_file->sections.str_offsets = sections.str_offsets;
10269 /* The "str" section is global to the entire DWP file. */
10270 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10271 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10272 there's no need to record it in dwo_file.
10273 Also, we can't simply record type sections in dwo_file because
10274 we record a pointer into the vector in dwo_unit. As we collect more
10275 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10276 for it, invalidating all copies of pointers into the previous
10277 contents. */
80626a55
DE
10278 *dwo_file_slot = dwo_file;
10279 }
10280 else
10281 {
b4f54984 10282 if (dwarf_read_debug)
80626a55
DE
10283 {
10284 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10285 virtual_dwo_name);
10286 }
9a3c8263 10287 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10288 }
10289 do_cleanups (cleanups);
10290
10291 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10292 dwo_unit->dwo_file = dwo_file;
10293 dwo_unit->signature = signature;
8d749320
SM
10294 dwo_unit->section =
10295 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10296 *dwo_unit->section = sections.info_or_types;
57d63ce2 10297 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10298
10299 return dwo_unit;
10300}
10301
73869dc2
DE
10302/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10303 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10304 piece within that section used by a TU/CU, return a virtual section
10305 of just that piece. */
10306
10307static struct dwarf2_section_info
10308create_dwp_v2_section (struct dwarf2_section_info *section,
10309 bfd_size_type offset, bfd_size_type size)
10310{
10311 struct dwarf2_section_info result;
10312 asection *sectp;
10313
10314 gdb_assert (section != NULL);
10315 gdb_assert (!section->is_virtual);
10316
10317 memset (&result, 0, sizeof (result));
10318 result.s.containing_section = section;
10319 result.is_virtual = 1;
10320
10321 if (size == 0)
10322 return result;
10323
10324 sectp = get_section_bfd_section (section);
10325
10326 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10327 bounds of the real section. This is a pretty-rare event, so just
10328 flag an error (easier) instead of a warning and trying to cope. */
10329 if (sectp == NULL
10330 || offset + size > bfd_get_section_size (sectp))
10331 {
10332 bfd *abfd = sectp->owner;
10333
10334 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10335 " in section %s [in module %s]"),
10336 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10337 objfile_name (dwarf2_per_objfile->objfile));
10338 }
10339
10340 result.virtual_offset = offset;
10341 result.size = size;
10342 return result;
10343}
10344
10345/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10346 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10347 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10348 This is for DWP version 2 files. */
10349
10350static struct dwo_unit *
10351create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10352 uint32_t unit_index,
10353 const char *comp_dir,
10354 ULONGEST signature, int is_debug_types)
10355{
10356 struct objfile *objfile = dwarf2_per_objfile->objfile;
10357 const struct dwp_hash_table *dwp_htab =
10358 is_debug_types ? dwp_file->tus : dwp_file->cus;
10359 bfd *dbfd = dwp_file->dbfd;
10360 const char *kind = is_debug_types ? "TU" : "CU";
10361 struct dwo_file *dwo_file;
10362 struct dwo_unit *dwo_unit;
10363 struct virtual_v2_dwo_sections sections;
10364 void **dwo_file_slot;
10365 char *virtual_dwo_name;
73869dc2
DE
10366 struct cleanup *cleanups;
10367 int i;
10368
10369 gdb_assert (dwp_file->version == 2);
10370
b4f54984 10371 if (dwarf_read_debug)
73869dc2
DE
10372 {
10373 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10374 kind,
10375 pulongest (unit_index), hex_string (signature),
10376 dwp_file->name);
10377 }
10378
10379 /* Fetch the section offsets of this DWO unit. */
10380
10381 memset (&sections, 0, sizeof (sections));
10382 cleanups = make_cleanup (null_cleanup, 0);
10383
10384 for (i = 0; i < dwp_htab->nr_columns; ++i)
10385 {
10386 uint32_t offset = read_4_bytes (dbfd,
10387 dwp_htab->section_pool.v2.offsets
10388 + (((unit_index - 1) * dwp_htab->nr_columns
10389 + i)
10390 * sizeof (uint32_t)));
10391 uint32_t size = read_4_bytes (dbfd,
10392 dwp_htab->section_pool.v2.sizes
10393 + (((unit_index - 1) * dwp_htab->nr_columns
10394 + i)
10395 * sizeof (uint32_t)));
10396
10397 switch (dwp_htab->section_pool.v2.section_ids[i])
10398 {
10399 case DW_SECT_INFO:
10400 case DW_SECT_TYPES:
10401 sections.info_or_types_offset = offset;
10402 sections.info_or_types_size = size;
10403 break;
10404 case DW_SECT_ABBREV:
10405 sections.abbrev_offset = offset;
10406 sections.abbrev_size = size;
10407 break;
10408 case DW_SECT_LINE:
10409 sections.line_offset = offset;
10410 sections.line_size = size;
10411 break;
10412 case DW_SECT_LOC:
10413 sections.loc_offset = offset;
10414 sections.loc_size = size;
10415 break;
10416 case DW_SECT_STR_OFFSETS:
10417 sections.str_offsets_offset = offset;
10418 sections.str_offsets_size = size;
10419 break;
10420 case DW_SECT_MACINFO:
10421 sections.macinfo_offset = offset;
10422 sections.macinfo_size = size;
10423 break;
10424 case DW_SECT_MACRO:
10425 sections.macro_offset = offset;
10426 sections.macro_size = size;
10427 break;
10428 }
10429 }
10430
10431 /* It's easier for the rest of the code if we fake a struct dwo_file and
10432 have dwo_unit "live" in that. At least for now.
10433
10434 The DWP file can be made up of a random collection of CUs and TUs.
10435 However, for each CU + set of TUs that came from the same original DWO
10436 file, we can combine them back into a virtual DWO file to save space
10437 (fewer struct dwo_file objects to allocate). Remember that for really
10438 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10439
10440 virtual_dwo_name =
10441 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10442 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10443 (long) (sections.line_size ? sections.line_offset : 0),
10444 (long) (sections.loc_size ? sections.loc_offset : 0),
10445 (long) (sections.str_offsets_size
10446 ? sections.str_offsets_offset : 0));
10447 make_cleanup (xfree, virtual_dwo_name);
10448 /* Can we use an existing virtual DWO file? */
10449 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10450 /* Create one if necessary. */
10451 if (*dwo_file_slot == NULL)
10452 {
b4f54984 10453 if (dwarf_read_debug)
73869dc2
DE
10454 {
10455 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10456 virtual_dwo_name);
10457 }
10458 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10459 dwo_file->dwo_name
10460 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10461 virtual_dwo_name,
10462 strlen (virtual_dwo_name));
73869dc2
DE
10463 dwo_file->comp_dir = comp_dir;
10464 dwo_file->sections.abbrev =
10465 create_dwp_v2_section (&dwp_file->sections.abbrev,
10466 sections.abbrev_offset, sections.abbrev_size);
10467 dwo_file->sections.line =
10468 create_dwp_v2_section (&dwp_file->sections.line,
10469 sections.line_offset, sections.line_size);
10470 dwo_file->sections.loc =
10471 create_dwp_v2_section (&dwp_file->sections.loc,
10472 sections.loc_offset, sections.loc_size);
10473 dwo_file->sections.macinfo =
10474 create_dwp_v2_section (&dwp_file->sections.macinfo,
10475 sections.macinfo_offset, sections.macinfo_size);
10476 dwo_file->sections.macro =
10477 create_dwp_v2_section (&dwp_file->sections.macro,
10478 sections.macro_offset, sections.macro_size);
10479 dwo_file->sections.str_offsets =
10480 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10481 sections.str_offsets_offset,
10482 sections.str_offsets_size);
10483 /* The "str" section is global to the entire DWP file. */
10484 dwo_file->sections.str = dwp_file->sections.str;
10485 /* The info or types section is assigned below to dwo_unit,
10486 there's no need to record it in dwo_file.
10487 Also, we can't simply record type sections in dwo_file because
10488 we record a pointer into the vector in dwo_unit. As we collect more
10489 types we'll grow the vector and eventually have to reallocate space
10490 for it, invalidating all copies of pointers into the previous
10491 contents. */
10492 *dwo_file_slot = dwo_file;
10493 }
10494 else
10495 {
b4f54984 10496 if (dwarf_read_debug)
73869dc2
DE
10497 {
10498 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10499 virtual_dwo_name);
10500 }
9a3c8263 10501 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10502 }
10503 do_cleanups (cleanups);
10504
10505 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10506 dwo_unit->dwo_file = dwo_file;
10507 dwo_unit->signature = signature;
8d749320
SM
10508 dwo_unit->section =
10509 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10510 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10511 ? &dwp_file->sections.types
10512 : &dwp_file->sections.info,
10513 sections.info_or_types_offset,
10514 sections.info_or_types_size);
10515 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10516
10517 return dwo_unit;
10518}
10519
57d63ce2
DE
10520/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10521 Returns NULL if the signature isn't found. */
80626a55
DE
10522
10523static struct dwo_unit *
57d63ce2
DE
10524lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10525 ULONGEST signature, int is_debug_types)
80626a55 10526{
57d63ce2
DE
10527 const struct dwp_hash_table *dwp_htab =
10528 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10529 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10530 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10531 uint32_t hash = signature & mask;
10532 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10533 unsigned int i;
10534 void **slot;
870f88f7 10535 struct dwo_unit find_dwo_cu;
80626a55
DE
10536
10537 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10538 find_dwo_cu.signature = signature;
19ac8c2e
DE
10539 slot = htab_find_slot (is_debug_types
10540 ? dwp_file->loaded_tus
10541 : dwp_file->loaded_cus,
10542 &find_dwo_cu, INSERT);
80626a55
DE
10543
10544 if (*slot != NULL)
9a3c8263 10545 return (struct dwo_unit *) *slot;
80626a55
DE
10546
10547 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10548 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10549 {
10550 ULONGEST signature_in_table;
10551
10552 signature_in_table =
57d63ce2 10553 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10554 if (signature_in_table == signature)
10555 {
57d63ce2
DE
10556 uint32_t unit_index =
10557 read_4_bytes (dbfd,
10558 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10559
73869dc2
DE
10560 if (dwp_file->version == 1)
10561 {
10562 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10563 comp_dir, signature,
10564 is_debug_types);
10565 }
10566 else
10567 {
10568 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10569 comp_dir, signature,
10570 is_debug_types);
10571 }
9a3c8263 10572 return (struct dwo_unit *) *slot;
80626a55
DE
10573 }
10574 if (signature_in_table == 0)
10575 return NULL;
10576 hash = (hash + hash2) & mask;
10577 }
10578
10579 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10580 " [in module %s]"),
10581 dwp_file->name);
10582}
10583
ab5088bf 10584/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10585 Open the file specified by FILE_NAME and hand it off to BFD for
10586 preliminary analysis. Return a newly initialized bfd *, which
10587 includes a canonicalized copy of FILE_NAME.
80626a55 10588 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10589 SEARCH_CWD is true if the current directory is to be searched.
10590 It will be searched before debug-file-directory.
13aaf454
DE
10591 If successful, the file is added to the bfd include table of the
10592 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10593 If unable to find/open the file, return NULL.
3019eac3
DE
10594 NOTE: This function is derived from symfile_bfd_open. */
10595
192b62ce 10596static gdb_bfd_ref_ptr
6ac97d4c 10597try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10598{
80626a55 10599 int desc, flags;
3019eac3 10600 char *absolute_name;
9c02c129
DE
10601 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10602 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10603 to debug_file_directory. */
10604 char *search_path;
10605 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10606
6ac97d4c
DE
10607 if (search_cwd)
10608 {
10609 if (*debug_file_directory != '\0')
10610 search_path = concat (".", dirname_separator_string,
b36cec19 10611 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10612 else
10613 search_path = xstrdup (".");
10614 }
9c02c129 10615 else
6ac97d4c 10616 search_path = xstrdup (debug_file_directory);
3019eac3 10617
492c0ab7 10618 flags = OPF_RETURN_REALPATH;
80626a55
DE
10619 if (is_dwp)
10620 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10621 desc = openp (search_path, flags, file_name,
3019eac3 10622 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10623 xfree (search_path);
3019eac3
DE
10624 if (desc < 0)
10625 return NULL;
10626
192b62ce 10627 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10628 xfree (absolute_name);
9c02c129
DE
10629 if (sym_bfd == NULL)
10630 return NULL;
192b62ce 10631 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10632
192b62ce
TT
10633 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10634 return NULL;
3019eac3 10635
13aaf454
DE
10636 /* Success. Record the bfd as having been included by the objfile's bfd.
10637 This is important because things like demangled_names_hash lives in the
10638 objfile's per_bfd space and may have references to things like symbol
10639 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10640 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10641
3019eac3
DE
10642 return sym_bfd;
10643}
10644
ab5088bf 10645/* Try to open DWO file FILE_NAME.
3019eac3
DE
10646 COMP_DIR is the DW_AT_comp_dir attribute.
10647 The result is the bfd handle of the file.
10648 If there is a problem finding or opening the file, return NULL.
10649 Upon success, the canonicalized path of the file is stored in the bfd,
10650 same as symfile_bfd_open. */
10651
192b62ce 10652static gdb_bfd_ref_ptr
ab5088bf 10653open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10654{
80626a55 10655 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10656 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10657
10658 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10659
10660 if (comp_dir != NULL)
10661 {
b36cec19
PA
10662 char *path_to_try = concat (comp_dir, SLASH_STRING,
10663 file_name, (char *) NULL);
3019eac3
DE
10664
10665 /* NOTE: If comp_dir is a relative path, this will also try the
10666 search path, which seems useful. */
192b62ce
TT
10667 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10668 1 /*search_cwd*/));
3019eac3
DE
10669 xfree (path_to_try);
10670 if (abfd != NULL)
10671 return abfd;
10672 }
10673
10674 /* That didn't work, try debug-file-directory, which, despite its name,
10675 is a list of paths. */
10676
10677 if (*debug_file_directory == '\0')
10678 return NULL;
10679
6ac97d4c 10680 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10681}
10682
80626a55
DE
10683/* This function is mapped across the sections and remembers the offset and
10684 size of each of the DWO debugging sections we are interested in. */
10685
10686static void
10687dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10688{
9a3c8263 10689 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10690 const struct dwop_section_names *names = &dwop_section_names;
10691
10692 if (section_is_p (sectp->name, &names->abbrev_dwo))
10693 {
049412e3 10694 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10695 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10696 }
10697 else if (section_is_p (sectp->name, &names->info_dwo))
10698 {
049412e3 10699 dwo_sections->info.s.section = sectp;
80626a55
DE
10700 dwo_sections->info.size = bfd_get_section_size (sectp);
10701 }
10702 else if (section_is_p (sectp->name, &names->line_dwo))
10703 {
049412e3 10704 dwo_sections->line.s.section = sectp;
80626a55
DE
10705 dwo_sections->line.size = bfd_get_section_size (sectp);
10706 }
10707 else if (section_is_p (sectp->name, &names->loc_dwo))
10708 {
049412e3 10709 dwo_sections->loc.s.section = sectp;
80626a55
DE
10710 dwo_sections->loc.size = bfd_get_section_size (sectp);
10711 }
10712 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10713 {
049412e3 10714 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10715 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10716 }
10717 else if (section_is_p (sectp->name, &names->macro_dwo))
10718 {
049412e3 10719 dwo_sections->macro.s.section = sectp;
80626a55
DE
10720 dwo_sections->macro.size = bfd_get_section_size (sectp);
10721 }
10722 else if (section_is_p (sectp->name, &names->str_dwo))
10723 {
049412e3 10724 dwo_sections->str.s.section = sectp;
80626a55
DE
10725 dwo_sections->str.size = bfd_get_section_size (sectp);
10726 }
10727 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10728 {
049412e3 10729 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10730 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10731 }
10732 else if (section_is_p (sectp->name, &names->types_dwo))
10733 {
10734 struct dwarf2_section_info type_section;
10735
10736 memset (&type_section, 0, sizeof (type_section));
049412e3 10737 type_section.s.section = sectp;
80626a55
DE
10738 type_section.size = bfd_get_section_size (sectp);
10739 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10740 &type_section);
10741 }
10742}
10743
ab5088bf 10744/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10745 by PER_CU. This is for the non-DWP case.
80626a55 10746 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10747
10748static struct dwo_file *
0ac5b59e
DE
10749open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10750 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10751{
10752 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10753 struct dwo_file *dwo_file;
3019eac3
DE
10754 struct cleanup *cleanups;
10755
192b62ce 10756 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10757 if (dbfd == NULL)
10758 {
b4f54984 10759 if (dwarf_read_debug)
80626a55
DE
10760 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10761 return NULL;
10762 }
10763 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10764 dwo_file->dwo_name = dwo_name;
10765 dwo_file->comp_dir = comp_dir;
192b62ce 10766 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10767
10768 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10769
192b62ce
TT
10770 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10771 &dwo_file->sections);
3019eac3 10772
19c3d4c9 10773 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10774
78d4d2c5
JK
10775 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10776 dwo_file->tus);
3019eac3
DE
10777
10778 discard_cleanups (cleanups);
10779
b4f54984 10780 if (dwarf_read_debug)
80626a55
DE
10781 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10782
3019eac3
DE
10783 return dwo_file;
10784}
10785
80626a55 10786/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10787 size of each of the DWP debugging sections common to version 1 and 2 that
10788 we are interested in. */
3019eac3 10789
80626a55 10790static void
73869dc2
DE
10791dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10792 void *dwp_file_ptr)
3019eac3 10793{
9a3c8263 10794 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10795 const struct dwop_section_names *names = &dwop_section_names;
10796 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10797
80626a55 10798 /* Record the ELF section number for later lookup: this is what the
73869dc2 10799 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10800 gdb_assert (elf_section_nr < dwp_file->num_sections);
10801 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10802
80626a55
DE
10803 /* Look for specific sections that we need. */
10804 if (section_is_p (sectp->name, &names->str_dwo))
10805 {
049412e3 10806 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10807 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10808 }
10809 else if (section_is_p (sectp->name, &names->cu_index))
10810 {
049412e3 10811 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10812 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10813 }
10814 else if (section_is_p (sectp->name, &names->tu_index))
10815 {
049412e3 10816 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10817 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10818 }
10819}
3019eac3 10820
73869dc2
DE
10821/* This function is mapped across the sections and remembers the offset and
10822 size of each of the DWP version 2 debugging sections that we are interested
10823 in. This is split into a separate function because we don't know if we
10824 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10825
10826static void
10827dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10828{
9a3c8263 10829 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10830 const struct dwop_section_names *names = &dwop_section_names;
10831 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10832
10833 /* Record the ELF section number for later lookup: this is what the
10834 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10835 gdb_assert (elf_section_nr < dwp_file->num_sections);
10836 dwp_file->elf_sections[elf_section_nr] = sectp;
10837
10838 /* Look for specific sections that we need. */
10839 if (section_is_p (sectp->name, &names->abbrev_dwo))
10840 {
049412e3 10841 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10842 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10843 }
10844 else if (section_is_p (sectp->name, &names->info_dwo))
10845 {
049412e3 10846 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10847 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10848 }
10849 else if (section_is_p (sectp->name, &names->line_dwo))
10850 {
049412e3 10851 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10852 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10853 }
10854 else if (section_is_p (sectp->name, &names->loc_dwo))
10855 {
049412e3 10856 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10857 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10858 }
10859 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10860 {
049412e3 10861 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10862 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10863 }
10864 else if (section_is_p (sectp->name, &names->macro_dwo))
10865 {
049412e3 10866 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10867 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10868 }
10869 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10870 {
049412e3 10871 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10872 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10873 }
10874 else if (section_is_p (sectp->name, &names->types_dwo))
10875 {
049412e3 10876 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10877 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10878 }
10879}
10880
80626a55 10881/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10882
80626a55
DE
10883static hashval_t
10884hash_dwp_loaded_cutus (const void *item)
10885{
9a3c8263 10886 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10887
80626a55
DE
10888 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10889 return dwo_unit->signature;
3019eac3
DE
10890}
10891
80626a55 10892/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10893
80626a55
DE
10894static int
10895eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10896{
9a3c8263
SM
10897 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10898 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10899
80626a55
DE
10900 return dua->signature == dub->signature;
10901}
3019eac3 10902
80626a55 10903/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10904
80626a55
DE
10905static htab_t
10906allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10907{
10908 return htab_create_alloc_ex (3,
10909 hash_dwp_loaded_cutus,
10910 eq_dwp_loaded_cutus,
10911 NULL,
10912 &objfile->objfile_obstack,
10913 hashtab_obstack_allocate,
10914 dummy_obstack_deallocate);
10915}
3019eac3 10916
ab5088bf
DE
10917/* Try to open DWP file FILE_NAME.
10918 The result is the bfd handle of the file.
10919 If there is a problem finding or opening the file, return NULL.
10920 Upon success, the canonicalized path of the file is stored in the bfd,
10921 same as symfile_bfd_open. */
10922
192b62ce 10923static gdb_bfd_ref_ptr
ab5088bf
DE
10924open_dwp_file (const char *file_name)
10925{
192b62ce
TT
10926 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10927 1 /*search_cwd*/));
6ac97d4c
DE
10928 if (abfd != NULL)
10929 return abfd;
10930
10931 /* Work around upstream bug 15652.
10932 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10933 [Whether that's a "bug" is debatable, but it is getting in our way.]
10934 We have no real idea where the dwp file is, because gdb's realpath-ing
10935 of the executable's path may have discarded the needed info.
10936 [IWBN if the dwp file name was recorded in the executable, akin to
10937 .gnu_debuglink, but that doesn't exist yet.]
10938 Strip the directory from FILE_NAME and search again. */
10939 if (*debug_file_directory != '\0')
10940 {
10941 /* Don't implicitly search the current directory here.
10942 If the user wants to search "." to handle this case,
10943 it must be added to debug-file-directory. */
10944 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10945 0 /*search_cwd*/);
10946 }
10947
10948 return NULL;
ab5088bf
DE
10949}
10950
80626a55
DE
10951/* Initialize the use of the DWP file for the current objfile.
10952 By convention the name of the DWP file is ${objfile}.dwp.
10953 The result is NULL if it can't be found. */
a766d390 10954
80626a55 10955static struct dwp_file *
ab5088bf 10956open_and_init_dwp_file (void)
80626a55
DE
10957{
10958 struct objfile *objfile = dwarf2_per_objfile->objfile;
10959 struct dwp_file *dwp_file;
80626a55 10960
82bf32bc
JK
10961 /* Try to find first .dwp for the binary file before any symbolic links
10962 resolving. */
6c447423
DE
10963
10964 /* If the objfile is a debug file, find the name of the real binary
10965 file and get the name of dwp file from there. */
d721ba37 10966 std::string dwp_name;
6c447423
DE
10967 if (objfile->separate_debug_objfile_backlink != NULL)
10968 {
10969 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10970 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 10971
d721ba37 10972 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
10973 }
10974 else
d721ba37
PA
10975 dwp_name = objfile->original_name;
10976
10977 dwp_name += ".dwp";
80626a55 10978
d721ba37 10979 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
10980 if (dbfd == NULL
10981 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10982 {
10983 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
10984 dwp_name = objfile_name (objfile);
10985 dwp_name += ".dwp";
10986 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
10987 }
10988
80626a55
DE
10989 if (dbfd == NULL)
10990 {
b4f54984 10991 if (dwarf_read_debug)
d721ba37 10992 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 10993 return NULL;
3019eac3 10994 }
80626a55 10995 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10996 dwp_file->name = bfd_get_filename (dbfd.get ());
10997 dwp_file->dbfd = dbfd.release ();
c906108c 10998
80626a55 10999 /* +1: section 0 is unused */
192b62ce 11000 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11001 dwp_file->elf_sections =
11002 OBSTACK_CALLOC (&objfile->objfile_obstack,
11003 dwp_file->num_sections, asection *);
11004
192b62ce
TT
11005 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11006 dwp_file);
80626a55
DE
11007
11008 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11009
11010 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11011
73869dc2
DE
11012 /* The DWP file version is stored in the hash table. Oh well. */
11013 if (dwp_file->cus->version != dwp_file->tus->version)
11014 {
11015 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11016 pretty bizarre. We use pulongest here because that's the established
4d65956b 11017 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11018 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11019 " TU version %s [in DWP file %s]"),
11020 pulongest (dwp_file->cus->version),
d721ba37 11021 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11022 }
11023 dwp_file->version = dwp_file->cus->version;
11024
11025 if (dwp_file->version == 2)
192b62ce
TT
11026 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11027 dwp_file);
73869dc2 11028
19ac8c2e
DE
11029 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11030 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11031
b4f54984 11032 if (dwarf_read_debug)
80626a55
DE
11033 {
11034 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11035 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11036 " %s CUs, %s TUs\n",
11037 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11038 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11039 }
11040
11041 return dwp_file;
3019eac3 11042}
c906108c 11043
ab5088bf
DE
11044/* Wrapper around open_and_init_dwp_file, only open it once. */
11045
11046static struct dwp_file *
11047get_dwp_file (void)
11048{
11049 if (! dwarf2_per_objfile->dwp_checked)
11050 {
11051 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11052 dwarf2_per_objfile->dwp_checked = 1;
11053 }
11054 return dwarf2_per_objfile->dwp_file;
11055}
11056
80626a55
DE
11057/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11058 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11059 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11060 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11061 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11062
11063 This is called, for example, when wanting to read a variable with a
11064 complex location. Therefore we don't want to do file i/o for every call.
11065 Therefore we don't want to look for a DWO file on every call.
11066 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11067 then we check if we've already seen DWO_NAME, and only THEN do we check
11068 for a DWO file.
11069
1c658ad5 11070 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11071 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11072
3019eac3 11073static struct dwo_unit *
80626a55
DE
11074lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11075 const char *dwo_name, const char *comp_dir,
11076 ULONGEST signature, int is_debug_types)
3019eac3
DE
11077{
11078 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11079 const char *kind = is_debug_types ? "TU" : "CU";
11080 void **dwo_file_slot;
3019eac3 11081 struct dwo_file *dwo_file;
80626a55 11082 struct dwp_file *dwp_file;
cb1df416 11083
6a506a2d
DE
11084 /* First see if there's a DWP file.
11085 If we have a DWP file but didn't find the DWO inside it, don't
11086 look for the original DWO file. It makes gdb behave differently
11087 depending on whether one is debugging in the build tree. */
cf2c3c16 11088
ab5088bf 11089 dwp_file = get_dwp_file ();
80626a55 11090 if (dwp_file != NULL)
cf2c3c16 11091 {
80626a55
DE
11092 const struct dwp_hash_table *dwp_htab =
11093 is_debug_types ? dwp_file->tus : dwp_file->cus;
11094
11095 if (dwp_htab != NULL)
11096 {
11097 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11098 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11099 signature, is_debug_types);
80626a55
DE
11100
11101 if (dwo_cutu != NULL)
11102 {
b4f54984 11103 if (dwarf_read_debug)
80626a55
DE
11104 {
11105 fprintf_unfiltered (gdb_stdlog,
11106 "Virtual DWO %s %s found: @%s\n",
11107 kind, hex_string (signature),
11108 host_address_to_string (dwo_cutu));
11109 }
11110 return dwo_cutu;
11111 }
11112 }
11113 }
6a506a2d 11114 else
80626a55 11115 {
6a506a2d 11116 /* No DWP file, look for the DWO file. */
80626a55 11117
6a506a2d
DE
11118 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11119 if (*dwo_file_slot == NULL)
80626a55 11120 {
6a506a2d
DE
11121 /* Read in the file and build a table of the CUs/TUs it contains. */
11122 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11123 }
6a506a2d 11124 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11125 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11126
6a506a2d 11127 if (dwo_file != NULL)
19c3d4c9 11128 {
6a506a2d
DE
11129 struct dwo_unit *dwo_cutu = NULL;
11130
11131 if (is_debug_types && dwo_file->tus)
11132 {
11133 struct dwo_unit find_dwo_cutu;
11134
11135 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11136 find_dwo_cutu.signature = signature;
9a3c8263
SM
11137 dwo_cutu
11138 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11139 }
11140 else if (!is_debug_types && dwo_file->cu)
80626a55 11141 {
6a506a2d
DE
11142 if (signature == dwo_file->cu->signature)
11143 dwo_cutu = dwo_file->cu;
11144 }
11145
11146 if (dwo_cutu != NULL)
11147 {
b4f54984 11148 if (dwarf_read_debug)
6a506a2d
DE
11149 {
11150 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11151 kind, dwo_name, hex_string (signature),
11152 host_address_to_string (dwo_cutu));
11153 }
11154 return dwo_cutu;
80626a55
DE
11155 }
11156 }
2e276125 11157 }
9cdd5dbd 11158
80626a55
DE
11159 /* We didn't find it. This could mean a dwo_id mismatch, or
11160 someone deleted the DWO/DWP file, or the search path isn't set up
11161 correctly to find the file. */
11162
b4f54984 11163 if (dwarf_read_debug)
80626a55
DE
11164 {
11165 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11166 kind, dwo_name, hex_string (signature));
11167 }
3019eac3 11168
6656a72d
DE
11169 /* This is a warning and not a complaint because it can be caused by
11170 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11171 {
11172 /* Print the name of the DWP file if we looked there, helps the user
11173 better diagnose the problem. */
11174 char *dwp_text = NULL;
11175 struct cleanup *cleanups;
11176
11177 if (dwp_file != NULL)
11178 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11179 cleanups = make_cleanup (xfree, dwp_text);
11180
11181 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11182 " [in module %s]"),
11183 kind, dwo_name, hex_string (signature),
11184 dwp_text != NULL ? dwp_text : "",
11185 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11186 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11187
11188 do_cleanups (cleanups);
11189 }
3019eac3 11190 return NULL;
5fb290d7
DJ
11191}
11192
80626a55
DE
11193/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11194 See lookup_dwo_cutu_unit for details. */
11195
11196static struct dwo_unit *
11197lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11198 const char *dwo_name, const char *comp_dir,
11199 ULONGEST signature)
11200{
11201 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11202}
11203
11204/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11205 See lookup_dwo_cutu_unit for details. */
11206
11207static struct dwo_unit *
11208lookup_dwo_type_unit (struct signatured_type *this_tu,
11209 const char *dwo_name, const char *comp_dir)
11210{
11211 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11212}
11213
89e63ee4
DE
11214/* Traversal function for queue_and_load_all_dwo_tus. */
11215
11216static int
11217queue_and_load_dwo_tu (void **slot, void *info)
11218{
11219 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11220 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11221 ULONGEST signature = dwo_unit->signature;
11222 struct signatured_type *sig_type =
11223 lookup_dwo_signatured_type (per_cu->cu, signature);
11224
11225 if (sig_type != NULL)
11226 {
11227 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11228
11229 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11230 a real dependency of PER_CU on SIG_TYPE. That is detected later
11231 while processing PER_CU. */
11232 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11233 load_full_type_unit (sig_cu);
11234 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11235 }
11236
11237 return 1;
11238}
11239
11240/* Queue all TUs contained in the DWO of PER_CU to be read in.
11241 The DWO may have the only definition of the type, though it may not be
11242 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11243 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11244
11245static void
11246queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11247{
11248 struct dwo_unit *dwo_unit;
11249 struct dwo_file *dwo_file;
11250
11251 gdb_assert (!per_cu->is_debug_types);
11252 gdb_assert (get_dwp_file () == NULL);
11253 gdb_assert (per_cu->cu != NULL);
11254
11255 dwo_unit = per_cu->cu->dwo_unit;
11256 gdb_assert (dwo_unit != NULL);
11257
11258 dwo_file = dwo_unit->dwo_file;
11259 if (dwo_file->tus != NULL)
11260 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11261}
11262
3019eac3
DE
11263/* Free all resources associated with DWO_FILE.
11264 Close the DWO file and munmap the sections.
11265 All memory should be on the objfile obstack. */
348e048f
DE
11266
11267static void
3019eac3 11268free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11269{
348e048f 11270
5c6fa7ab 11271 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11272 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11273
3019eac3
DE
11274 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11275}
348e048f 11276
3019eac3 11277/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11278
3019eac3
DE
11279static void
11280free_dwo_file_cleanup (void *arg)
11281{
11282 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11283 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11284
3019eac3
DE
11285 free_dwo_file (dwo_file, objfile);
11286}
348e048f 11287
3019eac3 11288/* Traversal function for free_dwo_files. */
2ab95328 11289
3019eac3
DE
11290static int
11291free_dwo_file_from_slot (void **slot, void *info)
11292{
11293 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11294 struct objfile *objfile = (struct objfile *) info;
348e048f 11295
3019eac3 11296 free_dwo_file (dwo_file, objfile);
348e048f 11297
3019eac3
DE
11298 return 1;
11299}
348e048f 11300
3019eac3 11301/* Free all resources associated with DWO_FILES. */
348e048f 11302
3019eac3
DE
11303static void
11304free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11305{
11306 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11307}
3019eac3
DE
11308\f
11309/* Read in various DIEs. */
348e048f 11310
d389af10
JK
11311/* qsort helper for inherit_abstract_dies. */
11312
11313static int
11314unsigned_int_compar (const void *ap, const void *bp)
11315{
11316 unsigned int a = *(unsigned int *) ap;
11317 unsigned int b = *(unsigned int *) bp;
11318
11319 return (a > b) - (b > a);
11320}
11321
11322/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11323 Inherit only the children of the DW_AT_abstract_origin DIE not being
11324 already referenced by DW_AT_abstract_origin from the children of the
11325 current DIE. */
d389af10
JK
11326
11327static void
11328inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11329{
11330 struct die_info *child_die;
11331 unsigned die_children_count;
11332 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11333 sect_offset *offsets;
11334 sect_offset *offsets_end, *offsetp;
d389af10
JK
11335 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11336 struct die_info *origin_die;
11337 /* Iterator of the ORIGIN_DIE children. */
11338 struct die_info *origin_child_die;
11339 struct cleanup *cleanups;
11340 struct attribute *attr;
cd02d79d
PA
11341 struct dwarf2_cu *origin_cu;
11342 struct pending **origin_previous_list_in_scope;
d389af10
JK
11343
11344 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11345 if (!attr)
11346 return;
11347
cd02d79d
PA
11348 /* Note that following die references may follow to a die in a
11349 different cu. */
11350
11351 origin_cu = cu;
11352 origin_die = follow_die_ref (die, attr, &origin_cu);
11353
11354 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11355 symbols in. */
11356 origin_previous_list_in_scope = origin_cu->list_in_scope;
11357 origin_cu->list_in_scope = cu->list_in_scope;
11358
edb3359d
DJ
11359 if (die->tag != origin_die->tag
11360 && !(die->tag == DW_TAG_inlined_subroutine
11361 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11362 complaint (&symfile_complaints,
11363 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11364 to_underlying (die->sect_off),
11365 to_underlying (origin_die->sect_off));
d389af10
JK
11366
11367 child_die = die->child;
11368 die_children_count = 0;
11369 while (child_die && child_die->tag)
11370 {
11371 child_die = sibling_die (child_die);
11372 die_children_count++;
11373 }
8d749320 11374 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11375 cleanups = make_cleanup (xfree, offsets);
11376
11377 offsets_end = offsets;
3ea89b92
PMR
11378 for (child_die = die->child;
11379 child_die && child_die->tag;
11380 child_die = sibling_die (child_die))
11381 {
11382 struct die_info *child_origin_die;
11383 struct dwarf2_cu *child_origin_cu;
11384
11385 /* We are trying to process concrete instance entries:
216f72a1 11386 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11387 it's not relevant to our analysis here. i.e. detecting DIEs that are
11388 present in the abstract instance but not referenced in the concrete
11389 one. */
216f72a1
JK
11390 if (child_die->tag == DW_TAG_call_site
11391 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11392 continue;
11393
c38f313d
DJ
11394 /* For each CHILD_DIE, find the corresponding child of
11395 ORIGIN_DIE. If there is more than one layer of
11396 DW_AT_abstract_origin, follow them all; there shouldn't be,
11397 but GCC versions at least through 4.4 generate this (GCC PR
11398 40573). */
3ea89b92
PMR
11399 child_origin_die = child_die;
11400 child_origin_cu = cu;
c38f313d
DJ
11401 while (1)
11402 {
cd02d79d
PA
11403 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11404 child_origin_cu);
c38f313d
DJ
11405 if (attr == NULL)
11406 break;
cd02d79d
PA
11407 child_origin_die = follow_die_ref (child_origin_die, attr,
11408 &child_origin_cu);
c38f313d
DJ
11409 }
11410
d389af10
JK
11411 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11412 counterpart may exist. */
c38f313d 11413 if (child_origin_die != child_die)
d389af10 11414 {
edb3359d
DJ
11415 if (child_die->tag != child_origin_die->tag
11416 && !(child_die->tag == DW_TAG_inlined_subroutine
11417 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11418 complaint (&symfile_complaints,
11419 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11420 "different tags"),
11421 to_underlying (child_die->sect_off),
11422 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11423 if (child_origin_die->parent != origin_die)
11424 complaint (&symfile_complaints,
11425 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11426 "different parents"),
11427 to_underlying (child_die->sect_off),
11428 to_underlying (child_origin_die->sect_off));
c38f313d 11429 else
9c541725 11430 *offsets_end++ = child_origin_die->sect_off;
d389af10 11431 }
d389af10
JK
11432 }
11433 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11434 unsigned_int_compar);
11435 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11436 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11437 complaint (&symfile_complaints,
11438 _("Multiple children of DIE 0x%x refer "
11439 "to DIE 0x%x as their abstract origin"),
9c541725 11440 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11441
11442 offsetp = offsets;
11443 origin_child_die = origin_die->child;
11444 while (origin_child_die && origin_child_die->tag)
11445 {
11446 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11447 while (offsetp < offsets_end
9c541725 11448 && *offsetp < origin_child_die->sect_off)
d389af10 11449 offsetp++;
b64f50a1 11450 if (offsetp >= offsets_end
9c541725 11451 || *offsetp > origin_child_die->sect_off)
d389af10 11452 {
adde2bff
DE
11453 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11454 Check whether we're already processing ORIGIN_CHILD_DIE.
11455 This can happen with mutually referenced abstract_origins.
11456 PR 16581. */
11457 if (!origin_child_die->in_process)
11458 process_die (origin_child_die, origin_cu);
d389af10
JK
11459 }
11460 origin_child_die = sibling_die (origin_child_die);
11461 }
cd02d79d 11462 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11463
11464 do_cleanups (cleanups);
11465}
11466
c906108c 11467static void
e7c27a73 11468read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11469{
e7c27a73 11470 struct objfile *objfile = cu->objfile;
3e29f34a 11471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11472 struct context_stack *newobj;
c906108c
SS
11473 CORE_ADDR lowpc;
11474 CORE_ADDR highpc;
11475 struct die_info *child_die;
edb3359d 11476 struct attribute *attr, *call_line, *call_file;
15d034d0 11477 const char *name;
e142c38c 11478 CORE_ADDR baseaddr;
801e3a5b 11479 struct block *block;
edb3359d 11480 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11481 VEC (symbolp) *template_args = NULL;
11482 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11483
11484 if (inlined_func)
11485 {
11486 /* If we do not have call site information, we can't show the
11487 caller of this inlined function. That's too confusing, so
11488 only use the scope for local variables. */
11489 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11490 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11491 if (call_line == NULL || call_file == NULL)
11492 {
11493 read_lexical_block_scope (die, cu);
11494 return;
11495 }
11496 }
c906108c 11497
e142c38c
DJ
11498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11499
94af9270 11500 name = dwarf2_name (die, cu);
c906108c 11501
e8d05480
JB
11502 /* Ignore functions with missing or empty names. These are actually
11503 illegal according to the DWARF standard. */
11504 if (name == NULL)
11505 {
11506 complaint (&symfile_complaints,
b64f50a1 11507 _("missing name for subprogram DIE at %d"),
9c541725 11508 to_underlying (die->sect_off));
e8d05480
JB
11509 return;
11510 }
11511
11512 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11513 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11514 <= PC_BOUNDS_INVALID)
e8d05480 11515 {
ae4d0c03
PM
11516 attr = dwarf2_attr (die, DW_AT_external, cu);
11517 if (!attr || !DW_UNSND (attr))
11518 complaint (&symfile_complaints,
3e43a32a
MS
11519 _("cannot get low and high bounds "
11520 "for subprogram DIE at %d"),
9c541725 11521 to_underlying (die->sect_off));
e8d05480
JB
11522 return;
11523 }
c906108c 11524
3e29f34a
MR
11525 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11526 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11527
34eaf542
TT
11528 /* If we have any template arguments, then we must allocate a
11529 different sort of symbol. */
11530 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11531 {
11532 if (child_die->tag == DW_TAG_template_type_param
11533 || child_die->tag == DW_TAG_template_value_param)
11534 {
e623cf5d 11535 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11536 templ_func->base.is_cplus_template_function = 1;
11537 break;
11538 }
11539 }
11540
fe978cb0
PA
11541 newobj = push_context (0, lowpc);
11542 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11543 (struct symbol *) templ_func);
4c2df51b 11544
4cecd739
DJ
11545 /* If there is a location expression for DW_AT_frame_base, record
11546 it. */
e142c38c 11547 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11548 if (attr)
fe978cb0 11549 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11550
63e43d3a
PMR
11551 /* If there is a location for the static link, record it. */
11552 newobj->static_link = NULL;
11553 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11554 if (attr)
11555 {
224c3ddb
SM
11556 newobj->static_link
11557 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11558 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11559 }
11560
e142c38c 11561 cu->list_in_scope = &local_symbols;
c906108c 11562
639d11d3 11563 if (die->child != NULL)
c906108c 11564 {
639d11d3 11565 child_die = die->child;
c906108c
SS
11566 while (child_die && child_die->tag)
11567 {
34eaf542
TT
11568 if (child_die->tag == DW_TAG_template_type_param
11569 || child_die->tag == DW_TAG_template_value_param)
11570 {
11571 struct symbol *arg = new_symbol (child_die, NULL, cu);
11572
f1078f66
DJ
11573 if (arg != NULL)
11574 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11575 }
11576 else
11577 process_die (child_die, cu);
c906108c
SS
11578 child_die = sibling_die (child_die);
11579 }
11580 }
11581
d389af10
JK
11582 inherit_abstract_dies (die, cu);
11583
4a811a97
UW
11584 /* If we have a DW_AT_specification, we might need to import using
11585 directives from the context of the specification DIE. See the
11586 comment in determine_prefix. */
11587 if (cu->language == language_cplus
11588 && dwarf2_attr (die, DW_AT_specification, cu))
11589 {
11590 struct dwarf2_cu *spec_cu = cu;
11591 struct die_info *spec_die = die_specification (die, &spec_cu);
11592
11593 while (spec_die)
11594 {
11595 child_die = spec_die->child;
11596 while (child_die && child_die->tag)
11597 {
11598 if (child_die->tag == DW_TAG_imported_module)
11599 process_die (child_die, spec_cu);
11600 child_die = sibling_die (child_die);
11601 }
11602
11603 /* In some cases, GCC generates specification DIEs that
11604 themselves contain DW_AT_specification attributes. */
11605 spec_die = die_specification (spec_die, &spec_cu);
11606 }
11607 }
11608
fe978cb0 11609 newobj = pop_context ();
c906108c 11610 /* Make a block for the local symbols within. */
fe978cb0 11611 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11612 newobj->static_link, lowpc, highpc);
801e3a5b 11613
df8a16a1 11614 /* For C++, set the block's scope. */
45280282
IB
11615 if ((cu->language == language_cplus
11616 || cu->language == language_fortran
c44af4eb
TT
11617 || cu->language == language_d
11618 || cu->language == language_rust)
4d4ec4e5 11619 && cu->processing_has_namespace_info)
195a3f6c
TT
11620 block_set_scope (block, determine_prefix (die, cu),
11621 &objfile->objfile_obstack);
df8a16a1 11622
801e3a5b
JB
11623 /* If we have address ranges, record them. */
11624 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11625
fe978cb0 11626 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11627
34eaf542
TT
11628 /* Attach template arguments to function. */
11629 if (! VEC_empty (symbolp, template_args))
11630 {
11631 gdb_assert (templ_func != NULL);
11632
11633 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11634 templ_func->template_arguments
8d749320
SM
11635 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11636 templ_func->n_template_arguments);
34eaf542
TT
11637 memcpy (templ_func->template_arguments,
11638 VEC_address (symbolp, template_args),
11639 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11640 VEC_free (symbolp, template_args);
11641 }
11642
208d8187
JB
11643 /* In C++, we can have functions nested inside functions (e.g., when
11644 a function declares a class that has methods). This means that
11645 when we finish processing a function scope, we may need to go
11646 back to building a containing block's symbol lists. */
fe978cb0 11647 local_symbols = newobj->locals;
22cee43f 11648 local_using_directives = newobj->local_using_directives;
208d8187 11649
921e78cf
JB
11650 /* If we've finished processing a top-level function, subsequent
11651 symbols go in the file symbol list. */
11652 if (outermost_context_p ())
e142c38c 11653 cu->list_in_scope = &file_symbols;
c906108c
SS
11654}
11655
11656/* Process all the DIES contained within a lexical block scope. Start
11657 a new scope, process the dies, and then close the scope. */
11658
11659static void
e7c27a73 11660read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11661{
e7c27a73 11662 struct objfile *objfile = cu->objfile;
3e29f34a 11663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11664 struct context_stack *newobj;
c906108c
SS
11665 CORE_ADDR lowpc, highpc;
11666 struct die_info *child_die;
e142c38c
DJ
11667 CORE_ADDR baseaddr;
11668
11669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11670
11671 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11672 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11673 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11674 be nasty. Might be easier to properly extend generic blocks to
af34e669 11675 describe ranges. */
e385593e
JK
11676 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11677 {
11678 case PC_BOUNDS_NOT_PRESENT:
11679 /* DW_TAG_lexical_block has no attributes, process its children as if
11680 there was no wrapping by that DW_TAG_lexical_block.
11681 GCC does no longer produces such DWARF since GCC r224161. */
11682 for (child_die = die->child;
11683 child_die != NULL && child_die->tag;
11684 child_die = sibling_die (child_die))
11685 process_die (child_die, cu);
11686 return;
11687 case PC_BOUNDS_INVALID:
11688 return;
11689 }
3e29f34a
MR
11690 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11691 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11692
11693 push_context (0, lowpc);
639d11d3 11694 if (die->child != NULL)
c906108c 11695 {
639d11d3 11696 child_die = die->child;
c906108c
SS
11697 while (child_die && child_die->tag)
11698 {
e7c27a73 11699 process_die (child_die, cu);
c906108c
SS
11700 child_die = sibling_die (child_die);
11701 }
11702 }
3ea89b92 11703 inherit_abstract_dies (die, cu);
fe978cb0 11704 newobj = pop_context ();
c906108c 11705
22cee43f 11706 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11707 {
801e3a5b 11708 struct block *block
63e43d3a 11709 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11710 newobj->start_addr, highpc);
801e3a5b
JB
11711
11712 /* Note that recording ranges after traversing children, as we
11713 do here, means that recording a parent's ranges entails
11714 walking across all its children's ranges as they appear in
11715 the address map, which is quadratic behavior.
11716
11717 It would be nicer to record the parent's ranges before
11718 traversing its children, simply overriding whatever you find
11719 there. But since we don't even decide whether to create a
11720 block until after we've traversed its children, that's hard
11721 to do. */
11722 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11723 }
fe978cb0 11724 local_symbols = newobj->locals;
22cee43f 11725 local_using_directives = newobj->local_using_directives;
c906108c
SS
11726}
11727
216f72a1 11728/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11729
11730static void
11731read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11732{
11733 struct objfile *objfile = cu->objfile;
11734 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11735 CORE_ADDR pc, baseaddr;
11736 struct attribute *attr;
11737 struct call_site *call_site, call_site_local;
11738 void **slot;
11739 int nparams;
11740 struct die_info *child_die;
11741
11742 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11743
216f72a1
JK
11744 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11745 if (attr == NULL)
11746 {
11747 /* This was a pre-DWARF-5 GNU extension alias
11748 for DW_AT_call_return_pc. */
11749 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11750 }
96408a79
SA
11751 if (!attr)
11752 {
11753 complaint (&symfile_complaints,
216f72a1 11754 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11755 "DIE 0x%x [in module %s]"),
9c541725 11756 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11757 return;
11758 }
31aa7e4e 11759 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11760 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11761
11762 if (cu->call_site_htab == NULL)
11763 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11764 NULL, &objfile->objfile_obstack,
11765 hashtab_obstack_allocate, NULL);
11766 call_site_local.pc = pc;
11767 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11768 if (*slot != NULL)
11769 {
11770 complaint (&symfile_complaints,
216f72a1 11771 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11772 "DIE 0x%x [in module %s]"),
9c541725 11773 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11774 objfile_name (objfile));
96408a79
SA
11775 return;
11776 }
11777
11778 /* Count parameters at the caller. */
11779
11780 nparams = 0;
11781 for (child_die = die->child; child_die && child_die->tag;
11782 child_die = sibling_die (child_die))
11783 {
216f72a1
JK
11784 if (child_die->tag != DW_TAG_call_site_parameter
11785 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11786 {
11787 complaint (&symfile_complaints,
216f72a1
JK
11788 _("Tag %d is not DW_TAG_call_site_parameter in "
11789 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11790 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11791 objfile_name (objfile));
96408a79
SA
11792 continue;
11793 }
11794
11795 nparams++;
11796 }
11797
224c3ddb
SM
11798 call_site
11799 = ((struct call_site *)
11800 obstack_alloc (&objfile->objfile_obstack,
11801 sizeof (*call_site)
11802 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11803 *slot = call_site;
11804 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11805 call_site->pc = pc;
11806
216f72a1
JK
11807 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11808 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11809 {
11810 struct die_info *func_die;
11811
11812 /* Skip also over DW_TAG_inlined_subroutine. */
11813 for (func_die = die->parent;
11814 func_die && func_die->tag != DW_TAG_subprogram
11815 && func_die->tag != DW_TAG_subroutine_type;
11816 func_die = func_die->parent);
11817
216f72a1
JK
11818 /* DW_AT_call_all_calls is a superset
11819 of DW_AT_call_all_tail_calls. */
96408a79 11820 if (func_die
216f72a1 11821 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11822 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11823 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11824 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11825 {
11826 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11827 not complete. But keep CALL_SITE for look ups via call_site_htab,
11828 both the initial caller containing the real return address PC and
11829 the final callee containing the current PC of a chain of tail
11830 calls do not need to have the tail call list complete. But any
11831 function candidate for a virtual tail call frame searched via
11832 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11833 determined unambiguously. */
11834 }
11835 else
11836 {
11837 struct type *func_type = NULL;
11838
11839 if (func_die)
11840 func_type = get_die_type (func_die, cu);
11841 if (func_type != NULL)
11842 {
11843 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11844
11845 /* Enlist this call site to the function. */
11846 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11847 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11848 }
11849 else
11850 complaint (&symfile_complaints,
216f72a1 11851 _("Cannot find function owning DW_TAG_call_site "
96408a79 11852 "DIE 0x%x [in module %s]"),
9c541725 11853 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11854 }
11855 }
11856
216f72a1
JK
11857 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11858 if (attr == NULL)
11859 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11860 if (attr == NULL)
11861 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11862 if (attr == NULL)
216f72a1
JK
11863 {
11864 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11865 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11866 }
96408a79
SA
11867 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11868 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11869 /* Keep NULL DWARF_BLOCK. */;
11870 else if (attr_form_is_block (attr))
11871 {
11872 struct dwarf2_locexpr_baton *dlbaton;
11873
8d749320 11874 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11875 dlbaton->data = DW_BLOCK (attr)->data;
11876 dlbaton->size = DW_BLOCK (attr)->size;
11877 dlbaton->per_cu = cu->per_cu;
11878
11879 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11880 }
7771576e 11881 else if (attr_form_is_ref (attr))
96408a79 11882 {
96408a79
SA
11883 struct dwarf2_cu *target_cu = cu;
11884 struct die_info *target_die;
11885
ac9ec31b 11886 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11887 gdb_assert (target_cu->objfile == objfile);
11888 if (die_is_declaration (target_die, target_cu))
11889 {
7d45c7c3 11890 const char *target_physname;
9112db09
JK
11891
11892 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11893 target_physname = dwarf2_string_attr (target_die,
11894 DW_AT_linkage_name,
11895 target_cu);
11896 if (target_physname == NULL)
11897 target_physname = dwarf2_string_attr (target_die,
11898 DW_AT_MIPS_linkage_name,
11899 target_cu);
11900 if (target_physname == NULL)
9112db09 11901 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11902 if (target_physname == NULL)
11903 complaint (&symfile_complaints,
216f72a1 11904 _("DW_AT_call_target target DIE has invalid "
96408a79 11905 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 11906 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11907 else
7d455152 11908 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11909 }
11910 else
11911 {
11912 CORE_ADDR lowpc;
11913
11914 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11915 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11916 <= PC_BOUNDS_INVALID)
96408a79 11917 complaint (&symfile_complaints,
216f72a1 11918 _("DW_AT_call_target target DIE has invalid "
96408a79 11919 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 11920 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11921 else
3e29f34a
MR
11922 {
11923 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11924 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11925 }
96408a79
SA
11926 }
11927 }
11928 else
11929 complaint (&symfile_complaints,
216f72a1 11930 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11931 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 11932 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11933
11934 call_site->per_cu = cu->per_cu;
11935
11936 for (child_die = die->child;
11937 child_die && child_die->tag;
11938 child_die = sibling_die (child_die))
11939 {
96408a79 11940 struct call_site_parameter *parameter;
1788b2d3 11941 struct attribute *loc, *origin;
96408a79 11942
216f72a1
JK
11943 if (child_die->tag != DW_TAG_call_site_parameter
11944 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11945 {
11946 /* Already printed the complaint above. */
11947 continue;
11948 }
11949
11950 gdb_assert (call_site->parameter_count < nparams);
11951 parameter = &call_site->parameter[call_site->parameter_count];
11952
1788b2d3
JK
11953 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11954 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11955 register is contained in DW_AT_call_value. */
96408a79 11956
24c5c679 11957 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11958 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11959 if (origin == NULL)
11960 {
11961 /* This was a pre-DWARF-5 GNU extension alias
11962 for DW_AT_call_parameter. */
11963 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11964 }
7771576e 11965 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 11966 {
1788b2d3 11967 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
11968
11969 sect_offset sect_off
11970 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11971 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
11972 {
11973 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11974 binding can be done only inside one CU. Such referenced DIE
11975 therefore cannot be even moved to DW_TAG_partial_unit. */
11976 complaint (&symfile_complaints,
216f72a1
JK
11977 _("DW_AT_call_parameter offset is not in CU for "
11978 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
11979 to_underlying (child_die->sect_off),
11980 objfile_name (objfile));
d76b7dbc
JK
11981 continue;
11982 }
9c541725
PA
11983 parameter->u.param_cu_off
11984 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
11985 }
11986 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11987 {
11988 complaint (&symfile_complaints,
11989 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11990 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11991 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
11992 continue;
11993 }
24c5c679 11994 else
96408a79 11995 {
24c5c679
JK
11996 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11997 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11998 if (parameter->u.dwarf_reg != -1)
11999 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12000 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12001 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12002 &parameter->u.fb_offset))
12003 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12004 else
12005 {
12006 complaint (&symfile_complaints,
12007 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12008 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12009 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12010 "[in module %s]"),
9c541725
PA
12011 to_underlying (child_die->sect_off),
12012 objfile_name (objfile));
24c5c679
JK
12013 continue;
12014 }
96408a79
SA
12015 }
12016
216f72a1
JK
12017 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12018 if (attr == NULL)
12019 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12020 if (!attr_form_is_block (attr))
12021 {
12022 complaint (&symfile_complaints,
216f72a1
JK
12023 _("No DW_FORM_block* DW_AT_call_value for "
12024 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12025 to_underlying (child_die->sect_off),
12026 objfile_name (objfile));
96408a79
SA
12027 continue;
12028 }
12029 parameter->value = DW_BLOCK (attr)->data;
12030 parameter->value_size = DW_BLOCK (attr)->size;
12031
12032 /* Parameters are not pre-cleared by memset above. */
12033 parameter->data_value = NULL;
12034 parameter->data_value_size = 0;
12035 call_site->parameter_count++;
12036
216f72a1
JK
12037 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12038 if (attr == NULL)
12039 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12040 if (attr)
12041 {
12042 if (!attr_form_is_block (attr))
12043 complaint (&symfile_complaints,
216f72a1
JK
12044 _("No DW_FORM_block* DW_AT_call_data_value for "
12045 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12046 to_underlying (child_die->sect_off),
12047 objfile_name (objfile));
96408a79
SA
12048 else
12049 {
12050 parameter->data_value = DW_BLOCK (attr)->data;
12051 parameter->data_value_size = DW_BLOCK (attr)->size;
12052 }
12053 }
12054 }
12055}
12056
43988095
JK
12057/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12058 reading .debug_rnglists.
12059 Callback's type should be:
12060 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12061 Return true if the attributes are present and valid, otherwise,
12062 return false. */
12063
12064template <typename Callback>
12065static bool
12066dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12067 Callback &&callback)
12068{
12069 struct objfile *objfile = cu->objfile;
12070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12071 struct comp_unit_head *cu_header = &cu->header;
12072 bfd *obfd = objfile->obfd;
12073 unsigned int addr_size = cu_header->addr_size;
12074 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12075 /* Base address selection entry. */
12076 CORE_ADDR base;
12077 int found_base;
12078 unsigned int dummy;
12079 const gdb_byte *buffer;
12080 CORE_ADDR low = 0;
12081 CORE_ADDR high = 0;
12082 CORE_ADDR baseaddr;
12083 bool overflow = false;
12084
12085 found_base = cu->base_known;
12086 base = cu->base_address;
12087
12088 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12089 if (offset >= dwarf2_per_objfile->rnglists.size)
12090 {
12091 complaint (&symfile_complaints,
12092 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12093 offset);
12094 return false;
12095 }
12096 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12097
12098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12099
12100 while (1)
12101 {
7814882a
JK
12102 /* Initialize it due to a false compiler warning. */
12103 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12104 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12105 + dwarf2_per_objfile->rnglists.size);
12106 unsigned int bytes_read;
12107
12108 if (buffer == buf_end)
12109 {
12110 overflow = true;
12111 break;
12112 }
12113 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12114 switch (rlet)
12115 {
12116 case DW_RLE_end_of_list:
12117 break;
12118 case DW_RLE_base_address:
12119 if (buffer + cu->header.addr_size > buf_end)
12120 {
12121 overflow = true;
12122 break;
12123 }
12124 base = read_address (obfd, buffer, cu, &bytes_read);
12125 found_base = 1;
12126 buffer += bytes_read;
12127 break;
12128 case DW_RLE_start_length:
12129 if (buffer + cu->header.addr_size > buf_end)
12130 {
12131 overflow = true;
12132 break;
12133 }
12134 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12135 buffer += bytes_read;
12136 range_end = (range_beginning
12137 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12138 buffer += bytes_read;
12139 if (buffer > buf_end)
12140 {
12141 overflow = true;
12142 break;
12143 }
12144 break;
12145 case DW_RLE_offset_pair:
12146 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12147 buffer += bytes_read;
12148 if (buffer > buf_end)
12149 {
12150 overflow = true;
12151 break;
12152 }
12153 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12154 buffer += bytes_read;
12155 if (buffer > buf_end)
12156 {
12157 overflow = true;
12158 break;
12159 }
12160 break;
12161 case DW_RLE_start_end:
12162 if (buffer + 2 * cu->header.addr_size > buf_end)
12163 {
12164 overflow = true;
12165 break;
12166 }
12167 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12168 buffer += bytes_read;
12169 range_end = read_address (obfd, buffer, cu, &bytes_read);
12170 buffer += bytes_read;
12171 break;
12172 default:
12173 complaint (&symfile_complaints,
12174 _("Invalid .debug_rnglists data (no base address)"));
12175 return false;
12176 }
12177 if (rlet == DW_RLE_end_of_list || overflow)
12178 break;
12179 if (rlet == DW_RLE_base_address)
12180 continue;
12181
12182 if (!found_base)
12183 {
12184 /* We have no valid base address for the ranges
12185 data. */
12186 complaint (&symfile_complaints,
12187 _("Invalid .debug_rnglists data (no base address)"));
12188 return false;
12189 }
12190
12191 if (range_beginning > range_end)
12192 {
12193 /* Inverted range entries are invalid. */
12194 complaint (&symfile_complaints,
12195 _("Invalid .debug_rnglists data (inverted range)"));
12196 return false;
12197 }
12198
12199 /* Empty range entries have no effect. */
12200 if (range_beginning == range_end)
12201 continue;
12202
12203 range_beginning += base;
12204 range_end += base;
12205
12206 /* A not-uncommon case of bad debug info.
12207 Don't pollute the addrmap with bad data. */
12208 if (range_beginning + baseaddr == 0
12209 && !dwarf2_per_objfile->has_section_at_zero)
12210 {
12211 complaint (&symfile_complaints,
12212 _(".debug_rnglists entry has start address of zero"
12213 " [in module %s]"), objfile_name (objfile));
12214 continue;
12215 }
12216
12217 callback (range_beginning, range_end);
12218 }
12219
12220 if (overflow)
12221 {
12222 complaint (&symfile_complaints,
12223 _("Offset %d is not terminated "
12224 "for DW_AT_ranges attribute"),
12225 offset);
12226 return false;
12227 }
12228
12229 return true;
12230}
12231
12232/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12233 Callback's type should be:
12234 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12235 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12236
43988095 12237template <typename Callback>
43039443 12238static int
5f46c5a5 12239dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12240 Callback &&callback)
43039443
JK
12241{
12242 struct objfile *objfile = cu->objfile;
3e29f34a 12243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12244 struct comp_unit_head *cu_header = &cu->header;
12245 bfd *obfd = objfile->obfd;
12246 unsigned int addr_size = cu_header->addr_size;
12247 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12248 /* Base address selection entry. */
12249 CORE_ADDR base;
12250 int found_base;
12251 unsigned int dummy;
d521ce57 12252 const gdb_byte *buffer;
ff013f42 12253 CORE_ADDR baseaddr;
43039443 12254
43988095
JK
12255 if (cu_header->version >= 5)
12256 return dwarf2_rnglists_process (offset, cu, callback);
12257
d00adf39
DE
12258 found_base = cu->base_known;
12259 base = cu->base_address;
43039443 12260
be391dca 12261 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12262 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12263 {
12264 complaint (&symfile_complaints,
12265 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12266 offset);
12267 return 0;
12268 }
dce234bc 12269 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12270
e7030f15 12271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12272
43039443
JK
12273 while (1)
12274 {
12275 CORE_ADDR range_beginning, range_end;
12276
12277 range_beginning = read_address (obfd, buffer, cu, &dummy);
12278 buffer += addr_size;
12279 range_end = read_address (obfd, buffer, cu, &dummy);
12280 buffer += addr_size;
12281 offset += 2 * addr_size;
12282
12283 /* An end of list marker is a pair of zero addresses. */
12284 if (range_beginning == 0 && range_end == 0)
12285 /* Found the end of list entry. */
12286 break;
12287
12288 /* Each base address selection entry is a pair of 2 values.
12289 The first is the largest possible address, the second is
12290 the base address. Check for a base address here. */
12291 if ((range_beginning & mask) == mask)
12292 {
28d2bfb9
AB
12293 /* If we found the largest possible address, then we already
12294 have the base address in range_end. */
12295 base = range_end;
43039443
JK
12296 found_base = 1;
12297 continue;
12298 }
12299
12300 if (!found_base)
12301 {
12302 /* We have no valid base address for the ranges
12303 data. */
12304 complaint (&symfile_complaints,
12305 _("Invalid .debug_ranges data (no base address)"));
12306 return 0;
12307 }
12308
9277c30c
UW
12309 if (range_beginning > range_end)
12310 {
12311 /* Inverted range entries are invalid. */
12312 complaint (&symfile_complaints,
12313 _("Invalid .debug_ranges data (inverted range)"));
12314 return 0;
12315 }
12316
12317 /* Empty range entries have no effect. */
12318 if (range_beginning == range_end)
12319 continue;
12320
43039443
JK
12321 range_beginning += base;
12322 range_end += base;
12323
01093045
DE
12324 /* A not-uncommon case of bad debug info.
12325 Don't pollute the addrmap with bad data. */
12326 if (range_beginning + baseaddr == 0
12327 && !dwarf2_per_objfile->has_section_at_zero)
12328 {
12329 complaint (&symfile_complaints,
12330 _(".debug_ranges entry has start address of zero"
4262abfb 12331 " [in module %s]"), objfile_name (objfile));
01093045
DE
12332 continue;
12333 }
12334
5f46c5a5
JK
12335 callback (range_beginning, range_end);
12336 }
12337
12338 return 1;
12339}
12340
12341/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12342 Return 1 if the attributes are present and valid, otherwise, return 0.
12343 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12344
12345static int
12346dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12347 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12348 struct partial_symtab *ranges_pst)
12349{
12350 struct objfile *objfile = cu->objfile;
12351 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12352 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12353 SECT_OFF_TEXT (objfile));
12354 int low_set = 0;
12355 CORE_ADDR low = 0;
12356 CORE_ADDR high = 0;
12357 int retval;
12358
12359 retval = dwarf2_ranges_process (offset, cu,
12360 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12361 {
9277c30c 12362 if (ranges_pst != NULL)
3e29f34a
MR
12363 {
12364 CORE_ADDR lowpc;
12365 CORE_ADDR highpc;
12366
12367 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12368 range_beginning + baseaddr);
12369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12370 range_end + baseaddr);
12371 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12372 ranges_pst);
12373 }
ff013f42 12374
43039443
JK
12375 /* FIXME: This is recording everything as a low-high
12376 segment of consecutive addresses. We should have a
12377 data structure for discontiguous block ranges
12378 instead. */
12379 if (! low_set)
12380 {
12381 low = range_beginning;
12382 high = range_end;
12383 low_set = 1;
12384 }
12385 else
12386 {
12387 if (range_beginning < low)
12388 low = range_beginning;
12389 if (range_end > high)
12390 high = range_end;
12391 }
5f46c5a5
JK
12392 });
12393 if (!retval)
12394 return 0;
43039443
JK
12395
12396 if (! low_set)
12397 /* If the first entry is an end-of-list marker, the range
12398 describes an empty scope, i.e. no instructions. */
12399 return 0;
12400
12401 if (low_return)
12402 *low_return = low;
12403 if (high_return)
12404 *high_return = high;
12405 return 1;
12406}
12407
3a2b436a
JK
12408/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12409 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12410 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12411
3a2b436a 12412static enum pc_bounds_kind
af34e669 12413dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12414 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12415 struct partial_symtab *pst)
c906108c
SS
12416{
12417 struct attribute *attr;
91da1414 12418 struct attribute *attr_high;
af34e669
DJ
12419 CORE_ADDR low = 0;
12420 CORE_ADDR high = 0;
e385593e 12421 enum pc_bounds_kind ret;
c906108c 12422
91da1414
MW
12423 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12424 if (attr_high)
af34e669 12425 {
e142c38c 12426 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12427 if (attr)
91da1414 12428 {
31aa7e4e
JB
12429 low = attr_value_as_address (attr);
12430 high = attr_value_as_address (attr_high);
12431 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12432 high += low;
91da1414 12433 }
af34e669
DJ
12434 else
12435 /* Found high w/o low attribute. */
e385593e 12436 return PC_BOUNDS_INVALID;
af34e669
DJ
12437
12438 /* Found consecutive range of addresses. */
3a2b436a 12439 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12440 }
c906108c 12441 else
af34e669 12442 {
e142c38c 12443 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12444 if (attr != NULL)
12445 {
ab435259
DE
12446 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12447 We take advantage of the fact that DW_AT_ranges does not appear
12448 in DW_TAG_compile_unit of DWO files. */
12449 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12450 unsigned int ranges_offset = (DW_UNSND (attr)
12451 + (need_ranges_base
12452 ? cu->ranges_base
12453 : 0));
2e3cf129 12454
af34e669 12455 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12456 .debug_ranges section. */
2e3cf129 12457 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12458 return PC_BOUNDS_INVALID;
43039443 12459 /* Found discontinuous range of addresses. */
3a2b436a 12460 ret = PC_BOUNDS_RANGES;
af34e669 12461 }
e385593e
JK
12462 else
12463 return PC_BOUNDS_NOT_PRESENT;
af34e669 12464 }
c906108c 12465
9373cf26
JK
12466 /* read_partial_die has also the strict LOW < HIGH requirement. */
12467 if (high <= low)
e385593e 12468 return PC_BOUNDS_INVALID;
c906108c
SS
12469
12470 /* When using the GNU linker, .gnu.linkonce. sections are used to
12471 eliminate duplicate copies of functions and vtables and such.
12472 The linker will arbitrarily choose one and discard the others.
12473 The AT_*_pc values for such functions refer to local labels in
12474 these sections. If the section from that file was discarded, the
12475 labels are not in the output, so the relocs get a value of 0.
12476 If this is a discarded function, mark the pc bounds as invalid,
12477 so that GDB will ignore it. */
72dca2f5 12478 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12479 return PC_BOUNDS_INVALID;
c906108c
SS
12480
12481 *lowpc = low;
96408a79
SA
12482 if (highpc)
12483 *highpc = high;
af34e669 12484 return ret;
c906108c
SS
12485}
12486
b084d499
JB
12487/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12488 its low and high PC addresses. Do nothing if these addresses could not
12489 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12490 and HIGHPC to the high address if greater than HIGHPC. */
12491
12492static void
12493dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12494 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12495 struct dwarf2_cu *cu)
12496{
12497 CORE_ADDR low, high;
12498 struct die_info *child = die->child;
12499
e385593e 12500 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12501 {
325fac50
PA
12502 *lowpc = std::min (*lowpc, low);
12503 *highpc = std::max (*highpc, high);
b084d499
JB
12504 }
12505
12506 /* If the language does not allow nested subprograms (either inside
12507 subprograms or lexical blocks), we're done. */
12508 if (cu->language != language_ada)
12509 return;
6e70227d 12510
b084d499
JB
12511 /* Check all the children of the given DIE. If it contains nested
12512 subprograms, then check their pc bounds. Likewise, we need to
12513 check lexical blocks as well, as they may also contain subprogram
12514 definitions. */
12515 while (child && child->tag)
12516 {
12517 if (child->tag == DW_TAG_subprogram
12518 || child->tag == DW_TAG_lexical_block)
12519 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12520 child = sibling_die (child);
12521 }
12522}
12523
fae299cd
DC
12524/* Get the low and high pc's represented by the scope DIE, and store
12525 them in *LOWPC and *HIGHPC. If the correct values can't be
12526 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12527
12528static void
12529get_scope_pc_bounds (struct die_info *die,
12530 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12531 struct dwarf2_cu *cu)
12532{
12533 CORE_ADDR best_low = (CORE_ADDR) -1;
12534 CORE_ADDR best_high = (CORE_ADDR) 0;
12535 CORE_ADDR current_low, current_high;
12536
3a2b436a 12537 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12538 >= PC_BOUNDS_RANGES)
fae299cd
DC
12539 {
12540 best_low = current_low;
12541 best_high = current_high;
12542 }
12543 else
12544 {
12545 struct die_info *child = die->child;
12546
12547 while (child && child->tag)
12548 {
12549 switch (child->tag) {
12550 case DW_TAG_subprogram:
b084d499 12551 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12552 break;
12553 case DW_TAG_namespace:
f55ee35c 12554 case DW_TAG_module:
fae299cd
DC
12555 /* FIXME: carlton/2004-01-16: Should we do this for
12556 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12557 that current GCC's always emit the DIEs corresponding
12558 to definitions of methods of classes as children of a
12559 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12560 the DIEs giving the declarations, which could be
12561 anywhere). But I don't see any reason why the
12562 standards says that they have to be there. */
12563 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12564
12565 if (current_low != ((CORE_ADDR) -1))
12566 {
325fac50
PA
12567 best_low = std::min (best_low, current_low);
12568 best_high = std::max (best_high, current_high);
fae299cd
DC
12569 }
12570 break;
12571 default:
0963b4bd 12572 /* Ignore. */
fae299cd
DC
12573 break;
12574 }
12575
12576 child = sibling_die (child);
12577 }
12578 }
12579
12580 *lowpc = best_low;
12581 *highpc = best_high;
12582}
12583
801e3a5b
JB
12584/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12585 in DIE. */
380bca97 12586
801e3a5b
JB
12587static void
12588dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12589 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12590{
bb5ed363 12591 struct objfile *objfile = cu->objfile;
3e29f34a 12592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12593 struct attribute *attr;
91da1414 12594 struct attribute *attr_high;
801e3a5b 12595
91da1414
MW
12596 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12597 if (attr_high)
801e3a5b 12598 {
801e3a5b
JB
12599 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12600 if (attr)
12601 {
31aa7e4e
JB
12602 CORE_ADDR low = attr_value_as_address (attr);
12603 CORE_ADDR high = attr_value_as_address (attr_high);
12604
12605 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12606 high += low;
9a619af0 12607
3e29f34a
MR
12608 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12609 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12610 record_block_range (block, low, high - 1);
801e3a5b
JB
12611 }
12612 }
12613
12614 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12615 if (attr)
12616 {
bb5ed363 12617 bfd *obfd = objfile->obfd;
ab435259
DE
12618 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12619 We take advantage of the fact that DW_AT_ranges does not appear
12620 in DW_TAG_compile_unit of DWO files. */
12621 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12622
12623 /* The value of the DW_AT_ranges attribute is the offset of the
12624 address range list in the .debug_ranges section. */
ab435259
DE
12625 unsigned long offset = (DW_UNSND (attr)
12626 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12627 const gdb_byte *buffer;
801e3a5b
JB
12628
12629 /* For some target architectures, but not others, the
12630 read_address function sign-extends the addresses it returns.
12631 To recognize base address selection entries, we need a
12632 mask. */
12633 unsigned int addr_size = cu->header.addr_size;
12634 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12635
12636 /* The base address, to which the next pair is relative. Note
12637 that this 'base' is a DWARF concept: most entries in a range
12638 list are relative, to reduce the number of relocs against the
12639 debugging information. This is separate from this function's
12640 'baseaddr' argument, which GDB uses to relocate debugging
12641 information from a shared library based on the address at
12642 which the library was loaded. */
d00adf39
DE
12643 CORE_ADDR base = cu->base_address;
12644 int base_known = cu->base_known;
801e3a5b 12645
5f46c5a5
JK
12646 dwarf2_ranges_process (offset, cu,
12647 [&] (CORE_ADDR start, CORE_ADDR end)
12648 {
58fdfd2c
JK
12649 start += baseaddr;
12650 end += baseaddr;
5f46c5a5
JK
12651 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12652 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12653 record_block_range (block, start, end - 1);
12654 });
801e3a5b
JB
12655 }
12656}
12657
685b1105
JK
12658/* Check whether the producer field indicates either of GCC < 4.6, or the
12659 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12660
685b1105
JK
12661static void
12662check_producer (struct dwarf2_cu *cu)
60d5a603 12663{
38360086 12664 int major, minor;
60d5a603
JK
12665
12666 if (cu->producer == NULL)
12667 {
12668 /* For unknown compilers expect their behavior is DWARF version
12669 compliant.
12670
12671 GCC started to support .debug_types sections by -gdwarf-4 since
12672 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12673 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12674 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12675 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12676 }
b1ffba5a 12677 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12678 {
38360086
MW
12679 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12680 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12681 }
61012eef 12682 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12683 cu->producer_is_icc = 1;
12684 else
12685 {
12686 /* For other non-GCC compilers, expect their behavior is DWARF version
12687 compliant. */
60d5a603
JK
12688 }
12689
ba919b58 12690 cu->checked_producer = 1;
685b1105 12691}
ba919b58 12692
685b1105
JK
12693/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12694 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12695 during 4.6.0 experimental. */
12696
12697static int
12698producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12699{
12700 if (!cu->checked_producer)
12701 check_producer (cu);
12702
12703 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12704}
12705
12706/* Return the default accessibility type if it is not overriden by
12707 DW_AT_accessibility. */
12708
12709static enum dwarf_access_attribute
12710dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12711{
12712 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12713 {
12714 /* The default DWARF 2 accessibility for members is public, the default
12715 accessibility for inheritance is private. */
12716
12717 if (die->tag != DW_TAG_inheritance)
12718 return DW_ACCESS_public;
12719 else
12720 return DW_ACCESS_private;
12721 }
12722 else
12723 {
12724 /* DWARF 3+ defines the default accessibility a different way. The same
12725 rules apply now for DW_TAG_inheritance as for the members and it only
12726 depends on the container kind. */
12727
12728 if (die->parent->tag == DW_TAG_class_type)
12729 return DW_ACCESS_private;
12730 else
12731 return DW_ACCESS_public;
12732 }
12733}
12734
74ac6d43
TT
12735/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12736 offset. If the attribute was not found return 0, otherwise return
12737 1. If it was found but could not properly be handled, set *OFFSET
12738 to 0. */
12739
12740static int
12741handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12742 LONGEST *offset)
12743{
12744 struct attribute *attr;
12745
12746 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12747 if (attr != NULL)
12748 {
12749 *offset = 0;
12750
12751 /* Note that we do not check for a section offset first here.
12752 This is because DW_AT_data_member_location is new in DWARF 4,
12753 so if we see it, we can assume that a constant form is really
12754 a constant and not a section offset. */
12755 if (attr_form_is_constant (attr))
12756 *offset = dwarf2_get_attr_constant_value (attr, 0);
12757 else if (attr_form_is_section_offset (attr))
12758 dwarf2_complex_location_expr_complaint ();
12759 else if (attr_form_is_block (attr))
12760 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12761 else
12762 dwarf2_complex_location_expr_complaint ();
12763
12764 return 1;
12765 }
12766
12767 return 0;
12768}
12769
c906108c
SS
12770/* Add an aggregate field to the field list. */
12771
12772static void
107d2387 12773dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12774 struct dwarf2_cu *cu)
6e70227d 12775{
e7c27a73 12776 struct objfile *objfile = cu->objfile;
5e2b427d 12777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12778 struct nextfield *new_field;
12779 struct attribute *attr;
12780 struct field *fp;
15d034d0 12781 const char *fieldname = "";
c906108c
SS
12782
12783 /* Allocate a new field list entry and link it in. */
8d749320 12784 new_field = XNEW (struct nextfield);
b8c9b27d 12785 make_cleanup (xfree, new_field);
c906108c 12786 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12787
12788 if (die->tag == DW_TAG_inheritance)
12789 {
12790 new_field->next = fip->baseclasses;
12791 fip->baseclasses = new_field;
12792 }
12793 else
12794 {
12795 new_field->next = fip->fields;
12796 fip->fields = new_field;
12797 }
c906108c
SS
12798 fip->nfields++;
12799
e142c38c 12800 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12801 if (attr)
12802 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12803 else
12804 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12805 if (new_field->accessibility != DW_ACCESS_public)
12806 fip->non_public_fields = 1;
60d5a603 12807
e142c38c 12808 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12809 if (attr)
12810 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12811 else
12812 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12813
12814 fp = &new_field->field;
a9a9bd0f 12815
e142c38c 12816 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12817 {
74ac6d43
TT
12818 LONGEST offset;
12819
a9a9bd0f 12820 /* Data member other than a C++ static data member. */
6e70227d 12821
c906108c 12822 /* Get type of field. */
e7c27a73 12823 fp->type = die_type (die, cu);
c906108c 12824
d6a843b5 12825 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12826
c906108c 12827 /* Get bit size of field (zero if none). */
e142c38c 12828 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12829 if (attr)
12830 {
12831 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12832 }
12833 else
12834 {
12835 FIELD_BITSIZE (*fp) = 0;
12836 }
12837
12838 /* Get bit offset of field. */
74ac6d43
TT
12839 if (handle_data_member_location (die, cu, &offset))
12840 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12841 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12842 if (attr)
12843 {
5e2b427d 12844 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12845 {
12846 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12847 additional bit offset from the MSB of the containing
12848 anonymous object to the MSB of the field. We don't
12849 have to do anything special since we don't need to
12850 know the size of the anonymous object. */
f41f5e61 12851 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12852 }
12853 else
12854 {
12855 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12856 MSB of the anonymous object, subtract off the number of
12857 bits from the MSB of the field to the MSB of the
12858 object, and then subtract off the number of bits of
12859 the field itself. The result is the bit offset of
12860 the LSB of the field. */
c906108c
SS
12861 int anonymous_size;
12862 int bit_offset = DW_UNSND (attr);
12863
e142c38c 12864 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12865 if (attr)
12866 {
12867 /* The size of the anonymous object containing
12868 the bit field is explicit, so use the
12869 indicated size (in bytes). */
12870 anonymous_size = DW_UNSND (attr);
12871 }
12872 else
12873 {
12874 /* The size of the anonymous object containing
12875 the bit field must be inferred from the type
12876 attribute of the data member containing the
12877 bit field. */
12878 anonymous_size = TYPE_LENGTH (fp->type);
12879 }
f41f5e61
PA
12880 SET_FIELD_BITPOS (*fp,
12881 (FIELD_BITPOS (*fp)
12882 + anonymous_size * bits_per_byte
12883 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12884 }
12885 }
da5b30da
AA
12886 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12887 if (attr != NULL)
12888 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12889 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12890
12891 /* Get name of field. */
39cbfefa
DJ
12892 fieldname = dwarf2_name (die, cu);
12893 if (fieldname == NULL)
12894 fieldname = "";
d8151005
DJ
12895
12896 /* The name is already allocated along with this objfile, so we don't
12897 need to duplicate it for the type. */
12898 fp->name = fieldname;
c906108c
SS
12899
12900 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12901 pointer or virtual base class pointer) to private. */
e142c38c 12902 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12903 {
d48cc9dd 12904 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12905 new_field->accessibility = DW_ACCESS_private;
12906 fip->non_public_fields = 1;
12907 }
12908 }
a9a9bd0f 12909 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12910 {
a9a9bd0f
DC
12911 /* C++ static member. */
12912
12913 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12914 is a declaration, but all versions of G++ as of this writing
12915 (so through at least 3.2.1) incorrectly generate
12916 DW_TAG_variable tags. */
6e70227d 12917
ff355380 12918 const char *physname;
c906108c 12919
a9a9bd0f 12920 /* Get name of field. */
39cbfefa
DJ
12921 fieldname = dwarf2_name (die, cu);
12922 if (fieldname == NULL)
c906108c
SS
12923 return;
12924
254e6b9e 12925 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12926 if (attr
12927 /* Only create a symbol if this is an external value.
12928 new_symbol checks this and puts the value in the global symbol
12929 table, which we want. If it is not external, new_symbol
12930 will try to put the value in cu->list_in_scope which is wrong. */
12931 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12932 {
12933 /* A static const member, not much different than an enum as far as
12934 we're concerned, except that we can support more types. */
12935 new_symbol (die, NULL, cu);
12936 }
12937
2df3850c 12938 /* Get physical name. */
ff355380 12939 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12940
d8151005
DJ
12941 /* The name is already allocated along with this objfile, so we don't
12942 need to duplicate it for the type. */
12943 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12944 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12945 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12946 }
12947 else if (die->tag == DW_TAG_inheritance)
12948 {
74ac6d43 12949 LONGEST offset;
d4b96c9a 12950
74ac6d43
TT
12951 /* C++ base class field. */
12952 if (handle_data_member_location (die, cu, &offset))
12953 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12954 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12955 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12956 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12957 fip->nbaseclasses++;
12958 }
12959}
12960
98751a41
JK
12961/* Add a typedef defined in the scope of the FIP's class. */
12962
12963static void
12964dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12965 struct dwarf2_cu *cu)
6e70227d 12966{
98751a41 12967 struct typedef_field_list *new_field;
98751a41 12968 struct typedef_field *fp;
98751a41
JK
12969
12970 /* Allocate a new field list entry and link it in. */
8d749320 12971 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12972 make_cleanup (xfree, new_field);
12973
12974 gdb_assert (die->tag == DW_TAG_typedef);
12975
12976 fp = &new_field->field;
12977
12978 /* Get name of field. */
12979 fp->name = dwarf2_name (die, cu);
12980 if (fp->name == NULL)
12981 return;
12982
12983 fp->type = read_type_die (die, cu);
12984
12985 new_field->next = fip->typedef_field_list;
12986 fip->typedef_field_list = new_field;
12987 fip->typedef_field_list_count++;
12988}
12989
c906108c
SS
12990/* Create the vector of fields, and attach it to the type. */
12991
12992static void
fba45db2 12993dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12994 struct dwarf2_cu *cu)
c906108c
SS
12995{
12996 int nfields = fip->nfields;
12997
12998 /* Record the field count, allocate space for the array of fields,
12999 and create blank accessibility bitfields if necessary. */
13000 TYPE_NFIELDS (type) = nfields;
13001 TYPE_FIELDS (type) = (struct field *)
13002 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13003 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13004
b4ba55a1 13005 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13006 {
13007 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13008
13009 TYPE_FIELD_PRIVATE_BITS (type) =
13010 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13011 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13012
13013 TYPE_FIELD_PROTECTED_BITS (type) =
13014 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13015 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13016
774b6a14
TT
13017 TYPE_FIELD_IGNORE_BITS (type) =
13018 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13019 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13020 }
13021
13022 /* If the type has baseclasses, allocate and clear a bit vector for
13023 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13024 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13025 {
13026 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13027 unsigned char *pointer;
c906108c
SS
13028
13029 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13030 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13031 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13032 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13033 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13034 }
13035
3e43a32a
MS
13036 /* Copy the saved-up fields into the field vector. Start from the head of
13037 the list, adding to the tail of the field array, so that they end up in
13038 the same order in the array in which they were added to the list. */
c906108c
SS
13039 while (nfields-- > 0)
13040 {
7d0ccb61
DJ
13041 struct nextfield *fieldp;
13042
13043 if (fip->fields)
13044 {
13045 fieldp = fip->fields;
13046 fip->fields = fieldp->next;
13047 }
13048 else
13049 {
13050 fieldp = fip->baseclasses;
13051 fip->baseclasses = fieldp->next;
13052 }
13053
13054 TYPE_FIELD (type, nfields) = fieldp->field;
13055 switch (fieldp->accessibility)
c906108c 13056 {
c5aa993b 13057 case DW_ACCESS_private:
b4ba55a1
JB
13058 if (cu->language != language_ada)
13059 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13060 break;
c906108c 13061
c5aa993b 13062 case DW_ACCESS_protected:
b4ba55a1
JB
13063 if (cu->language != language_ada)
13064 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13065 break;
c906108c 13066
c5aa993b
JM
13067 case DW_ACCESS_public:
13068 break;
c906108c 13069
c5aa993b
JM
13070 default:
13071 /* Unknown accessibility. Complain and treat it as public. */
13072 {
e2e0b3e5 13073 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13074 fieldp->accessibility);
c5aa993b
JM
13075 }
13076 break;
c906108c
SS
13077 }
13078 if (nfields < fip->nbaseclasses)
13079 {
7d0ccb61 13080 switch (fieldp->virtuality)
c906108c 13081 {
c5aa993b
JM
13082 case DW_VIRTUALITY_virtual:
13083 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13084 if (cu->language == language_ada)
a73c6dcd 13085 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13086 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13087 break;
c906108c
SS
13088 }
13089 }
c906108c
SS
13090 }
13091}
13092
7d27a96d
TT
13093/* Return true if this member function is a constructor, false
13094 otherwise. */
13095
13096static int
13097dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13098{
13099 const char *fieldname;
fe978cb0 13100 const char *type_name;
7d27a96d
TT
13101 int len;
13102
13103 if (die->parent == NULL)
13104 return 0;
13105
13106 if (die->parent->tag != DW_TAG_structure_type
13107 && die->parent->tag != DW_TAG_union_type
13108 && die->parent->tag != DW_TAG_class_type)
13109 return 0;
13110
13111 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13112 type_name = dwarf2_name (die->parent, cu);
13113 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13114 return 0;
13115
13116 len = strlen (fieldname);
fe978cb0
PA
13117 return (strncmp (fieldname, type_name, len) == 0
13118 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13119}
13120
c906108c
SS
13121/* Add a member function to the proper fieldlist. */
13122
13123static void
107d2387 13124dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13125 struct type *type, struct dwarf2_cu *cu)
c906108c 13126{
e7c27a73 13127 struct objfile *objfile = cu->objfile;
c906108c
SS
13128 struct attribute *attr;
13129 struct fnfieldlist *flp;
13130 int i;
13131 struct fn_field *fnp;
15d034d0 13132 const char *fieldname;
c906108c 13133 struct nextfnfield *new_fnfield;
f792889a 13134 struct type *this_type;
60d5a603 13135 enum dwarf_access_attribute accessibility;
c906108c 13136
b4ba55a1 13137 if (cu->language == language_ada)
a73c6dcd 13138 error (_("unexpected member function in Ada type"));
b4ba55a1 13139
2df3850c 13140 /* Get name of member function. */
39cbfefa
DJ
13141 fieldname = dwarf2_name (die, cu);
13142 if (fieldname == NULL)
2df3850c 13143 return;
c906108c 13144
c906108c
SS
13145 /* Look up member function name in fieldlist. */
13146 for (i = 0; i < fip->nfnfields; i++)
13147 {
27bfe10e 13148 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13149 break;
13150 }
13151
13152 /* Create new list element if necessary. */
13153 if (i < fip->nfnfields)
13154 flp = &fip->fnfieldlists[i];
13155 else
13156 {
13157 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13158 {
13159 fip->fnfieldlists = (struct fnfieldlist *)
13160 xrealloc (fip->fnfieldlists,
13161 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13162 * sizeof (struct fnfieldlist));
c906108c 13163 if (fip->nfnfields == 0)
c13c43fd 13164 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13165 }
13166 flp = &fip->fnfieldlists[fip->nfnfields];
13167 flp->name = fieldname;
13168 flp->length = 0;
13169 flp->head = NULL;
3da10d80 13170 i = fip->nfnfields++;
c906108c
SS
13171 }
13172
13173 /* Create a new member function field and chain it to the field list
0963b4bd 13174 entry. */
8d749320 13175 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13176 make_cleanup (xfree, new_fnfield);
c906108c
SS
13177 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13178 new_fnfield->next = flp->head;
13179 flp->head = new_fnfield;
13180 flp->length++;
13181
13182 /* Fill in the member function field info. */
13183 fnp = &new_fnfield->fnfield;
3da10d80
KS
13184
13185 /* Delay processing of the physname until later. */
9c37b5ae 13186 if (cu->language == language_cplus)
3da10d80
KS
13187 {
13188 add_to_method_list (type, i, flp->length - 1, fieldname,
13189 die, cu);
13190 }
13191 else
13192 {
1d06ead6 13193 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13194 fnp->physname = physname ? physname : "";
13195 }
13196
c906108c 13197 fnp->type = alloc_type (objfile);
f792889a
DJ
13198 this_type = read_type_die (die, cu);
13199 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13200 {
f792889a 13201 int nparams = TYPE_NFIELDS (this_type);
c906108c 13202
f792889a 13203 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13204 of the method itself (TYPE_CODE_METHOD). */
13205 smash_to_method_type (fnp->type, type,
f792889a
DJ
13206 TYPE_TARGET_TYPE (this_type),
13207 TYPE_FIELDS (this_type),
13208 TYPE_NFIELDS (this_type),
13209 TYPE_VARARGS (this_type));
c906108c
SS
13210
13211 /* Handle static member functions.
c5aa993b 13212 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13213 member functions. G++ helps GDB by marking the first
13214 parameter for non-static member functions (which is the this
13215 pointer) as artificial. We obtain this information from
13216 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13217 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13218 fnp->voffset = VOFFSET_STATIC;
13219 }
13220 else
e2e0b3e5 13221 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13222 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13223
13224 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13225 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13226 fnp->fcontext = die_containing_type (die, cu);
c906108c 13227
3e43a32a
MS
13228 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13229 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13230
13231 /* Get accessibility. */
e142c38c 13232 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13233 if (attr)
aead7601 13234 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13235 else
13236 accessibility = dwarf2_default_access_attribute (die, cu);
13237 switch (accessibility)
c906108c 13238 {
60d5a603
JK
13239 case DW_ACCESS_private:
13240 fnp->is_private = 1;
13241 break;
13242 case DW_ACCESS_protected:
13243 fnp->is_protected = 1;
13244 break;
c906108c
SS
13245 }
13246
b02dede2 13247 /* Check for artificial methods. */
e142c38c 13248 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13249 if (attr && DW_UNSND (attr) != 0)
13250 fnp->is_artificial = 1;
13251
7d27a96d
TT
13252 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13253
0d564a31 13254 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13255 function. For older versions of GCC, this is an offset in the
13256 appropriate virtual table, as specified by DW_AT_containing_type.
13257 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13258 to the object address. */
13259
e142c38c 13260 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13261 if (attr)
8e19ed76 13262 {
aec5aa8b 13263 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13264 {
aec5aa8b
TT
13265 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13266 {
13267 /* Old-style GCC. */
13268 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13269 }
13270 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13271 || (DW_BLOCK (attr)->size > 1
13272 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13273 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13274 {
aec5aa8b
TT
13275 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13276 if ((fnp->voffset % cu->header.addr_size) != 0)
13277 dwarf2_complex_location_expr_complaint ();
13278 else
13279 fnp->voffset /= cu->header.addr_size;
13280 fnp->voffset += 2;
13281 }
13282 else
13283 dwarf2_complex_location_expr_complaint ();
13284
13285 if (!fnp->fcontext)
7e993ebf
KS
13286 {
13287 /* If there is no `this' field and no DW_AT_containing_type,
13288 we cannot actually find a base class context for the
13289 vtable! */
13290 if (TYPE_NFIELDS (this_type) == 0
13291 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13292 {
13293 complaint (&symfile_complaints,
13294 _("cannot determine context for virtual member "
13295 "function \"%s\" (offset %d)"),
9c541725 13296 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13297 }
13298 else
13299 {
13300 fnp->fcontext
13301 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13302 }
13303 }
aec5aa8b 13304 }
3690dd37 13305 else if (attr_form_is_section_offset (attr))
8e19ed76 13306 {
4d3c2250 13307 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13308 }
13309 else
13310 {
4d3c2250
KB
13311 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13312 fieldname);
8e19ed76 13313 }
0d564a31 13314 }
d48cc9dd
DJ
13315 else
13316 {
13317 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13318 if (attr && DW_UNSND (attr))
13319 {
13320 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13321 complaint (&symfile_complaints,
3e43a32a
MS
13322 _("Member function \"%s\" (offset %d) is virtual "
13323 "but the vtable offset is not specified"),
9c541725 13324 fieldname, to_underlying (die->sect_off));
9655fd1a 13325 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13326 TYPE_CPLUS_DYNAMIC (type) = 1;
13327 }
13328 }
c906108c
SS
13329}
13330
13331/* Create the vector of member function fields, and attach it to the type. */
13332
13333static void
fba45db2 13334dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13335 struct dwarf2_cu *cu)
c906108c
SS
13336{
13337 struct fnfieldlist *flp;
c906108c
SS
13338 int i;
13339
b4ba55a1 13340 if (cu->language == language_ada)
a73c6dcd 13341 error (_("unexpected member functions in Ada type"));
b4ba55a1 13342
c906108c
SS
13343 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13344 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13345 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13346
13347 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13348 {
13349 struct nextfnfield *nfp = flp->head;
13350 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13351 int k;
13352
13353 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13354 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13355 fn_flp->fn_fields = (struct fn_field *)
13356 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13357 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13358 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13359 }
13360
13361 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13362}
13363
1168df01
JB
13364/* Returns non-zero if NAME is the name of a vtable member in CU's
13365 language, zero otherwise. */
13366static int
13367is_vtable_name (const char *name, struct dwarf2_cu *cu)
13368{
13369 static const char vptr[] = "_vptr";
987504bb 13370 static const char vtable[] = "vtable";
1168df01 13371
9c37b5ae
TT
13372 /* Look for the C++ form of the vtable. */
13373 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13374 return 1;
13375
13376 return 0;
13377}
13378
c0dd20ea 13379/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13380 functions, with the ABI-specified layout. If TYPE describes
13381 such a structure, smash it into a member function type.
61049d3b
DJ
13382
13383 GCC shouldn't do this; it should just output pointer to member DIEs.
13384 This is GCC PR debug/28767. */
c0dd20ea 13385
0b92b5bb
TT
13386static void
13387quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13388{
09e2d7c7 13389 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13390
13391 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13392 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13393 return;
c0dd20ea
DJ
13394
13395 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13396 if (TYPE_FIELD_NAME (type, 0) == NULL
13397 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13398 || TYPE_FIELD_NAME (type, 1) == NULL
13399 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13400 return;
c0dd20ea
DJ
13401
13402 /* Find the type of the method. */
0b92b5bb 13403 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13404 if (pfn_type == NULL
13405 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13406 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13407 return;
c0dd20ea
DJ
13408
13409 /* Look for the "this" argument. */
13410 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13411 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13412 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13413 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13414 return;
c0dd20ea 13415
09e2d7c7 13416 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13417 new_type = alloc_type (objfile);
09e2d7c7 13418 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13419 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13420 TYPE_VARARGS (pfn_type));
0b92b5bb 13421 smash_to_methodptr_type (type, new_type);
c0dd20ea 13422}
1168df01 13423
685b1105
JK
13424/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13425 (icc). */
13426
13427static int
13428producer_is_icc (struct dwarf2_cu *cu)
13429{
13430 if (!cu->checked_producer)
13431 check_producer (cu);
13432
13433 return cu->producer_is_icc;
13434}
13435
c906108c 13436/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13437 (definition) to create a type for the structure or union. Fill in
13438 the type's name and general properties; the members will not be
83655187
DE
13439 processed until process_structure_scope. A symbol table entry for
13440 the type will also not be done until process_structure_scope (assuming
13441 the type has a name).
c906108c 13442
c767944b
DJ
13443 NOTE: we need to call these functions regardless of whether or not the
13444 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13445 structure or union. This gets the type entered into our set of
83655187 13446 user defined types. */
c906108c 13447
f792889a 13448static struct type *
134d01f1 13449read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13450{
e7c27a73 13451 struct objfile *objfile = cu->objfile;
c906108c
SS
13452 struct type *type;
13453 struct attribute *attr;
15d034d0 13454 const char *name;
c906108c 13455
348e048f
DE
13456 /* If the definition of this type lives in .debug_types, read that type.
13457 Don't follow DW_AT_specification though, that will take us back up
13458 the chain and we want to go down. */
45e58e77 13459 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13460 if (attr)
13461 {
ac9ec31b 13462 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13463
ac9ec31b 13464 /* The type's CU may not be the same as CU.
02142a6c 13465 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13466 return set_die_type (die, type, cu);
13467 }
13468
c0dd20ea 13469 type = alloc_type (objfile);
c906108c 13470 INIT_CPLUS_SPECIFIC (type);
93311388 13471
39cbfefa
DJ
13472 name = dwarf2_name (die, cu);
13473 if (name != NULL)
c906108c 13474 {
987504bb 13475 if (cu->language == language_cplus
c44af4eb
TT
13476 || cu->language == language_d
13477 || cu->language == language_rust)
63d06c5c 13478 {
15d034d0 13479 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13480
13481 /* dwarf2_full_name might have already finished building the DIE's
13482 type. If so, there is no need to continue. */
13483 if (get_die_type (die, cu) != NULL)
13484 return get_die_type (die, cu);
13485
13486 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13487 if (die->tag == DW_TAG_structure_type
13488 || die->tag == DW_TAG_class_type)
13489 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13490 }
13491 else
13492 {
d8151005
DJ
13493 /* The name is already allocated along with this objfile, so
13494 we don't need to duplicate it for the type. */
7d455152 13495 TYPE_TAG_NAME (type) = name;
94af9270
KS
13496 if (die->tag == DW_TAG_class_type)
13497 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13498 }
c906108c
SS
13499 }
13500
13501 if (die->tag == DW_TAG_structure_type)
13502 {
13503 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13504 }
13505 else if (die->tag == DW_TAG_union_type)
13506 {
13507 TYPE_CODE (type) = TYPE_CODE_UNION;
13508 }
13509 else
13510 {
4753d33b 13511 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13512 }
13513
0cc2414c
TT
13514 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13515 TYPE_DECLARED_CLASS (type) = 1;
13516
e142c38c 13517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13518 if (attr)
13519 {
155bfbd3
JB
13520 if (attr_form_is_constant (attr))
13521 TYPE_LENGTH (type) = DW_UNSND (attr);
13522 else
13523 {
13524 /* For the moment, dynamic type sizes are not supported
13525 by GDB's struct type. The actual size is determined
13526 on-demand when resolving the type of a given object,
13527 so set the type's length to zero for now. Otherwise,
13528 we record an expression as the length, and that expression
13529 could lead to a very large value, which could eventually
13530 lead to us trying to allocate that much memory when creating
13531 a value of that type. */
13532 TYPE_LENGTH (type) = 0;
13533 }
c906108c
SS
13534 }
13535 else
13536 {
13537 TYPE_LENGTH (type) = 0;
13538 }
13539
422b1cb0 13540 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13541 {
13542 /* ICC does not output the required DW_AT_declaration
13543 on incomplete types, but gives them a size of zero. */
422b1cb0 13544 TYPE_STUB (type) = 1;
685b1105
JK
13545 }
13546 else
13547 TYPE_STUB_SUPPORTED (type) = 1;
13548
dc718098 13549 if (die_is_declaration (die, cu))
876cecd0 13550 TYPE_STUB (type) = 1;
a6c727b2
DJ
13551 else if (attr == NULL && die->child == NULL
13552 && producer_is_realview (cu->producer))
13553 /* RealView does not output the required DW_AT_declaration
13554 on incomplete types. */
13555 TYPE_STUB (type) = 1;
dc718098 13556
c906108c
SS
13557 /* We need to add the type field to the die immediately so we don't
13558 infinitely recurse when dealing with pointers to the structure
0963b4bd 13559 type within the structure itself. */
1c379e20 13560 set_die_type (die, type, cu);
c906108c 13561
7e314c57
JK
13562 /* set_die_type should be already done. */
13563 set_descriptive_type (type, die, cu);
13564
c767944b
DJ
13565 return type;
13566}
13567
13568/* Finish creating a structure or union type, including filling in
13569 its members and creating a symbol for it. */
13570
13571static void
13572process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13573{
13574 struct objfile *objfile = cu->objfile;
ca040673 13575 struct die_info *child_die;
c767944b
DJ
13576 struct type *type;
13577
13578 type = get_die_type (die, cu);
13579 if (type == NULL)
13580 type = read_structure_type (die, cu);
13581
e142c38c 13582 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13583 {
13584 struct field_info fi;
34eaf542 13585 VEC (symbolp) *template_args = NULL;
c767944b 13586 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13587
13588 memset (&fi, 0, sizeof (struct field_info));
13589
639d11d3 13590 child_die = die->child;
c906108c
SS
13591
13592 while (child_die && child_die->tag)
13593 {
a9a9bd0f
DC
13594 if (child_die->tag == DW_TAG_member
13595 || child_die->tag == DW_TAG_variable)
c906108c 13596 {
a9a9bd0f
DC
13597 /* NOTE: carlton/2002-11-05: A C++ static data member
13598 should be a DW_TAG_member that is a declaration, but
13599 all versions of G++ as of this writing (so through at
13600 least 3.2.1) incorrectly generate DW_TAG_variable
13601 tags for them instead. */
e7c27a73 13602 dwarf2_add_field (&fi, child_die, cu);
c906108c 13603 }
8713b1b1 13604 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13605 {
e98c9e7c
TT
13606 /* Rust doesn't have member functions in the C++ sense.
13607 However, it does emit ordinary functions as children
13608 of a struct DIE. */
13609 if (cu->language == language_rust)
13610 read_func_scope (child_die, cu);
13611 else
13612 {
13613 /* C++ member function. */
13614 dwarf2_add_member_fn (&fi, child_die, type, cu);
13615 }
c906108c
SS
13616 }
13617 else if (child_die->tag == DW_TAG_inheritance)
13618 {
13619 /* C++ base class field. */
e7c27a73 13620 dwarf2_add_field (&fi, child_die, cu);
c906108c 13621 }
98751a41
JK
13622 else if (child_die->tag == DW_TAG_typedef)
13623 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13624 else if (child_die->tag == DW_TAG_template_type_param
13625 || child_die->tag == DW_TAG_template_value_param)
13626 {
13627 struct symbol *arg = new_symbol (child_die, NULL, cu);
13628
f1078f66
DJ
13629 if (arg != NULL)
13630 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13631 }
13632
c906108c
SS
13633 child_die = sibling_die (child_die);
13634 }
13635
34eaf542
TT
13636 /* Attach template arguments to type. */
13637 if (! VEC_empty (symbolp, template_args))
13638 {
13639 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13640 TYPE_N_TEMPLATE_ARGUMENTS (type)
13641 = VEC_length (symbolp, template_args);
13642 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13643 = XOBNEWVEC (&objfile->objfile_obstack,
13644 struct symbol *,
13645 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13646 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13647 VEC_address (symbolp, template_args),
13648 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13649 * sizeof (struct symbol *)));
13650 VEC_free (symbolp, template_args);
13651 }
13652
c906108c
SS
13653 /* Attach fields and member functions to the type. */
13654 if (fi.nfields)
e7c27a73 13655 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13656 if (fi.nfnfields)
13657 {
e7c27a73 13658 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13659
c5aa993b 13660 /* Get the type which refers to the base class (possibly this
c906108c 13661 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13662 class from the DW_AT_containing_type attribute. This use of
13663 DW_AT_containing_type is a GNU extension. */
c906108c 13664
e142c38c 13665 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13666 {
e7c27a73 13667 struct type *t = die_containing_type (die, cu);
c906108c 13668
ae6ae975 13669 set_type_vptr_basetype (type, t);
c906108c
SS
13670 if (type == t)
13671 {
c906108c
SS
13672 int i;
13673
13674 /* Our own class provides vtbl ptr. */
13675 for (i = TYPE_NFIELDS (t) - 1;
13676 i >= TYPE_N_BASECLASSES (t);
13677 --i)
13678 {
0d5cff50 13679 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13680
1168df01 13681 if (is_vtable_name (fieldname, cu))
c906108c 13682 {
ae6ae975 13683 set_type_vptr_fieldno (type, i);
c906108c
SS
13684 break;
13685 }
13686 }
13687
13688 /* Complain if virtual function table field not found. */
13689 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13690 complaint (&symfile_complaints,
3e43a32a
MS
13691 _("virtual function table pointer "
13692 "not found when defining class '%s'"),
4d3c2250
KB
13693 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13694 "");
c906108c
SS
13695 }
13696 else
13697 {
ae6ae975 13698 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13699 }
13700 }
f6235d4c 13701 else if (cu->producer
61012eef 13702 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13703 {
13704 /* The IBM XLC compiler does not provide direct indication
13705 of the containing type, but the vtable pointer is
13706 always named __vfp. */
13707
13708 int i;
13709
13710 for (i = TYPE_NFIELDS (type) - 1;
13711 i >= TYPE_N_BASECLASSES (type);
13712 --i)
13713 {
13714 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13715 {
ae6ae975
DE
13716 set_type_vptr_fieldno (type, i);
13717 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13718 break;
13719 }
13720 }
13721 }
c906108c 13722 }
98751a41
JK
13723
13724 /* Copy fi.typedef_field_list linked list elements content into the
13725 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13726 if (fi.typedef_field_list)
13727 {
13728 int i = fi.typedef_field_list_count;
13729
a0d7a4ff 13730 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13731 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13732 = ((struct typedef_field *)
13733 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13734 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13735
13736 /* Reverse the list order to keep the debug info elements order. */
13737 while (--i >= 0)
13738 {
13739 struct typedef_field *dest, *src;
6e70227d 13740
98751a41
JK
13741 dest = &TYPE_TYPEDEF_FIELD (type, i);
13742 src = &fi.typedef_field_list->field;
13743 fi.typedef_field_list = fi.typedef_field_list->next;
13744 *dest = *src;
13745 }
13746 }
c767944b
DJ
13747
13748 do_cleanups (back_to);
c906108c 13749 }
63d06c5c 13750
bb5ed363 13751 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13752
90aeadfc
DC
13753 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13754 snapshots) has been known to create a die giving a declaration
13755 for a class that has, as a child, a die giving a definition for a
13756 nested class. So we have to process our children even if the
13757 current die is a declaration. Normally, of course, a declaration
13758 won't have any children at all. */
134d01f1 13759
ca040673
DE
13760 child_die = die->child;
13761
90aeadfc
DC
13762 while (child_die != NULL && child_die->tag)
13763 {
13764 if (child_die->tag == DW_TAG_member
13765 || child_die->tag == DW_TAG_variable
34eaf542
TT
13766 || child_die->tag == DW_TAG_inheritance
13767 || child_die->tag == DW_TAG_template_value_param
13768 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13769 {
90aeadfc 13770 /* Do nothing. */
134d01f1 13771 }
90aeadfc
DC
13772 else
13773 process_die (child_die, cu);
134d01f1 13774
90aeadfc 13775 child_die = sibling_die (child_die);
134d01f1
DJ
13776 }
13777
fa4028e9
JB
13778 /* Do not consider external references. According to the DWARF standard,
13779 these DIEs are identified by the fact that they have no byte_size
13780 attribute, and a declaration attribute. */
13781 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13782 || !die_is_declaration (die, cu))
c767944b 13783 new_symbol (die, type, cu);
134d01f1
DJ
13784}
13785
55426c9d
JB
13786/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13787 update TYPE using some information only available in DIE's children. */
13788
13789static void
13790update_enumeration_type_from_children (struct die_info *die,
13791 struct type *type,
13792 struct dwarf2_cu *cu)
13793{
60f7655a 13794 struct die_info *child_die;
55426c9d
JB
13795 int unsigned_enum = 1;
13796 int flag_enum = 1;
13797 ULONGEST mask = 0;
55426c9d 13798
8268c778 13799 auto_obstack obstack;
55426c9d 13800
60f7655a
DE
13801 for (child_die = die->child;
13802 child_die != NULL && child_die->tag;
13803 child_die = sibling_die (child_die))
55426c9d
JB
13804 {
13805 struct attribute *attr;
13806 LONGEST value;
13807 const gdb_byte *bytes;
13808 struct dwarf2_locexpr_baton *baton;
13809 const char *name;
60f7655a 13810
55426c9d
JB
13811 if (child_die->tag != DW_TAG_enumerator)
13812 continue;
13813
13814 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13815 if (attr == NULL)
13816 continue;
13817
13818 name = dwarf2_name (child_die, cu);
13819 if (name == NULL)
13820 name = "<anonymous enumerator>";
13821
13822 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13823 &value, &bytes, &baton);
13824 if (value < 0)
13825 {
13826 unsigned_enum = 0;
13827 flag_enum = 0;
13828 }
13829 else if ((mask & value) != 0)
13830 flag_enum = 0;
13831 else
13832 mask |= value;
13833
13834 /* If we already know that the enum type is neither unsigned, nor
13835 a flag type, no need to look at the rest of the enumerates. */
13836 if (!unsigned_enum && !flag_enum)
13837 break;
55426c9d
JB
13838 }
13839
13840 if (unsigned_enum)
13841 TYPE_UNSIGNED (type) = 1;
13842 if (flag_enum)
13843 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
13844}
13845
134d01f1
DJ
13846/* Given a DW_AT_enumeration_type die, set its type. We do not
13847 complete the type's fields yet, or create any symbols. */
c906108c 13848
f792889a 13849static struct type *
134d01f1 13850read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13851{
e7c27a73 13852 struct objfile *objfile = cu->objfile;
c906108c 13853 struct type *type;
c906108c 13854 struct attribute *attr;
0114d602 13855 const char *name;
134d01f1 13856
348e048f
DE
13857 /* If the definition of this type lives in .debug_types, read that type.
13858 Don't follow DW_AT_specification though, that will take us back up
13859 the chain and we want to go down. */
45e58e77 13860 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13861 if (attr)
13862 {
ac9ec31b 13863 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13864
ac9ec31b 13865 /* The type's CU may not be the same as CU.
02142a6c 13866 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13867 return set_die_type (die, type, cu);
13868 }
13869
c906108c
SS
13870 type = alloc_type (objfile);
13871
13872 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13873 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13874 if (name != NULL)
7d455152 13875 TYPE_TAG_NAME (type) = name;
c906108c 13876
0626fc76
TT
13877 attr = dwarf2_attr (die, DW_AT_type, cu);
13878 if (attr != NULL)
13879 {
13880 struct type *underlying_type = die_type (die, cu);
13881
13882 TYPE_TARGET_TYPE (type) = underlying_type;
13883 }
13884
e142c38c 13885 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13886 if (attr)
13887 {
13888 TYPE_LENGTH (type) = DW_UNSND (attr);
13889 }
13890 else
13891 {
13892 TYPE_LENGTH (type) = 0;
13893 }
13894
137033e9
JB
13895 /* The enumeration DIE can be incomplete. In Ada, any type can be
13896 declared as private in the package spec, and then defined only
13897 inside the package body. Such types are known as Taft Amendment
13898 Types. When another package uses such a type, an incomplete DIE
13899 may be generated by the compiler. */
02eb380e 13900 if (die_is_declaration (die, cu))
876cecd0 13901 TYPE_STUB (type) = 1;
02eb380e 13902
0626fc76
TT
13903 /* Finish the creation of this type by using the enum's children.
13904 We must call this even when the underlying type has been provided
13905 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13906 update_enumeration_type_from_children (die, type, cu);
13907
0626fc76
TT
13908 /* If this type has an underlying type that is not a stub, then we
13909 may use its attributes. We always use the "unsigned" attribute
13910 in this situation, because ordinarily we guess whether the type
13911 is unsigned -- but the guess can be wrong and the underlying type
13912 can tell us the reality. However, we defer to a local size
13913 attribute if one exists, because this lets the compiler override
13914 the underlying type if needed. */
13915 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13916 {
13917 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13918 if (TYPE_LENGTH (type) == 0)
13919 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13920 }
13921
3d567982
TT
13922 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13923
f792889a 13924 return set_die_type (die, type, cu);
134d01f1
DJ
13925}
13926
13927/* Given a pointer to a die which begins an enumeration, process all
13928 the dies that define the members of the enumeration, and create the
13929 symbol for the enumeration type.
13930
13931 NOTE: We reverse the order of the element list. */
13932
13933static void
13934process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13935{
f792889a 13936 struct type *this_type;
134d01f1 13937
f792889a
DJ
13938 this_type = get_die_type (die, cu);
13939 if (this_type == NULL)
13940 this_type = read_enumeration_type (die, cu);
9dc481d3 13941
639d11d3 13942 if (die->child != NULL)
c906108c 13943 {
9dc481d3
DE
13944 struct die_info *child_die;
13945 struct symbol *sym;
13946 struct field *fields = NULL;
13947 int num_fields = 0;
15d034d0 13948 const char *name;
9dc481d3 13949
639d11d3 13950 child_die = die->child;
c906108c
SS
13951 while (child_die && child_die->tag)
13952 {
13953 if (child_die->tag != DW_TAG_enumerator)
13954 {
e7c27a73 13955 process_die (child_die, cu);
c906108c
SS
13956 }
13957 else
13958 {
39cbfefa
DJ
13959 name = dwarf2_name (child_die, cu);
13960 if (name)
c906108c 13961 {
f792889a 13962 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13963
13964 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13965 {
13966 fields = (struct field *)
13967 xrealloc (fields,
13968 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13969 * sizeof (struct field));
c906108c
SS
13970 }
13971
3567439c 13972 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13973 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13974 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13975 FIELD_BITSIZE (fields[num_fields]) = 0;
13976
13977 num_fields++;
13978 }
13979 }
13980
13981 child_die = sibling_die (child_die);
13982 }
13983
13984 if (num_fields)
13985 {
f792889a
DJ
13986 TYPE_NFIELDS (this_type) = num_fields;
13987 TYPE_FIELDS (this_type) = (struct field *)
13988 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13989 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13990 sizeof (struct field) * num_fields);
b8c9b27d 13991 xfree (fields);
c906108c 13992 }
c906108c 13993 }
134d01f1 13994
6c83ed52
TT
13995 /* If we are reading an enum from a .debug_types unit, and the enum
13996 is a declaration, and the enum is not the signatured type in the
13997 unit, then we do not want to add a symbol for it. Adding a
13998 symbol would in some cases obscure the true definition of the
13999 enum, giving users an incomplete type when the definition is
14000 actually available. Note that we do not want to do this for all
14001 enums which are just declarations, because C++0x allows forward
14002 enum declarations. */
3019eac3 14003 if (cu->per_cu->is_debug_types
6c83ed52
TT
14004 && die_is_declaration (die, cu))
14005 {
52dc124a 14006 struct signatured_type *sig_type;
6c83ed52 14007
c0f78cd4 14008 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14009 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14010 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14011 return;
14012 }
14013
f792889a 14014 new_symbol (die, this_type, cu);
c906108c
SS
14015}
14016
14017/* Extract all information from a DW_TAG_array_type DIE and put it in
14018 the DIE's type field. For now, this only handles one dimensional
14019 arrays. */
14020
f792889a 14021static struct type *
e7c27a73 14022read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14023{
e7c27a73 14024 struct objfile *objfile = cu->objfile;
c906108c 14025 struct die_info *child_die;
7e314c57 14026 struct type *type;
c906108c
SS
14027 struct type *element_type, *range_type, *index_type;
14028 struct type **range_types = NULL;
14029 struct attribute *attr;
14030 int ndim = 0;
14031 struct cleanup *back_to;
15d034d0 14032 const char *name;
dc53a7ad 14033 unsigned int bit_stride = 0;
c906108c 14034
e7c27a73 14035 element_type = die_type (die, cu);
c906108c 14036
7e314c57
JK
14037 /* The die_type call above may have already set the type for this DIE. */
14038 type = get_die_type (die, cu);
14039 if (type)
14040 return type;
14041
dc53a7ad
JB
14042 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14043 if (attr != NULL)
14044 bit_stride = DW_UNSND (attr) * 8;
14045
14046 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14047 if (attr != NULL)
14048 bit_stride = DW_UNSND (attr);
14049
c906108c
SS
14050 /* Irix 6.2 native cc creates array types without children for
14051 arrays with unspecified length. */
639d11d3 14052 if (die->child == NULL)
c906108c 14053 {
46bf5051 14054 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14055 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14056 type = create_array_type_with_stride (NULL, element_type, range_type,
14057 bit_stride);
f792889a 14058 return set_die_type (die, type, cu);
c906108c
SS
14059 }
14060
14061 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14062 child_die = die->child;
c906108c
SS
14063 while (child_die && child_die->tag)
14064 {
14065 if (child_die->tag == DW_TAG_subrange_type)
14066 {
f792889a 14067 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14068
f792889a 14069 if (child_type != NULL)
a02abb62 14070 {
0963b4bd
MS
14071 /* The range type was succesfully read. Save it for the
14072 array type creation. */
a02abb62
JB
14073 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14074 {
14075 range_types = (struct type **)
14076 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14077 * sizeof (struct type *));
14078 if (ndim == 0)
14079 make_cleanup (free_current_contents, &range_types);
14080 }
f792889a 14081 range_types[ndim++] = child_type;
a02abb62 14082 }
c906108c
SS
14083 }
14084 child_die = sibling_die (child_die);
14085 }
14086
14087 /* Dwarf2 dimensions are output from left to right, create the
14088 necessary array types in backwards order. */
7ca2d3a3 14089
c906108c 14090 type = element_type;
7ca2d3a3
DL
14091
14092 if (read_array_order (die, cu) == DW_ORD_col_major)
14093 {
14094 int i = 0;
9a619af0 14095
7ca2d3a3 14096 while (i < ndim)
dc53a7ad
JB
14097 type = create_array_type_with_stride (NULL, type, range_types[i++],
14098 bit_stride);
7ca2d3a3
DL
14099 }
14100 else
14101 {
14102 while (ndim-- > 0)
dc53a7ad
JB
14103 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14104 bit_stride);
7ca2d3a3 14105 }
c906108c 14106
f5f8a009
EZ
14107 /* Understand Dwarf2 support for vector types (like they occur on
14108 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14109 array type. This is not part of the Dwarf2/3 standard yet, but a
14110 custom vendor extension. The main difference between a regular
14111 array and the vector variant is that vectors are passed by value
14112 to functions. */
e142c38c 14113 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14114 if (attr)
ea37ba09 14115 make_vector_type (type);
f5f8a009 14116
dbc98a8b
KW
14117 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14118 implementation may choose to implement triple vectors using this
14119 attribute. */
14120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14121 if (attr)
14122 {
14123 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14124 TYPE_LENGTH (type) = DW_UNSND (attr);
14125 else
3e43a32a
MS
14126 complaint (&symfile_complaints,
14127 _("DW_AT_byte_size for array type smaller "
14128 "than the total size of elements"));
dbc98a8b
KW
14129 }
14130
39cbfefa
DJ
14131 name = dwarf2_name (die, cu);
14132 if (name)
14133 TYPE_NAME (type) = name;
6e70227d 14134
0963b4bd 14135 /* Install the type in the die. */
7e314c57
JK
14136 set_die_type (die, type, cu);
14137
14138 /* set_die_type should be already done. */
b4ba55a1
JB
14139 set_descriptive_type (type, die, cu);
14140
c906108c
SS
14141 do_cleanups (back_to);
14142
7e314c57 14143 return type;
c906108c
SS
14144}
14145
7ca2d3a3 14146static enum dwarf_array_dim_ordering
6e70227d 14147read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14148{
14149 struct attribute *attr;
14150
14151 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14152
aead7601
SM
14153 if (attr)
14154 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14155
0963b4bd
MS
14156 /* GNU F77 is a special case, as at 08/2004 array type info is the
14157 opposite order to the dwarf2 specification, but data is still
14158 laid out as per normal fortran.
7ca2d3a3 14159
0963b4bd
MS
14160 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14161 version checking. */
7ca2d3a3 14162
905e0470
PM
14163 if (cu->language == language_fortran
14164 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14165 {
14166 return DW_ORD_row_major;
14167 }
14168
6e70227d 14169 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14170 {
14171 case array_column_major:
14172 return DW_ORD_col_major;
14173 case array_row_major:
14174 default:
14175 return DW_ORD_row_major;
14176 };
14177}
14178
72019c9c 14179/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14180 the DIE's type field. */
72019c9c 14181
f792889a 14182static struct type *
72019c9c
GM
14183read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14184{
7e314c57
JK
14185 struct type *domain_type, *set_type;
14186 struct attribute *attr;
f792889a 14187
7e314c57
JK
14188 domain_type = die_type (die, cu);
14189
14190 /* The die_type call above may have already set the type for this DIE. */
14191 set_type = get_die_type (die, cu);
14192 if (set_type)
14193 return set_type;
14194
14195 set_type = create_set_type (NULL, domain_type);
14196
14197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14198 if (attr)
14199 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14200
f792889a 14201 return set_die_type (die, set_type, cu);
72019c9c 14202}
7ca2d3a3 14203
0971de02
TT
14204/* A helper for read_common_block that creates a locexpr baton.
14205 SYM is the symbol which we are marking as computed.
14206 COMMON_DIE is the DIE for the common block.
14207 COMMON_LOC is the location expression attribute for the common
14208 block itself.
14209 MEMBER_LOC is the location expression attribute for the particular
14210 member of the common block that we are processing.
14211 CU is the CU from which the above come. */
14212
14213static void
14214mark_common_block_symbol_computed (struct symbol *sym,
14215 struct die_info *common_die,
14216 struct attribute *common_loc,
14217 struct attribute *member_loc,
14218 struct dwarf2_cu *cu)
14219{
14220 struct objfile *objfile = dwarf2_per_objfile->objfile;
14221 struct dwarf2_locexpr_baton *baton;
14222 gdb_byte *ptr;
14223 unsigned int cu_off;
14224 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14225 LONGEST offset = 0;
14226
14227 gdb_assert (common_loc && member_loc);
14228 gdb_assert (attr_form_is_block (common_loc));
14229 gdb_assert (attr_form_is_block (member_loc)
14230 || attr_form_is_constant (member_loc));
14231
8d749320 14232 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14233 baton->per_cu = cu->per_cu;
14234 gdb_assert (baton->per_cu);
14235
14236 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14237
14238 if (attr_form_is_constant (member_loc))
14239 {
14240 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14241 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14242 }
14243 else
14244 baton->size += DW_BLOCK (member_loc)->size;
14245
224c3ddb 14246 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14247 baton->data = ptr;
14248
14249 *ptr++ = DW_OP_call4;
9c541725 14250 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14251 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14252 ptr += 4;
14253
14254 if (attr_form_is_constant (member_loc))
14255 {
14256 *ptr++ = DW_OP_addr;
14257 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14258 ptr += cu->header.addr_size;
14259 }
14260 else
14261 {
14262 /* We have to copy the data here, because DW_OP_call4 will only
14263 use a DW_AT_location attribute. */
14264 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14265 ptr += DW_BLOCK (member_loc)->size;
14266 }
14267
14268 *ptr++ = DW_OP_plus;
14269 gdb_assert (ptr - baton->data == baton->size);
14270
0971de02 14271 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14272 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14273}
14274
4357ac6c
TT
14275/* Create appropriate locally-scoped variables for all the
14276 DW_TAG_common_block entries. Also create a struct common_block
14277 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14278 is used to sepate the common blocks name namespace from regular
14279 variable names. */
c906108c
SS
14280
14281static void
e7c27a73 14282read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14283{
0971de02
TT
14284 struct attribute *attr;
14285
14286 attr = dwarf2_attr (die, DW_AT_location, cu);
14287 if (attr)
14288 {
14289 /* Support the .debug_loc offsets. */
14290 if (attr_form_is_block (attr))
14291 {
14292 /* Ok. */
14293 }
14294 else if (attr_form_is_section_offset (attr))
14295 {
14296 dwarf2_complex_location_expr_complaint ();
14297 attr = NULL;
14298 }
14299 else
14300 {
14301 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14302 "common block member");
14303 attr = NULL;
14304 }
14305 }
14306
639d11d3 14307 if (die->child != NULL)
c906108c 14308 {
4357ac6c
TT
14309 struct objfile *objfile = cu->objfile;
14310 struct die_info *child_die;
14311 size_t n_entries = 0, size;
14312 struct common_block *common_block;
14313 struct symbol *sym;
74ac6d43 14314
4357ac6c
TT
14315 for (child_die = die->child;
14316 child_die && child_die->tag;
14317 child_die = sibling_die (child_die))
14318 ++n_entries;
14319
14320 size = (sizeof (struct common_block)
14321 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14322 common_block
14323 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14324 size);
4357ac6c
TT
14325 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14326 common_block->n_entries = 0;
14327
14328 for (child_die = die->child;
14329 child_die && child_die->tag;
14330 child_die = sibling_die (child_die))
14331 {
14332 /* Create the symbol in the DW_TAG_common_block block in the current
14333 symbol scope. */
e7c27a73 14334 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14335 if (sym != NULL)
14336 {
14337 struct attribute *member_loc;
14338
14339 common_block->contents[common_block->n_entries++] = sym;
14340
14341 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14342 cu);
14343 if (member_loc)
14344 {
14345 /* GDB has handled this for a long time, but it is
14346 not specified by DWARF. It seems to have been
14347 emitted by gfortran at least as recently as:
14348 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14349 complaint (&symfile_complaints,
14350 _("Variable in common block has "
14351 "DW_AT_data_member_location "
14352 "- DIE at 0x%x [in module %s]"),
9c541725 14353 to_underlying (child_die->sect_off),
4262abfb 14354 objfile_name (cu->objfile));
0971de02
TT
14355
14356 if (attr_form_is_section_offset (member_loc))
14357 dwarf2_complex_location_expr_complaint ();
14358 else if (attr_form_is_constant (member_loc)
14359 || attr_form_is_block (member_loc))
14360 {
14361 if (attr)
14362 mark_common_block_symbol_computed (sym, die, attr,
14363 member_loc, cu);
14364 }
14365 else
14366 dwarf2_complex_location_expr_complaint ();
14367 }
14368 }
c906108c 14369 }
4357ac6c
TT
14370
14371 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14372 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14373 }
14374}
14375
0114d602 14376/* Create a type for a C++ namespace. */
d9fa45fe 14377
0114d602
DJ
14378static struct type *
14379read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14380{
e7c27a73 14381 struct objfile *objfile = cu->objfile;
0114d602 14382 const char *previous_prefix, *name;
9219021c 14383 int is_anonymous;
0114d602
DJ
14384 struct type *type;
14385
14386 /* For extensions, reuse the type of the original namespace. */
14387 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14388 {
14389 struct die_info *ext_die;
14390 struct dwarf2_cu *ext_cu = cu;
9a619af0 14391
0114d602
DJ
14392 ext_die = dwarf2_extension (die, &ext_cu);
14393 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14394
14395 /* EXT_CU may not be the same as CU.
02142a6c 14396 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14397 return set_die_type (die, type, cu);
14398 }
9219021c 14399
e142c38c 14400 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14401
14402 /* Now build the name of the current namespace. */
14403
0114d602
DJ
14404 previous_prefix = determine_prefix (die, cu);
14405 if (previous_prefix[0] != '\0')
14406 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14407 previous_prefix, name, 0, cu);
0114d602
DJ
14408
14409 /* Create the type. */
19f392bc 14410 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14411 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14412
60531b24 14413 return set_die_type (die, type, cu);
0114d602
DJ
14414}
14415
22cee43f 14416/* Read a namespace scope. */
0114d602
DJ
14417
14418static void
14419read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14420{
14421 struct objfile *objfile = cu->objfile;
0114d602 14422 int is_anonymous;
9219021c 14423
5c4e30ca
DC
14424 /* Add a symbol associated to this if we haven't seen the namespace
14425 before. Also, add a using directive if it's an anonymous
14426 namespace. */
9219021c 14427
f2f0e013 14428 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14429 {
14430 struct type *type;
14431
0114d602 14432 type = read_type_die (die, cu);
e7c27a73 14433 new_symbol (die, type, cu);
5c4e30ca 14434
e8e80198 14435 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14436 if (is_anonymous)
0114d602
DJ
14437 {
14438 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14439
22cee43f
PMR
14440 add_using_directive (using_directives (cu->language),
14441 previous_prefix, TYPE_NAME (type), NULL,
14442 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14443 }
5c4e30ca 14444 }
9219021c 14445
639d11d3 14446 if (die->child != NULL)
d9fa45fe 14447 {
639d11d3 14448 struct die_info *child_die = die->child;
6e70227d 14449
d9fa45fe
DC
14450 while (child_die && child_die->tag)
14451 {
e7c27a73 14452 process_die (child_die, cu);
d9fa45fe
DC
14453 child_die = sibling_die (child_die);
14454 }
14455 }
38d518c9
EZ
14456}
14457
f55ee35c
JK
14458/* Read a Fortran module as type. This DIE can be only a declaration used for
14459 imported module. Still we need that type as local Fortran "use ... only"
14460 declaration imports depend on the created type in determine_prefix. */
14461
14462static struct type *
14463read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14464{
14465 struct objfile *objfile = cu->objfile;
15d034d0 14466 const char *module_name;
f55ee35c
JK
14467 struct type *type;
14468
14469 module_name = dwarf2_name (die, cu);
14470 if (!module_name)
3e43a32a
MS
14471 complaint (&symfile_complaints,
14472 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14473 to_underlying (die->sect_off));
19f392bc 14474 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14475
14476 /* determine_prefix uses TYPE_TAG_NAME. */
14477 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14478
14479 return set_die_type (die, type, cu);
14480}
14481
5d7cb8df
JK
14482/* Read a Fortran module. */
14483
14484static void
14485read_module (struct die_info *die, struct dwarf2_cu *cu)
14486{
14487 struct die_info *child_die = die->child;
530e8392
KB
14488 struct type *type;
14489
14490 type = read_type_die (die, cu);
14491 new_symbol (die, type, cu);
5d7cb8df 14492
5d7cb8df
JK
14493 while (child_die && child_die->tag)
14494 {
14495 process_die (child_die, cu);
14496 child_die = sibling_die (child_die);
14497 }
14498}
14499
38d518c9
EZ
14500/* Return the name of the namespace represented by DIE. Set
14501 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14502 namespace. */
14503
14504static const char *
e142c38c 14505namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14506{
14507 struct die_info *current_die;
14508 const char *name = NULL;
14509
14510 /* Loop through the extensions until we find a name. */
14511
14512 for (current_die = die;
14513 current_die != NULL;
f2f0e013 14514 current_die = dwarf2_extension (die, &cu))
38d518c9 14515 {
96553a0c
DE
14516 /* We don't use dwarf2_name here so that we can detect the absence
14517 of a name -> anonymous namespace. */
7d45c7c3 14518 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14519
38d518c9
EZ
14520 if (name != NULL)
14521 break;
14522 }
14523
14524 /* Is it an anonymous namespace? */
14525
14526 *is_anonymous = (name == NULL);
14527 if (*is_anonymous)
2b1dbab0 14528 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14529
14530 return name;
d9fa45fe
DC
14531}
14532
c906108c
SS
14533/* Extract all information from a DW_TAG_pointer_type DIE and add to
14534 the user defined type vector. */
14535
f792889a 14536static struct type *
e7c27a73 14537read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14538{
5e2b427d 14539 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14540 struct comp_unit_head *cu_header = &cu->header;
c906108c 14541 struct type *type;
8b2dbe47
KB
14542 struct attribute *attr_byte_size;
14543 struct attribute *attr_address_class;
14544 int byte_size, addr_class;
7e314c57
JK
14545 struct type *target_type;
14546
14547 target_type = die_type (die, cu);
c906108c 14548
7e314c57
JK
14549 /* The die_type call above may have already set the type for this DIE. */
14550 type = get_die_type (die, cu);
14551 if (type)
14552 return type;
14553
14554 type = lookup_pointer_type (target_type);
8b2dbe47 14555
e142c38c 14556 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14557 if (attr_byte_size)
14558 byte_size = DW_UNSND (attr_byte_size);
c906108c 14559 else
8b2dbe47
KB
14560 byte_size = cu_header->addr_size;
14561
e142c38c 14562 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14563 if (attr_address_class)
14564 addr_class = DW_UNSND (attr_address_class);
14565 else
14566 addr_class = DW_ADDR_none;
14567
14568 /* If the pointer size or address class is different than the
14569 default, create a type variant marked as such and set the
14570 length accordingly. */
14571 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14572 {
5e2b427d 14573 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14574 {
14575 int type_flags;
14576
849957d9 14577 type_flags = gdbarch_address_class_type_flags
5e2b427d 14578 (gdbarch, byte_size, addr_class);
876cecd0
TT
14579 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14580 == 0);
8b2dbe47
KB
14581 type = make_type_with_address_space (type, type_flags);
14582 }
14583 else if (TYPE_LENGTH (type) != byte_size)
14584 {
3e43a32a
MS
14585 complaint (&symfile_complaints,
14586 _("invalid pointer size %d"), byte_size);
8b2dbe47 14587 }
6e70227d 14588 else
9a619af0
MS
14589 {
14590 /* Should we also complain about unhandled address classes? */
14591 }
c906108c 14592 }
8b2dbe47
KB
14593
14594 TYPE_LENGTH (type) = byte_size;
f792889a 14595 return set_die_type (die, type, cu);
c906108c
SS
14596}
14597
14598/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14599 the user defined type vector. */
14600
f792889a 14601static struct type *
e7c27a73 14602read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14603{
14604 struct type *type;
14605 struct type *to_type;
14606 struct type *domain;
14607
e7c27a73
DJ
14608 to_type = die_type (die, cu);
14609 domain = die_containing_type (die, cu);
0d5de010 14610
7e314c57
JK
14611 /* The calls above may have already set the type for this DIE. */
14612 type = get_die_type (die, cu);
14613 if (type)
14614 return type;
14615
0d5de010
DJ
14616 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14617 type = lookup_methodptr_type (to_type);
7078baeb
TT
14618 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14619 {
14620 struct type *new_type = alloc_type (cu->objfile);
14621
14622 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14623 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14624 TYPE_VARARGS (to_type));
14625 type = lookup_methodptr_type (new_type);
14626 }
0d5de010
DJ
14627 else
14628 type = lookup_memberptr_type (to_type, domain);
c906108c 14629
f792889a 14630 return set_die_type (die, type, cu);
c906108c
SS
14631}
14632
4297a3f0 14633/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14634 the user defined type vector. */
14635
f792889a 14636static struct type *
4297a3f0
AV
14637read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14638 enum type_code refcode)
c906108c 14639{
e7c27a73 14640 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14641 struct type *type, *target_type;
c906108c
SS
14642 struct attribute *attr;
14643
4297a3f0
AV
14644 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14645
7e314c57
JK
14646 target_type = die_type (die, cu);
14647
14648 /* The die_type call above may have already set the type for this DIE. */
14649 type = get_die_type (die, cu);
14650 if (type)
14651 return type;
14652
4297a3f0 14653 type = lookup_reference_type (target_type, refcode);
e142c38c 14654 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14655 if (attr)
14656 {
14657 TYPE_LENGTH (type) = DW_UNSND (attr);
14658 }
14659 else
14660 {
107d2387 14661 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14662 }
f792889a 14663 return set_die_type (die, type, cu);
c906108c
SS
14664}
14665
cf363f18
MW
14666/* Add the given cv-qualifiers to the element type of the array. GCC
14667 outputs DWARF type qualifiers that apply to an array, not the
14668 element type. But GDB relies on the array element type to carry
14669 the cv-qualifiers. This mimics section 6.7.3 of the C99
14670 specification. */
14671
14672static struct type *
14673add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14674 struct type *base_type, int cnst, int voltl)
14675{
14676 struct type *el_type, *inner_array;
14677
14678 base_type = copy_type (base_type);
14679 inner_array = base_type;
14680
14681 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14682 {
14683 TYPE_TARGET_TYPE (inner_array) =
14684 copy_type (TYPE_TARGET_TYPE (inner_array));
14685 inner_array = TYPE_TARGET_TYPE (inner_array);
14686 }
14687
14688 el_type = TYPE_TARGET_TYPE (inner_array);
14689 cnst |= TYPE_CONST (el_type);
14690 voltl |= TYPE_VOLATILE (el_type);
14691 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14692
14693 return set_die_type (die, base_type, cu);
14694}
14695
f792889a 14696static struct type *
e7c27a73 14697read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14698{
f792889a 14699 struct type *base_type, *cv_type;
c906108c 14700
e7c27a73 14701 base_type = die_type (die, cu);
7e314c57
JK
14702
14703 /* The die_type call above may have already set the type for this DIE. */
14704 cv_type = get_die_type (die, cu);
14705 if (cv_type)
14706 return cv_type;
14707
2f608a3a
KW
14708 /* In case the const qualifier is applied to an array type, the element type
14709 is so qualified, not the array type (section 6.7.3 of C99). */
14710 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14711 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14712
f792889a
DJ
14713 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14714 return set_die_type (die, cv_type, cu);
c906108c
SS
14715}
14716
f792889a 14717static struct type *
e7c27a73 14718read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14719{
f792889a 14720 struct type *base_type, *cv_type;
c906108c 14721
e7c27a73 14722 base_type = die_type (die, cu);
7e314c57
JK
14723
14724 /* The die_type call above may have already set the type for this DIE. */
14725 cv_type = get_die_type (die, cu);
14726 if (cv_type)
14727 return cv_type;
14728
cf363f18
MW
14729 /* In case the volatile qualifier is applied to an array type, the
14730 element type is so qualified, not the array type (section 6.7.3
14731 of C99). */
14732 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14733 return add_array_cv_type (die, cu, base_type, 0, 1);
14734
f792889a
DJ
14735 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14736 return set_die_type (die, cv_type, cu);
c906108c
SS
14737}
14738
06d66ee9
TT
14739/* Handle DW_TAG_restrict_type. */
14740
14741static struct type *
14742read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14743{
14744 struct type *base_type, *cv_type;
14745
14746 base_type = die_type (die, cu);
14747
14748 /* The die_type call above may have already set the type for this DIE. */
14749 cv_type = get_die_type (die, cu);
14750 if (cv_type)
14751 return cv_type;
14752
14753 cv_type = make_restrict_type (base_type);
14754 return set_die_type (die, cv_type, cu);
14755}
14756
a2c2acaf
MW
14757/* Handle DW_TAG_atomic_type. */
14758
14759static struct type *
14760read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14761{
14762 struct type *base_type, *cv_type;
14763
14764 base_type = die_type (die, cu);
14765
14766 /* The die_type call above may have already set the type for this DIE. */
14767 cv_type = get_die_type (die, cu);
14768 if (cv_type)
14769 return cv_type;
14770
14771 cv_type = make_atomic_type (base_type);
14772 return set_die_type (die, cv_type, cu);
14773}
14774
c906108c
SS
14775/* Extract all information from a DW_TAG_string_type DIE and add to
14776 the user defined type vector. It isn't really a user defined type,
14777 but it behaves like one, with other DIE's using an AT_user_def_type
14778 attribute to reference it. */
14779
f792889a 14780static struct type *
e7c27a73 14781read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14782{
e7c27a73 14783 struct objfile *objfile = cu->objfile;
3b7538c0 14784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14785 struct type *type, *range_type, *index_type, *char_type;
14786 struct attribute *attr;
14787 unsigned int length;
14788
e142c38c 14789 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14790 if (attr)
14791 {
14792 length = DW_UNSND (attr);
14793 }
14794 else
14795 {
0963b4bd 14796 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14797 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14798 if (attr)
14799 {
14800 length = DW_UNSND (attr);
14801 }
14802 else
14803 {
14804 length = 1;
14805 }
c906108c 14806 }
6ccb9162 14807
46bf5051 14808 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14809 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14810 char_type = language_string_char_type (cu->language_defn, gdbarch);
14811 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14812
f792889a 14813 return set_die_type (die, type, cu);
c906108c
SS
14814}
14815
4d804846
JB
14816/* Assuming that DIE corresponds to a function, returns nonzero
14817 if the function is prototyped. */
14818
14819static int
14820prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14821{
14822 struct attribute *attr;
14823
14824 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14825 if (attr && (DW_UNSND (attr) != 0))
14826 return 1;
14827
14828 /* The DWARF standard implies that the DW_AT_prototyped attribute
14829 is only meaninful for C, but the concept also extends to other
14830 languages that allow unprototyped functions (Eg: Objective C).
14831 For all other languages, assume that functions are always
14832 prototyped. */
14833 if (cu->language != language_c
14834 && cu->language != language_objc
14835 && cu->language != language_opencl)
14836 return 1;
14837
14838 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14839 prototyped and unprototyped functions; default to prototyped,
14840 since that is more common in modern code (and RealView warns
14841 about unprototyped functions). */
14842 if (producer_is_realview (cu->producer))
14843 return 1;
14844
14845 return 0;
14846}
14847
c906108c
SS
14848/* Handle DIES due to C code like:
14849
14850 struct foo
c5aa993b
JM
14851 {
14852 int (*funcp)(int a, long l);
14853 int b;
14854 };
c906108c 14855
0963b4bd 14856 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14857
f792889a 14858static struct type *
e7c27a73 14859read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14860{
bb5ed363 14861 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14862 struct type *type; /* Type that this function returns. */
14863 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14864 struct attribute *attr;
14865
e7c27a73 14866 type = die_type (die, cu);
7e314c57
JK
14867
14868 /* The die_type call above may have already set the type for this DIE. */
14869 ftype = get_die_type (die, cu);
14870 if (ftype)
14871 return ftype;
14872
0c8b41f1 14873 ftype = lookup_function_type (type);
c906108c 14874
4d804846 14875 if (prototyped_function_p (die, cu))
a6c727b2 14876 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14877
c055b101
CV
14878 /* Store the calling convention in the type if it's available in
14879 the subroutine die. Otherwise set the calling convention to
14880 the default value DW_CC_normal. */
14881 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14882 if (attr)
14883 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14884 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14885 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14886 else
14887 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14888
743649fd
MW
14889 /* Record whether the function returns normally to its caller or not
14890 if the DWARF producer set that information. */
14891 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14892 if (attr && (DW_UNSND (attr) != 0))
14893 TYPE_NO_RETURN (ftype) = 1;
14894
76c10ea2
GM
14895 /* We need to add the subroutine type to the die immediately so
14896 we don't infinitely recurse when dealing with parameters
0963b4bd 14897 declared as the same subroutine type. */
76c10ea2 14898 set_die_type (die, ftype, cu);
6e70227d 14899
639d11d3 14900 if (die->child != NULL)
c906108c 14901 {
bb5ed363 14902 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14903 struct die_info *child_die;
8072405b 14904 int nparams, iparams;
c906108c
SS
14905
14906 /* Count the number of parameters.
14907 FIXME: GDB currently ignores vararg functions, but knows about
14908 vararg member functions. */
8072405b 14909 nparams = 0;
639d11d3 14910 child_die = die->child;
c906108c
SS
14911 while (child_die && child_die->tag)
14912 {
14913 if (child_die->tag == DW_TAG_formal_parameter)
14914 nparams++;
14915 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14916 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14917 child_die = sibling_die (child_die);
14918 }
14919
14920 /* Allocate storage for parameters and fill them in. */
14921 TYPE_NFIELDS (ftype) = nparams;
14922 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14923 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14924
8072405b
JK
14925 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14926 even if we error out during the parameters reading below. */
14927 for (iparams = 0; iparams < nparams; iparams++)
14928 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14929
14930 iparams = 0;
639d11d3 14931 child_die = die->child;
c906108c
SS
14932 while (child_die && child_die->tag)
14933 {
14934 if (child_die->tag == DW_TAG_formal_parameter)
14935 {
3ce3b1ba
PA
14936 struct type *arg_type;
14937
14938 /* DWARF version 2 has no clean way to discern C++
14939 static and non-static member functions. G++ helps
14940 GDB by marking the first parameter for non-static
14941 member functions (which is the this pointer) as
14942 artificial. We pass this information to
14943 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14944
14945 DWARF version 3 added DW_AT_object_pointer, which GCC
14946 4.5 does not yet generate. */
e142c38c 14947 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14948 if (attr)
14949 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14950 else
9c37b5ae 14951 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14952 arg_type = die_type (child_die, cu);
14953
14954 /* RealView does not mark THIS as const, which the testsuite
14955 expects. GCC marks THIS as const in method definitions,
14956 but not in the class specifications (GCC PR 43053). */
14957 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14958 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14959 {
14960 int is_this = 0;
14961 struct dwarf2_cu *arg_cu = cu;
14962 const char *name = dwarf2_name (child_die, cu);
14963
14964 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14965 if (attr)
14966 {
14967 /* If the compiler emits this, use it. */
14968 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14969 is_this = 1;
14970 }
14971 else if (name && strcmp (name, "this") == 0)
14972 /* Function definitions will have the argument names. */
14973 is_this = 1;
14974 else if (name == NULL && iparams == 0)
14975 /* Declarations may not have the names, so like
14976 elsewhere in GDB, assume an artificial first
14977 argument is "this". */
14978 is_this = 1;
14979
14980 if (is_this)
14981 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14982 arg_type, 0);
14983 }
14984
14985 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14986 iparams++;
14987 }
14988 child_die = sibling_die (child_die);
14989 }
14990 }
14991
76c10ea2 14992 return ftype;
c906108c
SS
14993}
14994
f792889a 14995static struct type *
e7c27a73 14996read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14997{
e7c27a73 14998 struct objfile *objfile = cu->objfile;
0114d602 14999 const char *name = NULL;
3c8e0968 15000 struct type *this_type, *target_type;
c906108c 15001
94af9270 15002 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15003 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15004 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15005 set_die_type (die, this_type, cu);
3c8e0968
DE
15006 target_type = die_type (die, cu);
15007 if (target_type != this_type)
15008 TYPE_TARGET_TYPE (this_type) = target_type;
15009 else
15010 {
15011 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15012 spec and cause infinite loops in GDB. */
15013 complaint (&symfile_complaints,
15014 _("Self-referential DW_TAG_typedef "
15015 "- DIE at 0x%x [in module %s]"),
9c541725 15016 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15017 TYPE_TARGET_TYPE (this_type) = NULL;
15018 }
f792889a 15019 return this_type;
c906108c
SS
15020}
15021
9b790ce7
UW
15022/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15023 (which may be different from NAME) to the architecture back-end to allow
15024 it to guess the correct format if necessary. */
15025
15026static struct type *
15027dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15028 const char *name_hint)
15029{
15030 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15031 const struct floatformat **format;
15032 struct type *type;
15033
15034 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15035 if (format)
15036 type = init_float_type (objfile, bits, name, format);
15037 else
15038 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15039
15040 return type;
15041}
15042
c906108c
SS
15043/* Find a representation of a given base type and install
15044 it in the TYPE field of the die. */
15045
f792889a 15046static struct type *
e7c27a73 15047read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15048{
e7c27a73 15049 struct objfile *objfile = cu->objfile;
c906108c
SS
15050 struct type *type;
15051 struct attribute *attr;
19f392bc 15052 int encoding = 0, bits = 0;
15d034d0 15053 const char *name;
c906108c 15054
e142c38c 15055 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15056 if (attr)
15057 {
15058 encoding = DW_UNSND (attr);
15059 }
e142c38c 15060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15061 if (attr)
15062 {
19f392bc 15063 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15064 }
39cbfefa 15065 name = dwarf2_name (die, cu);
6ccb9162 15066 if (!name)
c906108c 15067 {
6ccb9162
UW
15068 complaint (&symfile_complaints,
15069 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15070 }
6ccb9162
UW
15071
15072 switch (encoding)
c906108c 15073 {
6ccb9162
UW
15074 case DW_ATE_address:
15075 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15076 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15077 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15078 break;
15079 case DW_ATE_boolean:
19f392bc 15080 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15081 break;
15082 case DW_ATE_complex_float:
9b790ce7 15083 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15084 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15085 break;
15086 case DW_ATE_decimal_float:
19f392bc 15087 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15088 break;
15089 case DW_ATE_float:
9b790ce7 15090 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15091 break;
15092 case DW_ATE_signed:
19f392bc 15093 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15094 break;
15095 case DW_ATE_unsigned:
3b2b8fea
TT
15096 if (cu->language == language_fortran
15097 && name
61012eef 15098 && startswith (name, "character("))
19f392bc
UW
15099 type = init_character_type (objfile, bits, 1, name);
15100 else
15101 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15102 break;
15103 case DW_ATE_signed_char:
6e70227d 15104 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15105 || cu->language == language_pascal
15106 || cu->language == language_fortran)
19f392bc
UW
15107 type = init_character_type (objfile, bits, 0, name);
15108 else
15109 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15110 break;
15111 case DW_ATE_unsigned_char:
868a0084 15112 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15113 || cu->language == language_pascal
c44af4eb
TT
15114 || cu->language == language_fortran
15115 || cu->language == language_rust)
19f392bc
UW
15116 type = init_character_type (objfile, bits, 1, name);
15117 else
15118 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15119 break;
75079b2b 15120 case DW_ATE_UTF:
53e710ac
PA
15121 {
15122 gdbarch *arch = get_objfile_arch (objfile);
15123
15124 if (bits == 16)
15125 type = builtin_type (arch)->builtin_char16;
15126 else if (bits == 32)
15127 type = builtin_type (arch)->builtin_char32;
15128 else
15129 {
15130 complaint (&symfile_complaints,
15131 _("unsupported DW_ATE_UTF bit size: '%d'"),
15132 bits);
15133 type = init_integer_type (objfile, bits, 1, name);
15134 }
15135 return set_die_type (die, type, cu);
15136 }
75079b2b
TT
15137 break;
15138
6ccb9162
UW
15139 default:
15140 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15141 dwarf_type_encoding_name (encoding));
19f392bc
UW
15142 type = init_type (objfile, TYPE_CODE_ERROR,
15143 bits / TARGET_CHAR_BIT, name);
6ccb9162 15144 break;
c906108c 15145 }
6ccb9162 15146
0114d602 15147 if (name && strcmp (name, "char") == 0)
876cecd0 15148 TYPE_NOSIGN (type) = 1;
0114d602 15149
f792889a 15150 return set_die_type (die, type, cu);
c906108c
SS
15151}
15152
80180f79
SA
15153/* Parse dwarf attribute if it's a block, reference or constant and put the
15154 resulting value of the attribute into struct bound_prop.
15155 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15156
15157static int
15158attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15159 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15160{
15161 struct dwarf2_property_baton *baton;
15162 struct obstack *obstack = &cu->objfile->objfile_obstack;
15163
15164 if (attr == NULL || prop == NULL)
15165 return 0;
15166
15167 if (attr_form_is_block (attr))
15168 {
8d749320 15169 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15170 baton->referenced_type = NULL;
15171 baton->locexpr.per_cu = cu->per_cu;
15172 baton->locexpr.size = DW_BLOCK (attr)->size;
15173 baton->locexpr.data = DW_BLOCK (attr)->data;
15174 prop->data.baton = baton;
15175 prop->kind = PROP_LOCEXPR;
15176 gdb_assert (prop->data.baton != NULL);
15177 }
15178 else if (attr_form_is_ref (attr))
15179 {
15180 struct dwarf2_cu *target_cu = cu;
15181 struct die_info *target_die;
15182 struct attribute *target_attr;
15183
15184 target_die = follow_die_ref (die, attr, &target_cu);
15185 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15186 if (target_attr == NULL)
15187 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15188 target_cu);
80180f79
SA
15189 if (target_attr == NULL)
15190 return 0;
15191
df25ebbd 15192 switch (target_attr->name)
80180f79 15193 {
df25ebbd
JB
15194 case DW_AT_location:
15195 if (attr_form_is_section_offset (target_attr))
15196 {
8d749320 15197 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15198 baton->referenced_type = die_type (target_die, target_cu);
15199 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15200 prop->data.baton = baton;
15201 prop->kind = PROP_LOCLIST;
15202 gdb_assert (prop->data.baton != NULL);
15203 }
15204 else if (attr_form_is_block (target_attr))
15205 {
8d749320 15206 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15207 baton->referenced_type = die_type (target_die, target_cu);
15208 baton->locexpr.per_cu = cu->per_cu;
15209 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15210 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15211 prop->data.baton = baton;
15212 prop->kind = PROP_LOCEXPR;
15213 gdb_assert (prop->data.baton != NULL);
15214 }
15215 else
15216 {
15217 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15218 "dynamic property");
15219 return 0;
15220 }
15221 break;
15222 case DW_AT_data_member_location:
15223 {
15224 LONGEST offset;
15225
15226 if (!handle_data_member_location (target_die, target_cu,
15227 &offset))
15228 return 0;
15229
8d749320 15230 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15231 baton->referenced_type = read_type_die (target_die->parent,
15232 target_cu);
df25ebbd
JB
15233 baton->offset_info.offset = offset;
15234 baton->offset_info.type = die_type (target_die, target_cu);
15235 prop->data.baton = baton;
15236 prop->kind = PROP_ADDR_OFFSET;
15237 break;
15238 }
80180f79
SA
15239 }
15240 }
15241 else if (attr_form_is_constant (attr))
15242 {
15243 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15244 prop->kind = PROP_CONST;
15245 }
15246 else
15247 {
15248 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15249 dwarf2_name (die, cu));
15250 return 0;
15251 }
15252
15253 return 1;
15254}
15255
a02abb62
JB
15256/* Read the given DW_AT_subrange DIE. */
15257
f792889a 15258static struct type *
a02abb62
JB
15259read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15260{
4c9ad8c2 15261 struct type *base_type, *orig_base_type;
a02abb62
JB
15262 struct type *range_type;
15263 struct attribute *attr;
729efb13 15264 struct dynamic_prop low, high;
4fae6e18 15265 int low_default_is_valid;
c451ebe5 15266 int high_bound_is_count = 0;
15d034d0 15267 const char *name;
43bbcdc2 15268 LONGEST negative_mask;
e77813c8 15269
4c9ad8c2
TT
15270 orig_base_type = die_type (die, cu);
15271 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15272 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15273 creating the range type, but we use the result of check_typedef
15274 when examining properties of the type. */
15275 base_type = check_typedef (orig_base_type);
a02abb62 15276
7e314c57
JK
15277 /* The die_type call above may have already set the type for this DIE. */
15278 range_type = get_die_type (die, cu);
15279 if (range_type)
15280 return range_type;
15281
729efb13
SA
15282 low.kind = PROP_CONST;
15283 high.kind = PROP_CONST;
15284 high.data.const_val = 0;
15285
4fae6e18
JK
15286 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15287 omitting DW_AT_lower_bound. */
15288 switch (cu->language)
6e70227d 15289 {
4fae6e18
JK
15290 case language_c:
15291 case language_cplus:
729efb13 15292 low.data.const_val = 0;
4fae6e18
JK
15293 low_default_is_valid = 1;
15294 break;
15295 case language_fortran:
729efb13 15296 low.data.const_val = 1;
4fae6e18
JK
15297 low_default_is_valid = 1;
15298 break;
15299 case language_d:
4fae6e18 15300 case language_objc:
c44af4eb 15301 case language_rust:
729efb13 15302 low.data.const_val = 0;
4fae6e18
JK
15303 low_default_is_valid = (cu->header.version >= 4);
15304 break;
15305 case language_ada:
15306 case language_m2:
15307 case language_pascal:
729efb13 15308 low.data.const_val = 1;
4fae6e18
JK
15309 low_default_is_valid = (cu->header.version >= 4);
15310 break;
15311 default:
729efb13 15312 low.data.const_val = 0;
4fae6e18
JK
15313 low_default_is_valid = 0;
15314 break;
a02abb62
JB
15315 }
15316
e142c38c 15317 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15318 if (attr)
11c1ba78 15319 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15320 else if (!low_default_is_valid)
15321 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15322 "- DIE at 0x%x [in module %s]"),
9c541725 15323 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15324
e142c38c 15325 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15326 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15327 {
15328 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15329 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15330 {
c451ebe5
SA
15331 /* If bounds are constant do the final calculation here. */
15332 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15333 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15334 else
15335 high_bound_is_count = 1;
c2ff108b 15336 }
e77813c8
PM
15337 }
15338
15339 /* Dwarf-2 specifications explicitly allows to create subrange types
15340 without specifying a base type.
15341 In that case, the base type must be set to the type of
15342 the lower bound, upper bound or count, in that order, if any of these
15343 three attributes references an object that has a type.
15344 If no base type is found, the Dwarf-2 specifications say that
15345 a signed integer type of size equal to the size of an address should
15346 be used.
15347 For the following C code: `extern char gdb_int [];'
15348 GCC produces an empty range DIE.
15349 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15350 high bound or count are not yet handled by this code. */
e77813c8
PM
15351 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15352 {
15353 struct objfile *objfile = cu->objfile;
15354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15355 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15356 struct type *int_type = objfile_type (objfile)->builtin_int;
15357
15358 /* Test "int", "long int", and "long long int" objfile types,
15359 and select the first one having a size above or equal to the
15360 architecture address size. */
15361 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15362 base_type = int_type;
15363 else
15364 {
15365 int_type = objfile_type (objfile)->builtin_long;
15366 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15367 base_type = int_type;
15368 else
15369 {
15370 int_type = objfile_type (objfile)->builtin_long_long;
15371 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15372 base_type = int_type;
15373 }
15374 }
15375 }
a02abb62 15376
dbb9c2b1
JB
15377 /* Normally, the DWARF producers are expected to use a signed
15378 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15379 But this is unfortunately not always the case, as witnessed
15380 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15381 is used instead. To work around that ambiguity, we treat
15382 the bounds as signed, and thus sign-extend their values, when
15383 the base type is signed. */
6e70227d 15384 negative_mask =
66c6502d 15385 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15386 if (low.kind == PROP_CONST
15387 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15388 low.data.const_val |= negative_mask;
15389 if (high.kind == PROP_CONST
15390 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15391 high.data.const_val |= negative_mask;
43bbcdc2 15392
729efb13 15393 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15394
c451ebe5
SA
15395 if (high_bound_is_count)
15396 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15397
c2ff108b
JK
15398 /* Ada expects an empty array on no boundary attributes. */
15399 if (attr == NULL && cu->language != language_ada)
729efb13 15400 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15401
39cbfefa
DJ
15402 name = dwarf2_name (die, cu);
15403 if (name)
15404 TYPE_NAME (range_type) = name;
6e70227d 15405
e142c38c 15406 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15407 if (attr)
15408 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15409
7e314c57
JK
15410 set_die_type (die, range_type, cu);
15411
15412 /* set_die_type should be already done. */
b4ba55a1
JB
15413 set_descriptive_type (range_type, die, cu);
15414
7e314c57 15415 return range_type;
a02abb62 15416}
6e70227d 15417
f792889a 15418static struct type *
81a17f79
JB
15419read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15420{
15421 struct type *type;
81a17f79 15422
81a17f79
JB
15423 /* For now, we only support the C meaning of an unspecified type: void. */
15424
19f392bc 15425 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15426 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15427
f792889a 15428 return set_die_type (die, type, cu);
81a17f79 15429}
a02abb62 15430
639d11d3
DC
15431/* Read a single die and all its descendents. Set the die's sibling
15432 field to NULL; set other fields in the die correctly, and set all
15433 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15434 location of the info_ptr after reading all of those dies. PARENT
15435 is the parent of the die in question. */
15436
15437static struct die_info *
dee91e82 15438read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15439 const gdb_byte *info_ptr,
15440 const gdb_byte **new_info_ptr,
dee91e82 15441 struct die_info *parent)
639d11d3
DC
15442{
15443 struct die_info *die;
d521ce57 15444 const gdb_byte *cur_ptr;
639d11d3
DC
15445 int has_children;
15446
bf6af496 15447 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15448 if (die == NULL)
15449 {
15450 *new_info_ptr = cur_ptr;
15451 return NULL;
15452 }
93311388 15453 store_in_ref_table (die, reader->cu);
639d11d3
DC
15454
15455 if (has_children)
bf6af496 15456 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15457 else
15458 {
15459 die->child = NULL;
15460 *new_info_ptr = cur_ptr;
15461 }
15462
15463 die->sibling = NULL;
15464 die->parent = parent;
15465 return die;
15466}
15467
15468/* Read a die, all of its descendents, and all of its siblings; set
15469 all of the fields of all of the dies correctly. Arguments are as
15470 in read_die_and_children. */
15471
15472static struct die_info *
bf6af496 15473read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15474 const gdb_byte *info_ptr,
15475 const gdb_byte **new_info_ptr,
bf6af496 15476 struct die_info *parent)
639d11d3
DC
15477{
15478 struct die_info *first_die, *last_sibling;
d521ce57 15479 const gdb_byte *cur_ptr;
639d11d3 15480
c906108c 15481 cur_ptr = info_ptr;
639d11d3
DC
15482 first_die = last_sibling = NULL;
15483
15484 while (1)
c906108c 15485 {
639d11d3 15486 struct die_info *die
dee91e82 15487 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15488
1d325ec1 15489 if (die == NULL)
c906108c 15490 {
639d11d3
DC
15491 *new_info_ptr = cur_ptr;
15492 return first_die;
c906108c 15493 }
1d325ec1
DJ
15494
15495 if (!first_die)
15496 first_die = die;
c906108c 15497 else
1d325ec1
DJ
15498 last_sibling->sibling = die;
15499
15500 last_sibling = die;
c906108c 15501 }
c906108c
SS
15502}
15503
bf6af496
DE
15504/* Read a die, all of its descendents, and all of its siblings; set
15505 all of the fields of all of the dies correctly. Arguments are as
15506 in read_die_and_children.
15507 This the main entry point for reading a DIE and all its children. */
15508
15509static struct die_info *
15510read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15511 const gdb_byte *info_ptr,
15512 const gdb_byte **new_info_ptr,
bf6af496
DE
15513 struct die_info *parent)
15514{
15515 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15516 new_info_ptr, parent);
15517
b4f54984 15518 if (dwarf_die_debug)
bf6af496
DE
15519 {
15520 fprintf_unfiltered (gdb_stdlog,
15521 "Read die from %s@0x%x of %s:\n",
a32a8923 15522 get_section_name (reader->die_section),
bf6af496
DE
15523 (unsigned) (info_ptr - reader->die_section->buffer),
15524 bfd_get_filename (reader->abfd));
b4f54984 15525 dump_die (die, dwarf_die_debug);
bf6af496
DE
15526 }
15527
15528 return die;
15529}
15530
3019eac3
DE
15531/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15532 attributes.
15533 The caller is responsible for filling in the extra attributes
15534 and updating (*DIEP)->num_attrs.
15535 Set DIEP to point to a newly allocated die with its information,
15536 except for its child, sibling, and parent fields.
15537 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15538
d521ce57 15539static const gdb_byte *
3019eac3 15540read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15541 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15542 int *has_children, int num_extra_attrs)
93311388 15543{
b64f50a1 15544 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15545 struct abbrev_info *abbrev;
15546 struct die_info *die;
15547 struct dwarf2_cu *cu = reader->cu;
15548 bfd *abfd = reader->abfd;
15549
9c541725 15550 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15551 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15552 info_ptr += bytes_read;
15553 if (!abbrev_number)
15554 {
15555 *diep = NULL;
15556 *has_children = 0;
15557 return info_ptr;
15558 }
15559
433df2d4 15560 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15561 if (!abbrev)
348e048f
DE
15562 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15563 abbrev_number,
15564 bfd_get_filename (abfd));
15565
3019eac3 15566 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15567 die->sect_off = sect_off;
93311388
DE
15568 die->tag = abbrev->tag;
15569 die->abbrev = abbrev_number;
15570
3019eac3
DE
15571 /* Make the result usable.
15572 The caller needs to update num_attrs after adding the extra
15573 attributes. */
93311388
DE
15574 die->num_attrs = abbrev->num_attrs;
15575
15576 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15577 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15578 info_ptr);
93311388
DE
15579
15580 *diep = die;
15581 *has_children = abbrev->has_children;
15582 return info_ptr;
15583}
15584
3019eac3
DE
15585/* Read a die and all its attributes.
15586 Set DIEP to point to a newly allocated die with its information,
15587 except for its child, sibling, and parent fields.
15588 Set HAS_CHILDREN to tell whether the die has children or not. */
15589
d521ce57 15590static const gdb_byte *
3019eac3 15591read_full_die (const struct die_reader_specs *reader,
d521ce57 15592 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15593 int *has_children)
15594{
d521ce57 15595 const gdb_byte *result;
bf6af496
DE
15596
15597 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15598
b4f54984 15599 if (dwarf_die_debug)
bf6af496
DE
15600 {
15601 fprintf_unfiltered (gdb_stdlog,
15602 "Read die from %s@0x%x of %s:\n",
a32a8923 15603 get_section_name (reader->die_section),
bf6af496
DE
15604 (unsigned) (info_ptr - reader->die_section->buffer),
15605 bfd_get_filename (reader->abfd));
b4f54984 15606 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15607 }
15608
15609 return result;
3019eac3 15610}
433df2d4
DE
15611\f
15612/* Abbreviation tables.
3019eac3 15613
433df2d4 15614 In DWARF version 2, the description of the debugging information is
c906108c
SS
15615 stored in a separate .debug_abbrev section. Before we read any
15616 dies from a section we read in all abbreviations and install them
433df2d4
DE
15617 in a hash table. */
15618
15619/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15620
15621static struct abbrev_info *
15622abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15623{
15624 struct abbrev_info *abbrev;
15625
8d749320 15626 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15627 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15628
433df2d4
DE
15629 return abbrev;
15630}
15631
15632/* Add an abbreviation to the table. */
c906108c
SS
15633
15634static void
433df2d4
DE
15635abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15636 unsigned int abbrev_number,
15637 struct abbrev_info *abbrev)
15638{
15639 unsigned int hash_number;
15640
15641 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15642 abbrev->next = abbrev_table->abbrevs[hash_number];
15643 abbrev_table->abbrevs[hash_number] = abbrev;
15644}
dee91e82 15645
433df2d4
DE
15646/* Look up an abbrev in the table.
15647 Returns NULL if the abbrev is not found. */
15648
15649static struct abbrev_info *
15650abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15651 unsigned int abbrev_number)
c906108c 15652{
433df2d4
DE
15653 unsigned int hash_number;
15654 struct abbrev_info *abbrev;
15655
15656 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15657 abbrev = abbrev_table->abbrevs[hash_number];
15658
15659 while (abbrev)
15660 {
15661 if (abbrev->number == abbrev_number)
15662 return abbrev;
15663 abbrev = abbrev->next;
15664 }
15665 return NULL;
15666}
15667
15668/* Read in an abbrev table. */
15669
15670static struct abbrev_table *
15671abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15672 sect_offset sect_off)
433df2d4
DE
15673{
15674 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15675 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15676 struct abbrev_table *abbrev_table;
d521ce57 15677 const gdb_byte *abbrev_ptr;
c906108c
SS
15678 struct abbrev_info *cur_abbrev;
15679 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15680 unsigned int abbrev_form;
f3dd6933
DJ
15681 struct attr_abbrev *cur_attrs;
15682 unsigned int allocated_attrs;
c906108c 15683
70ba0933 15684 abbrev_table = XNEW (struct abbrev_table);
9c541725 15685 abbrev_table->sect_off = sect_off;
433df2d4 15686 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15687 abbrev_table->abbrevs =
15688 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15689 ABBREV_HASH_SIZE);
433df2d4
DE
15690 memset (abbrev_table->abbrevs, 0,
15691 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15692
433df2d4 15693 dwarf2_read_section (objfile, section);
9c541725 15694 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15695 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15696 abbrev_ptr += bytes_read;
15697
f3dd6933 15698 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15699 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15700
0963b4bd 15701 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15702 while (abbrev_number)
15703 {
433df2d4 15704 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15705
15706 /* read in abbrev header */
15707 cur_abbrev->number = abbrev_number;
aead7601
SM
15708 cur_abbrev->tag
15709 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15710 abbrev_ptr += bytes_read;
15711 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15712 abbrev_ptr += 1;
15713
15714 /* now read in declarations */
22d2f3ab 15715 for (;;)
c906108c 15716 {
43988095
JK
15717 LONGEST implicit_const;
15718
22d2f3ab
JK
15719 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15720 abbrev_ptr += bytes_read;
15721 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15722 abbrev_ptr += bytes_read;
43988095
JK
15723 if (abbrev_form == DW_FORM_implicit_const)
15724 {
15725 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15726 &bytes_read);
15727 abbrev_ptr += bytes_read;
15728 }
15729 else
15730 {
15731 /* Initialize it due to a false compiler warning. */
15732 implicit_const = -1;
15733 }
22d2f3ab
JK
15734
15735 if (abbrev_name == 0)
15736 break;
15737
f3dd6933 15738 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15739 {
f3dd6933
DJ
15740 allocated_attrs += ATTR_ALLOC_CHUNK;
15741 cur_attrs
224c3ddb 15742 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15743 }
ae038cb0 15744
aead7601
SM
15745 cur_attrs[cur_abbrev->num_attrs].name
15746 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15747 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15748 = (enum dwarf_form) abbrev_form;
43988095 15749 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15750 ++cur_abbrev->num_attrs;
c906108c
SS
15751 }
15752
8d749320
SM
15753 cur_abbrev->attrs =
15754 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15755 cur_abbrev->num_attrs);
f3dd6933
DJ
15756 memcpy (cur_abbrev->attrs, cur_attrs,
15757 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15758
433df2d4 15759 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15760
15761 /* Get next abbreviation.
15762 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15763 always properly terminated with an abbrev number of 0.
15764 Exit loop if we encounter an abbreviation which we have
15765 already read (which means we are about to read the abbreviations
15766 for the next compile unit) or if the end of the abbreviation
15767 table is reached. */
433df2d4 15768 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15769 break;
15770 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15771 abbrev_ptr += bytes_read;
433df2d4 15772 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15773 break;
15774 }
f3dd6933
DJ
15775
15776 xfree (cur_attrs);
433df2d4 15777 return abbrev_table;
c906108c
SS
15778}
15779
433df2d4 15780/* Free the resources held by ABBREV_TABLE. */
c906108c 15781
c906108c 15782static void
433df2d4 15783abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15784{
433df2d4
DE
15785 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15786 xfree (abbrev_table);
c906108c
SS
15787}
15788
f4dc4d17
DE
15789/* Same as abbrev_table_free but as a cleanup.
15790 We pass in a pointer to the pointer to the table so that we can
15791 set the pointer to NULL when we're done. It also simplifies
73051182 15792 build_type_psymtabs_1. */
f4dc4d17
DE
15793
15794static void
15795abbrev_table_free_cleanup (void *table_ptr)
15796{
9a3c8263 15797 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15798
15799 if (*abbrev_table_ptr != NULL)
15800 abbrev_table_free (*abbrev_table_ptr);
15801 *abbrev_table_ptr = NULL;
15802}
15803
433df2d4
DE
15804/* Read the abbrev table for CU from ABBREV_SECTION. */
15805
15806static void
15807dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15808 struct dwarf2_section_info *abbrev_section)
c906108c 15809{
433df2d4 15810 cu->abbrev_table =
9c541725 15811 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15812}
c906108c 15813
433df2d4 15814/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15815
433df2d4
DE
15816static void
15817dwarf2_free_abbrev_table (void *ptr_to_cu)
15818{
9a3c8263 15819 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15820
a2ce51a0
DE
15821 if (cu->abbrev_table != NULL)
15822 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15823 /* Set this to NULL so that we SEGV if we try to read it later,
15824 and also because free_comp_unit verifies this is NULL. */
15825 cu->abbrev_table = NULL;
15826}
15827\f
72bf9492
DJ
15828/* Returns nonzero if TAG represents a type that we might generate a partial
15829 symbol for. */
15830
15831static int
15832is_type_tag_for_partial (int tag)
15833{
15834 switch (tag)
15835 {
15836#if 0
15837 /* Some types that would be reasonable to generate partial symbols for,
15838 that we don't at present. */
15839 case DW_TAG_array_type:
15840 case DW_TAG_file_type:
15841 case DW_TAG_ptr_to_member_type:
15842 case DW_TAG_set_type:
15843 case DW_TAG_string_type:
15844 case DW_TAG_subroutine_type:
15845#endif
15846 case DW_TAG_base_type:
15847 case DW_TAG_class_type:
680b30c7 15848 case DW_TAG_interface_type:
72bf9492
DJ
15849 case DW_TAG_enumeration_type:
15850 case DW_TAG_structure_type:
15851 case DW_TAG_subrange_type:
15852 case DW_TAG_typedef:
15853 case DW_TAG_union_type:
15854 return 1;
15855 default:
15856 return 0;
15857 }
15858}
15859
15860/* Load all DIEs that are interesting for partial symbols into memory. */
15861
15862static struct partial_die_info *
dee91e82 15863load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15864 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15865{
dee91e82 15866 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15867 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15868 struct partial_die_info *part_die;
15869 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15870 struct abbrev_info *abbrev;
15871 unsigned int bytes_read;
5afb4e99 15872 unsigned int load_all = 0;
72bf9492
DJ
15873 int nesting_level = 1;
15874
15875 parent_die = NULL;
15876 last_die = NULL;
15877
7adf1e79
DE
15878 gdb_assert (cu->per_cu != NULL);
15879 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15880 load_all = 1;
15881
72bf9492
DJ
15882 cu->partial_dies
15883 = htab_create_alloc_ex (cu->header.length / 12,
15884 partial_die_hash,
15885 partial_die_eq,
15886 NULL,
15887 &cu->comp_unit_obstack,
15888 hashtab_obstack_allocate,
15889 dummy_obstack_deallocate);
15890
8d749320 15891 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15892
15893 while (1)
15894 {
15895 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15896
15897 /* A NULL abbrev means the end of a series of children. */
15898 if (abbrev == NULL)
15899 {
15900 if (--nesting_level == 0)
15901 {
15902 /* PART_DIE was probably the last thing allocated on the
15903 comp_unit_obstack, so we could call obstack_free
15904 here. We don't do that because the waste is small,
15905 and will be cleaned up when we're done with this
15906 compilation unit. This way, we're also more robust
15907 against other users of the comp_unit_obstack. */
15908 return first_die;
15909 }
15910 info_ptr += bytes_read;
15911 last_die = parent_die;
15912 parent_die = parent_die->die_parent;
15913 continue;
15914 }
15915
98bfdba5
PA
15916 /* Check for template arguments. We never save these; if
15917 they're seen, we just mark the parent, and go on our way. */
15918 if (parent_die != NULL
15919 && cu->language == language_cplus
15920 && (abbrev->tag == DW_TAG_template_type_param
15921 || abbrev->tag == DW_TAG_template_value_param))
15922 {
15923 parent_die->has_template_arguments = 1;
15924
15925 if (!load_all)
15926 {
15927 /* We don't need a partial DIE for the template argument. */
dee91e82 15928 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15929 continue;
15930 }
15931 }
15932
0d99eb77 15933 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15934 Skip their other children. */
15935 if (!load_all
15936 && cu->language == language_cplus
15937 && parent_die != NULL
15938 && parent_die->tag == DW_TAG_subprogram)
15939 {
dee91e82 15940 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15941 continue;
15942 }
15943
5afb4e99
DJ
15944 /* Check whether this DIE is interesting enough to save. Normally
15945 we would not be interested in members here, but there may be
15946 later variables referencing them via DW_AT_specification (for
15947 static members). */
15948 if (!load_all
15949 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15950 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15951 && abbrev->tag != DW_TAG_enumerator
15952 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15953 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15954 && abbrev->tag != DW_TAG_variable
5afb4e99 15955 && abbrev->tag != DW_TAG_namespace
f55ee35c 15956 && abbrev->tag != DW_TAG_module
95554aad 15957 && abbrev->tag != DW_TAG_member
74921315
KS
15958 && abbrev->tag != DW_TAG_imported_unit
15959 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15960 {
15961 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15962 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15963 continue;
15964 }
15965
dee91e82
DE
15966 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15967 info_ptr);
72bf9492
DJ
15968
15969 /* This two-pass algorithm for processing partial symbols has a
15970 high cost in cache pressure. Thus, handle some simple cases
15971 here which cover the majority of C partial symbols. DIEs
15972 which neither have specification tags in them, nor could have
15973 specification tags elsewhere pointing at them, can simply be
15974 processed and discarded.
15975
15976 This segment is also optional; scan_partial_symbols and
15977 add_partial_symbol will handle these DIEs if we chain
15978 them in normally. When compilers which do not emit large
15979 quantities of duplicate debug information are more common,
15980 this code can probably be removed. */
15981
15982 /* Any complete simple types at the top level (pretty much all
15983 of them, for a language without namespaces), can be processed
15984 directly. */
15985 if (parent_die == NULL
15986 && part_die->has_specification == 0
15987 && part_die->is_declaration == 0
d8228535 15988 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15989 || part_die->tag == DW_TAG_base_type
15990 || part_die->tag == DW_TAG_subrange_type))
15991 {
15992 if (building_psymtab && part_die->name != NULL)
04a679b8 15993 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15994 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15995 &objfile->static_psymbols,
1762568f 15996 0, cu->language, objfile);
dee91e82 15997 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15998 continue;
15999 }
16000
d8228535
JK
16001 /* The exception for DW_TAG_typedef with has_children above is
16002 a workaround of GCC PR debug/47510. In the case of this complaint
16003 type_name_no_tag_or_error will error on such types later.
16004
16005 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16006 it could not find the child DIEs referenced later, this is checked
16007 above. In correct DWARF DW_TAG_typedef should have no children. */
16008
16009 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16010 complaint (&symfile_complaints,
16011 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16012 "- DIE at 0x%x [in module %s]"),
9c541725 16013 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16014
72bf9492
DJ
16015 /* If we're at the second level, and we're an enumerator, and
16016 our parent has no specification (meaning possibly lives in a
16017 namespace elsewhere), then we can add the partial symbol now
16018 instead of queueing it. */
16019 if (part_die->tag == DW_TAG_enumerator
16020 && parent_die != NULL
16021 && parent_die->die_parent == NULL
16022 && parent_die->tag == DW_TAG_enumeration_type
16023 && parent_die->has_specification == 0)
16024 {
16025 if (part_die->name == NULL)
3e43a32a
MS
16026 complaint (&symfile_complaints,
16027 _("malformed enumerator DIE ignored"));
72bf9492 16028 else if (building_psymtab)
04a679b8 16029 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16030 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16031 cu->language == language_cplus
bb5ed363
DE
16032 ? &objfile->global_psymbols
16033 : &objfile->static_psymbols,
1762568f 16034 0, cu->language, objfile);
72bf9492 16035
dee91e82 16036 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16037 continue;
16038 }
16039
16040 /* We'll save this DIE so link it in. */
16041 part_die->die_parent = parent_die;
16042 part_die->die_sibling = NULL;
16043 part_die->die_child = NULL;
16044
16045 if (last_die && last_die == parent_die)
16046 last_die->die_child = part_die;
16047 else if (last_die)
16048 last_die->die_sibling = part_die;
16049
16050 last_die = part_die;
16051
16052 if (first_die == NULL)
16053 first_die = part_die;
16054
16055 /* Maybe add the DIE to the hash table. Not all DIEs that we
16056 find interesting need to be in the hash table, because we
16057 also have the parent/sibling/child chains; only those that we
16058 might refer to by offset later during partial symbol reading.
16059
16060 For now this means things that might have be the target of a
16061 DW_AT_specification, DW_AT_abstract_origin, or
16062 DW_AT_extension. DW_AT_extension will refer only to
16063 namespaces; DW_AT_abstract_origin refers to functions (and
16064 many things under the function DIE, but we do not recurse
16065 into function DIEs during partial symbol reading) and
16066 possibly variables as well; DW_AT_specification refers to
16067 declarations. Declarations ought to have the DW_AT_declaration
16068 flag. It happens that GCC forgets to put it in sometimes, but
16069 only for functions, not for types.
16070
16071 Adding more things than necessary to the hash table is harmless
16072 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16073 wasted time in find_partial_die, when we reread the compilation
16074 unit with load_all_dies set. */
72bf9492 16075
5afb4e99 16076 if (load_all
72929c62 16077 || abbrev->tag == DW_TAG_constant
5afb4e99 16078 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16079 || abbrev->tag == DW_TAG_variable
16080 || abbrev->tag == DW_TAG_namespace
16081 || part_die->is_declaration)
16082 {
16083 void **slot;
16084
16085 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16086 to_underlying (part_die->sect_off),
16087 INSERT);
72bf9492
DJ
16088 *slot = part_die;
16089 }
16090
8d749320 16091 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16092
16093 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16094 we have no reason to follow the children of structures; for other
98bfdba5
PA
16095 languages we have to, so that we can get at method physnames
16096 to infer fully qualified class names, for DW_AT_specification,
16097 and for C++ template arguments. For C++, we also look one level
16098 inside functions to find template arguments (if the name of the
16099 function does not already contain the template arguments).
bc30ff58
JB
16100
16101 For Ada, we need to scan the children of subprograms and lexical
16102 blocks as well because Ada allows the definition of nested
16103 entities that could be interesting for the debugger, such as
16104 nested subprograms for instance. */
72bf9492 16105 if (last_die->has_children
5afb4e99
DJ
16106 && (load_all
16107 || last_die->tag == DW_TAG_namespace
f55ee35c 16108 || last_die->tag == DW_TAG_module
72bf9492 16109 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16110 || (cu->language == language_cplus
16111 && last_die->tag == DW_TAG_subprogram
16112 && (last_die->name == NULL
16113 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16114 || (cu->language != language_c
16115 && (last_die->tag == DW_TAG_class_type
680b30c7 16116 || last_die->tag == DW_TAG_interface_type
72bf9492 16117 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16118 || last_die->tag == DW_TAG_union_type))
16119 || (cu->language == language_ada
16120 && (last_die->tag == DW_TAG_subprogram
16121 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16122 {
16123 nesting_level++;
16124 parent_die = last_die;
16125 continue;
16126 }
16127
16128 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16129 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16130
16131 /* Back to the top, do it again. */
16132 }
16133}
16134
c906108c
SS
16135/* Read a minimal amount of information into the minimal die structure. */
16136
d521ce57 16137static const gdb_byte *
dee91e82
DE
16138read_partial_die (const struct die_reader_specs *reader,
16139 struct partial_die_info *part_die,
16140 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16141 const gdb_byte *info_ptr)
c906108c 16142{
dee91e82 16143 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16144 struct objfile *objfile = cu->objfile;
d521ce57 16145 const gdb_byte *buffer = reader->buffer;
fa238c03 16146 unsigned int i;
c906108c 16147 struct attribute attr;
c5aa993b 16148 int has_low_pc_attr = 0;
c906108c 16149 int has_high_pc_attr = 0;
91da1414 16150 int high_pc_relative = 0;
c906108c 16151
72bf9492 16152 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16153
9c541725 16154 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16155
16156 info_ptr += abbrev_len;
16157
16158 if (abbrev == NULL)
16159 return info_ptr;
16160
c906108c
SS
16161 part_die->tag = abbrev->tag;
16162 part_die->has_children = abbrev->has_children;
c906108c
SS
16163
16164 for (i = 0; i < abbrev->num_attrs; ++i)
16165 {
dee91e82 16166 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16167
16168 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16169 partial symbol table. */
c906108c
SS
16170 switch (attr.name)
16171 {
16172 case DW_AT_name:
71c25dea
TT
16173 switch (part_die->tag)
16174 {
16175 case DW_TAG_compile_unit:
95554aad 16176 case DW_TAG_partial_unit:
348e048f 16177 case DW_TAG_type_unit:
71c25dea
TT
16178 /* Compilation units have a DW_AT_name that is a filename, not
16179 a source language identifier. */
16180 case DW_TAG_enumeration_type:
16181 case DW_TAG_enumerator:
16182 /* These tags always have simple identifiers already; no need
16183 to canonicalize them. */
16184 part_die->name = DW_STRING (&attr);
16185 break;
16186 default:
16187 part_die->name
16188 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16189 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16190 break;
16191 }
c906108c 16192 break;
31ef98ae 16193 case DW_AT_linkage_name:
c906108c 16194 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16195 /* Note that both forms of linkage name might appear. We
16196 assume they will be the same, and we only store the last
16197 one we see. */
94af9270
KS
16198 if (cu->language == language_ada)
16199 part_die->name = DW_STRING (&attr);
abc72ce4 16200 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16201 break;
16202 case DW_AT_low_pc:
16203 has_low_pc_attr = 1;
31aa7e4e 16204 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16205 break;
16206 case DW_AT_high_pc:
16207 has_high_pc_attr = 1;
31aa7e4e
JB
16208 part_die->highpc = attr_value_as_address (&attr);
16209 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16210 high_pc_relative = 1;
c906108c
SS
16211 break;
16212 case DW_AT_location:
0963b4bd 16213 /* Support the .debug_loc offsets. */
8e19ed76
PS
16214 if (attr_form_is_block (&attr))
16215 {
95554aad 16216 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16217 }
3690dd37 16218 else if (attr_form_is_section_offset (&attr))
8e19ed76 16219 {
4d3c2250 16220 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16221 }
16222 else
16223 {
4d3c2250
KB
16224 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16225 "partial symbol information");
8e19ed76 16226 }
c906108c 16227 break;
c906108c
SS
16228 case DW_AT_external:
16229 part_die->is_external = DW_UNSND (&attr);
16230 break;
16231 case DW_AT_declaration:
16232 part_die->is_declaration = DW_UNSND (&attr);
16233 break;
16234 case DW_AT_type:
16235 part_die->has_type = 1;
16236 break;
16237 case DW_AT_abstract_origin:
16238 case DW_AT_specification:
72bf9492
DJ
16239 case DW_AT_extension:
16240 part_die->has_specification = 1;
c764a876 16241 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16242 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16243 || cu->per_cu->is_dwz);
c906108c
SS
16244 break;
16245 case DW_AT_sibling:
16246 /* Ignore absolute siblings, they might point outside of
16247 the current compile unit. */
16248 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16249 complaint (&symfile_complaints,
16250 _("ignoring absolute DW_AT_sibling"));
c906108c 16251 else
b9502d3f 16252 {
9c541725
PA
16253 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16254 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16255
16256 if (sibling_ptr < info_ptr)
16257 complaint (&symfile_complaints,
16258 _("DW_AT_sibling points backwards"));
22869d73
KS
16259 else if (sibling_ptr > reader->buffer_end)
16260 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16261 else
16262 part_die->sibling = sibling_ptr;
16263 }
c906108c 16264 break;
fa4028e9
JB
16265 case DW_AT_byte_size:
16266 part_die->has_byte_size = 1;
16267 break;
ff908ebf
AW
16268 case DW_AT_const_value:
16269 part_die->has_const_value = 1;
16270 break;
68511cec
CES
16271 case DW_AT_calling_convention:
16272 /* DWARF doesn't provide a way to identify a program's source-level
16273 entry point. DW_AT_calling_convention attributes are only meant
16274 to describe functions' calling conventions.
16275
16276 However, because it's a necessary piece of information in
0c1b455e
TT
16277 Fortran, and before DWARF 4 DW_CC_program was the only
16278 piece of debugging information whose definition refers to
16279 a 'main program' at all, several compilers marked Fortran
16280 main programs with DW_CC_program --- even when those
16281 functions use the standard calling conventions.
16282
16283 Although DWARF now specifies a way to provide this
16284 information, we support this practice for backward
16285 compatibility. */
68511cec 16286 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16287 && cu->language == language_fortran)
16288 part_die->main_subprogram = 1;
68511cec 16289 break;
481860b3
GB
16290 case DW_AT_inline:
16291 if (DW_UNSND (&attr) == DW_INL_inlined
16292 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16293 part_die->may_be_inlined = 1;
16294 break;
95554aad
TT
16295
16296 case DW_AT_import:
16297 if (part_die->tag == DW_TAG_imported_unit)
36586728 16298 {
9c541725 16299 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16300 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16301 || cu->per_cu->is_dwz);
16302 }
95554aad
TT
16303 break;
16304
0c1b455e
TT
16305 case DW_AT_main_subprogram:
16306 part_die->main_subprogram = DW_UNSND (&attr);
16307 break;
16308
c906108c
SS
16309 default:
16310 break;
16311 }
16312 }
16313
91da1414
MW
16314 if (high_pc_relative)
16315 part_die->highpc += part_die->lowpc;
16316
9373cf26
JK
16317 if (has_low_pc_attr && has_high_pc_attr)
16318 {
16319 /* When using the GNU linker, .gnu.linkonce. sections are used to
16320 eliminate duplicate copies of functions and vtables and such.
16321 The linker will arbitrarily choose one and discard the others.
16322 The AT_*_pc values for such functions refer to local labels in
16323 these sections. If the section from that file was discarded, the
16324 labels are not in the output, so the relocs get a value of 0.
16325 If this is a discarded function, mark the pc bounds as invalid,
16326 so that GDB will ignore it. */
16327 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16328 {
bb5ed363 16329 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16330
16331 complaint (&symfile_complaints,
16332 _("DW_AT_low_pc %s is zero "
16333 "for DIE at 0x%x [in module %s]"),
16334 paddress (gdbarch, part_die->lowpc),
9c541725 16335 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16336 }
16337 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16338 else if (part_die->lowpc >= part_die->highpc)
16339 {
bb5ed363 16340 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16341
16342 complaint (&symfile_complaints,
16343 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16344 "for DIE at 0x%x [in module %s]"),
16345 paddress (gdbarch, part_die->lowpc),
16346 paddress (gdbarch, part_die->highpc),
9c541725
PA
16347 to_underlying (part_die->sect_off),
16348 objfile_name (objfile));
9373cf26
JK
16349 }
16350 else
16351 part_die->has_pc_info = 1;
16352 }
85cbf3d3 16353
c906108c
SS
16354 return info_ptr;
16355}
16356
72bf9492
DJ
16357/* Find a cached partial DIE at OFFSET in CU. */
16358
16359static struct partial_die_info *
9c541725 16360find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16361{
16362 struct partial_die_info *lookup_die = NULL;
16363 struct partial_die_info part_die;
16364
9c541725 16365 part_die.sect_off = sect_off;
9a3c8263
SM
16366 lookup_die = ((struct partial_die_info *)
16367 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16368 to_underlying (sect_off)));
72bf9492 16369
72bf9492
DJ
16370 return lookup_die;
16371}
16372
348e048f
DE
16373/* Find a partial DIE at OFFSET, which may or may not be in CU,
16374 except in the case of .debug_types DIEs which do not reference
16375 outside their CU (they do however referencing other types via
55f1336d 16376 DW_FORM_ref_sig8). */
72bf9492
DJ
16377
16378static struct partial_die_info *
9c541725 16379find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16380{
bb5ed363 16381 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16382 struct dwarf2_per_cu_data *per_cu = NULL;
16383 struct partial_die_info *pd = NULL;
72bf9492 16384
36586728 16385 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16386 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16387 {
9c541725 16388 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16389 if (pd != NULL)
16390 return pd;
0d99eb77
DE
16391 /* We missed recording what we needed.
16392 Load all dies and try again. */
16393 per_cu = cu->per_cu;
5afb4e99 16394 }
0d99eb77
DE
16395 else
16396 {
16397 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16398 if (cu->per_cu->is_debug_types)
0d99eb77 16399 {
9c541725
PA
16400 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16401 " external reference to offset 0x%x [in module %s].\n"),
16402 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16403 bfd_get_filename (objfile->obfd));
16404 }
9c541725 16405 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16406 objfile);
72bf9492 16407
0d99eb77
DE
16408 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16409 load_partial_comp_unit (per_cu);
ae038cb0 16410
0d99eb77 16411 per_cu->cu->last_used = 0;
9c541725 16412 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16413 }
5afb4e99 16414
dee91e82
DE
16415 /* If we didn't find it, and not all dies have been loaded,
16416 load them all and try again. */
16417
5afb4e99
DJ
16418 if (pd == NULL && per_cu->load_all_dies == 0)
16419 {
5afb4e99 16420 per_cu->load_all_dies = 1;
fd820528
DE
16421
16422 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16423 THIS_CU->cu may already be in use. So we can't just free it and
16424 replace its DIEs with the ones we read in. Instead, we leave those
16425 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16426 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16427 set. */
dee91e82 16428 load_partial_comp_unit (per_cu);
5afb4e99 16429
9c541725 16430 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16431 }
16432
16433 if (pd == NULL)
16434 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16435 _("could not find partial DIE 0x%x "
16436 "in cache [from module %s]\n"),
9c541725 16437 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16438 return pd;
72bf9492
DJ
16439}
16440
abc72ce4
DE
16441/* See if we can figure out if the class lives in a namespace. We do
16442 this by looking for a member function; its demangled name will
16443 contain namespace info, if there is any. */
16444
16445static void
16446guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16447 struct dwarf2_cu *cu)
16448{
16449 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16450 what template types look like, because the demangler
16451 frequently doesn't give the same name as the debug info. We
16452 could fix this by only using the demangled name to get the
16453 prefix (but see comment in read_structure_type). */
16454
16455 struct partial_die_info *real_pdi;
16456 struct partial_die_info *child_pdi;
16457
16458 /* If this DIE (this DIE's specification, if any) has a parent, then
16459 we should not do this. We'll prepend the parent's fully qualified
16460 name when we create the partial symbol. */
16461
16462 real_pdi = struct_pdi;
16463 while (real_pdi->has_specification)
36586728
TT
16464 real_pdi = find_partial_die (real_pdi->spec_offset,
16465 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16466
16467 if (real_pdi->die_parent != NULL)
16468 return;
16469
16470 for (child_pdi = struct_pdi->die_child;
16471 child_pdi != NULL;
16472 child_pdi = child_pdi->die_sibling)
16473 {
16474 if (child_pdi->tag == DW_TAG_subprogram
16475 && child_pdi->linkage_name != NULL)
16476 {
16477 char *actual_class_name
16478 = language_class_name_from_physname (cu->language_defn,
16479 child_pdi->linkage_name);
16480 if (actual_class_name != NULL)
16481 {
16482 struct_pdi->name
224c3ddb
SM
16483 = ((const char *)
16484 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16485 actual_class_name,
16486 strlen (actual_class_name)));
abc72ce4
DE
16487 xfree (actual_class_name);
16488 }
16489 break;
16490 }
16491 }
16492}
16493
72bf9492
DJ
16494/* Adjust PART_DIE before generating a symbol for it. This function
16495 may set the is_external flag or change the DIE's name. */
16496
16497static void
16498fixup_partial_die (struct partial_die_info *part_die,
16499 struct dwarf2_cu *cu)
16500{
abc72ce4
DE
16501 /* Once we've fixed up a die, there's no point in doing so again.
16502 This also avoids a memory leak if we were to call
16503 guess_partial_die_structure_name multiple times. */
16504 if (part_die->fixup_called)
16505 return;
16506
72bf9492
DJ
16507 /* If we found a reference attribute and the DIE has no name, try
16508 to find a name in the referred to DIE. */
16509
16510 if (part_die->name == NULL && part_die->has_specification)
16511 {
16512 struct partial_die_info *spec_die;
72bf9492 16513
36586728
TT
16514 spec_die = find_partial_die (part_die->spec_offset,
16515 part_die->spec_is_dwz, cu);
72bf9492 16516
10b3939b 16517 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16518
16519 if (spec_die->name)
16520 {
16521 part_die->name = spec_die->name;
16522
16523 /* Copy DW_AT_external attribute if it is set. */
16524 if (spec_die->is_external)
16525 part_die->is_external = spec_die->is_external;
16526 }
16527 }
16528
16529 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16530
16531 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16532 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16533
abc72ce4
DE
16534 /* If there is no parent die to provide a namespace, and there are
16535 children, see if we can determine the namespace from their linkage
122d1940 16536 name. */
abc72ce4 16537 if (cu->language == language_cplus
8b70b953 16538 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16539 && part_die->die_parent == NULL
16540 && part_die->has_children
16541 && (part_die->tag == DW_TAG_class_type
16542 || part_die->tag == DW_TAG_structure_type
16543 || part_die->tag == DW_TAG_union_type))
16544 guess_partial_die_structure_name (part_die, cu);
16545
53832f31
TT
16546 /* GCC might emit a nameless struct or union that has a linkage
16547 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16548 if (part_die->name == NULL
96408a79
SA
16549 && (part_die->tag == DW_TAG_class_type
16550 || part_die->tag == DW_TAG_interface_type
16551 || part_die->tag == DW_TAG_structure_type
16552 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16553 && part_die->linkage_name != NULL)
16554 {
16555 char *demangled;
16556
8de20a37 16557 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16558 if (demangled)
16559 {
96408a79
SA
16560 const char *base;
16561
16562 /* Strip any leading namespaces/classes, keep only the base name.
16563 DW_AT_name for named DIEs does not contain the prefixes. */
16564 base = strrchr (demangled, ':');
16565 if (base && base > demangled && base[-1] == ':')
16566 base++;
16567 else
16568 base = demangled;
16569
34a68019 16570 part_die->name
224c3ddb
SM
16571 = ((const char *)
16572 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16573 base, strlen (base)));
53832f31
TT
16574 xfree (demangled);
16575 }
16576 }
16577
abc72ce4 16578 part_die->fixup_called = 1;
72bf9492
DJ
16579}
16580
a8329558 16581/* Read an attribute value described by an attribute form. */
c906108c 16582
d521ce57 16583static const gdb_byte *
dee91e82
DE
16584read_attribute_value (const struct die_reader_specs *reader,
16585 struct attribute *attr, unsigned form,
43988095 16586 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16587{
dee91e82 16588 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16589 struct objfile *objfile = cu->objfile;
16590 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16591 bfd *abfd = reader->abfd;
e7c27a73 16592 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16593 unsigned int bytes_read;
16594 struct dwarf_block *blk;
16595
aead7601 16596 attr->form = (enum dwarf_form) form;
a8329558 16597 switch (form)
c906108c 16598 {
c906108c 16599 case DW_FORM_ref_addr:
ae411497 16600 if (cu->header.version == 2)
4568ecf9 16601 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16602 else
4568ecf9
DE
16603 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16604 &cu->header, &bytes_read);
ae411497
TT
16605 info_ptr += bytes_read;
16606 break;
36586728
TT
16607 case DW_FORM_GNU_ref_alt:
16608 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16609 info_ptr += bytes_read;
16610 break;
ae411497 16611 case DW_FORM_addr:
e7c27a73 16612 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16613 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16614 info_ptr += bytes_read;
c906108c
SS
16615 break;
16616 case DW_FORM_block2:
7b5a2f43 16617 blk = dwarf_alloc_block (cu);
c906108c
SS
16618 blk->size = read_2_bytes (abfd, info_ptr);
16619 info_ptr += 2;
16620 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16621 info_ptr += blk->size;
16622 DW_BLOCK (attr) = blk;
16623 break;
16624 case DW_FORM_block4:
7b5a2f43 16625 blk = dwarf_alloc_block (cu);
c906108c
SS
16626 blk->size = read_4_bytes (abfd, info_ptr);
16627 info_ptr += 4;
16628 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16629 info_ptr += blk->size;
16630 DW_BLOCK (attr) = blk;
16631 break;
16632 case DW_FORM_data2:
16633 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16634 info_ptr += 2;
16635 break;
16636 case DW_FORM_data4:
16637 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16638 info_ptr += 4;
16639 break;
16640 case DW_FORM_data8:
16641 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16642 info_ptr += 8;
16643 break;
0224619f
JK
16644 case DW_FORM_data16:
16645 blk = dwarf_alloc_block (cu);
16646 blk->size = 16;
16647 blk->data = read_n_bytes (abfd, info_ptr, 16);
16648 info_ptr += 16;
16649 DW_BLOCK (attr) = blk;
16650 break;
2dc7f7b3
TT
16651 case DW_FORM_sec_offset:
16652 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16653 info_ptr += bytes_read;
16654 break;
c906108c 16655 case DW_FORM_string:
9b1c24c8 16656 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16657 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16658 info_ptr += bytes_read;
16659 break;
4bdf3d34 16660 case DW_FORM_strp:
36586728
TT
16661 if (!cu->per_cu->is_dwz)
16662 {
16663 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16664 &bytes_read);
16665 DW_STRING_IS_CANONICAL (attr) = 0;
16666 info_ptr += bytes_read;
16667 break;
16668 }
16669 /* FALLTHROUGH */
43988095
JK
16670 case DW_FORM_line_strp:
16671 if (!cu->per_cu->is_dwz)
16672 {
16673 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16674 cu_header, &bytes_read);
16675 DW_STRING_IS_CANONICAL (attr) = 0;
16676 info_ptr += bytes_read;
16677 break;
16678 }
16679 /* FALLTHROUGH */
36586728
TT
16680 case DW_FORM_GNU_strp_alt:
16681 {
16682 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16683 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16684 &bytes_read);
16685
16686 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16687 DW_STRING_IS_CANONICAL (attr) = 0;
16688 info_ptr += bytes_read;
16689 }
4bdf3d34 16690 break;
2dc7f7b3 16691 case DW_FORM_exprloc:
c906108c 16692 case DW_FORM_block:
7b5a2f43 16693 blk = dwarf_alloc_block (cu);
c906108c
SS
16694 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16695 info_ptr += bytes_read;
16696 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16697 info_ptr += blk->size;
16698 DW_BLOCK (attr) = blk;
16699 break;
16700 case DW_FORM_block1:
7b5a2f43 16701 blk = dwarf_alloc_block (cu);
c906108c
SS
16702 blk->size = read_1_byte (abfd, info_ptr);
16703 info_ptr += 1;
16704 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16705 info_ptr += blk->size;
16706 DW_BLOCK (attr) = blk;
16707 break;
16708 case DW_FORM_data1:
16709 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16710 info_ptr += 1;
16711 break;
16712 case DW_FORM_flag:
16713 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16714 info_ptr += 1;
16715 break;
2dc7f7b3
TT
16716 case DW_FORM_flag_present:
16717 DW_UNSND (attr) = 1;
16718 break;
c906108c
SS
16719 case DW_FORM_sdata:
16720 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16721 info_ptr += bytes_read;
16722 break;
16723 case DW_FORM_udata:
16724 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16725 info_ptr += bytes_read;
16726 break;
16727 case DW_FORM_ref1:
9c541725 16728 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16729 + read_1_byte (abfd, info_ptr));
c906108c
SS
16730 info_ptr += 1;
16731 break;
16732 case DW_FORM_ref2:
9c541725 16733 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16734 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16735 info_ptr += 2;
16736 break;
16737 case DW_FORM_ref4:
9c541725 16738 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16739 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16740 info_ptr += 4;
16741 break;
613e1657 16742 case DW_FORM_ref8:
9c541725 16743 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16744 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16745 info_ptr += 8;
16746 break;
55f1336d 16747 case DW_FORM_ref_sig8:
ac9ec31b 16748 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16749 info_ptr += 8;
16750 break;
c906108c 16751 case DW_FORM_ref_udata:
9c541725 16752 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16753 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16754 info_ptr += bytes_read;
16755 break;
c906108c 16756 case DW_FORM_indirect:
a8329558
KW
16757 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16758 info_ptr += bytes_read;
43988095
JK
16759 if (form == DW_FORM_implicit_const)
16760 {
16761 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16762 info_ptr += bytes_read;
16763 }
16764 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16765 info_ptr);
16766 break;
16767 case DW_FORM_implicit_const:
16768 DW_SND (attr) = implicit_const;
a8329558 16769 break;
3019eac3
DE
16770 case DW_FORM_GNU_addr_index:
16771 if (reader->dwo_file == NULL)
16772 {
16773 /* For now flag a hard error.
16774 Later we can turn this into a complaint. */
16775 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16776 dwarf_form_name (form),
16777 bfd_get_filename (abfd));
16778 }
16779 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16780 info_ptr += bytes_read;
16781 break;
16782 case DW_FORM_GNU_str_index:
16783 if (reader->dwo_file == NULL)
16784 {
16785 /* For now flag a hard error.
16786 Later we can turn this into a complaint if warranted. */
16787 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16788 dwarf_form_name (form),
16789 bfd_get_filename (abfd));
16790 }
16791 {
16792 ULONGEST str_index =
16793 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16794
342587c4 16795 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16796 DW_STRING_IS_CANONICAL (attr) = 0;
16797 info_ptr += bytes_read;
16798 }
16799 break;
c906108c 16800 default:
8a3fe4f8 16801 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16802 dwarf_form_name (form),
16803 bfd_get_filename (abfd));
c906108c 16804 }
28e94949 16805
36586728 16806 /* Super hack. */
7771576e 16807 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16808 attr->form = DW_FORM_GNU_ref_alt;
16809
28e94949
JB
16810 /* We have seen instances where the compiler tried to emit a byte
16811 size attribute of -1 which ended up being encoded as an unsigned
16812 0xffffffff. Although 0xffffffff is technically a valid size value,
16813 an object of this size seems pretty unlikely so we can relatively
16814 safely treat these cases as if the size attribute was invalid and
16815 treat them as zero by default. */
16816 if (attr->name == DW_AT_byte_size
16817 && form == DW_FORM_data4
16818 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16819 {
16820 complaint
16821 (&symfile_complaints,
43bbcdc2
PH
16822 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16823 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16824 DW_UNSND (attr) = 0;
16825 }
28e94949 16826
c906108c
SS
16827 return info_ptr;
16828}
16829
a8329558
KW
16830/* Read an attribute described by an abbreviated attribute. */
16831
d521ce57 16832static const gdb_byte *
dee91e82
DE
16833read_attribute (const struct die_reader_specs *reader,
16834 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16835 const gdb_byte *info_ptr)
a8329558
KW
16836{
16837 attr->name = abbrev->name;
43988095
JK
16838 return read_attribute_value (reader, attr, abbrev->form,
16839 abbrev->implicit_const, info_ptr);
a8329558
KW
16840}
16841
0963b4bd 16842/* Read dwarf information from a buffer. */
c906108c
SS
16843
16844static unsigned int
a1855c1d 16845read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16846{
fe1b8b76 16847 return bfd_get_8 (abfd, buf);
c906108c
SS
16848}
16849
16850static int
a1855c1d 16851read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16852{
fe1b8b76 16853 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16854}
16855
16856static unsigned int
a1855c1d 16857read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16858{
fe1b8b76 16859 return bfd_get_16 (abfd, buf);
c906108c
SS
16860}
16861
21ae7a4d 16862static int
a1855c1d 16863read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16864{
16865 return bfd_get_signed_16 (abfd, buf);
16866}
16867
c906108c 16868static unsigned int
a1855c1d 16869read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16870{
fe1b8b76 16871 return bfd_get_32 (abfd, buf);
c906108c
SS
16872}
16873
21ae7a4d 16874static int
a1855c1d 16875read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16876{
16877 return bfd_get_signed_32 (abfd, buf);
16878}
16879
93311388 16880static ULONGEST
a1855c1d 16881read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16882{
fe1b8b76 16883 return bfd_get_64 (abfd, buf);
c906108c
SS
16884}
16885
16886static CORE_ADDR
d521ce57 16887read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16888 unsigned int *bytes_read)
c906108c 16889{
e7c27a73 16890 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16891 CORE_ADDR retval = 0;
16892
107d2387 16893 if (cu_header->signed_addr_p)
c906108c 16894 {
107d2387
AC
16895 switch (cu_header->addr_size)
16896 {
16897 case 2:
fe1b8b76 16898 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16899 break;
16900 case 4:
fe1b8b76 16901 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16902 break;
16903 case 8:
fe1b8b76 16904 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16905 break;
16906 default:
8e65ff28 16907 internal_error (__FILE__, __LINE__,
e2e0b3e5 16908 _("read_address: bad switch, signed [in module %s]"),
659b0389 16909 bfd_get_filename (abfd));
107d2387
AC
16910 }
16911 }
16912 else
16913 {
16914 switch (cu_header->addr_size)
16915 {
16916 case 2:
fe1b8b76 16917 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16918 break;
16919 case 4:
fe1b8b76 16920 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16921 break;
16922 case 8:
fe1b8b76 16923 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16924 break;
16925 default:
8e65ff28 16926 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16927 _("read_address: bad switch, "
16928 "unsigned [in module %s]"),
659b0389 16929 bfd_get_filename (abfd));
107d2387 16930 }
c906108c 16931 }
64367e0a 16932
107d2387
AC
16933 *bytes_read = cu_header->addr_size;
16934 return retval;
c906108c
SS
16935}
16936
f7ef9339 16937/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16938 specification allows the initial length to take up either 4 bytes
16939 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16940 bytes describe the length and all offsets will be 8 bytes in length
16941 instead of 4.
16942
f7ef9339
KB
16943 An older, non-standard 64-bit format is also handled by this
16944 function. The older format in question stores the initial length
16945 as an 8-byte quantity without an escape value. Lengths greater
16946 than 2^32 aren't very common which means that the initial 4 bytes
16947 is almost always zero. Since a length value of zero doesn't make
16948 sense for the 32-bit format, this initial zero can be considered to
16949 be an escape value which indicates the presence of the older 64-bit
16950 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16951 greater than 4GB. If it becomes necessary to handle lengths
16952 somewhat larger than 4GB, we could allow other small values (such
16953 as the non-sensical values of 1, 2, and 3) to also be used as
16954 escape values indicating the presence of the old format.
f7ef9339 16955
917c78fc
MK
16956 The value returned via bytes_read should be used to increment the
16957 relevant pointer after calling read_initial_length().
c764a876 16958
613e1657
KB
16959 [ Note: read_initial_length() and read_offset() are based on the
16960 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16961 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16962 from:
16963
f7ef9339 16964 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16965
613e1657
KB
16966 This document is only a draft and is subject to change. (So beware.)
16967
f7ef9339 16968 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16969 determined empirically by examining 64-bit ELF files produced by
16970 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16971
16972 - Kevin, July 16, 2002
613e1657
KB
16973 ] */
16974
16975static LONGEST
d521ce57 16976read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16977{
fe1b8b76 16978 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16979
dd373385 16980 if (length == 0xffffffff)
613e1657 16981 {
fe1b8b76 16982 length = bfd_get_64 (abfd, buf + 4);
613e1657 16983 *bytes_read = 12;
613e1657 16984 }
dd373385 16985 else if (length == 0)
f7ef9339 16986 {
dd373385 16987 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16988 length = bfd_get_64 (abfd, buf);
f7ef9339 16989 *bytes_read = 8;
f7ef9339 16990 }
613e1657
KB
16991 else
16992 {
16993 *bytes_read = 4;
613e1657
KB
16994 }
16995
c764a876
DE
16996 return length;
16997}
dd373385 16998
c764a876
DE
16999/* Cover function for read_initial_length.
17000 Returns the length of the object at BUF, and stores the size of the
17001 initial length in *BYTES_READ and stores the size that offsets will be in
17002 *OFFSET_SIZE.
17003 If the initial length size is not equivalent to that specified in
17004 CU_HEADER then issue a complaint.
17005 This is useful when reading non-comp-unit headers. */
dd373385 17006
c764a876 17007static LONGEST
d521ce57 17008read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17009 const struct comp_unit_head *cu_header,
17010 unsigned int *bytes_read,
17011 unsigned int *offset_size)
17012{
17013 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17014
17015 gdb_assert (cu_header->initial_length_size == 4
17016 || cu_header->initial_length_size == 8
17017 || cu_header->initial_length_size == 12);
17018
17019 if (cu_header->initial_length_size != *bytes_read)
17020 complaint (&symfile_complaints,
17021 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17022
c764a876 17023 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17024 return length;
613e1657
KB
17025}
17026
17027/* Read an offset from the data stream. The size of the offset is
917c78fc 17028 given by cu_header->offset_size. */
613e1657
KB
17029
17030static LONGEST
d521ce57
TT
17031read_offset (bfd *abfd, const gdb_byte *buf,
17032 const struct comp_unit_head *cu_header,
891d2f0b 17033 unsigned int *bytes_read)
c764a876
DE
17034{
17035 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17036
c764a876
DE
17037 *bytes_read = cu_header->offset_size;
17038 return offset;
17039}
17040
17041/* Read an offset from the data stream. */
17042
17043static LONGEST
d521ce57 17044read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17045{
17046 LONGEST retval = 0;
17047
c764a876 17048 switch (offset_size)
613e1657
KB
17049 {
17050 case 4:
fe1b8b76 17051 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17052 break;
17053 case 8:
fe1b8b76 17054 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17055 break;
17056 default:
8e65ff28 17057 internal_error (__FILE__, __LINE__,
c764a876 17058 _("read_offset_1: bad switch [in module %s]"),
659b0389 17059 bfd_get_filename (abfd));
613e1657
KB
17060 }
17061
917c78fc 17062 return retval;
613e1657
KB
17063}
17064
d521ce57
TT
17065static const gdb_byte *
17066read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17067{
17068 /* If the size of a host char is 8 bits, we can return a pointer
17069 to the buffer, otherwise we have to copy the data to a buffer
17070 allocated on the temporary obstack. */
4bdf3d34 17071 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17072 return buf;
c906108c
SS
17073}
17074
d521ce57
TT
17075static const char *
17076read_direct_string (bfd *abfd, const gdb_byte *buf,
17077 unsigned int *bytes_read_ptr)
c906108c
SS
17078{
17079 /* If the size of a host char is 8 bits, we can return a pointer
17080 to the string, otherwise we have to copy the string to a buffer
17081 allocated on the temporary obstack. */
4bdf3d34 17082 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17083 if (*buf == '\0')
17084 {
17085 *bytes_read_ptr = 1;
17086 return NULL;
17087 }
d521ce57
TT
17088 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17089 return (const char *) buf;
4bdf3d34
JJ
17090}
17091
43988095
JK
17092/* Return pointer to string at section SECT offset STR_OFFSET with error
17093 reporting strings FORM_NAME and SECT_NAME. */
17094
d521ce57 17095static const char *
43988095
JK
17096read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17097 struct dwarf2_section_info *sect,
17098 const char *form_name,
17099 const char *sect_name)
17100{
17101 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17102 if (sect->buffer == NULL)
17103 error (_("%s used without %s section [in module %s]"),
17104 form_name, sect_name, bfd_get_filename (abfd));
17105 if (str_offset >= sect->size)
17106 error (_("%s pointing outside of %s section [in module %s]"),
17107 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17108 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17109 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17110 return NULL;
43988095
JK
17111 return (const char *) (sect->buffer + str_offset);
17112}
17113
17114/* Return pointer to string at .debug_str offset STR_OFFSET. */
17115
17116static const char *
17117read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17118{
17119 return read_indirect_string_at_offset_from (abfd, str_offset,
17120 &dwarf2_per_objfile->str,
17121 "DW_FORM_strp", ".debug_str");
17122}
17123
17124/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17125
17126static const char *
17127read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17128{
17129 return read_indirect_string_at_offset_from (abfd, str_offset,
17130 &dwarf2_per_objfile->line_str,
17131 "DW_FORM_line_strp",
17132 ".debug_line_str");
c906108c
SS
17133}
17134
36586728
TT
17135/* Read a string at offset STR_OFFSET in the .debug_str section from
17136 the .dwz file DWZ. Throw an error if the offset is too large. If
17137 the string consists of a single NUL byte, return NULL; otherwise
17138 return a pointer to the string. */
17139
d521ce57 17140static const char *
36586728
TT
17141read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17142{
17143 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17144
17145 if (dwz->str.buffer == NULL)
17146 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17147 "section [in module %s]"),
17148 bfd_get_filename (dwz->dwz_bfd));
17149 if (str_offset >= dwz->str.size)
17150 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17151 ".debug_str section [in module %s]"),
17152 bfd_get_filename (dwz->dwz_bfd));
17153 gdb_assert (HOST_CHAR_BIT == 8);
17154 if (dwz->str.buffer[str_offset] == '\0')
17155 return NULL;
d521ce57 17156 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17157}
17158
43988095
JK
17159/* Return pointer to string at .debug_str offset as read from BUF.
17160 BUF is assumed to be in a compilation unit described by CU_HEADER.
17161 Return *BYTES_READ_PTR count of bytes read from BUF. */
17162
d521ce57
TT
17163static const char *
17164read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17165 const struct comp_unit_head *cu_header,
17166 unsigned int *bytes_read_ptr)
17167{
17168 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17169
17170 return read_indirect_string_at_offset (abfd, str_offset);
17171}
17172
43988095
JK
17173/* Return pointer to string at .debug_line_str offset as read from BUF.
17174 BUF is assumed to be in a compilation unit described by CU_HEADER.
17175 Return *BYTES_READ_PTR count of bytes read from BUF. */
17176
17177static const char *
17178read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17179 const struct comp_unit_head *cu_header,
17180 unsigned int *bytes_read_ptr)
17181{
17182 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17183
17184 return read_indirect_line_string_at_offset (abfd, str_offset);
17185}
17186
17187ULONGEST
d521ce57 17188read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17189 unsigned int *bytes_read_ptr)
c906108c 17190{
12df843f 17191 ULONGEST result;
ce5d95e1 17192 unsigned int num_read;
870f88f7 17193 int shift;
c906108c
SS
17194 unsigned char byte;
17195
17196 result = 0;
17197 shift = 0;
17198 num_read = 0;
c906108c
SS
17199 while (1)
17200 {
fe1b8b76 17201 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17202 buf++;
17203 num_read++;
12df843f 17204 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17205 if ((byte & 128) == 0)
17206 {
17207 break;
17208 }
17209 shift += 7;
17210 }
17211 *bytes_read_ptr = num_read;
17212 return result;
17213}
17214
12df843f 17215static LONGEST
d521ce57
TT
17216read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17217 unsigned int *bytes_read_ptr)
c906108c 17218{
12df843f 17219 LONGEST result;
870f88f7 17220 int shift, num_read;
c906108c
SS
17221 unsigned char byte;
17222
17223 result = 0;
17224 shift = 0;
c906108c 17225 num_read = 0;
c906108c
SS
17226 while (1)
17227 {
fe1b8b76 17228 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17229 buf++;
17230 num_read++;
12df843f 17231 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17232 shift += 7;
17233 if ((byte & 128) == 0)
17234 {
17235 break;
17236 }
17237 }
77e0b926 17238 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17239 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17240 *bytes_read_ptr = num_read;
17241 return result;
17242}
17243
3019eac3
DE
17244/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17245 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17246 ADDR_SIZE is the size of addresses from the CU header. */
17247
17248static CORE_ADDR
17249read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17250{
17251 struct objfile *objfile = dwarf2_per_objfile->objfile;
17252 bfd *abfd = objfile->obfd;
17253 const gdb_byte *info_ptr;
17254
17255 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17256 if (dwarf2_per_objfile->addr.buffer == NULL)
17257 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17258 objfile_name (objfile));
3019eac3
DE
17259 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17260 error (_("DW_FORM_addr_index pointing outside of "
17261 ".debug_addr section [in module %s]"),
4262abfb 17262 objfile_name (objfile));
3019eac3
DE
17263 info_ptr = (dwarf2_per_objfile->addr.buffer
17264 + addr_base + addr_index * addr_size);
17265 if (addr_size == 4)
17266 return bfd_get_32 (abfd, info_ptr);
17267 else
17268 return bfd_get_64 (abfd, info_ptr);
17269}
17270
17271/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17272
17273static CORE_ADDR
17274read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17275{
17276 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17277}
17278
17279/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17280
17281static CORE_ADDR
d521ce57 17282read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17283 unsigned int *bytes_read)
17284{
17285 bfd *abfd = cu->objfile->obfd;
17286 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17287
17288 return read_addr_index (cu, addr_index);
17289}
17290
17291/* Data structure to pass results from dwarf2_read_addr_index_reader
17292 back to dwarf2_read_addr_index. */
17293
17294struct dwarf2_read_addr_index_data
17295{
17296 ULONGEST addr_base;
17297 int addr_size;
17298};
17299
17300/* die_reader_func for dwarf2_read_addr_index. */
17301
17302static void
17303dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17304 const gdb_byte *info_ptr,
3019eac3
DE
17305 struct die_info *comp_unit_die,
17306 int has_children,
17307 void *data)
17308{
17309 struct dwarf2_cu *cu = reader->cu;
17310 struct dwarf2_read_addr_index_data *aidata =
17311 (struct dwarf2_read_addr_index_data *) data;
17312
17313 aidata->addr_base = cu->addr_base;
17314 aidata->addr_size = cu->header.addr_size;
17315}
17316
17317/* Given an index in .debug_addr, fetch the value.
17318 NOTE: This can be called during dwarf expression evaluation,
17319 long after the debug information has been read, and thus per_cu->cu
17320 may no longer exist. */
17321
17322CORE_ADDR
17323dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17324 unsigned int addr_index)
17325{
17326 struct objfile *objfile = per_cu->objfile;
17327 struct dwarf2_cu *cu = per_cu->cu;
17328 ULONGEST addr_base;
17329 int addr_size;
17330
17331 /* This is intended to be called from outside this file. */
17332 dw2_setup (objfile);
17333
17334 /* We need addr_base and addr_size.
17335 If we don't have PER_CU->cu, we have to get it.
17336 Nasty, but the alternative is storing the needed info in PER_CU,
17337 which at this point doesn't seem justified: it's not clear how frequently
17338 it would get used and it would increase the size of every PER_CU.
17339 Entry points like dwarf2_per_cu_addr_size do a similar thing
17340 so we're not in uncharted territory here.
17341 Alas we need to be a bit more complicated as addr_base is contained
17342 in the DIE.
17343
17344 We don't need to read the entire CU(/TU).
17345 We just need the header and top level die.
a1b64ce1 17346
3019eac3 17347 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17348 For now we skip this optimization. */
3019eac3
DE
17349
17350 if (cu != NULL)
17351 {
17352 addr_base = cu->addr_base;
17353 addr_size = cu->header.addr_size;
17354 }
17355 else
17356 {
17357 struct dwarf2_read_addr_index_data aidata;
17358
a1b64ce1
DE
17359 /* Note: We can't use init_cutu_and_read_dies_simple here,
17360 we need addr_base. */
17361 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17362 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17363 addr_base = aidata.addr_base;
17364 addr_size = aidata.addr_size;
17365 }
17366
17367 return read_addr_index_1 (addr_index, addr_base, addr_size);
17368}
17369
57d63ce2
DE
17370/* Given a DW_FORM_GNU_str_index, fetch the string.
17371 This is only used by the Fission support. */
3019eac3 17372
d521ce57 17373static const char *
342587c4 17374read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17375{
17376 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17377 const char *objf_name = objfile_name (objfile);
3019eac3 17378 bfd *abfd = objfile->obfd;
342587c4 17379 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17380 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17381 struct dwarf2_section_info *str_offsets_section =
17382 &reader->dwo_file->sections.str_offsets;
d521ce57 17383 const gdb_byte *info_ptr;
3019eac3 17384 ULONGEST str_offset;
57d63ce2 17385 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17386
73869dc2
DE
17387 dwarf2_read_section (objfile, str_section);
17388 dwarf2_read_section (objfile, str_offsets_section);
17389 if (str_section->buffer == NULL)
57d63ce2 17390 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17391 " in CU at offset 0x%x [in module %s]"),
17392 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17393 if (str_offsets_section->buffer == NULL)
57d63ce2 17394 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17395 " in CU at offset 0x%x [in module %s]"),
17396 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17397 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17398 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17399 " section in CU at offset 0x%x [in module %s]"),
17400 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17401 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17402 + str_index * cu->header.offset_size);
17403 if (cu->header.offset_size == 4)
17404 str_offset = bfd_get_32 (abfd, info_ptr);
17405 else
17406 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17407 if (str_offset >= str_section->size)
57d63ce2 17408 error (_("Offset from %s pointing outside of"
9c541725
PA
17409 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17410 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17411 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17412}
17413
3019eac3
DE
17414/* Return the length of an LEB128 number in BUF. */
17415
17416static int
17417leb128_size (const gdb_byte *buf)
17418{
17419 const gdb_byte *begin = buf;
17420 gdb_byte byte;
17421
17422 while (1)
17423 {
17424 byte = *buf++;
17425 if ((byte & 128) == 0)
17426 return buf - begin;
17427 }
17428}
17429
c906108c 17430static void
e142c38c 17431set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17432{
17433 switch (lang)
17434 {
17435 case DW_LANG_C89:
76bee0cc 17436 case DW_LANG_C99:
0cfd832f 17437 case DW_LANG_C11:
c906108c 17438 case DW_LANG_C:
d1be3247 17439 case DW_LANG_UPC:
e142c38c 17440 cu->language = language_c;
c906108c 17441 break;
9c37b5ae 17442 case DW_LANG_Java:
c906108c 17443 case DW_LANG_C_plus_plus:
0cfd832f
MW
17444 case DW_LANG_C_plus_plus_11:
17445 case DW_LANG_C_plus_plus_14:
e142c38c 17446 cu->language = language_cplus;
c906108c 17447 break;
6aecb9c2
JB
17448 case DW_LANG_D:
17449 cu->language = language_d;
17450 break;
c906108c
SS
17451 case DW_LANG_Fortran77:
17452 case DW_LANG_Fortran90:
b21b22e0 17453 case DW_LANG_Fortran95:
f7de9aab
MW
17454 case DW_LANG_Fortran03:
17455 case DW_LANG_Fortran08:
e142c38c 17456 cu->language = language_fortran;
c906108c 17457 break;
a766d390
DE
17458 case DW_LANG_Go:
17459 cu->language = language_go;
17460 break;
c906108c 17461 case DW_LANG_Mips_Assembler:
e142c38c 17462 cu->language = language_asm;
c906108c
SS
17463 break;
17464 case DW_LANG_Ada83:
8aaf0b47 17465 case DW_LANG_Ada95:
bc5f45f8
JB
17466 cu->language = language_ada;
17467 break;
72019c9c
GM
17468 case DW_LANG_Modula2:
17469 cu->language = language_m2;
17470 break;
fe8e67fd
PM
17471 case DW_LANG_Pascal83:
17472 cu->language = language_pascal;
17473 break;
22566fbd
DJ
17474 case DW_LANG_ObjC:
17475 cu->language = language_objc;
17476 break;
c44af4eb
TT
17477 case DW_LANG_Rust:
17478 case DW_LANG_Rust_old:
17479 cu->language = language_rust;
17480 break;
c906108c
SS
17481 case DW_LANG_Cobol74:
17482 case DW_LANG_Cobol85:
c906108c 17483 default:
e142c38c 17484 cu->language = language_minimal;
c906108c
SS
17485 break;
17486 }
e142c38c 17487 cu->language_defn = language_def (cu->language);
c906108c
SS
17488}
17489
17490/* Return the named attribute or NULL if not there. */
17491
17492static struct attribute *
e142c38c 17493dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17494{
a48e046c 17495 for (;;)
c906108c 17496 {
a48e046c
TT
17497 unsigned int i;
17498 struct attribute *spec = NULL;
17499
17500 for (i = 0; i < die->num_attrs; ++i)
17501 {
17502 if (die->attrs[i].name == name)
17503 return &die->attrs[i];
17504 if (die->attrs[i].name == DW_AT_specification
17505 || die->attrs[i].name == DW_AT_abstract_origin)
17506 spec = &die->attrs[i];
17507 }
17508
17509 if (!spec)
17510 break;
c906108c 17511
f2f0e013 17512 die = follow_die_ref (die, spec, &cu);
f2f0e013 17513 }
c5aa993b 17514
c906108c
SS
17515 return NULL;
17516}
17517
348e048f
DE
17518/* Return the named attribute or NULL if not there,
17519 but do not follow DW_AT_specification, etc.
17520 This is for use in contexts where we're reading .debug_types dies.
17521 Following DW_AT_specification, DW_AT_abstract_origin will take us
17522 back up the chain, and we want to go down. */
17523
17524static struct attribute *
45e58e77 17525dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17526{
17527 unsigned int i;
17528
17529 for (i = 0; i < die->num_attrs; ++i)
17530 if (die->attrs[i].name == name)
17531 return &die->attrs[i];
17532
17533 return NULL;
17534}
17535
7d45c7c3
KB
17536/* Return the string associated with a string-typed attribute, or NULL if it
17537 is either not found or is of an incorrect type. */
17538
17539static const char *
17540dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17541{
17542 struct attribute *attr;
17543 const char *str = NULL;
17544
17545 attr = dwarf2_attr (die, name, cu);
17546
17547 if (attr != NULL)
17548 {
43988095
JK
17549 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17550 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17551 str = DW_STRING (attr);
17552 else
17553 complaint (&symfile_complaints,
17554 _("string type expected for attribute %s for "
17555 "DIE at 0x%x in module %s"),
9c541725 17556 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17557 objfile_name (cu->objfile));
17558 }
17559
17560 return str;
17561}
17562
05cf31d1
JB
17563/* Return non-zero iff the attribute NAME is defined for the given DIE,
17564 and holds a non-zero value. This function should only be used for
2dc7f7b3 17565 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17566
17567static int
17568dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17569{
17570 struct attribute *attr = dwarf2_attr (die, name, cu);
17571
17572 return (attr && DW_UNSND (attr));
17573}
17574
3ca72b44 17575static int
e142c38c 17576die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17577{
05cf31d1
JB
17578 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17579 which value is non-zero. However, we have to be careful with
17580 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17581 (via dwarf2_flag_true_p) follows this attribute. So we may
17582 end up accidently finding a declaration attribute that belongs
17583 to a different DIE referenced by the specification attribute,
17584 even though the given DIE does not have a declaration attribute. */
17585 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17586 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17587}
17588
63d06c5c 17589/* Return the die giving the specification for DIE, if there is
f2f0e013 17590 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17591 containing the return value on output. If there is no
17592 specification, but there is an abstract origin, that is
17593 returned. */
63d06c5c
DC
17594
17595static struct die_info *
f2f0e013 17596die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17597{
f2f0e013
DJ
17598 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17599 *spec_cu);
63d06c5c 17600
edb3359d
DJ
17601 if (spec_attr == NULL)
17602 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17603
63d06c5c
DC
17604 if (spec_attr == NULL)
17605 return NULL;
17606 else
f2f0e013 17607 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17608}
c906108c 17609
527f3840
JK
17610/* Stub for free_line_header to match void * callback types. */
17611
17612static void
17613free_line_header_voidp (void *arg)
17614{
9a3c8263 17615 struct line_header *lh = (struct line_header *) arg;
527f3840 17616
fff8551c 17617 delete lh;
527f3840
JK
17618}
17619
fff8551c
PA
17620void
17621line_header::add_include_dir (const char *include_dir)
c906108c 17622{
27e0867f 17623 if (dwarf_line_debug >= 2)
fff8551c
PA
17624 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17625 include_dirs.size () + 1, include_dir);
27e0867f 17626
fff8551c 17627 include_dirs.push_back (include_dir);
debd256d 17628}
6e70227d 17629
fff8551c
PA
17630void
17631line_header::add_file_name (const char *name,
ecfb656c 17632 dir_index d_index,
fff8551c
PA
17633 unsigned int mod_time,
17634 unsigned int length)
debd256d 17635{
27e0867f
DE
17636 if (dwarf_line_debug >= 2)
17637 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17638 (unsigned) file_names.size () + 1, name);
27e0867f 17639
ecfb656c 17640 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17641}
6e70227d 17642
83769d0b 17643/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17644
17645static struct dwarf2_section_info *
17646get_debug_line_section (struct dwarf2_cu *cu)
17647{
17648 struct dwarf2_section_info *section;
17649
17650 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17651 DWO file. */
17652 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17653 section = &cu->dwo_unit->dwo_file->sections.line;
17654 else if (cu->per_cu->is_dwz)
17655 {
17656 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17657
17658 section = &dwz->line;
17659 }
17660 else
17661 section = &dwarf2_per_objfile->line;
17662
17663 return section;
17664}
17665
43988095
JK
17666/* Read directory or file name entry format, starting with byte of
17667 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17668 entries count and the entries themselves in the described entry
17669 format. */
17670
17671static void
17672read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17673 struct line_header *lh,
17674 const struct comp_unit_head *cu_header,
17675 void (*callback) (struct line_header *lh,
17676 const char *name,
ecfb656c 17677 dir_index d_index,
43988095
JK
17678 unsigned int mod_time,
17679 unsigned int length))
17680{
17681 gdb_byte format_count, formati;
17682 ULONGEST data_count, datai;
17683 const gdb_byte *buf = *bufp;
17684 const gdb_byte *format_header_data;
17685 int i;
17686 unsigned int bytes_read;
17687
17688 format_count = read_1_byte (abfd, buf);
17689 buf += 1;
17690 format_header_data = buf;
17691 for (formati = 0; formati < format_count; formati++)
17692 {
17693 read_unsigned_leb128 (abfd, buf, &bytes_read);
17694 buf += bytes_read;
17695 read_unsigned_leb128 (abfd, buf, &bytes_read);
17696 buf += bytes_read;
17697 }
17698
17699 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17700 buf += bytes_read;
17701 for (datai = 0; datai < data_count; datai++)
17702 {
17703 const gdb_byte *format = format_header_data;
17704 struct file_entry fe;
17705
43988095
JK
17706 for (formati = 0; formati < format_count; formati++)
17707 {
ecfb656c 17708 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17709 format += bytes_read;
43988095 17710
ecfb656c 17711 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17712 format += bytes_read;
ecfb656c
PA
17713
17714 gdb::optional<const char *> string;
17715 gdb::optional<unsigned int> uint;
17716
43988095
JK
17717 switch (form)
17718 {
17719 case DW_FORM_string:
ecfb656c 17720 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17721 buf += bytes_read;
17722 break;
17723
17724 case DW_FORM_line_strp:
ecfb656c
PA
17725 string.emplace (read_indirect_line_string (abfd, buf,
17726 cu_header,
17727 &bytes_read));
43988095
JK
17728 buf += bytes_read;
17729 break;
17730
17731 case DW_FORM_data1:
ecfb656c 17732 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17733 buf += 1;
17734 break;
17735
17736 case DW_FORM_data2:
ecfb656c 17737 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17738 buf += 2;
17739 break;
17740
17741 case DW_FORM_data4:
ecfb656c 17742 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17743 buf += 4;
17744 break;
17745
17746 case DW_FORM_data8:
ecfb656c 17747 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17748 buf += 8;
17749 break;
17750
17751 case DW_FORM_udata:
ecfb656c 17752 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17753 buf += bytes_read;
17754 break;
17755
17756 case DW_FORM_block:
17757 /* It is valid only for DW_LNCT_timestamp which is ignored by
17758 current GDB. */
17759 break;
17760 }
ecfb656c
PA
17761
17762 switch (content_type)
17763 {
17764 case DW_LNCT_path:
17765 if (string.has_value ())
17766 fe.name = *string;
17767 break;
17768 case DW_LNCT_directory_index:
17769 if (uint.has_value ())
17770 fe.d_index = (dir_index) *uint;
17771 break;
17772 case DW_LNCT_timestamp:
17773 if (uint.has_value ())
17774 fe.mod_time = *uint;
17775 break;
17776 case DW_LNCT_size:
17777 if (uint.has_value ())
17778 fe.length = *uint;
17779 break;
17780 case DW_LNCT_MD5:
17781 break;
17782 default:
17783 complaint (&symfile_complaints,
17784 _("Unknown format content type %s"),
17785 pulongest (content_type));
17786 }
43988095
JK
17787 }
17788
ecfb656c 17789 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17790 }
17791
17792 *bufp = buf;
17793}
17794
debd256d 17795/* Read the statement program header starting at OFFSET in
3019eac3 17796 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17797 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17798 Returns NULL if there is a problem reading the header, e.g., if it
17799 has a version we don't understand.
debd256d
JB
17800
17801 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17802 the returned object point into the dwarf line section buffer,
17803 and must not be freed. */
ae2de4f8 17804
fff8551c 17805static line_header_up
9c541725 17806dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17807{
d521ce57 17808 const gdb_byte *line_ptr;
c764a876 17809 unsigned int bytes_read, offset_size;
debd256d 17810 int i;
d521ce57 17811 const char *cur_dir, *cur_file;
3019eac3
DE
17812 struct dwarf2_section_info *section;
17813 bfd *abfd;
17814
36586728 17815 section = get_debug_line_section (cu);
3019eac3
DE
17816 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17817 if (section->buffer == NULL)
debd256d 17818 {
3019eac3
DE
17819 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17820 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17821 else
17822 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17823 return 0;
17824 }
17825
fceca515
DE
17826 /* We can't do this until we know the section is non-empty.
17827 Only then do we know we have such a section. */
a32a8923 17828 abfd = get_section_bfd_owner (section);
fceca515 17829
a738430d
MK
17830 /* Make sure that at least there's room for the total_length field.
17831 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17832 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17833 {
4d3c2250 17834 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17835 return 0;
17836 }
17837
fff8551c 17838 line_header_up lh (new line_header ());
debd256d 17839
9c541725 17840 lh->sect_off = sect_off;
527f3840
JK
17841 lh->offset_in_dwz = cu->per_cu->is_dwz;
17842
9c541725 17843 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 17844
a738430d 17845 /* Read in the header. */
6e70227d 17846 lh->total_length =
c764a876
DE
17847 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17848 &bytes_read, &offset_size);
debd256d 17849 line_ptr += bytes_read;
3019eac3 17850 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17851 {
4d3c2250 17852 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17853 return 0;
17854 }
17855 lh->statement_program_end = line_ptr + lh->total_length;
17856 lh->version = read_2_bytes (abfd, line_ptr);
17857 line_ptr += 2;
43988095 17858 if (lh->version > 5)
cd366ee8
DE
17859 {
17860 /* This is a version we don't understand. The format could have
17861 changed in ways we don't handle properly so just punt. */
17862 complaint (&symfile_complaints,
17863 _("unsupported version in .debug_line section"));
17864 return NULL;
17865 }
43988095
JK
17866 if (lh->version >= 5)
17867 {
17868 gdb_byte segment_selector_size;
17869
17870 /* Skip address size. */
17871 read_1_byte (abfd, line_ptr);
17872 line_ptr += 1;
17873
17874 segment_selector_size = read_1_byte (abfd, line_ptr);
17875 line_ptr += 1;
17876 if (segment_selector_size != 0)
17877 {
17878 complaint (&symfile_complaints,
17879 _("unsupported segment selector size %u "
17880 "in .debug_line section"),
17881 segment_selector_size);
17882 return NULL;
17883 }
17884 }
c764a876
DE
17885 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17886 line_ptr += offset_size;
debd256d
JB
17887 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17888 line_ptr += 1;
2dc7f7b3
TT
17889 if (lh->version >= 4)
17890 {
17891 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17892 line_ptr += 1;
17893 }
17894 else
17895 lh->maximum_ops_per_instruction = 1;
17896
17897 if (lh->maximum_ops_per_instruction == 0)
17898 {
17899 lh->maximum_ops_per_instruction = 1;
17900 complaint (&symfile_complaints,
3e43a32a
MS
17901 _("invalid maximum_ops_per_instruction "
17902 "in `.debug_line' section"));
2dc7f7b3
TT
17903 }
17904
debd256d
JB
17905 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17906 line_ptr += 1;
17907 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17908 line_ptr += 1;
17909 lh->line_range = read_1_byte (abfd, line_ptr);
17910 line_ptr += 1;
17911 lh->opcode_base = read_1_byte (abfd, line_ptr);
17912 line_ptr += 1;
fff8551c 17913 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
17914
17915 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17916 for (i = 1; i < lh->opcode_base; ++i)
17917 {
17918 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17919 line_ptr += 1;
17920 }
17921
43988095 17922 if (lh->version >= 5)
debd256d 17923 {
43988095 17924 /* Read directory table. */
fff8551c
PA
17925 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17926 [] (struct line_header *lh, const char *name,
ecfb656c 17927 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17928 unsigned int length)
17929 {
17930 lh->add_include_dir (name);
17931 });
debd256d 17932
43988095 17933 /* Read file name table. */
fff8551c
PA
17934 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17935 [] (struct line_header *lh, const char *name,
ecfb656c 17936 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17937 unsigned int length)
17938 {
ecfb656c 17939 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 17940 });
43988095
JK
17941 }
17942 else
debd256d 17943 {
43988095
JK
17944 /* Read directory table. */
17945 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17946 {
17947 line_ptr += bytes_read;
fff8551c 17948 lh->add_include_dir (cur_dir);
43988095 17949 }
debd256d
JB
17950 line_ptr += bytes_read;
17951
43988095
JK
17952 /* Read file name table. */
17953 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17954 {
ecfb656c
PA
17955 unsigned int mod_time, length;
17956 dir_index d_index;
43988095
JK
17957
17958 line_ptr += bytes_read;
ecfb656c 17959 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
17960 line_ptr += bytes_read;
17961 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17962 line_ptr += bytes_read;
17963 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17964 line_ptr += bytes_read;
17965
ecfb656c 17966 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
17967 }
17968 line_ptr += bytes_read;
debd256d 17969 }
6e70227d 17970 lh->statement_program_start = line_ptr;
debd256d 17971
3019eac3 17972 if (line_ptr > (section->buffer + section->size))
4d3c2250 17973 complaint (&symfile_complaints,
3e43a32a
MS
17974 _("line number info header doesn't "
17975 "fit in `.debug_line' section"));
debd256d 17976
debd256d
JB
17977 return lh;
17978}
c906108c 17979
c6da4cef
DE
17980/* Subroutine of dwarf_decode_lines to simplify it.
17981 Return the file name of the psymtab for included file FILE_INDEX
17982 in line header LH of PST.
17983 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17984 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17985 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17986
17987 The function creates dangling cleanup registration. */
c6da4cef 17988
d521ce57 17989static const char *
c6da4cef
DE
17990psymtab_include_file_name (const struct line_header *lh, int file_index,
17991 const struct partial_symtab *pst,
17992 const char *comp_dir)
17993{
8c43009f 17994 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
17995 const char *include_name = fe.name;
17996 const char *include_name_to_compare = include_name;
72b9f47f
TT
17997 const char *pst_filename;
17998 char *copied_name = NULL;
c6da4cef
DE
17999 int file_is_pst;
18000
8c43009f 18001 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18002
18003 if (!IS_ABSOLUTE_PATH (include_name)
18004 && (dir_name != NULL || comp_dir != NULL))
18005 {
18006 /* Avoid creating a duplicate psymtab for PST.
18007 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18008 Before we do the comparison, however, we need to account
18009 for DIR_NAME and COMP_DIR.
18010 First prepend dir_name (if non-NULL). If we still don't
18011 have an absolute path prepend comp_dir (if non-NULL).
18012 However, the directory we record in the include-file's
18013 psymtab does not contain COMP_DIR (to match the
18014 corresponding symtab(s)).
18015
18016 Example:
18017
18018 bash$ cd /tmp
18019 bash$ gcc -g ./hello.c
18020 include_name = "hello.c"
18021 dir_name = "."
18022 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18023 DW_AT_name = "./hello.c"
18024
18025 */
c6da4cef
DE
18026
18027 if (dir_name != NULL)
18028 {
d521ce57
TT
18029 char *tem = concat (dir_name, SLASH_STRING,
18030 include_name, (char *)NULL);
18031
18032 make_cleanup (xfree, tem);
18033 include_name = tem;
c6da4cef 18034 include_name_to_compare = include_name;
c6da4cef
DE
18035 }
18036 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18037 {
d521ce57
TT
18038 char *tem = concat (comp_dir, SLASH_STRING,
18039 include_name, (char *)NULL);
18040
18041 make_cleanup (xfree, tem);
18042 include_name_to_compare = tem;
c6da4cef
DE
18043 }
18044 }
18045
18046 pst_filename = pst->filename;
18047 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18048 {
72b9f47f
TT
18049 copied_name = concat (pst->dirname, SLASH_STRING,
18050 pst_filename, (char *)NULL);
18051 pst_filename = copied_name;
c6da4cef
DE
18052 }
18053
1e3fad37 18054 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18055
72b9f47f
TT
18056 if (copied_name != NULL)
18057 xfree (copied_name);
c6da4cef
DE
18058
18059 if (file_is_pst)
18060 return NULL;
18061 return include_name;
18062}
18063
d9b3de22
DE
18064/* State machine to track the state of the line number program. */
18065
6f77053d 18066class lnp_state_machine
d9b3de22 18067{
6f77053d
PA
18068public:
18069 /* Initialize a machine state for the start of a line number
18070 program. */
18071 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18072
8c43009f
PA
18073 file_entry *current_file ()
18074 {
18075 /* lh->file_names is 0-based, but the file name numbers in the
18076 statement program are 1-based. */
6f77053d
PA
18077 return m_line_header->file_name_at (m_file);
18078 }
18079
18080 /* Record the line in the state machine. END_SEQUENCE is true if
18081 we're processing the end of a sequence. */
18082 void record_line (bool end_sequence);
18083
18084 /* Check address and if invalid nop-out the rest of the lines in this
18085 sequence. */
18086 void check_line_address (struct dwarf2_cu *cu,
18087 const gdb_byte *line_ptr,
18088 CORE_ADDR lowpc, CORE_ADDR address);
18089
18090 void handle_set_discriminator (unsigned int discriminator)
18091 {
18092 m_discriminator = discriminator;
18093 m_line_has_non_zero_discriminator |= discriminator != 0;
18094 }
18095
18096 /* Handle DW_LNE_set_address. */
18097 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18098 {
18099 m_op_index = 0;
18100 address += baseaddr;
18101 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18102 }
18103
18104 /* Handle DW_LNS_advance_pc. */
18105 void handle_advance_pc (CORE_ADDR adjust);
18106
18107 /* Handle a special opcode. */
18108 void handle_special_opcode (unsigned char op_code);
18109
18110 /* Handle DW_LNS_advance_line. */
18111 void handle_advance_line (int line_delta)
18112 {
18113 advance_line (line_delta);
18114 }
18115
18116 /* Handle DW_LNS_set_file. */
18117 void handle_set_file (file_name_index file);
18118
18119 /* Handle DW_LNS_negate_stmt. */
18120 void handle_negate_stmt ()
18121 {
18122 m_is_stmt = !m_is_stmt;
18123 }
18124
18125 /* Handle DW_LNS_const_add_pc. */
18126 void handle_const_add_pc ();
18127
18128 /* Handle DW_LNS_fixed_advance_pc. */
18129 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18130 {
18131 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18132 m_op_index = 0;
18133 }
18134
18135 /* Handle DW_LNS_copy. */
18136 void handle_copy ()
18137 {
18138 record_line (false);
18139 m_discriminator = 0;
18140 }
18141
18142 /* Handle DW_LNE_end_sequence. */
18143 void handle_end_sequence ()
18144 {
18145 m_record_line_callback = ::record_line;
18146 }
18147
18148private:
18149 /* Advance the line by LINE_DELTA. */
18150 void advance_line (int line_delta)
18151 {
18152 m_line += line_delta;
18153
18154 if (line_delta != 0)
18155 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18156 }
18157
6f77053d
PA
18158 gdbarch *m_gdbarch;
18159
18160 /* True if we're recording lines.
18161 Otherwise we're building partial symtabs and are just interested in
18162 finding include files mentioned by the line number program. */
18163 bool m_record_lines_p;
18164
8c43009f 18165 /* The line number header. */
6f77053d 18166 line_header *m_line_header;
8c43009f 18167
6f77053d
PA
18168 /* These are part of the standard DWARF line number state machine,
18169 and initialized according to the DWARF spec. */
d9b3de22 18170
6f77053d 18171 unsigned char m_op_index = 0;
8c43009f 18172 /* The line table index (1-based) of the current file. */
6f77053d
PA
18173 file_name_index m_file = (file_name_index) 1;
18174 unsigned int m_line = 1;
18175
18176 /* These are initialized in the constructor. */
18177
18178 CORE_ADDR m_address;
18179 bool m_is_stmt;
18180 unsigned int m_discriminator;
d9b3de22
DE
18181
18182 /* Additional bits of state we need to track. */
18183
18184 /* The last file that we called dwarf2_start_subfile for.
18185 This is only used for TLLs. */
6f77053d 18186 unsigned int m_last_file = 0;
d9b3de22 18187 /* The last file a line number was recorded for. */
6f77053d 18188 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18189
18190 /* The function to call to record a line. */
6f77053d 18191 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18192
18193 /* The last line number that was recorded, used to coalesce
18194 consecutive entries for the same line. This can happen, for
18195 example, when discriminators are present. PR 17276. */
6f77053d
PA
18196 unsigned int m_last_line = 0;
18197 bool m_line_has_non_zero_discriminator = false;
8c43009f 18198};
d9b3de22 18199
6f77053d
PA
18200void
18201lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18202{
18203 CORE_ADDR addr_adj = (((m_op_index + adjust)
18204 / m_line_header->maximum_ops_per_instruction)
18205 * m_line_header->minimum_instruction_length);
18206 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18207 m_op_index = ((m_op_index + adjust)
18208 % m_line_header->maximum_ops_per_instruction);
18209}
d9b3de22 18210
6f77053d
PA
18211void
18212lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18213{
6f77053d
PA
18214 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18215 CORE_ADDR addr_adj = (((m_op_index
18216 + (adj_opcode / m_line_header->line_range))
18217 / m_line_header->maximum_ops_per_instruction)
18218 * m_line_header->minimum_instruction_length);
18219 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18220 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18221 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18222
6f77053d
PA
18223 int line_delta = (m_line_header->line_base
18224 + (adj_opcode % m_line_header->line_range));
18225 advance_line (line_delta);
18226 record_line (false);
18227 m_discriminator = 0;
18228}
d9b3de22 18229
6f77053d
PA
18230void
18231lnp_state_machine::handle_set_file (file_name_index file)
18232{
18233 m_file = file;
18234
18235 const file_entry *fe = current_file ();
18236 if (fe == NULL)
18237 dwarf2_debug_line_missing_file_complaint ();
18238 else if (m_record_lines_p)
18239 {
18240 const char *dir = fe->include_dir (m_line_header);
18241
18242 m_last_subfile = current_subfile;
18243 m_line_has_non_zero_discriminator = m_discriminator != 0;
18244 dwarf2_start_subfile (fe->name, dir);
18245 }
18246}
18247
18248void
18249lnp_state_machine::handle_const_add_pc ()
18250{
18251 CORE_ADDR adjust
18252 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18253
18254 CORE_ADDR addr_adj
18255 = (((m_op_index + adjust)
18256 / m_line_header->maximum_ops_per_instruction)
18257 * m_line_header->minimum_instruction_length);
18258
18259 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18260 m_op_index = ((m_op_index + adjust)
18261 % m_line_header->maximum_ops_per_instruction);
18262}
d9b3de22 18263
c91513d8
PP
18264/* Ignore this record_line request. */
18265
18266static void
18267noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18268{
18269 return;
18270}
18271
a05a36a5
DE
18272/* Return non-zero if we should add LINE to the line number table.
18273 LINE is the line to add, LAST_LINE is the last line that was added,
18274 LAST_SUBFILE is the subfile for LAST_LINE.
18275 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18276 had a non-zero discriminator.
18277
18278 We have to be careful in the presence of discriminators.
18279 E.g., for this line:
18280
18281 for (i = 0; i < 100000; i++);
18282
18283 clang can emit four line number entries for that one line,
18284 each with a different discriminator.
18285 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18286
18287 However, we want gdb to coalesce all four entries into one.
18288 Otherwise the user could stepi into the middle of the line and
18289 gdb would get confused about whether the pc really was in the
18290 middle of the line.
18291
18292 Things are further complicated by the fact that two consecutive
18293 line number entries for the same line is a heuristic used by gcc
18294 to denote the end of the prologue. So we can't just discard duplicate
18295 entries, we have to be selective about it. The heuristic we use is
18296 that we only collapse consecutive entries for the same line if at least
18297 one of those entries has a non-zero discriminator. PR 17276.
18298
18299 Note: Addresses in the line number state machine can never go backwards
18300 within one sequence, thus this coalescing is ok. */
18301
18302static int
18303dwarf_record_line_p (unsigned int line, unsigned int last_line,
18304 int line_has_non_zero_discriminator,
18305 struct subfile *last_subfile)
18306{
18307 if (current_subfile != last_subfile)
18308 return 1;
18309 if (line != last_line)
18310 return 1;
18311 /* Same line for the same file that we've seen already.
18312 As a last check, for pr 17276, only record the line if the line
18313 has never had a non-zero discriminator. */
18314 if (!line_has_non_zero_discriminator)
18315 return 1;
18316 return 0;
18317}
18318
252a6764
DE
18319/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18320 in the line table of subfile SUBFILE. */
18321
18322static void
d9b3de22
DE
18323dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18324 unsigned int line, CORE_ADDR address,
18325 record_line_ftype p_record_line)
252a6764
DE
18326{
18327 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18328
27e0867f
DE
18329 if (dwarf_line_debug)
18330 {
18331 fprintf_unfiltered (gdb_stdlog,
18332 "Recording line %u, file %s, address %s\n",
18333 line, lbasename (subfile->name),
18334 paddress (gdbarch, address));
18335 }
18336
d5962de5 18337 (*p_record_line) (subfile, line, addr);
252a6764
DE
18338}
18339
18340/* Subroutine of dwarf_decode_lines_1 to simplify it.
18341 Mark the end of a set of line number records.
d9b3de22 18342 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18343 If SUBFILE is NULL the request is ignored. */
18344
18345static void
18346dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18347 CORE_ADDR address, record_line_ftype p_record_line)
18348{
27e0867f
DE
18349 if (subfile == NULL)
18350 return;
18351
18352 if (dwarf_line_debug)
18353 {
18354 fprintf_unfiltered (gdb_stdlog,
18355 "Finishing current line, file %s, address %s\n",
18356 lbasename (subfile->name),
18357 paddress (gdbarch, address));
18358 }
18359
d9b3de22
DE
18360 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18361}
18362
6f77053d
PA
18363void
18364lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18365{
d9b3de22
DE
18366 if (dwarf_line_debug)
18367 {
18368 fprintf_unfiltered (gdb_stdlog,
18369 "Processing actual line %u: file %u,"
18370 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18371 m_line, to_underlying (m_file),
18372 paddress (m_gdbarch, m_address),
18373 m_is_stmt, m_discriminator);
d9b3de22
DE
18374 }
18375
6f77053d 18376 file_entry *fe = current_file ();
8c43009f
PA
18377
18378 if (fe == NULL)
d9b3de22
DE
18379 dwarf2_debug_line_missing_file_complaint ();
18380 /* For now we ignore lines not starting on an instruction boundary.
18381 But not when processing end_sequence for compatibility with the
18382 previous version of the code. */
6f77053d 18383 else if (m_op_index == 0 || end_sequence)
d9b3de22 18384 {
8c43009f 18385 fe->included_p = 1;
6f77053d 18386 if (m_record_lines_p && m_is_stmt)
d9b3de22 18387 {
6f77053d 18388 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18389 {
6f77053d
PA
18390 dwarf_finish_line (m_gdbarch, m_last_subfile,
18391 m_address, m_record_line_callback);
d9b3de22
DE
18392 }
18393
18394 if (!end_sequence)
18395 {
6f77053d
PA
18396 if (dwarf_record_line_p (m_line, m_last_line,
18397 m_line_has_non_zero_discriminator,
18398 m_last_subfile))
d9b3de22 18399 {
6f77053d
PA
18400 dwarf_record_line_1 (m_gdbarch, current_subfile,
18401 m_line, m_address,
18402 m_record_line_callback);
d9b3de22 18403 }
6f77053d
PA
18404 m_last_subfile = current_subfile;
18405 m_last_line = m_line;
d9b3de22
DE
18406 }
18407 }
18408 }
18409}
18410
6f77053d
PA
18411lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18412 bool record_lines_p)
d9b3de22 18413{
6f77053d
PA
18414 m_gdbarch = arch;
18415 m_record_lines_p = record_lines_p;
18416 m_line_header = lh;
d9b3de22 18417
6f77053d 18418 m_record_line_callback = ::record_line;
d9b3de22 18419
d9b3de22
DE
18420 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18421 was a line entry for it so that the backend has a chance to adjust it
18422 and also record it in case it needs it. This is currently used by MIPS
18423 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18424 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18425 m_is_stmt = lh->default_is_stmt;
18426 m_discriminator = 0;
252a6764
DE
18427}
18428
6f77053d
PA
18429void
18430lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18431 const gdb_byte *line_ptr,
18432 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18433{
18434 /* If address < lowpc then it's not a usable value, it's outside the
18435 pc range of the CU. However, we restrict the test to only address
18436 values of zero to preserve GDB's previous behaviour which is to
18437 handle the specific case of a function being GC'd by the linker. */
18438
18439 if (address == 0 && address < lowpc)
18440 {
18441 /* This line table is for a function which has been
18442 GCd by the linker. Ignore it. PR gdb/12528 */
18443
18444 struct objfile *objfile = cu->objfile;
18445 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18446
18447 complaint (&symfile_complaints,
18448 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18449 line_offset, objfile_name (objfile));
6f77053d
PA
18450 m_record_line_callback = noop_record_line;
18451 /* Note: record_line_callback is left as noop_record_line until
18452 we see DW_LNE_end_sequence. */
924c2928
DE
18453 }
18454}
18455
f3f5162e 18456/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18457 Process the line number information in LH.
18458 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18459 program in order to set included_p for every referenced header. */
debd256d 18460
c906108c 18461static void
43f3e411
DE
18462dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18463 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18464{
d521ce57
TT
18465 const gdb_byte *line_ptr, *extended_end;
18466 const gdb_byte *line_end;
a8c50c1f 18467 unsigned int bytes_read, extended_len;
699ca60a 18468 unsigned char op_code, extended_op;
e142c38c
DJ
18469 CORE_ADDR baseaddr;
18470 struct objfile *objfile = cu->objfile;
f3f5162e 18471 bfd *abfd = objfile->obfd;
fbf65064 18472 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18473 /* True if we're recording line info (as opposed to building partial
18474 symtabs and just interested in finding include files mentioned by
18475 the line number program). */
18476 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18477
18478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18479
debd256d
JB
18480 line_ptr = lh->statement_program_start;
18481 line_end = lh->statement_program_end;
c906108c
SS
18482
18483 /* Read the statement sequences until there's nothing left. */
18484 while (line_ptr < line_end)
18485 {
6f77053d
PA
18486 /* The DWARF line number program state machine. Reset the state
18487 machine at the start of each sequence. */
18488 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18489 bool end_sequence = false;
d9b3de22 18490
8c43009f 18491 if (record_lines_p)
c906108c 18492 {
8c43009f
PA
18493 /* Start a subfile for the current file of the state
18494 machine. */
18495 const file_entry *fe = state_machine.current_file ();
18496
18497 if (fe != NULL)
18498 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18499 }
18500
a738430d 18501 /* Decode the table. */
d9b3de22 18502 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18503 {
18504 op_code = read_1_byte (abfd, line_ptr);
18505 line_ptr += 1;
9aa1fe7e 18506
debd256d 18507 if (op_code >= lh->opcode_base)
6e70227d 18508 {
8e07a239 18509 /* Special opcode. */
6f77053d 18510 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18511 }
18512 else switch (op_code)
c906108c
SS
18513 {
18514 case DW_LNS_extended_op:
3e43a32a
MS
18515 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18516 &bytes_read);
473b7be6 18517 line_ptr += bytes_read;
a8c50c1f 18518 extended_end = line_ptr + extended_len;
c906108c
SS
18519 extended_op = read_1_byte (abfd, line_ptr);
18520 line_ptr += 1;
18521 switch (extended_op)
18522 {
18523 case DW_LNE_end_sequence:
6f77053d
PA
18524 state_machine.handle_end_sequence ();
18525 end_sequence = true;
c906108c
SS
18526 break;
18527 case DW_LNE_set_address:
d9b3de22
DE
18528 {
18529 CORE_ADDR address
18530 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18531 line_ptr += bytes_read;
6f77053d
PA
18532
18533 state_machine.check_line_address (cu, line_ptr,
18534 lowpc, address);
18535 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18536 }
c906108c
SS
18537 break;
18538 case DW_LNE_define_file:
debd256d 18539 {
d521ce57 18540 const char *cur_file;
ecfb656c
PA
18541 unsigned int mod_time, length;
18542 dir_index dindex;
6e70227d 18543
3e43a32a
MS
18544 cur_file = read_direct_string (abfd, line_ptr,
18545 &bytes_read);
debd256d 18546 line_ptr += bytes_read;
ecfb656c 18547 dindex = (dir_index)
debd256d
JB
18548 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18549 line_ptr += bytes_read;
18550 mod_time =
18551 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18552 line_ptr += bytes_read;
18553 length =
18554 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18555 line_ptr += bytes_read;
ecfb656c 18556 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18557 }
c906108c 18558 break;
d0c6ba3d 18559 case DW_LNE_set_discriminator:
6f77053d
PA
18560 {
18561 /* The discriminator is not interesting to the
18562 debugger; just ignore it. We still need to
18563 check its value though:
18564 if there are consecutive entries for the same
18565 (non-prologue) line we want to coalesce them.
18566 PR 17276. */
18567 unsigned int discr
18568 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18569 line_ptr += bytes_read;
18570
18571 state_machine.handle_set_discriminator (discr);
18572 }
d0c6ba3d 18573 break;
c906108c 18574 default:
4d3c2250 18575 complaint (&symfile_complaints,
e2e0b3e5 18576 _("mangled .debug_line section"));
debd256d 18577 return;
c906108c 18578 }
a8c50c1f
DJ
18579 /* Make sure that we parsed the extended op correctly. If e.g.
18580 we expected a different address size than the producer used,
18581 we may have read the wrong number of bytes. */
18582 if (line_ptr != extended_end)
18583 {
18584 complaint (&symfile_complaints,
18585 _("mangled .debug_line section"));
18586 return;
18587 }
c906108c
SS
18588 break;
18589 case DW_LNS_copy:
6f77053d 18590 state_machine.handle_copy ();
c906108c
SS
18591 break;
18592 case DW_LNS_advance_pc:
2dc7f7b3
TT
18593 {
18594 CORE_ADDR adjust
18595 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18596 line_ptr += bytes_read;
6f77053d
PA
18597
18598 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18599 }
c906108c
SS
18600 break;
18601 case DW_LNS_advance_line:
a05a36a5
DE
18602 {
18603 int line_delta
18604 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18605 line_ptr += bytes_read;
6f77053d
PA
18606
18607 state_machine.handle_advance_line (line_delta);
a05a36a5 18608 }
c906108c
SS
18609 break;
18610 case DW_LNS_set_file:
d9b3de22 18611 {
6f77053d 18612 file_name_index file
ecfb656c
PA
18613 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18614 &bytes_read);
d9b3de22 18615 line_ptr += bytes_read;
8c43009f 18616
6f77053d 18617 state_machine.handle_set_file (file);
d9b3de22 18618 }
c906108c
SS
18619 break;
18620 case DW_LNS_set_column:
0ad93d4f 18621 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18622 line_ptr += bytes_read;
18623 break;
18624 case DW_LNS_negate_stmt:
6f77053d 18625 state_machine.handle_negate_stmt ();
c906108c
SS
18626 break;
18627 case DW_LNS_set_basic_block:
c906108c 18628 break;
c2c6d25f
JM
18629 /* Add to the address register of the state machine the
18630 address increment value corresponding to special opcode
a738430d
MK
18631 255. I.e., this value is scaled by the minimum
18632 instruction length since special opcode 255 would have
b021a221 18633 scaled the increment. */
c906108c 18634 case DW_LNS_const_add_pc:
6f77053d 18635 state_machine.handle_const_add_pc ();
c906108c
SS
18636 break;
18637 case DW_LNS_fixed_advance_pc:
3e29f34a 18638 {
6f77053d 18639 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18640 line_ptr += 2;
6f77053d
PA
18641
18642 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18643 }
c906108c 18644 break;
9aa1fe7e 18645 default:
a738430d
MK
18646 {
18647 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18648 int i;
a738430d 18649
debd256d 18650 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18651 {
18652 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18653 line_ptr += bytes_read;
18654 }
18655 }
c906108c
SS
18656 }
18657 }
d9b3de22
DE
18658
18659 if (!end_sequence)
18660 dwarf2_debug_line_missing_end_sequence_complaint ();
18661
18662 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18663 in which case we still finish recording the last line). */
6f77053d 18664 state_machine.record_line (true);
c906108c 18665 }
f3f5162e
DE
18666}
18667
18668/* Decode the Line Number Program (LNP) for the given line_header
18669 structure and CU. The actual information extracted and the type
18670 of structures created from the LNP depends on the value of PST.
18671
18672 1. If PST is NULL, then this procedure uses the data from the program
18673 to create all necessary symbol tables, and their linetables.
18674
18675 2. If PST is not NULL, this procedure reads the program to determine
18676 the list of files included by the unit represented by PST, and
18677 builds all the associated partial symbol tables.
18678
18679 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18680 It is used for relative paths in the line table.
18681 NOTE: When processing partial symtabs (pst != NULL),
18682 comp_dir == pst->dirname.
18683
18684 NOTE: It is important that psymtabs have the same file name (via strcmp)
18685 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18686 symtab we don't use it in the name of the psymtabs we create.
18687 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18688 A good testcase for this is mb-inline.exp.
18689
527f3840
JK
18690 LOWPC is the lowest address in CU (or 0 if not known).
18691
18692 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18693 for its PC<->lines mapping information. Otherwise only the filename
18694 table is read in. */
f3f5162e
DE
18695
18696static void
18697dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18698 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18699 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18700{
18701 struct objfile *objfile = cu->objfile;
18702 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18703
527f3840
JK
18704 if (decode_mapping)
18705 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18706
18707 if (decode_for_pst_p)
18708 {
18709 int file_index;
18710
18711 /* Now that we're done scanning the Line Header Program, we can
18712 create the psymtab of each included file. */
fff8551c 18713 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18714 if (lh->file_names[file_index].included_p == 1)
18715 {
d521ce57 18716 const char *include_name =
c6da4cef
DE
18717 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18718 if (include_name != NULL)
aaa75496
JB
18719 dwarf2_create_include_psymtab (include_name, pst, objfile);
18720 }
18721 }
cb1df416
DJ
18722 else
18723 {
18724 /* Make sure a symtab is created for every file, even files
18725 which contain only variables (i.e. no code with associated
18726 line numbers). */
43f3e411 18727 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18728 int i;
cb1df416 18729
fff8551c 18730 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18731 {
8c43009f 18732 file_entry &fe = lh->file_names[i];
9a619af0 18733
8c43009f 18734 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18735
cb1df416 18736 if (current_subfile->symtab == NULL)
43f3e411
DE
18737 {
18738 current_subfile->symtab
18739 = allocate_symtab (cust, current_subfile->name);
18740 }
8c43009f 18741 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18742 }
18743 }
c906108c
SS
18744}
18745
18746/* Start a subfile for DWARF. FILENAME is the name of the file and
18747 DIRNAME the name of the source directory which contains FILENAME
4d663531 18748 or NULL if not known.
c906108c
SS
18749 This routine tries to keep line numbers from identical absolute and
18750 relative file names in a common subfile.
18751
18752 Using the `list' example from the GDB testsuite, which resides in
18753 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18754 of /srcdir/list0.c yields the following debugging information for list0.c:
18755
c5aa993b 18756 DW_AT_name: /srcdir/list0.c
4d663531 18757 DW_AT_comp_dir: /compdir
357e46e7 18758 files.files[0].name: list0.h
c5aa993b 18759 files.files[0].dir: /srcdir
357e46e7 18760 files.files[1].name: list0.c
c5aa993b 18761 files.files[1].dir: /srcdir
c906108c
SS
18762
18763 The line number information for list0.c has to end up in a single
4f1520fb
FR
18764 subfile, so that `break /srcdir/list0.c:1' works as expected.
18765 start_subfile will ensure that this happens provided that we pass the
18766 concatenation of files.files[1].dir and files.files[1].name as the
18767 subfile's name. */
c906108c
SS
18768
18769static void
4d663531 18770dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18771{
d521ce57 18772 char *copy = NULL;
4f1520fb 18773
4d663531 18774 /* In order not to lose the line information directory,
4f1520fb
FR
18775 we concatenate it to the filename when it makes sense.
18776 Note that the Dwarf3 standard says (speaking of filenames in line
18777 information): ``The directory index is ignored for file names
18778 that represent full path names''. Thus ignoring dirname in the
18779 `else' branch below isn't an issue. */
c906108c 18780
d5166ae1 18781 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18782 {
18783 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18784 filename = copy;
18785 }
c906108c 18786
4d663531 18787 start_subfile (filename);
4f1520fb 18788
d521ce57
TT
18789 if (copy != NULL)
18790 xfree (copy);
c906108c
SS
18791}
18792
f4dc4d17
DE
18793/* Start a symtab for DWARF.
18794 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18795
43f3e411 18796static struct compunit_symtab *
f4dc4d17 18797dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18798 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18799{
43f3e411
DE
18800 struct compunit_symtab *cust
18801 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18802
f4dc4d17
DE
18803 record_debugformat ("DWARF 2");
18804 record_producer (cu->producer);
18805
18806 /* We assume that we're processing GCC output. */
18807 processing_gcc_compilation = 2;
18808
4d4ec4e5 18809 cu->processing_has_namespace_info = 0;
43f3e411
DE
18810
18811 return cust;
f4dc4d17
DE
18812}
18813
4c2df51b
DJ
18814static void
18815var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18816 struct dwarf2_cu *cu)
4c2df51b 18817{
e7c27a73
DJ
18818 struct objfile *objfile = cu->objfile;
18819 struct comp_unit_head *cu_header = &cu->header;
18820
4c2df51b
DJ
18821 /* NOTE drow/2003-01-30: There used to be a comment and some special
18822 code here to turn a symbol with DW_AT_external and a
18823 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18824 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18825 with some versions of binutils) where shared libraries could have
18826 relocations against symbols in their debug information - the
18827 minimal symbol would have the right address, but the debug info
18828 would not. It's no longer necessary, because we will explicitly
18829 apply relocations when we read in the debug information now. */
18830
18831 /* A DW_AT_location attribute with no contents indicates that a
18832 variable has been optimized away. */
18833 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18834 {
f1e6e072 18835 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18836 return;
18837 }
18838
18839 /* Handle one degenerate form of location expression specially, to
18840 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18841 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18842 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18843
18844 if (attr_form_is_block (attr)
3019eac3
DE
18845 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18846 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18847 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18848 && (DW_BLOCK (attr)->size
18849 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18850 {
891d2f0b 18851 unsigned int dummy;
4c2df51b 18852
3019eac3
DE
18853 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18854 SYMBOL_VALUE_ADDRESS (sym) =
18855 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18856 else
18857 SYMBOL_VALUE_ADDRESS (sym) =
18858 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18859 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18860 fixup_symbol_section (sym, objfile);
18861 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18862 SYMBOL_SECTION (sym));
4c2df51b
DJ
18863 return;
18864 }
18865
18866 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18867 expression evaluator, and use LOC_COMPUTED only when necessary
18868 (i.e. when the value of a register or memory location is
18869 referenced, or a thread-local block, etc.). Then again, it might
18870 not be worthwhile. I'm assuming that it isn't unless performance
18871 or memory numbers show me otherwise. */
18872
f1e6e072 18873 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18874
f1e6e072 18875 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18876 cu->has_loclist = 1;
4c2df51b
DJ
18877}
18878
c906108c
SS
18879/* Given a pointer to a DWARF information entry, figure out if we need
18880 to make a symbol table entry for it, and if so, create a new entry
18881 and return a pointer to it.
18882 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18883 used the passed type.
18884 If SPACE is not NULL, use it to hold the new symbol. If it is
18885 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18886
18887static struct symbol *
34eaf542
TT
18888new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18889 struct symbol *space)
c906108c 18890{
e7c27a73 18891 struct objfile *objfile = cu->objfile;
3e29f34a 18892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18893 struct symbol *sym = NULL;
15d034d0 18894 const char *name;
c906108c
SS
18895 struct attribute *attr = NULL;
18896 struct attribute *attr2 = NULL;
e142c38c 18897 CORE_ADDR baseaddr;
e37fd15a
SW
18898 struct pending **list_to_add = NULL;
18899
edb3359d 18900 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18901
18902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18903
94af9270 18904 name = dwarf2_name (die, cu);
c906108c
SS
18905 if (name)
18906 {
94af9270 18907 const char *linkagename;
34eaf542 18908 int suppress_add = 0;
94af9270 18909
34eaf542
TT
18910 if (space)
18911 sym = space;
18912 else
e623cf5d 18913 sym = allocate_symbol (objfile);
c906108c 18914 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18915
18916 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18917 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18918 linkagename = dwarf2_physname (name, die, cu);
18919 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18920
f55ee35c
JK
18921 /* Fortran does not have mangling standard and the mangling does differ
18922 between gfortran, iFort etc. */
18923 if (cu->language == language_fortran
b250c185 18924 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18925 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18926 dwarf2_full_name (name, die, cu),
29df156d 18927 NULL);
f55ee35c 18928
c906108c 18929 /* Default assumptions.
c5aa993b 18930 Use the passed type or decode it from the die. */
176620f1 18931 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18932 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18933 if (type != NULL)
18934 SYMBOL_TYPE (sym) = type;
18935 else
e7c27a73 18936 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18937 attr = dwarf2_attr (die,
18938 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18939 cu);
c906108c
SS
18940 if (attr)
18941 {
18942 SYMBOL_LINE (sym) = DW_UNSND (attr);
18943 }
cb1df416 18944
edb3359d
DJ
18945 attr = dwarf2_attr (die,
18946 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18947 cu);
cb1df416
DJ
18948 if (attr)
18949 {
ecfb656c 18950 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 18951 struct file_entry *fe;
9a619af0 18952
ecfb656c
PA
18953 if (cu->line_header != NULL)
18954 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
18955 else
18956 fe = NULL;
18957
18958 if (fe == NULL)
cb1df416
DJ
18959 complaint (&symfile_complaints,
18960 _("file index out of range"));
8c43009f
PA
18961 else
18962 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18963 }
18964
c906108c
SS
18965 switch (die->tag)
18966 {
18967 case DW_TAG_label:
e142c38c 18968 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18969 if (attr)
3e29f34a
MR
18970 {
18971 CORE_ADDR addr;
18972
18973 addr = attr_value_as_address (attr);
18974 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18975 SYMBOL_VALUE_ADDRESS (sym) = addr;
18976 }
0f5238ed
TT
18977 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18978 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18979 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18980 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18981 break;
18982 case DW_TAG_subprogram:
18983 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18984 finish_block. */
f1e6e072 18985 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18986 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18987 if ((attr2 && (DW_UNSND (attr2) != 0))
18988 || cu->language == language_ada)
c906108c 18989 {
2cfa0c8d
JB
18990 /* Subprograms marked external are stored as a global symbol.
18991 Ada subprograms, whether marked external or not, are always
18992 stored as a global symbol, because we want to be able to
18993 access them globally. For instance, we want to be able
18994 to break on a nested subprogram without having to
18995 specify the context. */
e37fd15a 18996 list_to_add = &global_symbols;
c906108c
SS
18997 }
18998 else
18999 {
e37fd15a 19000 list_to_add = cu->list_in_scope;
c906108c
SS
19001 }
19002 break;
edb3359d
DJ
19003 case DW_TAG_inlined_subroutine:
19004 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19005 finish_block. */
f1e6e072 19006 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19007 SYMBOL_INLINED (sym) = 1;
481860b3 19008 list_to_add = cu->list_in_scope;
edb3359d 19009 break;
34eaf542
TT
19010 case DW_TAG_template_value_param:
19011 suppress_add = 1;
19012 /* Fall through. */
72929c62 19013 case DW_TAG_constant:
c906108c 19014 case DW_TAG_variable:
254e6b9e 19015 case DW_TAG_member:
0963b4bd
MS
19016 /* Compilation with minimal debug info may result in
19017 variables with missing type entries. Change the
19018 misleading `void' type to something sensible. */
c906108c 19019 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 19020 SYMBOL_TYPE (sym)
46bf5051 19021 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 19022
e142c38c 19023 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19024 /* In the case of DW_TAG_member, we should only be called for
19025 static const members. */
19026 if (die->tag == DW_TAG_member)
19027 {
3863f96c
DE
19028 /* dwarf2_add_field uses die_is_declaration,
19029 so we do the same. */
254e6b9e
DE
19030 gdb_assert (die_is_declaration (die, cu));
19031 gdb_assert (attr);
19032 }
c906108c
SS
19033 if (attr)
19034 {
e7c27a73 19035 dwarf2_const_value (attr, sym, cu);
e142c38c 19036 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19037 if (!suppress_add)
34eaf542
TT
19038 {
19039 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19040 list_to_add = &global_symbols;
34eaf542 19041 else
e37fd15a 19042 list_to_add = cu->list_in_scope;
34eaf542 19043 }
c906108c
SS
19044 break;
19045 }
e142c38c 19046 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19047 if (attr)
19048 {
e7c27a73 19049 var_decode_location (attr, sym, cu);
e142c38c 19050 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19051
19052 /* Fortran explicitly imports any global symbols to the local
19053 scope by DW_TAG_common_block. */
19054 if (cu->language == language_fortran && die->parent
19055 && die->parent->tag == DW_TAG_common_block)
19056 attr2 = NULL;
19057
caac4577
JG
19058 if (SYMBOL_CLASS (sym) == LOC_STATIC
19059 && SYMBOL_VALUE_ADDRESS (sym) == 0
19060 && !dwarf2_per_objfile->has_section_at_zero)
19061 {
19062 /* When a static variable is eliminated by the linker,
19063 the corresponding debug information is not stripped
19064 out, but the variable address is set to null;
19065 do not add such variables into symbol table. */
19066 }
19067 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19068 {
f55ee35c
JK
19069 /* Workaround gfortran PR debug/40040 - it uses
19070 DW_AT_location for variables in -fPIC libraries which may
19071 get overriden by other libraries/executable and get
19072 a different address. Resolve it by the minimal symbol
19073 which may come from inferior's executable using copy
19074 relocation. Make this workaround only for gfortran as for
19075 other compilers GDB cannot guess the minimal symbol
19076 Fortran mangling kind. */
19077 if (cu->language == language_fortran && die->parent
19078 && die->parent->tag == DW_TAG_module
19079 && cu->producer
28586665 19080 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19081 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19082
1c809c68
TT
19083 /* A variable with DW_AT_external is never static,
19084 but it may be block-scoped. */
19085 list_to_add = (cu->list_in_scope == &file_symbols
19086 ? &global_symbols : cu->list_in_scope);
1c809c68 19087 }
c906108c 19088 else
e37fd15a 19089 list_to_add = cu->list_in_scope;
c906108c
SS
19090 }
19091 else
19092 {
19093 /* We do not know the address of this symbol.
c5aa993b
JM
19094 If it is an external symbol and we have type information
19095 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19096 The address of the variable will then be determined from
19097 the minimal symbol table whenever the variable is
19098 referenced. */
e142c38c 19099 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19100
19101 /* Fortran explicitly imports any global symbols to the local
19102 scope by DW_TAG_common_block. */
19103 if (cu->language == language_fortran && die->parent
19104 && die->parent->tag == DW_TAG_common_block)
19105 {
19106 /* SYMBOL_CLASS doesn't matter here because
19107 read_common_block is going to reset it. */
19108 if (!suppress_add)
19109 list_to_add = cu->list_in_scope;
19110 }
19111 else if (attr2 && (DW_UNSND (attr2) != 0)
19112 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19113 {
0fe7935b
DJ
19114 /* A variable with DW_AT_external is never static, but it
19115 may be block-scoped. */
19116 list_to_add = (cu->list_in_scope == &file_symbols
19117 ? &global_symbols : cu->list_in_scope);
19118
f1e6e072 19119 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19120 }
442ddf59
JK
19121 else if (!die_is_declaration (die, cu))
19122 {
19123 /* Use the default LOC_OPTIMIZED_OUT class. */
19124 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19125 if (!suppress_add)
19126 list_to_add = cu->list_in_scope;
442ddf59 19127 }
c906108c
SS
19128 }
19129 break;
19130 case DW_TAG_formal_parameter:
edb3359d
DJ
19131 /* If we are inside a function, mark this as an argument. If
19132 not, we might be looking at an argument to an inlined function
19133 when we do not have enough information to show inlined frames;
19134 pretend it's a local variable in that case so that the user can
19135 still see it. */
19136 if (context_stack_depth > 0
19137 && context_stack[context_stack_depth - 1].name != NULL)
19138 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19139 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19140 if (attr)
19141 {
e7c27a73 19142 var_decode_location (attr, sym, cu);
c906108c 19143 }
e142c38c 19144 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19145 if (attr)
19146 {
e7c27a73 19147 dwarf2_const_value (attr, sym, cu);
c906108c 19148 }
f346a30d 19149
e37fd15a 19150 list_to_add = cu->list_in_scope;
c906108c
SS
19151 break;
19152 case DW_TAG_unspecified_parameters:
19153 /* From varargs functions; gdb doesn't seem to have any
19154 interest in this information, so just ignore it for now.
19155 (FIXME?) */
19156 break;
34eaf542
TT
19157 case DW_TAG_template_type_param:
19158 suppress_add = 1;
19159 /* Fall through. */
c906108c 19160 case DW_TAG_class_type:
680b30c7 19161 case DW_TAG_interface_type:
c906108c
SS
19162 case DW_TAG_structure_type:
19163 case DW_TAG_union_type:
72019c9c 19164 case DW_TAG_set_type:
c906108c 19165 case DW_TAG_enumeration_type:
f1e6e072 19166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19167 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19168
63d06c5c 19169 {
9c37b5ae 19170 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19171 really ever be static objects: otherwise, if you try
19172 to, say, break of a class's method and you're in a file
19173 which doesn't mention that class, it won't work unless
19174 the check for all static symbols in lookup_symbol_aux
19175 saves you. See the OtherFileClass tests in
19176 gdb.c++/namespace.exp. */
19177
e37fd15a 19178 if (!suppress_add)
34eaf542 19179 {
34eaf542 19180 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19181 && cu->language == language_cplus
34eaf542 19182 ? &global_symbols : cu->list_in_scope);
63d06c5c 19183
64382290 19184 /* The semantics of C++ state that "struct foo {
9c37b5ae 19185 ... }" also defines a typedef for "foo". */
64382290 19186 if (cu->language == language_cplus
45280282 19187 || cu->language == language_ada
c44af4eb
TT
19188 || cu->language == language_d
19189 || cu->language == language_rust)
64382290
TT
19190 {
19191 /* The symbol's name is already allocated along
19192 with this objfile, so we don't need to
19193 duplicate it for the type. */
19194 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19195 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19196 }
63d06c5c
DC
19197 }
19198 }
c906108c
SS
19199 break;
19200 case DW_TAG_typedef:
f1e6e072 19201 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19202 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19203 list_to_add = cu->list_in_scope;
63d06c5c 19204 break;
c906108c 19205 case DW_TAG_base_type:
a02abb62 19206 case DW_TAG_subrange_type:
f1e6e072 19207 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19208 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19209 list_to_add = cu->list_in_scope;
c906108c
SS
19210 break;
19211 case DW_TAG_enumerator:
e142c38c 19212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19213 if (attr)
19214 {
e7c27a73 19215 dwarf2_const_value (attr, sym, cu);
c906108c 19216 }
63d06c5c
DC
19217 {
19218 /* NOTE: carlton/2003-11-10: See comment above in the
19219 DW_TAG_class_type, etc. block. */
19220
e142c38c 19221 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19222 && cu->language == language_cplus
e142c38c 19223 ? &global_symbols : cu->list_in_scope);
63d06c5c 19224 }
c906108c 19225 break;
74921315 19226 case DW_TAG_imported_declaration:
5c4e30ca 19227 case DW_TAG_namespace:
f1e6e072 19228 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19229 list_to_add = &global_symbols;
5c4e30ca 19230 break;
530e8392
KB
19231 case DW_TAG_module:
19232 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19233 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19234 list_to_add = &global_symbols;
19235 break;
4357ac6c 19236 case DW_TAG_common_block:
f1e6e072 19237 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19238 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19239 add_symbol_to_list (sym, cu->list_in_scope);
19240 break;
c906108c
SS
19241 default:
19242 /* Not a tag we recognize. Hopefully we aren't processing
19243 trash data, but since we must specifically ignore things
19244 we don't recognize, there is nothing else we should do at
0963b4bd 19245 this point. */
e2e0b3e5 19246 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19247 dwarf_tag_name (die->tag));
c906108c
SS
19248 break;
19249 }
df8a16a1 19250
e37fd15a
SW
19251 if (suppress_add)
19252 {
19253 sym->hash_next = objfile->template_symbols;
19254 objfile->template_symbols = sym;
19255 list_to_add = NULL;
19256 }
19257
19258 if (list_to_add != NULL)
19259 add_symbol_to_list (sym, list_to_add);
19260
df8a16a1
DJ
19261 /* For the benefit of old versions of GCC, check for anonymous
19262 namespaces based on the demangled name. */
4d4ec4e5 19263 if (!cu->processing_has_namespace_info
94af9270 19264 && cu->language == language_cplus)
a10964d1 19265 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19266 }
19267 return (sym);
19268}
19269
34eaf542
TT
19270/* A wrapper for new_symbol_full that always allocates a new symbol. */
19271
19272static struct symbol *
19273new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19274{
19275 return new_symbol_full (die, type, cu, NULL);
19276}
19277
98bfdba5
PA
19278/* Given an attr with a DW_FORM_dataN value in host byte order,
19279 zero-extend it as appropriate for the symbol's type. The DWARF
19280 standard (v4) is not entirely clear about the meaning of using
19281 DW_FORM_dataN for a constant with a signed type, where the type is
19282 wider than the data. The conclusion of a discussion on the DWARF
19283 list was that this is unspecified. We choose to always zero-extend
19284 because that is the interpretation long in use by GCC. */
c906108c 19285
98bfdba5 19286static gdb_byte *
ff39bb5e 19287dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19288 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19289{
e7c27a73 19290 struct objfile *objfile = cu->objfile;
e17a4113
UW
19291 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19292 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19293 LONGEST l = DW_UNSND (attr);
19294
19295 if (bits < sizeof (*value) * 8)
19296 {
19297 l &= ((LONGEST) 1 << bits) - 1;
19298 *value = l;
19299 }
19300 else if (bits == sizeof (*value) * 8)
19301 *value = l;
19302 else
19303 {
224c3ddb 19304 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19305 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19306 return bytes;
19307 }
19308
19309 return NULL;
19310}
19311
19312/* Read a constant value from an attribute. Either set *VALUE, or if
19313 the value does not fit in *VALUE, set *BYTES - either already
19314 allocated on the objfile obstack, or newly allocated on OBSTACK,
19315 or, set *BATON, if we translated the constant to a location
19316 expression. */
19317
19318static void
ff39bb5e 19319dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19320 const char *name, struct obstack *obstack,
19321 struct dwarf2_cu *cu,
d521ce57 19322 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19323 struct dwarf2_locexpr_baton **baton)
19324{
19325 struct objfile *objfile = cu->objfile;
19326 struct comp_unit_head *cu_header = &cu->header;
c906108c 19327 struct dwarf_block *blk;
98bfdba5
PA
19328 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19329 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19330
19331 *value = 0;
19332 *bytes = NULL;
19333 *baton = NULL;
c906108c
SS
19334
19335 switch (attr->form)
19336 {
19337 case DW_FORM_addr:
3019eac3 19338 case DW_FORM_GNU_addr_index:
ac56253d 19339 {
ac56253d
TT
19340 gdb_byte *data;
19341
98bfdba5
PA
19342 if (TYPE_LENGTH (type) != cu_header->addr_size)
19343 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19344 cu_header->addr_size,
98bfdba5 19345 TYPE_LENGTH (type));
ac56253d
TT
19346 /* Symbols of this form are reasonably rare, so we just
19347 piggyback on the existing location code rather than writing
19348 a new implementation of symbol_computed_ops. */
8d749320 19349 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19350 (*baton)->per_cu = cu->per_cu;
19351 gdb_assert ((*baton)->per_cu);
ac56253d 19352
98bfdba5 19353 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19354 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19355 (*baton)->data = data;
ac56253d
TT
19356
19357 data[0] = DW_OP_addr;
19358 store_unsigned_integer (&data[1], cu_header->addr_size,
19359 byte_order, DW_ADDR (attr));
19360 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19361 }
c906108c 19362 break;
4ac36638 19363 case DW_FORM_string:
93b5768b 19364 case DW_FORM_strp:
3019eac3 19365 case DW_FORM_GNU_str_index:
36586728 19366 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19367 /* DW_STRING is already allocated on the objfile obstack, point
19368 directly to it. */
d521ce57 19369 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19370 break;
c906108c
SS
19371 case DW_FORM_block1:
19372 case DW_FORM_block2:
19373 case DW_FORM_block4:
19374 case DW_FORM_block:
2dc7f7b3 19375 case DW_FORM_exprloc:
0224619f 19376 case DW_FORM_data16:
c906108c 19377 blk = DW_BLOCK (attr);
98bfdba5
PA
19378 if (TYPE_LENGTH (type) != blk->size)
19379 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19380 TYPE_LENGTH (type));
19381 *bytes = blk->data;
c906108c 19382 break;
2df3850c
JM
19383
19384 /* The DW_AT_const_value attributes are supposed to carry the
19385 symbol's value "represented as it would be on the target
19386 architecture." By the time we get here, it's already been
19387 converted to host endianness, so we just need to sign- or
19388 zero-extend it as appropriate. */
19389 case DW_FORM_data1:
3aef2284 19390 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19391 break;
c906108c 19392 case DW_FORM_data2:
3aef2284 19393 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19394 break;
c906108c 19395 case DW_FORM_data4:
3aef2284 19396 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19397 break;
c906108c 19398 case DW_FORM_data8:
3aef2284 19399 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19400 break;
19401
c906108c 19402 case DW_FORM_sdata:
98bfdba5 19403 *value = DW_SND (attr);
2df3850c
JM
19404 break;
19405
c906108c 19406 case DW_FORM_udata:
98bfdba5 19407 *value = DW_UNSND (attr);
c906108c 19408 break;
2df3850c 19409
c906108c 19410 default:
4d3c2250 19411 complaint (&symfile_complaints,
e2e0b3e5 19412 _("unsupported const value attribute form: '%s'"),
4d3c2250 19413 dwarf_form_name (attr->form));
98bfdba5 19414 *value = 0;
c906108c
SS
19415 break;
19416 }
19417}
19418
2df3850c 19419
98bfdba5
PA
19420/* Copy constant value from an attribute to a symbol. */
19421
2df3850c 19422static void
ff39bb5e 19423dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19424 struct dwarf2_cu *cu)
2df3850c 19425{
98bfdba5 19426 struct objfile *objfile = cu->objfile;
12df843f 19427 LONGEST value;
d521ce57 19428 const gdb_byte *bytes;
98bfdba5 19429 struct dwarf2_locexpr_baton *baton;
2df3850c 19430
98bfdba5
PA
19431 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19432 SYMBOL_PRINT_NAME (sym),
19433 &objfile->objfile_obstack, cu,
19434 &value, &bytes, &baton);
2df3850c 19435
98bfdba5
PA
19436 if (baton != NULL)
19437 {
98bfdba5 19438 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19439 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19440 }
19441 else if (bytes != NULL)
19442 {
19443 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19444 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19445 }
19446 else
19447 {
19448 SYMBOL_VALUE (sym) = value;
f1e6e072 19449 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19450 }
2df3850c
JM
19451}
19452
c906108c
SS
19453/* Return the type of the die in question using its DW_AT_type attribute. */
19454
19455static struct type *
e7c27a73 19456die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19457{
c906108c 19458 struct attribute *type_attr;
c906108c 19459
e142c38c 19460 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19461 if (!type_attr)
19462 {
19463 /* A missing DW_AT_type represents a void type. */
46bf5051 19464 return objfile_type (cu->objfile)->builtin_void;
c906108c 19465 }
348e048f 19466
673bfd45 19467 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19468}
19469
b4ba55a1
JB
19470/* True iff CU's producer generates GNAT Ada auxiliary information
19471 that allows to find parallel types through that information instead
19472 of having to do expensive parallel lookups by type name. */
19473
19474static int
19475need_gnat_info (struct dwarf2_cu *cu)
19476{
19477 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19478 of GNAT produces this auxiliary information, without any indication
19479 that it is produced. Part of enhancing the FSF version of GNAT
19480 to produce that information will be to put in place an indicator
19481 that we can use in order to determine whether the descriptive type
19482 info is available or not. One suggestion that has been made is
19483 to use a new attribute, attached to the CU die. For now, assume
19484 that the descriptive type info is not available. */
19485 return 0;
19486}
19487
b4ba55a1
JB
19488/* Return the auxiliary type of the die in question using its
19489 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19490 attribute is not present. */
19491
19492static struct type *
19493die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19494{
b4ba55a1 19495 struct attribute *type_attr;
b4ba55a1
JB
19496
19497 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19498 if (!type_attr)
19499 return NULL;
19500
673bfd45 19501 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19502}
19503
19504/* If DIE has a descriptive_type attribute, then set the TYPE's
19505 descriptive type accordingly. */
19506
19507static void
19508set_descriptive_type (struct type *type, struct die_info *die,
19509 struct dwarf2_cu *cu)
19510{
19511 struct type *descriptive_type = die_descriptive_type (die, cu);
19512
19513 if (descriptive_type)
19514 {
19515 ALLOCATE_GNAT_AUX_TYPE (type);
19516 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19517 }
19518}
19519
c906108c
SS
19520/* Return the containing type of the die in question using its
19521 DW_AT_containing_type attribute. */
19522
19523static struct type *
e7c27a73 19524die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19525{
c906108c 19526 struct attribute *type_attr;
c906108c 19527
e142c38c 19528 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19529 if (!type_attr)
19530 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19531 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19532
673bfd45 19533 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19534}
19535
ac9ec31b
DE
19536/* Return an error marker type to use for the ill formed type in DIE/CU. */
19537
19538static struct type *
19539build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19540{
19541 struct objfile *objfile = dwarf2_per_objfile->objfile;
19542 char *message, *saved;
19543
19544 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19545 objfile_name (objfile),
9c541725
PA
19546 to_underlying (cu->header.sect_off),
19547 to_underlying (die->sect_off));
224c3ddb
SM
19548 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19549 message, strlen (message));
ac9ec31b
DE
19550 xfree (message);
19551
19f392bc 19552 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19553}
19554
673bfd45 19555/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19556 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19557 DW_AT_containing_type.
673bfd45
DE
19558 If there is no type substitute an error marker. */
19559
c906108c 19560static struct type *
ff39bb5e 19561lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19562 struct dwarf2_cu *cu)
c906108c 19563{
bb5ed363 19564 struct objfile *objfile = cu->objfile;
f792889a
DJ
19565 struct type *this_type;
19566
ac9ec31b
DE
19567 gdb_assert (attr->name == DW_AT_type
19568 || attr->name == DW_AT_GNAT_descriptive_type
19569 || attr->name == DW_AT_containing_type);
19570
673bfd45
DE
19571 /* First see if we have it cached. */
19572
36586728
TT
19573 if (attr->form == DW_FORM_GNU_ref_alt)
19574 {
19575 struct dwarf2_per_cu_data *per_cu;
9c541725 19576 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19577
9c541725
PA
19578 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19579 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19580 }
7771576e 19581 else if (attr_form_is_ref (attr))
673bfd45 19582 {
9c541725 19583 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19584
9c541725 19585 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19586 }
55f1336d 19587 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19588 {
ac9ec31b 19589 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19590
ac9ec31b 19591 return get_signatured_type (die, signature, cu);
673bfd45
DE
19592 }
19593 else
19594 {
ac9ec31b
DE
19595 complaint (&symfile_complaints,
19596 _("Dwarf Error: Bad type attribute %s in DIE"
19597 " at 0x%x [in module %s]"),
9c541725 19598 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19599 objfile_name (objfile));
ac9ec31b 19600 return build_error_marker_type (cu, die);
673bfd45
DE
19601 }
19602
19603 /* If not cached we need to read it in. */
19604
19605 if (this_type == NULL)
19606 {
ac9ec31b 19607 struct die_info *type_die = NULL;
673bfd45
DE
19608 struct dwarf2_cu *type_cu = cu;
19609
7771576e 19610 if (attr_form_is_ref (attr))
ac9ec31b
DE
19611 type_die = follow_die_ref (die, attr, &type_cu);
19612 if (type_die == NULL)
19613 return build_error_marker_type (cu, die);
19614 /* If we find the type now, it's probably because the type came
3019eac3
DE
19615 from an inter-CU reference and the type's CU got expanded before
19616 ours. */
ac9ec31b 19617 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19618 }
19619
19620 /* If we still don't have a type use an error marker. */
19621
19622 if (this_type == NULL)
ac9ec31b 19623 return build_error_marker_type (cu, die);
673bfd45 19624
f792889a 19625 return this_type;
c906108c
SS
19626}
19627
673bfd45
DE
19628/* Return the type in DIE, CU.
19629 Returns NULL for invalid types.
19630
02142a6c 19631 This first does a lookup in die_type_hash,
673bfd45
DE
19632 and only reads the die in if necessary.
19633
19634 NOTE: This can be called when reading in partial or full symbols. */
19635
f792889a 19636static struct type *
e7c27a73 19637read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19638{
f792889a
DJ
19639 struct type *this_type;
19640
19641 this_type = get_die_type (die, cu);
19642 if (this_type)
19643 return this_type;
19644
673bfd45
DE
19645 return read_type_die_1 (die, cu);
19646}
19647
19648/* Read the type in DIE, CU.
19649 Returns NULL for invalid types. */
19650
19651static struct type *
19652read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19653{
19654 struct type *this_type = NULL;
19655
c906108c
SS
19656 switch (die->tag)
19657 {
19658 case DW_TAG_class_type:
680b30c7 19659 case DW_TAG_interface_type:
c906108c
SS
19660 case DW_TAG_structure_type:
19661 case DW_TAG_union_type:
f792889a 19662 this_type = read_structure_type (die, cu);
c906108c
SS
19663 break;
19664 case DW_TAG_enumeration_type:
f792889a 19665 this_type = read_enumeration_type (die, cu);
c906108c
SS
19666 break;
19667 case DW_TAG_subprogram:
19668 case DW_TAG_subroutine_type:
edb3359d 19669 case DW_TAG_inlined_subroutine:
f792889a 19670 this_type = read_subroutine_type (die, cu);
c906108c
SS
19671 break;
19672 case DW_TAG_array_type:
f792889a 19673 this_type = read_array_type (die, cu);
c906108c 19674 break;
72019c9c 19675 case DW_TAG_set_type:
f792889a 19676 this_type = read_set_type (die, cu);
72019c9c 19677 break;
c906108c 19678 case DW_TAG_pointer_type:
f792889a 19679 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19680 break;
19681 case DW_TAG_ptr_to_member_type:
f792889a 19682 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19683 break;
19684 case DW_TAG_reference_type:
4297a3f0
AV
19685 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19686 break;
19687 case DW_TAG_rvalue_reference_type:
19688 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19689 break;
19690 case DW_TAG_const_type:
f792889a 19691 this_type = read_tag_const_type (die, cu);
c906108c
SS
19692 break;
19693 case DW_TAG_volatile_type:
f792889a 19694 this_type = read_tag_volatile_type (die, cu);
c906108c 19695 break;
06d66ee9
TT
19696 case DW_TAG_restrict_type:
19697 this_type = read_tag_restrict_type (die, cu);
19698 break;
c906108c 19699 case DW_TAG_string_type:
f792889a 19700 this_type = read_tag_string_type (die, cu);
c906108c
SS
19701 break;
19702 case DW_TAG_typedef:
f792889a 19703 this_type = read_typedef (die, cu);
c906108c 19704 break;
a02abb62 19705 case DW_TAG_subrange_type:
f792889a 19706 this_type = read_subrange_type (die, cu);
a02abb62 19707 break;
c906108c 19708 case DW_TAG_base_type:
f792889a 19709 this_type = read_base_type (die, cu);
c906108c 19710 break;
81a17f79 19711 case DW_TAG_unspecified_type:
f792889a 19712 this_type = read_unspecified_type (die, cu);
81a17f79 19713 break;
0114d602
DJ
19714 case DW_TAG_namespace:
19715 this_type = read_namespace_type (die, cu);
19716 break;
f55ee35c
JK
19717 case DW_TAG_module:
19718 this_type = read_module_type (die, cu);
19719 break;
a2c2acaf
MW
19720 case DW_TAG_atomic_type:
19721 this_type = read_tag_atomic_type (die, cu);
19722 break;
c906108c 19723 default:
3e43a32a
MS
19724 complaint (&symfile_complaints,
19725 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19726 dwarf_tag_name (die->tag));
c906108c
SS
19727 break;
19728 }
63d06c5c 19729
f792889a 19730 return this_type;
63d06c5c
DC
19731}
19732
abc72ce4
DE
19733/* See if we can figure out if the class lives in a namespace. We do
19734 this by looking for a member function; its demangled name will
19735 contain namespace info, if there is any.
19736 Return the computed name or NULL.
19737 Space for the result is allocated on the objfile's obstack.
19738 This is the full-die version of guess_partial_die_structure_name.
19739 In this case we know DIE has no useful parent. */
19740
19741static char *
19742guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19743{
19744 struct die_info *spec_die;
19745 struct dwarf2_cu *spec_cu;
19746 struct die_info *child;
19747
19748 spec_cu = cu;
19749 spec_die = die_specification (die, &spec_cu);
19750 if (spec_die != NULL)
19751 {
19752 die = spec_die;
19753 cu = spec_cu;
19754 }
19755
19756 for (child = die->child;
19757 child != NULL;
19758 child = child->sibling)
19759 {
19760 if (child->tag == DW_TAG_subprogram)
19761 {
7d45c7c3 19762 const char *linkage_name;
abc72ce4 19763
7d45c7c3
KB
19764 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19765 if (linkage_name == NULL)
19766 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19767 cu);
19768 if (linkage_name != NULL)
abc72ce4
DE
19769 {
19770 char *actual_name
19771 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19772 linkage_name);
abc72ce4
DE
19773 char *name = NULL;
19774
19775 if (actual_name != NULL)
19776 {
15d034d0 19777 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19778
19779 if (die_name != NULL
19780 && strcmp (die_name, actual_name) != 0)
19781 {
19782 /* Strip off the class name from the full name.
19783 We want the prefix. */
19784 int die_name_len = strlen (die_name);
19785 int actual_name_len = strlen (actual_name);
19786
19787 /* Test for '::' as a sanity check. */
19788 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19789 && actual_name[actual_name_len
19790 - die_name_len - 1] == ':')
224c3ddb
SM
19791 name = (char *) obstack_copy0 (
19792 &cu->objfile->per_bfd->storage_obstack,
19793 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19794 }
19795 }
19796 xfree (actual_name);
19797 return name;
19798 }
19799 }
19800 }
19801
19802 return NULL;
19803}
19804
96408a79
SA
19805/* GCC might emit a nameless typedef that has a linkage name. Determine the
19806 prefix part in such case. See
19807 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19808
a121b7c1 19809static const char *
96408a79
SA
19810anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19811{
19812 struct attribute *attr;
e6a959d6 19813 const char *base;
96408a79
SA
19814
19815 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19816 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19817 return NULL;
19818
7d45c7c3 19819 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19820 return NULL;
19821
19822 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19823 if (attr == NULL)
19824 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19825 if (attr == NULL || DW_STRING (attr) == NULL)
19826 return NULL;
19827
19828 /* dwarf2_name had to be already called. */
19829 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19830
19831 /* Strip the base name, keep any leading namespaces/classes. */
19832 base = strrchr (DW_STRING (attr), ':');
19833 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19834 return "";
19835
224c3ddb
SM
19836 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19837 DW_STRING (attr),
19838 &base[-1] - DW_STRING (attr));
96408a79
SA
19839}
19840
fdde2d81 19841/* Return the name of the namespace/class that DIE is defined within,
0114d602 19842 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19843
0114d602
DJ
19844 For example, if we're within the method foo() in the following
19845 code:
19846
19847 namespace N {
19848 class C {
19849 void foo () {
19850 }
19851 };
19852 }
19853
19854 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19855
0d5cff50 19856static const char *
e142c38c 19857determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19858{
0114d602
DJ
19859 struct die_info *parent, *spec_die;
19860 struct dwarf2_cu *spec_cu;
19861 struct type *parent_type;
a121b7c1 19862 const char *retval;
63d06c5c 19863
9c37b5ae 19864 if (cu->language != language_cplus
c44af4eb
TT
19865 && cu->language != language_fortran && cu->language != language_d
19866 && cu->language != language_rust)
0114d602
DJ
19867 return "";
19868
96408a79
SA
19869 retval = anonymous_struct_prefix (die, cu);
19870 if (retval)
19871 return retval;
19872
0114d602
DJ
19873 /* We have to be careful in the presence of DW_AT_specification.
19874 For example, with GCC 3.4, given the code
19875
19876 namespace N {
19877 void foo() {
19878 // Definition of N::foo.
19879 }
19880 }
19881
19882 then we'll have a tree of DIEs like this:
19883
19884 1: DW_TAG_compile_unit
19885 2: DW_TAG_namespace // N
19886 3: DW_TAG_subprogram // declaration of N::foo
19887 4: DW_TAG_subprogram // definition of N::foo
19888 DW_AT_specification // refers to die #3
19889
19890 Thus, when processing die #4, we have to pretend that we're in
19891 the context of its DW_AT_specification, namely the contex of die
19892 #3. */
19893 spec_cu = cu;
19894 spec_die = die_specification (die, &spec_cu);
19895 if (spec_die == NULL)
19896 parent = die->parent;
19897 else
63d06c5c 19898 {
0114d602
DJ
19899 parent = spec_die->parent;
19900 cu = spec_cu;
63d06c5c 19901 }
0114d602
DJ
19902
19903 if (parent == NULL)
19904 return "";
98bfdba5
PA
19905 else if (parent->building_fullname)
19906 {
19907 const char *name;
19908 const char *parent_name;
19909
19910 /* It has been seen on RealView 2.2 built binaries,
19911 DW_TAG_template_type_param types actually _defined_ as
19912 children of the parent class:
19913
19914 enum E {};
19915 template class <class Enum> Class{};
19916 Class<enum E> class_e;
19917
19918 1: DW_TAG_class_type (Class)
19919 2: DW_TAG_enumeration_type (E)
19920 3: DW_TAG_enumerator (enum1:0)
19921 3: DW_TAG_enumerator (enum2:1)
19922 ...
19923 2: DW_TAG_template_type_param
19924 DW_AT_type DW_FORM_ref_udata (E)
19925
19926 Besides being broken debug info, it can put GDB into an
19927 infinite loop. Consider:
19928
19929 When we're building the full name for Class<E>, we'll start
19930 at Class, and go look over its template type parameters,
19931 finding E. We'll then try to build the full name of E, and
19932 reach here. We're now trying to build the full name of E,
19933 and look over the parent DIE for containing scope. In the
19934 broken case, if we followed the parent DIE of E, we'd again
19935 find Class, and once again go look at its template type
19936 arguments, etc., etc. Simply don't consider such parent die
19937 as source-level parent of this die (it can't be, the language
19938 doesn't allow it), and break the loop here. */
19939 name = dwarf2_name (die, cu);
19940 parent_name = dwarf2_name (parent, cu);
19941 complaint (&symfile_complaints,
19942 _("template param type '%s' defined within parent '%s'"),
19943 name ? name : "<unknown>",
19944 parent_name ? parent_name : "<unknown>");
19945 return "";
19946 }
63d06c5c 19947 else
0114d602
DJ
19948 switch (parent->tag)
19949 {
63d06c5c 19950 case DW_TAG_namespace:
0114d602 19951 parent_type = read_type_die (parent, cu);
acebe513
UW
19952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19954 Work around this problem here. */
19955 if (cu->language == language_cplus
19956 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19957 return "";
0114d602
DJ
19958 /* We give a name to even anonymous namespaces. */
19959 return TYPE_TAG_NAME (parent_type);
63d06c5c 19960 case DW_TAG_class_type:
680b30c7 19961 case DW_TAG_interface_type:
63d06c5c 19962 case DW_TAG_structure_type:
0114d602 19963 case DW_TAG_union_type:
f55ee35c 19964 case DW_TAG_module:
0114d602
DJ
19965 parent_type = read_type_die (parent, cu);
19966 if (TYPE_TAG_NAME (parent_type) != NULL)
19967 return TYPE_TAG_NAME (parent_type);
19968 else
19969 /* An anonymous structure is only allowed non-static data
19970 members; no typedefs, no member functions, et cetera.
19971 So it does not need a prefix. */
19972 return "";
abc72ce4 19973 case DW_TAG_compile_unit:
95554aad 19974 case DW_TAG_partial_unit:
abc72ce4
DE
19975 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19976 if (cu->language == language_cplus
8b70b953 19977 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19978 && die->child != NULL
19979 && (die->tag == DW_TAG_class_type
19980 || die->tag == DW_TAG_structure_type
19981 || die->tag == DW_TAG_union_type))
19982 {
19983 char *name = guess_full_die_structure_name (die, cu);
19984 if (name != NULL)
19985 return name;
19986 }
19987 return "";
3d567982
TT
19988 case DW_TAG_enumeration_type:
19989 parent_type = read_type_die (parent, cu);
19990 if (TYPE_DECLARED_CLASS (parent_type))
19991 {
19992 if (TYPE_TAG_NAME (parent_type) != NULL)
19993 return TYPE_TAG_NAME (parent_type);
19994 return "";
19995 }
19996 /* Fall through. */
63d06c5c 19997 default:
8176b9b8 19998 return determine_prefix (parent, cu);
63d06c5c 19999 }
63d06c5c
DC
20000}
20001
3e43a32a
MS
20002/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20003 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20004 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20005 an obconcat, otherwise allocate storage for the result. The CU argument is
20006 used to determine the language and hence, the appropriate separator. */
987504bb 20007
f55ee35c 20008#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20009
20010static char *
f55ee35c
JK
20011typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20012 int physname, struct dwarf2_cu *cu)
63d06c5c 20013{
f55ee35c 20014 const char *lead = "";
5c315b68 20015 const char *sep;
63d06c5c 20016
3e43a32a
MS
20017 if (suffix == NULL || suffix[0] == '\0'
20018 || prefix == NULL || prefix[0] == '\0')
987504bb 20019 sep = "";
45280282
IB
20020 else if (cu->language == language_d)
20021 {
20022 /* For D, the 'main' function could be defined in any module, but it
20023 should never be prefixed. */
20024 if (strcmp (suffix, "D main") == 0)
20025 {
20026 prefix = "";
20027 sep = "";
20028 }
20029 else
20030 sep = ".";
20031 }
f55ee35c
JK
20032 else if (cu->language == language_fortran && physname)
20033 {
20034 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20035 DW_AT_MIPS_linkage_name is preferred and used instead. */
20036
20037 lead = "__";
20038 sep = "_MOD_";
20039 }
987504bb
JJ
20040 else
20041 sep = "::";
63d06c5c 20042
6dd47d34
DE
20043 if (prefix == NULL)
20044 prefix = "";
20045 if (suffix == NULL)
20046 suffix = "";
20047
987504bb
JJ
20048 if (obs == NULL)
20049 {
3e43a32a 20050 char *retval
224c3ddb
SM
20051 = ((char *)
20052 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20053
f55ee35c
JK
20054 strcpy (retval, lead);
20055 strcat (retval, prefix);
6dd47d34
DE
20056 strcat (retval, sep);
20057 strcat (retval, suffix);
63d06c5c
DC
20058 return retval;
20059 }
987504bb
JJ
20060 else
20061 {
20062 /* We have an obstack. */
f55ee35c 20063 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20064 }
63d06c5c
DC
20065}
20066
c906108c
SS
20067/* Return sibling of die, NULL if no sibling. */
20068
f9aca02d 20069static struct die_info *
fba45db2 20070sibling_die (struct die_info *die)
c906108c 20071{
639d11d3 20072 return die->sibling;
c906108c
SS
20073}
20074
71c25dea
TT
20075/* Get name of a die, return NULL if not found. */
20076
15d034d0
TT
20077static const char *
20078dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20079 struct obstack *obstack)
20080{
20081 if (name && cu->language == language_cplus)
20082 {
2f408ecb 20083 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20084
2f408ecb 20085 if (!canon_name.empty ())
71c25dea 20086 {
2f408ecb
PA
20087 if (canon_name != name)
20088 name = (const char *) obstack_copy0 (obstack,
20089 canon_name.c_str (),
20090 canon_name.length ());
71c25dea
TT
20091 }
20092 }
20093
20094 return name;
c906108c
SS
20095}
20096
96553a0c
DE
20097/* Get name of a die, return NULL if not found.
20098 Anonymous namespaces are converted to their magic string. */
9219021c 20099
15d034d0 20100static const char *
e142c38c 20101dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20102{
20103 struct attribute *attr;
20104
e142c38c 20105 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20106 if ((!attr || !DW_STRING (attr))
96553a0c 20107 && die->tag != DW_TAG_namespace
53832f31
TT
20108 && die->tag != DW_TAG_class_type
20109 && die->tag != DW_TAG_interface_type
20110 && die->tag != DW_TAG_structure_type
20111 && die->tag != DW_TAG_union_type)
71c25dea
TT
20112 return NULL;
20113
20114 switch (die->tag)
20115 {
20116 case DW_TAG_compile_unit:
95554aad 20117 case DW_TAG_partial_unit:
71c25dea
TT
20118 /* Compilation units have a DW_AT_name that is a filename, not
20119 a source language identifier. */
20120 case DW_TAG_enumeration_type:
20121 case DW_TAG_enumerator:
20122 /* These tags always have simple identifiers already; no need
20123 to canonicalize them. */
20124 return DW_STRING (attr);
907af001 20125
96553a0c
DE
20126 case DW_TAG_namespace:
20127 if (attr != NULL && DW_STRING (attr) != NULL)
20128 return DW_STRING (attr);
20129 return CP_ANONYMOUS_NAMESPACE_STR;
20130
907af001
UW
20131 case DW_TAG_class_type:
20132 case DW_TAG_interface_type:
20133 case DW_TAG_structure_type:
20134 case DW_TAG_union_type:
20135 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20136 structures or unions. These were of the form "._%d" in GCC 4.1,
20137 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20138 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20139 if (attr && DW_STRING (attr)
61012eef
GB
20140 && (startswith (DW_STRING (attr), "._")
20141 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20142 return NULL;
53832f31
TT
20143
20144 /* GCC might emit a nameless typedef that has a linkage name. See
20145 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20146 if (!attr || DW_STRING (attr) == NULL)
20147 {
df5c6c50 20148 char *demangled = NULL;
53832f31
TT
20149
20150 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20151 if (attr == NULL)
20152 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20153
20154 if (attr == NULL || DW_STRING (attr) == NULL)
20155 return NULL;
20156
df5c6c50
JK
20157 /* Avoid demangling DW_STRING (attr) the second time on a second
20158 call for the same DIE. */
20159 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20160 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20161
20162 if (demangled)
20163 {
e6a959d6 20164 const char *base;
96408a79 20165
53832f31 20166 /* FIXME: we already did this for the partial symbol... */
34a68019 20167 DW_STRING (attr)
224c3ddb
SM
20168 = ((const char *)
20169 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20170 demangled, strlen (demangled)));
53832f31
TT
20171 DW_STRING_IS_CANONICAL (attr) = 1;
20172 xfree (demangled);
96408a79
SA
20173
20174 /* Strip any leading namespaces/classes, keep only the base name.
20175 DW_AT_name for named DIEs does not contain the prefixes. */
20176 base = strrchr (DW_STRING (attr), ':');
20177 if (base && base > DW_STRING (attr) && base[-1] == ':')
20178 return &base[1];
20179 else
20180 return DW_STRING (attr);
53832f31
TT
20181 }
20182 }
907af001
UW
20183 break;
20184
71c25dea 20185 default:
907af001
UW
20186 break;
20187 }
20188
20189 if (!DW_STRING_IS_CANONICAL (attr))
20190 {
20191 DW_STRING (attr)
20192 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20193 &cu->objfile->per_bfd->storage_obstack);
907af001 20194 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20195 }
907af001 20196 return DW_STRING (attr);
9219021c
DC
20197}
20198
20199/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20200 is none. *EXT_CU is the CU containing DIE on input, and the CU
20201 containing the return value on output. */
9219021c
DC
20202
20203static struct die_info *
f2f0e013 20204dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20205{
20206 struct attribute *attr;
9219021c 20207
f2f0e013 20208 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20209 if (attr == NULL)
20210 return NULL;
20211
f2f0e013 20212 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20213}
20214
c906108c
SS
20215/* Convert a DIE tag into its string name. */
20216
f39c6ffd 20217static const char *
aa1ee363 20218dwarf_tag_name (unsigned tag)
c906108c 20219{
f39c6ffd
TT
20220 const char *name = get_DW_TAG_name (tag);
20221
20222 if (name == NULL)
20223 return "DW_TAG_<unknown>";
20224
20225 return name;
c906108c
SS
20226}
20227
20228/* Convert a DWARF attribute code into its string name. */
20229
f39c6ffd 20230static const char *
aa1ee363 20231dwarf_attr_name (unsigned attr)
c906108c 20232{
f39c6ffd
TT
20233 const char *name;
20234
c764a876 20235#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20236 if (attr == DW_AT_MIPS_fde)
20237 return "DW_AT_MIPS_fde";
20238#else
20239 if (attr == DW_AT_HP_block_index)
20240 return "DW_AT_HP_block_index";
c764a876 20241#endif
f39c6ffd
TT
20242
20243 name = get_DW_AT_name (attr);
20244
20245 if (name == NULL)
20246 return "DW_AT_<unknown>";
20247
20248 return name;
c906108c
SS
20249}
20250
20251/* Convert a DWARF value form code into its string name. */
20252
f39c6ffd 20253static const char *
aa1ee363 20254dwarf_form_name (unsigned form)
c906108c 20255{
f39c6ffd
TT
20256 const char *name = get_DW_FORM_name (form);
20257
20258 if (name == NULL)
20259 return "DW_FORM_<unknown>";
20260
20261 return name;
c906108c
SS
20262}
20263
a121b7c1 20264static const char *
fba45db2 20265dwarf_bool_name (unsigned mybool)
c906108c
SS
20266{
20267 if (mybool)
20268 return "TRUE";
20269 else
20270 return "FALSE";
20271}
20272
20273/* Convert a DWARF type code into its string name. */
20274
f39c6ffd 20275static const char *
aa1ee363 20276dwarf_type_encoding_name (unsigned enc)
c906108c 20277{
f39c6ffd 20278 const char *name = get_DW_ATE_name (enc);
c906108c 20279
f39c6ffd
TT
20280 if (name == NULL)
20281 return "DW_ATE_<unknown>";
c906108c 20282
f39c6ffd 20283 return name;
c906108c 20284}
c906108c 20285
f9aca02d 20286static void
d97bc12b 20287dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20288{
20289 unsigned int i;
20290
d97bc12b
DE
20291 print_spaces (indent, f);
20292 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20293 dwarf_tag_name (die->tag), die->abbrev,
20294 to_underlying (die->sect_off));
d97bc12b
DE
20295
20296 if (die->parent != NULL)
20297 {
20298 print_spaces (indent, f);
20299 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20300 to_underlying (die->parent->sect_off));
d97bc12b
DE
20301 }
20302
20303 print_spaces (indent, f);
20304 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20305 dwarf_bool_name (die->child != NULL));
c906108c 20306
d97bc12b
DE
20307 print_spaces (indent, f);
20308 fprintf_unfiltered (f, " attributes:\n");
20309
c906108c
SS
20310 for (i = 0; i < die->num_attrs; ++i)
20311 {
d97bc12b
DE
20312 print_spaces (indent, f);
20313 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20314 dwarf_attr_name (die->attrs[i].name),
20315 dwarf_form_name (die->attrs[i].form));
d97bc12b 20316
c906108c
SS
20317 switch (die->attrs[i].form)
20318 {
c906108c 20319 case DW_FORM_addr:
3019eac3 20320 case DW_FORM_GNU_addr_index:
d97bc12b 20321 fprintf_unfiltered (f, "address: ");
5af949e3 20322 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20323 break;
20324 case DW_FORM_block2:
20325 case DW_FORM_block4:
20326 case DW_FORM_block:
20327 case DW_FORM_block1:
56eb65bd
SP
20328 fprintf_unfiltered (f, "block: size %s",
20329 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20330 break;
2dc7f7b3 20331 case DW_FORM_exprloc:
56eb65bd
SP
20332 fprintf_unfiltered (f, "expression: size %s",
20333 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20334 break;
0224619f
JK
20335 case DW_FORM_data16:
20336 fprintf_unfiltered (f, "constant of 16 bytes");
20337 break;
4568ecf9
DE
20338 case DW_FORM_ref_addr:
20339 fprintf_unfiltered (f, "ref address: ");
20340 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20341 break;
36586728
TT
20342 case DW_FORM_GNU_ref_alt:
20343 fprintf_unfiltered (f, "alt ref address: ");
20344 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20345 break;
10b3939b
DJ
20346 case DW_FORM_ref1:
20347 case DW_FORM_ref2:
20348 case DW_FORM_ref4:
4568ecf9
DE
20349 case DW_FORM_ref8:
20350 case DW_FORM_ref_udata:
d97bc12b 20351 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20352 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20353 break;
c906108c
SS
20354 case DW_FORM_data1:
20355 case DW_FORM_data2:
20356 case DW_FORM_data4:
ce5d95e1 20357 case DW_FORM_data8:
c906108c
SS
20358 case DW_FORM_udata:
20359 case DW_FORM_sdata:
43bbcdc2
PH
20360 fprintf_unfiltered (f, "constant: %s",
20361 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20362 break;
2dc7f7b3
TT
20363 case DW_FORM_sec_offset:
20364 fprintf_unfiltered (f, "section offset: %s",
20365 pulongest (DW_UNSND (&die->attrs[i])));
20366 break;
55f1336d 20367 case DW_FORM_ref_sig8:
ac9ec31b
DE
20368 fprintf_unfiltered (f, "signature: %s",
20369 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20370 break;
c906108c 20371 case DW_FORM_string:
4bdf3d34 20372 case DW_FORM_strp:
43988095 20373 case DW_FORM_line_strp:
3019eac3 20374 case DW_FORM_GNU_str_index:
36586728 20375 case DW_FORM_GNU_strp_alt:
8285870a 20376 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20377 DW_STRING (&die->attrs[i])
8285870a
JK
20378 ? DW_STRING (&die->attrs[i]) : "",
20379 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20380 break;
20381 case DW_FORM_flag:
20382 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20383 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20384 else
d97bc12b 20385 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20386 break;
2dc7f7b3
TT
20387 case DW_FORM_flag_present:
20388 fprintf_unfiltered (f, "flag: TRUE");
20389 break;
a8329558 20390 case DW_FORM_indirect:
0963b4bd
MS
20391 /* The reader will have reduced the indirect form to
20392 the "base form" so this form should not occur. */
3e43a32a
MS
20393 fprintf_unfiltered (f,
20394 "unexpected attribute form: DW_FORM_indirect");
a8329558 20395 break;
c906108c 20396 default:
d97bc12b 20397 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20398 die->attrs[i].form);
d97bc12b 20399 break;
c906108c 20400 }
d97bc12b 20401 fprintf_unfiltered (f, "\n");
c906108c
SS
20402 }
20403}
20404
f9aca02d 20405static void
d97bc12b 20406dump_die_for_error (struct die_info *die)
c906108c 20407{
d97bc12b
DE
20408 dump_die_shallow (gdb_stderr, 0, die);
20409}
20410
20411static void
20412dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20413{
20414 int indent = level * 4;
20415
20416 gdb_assert (die != NULL);
20417
20418 if (level >= max_level)
20419 return;
20420
20421 dump_die_shallow (f, indent, die);
20422
20423 if (die->child != NULL)
c906108c 20424 {
d97bc12b
DE
20425 print_spaces (indent, f);
20426 fprintf_unfiltered (f, " Children:");
20427 if (level + 1 < max_level)
20428 {
20429 fprintf_unfiltered (f, "\n");
20430 dump_die_1 (f, level + 1, max_level, die->child);
20431 }
20432 else
20433 {
3e43a32a
MS
20434 fprintf_unfiltered (f,
20435 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20436 }
20437 }
20438
20439 if (die->sibling != NULL && level > 0)
20440 {
20441 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20442 }
20443}
20444
d97bc12b
DE
20445/* This is called from the pdie macro in gdbinit.in.
20446 It's not static so gcc will keep a copy callable from gdb. */
20447
20448void
20449dump_die (struct die_info *die, int max_level)
20450{
20451 dump_die_1 (gdb_stdlog, 0, max_level, die);
20452}
20453
f9aca02d 20454static void
51545339 20455store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20456{
51545339 20457 void **slot;
c906108c 20458
9c541725
PA
20459 slot = htab_find_slot_with_hash (cu->die_hash, die,
20460 to_underlying (die->sect_off),
b64f50a1 20461 INSERT);
51545339
DJ
20462
20463 *slot = die;
c906108c
SS
20464}
20465
b64f50a1
JK
20466/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20467 required kind. */
20468
20469static sect_offset
ff39bb5e 20470dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20471{
7771576e 20472 if (attr_form_is_ref (attr))
9c541725 20473 return (sect_offset) DW_UNSND (attr);
93311388
DE
20474
20475 complaint (&symfile_complaints,
20476 _("unsupported die ref attribute form: '%s'"),
20477 dwarf_form_name (attr->form));
9c541725 20478 return {};
c906108c
SS
20479}
20480
43bbcdc2
PH
20481/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20482 * the value held by the attribute is not constant. */
a02abb62 20483
43bbcdc2 20484static LONGEST
ff39bb5e 20485dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20486{
20487 if (attr->form == DW_FORM_sdata)
20488 return DW_SND (attr);
20489 else if (attr->form == DW_FORM_udata
20490 || attr->form == DW_FORM_data1
20491 || attr->form == DW_FORM_data2
20492 || attr->form == DW_FORM_data4
20493 || attr->form == DW_FORM_data8)
20494 return DW_UNSND (attr);
20495 else
20496 {
0224619f 20497 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20498 complaint (&symfile_complaints,
20499 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20500 dwarf_form_name (attr->form));
20501 return default_value;
20502 }
20503}
20504
348e048f
DE
20505/* Follow reference or signature attribute ATTR of SRC_DIE.
20506 On entry *REF_CU is the CU of SRC_DIE.
20507 On exit *REF_CU is the CU of the result. */
20508
20509static struct die_info *
ff39bb5e 20510follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20511 struct dwarf2_cu **ref_cu)
20512{
20513 struct die_info *die;
20514
7771576e 20515 if (attr_form_is_ref (attr))
348e048f 20516 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20517 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20518 die = follow_die_sig (src_die, attr, ref_cu);
20519 else
20520 {
20521 dump_die_for_error (src_die);
20522 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20523 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20524 }
20525
20526 return die;
03dd20cc
DJ
20527}
20528
5c631832 20529/* Follow reference OFFSET.
673bfd45
DE
20530 On entry *REF_CU is the CU of the source die referencing OFFSET.
20531 On exit *REF_CU is the CU of the result.
20532 Returns NULL if OFFSET is invalid. */
f504f079 20533
f9aca02d 20534static struct die_info *
9c541725 20535follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20536 struct dwarf2_cu **ref_cu)
c906108c 20537{
10b3939b 20538 struct die_info temp_die;
f2f0e013 20539 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20540
348e048f
DE
20541 gdb_assert (cu->per_cu != NULL);
20542
98bfdba5
PA
20543 target_cu = cu;
20544
3019eac3 20545 if (cu->per_cu->is_debug_types)
348e048f
DE
20546 {
20547 /* .debug_types CUs cannot reference anything outside their CU.
20548 If they need to, they have to reference a signatured type via
55f1336d 20549 DW_FORM_ref_sig8. */
9c541725 20550 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20551 return NULL;
348e048f 20552 }
36586728 20553 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20554 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20555 {
20556 struct dwarf2_per_cu_data *per_cu;
9a619af0 20557
9c541725 20558 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20559 cu->objfile);
03dd20cc
DJ
20560
20561 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20562 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20563 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20564
10b3939b
DJ
20565 target_cu = per_cu->cu;
20566 }
98bfdba5
PA
20567 else if (cu->dies == NULL)
20568 {
20569 /* We're loading full DIEs during partial symbol reading. */
20570 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20571 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20572 }
c906108c 20573
f2f0e013 20574 *ref_cu = target_cu;
9c541725 20575 temp_die.sect_off = sect_off;
9a3c8263 20576 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20577 &temp_die,
20578 to_underlying (sect_off));
5c631832 20579}
10b3939b 20580
5c631832
JK
20581/* Follow reference attribute ATTR of SRC_DIE.
20582 On entry *REF_CU is the CU of SRC_DIE.
20583 On exit *REF_CU is the CU of the result. */
20584
20585static struct die_info *
ff39bb5e 20586follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20587 struct dwarf2_cu **ref_cu)
20588{
9c541725 20589 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20590 struct dwarf2_cu *cu = *ref_cu;
20591 struct die_info *die;
20592
9c541725 20593 die = follow_die_offset (sect_off,
36586728
TT
20594 (attr->form == DW_FORM_GNU_ref_alt
20595 || cu->per_cu->is_dwz),
20596 ref_cu);
5c631832
JK
20597 if (!die)
20598 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20599 "at 0x%x [in module %s]"),
9c541725 20600 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20601 objfile_name (cu->objfile));
348e048f 20602
5c631832
JK
20603 return die;
20604}
20605
9c541725 20606/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20607 Returned value is intended for DW_OP_call*. Returned
20608 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20609
20610struct dwarf2_locexpr_baton
9c541725 20611dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20612 struct dwarf2_per_cu_data *per_cu,
20613 CORE_ADDR (*get_frame_pc) (void *baton),
20614 void *baton)
5c631832 20615{
918dd910 20616 struct dwarf2_cu *cu;
5c631832
JK
20617 struct die_info *die;
20618 struct attribute *attr;
20619 struct dwarf2_locexpr_baton retval;
20620
8cf6f0b1
TT
20621 dw2_setup (per_cu->objfile);
20622
918dd910
JK
20623 if (per_cu->cu == NULL)
20624 load_cu (per_cu);
20625 cu = per_cu->cu;
cc12ce38
DE
20626 if (cu == NULL)
20627 {
20628 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20629 Instead just throw an error, not much else we can do. */
20630 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20631 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20632 }
918dd910 20633
9c541725 20634 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20635 if (!die)
20636 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20637 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20638
20639 attr = dwarf2_attr (die, DW_AT_location, cu);
20640 if (!attr)
20641 {
e103e986
JK
20642 /* DWARF: "If there is no such attribute, then there is no effect.".
20643 DATA is ignored if SIZE is 0. */
5c631832 20644
e103e986 20645 retval.data = NULL;
5c631832
JK
20646 retval.size = 0;
20647 }
8cf6f0b1
TT
20648 else if (attr_form_is_section_offset (attr))
20649 {
20650 struct dwarf2_loclist_baton loclist_baton;
20651 CORE_ADDR pc = (*get_frame_pc) (baton);
20652 size_t size;
20653
20654 fill_in_loclist_baton (cu, &loclist_baton, attr);
20655
20656 retval.data = dwarf2_find_location_expression (&loclist_baton,
20657 &size, pc);
20658 retval.size = size;
20659 }
5c631832
JK
20660 else
20661 {
20662 if (!attr_form_is_block (attr))
20663 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20664 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20665 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20666
20667 retval.data = DW_BLOCK (attr)->data;
20668 retval.size = DW_BLOCK (attr)->size;
20669 }
20670 retval.per_cu = cu->per_cu;
918dd910 20671
918dd910
JK
20672 age_cached_comp_units ();
20673
5c631832 20674 return retval;
348e048f
DE
20675}
20676
8b9737bf
TT
20677/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20678 offset. */
20679
20680struct dwarf2_locexpr_baton
20681dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20682 struct dwarf2_per_cu_data *per_cu,
20683 CORE_ADDR (*get_frame_pc) (void *baton),
20684 void *baton)
20685{
9c541725 20686 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20687
9c541725 20688 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20689}
20690
b6807d98
TT
20691/* Write a constant of a given type as target-ordered bytes into
20692 OBSTACK. */
20693
20694static const gdb_byte *
20695write_constant_as_bytes (struct obstack *obstack,
20696 enum bfd_endian byte_order,
20697 struct type *type,
20698 ULONGEST value,
20699 LONGEST *len)
20700{
20701 gdb_byte *result;
20702
20703 *len = TYPE_LENGTH (type);
224c3ddb 20704 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20705 store_unsigned_integer (result, *len, byte_order, value);
20706
20707 return result;
20708}
20709
20710/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20711 pointer to the constant bytes and set LEN to the length of the
20712 data. If memory is needed, allocate it on OBSTACK. If the DIE
20713 does not have a DW_AT_const_value, return NULL. */
20714
20715const gdb_byte *
9c541725 20716dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20717 struct dwarf2_per_cu_data *per_cu,
20718 struct obstack *obstack,
20719 LONGEST *len)
20720{
20721 struct dwarf2_cu *cu;
20722 struct die_info *die;
20723 struct attribute *attr;
20724 const gdb_byte *result = NULL;
20725 struct type *type;
20726 LONGEST value;
20727 enum bfd_endian byte_order;
20728
20729 dw2_setup (per_cu->objfile);
20730
20731 if (per_cu->cu == NULL)
20732 load_cu (per_cu);
20733 cu = per_cu->cu;
cc12ce38
DE
20734 if (cu == NULL)
20735 {
20736 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20737 Instead just throw an error, not much else we can do. */
20738 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20739 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20740 }
b6807d98 20741
9c541725 20742 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20743 if (!die)
20744 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20745 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20746
20747
20748 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20749 if (attr == NULL)
20750 return NULL;
20751
20752 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20753 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20754
20755 switch (attr->form)
20756 {
20757 case DW_FORM_addr:
20758 case DW_FORM_GNU_addr_index:
20759 {
20760 gdb_byte *tem;
20761
20762 *len = cu->header.addr_size;
224c3ddb 20763 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20764 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20765 result = tem;
20766 }
20767 break;
20768 case DW_FORM_string:
20769 case DW_FORM_strp:
20770 case DW_FORM_GNU_str_index:
20771 case DW_FORM_GNU_strp_alt:
20772 /* DW_STRING is already allocated on the objfile obstack, point
20773 directly to it. */
20774 result = (const gdb_byte *) DW_STRING (attr);
20775 *len = strlen (DW_STRING (attr));
20776 break;
20777 case DW_FORM_block1:
20778 case DW_FORM_block2:
20779 case DW_FORM_block4:
20780 case DW_FORM_block:
20781 case DW_FORM_exprloc:
0224619f 20782 case DW_FORM_data16:
b6807d98
TT
20783 result = DW_BLOCK (attr)->data;
20784 *len = DW_BLOCK (attr)->size;
20785 break;
20786
20787 /* The DW_AT_const_value attributes are supposed to carry the
20788 symbol's value "represented as it would be on the target
20789 architecture." By the time we get here, it's already been
20790 converted to host endianness, so we just need to sign- or
20791 zero-extend it as appropriate. */
20792 case DW_FORM_data1:
20793 type = die_type (die, cu);
20794 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20795 if (result == NULL)
20796 result = write_constant_as_bytes (obstack, byte_order,
20797 type, value, len);
20798 break;
20799 case DW_FORM_data2:
20800 type = die_type (die, cu);
20801 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20802 if (result == NULL)
20803 result = write_constant_as_bytes (obstack, byte_order,
20804 type, value, len);
20805 break;
20806 case DW_FORM_data4:
20807 type = die_type (die, cu);
20808 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20809 if (result == NULL)
20810 result = write_constant_as_bytes (obstack, byte_order,
20811 type, value, len);
20812 break;
20813 case DW_FORM_data8:
20814 type = die_type (die, cu);
20815 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20816 if (result == NULL)
20817 result = write_constant_as_bytes (obstack, byte_order,
20818 type, value, len);
20819 break;
20820
20821 case DW_FORM_sdata:
20822 type = die_type (die, cu);
20823 result = write_constant_as_bytes (obstack, byte_order,
20824 type, DW_SND (attr), len);
20825 break;
20826
20827 case DW_FORM_udata:
20828 type = die_type (die, cu);
20829 result = write_constant_as_bytes (obstack, byte_order,
20830 type, DW_UNSND (attr), len);
20831 break;
20832
20833 default:
20834 complaint (&symfile_complaints,
20835 _("unsupported const value attribute form: '%s'"),
20836 dwarf_form_name (attr->form));
20837 break;
20838 }
20839
20840 return result;
20841}
20842
7942e96e
AA
20843/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20844 valid type for this die is found. */
20845
20846struct type *
9c541725 20847dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
20848 struct dwarf2_per_cu_data *per_cu)
20849{
20850 struct dwarf2_cu *cu;
20851 struct die_info *die;
20852
20853 dw2_setup (per_cu->objfile);
20854
20855 if (per_cu->cu == NULL)
20856 load_cu (per_cu);
20857 cu = per_cu->cu;
20858 if (!cu)
20859 return NULL;
20860
9c541725 20861 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
20862 if (!die)
20863 return NULL;
20864
20865 return die_type (die, cu);
20866}
20867
8a9b8146
TT
20868/* Return the type of the DIE at DIE_OFFSET in the CU named by
20869 PER_CU. */
20870
20871struct type *
b64f50a1 20872dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20873 struct dwarf2_per_cu_data *per_cu)
20874{
8a9b8146 20875 dw2_setup (per_cu->objfile);
b64f50a1 20876
9c541725 20877 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 20878 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20879}
20880
ac9ec31b 20881/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20882 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20883 On exit *REF_CU is the CU of the result.
20884 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20885
20886static struct die_info *
ac9ec31b
DE
20887follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20888 struct dwarf2_cu **ref_cu)
348e048f 20889{
348e048f 20890 struct die_info temp_die;
348e048f
DE
20891 struct dwarf2_cu *sig_cu;
20892 struct die_info *die;
20893
ac9ec31b
DE
20894 /* While it might be nice to assert sig_type->type == NULL here,
20895 we can get here for DW_AT_imported_declaration where we need
20896 the DIE not the type. */
348e048f
DE
20897
20898 /* If necessary, add it to the queue and load its DIEs. */
20899
95554aad 20900 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20901 read_signatured_type (sig_type);
348e048f 20902
348e048f 20903 sig_cu = sig_type->per_cu.cu;
69d751e3 20904 gdb_assert (sig_cu != NULL);
9c541725
PA
20905 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20906 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 20907 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 20908 to_underlying (temp_die.sect_off));
348e048f
DE
20909 if (die)
20910 {
796a7ff8
DE
20911 /* For .gdb_index version 7 keep track of included TUs.
20912 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20913 if (dwarf2_per_objfile->index_table != NULL
20914 && dwarf2_per_objfile->index_table->version <= 7)
20915 {
20916 VEC_safe_push (dwarf2_per_cu_ptr,
20917 (*ref_cu)->per_cu->imported_symtabs,
20918 sig_cu->per_cu);
20919 }
20920
348e048f
DE
20921 *ref_cu = sig_cu;
20922 return die;
20923 }
20924
ac9ec31b
DE
20925 return NULL;
20926}
20927
20928/* Follow signatured type referenced by ATTR in SRC_DIE.
20929 On entry *REF_CU is the CU of SRC_DIE.
20930 On exit *REF_CU is the CU of the result.
20931 The result is the DIE of the type.
20932 If the referenced type cannot be found an error is thrown. */
20933
20934static struct die_info *
ff39bb5e 20935follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20936 struct dwarf2_cu **ref_cu)
20937{
20938 ULONGEST signature = DW_SIGNATURE (attr);
20939 struct signatured_type *sig_type;
20940 struct die_info *die;
20941
20942 gdb_assert (attr->form == DW_FORM_ref_sig8);
20943
a2ce51a0 20944 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20945 /* sig_type will be NULL if the signatured type is missing from
20946 the debug info. */
20947 if (sig_type == NULL)
20948 {
20949 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20950 " from DIE at 0x%x [in module %s]"),
9c541725 20951 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20952 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20953 }
20954
20955 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20956 if (die == NULL)
20957 {
20958 dump_die_for_error (src_die);
20959 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20960 " from DIE at 0x%x [in module %s]"),
9c541725 20961 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20962 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20963 }
20964
20965 return die;
20966}
20967
20968/* Get the type specified by SIGNATURE referenced in DIE/CU,
20969 reading in and processing the type unit if necessary. */
20970
20971static struct type *
20972get_signatured_type (struct die_info *die, ULONGEST signature,
20973 struct dwarf2_cu *cu)
20974{
20975 struct signatured_type *sig_type;
20976 struct dwarf2_cu *type_cu;
20977 struct die_info *type_die;
20978 struct type *type;
20979
a2ce51a0 20980 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20981 /* sig_type will be NULL if the signatured type is missing from
20982 the debug info. */
20983 if (sig_type == NULL)
20984 {
20985 complaint (&symfile_complaints,
20986 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20987 " from DIE at 0x%x [in module %s]"),
9c541725 20988 hex_string (signature), to_underlying (die->sect_off),
4262abfb 20989 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20990 return build_error_marker_type (cu, die);
20991 }
20992
20993 /* If we already know the type we're done. */
20994 if (sig_type->type != NULL)
20995 return sig_type->type;
20996
20997 type_cu = cu;
20998 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20999 if (type_die != NULL)
21000 {
21001 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21002 is created. This is important, for example, because for c++ classes
21003 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21004 type = read_type_die (type_die, type_cu);
21005 if (type == NULL)
21006 {
21007 complaint (&symfile_complaints,
21008 _("Dwarf Error: Cannot build signatured type %s"
21009 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21010 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21011 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21012 type = build_error_marker_type (cu, die);
21013 }
21014 }
21015 else
21016 {
21017 complaint (&symfile_complaints,
21018 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21019 " from DIE at 0x%x [in module %s]"),
9c541725 21020 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21021 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21022 type = build_error_marker_type (cu, die);
21023 }
21024 sig_type->type = type;
21025
21026 return type;
21027}
21028
21029/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21030 reading in and processing the type unit if necessary. */
21031
21032static struct type *
ff39bb5e 21033get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21034 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21035{
21036 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21037 if (attr_form_is_ref (attr))
ac9ec31b
DE
21038 {
21039 struct dwarf2_cu *type_cu = cu;
21040 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21041
21042 return read_type_die (type_die, type_cu);
21043 }
21044 else if (attr->form == DW_FORM_ref_sig8)
21045 {
21046 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21047 }
21048 else
21049 {
21050 complaint (&symfile_complaints,
21051 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21052 " at 0x%x [in module %s]"),
9c541725 21053 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21054 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21055 return build_error_marker_type (cu, die);
21056 }
348e048f
DE
21057}
21058
e5fe5e75 21059/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21060
21061static void
e5fe5e75 21062load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21063{
52dc124a 21064 struct signatured_type *sig_type;
348e048f 21065
f4dc4d17
DE
21066 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21067 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21068
6721b2ec
DE
21069 /* We have the per_cu, but we need the signatured_type.
21070 Fortunately this is an easy translation. */
21071 gdb_assert (per_cu->is_debug_types);
21072 sig_type = (struct signatured_type *) per_cu;
348e048f 21073
6721b2ec 21074 gdb_assert (per_cu->cu == NULL);
348e048f 21075
52dc124a 21076 read_signatured_type (sig_type);
348e048f 21077
6721b2ec 21078 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21079}
21080
dee91e82
DE
21081/* die_reader_func for read_signatured_type.
21082 This is identical to load_full_comp_unit_reader,
21083 but is kept separate for now. */
348e048f
DE
21084
21085static void
dee91e82 21086read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21087 const gdb_byte *info_ptr,
dee91e82
DE
21088 struct die_info *comp_unit_die,
21089 int has_children,
21090 void *data)
348e048f 21091{
dee91e82 21092 struct dwarf2_cu *cu = reader->cu;
348e048f 21093
dee91e82
DE
21094 gdb_assert (cu->die_hash == NULL);
21095 cu->die_hash =
21096 htab_create_alloc_ex (cu->header.length / 12,
21097 die_hash,
21098 die_eq,
21099 NULL,
21100 &cu->comp_unit_obstack,
21101 hashtab_obstack_allocate,
21102 dummy_obstack_deallocate);
348e048f 21103
dee91e82
DE
21104 if (has_children)
21105 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21106 &info_ptr, comp_unit_die);
21107 cu->dies = comp_unit_die;
21108 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21109
21110 /* We try not to read any attributes in this function, because not
9cdd5dbd 21111 all CUs needed for references have been loaded yet, and symbol
348e048f 21112 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21113 or we won't be able to build types correctly.
21114 Similarly, if we do not read the producer, we can not apply
21115 producer-specific interpretation. */
95554aad 21116 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21117}
348e048f 21118
3019eac3
DE
21119/* Read in a signatured type and build its CU and DIEs.
21120 If the type is a stub for the real type in a DWO file,
21121 read in the real type from the DWO file as well. */
dee91e82
DE
21122
21123static void
21124read_signatured_type (struct signatured_type *sig_type)
21125{
21126 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21127
3019eac3 21128 gdb_assert (per_cu->is_debug_types);
dee91e82 21129 gdb_assert (per_cu->cu == NULL);
348e048f 21130
f4dc4d17
DE
21131 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21132 read_signatured_type_reader, NULL);
7ee85ab1 21133 sig_type->per_cu.tu_read = 1;
c906108c
SS
21134}
21135
c906108c
SS
21136/* Decode simple location descriptions.
21137 Given a pointer to a dwarf block that defines a location, compute
21138 the location and return the value.
21139
4cecd739
DJ
21140 NOTE drow/2003-11-18: This function is called in two situations
21141 now: for the address of static or global variables (partial symbols
21142 only) and for offsets into structures which are expected to be
21143 (more or less) constant. The partial symbol case should go away,
21144 and only the constant case should remain. That will let this
21145 function complain more accurately. A few special modes are allowed
21146 without complaint for global variables (for instance, global
21147 register values and thread-local values).
c906108c
SS
21148
21149 A location description containing no operations indicates that the
4cecd739 21150 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21151 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21152 callers will only want a very basic result and this can become a
21ae7a4d
JK
21153 complaint.
21154
21155 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21156
21157static CORE_ADDR
e7c27a73 21158decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21159{
e7c27a73 21160 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21161 size_t i;
21162 size_t size = blk->size;
d521ce57 21163 const gdb_byte *data = blk->data;
21ae7a4d
JK
21164 CORE_ADDR stack[64];
21165 int stacki;
21166 unsigned int bytes_read, unsnd;
21167 gdb_byte op;
c906108c 21168
21ae7a4d
JK
21169 i = 0;
21170 stacki = 0;
21171 stack[stacki] = 0;
21172 stack[++stacki] = 0;
21173
21174 while (i < size)
21175 {
21176 op = data[i++];
21177 switch (op)
21178 {
21179 case DW_OP_lit0:
21180 case DW_OP_lit1:
21181 case DW_OP_lit2:
21182 case DW_OP_lit3:
21183 case DW_OP_lit4:
21184 case DW_OP_lit5:
21185 case DW_OP_lit6:
21186 case DW_OP_lit7:
21187 case DW_OP_lit8:
21188 case DW_OP_lit9:
21189 case DW_OP_lit10:
21190 case DW_OP_lit11:
21191 case DW_OP_lit12:
21192 case DW_OP_lit13:
21193 case DW_OP_lit14:
21194 case DW_OP_lit15:
21195 case DW_OP_lit16:
21196 case DW_OP_lit17:
21197 case DW_OP_lit18:
21198 case DW_OP_lit19:
21199 case DW_OP_lit20:
21200 case DW_OP_lit21:
21201 case DW_OP_lit22:
21202 case DW_OP_lit23:
21203 case DW_OP_lit24:
21204 case DW_OP_lit25:
21205 case DW_OP_lit26:
21206 case DW_OP_lit27:
21207 case DW_OP_lit28:
21208 case DW_OP_lit29:
21209 case DW_OP_lit30:
21210 case DW_OP_lit31:
21211 stack[++stacki] = op - DW_OP_lit0;
21212 break;
f1bea926 21213
21ae7a4d
JK
21214 case DW_OP_reg0:
21215 case DW_OP_reg1:
21216 case DW_OP_reg2:
21217 case DW_OP_reg3:
21218 case DW_OP_reg4:
21219 case DW_OP_reg5:
21220 case DW_OP_reg6:
21221 case DW_OP_reg7:
21222 case DW_OP_reg8:
21223 case DW_OP_reg9:
21224 case DW_OP_reg10:
21225 case DW_OP_reg11:
21226 case DW_OP_reg12:
21227 case DW_OP_reg13:
21228 case DW_OP_reg14:
21229 case DW_OP_reg15:
21230 case DW_OP_reg16:
21231 case DW_OP_reg17:
21232 case DW_OP_reg18:
21233 case DW_OP_reg19:
21234 case DW_OP_reg20:
21235 case DW_OP_reg21:
21236 case DW_OP_reg22:
21237 case DW_OP_reg23:
21238 case DW_OP_reg24:
21239 case DW_OP_reg25:
21240 case DW_OP_reg26:
21241 case DW_OP_reg27:
21242 case DW_OP_reg28:
21243 case DW_OP_reg29:
21244 case DW_OP_reg30:
21245 case DW_OP_reg31:
21246 stack[++stacki] = op - DW_OP_reg0;
21247 if (i < size)
21248 dwarf2_complex_location_expr_complaint ();
21249 break;
c906108c 21250
21ae7a4d
JK
21251 case DW_OP_regx:
21252 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21253 i += bytes_read;
21254 stack[++stacki] = unsnd;
21255 if (i < size)
21256 dwarf2_complex_location_expr_complaint ();
21257 break;
c906108c 21258
21ae7a4d
JK
21259 case DW_OP_addr:
21260 stack[++stacki] = read_address (objfile->obfd, &data[i],
21261 cu, &bytes_read);
21262 i += bytes_read;
21263 break;
d53d4ac5 21264
21ae7a4d
JK
21265 case DW_OP_const1u:
21266 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21267 i += 1;
21268 break;
21269
21270 case DW_OP_const1s:
21271 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21272 i += 1;
21273 break;
21274
21275 case DW_OP_const2u:
21276 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21277 i += 2;
21278 break;
21279
21280 case DW_OP_const2s:
21281 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21282 i += 2;
21283 break;
d53d4ac5 21284
21ae7a4d
JK
21285 case DW_OP_const4u:
21286 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21287 i += 4;
21288 break;
21289
21290 case DW_OP_const4s:
21291 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21292 i += 4;
21293 break;
21294
585861ea
JK
21295 case DW_OP_const8u:
21296 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21297 i += 8;
21298 break;
21299
21ae7a4d
JK
21300 case DW_OP_constu:
21301 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21302 &bytes_read);
21303 i += bytes_read;
21304 break;
21305
21306 case DW_OP_consts:
21307 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21308 i += bytes_read;
21309 break;
21310
21311 case DW_OP_dup:
21312 stack[stacki + 1] = stack[stacki];
21313 stacki++;
21314 break;
21315
21316 case DW_OP_plus:
21317 stack[stacki - 1] += stack[stacki];
21318 stacki--;
21319 break;
21320
21321 case DW_OP_plus_uconst:
21322 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21323 &bytes_read);
21324 i += bytes_read;
21325 break;
21326
21327 case DW_OP_minus:
21328 stack[stacki - 1] -= stack[stacki];
21329 stacki--;
21330 break;
21331
21332 case DW_OP_deref:
21333 /* If we're not the last op, then we definitely can't encode
21334 this using GDB's address_class enum. This is valid for partial
21335 global symbols, although the variable's address will be bogus
21336 in the psymtab. */
21337 if (i < size)
21338 dwarf2_complex_location_expr_complaint ();
21339 break;
21340
21341 case DW_OP_GNU_push_tls_address:
4aa4e28b 21342 case DW_OP_form_tls_address:
21ae7a4d
JK
21343 /* The top of the stack has the offset from the beginning
21344 of the thread control block at which the variable is located. */
21345 /* Nothing should follow this operator, so the top of stack would
21346 be returned. */
21347 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21348 address will be bogus in the psymtab. Make it always at least
21349 non-zero to not look as a variable garbage collected by linker
21350 which have DW_OP_addr 0. */
21ae7a4d
JK
21351 if (i < size)
21352 dwarf2_complex_location_expr_complaint ();
585861ea 21353 stack[stacki]++;
21ae7a4d
JK
21354 break;
21355
21356 case DW_OP_GNU_uninit:
21357 break;
21358
3019eac3 21359 case DW_OP_GNU_addr_index:
49f6c839 21360 case DW_OP_GNU_const_index:
3019eac3
DE
21361 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21362 &bytes_read);
21363 i += bytes_read;
21364 break;
21365
21ae7a4d
JK
21366 default:
21367 {
f39c6ffd 21368 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21369
21370 if (name)
21371 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21372 name);
21373 else
21374 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21375 op);
21376 }
21377
21378 return (stack[stacki]);
d53d4ac5 21379 }
3c6e0cb3 21380
21ae7a4d
JK
21381 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21382 outside of the allocated space. Also enforce minimum>0. */
21383 if (stacki >= ARRAY_SIZE (stack) - 1)
21384 {
21385 complaint (&symfile_complaints,
21386 _("location description stack overflow"));
21387 return 0;
21388 }
21389
21390 if (stacki <= 0)
21391 {
21392 complaint (&symfile_complaints,
21393 _("location description stack underflow"));
21394 return 0;
21395 }
21396 }
21397 return (stack[stacki]);
c906108c
SS
21398}
21399
21400/* memory allocation interface */
21401
c906108c 21402static struct dwarf_block *
7b5a2f43 21403dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21404{
8d749320 21405 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21406}
21407
c906108c 21408static struct die_info *
b60c80d6 21409dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21410{
21411 struct die_info *die;
b60c80d6
DJ
21412 size_t size = sizeof (struct die_info);
21413
21414 if (num_attrs > 1)
21415 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21416
b60c80d6 21417 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21418 memset (die, 0, sizeof (struct die_info));
21419 return (die);
21420}
2e276125
JB
21421
21422\f
21423/* Macro support. */
21424
233d95b5
JK
21425/* Return file name relative to the compilation directory of file number I in
21426 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21427 responsible for freeing it. */
233d95b5 21428
2e276125 21429static char *
233d95b5 21430file_file_name (int file, struct line_header *lh)
2e276125 21431{
6a83a1e6
EZ
21432 /* Is the file number a valid index into the line header's file name
21433 table? Remember that file numbers start with one, not zero. */
fff8551c 21434 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21435 {
8c43009f 21436 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21437
8c43009f
PA
21438 if (!IS_ABSOLUTE_PATH (fe.name))
21439 {
21440 const char *dir = fe.include_dir (lh);
21441 if (dir != NULL)
21442 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21443 }
21444 return xstrdup (fe.name);
6a83a1e6 21445 }
2e276125
JB
21446 else
21447 {
6a83a1e6
EZ
21448 /* The compiler produced a bogus file number. We can at least
21449 record the macro definitions made in the file, even if we
21450 won't be able to find the file by name. */
21451 char fake_name[80];
9a619af0 21452
8c042590
PM
21453 xsnprintf (fake_name, sizeof (fake_name),
21454 "<bad macro file number %d>", file);
2e276125 21455
6e70227d 21456 complaint (&symfile_complaints,
6a83a1e6
EZ
21457 _("bad file number in macro information (%d)"),
21458 file);
2e276125 21459
6a83a1e6 21460 return xstrdup (fake_name);
2e276125
JB
21461 }
21462}
21463
233d95b5
JK
21464/* Return the full name of file number I in *LH's file name table.
21465 Use COMP_DIR as the name of the current directory of the
21466 compilation. The result is allocated using xmalloc; the caller is
21467 responsible for freeing it. */
21468static char *
21469file_full_name (int file, struct line_header *lh, const char *comp_dir)
21470{
21471 /* Is the file number a valid index into the line header's file name
21472 table? Remember that file numbers start with one, not zero. */
fff8551c 21473 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21474 {
21475 char *relative = file_file_name (file, lh);
21476
21477 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21478 return relative;
b36cec19
PA
21479 return reconcat (relative, comp_dir, SLASH_STRING,
21480 relative, (char *) NULL);
233d95b5
JK
21481 }
21482 else
21483 return file_file_name (file, lh);
21484}
21485
2e276125
JB
21486
21487static struct macro_source_file *
21488macro_start_file (int file, int line,
21489 struct macro_source_file *current_file,
43f3e411 21490 struct line_header *lh)
2e276125 21491{
233d95b5
JK
21492 /* File name relative to the compilation directory of this source file. */
21493 char *file_name = file_file_name (file, lh);
2e276125 21494
2e276125 21495 if (! current_file)
abc9d0dc 21496 {
fc474241
DE
21497 /* Note: We don't create a macro table for this compilation unit
21498 at all until we actually get a filename. */
43f3e411 21499 struct macro_table *macro_table = get_macro_table ();
fc474241 21500
abc9d0dc
TT
21501 /* If we have no current file, then this must be the start_file
21502 directive for the compilation unit's main source file. */
fc474241
DE
21503 current_file = macro_set_main (macro_table, file_name);
21504 macro_define_special (macro_table);
abc9d0dc 21505 }
2e276125 21506 else
233d95b5 21507 current_file = macro_include (current_file, line, file_name);
2e276125 21508
233d95b5 21509 xfree (file_name);
6e70227d 21510
2e276125
JB
21511 return current_file;
21512}
21513
21514
21515/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21516 followed by a null byte. */
21517static char *
21518copy_string (const char *buf, int len)
21519{
224c3ddb 21520 char *s = (char *) xmalloc (len + 1);
9a619af0 21521
2e276125
JB
21522 memcpy (s, buf, len);
21523 s[len] = '\0';
2e276125
JB
21524 return s;
21525}
21526
21527
21528static const char *
21529consume_improper_spaces (const char *p, const char *body)
21530{
21531 if (*p == ' ')
21532 {
4d3c2250 21533 complaint (&symfile_complaints,
3e43a32a
MS
21534 _("macro definition contains spaces "
21535 "in formal argument list:\n`%s'"),
4d3c2250 21536 body);
2e276125
JB
21537
21538 while (*p == ' ')
21539 p++;
21540 }
21541
21542 return p;
21543}
21544
21545
21546static void
21547parse_macro_definition (struct macro_source_file *file, int line,
21548 const char *body)
21549{
21550 const char *p;
21551
21552 /* The body string takes one of two forms. For object-like macro
21553 definitions, it should be:
21554
21555 <macro name> " " <definition>
21556
21557 For function-like macro definitions, it should be:
21558
21559 <macro name> "() " <definition>
21560 or
21561 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21562
21563 Spaces may appear only where explicitly indicated, and in the
21564 <definition>.
21565
21566 The Dwarf 2 spec says that an object-like macro's name is always
21567 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21568 the space when the macro's definition is the empty string.
2e276125
JB
21569
21570 The Dwarf 2 spec says that there should be no spaces between the
21571 formal arguments in a function-like macro's formal argument list,
21572 but versions of GCC around March 2002 include spaces after the
21573 commas. */
21574
21575
21576 /* Find the extent of the macro name. The macro name is terminated
21577 by either a space or null character (for an object-like macro) or
21578 an opening paren (for a function-like macro). */
21579 for (p = body; *p; p++)
21580 if (*p == ' ' || *p == '(')
21581 break;
21582
21583 if (*p == ' ' || *p == '\0')
21584 {
21585 /* It's an object-like macro. */
21586 int name_len = p - body;
21587 char *name = copy_string (body, name_len);
21588 const char *replacement;
21589
21590 if (*p == ' ')
21591 replacement = body + name_len + 1;
21592 else
21593 {
4d3c2250 21594 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21595 replacement = body + name_len;
21596 }
6e70227d 21597
2e276125
JB
21598 macro_define_object (file, line, name, replacement);
21599
21600 xfree (name);
21601 }
21602 else if (*p == '(')
21603 {
21604 /* It's a function-like macro. */
21605 char *name = copy_string (body, p - body);
21606 int argc = 0;
21607 int argv_size = 1;
8d749320 21608 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21609
21610 p++;
21611
21612 p = consume_improper_spaces (p, body);
21613
21614 /* Parse the formal argument list. */
21615 while (*p && *p != ')')
21616 {
21617 /* Find the extent of the current argument name. */
21618 const char *arg_start = p;
21619
21620 while (*p && *p != ',' && *p != ')' && *p != ' ')
21621 p++;
21622
21623 if (! *p || p == arg_start)
4d3c2250 21624 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21625 else
21626 {
21627 /* Make sure argv has room for the new argument. */
21628 if (argc >= argv_size)
21629 {
21630 argv_size *= 2;
224c3ddb 21631 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21632 }
21633
21634 argv[argc++] = copy_string (arg_start, p - arg_start);
21635 }
21636
21637 p = consume_improper_spaces (p, body);
21638
21639 /* Consume the comma, if present. */
21640 if (*p == ',')
21641 {
21642 p++;
21643
21644 p = consume_improper_spaces (p, body);
21645 }
21646 }
21647
21648 if (*p == ')')
21649 {
21650 p++;
21651
21652 if (*p == ' ')
21653 /* Perfectly formed definition, no complaints. */
21654 macro_define_function (file, line, name,
6e70227d 21655 argc, (const char **) argv,
2e276125
JB
21656 p + 1);
21657 else if (*p == '\0')
21658 {
21659 /* Complain, but do define it. */
4d3c2250 21660 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21661 macro_define_function (file, line, name,
6e70227d 21662 argc, (const char **) argv,
2e276125
JB
21663 p);
21664 }
21665 else
21666 /* Just complain. */
4d3c2250 21667 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21668 }
21669 else
21670 /* Just complain. */
4d3c2250 21671 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21672
21673 xfree (name);
21674 {
21675 int i;
21676
21677 for (i = 0; i < argc; i++)
21678 xfree (argv[i]);
21679 }
21680 xfree (argv);
21681 }
21682 else
4d3c2250 21683 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21684}
21685
cf2c3c16
TT
21686/* Skip some bytes from BYTES according to the form given in FORM.
21687 Returns the new pointer. */
2e276125 21688
d521ce57
TT
21689static const gdb_byte *
21690skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21691 enum dwarf_form form,
21692 unsigned int offset_size,
21693 struct dwarf2_section_info *section)
2e276125 21694{
cf2c3c16 21695 unsigned int bytes_read;
2e276125 21696
cf2c3c16 21697 switch (form)
2e276125 21698 {
cf2c3c16
TT
21699 case DW_FORM_data1:
21700 case DW_FORM_flag:
21701 ++bytes;
21702 break;
21703
21704 case DW_FORM_data2:
21705 bytes += 2;
21706 break;
21707
21708 case DW_FORM_data4:
21709 bytes += 4;
21710 break;
21711
21712 case DW_FORM_data8:
21713 bytes += 8;
21714 break;
21715
0224619f
JK
21716 case DW_FORM_data16:
21717 bytes += 16;
21718 break;
21719
cf2c3c16
TT
21720 case DW_FORM_string:
21721 read_direct_string (abfd, bytes, &bytes_read);
21722 bytes += bytes_read;
21723 break;
21724
21725 case DW_FORM_sec_offset:
21726 case DW_FORM_strp:
36586728 21727 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21728 bytes += offset_size;
21729 break;
21730
21731 case DW_FORM_block:
21732 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21733 bytes += bytes_read;
21734 break;
21735
21736 case DW_FORM_block1:
21737 bytes += 1 + read_1_byte (abfd, bytes);
21738 break;
21739 case DW_FORM_block2:
21740 bytes += 2 + read_2_bytes (abfd, bytes);
21741 break;
21742 case DW_FORM_block4:
21743 bytes += 4 + read_4_bytes (abfd, bytes);
21744 break;
21745
21746 case DW_FORM_sdata:
21747 case DW_FORM_udata:
3019eac3
DE
21748 case DW_FORM_GNU_addr_index:
21749 case DW_FORM_GNU_str_index:
d521ce57 21750 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21751 if (bytes == NULL)
21752 {
21753 dwarf2_section_buffer_overflow_complaint (section);
21754 return NULL;
21755 }
cf2c3c16
TT
21756 break;
21757
21758 default:
21759 {
21760 complain:
21761 complaint (&symfile_complaints,
21762 _("invalid form 0x%x in `%s'"),
a32a8923 21763 form, get_section_name (section));
cf2c3c16
TT
21764 return NULL;
21765 }
2e276125
JB
21766 }
21767
cf2c3c16
TT
21768 return bytes;
21769}
757a13d0 21770
cf2c3c16
TT
21771/* A helper for dwarf_decode_macros that handles skipping an unknown
21772 opcode. Returns an updated pointer to the macro data buffer; or,
21773 on error, issues a complaint and returns NULL. */
757a13d0 21774
d521ce57 21775static const gdb_byte *
cf2c3c16 21776skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21777 const gdb_byte **opcode_definitions,
21778 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21779 bfd *abfd,
21780 unsigned int offset_size,
21781 struct dwarf2_section_info *section)
21782{
21783 unsigned int bytes_read, i;
21784 unsigned long arg;
d521ce57 21785 const gdb_byte *defn;
2e276125 21786
cf2c3c16 21787 if (opcode_definitions[opcode] == NULL)
2e276125 21788 {
cf2c3c16
TT
21789 complaint (&symfile_complaints,
21790 _("unrecognized DW_MACFINO opcode 0x%x"),
21791 opcode);
21792 return NULL;
21793 }
2e276125 21794
cf2c3c16
TT
21795 defn = opcode_definitions[opcode];
21796 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21797 defn += bytes_read;
2e276125 21798
cf2c3c16
TT
21799 for (i = 0; i < arg; ++i)
21800 {
aead7601
SM
21801 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21802 (enum dwarf_form) defn[i], offset_size,
f664829e 21803 section);
cf2c3c16
TT
21804 if (mac_ptr == NULL)
21805 {
21806 /* skip_form_bytes already issued the complaint. */
21807 return NULL;
21808 }
21809 }
757a13d0 21810
cf2c3c16
TT
21811 return mac_ptr;
21812}
757a13d0 21813
cf2c3c16
TT
21814/* A helper function which parses the header of a macro section.
21815 If the macro section is the extended (for now called "GNU") type,
21816 then this updates *OFFSET_SIZE. Returns a pointer to just after
21817 the header, or issues a complaint and returns NULL on error. */
757a13d0 21818
d521ce57
TT
21819static const gdb_byte *
21820dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21821 bfd *abfd,
d521ce57 21822 const gdb_byte *mac_ptr,
cf2c3c16
TT
21823 unsigned int *offset_size,
21824 int section_is_gnu)
21825{
21826 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21827
cf2c3c16
TT
21828 if (section_is_gnu)
21829 {
21830 unsigned int version, flags;
757a13d0 21831
cf2c3c16 21832 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21833 if (version != 4 && version != 5)
cf2c3c16
TT
21834 {
21835 complaint (&symfile_complaints,
21836 _("unrecognized version `%d' in .debug_macro section"),
21837 version);
21838 return NULL;
21839 }
21840 mac_ptr += 2;
757a13d0 21841
cf2c3c16
TT
21842 flags = read_1_byte (abfd, mac_ptr);
21843 ++mac_ptr;
21844 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21845
cf2c3c16
TT
21846 if ((flags & 2) != 0)
21847 /* We don't need the line table offset. */
21848 mac_ptr += *offset_size;
757a13d0 21849
cf2c3c16
TT
21850 /* Vendor opcode descriptions. */
21851 if ((flags & 4) != 0)
21852 {
21853 unsigned int i, count;
757a13d0 21854
cf2c3c16
TT
21855 count = read_1_byte (abfd, mac_ptr);
21856 ++mac_ptr;
21857 for (i = 0; i < count; ++i)
21858 {
21859 unsigned int opcode, bytes_read;
21860 unsigned long arg;
21861
21862 opcode = read_1_byte (abfd, mac_ptr);
21863 ++mac_ptr;
21864 opcode_definitions[opcode] = mac_ptr;
21865 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21866 mac_ptr += bytes_read;
21867 mac_ptr += arg;
21868 }
757a13d0 21869 }
cf2c3c16 21870 }
757a13d0 21871
cf2c3c16
TT
21872 return mac_ptr;
21873}
757a13d0 21874
cf2c3c16 21875/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21876 including DW_MACRO_import. */
cf2c3c16
TT
21877
21878static void
d521ce57
TT
21879dwarf_decode_macro_bytes (bfd *abfd,
21880 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21881 struct macro_source_file *current_file,
43f3e411 21882 struct line_header *lh,
cf2c3c16 21883 struct dwarf2_section_info *section,
36586728 21884 int section_is_gnu, int section_is_dwz,
cf2c3c16 21885 unsigned int offset_size,
8fc3fc34 21886 htab_t include_hash)
cf2c3c16 21887{
4d663531 21888 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21889 enum dwarf_macro_record_type macinfo_type;
21890 int at_commandline;
d521ce57 21891 const gdb_byte *opcode_definitions[256];
757a13d0 21892
cf2c3c16
TT
21893 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21894 &offset_size, section_is_gnu);
21895 if (mac_ptr == NULL)
21896 {
21897 /* We already issued a complaint. */
21898 return;
21899 }
757a13d0
JK
21900
21901 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21902 GDB is still reading the definitions from command line. First
21903 DW_MACINFO_start_file will need to be ignored as it was already executed
21904 to create CURRENT_FILE for the main source holding also the command line
21905 definitions. On first met DW_MACINFO_start_file this flag is reset to
21906 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21907
21908 at_commandline = 1;
21909
21910 do
21911 {
21912 /* Do we at least have room for a macinfo type byte? */
21913 if (mac_ptr >= mac_end)
21914 {
f664829e 21915 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21916 break;
21917 }
21918
aead7601 21919 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21920 mac_ptr++;
21921
cf2c3c16
TT
21922 /* Note that we rely on the fact that the corresponding GNU and
21923 DWARF constants are the same. */
757a13d0
JK
21924 switch (macinfo_type)
21925 {
21926 /* A zero macinfo type indicates the end of the macro
21927 information. */
21928 case 0:
21929 break;
2e276125 21930
0af92d60
JK
21931 case DW_MACRO_define:
21932 case DW_MACRO_undef:
21933 case DW_MACRO_define_strp:
21934 case DW_MACRO_undef_strp:
21935 case DW_MACRO_define_sup:
21936 case DW_MACRO_undef_sup:
2e276125 21937 {
891d2f0b 21938 unsigned int bytes_read;
2e276125 21939 int line;
d521ce57 21940 const char *body;
cf2c3c16 21941 int is_define;
2e276125 21942
cf2c3c16
TT
21943 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21944 mac_ptr += bytes_read;
21945
0af92d60
JK
21946 if (macinfo_type == DW_MACRO_define
21947 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21948 {
21949 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21950 mac_ptr += bytes_read;
21951 }
21952 else
21953 {
21954 LONGEST str_offset;
21955
21956 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21957 mac_ptr += offset_size;
2e276125 21958
0af92d60
JK
21959 if (macinfo_type == DW_MACRO_define_sup
21960 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21961 || section_is_dwz)
36586728
TT
21962 {
21963 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21964
21965 body = read_indirect_string_from_dwz (dwz, str_offset);
21966 }
21967 else
21968 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21969 }
21970
0af92d60
JK
21971 is_define = (macinfo_type == DW_MACRO_define
21972 || macinfo_type == DW_MACRO_define_strp
21973 || macinfo_type == DW_MACRO_define_sup);
2e276125 21974 if (! current_file)
757a13d0
JK
21975 {
21976 /* DWARF violation as no main source is present. */
21977 complaint (&symfile_complaints,
21978 _("debug info with no main source gives macro %s "
21979 "on line %d: %s"),
cf2c3c16
TT
21980 is_define ? _("definition") : _("undefinition"),
21981 line, body);
757a13d0
JK
21982 break;
21983 }
3e43a32a
MS
21984 if ((line == 0 && !at_commandline)
21985 || (line != 0 && at_commandline))
4d3c2250 21986 complaint (&symfile_complaints,
757a13d0
JK
21987 _("debug info gives %s macro %s with %s line %d: %s"),
21988 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21989 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21990 line == 0 ? _("zero") : _("non-zero"), line, body);
21991
cf2c3c16 21992 if (is_define)
757a13d0 21993 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21994 else
21995 {
0af92d60
JK
21996 gdb_assert (macinfo_type == DW_MACRO_undef
21997 || macinfo_type == DW_MACRO_undef_strp
21998 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
21999 macro_undef (current_file, line, body);
22000 }
2e276125
JB
22001 }
22002 break;
22003
0af92d60 22004 case DW_MACRO_start_file:
2e276125 22005 {
891d2f0b 22006 unsigned int bytes_read;
2e276125
JB
22007 int line, file;
22008
22009 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22010 mac_ptr += bytes_read;
22011 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22012 mac_ptr += bytes_read;
22013
3e43a32a
MS
22014 if ((line == 0 && !at_commandline)
22015 || (line != 0 && at_commandline))
757a13d0
JK
22016 complaint (&symfile_complaints,
22017 _("debug info gives source %d included "
22018 "from %s at %s line %d"),
22019 file, at_commandline ? _("command-line") : _("file"),
22020 line == 0 ? _("zero") : _("non-zero"), line);
22021
22022 if (at_commandline)
22023 {
0af92d60 22024 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22025 pass one. */
757a13d0
JK
22026 at_commandline = 0;
22027 }
22028 else
43f3e411 22029 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22030 }
22031 break;
22032
0af92d60 22033 case DW_MACRO_end_file:
2e276125 22034 if (! current_file)
4d3c2250 22035 complaint (&symfile_complaints,
3e43a32a
MS
22036 _("macro debug info has an unmatched "
22037 "`close_file' directive"));
2e276125
JB
22038 else
22039 {
22040 current_file = current_file->included_by;
22041 if (! current_file)
22042 {
cf2c3c16 22043 enum dwarf_macro_record_type next_type;
2e276125
JB
22044
22045 /* GCC circa March 2002 doesn't produce the zero
22046 type byte marking the end of the compilation
22047 unit. Complain if it's not there, but exit no
22048 matter what. */
22049
22050 /* Do we at least have room for a macinfo type byte? */
22051 if (mac_ptr >= mac_end)
22052 {
f664829e 22053 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22054 return;
22055 }
22056
22057 /* We don't increment mac_ptr here, so this is just
22058 a look-ahead. */
aead7601
SM
22059 next_type
22060 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22061 mac_ptr);
2e276125 22062 if (next_type != 0)
4d3c2250 22063 complaint (&symfile_complaints,
3e43a32a
MS
22064 _("no terminating 0-type entry for "
22065 "macros in `.debug_macinfo' section"));
2e276125
JB
22066
22067 return;
22068 }
22069 }
22070 break;
22071
0af92d60
JK
22072 case DW_MACRO_import:
22073 case DW_MACRO_import_sup:
cf2c3c16
TT
22074 {
22075 LONGEST offset;
8fc3fc34 22076 void **slot;
a036ba48
TT
22077 bfd *include_bfd = abfd;
22078 struct dwarf2_section_info *include_section = section;
d521ce57 22079 const gdb_byte *include_mac_end = mac_end;
a036ba48 22080 int is_dwz = section_is_dwz;
d521ce57 22081 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22082
22083 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22084 mac_ptr += offset_size;
22085
0af92d60 22086 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22087 {
22088 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22089
4d663531 22090 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22091
a036ba48 22092 include_section = &dwz->macro;
a32a8923 22093 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22094 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22095 is_dwz = 1;
22096 }
22097
22098 new_mac_ptr = include_section->buffer + offset;
22099 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22100
8fc3fc34
TT
22101 if (*slot != NULL)
22102 {
22103 /* This has actually happened; see
22104 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22105 complaint (&symfile_complaints,
0af92d60 22106 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22107 ".debug_macro section"));
22108 }
22109 else
22110 {
d521ce57 22111 *slot = (void *) new_mac_ptr;
36586728 22112
a036ba48 22113 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22114 include_mac_end, current_file, lh,
36586728 22115 section, section_is_gnu, is_dwz,
4d663531 22116 offset_size, include_hash);
8fc3fc34 22117
d521ce57 22118 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22119 }
cf2c3c16
TT
22120 }
22121 break;
22122
2e276125 22123 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22124 if (!section_is_gnu)
22125 {
22126 unsigned int bytes_read;
2e276125 22127
ac298888
TT
22128 /* This reads the constant, but since we don't recognize
22129 any vendor extensions, we ignore it. */
22130 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22131 mac_ptr += bytes_read;
22132 read_direct_string (abfd, mac_ptr, &bytes_read);
22133 mac_ptr += bytes_read;
2e276125 22134
cf2c3c16
TT
22135 /* We don't recognize any vendor extensions. */
22136 break;
22137 }
22138 /* FALLTHROUGH */
22139
22140 default:
22141 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22142 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22143 section);
22144 if (mac_ptr == NULL)
22145 return;
22146 break;
2e276125 22147 }
757a13d0 22148 } while (macinfo_type != 0);
2e276125 22149}
8e19ed76 22150
cf2c3c16 22151static void
09262596 22152dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22153 int section_is_gnu)
cf2c3c16 22154{
bb5ed363 22155 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22156 struct line_header *lh = cu->line_header;
22157 bfd *abfd;
d521ce57 22158 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22159 struct macro_source_file *current_file = 0;
22160 enum dwarf_macro_record_type macinfo_type;
22161 unsigned int offset_size = cu->header.offset_size;
d521ce57 22162 const gdb_byte *opcode_definitions[256];
8fc3fc34 22163 struct cleanup *cleanup;
8fc3fc34 22164 void **slot;
09262596
DE
22165 struct dwarf2_section_info *section;
22166 const char *section_name;
22167
22168 if (cu->dwo_unit != NULL)
22169 {
22170 if (section_is_gnu)
22171 {
22172 section = &cu->dwo_unit->dwo_file->sections.macro;
22173 section_name = ".debug_macro.dwo";
22174 }
22175 else
22176 {
22177 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22178 section_name = ".debug_macinfo.dwo";
22179 }
22180 }
22181 else
22182 {
22183 if (section_is_gnu)
22184 {
22185 section = &dwarf2_per_objfile->macro;
22186 section_name = ".debug_macro";
22187 }
22188 else
22189 {
22190 section = &dwarf2_per_objfile->macinfo;
22191 section_name = ".debug_macinfo";
22192 }
22193 }
cf2c3c16 22194
bb5ed363 22195 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22196 if (section->buffer == NULL)
22197 {
fceca515 22198 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22199 return;
22200 }
a32a8923 22201 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22202
22203 /* First pass: Find the name of the base filename.
22204 This filename is needed in order to process all macros whose definition
22205 (or undefinition) comes from the command line. These macros are defined
22206 before the first DW_MACINFO_start_file entry, and yet still need to be
22207 associated to the base file.
22208
22209 To determine the base file name, we scan the macro definitions until we
22210 reach the first DW_MACINFO_start_file entry. We then initialize
22211 CURRENT_FILE accordingly so that any macro definition found before the
22212 first DW_MACINFO_start_file can still be associated to the base file. */
22213
22214 mac_ptr = section->buffer + offset;
22215 mac_end = section->buffer + section->size;
22216
22217 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22218 &offset_size, section_is_gnu);
22219 if (mac_ptr == NULL)
22220 {
22221 /* We already issued a complaint. */
22222 return;
22223 }
22224
22225 do
22226 {
22227 /* Do we at least have room for a macinfo type byte? */
22228 if (mac_ptr >= mac_end)
22229 {
22230 /* Complaint is printed during the second pass as GDB will probably
22231 stop the first pass earlier upon finding
22232 DW_MACINFO_start_file. */
22233 break;
22234 }
22235
aead7601 22236 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22237 mac_ptr++;
22238
22239 /* Note that we rely on the fact that the corresponding GNU and
22240 DWARF constants are the same. */
22241 switch (macinfo_type)
22242 {
22243 /* A zero macinfo type indicates the end of the macro
22244 information. */
22245 case 0:
22246 break;
22247
0af92d60
JK
22248 case DW_MACRO_define:
22249 case DW_MACRO_undef:
cf2c3c16
TT
22250 /* Only skip the data by MAC_PTR. */
22251 {
22252 unsigned int bytes_read;
22253
22254 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22255 mac_ptr += bytes_read;
22256 read_direct_string (abfd, mac_ptr, &bytes_read);
22257 mac_ptr += bytes_read;
22258 }
22259 break;
22260
0af92d60 22261 case DW_MACRO_start_file:
cf2c3c16
TT
22262 {
22263 unsigned int bytes_read;
22264 int line, file;
22265
22266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22267 mac_ptr += bytes_read;
22268 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22269 mac_ptr += bytes_read;
22270
43f3e411 22271 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22272 }
22273 break;
22274
0af92d60 22275 case DW_MACRO_end_file:
cf2c3c16
TT
22276 /* No data to skip by MAC_PTR. */
22277 break;
22278
0af92d60
JK
22279 case DW_MACRO_define_strp:
22280 case DW_MACRO_undef_strp:
22281 case DW_MACRO_define_sup:
22282 case DW_MACRO_undef_sup:
cf2c3c16
TT
22283 {
22284 unsigned int bytes_read;
22285
22286 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22287 mac_ptr += bytes_read;
22288 mac_ptr += offset_size;
22289 }
22290 break;
22291
0af92d60
JK
22292 case DW_MACRO_import:
22293 case DW_MACRO_import_sup:
cf2c3c16 22294 /* Note that, according to the spec, a transparent include
0af92d60 22295 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22296 skip this opcode. */
22297 mac_ptr += offset_size;
22298 break;
22299
22300 case DW_MACINFO_vendor_ext:
22301 /* Only skip the data by MAC_PTR. */
22302 if (!section_is_gnu)
22303 {
22304 unsigned int bytes_read;
22305
22306 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22307 mac_ptr += bytes_read;
22308 read_direct_string (abfd, mac_ptr, &bytes_read);
22309 mac_ptr += bytes_read;
22310 }
22311 /* FALLTHROUGH */
22312
22313 default:
22314 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22315 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22316 section);
22317 if (mac_ptr == NULL)
22318 return;
22319 break;
22320 }
22321 } while (macinfo_type != 0 && current_file == NULL);
22322
22323 /* Second pass: Process all entries.
22324
22325 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22326 command-line macro definitions/undefinitions. This flag is unset when we
22327 reach the first DW_MACINFO_start_file entry. */
22328
fc4007c9
TT
22329 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22330 htab_eq_pointer,
22331 NULL, xcalloc, xfree));
8fc3fc34 22332 mac_ptr = section->buffer + offset;
fc4007c9 22333 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22334 *slot = (void *) mac_ptr;
8fc3fc34 22335 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22336 current_file, lh, section,
fc4007c9
TT
22337 section_is_gnu, 0, offset_size,
22338 include_hash.get ());
cf2c3c16
TT
22339}
22340
8e19ed76 22341/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22342 if so return true else false. */
380bca97 22343
8e19ed76 22344static int
6e5a29e1 22345attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22346{
22347 return (attr == NULL ? 0 :
22348 attr->form == DW_FORM_block1
22349 || attr->form == DW_FORM_block2
22350 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22351 || attr->form == DW_FORM_block
22352 || attr->form == DW_FORM_exprloc);
8e19ed76 22353}
4c2df51b 22354
c6a0999f
JB
22355/* Return non-zero if ATTR's value is a section offset --- classes
22356 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22357 You may use DW_UNSND (attr) to retrieve such offsets.
22358
22359 Section 7.5.4, "Attribute Encodings", explains that no attribute
22360 may have a value that belongs to more than one of these classes; it
22361 would be ambiguous if we did, because we use the same forms for all
22362 of them. */
380bca97 22363
3690dd37 22364static int
6e5a29e1 22365attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22366{
22367 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22368 || attr->form == DW_FORM_data8
22369 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22370}
22371
3690dd37
JB
22372/* Return non-zero if ATTR's value falls in the 'constant' class, or
22373 zero otherwise. When this function returns true, you can apply
22374 dwarf2_get_attr_constant_value to it.
22375
22376 However, note that for some attributes you must check
22377 attr_form_is_section_offset before using this test. DW_FORM_data4
22378 and DW_FORM_data8 are members of both the constant class, and of
22379 the classes that contain offsets into other debug sections
22380 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22381 that, if an attribute's can be either a constant or one of the
22382 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22383 taken as section offsets, not constants.
22384
22385 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22386 cannot handle that. */
380bca97 22387
3690dd37 22388static int
6e5a29e1 22389attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22390{
22391 switch (attr->form)
22392 {
22393 case DW_FORM_sdata:
22394 case DW_FORM_udata:
22395 case DW_FORM_data1:
22396 case DW_FORM_data2:
22397 case DW_FORM_data4:
22398 case DW_FORM_data8:
22399 return 1;
22400 default:
22401 return 0;
22402 }
22403}
22404
7771576e
SA
22405
22406/* DW_ADDR is always stored already as sect_offset; despite for the forms
22407 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22408
22409static int
6e5a29e1 22410attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22411{
22412 switch (attr->form)
22413 {
22414 case DW_FORM_ref_addr:
22415 case DW_FORM_ref1:
22416 case DW_FORM_ref2:
22417 case DW_FORM_ref4:
22418 case DW_FORM_ref8:
22419 case DW_FORM_ref_udata:
22420 case DW_FORM_GNU_ref_alt:
22421 return 1;
22422 default:
22423 return 0;
22424 }
22425}
22426
3019eac3
DE
22427/* Return the .debug_loc section to use for CU.
22428 For DWO files use .debug_loc.dwo. */
22429
22430static struct dwarf2_section_info *
22431cu_debug_loc_section (struct dwarf2_cu *cu)
22432{
22433 if (cu->dwo_unit)
43988095
JK
22434 {
22435 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22436
22437 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22438 }
22439 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22440 : &dwarf2_per_objfile->loc);
3019eac3
DE
22441}
22442
8cf6f0b1
TT
22443/* A helper function that fills in a dwarf2_loclist_baton. */
22444
22445static void
22446fill_in_loclist_baton (struct dwarf2_cu *cu,
22447 struct dwarf2_loclist_baton *baton,
ff39bb5e 22448 const struct attribute *attr)
8cf6f0b1 22449{
3019eac3
DE
22450 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22451
22452 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22453
22454 baton->per_cu = cu->per_cu;
22455 gdb_assert (baton->per_cu);
22456 /* We don't know how long the location list is, but make sure we
22457 don't run off the edge of the section. */
3019eac3
DE
22458 baton->size = section->size - DW_UNSND (attr);
22459 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22460 baton->base_address = cu->base_address;
f664829e 22461 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22462}
22463
4c2df51b 22464static void
ff39bb5e 22465dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22466 struct dwarf2_cu *cu, int is_block)
4c2df51b 22467{
bb5ed363 22468 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22469 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22470
3690dd37 22471 if (attr_form_is_section_offset (attr)
3019eac3 22472 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22473 the section. If so, fall through to the complaint in the
22474 other branch. */
3019eac3 22475 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22476 {
0d53c4c4 22477 struct dwarf2_loclist_baton *baton;
4c2df51b 22478
8d749320 22479 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22480
8cf6f0b1 22481 fill_in_loclist_baton (cu, baton, attr);
be391dca 22482
d00adf39 22483 if (cu->base_known == 0)
0d53c4c4 22484 complaint (&symfile_complaints,
3e43a32a
MS
22485 _("Location list used without "
22486 "specifying the CU base address."));
4c2df51b 22487
f1e6e072
TT
22488 SYMBOL_ACLASS_INDEX (sym) = (is_block
22489 ? dwarf2_loclist_block_index
22490 : dwarf2_loclist_index);
0d53c4c4
DJ
22491 SYMBOL_LOCATION_BATON (sym) = baton;
22492 }
22493 else
22494 {
22495 struct dwarf2_locexpr_baton *baton;
22496
8d749320 22497 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22498 baton->per_cu = cu->per_cu;
22499 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22500
22501 if (attr_form_is_block (attr))
22502 {
22503 /* Note that we're just copying the block's data pointer
22504 here, not the actual data. We're still pointing into the
6502dd73
DJ
22505 info_buffer for SYM's objfile; right now we never release
22506 that buffer, but when we do clean up properly this may
22507 need to change. */
0d53c4c4
DJ
22508 baton->size = DW_BLOCK (attr)->size;
22509 baton->data = DW_BLOCK (attr)->data;
22510 }
22511 else
22512 {
22513 dwarf2_invalid_attrib_class_complaint ("location description",
22514 SYMBOL_NATURAL_NAME (sym));
22515 baton->size = 0;
0d53c4c4 22516 }
6e70227d 22517
f1e6e072
TT
22518 SYMBOL_ACLASS_INDEX (sym) = (is_block
22519 ? dwarf2_locexpr_block_index
22520 : dwarf2_locexpr_index);
0d53c4c4
DJ
22521 SYMBOL_LOCATION_BATON (sym) = baton;
22522 }
4c2df51b 22523}
6502dd73 22524
9aa1f1e3
TT
22525/* Return the OBJFILE associated with the compilation unit CU. If CU
22526 came from a separate debuginfo file, then the master objfile is
22527 returned. */
ae0d2f24
UW
22528
22529struct objfile *
22530dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22531{
9291a0cd 22532 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22533
22534 /* Return the master objfile, so that we can report and look up the
22535 correct file containing this variable. */
22536 if (objfile->separate_debug_objfile_backlink)
22537 objfile = objfile->separate_debug_objfile_backlink;
22538
22539 return objfile;
22540}
22541
96408a79
SA
22542/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22543 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22544 CU_HEADERP first. */
22545
22546static const struct comp_unit_head *
22547per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22548 struct dwarf2_per_cu_data *per_cu)
22549{
d521ce57 22550 const gdb_byte *info_ptr;
96408a79
SA
22551
22552 if (per_cu->cu)
22553 return &per_cu->cu->header;
22554
9c541725 22555 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22556
22557 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22558 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22559 rcuh_kind::COMPILE);
96408a79
SA
22560
22561 return cu_headerp;
22562}
22563
ae0d2f24
UW
22564/* Return the address size given in the compilation unit header for CU. */
22565
98714339 22566int
ae0d2f24
UW
22567dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22568{
96408a79
SA
22569 struct comp_unit_head cu_header_local;
22570 const struct comp_unit_head *cu_headerp;
c471e790 22571
96408a79
SA
22572 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22573
22574 return cu_headerp->addr_size;
ae0d2f24
UW
22575}
22576
9eae7c52
TT
22577/* Return the offset size given in the compilation unit header for CU. */
22578
22579int
22580dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22581{
96408a79
SA
22582 struct comp_unit_head cu_header_local;
22583 const struct comp_unit_head *cu_headerp;
9c6c53f7 22584
96408a79
SA
22585 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22586
22587 return cu_headerp->offset_size;
22588}
22589
22590/* See its dwarf2loc.h declaration. */
22591
22592int
22593dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22594{
22595 struct comp_unit_head cu_header_local;
22596 const struct comp_unit_head *cu_headerp;
22597
22598 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22599
22600 if (cu_headerp->version == 2)
22601 return cu_headerp->addr_size;
22602 else
22603 return cu_headerp->offset_size;
181cebd4
JK
22604}
22605
9aa1f1e3
TT
22606/* Return the text offset of the CU. The returned offset comes from
22607 this CU's objfile. If this objfile came from a separate debuginfo
22608 file, then the offset may be different from the corresponding
22609 offset in the parent objfile. */
22610
22611CORE_ADDR
22612dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22613{
bb3fa9d0 22614 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22615
22616 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22617}
22618
43988095
JK
22619/* Return DWARF version number of PER_CU. */
22620
22621short
22622dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22623{
22624 return per_cu->dwarf_version;
22625}
22626
348e048f
DE
22627/* Locate the .debug_info compilation unit from CU's objfile which contains
22628 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22629
22630static struct dwarf2_per_cu_data *
9c541725 22631dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22632 unsigned int offset_in_dwz,
ae038cb0
DJ
22633 struct objfile *objfile)
22634{
22635 struct dwarf2_per_cu_data *this_cu;
22636 int low, high;
36586728 22637 const sect_offset *cu_off;
ae038cb0 22638
ae038cb0
DJ
22639 low = 0;
22640 high = dwarf2_per_objfile->n_comp_units - 1;
22641 while (high > low)
22642 {
36586728 22643 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22644 int mid = low + (high - low) / 2;
9a619af0 22645
36586728 22646 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22647 cu_off = &mid_cu->sect_off;
36586728 22648 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22649 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22650 high = mid;
22651 else
22652 low = mid + 1;
22653 }
22654 gdb_assert (low == high);
36586728 22655 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22656 cu_off = &this_cu->sect_off;
22657 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22658 {
36586728 22659 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22660 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22661 "offset 0x%x [in module %s]"),
22662 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22663
9c541725
PA
22664 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22665 <= sect_off);
ae038cb0
DJ
22666 return dwarf2_per_objfile->all_comp_units[low-1];
22667 }
22668 else
22669 {
22670 this_cu = dwarf2_per_objfile->all_comp_units[low];
22671 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22672 && sect_off >= this_cu->sect_off + this_cu->length)
22673 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22674 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22675 return this_cu;
22676 }
22677}
22678
23745b47 22679/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22680
9816fde3 22681static void
23745b47 22682init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22683{
9816fde3 22684 memset (cu, 0, sizeof (*cu));
23745b47
DE
22685 per_cu->cu = cu;
22686 cu->per_cu = per_cu;
22687 cu->objfile = per_cu->objfile;
93311388 22688 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22689}
22690
22691/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22692
22693static void
95554aad
TT
22694prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22695 enum language pretend_language)
9816fde3
JK
22696{
22697 struct attribute *attr;
22698
22699 /* Set the language we're debugging. */
22700 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22701 if (attr)
22702 set_cu_language (DW_UNSND (attr), cu);
22703 else
9cded63f 22704 {
95554aad 22705 cu->language = pretend_language;
9cded63f
TT
22706 cu->language_defn = language_def (cu->language);
22707 }
dee91e82 22708
7d45c7c3 22709 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22710}
22711
ae038cb0
DJ
22712/* Release one cached compilation unit, CU. We unlink it from the tree
22713 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22714 the caller is responsible for that.
22715 NOTE: DATA is a void * because this function is also used as a
22716 cleanup routine. */
ae038cb0
DJ
22717
22718static void
68dc6402 22719free_heap_comp_unit (void *data)
ae038cb0 22720{
9a3c8263 22721 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22722
23745b47
DE
22723 gdb_assert (cu->per_cu != NULL);
22724 cu->per_cu->cu = NULL;
ae038cb0
DJ
22725 cu->per_cu = NULL;
22726
22727 obstack_free (&cu->comp_unit_obstack, NULL);
22728
22729 xfree (cu);
22730}
22731
72bf9492 22732/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22733 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22734 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22735
22736static void
22737free_stack_comp_unit (void *data)
22738{
9a3c8263 22739 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22740
23745b47
DE
22741 gdb_assert (cu->per_cu != NULL);
22742 cu->per_cu->cu = NULL;
22743 cu->per_cu = NULL;
22744
72bf9492
DJ
22745 obstack_free (&cu->comp_unit_obstack, NULL);
22746 cu->partial_dies = NULL;
ae038cb0
DJ
22747}
22748
22749/* Free all cached compilation units. */
22750
22751static void
22752free_cached_comp_units (void *data)
22753{
22754 struct dwarf2_per_cu_data *per_cu, **last_chain;
22755
22756 per_cu = dwarf2_per_objfile->read_in_chain;
22757 last_chain = &dwarf2_per_objfile->read_in_chain;
22758 while (per_cu != NULL)
22759 {
22760 struct dwarf2_per_cu_data *next_cu;
22761
22762 next_cu = per_cu->cu->read_in_chain;
22763
68dc6402 22764 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22765 *last_chain = next_cu;
22766
22767 per_cu = next_cu;
22768 }
22769}
22770
22771/* Increase the age counter on each cached compilation unit, and free
22772 any that are too old. */
22773
22774static void
22775age_cached_comp_units (void)
22776{
22777 struct dwarf2_per_cu_data *per_cu, **last_chain;
22778
22779 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22780 per_cu = dwarf2_per_objfile->read_in_chain;
22781 while (per_cu != NULL)
22782 {
22783 per_cu->cu->last_used ++;
b4f54984 22784 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22785 dwarf2_mark (per_cu->cu);
22786 per_cu = per_cu->cu->read_in_chain;
22787 }
22788
22789 per_cu = dwarf2_per_objfile->read_in_chain;
22790 last_chain = &dwarf2_per_objfile->read_in_chain;
22791 while (per_cu != NULL)
22792 {
22793 struct dwarf2_per_cu_data *next_cu;
22794
22795 next_cu = per_cu->cu->read_in_chain;
22796
22797 if (!per_cu->cu->mark)
22798 {
68dc6402 22799 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22800 *last_chain = next_cu;
22801 }
22802 else
22803 last_chain = &per_cu->cu->read_in_chain;
22804
22805 per_cu = next_cu;
22806 }
22807}
22808
22809/* Remove a single compilation unit from the cache. */
22810
22811static void
dee91e82 22812free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22813{
22814 struct dwarf2_per_cu_data *per_cu, **last_chain;
22815
22816 per_cu = dwarf2_per_objfile->read_in_chain;
22817 last_chain = &dwarf2_per_objfile->read_in_chain;
22818 while (per_cu != NULL)
22819 {
22820 struct dwarf2_per_cu_data *next_cu;
22821
22822 next_cu = per_cu->cu->read_in_chain;
22823
dee91e82 22824 if (per_cu == target_per_cu)
ae038cb0 22825 {
68dc6402 22826 free_heap_comp_unit (per_cu->cu);
dee91e82 22827 per_cu->cu = NULL;
ae038cb0
DJ
22828 *last_chain = next_cu;
22829 break;
22830 }
22831 else
22832 last_chain = &per_cu->cu->read_in_chain;
22833
22834 per_cu = next_cu;
22835 }
22836}
22837
fe3e1990
DJ
22838/* Release all extra memory associated with OBJFILE. */
22839
22840void
22841dwarf2_free_objfile (struct objfile *objfile)
22842{
9a3c8263
SM
22843 dwarf2_per_objfile
22844 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22845 dwarf2_objfile_data_key);
fe3e1990
DJ
22846
22847 if (dwarf2_per_objfile == NULL)
22848 return;
22849
22850 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22851 free_cached_comp_units (NULL);
22852
7b9f3c50
DE
22853 if (dwarf2_per_objfile->quick_file_names_table)
22854 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22855
527f3840
JK
22856 if (dwarf2_per_objfile->line_header_hash)
22857 htab_delete (dwarf2_per_objfile->line_header_hash);
22858
fe3e1990
DJ
22859 /* Everything else should be on the objfile obstack. */
22860}
22861
dee91e82
DE
22862/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22863 We store these in a hash table separate from the DIEs, and preserve them
22864 when the DIEs are flushed out of cache.
22865
22866 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22867 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22868 or the type may come from a DWO file. Furthermore, while it's more logical
22869 to use per_cu->section+offset, with Fission the section with the data is in
22870 the DWO file but we don't know that section at the point we need it.
22871 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22872 because we can enter the lookup routine, get_die_type_at_offset, from
22873 outside this file, and thus won't necessarily have PER_CU->cu.
22874 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22875
dee91e82 22876struct dwarf2_per_cu_offset_and_type
1c379e20 22877{
dee91e82 22878 const struct dwarf2_per_cu_data *per_cu;
9c541725 22879 sect_offset sect_off;
1c379e20
DJ
22880 struct type *type;
22881};
22882
dee91e82 22883/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22884
22885static hashval_t
dee91e82 22886per_cu_offset_and_type_hash (const void *item)
1c379e20 22887{
9a3c8263
SM
22888 const struct dwarf2_per_cu_offset_and_type *ofs
22889 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22890
9c541725 22891 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22892}
22893
dee91e82 22894/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22895
22896static int
dee91e82 22897per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22898{
9a3c8263
SM
22899 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22900 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22901 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22902 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22903
dee91e82 22904 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 22905 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
22906}
22907
22908/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22909 table if necessary. For convenience, return TYPE.
22910
22911 The DIEs reading must have careful ordering to:
22912 * Not cause infite loops trying to read in DIEs as a prerequisite for
22913 reading current DIE.
22914 * Not trying to dereference contents of still incompletely read in types
22915 while reading in other DIEs.
22916 * Enable referencing still incompletely read in types just by a pointer to
22917 the type without accessing its fields.
22918
22919 Therefore caller should follow these rules:
22920 * Try to fetch any prerequisite types we may need to build this DIE type
22921 before building the type and calling set_die_type.
e71ec853 22922 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22923 possible before fetching more types to complete the current type.
22924 * Make the type as complete as possible before fetching more types. */
1c379e20 22925
f792889a 22926static struct type *
1c379e20
DJ
22927set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22928{
dee91e82 22929 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22930 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22931 struct attribute *attr;
22932 struct dynamic_prop prop;
1c379e20 22933
b4ba55a1
JB
22934 /* For Ada types, make sure that the gnat-specific data is always
22935 initialized (if not already set). There are a few types where
22936 we should not be doing so, because the type-specific area is
22937 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22938 where the type-specific area is used to store the floatformat).
22939 But this is not a problem, because the gnat-specific information
22940 is actually not needed for these types. */
22941 if (need_gnat_info (cu)
22942 && TYPE_CODE (type) != TYPE_CODE_FUNC
22943 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22944 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22945 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22946 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22947 && !HAVE_GNAT_AUX_INFO (type))
22948 INIT_GNAT_SPECIFIC (type);
22949
3f2f83dd
KB
22950 /* Read DW_AT_allocated and set in type. */
22951 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22952 if (attr_form_is_block (attr))
22953 {
22954 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22955 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22956 }
22957 else if (attr != NULL)
22958 {
22959 complaint (&symfile_complaints,
9c541725
PA
22960 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22961 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22962 to_underlying (die->sect_off));
3f2f83dd
KB
22963 }
22964
22965 /* Read DW_AT_associated and set in type. */
22966 attr = dwarf2_attr (die, DW_AT_associated, cu);
22967 if (attr_form_is_block (attr))
22968 {
22969 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22970 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22971 }
22972 else if (attr != NULL)
22973 {
22974 complaint (&symfile_complaints,
9c541725
PA
22975 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22976 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22977 to_underlying (die->sect_off));
3f2f83dd
KB
22978 }
22979
3cdcd0ce
JB
22980 /* Read DW_AT_data_location and set in type. */
22981 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22982 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22983 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22984
dee91e82 22985 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22986 {
dee91e82
DE
22987 dwarf2_per_objfile->die_type_hash =
22988 htab_create_alloc_ex (127,
22989 per_cu_offset_and_type_hash,
22990 per_cu_offset_and_type_eq,
22991 NULL,
22992 &objfile->objfile_obstack,
22993 hashtab_obstack_allocate,
22994 dummy_obstack_deallocate);
f792889a 22995 }
1c379e20 22996
dee91e82 22997 ofs.per_cu = cu->per_cu;
9c541725 22998 ofs.sect_off = die->sect_off;
1c379e20 22999 ofs.type = type;
dee91e82
DE
23000 slot = (struct dwarf2_per_cu_offset_and_type **)
23001 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23002 if (*slot)
23003 complaint (&symfile_complaints,
23004 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23005 to_underlying (die->sect_off));
8d749320
SM
23006 *slot = XOBNEW (&objfile->objfile_obstack,
23007 struct dwarf2_per_cu_offset_and_type);
1c379e20 23008 **slot = ofs;
f792889a 23009 return type;
1c379e20
DJ
23010}
23011
9c541725 23012/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23013 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23014
23015static struct type *
9c541725 23016get_die_type_at_offset (sect_offset sect_off,
673bfd45 23017 struct dwarf2_per_cu_data *per_cu)
1c379e20 23018{
dee91e82 23019 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23020
dee91e82 23021 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23022 return NULL;
1c379e20 23023
dee91e82 23024 ofs.per_cu = per_cu;
9c541725 23025 ofs.sect_off = sect_off;
9a3c8263
SM
23026 slot = ((struct dwarf2_per_cu_offset_and_type *)
23027 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23028 if (slot)
23029 return slot->type;
23030 else
23031 return NULL;
23032}
23033
02142a6c 23034/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23035 or return NULL if DIE does not have a saved type. */
23036
23037static struct type *
23038get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23039{
9c541725 23040 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23041}
23042
10b3939b
DJ
23043/* Add a dependence relationship from CU to REF_PER_CU. */
23044
23045static void
23046dwarf2_add_dependence (struct dwarf2_cu *cu,
23047 struct dwarf2_per_cu_data *ref_per_cu)
23048{
23049 void **slot;
23050
23051 if (cu->dependencies == NULL)
23052 cu->dependencies
23053 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23054 NULL, &cu->comp_unit_obstack,
23055 hashtab_obstack_allocate,
23056 dummy_obstack_deallocate);
23057
23058 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23059 if (*slot == NULL)
23060 *slot = ref_per_cu;
23061}
1c379e20 23062
f504f079
DE
23063/* Subroutine of dwarf2_mark to pass to htab_traverse.
23064 Set the mark field in every compilation unit in the
ae038cb0
DJ
23065 cache that we must keep because we are keeping CU. */
23066
10b3939b
DJ
23067static int
23068dwarf2_mark_helper (void **slot, void *data)
23069{
23070 struct dwarf2_per_cu_data *per_cu;
23071
23072 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23073
23074 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23075 reading of the chain. As such dependencies remain valid it is not much
23076 useful to track and undo them during QUIT cleanups. */
23077 if (per_cu->cu == NULL)
23078 return 1;
23079
10b3939b
DJ
23080 if (per_cu->cu->mark)
23081 return 1;
23082 per_cu->cu->mark = 1;
23083
23084 if (per_cu->cu->dependencies != NULL)
23085 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23086
23087 return 1;
23088}
23089
f504f079
DE
23090/* Set the mark field in CU and in every other compilation unit in the
23091 cache that we must keep because we are keeping CU. */
23092
ae038cb0
DJ
23093static void
23094dwarf2_mark (struct dwarf2_cu *cu)
23095{
23096 if (cu->mark)
23097 return;
23098 cu->mark = 1;
10b3939b
DJ
23099 if (cu->dependencies != NULL)
23100 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23101}
23102
23103static void
23104dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23105{
23106 while (per_cu)
23107 {
23108 per_cu->cu->mark = 0;
23109 per_cu = per_cu->cu->read_in_chain;
23110 }
72bf9492
DJ
23111}
23112
72bf9492
DJ
23113/* Trivial hash function for partial_die_info: the hash value of a DIE
23114 is its offset in .debug_info for this objfile. */
23115
23116static hashval_t
23117partial_die_hash (const void *item)
23118{
9a3c8263
SM
23119 const struct partial_die_info *part_die
23120 = (const struct partial_die_info *) item;
9a619af0 23121
9c541725 23122 return to_underlying (part_die->sect_off);
72bf9492
DJ
23123}
23124
23125/* Trivial comparison function for partial_die_info structures: two DIEs
23126 are equal if they have the same offset. */
23127
23128static int
23129partial_die_eq (const void *item_lhs, const void *item_rhs)
23130{
9a3c8263
SM
23131 const struct partial_die_info *part_die_lhs
23132 = (const struct partial_die_info *) item_lhs;
23133 const struct partial_die_info *part_die_rhs
23134 = (const struct partial_die_info *) item_rhs;
9a619af0 23135
9c541725 23136 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23137}
23138
b4f54984
DE
23139static struct cmd_list_element *set_dwarf_cmdlist;
23140static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23141
23142static void
b4f54984 23143set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23144{
b4f54984 23145 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23146 gdb_stdout);
ae038cb0
DJ
23147}
23148
23149static void
b4f54984 23150show_dwarf_cmd (char *args, int from_tty)
6e70227d 23151{
b4f54984 23152 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23153}
23154
4bf44c1c 23155/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23156
23157static void
c1bd65d0 23158dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23159{
9a3c8263 23160 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23161 int ix;
8b70b953 23162
626f2d1c
TT
23163 /* Make sure we don't accidentally use dwarf2_per_objfile while
23164 cleaning up. */
23165 dwarf2_per_objfile = NULL;
23166
59b0c7c1
JB
23167 for (ix = 0; ix < data->n_comp_units; ++ix)
23168 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23169
59b0c7c1 23170 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23171 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23172 data->all_type_units[ix]->per_cu.imported_symtabs);
23173 xfree (data->all_type_units);
95554aad 23174
8b70b953 23175 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23176
23177 if (data->dwo_files)
23178 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23179 if (data->dwp_file)
23180 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23181
23182 if (data->dwz_file && data->dwz_file->dwz_bfd)
23183 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23184}
23185
23186\f
ae2de4f8 23187/* The "save gdb-index" command. */
9291a0cd 23188
bc8f2430
JK
23189/* In-memory buffer to prepare data to be written later to a file. */
23190class data_buf
9291a0cd 23191{
bc8f2430 23192public:
bc8f2430
JK
23193 /* Copy DATA to the end of the buffer. */
23194 template<typename T>
23195 void append_data (const T &data)
23196 {
23197 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23198 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23199 grow (sizeof (data)));
bc8f2430 23200 }
b89be57b 23201
c2f134ac
PA
23202 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23203 terminating zero is appended too. */
bc8f2430
JK
23204 void append_cstr0 (const char *cstr)
23205 {
23206 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23207 std::copy (cstr, cstr + size, grow (size));
23208 }
23209
23210 /* Accept a host-format integer in VAL and append it to the buffer
23211 as a target-format integer which is LEN bytes long. */
23212 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23213 {
23214 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23215 }
9291a0cd 23216
bc8f2430
JK
23217 /* Return the size of the buffer. */
23218 size_t size () const
23219 {
23220 return m_vec.size ();
23221 }
23222
23223 /* Write the buffer to FILE. */
23224 void file_write (FILE *file) const
23225 {
a81e6d4d
PA
23226 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23227 error (_("couldn't write data to file"));
bc8f2430
JK
23228 }
23229
23230private:
c2f134ac
PA
23231 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23232 the start of the new block. */
23233 gdb_byte *grow (size_t size)
23234 {
23235 m_vec.resize (m_vec.size () + size);
23236 return &*m_vec.end () - size;
23237 }
23238
d5722aa2 23239 gdb::byte_vector m_vec;
bc8f2430 23240};
9291a0cd
TT
23241
23242/* An entry in the symbol table. */
23243struct symtab_index_entry
23244{
23245 /* The name of the symbol. */
23246 const char *name;
23247 /* The offset of the name in the constant pool. */
23248 offset_type index_offset;
23249 /* A sorted vector of the indices of all the CUs that hold an object
23250 of this name. */
bc8f2430 23251 std::vector<offset_type> cu_indices;
9291a0cd
TT
23252};
23253
23254/* The symbol table. This is a power-of-2-sized hash table. */
23255struct mapped_symtab
23256{
bc8f2430
JK
23257 mapped_symtab ()
23258 {
23259 data.resize (1024);
23260 }
b89be57b 23261
bc8f2430 23262 offset_type n_elements = 0;
4b76cda9 23263 std::vector<symtab_index_entry> data;
bc8f2430 23264};
9291a0cd 23265
bc8f2430 23266/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23267 the slot.
23268
23269 Function is used only during write_hash_table so no index format backward
23270 compatibility is needed. */
b89be57b 23271
4b76cda9 23272static symtab_index_entry &
9291a0cd
TT
23273find_slot (struct mapped_symtab *symtab, const char *name)
23274{
559a7a62 23275 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23276
bc8f2430
JK
23277 index = hash & (symtab->data.size () - 1);
23278 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23279
23280 for (;;)
23281 {
4b76cda9
PA
23282 if (symtab->data[index].name == NULL
23283 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23284 return symtab->data[index];
23285 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23286 }
23287}
23288
23289/* Expand SYMTAB's hash table. */
b89be57b 23290
9291a0cd
TT
23291static void
23292hash_expand (struct mapped_symtab *symtab)
23293{
bc8f2430 23294 auto old_entries = std::move (symtab->data);
9291a0cd 23295
bc8f2430
JK
23296 symtab->data.clear ();
23297 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23298
bc8f2430 23299 for (auto &it : old_entries)
4b76cda9 23300 if (it.name != NULL)
bc8f2430 23301 {
4b76cda9 23302 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23303 ref = std::move (it);
23304 }
9291a0cd
TT
23305}
23306
156942c7
DE
23307/* Add an entry to SYMTAB. NAME is the name of the symbol.
23308 CU_INDEX is the index of the CU in which the symbol appears.
23309 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23310
9291a0cd
TT
23311static void
23312add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23313 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23314 offset_type cu_index)
23315{
156942c7 23316 offset_type cu_index_and_attrs;
9291a0cd
TT
23317
23318 ++symtab->n_elements;
bc8f2430 23319 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23320 hash_expand (symtab);
23321
4b76cda9
PA
23322 symtab_index_entry &slot = find_slot (symtab, name);
23323 if (slot.name == NULL)
9291a0cd 23324 {
4b76cda9 23325 slot.name = name;
156942c7 23326 /* index_offset is set later. */
9291a0cd 23327 }
156942c7
DE
23328
23329 cu_index_and_attrs = 0;
23330 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23331 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23332 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23333
23334 /* We don't want to record an index value twice as we want to avoid the
23335 duplication.
23336 We process all global symbols and then all static symbols
23337 (which would allow us to avoid the duplication by only having to check
23338 the last entry pushed), but a symbol could have multiple kinds in one CU.
23339 To keep things simple we don't worry about the duplication here and
23340 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23341 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23342}
23343
23344/* Sort and remove duplicates of all symbols' cu_indices lists. */
23345
23346static void
23347uniquify_cu_indices (struct mapped_symtab *symtab)
23348{
4b76cda9 23349 for (auto &entry : symtab->data)
156942c7 23350 {
4b76cda9 23351 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23352 {
4b76cda9 23353 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23354 std::sort (cu_indices.begin (), cu_indices.end ());
23355 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23356 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23357 }
23358 }
9291a0cd
TT
23359}
23360
bc8f2430
JK
23361/* A form of 'const char *' suitable for container keys. Only the
23362 pointer is stored. The strings themselves are compared, not the
23363 pointers. */
23364class c_str_view
9291a0cd 23365{
bc8f2430
JK
23366public:
23367 c_str_view (const char *cstr)
23368 : m_cstr (cstr)
23369 {}
9291a0cd 23370
bc8f2430
JK
23371 bool operator== (const c_str_view &other) const
23372 {
23373 return strcmp (m_cstr, other.m_cstr) == 0;
23374 }
9291a0cd 23375
bc8f2430
JK
23376private:
23377 friend class c_str_view_hasher;
23378 const char *const m_cstr;
23379};
9291a0cd 23380
bc8f2430
JK
23381/* A std::unordered_map::hasher for c_str_view that uses the right
23382 hash function for strings in a mapped index. */
23383class c_str_view_hasher
23384{
23385public:
23386 size_t operator () (const c_str_view &x) const
23387 {
23388 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23389 }
23390};
b89be57b 23391
bc8f2430
JK
23392/* A std::unordered_map::hasher for std::vector<>. */
23393template<typename T>
23394class vector_hasher
9291a0cd 23395{
bc8f2430
JK
23396public:
23397 size_t operator () (const std::vector<T> &key) const
23398 {
23399 return iterative_hash (key.data (),
23400 sizeof (key.front ()) * key.size (), 0);
23401 }
23402};
9291a0cd 23403
bc8f2430
JK
23404/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23405 constant pool entries going into the data buffer CPOOL. */
3876f04e 23406
bc8f2430
JK
23407static void
23408write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23409{
23410 {
23411 /* Elements are sorted vectors of the indices of all the CUs that
23412 hold an object of this name. */
23413 std::unordered_map<std::vector<offset_type>, offset_type,
23414 vector_hasher<offset_type>>
23415 symbol_hash_table;
23416
23417 /* We add all the index vectors to the constant pool first, to
23418 ensure alignment is ok. */
4b76cda9 23419 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23420 {
4b76cda9 23421 if (entry.name == NULL)
bc8f2430 23422 continue;
4b76cda9 23423 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23424
23425 /* Finding before inserting is faster than always trying to
23426 insert, because inserting always allocates a node, does the
23427 lookup, and then destroys the new node if another node
23428 already had the same key. C++17 try_emplace will avoid
23429 this. */
23430 const auto found
4b76cda9 23431 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23432 if (found != symbol_hash_table.end ())
23433 {
4b76cda9 23434 entry.index_offset = found->second;
70a1152b
PA
23435 continue;
23436 }
23437
4b76cda9
PA
23438 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23439 entry.index_offset = cpool.size ();
23440 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23441 for (const auto index : entry.cu_indices)
23442 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23443 }
23444 }
9291a0cd
TT
23445
23446 /* Now write out the hash table. */
bc8f2430 23447 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23448 for (const auto &entry : symtab->data)
9291a0cd
TT
23449 {
23450 offset_type str_off, vec_off;
23451
4b76cda9 23452 if (entry.name != NULL)
9291a0cd 23453 {
4b76cda9 23454 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23455 if (insertpair.second)
4b76cda9 23456 cpool.append_cstr0 (entry.name);
bc8f2430 23457 str_off = insertpair.first->second;
4b76cda9 23458 vec_off = entry.index_offset;
9291a0cd
TT
23459 }
23460 else
23461 {
23462 /* While 0 is a valid constant pool index, it is not valid
23463 to have 0 for both offsets. */
23464 str_off = 0;
23465 vec_off = 0;
23466 }
23467
bc8f2430
JK
23468 output.append_data (MAYBE_SWAP (str_off));
23469 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23470 }
9291a0cd
TT
23471}
23472
bc8f2430 23473typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23474
23475/* Helper struct for building the address table. */
23476struct addrmap_index_data
23477{
bc8f2430
JK
23478 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23479 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23480 {}
23481
0a5429f6 23482 struct objfile *objfile;
bc8f2430
JK
23483 data_buf &addr_vec;
23484 psym_index_map &cu_index_htab;
0a5429f6
DE
23485
23486 /* Non-zero if the previous_* fields are valid.
23487 We can't write an entry until we see the next entry (since it is only then
23488 that we know the end of the entry). */
23489 int previous_valid;
23490 /* Index of the CU in the table of all CUs in the index file. */
23491 unsigned int previous_cu_index;
0963b4bd 23492 /* Start address of the CU. */
0a5429f6
DE
23493 CORE_ADDR previous_cu_start;
23494};
23495
bc8f2430 23496/* Write an address entry to ADDR_VEC. */
b89be57b 23497
9291a0cd 23498static void
bc8f2430 23499add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23500 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23501{
9291a0cd
TT
23502 CORE_ADDR baseaddr;
23503
23504 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23505
c2f134ac
PA
23506 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23507 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23508 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23509}
23510
23511/* Worker function for traversing an addrmap to build the address table. */
23512
23513static int
23514add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23515{
9a3c8263
SM
23516 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23517 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23518
23519 if (data->previous_valid)
bc8f2430 23520 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23521 data->previous_cu_start, start_addr,
23522 data->previous_cu_index);
23523
23524 data->previous_cu_start = start_addr;
23525 if (pst != NULL)
23526 {
bc8f2430
JK
23527 const auto it = data->cu_index_htab.find (pst);
23528 gdb_assert (it != data->cu_index_htab.cend ());
23529 data->previous_cu_index = it->second;
0a5429f6
DE
23530 data->previous_valid = 1;
23531 }
23532 else
bc8f2430 23533 data->previous_valid = 0;
0a5429f6
DE
23534
23535 return 0;
23536}
23537
bc8f2430 23538/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23539 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23540 in the index file. */
23541
23542static void
bc8f2430
JK
23543write_address_map (struct objfile *objfile, data_buf &addr_vec,
23544 psym_index_map &cu_index_htab)
0a5429f6 23545{
bc8f2430 23546 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23547
23548 /* When writing the address table, we have to cope with the fact that
23549 the addrmap iterator only provides the start of a region; we have to
23550 wait until the next invocation to get the start of the next region. */
23551
23552 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23553 addrmap_index_data.previous_valid = 0;
23554
23555 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23556 &addrmap_index_data);
23557
23558 /* It's highly unlikely the last entry (end address = 0xff...ff)
23559 is valid, but we should still handle it.
23560 The end address is recorded as the start of the next region, but that
23561 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23562 anyway. */
23563 if (addrmap_index_data.previous_valid)
bc8f2430 23564 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23565 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23566 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23567}
23568
156942c7
DE
23569/* Return the symbol kind of PSYM. */
23570
23571static gdb_index_symbol_kind
23572symbol_kind (struct partial_symbol *psym)
23573{
23574 domain_enum domain = PSYMBOL_DOMAIN (psym);
23575 enum address_class aclass = PSYMBOL_CLASS (psym);
23576
23577 switch (domain)
23578 {
23579 case VAR_DOMAIN:
23580 switch (aclass)
23581 {
23582 case LOC_BLOCK:
23583 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23584 case LOC_TYPEDEF:
23585 return GDB_INDEX_SYMBOL_KIND_TYPE;
23586 case LOC_COMPUTED:
23587 case LOC_CONST_BYTES:
23588 case LOC_OPTIMIZED_OUT:
23589 case LOC_STATIC:
23590 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23591 case LOC_CONST:
23592 /* Note: It's currently impossible to recognize psyms as enum values
23593 short of reading the type info. For now punt. */
23594 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23595 default:
23596 /* There are other LOC_FOO values that one might want to classify
23597 as variables, but dwarf2read.c doesn't currently use them. */
23598 return GDB_INDEX_SYMBOL_KIND_OTHER;
23599 }
23600 case STRUCT_DOMAIN:
23601 return GDB_INDEX_SYMBOL_KIND_TYPE;
23602 default:
23603 return GDB_INDEX_SYMBOL_KIND_OTHER;
23604 }
23605}
23606
9291a0cd 23607/* Add a list of partial symbols to SYMTAB. */
b89be57b 23608
9291a0cd
TT
23609static void
23610write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23611 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23612 struct partial_symbol **psymp,
23613 int count,
987d643c
TT
23614 offset_type cu_index,
23615 int is_static)
9291a0cd
TT
23616{
23617 for (; count-- > 0; ++psymp)
23618 {
156942c7 23619 struct partial_symbol *psym = *psymp;
987d643c 23620
156942c7 23621 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23622 error (_("Ada is not currently supported by the index"));
987d643c 23623
987d643c 23624 /* Only add a given psymbol once. */
bc8f2430 23625 if (psyms_seen.insert (psym).second)
987d643c 23626 {
156942c7
DE
23627 gdb_index_symbol_kind kind = symbol_kind (psym);
23628
156942c7
DE
23629 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23630 is_static, kind, cu_index);
987d643c 23631 }
9291a0cd
TT
23632 }
23633}
23634
1fd400ff
TT
23635/* A helper struct used when iterating over debug_types. */
23636struct signatured_type_index_data
23637{
bc8f2430
JK
23638 signatured_type_index_data (data_buf &types_list_,
23639 std::unordered_set<partial_symbol *> &psyms_seen_)
23640 : types_list (types_list_), psyms_seen (psyms_seen_)
23641 {}
23642
1fd400ff
TT
23643 struct objfile *objfile;
23644 struct mapped_symtab *symtab;
bc8f2430
JK
23645 data_buf &types_list;
23646 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23647 int cu_index;
23648};
23649
23650/* A helper function that writes a single signatured_type to an
23651 obstack. */
b89be57b 23652
1fd400ff
TT
23653static int
23654write_one_signatured_type (void **slot, void *d)
23655{
9a3c8263
SM
23656 struct signatured_type_index_data *info
23657 = (struct signatured_type_index_data *) d;
1fd400ff 23658 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23659 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23660
23661 write_psymbols (info->symtab,
987d643c 23662 info->psyms_seen,
3e43a32a
MS
23663 info->objfile->global_psymbols.list
23664 + psymtab->globals_offset,
987d643c
TT
23665 psymtab->n_global_syms, info->cu_index,
23666 0);
1fd400ff 23667 write_psymbols (info->symtab,
987d643c 23668 info->psyms_seen,
3e43a32a
MS
23669 info->objfile->static_psymbols.list
23670 + psymtab->statics_offset,
987d643c
TT
23671 psymtab->n_static_syms, info->cu_index,
23672 1);
1fd400ff 23673
c2f134ac
PA
23674 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23675 to_underlying (entry->per_cu.sect_off));
23676 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23677 to_underlying (entry->type_offset_in_tu));
23678 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23679
23680 ++info->cu_index;
23681
23682 return 1;
23683}
23684
e8f8bcb3
PA
23685/* Recurse into all "included" dependencies and count their symbols as
23686 if they appeared in this psymtab. */
23687
23688static void
23689recursively_count_psymbols (struct partial_symtab *psymtab,
23690 size_t &psyms_seen)
23691{
23692 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23693 if (psymtab->dependencies[i]->user != NULL)
23694 recursively_count_psymbols (psymtab->dependencies[i],
23695 psyms_seen);
23696
23697 psyms_seen += psymtab->n_global_syms;
23698 psyms_seen += psymtab->n_static_syms;
23699}
23700
95554aad
TT
23701/* Recurse into all "included" dependencies and write their symbols as
23702 if they appeared in this psymtab. */
23703
23704static void
23705recursively_write_psymbols (struct objfile *objfile,
23706 struct partial_symtab *psymtab,
23707 struct mapped_symtab *symtab,
bc8f2430 23708 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23709 offset_type cu_index)
23710{
23711 int i;
23712
23713 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23714 if (psymtab->dependencies[i]->user != NULL)
23715 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23716 symtab, psyms_seen, cu_index);
23717
23718 write_psymbols (symtab,
23719 psyms_seen,
23720 objfile->global_psymbols.list + psymtab->globals_offset,
23721 psymtab->n_global_syms, cu_index,
23722 0);
23723 write_psymbols (symtab,
23724 psyms_seen,
23725 objfile->static_psymbols.list + psymtab->statics_offset,
23726 psymtab->n_static_syms, cu_index,
23727 1);
23728}
23729
bc8f2430
JK
23730/* Closes FILE on scope exit. */
23731struct file_closer
23732{
23733 explicit file_closer (FILE *file)
23734 : m_file (file)
23735 {}
23736
23737 ~file_closer ()
23738 { fclose (m_file); }
23739
23740private:
23741 FILE *m_file;
23742};
23743
9291a0cd 23744/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23745
9291a0cd
TT
23746static void
23747write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23748{
9291a0cd
TT
23749 if (dwarf2_per_objfile->using_index)
23750 error (_("Cannot use an index to create the index"));
23751
8b70b953
TT
23752 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23753 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23754
260b681b
DE
23755 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23756 return;
23757
bc8f2430 23758 struct stat st;
4262abfb
JK
23759 if (stat (objfile_name (objfile), &st) < 0)
23760 perror_with_name (objfile_name (objfile));
9291a0cd 23761
bc8f2430
JK
23762 std::string filename (std::string (dir) + SLASH_STRING
23763 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23764
bc8f2430 23765 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb");
9291a0cd 23766 if (!out_file)
bc8f2430 23767 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23768
16b7a719
PA
23769 /* Order matters here; we want FILE to be closed before FILENAME is
23770 unlinked, because on MS-Windows one cannot delete a file that is
23771 still open. (Don't call anything here that might throw until
23772 file_closer is created.) */
bc8f2430 23773 gdb::unlinker unlink_file (filename.c_str ());
16b7a719 23774 file_closer close_out_file (out_file);
9291a0cd 23775
bc8f2430
JK
23776 mapped_symtab symtab;
23777 data_buf cu_list;
987d643c 23778
0a5429f6
DE
23779 /* While we're scanning CU's create a table that maps a psymtab pointer
23780 (which is what addrmap records) to its index (which is what is recorded
23781 in the index file). This will later be needed to write the address
23782 table. */
bc8f2430
JK
23783 psym_index_map cu_index_htab;
23784 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23785
23786 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23787 work here. Also, the debug_types entries do not appear in
23788 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23789
23790 /* The psyms_seen set is potentially going to be largish (~40k
23791 elements when indexing a -g3 build of GDB itself). Estimate the
23792 number of elements in order to avoid too many rehashes, which
23793 require rebuilding buckets and thus many trips to
23794 malloc/free. */
23795 size_t psyms_count = 0;
23796 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23797 {
23798 struct dwarf2_per_cu_data *per_cu
23799 = dwarf2_per_objfile->all_comp_units[i];
23800 struct partial_symtab *psymtab = per_cu->v.psymtab;
23801
23802 if (psymtab != NULL && psymtab->user == NULL)
23803 recursively_count_psymbols (psymtab, psyms_count);
23804 }
23805 /* Generating an index for gdb itself shows a ratio of
23806 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23807 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23808 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23809 {
3e43a32a
MS
23810 struct dwarf2_per_cu_data *per_cu
23811 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23812 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23813
92fac807
JK
23814 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23815 It may be referenced from a local scope but in such case it does not
23816 need to be present in .gdb_index. */
23817 if (psymtab == NULL)
23818 continue;
23819
95554aad 23820 if (psymtab->user == NULL)
bc8f2430
JK
23821 recursively_write_psymbols (objfile, psymtab, &symtab,
23822 psyms_seen, i);
9291a0cd 23823
bc8f2430
JK
23824 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23825 gdb_assert (insertpair.second);
9291a0cd 23826
c2f134ac
PA
23827 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23828 to_underlying (per_cu->sect_off));
23829 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23830 }
23831
0a5429f6 23832 /* Dump the address map. */
bc8f2430
JK
23833 data_buf addr_vec;
23834 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23835
1fd400ff 23836 /* Write out the .debug_type entries, if any. */
bc8f2430 23837 data_buf types_cu_list;
1fd400ff
TT
23838 if (dwarf2_per_objfile->signatured_types)
23839 {
bc8f2430
JK
23840 signatured_type_index_data sig_data (types_cu_list,
23841 psyms_seen);
1fd400ff
TT
23842
23843 sig_data.objfile = objfile;
bc8f2430 23844 sig_data.symtab = &symtab;
1fd400ff
TT
23845 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23846 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23847 write_one_signatured_type, &sig_data);
23848 }
23849
156942c7
DE
23850 /* Now that we've processed all symbols we can shrink their cu_indices
23851 lists. */
bc8f2430 23852 uniquify_cu_indices (&symtab);
156942c7 23853
bc8f2430
JK
23854 data_buf symtab_vec, constant_pool;
23855 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23856
bc8f2430
JK
23857 data_buf contents;
23858 const offset_type size_of_contents = 6 * sizeof (offset_type);
23859 offset_type total_len = size_of_contents;
9291a0cd
TT
23860
23861 /* The version number. */
bc8f2430 23862 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
23863
23864 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
23865 contents.append_data (MAYBE_SWAP (total_len));
23866 total_len += cu_list.size ();
9291a0cd 23867
1fd400ff 23868 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
23869 contents.append_data (MAYBE_SWAP (total_len));
23870 total_len += types_cu_list.size ();
1fd400ff 23871
9291a0cd 23872 /* The offset of the address table from the start of the file. */
bc8f2430
JK
23873 contents.append_data (MAYBE_SWAP (total_len));
23874 total_len += addr_vec.size ();
9291a0cd
TT
23875
23876 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
23877 contents.append_data (MAYBE_SWAP (total_len));
23878 total_len += symtab_vec.size ();
9291a0cd
TT
23879
23880 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
23881 contents.append_data (MAYBE_SWAP (total_len));
23882 total_len += constant_pool.size ();
9291a0cd 23883
bc8f2430 23884 gdb_assert (contents.size () == size_of_contents);
9291a0cd 23885
bc8f2430
JK
23886 contents.file_write (out_file);
23887 cu_list.file_write (out_file);
23888 types_cu_list.file_write (out_file);
23889 addr_vec.file_write (out_file);
23890 symtab_vec.file_write (out_file);
23891 constant_pool.file_write (out_file);
9291a0cd 23892
bef155c3
TT
23893 /* We want to keep the file. */
23894 unlink_file.keep ();
9291a0cd
TT
23895}
23896
90476074
TT
23897/* Implementation of the `save gdb-index' command.
23898
23899 Note that the file format used by this command is documented in the
23900 GDB manual. Any changes here must be documented there. */
11570e71 23901
9291a0cd
TT
23902static void
23903save_gdb_index_command (char *arg, int from_tty)
23904{
23905 struct objfile *objfile;
23906
23907 if (!arg || !*arg)
96d19272 23908 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23909
23910 ALL_OBJFILES (objfile)
23911 {
23912 struct stat st;
23913
23914 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23915 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23916 continue;
23917
9a3c8263
SM
23918 dwarf2_per_objfile
23919 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23920 dwarf2_objfile_data_key);
9291a0cd
TT
23921 if (dwarf2_per_objfile)
23922 {
9291a0cd 23923
492d29ea 23924 TRY
9291a0cd
TT
23925 {
23926 write_psymtabs_to_index (objfile, arg);
23927 }
492d29ea
PA
23928 CATCH (except, RETURN_MASK_ERROR)
23929 {
23930 exception_fprintf (gdb_stderr, except,
23931 _("Error while writing index for `%s': "),
23932 objfile_name (objfile));
23933 }
23934 END_CATCH
9291a0cd
TT
23935 }
23936 }
dce234bc
PP
23937}
23938
9291a0cd
TT
23939\f
23940
b4f54984 23941int dwarf_always_disassemble;
9eae7c52
TT
23942
23943static void
b4f54984
DE
23944show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23945 struct cmd_list_element *c, const char *value)
9eae7c52 23946{
3e43a32a
MS
23947 fprintf_filtered (file,
23948 _("Whether to always disassemble "
23949 "DWARF expressions is %s.\n"),
9eae7c52
TT
23950 value);
23951}
23952
900e11f9
JK
23953static void
23954show_check_physname (struct ui_file *file, int from_tty,
23955 struct cmd_list_element *c, const char *value)
23956{
23957 fprintf_filtered (file,
23958 _("Whether to check \"physname\" is %s.\n"),
23959 value);
23960}
23961
6502dd73
DJ
23962void _initialize_dwarf2_read (void);
23963
23964void
23965_initialize_dwarf2_read (void)
23966{
96d19272
JK
23967 struct cmd_list_element *c;
23968
dce234bc 23969 dwarf2_objfile_data_key
c1bd65d0 23970 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23971
b4f54984
DE
23972 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23973Set DWARF specific variables.\n\
23974Configure DWARF variables such as the cache size"),
23975 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
23976 0/*allow-unknown*/, &maintenance_set_cmdlist);
23977
b4f54984
DE
23978 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23979Show DWARF specific variables\n\
23980Show DWARF variables such as the cache size"),
23981 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
23982 0/*allow-unknown*/, &maintenance_show_cmdlist);
23983
23984 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
23985 &dwarf_max_cache_age, _("\
23986Set the upper bound on the age of cached DWARF compilation units."), _("\
23987Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
23988A higher limit means that cached compilation units will be stored\n\
23989in memory longer, and more total memory will be used. Zero disables\n\
23990caching, which can slow down startup."),
2c5b56ce 23991 NULL,
b4f54984
DE
23992 show_dwarf_max_cache_age,
23993 &set_dwarf_cmdlist,
23994 &show_dwarf_cmdlist);
d97bc12b 23995
9eae7c52 23996 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 23997 &dwarf_always_disassemble, _("\
9eae7c52
TT
23998Set whether `info address' always disassembles DWARF expressions."), _("\
23999Show whether `info address' always disassembles DWARF expressions."), _("\
24000When enabled, DWARF expressions are always printed in an assembly-like\n\
24001syntax. When disabled, expressions will be printed in a more\n\
24002conversational style, when possible."),
24003 NULL,
b4f54984
DE
24004 show_dwarf_always_disassemble,
24005 &set_dwarf_cmdlist,
24006 &show_dwarf_cmdlist);
24007
24008 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24009Set debugging of the DWARF reader."), _("\
24010Show debugging of the DWARF reader."), _("\
24011When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24012reading and symtab expansion. A value of 1 (one) provides basic\n\
24013information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24014 NULL,
24015 NULL,
24016 &setdebuglist, &showdebuglist);
24017
b4f54984
DE
24018 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24019Set debugging of the DWARF DIE reader."), _("\
24020Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24021When enabled (non-zero), DIEs are dumped after they are read in.\n\
24022The value is the maximum depth to print."),
ccce17b0
YQ
24023 NULL,
24024 NULL,
24025 &setdebuglist, &showdebuglist);
9291a0cd 24026
27e0867f
DE
24027 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24028Set debugging of the dwarf line reader."), _("\
24029Show debugging of the dwarf line reader."), _("\
24030When enabled (non-zero), line number entries are dumped as they are read in.\n\
24031A value of 1 (one) provides basic information.\n\
24032A value greater than 1 provides more verbose information."),
24033 NULL,
24034 NULL,
24035 &setdebuglist, &showdebuglist);
24036
900e11f9
JK
24037 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24038Set cross-checking of \"physname\" code against demangler."), _("\
24039Show cross-checking of \"physname\" code against demangler."), _("\
24040When enabled, GDB's internal \"physname\" code is checked against\n\
24041the demangler."),
24042 NULL, show_check_physname,
24043 &setdebuglist, &showdebuglist);
24044
e615022a
DE
24045 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24046 no_class, &use_deprecated_index_sections, _("\
24047Set whether to use deprecated gdb_index sections."), _("\
24048Show whether to use deprecated gdb_index sections."), _("\
24049When enabled, deprecated .gdb_index sections are used anyway.\n\
24050Normally they are ignored either because of a missing feature or\n\
24051performance issue.\n\
24052Warning: This option must be enabled before gdb reads the file."),
24053 NULL,
24054 NULL,
24055 &setlist, &showlist);
24056
96d19272 24057 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24058 _("\
fc1a9d6e 24059Save a gdb-index file.\n\
11570e71 24060Usage: save gdb-index DIRECTORY"),
96d19272
JK
24061 &save_cmdlist);
24062 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24063
24064 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24065 &dwarf2_locexpr_funcs);
24066 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24067 &dwarf2_loclist_funcs);
24068
24069 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24070 &dwarf2_block_frame_base_locexpr_funcs);
24071 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24072 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24073}
This page took 4.47042 seconds and 4 git commands to generate.